xref: /titanic_52/usr/src/uts/common/inet/ip/ip_if.c (revision fcf3ce441efd61da9bb2884968af01cb7c1452cc)
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 	hook_nic_event_int_t *info;
4313 	ip_stack_t	*ipst;
4314 
4315 	if (ill == NULL)
4316 		return;
4317 	ipst = ill->ill_ipst;
4318 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4319 
4320 	/*
4321 	 * If the ill was never inserted into the AVL tree
4322 	 * we skip the if branch.
4323 	 */
4324 	if (ill->ill_ifptr != NULL) {
4325 		/*
4326 		 * remove from AVL tree and free ppa number
4327 		 */
4328 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4329 
4330 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4331 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4332 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4333 		}
4334 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4335 			ill_delete_interface_type(ill->ill_ifptr);
4336 		}
4337 
4338 		/*
4339 		 * Indicate ill is no longer in the list.
4340 		 */
4341 		ill->ill_ifptr = NULL;
4342 		ill->ill_name_length = 0;
4343 		ill->ill_name[0] = '\0';
4344 		ill->ill_ppa = UINT_MAX;
4345 	}
4346 
4347 	/*
4348 	 * Run the unplumb hook after the NIC has disappeared from being
4349 	 * visible so that attempts to revalidate its existance will fail.
4350 	 *
4351 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4352 	 * that the ordering of delivered events to listeners matches the
4353 	 * order of them in the kernel.
4354 	 */
4355 	info = ill->ill_nic_event_info;
4356 	if (info != NULL && info->hnei_event.hne_event == NE_DOWN) {
4357 		mutex_enter(&ill->ill_lock);
4358 		ill_nic_info_dispatch(ill);
4359 		mutex_exit(&ill->ill_lock);
4360 	}
4361 
4362 	/* Generate NE_UNPLUMB event for ill_name. */
4363 	(void) ill_hook_event_create(ill, 0, NE_UNPLUMB, ill->ill_name,
4364 	    ill->ill_name_length);
4365 
4366 	ill_phyint_free(ill);
4367 	rw_exit(&ipst->ips_ill_g_lock);
4368 }
4369 
4370 /*
4371  * allocate a ppa, if the number of plumbed interfaces of this type are
4372  * less than ill_no_arena do a linear search to find a unused ppa.
4373  * When the number goes beyond ill_no_arena switch to using an arena.
4374  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4375  * is the return value for an error condition, so allocation starts at one
4376  * and is decremented by one.
4377  */
4378 static int
4379 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4380 {
4381 	ill_t *tmp_ill;
4382 	uint_t start, end;
4383 	int ppa;
4384 
4385 	if (ifp->illif_ppa_arena == NULL &&
4386 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4387 		/*
4388 		 * Create an arena.
4389 		 */
4390 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4391 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4392 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4393 			/* allocate what has already been assigned */
4394 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4395 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4396 		    tmp_ill, AVL_AFTER)) {
4397 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4398 			    1,		/* size */
4399 			    1,		/* align/quantum */
4400 			    0,		/* phase */
4401 			    0,		/* nocross */
4402 			    /* minaddr */
4403 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4404 			    /* maxaddr */
4405 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4406 			    VM_NOSLEEP|VM_FIRSTFIT);
4407 			if (ppa == 0) {
4408 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4409 				    " failed while switching"));
4410 				vmem_destroy(ifp->illif_ppa_arena);
4411 				ifp->illif_ppa_arena = NULL;
4412 				break;
4413 			}
4414 		}
4415 	}
4416 
4417 	if (ifp->illif_ppa_arena != NULL) {
4418 		if (ill->ill_ppa == UINT_MAX) {
4419 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4420 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4421 			if (ppa == 0)
4422 				return (EAGAIN);
4423 			ill->ill_ppa = --ppa;
4424 		} else {
4425 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4426 			    1, 		/* size */
4427 			    1, 		/* align/quantum */
4428 			    0, 		/* phase */
4429 			    0, 		/* nocross */
4430 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4431 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4432 			    VM_NOSLEEP|VM_FIRSTFIT);
4433 			/*
4434 			 * Most likely the allocation failed because
4435 			 * the requested ppa was in use.
4436 			 */
4437 			if (ppa == 0)
4438 				return (EEXIST);
4439 		}
4440 		return (0);
4441 	}
4442 
4443 	/*
4444 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4445 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4446 	 */
4447 	if (ill->ill_ppa == UINT_MAX) {
4448 		end = UINT_MAX - 1;
4449 		start = 0;
4450 	} else {
4451 		end = start = ill->ill_ppa;
4452 	}
4453 
4454 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4455 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4456 		if (start++ >= end) {
4457 			if (ill->ill_ppa == UINT_MAX)
4458 				return (EAGAIN);
4459 			else
4460 				return (EEXIST);
4461 		}
4462 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4463 	}
4464 	ill->ill_ppa = start;
4465 	return (0);
4466 }
4467 
4468 /*
4469  * Insert ill into the list of configured ill's. Once this function completes,
4470  * the ill is globally visible and is available through lookups. More precisely
4471  * this happens after the caller drops the ill_g_lock.
4472  */
4473 static int
4474 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4475 {
4476 	ill_if_t *ill_interface;
4477 	avl_index_t where = 0;
4478 	int error;
4479 	int name_length;
4480 	int index;
4481 	boolean_t check_length = B_FALSE;
4482 	ip_stack_t	*ipst = ill->ill_ipst;
4483 
4484 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4485 
4486 	name_length = mi_strlen(name) + 1;
4487 
4488 	if (isv6)
4489 		index = IP_V6_G_HEAD;
4490 	else
4491 		index = IP_V4_G_HEAD;
4492 
4493 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4494 	/*
4495 	 * Search for interface type based on name
4496 	 */
4497 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4498 		if ((ill_interface->illif_name_len == name_length) &&
4499 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4500 			break;
4501 		}
4502 		ill_interface = ill_interface->illif_next;
4503 	}
4504 
4505 	/*
4506 	 * Interface type not found, create one.
4507 	 */
4508 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4509 
4510 		ill_g_head_t ghead;
4511 
4512 		/*
4513 		 * allocate ill_if_t structure
4514 		 */
4515 
4516 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4517 		if (ill_interface == NULL) {
4518 			return (ENOMEM);
4519 		}
4520 
4521 
4522 
4523 		(void) strcpy(ill_interface->illif_name, name);
4524 		ill_interface->illif_name_len = name_length;
4525 
4526 		avl_create(&ill_interface->illif_avl_by_ppa,
4527 		    ill_compare_ppa, sizeof (ill_t),
4528 		    offsetof(struct ill_s, ill_avl_byppa));
4529 
4530 		/*
4531 		 * link the structure in the back to maintain order
4532 		 * of configuration for ifconfig output.
4533 		 */
4534 		ghead = ipst->ips_ill_g_heads[index];
4535 		insque(ill_interface, ghead.ill_g_list_tail);
4536 
4537 	}
4538 
4539 	if (ill->ill_ppa == UINT_MAX)
4540 		check_length = B_TRUE;
4541 
4542 	error = ill_alloc_ppa(ill_interface, ill);
4543 	if (error != 0) {
4544 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4545 			ill_delete_interface_type(ill->ill_ifptr);
4546 		return (error);
4547 	}
4548 
4549 	/*
4550 	 * When the ppa is choosen by the system, check that there is
4551 	 * enough space to insert ppa. if a specific ppa was passed in this
4552 	 * check is not required as the interface name passed in will have
4553 	 * the right ppa in it.
4554 	 */
4555 	if (check_length) {
4556 		/*
4557 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4558 		 */
4559 		char buf[sizeof (uint_t) * 3];
4560 
4561 		/*
4562 		 * convert ppa to string to calculate the amount of space
4563 		 * required for it in the name.
4564 		 */
4565 		numtos(ill->ill_ppa, buf);
4566 
4567 		/* Do we have enough space to insert ppa ? */
4568 
4569 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4570 			/* Free ppa and interface type struct */
4571 			if (ill_interface->illif_ppa_arena != NULL) {
4572 				vmem_free(ill_interface->illif_ppa_arena,
4573 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4574 			}
4575 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4576 				ill_delete_interface_type(ill->ill_ifptr);
4577 
4578 			return (EINVAL);
4579 		}
4580 	}
4581 
4582 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4583 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4584 
4585 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4586 	    &where);
4587 	ill->ill_ifptr = ill_interface;
4588 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4589 
4590 	ill_phyint_reinit(ill);
4591 	return (0);
4592 }
4593 
4594 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4595 static boolean_t
4596 ipsq_init(ill_t *ill)
4597 {
4598 	ipsq_t  *ipsq;
4599 
4600 	/* Init the ipsq and impicitly enter as writer */
4601 	ill->ill_phyint->phyint_ipsq =
4602 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4603 	if (ill->ill_phyint->phyint_ipsq == NULL)
4604 		return (B_FALSE);
4605 	ipsq = ill->ill_phyint->phyint_ipsq;
4606 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4607 	ill->ill_phyint->phyint_ipsq_next = NULL;
4608 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4609 	ipsq->ipsq_refs = 1;
4610 	ipsq->ipsq_writer = curthread;
4611 	ipsq->ipsq_reentry_cnt = 1;
4612 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4613 #ifdef DEBUG
4614 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack,
4615 	    IPSQ_STACK_DEPTH);
4616 #endif
4617 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4618 	return (B_TRUE);
4619 }
4620 
4621 /*
4622  * ill_init is called by ip_open when a device control stream is opened.
4623  * It does a few initializations, and shoots a DL_INFO_REQ message down
4624  * to the driver.  The response is later picked up in ip_rput_dlpi and
4625  * used to set up default mechanisms for talking to the driver.  (Always
4626  * called as writer.)
4627  *
4628  * If this function returns error, ip_open will call ip_close which in
4629  * turn will call ill_delete to clean up any memory allocated here that
4630  * is not yet freed.
4631  */
4632 int
4633 ill_init(queue_t *q, ill_t *ill)
4634 {
4635 	int	count;
4636 	dl_info_req_t	*dlir;
4637 	mblk_t	*info_mp;
4638 	uchar_t *frag_ptr;
4639 
4640 	/*
4641 	 * The ill is initialized to zero by mi_alloc*(). In addition
4642 	 * some fields already contain valid values, initialized in
4643 	 * ip_open(), before we reach here.
4644 	 */
4645 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4646 
4647 	ill->ill_rq = q;
4648 	ill->ill_wq = WR(q);
4649 
4650 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4651 	    BPRI_HI);
4652 	if (info_mp == NULL)
4653 		return (ENOMEM);
4654 
4655 	/*
4656 	 * Allocate sufficient space to contain our fragment hash table and
4657 	 * the device name.
4658 	 */
4659 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4660 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4661 	if (frag_ptr == NULL) {
4662 		freemsg(info_mp);
4663 		return (ENOMEM);
4664 	}
4665 	ill->ill_frag_ptr = frag_ptr;
4666 	ill->ill_frag_free_num_pkts = 0;
4667 	ill->ill_last_frag_clean_time = 0;
4668 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4669 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4670 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4671 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4672 		    NULL, MUTEX_DEFAULT, NULL);
4673 	}
4674 
4675 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4676 	if (ill->ill_phyint == NULL) {
4677 		freemsg(info_mp);
4678 		mi_free(frag_ptr);
4679 		return (ENOMEM);
4680 	}
4681 
4682 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4683 	/*
4684 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4685 	 * at this point because of the following reason. If we can't
4686 	 * enter the ipsq at some point and cv_wait, the writer that
4687 	 * wakes us up tries to locate us using the list of all phyints
4688 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4689 	 * If we don't set it now, we risk a missed wakeup.
4690 	 */
4691 	ill->ill_phyint->phyint_illv4 = ill;
4692 	ill->ill_ppa = UINT_MAX;
4693 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4694 
4695 	if (!ipsq_init(ill)) {
4696 		freemsg(info_mp);
4697 		mi_free(frag_ptr);
4698 		mi_free(ill->ill_phyint);
4699 		return (ENOMEM);
4700 	}
4701 
4702 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4703 
4704 	/* Frag queue limit stuff */
4705 	ill->ill_frag_count = 0;
4706 	ill->ill_ipf_gen = 0;
4707 
4708 	ill->ill_global_timer = INFINITY;
4709 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4710 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4711 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4712 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4713 
4714 	/*
4715 	 * Initialize IPv6 configuration variables.  The IP module is always
4716 	 * opened as an IPv4 module.  Instead tracking down the cases where
4717 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4718 	 * here for convenience, this has no effect until the ill is set to do
4719 	 * IPv6.
4720 	 */
4721 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4722 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4723 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4724 	ill->ill_max_buf = ND_MAX_Q;
4725 	ill->ill_refcnt = 0;
4726 
4727 	/* Send down the Info Request to the driver. */
4728 	info_mp->b_datap->db_type = M_PCPROTO;
4729 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4730 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4731 	dlir->dl_primitive = DL_INFO_REQ;
4732 
4733 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4734 
4735 	qprocson(q);
4736 	ill_dlpi_send(ill, info_mp);
4737 
4738 	return (0);
4739 }
4740 
4741 /*
4742  * ill_dls_info
4743  * creates datalink socket info from the device.
4744  */
4745 int
4746 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4747 {
4748 	size_t	len;
4749 	ill_t	*ill = ipif->ipif_ill;
4750 
4751 	sdl->sdl_family = AF_LINK;
4752 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4753 	sdl->sdl_type = ill->ill_type;
4754 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4755 	len = strlen(sdl->sdl_data);
4756 	ASSERT(len < 256);
4757 	sdl->sdl_nlen = (uchar_t)len;
4758 	sdl->sdl_alen = ill->ill_phys_addr_length;
4759 	sdl->sdl_slen = 0;
4760 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4761 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4762 
4763 	return (sizeof (struct sockaddr_dl));
4764 }
4765 
4766 /*
4767  * ill_xarp_info
4768  * creates xarp info from the device.
4769  */
4770 static int
4771 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4772 {
4773 	sdl->sdl_family = AF_LINK;
4774 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4775 	sdl->sdl_type = ill->ill_type;
4776 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4777 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4778 	sdl->sdl_alen = ill->ill_phys_addr_length;
4779 	sdl->sdl_slen = 0;
4780 	return (sdl->sdl_nlen);
4781 }
4782 
4783 static int
4784 loopback_kstat_update(kstat_t *ksp, int rw)
4785 {
4786 	kstat_named_t *kn;
4787 	netstackid_t	stackid;
4788 	netstack_t	*ns;
4789 	ip_stack_t	*ipst;
4790 
4791 	if (ksp == NULL || ksp->ks_data == NULL)
4792 		return (EIO);
4793 
4794 	if (rw == KSTAT_WRITE)
4795 		return (EACCES);
4796 
4797 	kn = KSTAT_NAMED_PTR(ksp);
4798 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4799 
4800 	ns = netstack_find_by_stackid(stackid);
4801 	if (ns == NULL)
4802 		return (-1);
4803 
4804 	ipst = ns->netstack_ip;
4805 	if (ipst == NULL) {
4806 		netstack_rele(ns);
4807 		return (-1);
4808 	}
4809 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4810 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4811 	netstack_rele(ns);
4812 	return (0);
4813 }
4814 
4815 /*
4816  * Has ifindex been plumbed already.
4817  * Compares both phyint_ifindex and phyint_group_ifindex.
4818  */
4819 static boolean_t
4820 phyint_exists(uint_t index, ip_stack_t *ipst)
4821 {
4822 	phyint_t *phyi;
4823 
4824 	ASSERT(index != 0);
4825 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4826 	/*
4827 	 * Indexes are stored in the phyint - a common structure
4828 	 * to both IPv4 and IPv6.
4829 	 */
4830 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4831 	for (; phyi != NULL;
4832 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4833 	    phyi, AVL_AFTER)) {
4834 		if (phyi->phyint_ifindex == index ||
4835 		    phyi->phyint_group_ifindex == index)
4836 			return (B_TRUE);
4837 	}
4838 	return (B_FALSE);
4839 }
4840 
4841 /* Pick a unique ifindex */
4842 boolean_t
4843 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4844 {
4845 	uint_t starting_index;
4846 
4847 	if (!ipst->ips_ill_index_wrap) {
4848 		*indexp = ipst->ips_ill_index++;
4849 		if (ipst->ips_ill_index == 0) {
4850 			/* Reached the uint_t limit Next time wrap  */
4851 			ipst->ips_ill_index_wrap = B_TRUE;
4852 		}
4853 		return (B_TRUE);
4854 	}
4855 
4856 	/*
4857 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4858 	 * at this point and don't want to call any function that attempts
4859 	 * to get the lock again.
4860 	 */
4861 	starting_index = ipst->ips_ill_index++;
4862 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4863 		if (ipst->ips_ill_index != 0 &&
4864 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4865 			/* found unused index - use it */
4866 			*indexp = ipst->ips_ill_index;
4867 			return (B_TRUE);
4868 		}
4869 	}
4870 
4871 	/*
4872 	 * all interface indicies are inuse.
4873 	 */
4874 	return (B_FALSE);
4875 }
4876 
4877 /*
4878  * Assign a unique interface index for the phyint.
4879  */
4880 static boolean_t
4881 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4882 {
4883 	ASSERT(phyi->phyint_ifindex == 0);
4884 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4885 }
4886 
4887 /*
4888  * Return a pointer to the ill which matches the supplied name.  Note that
4889  * the ill name length includes the null termination character.  (May be
4890  * called as writer.)
4891  * If do_alloc and the interface is "lo0" it will be automatically created.
4892  * Cannot bump up reference on condemned ills. So dup detect can't be done
4893  * using this func.
4894  */
4895 ill_t *
4896 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4897     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4898     ip_stack_t *ipst)
4899 {
4900 	ill_t	*ill;
4901 	ipif_t	*ipif;
4902 	kstat_named_t	*kn;
4903 	boolean_t isloopback;
4904 	ipsq_t *old_ipsq;
4905 	in6_addr_t ov6addr;
4906 
4907 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4908 
4909 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4910 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4911 	rw_exit(&ipst->ips_ill_g_lock);
4912 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4913 		return (ill);
4914 
4915 	/*
4916 	 * Couldn't find it.  Does this happen to be a lookup for the
4917 	 * loopback device and are we allowed to allocate it?
4918 	 */
4919 	if (!isloopback || !do_alloc)
4920 		return (NULL);
4921 
4922 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4923 
4924 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4925 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4926 		rw_exit(&ipst->ips_ill_g_lock);
4927 		return (ill);
4928 	}
4929 
4930 	/* Create the loopback device on demand */
4931 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4932 	    sizeof (ipif_loopback_name), BPRI_MED));
4933 	if (ill == NULL)
4934 		goto done;
4935 
4936 	*ill = ill_null;
4937 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4938 	ill->ill_ipst = ipst;
4939 	netstack_hold(ipst->ips_netstack);
4940 	/*
4941 	 * For exclusive stacks we set the zoneid to zero
4942 	 * to make IP operate as if in the global zone.
4943 	 */
4944 	ill->ill_zoneid = GLOBAL_ZONEID;
4945 
4946 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4947 	if (ill->ill_phyint == NULL)
4948 		goto done;
4949 
4950 	if (isv6)
4951 		ill->ill_phyint->phyint_illv6 = ill;
4952 	else
4953 		ill->ill_phyint->phyint_illv4 = ill;
4954 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4955 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4956 	/* Add room for tcp+ip headers */
4957 	if (isv6) {
4958 		ill->ill_isv6 = B_TRUE;
4959 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4960 	} else {
4961 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4962 	}
4963 	if (!ill_allocate_mibs(ill))
4964 		goto done;
4965 	ill->ill_max_mtu = ill->ill_max_frag;
4966 	/*
4967 	 * ipif_loopback_name can't be pointed at directly because its used
4968 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4969 	 * from the glist, ill_glist_delete() sets the first character of
4970 	 * ill_name to '\0'.
4971 	 */
4972 	ill->ill_name = (char *)ill + sizeof (*ill);
4973 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4974 	ill->ill_name_length = sizeof (ipif_loopback_name);
4975 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
4976 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4977 
4978 	ill->ill_global_timer = INFINITY;
4979 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4980 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4981 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4982 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4983 
4984 	/* No resolver here. */
4985 	ill->ill_net_type = IRE_LOOPBACK;
4986 
4987 	/* Initialize the ipsq */
4988 	if (!ipsq_init(ill))
4989 		goto done;
4990 
4991 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
4992 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
4993 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
4994 #ifdef DEBUG
4995 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
4996 #endif
4997 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
4998 	if (ipif == NULL)
4999 		goto done;
5000 
5001 	ill->ill_flags = ILLF_MULTICAST;
5002 
5003 	ov6addr = ipif->ipif_v6lcl_addr;
5004 	/* Set up default loopback address and mask. */
5005 	if (!isv6) {
5006 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5007 
5008 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5009 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5010 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5011 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5012 		    ipif->ipif_v6subnet);
5013 		ill->ill_flags |= ILLF_IPV4;
5014 	} else {
5015 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5016 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5017 		ipif->ipif_v6net_mask = ipv6_all_ones;
5018 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5019 		    ipif->ipif_v6subnet);
5020 		ill->ill_flags |= ILLF_IPV6;
5021 	}
5022 
5023 	/*
5024 	 * Chain us in at the end of the ill list. hold the ill
5025 	 * before we make it globally visible. 1 for the lookup.
5026 	 */
5027 	ill->ill_refcnt = 0;
5028 	ill_refhold(ill);
5029 
5030 	ill->ill_frag_count = 0;
5031 	ill->ill_frag_free_num_pkts = 0;
5032 	ill->ill_last_frag_clean_time = 0;
5033 
5034 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5035 
5036 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5037 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5038 
5039 	/* Let SCTP know so that it can add this to its list */
5040 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5041 
5042 	/*
5043 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5044 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5045 	 * requires to be after ill_glist_insert() since we need the
5046 	 * ill_index set. Pass on ipv6_loopback as the old address.
5047 	 */
5048 	sctp_update_ipif_addr(ipif, ov6addr);
5049 
5050 	/*
5051 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5052 	 */
5053 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5054 		/* Loopback ills aren't in any IPMP group */
5055 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5056 		ipsq_delete(old_ipsq);
5057 	}
5058 
5059 	/*
5060 	 * Delay this till the ipif is allocated as ipif_allocate
5061 	 * de-references ill_phyint for getting the ifindex. We
5062 	 * can't do this before ipif_allocate because ill_phyint_reinit
5063 	 * -> phyint_assign_ifindex expects ipif to be present.
5064 	 */
5065 	mutex_enter(&ill->ill_phyint->phyint_lock);
5066 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5067 	mutex_exit(&ill->ill_phyint->phyint_lock);
5068 
5069 	if (ipst->ips_loopback_ksp == NULL) {
5070 		/* Export loopback interface statistics */
5071 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5072 		    ipif_loopback_name, "net",
5073 		    KSTAT_TYPE_NAMED, 2, 0,
5074 		    ipst->ips_netstack->netstack_stackid);
5075 		if (ipst->ips_loopback_ksp != NULL) {
5076 			ipst->ips_loopback_ksp->ks_update =
5077 			    loopback_kstat_update;
5078 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5079 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5080 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5081 			ipst->ips_loopback_ksp->ks_private =
5082 			    (void *)(uintptr_t)ipst->ips_netstack->
5083 			    netstack_stackid;
5084 			kstat_install(ipst->ips_loopback_ksp);
5085 		}
5086 	}
5087 
5088 	if (error != NULL)
5089 		*error = 0;
5090 	*did_alloc = B_TRUE;
5091 	rw_exit(&ipst->ips_ill_g_lock);
5092 	return (ill);
5093 done:
5094 	if (ill != NULL) {
5095 		if (ill->ill_phyint != NULL) {
5096 			ipsq_t	*ipsq;
5097 
5098 			ipsq = ill->ill_phyint->phyint_ipsq;
5099 			if (ipsq != NULL) {
5100 				ipsq->ipsq_ipst = NULL;
5101 				kmem_free(ipsq, sizeof (ipsq_t));
5102 			}
5103 			mi_free(ill->ill_phyint);
5104 		}
5105 		ill_free_mib(ill);
5106 		if (ill->ill_ipst != NULL)
5107 			netstack_rele(ill->ill_ipst->ips_netstack);
5108 		mi_free(ill);
5109 	}
5110 	rw_exit(&ipst->ips_ill_g_lock);
5111 	if (error != NULL)
5112 		*error = ENOMEM;
5113 	return (NULL);
5114 }
5115 
5116 /*
5117  * For IPP calls - use the ip_stack_t for global stack.
5118  */
5119 ill_t *
5120 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5121     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5122 {
5123 	ip_stack_t	*ipst;
5124 	ill_t		*ill;
5125 
5126 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5127 	if (ipst == NULL) {
5128 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5129 		return (NULL);
5130 	}
5131 
5132 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5133 	netstack_rele(ipst->ips_netstack);
5134 	return (ill);
5135 }
5136 
5137 /*
5138  * Return a pointer to the ill which matches the index and IP version type.
5139  */
5140 ill_t *
5141 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5142     ipsq_func_t func, int *err, ip_stack_t *ipst)
5143 {
5144 	ill_t	*ill;
5145 	ipsq_t  *ipsq;
5146 	phyint_t *phyi;
5147 
5148 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5149 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5150 
5151 	if (err != NULL)
5152 		*err = 0;
5153 
5154 	/*
5155 	 * Indexes are stored in the phyint - a common structure
5156 	 * to both IPv4 and IPv6.
5157 	 */
5158 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5159 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5160 	    (void *) &index, NULL);
5161 	if (phyi != NULL) {
5162 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5163 		if (ill != NULL) {
5164 			/*
5165 			 * The block comment at the start of ipif_down
5166 			 * explains the use of the macros used below
5167 			 */
5168 			GRAB_CONN_LOCK(q);
5169 			mutex_enter(&ill->ill_lock);
5170 			if (ILL_CAN_LOOKUP(ill)) {
5171 				ill_refhold_locked(ill);
5172 				mutex_exit(&ill->ill_lock);
5173 				RELEASE_CONN_LOCK(q);
5174 				rw_exit(&ipst->ips_ill_g_lock);
5175 				return (ill);
5176 			} else if (ILL_CAN_WAIT(ill, q)) {
5177 				ipsq = ill->ill_phyint->phyint_ipsq;
5178 				mutex_enter(&ipsq->ipsq_lock);
5179 				rw_exit(&ipst->ips_ill_g_lock);
5180 				mutex_exit(&ill->ill_lock);
5181 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5182 				mutex_exit(&ipsq->ipsq_lock);
5183 				RELEASE_CONN_LOCK(q);
5184 				if (err != NULL)
5185 					*err = EINPROGRESS;
5186 				return (NULL);
5187 			}
5188 			RELEASE_CONN_LOCK(q);
5189 			mutex_exit(&ill->ill_lock);
5190 		}
5191 	}
5192 	rw_exit(&ipst->ips_ill_g_lock);
5193 	if (err != NULL)
5194 		*err = ENXIO;
5195 	return (NULL);
5196 }
5197 
5198 /*
5199  * Return the ifindex next in sequence after the passed in ifindex.
5200  * If there is no next ifindex for the given protocol, return 0.
5201  */
5202 uint_t
5203 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5204 {
5205 	phyint_t *phyi;
5206 	phyint_t *phyi_initial;
5207 	uint_t   ifindex;
5208 
5209 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5210 
5211 	if (index == 0) {
5212 		phyi = avl_first(
5213 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5214 	} else {
5215 		phyi = phyi_initial = avl_find(
5216 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5217 		    (void *) &index, NULL);
5218 	}
5219 
5220 	for (; phyi != NULL;
5221 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5222 	    phyi, AVL_AFTER)) {
5223 		/*
5224 		 * If we're not returning the first interface in the tree
5225 		 * and we still haven't moved past the phyint_t that
5226 		 * corresponds to index, avl_walk needs to be called again
5227 		 */
5228 		if (!((index != 0) && (phyi == phyi_initial))) {
5229 			if (isv6) {
5230 				if ((phyi->phyint_illv6) &&
5231 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5232 				    (phyi->phyint_illv6->ill_isv6 == 1))
5233 					break;
5234 			} else {
5235 				if ((phyi->phyint_illv4) &&
5236 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5237 				    (phyi->phyint_illv4->ill_isv6 == 0))
5238 					break;
5239 			}
5240 		}
5241 	}
5242 
5243 	rw_exit(&ipst->ips_ill_g_lock);
5244 
5245 	if (phyi != NULL)
5246 		ifindex = phyi->phyint_ifindex;
5247 	else
5248 		ifindex = 0;
5249 
5250 	return (ifindex);
5251 }
5252 
5253 /*
5254  * Return the ifindex for the named interface.
5255  * If there is no next ifindex for the interface, return 0.
5256  */
5257 uint_t
5258 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5259 {
5260 	phyint_t	*phyi;
5261 	avl_index_t	where = 0;
5262 	uint_t		ifindex;
5263 
5264 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5265 
5266 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5267 	    name, &where)) == NULL) {
5268 		rw_exit(&ipst->ips_ill_g_lock);
5269 		return (0);
5270 	}
5271 
5272 	ifindex = phyi->phyint_ifindex;
5273 
5274 	rw_exit(&ipst->ips_ill_g_lock);
5275 
5276 	return (ifindex);
5277 }
5278 
5279 /*
5280  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5281  * that gives a running thread a reference to the ill. This reference must be
5282  * released by the thread when it is done accessing the ill and related
5283  * objects. ill_refcnt can not be used to account for static references
5284  * such as other structures pointing to an ill. Callers must generally
5285  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5286  * or be sure that the ill is not being deleted or changing state before
5287  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5288  * ill won't change any of its critical state such as address, netmask etc.
5289  */
5290 void
5291 ill_refhold(ill_t *ill)
5292 {
5293 	mutex_enter(&ill->ill_lock);
5294 	ill->ill_refcnt++;
5295 	ILL_TRACE_REF(ill);
5296 	mutex_exit(&ill->ill_lock);
5297 }
5298 
5299 void
5300 ill_refhold_locked(ill_t *ill)
5301 {
5302 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5303 	ill->ill_refcnt++;
5304 	ILL_TRACE_REF(ill);
5305 }
5306 
5307 int
5308 ill_check_and_refhold(ill_t *ill)
5309 {
5310 	mutex_enter(&ill->ill_lock);
5311 	if (ILL_CAN_LOOKUP(ill)) {
5312 		ill_refhold_locked(ill);
5313 		mutex_exit(&ill->ill_lock);
5314 		return (0);
5315 	}
5316 	mutex_exit(&ill->ill_lock);
5317 	return (ILL_LOOKUP_FAILED);
5318 }
5319 
5320 /*
5321  * Must not be called while holding any locks. Otherwise if this is
5322  * the last reference to be released, there is a chance of recursive mutex
5323  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5324  * to restart an ioctl.
5325  */
5326 void
5327 ill_refrele(ill_t *ill)
5328 {
5329 	mutex_enter(&ill->ill_lock);
5330 	ASSERT(ill->ill_refcnt != 0);
5331 	ill->ill_refcnt--;
5332 	ILL_UNTRACE_REF(ill);
5333 	if (ill->ill_refcnt != 0) {
5334 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5335 		mutex_exit(&ill->ill_lock);
5336 		return;
5337 	}
5338 
5339 	/* Drops the ill_lock */
5340 	ipif_ill_refrele_tail(ill);
5341 }
5342 
5343 /*
5344  * Obtain a weak reference count on the ill. This reference ensures the
5345  * ill won't be freed, but the ill may change any of its critical state
5346  * such as netmask, address etc. Returns an error if the ill has started
5347  * closing.
5348  */
5349 boolean_t
5350 ill_waiter_inc(ill_t *ill)
5351 {
5352 	mutex_enter(&ill->ill_lock);
5353 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5354 		mutex_exit(&ill->ill_lock);
5355 		return (B_FALSE);
5356 	}
5357 	ill->ill_waiters++;
5358 	mutex_exit(&ill->ill_lock);
5359 	return (B_TRUE);
5360 }
5361 
5362 void
5363 ill_waiter_dcr(ill_t *ill)
5364 {
5365 	mutex_enter(&ill->ill_lock);
5366 	ill->ill_waiters--;
5367 	if (ill->ill_waiters == 0)
5368 		cv_broadcast(&ill->ill_cv);
5369 	mutex_exit(&ill->ill_lock);
5370 }
5371 
5372 /*
5373  * Named Dispatch routine to produce a formatted report on all ILLs.
5374  * This report is accessed by using the ndd utility to "get" ND variable
5375  * "ip_ill_status".
5376  */
5377 /* ARGSUSED */
5378 int
5379 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5380 {
5381 	ill_t		*ill;
5382 	ill_walk_context_t ctx;
5383 	ip_stack_t	*ipst;
5384 
5385 	ipst = CONNQ_TO_IPST(q);
5386 
5387 	(void) mi_mpprintf(mp,
5388 	    "ILL      " MI_COL_HDRPAD_STR
5389 	/*   01234567[89ABCDEF] */
5390 	    "rq       " MI_COL_HDRPAD_STR
5391 	/*   01234567[89ABCDEF] */
5392 	    "wq       " MI_COL_HDRPAD_STR
5393 	/*   01234567[89ABCDEF] */
5394 	    "upcnt mxfrg err name");
5395 	/*   12345 12345 123 xxxxxxxx  */
5396 
5397 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5398 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5399 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5400 		(void) mi_mpprintf(mp,
5401 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5402 		    "%05u %05u %03d %s",
5403 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5404 		    ill->ill_ipif_up_count,
5405 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5406 	}
5407 	rw_exit(&ipst->ips_ill_g_lock);
5408 
5409 	return (0);
5410 }
5411 
5412 /*
5413  * Named Dispatch routine to produce a formatted report on all IPIFs.
5414  * This report is accessed by using the ndd utility to "get" ND variable
5415  * "ip_ipif_status".
5416  */
5417 /* ARGSUSED */
5418 int
5419 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5420 {
5421 	char	buf1[INET6_ADDRSTRLEN];
5422 	char	buf2[INET6_ADDRSTRLEN];
5423 	char	buf3[INET6_ADDRSTRLEN];
5424 	char	buf4[INET6_ADDRSTRLEN];
5425 	char	buf5[INET6_ADDRSTRLEN];
5426 	char	buf6[INET6_ADDRSTRLEN];
5427 	char	buf[LIFNAMSIZ];
5428 	ill_t	*ill;
5429 	ipif_t	*ipif;
5430 	nv_t	*nvp;
5431 	uint64_t flags;
5432 	zoneid_t zoneid;
5433 	ill_walk_context_t ctx;
5434 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5435 
5436 	(void) mi_mpprintf(mp,
5437 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5438 	    "\tlocal address\n"
5439 	    "\tsrc address\n"
5440 	    "\tsubnet\n"
5441 	    "\tmask\n"
5442 	    "\tbroadcast\n"
5443 	    "\tp-p-dst");
5444 
5445 	ASSERT(q->q_next == NULL);
5446 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5447 
5448 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5449 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5450 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5451 		for (ipif = ill->ill_ipif; ipif != NULL;
5452 		    ipif = ipif->ipif_next) {
5453 			if (zoneid != GLOBAL_ZONEID &&
5454 			    zoneid != ipif->ipif_zoneid &&
5455 			    ipif->ipif_zoneid != ALL_ZONES)
5456 				continue;
5457 
5458 			ipif_get_name(ipif, buf, sizeof (buf));
5459 			(void) mi_mpprintf(mp,
5460 			    MI_COL_PTRFMT_STR
5461 			    "%04u %05u %u/%u/%u %s %d",
5462 			    (void *)ipif,
5463 			    ipif->ipif_metric, ipif->ipif_mtu,
5464 			    ipif->ipif_ib_pkt_count,
5465 			    ipif->ipif_ob_pkt_count,
5466 			    ipif->ipif_fo_pkt_count,
5467 			    buf,
5468 			    ipif->ipif_zoneid);
5469 
5470 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5471 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5472 
5473 		/* Tack on text strings for any flags. */
5474 		nvp = ipif_nv_tbl;
5475 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5476 			if (nvp->nv_value & flags)
5477 				(void) mi_mpprintf_nr(mp, " %s",
5478 				    nvp->nv_name);
5479 		}
5480 		(void) mi_mpprintf(mp,
5481 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5482 		    inet_ntop(AF_INET6,
5483 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5484 		    inet_ntop(AF_INET6,
5485 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5486 		    inet_ntop(AF_INET6,
5487 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5488 		    inet_ntop(AF_INET6,
5489 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5490 		    inet_ntop(AF_INET6,
5491 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5492 		    inet_ntop(AF_INET6,
5493 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5494 		}
5495 	}
5496 	rw_exit(&ipst->ips_ill_g_lock);
5497 	return (0);
5498 }
5499 
5500 /*
5501  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5502  * driver.  We construct best guess defaults for lower level information that
5503  * we need.  If an interface is brought up without injection of any overriding
5504  * information from outside, we have to be ready to go with these defaults.
5505  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5506  * we primarely want the dl_provider_style.
5507  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5508  * at which point we assume the other part of the information is valid.
5509  */
5510 void
5511 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5512 {
5513 	uchar_t		*brdcst_addr;
5514 	uint_t		brdcst_addr_length, phys_addr_length;
5515 	t_scalar_t	sap_length;
5516 	dl_info_ack_t	*dlia;
5517 	ip_m_t		*ipm;
5518 	dl_qos_cl_sel1_t *sel1;
5519 
5520 	ASSERT(IAM_WRITER_ILL(ill));
5521 
5522 	/*
5523 	 * Till the ill is fully up ILL_CHANGING will be set and
5524 	 * the ill is not globally visible. So no need for a lock.
5525 	 */
5526 	dlia = (dl_info_ack_t *)mp->b_rptr;
5527 	ill->ill_mactype = dlia->dl_mac_type;
5528 
5529 	ipm = ip_m_lookup(dlia->dl_mac_type);
5530 	if (ipm == NULL) {
5531 		ipm = ip_m_lookup(DL_OTHER);
5532 		ASSERT(ipm != NULL);
5533 	}
5534 	ill->ill_media = ipm;
5535 
5536 	/*
5537 	 * When the new DLPI stuff is ready we'll pull lengths
5538 	 * from dlia.
5539 	 */
5540 	if (dlia->dl_version == DL_VERSION_2) {
5541 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5542 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5543 		    brdcst_addr_length);
5544 		if (brdcst_addr == NULL) {
5545 			brdcst_addr_length = 0;
5546 		}
5547 		sap_length = dlia->dl_sap_length;
5548 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5549 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5550 		    brdcst_addr_length, sap_length, phys_addr_length));
5551 	} else {
5552 		brdcst_addr_length = 6;
5553 		brdcst_addr = ip_six_byte_all_ones;
5554 		sap_length = -2;
5555 		phys_addr_length = brdcst_addr_length;
5556 	}
5557 
5558 	ill->ill_bcast_addr_length = brdcst_addr_length;
5559 	ill->ill_phys_addr_length = phys_addr_length;
5560 	ill->ill_sap_length = sap_length;
5561 	ill->ill_max_frag = dlia->dl_max_sdu;
5562 	ill->ill_max_mtu = ill->ill_max_frag;
5563 
5564 	ill->ill_type = ipm->ip_m_type;
5565 
5566 	if (!ill->ill_dlpi_style_set) {
5567 		if (dlia->dl_provider_style == DL_STYLE2)
5568 			ill->ill_needs_attach = 1;
5569 
5570 		/*
5571 		 * Allocate the first ipif on this ill. We don't delay it
5572 		 * further as ioctl handling assumes atleast one ipif to
5573 		 * be present.
5574 		 *
5575 		 * At this point we don't know whether the ill is v4 or v6.
5576 		 * We will know this whan the SIOCSLIFNAME happens and
5577 		 * the correct value for ill_isv6 will be assigned in
5578 		 * ipif_set_values(). We need to hold the ill lock and
5579 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5580 		 * the wakeup.
5581 		 */
5582 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5583 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5584 		mutex_enter(&ill->ill_lock);
5585 		ASSERT(ill->ill_dlpi_style_set == 0);
5586 		ill->ill_dlpi_style_set = 1;
5587 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5588 		cv_broadcast(&ill->ill_cv);
5589 		mutex_exit(&ill->ill_lock);
5590 		freemsg(mp);
5591 		return;
5592 	}
5593 	ASSERT(ill->ill_ipif != NULL);
5594 	/*
5595 	 * We know whether it is IPv4 or IPv6 now, as this is the
5596 	 * second DL_INFO_ACK we are recieving in response to the
5597 	 * DL_INFO_REQ sent in ipif_set_values.
5598 	 */
5599 	if (ill->ill_isv6)
5600 		ill->ill_sap = IP6_DL_SAP;
5601 	else
5602 		ill->ill_sap = IP_DL_SAP;
5603 	/*
5604 	 * Set ipif_mtu which is used to set the IRE's
5605 	 * ire_max_frag value. The driver could have sent
5606 	 * a different mtu from what it sent last time. No
5607 	 * need to call ipif_mtu_change because IREs have
5608 	 * not yet been created.
5609 	 */
5610 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5611 	/*
5612 	 * Clear all the flags that were set based on ill_bcast_addr_length
5613 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5614 	 * changed now and we need to re-evaluate.
5615 	 */
5616 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5617 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5618 
5619 	/*
5620 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5621 	 * changed now.
5622 	 */
5623 	if (ill->ill_bcast_addr_length == 0) {
5624 		if (ill->ill_resolver_mp != NULL)
5625 			freemsg(ill->ill_resolver_mp);
5626 		if (ill->ill_bcast_mp != NULL)
5627 			freemsg(ill->ill_bcast_mp);
5628 		if (ill->ill_flags & ILLF_XRESOLV)
5629 			ill->ill_net_type = IRE_IF_RESOLVER;
5630 		else
5631 			ill->ill_net_type = IRE_IF_NORESOLVER;
5632 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5633 		    ill->ill_phys_addr_length,
5634 		    ill->ill_sap,
5635 		    ill->ill_sap_length);
5636 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5637 
5638 		if (ill->ill_isv6)
5639 			/*
5640 			 * Note: xresolv interfaces will eventually need NOARP
5641 			 * set here as well, but that will require those
5642 			 * external resolvers to have some knowledge of
5643 			 * that flag and act appropriately. Not to be changed
5644 			 * at present.
5645 			 */
5646 			ill->ill_flags |= ILLF_NONUD;
5647 		else
5648 			ill->ill_flags |= ILLF_NOARP;
5649 
5650 		if (ill->ill_phys_addr_length == 0) {
5651 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5652 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5653 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5654 			} else {
5655 				/* pt-pt supports multicast. */
5656 				ill->ill_flags |= ILLF_MULTICAST;
5657 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5658 			}
5659 		}
5660 	} else {
5661 		ill->ill_net_type = IRE_IF_RESOLVER;
5662 		if (ill->ill_bcast_mp != NULL)
5663 			freemsg(ill->ill_bcast_mp);
5664 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5665 		    ill->ill_bcast_addr_length, ill->ill_sap,
5666 		    ill->ill_sap_length);
5667 		/*
5668 		 * Later detect lack of DLPI driver multicast
5669 		 * capability by catching DL_ENABMULTI errors in
5670 		 * ip_rput_dlpi.
5671 		 */
5672 		ill->ill_flags |= ILLF_MULTICAST;
5673 		if (!ill->ill_isv6)
5674 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5675 	}
5676 	/* By default an interface does not support any CoS marking */
5677 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5678 
5679 	/*
5680 	 * If we get QoS information in DL_INFO_ACK, the device supports
5681 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5682 	 */
5683 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5684 	    dlia->dl_qos_length);
5685 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5686 		ill->ill_flags |= ILLF_COS_ENABLED;
5687 	}
5688 
5689 	/* Clear any previous error indication. */
5690 	ill->ill_error = 0;
5691 	freemsg(mp);
5692 }
5693 
5694 /*
5695  * Perform various checks to verify that an address would make sense as a
5696  * local, remote, or subnet interface address.
5697  */
5698 static boolean_t
5699 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5700 {
5701 	ipaddr_t	net_mask;
5702 
5703 	/*
5704 	 * Don't allow all zeroes, or all ones, but allow
5705 	 * all ones netmask.
5706 	 */
5707 	if ((net_mask = ip_net_mask(addr)) == 0)
5708 		return (B_FALSE);
5709 	/* A given netmask overrides the "guess" netmask */
5710 	if (subnet_mask != 0)
5711 		net_mask = subnet_mask;
5712 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5713 	    (addr == (addr | ~net_mask)))) {
5714 		return (B_FALSE);
5715 	}
5716 
5717 	/*
5718 	 * Even if the netmask is all ones, we do not allow address to be
5719 	 * 255.255.255.255
5720 	 */
5721 	if (addr == INADDR_BROADCAST)
5722 		return (B_FALSE);
5723 
5724 	if (CLASSD(addr))
5725 		return (B_FALSE);
5726 
5727 	return (B_TRUE);
5728 }
5729 
5730 #define	V6_IPIF_LINKLOCAL(p)	\
5731 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5732 
5733 /*
5734  * Compare two given ipifs and check if the second one is better than
5735  * the first one using the order of preference (not taking deprecated
5736  * into acount) specified in ipif_lookup_multicast().
5737  */
5738 static boolean_t
5739 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5740 {
5741 	/* Check the least preferred first. */
5742 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5743 		/* If both ipifs are the same, use the first one. */
5744 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5745 			return (B_FALSE);
5746 		else
5747 			return (B_TRUE);
5748 	}
5749 
5750 	/* For IPv6, check for link local address. */
5751 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5752 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5753 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5754 			/* The second one is equal or less preferred. */
5755 			return (B_FALSE);
5756 		} else {
5757 			return (B_TRUE);
5758 		}
5759 	}
5760 
5761 	/* Then check for point to point interface. */
5762 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5763 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5764 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5765 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5766 			return (B_FALSE);
5767 		} else {
5768 			return (B_TRUE);
5769 		}
5770 	}
5771 
5772 	/* old_ipif is a normal interface, so no need to use the new one. */
5773 	return (B_FALSE);
5774 }
5775 
5776 /*
5777  * Find any non-virtual, not condemned, and up multicast capable interface
5778  * given an IP instance and zoneid.  Order of preference is:
5779  *
5780  * 1. normal
5781  * 1.1 normal, but deprecated
5782  * 2. point to point
5783  * 2.1 point to point, but deprecated
5784  * 3. link local
5785  * 3.1 link local, but deprecated
5786  * 4. loopback.
5787  */
5788 ipif_t *
5789 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5790 {
5791 	ill_t			*ill;
5792 	ill_walk_context_t	ctx;
5793 	ipif_t			*ipif;
5794 	ipif_t			*saved_ipif = NULL;
5795 	ipif_t			*dep_ipif = NULL;
5796 
5797 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5798 	if (isv6)
5799 		ill = ILL_START_WALK_V6(&ctx, ipst);
5800 	else
5801 		ill = ILL_START_WALK_V4(&ctx, ipst);
5802 
5803 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5804 		mutex_enter(&ill->ill_lock);
5805 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5806 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5807 			mutex_exit(&ill->ill_lock);
5808 			continue;
5809 		}
5810 		for (ipif = ill->ill_ipif; ipif != NULL;
5811 		    ipif = ipif->ipif_next) {
5812 			if (zoneid != ipif->ipif_zoneid &&
5813 			    zoneid != ALL_ZONES &&
5814 			    ipif->ipif_zoneid != ALL_ZONES) {
5815 				continue;
5816 			}
5817 			if (!(ipif->ipif_flags & IPIF_UP) ||
5818 			    !IPIF_CAN_LOOKUP(ipif)) {
5819 				continue;
5820 			}
5821 
5822 			/*
5823 			 * Found one candidate.  If it is deprecated,
5824 			 * remember it in dep_ipif.  If it is not deprecated,
5825 			 * remember it in saved_ipif.
5826 			 */
5827 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5828 				if (dep_ipif == NULL) {
5829 					dep_ipif = ipif;
5830 				} else if (ipif_comp_multi(dep_ipif, ipif,
5831 				    isv6)) {
5832 					/*
5833 					 * If the previous dep_ipif does not
5834 					 * belong to the same ill, we've done
5835 					 * a ipif_refhold() on it.  So we need
5836 					 * to release it.
5837 					 */
5838 					if (dep_ipif->ipif_ill != ill)
5839 						ipif_refrele(dep_ipif);
5840 					dep_ipif = ipif;
5841 				}
5842 				continue;
5843 			}
5844 			if (saved_ipif == NULL) {
5845 				saved_ipif = ipif;
5846 			} else {
5847 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5848 					if (saved_ipif->ipif_ill != ill)
5849 						ipif_refrele(saved_ipif);
5850 					saved_ipif = ipif;
5851 				}
5852 			}
5853 		}
5854 		/*
5855 		 * Before going to the next ill, do a ipif_refhold() on the
5856 		 * saved ones.
5857 		 */
5858 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5859 			ipif_refhold_locked(saved_ipif);
5860 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5861 			ipif_refhold_locked(dep_ipif);
5862 		mutex_exit(&ill->ill_lock);
5863 	}
5864 	rw_exit(&ipst->ips_ill_g_lock);
5865 
5866 	/*
5867 	 * If we have only the saved_ipif, return it.  But if we have both
5868 	 * saved_ipif and dep_ipif, check to see which one is better.
5869 	 */
5870 	if (saved_ipif != NULL) {
5871 		if (dep_ipif != NULL) {
5872 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5873 				ipif_refrele(saved_ipif);
5874 				return (dep_ipif);
5875 			} else {
5876 				ipif_refrele(dep_ipif);
5877 				return (saved_ipif);
5878 			}
5879 		}
5880 		return (saved_ipif);
5881 	} else {
5882 		return (dep_ipif);
5883 	}
5884 }
5885 
5886 /*
5887  * This function is called when an application does not specify an interface
5888  * to be used for multicast traffic (joining a group/sending data).  It
5889  * calls ire_lookup_multi() to look for an interface route for the
5890  * specified multicast group.  Doing this allows the administrator to add
5891  * prefix routes for multicast to indicate which interface to be used for
5892  * multicast traffic in the above scenario.  The route could be for all
5893  * multicast (224.0/4), for a single multicast group (a /32 route) or
5894  * anything in between.  If there is no such multicast route, we just find
5895  * any multicast capable interface and return it.  The returned ipif
5896  * is refhold'ed.
5897  */
5898 ipif_t *
5899 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5900 {
5901 	ire_t			*ire;
5902 	ipif_t			*ipif;
5903 
5904 	ire = ire_lookup_multi(group, zoneid, ipst);
5905 	if (ire != NULL) {
5906 		ipif = ire->ire_ipif;
5907 		ipif_refhold(ipif);
5908 		ire_refrele(ire);
5909 		return (ipif);
5910 	}
5911 
5912 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5913 }
5914 
5915 /*
5916  * Look for an ipif with the specified interface address and destination.
5917  * The destination address is used only for matching point-to-point interfaces.
5918  */
5919 ipif_t *
5920 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5921     ipsq_func_t func, int *error, ip_stack_t *ipst)
5922 {
5923 	ipif_t	*ipif;
5924 	ill_t	*ill;
5925 	ill_walk_context_t ctx;
5926 	ipsq_t	*ipsq;
5927 
5928 	if (error != NULL)
5929 		*error = 0;
5930 
5931 	/*
5932 	 * First match all the point-to-point interfaces
5933 	 * before looking at non-point-to-point interfaces.
5934 	 * This is done to avoid returning non-point-to-point
5935 	 * ipif instead of unnumbered point-to-point ipif.
5936 	 */
5937 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5938 	ill = ILL_START_WALK_V4(&ctx, ipst);
5939 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5940 		GRAB_CONN_LOCK(q);
5941 		mutex_enter(&ill->ill_lock);
5942 		for (ipif = ill->ill_ipif; ipif != NULL;
5943 		    ipif = ipif->ipif_next) {
5944 			/* Allow the ipif to be down */
5945 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5946 			    (ipif->ipif_lcl_addr == if_addr) &&
5947 			    (ipif->ipif_pp_dst_addr == dst)) {
5948 				/*
5949 				 * The block comment at the start of ipif_down
5950 				 * explains the use of the macros used below
5951 				 */
5952 				if (IPIF_CAN_LOOKUP(ipif)) {
5953 					ipif_refhold_locked(ipif);
5954 					mutex_exit(&ill->ill_lock);
5955 					RELEASE_CONN_LOCK(q);
5956 					rw_exit(&ipst->ips_ill_g_lock);
5957 					return (ipif);
5958 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5959 					ipsq = ill->ill_phyint->phyint_ipsq;
5960 					mutex_enter(&ipsq->ipsq_lock);
5961 					mutex_exit(&ill->ill_lock);
5962 					rw_exit(&ipst->ips_ill_g_lock);
5963 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5964 					    ill);
5965 					mutex_exit(&ipsq->ipsq_lock);
5966 					RELEASE_CONN_LOCK(q);
5967 					if (error != NULL)
5968 						*error = EINPROGRESS;
5969 					return (NULL);
5970 				}
5971 			}
5972 		}
5973 		mutex_exit(&ill->ill_lock);
5974 		RELEASE_CONN_LOCK(q);
5975 	}
5976 	rw_exit(&ipst->ips_ill_g_lock);
5977 
5978 	/* lookup the ipif based on interface address */
5979 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5980 	    ipst);
5981 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5982 	return (ipif);
5983 }
5984 
5985 /*
5986  * Look for an ipif with the specified address. For point-point links
5987  * we look for matches on either the destination address and the local
5988  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5989  * is set.
5990  * Matches on a specific ill if match_ill is set.
5991  */
5992 ipif_t *
5993 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5994     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
5995 {
5996 	ipif_t  *ipif;
5997 	ill_t   *ill;
5998 	boolean_t ptp = B_FALSE;
5999 	ipsq_t	*ipsq;
6000 	ill_walk_context_t	ctx;
6001 
6002 	if (error != NULL)
6003 		*error = 0;
6004 
6005 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6006 	/*
6007 	 * Repeat twice, first based on local addresses and
6008 	 * next time for pointopoint.
6009 	 */
6010 repeat:
6011 	ill = ILL_START_WALK_V4(&ctx, ipst);
6012 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6013 		if (match_ill != NULL && ill != match_ill) {
6014 			continue;
6015 		}
6016 		GRAB_CONN_LOCK(q);
6017 		mutex_enter(&ill->ill_lock);
6018 		for (ipif = ill->ill_ipif; ipif != NULL;
6019 		    ipif = ipif->ipif_next) {
6020 			if (zoneid != ALL_ZONES &&
6021 			    zoneid != ipif->ipif_zoneid &&
6022 			    ipif->ipif_zoneid != ALL_ZONES)
6023 				continue;
6024 			/* Allow the ipif to be down */
6025 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6026 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6027 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6028 			    (ipif->ipif_pp_dst_addr == addr))) {
6029 				/*
6030 				 * The block comment at the start of ipif_down
6031 				 * explains the use of the macros used below
6032 				 */
6033 				if (IPIF_CAN_LOOKUP(ipif)) {
6034 					ipif_refhold_locked(ipif);
6035 					mutex_exit(&ill->ill_lock);
6036 					RELEASE_CONN_LOCK(q);
6037 					rw_exit(&ipst->ips_ill_g_lock);
6038 					return (ipif);
6039 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6040 					ipsq = ill->ill_phyint->phyint_ipsq;
6041 					mutex_enter(&ipsq->ipsq_lock);
6042 					mutex_exit(&ill->ill_lock);
6043 					rw_exit(&ipst->ips_ill_g_lock);
6044 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6045 					    ill);
6046 					mutex_exit(&ipsq->ipsq_lock);
6047 					RELEASE_CONN_LOCK(q);
6048 					if (error != NULL)
6049 						*error = EINPROGRESS;
6050 					return (NULL);
6051 				}
6052 			}
6053 		}
6054 		mutex_exit(&ill->ill_lock);
6055 		RELEASE_CONN_LOCK(q);
6056 	}
6057 
6058 	/* If we already did the ptp case, then we are done */
6059 	if (ptp) {
6060 		rw_exit(&ipst->ips_ill_g_lock);
6061 		if (error != NULL)
6062 			*error = ENXIO;
6063 		return (NULL);
6064 	}
6065 	ptp = B_TRUE;
6066 	goto repeat;
6067 }
6068 
6069 /*
6070  * Look for an ipif with the specified address. For point-point links
6071  * we look for matches on either the destination address and the local
6072  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6073  * is set.
6074  * Matches on a specific ill if match_ill is set.
6075  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6076  */
6077 zoneid_t
6078 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6079 {
6080 	zoneid_t zoneid;
6081 	ipif_t  *ipif;
6082 	ill_t   *ill;
6083 	boolean_t ptp = B_FALSE;
6084 	ill_walk_context_t	ctx;
6085 
6086 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6087 	/*
6088 	 * Repeat twice, first based on local addresses and
6089 	 * next time for pointopoint.
6090 	 */
6091 repeat:
6092 	ill = ILL_START_WALK_V4(&ctx, ipst);
6093 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6094 		if (match_ill != NULL && ill != match_ill) {
6095 			continue;
6096 		}
6097 		mutex_enter(&ill->ill_lock);
6098 		for (ipif = ill->ill_ipif; ipif != NULL;
6099 		    ipif = ipif->ipif_next) {
6100 			/* Allow the ipif to be down */
6101 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6102 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6103 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6104 			    (ipif->ipif_pp_dst_addr == addr)) &&
6105 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6106 				zoneid = ipif->ipif_zoneid;
6107 				mutex_exit(&ill->ill_lock);
6108 				rw_exit(&ipst->ips_ill_g_lock);
6109 				/*
6110 				 * If ipif_zoneid was ALL_ZONES then we have
6111 				 * a trusted extensions shared IP address.
6112 				 * In that case GLOBAL_ZONEID works to send.
6113 				 */
6114 				if (zoneid == ALL_ZONES)
6115 					zoneid = GLOBAL_ZONEID;
6116 				return (zoneid);
6117 			}
6118 		}
6119 		mutex_exit(&ill->ill_lock);
6120 	}
6121 
6122 	/* If we already did the ptp case, then we are done */
6123 	if (ptp) {
6124 		rw_exit(&ipst->ips_ill_g_lock);
6125 		return (ALL_ZONES);
6126 	}
6127 	ptp = B_TRUE;
6128 	goto repeat;
6129 }
6130 
6131 /*
6132  * Look for an ipif that matches the specified remote address i.e. the
6133  * ipif that would receive the specified packet.
6134  * First look for directly connected interfaces and then do a recursive
6135  * IRE lookup and pick the first ipif corresponding to the source address in the
6136  * ire.
6137  * Returns: held ipif
6138  */
6139 ipif_t *
6140 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6141 {
6142 	ipif_t	*ipif;
6143 	ire_t	*ire;
6144 	ip_stack_t	*ipst = ill->ill_ipst;
6145 
6146 	ASSERT(!ill->ill_isv6);
6147 
6148 	/*
6149 	 * Someone could be changing this ipif currently or change it
6150 	 * after we return this. Thus  a few packets could use the old
6151 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6152 	 * will atomically be updated or cleaned up with the new value
6153 	 * Thus we don't need a lock to check the flags or other attrs below.
6154 	 */
6155 	mutex_enter(&ill->ill_lock);
6156 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6157 		if (!IPIF_CAN_LOOKUP(ipif))
6158 			continue;
6159 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6160 		    ipif->ipif_zoneid != ALL_ZONES)
6161 			continue;
6162 		/* Allow the ipif to be down */
6163 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6164 			if ((ipif->ipif_pp_dst_addr == addr) ||
6165 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6166 			    ipif->ipif_lcl_addr == addr)) {
6167 				ipif_refhold_locked(ipif);
6168 				mutex_exit(&ill->ill_lock);
6169 				return (ipif);
6170 			}
6171 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6172 			ipif_refhold_locked(ipif);
6173 			mutex_exit(&ill->ill_lock);
6174 			return (ipif);
6175 		}
6176 	}
6177 	mutex_exit(&ill->ill_lock);
6178 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6179 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6180 	if (ire != NULL) {
6181 		/*
6182 		 * The callers of this function wants to know the
6183 		 * interface on which they have to send the replies
6184 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6185 		 * derived from different ills, we really don't care
6186 		 * what we return here.
6187 		 */
6188 		ipif = ire->ire_ipif;
6189 		if (ipif != NULL) {
6190 			ipif_refhold(ipif);
6191 			ire_refrele(ire);
6192 			return (ipif);
6193 		}
6194 		ire_refrele(ire);
6195 	}
6196 	/* Pick the first interface */
6197 	ipif = ipif_get_next_ipif(NULL, ill);
6198 	return (ipif);
6199 }
6200 
6201 /*
6202  * This func does not prevent refcnt from increasing. But if
6203  * the caller has taken steps to that effect, then this func
6204  * can be used to determine whether the ill has become quiescent
6205  */
6206 static boolean_t
6207 ill_is_quiescent(ill_t *ill)
6208 {
6209 	ipif_t	*ipif;
6210 
6211 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6212 
6213 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6214 		if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6215 			return (B_FALSE);
6216 		}
6217 	}
6218 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6219 		return (B_FALSE);
6220 	}
6221 	return (B_TRUE);
6222 }
6223 
6224 boolean_t
6225 ill_is_freeable(ill_t *ill)
6226 {
6227 	ipif_t	*ipif;
6228 
6229 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6230 
6231 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6232 		if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6233 			return (B_FALSE);
6234 		}
6235 	}
6236 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6237 		return (B_FALSE);
6238 	}
6239 	return (B_TRUE);
6240 }
6241 
6242 /*
6243  * This func does not prevent refcnt from increasing. But if
6244  * the caller has taken steps to that effect, then this func
6245  * can be used to determine whether the ipif has become quiescent
6246  */
6247 static boolean_t
6248 ipif_is_quiescent(ipif_t *ipif)
6249 {
6250 	ill_t *ill;
6251 
6252 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6253 
6254 	if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6255 		return (B_FALSE);
6256 	}
6257 
6258 	ill = ipif->ipif_ill;
6259 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6260 	    ill->ill_logical_down) {
6261 		return (B_TRUE);
6262 	}
6263 
6264 	/* This is the last ipif going down or being deleted on this ill */
6265 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6266 		return (B_FALSE);
6267 	}
6268 
6269 	return (B_TRUE);
6270 }
6271 
6272 /*
6273  * return true if the ipif can be destroyed: the ipif has to be quiescent
6274  * with zero references from ire/nce/ilm to it.
6275  */
6276 static boolean_t
6277 ipif_is_freeable(ipif_t *ipif)
6278 {
6279 
6280 	ill_t *ill;
6281 
6282 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6283 
6284 	if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6285 		return (B_FALSE);
6286 	}
6287 
6288 	ill = ipif->ipif_ill;
6289 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6290 	    ill->ill_logical_down) {
6291 		return (B_TRUE);
6292 	}
6293 
6294 	/* This is the last ipif going down or being deleted on this ill */
6295 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6296 		return (B_FALSE);
6297 	}
6298 
6299 	return (B_TRUE);
6300 }
6301 
6302 /*
6303  * This func does not prevent refcnt from increasing. But if
6304  * the caller has taken steps to that effect, then this func
6305  * can be used to determine whether the ipifs marked with IPIF_MOVING
6306  * have become quiescent and can be moved in a failover/failback.
6307  */
6308 static ipif_t *
6309 ill_quiescent_to_move(ill_t *ill)
6310 {
6311 	ipif_t  *ipif;
6312 
6313 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6314 
6315 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6316 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6317 			if (ipif->ipif_refcnt != 0 ||
6318 			    !IPIF_DOWN_OK(ipif)) {
6319 				return (ipif);
6320 			}
6321 		}
6322 	}
6323 	return (NULL);
6324 }
6325 
6326 /*
6327  * The ipif/ill/ire has been refreled. Do the tail processing.
6328  * Determine if the ipif or ill in question has become quiescent and if so
6329  * wakeup close and/or restart any queued pending ioctl that is waiting
6330  * for the ipif_down (or ill_down)
6331  */
6332 void
6333 ipif_ill_refrele_tail(ill_t *ill)
6334 {
6335 	mblk_t	*mp;
6336 	conn_t	*connp;
6337 	ipsq_t	*ipsq;
6338 	ipif_t	*ipif;
6339 	dl_notify_ind_t *dlindp;
6340 
6341 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6342 
6343 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6344 	    ill_is_freeable(ill)) {
6345 		/* ill_close may be waiting */
6346 		cv_broadcast(&ill->ill_cv);
6347 	}
6348 
6349 	/* ipsq can't change because ill_lock  is held */
6350 	ipsq = ill->ill_phyint->phyint_ipsq;
6351 	if (ipsq->ipsq_waitfor == 0) {
6352 		/* Not waiting for anything, just return. */
6353 		mutex_exit(&ill->ill_lock);
6354 		return;
6355 	}
6356 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6357 	    ipsq->ipsq_pending_ipif != NULL);
6358 	/*
6359 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6360 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6361 	 * be zero for restarting an ioctl that ends up downing the ill.
6362 	 */
6363 	ipif = ipsq->ipsq_pending_ipif;
6364 	if (ipif->ipif_ill != ill) {
6365 		/* The ioctl is pending on some other ill. */
6366 		mutex_exit(&ill->ill_lock);
6367 		return;
6368 	}
6369 
6370 	switch (ipsq->ipsq_waitfor) {
6371 	case IPIF_DOWN:
6372 		if (!ipif_is_quiescent(ipif)) {
6373 			mutex_exit(&ill->ill_lock);
6374 			return;
6375 		}
6376 		break;
6377 	case IPIF_FREE:
6378 		if (!ipif_is_freeable(ipif)) {
6379 			mutex_exit(&ill->ill_lock);
6380 			return;
6381 		}
6382 		break;
6383 
6384 	case ILL_DOWN:
6385 		if (!ill_is_quiescent(ill)) {
6386 			mutex_exit(&ill->ill_lock);
6387 			return;
6388 		}
6389 		break;
6390 	case ILL_FREE:
6391 		/*
6392 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6393 		 * waits synchronously in ip_close, and no message is queued in
6394 		 * ipsq_pending_mp at all in this case
6395 		 */
6396 		if (!ill_is_freeable(ill)) {
6397 			mutex_exit(&ill->ill_lock);
6398 			return;
6399 		}
6400 		break;
6401 
6402 	case ILL_MOVE_OK:
6403 		if (ill_quiescent_to_move(ill) != NULL) {
6404 			mutex_exit(&ill->ill_lock);
6405 			return;
6406 		}
6407 		break;
6408 	default:
6409 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6410 		    (void *)ipsq, ipsq->ipsq_waitfor);
6411 	}
6412 
6413 	/*
6414 	 * Incr refcnt for the qwriter_ip call below which
6415 	 * does a refrele
6416 	 */
6417 	ill_refhold_locked(ill);
6418 	mp = ipsq_pending_mp_get(ipsq, &connp);
6419 	mutex_exit(&ill->ill_lock);
6420 
6421 	ASSERT(mp != NULL);
6422 	/*
6423 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6424 	 * we can only get here when the current operation decides it
6425 	 * it needs to quiesce via ipsq_pending_mp_add().
6426 	 */
6427 	switch (mp->b_datap->db_type) {
6428 	case M_PCPROTO:
6429 	case M_PROTO:
6430 		/*
6431 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6432 		 */
6433 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6434 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6435 
6436 		switch (dlindp->dl_notification) {
6437 		case DL_NOTE_PHYS_ADDR:
6438 			qwriter_ip(ill, ill->ill_rq, mp,
6439 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6440 			return;
6441 		default:
6442 			ASSERT(0);
6443 		}
6444 		break;
6445 
6446 	case M_ERROR:
6447 	case M_HANGUP:
6448 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6449 		    B_TRUE);
6450 		return;
6451 
6452 	case M_IOCTL:
6453 	case M_IOCDATA:
6454 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6455 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6456 		return;
6457 
6458 	default:
6459 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6460 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6461 	}
6462 }
6463 
6464 #ifdef DEBUG
6465 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6466 static void
6467 th_trace_rrecord(th_trace_t *th_trace)
6468 {
6469 	tr_buf_t *tr_buf;
6470 	uint_t lastref;
6471 
6472 	lastref = th_trace->th_trace_lastref;
6473 	lastref++;
6474 	if (lastref == TR_BUF_MAX)
6475 		lastref = 0;
6476 	th_trace->th_trace_lastref = lastref;
6477 	tr_buf = &th_trace->th_trbuf[lastref];
6478 	tr_buf->tr_time = lbolt;
6479 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6480 }
6481 
6482 static void
6483 th_trace_free(void *value)
6484 {
6485 	th_trace_t *th_trace = value;
6486 
6487 	ASSERT(th_trace->th_refcnt == 0);
6488 	kmem_free(th_trace, sizeof (*th_trace));
6489 }
6490 
6491 /*
6492  * Find or create the per-thread hash table used to track object references.
6493  * The ipst argument is NULL if we shouldn't allocate.
6494  *
6495  * Accesses per-thread data, so there's no need to lock here.
6496  */
6497 static mod_hash_t *
6498 th_trace_gethash(ip_stack_t *ipst)
6499 {
6500 	th_hash_t *thh;
6501 
6502 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6503 		mod_hash_t *mh;
6504 		char name[256];
6505 		size_t objsize, rshift;
6506 		int retv;
6507 
6508 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6509 			return (NULL);
6510 		(void) snprintf(name, sizeof (name), "th_trace_%p",
6511 		    (void *)curthread);
6512 
6513 		/*
6514 		 * We use mod_hash_create_extended here rather than the more
6515 		 * obvious mod_hash_create_ptrhash because the latter has a
6516 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6517 		 * block.
6518 		 */
6519 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6520 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6521 		rshift = highbit(objsize);
6522 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6523 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6524 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6525 		if (mh == NULL) {
6526 			kmem_free(thh, sizeof (*thh));
6527 			return (NULL);
6528 		}
6529 		thh->thh_hash = mh;
6530 		thh->thh_ipst = ipst;
6531 		/*
6532 		 * We trace ills, ipifs, ires, and nces.  All of these are
6533 		 * per-IP-stack, so the lock on the thread list is as well.
6534 		 */
6535 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6536 		list_insert_tail(&ip_thread_list, thh);
6537 		rw_exit(&ip_thread_rwlock);
6538 		retv = tsd_set(ip_thread_data, thh);
6539 		ASSERT(retv == 0);
6540 	}
6541 	return (thh != NULL ? thh->thh_hash : NULL);
6542 }
6543 
6544 boolean_t
6545 th_trace_ref(const void *obj, ip_stack_t *ipst)
6546 {
6547 	th_trace_t *th_trace;
6548 	mod_hash_t *mh;
6549 	mod_hash_val_t val;
6550 
6551 	if ((mh = th_trace_gethash(ipst)) == NULL)
6552 		return (B_FALSE);
6553 
6554 	/*
6555 	 * Attempt to locate the trace buffer for this obj and thread.
6556 	 * If it does not exist, then allocate a new trace buffer and
6557 	 * insert into the hash.
6558 	 */
6559 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6560 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6561 		if (th_trace == NULL)
6562 			return (B_FALSE);
6563 
6564 		th_trace->th_id = curthread;
6565 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6566 		    (mod_hash_val_t)th_trace) != 0) {
6567 			kmem_free(th_trace, sizeof (th_trace_t));
6568 			return (B_FALSE);
6569 		}
6570 	} else {
6571 		th_trace = (th_trace_t *)val;
6572 	}
6573 
6574 	ASSERT(th_trace->th_refcnt >= 0 &&
6575 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6576 
6577 	th_trace->th_refcnt++;
6578 	th_trace_rrecord(th_trace);
6579 	return (B_TRUE);
6580 }
6581 
6582 /*
6583  * For the purpose of tracing a reference release, we assume that global
6584  * tracing is always on and that the same thread initiated the reference hold
6585  * is releasing.
6586  */
6587 void
6588 th_trace_unref(const void *obj)
6589 {
6590 	int retv;
6591 	mod_hash_t *mh;
6592 	th_trace_t *th_trace;
6593 	mod_hash_val_t val;
6594 
6595 	mh = th_trace_gethash(NULL);
6596 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6597 	ASSERT(retv == 0);
6598 	th_trace = (th_trace_t *)val;
6599 
6600 	ASSERT(th_trace->th_refcnt > 0);
6601 	th_trace->th_refcnt--;
6602 	th_trace_rrecord(th_trace);
6603 }
6604 
6605 /*
6606  * If tracing has been disabled, then we assume that the reference counts are
6607  * now useless, and we clear them out before destroying the entries.
6608  */
6609 void
6610 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6611 {
6612 	th_hash_t	*thh;
6613 	mod_hash_t	*mh;
6614 	mod_hash_val_t	val;
6615 	th_trace_t	*th_trace;
6616 	int		retv;
6617 
6618 	rw_enter(&ip_thread_rwlock, RW_READER);
6619 	for (thh = list_head(&ip_thread_list); thh != NULL;
6620 	    thh = list_next(&ip_thread_list, thh)) {
6621 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6622 		    &val) == 0) {
6623 			th_trace = (th_trace_t *)val;
6624 			if (trace_disable)
6625 				th_trace->th_refcnt = 0;
6626 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6627 			ASSERT(retv == 0);
6628 		}
6629 	}
6630 	rw_exit(&ip_thread_rwlock);
6631 }
6632 
6633 void
6634 ipif_trace_ref(ipif_t *ipif)
6635 {
6636 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6637 
6638 	if (ipif->ipif_trace_disable)
6639 		return;
6640 
6641 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6642 		ipif->ipif_trace_disable = B_TRUE;
6643 		ipif_trace_cleanup(ipif);
6644 	}
6645 }
6646 
6647 void
6648 ipif_untrace_ref(ipif_t *ipif)
6649 {
6650 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6651 
6652 	if (!ipif->ipif_trace_disable)
6653 		th_trace_unref(ipif);
6654 }
6655 
6656 void
6657 ill_trace_ref(ill_t *ill)
6658 {
6659 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6660 
6661 	if (ill->ill_trace_disable)
6662 		return;
6663 
6664 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6665 		ill->ill_trace_disable = B_TRUE;
6666 		ill_trace_cleanup(ill);
6667 	}
6668 }
6669 
6670 void
6671 ill_untrace_ref(ill_t *ill)
6672 {
6673 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6674 
6675 	if (!ill->ill_trace_disable)
6676 		th_trace_unref(ill);
6677 }
6678 
6679 /*
6680  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6681  * failure, ipif_trace_disable is set.
6682  */
6683 static void
6684 ipif_trace_cleanup(const ipif_t *ipif)
6685 {
6686 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6687 }
6688 
6689 /*
6690  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6691  * failure, ill_trace_disable is set.
6692  */
6693 static void
6694 ill_trace_cleanup(const ill_t *ill)
6695 {
6696 	th_trace_cleanup(ill, ill->ill_trace_disable);
6697 }
6698 #endif /* DEBUG */
6699 
6700 void
6701 ipif_refhold_locked(ipif_t *ipif)
6702 {
6703 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6704 	ipif->ipif_refcnt++;
6705 	IPIF_TRACE_REF(ipif);
6706 }
6707 
6708 void
6709 ipif_refhold(ipif_t *ipif)
6710 {
6711 	ill_t	*ill;
6712 
6713 	ill = ipif->ipif_ill;
6714 	mutex_enter(&ill->ill_lock);
6715 	ipif->ipif_refcnt++;
6716 	IPIF_TRACE_REF(ipif);
6717 	mutex_exit(&ill->ill_lock);
6718 }
6719 
6720 /*
6721  * Must not be called while holding any locks. Otherwise if this is
6722  * the last reference to be released there is a chance of recursive mutex
6723  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6724  * to restart an ioctl.
6725  */
6726 void
6727 ipif_refrele(ipif_t *ipif)
6728 {
6729 	ill_t	*ill;
6730 
6731 	ill = ipif->ipif_ill;
6732 
6733 	mutex_enter(&ill->ill_lock);
6734 	ASSERT(ipif->ipif_refcnt != 0);
6735 	ipif->ipif_refcnt--;
6736 	IPIF_UNTRACE_REF(ipif);
6737 	if (ipif->ipif_refcnt != 0) {
6738 		mutex_exit(&ill->ill_lock);
6739 		return;
6740 	}
6741 
6742 	/* Drops the ill_lock */
6743 	ipif_ill_refrele_tail(ill);
6744 }
6745 
6746 ipif_t *
6747 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6748 {
6749 	ipif_t	*ipif;
6750 
6751 	mutex_enter(&ill->ill_lock);
6752 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6753 	    ipif != NULL; ipif = ipif->ipif_next) {
6754 		if (!IPIF_CAN_LOOKUP(ipif))
6755 			continue;
6756 		ipif_refhold_locked(ipif);
6757 		mutex_exit(&ill->ill_lock);
6758 		return (ipif);
6759 	}
6760 	mutex_exit(&ill->ill_lock);
6761 	return (NULL);
6762 }
6763 
6764 /*
6765  * TODO: make this table extendible at run time
6766  * Return a pointer to the mac type info for 'mac_type'
6767  */
6768 static ip_m_t *
6769 ip_m_lookup(t_uscalar_t mac_type)
6770 {
6771 	ip_m_t	*ipm;
6772 
6773 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6774 		if (ipm->ip_m_mac_type == mac_type)
6775 			return (ipm);
6776 	return (NULL);
6777 }
6778 
6779 /*
6780  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6781  * ipif_arg is passed in to associate it with the correct interface.
6782  * We may need to restart this operation if the ipif cannot be looked up
6783  * due to an exclusive operation that is currently in progress. The restart
6784  * entry point is specified by 'func'
6785  */
6786 int
6787 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6788     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6789     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6790     struct rtsa_s *sp, ip_stack_t *ipst)
6791 {
6792 	ire_t	*ire;
6793 	ire_t	*gw_ire = NULL;
6794 	ipif_t	*ipif = NULL;
6795 	boolean_t ipif_refheld = B_FALSE;
6796 	uint_t	type;
6797 	int	match_flags = MATCH_IRE_TYPE;
6798 	int	error;
6799 	tsol_gc_t *gc = NULL;
6800 	tsol_gcgrp_t *gcgrp = NULL;
6801 	boolean_t gcgrp_xtraref = B_FALSE;
6802 
6803 	ip1dbg(("ip_rt_add:"));
6804 
6805 	if (ire_arg != NULL)
6806 		*ire_arg = NULL;
6807 
6808 	/*
6809 	 * If this is the case of RTF_HOST being set, then we set the netmask
6810 	 * to all ones (regardless if one was supplied).
6811 	 */
6812 	if (flags & RTF_HOST)
6813 		mask = IP_HOST_MASK;
6814 
6815 	/*
6816 	 * Prevent routes with a zero gateway from being created (since
6817 	 * interfaces can currently be plumbed and brought up no assigned
6818 	 * address).
6819 	 */
6820 	if (gw_addr == 0)
6821 		return (ENETUNREACH);
6822 	/*
6823 	 * Get the ipif, if any, corresponding to the gw_addr
6824 	 */
6825 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6826 	    ipst);
6827 	if (ipif != NULL) {
6828 		if (IS_VNI(ipif->ipif_ill)) {
6829 			ipif_refrele(ipif);
6830 			return (EINVAL);
6831 		}
6832 		ipif_refheld = B_TRUE;
6833 	} else if (error == EINPROGRESS) {
6834 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6835 		return (EINPROGRESS);
6836 	} else {
6837 		error = 0;
6838 	}
6839 
6840 	if (ipif != NULL) {
6841 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6842 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6843 	} else {
6844 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6845 	}
6846 
6847 	/*
6848 	 * GateD will attempt to create routes with a loopback interface
6849 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6850 	 * these routes to be added, but create them as interface routes
6851 	 * since the gateway is an interface address.
6852 	 */
6853 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6854 		flags &= ~RTF_GATEWAY;
6855 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6856 		    mask == IP_HOST_MASK) {
6857 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6858 			    ALL_ZONES, NULL, match_flags, ipst);
6859 			if (ire != NULL) {
6860 				ire_refrele(ire);
6861 				if (ipif_refheld)
6862 					ipif_refrele(ipif);
6863 				return (EEXIST);
6864 			}
6865 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6866 			    "for 0x%x\n", (void *)ipif,
6867 			    ipif->ipif_ire_type,
6868 			    ntohl(ipif->ipif_lcl_addr)));
6869 			ire = ire_create(
6870 			    (uchar_t *)&dst_addr,	/* dest address */
6871 			    (uchar_t *)&mask,		/* mask */
6872 			    (uchar_t *)&ipif->ipif_src_addr,
6873 			    NULL,			/* no gateway */
6874 			    &ipif->ipif_mtu,
6875 			    NULL,
6876 			    ipif->ipif_rq,		/* recv-from queue */
6877 			    NULL,			/* no send-to queue */
6878 			    ipif->ipif_ire_type,	/* LOOPBACK */
6879 			    ipif,
6880 			    0,
6881 			    0,
6882 			    0,
6883 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6884 			    RTF_PRIVATE : 0,
6885 			    &ire_uinfo_null,
6886 			    NULL,
6887 			    NULL,
6888 			    ipst);
6889 
6890 			if (ire == NULL) {
6891 				if (ipif_refheld)
6892 					ipif_refrele(ipif);
6893 				return (ENOMEM);
6894 			}
6895 			error = ire_add(&ire, q, mp, func, B_FALSE);
6896 			if (error == 0)
6897 				goto save_ire;
6898 			if (ipif_refheld)
6899 				ipif_refrele(ipif);
6900 			return (error);
6901 
6902 		}
6903 	}
6904 
6905 	/*
6906 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6907 	 * and the gateway address provided is one of the system's interface
6908 	 * addresses.  By using the routing socket interface and supplying an
6909 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6910 	 * specifying an interface route to be created is available which uses
6911 	 * the interface index that specifies the outgoing interface rather than
6912 	 * the address of an outgoing interface (which may not be able to
6913 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6914 	 * flag, routes can be specified which not only specify the next-hop to
6915 	 * be used when routing to a certain prefix, but also which outgoing
6916 	 * interface should be used.
6917 	 *
6918 	 * Previously, interfaces would have unique addresses assigned to them
6919 	 * and so the address assigned to a particular interface could be used
6920 	 * to identify a particular interface.  One exception to this was the
6921 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6922 	 *
6923 	 * With the advent of IPv6 and its link-local addresses, this
6924 	 * restriction was relaxed and interfaces could share addresses between
6925 	 * themselves.  In fact, typically all of the link-local interfaces on
6926 	 * an IPv6 node or router will have the same link-local address.  In
6927 	 * order to differentiate between these interfaces, the use of an
6928 	 * interface index is necessary and this index can be carried inside a
6929 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6930 	 * of using the interface index, however, is that all of the ipif's that
6931 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6932 	 * cannot be used to differentiate between ipif's (or logical
6933 	 * interfaces) that belong to the same ill (physical interface).
6934 	 *
6935 	 * For example, in the following case involving IPv4 interfaces and
6936 	 * logical interfaces
6937 	 *
6938 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6939 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6940 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6941 	 *
6942 	 * the ipif's corresponding to each of these interface routes can be
6943 	 * uniquely identified by the "gateway" (actually interface address).
6944 	 *
6945 	 * In this case involving multiple IPv6 default routes to a particular
6946 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6947 	 * default route is of interest:
6948 	 *
6949 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6950 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6951 	 */
6952 
6953 	/* RTF_GATEWAY not set */
6954 	if (!(flags & RTF_GATEWAY)) {
6955 		queue_t	*stq;
6956 
6957 		if (sp != NULL) {
6958 			ip2dbg(("ip_rt_add: gateway security attributes "
6959 			    "cannot be set with interface route\n"));
6960 			if (ipif_refheld)
6961 				ipif_refrele(ipif);
6962 			return (EINVAL);
6963 		}
6964 
6965 		/*
6966 		 * As the interface index specified with the RTA_IFP sockaddr is
6967 		 * the same for all ipif's off of an ill, the matching logic
6968 		 * below uses MATCH_IRE_ILL if such an index was specified.
6969 		 * This means that routes sharing the same prefix when added
6970 		 * using a RTA_IFP sockaddr must have distinct interface
6971 		 * indices (namely, they must be on distinct ill's).
6972 		 *
6973 		 * On the other hand, since the gateway address will usually be
6974 		 * different for each ipif on the system, the matching logic
6975 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6976 		 * route.  This means that interface routes for the same prefix
6977 		 * can be created if they belong to distinct ipif's and if a
6978 		 * RTA_IFP sockaddr is not present.
6979 		 */
6980 		if (ipif_arg != NULL) {
6981 			if (ipif_refheld)  {
6982 				ipif_refrele(ipif);
6983 				ipif_refheld = B_FALSE;
6984 			}
6985 			ipif = ipif_arg;
6986 			match_flags |= MATCH_IRE_ILL;
6987 		} else {
6988 			/*
6989 			 * Check the ipif corresponding to the gw_addr
6990 			 */
6991 			if (ipif == NULL)
6992 				return (ENETUNREACH);
6993 			match_flags |= MATCH_IRE_IPIF;
6994 		}
6995 		ASSERT(ipif != NULL);
6996 
6997 		/*
6998 		 * We check for an existing entry at this point.
6999 		 *
7000 		 * Since a netmask isn't passed in via the ioctl interface
7001 		 * (SIOCADDRT), we don't check for a matching netmask in that
7002 		 * case.
7003 		 */
7004 		if (!ioctl_msg)
7005 			match_flags |= MATCH_IRE_MASK;
7006 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
7007 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7008 		if (ire != NULL) {
7009 			ire_refrele(ire);
7010 			if (ipif_refheld)
7011 				ipif_refrele(ipif);
7012 			return (EEXIST);
7013 		}
7014 
7015 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
7016 		    ? ipif->ipif_rq : ipif->ipif_wq;
7017 
7018 		/*
7019 		 * Create a copy of the IRE_LOOPBACK,
7020 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
7021 		 * the modified address and netmask.
7022 		 */
7023 		ire = ire_create(
7024 		    (uchar_t *)&dst_addr,
7025 		    (uint8_t *)&mask,
7026 		    (uint8_t *)&ipif->ipif_src_addr,
7027 		    NULL,
7028 		    &ipif->ipif_mtu,
7029 		    NULL,
7030 		    NULL,
7031 		    stq,
7032 		    ipif->ipif_net_type,
7033 		    ipif,
7034 		    0,
7035 		    0,
7036 		    0,
7037 		    flags,
7038 		    &ire_uinfo_null,
7039 		    NULL,
7040 		    NULL,
7041 		    ipst);
7042 		if (ire == NULL) {
7043 			if (ipif_refheld)
7044 				ipif_refrele(ipif);
7045 			return (ENOMEM);
7046 		}
7047 
7048 		/*
7049 		 * Some software (for example, GateD and Sun Cluster) attempts
7050 		 * to create (what amount to) IRE_PREFIX routes with the
7051 		 * loopback address as the gateway.  This is primarily done to
7052 		 * set up prefixes with the RTF_REJECT flag set (for example,
7053 		 * when generating aggregate routes.)
7054 		 *
7055 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7056 		 * IRE_LOOPBACK, then we map the request into a
7057 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
7058 		 * these interface routes, by definition, can only be that.
7059 		 *
7060 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7061 		 * routine, but rather using ire_create() directly.
7062 		 *
7063 		 */
7064 		if (ipif->ipif_net_type == IRE_LOOPBACK) {
7065 			ire->ire_type = IRE_IF_NORESOLVER;
7066 			ire->ire_flags |= RTF_BLACKHOLE;
7067 		}
7068 
7069 		error = ire_add(&ire, q, mp, func, B_FALSE);
7070 		if (error == 0)
7071 			goto save_ire;
7072 
7073 		/*
7074 		 * In the result of failure, ire_add() will have already
7075 		 * deleted the ire in question, so there is no need to
7076 		 * do that here.
7077 		 */
7078 		if (ipif_refheld)
7079 			ipif_refrele(ipif);
7080 		return (error);
7081 	}
7082 	if (ipif_refheld) {
7083 		ipif_refrele(ipif);
7084 		ipif_refheld = B_FALSE;
7085 	}
7086 
7087 	/*
7088 	 * Get an interface IRE for the specified gateway.
7089 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7090 	 * gateway, it is currently unreachable and we fail the request
7091 	 * accordingly.
7092 	 */
7093 	ipif = ipif_arg;
7094 	if (ipif_arg != NULL)
7095 		match_flags |= MATCH_IRE_ILL;
7096 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7097 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7098 	if (gw_ire == NULL)
7099 		return (ENETUNREACH);
7100 
7101 	/*
7102 	 * We create one of three types of IREs as a result of this request
7103 	 * based on the netmask.  A netmask of all ones (which is automatically
7104 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7105 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7106 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7107 	 * destination prefix.
7108 	 */
7109 	if (mask == IP_HOST_MASK)
7110 		type = IRE_HOST;
7111 	else if (mask == 0)
7112 		type = IRE_DEFAULT;
7113 	else
7114 		type = IRE_PREFIX;
7115 
7116 	/* check for a duplicate entry */
7117 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7118 	    NULL, ALL_ZONES, 0, NULL,
7119 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7120 	if (ire != NULL) {
7121 		ire_refrele(gw_ire);
7122 		ire_refrele(ire);
7123 		return (EEXIST);
7124 	}
7125 
7126 	/* Security attribute exists */
7127 	if (sp != NULL) {
7128 		tsol_gcgrp_addr_t ga;
7129 
7130 		/* find or create the gateway credentials group */
7131 		ga.ga_af = AF_INET;
7132 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7133 
7134 		/* we hold reference to it upon success */
7135 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7136 		if (gcgrp == NULL) {
7137 			ire_refrele(gw_ire);
7138 			return (ENOMEM);
7139 		}
7140 
7141 		/*
7142 		 * Create and add the security attribute to the group; a
7143 		 * reference to the group is made upon allocating a new
7144 		 * entry successfully.  If it finds an already-existing
7145 		 * entry for the security attribute in the group, it simply
7146 		 * returns it and no new reference is made to the group.
7147 		 */
7148 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7149 		if (gc == NULL) {
7150 			/* release reference held by gcgrp_lookup */
7151 			GCGRP_REFRELE(gcgrp);
7152 			ire_refrele(gw_ire);
7153 			return (ENOMEM);
7154 		}
7155 	}
7156 
7157 	/* Create the IRE. */
7158 	ire = ire_create(
7159 	    (uchar_t *)&dst_addr,		/* dest address */
7160 	    (uchar_t *)&mask,			/* mask */
7161 	    /* src address assigned by the caller? */
7162 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7163 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7164 	    (uchar_t *)&gw_addr,		/* gateway address */
7165 	    &gw_ire->ire_max_frag,
7166 	    NULL,				/* no src nce */
7167 	    NULL,				/* no recv-from queue */
7168 	    NULL,				/* no send-to queue */
7169 	    (ushort_t)type,			/* IRE type */
7170 	    ipif_arg,
7171 	    0,
7172 	    0,
7173 	    0,
7174 	    flags,
7175 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7176 	    gc,					/* security attribute */
7177 	    NULL,
7178 	    ipst);
7179 
7180 	/*
7181 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7182 	 * reference to the 'gcgrp'. We can now release the extra reference
7183 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7184 	 */
7185 	if (gcgrp_xtraref)
7186 		GCGRP_REFRELE(gcgrp);
7187 	if (ire == NULL) {
7188 		if (gc != NULL)
7189 			GC_REFRELE(gc);
7190 		ire_refrele(gw_ire);
7191 		return (ENOMEM);
7192 	}
7193 
7194 	/*
7195 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7196 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7197 	 */
7198 
7199 	/* Add the new IRE. */
7200 	error = ire_add(&ire, q, mp, func, B_FALSE);
7201 	if (error != 0) {
7202 		/*
7203 		 * In the result of failure, ire_add() will have already
7204 		 * deleted the ire in question, so there is no need to
7205 		 * do that here.
7206 		 */
7207 		ire_refrele(gw_ire);
7208 		return (error);
7209 	}
7210 
7211 	if (flags & RTF_MULTIRT) {
7212 		/*
7213 		 * Invoke the CGTP (multirouting) filtering module
7214 		 * to add the dst address in the filtering database.
7215 		 * Replicated inbound packets coming from that address
7216 		 * will be filtered to discard the duplicates.
7217 		 * It is not necessary to call the CGTP filter hook
7218 		 * when the dst address is a broadcast or multicast,
7219 		 * because an IP source address cannot be a broadcast
7220 		 * or a multicast.
7221 		 */
7222 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7223 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7224 		if (ire_dst != NULL) {
7225 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7226 			ire_refrele(ire_dst);
7227 			goto save_ire;
7228 		}
7229 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7230 		    !CLASSD(ire->ire_addr)) {
7231 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7232 			    ipst->ips_netstack->netstack_stackid,
7233 			    ire->ire_addr,
7234 			    ire->ire_gateway_addr,
7235 			    ire->ire_src_addr,
7236 			    gw_ire->ire_src_addr);
7237 			if (res != 0) {
7238 				ire_refrele(gw_ire);
7239 				ire_delete(ire);
7240 				return (res);
7241 			}
7242 		}
7243 	}
7244 
7245 	/*
7246 	 * Now that the prefix IRE entry has been created, delete any
7247 	 * existing gateway IRE cache entries as well as any IRE caches
7248 	 * using the gateway, and force them to be created through
7249 	 * ip_newroute.
7250 	 */
7251 	if (gc != NULL) {
7252 		ASSERT(gcgrp != NULL);
7253 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7254 	}
7255 
7256 save_ire:
7257 	if (gw_ire != NULL) {
7258 		ire_refrele(gw_ire);
7259 	}
7260 	if (ipif != NULL) {
7261 		/*
7262 		 * Save enough information so that we can recreate the IRE if
7263 		 * the interface goes down and then up.  The metrics associated
7264 		 * with the route will be saved as well when rts_setmetrics() is
7265 		 * called after the IRE has been created.  In the case where
7266 		 * memory cannot be allocated, none of this information will be
7267 		 * saved.
7268 		 */
7269 		ipif_save_ire(ipif, ire);
7270 	}
7271 	if (ioctl_msg)
7272 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7273 	if (ire_arg != NULL) {
7274 		/*
7275 		 * Store the ire that was successfully added into where ire_arg
7276 		 * points to so that callers don't have to look it up
7277 		 * themselves (but they are responsible for ire_refrele()ing
7278 		 * the ire when they are finished with it).
7279 		 */
7280 		*ire_arg = ire;
7281 	} else {
7282 		ire_refrele(ire);		/* Held in ire_add */
7283 	}
7284 	if (ipif_refheld)
7285 		ipif_refrele(ipif);
7286 	return (0);
7287 }
7288 
7289 /*
7290  * ip_rt_delete is called to delete an IPv4 route.
7291  * ipif_arg is passed in to associate it with the correct interface.
7292  * We may need to restart this operation if the ipif cannot be looked up
7293  * due to an exclusive operation that is currently in progress. The restart
7294  * entry point is specified by 'func'
7295  */
7296 /* ARGSUSED4 */
7297 int
7298 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7299     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7300     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7301 {
7302 	ire_t	*ire = NULL;
7303 	ipif_t	*ipif;
7304 	boolean_t ipif_refheld = B_FALSE;
7305 	uint_t	type;
7306 	uint_t	match_flags = MATCH_IRE_TYPE;
7307 	int	err = 0;
7308 
7309 	ip1dbg(("ip_rt_delete:"));
7310 	/*
7311 	 * If this is the case of RTF_HOST being set, then we set the netmask
7312 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7313 	 */
7314 	if (flags & RTF_HOST) {
7315 		mask = IP_HOST_MASK;
7316 		match_flags |= MATCH_IRE_MASK;
7317 	} else if (rtm_addrs & RTA_NETMASK) {
7318 		match_flags |= MATCH_IRE_MASK;
7319 	}
7320 
7321 	/*
7322 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7323 	 * we check if the gateway address is one of our interfaces first,
7324 	 * and fall back on RTF_GATEWAY routes.
7325 	 *
7326 	 * This makes it possible to delete an original
7327 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7328 	 *
7329 	 * As the interface index specified with the RTA_IFP sockaddr is the
7330 	 * same for all ipif's off of an ill, the matching logic below uses
7331 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7332 	 * sharing the same prefix and interface index as the the route
7333 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7334 	 * is specified in the request.
7335 	 *
7336 	 * On the other hand, since the gateway address will usually be
7337 	 * different for each ipif on the system, the matching logic
7338 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7339 	 * route.  This means that interface routes for the same prefix can be
7340 	 * uniquely identified if they belong to distinct ipif's and if a
7341 	 * RTA_IFP sockaddr is not present.
7342 	 *
7343 	 * For more detail on specifying routes by gateway address and by
7344 	 * interface index, see the comments in ip_rt_add().
7345 	 */
7346 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7347 	    ipst);
7348 	if (ipif != NULL)
7349 		ipif_refheld = B_TRUE;
7350 	else if (err == EINPROGRESS)
7351 		return (err);
7352 	else
7353 		err = 0;
7354 	if (ipif != NULL) {
7355 		if (ipif_arg != NULL) {
7356 			if (ipif_refheld) {
7357 				ipif_refrele(ipif);
7358 				ipif_refheld = B_FALSE;
7359 			}
7360 			ipif = ipif_arg;
7361 			match_flags |= MATCH_IRE_ILL;
7362 		} else {
7363 			match_flags |= MATCH_IRE_IPIF;
7364 		}
7365 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7366 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7367 			    ALL_ZONES, NULL, match_flags, ipst);
7368 		}
7369 		if (ire == NULL) {
7370 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7371 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7372 			    match_flags, ipst);
7373 		}
7374 	}
7375 
7376 	if (ire == NULL) {
7377 		/*
7378 		 * At this point, the gateway address is not one of our own
7379 		 * addresses or a matching interface route was not found.  We
7380 		 * set the IRE type to lookup based on whether
7381 		 * this is a host route, a default route or just a prefix.
7382 		 *
7383 		 * If an ipif_arg was passed in, then the lookup is based on an
7384 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7385 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7386 		 * set as the route being looked up is not a traditional
7387 		 * interface route.
7388 		 */
7389 		match_flags &= ~MATCH_IRE_IPIF;
7390 		match_flags |= MATCH_IRE_GW;
7391 		if (ipif_arg != NULL)
7392 			match_flags |= MATCH_IRE_ILL;
7393 		if (mask == IP_HOST_MASK)
7394 			type = IRE_HOST;
7395 		else if (mask == 0)
7396 			type = IRE_DEFAULT;
7397 		else
7398 			type = IRE_PREFIX;
7399 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7400 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7401 	}
7402 
7403 	if (ipif_refheld)
7404 		ipif_refrele(ipif);
7405 
7406 	/* ipif is not refheld anymore */
7407 	if (ire == NULL)
7408 		return (ESRCH);
7409 
7410 	if (ire->ire_flags & RTF_MULTIRT) {
7411 		/*
7412 		 * Invoke the CGTP (multirouting) filtering module
7413 		 * to remove the dst address from the filtering database.
7414 		 * Packets coming from that address will no longer be
7415 		 * filtered to remove duplicates.
7416 		 */
7417 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7418 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7419 			    ipst->ips_netstack->netstack_stackid,
7420 			    ire->ire_addr, ire->ire_gateway_addr);
7421 		}
7422 		ip_cgtp_bcast_delete(ire, ipst);
7423 	}
7424 
7425 	ipif = ire->ire_ipif;
7426 	if (ipif != NULL)
7427 		ipif_remove_ire(ipif, ire);
7428 	if (ioctl_msg)
7429 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7430 	ire_delete(ire);
7431 	ire_refrele(ire);
7432 	return (err);
7433 }
7434 
7435 /*
7436  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7437  */
7438 /* ARGSUSED */
7439 int
7440 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7441     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7442 {
7443 	ipaddr_t dst_addr;
7444 	ipaddr_t gw_addr;
7445 	ipaddr_t mask;
7446 	int error = 0;
7447 	mblk_t *mp1;
7448 	struct rtentry *rt;
7449 	ipif_t *ipif = NULL;
7450 	ip_stack_t	*ipst;
7451 
7452 	ASSERT(q->q_next == NULL);
7453 	ipst = CONNQ_TO_IPST(q);
7454 
7455 	ip1dbg(("ip_siocaddrt:"));
7456 	/* Existence of mp1 verified in ip_wput_nondata */
7457 	mp1 = mp->b_cont->b_cont;
7458 	rt = (struct rtentry *)mp1->b_rptr;
7459 
7460 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7461 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7462 
7463 	/*
7464 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7465 	 * to a particular host address.  In this case, we set the netmask to
7466 	 * all ones for the particular destination address.  Otherwise,
7467 	 * determine the netmask to be used based on dst_addr and the interfaces
7468 	 * in use.
7469 	 */
7470 	if (rt->rt_flags & RTF_HOST) {
7471 		mask = IP_HOST_MASK;
7472 	} else {
7473 		/*
7474 		 * Note that ip_subnet_mask returns a zero mask in the case of
7475 		 * default (an all-zeroes address).
7476 		 */
7477 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7478 	}
7479 
7480 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7481 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7482 	if (ipif != NULL)
7483 		ipif_refrele(ipif);
7484 	return (error);
7485 }
7486 
7487 /*
7488  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7489  */
7490 /* ARGSUSED */
7491 int
7492 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7493     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7494 {
7495 	ipaddr_t dst_addr;
7496 	ipaddr_t gw_addr;
7497 	ipaddr_t mask;
7498 	int error;
7499 	mblk_t *mp1;
7500 	struct rtentry *rt;
7501 	ipif_t *ipif = NULL;
7502 	ip_stack_t	*ipst;
7503 
7504 	ASSERT(q->q_next == NULL);
7505 	ipst = CONNQ_TO_IPST(q);
7506 
7507 	ip1dbg(("ip_siocdelrt:"));
7508 	/* Existence of mp1 verified in ip_wput_nondata */
7509 	mp1 = mp->b_cont->b_cont;
7510 	rt = (struct rtentry *)mp1->b_rptr;
7511 
7512 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7513 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7514 
7515 	/*
7516 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7517 	 * to a particular host address.  In this case, we set the netmask to
7518 	 * all ones for the particular destination address.  Otherwise,
7519 	 * determine the netmask to be used based on dst_addr and the interfaces
7520 	 * in use.
7521 	 */
7522 	if (rt->rt_flags & RTF_HOST) {
7523 		mask = IP_HOST_MASK;
7524 	} else {
7525 		/*
7526 		 * Note that ip_subnet_mask returns a zero mask in the case of
7527 		 * default (an all-zeroes address).
7528 		 */
7529 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7530 	}
7531 
7532 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7533 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7534 	    mp, ip_process_ioctl, ipst);
7535 	if (ipif != NULL)
7536 		ipif_refrele(ipif);
7537 	return (error);
7538 }
7539 
7540 /*
7541  * Enqueue the mp onto the ipsq, chained by b_next.
7542  * b_prev stores the function to be executed later, and b_queue the queue
7543  * where this mp originated.
7544  */
7545 void
7546 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7547     ill_t *pending_ill)
7548 {
7549 	conn_t	*connp = NULL;
7550 
7551 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7552 	ASSERT(func != NULL);
7553 
7554 	mp->b_queue = q;
7555 	mp->b_prev = (void *)func;
7556 	mp->b_next = NULL;
7557 
7558 	switch (type) {
7559 	case CUR_OP:
7560 		if (ipsq->ipsq_mptail != NULL) {
7561 			ASSERT(ipsq->ipsq_mphead != NULL);
7562 			ipsq->ipsq_mptail->b_next = mp;
7563 		} else {
7564 			ASSERT(ipsq->ipsq_mphead == NULL);
7565 			ipsq->ipsq_mphead = mp;
7566 		}
7567 		ipsq->ipsq_mptail = mp;
7568 		break;
7569 
7570 	case NEW_OP:
7571 		if (ipsq->ipsq_xopq_mptail != NULL) {
7572 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7573 			ipsq->ipsq_xopq_mptail->b_next = mp;
7574 		} else {
7575 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7576 			ipsq->ipsq_xopq_mphead = mp;
7577 		}
7578 		ipsq->ipsq_xopq_mptail = mp;
7579 		break;
7580 	default:
7581 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7582 	}
7583 
7584 	if (CONN_Q(q) && pending_ill != NULL) {
7585 		connp = Q_TO_CONN(q);
7586 
7587 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7588 		connp->conn_oper_pending_ill = pending_ill;
7589 	}
7590 }
7591 
7592 /*
7593  * Return the mp at the head of the ipsq. After emptying the ipsq
7594  * look at the next ioctl, if this ioctl is complete. Otherwise
7595  * return, we will resume when we complete the current ioctl.
7596  * The current ioctl will wait till it gets a response from the
7597  * driver below.
7598  */
7599 static mblk_t *
7600 ipsq_dq(ipsq_t *ipsq)
7601 {
7602 	mblk_t	*mp;
7603 
7604 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7605 
7606 	mp = ipsq->ipsq_mphead;
7607 	if (mp != NULL) {
7608 		ipsq->ipsq_mphead = mp->b_next;
7609 		if (ipsq->ipsq_mphead == NULL)
7610 			ipsq->ipsq_mptail = NULL;
7611 		mp->b_next = NULL;
7612 		return (mp);
7613 	}
7614 	if (ipsq->ipsq_current_ipif != NULL)
7615 		return (NULL);
7616 	mp = ipsq->ipsq_xopq_mphead;
7617 	if (mp != NULL) {
7618 		ipsq->ipsq_xopq_mphead = mp->b_next;
7619 		if (ipsq->ipsq_xopq_mphead == NULL)
7620 			ipsq->ipsq_xopq_mptail = NULL;
7621 		mp->b_next = NULL;
7622 		return (mp);
7623 	}
7624 	return (NULL);
7625 }
7626 
7627 /*
7628  * Enter the ipsq corresponding to ill, by waiting synchronously till
7629  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7630  * will have to drain completely before ipsq_enter returns success.
7631  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7632  * and the ipsq_exit logic will start the next enqueued ioctl after
7633  * completion of the current ioctl. If 'force' is used, we don't wait
7634  * for the enqueued ioctls. This is needed when a conn_close wants to
7635  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7636  * of an ill can also use this option. But we dont' use it currently.
7637  */
7638 #define	ENTER_SQ_WAIT_TICKS 100
7639 boolean_t
7640 ipsq_enter(ill_t *ill, boolean_t force)
7641 {
7642 	ipsq_t	*ipsq;
7643 	boolean_t waited_enough = B_FALSE;
7644 
7645 	/*
7646 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7647 	 * Since the <ill-ipsq> assocs could change while we wait for the
7648 	 * writer, it is easier to wait on a fixed global rather than try to
7649 	 * cv_wait on a changing ipsq.
7650 	 */
7651 	mutex_enter(&ill->ill_lock);
7652 	for (;;) {
7653 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7654 			mutex_exit(&ill->ill_lock);
7655 			return (B_FALSE);
7656 		}
7657 
7658 		ipsq = ill->ill_phyint->phyint_ipsq;
7659 		mutex_enter(&ipsq->ipsq_lock);
7660 		if (ipsq->ipsq_writer == NULL &&
7661 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7662 			break;
7663 		} else if (ipsq->ipsq_writer != NULL) {
7664 			mutex_exit(&ipsq->ipsq_lock);
7665 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7666 		} else {
7667 			mutex_exit(&ipsq->ipsq_lock);
7668 			if (force) {
7669 				(void) cv_timedwait(&ill->ill_cv,
7670 				    &ill->ill_lock,
7671 				    lbolt + ENTER_SQ_WAIT_TICKS);
7672 				waited_enough = B_TRUE;
7673 				continue;
7674 			} else {
7675 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7676 			}
7677 		}
7678 	}
7679 
7680 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7681 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7682 	ipsq->ipsq_writer = curthread;
7683 	ipsq->ipsq_reentry_cnt++;
7684 #ifdef DEBUG
7685 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IPSQ_STACK_DEPTH);
7686 #endif
7687 	mutex_exit(&ipsq->ipsq_lock);
7688 	mutex_exit(&ill->ill_lock);
7689 	return (B_TRUE);
7690 }
7691 
7692 /*
7693  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7694  * certain critical operations like plumbing (i.e. most set ioctls),
7695  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7696  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7697  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7698  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7699  * threads executing in the ipsq. Responses from the driver pertain to the
7700  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7701  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7702  *
7703  * If a thread does not want to reenter the ipsq when it is already writer,
7704  * it must make sure that the specified reentry point to be called later
7705  * when the ipsq is empty, nor any code path starting from the specified reentry
7706  * point must never ever try to enter the ipsq again. Otherwise it can lead
7707  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7708  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7709  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7710  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7711  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7712  * ioctl if the current ioctl has completed. If the current ioctl is still
7713  * in progress it simply returns. The current ioctl could be waiting for
7714  * a response from another module (arp_ or the driver or could be waiting for
7715  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7716  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7717  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7718  * ipsq_current_ipif is clear which happens only on ioctl completion.
7719  */
7720 
7721 /*
7722  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7723  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7724  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7725  * completion.
7726  */
7727 ipsq_t *
7728 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7729     ipsq_func_t func, int type, boolean_t reentry_ok)
7730 {
7731 	ipsq_t	*ipsq;
7732 
7733 	/* Only 1 of ipif or ill can be specified */
7734 	ASSERT((ipif != NULL) ^ (ill != NULL));
7735 	if (ipif != NULL)
7736 		ill = ipif->ipif_ill;
7737 
7738 	/*
7739 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7740 	 * ipsq of an ill can't change when ill_lock is held.
7741 	 */
7742 	GRAB_CONN_LOCK(q);
7743 	mutex_enter(&ill->ill_lock);
7744 	ipsq = ill->ill_phyint->phyint_ipsq;
7745 	mutex_enter(&ipsq->ipsq_lock);
7746 
7747 	/*
7748 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7749 	 *    (Note: If the caller does not specify reentry_ok then neither
7750 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7751 	 *    again. Otherwise it can lead to an infinite loop
7752 	 * 2. Enter the ipsq if there is no current writer and this attempted
7753 	 *    entry is part of the current ioctl or operation
7754 	 * 3. Enter the ipsq if there is no current writer and this is a new
7755 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7756 	 *    empty and there is no ioctl (or operation) currently in progress
7757 	 */
7758 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7759 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7760 	    ipsq->ipsq_current_ipif == NULL))) ||
7761 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7762 		/* Success. */
7763 		ipsq->ipsq_reentry_cnt++;
7764 		ipsq->ipsq_writer = curthread;
7765 		mutex_exit(&ipsq->ipsq_lock);
7766 		mutex_exit(&ill->ill_lock);
7767 		RELEASE_CONN_LOCK(q);
7768 #ifdef DEBUG
7769 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack,
7770 		    IPSQ_STACK_DEPTH);
7771 #endif
7772 		return (ipsq);
7773 	}
7774 
7775 	ipsq_enq(ipsq, q, mp, func, type, ill);
7776 
7777 	mutex_exit(&ipsq->ipsq_lock);
7778 	mutex_exit(&ill->ill_lock);
7779 	RELEASE_CONN_LOCK(q);
7780 	return (NULL);
7781 }
7782 
7783 /*
7784  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7785  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7786  * cannot be entered, the mp is queued for completion.
7787  */
7788 void
7789 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7790     boolean_t reentry_ok)
7791 {
7792 	ipsq_t	*ipsq;
7793 
7794 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7795 
7796 	/*
7797 	 * Drop the caller's refhold on the ill.  This is safe since we either
7798 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7799 	 * IPSQ, in which case we return without accessing ill anymore.  This
7800 	 * is needed because func needs to see the correct refcount.
7801 	 * e.g. removeif can work only then.
7802 	 */
7803 	ill_refrele(ill);
7804 	if (ipsq != NULL) {
7805 		(*func)(ipsq, q, mp, NULL);
7806 		ipsq_exit(ipsq);
7807 	}
7808 }
7809 
7810 /*
7811  * If there are more than ILL_GRP_CNT ills in a group,
7812  * we use kmem alloc'd buffers, else use the stack
7813  */
7814 #define	ILL_GRP_CNT	14
7815 /*
7816  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7817  * Called by a thread that is currently exclusive on this ipsq.
7818  */
7819 void
7820 ipsq_exit(ipsq_t *ipsq)
7821 {
7822 	queue_t	*q;
7823 	mblk_t	*mp;
7824 	ipsq_func_t	func;
7825 	int	next;
7826 	ill_t	**ill_list = NULL;
7827 	size_t	ill_list_size = 0;
7828 	int	cnt = 0;
7829 	boolean_t need_ipsq_free = B_FALSE;
7830 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7831 
7832 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7833 	mutex_enter(&ipsq->ipsq_lock);
7834 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7835 	if (ipsq->ipsq_reentry_cnt != 1) {
7836 		ipsq->ipsq_reentry_cnt--;
7837 		mutex_exit(&ipsq->ipsq_lock);
7838 		return;
7839 	}
7840 
7841 	mp = ipsq_dq(ipsq);
7842 	while (mp != NULL) {
7843 again:
7844 		mutex_exit(&ipsq->ipsq_lock);
7845 		func = (ipsq_func_t)mp->b_prev;
7846 		q = (queue_t *)mp->b_queue;
7847 		mp->b_prev = NULL;
7848 		mp->b_queue = NULL;
7849 
7850 		/*
7851 		 * If 'q' is an conn queue, it is valid, since we did a
7852 		 * a refhold on the connp, at the start of the ioctl.
7853 		 * If 'q' is an ill queue, it is valid, since close of an
7854 		 * ill will clean up the 'ipsq'.
7855 		 */
7856 		(*func)(ipsq, q, mp, NULL);
7857 
7858 		mutex_enter(&ipsq->ipsq_lock);
7859 		mp = ipsq_dq(ipsq);
7860 	}
7861 
7862 	mutex_exit(&ipsq->ipsq_lock);
7863 
7864 	/*
7865 	 * Need to grab the locks in the right order. Need to
7866 	 * atomically check (under ipsq_lock) that there are no
7867 	 * messages before relinquishing the ipsq. Also need to
7868 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7869 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7870 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7871 	 * to grab ill_g_lock as writer.
7872 	 */
7873 	rw_enter(&ipst->ips_ill_g_lock,
7874 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7875 
7876 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7877 	if (ipsq->ipsq_refs != 0) {
7878 		/* At most 2 ills v4/v6 per phyint */
7879 		cnt = ipsq->ipsq_refs << 1;
7880 		ill_list_size = cnt * sizeof (ill_t *);
7881 		/*
7882 		 * If memory allocation fails, we will do the split
7883 		 * the next time ipsq_exit is called for whatever reason.
7884 		 * As long as the ipsq_split flag is set the need to
7885 		 * split is remembered.
7886 		 */
7887 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7888 		if (ill_list != NULL)
7889 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7890 	}
7891 	mutex_enter(&ipsq->ipsq_lock);
7892 	mp = ipsq_dq(ipsq);
7893 	if (mp != NULL) {
7894 		/* oops, some message has landed up, we can't get out */
7895 		if (ill_list != NULL)
7896 			ill_unlock_ills(ill_list, cnt);
7897 		rw_exit(&ipst->ips_ill_g_lock);
7898 		if (ill_list != NULL)
7899 			kmem_free(ill_list, ill_list_size);
7900 		ill_list = NULL;
7901 		ill_list_size = 0;
7902 		cnt = 0;
7903 		goto again;
7904 	}
7905 
7906 	/*
7907 	 * Split only if no ioctl is pending and if memory alloc succeeded
7908 	 * above.
7909 	 */
7910 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7911 	    ill_list != NULL) {
7912 		/*
7913 		 * No new ill can join this ipsq since we are holding the
7914 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7915 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7916 		 * If so we will retry on the next ipsq_exit.
7917 		 */
7918 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7919 	}
7920 
7921 	/*
7922 	 * We are holding the ipsq lock, hence no new messages can
7923 	 * land up on the ipsq, and there are no messages currently.
7924 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7925 	 * atomically while holding ill locks.
7926 	 */
7927 	ipsq->ipsq_writer = NULL;
7928 	ipsq->ipsq_reentry_cnt--;
7929 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7930 #ifdef DEBUG
7931 	ipsq->ipsq_depth = 0;
7932 #endif
7933 	mutex_exit(&ipsq->ipsq_lock);
7934 	/*
7935 	 * For IPMP this should wake up all ills in this ipsq.
7936 	 * We need to hold the ill_lock while waking up waiters to
7937 	 * avoid missed wakeups. But there is no need to acquire all
7938 	 * the ill locks and then wakeup. If we have not acquired all
7939 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7940 	 * wakes up ills one at a time after getting the right ill_lock
7941 	 */
7942 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7943 	if (ill_list != NULL)
7944 		ill_unlock_ills(ill_list, cnt);
7945 	if (ipsq->ipsq_refs == 0)
7946 		need_ipsq_free = B_TRUE;
7947 	rw_exit(&ipst->ips_ill_g_lock);
7948 	if (ill_list != 0)
7949 		kmem_free(ill_list, ill_list_size);
7950 
7951 	if (need_ipsq_free) {
7952 		/*
7953 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7954 		 * looked up. ipsq can be looked up only thru ill or phyint
7955 		 * and there are no ills/phyint on this ipsq.
7956 		 */
7957 		ipsq_delete(ipsq);
7958 	}
7959 
7960 	/*
7961 	 * Now that we're outside the IPSQ, start any IGMP/MLD timers.  We
7962 	 * can't start these inside the IPSQ since e.g. igmp_start_timers() ->
7963 	 * untimeout() (inside the IPSQ, waiting for an executing timeout to
7964 	 * finish) could deadlock with igmp_timeout_handler() -> ipsq_enter()
7965 	 * (executing the timeout, waiting to get inside the IPSQ).
7966 	 *
7967 	 * However, there is one exception to the above: if this thread *is*
7968 	 * the IGMP/MLD timeout handler thread, then we must not start its
7969 	 * timer until the current handler is done.
7970 	 */
7971 	mutex_enter(&ipst->ips_igmp_timer_lock);
7972 	if (curthread != ipst->ips_igmp_timer_thread) {
7973 		next = ipst->ips_igmp_deferred_next;
7974 		ipst->ips_igmp_deferred_next = INFINITY;
7975 		mutex_exit(&ipst->ips_igmp_timer_lock);
7976 
7977 		if (next != INFINITY)
7978 			igmp_start_timers(next, ipst);
7979 	} else {
7980 		mutex_exit(&ipst->ips_igmp_timer_lock);
7981 	}
7982 
7983 	mutex_enter(&ipst->ips_mld_timer_lock);
7984 	if (curthread != ipst->ips_mld_timer_thread) {
7985 		next = ipst->ips_mld_deferred_next;
7986 		ipst->ips_mld_deferred_next = INFINITY;
7987 		mutex_exit(&ipst->ips_mld_timer_lock);
7988 
7989 		if (next != INFINITY)
7990 			mld_start_timers(next, ipst);
7991 	} else {
7992 		mutex_exit(&ipst->ips_mld_timer_lock);
7993 	}
7994 }
7995 
7996 /*
7997  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
7998  * and `ioccmd'.
7999  */
8000 void
8001 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
8002 {
8003 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8004 
8005 	mutex_enter(&ipsq->ipsq_lock);
8006 	ASSERT(ipsq->ipsq_current_ipif == NULL);
8007 	ASSERT(ipsq->ipsq_current_ioctl == 0);
8008 	ipsq->ipsq_current_done = B_FALSE;
8009 	ipsq->ipsq_current_ipif = ipif;
8010 	ipsq->ipsq_current_ioctl = ioccmd;
8011 	mutex_exit(&ipsq->ipsq_lock);
8012 }
8013 
8014 /*
8015  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
8016  * the next exclusive operation to begin once we ipsq_exit().  However, if
8017  * pending DLPI operations remain, then we will wait for the queue to drain
8018  * before allowing the next exclusive operation to begin.  This ensures that
8019  * DLPI operations from one exclusive operation are never improperly processed
8020  * as part of a subsequent exclusive operation.
8021  */
8022 void
8023 ipsq_current_finish(ipsq_t *ipsq)
8024 {
8025 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8026 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
8027 
8028 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8029 
8030 	/*
8031 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8032 	 * (but in that case, IPIF_CHANGING will already be clear and no
8033 	 * pending DLPI messages can remain).
8034 	 */
8035 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8036 		ill_t *ill = ipif->ipif_ill;
8037 
8038 		mutex_enter(&ill->ill_lock);
8039 		dlpi_pending = ill->ill_dlpi_pending;
8040 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8041 		/* Send any queued event */
8042 		ill_nic_info_dispatch(ill);
8043 		mutex_exit(&ill->ill_lock);
8044 	}
8045 
8046 	mutex_enter(&ipsq->ipsq_lock);
8047 	ipsq->ipsq_current_ioctl = 0;
8048 	ipsq->ipsq_current_done = B_TRUE;
8049 	if (dlpi_pending == DL_PRIM_INVAL)
8050 		ipsq->ipsq_current_ipif = NULL;
8051 	mutex_exit(&ipsq->ipsq_lock);
8052 }
8053 
8054 /*
8055  * The ill is closing. Flush all messages on the ipsq that originated
8056  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8057  * for this ill since ipsq_enter could not have entered until then.
8058  * New messages can't be queued since the CONDEMNED flag is set.
8059  */
8060 static void
8061 ipsq_flush(ill_t *ill)
8062 {
8063 	queue_t	*q;
8064 	mblk_t	*prev;
8065 	mblk_t	*mp;
8066 	mblk_t	*mp_next;
8067 	ipsq_t	*ipsq;
8068 
8069 	ASSERT(IAM_WRITER_ILL(ill));
8070 	ipsq = ill->ill_phyint->phyint_ipsq;
8071 	/*
8072 	 * Flush any messages sent up by the driver.
8073 	 */
8074 	mutex_enter(&ipsq->ipsq_lock);
8075 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8076 		mp_next = mp->b_next;
8077 		q = mp->b_queue;
8078 		if (q == ill->ill_rq || q == ill->ill_wq) {
8079 			/* Remove the mp from the ipsq */
8080 			if (prev == NULL)
8081 				ipsq->ipsq_mphead = mp->b_next;
8082 			else
8083 				prev->b_next = mp->b_next;
8084 			if (ipsq->ipsq_mptail == mp) {
8085 				ASSERT(mp_next == NULL);
8086 				ipsq->ipsq_mptail = prev;
8087 			}
8088 			inet_freemsg(mp);
8089 		} else {
8090 			prev = mp;
8091 		}
8092 	}
8093 	mutex_exit(&ipsq->ipsq_lock);
8094 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8095 	ipsq_xopq_mp_cleanup(ill, NULL);
8096 	ill_pending_mp_cleanup(ill);
8097 }
8098 
8099 /* ARGSUSED */
8100 int
8101 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8102     ip_ioctl_cmd_t *ipip, void *ifreq)
8103 {
8104 	ill_t	*ill;
8105 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8106 	boolean_t isv6;
8107 	conn_t	*connp;
8108 	ip_stack_t	*ipst;
8109 
8110 	connp = Q_TO_CONN(q);
8111 	ipst = connp->conn_netstack->netstack_ip;
8112 	isv6 = connp->conn_af_isv6;
8113 	/*
8114 	 * Set original index.
8115 	 * Failover and failback move logical interfaces
8116 	 * from one physical interface to another.  The
8117 	 * original index indicates the parent of a logical
8118 	 * interface, in other words, the physical interface
8119 	 * the logical interface will be moved back to on
8120 	 * failback.
8121 	 */
8122 
8123 	/*
8124 	 * Don't allow the original index to be changed
8125 	 * for non-failover addresses, autoconfigured
8126 	 * addresses, or IPv6 link local addresses.
8127 	 */
8128 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8129 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8130 		return (EINVAL);
8131 	}
8132 	/*
8133 	 * The new original index must be in use by some
8134 	 * physical interface.
8135 	 */
8136 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8137 	    NULL, NULL, ipst);
8138 	if (ill == NULL)
8139 		return (ENXIO);
8140 	ill_refrele(ill);
8141 
8142 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8143 	/*
8144 	 * When this ipif gets failed back, don't
8145 	 * preserve the original id, as it is no
8146 	 * longer applicable.
8147 	 */
8148 	ipif->ipif_orig_ipifid = 0;
8149 	/*
8150 	 * For IPv4, change the original index of any
8151 	 * multicast addresses associated with the
8152 	 * ipif to the new value.
8153 	 */
8154 	if (!isv6) {
8155 		ilm_t *ilm;
8156 
8157 		mutex_enter(&ipif->ipif_ill->ill_lock);
8158 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8159 		    ilm = ilm->ilm_next) {
8160 			if (ilm->ilm_ipif == ipif) {
8161 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8162 			}
8163 		}
8164 		mutex_exit(&ipif->ipif_ill->ill_lock);
8165 	}
8166 	return (0);
8167 }
8168 
8169 /* ARGSUSED */
8170 int
8171 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8172     ip_ioctl_cmd_t *ipip, void *ifreq)
8173 {
8174 	struct lifreq *lifr = (struct lifreq *)ifreq;
8175 
8176 	/*
8177 	 * Get the original interface index i.e the one
8178 	 * before FAILOVER if it ever happened.
8179 	 */
8180 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8181 	return (0);
8182 }
8183 
8184 /*
8185  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8186  * refhold and return the associated ipif
8187  */
8188 /* ARGSUSED */
8189 int
8190 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8191     cmd_info_t *ci, ipsq_func_t func)
8192 {
8193 	boolean_t exists;
8194 	struct iftun_req *ta;
8195 	ipif_t	*ipif;
8196 	ill_t	*ill;
8197 	boolean_t isv6;
8198 	mblk_t	*mp1;
8199 	int	error;
8200 	conn_t	*connp;
8201 	ip_stack_t	*ipst;
8202 
8203 	/* Existence verified in ip_wput_nondata */
8204 	mp1 = mp->b_cont->b_cont;
8205 	ta = (struct iftun_req *)mp1->b_rptr;
8206 	/*
8207 	 * Null terminate the string to protect against buffer
8208 	 * overrun. String was generated by user code and may not
8209 	 * be trusted.
8210 	 */
8211 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8212 
8213 	connp = Q_TO_CONN(q);
8214 	isv6 = connp->conn_af_isv6;
8215 	ipst = connp->conn_netstack->netstack_ip;
8216 
8217 	/* Disallows implicit create */
8218 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8219 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8220 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8221 	if (ipif == NULL)
8222 		return (error);
8223 
8224 	if (ipif->ipif_id != 0) {
8225 		/*
8226 		 * We really don't want to set/get tunnel parameters
8227 		 * on virtual tunnel interfaces.  Only allow the
8228 		 * base tunnel to do these.
8229 		 */
8230 		ipif_refrele(ipif);
8231 		return (EINVAL);
8232 	}
8233 
8234 	/*
8235 	 * Send down to tunnel mod for ioctl processing.
8236 	 * Will finish ioctl in ip_rput_other().
8237 	 */
8238 	ill = ipif->ipif_ill;
8239 	if (ill->ill_net_type == IRE_LOOPBACK) {
8240 		ipif_refrele(ipif);
8241 		return (EOPNOTSUPP);
8242 	}
8243 
8244 	if (ill->ill_wq == NULL) {
8245 		ipif_refrele(ipif);
8246 		return (ENXIO);
8247 	}
8248 	/*
8249 	 * Mark the ioctl as coming from an IPv6 interface for
8250 	 * tun's convenience.
8251 	 */
8252 	if (ill->ill_isv6)
8253 		ta->ifta_flags |= 0x80000000;
8254 	ci->ci_ipif = ipif;
8255 	return (0);
8256 }
8257 
8258 /*
8259  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8260  * and return the associated ipif.
8261  * Return value:
8262  *	Non zero: An error has occurred. ci may not be filled out.
8263  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8264  *	a held ipif in ci.ci_ipif.
8265  */
8266 int
8267 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8268     cmd_info_t *ci, ipsq_func_t func)
8269 {
8270 	sin_t		*sin;
8271 	sin6_t		*sin6;
8272 	char		*name;
8273 	struct ifreq    *ifr;
8274 	struct lifreq    *lifr;
8275 	ipif_t		*ipif = NULL;
8276 	ill_t		*ill;
8277 	conn_t		*connp;
8278 	boolean_t	isv6;
8279 	boolean_t	exists;
8280 	int		err;
8281 	mblk_t		*mp1;
8282 	zoneid_t	zoneid;
8283 	ip_stack_t	*ipst;
8284 
8285 	if (q->q_next != NULL) {
8286 		ill = (ill_t *)q->q_ptr;
8287 		isv6 = ill->ill_isv6;
8288 		connp = NULL;
8289 		zoneid = ALL_ZONES;
8290 		ipst = ill->ill_ipst;
8291 	} else {
8292 		ill = NULL;
8293 		connp = Q_TO_CONN(q);
8294 		isv6 = connp->conn_af_isv6;
8295 		zoneid = connp->conn_zoneid;
8296 		if (zoneid == GLOBAL_ZONEID) {
8297 			/* global zone can access ipifs in all zones */
8298 			zoneid = ALL_ZONES;
8299 		}
8300 		ipst = connp->conn_netstack->netstack_ip;
8301 	}
8302 
8303 	/* Has been checked in ip_wput_nondata */
8304 	mp1 = mp->b_cont->b_cont;
8305 
8306 	if (ipip->ipi_cmd_type == IF_CMD) {
8307 		/* This a old style SIOC[GS]IF* command */
8308 		ifr = (struct ifreq *)mp1->b_rptr;
8309 		/*
8310 		 * Null terminate the string to protect against buffer
8311 		 * overrun. String was generated by user code and may not
8312 		 * be trusted.
8313 		 */
8314 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8315 		sin = (sin_t *)&ifr->ifr_addr;
8316 		name = ifr->ifr_name;
8317 		ci->ci_sin = sin;
8318 		ci->ci_sin6 = NULL;
8319 		ci->ci_lifr = (struct lifreq *)ifr;
8320 	} else {
8321 		/* This a new style SIOC[GS]LIF* command */
8322 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8323 		lifr = (struct lifreq *)mp1->b_rptr;
8324 		/*
8325 		 * Null terminate the string to protect against buffer
8326 		 * overrun. String was generated by user code and may not
8327 		 * be trusted.
8328 		 */
8329 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8330 		name = lifr->lifr_name;
8331 		sin = (sin_t *)&lifr->lifr_addr;
8332 		sin6 = (sin6_t *)&lifr->lifr_addr;
8333 		if (ipip->ipi_cmd == SIOCSLIFGROUPNAME) {
8334 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8335 			    LIFNAMSIZ);
8336 		}
8337 		ci->ci_sin = sin;
8338 		ci->ci_sin6 = sin6;
8339 		ci->ci_lifr = lifr;
8340 	}
8341 
8342 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8343 		/*
8344 		 * The ioctl will be failed if the ioctl comes down
8345 		 * an conn stream
8346 		 */
8347 		if (ill == NULL) {
8348 			/*
8349 			 * Not an ill queue, return EINVAL same as the
8350 			 * old error code.
8351 			 */
8352 			return (ENXIO);
8353 		}
8354 		ipif = ill->ill_ipif;
8355 		ipif_refhold(ipif);
8356 	} else {
8357 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8358 		    &exists, isv6, zoneid,
8359 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8360 		    ipst);
8361 		if (ipif == NULL) {
8362 			if (err == EINPROGRESS)
8363 				return (err);
8364 			if (ipip->ipi_cmd == SIOCLIFFAILOVER ||
8365 			    ipip->ipi_cmd == SIOCLIFFAILBACK) {
8366 				/*
8367 				 * Need to try both v4 and v6 since this
8368 				 * ioctl can come down either v4 or v6
8369 				 * socket. The lifreq.lifr_family passed
8370 				 * down by this ioctl is AF_UNSPEC.
8371 				 */
8372 				ipif = ipif_lookup_on_name(name,
8373 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8374 				    zoneid, (connp == NULL) ? q :
8375 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8376 				if (err == EINPROGRESS)
8377 					return (err);
8378 			}
8379 			err = 0;	/* Ensure we don't use it below */
8380 		}
8381 	}
8382 
8383 	/*
8384 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8385 	 */
8386 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8387 		ipif_refrele(ipif);
8388 		return (ENXIO);
8389 	}
8390 
8391 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8392 	    name[0] == '\0') {
8393 		/*
8394 		 * Handle a or a SIOC?IF* with a null name
8395 		 * during plumb (on the ill queue before the I_PLINK).
8396 		 */
8397 		ipif = ill->ill_ipif;
8398 		ipif_refhold(ipif);
8399 	}
8400 
8401 	if (ipif == NULL)
8402 		return (ENXIO);
8403 
8404 	/*
8405 	 * Allow only GET operations if this ipif has been created
8406 	 * temporarily due to a MOVE operation.
8407 	 */
8408 	if (ipif->ipif_replace_zero && !(ipip->ipi_flags & IPI_REPL)) {
8409 		ipif_refrele(ipif);
8410 		return (EINVAL);
8411 	}
8412 
8413 	ci->ci_ipif = ipif;
8414 	return (0);
8415 }
8416 
8417 /*
8418  * Return the total number of ipifs.
8419  */
8420 static uint_t
8421 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8422 {
8423 	uint_t numifs = 0;
8424 	ill_t	*ill;
8425 	ill_walk_context_t	ctx;
8426 	ipif_t	*ipif;
8427 
8428 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8429 	ill = ILL_START_WALK_V4(&ctx, ipst);
8430 
8431 	while (ill != NULL) {
8432 		for (ipif = ill->ill_ipif; ipif != NULL;
8433 		    ipif = ipif->ipif_next) {
8434 			if (ipif->ipif_zoneid == zoneid ||
8435 			    ipif->ipif_zoneid == ALL_ZONES)
8436 				numifs++;
8437 		}
8438 		ill = ill_next(&ctx, ill);
8439 	}
8440 	rw_exit(&ipst->ips_ill_g_lock);
8441 	return (numifs);
8442 }
8443 
8444 /*
8445  * Return the total number of ipifs.
8446  */
8447 static uint_t
8448 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8449 {
8450 	uint_t numifs = 0;
8451 	ill_t	*ill;
8452 	ipif_t	*ipif;
8453 	ill_walk_context_t	ctx;
8454 
8455 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8456 
8457 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8458 	if (family == AF_INET)
8459 		ill = ILL_START_WALK_V4(&ctx, ipst);
8460 	else if (family == AF_INET6)
8461 		ill = ILL_START_WALK_V6(&ctx, ipst);
8462 	else
8463 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8464 
8465 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8466 		for (ipif = ill->ill_ipif; ipif != NULL;
8467 		    ipif = ipif->ipif_next) {
8468 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8469 			    !(lifn_flags & LIFC_NOXMIT))
8470 				continue;
8471 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8472 			    !(lifn_flags & LIFC_TEMPORARY))
8473 				continue;
8474 			if (((ipif->ipif_flags &
8475 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8476 			    IPIF_DEPRECATED)) ||
8477 			    IS_LOOPBACK(ill) ||
8478 			    !(ipif->ipif_flags & IPIF_UP)) &&
8479 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8480 				continue;
8481 
8482 			if (zoneid != ipif->ipif_zoneid &&
8483 			    ipif->ipif_zoneid != ALL_ZONES &&
8484 			    (zoneid != GLOBAL_ZONEID ||
8485 			    !(lifn_flags & LIFC_ALLZONES)))
8486 				continue;
8487 
8488 			numifs++;
8489 		}
8490 	}
8491 	rw_exit(&ipst->ips_ill_g_lock);
8492 	return (numifs);
8493 }
8494 
8495 uint_t
8496 ip_get_lifsrcofnum(ill_t *ill)
8497 {
8498 	uint_t numifs = 0;
8499 	ill_t	*ill_head = ill;
8500 	ip_stack_t	*ipst = ill->ill_ipst;
8501 
8502 	/*
8503 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8504 	 * other thread may be trying to relink the ILLs in this usesrc group
8505 	 * and adjusting the ill_usesrc_grp_next pointers
8506 	 */
8507 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8508 	if ((ill->ill_usesrc_ifindex == 0) &&
8509 	    (ill->ill_usesrc_grp_next != NULL)) {
8510 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8511 		    ill = ill->ill_usesrc_grp_next)
8512 			numifs++;
8513 	}
8514 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8515 
8516 	return (numifs);
8517 }
8518 
8519 /* Null values are passed in for ipif, sin, and ifreq */
8520 /* ARGSUSED */
8521 int
8522 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8523     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8524 {
8525 	int *nump;
8526 	conn_t *connp = Q_TO_CONN(q);
8527 
8528 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8529 
8530 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8531 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8532 
8533 	*nump = ip_get_numifs(connp->conn_zoneid,
8534 	    connp->conn_netstack->netstack_ip);
8535 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8536 	return (0);
8537 }
8538 
8539 /* Null values are passed in for ipif, sin, and ifreq */
8540 /* ARGSUSED */
8541 int
8542 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8543     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8544 {
8545 	struct lifnum *lifn;
8546 	mblk_t	*mp1;
8547 	conn_t *connp = Q_TO_CONN(q);
8548 
8549 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8550 
8551 	/* Existence checked in ip_wput_nondata */
8552 	mp1 = mp->b_cont->b_cont;
8553 
8554 	lifn = (struct lifnum *)mp1->b_rptr;
8555 	switch (lifn->lifn_family) {
8556 	case AF_UNSPEC:
8557 	case AF_INET:
8558 	case AF_INET6:
8559 		break;
8560 	default:
8561 		return (EAFNOSUPPORT);
8562 	}
8563 
8564 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8565 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8566 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8567 	return (0);
8568 }
8569 
8570 /* ARGSUSED */
8571 int
8572 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8573     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8574 {
8575 	STRUCT_HANDLE(ifconf, ifc);
8576 	mblk_t *mp1;
8577 	struct iocblk *iocp;
8578 	struct ifreq *ifr;
8579 	ill_walk_context_t	ctx;
8580 	ill_t	*ill;
8581 	ipif_t	*ipif;
8582 	struct sockaddr_in *sin;
8583 	int32_t	ifclen;
8584 	zoneid_t zoneid;
8585 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8586 
8587 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8588 
8589 	ip1dbg(("ip_sioctl_get_ifconf"));
8590 	/* Existence verified in ip_wput_nondata */
8591 	mp1 = mp->b_cont->b_cont;
8592 	iocp = (struct iocblk *)mp->b_rptr;
8593 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8594 
8595 	/*
8596 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8597 	 * the user buffer address and length into which the list of struct
8598 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8599 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8600 	 * the SIOCGIFCONF operation was redefined to simply provide
8601 	 * a large output buffer into which we are supposed to jam the ifreq
8602 	 * array.  The same ioctl command code was used, despite the fact that
8603 	 * both the applications and the kernel code had to change, thus making
8604 	 * it impossible to support both interfaces.
8605 	 *
8606 	 * For reasons not good enough to try to explain, the following
8607 	 * algorithm is used for deciding what to do with one of these:
8608 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8609 	 * form with the output buffer coming down as the continuation message.
8610 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8611 	 * and we have to copy in the ifconf structure to find out how big the
8612 	 * output buffer is and where to copy out to.  Sure no problem...
8613 	 *
8614 	 */
8615 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8616 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8617 		int numifs = 0;
8618 		size_t ifc_bufsize;
8619 
8620 		/*
8621 		 * Must be (better be!) continuation of a TRANSPARENT
8622 		 * IOCTL.  We just copied in the ifconf structure.
8623 		 */
8624 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8625 		    (struct ifconf *)mp1->b_rptr);
8626 
8627 		/*
8628 		 * Allocate a buffer to hold requested information.
8629 		 *
8630 		 * If ifc_len is larger than what is needed, we only
8631 		 * allocate what we will use.
8632 		 *
8633 		 * If ifc_len is smaller than what is needed, return
8634 		 * EINVAL.
8635 		 *
8636 		 * XXX: the ill_t structure can hava 2 counters, for
8637 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8638 		 * number of interfaces for a device, so we don't need
8639 		 * to count them here...
8640 		 */
8641 		numifs = ip_get_numifs(zoneid, ipst);
8642 
8643 		ifclen = STRUCT_FGET(ifc, ifc_len);
8644 		ifc_bufsize = numifs * sizeof (struct ifreq);
8645 		if (ifc_bufsize > ifclen) {
8646 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8647 				/* old behaviour */
8648 				return (EINVAL);
8649 			} else {
8650 				ifc_bufsize = ifclen;
8651 			}
8652 		}
8653 
8654 		mp1 = mi_copyout_alloc(q, mp,
8655 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8656 		if (mp1 == NULL)
8657 			return (ENOMEM);
8658 
8659 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8660 	}
8661 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8662 	/*
8663 	 * the SIOCGIFCONF ioctl only knows about
8664 	 * IPv4 addresses, so don't try to tell
8665 	 * it about interfaces with IPv6-only
8666 	 * addresses. (Last parm 'isv6' is B_FALSE)
8667 	 */
8668 
8669 	ifr = (struct ifreq *)mp1->b_rptr;
8670 
8671 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8672 	ill = ILL_START_WALK_V4(&ctx, ipst);
8673 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8674 		for (ipif = ill->ill_ipif; ipif != NULL;
8675 		    ipif = ipif->ipif_next) {
8676 			if (zoneid != ipif->ipif_zoneid &&
8677 			    ipif->ipif_zoneid != ALL_ZONES)
8678 				continue;
8679 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8680 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8681 					/* old behaviour */
8682 					rw_exit(&ipst->ips_ill_g_lock);
8683 					return (EINVAL);
8684 				} else {
8685 					goto if_copydone;
8686 				}
8687 			}
8688 			ipif_get_name(ipif, ifr->ifr_name,
8689 			    sizeof (ifr->ifr_name));
8690 			sin = (sin_t *)&ifr->ifr_addr;
8691 			*sin = sin_null;
8692 			sin->sin_family = AF_INET;
8693 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8694 			ifr++;
8695 		}
8696 	}
8697 if_copydone:
8698 	rw_exit(&ipst->ips_ill_g_lock);
8699 	mp1->b_wptr = (uchar_t *)ifr;
8700 
8701 	if (STRUCT_BUF(ifc) != NULL) {
8702 		STRUCT_FSET(ifc, ifc_len,
8703 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8704 	}
8705 	return (0);
8706 }
8707 
8708 /*
8709  * Get the interfaces using the address hosted on the interface passed in,
8710  * as a source adddress
8711  */
8712 /* ARGSUSED */
8713 int
8714 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8715     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8716 {
8717 	mblk_t *mp1;
8718 	ill_t	*ill, *ill_head;
8719 	ipif_t	*ipif, *orig_ipif;
8720 	int	numlifs = 0;
8721 	size_t	lifs_bufsize, lifsmaxlen;
8722 	struct	lifreq *lifr;
8723 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8724 	uint_t	ifindex;
8725 	zoneid_t zoneid;
8726 	int err = 0;
8727 	boolean_t isv6 = B_FALSE;
8728 	struct	sockaddr_in	*sin;
8729 	struct	sockaddr_in6	*sin6;
8730 	STRUCT_HANDLE(lifsrcof, lifs);
8731 	ip_stack_t		*ipst;
8732 
8733 	ipst = CONNQ_TO_IPST(q);
8734 
8735 	ASSERT(q->q_next == NULL);
8736 
8737 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8738 
8739 	/* Existence verified in ip_wput_nondata */
8740 	mp1 = mp->b_cont->b_cont;
8741 
8742 	/*
8743 	 * Must be (better be!) continuation of a TRANSPARENT
8744 	 * IOCTL.  We just copied in the lifsrcof structure.
8745 	 */
8746 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8747 	    (struct lifsrcof *)mp1->b_rptr);
8748 
8749 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8750 		return (EINVAL);
8751 
8752 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8753 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8754 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8755 	    ip_process_ioctl, &err, ipst);
8756 	if (ipif == NULL) {
8757 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8758 		    ifindex));
8759 		return (err);
8760 	}
8761 
8762 	/* Allocate a buffer to hold requested information */
8763 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8764 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8765 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8766 	/* The actual size needed is always returned in lifs_len */
8767 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8768 
8769 	/* If the amount we need is more than what is passed in, abort */
8770 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8771 		ipif_refrele(ipif);
8772 		return (0);
8773 	}
8774 
8775 	mp1 = mi_copyout_alloc(q, mp,
8776 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8777 	if (mp1 == NULL) {
8778 		ipif_refrele(ipif);
8779 		return (ENOMEM);
8780 	}
8781 
8782 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8783 	bzero(mp1->b_rptr, lifs_bufsize);
8784 
8785 	lifr = (struct lifreq *)mp1->b_rptr;
8786 
8787 	ill = ill_head = ipif->ipif_ill;
8788 	orig_ipif = ipif;
8789 
8790 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8791 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8792 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8793 
8794 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8795 	for (; (ill != NULL) && (ill != ill_head);
8796 	    ill = ill->ill_usesrc_grp_next) {
8797 
8798 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8799 			break;
8800 
8801 		ipif = ill->ill_ipif;
8802 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8803 		if (ipif->ipif_isv6) {
8804 			sin6 = (sin6_t *)&lifr->lifr_addr;
8805 			*sin6 = sin6_null;
8806 			sin6->sin6_family = AF_INET6;
8807 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8808 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8809 			    &ipif->ipif_v6net_mask);
8810 		} else {
8811 			sin = (sin_t *)&lifr->lifr_addr;
8812 			*sin = sin_null;
8813 			sin->sin_family = AF_INET;
8814 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8815 			lifr->lifr_addrlen = ip_mask_to_plen(
8816 			    ipif->ipif_net_mask);
8817 		}
8818 		lifr++;
8819 	}
8820 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8821 	rw_exit(&ipst->ips_ill_g_lock);
8822 	ipif_refrele(orig_ipif);
8823 	mp1->b_wptr = (uchar_t *)lifr;
8824 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8825 
8826 	return (0);
8827 }
8828 
8829 /* ARGSUSED */
8830 int
8831 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8832     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8833 {
8834 	mblk_t *mp1;
8835 	int	list;
8836 	ill_t	*ill;
8837 	ipif_t	*ipif;
8838 	int	flags;
8839 	int	numlifs = 0;
8840 	size_t	lifc_bufsize;
8841 	struct	lifreq *lifr;
8842 	sa_family_t	family;
8843 	struct	sockaddr_in	*sin;
8844 	struct	sockaddr_in6	*sin6;
8845 	ill_walk_context_t	ctx;
8846 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8847 	int32_t	lifclen;
8848 	zoneid_t zoneid;
8849 	STRUCT_HANDLE(lifconf, lifc);
8850 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8851 
8852 	ip1dbg(("ip_sioctl_get_lifconf"));
8853 
8854 	ASSERT(q->q_next == NULL);
8855 
8856 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8857 
8858 	/* Existence verified in ip_wput_nondata */
8859 	mp1 = mp->b_cont->b_cont;
8860 
8861 	/*
8862 	 * An extended version of SIOCGIFCONF that takes an
8863 	 * additional address family and flags field.
8864 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8865 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8866 	 * interfaces are omitted.
8867 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8868 	 * unless LIFC_TEMPORARY is specified.
8869 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8870 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8871 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8872 	 * has priority over LIFC_NOXMIT.
8873 	 */
8874 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8875 
8876 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8877 		return (EINVAL);
8878 
8879 	/*
8880 	 * Must be (better be!) continuation of a TRANSPARENT
8881 	 * IOCTL.  We just copied in the lifconf structure.
8882 	 */
8883 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8884 
8885 	family = STRUCT_FGET(lifc, lifc_family);
8886 	flags = STRUCT_FGET(lifc, lifc_flags);
8887 
8888 	switch (family) {
8889 	case AF_UNSPEC:
8890 		/*
8891 		 * walk all ILL's.
8892 		 */
8893 		list = MAX_G_HEADS;
8894 		break;
8895 	case AF_INET:
8896 		/*
8897 		 * walk only IPV4 ILL's.
8898 		 */
8899 		list = IP_V4_G_HEAD;
8900 		break;
8901 	case AF_INET6:
8902 		/*
8903 		 * walk only IPV6 ILL's.
8904 		 */
8905 		list = IP_V6_G_HEAD;
8906 		break;
8907 	default:
8908 		return (EAFNOSUPPORT);
8909 	}
8910 
8911 	/*
8912 	 * Allocate a buffer to hold requested information.
8913 	 *
8914 	 * If lifc_len is larger than what is needed, we only
8915 	 * allocate what we will use.
8916 	 *
8917 	 * If lifc_len is smaller than what is needed, return
8918 	 * EINVAL.
8919 	 */
8920 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8921 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8922 	lifclen = STRUCT_FGET(lifc, lifc_len);
8923 	if (lifc_bufsize > lifclen) {
8924 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8925 			return (EINVAL);
8926 		else
8927 			lifc_bufsize = lifclen;
8928 	}
8929 
8930 	mp1 = mi_copyout_alloc(q, mp,
8931 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8932 	if (mp1 == NULL)
8933 		return (ENOMEM);
8934 
8935 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8936 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8937 
8938 	lifr = (struct lifreq *)mp1->b_rptr;
8939 
8940 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8941 	ill = ill_first(list, list, &ctx, ipst);
8942 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8943 		for (ipif = ill->ill_ipif; ipif != NULL;
8944 		    ipif = ipif->ipif_next) {
8945 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8946 			    !(flags & LIFC_NOXMIT))
8947 				continue;
8948 
8949 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8950 			    !(flags & LIFC_TEMPORARY))
8951 				continue;
8952 
8953 			if (((ipif->ipif_flags &
8954 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8955 			    IPIF_DEPRECATED)) ||
8956 			    IS_LOOPBACK(ill) ||
8957 			    !(ipif->ipif_flags & IPIF_UP)) &&
8958 			    (flags & LIFC_EXTERNAL_SOURCE))
8959 				continue;
8960 
8961 			if (zoneid != ipif->ipif_zoneid &&
8962 			    ipif->ipif_zoneid != ALL_ZONES &&
8963 			    (zoneid != GLOBAL_ZONEID ||
8964 			    !(flags & LIFC_ALLZONES)))
8965 				continue;
8966 
8967 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8968 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8969 					rw_exit(&ipst->ips_ill_g_lock);
8970 					return (EINVAL);
8971 				} else {
8972 					goto lif_copydone;
8973 				}
8974 			}
8975 
8976 			ipif_get_name(ipif, lifr->lifr_name,
8977 			    sizeof (lifr->lifr_name));
8978 			if (ipif->ipif_isv6) {
8979 				sin6 = (sin6_t *)&lifr->lifr_addr;
8980 				*sin6 = sin6_null;
8981 				sin6->sin6_family = AF_INET6;
8982 				sin6->sin6_addr =
8983 				    ipif->ipif_v6lcl_addr;
8984 				lifr->lifr_addrlen =
8985 				    ip_mask_to_plen_v6(
8986 				    &ipif->ipif_v6net_mask);
8987 			} else {
8988 				sin = (sin_t *)&lifr->lifr_addr;
8989 				*sin = sin_null;
8990 				sin->sin_family = AF_INET;
8991 				sin->sin_addr.s_addr =
8992 				    ipif->ipif_lcl_addr;
8993 				lifr->lifr_addrlen =
8994 				    ip_mask_to_plen(
8995 				    ipif->ipif_net_mask);
8996 			}
8997 			lifr++;
8998 		}
8999 	}
9000 lif_copydone:
9001 	rw_exit(&ipst->ips_ill_g_lock);
9002 
9003 	mp1->b_wptr = (uchar_t *)lifr;
9004 	if (STRUCT_BUF(lifc) != NULL) {
9005 		STRUCT_FSET(lifc, lifc_len,
9006 		    (int)((uchar_t *)lifr - mp1->b_rptr));
9007 	}
9008 	return (0);
9009 }
9010 
9011 /* ARGSUSED */
9012 int
9013 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
9014     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9015 {
9016 	ip_stack_t	*ipst;
9017 
9018 	if (q->q_next == NULL)
9019 		ipst = CONNQ_TO_IPST(q);
9020 	else
9021 		ipst = ILLQ_TO_IPST(q);
9022 
9023 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9024 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9025 	return (0);
9026 }
9027 
9028 static void
9029 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9030 {
9031 	ip6_asp_t *table;
9032 	size_t table_size;
9033 	mblk_t *data_mp;
9034 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9035 	ip_stack_t	*ipst;
9036 
9037 	if (q->q_next == NULL)
9038 		ipst = CONNQ_TO_IPST(q);
9039 	else
9040 		ipst = ILLQ_TO_IPST(q);
9041 
9042 	/* These two ioctls are I_STR only */
9043 	if (iocp->ioc_count == TRANSPARENT) {
9044 		miocnak(q, mp, 0, EINVAL);
9045 		return;
9046 	}
9047 
9048 	data_mp = mp->b_cont;
9049 	if (data_mp == NULL) {
9050 		/* The user passed us a NULL argument */
9051 		table = NULL;
9052 		table_size = iocp->ioc_count;
9053 	} else {
9054 		/*
9055 		 * The user provided a table.  The stream head
9056 		 * may have copied in the user data in chunks,
9057 		 * so make sure everything is pulled up
9058 		 * properly.
9059 		 */
9060 		if (MBLKL(data_mp) < iocp->ioc_count) {
9061 			mblk_t *new_data_mp;
9062 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9063 			    NULL) {
9064 				miocnak(q, mp, 0, ENOMEM);
9065 				return;
9066 			}
9067 			freemsg(data_mp);
9068 			data_mp = new_data_mp;
9069 			mp->b_cont = data_mp;
9070 		}
9071 		table = (ip6_asp_t *)data_mp->b_rptr;
9072 		table_size = iocp->ioc_count;
9073 	}
9074 
9075 	switch (iocp->ioc_cmd) {
9076 	case SIOCGIP6ADDRPOLICY:
9077 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9078 		if (iocp->ioc_rval == -1)
9079 			iocp->ioc_error = EINVAL;
9080 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9081 		else if (table != NULL &&
9082 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9083 			ip6_asp_t *src = table;
9084 			ip6_asp32_t *dst = (void *)table;
9085 			int count = table_size / sizeof (ip6_asp_t);
9086 			int i;
9087 
9088 			/*
9089 			 * We need to do an in-place shrink of the array
9090 			 * to match the alignment attributes of the
9091 			 * 32-bit ABI looking at it.
9092 			 */
9093 			/* LINTED: logical expression always true: op "||" */
9094 			ASSERT(sizeof (*src) > sizeof (*dst));
9095 			for (i = 1; i < count; i++)
9096 				bcopy(src + i, dst + i, sizeof (*dst));
9097 		}
9098 #endif
9099 		break;
9100 
9101 	case SIOCSIP6ADDRPOLICY:
9102 		ASSERT(mp->b_prev == NULL);
9103 		mp->b_prev = (void *)q;
9104 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9105 		/*
9106 		 * We pass in the datamodel here so that the ip6_asp_replace()
9107 		 * routine can handle converting from 32-bit to native formats
9108 		 * where necessary.
9109 		 *
9110 		 * A better way to handle this might be to convert the inbound
9111 		 * data structure here, and hang it off a new 'mp'; thus the
9112 		 * ip6_asp_replace() logic would always be dealing with native
9113 		 * format data structures..
9114 		 *
9115 		 * (An even simpler way to handle these ioctls is to just
9116 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9117 		 * and just recompile everything that depends on it.)
9118 		 */
9119 #endif
9120 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9121 		    iocp->ioc_flag & IOC_MODELS);
9122 		return;
9123 	}
9124 
9125 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9126 	qreply(q, mp);
9127 }
9128 
9129 static void
9130 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9131 {
9132 	mblk_t 		*data_mp;
9133 	struct dstinforeq	*dir;
9134 	uint8_t		*end, *cur;
9135 	in6_addr_t	*daddr, *saddr;
9136 	ipaddr_t	v4daddr;
9137 	ire_t		*ire;
9138 	char		*slabel, *dlabel;
9139 	boolean_t	isipv4;
9140 	int		match_ire;
9141 	ill_t		*dst_ill;
9142 	ipif_t		*src_ipif, *ire_ipif;
9143 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9144 	zoneid_t	zoneid;
9145 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9146 
9147 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9148 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9149 
9150 	/*
9151 	 * This ioctl is I_STR only, and must have a
9152 	 * data mblk following the M_IOCTL mblk.
9153 	 */
9154 	data_mp = mp->b_cont;
9155 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9156 		miocnak(q, mp, 0, EINVAL);
9157 		return;
9158 	}
9159 
9160 	if (MBLKL(data_mp) < iocp->ioc_count) {
9161 		mblk_t *new_data_mp;
9162 
9163 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9164 			miocnak(q, mp, 0, ENOMEM);
9165 			return;
9166 		}
9167 		freemsg(data_mp);
9168 		data_mp = new_data_mp;
9169 		mp->b_cont = data_mp;
9170 	}
9171 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9172 
9173 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9174 	    end - cur >= sizeof (struct dstinforeq);
9175 	    cur += sizeof (struct dstinforeq)) {
9176 		dir = (struct dstinforeq *)cur;
9177 		daddr = &dir->dir_daddr;
9178 		saddr = &dir->dir_saddr;
9179 
9180 		/*
9181 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9182 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9183 		 * and ipif_select_source[_v6]() do not.
9184 		 */
9185 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9186 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9187 
9188 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9189 		if (isipv4) {
9190 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9191 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9192 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9193 		} else {
9194 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9195 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9196 		}
9197 		if (ire == NULL) {
9198 			dir->dir_dreachable = 0;
9199 
9200 			/* move on to next dst addr */
9201 			continue;
9202 		}
9203 		dir->dir_dreachable = 1;
9204 
9205 		ire_ipif = ire->ire_ipif;
9206 		if (ire_ipif == NULL)
9207 			goto next_dst;
9208 
9209 		/*
9210 		 * We expect to get back an interface ire or a
9211 		 * gateway ire cache entry.  For both types, the
9212 		 * output interface is ire_ipif->ipif_ill.
9213 		 */
9214 		dst_ill = ire_ipif->ipif_ill;
9215 		dir->dir_dmactype = dst_ill->ill_mactype;
9216 
9217 		if (isipv4) {
9218 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9219 		} else {
9220 			src_ipif = ipif_select_source_v6(dst_ill,
9221 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9222 			    zoneid);
9223 		}
9224 		if (src_ipif == NULL)
9225 			goto next_dst;
9226 
9227 		*saddr = src_ipif->ipif_v6lcl_addr;
9228 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9229 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9230 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9231 		dir->dir_sdeprecated =
9232 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9233 		ipif_refrele(src_ipif);
9234 next_dst:
9235 		ire_refrele(ire);
9236 	}
9237 	miocack(q, mp, iocp->ioc_count, 0);
9238 }
9239 
9240 /*
9241  * Check if this is an address assigned to this machine.
9242  * Skips interfaces that are down by using ire checks.
9243  * Translates mapped addresses to v4 addresses and then
9244  * treats them as such, returning true if the v4 address
9245  * associated with this mapped address is configured.
9246  * Note: Applications will have to be careful what they do
9247  * with the response; use of mapped addresses limits
9248  * what can be done with the socket, especially with
9249  * respect to socket options and ioctls - neither IPv4
9250  * options nor IPv6 sticky options/ancillary data options
9251  * may be used.
9252  */
9253 /* ARGSUSED */
9254 int
9255 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9256     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9257 {
9258 	struct sioc_addrreq *sia;
9259 	sin_t *sin;
9260 	ire_t *ire;
9261 	mblk_t *mp1;
9262 	zoneid_t zoneid;
9263 	ip_stack_t	*ipst;
9264 
9265 	ip1dbg(("ip_sioctl_tmyaddr"));
9266 
9267 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9268 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9269 	ipst = CONNQ_TO_IPST(q);
9270 
9271 	/* Existence verified in ip_wput_nondata */
9272 	mp1 = mp->b_cont->b_cont;
9273 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9274 	sin = (sin_t *)&sia->sa_addr;
9275 	switch (sin->sin_family) {
9276 	case AF_INET6: {
9277 		sin6_t *sin6 = (sin6_t *)sin;
9278 
9279 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9280 			ipaddr_t v4_addr;
9281 
9282 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9283 			    v4_addr);
9284 			ire = ire_ctable_lookup(v4_addr, 0,
9285 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9286 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9287 		} else {
9288 			in6_addr_t v6addr;
9289 
9290 			v6addr = sin6->sin6_addr;
9291 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9292 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9293 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9294 		}
9295 		break;
9296 	}
9297 	case AF_INET: {
9298 		ipaddr_t v4addr;
9299 
9300 		v4addr = sin->sin_addr.s_addr;
9301 		ire = ire_ctable_lookup(v4addr, 0,
9302 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9303 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9304 		break;
9305 	}
9306 	default:
9307 		return (EAFNOSUPPORT);
9308 	}
9309 	if (ire != NULL) {
9310 		sia->sa_res = 1;
9311 		ire_refrele(ire);
9312 	} else {
9313 		sia->sa_res = 0;
9314 	}
9315 	return (0);
9316 }
9317 
9318 /*
9319  * Check if this is an address assigned on-link i.e. neighbor,
9320  * and makes sure it's reachable from the current zone.
9321  * Returns true for my addresses as well.
9322  * Translates mapped addresses to v4 addresses and then
9323  * treats them as such, returning true if the v4 address
9324  * associated with this mapped address is configured.
9325  * Note: Applications will have to be careful what they do
9326  * with the response; use of mapped addresses limits
9327  * what can be done with the socket, especially with
9328  * respect to socket options and ioctls - neither IPv4
9329  * options nor IPv6 sticky options/ancillary data options
9330  * may be used.
9331  */
9332 /* ARGSUSED */
9333 int
9334 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9335     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9336 {
9337 	struct sioc_addrreq *sia;
9338 	sin_t *sin;
9339 	mblk_t	*mp1;
9340 	ire_t *ire = NULL;
9341 	zoneid_t zoneid;
9342 	ip_stack_t	*ipst;
9343 
9344 	ip1dbg(("ip_sioctl_tonlink"));
9345 
9346 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9347 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9348 	ipst = CONNQ_TO_IPST(q);
9349 
9350 	/* Existence verified in ip_wput_nondata */
9351 	mp1 = mp->b_cont->b_cont;
9352 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9353 	sin = (sin_t *)&sia->sa_addr;
9354 
9355 	/*
9356 	 * Match addresses with a zero gateway field to avoid
9357 	 * routes going through a router.
9358 	 * Exclude broadcast and multicast addresses.
9359 	 */
9360 	switch (sin->sin_family) {
9361 	case AF_INET6: {
9362 		sin6_t *sin6 = (sin6_t *)sin;
9363 
9364 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9365 			ipaddr_t v4_addr;
9366 
9367 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9368 			    v4_addr);
9369 			if (!CLASSD(v4_addr)) {
9370 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9371 				    NULL, NULL, zoneid, NULL,
9372 				    MATCH_IRE_GW, ipst);
9373 			}
9374 		} else {
9375 			in6_addr_t v6addr;
9376 			in6_addr_t v6gw;
9377 
9378 			v6addr = sin6->sin6_addr;
9379 			v6gw = ipv6_all_zeros;
9380 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9381 				ire = ire_route_lookup_v6(&v6addr, 0,
9382 				    &v6gw, 0, NULL, NULL, zoneid,
9383 				    NULL, MATCH_IRE_GW, ipst);
9384 			}
9385 		}
9386 		break;
9387 	}
9388 	case AF_INET: {
9389 		ipaddr_t v4addr;
9390 
9391 		v4addr = sin->sin_addr.s_addr;
9392 		if (!CLASSD(v4addr)) {
9393 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9394 			    NULL, NULL, zoneid, NULL,
9395 			    MATCH_IRE_GW, ipst);
9396 		}
9397 		break;
9398 	}
9399 	default:
9400 		return (EAFNOSUPPORT);
9401 	}
9402 	sia->sa_res = 0;
9403 	if (ire != NULL) {
9404 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9405 		    IRE_LOCAL|IRE_LOOPBACK)) {
9406 			sia->sa_res = 1;
9407 		}
9408 		ire_refrele(ire);
9409 	}
9410 	return (0);
9411 }
9412 
9413 /*
9414  * TBD: implement when kernel maintaines a list of site prefixes.
9415  */
9416 /* ARGSUSED */
9417 int
9418 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9419     ip_ioctl_cmd_t *ipip, void *ifreq)
9420 {
9421 	return (ENXIO);
9422 }
9423 
9424 /* ARGSUSED */
9425 int
9426 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9427     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9428 {
9429 	ill_t  		*ill;
9430 	mblk_t		*mp1;
9431 	conn_t		*connp;
9432 	boolean_t	success;
9433 
9434 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9435 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9436 	/* ioctl comes down on an conn */
9437 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9438 	connp = Q_TO_CONN(q);
9439 
9440 	mp->b_datap->db_type = M_IOCTL;
9441 
9442 	/*
9443 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9444 	 * The original mp contains contaminated b_next values due to 'mi',
9445 	 * which is needed to do the mi_copy_done. Unfortunately if we
9446 	 * send down the original mblk itself and if we are popped due to an
9447 	 * an unplumb before the response comes back from tunnel,
9448 	 * the streamhead (which does a freemsg) will see this contaminated
9449 	 * message and the assertion in freemsg about non-null b_next/b_prev
9450 	 * will panic a DEBUG kernel.
9451 	 */
9452 	mp1 = copymsg(mp);
9453 	if (mp1 == NULL)
9454 		return (ENOMEM);
9455 
9456 	ill = ipif->ipif_ill;
9457 	mutex_enter(&connp->conn_lock);
9458 	mutex_enter(&ill->ill_lock);
9459 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9460 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9461 		    mp, 0);
9462 	} else {
9463 		success = ill_pending_mp_add(ill, connp, mp);
9464 	}
9465 	mutex_exit(&ill->ill_lock);
9466 	mutex_exit(&connp->conn_lock);
9467 
9468 	if (success) {
9469 		ip1dbg(("sending down tunparam request "));
9470 		putnext(ill->ill_wq, mp1);
9471 		return (EINPROGRESS);
9472 	} else {
9473 		/* The conn has started closing */
9474 		freemsg(mp1);
9475 		return (EINTR);
9476 	}
9477 }
9478 
9479 /*
9480  * ARP IOCTLs.
9481  * How does IP get in the business of fronting ARP configuration/queries?
9482  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9483  * are by tradition passed in through a datagram socket.  That lands in IP.
9484  * As it happens, this is just as well since the interface is quite crude in
9485  * that it passes in no information about protocol or hardware types, or
9486  * interface association.  After making the protocol assumption, IP is in
9487  * the position to look up the name of the ILL, which ARP will need, and
9488  * format a request that can be handled by ARP.  The request is passed up
9489  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9490  * back a response.  ARP supports its own set of more general IOCTLs, in
9491  * case anyone is interested.
9492  */
9493 /* ARGSUSED */
9494 int
9495 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9496     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9497 {
9498 	mblk_t *mp1;
9499 	mblk_t *mp2;
9500 	mblk_t *pending_mp;
9501 	ipaddr_t ipaddr;
9502 	area_t *area;
9503 	struct iocblk *iocp;
9504 	conn_t *connp;
9505 	struct arpreq *ar;
9506 	struct xarpreq *xar;
9507 	int flags, alength;
9508 	char *lladdr;
9509 	ip_stack_t	*ipst;
9510 	ill_t *ill = ipif->ipif_ill;
9511 	boolean_t if_arp_ioctl = B_FALSE;
9512 
9513 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9514 	connp = Q_TO_CONN(q);
9515 	ipst = connp->conn_netstack->netstack_ip;
9516 
9517 	if (ipip->ipi_cmd_type == XARP_CMD) {
9518 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9519 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9520 		ar = NULL;
9521 
9522 		flags = xar->xarp_flags;
9523 		lladdr = LLADDR(&xar->xarp_ha);
9524 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9525 		/*
9526 		 * Validate against user's link layer address length
9527 		 * input and name and addr length limits.
9528 		 */
9529 		alength = ill->ill_phys_addr_length;
9530 		if (ipip->ipi_cmd == SIOCSXARP) {
9531 			if (alength != xar->xarp_ha.sdl_alen ||
9532 			    (alength + xar->xarp_ha.sdl_nlen >
9533 			    sizeof (xar->xarp_ha.sdl_data)))
9534 				return (EINVAL);
9535 		}
9536 	} else {
9537 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9538 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9539 		xar = NULL;
9540 
9541 		flags = ar->arp_flags;
9542 		lladdr = ar->arp_ha.sa_data;
9543 		/*
9544 		 * Theoretically, the sa_family could tell us what link
9545 		 * layer type this operation is trying to deal with. By
9546 		 * common usage AF_UNSPEC means ethernet. We'll assume
9547 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9548 		 * for now. Our new SIOC*XARP ioctls can be used more
9549 		 * generally.
9550 		 *
9551 		 * If the underlying media happens to have a non 6 byte
9552 		 * address, arp module will fail set/get, but the del
9553 		 * operation will succeed.
9554 		 */
9555 		alength = 6;
9556 		if ((ipip->ipi_cmd != SIOCDARP) &&
9557 		    (alength != ill->ill_phys_addr_length)) {
9558 			return (EINVAL);
9559 		}
9560 	}
9561 
9562 	/*
9563 	 * We are going to pass up to ARP a packet chain that looks
9564 	 * like:
9565 	 *
9566 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9567 	 *
9568 	 * Get a copy of the original IOCTL mblk to head the chain,
9569 	 * to be sent up (in mp1). Also get another copy to store
9570 	 * in the ill_pending_mp list, for matching the response
9571 	 * when it comes back from ARP.
9572 	 */
9573 	mp1 = copyb(mp);
9574 	pending_mp = copymsg(mp);
9575 	if (mp1 == NULL || pending_mp == NULL) {
9576 		if (mp1 != NULL)
9577 			freeb(mp1);
9578 		if (pending_mp != NULL)
9579 			inet_freemsg(pending_mp);
9580 		return (ENOMEM);
9581 	}
9582 
9583 	ipaddr = sin->sin_addr.s_addr;
9584 
9585 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9586 	    (caddr_t)&ipaddr);
9587 	if (mp2 == NULL) {
9588 		freeb(mp1);
9589 		inet_freemsg(pending_mp);
9590 		return (ENOMEM);
9591 	}
9592 	/* Put together the chain. */
9593 	mp1->b_cont = mp2;
9594 	mp1->b_datap->db_type = M_IOCTL;
9595 	mp2->b_cont = mp;
9596 	mp2->b_datap->db_type = M_DATA;
9597 
9598 	iocp = (struct iocblk *)mp1->b_rptr;
9599 
9600 	/*
9601 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9602 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9603 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9604 	 * ioc_count field; set ioc_count to be correct.
9605 	 */
9606 	iocp->ioc_count = MBLKL(mp1->b_cont);
9607 
9608 	/*
9609 	 * Set the proper command in the ARP message.
9610 	 * Convert the SIOC{G|S|D}ARP calls into our
9611 	 * AR_ENTRY_xxx calls.
9612 	 */
9613 	area = (area_t *)mp2->b_rptr;
9614 	switch (iocp->ioc_cmd) {
9615 	case SIOCDARP:
9616 	case SIOCDXARP:
9617 		/*
9618 		 * We defer deleting the corresponding IRE until
9619 		 * we return from arp.
9620 		 */
9621 		area->area_cmd = AR_ENTRY_DELETE;
9622 		area->area_proto_mask_offset = 0;
9623 		break;
9624 	case SIOCGARP:
9625 	case SIOCGXARP:
9626 		area->area_cmd = AR_ENTRY_SQUERY;
9627 		area->area_proto_mask_offset = 0;
9628 		break;
9629 	case SIOCSARP:
9630 	case SIOCSXARP:
9631 		/*
9632 		 * Delete the corresponding ire to make sure IP will
9633 		 * pick up any change from arp.
9634 		 */
9635 		if (!if_arp_ioctl) {
9636 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9637 		} else {
9638 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9639 			if (ipif != NULL) {
9640 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9641 				    ipst);
9642 				ipif_refrele(ipif);
9643 			}
9644 		}
9645 		break;
9646 	}
9647 	iocp->ioc_cmd = area->area_cmd;
9648 
9649 	/*
9650 	 * Fill in the rest of the ARP operation fields.
9651 	 */
9652 	area->area_hw_addr_length = alength;
9653 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9654 
9655 	/* Translate the flags. */
9656 	if (flags & ATF_PERM)
9657 		area->area_flags |= ACE_F_PERMANENT;
9658 	if (flags & ATF_PUBL)
9659 		area->area_flags |= ACE_F_PUBLISH;
9660 	if (flags & ATF_AUTHORITY)
9661 		area->area_flags |= ACE_F_AUTHORITY;
9662 
9663 	/*
9664 	 * Before sending 'mp' to ARP, we have to clear the b_next
9665 	 * and b_prev. Otherwise if STREAMS encounters such a message
9666 	 * in freemsg(), (because ARP can close any time) it can cause
9667 	 * a panic. But mi code needs the b_next and b_prev values of
9668 	 * mp->b_cont, to complete the ioctl. So we store it here
9669 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9670 	 * when the response comes down from ARP.
9671 	 */
9672 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9673 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9674 	mp->b_cont->b_next = NULL;
9675 	mp->b_cont->b_prev = NULL;
9676 
9677 	mutex_enter(&connp->conn_lock);
9678 	mutex_enter(&ill->ill_lock);
9679 	/* conn has not yet started closing, hence this can't fail */
9680 	VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9681 	mutex_exit(&ill->ill_lock);
9682 	mutex_exit(&connp->conn_lock);
9683 
9684 	/*
9685 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9686 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9687 	 */
9688 	putnext(ill->ill_rq, mp1);
9689 	return (EINPROGRESS);
9690 }
9691 
9692 /*
9693  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9694  * the associated sin and refhold and return the associated ipif via `ci'.
9695  */
9696 int
9697 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9698     cmd_info_t *ci, ipsq_func_t func)
9699 {
9700 	mblk_t	*mp1;
9701 	int	err;
9702 	sin_t	*sin;
9703 	conn_t	*connp;
9704 	ipif_t	*ipif;
9705 	ire_t	*ire = NULL;
9706 	ill_t	*ill = NULL;
9707 	boolean_t exists;
9708 	ip_stack_t *ipst;
9709 	struct arpreq *ar;
9710 	struct xarpreq *xar;
9711 	struct sockaddr_dl *sdl;
9712 
9713 	/* ioctl comes down on a conn */
9714 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9715 	connp = Q_TO_CONN(q);
9716 	if (connp->conn_af_isv6)
9717 		return (ENXIO);
9718 
9719 	ipst = connp->conn_netstack->netstack_ip;
9720 
9721 	/* Verified in ip_wput_nondata */
9722 	mp1 = mp->b_cont->b_cont;
9723 
9724 	if (ipip->ipi_cmd_type == XARP_CMD) {
9725 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9726 		xar = (struct xarpreq *)mp1->b_rptr;
9727 		sin = (sin_t *)&xar->xarp_pa;
9728 		sdl = &xar->xarp_ha;
9729 
9730 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9731 			return (ENXIO);
9732 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9733 			return (EINVAL);
9734 	} else {
9735 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9736 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9737 		ar = (struct arpreq *)mp1->b_rptr;
9738 		sin = (sin_t *)&ar->arp_pa;
9739 	}
9740 
9741 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9742 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9743 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9744 		    mp, func, &err, ipst);
9745 		if (ipif == NULL)
9746 			return (err);
9747 		if (ipif->ipif_id != 0 ||
9748 		    ipif->ipif_net_type != IRE_IF_RESOLVER) {
9749 			ipif_refrele(ipif);
9750 			return (ENXIO);
9751 		}
9752 	} else {
9753 		/*
9754 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with sdl_nlen ==
9755 		 * 0: use the IP address to figure out the ill.	 In the IPMP
9756 		 * case, a simple forwarding table lookup will return the
9757 		 * IRE_IF_RESOLVER for the first interface in the group, which
9758 		 * might not be the interface on which the requested IP
9759 		 * address was resolved due to the ill selection algorithm
9760 		 * (see ip_newroute_get_dst_ill()).  So we do a cache table
9761 		 * lookup first: if the IRE cache entry for the IP address is
9762 		 * still there, it will contain the ill pointer for the right
9763 		 * interface, so we use that. If the cache entry has been
9764 		 * flushed, we fall back to the forwarding table lookup. This
9765 		 * should be rare enough since IRE cache entries have a longer
9766 		 * life expectancy than ARP cache entries.
9767 		 */
9768 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9769 		    ipst);
9770 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9771 		    ((ill = ire_to_ill(ire)) == NULL) ||
9772 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9773 			if (ire != NULL)
9774 				ire_refrele(ire);
9775 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9776 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9777 			    NULL, MATCH_IRE_TYPE, ipst);
9778 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9779 
9780 				if (ire != NULL)
9781 					ire_refrele(ire);
9782 				return (ENXIO);
9783 			}
9784 		}
9785 		ASSERT(ire != NULL && ill != NULL);
9786 		ipif = ill->ill_ipif;
9787 		ipif_refhold(ipif);
9788 		ire_refrele(ire);
9789 	}
9790 	ci->ci_sin = sin;
9791 	ci->ci_ipif = ipif;
9792 	return (0);
9793 }
9794 
9795 /*
9796  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9797  * atomically set/clear the muxids. Also complete the ioctl by acking or
9798  * naking it.  Note that the code is structured such that the link type,
9799  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9800  * its clones use the persistent link, while pppd(1M) and perhaps many
9801  * other daemons may use non-persistent link.  When combined with some
9802  * ill_t states, linking and unlinking lower streams may be used as
9803  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9804  */
9805 /* ARGSUSED */
9806 void
9807 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9808 {
9809 	mblk_t		*mp1, *mp2;
9810 	struct linkblk	*li;
9811 	struct ipmx_s	*ipmxp;
9812 	ill_t		*ill;
9813 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9814 	int		err = 0;
9815 	boolean_t	entered_ipsq = B_FALSE;
9816 	boolean_t	islink;
9817 	ip_stack_t	*ipst;
9818 
9819 	if (CONN_Q(q))
9820 		ipst = CONNQ_TO_IPST(q);
9821 	else
9822 		ipst = ILLQ_TO_IPST(q);
9823 
9824 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9825 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9826 
9827 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9828 
9829 	mp1 = mp->b_cont;	/* This is the linkblk info */
9830 	li = (struct linkblk *)mp1->b_rptr;
9831 
9832 	/*
9833 	 * ARP has added this special mblk, and the utility is asking us
9834 	 * to perform consistency checks, and also atomically set the
9835 	 * muxid. Ifconfig is an example.  It achieves this by using
9836 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9837 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9838 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9839 	 * and other comments in this routine for more details.
9840 	 */
9841 	mp2 = mp1->b_cont;	/* This is added by ARP */
9842 
9843 	/*
9844 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9845 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9846 	 * get the special mblk above.  For backward compatibility, we
9847 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9848 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9849 	 * not atomic, and can leave the streams unplumbable if the utility
9850 	 * is interrupted before it does the SIOCSLIFMUXID.
9851 	 */
9852 	if (mp2 == NULL) {
9853 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9854 		if (err == EINPROGRESS)
9855 			return;
9856 		goto done;
9857 	}
9858 
9859 	/*
9860 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9861 	 * ARP has appended this last mblk to tell us whether the lower stream
9862 	 * is an arp-dev stream or an IP module stream.
9863 	 */
9864 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9865 	if (ipmxp->ipmx_arpdev_stream) {
9866 		/*
9867 		 * The lower stream is the arp-dev stream.
9868 		 */
9869 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9870 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9871 		if (ill == NULL) {
9872 			if (err == EINPROGRESS)
9873 				return;
9874 			err = EINVAL;
9875 			goto done;
9876 		}
9877 
9878 		if (ipsq == NULL) {
9879 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9880 			    NEW_OP, B_TRUE);
9881 			if (ipsq == NULL) {
9882 				ill_refrele(ill);
9883 				return;
9884 			}
9885 			entered_ipsq = B_TRUE;
9886 		}
9887 		ASSERT(IAM_WRITER_ILL(ill));
9888 		ill_refrele(ill);
9889 
9890 		/*
9891 		 * To ensure consistency between IP and ARP, the following
9892 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9893 		 * This is because the muxid's are stored in the IP stream on
9894 		 * the ill.
9895 		 *
9896 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9897 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9898 		 * not yet plinked, and it also checks that the corresponding
9899 		 * IP stream is already plinked.
9900 		 *
9901 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9902 		 * punlinking the IP stream. IP does not allow punlink of the
9903 		 * IP stream unless the arp stream has been punlinked.
9904 		 */
9905 		if ((islink &&
9906 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9907 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9908 			err = EINVAL;
9909 			goto done;
9910 		}
9911 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9912 	} else {
9913 		/*
9914 		 * The lower stream is probably an IP module stream.  Do
9915 		 * consistency checking.
9916 		 */
9917 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9918 		if (err == EINPROGRESS)
9919 			return;
9920 	}
9921 done:
9922 	if (err == 0)
9923 		miocack(q, mp, 0, 0);
9924 	else
9925 		miocnak(q, mp, 0, err);
9926 
9927 	/* Conn was refheld in ip_sioctl_copyin_setup */
9928 	if (CONN_Q(q))
9929 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9930 	if (entered_ipsq)
9931 		ipsq_exit(ipsq);
9932 }
9933 
9934 /*
9935  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9936  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9937  * module stream).  If `doconsist' is set, then do the extended consistency
9938  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9939  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9940  * an error code on failure.
9941  */
9942 static int
9943 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9944     struct linkblk *li, boolean_t doconsist)
9945 {
9946 	ill_t  		*ill;
9947 	queue_t		*ipwq, *dwq;
9948 	const char	*name;
9949 	struct qinit	*qinfo;
9950 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9951 	boolean_t	entered_ipsq = B_FALSE;
9952 
9953 	/*
9954 	 * Walk the lower stream to verify it's the IP module stream.
9955 	 * The IP module is identified by its name, wput function,
9956 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9957 	 * (li->l_qbot) will not vanish until this ioctl completes.
9958 	 */
9959 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
9960 		qinfo = ipwq->q_qinfo;
9961 		name = qinfo->qi_minfo->mi_idname;
9962 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
9963 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
9964 			break;
9965 		}
9966 	}
9967 
9968 	/*
9969 	 * If this isn't an IP module stream, bail.
9970 	 */
9971 	if (ipwq == NULL)
9972 		return (0);
9973 
9974 	ill = ipwq->q_ptr;
9975 	ASSERT(ill != NULL);
9976 
9977 	if (ipsq == NULL) {
9978 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9979 		    NEW_OP, B_TRUE);
9980 		if (ipsq == NULL)
9981 			return (EINPROGRESS);
9982 		entered_ipsq = B_TRUE;
9983 	}
9984 	ASSERT(IAM_WRITER_ILL(ill));
9985 
9986 	if (doconsist) {
9987 		/*
9988 		 * Consistency checking requires that I_{P}LINK occurs
9989 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
9990 		 * occurs prior to clearing ill_arp_muxid.
9991 		 */
9992 		if ((islink && ill->ill_ip_muxid != 0) ||
9993 		    (!islink && ill->ill_arp_muxid != 0)) {
9994 			if (entered_ipsq)
9995 				ipsq_exit(ipsq);
9996 			return (EINVAL);
9997 		}
9998 	}
9999 
10000 	/*
10001 	 * As part of I_{P}LINKing, stash the number of downstream modules and
10002 	 * the read queue of the module immediately below IP in the ill.
10003 	 * These are used during the capability negotiation below.
10004 	 */
10005 	ill->ill_lmod_rq = NULL;
10006 	ill->ill_lmod_cnt = 0;
10007 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
10008 		ill->ill_lmod_rq = RD(dwq);
10009 		for (; dwq != NULL; dwq = dwq->q_next)
10010 			ill->ill_lmod_cnt++;
10011 	}
10012 
10013 	if (doconsist)
10014 		ill->ill_ip_muxid = islink ? li->l_index : 0;
10015 
10016 	/*
10017 	 * If there's at least one up ipif on this ill, then we're bound to
10018 	 * the underlying driver via DLPI.  In that case, renegotiate
10019 	 * capabilities to account for any possible change in modules
10020 	 * interposed between IP and the driver.
10021 	 */
10022 	if (ill->ill_ipif_up_count > 0) {
10023 		if (islink)
10024 			ill_capability_probe(ill);
10025 		else
10026 			ill_capability_reset(ill);
10027 	}
10028 
10029 	if (entered_ipsq)
10030 		ipsq_exit(ipsq);
10031 
10032 	return (0);
10033 }
10034 
10035 /*
10036  * Search the ioctl command in the ioctl tables and return a pointer
10037  * to the ioctl command information. The ioctl command tables are
10038  * static and fully populated at compile time.
10039  */
10040 ip_ioctl_cmd_t *
10041 ip_sioctl_lookup(int ioc_cmd)
10042 {
10043 	int index;
10044 	ip_ioctl_cmd_t *ipip;
10045 	ip_ioctl_cmd_t *ipip_end;
10046 
10047 	if (ioc_cmd == IPI_DONTCARE)
10048 		return (NULL);
10049 
10050 	/*
10051 	 * Do a 2 step search. First search the indexed table
10052 	 * based on the least significant byte of the ioctl cmd.
10053 	 * If we don't find a match, then search the misc table
10054 	 * serially.
10055 	 */
10056 	index = ioc_cmd & 0xFF;
10057 	if (index < ip_ndx_ioctl_count) {
10058 		ipip = &ip_ndx_ioctl_table[index];
10059 		if (ipip->ipi_cmd == ioc_cmd) {
10060 			/* Found a match in the ndx table */
10061 			return (ipip);
10062 		}
10063 	}
10064 
10065 	/* Search the misc table */
10066 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10067 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10068 		if (ipip->ipi_cmd == ioc_cmd)
10069 			/* Found a match in the misc table */
10070 			return (ipip);
10071 	}
10072 
10073 	return (NULL);
10074 }
10075 
10076 /*
10077  * Wrapper function for resuming deferred ioctl processing
10078  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10079  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10080  */
10081 /* ARGSUSED */
10082 void
10083 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10084     void *dummy_arg)
10085 {
10086 	ip_sioctl_copyin_setup(q, mp);
10087 }
10088 
10089 /*
10090  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10091  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10092  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10093  * We establish here the size of the block to be copied in.  mi_copyin
10094  * arranges for this to happen, an processing continues in ip_wput with
10095  * an M_IOCDATA message.
10096  */
10097 void
10098 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10099 {
10100 	int	copyin_size;
10101 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10102 	ip_ioctl_cmd_t *ipip;
10103 	cred_t *cr;
10104 	ip_stack_t	*ipst;
10105 
10106 	if (CONN_Q(q))
10107 		ipst = CONNQ_TO_IPST(q);
10108 	else
10109 		ipst = ILLQ_TO_IPST(q);
10110 
10111 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10112 	if (ipip == NULL) {
10113 		/*
10114 		 * The ioctl is not one we understand or own.
10115 		 * Pass it along to be processed down stream,
10116 		 * if this is a module instance of IP, else nak
10117 		 * the ioctl.
10118 		 */
10119 		if (q->q_next == NULL) {
10120 			goto nak;
10121 		} else {
10122 			putnext(q, mp);
10123 			return;
10124 		}
10125 	}
10126 
10127 	/*
10128 	 * If this is deferred, then we will do all the checks when we
10129 	 * come back.
10130 	 */
10131 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10132 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10133 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10134 		return;
10135 	}
10136 
10137 	/*
10138 	 * Only allow a very small subset of IP ioctls on this stream if
10139 	 * IP is a module and not a driver. Allowing ioctls to be processed
10140 	 * in this case may cause assert failures or data corruption.
10141 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10142 	 * ioctls allowed on an IP module stream, after which this stream
10143 	 * normally becomes a multiplexor (at which time the stream head
10144 	 * will fail all ioctls).
10145 	 */
10146 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10147 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10148 			/*
10149 			 * Pass common Streams ioctls which the IP
10150 			 * module does not own or consume along to
10151 			 * be processed down stream.
10152 			 */
10153 			putnext(q, mp);
10154 			return;
10155 		} else {
10156 			goto nak;
10157 		}
10158 	}
10159 
10160 	/* Make sure we have ioctl data to process. */
10161 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10162 		goto nak;
10163 
10164 	/*
10165 	 * Prefer dblk credential over ioctl credential; some synthesized
10166 	 * ioctls have kcred set because there's no way to crhold()
10167 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10168 	 * the framework; the caller of ioctl needs to hold the reference
10169 	 * for the duration of the call).
10170 	 */
10171 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10172 
10173 	/* Make sure normal users don't send down privileged ioctls */
10174 	if ((ipip->ipi_flags & IPI_PRIV) &&
10175 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10176 		/* We checked the privilege earlier but log it here */
10177 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10178 		return;
10179 	}
10180 
10181 	/*
10182 	 * The ioctl command tables can only encode fixed length
10183 	 * ioctl data. If the length is variable, the table will
10184 	 * encode the length as zero. Such special cases are handled
10185 	 * below in the switch.
10186 	 */
10187 	if (ipip->ipi_copyin_size != 0) {
10188 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10189 		return;
10190 	}
10191 
10192 	switch (iocp->ioc_cmd) {
10193 	case O_SIOCGIFCONF:
10194 	case SIOCGIFCONF:
10195 		/*
10196 		 * This IOCTL is hilarious.  See comments in
10197 		 * ip_sioctl_get_ifconf for the story.
10198 		 */
10199 		if (iocp->ioc_count == TRANSPARENT)
10200 			copyin_size = SIZEOF_STRUCT(ifconf,
10201 			    iocp->ioc_flag);
10202 		else
10203 			copyin_size = iocp->ioc_count;
10204 		mi_copyin(q, mp, NULL, copyin_size);
10205 		return;
10206 
10207 	case O_SIOCGLIFCONF:
10208 	case SIOCGLIFCONF:
10209 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10210 		mi_copyin(q, mp, NULL, copyin_size);
10211 		return;
10212 
10213 	case SIOCGLIFSRCOF:
10214 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10215 		mi_copyin(q, mp, NULL, copyin_size);
10216 		return;
10217 	case SIOCGIP6ADDRPOLICY:
10218 		ip_sioctl_ip6addrpolicy(q, mp);
10219 		ip6_asp_table_refrele(ipst);
10220 		return;
10221 
10222 	case SIOCSIP6ADDRPOLICY:
10223 		ip_sioctl_ip6addrpolicy(q, mp);
10224 		return;
10225 
10226 	case SIOCGDSTINFO:
10227 		ip_sioctl_dstinfo(q, mp);
10228 		ip6_asp_table_refrele(ipst);
10229 		return;
10230 
10231 	case I_PLINK:
10232 	case I_PUNLINK:
10233 	case I_LINK:
10234 	case I_UNLINK:
10235 		/*
10236 		 * We treat non-persistent link similarly as the persistent
10237 		 * link case, in terms of plumbing/unplumbing, as well as
10238 		 * dynamic re-plumbing events indicator.  See comments
10239 		 * in ip_sioctl_plink() for more.
10240 		 *
10241 		 * Request can be enqueued in the 'ipsq' while waiting
10242 		 * to become exclusive. So bump up the conn ref.
10243 		 */
10244 		if (CONN_Q(q))
10245 			CONN_INC_REF(Q_TO_CONN(q));
10246 		ip_sioctl_plink(NULL, q, mp, NULL);
10247 		return;
10248 
10249 	case ND_GET:
10250 	case ND_SET:
10251 		/*
10252 		 * Use of the nd table requires holding the reader lock.
10253 		 * Modifying the nd table thru nd_load/nd_unload requires
10254 		 * the writer lock.
10255 		 */
10256 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10257 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10258 			rw_exit(&ipst->ips_ip_g_nd_lock);
10259 
10260 			if (iocp->ioc_error)
10261 				iocp->ioc_count = 0;
10262 			mp->b_datap->db_type = M_IOCACK;
10263 			qreply(q, mp);
10264 			return;
10265 		}
10266 		rw_exit(&ipst->ips_ip_g_nd_lock);
10267 		/*
10268 		 * We don't understand this subioctl of ND_GET / ND_SET.
10269 		 * Maybe intended for some driver / module below us
10270 		 */
10271 		if (q->q_next) {
10272 			putnext(q, mp);
10273 		} else {
10274 			iocp->ioc_error = ENOENT;
10275 			mp->b_datap->db_type = M_IOCNAK;
10276 			iocp->ioc_count = 0;
10277 			qreply(q, mp);
10278 		}
10279 		return;
10280 
10281 	case IP_IOCTL:
10282 		ip_wput_ioctl(q, mp);
10283 		return;
10284 	default:
10285 		cmn_err(CE_PANIC, "should not happen ");
10286 	}
10287 nak:
10288 	if (mp->b_cont != NULL) {
10289 		freemsg(mp->b_cont);
10290 		mp->b_cont = NULL;
10291 	}
10292 	iocp->ioc_error = EINVAL;
10293 	mp->b_datap->db_type = M_IOCNAK;
10294 	iocp->ioc_count = 0;
10295 	qreply(q, mp);
10296 }
10297 
10298 /* ip_wput hands off ARP IOCTL responses to us */
10299 void
10300 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10301 {
10302 	struct arpreq *ar;
10303 	struct xarpreq *xar;
10304 	area_t	*area;
10305 	mblk_t	*area_mp;
10306 	struct iocblk *iocp;
10307 	mblk_t	*orig_ioc_mp, *tmp;
10308 	struct iocblk	*orig_iocp;
10309 	ill_t *ill;
10310 	conn_t *connp = NULL;
10311 	uint_t ioc_id;
10312 	mblk_t *pending_mp;
10313 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10314 	int *flagsp;
10315 	char *storage = NULL;
10316 	sin_t *sin;
10317 	ipaddr_t addr;
10318 	int err;
10319 	ip_stack_t *ipst;
10320 
10321 	ill = q->q_ptr;
10322 	ASSERT(ill != NULL);
10323 	ipst = ill->ill_ipst;
10324 
10325 	/*
10326 	 * We should get back from ARP a packet chain that looks like:
10327 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10328 	 */
10329 	if (!(area_mp = mp->b_cont) ||
10330 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10331 	    !(orig_ioc_mp = area_mp->b_cont) ||
10332 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10333 		freemsg(mp);
10334 		return;
10335 	}
10336 
10337 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10338 
10339 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10340 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10341 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10342 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10343 		x_arp_ioctl = B_TRUE;
10344 		xar = (struct xarpreq *)tmp->b_rptr;
10345 		sin = (sin_t *)&xar->xarp_pa;
10346 		flagsp = &xar->xarp_flags;
10347 		storage = xar->xarp_ha.sdl_data;
10348 		if (xar->xarp_ha.sdl_nlen != 0)
10349 			ifx_arp_ioctl = B_TRUE;
10350 	} else {
10351 		ar = (struct arpreq *)tmp->b_rptr;
10352 		sin = (sin_t *)&ar->arp_pa;
10353 		flagsp = &ar->arp_flags;
10354 		storage = ar->arp_ha.sa_data;
10355 	}
10356 
10357 	iocp = (struct iocblk *)mp->b_rptr;
10358 
10359 	/*
10360 	 * Pick out the originating queue based on the ioc_id.
10361 	 */
10362 	ioc_id = iocp->ioc_id;
10363 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10364 	if (pending_mp == NULL) {
10365 		ASSERT(connp == NULL);
10366 		inet_freemsg(mp);
10367 		return;
10368 	}
10369 	ASSERT(connp != NULL);
10370 	q = CONNP_TO_WQ(connp);
10371 
10372 	/* Uncouple the internally generated IOCTL from the original one */
10373 	area = (area_t *)area_mp->b_rptr;
10374 	area_mp->b_cont = NULL;
10375 
10376 	/*
10377 	 * Restore the b_next and b_prev used by mi code. This is needed
10378 	 * to complete the ioctl using mi* functions. We stored them in
10379 	 * the pending mp prior to sending the request to ARP.
10380 	 */
10381 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10382 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10383 	inet_freemsg(pending_mp);
10384 
10385 	/*
10386 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10387 	 * Catch the case where there is an IRE_CACHE by no entry in the
10388 	 * arp table.
10389 	 */
10390 	addr = sin->sin_addr.s_addr;
10391 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10392 		ire_t			*ire;
10393 		dl_unitdata_req_t	*dlup;
10394 		mblk_t			*llmp;
10395 		int			addr_len;
10396 		ill_t			*ipsqill = NULL;
10397 
10398 		if (ifx_arp_ioctl) {
10399 			/*
10400 			 * There's no need to lookup the ill, since
10401 			 * we've already done that when we started
10402 			 * processing the ioctl and sent the message
10403 			 * to ARP on that ill.  So use the ill that
10404 			 * is stored in q->q_ptr.
10405 			 */
10406 			ipsqill = ill;
10407 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10408 			    ipsqill->ill_ipif, ALL_ZONES,
10409 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10410 		} else {
10411 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10412 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10413 			if (ire != NULL)
10414 				ipsqill = ire_to_ill(ire);
10415 		}
10416 
10417 		if ((x_arp_ioctl) && (ipsqill != NULL))
10418 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10419 
10420 		if (ire != NULL) {
10421 			/*
10422 			 * Since the ire obtained from cachetable is used for
10423 			 * mac addr copying below, treat an incomplete ire as if
10424 			 * as if we never found it.
10425 			 */
10426 			if (ire->ire_nce != NULL &&
10427 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10428 				ire_refrele(ire);
10429 				ire = NULL;
10430 				ipsqill = NULL;
10431 				goto errack;
10432 			}
10433 			*flagsp = ATF_INUSE;
10434 			llmp = (ire->ire_nce != NULL ?
10435 			    ire->ire_nce->nce_res_mp : NULL);
10436 			if (llmp != NULL && ipsqill != NULL) {
10437 				uchar_t *macaddr;
10438 
10439 				addr_len = ipsqill->ill_phys_addr_length;
10440 				if (x_arp_ioctl && ((addr_len +
10441 				    ipsqill->ill_name_length) >
10442 				    sizeof (xar->xarp_ha.sdl_data))) {
10443 					ire_refrele(ire);
10444 					freemsg(mp);
10445 					ip_ioctl_finish(q, orig_ioc_mp,
10446 					    EINVAL, NO_COPYOUT, NULL);
10447 					return;
10448 				}
10449 				*flagsp |= ATF_COM;
10450 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10451 				if (ipsqill->ill_sap_length < 0)
10452 					macaddr = llmp->b_rptr +
10453 					    dlup->dl_dest_addr_offset;
10454 				else
10455 					macaddr = llmp->b_rptr +
10456 					    dlup->dl_dest_addr_offset +
10457 					    ipsqill->ill_sap_length;
10458 				/*
10459 				 * For SIOCGARP, MAC address length
10460 				 * validation has already been done
10461 				 * before the ioctl was issued to ARP to
10462 				 * allow it to progress only on 6 byte
10463 				 * addressable (ethernet like) media. Thus
10464 				 * the mac address copying can not overwrite
10465 				 * the sa_data area below.
10466 				 */
10467 				bcopy(macaddr, storage, addr_len);
10468 			}
10469 			/* Ditch the internal IOCTL. */
10470 			freemsg(mp);
10471 			ire_refrele(ire);
10472 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10473 			return;
10474 		}
10475 	}
10476 
10477 	/*
10478 	 * Delete the coresponding IRE_CACHE if any.
10479 	 * Reset the error if there was one (in case there was no entry
10480 	 * in arp.)
10481 	 */
10482 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10483 		ipif_t *ipintf = NULL;
10484 
10485 		if (ifx_arp_ioctl) {
10486 			/*
10487 			 * There's no need to lookup the ill, since
10488 			 * we've already done that when we started
10489 			 * processing the ioctl and sent the message
10490 			 * to ARP on that ill.  So use the ill that
10491 			 * is stored in q->q_ptr.
10492 			 */
10493 			ipintf = ill->ill_ipif;
10494 		}
10495 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10496 			/*
10497 			 * The address in "addr" may be an entry for a
10498 			 * router. If that's true, then any off-net
10499 			 * IRE_CACHE entries that go through the router
10500 			 * with address "addr" must be clobbered. Use
10501 			 * ire_walk to achieve this goal.
10502 			 */
10503 			if (ifx_arp_ioctl)
10504 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10505 				    ire_delete_cache_gw, (char *)&addr, ill);
10506 			else
10507 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10508 				    ALL_ZONES, ipst);
10509 			iocp->ioc_error = 0;
10510 		}
10511 	}
10512 errack:
10513 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10514 		err = iocp->ioc_error;
10515 		freemsg(mp);
10516 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10517 		return;
10518 	}
10519 
10520 	/*
10521 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10522 	 * the area_t into the struct {x}arpreq.
10523 	 */
10524 	if (x_arp_ioctl) {
10525 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10526 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10527 		    sizeof (xar->xarp_ha.sdl_data)) {
10528 			freemsg(mp);
10529 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10530 			    NULL);
10531 			return;
10532 		}
10533 	}
10534 	*flagsp = ATF_INUSE;
10535 	if (area->area_flags & ACE_F_PERMANENT)
10536 		*flagsp |= ATF_PERM;
10537 	if (area->area_flags & ACE_F_PUBLISH)
10538 		*flagsp |= ATF_PUBL;
10539 	if (area->area_flags & ACE_F_AUTHORITY)
10540 		*flagsp |= ATF_AUTHORITY;
10541 	if (area->area_hw_addr_length != 0) {
10542 		*flagsp |= ATF_COM;
10543 		/*
10544 		 * For SIOCGARP, MAC address length validation has
10545 		 * already been done before the ioctl was issued to ARP
10546 		 * to allow it to progress only on 6 byte addressable
10547 		 * (ethernet like) media. Thus the mac address copying
10548 		 * can not overwrite the sa_data area below.
10549 		 */
10550 		bcopy((char *)area + area->area_hw_addr_offset,
10551 		    storage, area->area_hw_addr_length);
10552 	}
10553 
10554 	/* Ditch the internal IOCTL. */
10555 	freemsg(mp);
10556 	/* Complete the original. */
10557 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10558 }
10559 
10560 /*
10561  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10562  * interface) create the next available logical interface for this
10563  * physical interface.
10564  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10565  * ipif with the specified name.
10566  *
10567  * If the address family is not AF_UNSPEC then set the address as well.
10568  *
10569  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10570  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10571  *
10572  * Executed as a writer on the ill or ill group.
10573  * So no lock is needed to traverse the ipif chain, or examine the
10574  * phyint flags.
10575  */
10576 /* ARGSUSED */
10577 int
10578 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10579     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10580 {
10581 	mblk_t	*mp1;
10582 	struct lifreq *lifr;
10583 	boolean_t	isv6;
10584 	boolean_t	exists;
10585 	char 	*name;
10586 	char	*endp;
10587 	char	*cp;
10588 	int	namelen;
10589 	ipif_t	*ipif;
10590 	long	id;
10591 	ipsq_t	*ipsq;
10592 	ill_t	*ill;
10593 	sin_t	*sin;
10594 	int	err = 0;
10595 	boolean_t found_sep = B_FALSE;
10596 	conn_t	*connp;
10597 	zoneid_t zoneid;
10598 	int	orig_ifindex = 0;
10599 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10600 
10601 	ASSERT(q->q_next == NULL);
10602 	ip1dbg(("ip_sioctl_addif\n"));
10603 	/* Existence of mp1 has been checked in ip_wput_nondata */
10604 	mp1 = mp->b_cont->b_cont;
10605 	/*
10606 	 * Null terminate the string to protect against buffer
10607 	 * overrun. String was generated by user code and may not
10608 	 * be trusted.
10609 	 */
10610 	lifr = (struct lifreq *)mp1->b_rptr;
10611 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10612 	name = lifr->lifr_name;
10613 	ASSERT(CONN_Q(q));
10614 	connp = Q_TO_CONN(q);
10615 	isv6 = connp->conn_af_isv6;
10616 	zoneid = connp->conn_zoneid;
10617 	namelen = mi_strlen(name);
10618 	if (namelen == 0)
10619 		return (EINVAL);
10620 
10621 	exists = B_FALSE;
10622 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10623 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10624 		/*
10625 		 * Allow creating lo0 using SIOCLIFADDIF.
10626 		 * can't be any other writer thread. So can pass null below
10627 		 * for the last 4 args to ipif_lookup_name.
10628 		 */
10629 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10630 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10631 		/* Prevent any further action */
10632 		if (ipif == NULL) {
10633 			return (ENOBUFS);
10634 		} else if (!exists) {
10635 			/* We created the ipif now and as writer */
10636 			ipif_refrele(ipif);
10637 			return (0);
10638 		} else {
10639 			ill = ipif->ipif_ill;
10640 			ill_refhold(ill);
10641 			ipif_refrele(ipif);
10642 		}
10643 	} else {
10644 		/* Look for a colon in the name. */
10645 		endp = &name[namelen];
10646 		for (cp = endp; --cp > name; ) {
10647 			if (*cp == IPIF_SEPARATOR_CHAR) {
10648 				found_sep = B_TRUE;
10649 				/*
10650 				 * Reject any non-decimal aliases for plumbing
10651 				 * of logical interfaces. Aliases with leading
10652 				 * zeroes are also rejected as they introduce
10653 				 * ambiguity in the naming of the interfaces.
10654 				 * Comparing with "0" takes care of all such
10655 				 * cases.
10656 				 */
10657 				if ((strncmp("0", cp+1, 1)) == 0)
10658 					return (EINVAL);
10659 
10660 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10661 				    id <= 0 || *endp != '\0') {
10662 					return (EINVAL);
10663 				}
10664 				*cp = '\0';
10665 				break;
10666 			}
10667 		}
10668 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10669 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10670 		if (found_sep)
10671 			*cp = IPIF_SEPARATOR_CHAR;
10672 		if (ill == NULL)
10673 			return (err);
10674 	}
10675 
10676 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10677 	    B_TRUE);
10678 
10679 	/*
10680 	 * Release the refhold due to the lookup, now that we are excl
10681 	 * or we are just returning
10682 	 */
10683 	ill_refrele(ill);
10684 
10685 	if (ipsq == NULL)
10686 		return (EINPROGRESS);
10687 
10688 	/*
10689 	 * If the interface is failed, inactive or offlined, look for a working
10690 	 * interface in the ill group and create the ipif there. If we can't
10691 	 * find a good interface, create the ipif anyway so that in.mpathd can
10692 	 * move it to the first repaired interface.
10693 	 */
10694 	if ((ill->ill_phyint->phyint_flags &
10695 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10696 	    ill->ill_phyint->phyint_groupname_len != 0) {
10697 		phyint_t *phyi;
10698 		char *groupname = ill->ill_phyint->phyint_groupname;
10699 
10700 		/*
10701 		 * We're looking for a working interface, but it doesn't matter
10702 		 * if it's up or down; so instead of following the group lists,
10703 		 * we look at each physical interface and compare the groupname.
10704 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10705 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10706 		 * Otherwise we create the ipif on the failed interface.
10707 		 */
10708 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10709 		phyi = avl_first(&ipst->ips_phyint_g_list->
10710 		    phyint_list_avl_by_index);
10711 		for (; phyi != NULL;
10712 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10713 		    phyint_list_avl_by_index,
10714 		    phyi, AVL_AFTER)) {
10715 			if (phyi->phyint_groupname_len == 0)
10716 				continue;
10717 			ASSERT(phyi->phyint_groupname != NULL);
10718 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10719 			    !(phyi->phyint_flags &
10720 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10721 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10722 			    (phyi->phyint_illv4 != NULL))) {
10723 				break;
10724 			}
10725 		}
10726 		rw_exit(&ipst->ips_ill_g_lock);
10727 
10728 		if (phyi != NULL) {
10729 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10730 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10731 			    phyi->phyint_illv4);
10732 		}
10733 	}
10734 
10735 	/*
10736 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10737 	 * before or after us.
10738 	 */
10739 	ASSERT(IAM_WRITER_ILL(ill));
10740 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10741 
10742 	if (found_sep && orig_ifindex == 0) {
10743 		/* Now see if there is an IPIF with this unit number. */
10744 		for (ipif = ill->ill_ipif; ipif != NULL;
10745 		    ipif = ipif->ipif_next) {
10746 			if (ipif->ipif_id == id) {
10747 				err = EEXIST;
10748 				goto done;
10749 			}
10750 		}
10751 	}
10752 
10753 	/*
10754 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10755 	 * of lo0. We never come here when we plumb lo0:0. It
10756 	 * happens in ipif_lookup_on_name.
10757 	 * The specified unit number is ignored when we create the ipif on a
10758 	 * different interface. However, we save it in ipif_orig_ipifid below so
10759 	 * that the ipif fails back to the right position.
10760 	 */
10761 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10762 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10763 		err = ENOBUFS;
10764 		goto done;
10765 	}
10766 
10767 	/* Return created name with ioctl */
10768 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10769 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10770 	ip1dbg(("created %s\n", lifr->lifr_name));
10771 
10772 	/* Set address */
10773 	sin = (sin_t *)&lifr->lifr_addr;
10774 	if (sin->sin_family != AF_UNSPEC) {
10775 		err = ip_sioctl_addr(ipif, sin, q, mp,
10776 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10777 	}
10778 
10779 	/* Set ifindex and unit number for failback */
10780 	if (err == 0 && orig_ifindex != 0) {
10781 		ipif->ipif_orig_ifindex = orig_ifindex;
10782 		if (found_sep) {
10783 			ipif->ipif_orig_ipifid = id;
10784 		}
10785 	}
10786 
10787 done:
10788 	ipsq_exit(ipsq);
10789 	return (err);
10790 }
10791 
10792 /*
10793  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10794  * interface) delete it based on the IP address (on this physical interface).
10795  * Otherwise delete it based on the ipif_id.
10796  * Also, special handling to allow a removeif of lo0.
10797  */
10798 /* ARGSUSED */
10799 int
10800 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10801     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10802 {
10803 	conn_t		*connp;
10804 	ill_t		*ill = ipif->ipif_ill;
10805 	boolean_t	 success;
10806 	ip_stack_t	*ipst;
10807 
10808 	ipst = CONNQ_TO_IPST(q);
10809 
10810 	ASSERT(q->q_next == NULL);
10811 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10812 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10813 	ASSERT(IAM_WRITER_IPIF(ipif));
10814 
10815 	connp = Q_TO_CONN(q);
10816 	/*
10817 	 * Special case for unplumbing lo0 (the loopback physical interface).
10818 	 * If unplumbing lo0, the incoming address structure has been
10819 	 * initialized to all zeros. When unplumbing lo0, all its logical
10820 	 * interfaces must be removed too.
10821 	 *
10822 	 * Note that this interface may be called to remove a specific
10823 	 * loopback logical interface (eg, lo0:1). But in that case
10824 	 * ipif->ipif_id != 0 so that the code path for that case is the
10825 	 * same as any other interface (meaning it skips the code directly
10826 	 * below).
10827 	 */
10828 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10829 		if (sin->sin_family == AF_UNSPEC &&
10830 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10831 			/*
10832 			 * Mark it condemned. No new ref. will be made to ill.
10833 			 */
10834 			mutex_enter(&ill->ill_lock);
10835 			ill->ill_state_flags |= ILL_CONDEMNED;
10836 			for (ipif = ill->ill_ipif; ipif != NULL;
10837 			    ipif = ipif->ipif_next) {
10838 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10839 			}
10840 			mutex_exit(&ill->ill_lock);
10841 
10842 			ipif = ill->ill_ipif;
10843 			/* unplumb the loopback interface */
10844 			ill_delete(ill);
10845 			mutex_enter(&connp->conn_lock);
10846 			mutex_enter(&ill->ill_lock);
10847 			ASSERT(ill->ill_group == NULL);
10848 
10849 			/* Are any references to this ill active */
10850 			if (ill_is_freeable(ill)) {
10851 				mutex_exit(&ill->ill_lock);
10852 				mutex_exit(&connp->conn_lock);
10853 				ill_delete_tail(ill);
10854 				mutex_enter(&ill->ill_lock);
10855 				ill_nic_info_dispatch(ill);
10856 				mutex_exit(&ill->ill_lock);
10857 				mi_free(ill);
10858 				return (0);
10859 			}
10860 			success = ipsq_pending_mp_add(connp, ipif,
10861 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10862 			mutex_exit(&connp->conn_lock);
10863 			mutex_exit(&ill->ill_lock);
10864 			if (success)
10865 				return (EINPROGRESS);
10866 			else
10867 				return (EINTR);
10868 		}
10869 	}
10870 
10871 	/*
10872 	 * We are exclusive on the ipsq, so an ill move will be serialized
10873 	 * before or after us.
10874 	 */
10875 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10876 
10877 	if (ipif->ipif_id == 0) {
10878 
10879 		ipsq_t *ipsq;
10880 
10881 		/* Find based on address */
10882 		if (ipif->ipif_isv6) {
10883 			sin6_t *sin6;
10884 
10885 			if (sin->sin_family != AF_INET6)
10886 				return (EAFNOSUPPORT);
10887 
10888 			sin6 = (sin6_t *)sin;
10889 			/* We are a writer, so we should be able to lookup */
10890 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10891 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
10892 			if (ipif == NULL) {
10893 				/*
10894 				 * Maybe the address in on another interface in
10895 				 * the same IPMP group? We check this below.
10896 				 */
10897 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10898 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
10899 				    ipst);
10900 			}
10901 		} else {
10902 			ipaddr_t addr;
10903 
10904 			if (sin->sin_family != AF_INET)
10905 				return (EAFNOSUPPORT);
10906 
10907 			addr = sin->sin_addr.s_addr;
10908 			/* We are a writer, so we should be able to lookup */
10909 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10910 			    NULL, NULL, NULL, ipst);
10911 			if (ipif == NULL) {
10912 				/*
10913 				 * Maybe the address in on another interface in
10914 				 * the same IPMP group? We check this below.
10915 				 */
10916 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10917 				    NULL, NULL, NULL, NULL, ipst);
10918 			}
10919 		}
10920 		if (ipif == NULL) {
10921 			return (EADDRNOTAVAIL);
10922 		}
10923 
10924 		/*
10925 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
10926 		 * lifr_name of the physical interface but with an ip address
10927 		 * lifr_addr of a logical interface plumbed over it.
10928 		 * So update ipsq_current_ipif once ipif points to the
10929 		 * correct interface after doing ipif_lookup_addr().
10930 		 */
10931 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
10932 		ASSERT(ipsq != NULL);
10933 
10934 		mutex_enter(&ipsq->ipsq_lock);
10935 		ipsq->ipsq_current_ipif = ipif;
10936 		mutex_exit(&ipsq->ipsq_lock);
10937 
10938 		/*
10939 		 * When the address to be removed is hosted on a different
10940 		 * interface, we check if the interface is in the same IPMP
10941 		 * group as the specified one; if so we proceed with the
10942 		 * removal.
10943 		 * ill->ill_group is NULL when the ill is down, so we have to
10944 		 * compare the group names instead.
10945 		 */
10946 		if (ipif->ipif_ill != ill &&
10947 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10948 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10949 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10950 		    ill->ill_phyint->phyint_groupname) != 0)) {
10951 			ipif_refrele(ipif);
10952 			return (EADDRNOTAVAIL);
10953 		}
10954 
10955 		/* This is a writer */
10956 		ipif_refrele(ipif);
10957 	}
10958 
10959 	/*
10960 	 * Can not delete instance zero since it is tied to the ill.
10961 	 */
10962 	if (ipif->ipif_id == 0)
10963 		return (EBUSY);
10964 
10965 	mutex_enter(&ill->ill_lock);
10966 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10967 	mutex_exit(&ill->ill_lock);
10968 
10969 	ipif_free(ipif);
10970 
10971 	mutex_enter(&connp->conn_lock);
10972 	mutex_enter(&ill->ill_lock);
10973 
10974 
10975 	/* Are any references to this ipif active */
10976 	if (ipif_is_freeable(ipif)) {
10977 		mutex_exit(&ill->ill_lock);
10978 		mutex_exit(&connp->conn_lock);
10979 		ipif_non_duplicate(ipif);
10980 		ipif_down_tail(ipif);
10981 		ipif_free_tail(ipif); /* frees ipif */
10982 		return (0);
10983 	}
10984 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10985 	    IPIF_FREE);
10986 	mutex_exit(&ill->ill_lock);
10987 	mutex_exit(&connp->conn_lock);
10988 	if (success)
10989 		return (EINPROGRESS);
10990 	else
10991 		return (EINTR);
10992 }
10993 
10994 /*
10995  * Restart the removeif ioctl. The refcnt has gone down to 0.
10996  * The ipif is already condemned. So can't find it thru lookups.
10997  */
10998 /* ARGSUSED */
10999 int
11000 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
11001     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11002 {
11003 	ill_t *ill = ipif->ipif_ill;
11004 
11005 	ASSERT(IAM_WRITER_IPIF(ipif));
11006 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
11007 
11008 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
11009 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11010 
11011 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11012 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
11013 		ill_delete_tail(ill);
11014 		mutex_enter(&ill->ill_lock);
11015 		ill_nic_info_dispatch(ill);
11016 		mutex_exit(&ill->ill_lock);
11017 		mi_free(ill);
11018 		return (0);
11019 	}
11020 
11021 	ipif_non_duplicate(ipif);
11022 	ipif_down_tail(ipif);
11023 	ipif_free_tail(ipif);
11024 
11025 	ILL_UNMARK_CHANGING(ill);
11026 	return (0);
11027 }
11028 
11029 /*
11030  * Set the local interface address.
11031  * Allow an address of all zero when the interface is down.
11032  */
11033 /* ARGSUSED */
11034 int
11035 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11036     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11037 {
11038 	int err = 0;
11039 	in6_addr_t v6addr;
11040 	boolean_t need_up = B_FALSE;
11041 
11042 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11043 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11044 
11045 	ASSERT(IAM_WRITER_IPIF(ipif));
11046 
11047 	if (ipif->ipif_isv6) {
11048 		sin6_t *sin6;
11049 		ill_t *ill;
11050 		phyint_t *phyi;
11051 
11052 		if (sin->sin_family != AF_INET6)
11053 			return (EAFNOSUPPORT);
11054 
11055 		sin6 = (sin6_t *)sin;
11056 		v6addr = sin6->sin6_addr;
11057 		ill = ipif->ipif_ill;
11058 		phyi = ill->ill_phyint;
11059 
11060 		/*
11061 		 * Enforce that true multicast interfaces have a link-local
11062 		 * address for logical unit 0.
11063 		 */
11064 		if (ipif->ipif_id == 0 &&
11065 		    (ill->ill_flags & ILLF_MULTICAST) &&
11066 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11067 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11068 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11069 			return (EADDRNOTAVAIL);
11070 		}
11071 
11072 		/*
11073 		 * up interfaces shouldn't have the unspecified address
11074 		 * unless they also have the IPIF_NOLOCAL flags set and
11075 		 * have a subnet assigned.
11076 		 */
11077 		if ((ipif->ipif_flags & IPIF_UP) &&
11078 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11079 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11080 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11081 			return (EADDRNOTAVAIL);
11082 		}
11083 
11084 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11085 			return (EADDRNOTAVAIL);
11086 	} else {
11087 		ipaddr_t addr;
11088 
11089 		if (sin->sin_family != AF_INET)
11090 			return (EAFNOSUPPORT);
11091 
11092 		addr = sin->sin_addr.s_addr;
11093 
11094 		/* Allow 0 as the local address. */
11095 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11096 			return (EADDRNOTAVAIL);
11097 
11098 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11099 	}
11100 
11101 	/*
11102 	 * Even if there is no change we redo things just to rerun
11103 	 * ipif_set_default.
11104 	 */
11105 	if (ipif->ipif_flags & IPIF_UP) {
11106 		/*
11107 		 * Setting a new local address, make sure
11108 		 * we have net and subnet bcast ire's for
11109 		 * the old address if we need them.
11110 		 */
11111 		if (!ipif->ipif_isv6)
11112 			ipif_check_bcast_ires(ipif);
11113 		/*
11114 		 * If the interface is already marked up,
11115 		 * we call ipif_down which will take care
11116 		 * of ditching any IREs that have been set
11117 		 * up based on the old interface address.
11118 		 */
11119 		err = ipif_logical_down(ipif, q, mp);
11120 		if (err == EINPROGRESS)
11121 			return (err);
11122 		ipif_down_tail(ipif);
11123 		need_up = 1;
11124 	}
11125 
11126 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11127 	return (err);
11128 }
11129 
11130 int
11131 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11132     boolean_t need_up)
11133 {
11134 	in6_addr_t v6addr;
11135 	in6_addr_t ov6addr;
11136 	ipaddr_t addr;
11137 	sin6_t	*sin6;
11138 	int	sinlen;
11139 	int	err = 0;
11140 	ill_t	*ill = ipif->ipif_ill;
11141 	boolean_t need_dl_down;
11142 	boolean_t need_arp_down;
11143 	struct iocblk *iocp;
11144 
11145 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11146 
11147 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11148 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11149 	ASSERT(IAM_WRITER_IPIF(ipif));
11150 
11151 	/* Must cancel any pending timer before taking the ill_lock */
11152 	if (ipif->ipif_recovery_id != 0)
11153 		(void) untimeout(ipif->ipif_recovery_id);
11154 	ipif->ipif_recovery_id = 0;
11155 
11156 	if (ipif->ipif_isv6) {
11157 		sin6 = (sin6_t *)sin;
11158 		v6addr = sin6->sin6_addr;
11159 		sinlen = sizeof (struct sockaddr_in6);
11160 	} else {
11161 		addr = sin->sin_addr.s_addr;
11162 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11163 		sinlen = sizeof (struct sockaddr_in);
11164 	}
11165 	mutex_enter(&ill->ill_lock);
11166 	ov6addr = ipif->ipif_v6lcl_addr;
11167 	ipif->ipif_v6lcl_addr = v6addr;
11168 	sctp_update_ipif_addr(ipif, ov6addr);
11169 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11170 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11171 	} else {
11172 		ipif->ipif_v6src_addr = v6addr;
11173 	}
11174 	ipif->ipif_addr_ready = 0;
11175 
11176 	/*
11177 	 * If the interface was previously marked as a duplicate, then since
11178 	 * we've now got a "new" address, it should no longer be considered a
11179 	 * duplicate -- even if the "new" address is the same as the old one.
11180 	 * Note that if all ipifs are down, we may have a pending ARP down
11181 	 * event to handle.  This is because we want to recover from duplicates
11182 	 * and thus delay tearing down ARP until the duplicates have been
11183 	 * removed or disabled.
11184 	 */
11185 	need_dl_down = need_arp_down = B_FALSE;
11186 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11187 		need_arp_down = !need_up;
11188 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11189 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11190 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11191 			need_dl_down = B_TRUE;
11192 		}
11193 	}
11194 
11195 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11196 	    !ill->ill_is_6to4tun) {
11197 		queue_t *wqp = ill->ill_wq;
11198 
11199 		/*
11200 		 * The local address of this interface is a 6to4 address,
11201 		 * check if this interface is in fact a 6to4 tunnel or just
11202 		 * an interface configured with a 6to4 address.  We are only
11203 		 * interested in the former.
11204 		 */
11205 		if (wqp != NULL) {
11206 			while ((wqp->q_next != NULL) &&
11207 			    (wqp->q_next->q_qinfo != NULL) &&
11208 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11209 
11210 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11211 				    == TUN6TO4_MODID) {
11212 					/* set for use in IP */
11213 					ill->ill_is_6to4tun = 1;
11214 					break;
11215 				}
11216 				wqp = wqp->q_next;
11217 			}
11218 		}
11219 	}
11220 
11221 	ipif_set_default(ipif);
11222 
11223 	/*
11224 	 * When publishing an interface address change event, we only notify
11225 	 * the event listeners of the new address.  It is assumed that if they
11226 	 * actively care about the addresses assigned that they will have
11227 	 * already discovered the previous address assigned (if there was one.)
11228 	 *
11229 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11230 	 */
11231 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11232 		(void) ill_hook_event_create(ill, MAP_IPIF_ID(ipif->ipif_id),
11233 		    NE_ADDRESS_CHANGE, sin, sinlen);
11234 	}
11235 
11236 	mutex_exit(&ill->ill_lock);
11237 
11238 	if (need_up) {
11239 		/*
11240 		 * Now bring the interface back up.  If this
11241 		 * is the only IPIF for the ILL, ipif_up
11242 		 * will have to re-bind to the device, so
11243 		 * we may get back EINPROGRESS, in which
11244 		 * case, this IOCTL will get completed in
11245 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11246 		 */
11247 		err = ipif_up(ipif, q, mp);
11248 	}
11249 
11250 	if (need_dl_down)
11251 		ill_dl_down(ill);
11252 	if (need_arp_down)
11253 		ipif_arp_down(ipif);
11254 
11255 	return (err);
11256 }
11257 
11258 
11259 /*
11260  * Restart entry point to restart the address set operation after the
11261  * refcounts have dropped to zero.
11262  */
11263 /* ARGSUSED */
11264 int
11265 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11266     ip_ioctl_cmd_t *ipip, void *ifreq)
11267 {
11268 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11269 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11270 	ASSERT(IAM_WRITER_IPIF(ipif));
11271 	ipif_down_tail(ipif);
11272 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11273 }
11274 
11275 /* ARGSUSED */
11276 int
11277 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11278     ip_ioctl_cmd_t *ipip, void *if_req)
11279 {
11280 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11281 	struct lifreq *lifr = (struct lifreq *)if_req;
11282 
11283 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11284 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11285 	/*
11286 	 * The net mask and address can't change since we have a
11287 	 * reference to the ipif. So no lock is necessary.
11288 	 */
11289 	if (ipif->ipif_isv6) {
11290 		*sin6 = sin6_null;
11291 		sin6->sin6_family = AF_INET6;
11292 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11293 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11294 		lifr->lifr_addrlen =
11295 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11296 	} else {
11297 		*sin = sin_null;
11298 		sin->sin_family = AF_INET;
11299 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11300 		if (ipip->ipi_cmd_type == LIF_CMD) {
11301 			lifr->lifr_addrlen =
11302 			    ip_mask_to_plen(ipif->ipif_net_mask);
11303 		}
11304 	}
11305 	return (0);
11306 }
11307 
11308 /*
11309  * Set the destination address for a pt-pt interface.
11310  */
11311 /* ARGSUSED */
11312 int
11313 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11314     ip_ioctl_cmd_t *ipip, void *if_req)
11315 {
11316 	int err = 0;
11317 	in6_addr_t v6addr;
11318 	boolean_t need_up = B_FALSE;
11319 
11320 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11321 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11322 	ASSERT(IAM_WRITER_IPIF(ipif));
11323 
11324 	if (ipif->ipif_isv6) {
11325 		sin6_t *sin6;
11326 
11327 		if (sin->sin_family != AF_INET6)
11328 			return (EAFNOSUPPORT);
11329 
11330 		sin6 = (sin6_t *)sin;
11331 		v6addr = sin6->sin6_addr;
11332 
11333 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11334 			return (EADDRNOTAVAIL);
11335 	} else {
11336 		ipaddr_t addr;
11337 
11338 		if (sin->sin_family != AF_INET)
11339 			return (EAFNOSUPPORT);
11340 
11341 		addr = sin->sin_addr.s_addr;
11342 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11343 			return (EADDRNOTAVAIL);
11344 
11345 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11346 	}
11347 
11348 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11349 		return (0);	/* No change */
11350 
11351 	if (ipif->ipif_flags & IPIF_UP) {
11352 		/*
11353 		 * If the interface is already marked up,
11354 		 * we call ipif_down which will take care
11355 		 * of ditching any IREs that have been set
11356 		 * up based on the old pp dst address.
11357 		 */
11358 		err = ipif_logical_down(ipif, q, mp);
11359 		if (err == EINPROGRESS)
11360 			return (err);
11361 		ipif_down_tail(ipif);
11362 		need_up = B_TRUE;
11363 	}
11364 	/*
11365 	 * could return EINPROGRESS. If so ioctl will complete in
11366 	 * ip_rput_dlpi_writer
11367 	 */
11368 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11369 	return (err);
11370 }
11371 
11372 static int
11373 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11374     boolean_t need_up)
11375 {
11376 	in6_addr_t v6addr;
11377 	ill_t	*ill = ipif->ipif_ill;
11378 	int	err = 0;
11379 	boolean_t need_dl_down;
11380 	boolean_t need_arp_down;
11381 
11382 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11383 	    ipif->ipif_id, (void *)ipif));
11384 
11385 	/* Must cancel any pending timer before taking the ill_lock */
11386 	if (ipif->ipif_recovery_id != 0)
11387 		(void) untimeout(ipif->ipif_recovery_id);
11388 	ipif->ipif_recovery_id = 0;
11389 
11390 	if (ipif->ipif_isv6) {
11391 		sin6_t *sin6;
11392 
11393 		sin6 = (sin6_t *)sin;
11394 		v6addr = sin6->sin6_addr;
11395 	} else {
11396 		ipaddr_t addr;
11397 
11398 		addr = sin->sin_addr.s_addr;
11399 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11400 	}
11401 	mutex_enter(&ill->ill_lock);
11402 	/* Set point to point destination address. */
11403 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11404 		/*
11405 		 * Allow this as a means of creating logical
11406 		 * pt-pt interfaces on top of e.g. an Ethernet.
11407 		 * XXX Undocumented HACK for testing.
11408 		 * pt-pt interfaces are created with NUD disabled.
11409 		 */
11410 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11411 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11412 		if (ipif->ipif_isv6)
11413 			ill->ill_flags |= ILLF_NONUD;
11414 	}
11415 
11416 	/*
11417 	 * If the interface was previously marked as a duplicate, then since
11418 	 * we've now got a "new" address, it should no longer be considered a
11419 	 * duplicate -- even if the "new" address is the same as the old one.
11420 	 * Note that if all ipifs are down, we may have a pending ARP down
11421 	 * event to handle.
11422 	 */
11423 	need_dl_down = need_arp_down = B_FALSE;
11424 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11425 		need_arp_down = !need_up;
11426 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11427 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11428 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11429 			need_dl_down = B_TRUE;
11430 		}
11431 	}
11432 
11433 	/* Set the new address. */
11434 	ipif->ipif_v6pp_dst_addr = v6addr;
11435 	/* Make sure subnet tracks pp_dst */
11436 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11437 	mutex_exit(&ill->ill_lock);
11438 
11439 	if (need_up) {
11440 		/*
11441 		 * Now bring the interface back up.  If this
11442 		 * is the only IPIF for the ILL, ipif_up
11443 		 * will have to re-bind to the device, so
11444 		 * we may get back EINPROGRESS, in which
11445 		 * case, this IOCTL will get completed in
11446 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11447 		 */
11448 		err = ipif_up(ipif, q, mp);
11449 	}
11450 
11451 	if (need_dl_down)
11452 		ill_dl_down(ill);
11453 
11454 	if (need_arp_down)
11455 		ipif_arp_down(ipif);
11456 	return (err);
11457 }
11458 
11459 /*
11460  * Restart entry point to restart the dstaddress set operation after the
11461  * refcounts have dropped to zero.
11462  */
11463 /* ARGSUSED */
11464 int
11465 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11466     ip_ioctl_cmd_t *ipip, void *ifreq)
11467 {
11468 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11469 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11470 	ipif_down_tail(ipif);
11471 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11472 }
11473 
11474 /* ARGSUSED */
11475 int
11476 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11477     ip_ioctl_cmd_t *ipip, void *if_req)
11478 {
11479 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11480 
11481 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11482 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11483 	/*
11484 	 * Get point to point destination address. The addresses can't
11485 	 * change since we hold a reference to the ipif.
11486 	 */
11487 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11488 		return (EADDRNOTAVAIL);
11489 
11490 	if (ipif->ipif_isv6) {
11491 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11492 		*sin6 = sin6_null;
11493 		sin6->sin6_family = AF_INET6;
11494 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11495 	} else {
11496 		*sin = sin_null;
11497 		sin->sin_family = AF_INET;
11498 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11499 	}
11500 	return (0);
11501 }
11502 
11503 /*
11504  * part of ipmp, make this func return the active/inactive state and
11505  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11506  */
11507 /*
11508  * This function either sets or clears the IFF_INACTIVE flag.
11509  *
11510  * As long as there are some addresses or multicast memberships on the
11511  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11512  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11513  * will be used for outbound packets.
11514  *
11515  * Caller needs to verify the validity of setting IFF_INACTIVE.
11516  */
11517 static void
11518 phyint_inactive(phyint_t *phyi)
11519 {
11520 	ill_t *ill_v4;
11521 	ill_t *ill_v6;
11522 	ipif_t *ipif;
11523 	ilm_t *ilm;
11524 
11525 	ill_v4 = phyi->phyint_illv4;
11526 	ill_v6 = phyi->phyint_illv6;
11527 
11528 	/*
11529 	 * No need for a lock while traversing the list since iam
11530 	 * a writer
11531 	 */
11532 	if (ill_v4 != NULL) {
11533 		ASSERT(IAM_WRITER_ILL(ill_v4));
11534 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11535 		    ipif = ipif->ipif_next) {
11536 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11537 				mutex_enter(&phyi->phyint_lock);
11538 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11539 				mutex_exit(&phyi->phyint_lock);
11540 				return;
11541 			}
11542 		}
11543 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11544 		    ilm = ilm->ilm_next) {
11545 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11546 				mutex_enter(&phyi->phyint_lock);
11547 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11548 				mutex_exit(&phyi->phyint_lock);
11549 				return;
11550 			}
11551 		}
11552 	}
11553 	if (ill_v6 != NULL) {
11554 		ill_v6 = phyi->phyint_illv6;
11555 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11556 		    ipif = ipif->ipif_next) {
11557 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11558 				mutex_enter(&phyi->phyint_lock);
11559 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11560 				mutex_exit(&phyi->phyint_lock);
11561 				return;
11562 			}
11563 		}
11564 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11565 		    ilm = ilm->ilm_next) {
11566 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11567 				mutex_enter(&phyi->phyint_lock);
11568 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11569 				mutex_exit(&phyi->phyint_lock);
11570 				return;
11571 			}
11572 		}
11573 	}
11574 	mutex_enter(&phyi->phyint_lock);
11575 	phyi->phyint_flags |= PHYI_INACTIVE;
11576 	mutex_exit(&phyi->phyint_lock);
11577 }
11578 
11579 /*
11580  * This function is called only when the phyint flags change. Currently
11581  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11582  * that we can select a good ill.
11583  */
11584 static void
11585 ip_redo_nomination(phyint_t *phyi)
11586 {
11587 	ill_t *ill_v4;
11588 
11589 	ill_v4 = phyi->phyint_illv4;
11590 
11591 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11592 		ASSERT(IAM_WRITER_ILL(ill_v4));
11593 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11594 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11595 	}
11596 }
11597 
11598 /*
11599  * Heuristic to check if ill is INACTIVE.
11600  * Checks if ill has an ipif with an usable ip address.
11601  *
11602  * Return values:
11603  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11604  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11605  */
11606 static boolean_t
11607 ill_is_inactive(ill_t *ill)
11608 {
11609 	ipif_t *ipif;
11610 
11611 	/* Check whether it is in an IPMP group */
11612 	if (ill->ill_phyint->phyint_groupname == NULL)
11613 		return (B_FALSE);
11614 
11615 	if (ill->ill_ipif_up_count == 0)
11616 		return (B_TRUE);
11617 
11618 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11619 		uint64_t flags = ipif->ipif_flags;
11620 
11621 		/*
11622 		 * This ipif is usable if it is IPIF_UP and not a
11623 		 * dedicated test address.  A dedicated test address
11624 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11625 		 * (note in particular that V6 test addresses are
11626 		 * link-local data addresses and thus are marked
11627 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11628 		 */
11629 		if ((flags & IPIF_UP) &&
11630 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11631 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11632 			return (B_FALSE);
11633 	}
11634 	return (B_TRUE);
11635 }
11636 
11637 /*
11638  * Set interface flags.
11639  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11640  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11641  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11642  *
11643  * NOTE : We really don't enforce that ipif_id zero should be used
11644  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11645  *	  is because applications generally does SICGLIFFLAGS and
11646  *	  ORs in the new flags (that affects the logical) and does a
11647  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11648  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11649  *	  flags that will be turned on is correct with respect to
11650  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11651  */
11652 /* ARGSUSED */
11653 int
11654 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11655     ip_ioctl_cmd_t *ipip, void *if_req)
11656 {
11657 	uint64_t turn_on;
11658 	uint64_t turn_off;
11659 	int	err;
11660 	phyint_t *phyi;
11661 	ill_t *ill;
11662 	uint64_t intf_flags;
11663 	boolean_t phyint_flags_modified = B_FALSE;
11664 	uint64_t flags;
11665 	struct ifreq *ifr;
11666 	struct lifreq *lifr;
11667 	boolean_t set_linklocal = B_FALSE;
11668 	boolean_t zero_source = B_FALSE;
11669 	ip_stack_t *ipst;
11670 
11671 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11672 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11673 
11674 	ASSERT(IAM_WRITER_IPIF(ipif));
11675 
11676 	ill = ipif->ipif_ill;
11677 	phyi = ill->ill_phyint;
11678 	ipst = ill->ill_ipst;
11679 
11680 	if (ipip->ipi_cmd_type == IF_CMD) {
11681 		ifr = (struct ifreq *)if_req;
11682 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11683 	} else {
11684 		lifr = (struct lifreq *)if_req;
11685 		flags = lifr->lifr_flags;
11686 	}
11687 
11688 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11689 
11690 	/*
11691 	 * Have the flags been set correctly until now?
11692 	 */
11693 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11694 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11695 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11696 	/*
11697 	 * Compare the new flags to the old, and partition
11698 	 * into those coming on and those going off.
11699 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11700 	 */
11701 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11702 		flags |= intf_flags & ~0xFFFF;
11703 
11704 	/*
11705 	 * First check which bits will change and then which will
11706 	 * go on and off
11707 	 */
11708 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11709 	if (!turn_on)
11710 		return (0);	/* No change */
11711 
11712 	turn_off = intf_flags & turn_on;
11713 	turn_on ^= turn_off;
11714 	err = 0;
11715 
11716 	/*
11717 	 * Don't allow any bits belonging to the logical interface
11718 	 * to be set or cleared on the replacement ipif that was
11719 	 * created temporarily during a MOVE.
11720 	 */
11721 	if (ipif->ipif_replace_zero &&
11722 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11723 		return (EINVAL);
11724 	}
11725 
11726 	/*
11727 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11728 	 * IPv6 interfaces.
11729 	 */
11730 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11731 		return (EINVAL);
11732 
11733 	/*
11734 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11735 	 */
11736 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11737 		return (EINVAL);
11738 
11739 	/*
11740 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11741 	 * interfaces.  It makes no sense in that context.
11742 	 */
11743 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11744 		return (EINVAL);
11745 
11746 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11747 		zero_source = B_TRUE;
11748 
11749 	/*
11750 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11751 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11752 	 * If the link local address isn't set, and can be set, it will get
11753 	 * set later on in this function.
11754 	 */
11755 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11756 	    (flags & IFF_UP) && !zero_source &&
11757 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11758 		if (ipif_cant_setlinklocal(ipif))
11759 			return (EINVAL);
11760 		set_linklocal = B_TRUE;
11761 	}
11762 
11763 	/*
11764 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11765 	 * same time. No need to grab ill_g_usesrc_lock here, see
11766 	 * synchronization notes in ip.c
11767 	 */
11768 	if (turn_on & PHYI_STANDBY &&
11769 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11770 		return (EINVAL);
11771 	}
11772 
11773 	/*
11774 	 * If we modify physical interface flags, we'll potentially need to
11775 	 * send up two routing socket messages for the changes (one for the
11776 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11777 	 */
11778 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11779 		phyint_flags_modified = B_TRUE;
11780 
11781 	/*
11782 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11783 	 * we need to flush the IRE_CACHES belonging to this ill.
11784 	 * We handle this case here without doing the DOWN/UP dance
11785 	 * like it is done for other flags. If some other flags are
11786 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11787 	 * below will handle it by bringing it down and then
11788 	 * bringing it UP.
11789 	 */
11790 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11791 		ill_t *ill_v4, *ill_v6;
11792 
11793 		ill_v4 = phyi->phyint_illv4;
11794 		ill_v6 = phyi->phyint_illv6;
11795 
11796 		/*
11797 		 * First set the INACTIVE flag if needed. Then delete the ires.
11798 		 * ire_add will atomically prevent creating new IRE_CACHEs
11799 		 * unless hidden flag is set.
11800 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11801 		 */
11802 		if ((turn_on & PHYI_FAILED) &&
11803 		    ((intf_flags & PHYI_STANDBY) ||
11804 		    !ipst->ips_ipmp_enable_failback)) {
11805 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11806 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11807 		}
11808 		if ((turn_off & PHYI_FAILED) &&
11809 		    ((intf_flags & PHYI_STANDBY) ||
11810 		    (!ipst->ips_ipmp_enable_failback &&
11811 		    ill_is_inactive(ill)))) {
11812 			phyint_inactive(phyi);
11813 		}
11814 
11815 		if (turn_on & PHYI_STANDBY) {
11816 			/*
11817 			 * We implicitly set INACTIVE only when STANDBY is set.
11818 			 * INACTIVE is also set on non-STANDBY phyint when user
11819 			 * disables FAILBACK using configuration file.
11820 			 * Do not allow STANDBY to be set on such INACTIVE
11821 			 * phyint
11822 			 */
11823 			if (phyi->phyint_flags & PHYI_INACTIVE)
11824 				return (EINVAL);
11825 			if (!(phyi->phyint_flags & PHYI_FAILED))
11826 				phyint_inactive(phyi);
11827 		}
11828 		if (turn_off & PHYI_STANDBY) {
11829 			if (ipst->ips_ipmp_enable_failback) {
11830 				/*
11831 				 * Reset PHYI_INACTIVE.
11832 				 */
11833 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11834 			} else if (ill_is_inactive(ill) &&
11835 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11836 				/*
11837 				 * Need to set INACTIVE, when user sets
11838 				 * STANDBY on a non-STANDBY phyint and
11839 				 * later resets STANDBY
11840 				 */
11841 				phyint_inactive(phyi);
11842 			}
11843 		}
11844 		/*
11845 		 * We should always send up a message so that the
11846 		 * daemons come to know of it. Note that the zeroth
11847 		 * interface can be down and the check below for IPIF_UP
11848 		 * will not make sense as we are actually setting
11849 		 * a phyint flag here. We assume that the ipif used
11850 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11851 		 * send up any message for non-zero ipifs).
11852 		 */
11853 		phyint_flags_modified = B_TRUE;
11854 
11855 		if (ill_v4 != NULL) {
11856 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11857 			    IRE_CACHE, ill_stq_cache_delete,
11858 			    (char *)ill_v4, ill_v4);
11859 			illgrp_reset_schednext(ill_v4);
11860 		}
11861 		if (ill_v6 != NULL) {
11862 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11863 			    IRE_CACHE, ill_stq_cache_delete,
11864 			    (char *)ill_v6, ill_v6);
11865 			illgrp_reset_schednext(ill_v6);
11866 		}
11867 	}
11868 
11869 	/*
11870 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11871 	 * status of the interface and, if the interface is part of an IPMP
11872 	 * group, all other interfaces that are part of the same IPMP
11873 	 * group.
11874 	 */
11875 	if ((turn_on | turn_off) & ILLF_ROUTER)
11876 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11877 
11878 	/*
11879 	 * If the interface is not UP and we are not going to
11880 	 * bring it UP, record the flags and return. When the
11881 	 * interface comes UP later, the right actions will be
11882 	 * taken.
11883 	 */
11884 	if (!(ipif->ipif_flags & IPIF_UP) &&
11885 	    !(turn_on & IPIF_UP)) {
11886 		/* Record new flags in their respective places. */
11887 		mutex_enter(&ill->ill_lock);
11888 		mutex_enter(&ill->ill_phyint->phyint_lock);
11889 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11890 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11891 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11892 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11893 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11894 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11895 		mutex_exit(&ill->ill_lock);
11896 		mutex_exit(&ill->ill_phyint->phyint_lock);
11897 
11898 		/*
11899 		 * We do the broadcast and nomination here rather
11900 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11901 		 * the case of FAILBACK from INACTIVE standby to the
11902 		 * interface that has been repaired, PHYI_FAILED has not
11903 		 * been cleared yet. If there are only two interfaces in
11904 		 * that group, all we have is a FAILED and INACTIVE
11905 		 * interface. If we do the nomination soon after a failback,
11906 		 * the broadcast nomination code would select the
11907 		 * INACTIVE interface for receiving broadcasts as FAILED is
11908 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11909 		 * receive broadcast packets, we need to redo nomination
11910 		 * when the FAILED is cleared here. Thus, in general we
11911 		 * always do the nomination here for FAILED, STANDBY
11912 		 * and OFFLINE.
11913 		 */
11914 		if (((turn_on | turn_off) &
11915 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11916 			ip_redo_nomination(phyi);
11917 		}
11918 		if (phyint_flags_modified) {
11919 			if (phyi->phyint_illv4 != NULL) {
11920 				ip_rts_ifmsg(phyi->phyint_illv4->
11921 				    ill_ipif);
11922 			}
11923 			if (phyi->phyint_illv6 != NULL) {
11924 				ip_rts_ifmsg(phyi->phyint_illv6->
11925 				    ill_ipif);
11926 			}
11927 		}
11928 		return (0);
11929 	} else if (set_linklocal || zero_source) {
11930 		mutex_enter(&ill->ill_lock);
11931 		if (set_linklocal)
11932 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11933 		if (zero_source)
11934 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11935 		mutex_exit(&ill->ill_lock);
11936 	}
11937 
11938 	/*
11939 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11940 	 * or point-to-point interfaces with an unspecified destination. We do
11941 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11942 	 * have a subnet assigned, which is how in.ndpd currently manages its
11943 	 * onlink prefix list when no addresses are configured with those
11944 	 * prefixes.
11945 	 */
11946 	if (ipif->ipif_isv6 &&
11947 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11948 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11949 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11950 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11951 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11952 		return (EINVAL);
11953 	}
11954 
11955 	/*
11956 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11957 	 * from being brought up.
11958 	 */
11959 	if (!ipif->ipif_isv6 &&
11960 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11961 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11962 		return (EINVAL);
11963 	}
11964 
11965 	/*
11966 	 * The only flag changes that we currently take specific action on
11967 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11968 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11969 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11970 	 * the flags and bringing it back up again.
11971 	 */
11972 	if ((turn_on|turn_off) &
11973 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11974 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11975 		/*
11976 		 * Taking this ipif down, make sure we have
11977 		 * valid net and subnet bcast ire's for other
11978 		 * logical interfaces, if we need them.
11979 		 */
11980 		if (!ipif->ipif_isv6)
11981 			ipif_check_bcast_ires(ipif);
11982 
11983 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11984 		    !(turn_off & IPIF_UP)) {
11985 			if (ipif->ipif_flags & IPIF_UP)
11986 				ill->ill_logical_down = 1;
11987 			turn_on &= ~IPIF_UP;
11988 		}
11989 		err = ipif_down(ipif, q, mp);
11990 		ip1dbg(("ipif_down returns %d err ", err));
11991 		if (err == EINPROGRESS)
11992 			return (err);
11993 		ipif_down_tail(ipif);
11994 	}
11995 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11996 }
11997 
11998 static int
11999 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
12000 {
12001 	ill_t	*ill;
12002 	phyint_t *phyi;
12003 	uint64_t turn_on;
12004 	uint64_t turn_off;
12005 	uint64_t intf_flags;
12006 	boolean_t phyint_flags_modified = B_FALSE;
12007 	int	err = 0;
12008 	boolean_t set_linklocal = B_FALSE;
12009 	boolean_t zero_source = B_FALSE;
12010 
12011 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12012 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12013 
12014 	ASSERT(IAM_WRITER_IPIF(ipif));
12015 
12016 	ill = ipif->ipif_ill;
12017 	phyi = ill->ill_phyint;
12018 
12019 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12020 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12021 
12022 	turn_off = intf_flags & turn_on;
12023 	turn_on ^= turn_off;
12024 
12025 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12026 		phyint_flags_modified = B_TRUE;
12027 
12028 	/*
12029 	 * Now we change the flags. Track current value of
12030 	 * other flags in their respective places.
12031 	 */
12032 	mutex_enter(&ill->ill_lock);
12033 	mutex_enter(&phyi->phyint_lock);
12034 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12035 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12036 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12037 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12038 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12039 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12040 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12041 		set_linklocal = B_TRUE;
12042 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12043 	}
12044 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12045 		zero_source = B_TRUE;
12046 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12047 	}
12048 	mutex_exit(&ill->ill_lock);
12049 	mutex_exit(&phyi->phyint_lock);
12050 
12051 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12052 		ip_redo_nomination(phyi);
12053 
12054 	if (set_linklocal)
12055 		(void) ipif_setlinklocal(ipif);
12056 
12057 	if (zero_source)
12058 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12059 	else
12060 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12061 
12062 	if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
12063 		/*
12064 		 * XXX ipif_up really does not know whether a phyint flags
12065 		 * was modified or not. So, it sends up information on
12066 		 * only one routing sockets message. As we don't bring up
12067 		 * the interface and also set STANDBY/FAILED simultaneously
12068 		 * it should be okay.
12069 		 */
12070 		err = ipif_up(ipif, q, mp);
12071 	} else {
12072 		/*
12073 		 * Make sure routing socket sees all changes to the flags.
12074 		 * ipif_up_done* handles this when we use ipif_up.
12075 		 */
12076 		if (phyint_flags_modified) {
12077 			if (phyi->phyint_illv4 != NULL) {
12078 				ip_rts_ifmsg(phyi->phyint_illv4->
12079 				    ill_ipif);
12080 			}
12081 			if (phyi->phyint_illv6 != NULL) {
12082 				ip_rts_ifmsg(phyi->phyint_illv6->
12083 				    ill_ipif);
12084 			}
12085 		} else {
12086 			ip_rts_ifmsg(ipif);
12087 		}
12088 		/*
12089 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12090 		 * this in need_up case.
12091 		 */
12092 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12093 	}
12094 	return (err);
12095 }
12096 
12097 /*
12098  * Restart the flags operation now that the refcounts have dropped to zero.
12099  */
12100 /* ARGSUSED */
12101 int
12102 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12103     ip_ioctl_cmd_t *ipip, void *if_req)
12104 {
12105 	uint64_t flags;
12106 	struct ifreq *ifr = if_req;
12107 	struct lifreq *lifr = if_req;
12108 
12109 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12110 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12111 
12112 	ipif_down_tail(ipif);
12113 	if (ipip->ipi_cmd_type == IF_CMD) {
12114 		/* cast to uint16_t prevents unwanted sign extension */
12115 		flags = (uint16_t)ifr->ifr_flags;
12116 	} else {
12117 		flags = lifr->lifr_flags;
12118 	}
12119 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
12120 }
12121 
12122 /*
12123  * Can operate on either a module or a driver queue.
12124  */
12125 /* ARGSUSED */
12126 int
12127 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12128     ip_ioctl_cmd_t *ipip, void *if_req)
12129 {
12130 	/*
12131 	 * Has the flags been set correctly till now ?
12132 	 */
12133 	ill_t *ill = ipif->ipif_ill;
12134 	phyint_t *phyi = ill->ill_phyint;
12135 
12136 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12137 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12138 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12139 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12140 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12141 
12142 	/*
12143 	 * Need a lock since some flags can be set even when there are
12144 	 * references to the ipif.
12145 	 */
12146 	mutex_enter(&ill->ill_lock);
12147 	if (ipip->ipi_cmd_type == IF_CMD) {
12148 		struct ifreq *ifr = (struct ifreq *)if_req;
12149 
12150 		/* Get interface flags (low 16 only). */
12151 		ifr->ifr_flags = ((ipif->ipif_flags |
12152 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12153 	} else {
12154 		struct lifreq *lifr = (struct lifreq *)if_req;
12155 
12156 		/* Get interface flags. */
12157 		lifr->lifr_flags = ipif->ipif_flags |
12158 		    ill->ill_flags | phyi->phyint_flags;
12159 	}
12160 	mutex_exit(&ill->ill_lock);
12161 	return (0);
12162 }
12163 
12164 /* ARGSUSED */
12165 int
12166 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12167     ip_ioctl_cmd_t *ipip, void *if_req)
12168 {
12169 	int mtu;
12170 	int ip_min_mtu;
12171 	struct ifreq	*ifr;
12172 	struct lifreq *lifr;
12173 	ire_t	*ire;
12174 	ip_stack_t *ipst;
12175 
12176 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12177 	    ipif->ipif_id, (void *)ipif));
12178 	if (ipip->ipi_cmd_type == IF_CMD) {
12179 		ifr = (struct ifreq *)if_req;
12180 		mtu = ifr->ifr_metric;
12181 	} else {
12182 		lifr = (struct lifreq *)if_req;
12183 		mtu = lifr->lifr_mtu;
12184 	}
12185 
12186 	if (ipif->ipif_isv6)
12187 		ip_min_mtu = IPV6_MIN_MTU;
12188 	else
12189 		ip_min_mtu = IP_MIN_MTU;
12190 
12191 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12192 		return (EINVAL);
12193 
12194 	/*
12195 	 * Change the MTU size in all relevant ire's.
12196 	 * Mtu change Vs. new ire creation - protocol below.
12197 	 * First change ipif_mtu and the ire_max_frag of the
12198 	 * interface ire. Then do an ire walk and change the
12199 	 * ire_max_frag of all affected ires. During ire_add
12200 	 * under the bucket lock, set the ire_max_frag of the
12201 	 * new ire being created from the ipif/ire from which
12202 	 * it is being derived. If an mtu change happens after
12203 	 * the ire is added, the new ire will be cleaned up.
12204 	 * Conversely if the mtu change happens before the ire
12205 	 * is added, ire_add will see the new value of the mtu.
12206 	 */
12207 	ipif->ipif_mtu = mtu;
12208 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12209 
12210 	if (ipif->ipif_isv6)
12211 		ire = ipif_to_ire_v6(ipif);
12212 	else
12213 		ire = ipif_to_ire(ipif);
12214 	if (ire != NULL) {
12215 		ire->ire_max_frag = ipif->ipif_mtu;
12216 		ire_refrele(ire);
12217 	}
12218 	ipst = ipif->ipif_ill->ill_ipst;
12219 	if (ipif->ipif_flags & IPIF_UP) {
12220 		if (ipif->ipif_isv6)
12221 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12222 			    ipst);
12223 		else
12224 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12225 			    ipst);
12226 	}
12227 	/* Update the MTU in SCTP's list */
12228 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12229 	return (0);
12230 }
12231 
12232 /* Get interface MTU. */
12233 /* ARGSUSED */
12234 int
12235 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12236 	ip_ioctl_cmd_t *ipip, void *if_req)
12237 {
12238 	struct ifreq	*ifr;
12239 	struct lifreq	*lifr;
12240 
12241 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12242 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12243 	if (ipip->ipi_cmd_type == IF_CMD) {
12244 		ifr = (struct ifreq *)if_req;
12245 		ifr->ifr_metric = ipif->ipif_mtu;
12246 	} else {
12247 		lifr = (struct lifreq *)if_req;
12248 		lifr->lifr_mtu = ipif->ipif_mtu;
12249 	}
12250 	return (0);
12251 }
12252 
12253 /* Set interface broadcast address. */
12254 /* ARGSUSED2 */
12255 int
12256 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12257 	ip_ioctl_cmd_t *ipip, void *if_req)
12258 {
12259 	ipaddr_t addr;
12260 	ire_t	*ire;
12261 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12262 
12263 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12264 	    ipif->ipif_id));
12265 
12266 	ASSERT(IAM_WRITER_IPIF(ipif));
12267 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12268 		return (EADDRNOTAVAIL);
12269 
12270 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12271 
12272 	if (sin->sin_family != AF_INET)
12273 		return (EAFNOSUPPORT);
12274 
12275 	addr = sin->sin_addr.s_addr;
12276 	if (ipif->ipif_flags & IPIF_UP) {
12277 		/*
12278 		 * If we are already up, make sure the new
12279 		 * broadcast address makes sense.  If it does,
12280 		 * there should be an IRE for it already.
12281 		 * Don't match on ipif, only on the ill
12282 		 * since we are sharing these now. Don't use
12283 		 * MATCH_IRE_ILL_GROUP as we are looking for
12284 		 * the broadcast ire on this ill and each ill
12285 		 * in the group has its own broadcast ire.
12286 		 */
12287 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12288 		    ipif, ALL_ZONES, NULL,
12289 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12290 		if (ire == NULL) {
12291 			return (EINVAL);
12292 		} else {
12293 			ire_refrele(ire);
12294 		}
12295 	}
12296 	/*
12297 	 * Changing the broadcast addr for this ipif.
12298 	 * Make sure we have valid net and subnet bcast
12299 	 * ire's for other logical interfaces, if needed.
12300 	 */
12301 	if (addr != ipif->ipif_brd_addr)
12302 		ipif_check_bcast_ires(ipif);
12303 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12304 	return (0);
12305 }
12306 
12307 /* Get interface broadcast address. */
12308 /* ARGSUSED */
12309 int
12310 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12311     ip_ioctl_cmd_t *ipip, void *if_req)
12312 {
12313 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12314 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12315 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12316 		return (EADDRNOTAVAIL);
12317 
12318 	/* IPIF_BROADCAST not possible with IPv6 */
12319 	ASSERT(!ipif->ipif_isv6);
12320 	*sin = sin_null;
12321 	sin->sin_family = AF_INET;
12322 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12323 	return (0);
12324 }
12325 
12326 /*
12327  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12328  */
12329 /* ARGSUSED */
12330 int
12331 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12332     ip_ioctl_cmd_t *ipip, void *if_req)
12333 {
12334 	int err = 0;
12335 	in6_addr_t v6mask;
12336 
12337 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12338 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12339 
12340 	ASSERT(IAM_WRITER_IPIF(ipif));
12341 
12342 	if (ipif->ipif_isv6) {
12343 		sin6_t *sin6;
12344 
12345 		if (sin->sin_family != AF_INET6)
12346 			return (EAFNOSUPPORT);
12347 
12348 		sin6 = (sin6_t *)sin;
12349 		v6mask = sin6->sin6_addr;
12350 	} else {
12351 		ipaddr_t mask;
12352 
12353 		if (sin->sin_family != AF_INET)
12354 			return (EAFNOSUPPORT);
12355 
12356 		mask = sin->sin_addr.s_addr;
12357 		V4MASK_TO_V6(mask, v6mask);
12358 	}
12359 
12360 	/*
12361 	 * No big deal if the interface isn't already up, or the mask
12362 	 * isn't really changing, or this is pt-pt.
12363 	 */
12364 	if (!(ipif->ipif_flags & IPIF_UP) ||
12365 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12366 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12367 		ipif->ipif_v6net_mask = v6mask;
12368 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12369 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12370 			    ipif->ipif_v6net_mask,
12371 			    ipif->ipif_v6subnet);
12372 		}
12373 		return (0);
12374 	}
12375 	/*
12376 	 * Make sure we have valid net and subnet broadcast ire's
12377 	 * for the old netmask, if needed by other logical interfaces.
12378 	 */
12379 	if (!ipif->ipif_isv6)
12380 		ipif_check_bcast_ires(ipif);
12381 
12382 	err = ipif_logical_down(ipif, q, mp);
12383 	if (err == EINPROGRESS)
12384 		return (err);
12385 	ipif_down_tail(ipif);
12386 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12387 	return (err);
12388 }
12389 
12390 static int
12391 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12392 {
12393 	in6_addr_t v6mask;
12394 	int err = 0;
12395 
12396 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12397 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12398 
12399 	if (ipif->ipif_isv6) {
12400 		sin6_t *sin6;
12401 
12402 		sin6 = (sin6_t *)sin;
12403 		v6mask = sin6->sin6_addr;
12404 	} else {
12405 		ipaddr_t mask;
12406 
12407 		mask = sin->sin_addr.s_addr;
12408 		V4MASK_TO_V6(mask, v6mask);
12409 	}
12410 
12411 	ipif->ipif_v6net_mask = v6mask;
12412 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12413 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12414 		    ipif->ipif_v6subnet);
12415 	}
12416 	err = ipif_up(ipif, q, mp);
12417 
12418 	if (err == 0 || err == EINPROGRESS) {
12419 		/*
12420 		 * The interface must be DL_BOUND if this packet has to
12421 		 * go out on the wire. Since we only go through a logical
12422 		 * down and are bound with the driver during an internal
12423 		 * down/up that is satisfied.
12424 		 */
12425 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12426 			/* Potentially broadcast an address mask reply. */
12427 			ipif_mask_reply(ipif);
12428 		}
12429 	}
12430 	return (err);
12431 }
12432 
12433 /* ARGSUSED */
12434 int
12435 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12436     ip_ioctl_cmd_t *ipip, void *if_req)
12437 {
12438 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12439 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12440 	ipif_down_tail(ipif);
12441 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12442 }
12443 
12444 /* Get interface net mask. */
12445 /* ARGSUSED */
12446 int
12447 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12448     ip_ioctl_cmd_t *ipip, void *if_req)
12449 {
12450 	struct lifreq *lifr = (struct lifreq *)if_req;
12451 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12452 
12453 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12454 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12455 
12456 	/*
12457 	 * net mask can't change since we have a reference to the ipif.
12458 	 */
12459 	if (ipif->ipif_isv6) {
12460 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12461 		*sin6 = sin6_null;
12462 		sin6->sin6_family = AF_INET6;
12463 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12464 		lifr->lifr_addrlen =
12465 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12466 	} else {
12467 		*sin = sin_null;
12468 		sin->sin_family = AF_INET;
12469 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12470 		if (ipip->ipi_cmd_type == LIF_CMD) {
12471 			lifr->lifr_addrlen =
12472 			    ip_mask_to_plen(ipif->ipif_net_mask);
12473 		}
12474 	}
12475 	return (0);
12476 }
12477 
12478 /* ARGSUSED */
12479 int
12480 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12481     ip_ioctl_cmd_t *ipip, void *if_req)
12482 {
12483 
12484 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12485 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12486 	/*
12487 	 * Set interface metric.  We don't use this for
12488 	 * anything but we keep track of it in case it is
12489 	 * important to routing applications or such.
12490 	 */
12491 	if (ipip->ipi_cmd_type == IF_CMD) {
12492 		struct ifreq    *ifr;
12493 
12494 		ifr = (struct ifreq *)if_req;
12495 		ipif->ipif_metric = ifr->ifr_metric;
12496 	} else {
12497 		struct lifreq   *lifr;
12498 
12499 		lifr = (struct lifreq *)if_req;
12500 		ipif->ipif_metric = lifr->lifr_metric;
12501 	}
12502 	return (0);
12503 }
12504 
12505 /* ARGSUSED */
12506 int
12507 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12508     ip_ioctl_cmd_t *ipip, void *if_req)
12509 {
12510 	/* Get interface metric. */
12511 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12512 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12513 	if (ipip->ipi_cmd_type == IF_CMD) {
12514 		struct ifreq    *ifr;
12515 
12516 		ifr = (struct ifreq *)if_req;
12517 		ifr->ifr_metric = ipif->ipif_metric;
12518 	} else {
12519 		struct lifreq   *lifr;
12520 
12521 		lifr = (struct lifreq *)if_req;
12522 		lifr->lifr_metric = ipif->ipif_metric;
12523 	}
12524 
12525 	return (0);
12526 }
12527 
12528 /* ARGSUSED */
12529 int
12530 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12531     ip_ioctl_cmd_t *ipip, void *if_req)
12532 {
12533 
12534 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12535 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12536 	/*
12537 	 * Set the muxid returned from I_PLINK.
12538 	 */
12539 	if (ipip->ipi_cmd_type == IF_CMD) {
12540 		struct ifreq *ifr = (struct ifreq *)if_req;
12541 
12542 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12543 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12544 	} else {
12545 		struct lifreq *lifr = (struct lifreq *)if_req;
12546 
12547 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12548 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12549 	}
12550 	return (0);
12551 }
12552 
12553 /* ARGSUSED */
12554 int
12555 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12556     ip_ioctl_cmd_t *ipip, void *if_req)
12557 {
12558 
12559 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12560 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12561 	/*
12562 	 * Get the muxid saved in ill for I_PUNLINK.
12563 	 */
12564 	if (ipip->ipi_cmd_type == IF_CMD) {
12565 		struct ifreq *ifr = (struct ifreq *)if_req;
12566 
12567 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12568 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12569 	} else {
12570 		struct lifreq *lifr = (struct lifreq *)if_req;
12571 
12572 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12573 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12574 	}
12575 	return (0);
12576 }
12577 
12578 /*
12579  * Set the subnet prefix. Does not modify the broadcast address.
12580  */
12581 /* ARGSUSED */
12582 int
12583 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12584     ip_ioctl_cmd_t *ipip, void *if_req)
12585 {
12586 	int err = 0;
12587 	in6_addr_t v6addr;
12588 	in6_addr_t v6mask;
12589 	boolean_t need_up = B_FALSE;
12590 	int addrlen;
12591 
12592 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12593 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12594 
12595 	ASSERT(IAM_WRITER_IPIF(ipif));
12596 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12597 
12598 	if (ipif->ipif_isv6) {
12599 		sin6_t *sin6;
12600 
12601 		if (sin->sin_family != AF_INET6)
12602 			return (EAFNOSUPPORT);
12603 
12604 		sin6 = (sin6_t *)sin;
12605 		v6addr = sin6->sin6_addr;
12606 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12607 			return (EADDRNOTAVAIL);
12608 	} else {
12609 		ipaddr_t addr;
12610 
12611 		if (sin->sin_family != AF_INET)
12612 			return (EAFNOSUPPORT);
12613 
12614 		addr = sin->sin_addr.s_addr;
12615 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12616 			return (EADDRNOTAVAIL);
12617 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12618 		/* Add 96 bits */
12619 		addrlen += IPV6_ABITS - IP_ABITS;
12620 	}
12621 
12622 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12623 		return (EINVAL);
12624 
12625 	/* Check if bits in the address is set past the mask */
12626 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12627 		return (EINVAL);
12628 
12629 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12630 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12631 		return (0);	/* No change */
12632 
12633 	if (ipif->ipif_flags & IPIF_UP) {
12634 		/*
12635 		 * If the interface is already marked up,
12636 		 * we call ipif_down which will take care
12637 		 * of ditching any IREs that have been set
12638 		 * up based on the old interface address.
12639 		 */
12640 		err = ipif_logical_down(ipif, q, mp);
12641 		if (err == EINPROGRESS)
12642 			return (err);
12643 		ipif_down_tail(ipif);
12644 		need_up = B_TRUE;
12645 	}
12646 
12647 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12648 	return (err);
12649 }
12650 
12651 static int
12652 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12653     queue_t *q, mblk_t *mp, boolean_t need_up)
12654 {
12655 	ill_t	*ill = ipif->ipif_ill;
12656 	int	err = 0;
12657 
12658 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12659 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12660 
12661 	/* Set the new address. */
12662 	mutex_enter(&ill->ill_lock);
12663 	ipif->ipif_v6net_mask = v6mask;
12664 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12665 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12666 		    ipif->ipif_v6subnet);
12667 	}
12668 	mutex_exit(&ill->ill_lock);
12669 
12670 	if (need_up) {
12671 		/*
12672 		 * Now bring the interface back up.  If this
12673 		 * is the only IPIF for the ILL, ipif_up
12674 		 * will have to re-bind to the device, so
12675 		 * we may get back EINPROGRESS, in which
12676 		 * case, this IOCTL will get completed in
12677 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12678 		 */
12679 		err = ipif_up(ipif, q, mp);
12680 		if (err == EINPROGRESS)
12681 			return (err);
12682 	}
12683 	return (err);
12684 }
12685 
12686 /* ARGSUSED */
12687 int
12688 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12689     ip_ioctl_cmd_t *ipip, void *if_req)
12690 {
12691 	int	addrlen;
12692 	in6_addr_t v6addr;
12693 	in6_addr_t v6mask;
12694 	struct lifreq *lifr = (struct lifreq *)if_req;
12695 
12696 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12697 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12698 	ipif_down_tail(ipif);
12699 
12700 	addrlen = lifr->lifr_addrlen;
12701 	if (ipif->ipif_isv6) {
12702 		sin6_t *sin6;
12703 
12704 		sin6 = (sin6_t *)sin;
12705 		v6addr = sin6->sin6_addr;
12706 	} else {
12707 		ipaddr_t addr;
12708 
12709 		addr = sin->sin_addr.s_addr;
12710 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12711 		addrlen += IPV6_ABITS - IP_ABITS;
12712 	}
12713 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12714 
12715 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12716 }
12717 
12718 /* ARGSUSED */
12719 int
12720 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12721     ip_ioctl_cmd_t *ipip, void *if_req)
12722 {
12723 	struct lifreq *lifr = (struct lifreq *)if_req;
12724 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12725 
12726 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12727 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12728 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12729 
12730 	if (ipif->ipif_isv6) {
12731 		*sin6 = sin6_null;
12732 		sin6->sin6_family = AF_INET6;
12733 		sin6->sin6_addr = ipif->ipif_v6subnet;
12734 		lifr->lifr_addrlen =
12735 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12736 	} else {
12737 		*sin = sin_null;
12738 		sin->sin_family = AF_INET;
12739 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12740 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12741 	}
12742 	return (0);
12743 }
12744 
12745 /*
12746  * Set the IPv6 address token.
12747  */
12748 /* ARGSUSED */
12749 int
12750 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12751     ip_ioctl_cmd_t *ipi, void *if_req)
12752 {
12753 	ill_t *ill = ipif->ipif_ill;
12754 	int err;
12755 	in6_addr_t v6addr;
12756 	in6_addr_t v6mask;
12757 	boolean_t need_up = B_FALSE;
12758 	int i;
12759 	sin6_t *sin6 = (sin6_t *)sin;
12760 	struct lifreq *lifr = (struct lifreq *)if_req;
12761 	int addrlen;
12762 
12763 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12764 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12765 	ASSERT(IAM_WRITER_IPIF(ipif));
12766 
12767 	addrlen = lifr->lifr_addrlen;
12768 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12769 	if (ipif->ipif_id != 0)
12770 		return (EINVAL);
12771 
12772 	if (!ipif->ipif_isv6)
12773 		return (EINVAL);
12774 
12775 	if (addrlen > IPV6_ABITS)
12776 		return (EINVAL);
12777 
12778 	v6addr = sin6->sin6_addr;
12779 
12780 	/*
12781 	 * The length of the token is the length from the end.  To get
12782 	 * the proper mask for this, compute the mask of the bits not
12783 	 * in the token; ie. the prefix, and then xor to get the mask.
12784 	 */
12785 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12786 		return (EINVAL);
12787 	for (i = 0; i < 4; i++) {
12788 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12789 	}
12790 
12791 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12792 	    ill->ill_token_length == addrlen)
12793 		return (0);	/* No change */
12794 
12795 	if (ipif->ipif_flags & IPIF_UP) {
12796 		err = ipif_logical_down(ipif, q, mp);
12797 		if (err == EINPROGRESS)
12798 			return (err);
12799 		ipif_down_tail(ipif);
12800 		need_up = B_TRUE;
12801 	}
12802 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12803 	return (err);
12804 }
12805 
12806 static int
12807 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12808     mblk_t *mp, boolean_t need_up)
12809 {
12810 	in6_addr_t v6addr;
12811 	in6_addr_t v6mask;
12812 	ill_t	*ill = ipif->ipif_ill;
12813 	int	i;
12814 	int	err = 0;
12815 
12816 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12817 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12818 	v6addr = sin6->sin6_addr;
12819 	/*
12820 	 * The length of the token is the length from the end.  To get
12821 	 * the proper mask for this, compute the mask of the bits not
12822 	 * in the token; ie. the prefix, and then xor to get the mask.
12823 	 */
12824 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12825 	for (i = 0; i < 4; i++)
12826 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12827 
12828 	mutex_enter(&ill->ill_lock);
12829 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12830 	ill->ill_token_length = addrlen;
12831 	mutex_exit(&ill->ill_lock);
12832 
12833 	if (need_up) {
12834 		/*
12835 		 * Now bring the interface back up.  If this
12836 		 * is the only IPIF for the ILL, ipif_up
12837 		 * will have to re-bind to the device, so
12838 		 * we may get back EINPROGRESS, in which
12839 		 * case, this IOCTL will get completed in
12840 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12841 		 */
12842 		err = ipif_up(ipif, q, mp);
12843 		if (err == EINPROGRESS)
12844 			return (err);
12845 	}
12846 	return (err);
12847 }
12848 
12849 /* ARGSUSED */
12850 int
12851 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12852     ip_ioctl_cmd_t *ipi, void *if_req)
12853 {
12854 	ill_t *ill;
12855 	sin6_t *sin6 = (sin6_t *)sin;
12856 	struct lifreq *lifr = (struct lifreq *)if_req;
12857 
12858 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12859 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12860 	if (ipif->ipif_id != 0)
12861 		return (EINVAL);
12862 
12863 	ill = ipif->ipif_ill;
12864 	if (!ill->ill_isv6)
12865 		return (ENXIO);
12866 
12867 	*sin6 = sin6_null;
12868 	sin6->sin6_family = AF_INET6;
12869 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12870 	sin6->sin6_addr = ill->ill_token;
12871 	lifr->lifr_addrlen = ill->ill_token_length;
12872 	return (0);
12873 }
12874 
12875 /*
12876  * Set (hardware) link specific information that might override
12877  * what was acquired through the DL_INFO_ACK.
12878  * The logic is as follows.
12879  *
12880  * become exclusive
12881  * set CHANGING flag
12882  * change mtu on affected IREs
12883  * clear CHANGING flag
12884  *
12885  * An ire add that occurs before the CHANGING flag is set will have its mtu
12886  * changed by the ip_sioctl_lnkinfo.
12887  *
12888  * During the time the CHANGING flag is set, no new ires will be added to the
12889  * bucket, and ire add will fail (due the CHANGING flag).
12890  *
12891  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12892  * before it is added to the bucket.
12893  *
12894  * Obviously only 1 thread can set the CHANGING flag and we need to become
12895  * exclusive to set the flag.
12896  */
12897 /* ARGSUSED */
12898 int
12899 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12900     ip_ioctl_cmd_t *ipi, void *if_req)
12901 {
12902 	ill_t		*ill = ipif->ipif_ill;
12903 	ipif_t		*nipif;
12904 	int		ip_min_mtu;
12905 	boolean_t	mtu_walk = B_FALSE;
12906 	struct lifreq	*lifr = (struct lifreq *)if_req;
12907 	lif_ifinfo_req_t *lir;
12908 	ire_t		*ire;
12909 
12910 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12911 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12912 	lir = &lifr->lifr_ifinfo;
12913 	ASSERT(IAM_WRITER_IPIF(ipif));
12914 
12915 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12916 	if (ipif->ipif_id != 0)
12917 		return (EINVAL);
12918 
12919 	/* Set interface MTU. */
12920 	if (ipif->ipif_isv6)
12921 		ip_min_mtu = IPV6_MIN_MTU;
12922 	else
12923 		ip_min_mtu = IP_MIN_MTU;
12924 
12925 	/*
12926 	 * Verify values before we set anything. Allow zero to
12927 	 * mean unspecified.
12928 	 */
12929 	if (lir->lir_maxmtu != 0 &&
12930 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12931 	    lir->lir_maxmtu < ip_min_mtu))
12932 		return (EINVAL);
12933 	if (lir->lir_reachtime != 0 &&
12934 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12935 		return (EINVAL);
12936 	if (lir->lir_reachretrans != 0 &&
12937 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12938 		return (EINVAL);
12939 
12940 	mutex_enter(&ill->ill_lock);
12941 	ill->ill_state_flags |= ILL_CHANGING;
12942 	for (nipif = ill->ill_ipif; nipif != NULL;
12943 	    nipif = nipif->ipif_next) {
12944 		nipif->ipif_state_flags |= IPIF_CHANGING;
12945 	}
12946 
12947 	mutex_exit(&ill->ill_lock);
12948 
12949 	if (lir->lir_maxmtu != 0) {
12950 		ill->ill_max_mtu = lir->lir_maxmtu;
12951 		ill->ill_mtu_userspecified = 1;
12952 		mtu_walk = B_TRUE;
12953 	}
12954 
12955 	if (lir->lir_reachtime != 0)
12956 		ill->ill_reachable_time = lir->lir_reachtime;
12957 
12958 	if (lir->lir_reachretrans != 0)
12959 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12960 
12961 	ill->ill_max_hops = lir->lir_maxhops;
12962 
12963 	ill->ill_max_buf = ND_MAX_Q;
12964 
12965 	if (mtu_walk) {
12966 		/*
12967 		 * Set the MTU on all ipifs associated with this ill except
12968 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12969 		 */
12970 		for (nipif = ill->ill_ipif; nipif != NULL;
12971 		    nipif = nipif->ipif_next) {
12972 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12973 				continue;
12974 
12975 			nipif->ipif_mtu = ill->ill_max_mtu;
12976 
12977 			if (!(nipif->ipif_flags & IPIF_UP))
12978 				continue;
12979 
12980 			if (nipif->ipif_isv6)
12981 				ire = ipif_to_ire_v6(nipif);
12982 			else
12983 				ire = ipif_to_ire(nipif);
12984 			if (ire != NULL) {
12985 				ire->ire_max_frag = ipif->ipif_mtu;
12986 				ire_refrele(ire);
12987 			}
12988 
12989 			ire_walk_ill(MATCH_IRE_ILL, 0, ipif_mtu_change,
12990 			    nipif, ill);
12991 		}
12992 	}
12993 
12994 	mutex_enter(&ill->ill_lock);
12995 	for (nipif = ill->ill_ipif; nipif != NULL;
12996 	    nipif = nipif->ipif_next) {
12997 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12998 	}
12999 	ILL_UNMARK_CHANGING(ill);
13000 	mutex_exit(&ill->ill_lock);
13001 
13002 	return (0);
13003 }
13004 
13005 /* ARGSUSED */
13006 int
13007 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13008     ip_ioctl_cmd_t *ipi, void *if_req)
13009 {
13010 	struct lif_ifinfo_req *lir;
13011 	ill_t *ill = ipif->ipif_ill;
13012 
13013 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13014 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13015 	if (ipif->ipif_id != 0)
13016 		return (EINVAL);
13017 
13018 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13019 	lir->lir_maxhops = ill->ill_max_hops;
13020 	lir->lir_reachtime = ill->ill_reachable_time;
13021 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13022 	lir->lir_maxmtu = ill->ill_max_mtu;
13023 
13024 	return (0);
13025 }
13026 
13027 /*
13028  * Return best guess as to the subnet mask for the specified address.
13029  * Based on the subnet masks for all the configured interfaces.
13030  *
13031  * We end up returning a zero mask in the case of default, multicast or
13032  * experimental.
13033  */
13034 static ipaddr_t
13035 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13036 {
13037 	ipaddr_t net_mask;
13038 	ill_t	*ill;
13039 	ipif_t	*ipif;
13040 	ill_walk_context_t ctx;
13041 	ipif_t	*fallback_ipif = NULL;
13042 
13043 	net_mask = ip_net_mask(addr);
13044 	if (net_mask == 0) {
13045 		*ipifp = NULL;
13046 		return (0);
13047 	}
13048 
13049 	/* Let's check to see if this is maybe a local subnet route. */
13050 	/* this function only applies to IPv4 interfaces */
13051 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13052 	ill = ILL_START_WALK_V4(&ctx, ipst);
13053 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13054 		mutex_enter(&ill->ill_lock);
13055 		for (ipif = ill->ill_ipif; ipif != NULL;
13056 		    ipif = ipif->ipif_next) {
13057 			if (!IPIF_CAN_LOOKUP(ipif))
13058 				continue;
13059 			if (!(ipif->ipif_flags & IPIF_UP))
13060 				continue;
13061 			if ((ipif->ipif_subnet & net_mask) ==
13062 			    (addr & net_mask)) {
13063 				/*
13064 				 * Don't trust pt-pt interfaces if there are
13065 				 * other interfaces.
13066 				 */
13067 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13068 					if (fallback_ipif == NULL) {
13069 						ipif_refhold_locked(ipif);
13070 						fallback_ipif = ipif;
13071 					}
13072 					continue;
13073 				}
13074 
13075 				/*
13076 				 * Fine. Just assume the same net mask as the
13077 				 * directly attached subnet interface is using.
13078 				 */
13079 				ipif_refhold_locked(ipif);
13080 				mutex_exit(&ill->ill_lock);
13081 				rw_exit(&ipst->ips_ill_g_lock);
13082 				if (fallback_ipif != NULL)
13083 					ipif_refrele(fallback_ipif);
13084 				*ipifp = ipif;
13085 				return (ipif->ipif_net_mask);
13086 			}
13087 		}
13088 		mutex_exit(&ill->ill_lock);
13089 	}
13090 	rw_exit(&ipst->ips_ill_g_lock);
13091 
13092 	*ipifp = fallback_ipif;
13093 	return ((fallback_ipif != NULL) ?
13094 	    fallback_ipif->ipif_net_mask : net_mask);
13095 }
13096 
13097 /*
13098  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13099  */
13100 static void
13101 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13102 {
13103 	IOCP	iocp;
13104 	ipft_t	*ipft;
13105 	ipllc_t	*ipllc;
13106 	mblk_t	*mp1;
13107 	cred_t	*cr;
13108 	int	error = 0;
13109 	conn_t	*connp;
13110 
13111 	ip1dbg(("ip_wput_ioctl"));
13112 	iocp = (IOCP)mp->b_rptr;
13113 	mp1 = mp->b_cont;
13114 	if (mp1 == NULL) {
13115 		iocp->ioc_error = EINVAL;
13116 		mp->b_datap->db_type = M_IOCNAK;
13117 		iocp->ioc_count = 0;
13118 		qreply(q, mp);
13119 		return;
13120 	}
13121 
13122 	/*
13123 	 * These IOCTLs provide various control capabilities to
13124 	 * upstream agents such as ULPs and processes.	There
13125 	 * are currently two such IOCTLs implemented.  They
13126 	 * are used by TCP to provide update information for
13127 	 * existing IREs and to forcibly delete an IRE for a
13128 	 * host that is not responding, thereby forcing an
13129 	 * attempt at a new route.
13130 	 */
13131 	iocp->ioc_error = EINVAL;
13132 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13133 		goto done;
13134 
13135 	ipllc = (ipllc_t *)mp1->b_rptr;
13136 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13137 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13138 			break;
13139 	}
13140 	/*
13141 	 * prefer credential from mblk over ioctl;
13142 	 * see ip_sioctl_copyin_setup
13143 	 */
13144 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13145 
13146 	/*
13147 	 * Refhold the conn in case the request gets queued up in some lookup
13148 	 */
13149 	ASSERT(CONN_Q(q));
13150 	connp = Q_TO_CONN(q);
13151 	CONN_INC_REF(connp);
13152 	if (ipft->ipft_pfi &&
13153 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13154 	    pullupmsg(mp1, ipft->ipft_min_size))) {
13155 		error = (*ipft->ipft_pfi)(q,
13156 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13157 	}
13158 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13159 		/*
13160 		 * CONN_OPER_PENDING_DONE happens in the function called
13161 		 * through ipft_pfi above.
13162 		 */
13163 		return;
13164 	}
13165 
13166 	CONN_OPER_PENDING_DONE(connp);
13167 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13168 		freemsg(mp);
13169 		return;
13170 	}
13171 	iocp->ioc_error = error;
13172 
13173 done:
13174 	mp->b_datap->db_type = M_IOCACK;
13175 	if (iocp->ioc_error)
13176 		iocp->ioc_count = 0;
13177 	qreply(q, mp);
13178 }
13179 
13180 /*
13181  * Lookup an ipif using the sequence id (ipif_seqid)
13182  */
13183 ipif_t *
13184 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13185 {
13186 	ipif_t *ipif;
13187 
13188 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13189 
13190 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13191 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13192 			return (ipif);
13193 	}
13194 	return (NULL);
13195 }
13196 
13197 /*
13198  * Assign a unique id for the ipif. This is used later when we send
13199  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13200  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13201  * IRE is added, we verify that ipif has not disappeared.
13202  */
13203 
13204 static void
13205 ipif_assign_seqid(ipif_t *ipif)
13206 {
13207 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13208 
13209 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13210 }
13211 
13212 /*
13213  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13214  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13215  * be inserted into the first space available in the list. The value of
13216  * ipif_id will then be set to the appropriate value for its position.
13217  */
13218 static int
13219 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13220 {
13221 	ill_t *ill;
13222 	ipif_t *tipif;
13223 	ipif_t **tipifp;
13224 	int id;
13225 	ip_stack_t	*ipst;
13226 
13227 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13228 	    IAM_WRITER_IPIF(ipif));
13229 
13230 	ill = ipif->ipif_ill;
13231 	ASSERT(ill != NULL);
13232 	ipst = ill->ill_ipst;
13233 
13234 	/*
13235 	 * In the case of lo0:0 we already hold the ill_g_lock.
13236 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13237 	 * ipif_insert. Another such caller is ipif_move.
13238 	 */
13239 	if (acquire_g_lock)
13240 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13241 	if (acquire_ill_lock)
13242 		mutex_enter(&ill->ill_lock);
13243 	id = ipif->ipif_id;
13244 	tipifp = &(ill->ill_ipif);
13245 	if (id == -1) {	/* need to find a real id */
13246 		id = 0;
13247 		while ((tipif = *tipifp) != NULL) {
13248 			ASSERT(tipif->ipif_id >= id);
13249 			if (tipif->ipif_id != id)
13250 				break; /* non-consecutive id */
13251 			id++;
13252 			tipifp = &(tipif->ipif_next);
13253 		}
13254 		/* limit number of logical interfaces */
13255 		if (id >= ipst->ips_ip_addrs_per_if) {
13256 			if (acquire_ill_lock)
13257 				mutex_exit(&ill->ill_lock);
13258 			if (acquire_g_lock)
13259 				rw_exit(&ipst->ips_ill_g_lock);
13260 			return (-1);
13261 		}
13262 		ipif->ipif_id = id; /* assign new id */
13263 	} else if (id < ipst->ips_ip_addrs_per_if) {
13264 		/* we have a real id; insert ipif in the right place */
13265 		while ((tipif = *tipifp) != NULL) {
13266 			ASSERT(tipif->ipif_id != id);
13267 			if (tipif->ipif_id > id)
13268 				break; /* found correct location */
13269 			tipifp = &(tipif->ipif_next);
13270 		}
13271 	} else {
13272 		if (acquire_ill_lock)
13273 			mutex_exit(&ill->ill_lock);
13274 		if (acquire_g_lock)
13275 			rw_exit(&ipst->ips_ill_g_lock);
13276 		return (-1);
13277 	}
13278 
13279 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13280 
13281 	ipif->ipif_next = tipif;
13282 	*tipifp = ipif;
13283 	if (acquire_ill_lock)
13284 		mutex_exit(&ill->ill_lock);
13285 	if (acquire_g_lock)
13286 		rw_exit(&ipst->ips_ill_g_lock);
13287 	return (0);
13288 }
13289 
13290 static void
13291 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13292 {
13293 	ipif_t	**ipifp;
13294 	ill_t	*ill = ipif->ipif_ill;
13295 
13296 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13297 	if (acquire_ill_lock)
13298 		mutex_enter(&ill->ill_lock);
13299 	else
13300 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13301 
13302 	ipifp = &ill->ill_ipif;
13303 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13304 		if (*ipifp == ipif) {
13305 			*ipifp = ipif->ipif_next;
13306 			break;
13307 		}
13308 	}
13309 
13310 	if (acquire_ill_lock)
13311 		mutex_exit(&ill->ill_lock);
13312 }
13313 
13314 /*
13315  * Allocate and initialize a new interface control structure.  (Always
13316  * called as writer.)
13317  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13318  * is not part of the global linked list of ills. ipif_seqid is unique
13319  * in the system and to preserve the uniqueness, it is assigned only
13320  * when ill becomes part of the global list. At that point ill will
13321  * have a name. If it doesn't get assigned here, it will get assigned
13322  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13323  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13324  * the interface flags or any other information from the DL_INFO_ACK for
13325  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13326  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13327  * second DL_INFO_ACK comes in from the driver.
13328  */
13329 static ipif_t *
13330 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13331 {
13332 	ipif_t	*ipif;
13333 	phyint_t *phyi;
13334 
13335 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13336 	    ill->ill_name, id, (void *)ill));
13337 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13338 
13339 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13340 		return (NULL);
13341 	*ipif = ipif_zero;	/* start clean */
13342 
13343 	ipif->ipif_ill = ill;
13344 	ipif->ipif_id = id;	/* could be -1 */
13345 	/*
13346 	 * Inherit the zoneid from the ill; for the shared stack instance
13347 	 * this is always the global zone
13348 	 */
13349 	ipif->ipif_zoneid = ill->ill_zoneid;
13350 
13351 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13352 
13353 	ipif->ipif_refcnt = 0;
13354 	ipif->ipif_saved_ire_cnt = 0;
13355 
13356 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13357 		mi_free(ipif);
13358 		return (NULL);
13359 	}
13360 	/* -1 id should have been replaced by real id */
13361 	id = ipif->ipif_id;
13362 	ASSERT(id >= 0);
13363 
13364 	if (ill->ill_name[0] != '\0')
13365 		ipif_assign_seqid(ipif);
13366 
13367 	/*
13368 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13369 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13370 	 * ioctl sets ipif_orig_ipifid to zero.
13371 	 */
13372 	ipif->ipif_orig_ipifid = id;
13373 
13374 	/*
13375 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13376 	 * The ipif is still not up and can't be looked up until the
13377 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13378 	 */
13379 	mutex_enter(&ill->ill_lock);
13380 	mutex_enter(&ill->ill_phyint->phyint_lock);
13381 	/*
13382 	 * Set the running flag when logical interface zero is created.
13383 	 * For subsequent logical interfaces, a DLPI link down
13384 	 * notification message may have cleared the running flag to
13385 	 * indicate the link is down, so we shouldn't just blindly set it.
13386 	 */
13387 	if (id == 0)
13388 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13389 	ipif->ipif_ire_type = ire_type;
13390 	phyi = ill->ill_phyint;
13391 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13392 
13393 	if (ipif->ipif_isv6) {
13394 		ill->ill_flags |= ILLF_IPV6;
13395 	} else {
13396 		ipaddr_t inaddr_any = INADDR_ANY;
13397 
13398 		ill->ill_flags |= ILLF_IPV4;
13399 
13400 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13401 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13402 		    &ipif->ipif_v6lcl_addr);
13403 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13404 		    &ipif->ipif_v6src_addr);
13405 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13406 		    &ipif->ipif_v6subnet);
13407 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13408 		    &ipif->ipif_v6net_mask);
13409 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13410 		    &ipif->ipif_v6brd_addr);
13411 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13412 		    &ipif->ipif_v6pp_dst_addr);
13413 	}
13414 
13415 	/*
13416 	 * Don't set the interface flags etc. now, will do it in
13417 	 * ip_ll_subnet_defaults.
13418 	 */
13419 	if (!initialize) {
13420 		mutex_exit(&ill->ill_lock);
13421 		mutex_exit(&ill->ill_phyint->phyint_lock);
13422 		return (ipif);
13423 	}
13424 	ipif->ipif_mtu = ill->ill_max_mtu;
13425 
13426 	if (ill->ill_bcast_addr_length != 0) {
13427 		/*
13428 		 * Later detect lack of DLPI driver multicast
13429 		 * capability by catching DL_ENABMULTI errors in
13430 		 * ip_rput_dlpi.
13431 		 */
13432 		ill->ill_flags |= ILLF_MULTICAST;
13433 		if (!ipif->ipif_isv6)
13434 			ipif->ipif_flags |= IPIF_BROADCAST;
13435 	} else {
13436 		if (ill->ill_net_type != IRE_LOOPBACK) {
13437 			if (ipif->ipif_isv6)
13438 				/*
13439 				 * Note: xresolv interfaces will eventually need
13440 				 * NOARP set here as well, but that will require
13441 				 * those external resolvers to have some
13442 				 * knowledge of that flag and act appropriately.
13443 				 * Not to be changed at present.
13444 				 */
13445 				ill->ill_flags |= ILLF_NONUD;
13446 			else
13447 				ill->ill_flags |= ILLF_NOARP;
13448 		}
13449 		if (ill->ill_phys_addr_length == 0) {
13450 			if (ill->ill_media &&
13451 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13452 				ipif->ipif_flags |= IPIF_NOXMIT;
13453 				phyi->phyint_flags |= PHYI_VIRTUAL;
13454 			} else {
13455 				/* pt-pt supports multicast. */
13456 				ill->ill_flags |= ILLF_MULTICAST;
13457 				if (ill->ill_net_type == IRE_LOOPBACK) {
13458 					phyi->phyint_flags |=
13459 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13460 				} else {
13461 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13462 				}
13463 			}
13464 		}
13465 	}
13466 	mutex_exit(&ill->ill_lock);
13467 	mutex_exit(&ill->ill_phyint->phyint_lock);
13468 	return (ipif);
13469 }
13470 
13471 /*
13472  * If appropriate, send a message up to the resolver delete the entry
13473  * for the address of this interface which is going out of business.
13474  * (Always called as writer).
13475  *
13476  * NOTE : We need to check for NULL mps as some of the fields are
13477  *	  initialized only for some interface types. See ipif_resolver_up()
13478  *	  for details.
13479  */
13480 void
13481 ipif_arp_down(ipif_t *ipif)
13482 {
13483 	mblk_t	*mp;
13484 	ill_t	*ill = ipif->ipif_ill;
13485 
13486 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13487 	ASSERT(IAM_WRITER_IPIF(ipif));
13488 
13489 	/* Delete the mapping for the local address */
13490 	mp = ipif->ipif_arp_del_mp;
13491 	if (mp != NULL) {
13492 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13493 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13494 		putnext(ill->ill_rq, mp);
13495 		ipif->ipif_arp_del_mp = NULL;
13496 	}
13497 
13498 	/*
13499 	 * If this is the last ipif that is going down and there are no
13500 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13501 	 * clean up ARP completely.
13502 	 */
13503 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13504 
13505 		/* Send up AR_INTERFACE_DOWN message */
13506 		mp = ill->ill_arp_down_mp;
13507 		if (mp != NULL) {
13508 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13509 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13510 			    ipif->ipif_id));
13511 			putnext(ill->ill_rq, mp);
13512 			ill->ill_arp_down_mp = NULL;
13513 		}
13514 
13515 		/* Tell ARP to delete the multicast mappings */
13516 		mp = ill->ill_arp_del_mapping_mp;
13517 		if (mp != NULL) {
13518 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13519 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13520 			    ipif->ipif_id));
13521 			putnext(ill->ill_rq, mp);
13522 			ill->ill_arp_del_mapping_mp = NULL;
13523 		}
13524 	}
13525 }
13526 
13527 /*
13528  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13529  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13530  * that it wants the add_mp allocated in this function to be returned
13531  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13532  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13533  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13534  * as it does a ipif_arp_down after calling this function - which will
13535  * remove what we add here.
13536  *
13537  * Returns -1 on failures and 0 on success.
13538  */
13539 int
13540 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13541 {
13542 	mblk_t	*del_mp = NULL;
13543 	mblk_t *add_mp = NULL;
13544 	mblk_t *mp;
13545 	ill_t	*ill = ipif->ipif_ill;
13546 	phyint_t *phyi = ill->ill_phyint;
13547 	ipaddr_t addr, mask, extract_mask = 0;
13548 	arma_t	*arma;
13549 	uint8_t *maddr, *bphys_addr;
13550 	uint32_t hw_start;
13551 	dl_unitdata_req_t *dlur;
13552 
13553 	ASSERT(IAM_WRITER_IPIF(ipif));
13554 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13555 		return (0);
13556 
13557 	/*
13558 	 * Delete the existing mapping from ARP. Normally ipif_down
13559 	 * -> ipif_arp_down should send this up to ARP. The only
13560 	 * reason we would find this when we are switching from
13561 	 * Multicast to Broadcast where we did not do a down.
13562 	 */
13563 	mp = ill->ill_arp_del_mapping_mp;
13564 	if (mp != NULL) {
13565 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13566 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13567 		putnext(ill->ill_rq, mp);
13568 		ill->ill_arp_del_mapping_mp = NULL;
13569 	}
13570 
13571 	if (arp_add_mapping_mp != NULL)
13572 		*arp_add_mapping_mp = NULL;
13573 
13574 	/*
13575 	 * Check that the address is not to long for the constant
13576 	 * length reserved in the template arma_t.
13577 	 */
13578 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13579 		return (-1);
13580 
13581 	/* Add mapping mblk */
13582 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13583 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13584 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13585 	    (caddr_t)&addr);
13586 	if (add_mp == NULL)
13587 		return (-1);
13588 	arma = (arma_t *)add_mp->b_rptr;
13589 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13590 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13591 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13592 
13593 	/*
13594 	 * Determine the broadcast address.
13595 	 */
13596 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13597 	if (ill->ill_sap_length < 0)
13598 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13599 	else
13600 		bphys_addr = (uchar_t *)dlur +
13601 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13602 	/*
13603 	 * Check PHYI_MULTI_BCAST and length of physical
13604 	 * address to determine if we use the mapping or the
13605 	 * broadcast address.
13606 	 */
13607 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13608 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13609 		    bphys_addr, maddr, &hw_start, &extract_mask))
13610 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13611 
13612 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13613 	    (ill->ill_flags & ILLF_MULTICAST)) {
13614 		/* Make sure this will not match the "exact" entry. */
13615 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13616 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13617 		    (caddr_t)&addr);
13618 		if (del_mp == NULL) {
13619 			freemsg(add_mp);
13620 			return (-1);
13621 		}
13622 		bcopy(&extract_mask, (char *)arma +
13623 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13624 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13625 			/* Use link-layer broadcast address for MULTI_BCAST */
13626 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13627 			ip2dbg(("ipif_arp_setup_multicast: adding"
13628 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13629 		} else {
13630 			arma->arma_hw_mapping_start = hw_start;
13631 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13632 			    " ARP setup for %s\n", ill->ill_name));
13633 		}
13634 	} else {
13635 		freemsg(add_mp);
13636 		ASSERT(del_mp == NULL);
13637 		/* It is neither MULTICAST nor MULTI_BCAST */
13638 		return (0);
13639 	}
13640 	ASSERT(add_mp != NULL && del_mp != NULL);
13641 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13642 	ill->ill_arp_del_mapping_mp = del_mp;
13643 	if (arp_add_mapping_mp != NULL) {
13644 		/* The caller just wants the mblks allocated */
13645 		*arp_add_mapping_mp = add_mp;
13646 	} else {
13647 		/* The caller wants us to send it to arp */
13648 		putnext(ill->ill_rq, add_mp);
13649 	}
13650 	return (0);
13651 }
13652 
13653 /*
13654  * Get the resolver set up for a new interface address.
13655  * (Always called as writer.)
13656  * Called both for IPv4 and IPv6 interfaces,
13657  * though it only sets up the resolver for v6
13658  * if it's an xresolv interface (one using an external resolver).
13659  * Honors ILLF_NOARP.
13660  * The enumerated value res_act is used to tune the behavior.
13661  * If set to Res_act_initial, then we set up all the resolver
13662  * structures for a new interface.  If set to Res_act_move, then
13663  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13664  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13665  * asynchronous hardware address change notification.  If set to
13666  * Res_act_defend, then we tell ARP that it needs to send a single
13667  * gratuitous message in defense of the address.
13668  * Returns error on failure.
13669  */
13670 int
13671 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13672 {
13673 	caddr_t	addr;
13674 	mblk_t	*arp_up_mp = NULL;
13675 	mblk_t	*arp_down_mp = NULL;
13676 	mblk_t	*arp_add_mp = NULL;
13677 	mblk_t	*arp_del_mp = NULL;
13678 	mblk_t	*arp_add_mapping_mp = NULL;
13679 	mblk_t	*arp_del_mapping_mp = NULL;
13680 	ill_t	*ill = ipif->ipif_ill;
13681 	uchar_t	*area_p = NULL;
13682 	uchar_t	*ared_p = NULL;
13683 	int	err = ENOMEM;
13684 	boolean_t was_dup;
13685 
13686 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13687 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13688 	ASSERT(IAM_WRITER_IPIF(ipif));
13689 
13690 	was_dup = B_FALSE;
13691 	if (res_act == Res_act_initial) {
13692 		ipif->ipif_addr_ready = 0;
13693 		/*
13694 		 * We're bringing an interface up here.  There's no way that we
13695 		 * should need to shut down ARP now.
13696 		 */
13697 		mutex_enter(&ill->ill_lock);
13698 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13699 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13700 			ill->ill_ipif_dup_count--;
13701 			was_dup = B_TRUE;
13702 		}
13703 		mutex_exit(&ill->ill_lock);
13704 	}
13705 	if (ipif->ipif_recovery_id != 0)
13706 		(void) untimeout(ipif->ipif_recovery_id);
13707 	ipif->ipif_recovery_id = 0;
13708 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13709 		ipif->ipif_addr_ready = 1;
13710 		return (0);
13711 	}
13712 	/* NDP will set the ipif_addr_ready flag when it's ready */
13713 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13714 		return (0);
13715 
13716 	if (ill->ill_isv6) {
13717 		/*
13718 		 * External resolver for IPv6
13719 		 */
13720 		ASSERT(res_act == Res_act_initial);
13721 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13722 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13723 			area_p = (uchar_t *)&ip6_area_template;
13724 			ared_p = (uchar_t *)&ip6_ared_template;
13725 		}
13726 	} else {
13727 		/*
13728 		 * IPv4 arp case. If the ARP stream has already started
13729 		 * closing, fail this request for ARP bringup. Else
13730 		 * record the fact that an ARP bringup is pending.
13731 		 */
13732 		mutex_enter(&ill->ill_lock);
13733 		if (ill->ill_arp_closing) {
13734 			mutex_exit(&ill->ill_lock);
13735 			err = EINVAL;
13736 			goto failed;
13737 		} else {
13738 			if (ill->ill_ipif_up_count == 0 &&
13739 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13740 				ill->ill_arp_bringup_pending = 1;
13741 			mutex_exit(&ill->ill_lock);
13742 		}
13743 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13744 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13745 			area_p = (uchar_t *)&ip_area_template;
13746 			ared_p = (uchar_t *)&ip_ared_template;
13747 		}
13748 	}
13749 
13750 	/*
13751 	 * Add an entry for the local address in ARP only if it
13752 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13753 	 */
13754 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13755 		area_t *area;
13756 
13757 		/* Now ask ARP to publish our address. */
13758 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13759 		if (arp_add_mp == NULL)
13760 			goto failed;
13761 		area = (area_t *)arp_add_mp->b_rptr;
13762 		if (res_act != Res_act_initial) {
13763 			/*
13764 			 * Copy the new hardware address and length into
13765 			 * arp_add_mp to be sent to ARP.
13766 			 */
13767 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13768 			bcopy(ill->ill_phys_addr,
13769 			    ((char *)area + area->area_hw_addr_offset),
13770 			    area->area_hw_addr_length);
13771 		}
13772 
13773 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13774 		    ACE_F_MYADDR;
13775 
13776 		if (res_act == Res_act_defend) {
13777 			area->area_flags |= ACE_F_DEFEND;
13778 			/*
13779 			 * If we're just defending our address now, then
13780 			 * there's no need to set up ARP multicast mappings.
13781 			 * The publish command is enough.
13782 			 */
13783 			goto done;
13784 		}
13785 
13786 		if (res_act != Res_act_initial)
13787 			goto arp_setup_multicast;
13788 
13789 		/*
13790 		 * Allocate an ARP deletion message so we know we can tell ARP
13791 		 * when the interface goes down.
13792 		 */
13793 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13794 		if (arp_del_mp == NULL)
13795 			goto failed;
13796 
13797 	} else {
13798 		if (res_act != Res_act_initial)
13799 			goto done;
13800 	}
13801 	/*
13802 	 * Need to bring up ARP or setup multicast mapping only
13803 	 * when the first interface is coming UP.
13804 	 */
13805 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13806 	    was_dup) {
13807 		goto done;
13808 	}
13809 
13810 	/*
13811 	 * Allocate an ARP down message (to be saved) and an ARP up
13812 	 * message.
13813 	 */
13814 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13815 	if (arp_down_mp == NULL)
13816 		goto failed;
13817 
13818 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13819 	if (arp_up_mp == NULL)
13820 		goto failed;
13821 
13822 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13823 		goto done;
13824 
13825 arp_setup_multicast:
13826 	/*
13827 	 * Setup the multicast mappings. This function initializes
13828 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13829 	 * IPv6.
13830 	 */
13831 	if (!ill->ill_isv6) {
13832 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13833 		if (err != 0)
13834 			goto failed;
13835 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13836 		ASSERT(arp_add_mapping_mp != NULL);
13837 	}
13838 
13839 done:
13840 	if (arp_del_mp != NULL) {
13841 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13842 		ipif->ipif_arp_del_mp = arp_del_mp;
13843 	}
13844 	if (arp_down_mp != NULL) {
13845 		ASSERT(ill->ill_arp_down_mp == NULL);
13846 		ill->ill_arp_down_mp = arp_down_mp;
13847 	}
13848 	if (arp_del_mapping_mp != NULL) {
13849 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13850 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13851 	}
13852 	if (arp_up_mp != NULL) {
13853 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13854 		    ill->ill_name, ipif->ipif_id));
13855 		putnext(ill->ill_rq, arp_up_mp);
13856 	}
13857 	if (arp_add_mp != NULL) {
13858 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13859 		    ill->ill_name, ipif->ipif_id));
13860 		/*
13861 		 * If it's an extended ARP implementation, then we'll wait to
13862 		 * hear that DAD has finished before using the interface.
13863 		 */
13864 		if (!ill->ill_arp_extend)
13865 			ipif->ipif_addr_ready = 1;
13866 		putnext(ill->ill_rq, arp_add_mp);
13867 	} else {
13868 		ipif->ipif_addr_ready = 1;
13869 	}
13870 	if (arp_add_mapping_mp != NULL) {
13871 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13872 		    ill->ill_name, ipif->ipif_id));
13873 		putnext(ill->ill_rq, arp_add_mapping_mp);
13874 	}
13875 	if (res_act != Res_act_initial)
13876 		return (0);
13877 
13878 	if (ill->ill_flags & ILLF_NOARP)
13879 		err = ill_arp_off(ill);
13880 	else
13881 		err = ill_arp_on(ill);
13882 	if (err != 0) {
13883 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13884 		freemsg(ipif->ipif_arp_del_mp);
13885 		freemsg(ill->ill_arp_down_mp);
13886 		freemsg(ill->ill_arp_del_mapping_mp);
13887 		ipif->ipif_arp_del_mp = NULL;
13888 		ill->ill_arp_down_mp = NULL;
13889 		ill->ill_arp_del_mapping_mp = NULL;
13890 		return (err);
13891 	}
13892 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13893 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13894 
13895 failed:
13896 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13897 	freemsg(arp_add_mp);
13898 	freemsg(arp_del_mp);
13899 	freemsg(arp_add_mapping_mp);
13900 	freemsg(arp_up_mp);
13901 	freemsg(arp_down_mp);
13902 	ill->ill_arp_bringup_pending = 0;
13903 	return (err);
13904 }
13905 
13906 /*
13907  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13908  * just gone back up.
13909  */
13910 static void
13911 ipif_arp_start_dad(ipif_t *ipif)
13912 {
13913 	ill_t *ill = ipif->ipif_ill;
13914 	mblk_t *arp_add_mp;
13915 	area_t *area;
13916 
13917 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13918 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13919 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13920 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
13921 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
13922 		/*
13923 		 * If we can't contact ARP for some reason, that's not really a
13924 		 * problem.  Just send out the routing socket notification that
13925 		 * DAD completion would have done, and continue.
13926 		 */
13927 		ipif_mask_reply(ipif);
13928 		ip_rts_ifmsg(ipif);
13929 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13930 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13931 		ipif->ipif_addr_ready = 1;
13932 		return;
13933 	}
13934 
13935 	/* Setting the 'unverified' flag restarts DAD */
13936 	area = (area_t *)arp_add_mp->b_rptr;
13937 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
13938 	    ACE_F_UNVERIFIED;
13939 	putnext(ill->ill_rq, arp_add_mp);
13940 }
13941 
13942 static void
13943 ipif_ndp_start_dad(ipif_t *ipif)
13944 {
13945 	nce_t *nce;
13946 
13947 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
13948 	if (nce == NULL)
13949 		return;
13950 
13951 	if (!ndp_restart_dad(nce)) {
13952 		/*
13953 		 * If we can't restart DAD for some reason, that's not really a
13954 		 * problem.  Just send out the routing socket notification that
13955 		 * DAD completion would have done, and continue.
13956 		 */
13957 		ip_rts_ifmsg(ipif);
13958 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13959 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13960 		ipif->ipif_addr_ready = 1;
13961 	}
13962 	NCE_REFRELE(nce);
13963 }
13964 
13965 /*
13966  * Restart duplicate address detection on all interfaces on the given ill.
13967  *
13968  * This is called when an interface transitions from down to up
13969  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13970  *
13971  * Note that since the underlying physical link has transitioned, we must cause
13972  * at least one routing socket message to be sent here, either via DAD
13973  * completion or just by default on the first ipif.  (If we don't do this, then
13974  * in.mpathd will see long delays when doing link-based failure recovery.)
13975  */
13976 void
13977 ill_restart_dad(ill_t *ill, boolean_t went_up)
13978 {
13979 	ipif_t *ipif;
13980 
13981 	if (ill == NULL)
13982 		return;
13983 
13984 	/*
13985 	 * If layer two doesn't support duplicate address detection, then just
13986 	 * send the routing socket message now and be done with it.
13987 	 */
13988 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
13989 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
13990 		ip_rts_ifmsg(ill->ill_ipif);
13991 		return;
13992 	}
13993 
13994 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13995 		if (went_up) {
13996 			if (ipif->ipif_flags & IPIF_UP) {
13997 				if (ill->ill_isv6)
13998 					ipif_ndp_start_dad(ipif);
13999 				else
14000 					ipif_arp_start_dad(ipif);
14001 			} else if (ill->ill_isv6 &&
14002 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14003 				/*
14004 				 * For IPv4, the ARP module itself will
14005 				 * automatically start the DAD process when it
14006 				 * sees DL_NOTE_LINK_UP.  We respond to the
14007 				 * AR_CN_READY at the completion of that task.
14008 				 * For IPv6, we must kick off the bring-up
14009 				 * process now.
14010 				 */
14011 				ndp_do_recovery(ipif);
14012 			} else {
14013 				/*
14014 				 * Unfortunately, the first ipif is "special"
14015 				 * and represents the underlying ill in the
14016 				 * routing socket messages.  Thus, when this
14017 				 * one ipif is down, we must still notify so
14018 				 * that the user knows the IFF_RUNNING status
14019 				 * change.  (If the first ipif is up, then
14020 				 * we'll handle eventual routing socket
14021 				 * notification via DAD completion.)
14022 				 */
14023 				if (ipif == ill->ill_ipif)
14024 					ip_rts_ifmsg(ill->ill_ipif);
14025 			}
14026 		} else {
14027 			/*
14028 			 * After link down, we'll need to send a new routing
14029 			 * message when the link comes back, so clear
14030 			 * ipif_addr_ready.
14031 			 */
14032 			ipif->ipif_addr_ready = 0;
14033 		}
14034 	}
14035 
14036 	/*
14037 	 * If we've torn down links, then notify the user right away.
14038 	 */
14039 	if (!went_up)
14040 		ip_rts_ifmsg(ill->ill_ipif);
14041 }
14042 
14043 /*
14044  * Wakeup all threads waiting to enter the ipsq, and sleeping
14045  * on any of the ills in this ipsq. The ill_lock of the ill
14046  * must be held so that waiters don't miss wakeups
14047  */
14048 static void
14049 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14050 {
14051 	phyint_t *phyint;
14052 
14053 	phyint = ipsq->ipsq_phyint_list;
14054 	while (phyint != NULL) {
14055 		if (phyint->phyint_illv4) {
14056 			if (!caller_holds_lock)
14057 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14058 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14059 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14060 			if (!caller_holds_lock)
14061 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14062 		}
14063 		if (phyint->phyint_illv6) {
14064 			if (!caller_holds_lock)
14065 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14066 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14067 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14068 			if (!caller_holds_lock)
14069 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14070 		}
14071 		phyint = phyint->phyint_ipsq_next;
14072 	}
14073 }
14074 
14075 static ipsq_t *
14076 ipsq_create(char *groupname, ip_stack_t *ipst)
14077 {
14078 	ipsq_t	*ipsq;
14079 
14080 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14081 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14082 	if (ipsq == NULL) {
14083 		return (NULL);
14084 	}
14085 
14086 	if (groupname != NULL)
14087 		(void) strcpy(ipsq->ipsq_name, groupname);
14088 	else
14089 		ipsq->ipsq_name[0] = '\0';
14090 
14091 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14092 	ipsq->ipsq_flags |= IPSQ_GROUP;
14093 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14094 	ipst->ips_ipsq_g_head = ipsq;
14095 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14096 	return (ipsq);
14097 }
14098 
14099 /*
14100  * Return an ipsq correspoding to the groupname. If 'create' is true
14101  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14102  * uniquely with an IPMP group. However during IPMP groupname operations,
14103  * multiple IPMP groups may be associated with a single ipsq. But no
14104  * IPMP group can be associated with more than 1 ipsq at any time.
14105  * For example
14106  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14107  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14108  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14109  *
14110  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14111  * status shown below during the execution of the above command.
14112  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14113  *
14114  * After the completion of the above groupname command we return to the stable
14115  * state shown below.
14116  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14117  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14118  *
14119  * Because of the above, we don't search based on the ipsq_name since that
14120  * would miss the correct ipsq during certain windows as shown above.
14121  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14122  * natural state.
14123  */
14124 static ipsq_t *
14125 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14126     ip_stack_t *ipst)
14127 {
14128 	ipsq_t	*ipsq;
14129 	int	group_len;
14130 	phyint_t *phyint;
14131 
14132 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14133 
14134 	group_len = strlen(groupname);
14135 	ASSERT(group_len != 0);
14136 	group_len++;
14137 
14138 	for (ipsq = ipst->ips_ipsq_g_head;
14139 	    ipsq != NULL;
14140 	    ipsq = ipsq->ipsq_next) {
14141 		/*
14142 		 * When an ipsq is being split, and ill_split_ipsq
14143 		 * calls this function, we exclude it from being considered.
14144 		 */
14145 		if (ipsq == exclude_ipsq)
14146 			continue;
14147 
14148 		/*
14149 		 * Compare against the ipsq_name. The groupname change happens
14150 		 * in 2 phases. The 1st phase merges the from group into
14151 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14152 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14153 		 * ipsq_name. At this point the phyint_groupname has not been
14154 		 * updated.
14155 		 */
14156 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14157 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14158 			/*
14159 			 * Verify that an ipmp groupname is exactly
14160 			 * part of 1 ipsq and is not found in any other
14161 			 * ipsq.
14162 			 */
14163 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14164 			    NULL);
14165 			return (ipsq);
14166 		}
14167 
14168 		/*
14169 		 * Comparison against ipsq_name alone is not sufficient.
14170 		 * In the case when groups are currently being
14171 		 * merged, the ipsq could hold other IPMP groups temporarily.
14172 		 * so we walk the phyint list and compare against the
14173 		 * phyint_groupname as well.
14174 		 */
14175 		phyint = ipsq->ipsq_phyint_list;
14176 		while (phyint != NULL) {
14177 			if ((group_len == phyint->phyint_groupname_len) &&
14178 			    (bcmp(phyint->phyint_groupname, groupname,
14179 			    group_len) == 0)) {
14180 				/*
14181 				 * Verify that an ipmp groupname is exactly
14182 				 * part of 1 ipsq and is not found in any other
14183 				 * ipsq.
14184 				 */
14185 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14186 				    ipst) == NULL);
14187 				return (ipsq);
14188 			}
14189 			phyint = phyint->phyint_ipsq_next;
14190 		}
14191 	}
14192 	if (create)
14193 		ipsq = ipsq_create(groupname, ipst);
14194 	return (ipsq);
14195 }
14196 
14197 static void
14198 ipsq_delete(ipsq_t *ipsq)
14199 {
14200 	ipsq_t *nipsq;
14201 	ipsq_t *pipsq = NULL;
14202 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14203 
14204 	/*
14205 	 * We don't hold the ipsq lock, but we are sure no new
14206 	 * messages can land up, since the ipsq_refs is zero.
14207 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14208 	 * is associated with this ipsq. (Lookups are based on ill_name
14209 	 * or phyint_groupname)
14210 	 */
14211 	ASSERT(ipsq->ipsq_refs == 0);
14212 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14213 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14214 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14215 		/*
14216 		 * This is not the ipsq of an IPMP group.
14217 		 */
14218 		ipsq->ipsq_ipst = NULL;
14219 		kmem_free(ipsq, sizeof (ipsq_t));
14220 		return;
14221 	}
14222 
14223 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14224 
14225 	/*
14226 	 * Locate the ipsq  before we can remove it from
14227 	 * the singly linked list of ipsq's.
14228 	 */
14229 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14230 	    nipsq = nipsq->ipsq_next) {
14231 		if (nipsq == ipsq) {
14232 			break;
14233 		}
14234 		pipsq = nipsq;
14235 	}
14236 
14237 	ASSERT(nipsq == ipsq);
14238 
14239 	/* unlink ipsq from the list */
14240 	if (pipsq != NULL)
14241 		pipsq->ipsq_next = ipsq->ipsq_next;
14242 	else
14243 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14244 	ipsq->ipsq_ipst = NULL;
14245 	kmem_free(ipsq, sizeof (ipsq_t));
14246 	rw_exit(&ipst->ips_ill_g_lock);
14247 }
14248 
14249 static void
14250 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14251     queue_t *q)
14252 {
14253 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14254 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14255 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14256 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14257 	ASSERT(current_mp != NULL);
14258 
14259 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14260 	    NEW_OP, NULL);
14261 
14262 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14263 	    new_ipsq->ipsq_xopq_mphead != NULL);
14264 
14265 	/*
14266 	 * move from old ipsq to the new ipsq.
14267 	 */
14268 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14269 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14270 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14271 
14272 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14273 }
14274 
14275 void
14276 ill_group_cleanup(ill_t *ill)
14277 {
14278 	ill_t *ill_v4;
14279 	ill_t *ill_v6;
14280 	ipif_t *ipif;
14281 
14282 	ill_v4 = ill->ill_phyint->phyint_illv4;
14283 	ill_v6 = ill->ill_phyint->phyint_illv6;
14284 
14285 	if (ill_v4 != NULL) {
14286 		mutex_enter(&ill_v4->ill_lock);
14287 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14288 		    ipif = ipif->ipif_next) {
14289 			IPIF_UNMARK_MOVING(ipif);
14290 		}
14291 		ill_v4->ill_up_ipifs = B_FALSE;
14292 		mutex_exit(&ill_v4->ill_lock);
14293 	}
14294 
14295 	if (ill_v6 != NULL) {
14296 		mutex_enter(&ill_v6->ill_lock);
14297 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14298 		    ipif = ipif->ipif_next) {
14299 			IPIF_UNMARK_MOVING(ipif);
14300 		}
14301 		ill_v6->ill_up_ipifs = B_FALSE;
14302 		mutex_exit(&ill_v6->ill_lock);
14303 	}
14304 }
14305 /*
14306  * This function is called when an ill has had a change in its group status
14307  * to bring up all the ipifs that were up before the change.
14308  */
14309 int
14310 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14311 {
14312 	ipif_t *ipif;
14313 	ill_t *ill_v4;
14314 	ill_t *ill_v6;
14315 	ill_t *from_ill;
14316 	int err = 0;
14317 
14318 	ASSERT(IAM_WRITER_ILL(ill));
14319 
14320 	/*
14321 	 * Except for ipif_state_flags and ill_state_flags the other
14322 	 * fields of the ipif/ill that are modified below are protected
14323 	 * implicitly since we are a writer. We would have tried to down
14324 	 * even an ipif that was already down, in ill_down_ipifs. So we
14325 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14326 	 */
14327 	ill_v4 = ill->ill_phyint->phyint_illv4;
14328 	ill_v6 = ill->ill_phyint->phyint_illv6;
14329 	if (ill_v4 != NULL) {
14330 		ill_v4->ill_up_ipifs = B_TRUE;
14331 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14332 		    ipif = ipif->ipif_next) {
14333 			mutex_enter(&ill_v4->ill_lock);
14334 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14335 			IPIF_UNMARK_MOVING(ipif);
14336 			mutex_exit(&ill_v4->ill_lock);
14337 			if (ipif->ipif_was_up) {
14338 				if (!(ipif->ipif_flags & IPIF_UP))
14339 					err = ipif_up(ipif, q, mp);
14340 				ipif->ipif_was_up = B_FALSE;
14341 				if (err != 0) {
14342 					/*
14343 					 * Can there be any other error ?
14344 					 */
14345 					ASSERT(err == EINPROGRESS);
14346 					return (err);
14347 				}
14348 			}
14349 		}
14350 		mutex_enter(&ill_v4->ill_lock);
14351 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14352 		mutex_exit(&ill_v4->ill_lock);
14353 		ill_v4->ill_up_ipifs = B_FALSE;
14354 		if (ill_v4->ill_move_in_progress) {
14355 			ASSERT(ill_v4->ill_move_peer != NULL);
14356 			ill_v4->ill_move_in_progress = B_FALSE;
14357 			from_ill = ill_v4->ill_move_peer;
14358 			from_ill->ill_move_in_progress = B_FALSE;
14359 			from_ill->ill_move_peer = NULL;
14360 			mutex_enter(&from_ill->ill_lock);
14361 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14362 			mutex_exit(&from_ill->ill_lock);
14363 			if (ill_v6 == NULL) {
14364 				if (from_ill->ill_phyint->phyint_flags &
14365 				    PHYI_STANDBY) {
14366 					phyint_inactive(from_ill->ill_phyint);
14367 				}
14368 				if (ill_v4->ill_phyint->phyint_flags &
14369 				    PHYI_STANDBY) {
14370 					phyint_inactive(ill_v4->ill_phyint);
14371 				}
14372 			}
14373 			ill_v4->ill_move_peer = NULL;
14374 		}
14375 	}
14376 
14377 	if (ill_v6 != NULL) {
14378 		ill_v6->ill_up_ipifs = B_TRUE;
14379 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14380 		    ipif = ipif->ipif_next) {
14381 			mutex_enter(&ill_v6->ill_lock);
14382 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14383 			IPIF_UNMARK_MOVING(ipif);
14384 			mutex_exit(&ill_v6->ill_lock);
14385 			if (ipif->ipif_was_up) {
14386 				if (!(ipif->ipif_flags & IPIF_UP))
14387 					err = ipif_up(ipif, q, mp);
14388 				ipif->ipif_was_up = B_FALSE;
14389 				if (err != 0) {
14390 					/*
14391 					 * Can there be any other error ?
14392 					 */
14393 					ASSERT(err == EINPROGRESS);
14394 					return (err);
14395 				}
14396 			}
14397 		}
14398 		mutex_enter(&ill_v6->ill_lock);
14399 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14400 		mutex_exit(&ill_v6->ill_lock);
14401 		ill_v6->ill_up_ipifs = B_FALSE;
14402 		if (ill_v6->ill_move_in_progress) {
14403 			ASSERT(ill_v6->ill_move_peer != NULL);
14404 			ill_v6->ill_move_in_progress = B_FALSE;
14405 			from_ill = ill_v6->ill_move_peer;
14406 			from_ill->ill_move_in_progress = B_FALSE;
14407 			from_ill->ill_move_peer = NULL;
14408 			mutex_enter(&from_ill->ill_lock);
14409 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14410 			mutex_exit(&from_ill->ill_lock);
14411 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14412 				phyint_inactive(from_ill->ill_phyint);
14413 			}
14414 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14415 				phyint_inactive(ill_v6->ill_phyint);
14416 			}
14417 			ill_v6->ill_move_peer = NULL;
14418 		}
14419 	}
14420 	return (0);
14421 }
14422 
14423 /*
14424  * bring down all the approriate ipifs.
14425  */
14426 /* ARGSUSED */
14427 static void
14428 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14429 {
14430 	ipif_t *ipif;
14431 
14432 	ASSERT(IAM_WRITER_ILL(ill));
14433 
14434 	/*
14435 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14436 	 * are modified below are protected implicitly since we are a writer
14437 	 */
14438 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14439 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14440 			continue;
14441 		/*
14442 		 * Don't bring down the LINK LOCAL addresses as they are tied
14443 		 * to physical interface and they don't move. Treat them as
14444 		 * IPIF_NOFAILOVER.
14445 		 */
14446 		if (chk_nofailover && ill->ill_isv6 &&
14447 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))
14448 			continue;
14449 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14450 			/*
14451 			 * We go through the ipif_down logic even if the ipif
14452 			 * is already down, since routes can be added based
14453 			 * on down ipifs. Going through ipif_down once again
14454 			 * will delete any IREs created based on these routes.
14455 			 */
14456 			if (ipif->ipif_flags & IPIF_UP)
14457 				ipif->ipif_was_up = B_TRUE;
14458 			/*
14459 			 * If called with chk_nofailover true ipif is moving.
14460 			 */
14461 			mutex_enter(&ill->ill_lock);
14462 			if (chk_nofailover) {
14463 				ipif->ipif_state_flags |=
14464 				    IPIF_MOVING | IPIF_CHANGING;
14465 			} else {
14466 				ipif->ipif_state_flags |= IPIF_CHANGING;
14467 			}
14468 			mutex_exit(&ill->ill_lock);
14469 			/*
14470 			 * Need to re-create net/subnet bcast ires if
14471 			 * they are dependent on ipif.
14472 			 */
14473 			if (!ipif->ipif_isv6)
14474 				ipif_check_bcast_ires(ipif);
14475 			(void) ipif_logical_down(ipif, NULL, NULL);
14476 			ipif_non_duplicate(ipif);
14477 			ipif_down_tail(ipif);
14478 		}
14479 	}
14480 }
14481 
14482 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14483 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14484 	(ipsq)->ipsq_refs++;				\
14485 }
14486 
14487 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14488 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14489 	(ipsq)->ipsq_refs--;				\
14490 	if ((ipsq)->ipsq_refs == 0)				\
14491 		(ipsq)->ipsq_name[0] = '\0'; 		\
14492 }
14493 
14494 /*
14495  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14496  * new_ipsq.
14497  */
14498 static void
14499 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14500 {
14501 	phyint_t *phyint;
14502 	phyint_t *next_phyint;
14503 
14504 	/*
14505 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14506 	 * writer and the ill_lock of the ill in question. Also the dest
14507 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14508 	 */
14509 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14510 
14511 	phyint = cur_ipsq->ipsq_phyint_list;
14512 	cur_ipsq->ipsq_phyint_list = NULL;
14513 	while (phyint != NULL) {
14514 		next_phyint = phyint->phyint_ipsq_next;
14515 		IPSQ_DEC_REF(cur_ipsq, ipst);
14516 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14517 		new_ipsq->ipsq_phyint_list = phyint;
14518 		IPSQ_INC_REF(new_ipsq, ipst);
14519 		phyint->phyint_ipsq = new_ipsq;
14520 		phyint = next_phyint;
14521 	}
14522 }
14523 
14524 #define	SPLIT_SUCCESS		0
14525 #define	SPLIT_NOT_NEEDED	1
14526 #define	SPLIT_FAILED		2
14527 
14528 int
14529 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14530     ip_stack_t *ipst)
14531 {
14532 	ipsq_t *newipsq = NULL;
14533 
14534 	/*
14535 	 * Assertions denote pre-requisites for changing the ipsq of
14536 	 * a phyint
14537 	 */
14538 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14539 	/*
14540 	 * <ill-phyint> assocs can't change while ill_g_lock
14541 	 * is held as writer. See ill_phyint_reinit()
14542 	 */
14543 	ASSERT(phyint->phyint_illv4 == NULL ||
14544 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14545 	ASSERT(phyint->phyint_illv6 == NULL ||
14546 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14547 
14548 	if ((phyint->phyint_groupname_len !=
14549 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14550 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14551 	    phyint->phyint_groupname_len) != 0)) {
14552 		/*
14553 		 * Once we fail in creating a new ipsq due to memory shortage,
14554 		 * don't attempt to create new ipsq again, based on another
14555 		 * phyint, since we want all phyints belonging to an IPMP group
14556 		 * to be in the same ipsq even in the event of mem alloc fails.
14557 		 */
14558 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14559 		    cur_ipsq, ipst);
14560 		if (newipsq == NULL) {
14561 			/* Memory allocation failure */
14562 			return (SPLIT_FAILED);
14563 		} else {
14564 			/* ipsq_refs protected by ill_g_lock (writer) */
14565 			IPSQ_DEC_REF(cur_ipsq, ipst);
14566 			phyint->phyint_ipsq = newipsq;
14567 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14568 			newipsq->ipsq_phyint_list = phyint;
14569 			IPSQ_INC_REF(newipsq, ipst);
14570 			return (SPLIT_SUCCESS);
14571 		}
14572 	}
14573 	return (SPLIT_NOT_NEEDED);
14574 }
14575 
14576 /*
14577  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14578  * to do this split
14579  */
14580 static int
14581 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14582 {
14583 	ipsq_t *newipsq;
14584 
14585 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14586 	/*
14587 	 * <ill-phyint> assocs can't change while ill_g_lock
14588 	 * is held as writer. See ill_phyint_reinit()
14589 	 */
14590 
14591 	ASSERT(phyint->phyint_illv4 == NULL ||
14592 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14593 	ASSERT(phyint->phyint_illv6 == NULL ||
14594 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14595 
14596 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14597 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14598 		/*
14599 		 * ipsq_init failed due to no memory
14600 		 * caller will use the same ipsq
14601 		 */
14602 		return (SPLIT_FAILED);
14603 	}
14604 
14605 	/* ipsq_ref is protected by ill_g_lock (writer) */
14606 	IPSQ_DEC_REF(cur_ipsq, ipst);
14607 
14608 	/*
14609 	 * This is a new ipsq that is unknown to the world.
14610 	 * So we don't need to hold ipsq_lock,
14611 	 */
14612 	newipsq = phyint->phyint_ipsq;
14613 	newipsq->ipsq_writer = NULL;
14614 	newipsq->ipsq_reentry_cnt--;
14615 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14616 #ifdef DEBUG
14617 	newipsq->ipsq_depth = 0;
14618 #endif
14619 
14620 	return (SPLIT_SUCCESS);
14621 }
14622 
14623 /*
14624  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14625  * ipsq's representing their individual groups or themselves. Return
14626  * whether split needs to be retried again later.
14627  */
14628 static boolean_t
14629 ill_split_ipsq(ipsq_t *cur_ipsq)
14630 {
14631 	phyint_t *phyint;
14632 	phyint_t *next_phyint;
14633 	int	error;
14634 	boolean_t need_retry = B_FALSE;
14635 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14636 
14637 	phyint = cur_ipsq->ipsq_phyint_list;
14638 	cur_ipsq->ipsq_phyint_list = NULL;
14639 	while (phyint != NULL) {
14640 		next_phyint = phyint->phyint_ipsq_next;
14641 		/*
14642 		 * 'created' will tell us whether the callee actually
14643 		 * created an ipsq. Lack of memory may force the callee
14644 		 * to return without creating an ipsq.
14645 		 */
14646 		if (phyint->phyint_groupname == NULL) {
14647 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14648 		} else {
14649 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14650 			    need_retry, ipst);
14651 		}
14652 
14653 		switch (error) {
14654 		case SPLIT_FAILED:
14655 			need_retry = B_TRUE;
14656 			/* FALLTHRU */
14657 		case SPLIT_NOT_NEEDED:
14658 			/*
14659 			 * Keep it on the list.
14660 			 */
14661 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14662 			cur_ipsq->ipsq_phyint_list = phyint;
14663 			break;
14664 		case SPLIT_SUCCESS:
14665 			break;
14666 		default:
14667 			ASSERT(0);
14668 		}
14669 
14670 		phyint = next_phyint;
14671 	}
14672 	return (need_retry);
14673 }
14674 
14675 /*
14676  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14677  * and return the ills in the list. This list will be
14678  * needed to unlock all the ills later on by the caller.
14679  * The <ill-ipsq> associations could change between the
14680  * lock and unlock. Hence the unlock can't traverse the
14681  * ipsq to get the list of ills.
14682  */
14683 static int
14684 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14685 {
14686 	int	cnt = 0;
14687 	phyint_t	*phyint;
14688 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14689 
14690 	/*
14691 	 * The caller holds ill_g_lock to ensure that the ill memberships
14692 	 * of the ipsq don't change
14693 	 */
14694 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14695 
14696 	phyint = ipsq->ipsq_phyint_list;
14697 	while (phyint != NULL) {
14698 		if (phyint->phyint_illv4 != NULL) {
14699 			ASSERT(cnt < list_max);
14700 			list[cnt++] = phyint->phyint_illv4;
14701 		}
14702 		if (phyint->phyint_illv6 != NULL) {
14703 			ASSERT(cnt < list_max);
14704 			list[cnt++] = phyint->phyint_illv6;
14705 		}
14706 		phyint = phyint->phyint_ipsq_next;
14707 	}
14708 	ill_lock_ills(list, cnt);
14709 	return (cnt);
14710 }
14711 
14712 void
14713 ill_lock_ills(ill_t **list, int cnt)
14714 {
14715 	int	i;
14716 
14717 	if (cnt > 1) {
14718 		boolean_t try_again;
14719 		do {
14720 			try_again = B_FALSE;
14721 			for (i = 0; i < cnt - 1; i++) {
14722 				if (list[i] < list[i + 1]) {
14723 					ill_t	*tmp;
14724 
14725 					/* swap the elements */
14726 					tmp = list[i];
14727 					list[i] = list[i + 1];
14728 					list[i + 1] = tmp;
14729 					try_again = B_TRUE;
14730 				}
14731 			}
14732 		} while (try_again);
14733 	}
14734 
14735 	for (i = 0; i < cnt; i++) {
14736 		if (i == 0) {
14737 			if (list[i] != NULL)
14738 				mutex_enter(&list[i]->ill_lock);
14739 			else
14740 				return;
14741 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14742 			mutex_enter(&list[i]->ill_lock);
14743 		}
14744 	}
14745 }
14746 
14747 void
14748 ill_unlock_ills(ill_t **list, int cnt)
14749 {
14750 	int	i;
14751 
14752 	for (i = 0; i < cnt; i++) {
14753 		if ((i == 0) && (list[i] != NULL)) {
14754 			mutex_exit(&list[i]->ill_lock);
14755 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14756 			mutex_exit(&list[i]->ill_lock);
14757 		}
14758 	}
14759 }
14760 
14761 /*
14762  * Merge all the ills from 1 ipsq group into another ipsq group.
14763  * The source ipsq group is specified by the ipsq associated with
14764  * 'from_ill'. The destination ipsq group is specified by the ipsq
14765  * associated with 'to_ill' or 'groupname' respectively.
14766  * Note that ipsq itself does not have a reference count mechanism
14767  * and functions don't look up an ipsq and pass it around. Instead
14768  * functions pass around an ill or groupname, and the ipsq is looked
14769  * up from the ill or groupname and the required operation performed
14770  * atomically with the lookup on the ipsq.
14771  */
14772 static int
14773 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14774     queue_t *q)
14775 {
14776 	ipsq_t *old_ipsq;
14777 	ipsq_t *new_ipsq;
14778 	ill_t	**ill_list;
14779 	int	cnt;
14780 	size_t	ill_list_size;
14781 	boolean_t became_writer_on_new_sq = B_FALSE;
14782 	ip_stack_t	*ipst = from_ill->ill_ipst;
14783 
14784 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
14785 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14786 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14787 
14788 	/*
14789 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14790 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14791 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14792 	 */
14793 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14794 
14795 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14796 	if (groupname != NULL)
14797 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
14798 	else {
14799 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14800 	}
14801 
14802 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14803 
14804 	/*
14805 	 * both groups are on the same ipsq.
14806 	 */
14807 	if (old_ipsq == new_ipsq) {
14808 		rw_exit(&ipst->ips_ill_g_lock);
14809 		return (0);
14810 	}
14811 
14812 	cnt = old_ipsq->ipsq_refs << 1;
14813 	ill_list_size = cnt * sizeof (ill_t *);
14814 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14815 	if (ill_list == NULL) {
14816 		rw_exit(&ipst->ips_ill_g_lock);
14817 		return (ENOMEM);
14818 	}
14819 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14820 
14821 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14822 	mutex_enter(&new_ipsq->ipsq_lock);
14823 	if ((new_ipsq->ipsq_writer == NULL &&
14824 	    new_ipsq->ipsq_current_ipif == NULL) ||
14825 	    (new_ipsq->ipsq_writer == curthread)) {
14826 		new_ipsq->ipsq_writer = curthread;
14827 		new_ipsq->ipsq_reentry_cnt++;
14828 		became_writer_on_new_sq = B_TRUE;
14829 	}
14830 
14831 	/*
14832 	 * We are holding ill_g_lock as writer and all the ill locks of
14833 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14834 	 * message can land up on the old ipsq even though we don't hold the
14835 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14836 	 */
14837 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14838 
14839 	/*
14840 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14841 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14842 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14843 	 */
14844 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
14845 
14846 	/*
14847 	 * Mark the new ipsq as needing a split since it is currently
14848 	 * being shared by more than 1 IPMP group. The split will
14849 	 * occur at the end of ipsq_exit
14850 	 */
14851 	new_ipsq->ipsq_split = B_TRUE;
14852 
14853 	/* Now release all the locks */
14854 	mutex_exit(&new_ipsq->ipsq_lock);
14855 	ill_unlock_ills(ill_list, cnt);
14856 	rw_exit(&ipst->ips_ill_g_lock);
14857 
14858 	kmem_free(ill_list, ill_list_size);
14859 
14860 	/*
14861 	 * If we succeeded in becoming writer on the new ipsq, then
14862 	 * drain the new ipsq and start processing  all enqueued messages
14863 	 * including the current ioctl we are processing which is either
14864 	 * a set groupname or failover/failback.
14865 	 */
14866 	if (became_writer_on_new_sq)
14867 		ipsq_exit(new_ipsq);
14868 
14869 	/*
14870 	 * syncq has been changed and all the messages have been moved.
14871 	 */
14872 	mutex_enter(&old_ipsq->ipsq_lock);
14873 	old_ipsq->ipsq_current_ipif = NULL;
14874 	old_ipsq->ipsq_current_ioctl = 0;
14875 	old_ipsq->ipsq_current_done = B_TRUE;
14876 	mutex_exit(&old_ipsq->ipsq_lock);
14877 	return (EINPROGRESS);
14878 }
14879 
14880 /*
14881  * Delete and add the loopback copy and non-loopback copy of
14882  * the BROADCAST ire corresponding to ill and addr. Used to
14883  * group broadcast ires together when ill becomes part of
14884  * a group.
14885  *
14886  * This function is also called when ill is leaving the group
14887  * so that the ires belonging to the group gets re-grouped.
14888  */
14889 static void
14890 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14891 {
14892 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14893 	ire_t **ire_ptpn = &ire_head;
14894 	ip_stack_t	*ipst = ill->ill_ipst;
14895 
14896 	/*
14897 	 * The loopback and non-loopback IREs are inserted in the order in which
14898 	 * they're found, on the basis that they are correctly ordered (loopback
14899 	 * first).
14900 	 */
14901 	for (;;) {
14902 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14903 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
14904 		if (ire == NULL)
14905 			break;
14906 
14907 		/*
14908 		 * we are passing in KM_SLEEP because it is not easy to
14909 		 * go back to a sane state in case of memory failure.
14910 		 */
14911 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14912 		ASSERT(nire != NULL);
14913 		bzero(nire, sizeof (ire_t));
14914 		/*
14915 		 * Don't use ire_max_frag directly since we don't
14916 		 * hold on to 'ire' until we add the new ire 'nire' and
14917 		 * we don't want the new ire to have a dangling reference
14918 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14919 		 * be in sync with the ipif_mtu of the associate ipif.
14920 		 * For eg. this happens as a result of SIOCSLIFNAME,
14921 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14922 		 * the driver. A change in ire_max_frag triggered as
14923 		 * as a result of path mtu discovery, or due to an
14924 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14925 		 * route change -mtu command does not apply to broadcast ires.
14926 		 *
14927 		 * XXX We need a recovery strategy here if ire_init fails
14928 		 */
14929 		if (ire_init(nire,
14930 		    (uchar_t *)&ire->ire_addr,
14931 		    (uchar_t *)&ire->ire_mask,
14932 		    (uchar_t *)&ire->ire_src_addr,
14933 		    (uchar_t *)&ire->ire_gateway_addr,
14934 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
14935 		    &ire->ire_ipif->ipif_mtu,
14936 		    ire->ire_nce,
14937 		    ire->ire_rfq,
14938 		    ire->ire_stq,
14939 		    ire->ire_type,
14940 		    ire->ire_ipif,
14941 		    ire->ire_cmask,
14942 		    ire->ire_phandle,
14943 		    ire->ire_ihandle,
14944 		    ire->ire_flags,
14945 		    &ire->ire_uinfo,
14946 		    NULL,
14947 		    NULL,
14948 		    ipst) == NULL) {
14949 			cmn_err(CE_PANIC, "ire_init() failed");
14950 		}
14951 		ire_delete(ire);
14952 		ire_refrele(ire);
14953 
14954 		/*
14955 		 * The newly created IREs are inserted at the tail of the list
14956 		 * starting with ire_head. As we've just allocated them no one
14957 		 * knows about them so it's safe.
14958 		 */
14959 		*ire_ptpn = nire;
14960 		ire_ptpn = &nire->ire_next;
14961 	}
14962 
14963 	for (nire = ire_head; nire != NULL; nire = nire_next) {
14964 		int error;
14965 		ire_t *oire;
14966 		/* unlink the IRE from our list before calling ire_add() */
14967 		nire_next = nire->ire_next;
14968 		nire->ire_next = NULL;
14969 
14970 		/* ire_add adds the ire at the right place in the list */
14971 		oire = nire;
14972 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
14973 		ASSERT(error == 0);
14974 		ASSERT(oire == nire);
14975 		ire_refrele(nire);	/* Held in ire_add */
14976 	}
14977 }
14978 
14979 /*
14980  * This function is usually called when an ill is inserted in
14981  * a group and all the ipifs are already UP. As all the ipifs
14982  * are already UP, the broadcast ires have already been created
14983  * and been inserted. But, ire_add_v4 would not have grouped properly.
14984  * We need to re-group for the benefit of ip_wput_ire which
14985  * expects BROADCAST ires to be grouped properly to avoid sending
14986  * more than one copy of the broadcast packet per group.
14987  *
14988  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
14989  *	  because when ipif_up_done ends up calling this, ires have
14990  *        already been added before illgrp_insert i.e before ill_group
14991  *	  has been initialized.
14992  */
14993 static void
14994 ill_group_bcast_for_xmit(ill_t *ill)
14995 {
14996 	ill_group_t *illgrp;
14997 	ipif_t *ipif;
14998 	ipaddr_t addr;
14999 	ipaddr_t net_mask;
15000 	ipaddr_t subnet_netmask;
15001 
15002 	illgrp = ill->ill_group;
15003 
15004 	/*
15005 	 * This function is called even when an ill is deleted from
15006 	 * the group. Hence, illgrp could be null.
15007 	 */
15008 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15009 		return;
15010 
15011 	/*
15012 	 * Delete all the BROADCAST ires matching this ill and add
15013 	 * them back. This time, ire_add_v4 should take care of
15014 	 * grouping them with others because ill is part of the
15015 	 * group.
15016 	 */
15017 	ill_bcast_delete_and_add(ill, 0);
15018 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15019 
15020 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15021 
15022 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15023 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15024 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15025 		} else {
15026 			net_mask = htonl(IN_CLASSA_NET);
15027 		}
15028 		addr = net_mask & ipif->ipif_subnet;
15029 		ill_bcast_delete_and_add(ill, addr);
15030 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15031 
15032 		subnet_netmask = ipif->ipif_net_mask;
15033 		addr = ipif->ipif_subnet;
15034 		ill_bcast_delete_and_add(ill, addr);
15035 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15036 	}
15037 }
15038 
15039 /*
15040  * This function is called from illgrp_delete when ill is being deleted
15041  * from the group.
15042  *
15043  * As ill is not there in the group anymore, any address belonging
15044  * to this ill should be cleared of IRE_MARK_NORECV.
15045  */
15046 static void
15047 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15048 {
15049 	ire_t *ire;
15050 	irb_t *irb;
15051 	ip_stack_t	*ipst = ill->ill_ipst;
15052 
15053 	ASSERT(ill->ill_group == NULL);
15054 
15055 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15056 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15057 
15058 	if (ire != NULL) {
15059 		/*
15060 		 * IPMP and plumbing operations are serialized on the ipsq, so
15061 		 * no one will insert or delete a broadcast ire under our feet.
15062 		 */
15063 		irb = ire->ire_bucket;
15064 		rw_enter(&irb->irb_lock, RW_READER);
15065 		ire_refrele(ire);
15066 
15067 		for (; ire != NULL; ire = ire->ire_next) {
15068 			if (ire->ire_addr != addr)
15069 				break;
15070 			if (ire_to_ill(ire) != ill)
15071 				continue;
15072 
15073 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15074 			ire->ire_marks &= ~IRE_MARK_NORECV;
15075 		}
15076 		rw_exit(&irb->irb_lock);
15077 	}
15078 }
15079 
15080 /*
15081  * This function must be called only after the broadcast ires
15082  * have been grouped together. For a given address addr, nominate
15083  * only one of the ires whose interface is not FAILED or OFFLINE.
15084  *
15085  * This is also called when an ipif goes down, so that we can nominate
15086  * a different ire with the same address for receiving.
15087  */
15088 static void
15089 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15090 {
15091 	irb_t *irb;
15092 	ire_t *ire;
15093 	ire_t *ire1;
15094 	ire_t *save_ire;
15095 	ire_t **irep = NULL;
15096 	boolean_t first = B_TRUE;
15097 	ire_t *clear_ire = NULL;
15098 	ire_t *start_ire = NULL;
15099 	ire_t	*new_lb_ire;
15100 	ire_t	*new_nlb_ire;
15101 	boolean_t new_lb_ire_used = B_FALSE;
15102 	boolean_t new_nlb_ire_used = B_FALSE;
15103 	uint64_t match_flags;
15104 	uint64_t phyi_flags;
15105 	boolean_t fallback = B_FALSE;
15106 	uint_t	max_frag;
15107 
15108 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15109 	    NULL, MATCH_IRE_TYPE, ipst);
15110 	/*
15111 	 * We may not be able to find some ires if a previous
15112 	 * ire_create failed. This happens when an ipif goes
15113 	 * down and we are unable to create BROADCAST ires due
15114 	 * to memory failure. Thus, we have to check for NULL
15115 	 * below. This should handle the case for LOOPBACK,
15116 	 * POINTOPOINT and interfaces with some POINTOPOINT
15117 	 * logicals for which there are no BROADCAST ires.
15118 	 */
15119 	if (ire == NULL)
15120 		return;
15121 	/*
15122 	 * Currently IRE_BROADCASTS are deleted when an ipif
15123 	 * goes down which runs exclusively. Thus, setting
15124 	 * IRE_MARK_RCVD should not race with ire_delete marking
15125 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15126 	 * be consistent with other parts of the code that walks
15127 	 * a given bucket.
15128 	 */
15129 	save_ire = ire;
15130 	irb = ire->ire_bucket;
15131 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15132 	if (new_lb_ire == NULL) {
15133 		ire_refrele(ire);
15134 		return;
15135 	}
15136 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15137 	if (new_nlb_ire == NULL) {
15138 		ire_refrele(ire);
15139 		kmem_cache_free(ire_cache, new_lb_ire);
15140 		return;
15141 	}
15142 	IRB_REFHOLD(irb);
15143 	rw_enter(&irb->irb_lock, RW_WRITER);
15144 	/*
15145 	 * Get to the first ire matching the address and the
15146 	 * group. If the address does not match we are done
15147 	 * as we could not find the IRE. If the address matches
15148 	 * we should get to the first one matching the group.
15149 	 */
15150 	while (ire != NULL) {
15151 		if (ire->ire_addr != addr ||
15152 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15153 			break;
15154 		}
15155 		ire = ire->ire_next;
15156 	}
15157 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15158 	start_ire = ire;
15159 redo:
15160 	while (ire != NULL && ire->ire_addr == addr &&
15161 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15162 		/*
15163 		 * The first ire for any address within a group
15164 		 * should always be the one with IRE_MARK_NORECV cleared
15165 		 * so that ip_wput_ire can avoid searching for one.
15166 		 * Note down the insertion point which will be used
15167 		 * later.
15168 		 */
15169 		if (first && (irep == NULL))
15170 			irep = ire->ire_ptpn;
15171 		/*
15172 		 * PHYI_FAILED is set when the interface fails.
15173 		 * This interface might have become good, but the
15174 		 * daemon has not yet detected. We should still
15175 		 * not receive on this. PHYI_OFFLINE should never
15176 		 * be picked as this has been offlined and soon
15177 		 * be removed.
15178 		 */
15179 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15180 		if (phyi_flags & PHYI_OFFLINE) {
15181 			ire->ire_marks |= IRE_MARK_NORECV;
15182 			ire = ire->ire_next;
15183 			continue;
15184 		}
15185 		if (phyi_flags & match_flags) {
15186 			ire->ire_marks |= IRE_MARK_NORECV;
15187 			ire = ire->ire_next;
15188 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15189 			    PHYI_INACTIVE) {
15190 				fallback = B_TRUE;
15191 			}
15192 			continue;
15193 		}
15194 		if (first) {
15195 			/*
15196 			 * We will move this to the front of the list later
15197 			 * on.
15198 			 */
15199 			clear_ire = ire;
15200 			ire->ire_marks &= ~IRE_MARK_NORECV;
15201 		} else {
15202 			ire->ire_marks |= IRE_MARK_NORECV;
15203 		}
15204 		first = B_FALSE;
15205 		ire = ire->ire_next;
15206 	}
15207 	/*
15208 	 * If we never nominated anybody, try nominating at least
15209 	 * an INACTIVE, if we found one. Do it only once though.
15210 	 */
15211 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15212 	    fallback) {
15213 		match_flags = PHYI_FAILED;
15214 		ire = start_ire;
15215 		irep = NULL;
15216 		goto redo;
15217 	}
15218 	ire_refrele(save_ire);
15219 
15220 	/*
15221 	 * irep non-NULL indicates that we entered the while loop
15222 	 * above. If clear_ire is at the insertion point, we don't
15223 	 * have to do anything. clear_ire will be NULL if all the
15224 	 * interfaces are failed.
15225 	 *
15226 	 * We cannot unlink and reinsert the ire at the right place
15227 	 * in the list since there can be other walkers of this bucket.
15228 	 * Instead we delete and recreate the ire
15229 	 */
15230 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15231 		ire_t *clear_ire_stq = NULL;
15232 
15233 		bzero(new_lb_ire, sizeof (ire_t));
15234 		/* XXX We need a recovery strategy here. */
15235 		if (ire_init(new_lb_ire,
15236 		    (uchar_t *)&clear_ire->ire_addr,
15237 		    (uchar_t *)&clear_ire->ire_mask,
15238 		    (uchar_t *)&clear_ire->ire_src_addr,
15239 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15240 		    &clear_ire->ire_max_frag,
15241 		    NULL, /* let ire_nce_init derive the resolver info */
15242 		    clear_ire->ire_rfq,
15243 		    clear_ire->ire_stq,
15244 		    clear_ire->ire_type,
15245 		    clear_ire->ire_ipif,
15246 		    clear_ire->ire_cmask,
15247 		    clear_ire->ire_phandle,
15248 		    clear_ire->ire_ihandle,
15249 		    clear_ire->ire_flags,
15250 		    &clear_ire->ire_uinfo,
15251 		    NULL,
15252 		    NULL,
15253 		    ipst) == NULL)
15254 			cmn_err(CE_PANIC, "ire_init() failed");
15255 		if (clear_ire->ire_stq == NULL) {
15256 			ire_t *ire_next = clear_ire->ire_next;
15257 			if (ire_next != NULL &&
15258 			    ire_next->ire_stq != NULL &&
15259 			    ire_next->ire_addr == clear_ire->ire_addr &&
15260 			    ire_next->ire_ipif->ipif_ill ==
15261 			    clear_ire->ire_ipif->ipif_ill) {
15262 				clear_ire_stq = ire_next;
15263 
15264 				bzero(new_nlb_ire, sizeof (ire_t));
15265 				/* XXX We need a recovery strategy here. */
15266 				if (ire_init(new_nlb_ire,
15267 				    (uchar_t *)&clear_ire_stq->ire_addr,
15268 				    (uchar_t *)&clear_ire_stq->ire_mask,
15269 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15270 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15271 				    &clear_ire_stq->ire_max_frag,
15272 				    NULL,
15273 				    clear_ire_stq->ire_rfq,
15274 				    clear_ire_stq->ire_stq,
15275 				    clear_ire_stq->ire_type,
15276 				    clear_ire_stq->ire_ipif,
15277 				    clear_ire_stq->ire_cmask,
15278 				    clear_ire_stq->ire_phandle,
15279 				    clear_ire_stq->ire_ihandle,
15280 				    clear_ire_stq->ire_flags,
15281 				    &clear_ire_stq->ire_uinfo,
15282 				    NULL,
15283 				    NULL,
15284 				    ipst) == NULL)
15285 					cmn_err(CE_PANIC, "ire_init() failed");
15286 			}
15287 		}
15288 
15289 		/*
15290 		 * Delete the ire. We can't call ire_delete() since
15291 		 * we are holding the bucket lock. We can't release the
15292 		 * bucket lock since we can't allow irep to change. So just
15293 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15294 		 * ire from the list and do the refrele.
15295 		 */
15296 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15297 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15298 
15299 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15300 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15301 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15302 		}
15303 
15304 		/*
15305 		 * Also take care of otherfields like ib/ob pkt count
15306 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15307 		 */
15308 
15309 		/* Set the max_frag before adding the ire */
15310 		max_frag = *new_lb_ire->ire_max_fragp;
15311 		new_lb_ire->ire_max_fragp = NULL;
15312 		new_lb_ire->ire_max_frag = max_frag;
15313 
15314 		/* Add the new ire's. Insert at *irep */
15315 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15316 		ire1 = *irep;
15317 		if (ire1 != NULL)
15318 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15319 		new_lb_ire->ire_next = ire1;
15320 		/* Link the new one in. */
15321 		new_lb_ire->ire_ptpn = irep;
15322 		membar_producer();
15323 		*irep = new_lb_ire;
15324 		new_lb_ire_used = B_TRUE;
15325 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15326 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15327 		DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *), new_lb_ire->ire_ipif,
15328 		    (char *), "ire", (void *), new_lb_ire);
15329 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15330 
15331 		if (clear_ire_stq != NULL) {
15332 			/* Set the max_frag before adding the ire */
15333 			max_frag = *new_nlb_ire->ire_max_fragp;
15334 			new_nlb_ire->ire_max_fragp = NULL;
15335 			new_nlb_ire->ire_max_frag = max_frag;
15336 
15337 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15338 			irep = &new_lb_ire->ire_next;
15339 			/* Add the new ire. Insert at *irep */
15340 			ire1 = *irep;
15341 			if (ire1 != NULL)
15342 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15343 			new_nlb_ire->ire_next = ire1;
15344 			/* Link the new one in. */
15345 			new_nlb_ire->ire_ptpn = irep;
15346 			membar_producer();
15347 			*irep = new_nlb_ire;
15348 			new_nlb_ire_used = B_TRUE;
15349 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15350 			    ire_stats_inserted);
15351 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15352 			DTRACE_PROBE3(ipif__incr__cnt,
15353 			    (ipif_t *), new_nlb_ire->ire_ipif,
15354 			    (char *), "ire", (void *), new_nlb_ire);
15355 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15356 			DTRACE_PROBE3(ill__incr__cnt,
15357 			    (ill_t *), new_nlb_ire->ire_stq->q_ptr,
15358 			    (char *), "ire", (void *), new_nlb_ire);
15359 			((ill_t *)(new_nlb_ire->ire_stq->q_ptr))->ill_ire_cnt++;
15360 		}
15361 	}
15362 	rw_exit(&irb->irb_lock);
15363 	if (!new_lb_ire_used)
15364 		kmem_cache_free(ire_cache, new_lb_ire);
15365 	if (!new_nlb_ire_used)
15366 		kmem_cache_free(ire_cache, new_nlb_ire);
15367 	IRB_REFRELE(irb);
15368 }
15369 
15370 /*
15371  * Whenever an ipif goes down we have to renominate a different
15372  * broadcast ire to receive. Whenever an ipif comes up, we need
15373  * to make sure that we have only one nominated to receive.
15374  */
15375 static void
15376 ipif_renominate_bcast(ipif_t *ipif)
15377 {
15378 	ill_t *ill = ipif->ipif_ill;
15379 	ipaddr_t subnet_addr;
15380 	ipaddr_t net_addr;
15381 	ipaddr_t net_mask = 0;
15382 	ipaddr_t subnet_netmask;
15383 	ipaddr_t addr;
15384 	ill_group_t *illgrp;
15385 	ip_stack_t	*ipst = ill->ill_ipst;
15386 
15387 	illgrp = ill->ill_group;
15388 	/*
15389 	 * If this is the last ipif going down, it might take
15390 	 * the ill out of the group. In that case ipif_down ->
15391 	 * illgrp_delete takes care of doing the nomination.
15392 	 * ipif_down does not call for this case.
15393 	 */
15394 	ASSERT(illgrp != NULL);
15395 
15396 	/* There could not have been any ires associated with this */
15397 	if (ipif->ipif_subnet == 0)
15398 		return;
15399 
15400 	ill_mark_bcast(illgrp, 0, ipst);
15401 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15402 
15403 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15404 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15405 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15406 	} else {
15407 		net_mask = htonl(IN_CLASSA_NET);
15408 	}
15409 	addr = net_mask & ipif->ipif_subnet;
15410 	ill_mark_bcast(illgrp, addr, ipst);
15411 
15412 	net_addr = ~net_mask | addr;
15413 	ill_mark_bcast(illgrp, net_addr, ipst);
15414 
15415 	subnet_netmask = ipif->ipif_net_mask;
15416 	addr = ipif->ipif_subnet;
15417 	ill_mark_bcast(illgrp, addr, ipst);
15418 
15419 	subnet_addr = ~subnet_netmask | addr;
15420 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15421 }
15422 
15423 /*
15424  * Whenever we form or delete ill groups, we need to nominate one set of
15425  * BROADCAST ires for receiving in the group.
15426  *
15427  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15428  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15429  *    for ill_ipif_up_count to be non-zero. This is the only case where
15430  *    ill_ipif_up_count is zero and we would still find the ires.
15431  *
15432  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15433  *    ipif is UP and we just have to do the nomination.
15434  *
15435  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15436  *    from the group. So, we have to do the nomination.
15437  *
15438  * Because of (3), there could be just one ill in the group. But we have
15439  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15440  * Thus, this function does not optimize when there is only one ill as
15441  * it is not correct for (3).
15442  */
15443 static void
15444 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15445 {
15446 	ill_t *ill;
15447 	ipif_t *ipif;
15448 	ipaddr_t subnet_addr;
15449 	ipaddr_t prev_subnet_addr = 0;
15450 	ipaddr_t net_addr;
15451 	ipaddr_t prev_net_addr = 0;
15452 	ipaddr_t net_mask = 0;
15453 	ipaddr_t subnet_netmask;
15454 	ipaddr_t addr;
15455 	ip_stack_t	*ipst;
15456 
15457 	/*
15458 	 * When the last memeber is leaving, there is nothing to
15459 	 * nominate.
15460 	 */
15461 	if (illgrp->illgrp_ill_count == 0) {
15462 		ASSERT(illgrp->illgrp_ill == NULL);
15463 		return;
15464 	}
15465 
15466 	ill = illgrp->illgrp_ill;
15467 	ASSERT(!ill->ill_isv6);
15468 	ipst = ill->ill_ipst;
15469 	/*
15470 	 * We assume that ires with same address and belonging to the
15471 	 * same group, has been grouped together. Nominating a *single*
15472 	 * ill in the group for sending and receiving broadcast is done
15473 	 * by making sure that the first BROADCAST ire (which will be
15474 	 * the one returned by ire_ctable_lookup for ip_rput and the
15475 	 * one that will be used in ip_wput_ire) will be the one that
15476 	 * will not have IRE_MARK_NORECV set.
15477 	 *
15478 	 * 1) ip_rput checks and discards packets received on ires marked
15479 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15480 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15481 	 *    first ire in the group for every broadcast address in the group.
15482 	 *    ip_rput will accept packets only on the first ire i.e only
15483 	 *    one copy of the ill.
15484 	 *
15485 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15486 	 *    packet for the whole group. It needs to send out on the ill
15487 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15488 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15489 	 *    the copy echoed back on other port where the ire is not marked
15490 	 *    with IRE_MARK_NORECV.
15491 	 *
15492 	 * Note that we just need to have the first IRE either loopback or
15493 	 * non-loopback (either of them may not exist if ire_create failed
15494 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15495 	 * always hit the first one and hence will always accept one copy.
15496 	 *
15497 	 * We have a broadcast ire per ill for all the unique prefixes
15498 	 * hosted on that ill. As we don't have a way of knowing the
15499 	 * unique prefixes on a given ill and hence in the whole group,
15500 	 * we just call ill_mark_bcast on all the prefixes that exist
15501 	 * in the group. For the common case of one prefix, the code
15502 	 * below optimizes by remebering the last address used for
15503 	 * markng. In the case of multiple prefixes, this will still
15504 	 * optimize depending the order of prefixes.
15505 	 *
15506 	 * The only unique address across the whole group is 0.0.0.0 and
15507 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15508 	 * the first ire in the bucket for receiving and disables the
15509 	 * others.
15510 	 */
15511 	ill_mark_bcast(illgrp, 0, ipst);
15512 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15513 	for (; ill != NULL; ill = ill->ill_group_next) {
15514 
15515 		for (ipif = ill->ill_ipif; ipif != NULL;
15516 		    ipif = ipif->ipif_next) {
15517 
15518 			if (!(ipif->ipif_flags & IPIF_UP) ||
15519 			    ipif->ipif_subnet == 0) {
15520 				continue;
15521 			}
15522 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15523 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15524 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15525 			} else {
15526 				net_mask = htonl(IN_CLASSA_NET);
15527 			}
15528 			addr = net_mask & ipif->ipif_subnet;
15529 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15530 				ill_mark_bcast(illgrp, addr, ipst);
15531 				net_addr = ~net_mask | addr;
15532 				ill_mark_bcast(illgrp, net_addr, ipst);
15533 			}
15534 			prev_net_addr = addr;
15535 
15536 			subnet_netmask = ipif->ipif_net_mask;
15537 			addr = ipif->ipif_subnet;
15538 			if (prev_subnet_addr == 0 ||
15539 			    prev_subnet_addr != addr) {
15540 				ill_mark_bcast(illgrp, addr, ipst);
15541 				subnet_addr = ~subnet_netmask | addr;
15542 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15543 			}
15544 			prev_subnet_addr = addr;
15545 		}
15546 	}
15547 }
15548 
15549 /*
15550  * This function is called while forming ill groups.
15551  *
15552  * Currently, we handle only allmulti groups. We want to join
15553  * allmulti on only one of the ills in the groups. In future,
15554  * when we have link aggregation, we may have to join normal
15555  * multicast groups on multiple ills as switch does inbound load
15556  * balancing. Following are the functions that calls this
15557  * function :
15558  *
15559  * 1) ill_recover_multicast : Interface is coming back UP.
15560  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15561  *    will call ill_recover_multicast to recover all the multicast
15562  *    groups. We need to make sure that only one member is joined
15563  *    in the ill group.
15564  *
15565  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15566  *    Somebody is joining allmulti. We need to make sure that only one
15567  *    member is joined in the group.
15568  *
15569  * 3) illgrp_insert : If allmulti has already joined, we need to make
15570  *    sure that only one member is joined in the group.
15571  *
15572  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15573  *    allmulti who we have nominated. We need to pick someother ill.
15574  *
15575  * 5) illgrp_delete : The ill we nominated is leaving the group,
15576  *    we need to pick a new ill to join the group.
15577  *
15578  * For (1), (2), (5) - we just have to check whether there is
15579  * a good ill joined in the group. If we could not find any ills
15580  * joined the group, we should join.
15581  *
15582  * For (4), the one that was nominated to receive, left the group.
15583  * There could be nobody joined in the group when this function is
15584  * called.
15585  *
15586  * For (3) - we need to explicitly check whether there are multiple
15587  * ills joined in the group.
15588  *
15589  * For simplicity, we don't differentiate any of the above cases. We
15590  * just leave the group if it is joined on any of them and join on
15591  * the first good ill.
15592  */
15593 int
15594 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15595 {
15596 	ilm_t *ilm;
15597 	ill_t *ill;
15598 	ill_t *fallback_inactive_ill = NULL;
15599 	ill_t *fallback_failed_ill = NULL;
15600 	int ret = 0;
15601 
15602 	/*
15603 	 * Leave the allmulti on all the ills and start fresh.
15604 	 */
15605 	for (ill = illgrp->illgrp_ill; ill != NULL;
15606 	    ill = ill->ill_group_next) {
15607 		if (ill->ill_join_allmulti)
15608 			(void) ip_leave_allmulti(ill->ill_ipif);
15609 	}
15610 
15611 	/*
15612 	 * Choose a good ill. Fallback to inactive or failed if
15613 	 * none available. We need to fallback to FAILED in the
15614 	 * case where we have 2 interfaces in a group - where
15615 	 * one of them is failed and another is a good one and
15616 	 * the good one (not marked inactive) is leaving the group.
15617 	 */
15618 	ret = 0;
15619 	for (ill = illgrp->illgrp_ill; ill != NULL;
15620 	    ill = ill->ill_group_next) {
15621 		/* Never pick an offline interface */
15622 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15623 			continue;
15624 
15625 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15626 			fallback_failed_ill = ill;
15627 			continue;
15628 		}
15629 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15630 			fallback_inactive_ill = ill;
15631 			continue;
15632 		}
15633 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15634 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15635 				ret = ip_join_allmulti(ill->ill_ipif);
15636 				/*
15637 				 * ip_join_allmulti can fail because of memory
15638 				 * failures. So, make sure we join at least
15639 				 * on one ill.
15640 				 */
15641 				if (ill->ill_join_allmulti)
15642 					return (0);
15643 			}
15644 		}
15645 	}
15646 	if (ret != 0) {
15647 		/*
15648 		 * If we tried nominating above and failed to do so,
15649 		 * return error. We might have tried multiple times.
15650 		 * But, return the latest error.
15651 		 */
15652 		return (ret);
15653 	}
15654 	if ((ill = fallback_inactive_ill) != NULL) {
15655 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15656 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15657 				ret = ip_join_allmulti(ill->ill_ipif);
15658 				return (ret);
15659 			}
15660 		}
15661 	} else if ((ill = fallback_failed_ill) != NULL) {
15662 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15663 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15664 				ret = ip_join_allmulti(ill->ill_ipif);
15665 				return (ret);
15666 			}
15667 		}
15668 	}
15669 	return (0);
15670 }
15671 
15672 /*
15673  * This function is called from illgrp_delete after it is
15674  * deleted from the group to reschedule responsibilities
15675  * to a different ill.
15676  */
15677 static void
15678 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15679 {
15680 	ilm_t	*ilm;
15681 	ipif_t	*ipif;
15682 	ipaddr_t subnet_addr;
15683 	ipaddr_t net_addr;
15684 	ipaddr_t net_mask = 0;
15685 	ipaddr_t subnet_netmask;
15686 	ipaddr_t addr;
15687 	ip_stack_t *ipst = ill->ill_ipst;
15688 
15689 	ASSERT(ill->ill_group == NULL);
15690 	/*
15691 	 * Broadcast Responsibility:
15692 	 *
15693 	 * 1. If this ill has been nominated for receiving broadcast
15694 	 * packets, we need to find a new one. Before we find a new
15695 	 * one, we need to re-group the ires that are part of this new
15696 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15697 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15698 	 * thing for us.
15699 	 *
15700 	 * 2. If this ill was not nominated for receiving broadcast
15701 	 * packets, we need to clear the IRE_MARK_NORECV flag
15702 	 * so that we continue to send up broadcast packets.
15703 	 */
15704 	if (!ill->ill_isv6) {
15705 		/*
15706 		 * Case 1 above : No optimization here. Just redo the
15707 		 * nomination.
15708 		 */
15709 		ill_group_bcast_for_xmit(ill);
15710 		ill_nominate_bcast_rcv(illgrp);
15711 
15712 		/*
15713 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15714 		 */
15715 		ill_clear_bcast_mark(ill, 0);
15716 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15717 
15718 		for (ipif = ill->ill_ipif; ipif != NULL;
15719 		    ipif = ipif->ipif_next) {
15720 
15721 			if (!(ipif->ipif_flags & IPIF_UP) ||
15722 			    ipif->ipif_subnet == 0) {
15723 				continue;
15724 			}
15725 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15726 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15727 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15728 			} else {
15729 				net_mask = htonl(IN_CLASSA_NET);
15730 			}
15731 			addr = net_mask & ipif->ipif_subnet;
15732 			ill_clear_bcast_mark(ill, addr);
15733 
15734 			net_addr = ~net_mask | addr;
15735 			ill_clear_bcast_mark(ill, net_addr);
15736 
15737 			subnet_netmask = ipif->ipif_net_mask;
15738 			addr = ipif->ipif_subnet;
15739 			ill_clear_bcast_mark(ill, addr);
15740 
15741 			subnet_addr = ~subnet_netmask | addr;
15742 			ill_clear_bcast_mark(ill, subnet_addr);
15743 		}
15744 	}
15745 
15746 	/*
15747 	 * Multicast Responsibility.
15748 	 *
15749 	 * If we have joined allmulti on this one, find a new member
15750 	 * in the group to join allmulti. As this ill is already part
15751 	 * of allmulti, we don't have to join on this one.
15752 	 *
15753 	 * If we have not joined allmulti on this one, there is no
15754 	 * responsibility to handoff. But we need to take new
15755 	 * responsibility i.e, join allmulti on this one if we need
15756 	 * to.
15757 	 */
15758 	if (ill->ill_join_allmulti) {
15759 		(void) ill_nominate_mcast_rcv(illgrp);
15760 	} else {
15761 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15762 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15763 				(void) ip_join_allmulti(ill->ill_ipif);
15764 				break;
15765 			}
15766 		}
15767 	}
15768 
15769 	/*
15770 	 * We intentionally do the flushing of IRE_CACHES only matching
15771 	 * on the ill and not on groups. Note that we are already deleted
15772 	 * from the group.
15773 	 *
15774 	 * This will make sure that all IRE_CACHES whose stq is pointing
15775 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15776 	 * deleted and IRE_CACHES that are not pointing at this ill will
15777 	 * be left alone.
15778 	 */
15779 	ire_walk_ill(MATCH_IRE_ILL | MATCH_IRE_TYPE, IRE_CACHE,
15780 	    illgrp_cache_delete, ill, ill);
15781 
15782 	/*
15783 	 * Some conn may have cached one of the IREs deleted above. By removing
15784 	 * the ire reference, we clean up the extra reference to the ill held in
15785 	 * ire->ire_stq.
15786 	 */
15787 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
15788 
15789 	/*
15790 	 * Re-do source address selection for all the members in the
15791 	 * group, if they borrowed source address from one of the ipifs
15792 	 * in this ill.
15793 	 */
15794 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15795 		if (ill->ill_isv6) {
15796 			ipif_update_other_ipifs_v6(ipif, illgrp);
15797 		} else {
15798 			ipif_update_other_ipifs(ipif, illgrp);
15799 		}
15800 	}
15801 }
15802 
15803 /*
15804  * Delete the ill from the group. The caller makes sure that it is
15805  * in a group and it okay to delete from the group. So, we always
15806  * delete here.
15807  */
15808 static void
15809 illgrp_delete(ill_t *ill)
15810 {
15811 	ill_group_t *illgrp;
15812 	ill_group_t *tmpg;
15813 	ill_t *tmp_ill;
15814 	ip_stack_t	*ipst = ill->ill_ipst;
15815 
15816 	/*
15817 	 * Reset illgrp_ill_schednext if it was pointing at us.
15818 	 * We need to do this before we set ill_group to NULL.
15819 	 */
15820 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15821 	mutex_enter(&ill->ill_lock);
15822 
15823 	illgrp_reset_schednext(ill);
15824 
15825 	illgrp = ill->ill_group;
15826 
15827 	/* Delete the ill from illgrp. */
15828 	if (illgrp->illgrp_ill == ill) {
15829 		illgrp->illgrp_ill = ill->ill_group_next;
15830 	} else {
15831 		tmp_ill = illgrp->illgrp_ill;
15832 		while (tmp_ill->ill_group_next != ill) {
15833 			tmp_ill = tmp_ill->ill_group_next;
15834 			ASSERT(tmp_ill != NULL);
15835 		}
15836 		tmp_ill->ill_group_next = ill->ill_group_next;
15837 	}
15838 	ill->ill_group = NULL;
15839 	ill->ill_group_next = NULL;
15840 
15841 	illgrp->illgrp_ill_count--;
15842 	mutex_exit(&ill->ill_lock);
15843 	rw_exit(&ipst->ips_ill_g_lock);
15844 
15845 	/*
15846 	 * As this ill is leaving the group, we need to hand off
15847 	 * the responsibilities to the other ills in the group, if
15848 	 * this ill had some responsibilities.
15849 	 */
15850 
15851 	ill_handoff_responsibility(ill, illgrp);
15852 
15853 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15854 
15855 	if (illgrp->illgrp_ill_count == 0) {
15856 
15857 		ASSERT(illgrp->illgrp_ill == NULL);
15858 		if (ill->ill_isv6) {
15859 			if (illgrp == ipst->ips_illgrp_head_v6) {
15860 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
15861 			} else {
15862 				tmpg = ipst->ips_illgrp_head_v6;
15863 				while (tmpg->illgrp_next != illgrp) {
15864 					tmpg = tmpg->illgrp_next;
15865 					ASSERT(tmpg != NULL);
15866 				}
15867 				tmpg->illgrp_next = illgrp->illgrp_next;
15868 			}
15869 		} else {
15870 			if (illgrp == ipst->ips_illgrp_head_v4) {
15871 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
15872 			} else {
15873 				tmpg = ipst->ips_illgrp_head_v4;
15874 				while (tmpg->illgrp_next != illgrp) {
15875 					tmpg = tmpg->illgrp_next;
15876 					ASSERT(tmpg != NULL);
15877 				}
15878 				tmpg->illgrp_next = illgrp->illgrp_next;
15879 			}
15880 		}
15881 		mutex_destroy(&illgrp->illgrp_lock);
15882 		mi_free(illgrp);
15883 	}
15884 	rw_exit(&ipst->ips_ill_g_lock);
15885 
15886 	/*
15887 	 * Even though the ill is out of the group its not necessary
15888 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15889 	 * We will split the ipsq when phyint_groupname is set to NULL.
15890 	 */
15891 
15892 	/*
15893 	 * Send a routing sockets message if we are deleting from
15894 	 * groups with names.
15895 	 */
15896 	if (ill->ill_phyint->phyint_groupname_len != 0)
15897 		ip_rts_ifmsg(ill->ill_ipif);
15898 }
15899 
15900 /*
15901  * Re-do source address selection. This is normally called when
15902  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15903  * ipif comes up.
15904  */
15905 void
15906 ill_update_source_selection(ill_t *ill)
15907 {
15908 	ipif_t *ipif;
15909 
15910 	ASSERT(IAM_WRITER_ILL(ill));
15911 
15912 	if (ill->ill_group != NULL)
15913 		ill = ill->ill_group->illgrp_ill;
15914 
15915 	for (; ill != NULL; ill = ill->ill_group_next) {
15916 		for (ipif = ill->ill_ipif; ipif != NULL;
15917 		    ipif = ipif->ipif_next) {
15918 			if (ill->ill_isv6)
15919 				ipif_recreate_interface_routes_v6(NULL, ipif);
15920 			else
15921 				ipif_recreate_interface_routes(NULL, ipif);
15922 		}
15923 	}
15924 }
15925 
15926 /*
15927  * Insert ill in a group headed by illgrp_head. The caller can either
15928  * pass a groupname in which case we search for a group with the
15929  * same name to insert in or pass a group to insert in. This function
15930  * would only search groups with names.
15931  *
15932  * NOTE : The caller should make sure that there is at least one ipif
15933  *	  UP on this ill so that illgrp_scheduler can pick this ill
15934  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15935  *	  already sent a DL_UNBIND to the driver and we don't want to
15936  *	  send anymore packets. We don't assert for ipif_up_count
15937  *	  to be greater than zero, because ipif_up_done wants to call
15938  *	  this function before bumping up the ipif_up_count. See
15939  *	  ipif_up_done() for details.
15940  */
15941 int
15942 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
15943     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
15944 {
15945 	ill_group_t *illgrp;
15946 	ill_t *prev_ill;
15947 	phyint_t *phyi;
15948 	ip_stack_t	*ipst = ill->ill_ipst;
15949 
15950 	ASSERT(ill->ill_group == NULL);
15951 
15952 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15953 	mutex_enter(&ill->ill_lock);
15954 
15955 	if (groupname != NULL) {
15956 		/*
15957 		 * Look for a group with a matching groupname to insert.
15958 		 */
15959 		for (illgrp = *illgrp_head; illgrp != NULL;
15960 		    illgrp = illgrp->illgrp_next) {
15961 
15962 			ill_t *tmp_ill;
15963 
15964 			/*
15965 			 * If we have an ill_group_t in the list which has
15966 			 * no ill_t assigned then we must be in the process of
15967 			 * removing this group. We skip this as illgrp_delete()
15968 			 * will remove it from the list.
15969 			 */
15970 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
15971 				ASSERT(illgrp->illgrp_ill_count == 0);
15972 				continue;
15973 			}
15974 
15975 			ASSERT(tmp_ill->ill_phyint != NULL);
15976 			phyi = tmp_ill->ill_phyint;
15977 			/*
15978 			 * Look at groups which has names only.
15979 			 */
15980 			if (phyi->phyint_groupname_len == 0)
15981 				continue;
15982 			/*
15983 			 * Names are stored in the phyint common to both
15984 			 * IPv4 and IPv6.
15985 			 */
15986 			if (mi_strcmp(phyi->phyint_groupname,
15987 			    groupname) == 0) {
15988 				break;
15989 			}
15990 		}
15991 	} else {
15992 		/*
15993 		 * If the caller passes in a NULL "grp_to_insert", we
15994 		 * allocate one below and insert this singleton.
15995 		 */
15996 		illgrp = grp_to_insert;
15997 	}
15998 
15999 	ill->ill_group_next = NULL;
16000 
16001 	if (illgrp == NULL) {
16002 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16003 		if (illgrp == NULL) {
16004 			return (ENOMEM);
16005 		}
16006 		illgrp->illgrp_next = *illgrp_head;
16007 		*illgrp_head = illgrp;
16008 		illgrp->illgrp_ill = ill;
16009 		illgrp->illgrp_ill_count = 1;
16010 		ill->ill_group = illgrp;
16011 		/*
16012 		 * Used in illgrp_scheduler to protect multiple threads
16013 		 * from traversing the list.
16014 		 */
16015 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16016 	} else {
16017 		ASSERT(ill->ill_net_type ==
16018 		    illgrp->illgrp_ill->ill_net_type);
16019 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16020 
16021 		/* Insert ill at tail of this group */
16022 		prev_ill = illgrp->illgrp_ill;
16023 		while (prev_ill->ill_group_next != NULL)
16024 			prev_ill = prev_ill->ill_group_next;
16025 		prev_ill->ill_group_next = ill;
16026 		ill->ill_group = illgrp;
16027 		illgrp->illgrp_ill_count++;
16028 		/*
16029 		 * Inherit group properties. Currently only forwarding
16030 		 * is the property we try to keep the same with all the
16031 		 * ills. When there are more, we will abstract this into
16032 		 * a function.
16033 		 */
16034 		ill->ill_flags &= ~ILLF_ROUTER;
16035 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16036 	}
16037 	mutex_exit(&ill->ill_lock);
16038 	rw_exit(&ipst->ips_ill_g_lock);
16039 
16040 	/*
16041 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16042 	 *    may be zero as it has not yet been bumped. But the ires
16043 	 *    have already been added. So, we do the nomination here
16044 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16045 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16046 	 *    ill_ipif_up_count here while nominating broadcast ires for
16047 	 *    receive.
16048 	 *
16049 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16050 	 *    to group them properly as ire_add() has already happened
16051 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16052 	 *    case, we need to do it here anyway.
16053 	 */
16054 	if (!ill->ill_isv6) {
16055 		ill_group_bcast_for_xmit(ill);
16056 		ill_nominate_bcast_rcv(illgrp);
16057 	}
16058 
16059 	if (!ipif_is_coming_up) {
16060 		/*
16061 		 * When ipif_up_done() calls this function, the multicast
16062 		 * groups have not been joined yet. So, there is no point in
16063 		 * nomination. ip_join_allmulti will handle groups when
16064 		 * ill_recover_multicast is called from ipif_up_done() later.
16065 		 */
16066 		(void) ill_nominate_mcast_rcv(illgrp);
16067 		/*
16068 		 * ipif_up_done calls ill_update_source_selection
16069 		 * anyway. Moreover, we don't want to re-create
16070 		 * interface routes while ipif_up_done() still has reference
16071 		 * to them. Refer to ipif_up_done() for more details.
16072 		 */
16073 		ill_update_source_selection(ill);
16074 	}
16075 
16076 	/*
16077 	 * Send a routing sockets message if we are inserting into
16078 	 * groups with names.
16079 	 */
16080 	if (groupname != NULL)
16081 		ip_rts_ifmsg(ill->ill_ipif);
16082 	return (0);
16083 }
16084 
16085 /*
16086  * Return the first phyint matching the groupname. There could
16087  * be more than one when there are ill groups.
16088  *
16089  * If 'usable' is set, then we exclude ones that are marked with any of
16090  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16091  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16092  * emulation of ipmp.
16093  */
16094 phyint_t *
16095 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16096 {
16097 	phyint_t *phyi;
16098 
16099 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16100 	/*
16101 	 * Group names are stored in the phyint - a common structure
16102 	 * to both IPv4 and IPv6.
16103 	 */
16104 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16105 	for (; phyi != NULL;
16106 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16107 	    phyi, AVL_AFTER)) {
16108 		if (phyi->phyint_groupname_len == 0)
16109 			continue;
16110 		/*
16111 		 * Skip the ones that should not be used since the callers
16112 		 * sometime use this for sending packets.
16113 		 */
16114 		if (usable && (phyi->phyint_flags &
16115 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16116 			continue;
16117 
16118 		ASSERT(phyi->phyint_groupname != NULL);
16119 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16120 			return (phyi);
16121 	}
16122 	return (NULL);
16123 }
16124 
16125 
16126 /*
16127  * Return the first usable phyint matching the group index. By 'usable'
16128  * we exclude ones that are marked ununsable with any of
16129  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16130  *
16131  * Used only for the ipmp/netinfo emulation of ipmp.
16132  */
16133 phyint_t *
16134 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16135 {
16136 	phyint_t *phyi;
16137 
16138 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16139 
16140 	if (!ipst->ips_ipmp_hook_emulation)
16141 		return (NULL);
16142 
16143 	/*
16144 	 * Group indicies are stored in the phyint - a common structure
16145 	 * to both IPv4 and IPv6.
16146 	 */
16147 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16148 	for (; phyi != NULL;
16149 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16150 	    phyi, AVL_AFTER)) {
16151 		/* Ignore the ones that do not have a group */
16152 		if (phyi->phyint_groupname_len == 0)
16153 			continue;
16154 
16155 		ASSERT(phyi->phyint_group_ifindex != 0);
16156 		/*
16157 		 * Skip the ones that should not be used since the callers
16158 		 * sometime use this for sending packets.
16159 		 */
16160 		if (phyi->phyint_flags &
16161 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16162 			continue;
16163 		if (phyi->phyint_group_ifindex == group_ifindex)
16164 			return (phyi);
16165 	}
16166 	return (NULL);
16167 }
16168 
16169 /*
16170  * MT notes on creation and deletion of IPMP groups
16171  *
16172  * Creation and deletion of IPMP groups introduce the need to merge or
16173  * split the associated serialization objects i.e the ipsq's. Normally all
16174  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16175  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16176  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16177  * is a need to change the <ill-ipsq> association and we have to operate on both
16178  * the source and destination IPMP groups. For eg. attempting to set the
16179  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16180  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16181  * source or destination IPMP group are mapped to a single ipsq for executing
16182  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16183  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16184  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16185  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16186  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16187  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16188  *
16189  * In the above example the ioctl handling code locates the current ipsq of hme0
16190  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16191  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16192  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16193  * the destination ipsq. If the destination ipsq is not busy, it also enters
16194  * the destination ipsq exclusively. Now the actual groupname setting operation
16195  * can proceed. If the destination ipsq is busy, the operation is enqueued
16196  * on the destination (merged) ipsq and will be handled in the unwind from
16197  * ipsq_exit.
16198  *
16199  * To prevent other threads accessing the ill while the group name change is
16200  * in progres, we bring down the ipifs which also removes the ill from the
16201  * group. The group is changed in phyint and when the first ipif on the ill
16202  * is brought up, the ill is inserted into the right IPMP group by
16203  * illgrp_insert.
16204  */
16205 /* ARGSUSED */
16206 int
16207 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16208     ip_ioctl_cmd_t *ipip, void *ifreq)
16209 {
16210 	int i;
16211 	char *tmp;
16212 	int namelen;
16213 	ill_t *ill = ipif->ipif_ill;
16214 	ill_t *ill_v4, *ill_v6;
16215 	int err = 0;
16216 	phyint_t *phyi;
16217 	phyint_t *phyi_tmp;
16218 	struct lifreq *lifr;
16219 	mblk_t	*mp1;
16220 	char *groupname;
16221 	ipsq_t *ipsq;
16222 	ip_stack_t	*ipst = ill->ill_ipst;
16223 
16224 	ASSERT(IAM_WRITER_IPIF(ipif));
16225 
16226 	/* Existance verified in ip_wput_nondata */
16227 	mp1 = mp->b_cont->b_cont;
16228 	lifr = (struct lifreq *)mp1->b_rptr;
16229 	groupname = lifr->lifr_groupname;
16230 
16231 	if (ipif->ipif_id != 0)
16232 		return (EINVAL);
16233 
16234 	phyi = ill->ill_phyint;
16235 	ASSERT(phyi != NULL);
16236 
16237 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16238 		return (EINVAL);
16239 
16240 	tmp = groupname;
16241 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16242 		;
16243 
16244 	if (i == LIFNAMSIZ) {
16245 		/* no null termination */
16246 		return (EINVAL);
16247 	}
16248 
16249 	/*
16250 	 * Calculate the namelen exclusive of the null
16251 	 * termination character.
16252 	 */
16253 	namelen = tmp - groupname;
16254 
16255 	ill_v4 = phyi->phyint_illv4;
16256 	ill_v6 = phyi->phyint_illv6;
16257 
16258 	/*
16259 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16260 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16261 	 * synchronization notes in ip.c
16262 	 */
16263 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16264 		return (EINVAL);
16265 	}
16266 
16267 	/*
16268 	 * mark the ill as changing.
16269 	 * this should queue all new requests on the syncq.
16270 	 */
16271 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16272 
16273 	if (ill_v4 != NULL)
16274 		ill_v4->ill_state_flags |= ILL_CHANGING;
16275 	if (ill_v6 != NULL)
16276 		ill_v6->ill_state_flags |= ILL_CHANGING;
16277 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16278 
16279 	if (namelen == 0) {
16280 		/*
16281 		 * Null string means remove this interface from the
16282 		 * existing group.
16283 		 */
16284 		if (phyi->phyint_groupname_len == 0) {
16285 			/*
16286 			 * Never was in a group.
16287 			 */
16288 			err = 0;
16289 			goto done;
16290 		}
16291 
16292 		/*
16293 		 * IPv4 or IPv6 may be temporarily out of the group when all
16294 		 * the ipifs are down. Thus, we need to check for ill_group to
16295 		 * be non-NULL.
16296 		 */
16297 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16298 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16299 			mutex_enter(&ill_v4->ill_lock);
16300 			if (!ill_is_quiescent(ill_v4)) {
16301 				/*
16302 				 * ipsq_pending_mp_add will not fail since
16303 				 * connp is NULL
16304 				 */
16305 				(void) ipsq_pending_mp_add(NULL,
16306 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16307 				mutex_exit(&ill_v4->ill_lock);
16308 				err = EINPROGRESS;
16309 				goto done;
16310 			}
16311 			mutex_exit(&ill_v4->ill_lock);
16312 		}
16313 
16314 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16315 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16316 			mutex_enter(&ill_v6->ill_lock);
16317 			if (!ill_is_quiescent(ill_v6)) {
16318 				(void) ipsq_pending_mp_add(NULL,
16319 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16320 				mutex_exit(&ill_v6->ill_lock);
16321 				err = EINPROGRESS;
16322 				goto done;
16323 			}
16324 			mutex_exit(&ill_v6->ill_lock);
16325 		}
16326 
16327 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16328 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16329 		mutex_enter(&phyi->phyint_lock);
16330 		ASSERT(phyi->phyint_groupname != NULL);
16331 		mi_free(phyi->phyint_groupname);
16332 		phyi->phyint_groupname = NULL;
16333 		phyi->phyint_groupname_len = 0;
16334 
16335 		/* Restore the ifindex used to be the per interface one */
16336 		phyi->phyint_group_ifindex = 0;
16337 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16338 		mutex_exit(&phyi->phyint_lock);
16339 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16340 		rw_exit(&ipst->ips_ill_g_lock);
16341 		err = ill_up_ipifs(ill, q, mp);
16342 
16343 		/*
16344 		 * set the split flag so that the ipsq can be split
16345 		 */
16346 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16347 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16348 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16349 
16350 	} else {
16351 		if (phyi->phyint_groupname_len != 0) {
16352 			ASSERT(phyi->phyint_groupname != NULL);
16353 			/* Are we inserting in the same group ? */
16354 			if (mi_strcmp(groupname,
16355 			    phyi->phyint_groupname) == 0) {
16356 				err = 0;
16357 				goto done;
16358 			}
16359 		}
16360 
16361 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16362 		/*
16363 		 * Merge ipsq for the group's.
16364 		 * This check is here as multiple groups/ills might be
16365 		 * sharing the same ipsq.
16366 		 * If we have to merege than the operation is restarted
16367 		 * on the new ipsq.
16368 		 */
16369 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16370 		if (phyi->phyint_ipsq != ipsq) {
16371 			rw_exit(&ipst->ips_ill_g_lock);
16372 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16373 			goto done;
16374 		}
16375 		/*
16376 		 * Running exclusive on new ipsq.
16377 		 */
16378 
16379 		ASSERT(ipsq != NULL);
16380 		ASSERT(ipsq->ipsq_writer == curthread);
16381 
16382 		/*
16383 		 * Check whether the ill_type and ill_net_type matches before
16384 		 * we allocate any memory so that the cleanup is easier.
16385 		 *
16386 		 * We can't group dissimilar ones as we can't load spread
16387 		 * packets across the group because of potential link-level
16388 		 * header differences.
16389 		 */
16390 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16391 		if (phyi_tmp != NULL) {
16392 			if ((ill_v4 != NULL &&
16393 			    phyi_tmp->phyint_illv4 != NULL) &&
16394 			    ((ill_v4->ill_net_type !=
16395 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16396 			    (ill_v4->ill_type !=
16397 			    phyi_tmp->phyint_illv4->ill_type))) {
16398 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16399 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16400 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16401 				rw_exit(&ipst->ips_ill_g_lock);
16402 				return (EINVAL);
16403 			}
16404 			if ((ill_v6 != NULL &&
16405 			    phyi_tmp->phyint_illv6 != NULL) &&
16406 			    ((ill_v6->ill_net_type !=
16407 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16408 			    (ill_v6->ill_type !=
16409 			    phyi_tmp->phyint_illv6->ill_type))) {
16410 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16411 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16412 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16413 				rw_exit(&ipst->ips_ill_g_lock);
16414 				return (EINVAL);
16415 			}
16416 		}
16417 
16418 		rw_exit(&ipst->ips_ill_g_lock);
16419 
16420 		/*
16421 		 * bring down all v4 ipifs.
16422 		 */
16423 		if (ill_v4 != NULL) {
16424 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16425 		}
16426 
16427 		/*
16428 		 * bring down all v6 ipifs.
16429 		 */
16430 		if (ill_v6 != NULL) {
16431 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16432 		}
16433 
16434 		/*
16435 		 * make sure all ipifs are down and there are no active
16436 		 * references. Call to ipsq_pending_mp_add will not fail
16437 		 * since connp is NULL.
16438 		 */
16439 		if (ill_v4 != NULL) {
16440 			mutex_enter(&ill_v4->ill_lock);
16441 			if (!ill_is_quiescent(ill_v4)) {
16442 				(void) ipsq_pending_mp_add(NULL,
16443 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16444 				mutex_exit(&ill_v4->ill_lock);
16445 				err = EINPROGRESS;
16446 				goto done;
16447 			}
16448 			mutex_exit(&ill_v4->ill_lock);
16449 		}
16450 
16451 		if (ill_v6 != NULL) {
16452 			mutex_enter(&ill_v6->ill_lock);
16453 			if (!ill_is_quiescent(ill_v6)) {
16454 				(void) ipsq_pending_mp_add(NULL,
16455 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16456 				mutex_exit(&ill_v6->ill_lock);
16457 				err = EINPROGRESS;
16458 				goto done;
16459 			}
16460 			mutex_exit(&ill_v6->ill_lock);
16461 		}
16462 
16463 		/*
16464 		 * allocate including space for null terminator
16465 		 * before we insert.
16466 		 */
16467 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16468 		if (tmp == NULL)
16469 			return (ENOMEM);
16470 
16471 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16472 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16473 		mutex_enter(&phyi->phyint_lock);
16474 		if (phyi->phyint_groupname_len != 0) {
16475 			ASSERT(phyi->phyint_groupname != NULL);
16476 			mi_free(phyi->phyint_groupname);
16477 		}
16478 
16479 		/*
16480 		 * setup the new group name.
16481 		 */
16482 		phyi->phyint_groupname = tmp;
16483 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16484 		phyi->phyint_groupname_len = namelen + 1;
16485 
16486 		if (ipst->ips_ipmp_hook_emulation) {
16487 			/*
16488 			 * If the group already exists we use the existing
16489 			 * group_ifindex, otherwise we pick a new index here.
16490 			 */
16491 			if (phyi_tmp != NULL) {
16492 				phyi->phyint_group_ifindex =
16493 				    phyi_tmp->phyint_group_ifindex;
16494 			} else {
16495 				/* XXX We need a recovery strategy here. */
16496 				if (!ip_assign_ifindex(
16497 				    &phyi->phyint_group_ifindex, ipst))
16498 					cmn_err(CE_PANIC,
16499 					    "ip_assign_ifindex() failed");
16500 			}
16501 		}
16502 		/*
16503 		 * Select whether the netinfo and hook use the per-interface
16504 		 * or per-group ifindex.
16505 		 */
16506 		if (ipst->ips_ipmp_hook_emulation)
16507 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16508 		else
16509 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16510 
16511 		if (ipst->ips_ipmp_hook_emulation &&
16512 		    phyi_tmp != NULL) {
16513 			/* First phyint in group - group PLUMB event */
16514 			ill_nic_info_plumb(ill, B_TRUE);
16515 		}
16516 		mutex_exit(&phyi->phyint_lock);
16517 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16518 		rw_exit(&ipst->ips_ill_g_lock);
16519 
16520 		err = ill_up_ipifs(ill, q, mp);
16521 	}
16522 
16523 done:
16524 	/*
16525 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16526 	 */
16527 	if (err != EINPROGRESS) {
16528 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16529 		if (ill_v4 != NULL)
16530 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16531 		if (ill_v6 != NULL)
16532 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16533 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16534 	}
16535 	return (err);
16536 }
16537 
16538 /* ARGSUSED */
16539 int
16540 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16541     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16542 {
16543 	ill_t *ill;
16544 	phyint_t *phyi;
16545 	struct lifreq *lifr;
16546 	mblk_t	*mp1;
16547 
16548 	/* Existence verified in ip_wput_nondata */
16549 	mp1 = mp->b_cont->b_cont;
16550 	lifr = (struct lifreq *)mp1->b_rptr;
16551 	ill = ipif->ipif_ill;
16552 	phyi = ill->ill_phyint;
16553 
16554 	lifr->lifr_groupname[0] = '\0';
16555 	/*
16556 	 * ill_group may be null if all the interfaces
16557 	 * are down. But still, the phyint should always
16558 	 * hold the name.
16559 	 */
16560 	if (phyi->phyint_groupname_len != 0) {
16561 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16562 		    phyi->phyint_groupname_len);
16563 	}
16564 
16565 	return (0);
16566 }
16567 
16568 
16569 typedef struct conn_move_s {
16570 	ill_t	*cm_from_ill;
16571 	ill_t	*cm_to_ill;
16572 	int	cm_ifindex;
16573 } conn_move_t;
16574 
16575 /*
16576  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16577  */
16578 static void
16579 conn_move(conn_t *connp, caddr_t arg)
16580 {
16581 	conn_move_t *connm;
16582 	int ifindex;
16583 	int i;
16584 	ill_t *from_ill;
16585 	ill_t *to_ill;
16586 	ilg_t *ilg;
16587 	ilm_t *ret_ilm;
16588 
16589 	connm = (conn_move_t *)arg;
16590 	ifindex = connm->cm_ifindex;
16591 	from_ill = connm->cm_from_ill;
16592 	to_ill = connm->cm_to_ill;
16593 
16594 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16595 
16596 	/* All multicast fields protected by conn_lock */
16597 	mutex_enter(&connp->conn_lock);
16598 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16599 	if ((connp->conn_outgoing_ill == from_ill) &&
16600 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16601 		connp->conn_outgoing_ill = to_ill;
16602 		connp->conn_incoming_ill = to_ill;
16603 	}
16604 
16605 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16606 
16607 	if ((connp->conn_multicast_ill == from_ill) &&
16608 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16609 		connp->conn_multicast_ill = connm->cm_to_ill;
16610 	}
16611 
16612 	/*
16613 	 * Change the ilg_ill to point to the new one. This assumes
16614 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16615 	 * has been told to receive packets on this interface.
16616 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16617 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16618 	 * some ilms may not have moved. We check to see whether
16619 	 * the ilms have moved to to_ill. We can't check on from_ill
16620 	 * as in the process of moving, we could have split an ilm
16621 	 * in to two - which has the same orig_ifindex and v6group.
16622 	 *
16623 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16624 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16625 	 */
16626 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16627 		ilg = &connp->conn_ilg[i];
16628 		if ((ilg->ilg_ill == from_ill) &&
16629 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16630 			/* ifindex != 0 indicates failback */
16631 			if (ifindex != 0) {
16632 				connp->conn_ilg[i].ilg_ill = to_ill;
16633 				continue;
16634 			}
16635 
16636 			mutex_enter(&to_ill->ill_lock);
16637 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16638 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16639 			    connp->conn_zoneid);
16640 			mutex_exit(&to_ill->ill_lock);
16641 
16642 			if (ret_ilm != NULL)
16643 				connp->conn_ilg[i].ilg_ill = to_ill;
16644 		}
16645 	}
16646 	mutex_exit(&connp->conn_lock);
16647 }
16648 
16649 static void
16650 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16651 {
16652 	conn_move_t connm;
16653 	ip_stack_t	*ipst = from_ill->ill_ipst;
16654 
16655 	connm.cm_from_ill = from_ill;
16656 	connm.cm_to_ill = to_ill;
16657 	connm.cm_ifindex = ifindex;
16658 
16659 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16660 }
16661 
16662 /*
16663  * ilm has been moved from from_ill to to_ill.
16664  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16665  * appropriately.
16666  *
16667  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16668  *	  the code there de-references ipif_ill to get the ill to
16669  *	  send multicast requests. It does not work as ipif is on its
16670  *	  move and already moved when this function is called.
16671  *	  Thus, we need to use from_ill and to_ill send down multicast
16672  *	  requests.
16673  */
16674 static void
16675 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16676 {
16677 	ipif_t *ipif;
16678 	ilm_t *ilm;
16679 
16680 	/*
16681 	 * See whether we need to send down DL_ENABMULTI_REQ on
16682 	 * to_ill as ilm has just been added.
16683 	 */
16684 	ASSERT(IAM_WRITER_ILL(to_ill));
16685 	ASSERT(IAM_WRITER_ILL(from_ill));
16686 
16687 	ILM_WALKER_HOLD(to_ill);
16688 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16689 
16690 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16691 			continue;
16692 		/*
16693 		 * no locks held, ill/ipif cannot dissappear as long
16694 		 * as we are writer.
16695 		 */
16696 		ipif = to_ill->ill_ipif;
16697 		/*
16698 		 * No need to hold any lock as we are the writer and this
16699 		 * can only be changed by a writer.
16700 		 */
16701 		ilm->ilm_is_new = B_FALSE;
16702 
16703 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16704 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16705 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16706 			    "resolver\n"));
16707 			continue;		/* Must be IRE_IF_NORESOLVER */
16708 		}
16709 
16710 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16711 			ip1dbg(("ilm_send_multicast_reqs: "
16712 			    "to_ill MULTI_BCAST\n"));
16713 			goto from;
16714 		}
16715 
16716 		if (to_ill->ill_isv6)
16717 			mld_joingroup(ilm);
16718 		else
16719 			igmp_joingroup(ilm);
16720 
16721 		if (to_ill->ill_ipif_up_count == 0) {
16722 			/*
16723 			 * Nobody there. All multicast addresses will be
16724 			 * re-joined when we get the DL_BIND_ACK bringing the
16725 			 * interface up.
16726 			 */
16727 			ilm->ilm_notify_driver = B_FALSE;
16728 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16729 			goto from;
16730 		}
16731 
16732 		/*
16733 		 * For allmulti address, we want to join on only one interface.
16734 		 * Checking for ilm_numentries_v6 is not correct as you may
16735 		 * find an ilm with zero address on to_ill, but we may not
16736 		 * have nominated to_ill for receiving. Thus, if we have
16737 		 * nominated from_ill (ill_join_allmulti is set), nominate
16738 		 * only if to_ill is not already nominated (to_ill normally
16739 		 * should not have been nominated if "from_ill" has already
16740 		 * been nominated. As we don't prevent failovers from happening
16741 		 * across groups, we don't assert).
16742 		 */
16743 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16744 			/*
16745 			 * There is no need to hold ill locks as we are
16746 			 * writer on both ills and when ill_join_allmulti
16747 			 * is changed the thread is always a writer.
16748 			 */
16749 			if (from_ill->ill_join_allmulti &&
16750 			    !to_ill->ill_join_allmulti) {
16751 				(void) ip_join_allmulti(to_ill->ill_ipif);
16752 			}
16753 		} else if (ilm->ilm_notify_driver) {
16754 
16755 			/*
16756 			 * This is a newly moved ilm so we need to tell the
16757 			 * driver about the new group. There can be more than
16758 			 * one ilm's for the same group in the list each with a
16759 			 * different orig_ifindex. We have to inform the driver
16760 			 * once. In ilm_move_v[4,6] we only set the flag
16761 			 * ilm_notify_driver for the first ilm.
16762 			 */
16763 
16764 			(void) ip_ll_send_enabmulti_req(to_ill,
16765 			    &ilm->ilm_v6addr);
16766 		}
16767 
16768 		ilm->ilm_notify_driver = B_FALSE;
16769 
16770 		/*
16771 		 * See whether we need to send down DL_DISABMULTI_REQ on
16772 		 * from_ill as ilm has just been removed.
16773 		 */
16774 from:
16775 		ipif = from_ill->ill_ipif;
16776 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16777 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16778 			ip1dbg(("ilm_send_multicast_reqs: "
16779 			    "from_ill not resolver\n"));
16780 			continue;		/* Must be IRE_IF_NORESOLVER */
16781 		}
16782 
16783 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16784 			ip1dbg(("ilm_send_multicast_reqs: "
16785 			    "from_ill MULTI_BCAST\n"));
16786 			continue;
16787 		}
16788 
16789 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16790 			if (from_ill->ill_join_allmulti)
16791 				(void) ip_leave_allmulti(from_ill->ill_ipif);
16792 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16793 			(void) ip_ll_send_disabmulti_req(from_ill,
16794 			    &ilm->ilm_v6addr);
16795 		}
16796 	}
16797 	ILM_WALKER_RELE(to_ill);
16798 }
16799 
16800 /*
16801  * This function is called when all multicast memberships needs
16802  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16803  * called only once unlike the IPv4 counterpart where it is called after
16804  * every logical interface is moved. The reason is due to multicast
16805  * memberships are joined using an interface address in IPv4 while in
16806  * IPv6, interface index is used.
16807  */
16808 static void
16809 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16810 {
16811 	ilm_t	*ilm;
16812 	ilm_t	*ilm_next;
16813 	ilm_t	*new_ilm;
16814 	ilm_t	**ilmp;
16815 	int	count;
16816 	char buf[INET6_ADDRSTRLEN];
16817 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16818 	ip_stack_t	*ipst = from_ill->ill_ipst;
16819 
16820 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16821 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16822 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16823 
16824 	if (ifindex == 0) {
16825 		/*
16826 		 * Form the solicited node mcast address which is used later.
16827 		 */
16828 		ipif_t *ipif;
16829 
16830 		ipif = from_ill->ill_ipif;
16831 		ASSERT(ipif->ipif_id == 0);
16832 
16833 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16834 	}
16835 
16836 	ilmp = &from_ill->ill_ilm;
16837 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16838 		ilm_next = ilm->ilm_next;
16839 
16840 		if (ilm->ilm_flags & ILM_DELETED) {
16841 			ilmp = &ilm->ilm_next;
16842 			continue;
16843 		}
16844 
16845 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16846 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16847 		ASSERT(ilm->ilm_orig_ifindex != 0);
16848 		if (ilm->ilm_orig_ifindex == ifindex) {
16849 			/*
16850 			 * We are failing back multicast memberships.
16851 			 * If the same ilm exists in to_ill, it means somebody
16852 			 * has joined the same group there e.g. ff02::1
16853 			 * is joined within the kernel when the interfaces
16854 			 * came UP.
16855 			 */
16856 			ASSERT(ilm->ilm_ipif == NULL);
16857 			if (new_ilm != NULL) {
16858 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16859 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16860 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16861 					new_ilm->ilm_is_new = B_TRUE;
16862 				}
16863 			} else {
16864 				/*
16865 				 * check if we can just move the ilm
16866 				 */
16867 				if (from_ill->ill_ilm_walker_cnt != 0) {
16868 					/*
16869 					 * We have walkers we cannot move
16870 					 * the ilm, so allocate a new ilm,
16871 					 * this (old) ilm will be marked
16872 					 * ILM_DELETED at the end of the loop
16873 					 * and will be freed when the
16874 					 * last walker exits.
16875 					 */
16876 					new_ilm = (ilm_t *)mi_zalloc
16877 					    (sizeof (ilm_t));
16878 					if (new_ilm == NULL) {
16879 						ip0dbg(("ilm_move_v6: "
16880 						    "FAILBACK of IPv6"
16881 						    " multicast address %s : "
16882 						    "from %s to"
16883 						    " %s failed : ENOMEM \n",
16884 						    inet_ntop(AF_INET6,
16885 						    &ilm->ilm_v6addr, buf,
16886 						    sizeof (buf)),
16887 						    from_ill->ill_name,
16888 						    to_ill->ill_name));
16889 
16890 							ilmp = &ilm->ilm_next;
16891 							continue;
16892 					}
16893 					*new_ilm = *ilm;
16894 					/*
16895 					 * we don't want new_ilm linked to
16896 					 * ilm's filter list.
16897 					 */
16898 					new_ilm->ilm_filter = NULL;
16899 				} else {
16900 					/*
16901 					 * No walkers we can move the ilm.
16902 					 * lets take it out of the list.
16903 					 */
16904 					*ilmp = ilm->ilm_next;
16905 					ilm->ilm_next = NULL;
16906 					DTRACE_PROBE3(ill__decr__cnt,
16907 					    (ill_t *), from_ill,
16908 					    (char *), "ilm", (void *), ilm);
16909 					ASSERT(from_ill->ill_ilm_cnt > 0);
16910 					from_ill->ill_ilm_cnt--;
16911 
16912 					new_ilm = ilm;
16913 				}
16914 
16915 				/*
16916 				 * if this is the first ilm for the group
16917 				 * set ilm_notify_driver so that we notify the
16918 				 * driver in ilm_send_multicast_reqs.
16919 				 */
16920 				if (ilm_lookup_ill_v6(to_ill,
16921 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16922 					new_ilm->ilm_notify_driver = B_TRUE;
16923 
16924 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
16925 				    (char *), "ilm", (void *), new_ilm);
16926 				new_ilm->ilm_ill = to_ill;
16927 				to_ill->ill_ilm_cnt++;
16928 
16929 				/* Add to the to_ill's list */
16930 				new_ilm->ilm_next = to_ill->ill_ilm;
16931 				to_ill->ill_ilm = new_ilm;
16932 				/*
16933 				 * set the flag so that mld_joingroup is
16934 				 * called in ilm_send_multicast_reqs().
16935 				 */
16936 				new_ilm->ilm_is_new = B_TRUE;
16937 			}
16938 			goto bottom;
16939 		} else if (ifindex != 0) {
16940 			/*
16941 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16942 			 * has not matched above, look at the next ilm.
16943 			 */
16944 			ilmp = &ilm->ilm_next;
16945 			continue;
16946 		}
16947 		/*
16948 		 * If we are here, it means ifindex is 0. Failover
16949 		 * everything.
16950 		 *
16951 		 * We need to handle solicited node mcast address
16952 		 * and all_nodes mcast address differently as they
16953 		 * are joined witin the kenrel (ipif_multicast_up)
16954 		 * and potentially from the userland. We are called
16955 		 * after the ipifs of from_ill has been moved.
16956 		 * If we still find ilms on ill with solicited node
16957 		 * mcast address or all_nodes mcast address, it must
16958 		 * belong to the UP interface that has not moved e.g.
16959 		 * ipif_id 0 with the link local prefix does not move.
16960 		 * We join this on the new ill accounting for all the
16961 		 * userland memberships so that applications don't
16962 		 * see any failure.
16963 		 *
16964 		 * We need to make sure that we account only for the
16965 		 * solicited node and all node multicast addresses
16966 		 * that was brought UP on these. In the case of
16967 		 * a failover from A to B, we might have ilms belonging
16968 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
16969 		 * for the membership from the userland. If we are failing
16970 		 * over from B to C now, we will find the ones belonging
16971 		 * to A on B. These don't account for the ill_ipif_up_count.
16972 		 * They just move from B to C. The check below on
16973 		 * ilm_orig_ifindex ensures that.
16974 		 */
16975 		if ((ilm->ilm_orig_ifindex ==
16976 		    from_ill->ill_phyint->phyint_ifindex) &&
16977 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
16978 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
16979 		    &ilm->ilm_v6addr))) {
16980 			ASSERT(ilm->ilm_refcnt > 0);
16981 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
16982 			/*
16983 			 * For indentation reasons, we are not using a
16984 			 * "else" here.
16985 			 */
16986 			if (count == 0) {
16987 				ilmp = &ilm->ilm_next;
16988 				continue;
16989 			}
16990 			ilm->ilm_refcnt -= count;
16991 			if (new_ilm != NULL) {
16992 				/*
16993 				 * Can find one with the same
16994 				 * ilm_orig_ifindex, if we are failing
16995 				 * over to a STANDBY. This happens
16996 				 * when somebody wants to join a group
16997 				 * on a STANDBY interface and we
16998 				 * internally join on a different one.
16999 				 * If we had joined on from_ill then, a
17000 				 * failover now will find a new ilm
17001 				 * with this index.
17002 				 */
17003 				ip1dbg(("ilm_move_v6: FAILOVER, found"
17004 				    " new ilm on %s, group address %s\n",
17005 				    to_ill->ill_name,
17006 				    inet_ntop(AF_INET6,
17007 				    &ilm->ilm_v6addr, buf,
17008 				    sizeof (buf))));
17009 				new_ilm->ilm_refcnt += count;
17010 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17011 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17012 					new_ilm->ilm_is_new = B_TRUE;
17013 				}
17014 			} else {
17015 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17016 				if (new_ilm == NULL) {
17017 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17018 					    " multicast address %s : from %s to"
17019 					    " %s failed : ENOMEM \n",
17020 					    inet_ntop(AF_INET6,
17021 					    &ilm->ilm_v6addr, buf,
17022 					    sizeof (buf)), from_ill->ill_name,
17023 					    to_ill->ill_name));
17024 					ilmp = &ilm->ilm_next;
17025 					continue;
17026 				}
17027 				*new_ilm = *ilm;
17028 				new_ilm->ilm_filter = NULL;
17029 				new_ilm->ilm_refcnt = count;
17030 				new_ilm->ilm_timer = INFINITY;
17031 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17032 				new_ilm->ilm_is_new = B_TRUE;
17033 				/*
17034 				 * If the to_ill has not joined this
17035 				 * group we need to tell the driver in
17036 				 * ill_send_multicast_reqs.
17037 				 */
17038 				if (ilm_lookup_ill_v6(to_ill,
17039 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17040 					new_ilm->ilm_notify_driver = B_TRUE;
17041 
17042 				new_ilm->ilm_ill = to_ill;
17043 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
17044 				    (char *), "ilm", (void *), new_ilm);
17045 				to_ill->ill_ilm_cnt++;
17046 
17047 				/* Add to the to_ill's list */
17048 				new_ilm->ilm_next = to_ill->ill_ilm;
17049 				to_ill->ill_ilm = new_ilm;
17050 				ASSERT(new_ilm->ilm_ipif == NULL);
17051 			}
17052 			if (ilm->ilm_refcnt == 0) {
17053 				goto bottom;
17054 			} else {
17055 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17056 				CLEAR_SLIST(new_ilm->ilm_filter);
17057 				ilmp = &ilm->ilm_next;
17058 			}
17059 			continue;
17060 		} else {
17061 			/*
17062 			 * ifindex = 0 means, move everything pointing at
17063 			 * from_ill. We are doing this becuase ill has
17064 			 * either FAILED or became INACTIVE.
17065 			 *
17066 			 * As we would like to move things later back to
17067 			 * from_ill, we want to retain the identity of this
17068 			 * ilm. Thus, we don't blindly increment the reference
17069 			 * count on the ilms matching the address alone. We
17070 			 * need to match on the ilm_orig_index also. new_ilm
17071 			 * was obtained by matching ilm_orig_index also.
17072 			 */
17073 			if (new_ilm != NULL) {
17074 				/*
17075 				 * This is possible only if a previous restore
17076 				 * was incomplete i.e restore to
17077 				 * ilm_orig_ifindex left some ilms because
17078 				 * of some failures. Thus when we are failing
17079 				 * again, we might find our old friends there.
17080 				 */
17081 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17082 				    " on %s, group address %s\n",
17083 				    to_ill->ill_name,
17084 				    inet_ntop(AF_INET6,
17085 				    &ilm->ilm_v6addr, buf,
17086 				    sizeof (buf))));
17087 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17088 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17089 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17090 					new_ilm->ilm_is_new = B_TRUE;
17091 				}
17092 			} else {
17093 				if (from_ill->ill_ilm_walker_cnt != 0) {
17094 					new_ilm = (ilm_t *)
17095 					    mi_zalloc(sizeof (ilm_t));
17096 					if (new_ilm == NULL) {
17097 						ip0dbg(("ilm_move_v6: "
17098 						    "FAILOVER of IPv6"
17099 						    " multicast address %s : "
17100 						    "from %s to"
17101 						    " %s failed : ENOMEM \n",
17102 						    inet_ntop(AF_INET6,
17103 						    &ilm->ilm_v6addr, buf,
17104 						    sizeof (buf)),
17105 						    from_ill->ill_name,
17106 						    to_ill->ill_name));
17107 
17108 							ilmp = &ilm->ilm_next;
17109 							continue;
17110 					}
17111 					*new_ilm = *ilm;
17112 					new_ilm->ilm_filter = NULL;
17113 				} else {
17114 					*ilmp = ilm->ilm_next;
17115 					DTRACE_PROBE3(ill__decr__cnt,
17116 					    (ill_t *), from_ill,
17117 					    (char *), "ilm", (void *), ilm);
17118 					ASSERT(from_ill->ill_ilm_cnt > 0);
17119 					from_ill->ill_ilm_cnt--;
17120 
17121 					new_ilm = ilm;
17122 				}
17123 				/*
17124 				 * If the to_ill has not joined this
17125 				 * group we need to tell the driver in
17126 				 * ill_send_multicast_reqs.
17127 				 */
17128 				if (ilm_lookup_ill_v6(to_ill,
17129 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17130 					new_ilm->ilm_notify_driver = B_TRUE;
17131 
17132 				/* Add to the to_ill's list */
17133 				new_ilm->ilm_next = to_ill->ill_ilm;
17134 				to_ill->ill_ilm = new_ilm;
17135 				ASSERT(ilm->ilm_ipif == NULL);
17136 				new_ilm->ilm_ill = to_ill;
17137 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
17138 				    (char *), "ilm", (void *), new_ilm);
17139 				to_ill->ill_ilm_cnt++;
17140 				new_ilm->ilm_is_new = B_TRUE;
17141 			}
17142 
17143 		}
17144 
17145 bottom:
17146 		/*
17147 		 * Revert multicast filter state to (EXCLUDE, NULL).
17148 		 * new_ilm->ilm_is_new should already be set if needed.
17149 		 */
17150 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17151 		CLEAR_SLIST(new_ilm->ilm_filter);
17152 		/*
17153 		 * We allocated/got a new ilm, free the old one.
17154 		 */
17155 		if (new_ilm != ilm) {
17156 			if (from_ill->ill_ilm_walker_cnt == 0) {
17157 				*ilmp = ilm->ilm_next;
17158 
17159 				ASSERT(ilm->ilm_ipif == NULL); /* ipv6 */
17160 				DTRACE_PROBE3(ill__decr__cnt, (ill_t *),
17161 				    from_ill, (char *), "ilm", (void *), ilm);
17162 				ASSERT(from_ill->ill_ilm_cnt > 0);
17163 				from_ill->ill_ilm_cnt--;
17164 
17165 				ilm_inactive(ilm); /* frees this ilm */
17166 
17167 			} else {
17168 				ilm->ilm_flags |= ILM_DELETED;
17169 				from_ill->ill_ilm_cleanup_reqd = 1;
17170 				ilmp = &ilm->ilm_next;
17171 			}
17172 		}
17173 	}
17174 }
17175 
17176 /*
17177  * Move all the multicast memberships to to_ill. Called when
17178  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17179  * different from IPv6 counterpart as multicast memberships are associated
17180  * with ills in IPv6. This function is called after every ipif is moved
17181  * unlike IPv6, where it is moved only once.
17182  */
17183 static void
17184 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17185 {
17186 	ilm_t	*ilm;
17187 	ilm_t	*ilm_next;
17188 	ilm_t	*new_ilm;
17189 	ilm_t	**ilmp;
17190 	ip_stack_t	*ipst = from_ill->ill_ipst;
17191 
17192 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17193 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17194 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17195 
17196 	ilmp = &from_ill->ill_ilm;
17197 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17198 		ilm_next = ilm->ilm_next;
17199 
17200 		if (ilm->ilm_flags & ILM_DELETED) {
17201 			ilmp = &ilm->ilm_next;
17202 			continue;
17203 		}
17204 
17205 		ASSERT(ilm->ilm_ipif != NULL);
17206 
17207 		if (ilm->ilm_ipif != ipif) {
17208 			ilmp = &ilm->ilm_next;
17209 			continue;
17210 		}
17211 
17212 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17213 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17214 			new_ilm = ilm_lookup_ipif(ipif,
17215 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17216 			if (new_ilm != NULL) {
17217 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17218 				/*
17219 				 * We still need to deal with the from_ill.
17220 				 */
17221 				new_ilm->ilm_is_new = B_TRUE;
17222 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17223 				CLEAR_SLIST(new_ilm->ilm_filter);
17224 				ASSERT(ilm->ilm_ipif == ipif);
17225 				ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17226 				if (from_ill->ill_ilm_walker_cnt == 0) {
17227 					DTRACE_PROBE3(ill__decr__cnt,
17228 					    (ill_t *), from_ill,
17229 					    (char *), "ilm", (void *), ilm);
17230 					ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17231 				}
17232 				goto delete_ilm;
17233 			}
17234 			/*
17235 			 * If we could not find one e.g. ipif is
17236 			 * still down on to_ill, we add this ilm
17237 			 * on ill_new to preserve the reference
17238 			 * count.
17239 			 */
17240 		}
17241 		/*
17242 		 * When ipifs move, ilms always move with it
17243 		 * to the NEW ill. Thus we should never be
17244 		 * able to find ilm till we really move it here.
17245 		 */
17246 		ASSERT(ilm_lookup_ipif(ipif,
17247 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17248 
17249 		if (from_ill->ill_ilm_walker_cnt != 0) {
17250 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17251 			if (new_ilm == NULL) {
17252 				char buf[INET6_ADDRSTRLEN];
17253 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17254 				    " multicast address %s : "
17255 				    "from %s to"
17256 				    " %s failed : ENOMEM \n",
17257 				    inet_ntop(AF_INET,
17258 				    &ilm->ilm_v6addr, buf,
17259 				    sizeof (buf)),
17260 				    from_ill->ill_name,
17261 				    to_ill->ill_name));
17262 
17263 				ilmp = &ilm->ilm_next;
17264 				continue;
17265 			}
17266 			*new_ilm = *ilm;
17267 			DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *), ipif,
17268 			    (char *), "ilm", (void *), ilm);
17269 			new_ilm->ilm_ipif->ipif_ilm_cnt++;
17270 			/* We don't want new_ilm linked to ilm's filter list */
17271 			new_ilm->ilm_filter = NULL;
17272 		} else {
17273 			/* Remove from the list */
17274 			*ilmp = ilm->ilm_next;
17275 			new_ilm = ilm;
17276 		}
17277 
17278 		/*
17279 		 * If we have never joined this group on the to_ill
17280 		 * make sure we tell the driver.
17281 		 */
17282 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17283 		    ALL_ZONES) == NULL)
17284 			new_ilm->ilm_notify_driver = B_TRUE;
17285 
17286 		/* Add to the to_ill's list */
17287 		new_ilm->ilm_next = to_ill->ill_ilm;
17288 		to_ill->ill_ilm = new_ilm;
17289 		new_ilm->ilm_is_new = B_TRUE;
17290 
17291 		/*
17292 		 * Revert multicast filter state to (EXCLUDE, NULL)
17293 		 */
17294 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17295 		CLEAR_SLIST(new_ilm->ilm_filter);
17296 
17297 		/*
17298 		 * Delete only if we have allocated a new ilm.
17299 		 */
17300 		if (new_ilm != ilm) {
17301 delete_ilm:
17302 			if (from_ill->ill_ilm_walker_cnt == 0) {
17303 				/* Remove from the list */
17304 				*ilmp = ilm->ilm_next;
17305 				ilm->ilm_next = NULL;
17306 				DTRACE_PROBE3(ipif__decr__cnt,
17307 				    (ipif_t *), ilm->ilm_ipif,
17308 				    (char *), "ilm", (void *), ilm);
17309 				ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17310 				ilm->ilm_ipif->ipif_ilm_cnt--;
17311 				ilm_inactive(ilm);
17312 			} else {
17313 				ilm->ilm_flags |= ILM_DELETED;
17314 				from_ill->ill_ilm_cleanup_reqd = 1;
17315 				ilmp = &ilm->ilm_next;
17316 			}
17317 		}
17318 	}
17319 }
17320 
17321 static uint_t
17322 ipif_get_id(ill_t *ill, uint_t id)
17323 {
17324 	uint_t	unit;
17325 	ipif_t	*tipif;
17326 	boolean_t found = B_FALSE;
17327 	ip_stack_t	*ipst = ill->ill_ipst;
17328 
17329 	/*
17330 	 * During failback, we want to go back to the same id
17331 	 * instead of the smallest id so that the original
17332 	 * configuration is maintained. id is non-zero in that
17333 	 * case.
17334 	 */
17335 	if (id != 0) {
17336 		/*
17337 		 * While failing back, if we still have an ipif with
17338 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17339 		 * as soon as we return from this function. It was
17340 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17341 		 * we can choose the smallest id. Thus we return zero
17342 		 * in that case ignoring the hint.
17343 		 */
17344 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17345 			return (0);
17346 		for (tipif = ill->ill_ipif; tipif != NULL;
17347 		    tipif = tipif->ipif_next) {
17348 			if (tipif->ipif_id == id) {
17349 				found = B_TRUE;
17350 				break;
17351 			}
17352 		}
17353 		/*
17354 		 * If somebody already plumbed another logical
17355 		 * with the same id, we won't be able to find it.
17356 		 */
17357 		if (!found)
17358 			return (id);
17359 	}
17360 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17361 		found = B_FALSE;
17362 		for (tipif = ill->ill_ipif; tipif != NULL;
17363 		    tipif = tipif->ipif_next) {
17364 			if (tipif->ipif_id == unit) {
17365 				found = B_TRUE;
17366 				break;
17367 			}
17368 		}
17369 		if (!found)
17370 			break;
17371 	}
17372 	return (unit);
17373 }
17374 
17375 /* ARGSUSED */
17376 static int
17377 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17378     ipif_t **rep_ipif_ptr)
17379 {
17380 	ill_t	*from_ill;
17381 	ipif_t	*rep_ipif;
17382 	uint_t	unit;
17383 	int err = 0;
17384 	ipif_t	*to_ipif;
17385 	struct iocblk	*iocp;
17386 	boolean_t failback_cmd;
17387 	boolean_t remove_ipif;
17388 	int	rc;
17389 	ip_stack_t	*ipst;
17390 
17391 	ASSERT(IAM_WRITER_ILL(to_ill));
17392 	ASSERT(IAM_WRITER_IPIF(ipif));
17393 
17394 	iocp = (struct iocblk *)mp->b_rptr;
17395 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17396 	remove_ipif = B_FALSE;
17397 
17398 	from_ill = ipif->ipif_ill;
17399 	ipst = from_ill->ill_ipst;
17400 
17401 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17402 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17403 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17404 
17405 	/*
17406 	 * Don't move LINK LOCAL addresses as they are tied to
17407 	 * physical interface.
17408 	 */
17409 	if (from_ill->ill_isv6 &&
17410 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17411 		ipif->ipif_was_up = B_FALSE;
17412 		IPIF_UNMARK_MOVING(ipif);
17413 		return (0);
17414 	}
17415 
17416 	/*
17417 	 * We set the ipif_id to maximum so that the search for
17418 	 * ipif_id will pick the lowest number i.e 0 in the
17419 	 * following 2 cases :
17420 	 *
17421 	 * 1) We have a replacement ipif at the head of to_ill.
17422 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17423 	 *    on to_ill and hence the MOVE might fail. We want to
17424 	 *    remove it only if we could move the ipif. Thus, by
17425 	 *    setting it to the MAX value, we make the search in
17426 	 *    ipif_get_id return the zeroth id.
17427 	 *
17428 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17429 	 *    we might just have a zero address plumbed on the ipif
17430 	 *    with zero id in the case of IPv4. We remove that while
17431 	 *    doing the failback. We want to remove it only if we
17432 	 *    could move the ipif. Thus, by setting it to the MAX
17433 	 *    value, we make the search in ipif_get_id return the
17434 	 *    zeroth id.
17435 	 *
17436 	 * Both (1) and (2) are done only when when we are moving
17437 	 * an ipif (either due to failover/failback) which originally
17438 	 * belonged to this interface i.e the ipif_orig_ifindex is
17439 	 * the same as to_ill's ifindex. This is needed so that
17440 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17441 	 * from B -> A (B is being removed from the group) and
17442 	 * FAILBACK from A -> B restores the original configuration.
17443 	 * Without the check for orig_ifindex, the second FAILOVER
17444 	 * could make the ipif belonging to B replace the A's zeroth
17445 	 * ipif and the subsequent failback re-creating the replacement
17446 	 * ipif again.
17447 	 *
17448 	 * NOTE : We created the replacement ipif when we did a
17449 	 * FAILOVER (See below). We could check for FAILBACK and
17450 	 * then look for replacement ipif to be removed. But we don't
17451 	 * want to do that because we wan't to allow the possibility
17452 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17453 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17454 	 * from B -> A.
17455 	 */
17456 	to_ipif = to_ill->ill_ipif;
17457 	if ((to_ill->ill_phyint->phyint_ifindex ==
17458 	    ipif->ipif_orig_ifindex) &&
17459 	    to_ipif->ipif_replace_zero) {
17460 		ASSERT(to_ipif->ipif_id == 0);
17461 		remove_ipif = B_TRUE;
17462 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17463 	}
17464 	/*
17465 	 * Find the lowest logical unit number on the to_ill.
17466 	 * If we are failing back, try to get the original id
17467 	 * rather than the lowest one so that the original
17468 	 * configuration is maintained.
17469 	 *
17470 	 * XXX need a better scheme for this.
17471 	 */
17472 	if (failback_cmd) {
17473 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17474 	} else {
17475 		unit = ipif_get_id(to_ill, 0);
17476 	}
17477 
17478 	/* Reset back to zero in case we fail below */
17479 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17480 		to_ipif->ipif_id = 0;
17481 
17482 	if (unit == ipst->ips_ip_addrs_per_if) {
17483 		ipif->ipif_was_up = B_FALSE;
17484 		IPIF_UNMARK_MOVING(ipif);
17485 		return (EINVAL);
17486 	}
17487 
17488 	/*
17489 	 * ipif is ready to move from "from_ill" to "to_ill".
17490 	 *
17491 	 * 1) If we are moving ipif with id zero, create a
17492 	 *    replacement ipif for this ipif on from_ill. If this fails
17493 	 *    fail the MOVE operation.
17494 	 *
17495 	 * 2) Remove the replacement ipif on to_ill if any.
17496 	 *    We could remove the replacement ipif when we are moving
17497 	 *    the ipif with id zero. But what if somebody already
17498 	 *    unplumbed it ? Thus we always remove it if it is present.
17499 	 *    We want to do it only if we are sure we are going to
17500 	 *    move the ipif to to_ill which is why there are no
17501 	 *    returns due to error till ipif is linked to to_ill.
17502 	 *    Note that the first ipif that we failback will always
17503 	 *    be zero if it is present.
17504 	 */
17505 	if (ipif->ipif_id == 0) {
17506 		ipaddr_t inaddr_any = INADDR_ANY;
17507 
17508 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17509 		if (rep_ipif == NULL) {
17510 			ipif->ipif_was_up = B_FALSE;
17511 			IPIF_UNMARK_MOVING(ipif);
17512 			return (ENOMEM);
17513 		}
17514 		*rep_ipif = ipif_zero;
17515 		/*
17516 		 * Before we put the ipif on the list, store the addresses
17517 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17518 		 * assumes so. This logic is not any different from what
17519 		 * ipif_allocate does.
17520 		 */
17521 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17522 		    &rep_ipif->ipif_v6lcl_addr);
17523 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17524 		    &rep_ipif->ipif_v6src_addr);
17525 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17526 		    &rep_ipif->ipif_v6subnet);
17527 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17528 		    &rep_ipif->ipif_v6net_mask);
17529 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17530 		    &rep_ipif->ipif_v6brd_addr);
17531 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17532 		    &rep_ipif->ipif_v6pp_dst_addr);
17533 		/*
17534 		 * We mark IPIF_NOFAILOVER so that this can never
17535 		 * move.
17536 		 */
17537 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17538 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17539 		rep_ipif->ipif_replace_zero = B_TRUE;
17540 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17541 		    MUTEX_DEFAULT, NULL);
17542 		rep_ipif->ipif_id = 0;
17543 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17544 		rep_ipif->ipif_ill = from_ill;
17545 		rep_ipif->ipif_orig_ifindex =
17546 		    from_ill->ill_phyint->phyint_ifindex;
17547 		/* Insert at head */
17548 		rep_ipif->ipif_next = from_ill->ill_ipif;
17549 		from_ill->ill_ipif = rep_ipif;
17550 		/*
17551 		 * We don't really care to let apps know about
17552 		 * this interface.
17553 		 */
17554 	}
17555 
17556 	if (remove_ipif) {
17557 		/*
17558 		 * We set to a max value above for this case to get
17559 		 * id zero. ASSERT that we did get one.
17560 		 */
17561 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17562 		rep_ipif = to_ipif;
17563 		to_ill->ill_ipif = rep_ipif->ipif_next;
17564 		rep_ipif->ipif_next = NULL;
17565 		/*
17566 		 * If some apps scanned and find this interface,
17567 		 * it is time to let them know, so that they can
17568 		 * delete it.
17569 		 */
17570 
17571 		*rep_ipif_ptr = rep_ipif;
17572 	}
17573 
17574 	/* Get it out of the ILL interface list. */
17575 	ipif_remove(ipif, B_FALSE);
17576 
17577 	/* Assign the new ill */
17578 	ipif->ipif_ill = to_ill;
17579 	ipif->ipif_id = unit;
17580 	/* id has already been checked */
17581 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17582 	ASSERT(rc == 0);
17583 	/* Let SCTP update its list */
17584 	sctp_move_ipif(ipif, from_ill, to_ill);
17585 	/*
17586 	 * Handle the failover and failback of ipif_t between
17587 	 * ill_t that have differing maximum mtu values.
17588 	 */
17589 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17590 		if (ipif->ipif_saved_mtu == 0) {
17591 			/*
17592 			 * As this ipif_t is moving to an ill_t
17593 			 * that has a lower ill_max_mtu, its
17594 			 * ipif_mtu needs to be saved so it can
17595 			 * be restored during failback or during
17596 			 * failover to an ill_t which has a
17597 			 * higher ill_max_mtu.
17598 			 */
17599 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17600 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17601 		} else {
17602 			/*
17603 			 * The ipif_t is, once again, moving to
17604 			 * an ill_t that has a lower maximum mtu
17605 			 * value.
17606 			 */
17607 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17608 		}
17609 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17610 	    ipif->ipif_saved_mtu != 0) {
17611 		/*
17612 		 * The mtu of this ipif_t had to be reduced
17613 		 * during an earlier failover; this is an
17614 		 * opportunity for it to be increased (either as
17615 		 * part of another failover or a failback).
17616 		 */
17617 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17618 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17619 			ipif->ipif_saved_mtu = 0;
17620 		} else {
17621 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17622 		}
17623 	}
17624 
17625 	/*
17626 	 * We preserve all the other fields of the ipif including
17627 	 * ipif_saved_ire_mp. The routes that are saved here will
17628 	 * be recreated on the new interface and back on the old
17629 	 * interface when we move back.
17630 	 */
17631 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17632 
17633 	return (err);
17634 }
17635 
17636 static int
17637 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17638     int ifindex, ipif_t **rep_ipif_ptr)
17639 {
17640 	ipif_t *mipif;
17641 	ipif_t *ipif_next;
17642 	int err;
17643 
17644 	/*
17645 	 * We don't really try to MOVE back things if some of the
17646 	 * operations fail. The daemon will take care of moving again
17647 	 * later on.
17648 	 */
17649 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17650 		ipif_next = mipif->ipif_next;
17651 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17652 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17653 
17654 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17655 
17656 			/*
17657 			 * When the MOVE fails, it is the job of the
17658 			 * application to take care of this properly
17659 			 * i.e try again if it is ENOMEM.
17660 			 */
17661 			if (mipif->ipif_ill != from_ill) {
17662 				/*
17663 				 * ipif has moved.
17664 				 *
17665 				 * Move the multicast memberships associated
17666 				 * with this ipif to the new ill. For IPv6, we
17667 				 * do it once after all the ipifs are moved
17668 				 * (in ill_move) as they are not associated
17669 				 * with ipifs.
17670 				 *
17671 				 * We need to move the ilms as the ipif has
17672 				 * already been moved to a new ill even
17673 				 * in the case of errors. Neither
17674 				 * ilm_free(ipif) will find the ilm
17675 				 * when somebody unplumbs this ipif nor
17676 				 * ilm_delete(ilm) will be able to find the
17677 				 * ilm, if we don't move now.
17678 				 */
17679 				if (!from_ill->ill_isv6)
17680 					ilm_move_v4(from_ill, to_ill, mipif);
17681 			}
17682 
17683 			if (err != 0)
17684 				return (err);
17685 		}
17686 	}
17687 	return (0);
17688 }
17689 
17690 static int
17691 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17692 {
17693 	int ifindex;
17694 	int err;
17695 	struct iocblk	*iocp;
17696 	ipif_t	*ipif;
17697 	ipif_t *rep_ipif_ptr = NULL;
17698 	ipif_t	*from_ipif = NULL;
17699 	boolean_t check_rep_if = B_FALSE;
17700 	ip_stack_t	*ipst = from_ill->ill_ipst;
17701 
17702 	iocp = (struct iocblk *)mp->b_rptr;
17703 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17704 		/*
17705 		 * Move everything pointing at from_ill to to_ill.
17706 		 * We acheive this by passing in 0 as ifindex.
17707 		 */
17708 		ifindex = 0;
17709 	} else {
17710 		/*
17711 		 * Move everything pointing at from_ill whose original
17712 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17713 		 * We acheive this by passing in ifindex rather than 0.
17714 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17715 		 */
17716 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17717 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17718 	}
17719 
17720 	/*
17721 	 * Determine if there is at least one ipif that would move from
17722 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17723 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17724 	 * the move, in which case we need to quiesce the replacement ipif also.
17725 	 */
17726 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17727 	    from_ipif = from_ipif->ipif_next) {
17728 		if (((ifindex == 0) ||
17729 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17730 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17731 			check_rep_if = B_TRUE;
17732 			break;
17733 		}
17734 	}
17735 
17736 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17737 
17738 	GRAB_ILL_LOCKS(from_ill, to_ill);
17739 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17740 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17741 		    mp, ILL_MOVE_OK);
17742 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17743 		return (EINPROGRESS);
17744 	}
17745 
17746 	/* Check if the replacement ipif is quiescent to delete */
17747 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17748 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17749 		to_ill->ill_ipif->ipif_state_flags |=
17750 		    IPIF_MOVING | IPIF_CHANGING;
17751 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17752 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17753 			    mp, ILL_MOVE_OK);
17754 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17755 			return (EINPROGRESS);
17756 		}
17757 	}
17758 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17759 
17760 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17761 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17762 	GRAB_ILL_LOCKS(from_ill, to_ill);
17763 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17764 
17765 	/* ilm_move is done inside ipif_move for IPv4 */
17766 	if (err == 0 && from_ill->ill_isv6)
17767 		ilm_move_v6(from_ill, to_ill, ifindex);
17768 
17769 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17770 	rw_exit(&ipst->ips_ill_g_lock);
17771 
17772 	/*
17773 	 * send rts messages and multicast messages.
17774 	 */
17775 	if (rep_ipif_ptr != NULL) {
17776 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17777 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17778 			rep_ipif_ptr->ipif_recovery_id = 0;
17779 		}
17780 		ip_rts_ifmsg(rep_ipif_ptr);
17781 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17782 #ifdef DEBUG
17783 		ipif_trace_cleanup(rep_ipif_ptr);
17784 #endif
17785 		mi_free(rep_ipif_ptr);
17786 	}
17787 
17788 	conn_move_ill(from_ill, to_ill, ifindex);
17789 
17790 	return (err);
17791 }
17792 
17793 /*
17794  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17795  * Also checks for the validity of the arguments.
17796  * Note: We are already exclusive inside the from group.
17797  * It is upto the caller to release refcnt on the to_ill's.
17798  */
17799 static int
17800 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17801     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17802 {
17803 	int dst_index;
17804 	ipif_t *ipif_v4, *ipif_v6;
17805 	struct lifreq *lifr;
17806 	mblk_t *mp1;
17807 	boolean_t exists;
17808 	sin_t	*sin;
17809 	int	err = 0;
17810 	ip_stack_t	*ipst;
17811 
17812 	if (CONN_Q(q))
17813 		ipst = CONNQ_TO_IPST(q);
17814 	else
17815 		ipst = ILLQ_TO_IPST(q);
17816 
17817 	if ((mp1 = mp->b_cont) == NULL)
17818 		return (EPROTO);
17819 
17820 	if ((mp1 = mp1->b_cont) == NULL)
17821 		return (EPROTO);
17822 
17823 	lifr = (struct lifreq *)mp1->b_rptr;
17824 	sin = (sin_t *)&lifr->lifr_addr;
17825 
17826 	/*
17827 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17828 	 * specific operations.
17829 	 */
17830 	if (sin->sin_family != AF_UNSPEC)
17831 		return (EINVAL);
17832 
17833 	/*
17834 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17835 	 * NULLs for the last 4 args and we know the lookup won't fail
17836 	 * with EINPROGRESS.
17837 	 */
17838 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17839 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17840 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17841 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17842 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17843 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17844 
17845 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17846 		return (ENXIO);
17847 
17848 	if (ipif_v4 != NULL) {
17849 		ASSERT(ipif_v4->ipif_refcnt != 0);
17850 		if (ipif_v4->ipif_id != 0) {
17851 			err = EINVAL;
17852 			goto done;
17853 		}
17854 
17855 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17856 		*ill_from_v4 = ipif_v4->ipif_ill;
17857 	}
17858 
17859 	if (ipif_v6 != NULL) {
17860 		ASSERT(ipif_v6->ipif_refcnt != 0);
17861 		if (ipif_v6->ipif_id != 0) {
17862 			err = EINVAL;
17863 			goto done;
17864 		}
17865 
17866 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17867 		*ill_from_v6 = ipif_v6->ipif_ill;
17868 	}
17869 
17870 	err = 0;
17871 	dst_index = lifr->lifr_movetoindex;
17872 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17873 	    q, mp, ip_process_ioctl, &err, ipst);
17874 	if (err != 0) {
17875 		/*
17876 		 * A move may be in progress, EINPROGRESS looking up the "to"
17877 		 * ill means changes already done to the "from" ipsq need to
17878 		 * be undone to avoid potential deadlocks.
17879 		 *
17880 		 * ENXIO will usually be because there is only v6 on the ill,
17881 		 * that's not treated as an error unless an ENXIO is also
17882 		 * seen when looking up the v6 "to" ill.
17883 		 *
17884 		 * If EINPROGRESS, the mp has been enqueued and can not be
17885 		 * used to look up the v6 "to" ill, but a preemptive clean
17886 		 * up of changes to the v6 "from" ipsq is done.
17887 		 */
17888 		if (err == EINPROGRESS) {
17889 			if (*ill_from_v4 != NULL) {
17890 				ill_t   *from_ill;
17891 				ipsq_t  *from_ipsq;
17892 
17893 				from_ill = ipif_v4->ipif_ill;
17894 				from_ipsq = from_ill->ill_phyint->phyint_ipsq;
17895 
17896 				mutex_enter(&from_ipsq->ipsq_lock);
17897 				from_ipsq->ipsq_current_ipif = NULL;
17898 				mutex_exit(&from_ipsq->ipsq_lock);
17899 			}
17900 			if (*ill_from_v6 != NULL) {
17901 				ill_t   *from_ill;
17902 				ipsq_t  *from_ipsq;
17903 
17904 				from_ill = ipif_v6->ipif_ill;
17905 				from_ipsq = from_ill->ill_phyint->phyint_ipsq;
17906 
17907 				mutex_enter(&from_ipsq->ipsq_lock);
17908 				from_ipsq->ipsq_current_ipif = NULL;
17909 				mutex_exit(&from_ipsq->ipsq_lock);
17910 			}
17911 			goto done;
17912 		}
17913 		ASSERT(err == ENXIO);
17914 		err = 0;
17915 	}
17916 
17917 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17918 	    q, mp, ip_process_ioctl, &err, ipst);
17919 	if (err != 0) {
17920 		/*
17921 		 * A move may be in progress, EINPROGRESS looking up the "to"
17922 		 * ill means changes already done to the "from" ipsq need to
17923 		 * be undone to avoid potential deadlocks.
17924 		 */
17925 		if (err == EINPROGRESS) {
17926 			if (*ill_from_v6 != NULL) {
17927 				ill_t   *from_ill;
17928 				ipsq_t  *from_ipsq;
17929 
17930 				from_ill = ipif_v6->ipif_ill;
17931 				from_ipsq = from_ill->ill_phyint->phyint_ipsq;
17932 
17933 				mutex_enter(&from_ipsq->ipsq_lock);
17934 				from_ipsq->ipsq_current_ipif = NULL;
17935 				mutex_exit(&from_ipsq->ipsq_lock);
17936 			}
17937 			goto done;
17938 		}
17939 		ASSERT(err == ENXIO);
17940 
17941 		/* Both v4 and v6 lookup failed */
17942 		if (*ill_to_v4 == NULL) {
17943 			err = ENXIO;
17944 			goto done;
17945 		}
17946 		err = 0;
17947 	}
17948 
17949 	/*
17950 	 * If we have something to MOVE i.e "from" not NULL,
17951 	 * "to" should be non-NULL.
17952 	 */
17953 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
17954 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
17955 		err = EINVAL;
17956 	}
17957 
17958 done:
17959 	if (ipif_v4 != NULL)
17960 		ipif_refrele(ipif_v4);
17961 	if (ipif_v6 != NULL)
17962 		ipif_refrele(ipif_v6);
17963 	return (err);
17964 }
17965 
17966 /*
17967  * FAILOVER and FAILBACK are modelled as MOVE operations.
17968  *
17969  * We don't check whether the MOVE is within the same group or
17970  * not, because this ioctl can be used as a generic mechanism
17971  * to failover from interface A to B, though things will function
17972  * only if they are really part of the same group. Moreover,
17973  * all ipifs may be down and hence temporarily out of the group.
17974  *
17975  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17976  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17977  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17978  * have been deleted and there are no active references. Once quiescent the
17979  * ipif's are moved and brought up on the new ill.
17980  *
17981  * Normally the source ill and destination ill belong to the same IPMP group
17982  * and hence the same ipsq_t. In the event they don't belong to the same
17983  * same group the two ipsq's are first merged into one ipsq - that of the
17984  * to_ill. The multicast memberships on the source and destination ill cannot
17985  * change during the move operation since multicast joins/leaves also have to
17986  * execute on the same ipsq and are hence serialized.
17987  */
17988 /* ARGSUSED */
17989 int
17990 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17991     ip_ioctl_cmd_t *ipip, void *ifreq)
17992 {
17993 	ill_t *ill_to_v4 = NULL;
17994 	ill_t *ill_to_v6 = NULL;
17995 	ill_t *ill_from_v4 = NULL;
17996 	ill_t *ill_from_v6 = NULL;
17997 	int err = 0;
17998 
17999 	/*
18000 	 * setup from and to ill's, we can get EINPROGRESS only for
18001 	 * to_ill's.
18002 	 */
18003 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
18004 	    &ill_to_v4, &ill_to_v6);
18005 
18006 	if (err != 0) {
18007 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
18008 		goto done;
18009 	}
18010 
18011 	/*
18012 	 * nothing to do.
18013 	 */
18014 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
18015 		goto done;
18016 	}
18017 
18018 	/*
18019 	 * nothing to do.
18020 	 */
18021 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
18022 		goto done;
18023 	}
18024 
18025 	/*
18026 	 * Mark the ill as changing.
18027 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
18028 	 * in ill_up_ipifs in case of error they are cleared below.
18029 	 */
18030 
18031 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18032 	if (ill_from_v4 != NULL)
18033 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
18034 	if (ill_from_v6 != NULL)
18035 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
18036 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18037 
18038 	/*
18039 	 * Make sure that both src and dst are
18040 	 * in the same syncq group. If not make it happen.
18041 	 * We are not holding any locks because we are the writer
18042 	 * on the from_ipsq and we will hold locks in ill_merge_groups
18043 	 * to protect to_ipsq against changing.
18044 	 */
18045 	if (ill_from_v4 != NULL) {
18046 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
18047 		    ill_to_v4->ill_phyint->phyint_ipsq) {
18048 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
18049 			    NULL, mp, q);
18050 			goto err_ret;
18051 
18052 		}
18053 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
18054 	} else {
18055 
18056 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
18057 		    ill_to_v6->ill_phyint->phyint_ipsq) {
18058 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
18059 			    NULL, mp, q);
18060 			goto err_ret;
18061 
18062 		}
18063 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
18064 	}
18065 
18066 	/*
18067 	 * Now that the ipsq's have been merged and we are the writer
18068 	 * lets mark to_ill as changing as well.
18069 	 */
18070 
18071 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18072 	if (ill_to_v4 != NULL)
18073 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
18074 	if (ill_to_v6 != NULL)
18075 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
18076 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18077 
18078 	/*
18079 	 * Its ok for us to proceed with the move even if
18080 	 * ill_pending_mp is non null on one of the from ill's as the reply
18081 	 * should not be looking at the ipif, it should only care about the
18082 	 * ill itself.
18083 	 */
18084 
18085 	/*
18086 	 * lets move ipv4 first.
18087 	 */
18088 	if (ill_from_v4 != NULL) {
18089 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18090 		ill_from_v4->ill_move_in_progress = B_TRUE;
18091 		ill_to_v4->ill_move_in_progress = B_TRUE;
18092 		ill_to_v4->ill_move_peer = ill_from_v4;
18093 		ill_from_v4->ill_move_peer = ill_to_v4;
18094 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18095 	}
18096 
18097 	/*
18098 	 * Now lets move ipv6.
18099 	 */
18100 	if (err == 0 && ill_from_v6 != NULL) {
18101 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18102 		ill_from_v6->ill_move_in_progress = B_TRUE;
18103 		ill_to_v6->ill_move_in_progress = B_TRUE;
18104 		ill_to_v6->ill_move_peer = ill_from_v6;
18105 		ill_from_v6->ill_move_peer = ill_to_v6;
18106 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18107 	}
18108 
18109 err_ret:
18110 	/*
18111 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18112 	 * moved to become quiescent.
18113 	 */
18114 	if (err == EINPROGRESS) {
18115 		goto done;
18116 	}
18117 
18118 	/*
18119 	 * if err is set ill_up_ipifs will not be called
18120 	 * lets clear the flags.
18121 	 */
18122 
18123 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18124 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18125 	/*
18126 	 * Some of the clearing may be redundant. But it is simple
18127 	 * not making any extra checks.
18128 	 */
18129 	if (ill_from_v6 != NULL) {
18130 		ill_from_v6->ill_move_in_progress = B_FALSE;
18131 		ill_from_v6->ill_move_peer = NULL;
18132 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18133 	}
18134 	if (ill_from_v4 != NULL) {
18135 		ill_from_v4->ill_move_in_progress = B_FALSE;
18136 		ill_from_v4->ill_move_peer = NULL;
18137 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18138 	}
18139 	if (ill_to_v6 != NULL) {
18140 		ill_to_v6->ill_move_in_progress = B_FALSE;
18141 		ill_to_v6->ill_move_peer = NULL;
18142 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18143 	}
18144 	if (ill_to_v4 != NULL) {
18145 		ill_to_v4->ill_move_in_progress = B_FALSE;
18146 		ill_to_v4->ill_move_peer = NULL;
18147 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18148 	}
18149 
18150 	/*
18151 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18152 	 * Do this always to maintain proper state i.e even in case of errors.
18153 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18154 	 * we need not call on both v4 and v6 interfaces.
18155 	 */
18156 	if (ill_from_v4 != NULL) {
18157 		if ((ill_from_v4->ill_phyint->phyint_flags &
18158 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18159 			phyint_inactive(ill_from_v4->ill_phyint);
18160 		}
18161 	} else if (ill_from_v6 != NULL) {
18162 		if ((ill_from_v6->ill_phyint->phyint_flags &
18163 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18164 			phyint_inactive(ill_from_v6->ill_phyint);
18165 		}
18166 	}
18167 
18168 	if (ill_to_v4 != NULL) {
18169 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18170 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18171 		}
18172 	} else if (ill_to_v6 != NULL) {
18173 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18174 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18175 		}
18176 	}
18177 
18178 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18179 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18180 
18181 no_err:
18182 	/*
18183 	 * lets bring the interfaces up on the to_ill.
18184 	 */
18185 	if (err == 0) {
18186 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18187 		    q, mp);
18188 	}
18189 
18190 	if (err == 0) {
18191 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18192 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18193 
18194 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18195 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18196 	}
18197 done:
18198 
18199 	if (ill_to_v4 != NULL) {
18200 		ill_refrele(ill_to_v4);
18201 	}
18202 	if (ill_to_v6 != NULL) {
18203 		ill_refrele(ill_to_v6);
18204 	}
18205 
18206 	return (err);
18207 }
18208 
18209 static void
18210 ill_dl_down(ill_t *ill)
18211 {
18212 	/*
18213 	 * The ill is down; unbind but stay attached since we're still
18214 	 * associated with a PPA. If we have negotiated DLPI capabilites
18215 	 * with the data link service provider (IDS_OK) then reset them.
18216 	 * The interval between unbinding and rebinding is potentially
18217 	 * unbounded hence we cannot assume things will be the same.
18218 	 * The DLPI capabilities will be probed again when the data link
18219 	 * is brought up.
18220 	 */
18221 	mblk_t	*mp = ill->ill_unbind_mp;
18222 
18223 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18224 
18225 	ill->ill_unbind_mp = NULL;
18226 	if (mp != NULL) {
18227 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18228 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18229 		    ill->ill_name));
18230 		mutex_enter(&ill->ill_lock);
18231 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18232 		mutex_exit(&ill->ill_lock);
18233 		/*
18234 		 * Reset the capabilities if the negotiation is done or is
18235 		 * still in progress. Note that ill_capability_reset() will
18236 		 * set ill_dlpi_capab_state to IDS_UNKNOWN, so the subsequent
18237 		 * DL_CAPABILITY_ACK and DL_NOTE_CAPAB_RENEG will be ignored.
18238 		 *
18239 		 * Further, reset ill_capab_reneg to be B_FALSE so that the
18240 		 * subsequent DL_CAPABILITY_ACK can be ignored, to prevent
18241 		 * the capabilities renegotiation from happening.
18242 		 */
18243 		if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
18244 			ill_capability_reset(ill);
18245 		ill->ill_capab_reneg = B_FALSE;
18246 
18247 		ill_dlpi_send(ill, mp);
18248 	}
18249 
18250 	/*
18251 	 * Toss all of our multicast memberships.  We could keep them, but
18252 	 * then we'd have to do bookkeeping of any joins and leaves performed
18253 	 * by the application while the the interface is down (we can't just
18254 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18255 	 * on a downed interface).
18256 	 */
18257 	ill_leave_multicast(ill);
18258 
18259 	mutex_enter(&ill->ill_lock);
18260 	ill->ill_dl_up = 0;
18261 	(void) ill_hook_event_create(ill, 0, NE_DOWN, NULL, 0);
18262 	mutex_exit(&ill->ill_lock);
18263 }
18264 
18265 static void
18266 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18267 {
18268 	union DL_primitives *dlp;
18269 	t_uscalar_t prim;
18270 
18271 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18272 
18273 	dlp = (union DL_primitives *)mp->b_rptr;
18274 	prim = dlp->dl_primitive;
18275 
18276 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18277 	    dl_primstr(prim), prim, ill->ill_name));
18278 
18279 	switch (prim) {
18280 	case DL_PHYS_ADDR_REQ:
18281 	{
18282 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18283 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18284 		break;
18285 	}
18286 	case DL_BIND_REQ:
18287 		mutex_enter(&ill->ill_lock);
18288 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18289 		mutex_exit(&ill->ill_lock);
18290 		break;
18291 	}
18292 
18293 	/*
18294 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18295 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18296 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18297 	 */
18298 	mutex_enter(&ill->ill_lock);
18299 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18300 	    (prim == DL_UNBIND_REQ)) {
18301 		ill->ill_dlpi_pending = prim;
18302 	}
18303 	mutex_exit(&ill->ill_lock);
18304 
18305 	putnext(ill->ill_wq, mp);
18306 }
18307 
18308 /*
18309  * Helper function for ill_dlpi_send().
18310  */
18311 /* ARGSUSED */
18312 static void
18313 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18314 {
18315 	ill_dlpi_send(q->q_ptr, mp);
18316 }
18317 
18318 /*
18319  * Send a DLPI control message to the driver but make sure there
18320  * is only one outstanding message. Uses ill_dlpi_pending to tell
18321  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18322  * when an ACK or a NAK is received to process the next queued message.
18323  */
18324 void
18325 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18326 {
18327 	mblk_t **mpp;
18328 
18329 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18330 
18331 	/*
18332 	 * To ensure that any DLPI requests for current exclusive operation
18333 	 * are always completely sent before any DLPI messages for other
18334 	 * operations, require writer access before enqueuing.
18335 	 */
18336 	if (!IAM_WRITER_ILL(ill)) {
18337 		ill_refhold(ill);
18338 		/* qwriter_ip() does the ill_refrele() */
18339 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18340 		    NEW_OP, B_TRUE);
18341 		return;
18342 	}
18343 
18344 	mutex_enter(&ill->ill_lock);
18345 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18346 		/* Must queue message. Tail insertion */
18347 		mpp = &ill->ill_dlpi_deferred;
18348 		while (*mpp != NULL)
18349 			mpp = &((*mpp)->b_next);
18350 
18351 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18352 		    ill->ill_name));
18353 
18354 		*mpp = mp;
18355 		mutex_exit(&ill->ill_lock);
18356 		return;
18357 	}
18358 	mutex_exit(&ill->ill_lock);
18359 	ill_dlpi_dispatch(ill, mp);
18360 }
18361 
18362 /*
18363  * Send all deferred DLPI messages without waiting for their ACKs.
18364  */
18365 void
18366 ill_dlpi_send_deferred(ill_t *ill)
18367 {
18368 	mblk_t *mp, *nextmp;
18369 
18370 	/*
18371 	 * Clear ill_dlpi_pending so that the message is not queued in
18372 	 * ill_dlpi_send().
18373 	 */
18374 	mutex_enter(&ill->ill_lock);
18375 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18376 	mp = ill->ill_dlpi_deferred;
18377 	ill->ill_dlpi_deferred = NULL;
18378 	mutex_exit(&ill->ill_lock);
18379 
18380 	for (; mp != NULL; mp = nextmp) {
18381 		nextmp = mp->b_next;
18382 		mp->b_next = NULL;
18383 		ill_dlpi_send(ill, mp);
18384 	}
18385 }
18386 
18387 /*
18388  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18389  */
18390 boolean_t
18391 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18392 {
18393 	t_uscalar_t pending;
18394 
18395 	mutex_enter(&ill->ill_lock);
18396 	if (ill->ill_dlpi_pending == prim) {
18397 		mutex_exit(&ill->ill_lock);
18398 		return (B_TRUE);
18399 	}
18400 
18401 	/*
18402 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
18403 	 * without waiting, so don't print any warnings in that case.
18404 	 */
18405 	if (ill->ill_state_flags & ILL_CONDEMNED) {
18406 		mutex_exit(&ill->ill_lock);
18407 		return (B_FALSE);
18408 	}
18409 	pending = ill->ill_dlpi_pending;
18410 	mutex_exit(&ill->ill_lock);
18411 
18412 	if (pending == DL_PRIM_INVAL) {
18413 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18414 		    "received unsolicited ack for %s on %s\n",
18415 		    dl_primstr(prim), ill->ill_name);
18416 	} else {
18417 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18418 		    "received unexpected ack for %s on %s (expecting %s)\n",
18419 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
18420 	}
18421 	return (B_FALSE);
18422 }
18423 
18424 /*
18425  * Complete the current DLPI operation associated with `prim' on `ill' and
18426  * start the next queued DLPI operation (if any).  If there are no queued DLPI
18427  * operations and the ill's current exclusive IPSQ operation has finished
18428  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
18429  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
18430  * the comments above ipsq_current_finish() for details.
18431  */
18432 void
18433 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18434 {
18435 	mblk_t *mp;
18436 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
18437 
18438 	ASSERT(IAM_WRITER_IPSQ(ipsq));
18439 	mutex_enter(&ill->ill_lock);
18440 
18441 	ASSERT(prim != DL_PRIM_INVAL);
18442 	ASSERT(ill->ill_dlpi_pending == prim);
18443 
18444 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18445 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18446 
18447 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18448 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18449 
18450 		mutex_enter(&ipsq->ipsq_lock);
18451 		if (ipsq->ipsq_current_done)
18452 			ipsq->ipsq_current_ipif = NULL;
18453 		mutex_exit(&ipsq->ipsq_lock);
18454 
18455 		cv_signal(&ill->ill_cv);
18456 		mutex_exit(&ill->ill_lock);
18457 		return;
18458 	}
18459 
18460 	ill->ill_dlpi_deferred = mp->b_next;
18461 	mp->b_next = NULL;
18462 	mutex_exit(&ill->ill_lock);
18463 
18464 	ill_dlpi_dispatch(ill, mp);
18465 }
18466 
18467 void
18468 conn_delete_ire(conn_t *connp, caddr_t arg)
18469 {
18470 	ipif_t	*ipif = (ipif_t *)arg;
18471 	ire_t	*ire;
18472 
18473 	/*
18474 	 * Look at the cached ires on conns which has pointers to ipifs.
18475 	 * We just call ire_refrele which clears up the reference
18476 	 * to ire. Called when a conn closes. Also called from ipif_free
18477 	 * to cleanup indirect references to the stale ipif via the cached ire.
18478 	 */
18479 	mutex_enter(&connp->conn_lock);
18480 	ire = connp->conn_ire_cache;
18481 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18482 		connp->conn_ire_cache = NULL;
18483 		mutex_exit(&connp->conn_lock);
18484 		IRE_REFRELE_NOTR(ire);
18485 		return;
18486 	}
18487 	mutex_exit(&connp->conn_lock);
18488 
18489 }
18490 
18491 /*
18492  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18493  * of IREs. Those IREs may have been previously cached in the conn structure.
18494  * This ipcl_walk() walker function releases all references to such IREs based
18495  * on the condemned flag.
18496  */
18497 /* ARGSUSED */
18498 void
18499 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18500 {
18501 	ire_t	*ire;
18502 
18503 	mutex_enter(&connp->conn_lock);
18504 	ire = connp->conn_ire_cache;
18505 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18506 		connp->conn_ire_cache = NULL;
18507 		mutex_exit(&connp->conn_lock);
18508 		IRE_REFRELE_NOTR(ire);
18509 		return;
18510 	}
18511 	mutex_exit(&connp->conn_lock);
18512 }
18513 
18514 /*
18515  * Take down a specific interface, but don't lose any information about it.
18516  * Also delete interface from its interface group (ifgrp).
18517  * (Always called as writer.)
18518  * This function goes through the down sequence even if the interface is
18519  * already down. There are 2 reasons.
18520  * a. Currently we permit interface routes that depend on down interfaces
18521  *    to be added. This behaviour itself is questionable. However it appears
18522  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18523  *    time. We go thru the cleanup in order to remove these routes.
18524  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18525  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18526  *    down, but we need to cleanup i.e. do ill_dl_down and
18527  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18528  *
18529  * IP-MT notes:
18530  *
18531  * Model of reference to interfaces.
18532  *
18533  * The following members in ipif_t track references to the ipif.
18534  *	int     ipif_refcnt;    Active reference count
18535  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18536  *	uint_t  ipif_ilm_cnt;   Number of ilms's references this ipif.
18537  *
18538  * The following members in ill_t track references to the ill.
18539  *	int             ill_refcnt;     active refcnt
18540  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18541  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18542  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
18543  *
18544  * Reference to an ipif or ill can be obtained in any of the following ways.
18545  *
18546  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18547  * Pointers to ipif / ill from other data structures viz ire and conn.
18548  * Implicit reference to the ipif / ill by holding a reference to the ire.
18549  *
18550  * The ipif/ill lookup functions return a reference held ipif / ill.
18551  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18552  * This is a purely dynamic reference count associated with threads holding
18553  * references to the ipif / ill. Pointers from other structures do not
18554  * count towards this reference count.
18555  *
18556  * ipif_ire_cnt/ill_ire_cnt is the number of ire's
18557  * associated with the ipif/ill. This is incremented whenever a new
18558  * ire is created referencing the ipif/ill. This is done atomically inside
18559  * ire_add_v[46] where the ire is actually added to the ire hash table.
18560  * The count is decremented in ire_inactive where the ire is destroyed.
18561  *
18562  * nce's reference ill's thru nce_ill and the count of nce's associated with
18563  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18564  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18565  * table. Similarly it is decremented in ndp_inactive() where the nce
18566  * is destroyed.
18567  *
18568  * ilm's reference to the ipif (for IPv4 ilm's) or the ill (for IPv6 ilm's)
18569  * is incremented in ilm_add_v6() and decremented before the ilm is freed
18570  * in ilm_walker_cleanup() or ilm_delete().
18571  *
18572  * Flow of ioctls involving interface down/up
18573  *
18574  * The following is the sequence of an attempt to set some critical flags on an
18575  * up interface.
18576  * ip_sioctl_flags
18577  * ipif_down
18578  * wait for ipif to be quiescent
18579  * ipif_down_tail
18580  * ip_sioctl_flags_tail
18581  *
18582  * All set ioctls that involve down/up sequence would have a skeleton similar
18583  * to the above. All the *tail functions are called after the refcounts have
18584  * dropped to the appropriate values.
18585  *
18586  * The mechanism to quiesce an ipif is as follows.
18587  *
18588  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18589  * on the ipif. Callers either pass a flag requesting wait or the lookup
18590  *  functions will return NULL.
18591  *
18592  * Delete all ires referencing this ipif
18593  *
18594  * Any thread attempting to do an ipif_refhold on an ipif that has been
18595  * obtained thru a cached pointer will first make sure that
18596  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18597  * increment the refcount.
18598  *
18599  * The above guarantees that the ipif refcount will eventually come down to
18600  * zero and the ipif will quiesce, once all threads that currently hold a
18601  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18602  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18603  * have also been ire_inactive'd. i.e. when ipif_{ire, ill}_cnt and
18604  * ipif_refcnt both drop to zero. See also: comments above IPIF_DOWN_OK()
18605  * in ip.h
18606  *
18607  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18608  *
18609  * Threads trying to lookup an ipif or ill can pass a flag requesting
18610  * wait and restart if the ipif / ill cannot be looked up currently.
18611  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18612  * failure if the ipif is currently undergoing an exclusive operation, and
18613  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18614  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18615  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18616  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18617  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18618  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18619  * until we release the ipsq_lock, even though the the ill/ipif state flags
18620  * can change after we drop the ill_lock.
18621  *
18622  * An attempt to send out a packet using an ipif that is currently
18623  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18624  * operation and restart it later when the exclusive condition on the ipif ends.
18625  * This is an example of not passing the wait flag to the lookup functions. For
18626  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18627  * out a multicast packet on that ipif will fail while the ipif is
18628  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18629  * currently IPIF_CHANGING will also fail.
18630  */
18631 int
18632 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18633 {
18634 	ill_t		*ill = ipif->ipif_ill;
18635 	phyint_t	*phyi;
18636 	conn_t		*connp;
18637 	boolean_t	success;
18638 	boolean_t	ipif_was_up = B_FALSE;
18639 	ip_stack_t	*ipst = ill->ill_ipst;
18640 
18641 	ASSERT(IAM_WRITER_IPIF(ipif));
18642 
18643 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18644 
18645 	if (ipif->ipif_flags & IPIF_UP) {
18646 		mutex_enter(&ill->ill_lock);
18647 		ipif->ipif_flags &= ~IPIF_UP;
18648 		ASSERT(ill->ill_ipif_up_count > 0);
18649 		--ill->ill_ipif_up_count;
18650 		mutex_exit(&ill->ill_lock);
18651 		ipif_was_up = B_TRUE;
18652 		/* Update status in SCTP's list */
18653 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18654 	}
18655 
18656 	/*
18657 	 * Blow away memberships we established in ipif_multicast_up().
18658 	 */
18659 	ipif_multicast_down(ipif);
18660 
18661 	/*
18662 	 * Remove from the mapping for __sin6_src_id. We insert only
18663 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18664 	 * stored as mapped addresses, we need to check for mapped
18665 	 * INADDR_ANY also.
18666 	 */
18667 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18668 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18669 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18670 		int err;
18671 
18672 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18673 		    ipif->ipif_zoneid, ipst);
18674 		if (err != 0) {
18675 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18676 		}
18677 	}
18678 
18679 	/*
18680 	 * Before we delete the ill from the group (if any), we need
18681 	 * to make sure that we delete all the routes dependent on
18682 	 * this and also any ipifs dependent on this ipif for
18683 	 * source address. We need to do before we delete from
18684 	 * the group because
18685 	 *
18686 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18687 	 *
18688 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18689 	 *    for re-doing source address selection. Note that
18690 	 *    ipif_select_source[_v6] called from
18691 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18692 	 *    because we have already marked down here i.e cleared
18693 	 *    IPIF_UP.
18694 	 */
18695 	if (ipif->ipif_isv6) {
18696 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18697 		    ipst);
18698 	} else {
18699 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18700 		    ipst);
18701 	}
18702 
18703 	/*
18704 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18705 	 * ires have been deleted above. Otherwise a thread could end up
18706 	 * caching an ire in a conn after we have finished the cleanup of the
18707 	 * conn. The caching is done after making sure that the ire is not yet
18708 	 * condemned. Also documented in the block comment above ip_output
18709 	 */
18710 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18711 	/* Also, delete the ires cached in SCTP */
18712 	sctp_ire_cache_flush(ipif);
18713 
18714 	/*
18715 	 * Update any other ipifs which have used "our" local address as
18716 	 * a source address. This entails removing and recreating IRE_INTERFACE
18717 	 * entries for such ipifs.
18718 	 */
18719 	if (ipif->ipif_isv6)
18720 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18721 	else
18722 		ipif_update_other_ipifs(ipif, ill->ill_group);
18723 
18724 	if (ipif_was_up) {
18725 		/*
18726 		 * Check whether it is last ipif to leave this group.
18727 		 * If this is the last ipif to leave, we should remove
18728 		 * this ill from the group as ipif_select_source will not
18729 		 * be able to find any useful ipifs if this ill is selected
18730 		 * for load balancing.
18731 		 *
18732 		 * For nameless groups, we should call ifgrp_delete if this
18733 		 * belongs to some group. As this ipif is going down, we may
18734 		 * need to reconstruct groups.
18735 		 */
18736 		phyi = ill->ill_phyint;
18737 		/*
18738 		 * If the phyint_groupname_len is 0, it may or may not
18739 		 * be in the nameless group. If the phyint_groupname_len is
18740 		 * not 0, then this ill should be part of some group.
18741 		 * As we always insert this ill in the group if
18742 		 * phyint_groupname_len is not zero when the first ipif
18743 		 * comes up (in ipif_up_done), it should be in a group
18744 		 * when the namelen is not 0.
18745 		 *
18746 		 * NOTE : When we delete the ill from the group,it will
18747 		 * blow away all the IRE_CACHES pointing either at this ipif or
18748 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18749 		 * should be pointing at this ill.
18750 		 */
18751 		ASSERT(phyi->phyint_groupname_len == 0 ||
18752 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18753 
18754 		if (phyi->phyint_groupname_len != 0) {
18755 			if (ill->ill_ipif_up_count == 0)
18756 				illgrp_delete(ill);
18757 		}
18758 
18759 		/*
18760 		 * If we have deleted some of the broadcast ires associated
18761 		 * with this ipif, we need to re-nominate somebody else if
18762 		 * the ires that we deleted were the nominated ones.
18763 		 */
18764 		if (ill->ill_group != NULL && !ill->ill_isv6)
18765 			ipif_renominate_bcast(ipif);
18766 	}
18767 
18768 	/*
18769 	 * neighbor-discovery or arp entries for this interface.
18770 	 */
18771 	ipif_ndp_down(ipif);
18772 
18773 	/*
18774 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18775 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18776 	 * and ill_delete -> ipif_free -> ipif_down
18777 	 */
18778 	if (mp == NULL) {
18779 		ASSERT(q == NULL);
18780 		return (0);
18781 	}
18782 
18783 	if (CONN_Q(q)) {
18784 		connp = Q_TO_CONN(q);
18785 		mutex_enter(&connp->conn_lock);
18786 	} else {
18787 		connp = NULL;
18788 	}
18789 	mutex_enter(&ill->ill_lock);
18790 	/*
18791 	 * Are there any ire's pointing to this ipif that are still active ?
18792 	 * If this is the last ipif going down, are there any ire's pointing
18793 	 * to this ill that are still active ?
18794 	 */
18795 	if (ipif_is_quiescent(ipif)) {
18796 		mutex_exit(&ill->ill_lock);
18797 		if (connp != NULL)
18798 			mutex_exit(&connp->conn_lock);
18799 		return (0);
18800 	}
18801 
18802 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18803 	    ill->ill_name, (void *)ill));
18804 	/*
18805 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18806 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18807 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18808 	 */
18809 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18810 	if (!success) {
18811 		/* The conn is closing. So just return */
18812 		ASSERT(connp != NULL);
18813 		mutex_exit(&ill->ill_lock);
18814 		mutex_exit(&connp->conn_lock);
18815 		return (EINTR);
18816 	}
18817 
18818 	mutex_exit(&ill->ill_lock);
18819 	if (connp != NULL)
18820 		mutex_exit(&connp->conn_lock);
18821 	return (EINPROGRESS);
18822 }
18823 
18824 void
18825 ipif_down_tail(ipif_t *ipif)
18826 {
18827 	ill_t	*ill = ipif->ipif_ill;
18828 
18829 	/*
18830 	 * Skip any loopback interface (null wq).
18831 	 * If this is the last logical interface on the ill
18832 	 * have ill_dl_down tell the driver we are gone (unbind)
18833 	 * Note that lun 0 can ipif_down even though
18834 	 * there are other logical units that are up.
18835 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18836 	 */
18837 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18838 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18839 	    ill->ill_dl_up) {
18840 		ill_dl_down(ill);
18841 	}
18842 	ill->ill_logical_down = 0;
18843 
18844 	/*
18845 	 * Have to be after removing the routes in ipif_down_delete_ire.
18846 	 */
18847 	if (ipif->ipif_isv6) {
18848 		if (ill->ill_flags & ILLF_XRESOLV)
18849 			ipif_arp_down(ipif);
18850 	} else {
18851 		ipif_arp_down(ipif);
18852 	}
18853 
18854 	ip_rts_ifmsg(ipif);
18855 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18856 }
18857 
18858 /*
18859  * Bring interface logically down without bringing the physical interface
18860  * down e.g. when the netmask is changed. This avoids long lasting link
18861  * negotiations between an ethernet interface and a certain switches.
18862  */
18863 static int
18864 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18865 {
18866 	/*
18867 	 * The ill_logical_down flag is a transient flag. It is set here
18868 	 * and is cleared once the down has completed in ipif_down_tail.
18869 	 * This flag does not indicate whether the ill stream is in the
18870 	 * DL_BOUND state with the driver. Instead this flag is used by
18871 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18872 	 * the driver. The state of the ill stream i.e. whether it is
18873 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18874 	 */
18875 	ipif->ipif_ill->ill_logical_down = 1;
18876 	return (ipif_down(ipif, q, mp));
18877 }
18878 
18879 /*
18880  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18881  * If the usesrc client ILL is already part of a usesrc group or not,
18882  * in either case a ire_stq with the matching usesrc client ILL will
18883  * locate the IRE's that need to be deleted. We want IREs to be created
18884  * with the new source address.
18885  */
18886 static void
18887 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18888 {
18889 	ill_t	*ucill = (ill_t *)ill_arg;
18890 
18891 	ASSERT(IAM_WRITER_ILL(ucill));
18892 
18893 	if (ire->ire_stq == NULL)
18894 		return;
18895 
18896 	if ((ire->ire_type == IRE_CACHE) &&
18897 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18898 		ire_delete(ire);
18899 }
18900 
18901 /*
18902  * ire_walk routine to delete every IRE dependent on the interface
18903  * address that is going down.	(Always called as writer.)
18904  * Works for both v4 and v6.
18905  * In addition for checking for ire_ipif matches it also checks for
18906  * IRE_CACHE entries which have the same source address as the
18907  * disappearing ipif since ipif_select_source might have picked
18908  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18909  * care of any IRE_INTERFACE with the disappearing source address.
18910  */
18911 static void
18912 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18913 {
18914 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18915 	ill_t *ire_ill;
18916 	ill_t *ipif_ill;
18917 
18918 	ASSERT(IAM_WRITER_IPIF(ipif));
18919 	if (ire->ire_ipif == NULL)
18920 		return;
18921 
18922 	/*
18923 	 * For IPv4, we derive source addresses for an IRE from ipif's
18924 	 * belonging to the same IPMP group as the IRE's outgoing
18925 	 * interface.  If an IRE's outgoing interface isn't in the
18926 	 * same IPMP group as a particular ipif, then that ipif
18927 	 * couldn't have been used as a source address for this IRE.
18928 	 *
18929 	 * For IPv6, source addresses are only restricted to the IPMP group
18930 	 * if the IRE is for a link-local address or a multicast address.
18931 	 * Otherwise, source addresses for an IRE can be chosen from
18932 	 * interfaces other than the the outgoing interface for that IRE.
18933 	 *
18934 	 * For source address selection details, see ipif_select_source()
18935 	 * and ipif_select_source_v6().
18936 	 */
18937 	if (ire->ire_ipversion == IPV4_VERSION ||
18938 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18939 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18940 		ire_ill = ire->ire_ipif->ipif_ill;
18941 		ipif_ill = ipif->ipif_ill;
18942 
18943 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18944 			return;
18945 		}
18946 	}
18947 
18948 	if (ire->ire_ipif != ipif) {
18949 		/*
18950 		 * Look for a matching source address.
18951 		 */
18952 		if (ire->ire_type != IRE_CACHE)
18953 			return;
18954 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18955 			return;
18956 
18957 		if (ire->ire_ipversion == IPV4_VERSION) {
18958 			if (ire->ire_src_addr != ipif->ipif_src_addr)
18959 				return;
18960 		} else {
18961 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
18962 			    &ipif->ipif_v6lcl_addr))
18963 				return;
18964 		}
18965 		ire_delete(ire);
18966 		return;
18967 	}
18968 	/*
18969 	 * ire_delete() will do an ire_flush_cache which will delete
18970 	 * all ire_ipif matches
18971 	 */
18972 	ire_delete(ire);
18973 }
18974 
18975 /*
18976  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
18977  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
18978  * 2) when an interface is brought up or down (on that ill).
18979  * This ensures that the IRE_CACHE entries don't retain stale source
18980  * address selection results.
18981  */
18982 void
18983 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
18984 {
18985 	ill_t	*ill = (ill_t *)ill_arg;
18986 	ill_t	*ipif_ill;
18987 
18988 	ASSERT(IAM_WRITER_ILL(ill));
18989 	/*
18990 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18991 	 * Hence this should be IRE_CACHE.
18992 	 */
18993 	ASSERT(ire->ire_type == IRE_CACHE);
18994 
18995 	/*
18996 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
18997 	 * We are only interested in IRE_CACHES that has borrowed
18998 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
18999 	 * for which we need to look at ire_ipif->ipif_ill match
19000 	 * with ill.
19001 	 */
19002 	ASSERT(ire->ire_ipif != NULL);
19003 	ipif_ill = ire->ire_ipif->ipif_ill;
19004 	if (ipif_ill == ill || (ill->ill_group != NULL &&
19005 	    ipif_ill->ill_group == ill->ill_group)) {
19006 		ire_delete(ire);
19007 	}
19008 }
19009 
19010 /*
19011  * Delete all the ire whose stq references ill_arg.
19012  */
19013 static void
19014 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
19015 {
19016 	ill_t	*ill = (ill_t *)ill_arg;
19017 	ill_t	*ire_ill;
19018 
19019 	ASSERT(IAM_WRITER_ILL(ill));
19020 	/*
19021 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19022 	 * Hence this should be IRE_CACHE.
19023 	 */
19024 	ASSERT(ire->ire_type == IRE_CACHE);
19025 
19026 	/*
19027 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19028 	 * matches ill. We are only interested in IRE_CACHES that
19029 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
19030 	 * filtering here.
19031 	 */
19032 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
19033 
19034 	if (ire_ill == ill)
19035 		ire_delete(ire);
19036 }
19037 
19038 /*
19039  * This is called when an ill leaves the group. We want to delete
19040  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
19041  * pointing at ill.
19042  */
19043 static void
19044 illgrp_cache_delete(ire_t *ire, char *ill_arg)
19045 {
19046 	ill_t	*ill = (ill_t *)ill_arg;
19047 
19048 	ASSERT(IAM_WRITER_ILL(ill));
19049 	ASSERT(ill->ill_group == NULL);
19050 	/*
19051 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19052 	 * Hence this should be IRE_CACHE.
19053 	 */
19054 	ASSERT(ire->ire_type == IRE_CACHE);
19055 	/*
19056 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19057 	 * matches ill. We are interested in both.
19058 	 */
19059 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
19060 	    (ire->ire_ipif->ipif_ill == ill));
19061 
19062 	ire_delete(ire);
19063 }
19064 
19065 /*
19066  * Initiate deallocate of an IPIF. Always called as writer. Called by
19067  * ill_delete or ip_sioctl_removeif.
19068  */
19069 static void
19070 ipif_free(ipif_t *ipif)
19071 {
19072 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19073 
19074 	ASSERT(IAM_WRITER_IPIF(ipif));
19075 
19076 	if (ipif->ipif_recovery_id != 0)
19077 		(void) untimeout(ipif->ipif_recovery_id);
19078 	ipif->ipif_recovery_id = 0;
19079 
19080 	/* Remove conn references */
19081 	reset_conn_ipif(ipif);
19082 
19083 	/*
19084 	 * Make sure we have valid net and subnet broadcast ire's for the
19085 	 * other ipif's which share them with this ipif.
19086 	 */
19087 	if (!ipif->ipif_isv6)
19088 		ipif_check_bcast_ires(ipif);
19089 
19090 	/*
19091 	 * Take down the interface. We can be called either from ill_delete
19092 	 * or from ip_sioctl_removeif.
19093 	 */
19094 	(void) ipif_down(ipif, NULL, NULL);
19095 
19096 	/*
19097 	 * Now that the interface is down, there's no chance it can still
19098 	 * become a duplicate.  Cancel any timer that may have been set while
19099 	 * tearing down.
19100 	 */
19101 	if (ipif->ipif_recovery_id != 0)
19102 		(void) untimeout(ipif->ipif_recovery_id);
19103 	ipif->ipif_recovery_id = 0;
19104 
19105 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19106 	/* Remove pointers to this ill in the multicast routing tables */
19107 	reset_mrt_vif_ipif(ipif);
19108 	rw_exit(&ipst->ips_ill_g_lock);
19109 }
19110 
19111 /*
19112  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19113  * also ill_move().
19114  */
19115 static void
19116 ipif_free_tail(ipif_t *ipif)
19117 {
19118 	mblk_t	*mp;
19119 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19120 
19121 	/*
19122 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19123 	 */
19124 	mutex_enter(&ipif->ipif_saved_ire_lock);
19125 	mp = ipif->ipif_saved_ire_mp;
19126 	ipif->ipif_saved_ire_mp = NULL;
19127 	mutex_exit(&ipif->ipif_saved_ire_lock);
19128 	freemsg(mp);
19129 
19130 	/*
19131 	 * Need to hold both ill_g_lock and ill_lock while
19132 	 * inserting or removing an ipif from the linked list
19133 	 * of ipifs hanging off the ill.
19134 	 */
19135 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19136 
19137 	ASSERT(ilm_walk_ipif(ipif) == 0);
19138 
19139 #ifdef DEBUG
19140 	ipif_trace_cleanup(ipif);
19141 #endif
19142 
19143 	/* Ask SCTP to take it out of it list */
19144 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19145 
19146 	/* Get it out of the ILL interface list. */
19147 	ipif_remove(ipif, B_TRUE);
19148 	rw_exit(&ipst->ips_ill_g_lock);
19149 
19150 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19151 
19152 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19153 	ASSERT(ipif->ipif_recovery_id == 0);
19154 
19155 	/* Free the memory. */
19156 	mi_free(ipif);
19157 }
19158 
19159 /*
19160  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
19161  * is zero.
19162  */
19163 void
19164 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19165 {
19166 	char	lbuf[LIFNAMSIZ];
19167 	char	*name;
19168 	size_t	name_len;
19169 
19170 	buf[0] = '\0';
19171 	name = ipif->ipif_ill->ill_name;
19172 	name_len = ipif->ipif_ill->ill_name_length;
19173 	if (ipif->ipif_id != 0) {
19174 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19175 		    ipif->ipif_id);
19176 		name = lbuf;
19177 		name_len = mi_strlen(name) + 1;
19178 	}
19179 	len -= 1;
19180 	buf[len] = '\0';
19181 	len = MIN(len, name_len);
19182 	bcopy(name, buf, len);
19183 }
19184 
19185 /*
19186  * Find an IPIF based on the name passed in.  Names can be of the
19187  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19188  * The <phys> string can have forms like <dev><#> (e.g., le0),
19189  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19190  * When there is no colon, the implied unit id is zero. <phys> must
19191  * correspond to the name of an ILL.  (May be called as writer.)
19192  */
19193 static ipif_t *
19194 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19195     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19196     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19197 {
19198 	char	*cp;
19199 	char	*endp;
19200 	long	id;
19201 	ill_t	*ill;
19202 	ipif_t	*ipif;
19203 	uint_t	ire_type;
19204 	boolean_t did_alloc = B_FALSE;
19205 	ipsq_t	*ipsq;
19206 
19207 	if (error != NULL)
19208 		*error = 0;
19209 
19210 	/*
19211 	 * If the caller wants to us to create the ipif, make sure we have a
19212 	 * valid zoneid
19213 	 */
19214 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19215 
19216 	if (namelen == 0) {
19217 		if (error != NULL)
19218 			*error = ENXIO;
19219 		return (NULL);
19220 	}
19221 
19222 	*exists = B_FALSE;
19223 	/* Look for a colon in the name. */
19224 	endp = &name[namelen];
19225 	for (cp = endp; --cp > name; ) {
19226 		if (*cp == IPIF_SEPARATOR_CHAR)
19227 			break;
19228 	}
19229 
19230 	if (*cp == IPIF_SEPARATOR_CHAR) {
19231 		/*
19232 		 * Reject any non-decimal aliases for logical
19233 		 * interfaces. Aliases with leading zeroes
19234 		 * are also rejected as they introduce ambiguity
19235 		 * in the naming of the interfaces.
19236 		 * In order to confirm with existing semantics,
19237 		 * and to not break any programs/script relying
19238 		 * on that behaviour, if<0>:0 is considered to be
19239 		 * a valid interface.
19240 		 *
19241 		 * If alias has two or more digits and the first
19242 		 * is zero, fail.
19243 		 */
19244 		if (&cp[2] < endp && cp[1] == '0') {
19245 			if (error != NULL)
19246 				*error = EINVAL;
19247 			return (NULL);
19248 		}
19249 	}
19250 
19251 	if (cp <= name) {
19252 		cp = endp;
19253 	} else {
19254 		*cp = '\0';
19255 	}
19256 
19257 	/*
19258 	 * Look up the ILL, based on the portion of the name
19259 	 * before the slash. ill_lookup_on_name returns a held ill.
19260 	 * Temporary to check whether ill exists already. If so
19261 	 * ill_lookup_on_name will clear it.
19262 	 */
19263 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19264 	    q, mp, func, error, &did_alloc, ipst);
19265 	if (cp != endp)
19266 		*cp = IPIF_SEPARATOR_CHAR;
19267 	if (ill == NULL)
19268 		return (NULL);
19269 
19270 	/* Establish the unit number in the name. */
19271 	id = 0;
19272 	if (cp < endp && *endp == '\0') {
19273 		/* If there was a colon, the unit number follows. */
19274 		cp++;
19275 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19276 			ill_refrele(ill);
19277 			if (error != NULL)
19278 				*error = ENXIO;
19279 			return (NULL);
19280 		}
19281 	}
19282 
19283 	GRAB_CONN_LOCK(q);
19284 	mutex_enter(&ill->ill_lock);
19285 	/* Now see if there is an IPIF with this unit number. */
19286 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19287 		if (ipif->ipif_id == id) {
19288 			if (zoneid != ALL_ZONES &&
19289 			    zoneid != ipif->ipif_zoneid &&
19290 			    ipif->ipif_zoneid != ALL_ZONES) {
19291 				mutex_exit(&ill->ill_lock);
19292 				RELEASE_CONN_LOCK(q);
19293 				ill_refrele(ill);
19294 				if (error != NULL)
19295 					*error = ENXIO;
19296 				return (NULL);
19297 			}
19298 			/*
19299 			 * The block comment at the start of ipif_down
19300 			 * explains the use of the macros used below
19301 			 */
19302 			if (IPIF_CAN_LOOKUP(ipif)) {
19303 				ipif_refhold_locked(ipif);
19304 				mutex_exit(&ill->ill_lock);
19305 				if (!did_alloc)
19306 					*exists = B_TRUE;
19307 				/*
19308 				 * Drop locks before calling ill_refrele
19309 				 * since it can potentially call into
19310 				 * ipif_ill_refrele_tail which can end up
19311 				 * in trying to acquire any lock.
19312 				 */
19313 				RELEASE_CONN_LOCK(q);
19314 				ill_refrele(ill);
19315 				return (ipif);
19316 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19317 				ipsq = ill->ill_phyint->phyint_ipsq;
19318 				mutex_enter(&ipsq->ipsq_lock);
19319 				mutex_exit(&ill->ill_lock);
19320 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19321 				mutex_exit(&ipsq->ipsq_lock);
19322 				RELEASE_CONN_LOCK(q);
19323 				ill_refrele(ill);
19324 				if (error != NULL)
19325 					*error = EINPROGRESS;
19326 				return (NULL);
19327 			}
19328 		}
19329 	}
19330 	RELEASE_CONN_LOCK(q);
19331 
19332 	if (!do_alloc) {
19333 		mutex_exit(&ill->ill_lock);
19334 		ill_refrele(ill);
19335 		if (error != NULL)
19336 			*error = ENXIO;
19337 		return (NULL);
19338 	}
19339 
19340 	/*
19341 	 * If none found, atomically allocate and return a new one.
19342 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19343 	 * to support "receive only" use of lo0:1 etc. as is still done
19344 	 * below as an initial guess.
19345 	 * However, this is now likely to be overriden later in ipif_up_done()
19346 	 * when we know for sure what address has been configured on the
19347 	 * interface, since we might have more than one loopback interface
19348 	 * with a loopback address, e.g. in the case of zones, and all the
19349 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19350 	 */
19351 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19352 		ire_type = IRE_LOOPBACK;
19353 	else
19354 		ire_type = IRE_LOCAL;
19355 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19356 	if (ipif != NULL)
19357 		ipif_refhold_locked(ipif);
19358 	else if (error != NULL)
19359 		*error = ENOMEM;
19360 	mutex_exit(&ill->ill_lock);
19361 	ill_refrele(ill);
19362 	return (ipif);
19363 }
19364 
19365 /*
19366  * This routine is called whenever a new address comes up on an ipif.  If
19367  * we are configured to respond to address mask requests, then we are supposed
19368  * to broadcast an address mask reply at this time.  This routine is also
19369  * called if we are already up, but a netmask change is made.  This is legal
19370  * but might not make the system manager very popular.	(May be called
19371  * as writer.)
19372  */
19373 void
19374 ipif_mask_reply(ipif_t *ipif)
19375 {
19376 	icmph_t	*icmph;
19377 	ipha_t	*ipha;
19378 	mblk_t	*mp;
19379 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19380 
19381 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19382 
19383 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19384 		return;
19385 
19386 	/* ICMP mask reply is IPv4 only */
19387 	ASSERT(!ipif->ipif_isv6);
19388 	/* ICMP mask reply is not for a loopback interface */
19389 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19390 
19391 	mp = allocb(REPLY_LEN, BPRI_HI);
19392 	if (mp == NULL)
19393 		return;
19394 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19395 
19396 	ipha = (ipha_t *)mp->b_rptr;
19397 	bzero(ipha, REPLY_LEN);
19398 	*ipha = icmp_ipha;
19399 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19400 	ipha->ipha_src = ipif->ipif_src_addr;
19401 	ipha->ipha_dst = ipif->ipif_brd_addr;
19402 	ipha->ipha_length = htons(REPLY_LEN);
19403 	ipha->ipha_ident = 0;
19404 
19405 	icmph = (icmph_t *)&ipha[1];
19406 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19407 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19408 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19409 
19410 	put(ipif->ipif_wq, mp);
19411 
19412 #undef	REPLY_LEN
19413 }
19414 
19415 /*
19416  * When the mtu in the ipif changes, we call this routine through ire_walk
19417  * to update all the relevant IREs.
19418  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19419  */
19420 static void
19421 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19422 {
19423 	ipif_t *ipif = (ipif_t *)ipif_arg;
19424 
19425 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19426 		return;
19427 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19428 }
19429 
19430 /*
19431  * When the mtu in the ill changes, we call this routine through ire_walk
19432  * to update all the relevant IREs.
19433  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19434  */
19435 void
19436 ill_mtu_change(ire_t *ire, char *ill_arg)
19437 {
19438 	ill_t	*ill = (ill_t *)ill_arg;
19439 
19440 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19441 		return;
19442 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19443 }
19444 
19445 /*
19446  * Join the ipif specific multicast groups.
19447  * Must be called after a mapping has been set up in the resolver.  (Always
19448  * called as writer.)
19449  */
19450 void
19451 ipif_multicast_up(ipif_t *ipif)
19452 {
19453 	int err, index;
19454 	ill_t *ill;
19455 
19456 	ASSERT(IAM_WRITER_IPIF(ipif));
19457 
19458 	ill = ipif->ipif_ill;
19459 	index = ill->ill_phyint->phyint_ifindex;
19460 
19461 	ip1dbg(("ipif_multicast_up\n"));
19462 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19463 		return;
19464 
19465 	if (ipif->ipif_isv6) {
19466 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19467 			return;
19468 
19469 		/* Join the all hosts multicast address */
19470 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19471 		/*
19472 		 * Passing B_TRUE means we have to join the multicast
19473 		 * membership on this interface even though this is
19474 		 * FAILED. If we join on a different one in the group,
19475 		 * we will not be able to delete the membership later
19476 		 * as we currently don't track where we join when we
19477 		 * join within the kernel unlike applications where
19478 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19479 		 * for more on this.
19480 		 */
19481 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19482 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19483 		if (err != 0) {
19484 			ip0dbg(("ipif_multicast_up: "
19485 			    "all_hosts_mcast failed %d\n",
19486 			    err));
19487 			return;
19488 		}
19489 		/*
19490 		 * Enable multicast for the solicited node multicast address
19491 		 */
19492 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19493 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19494 
19495 			ipv6_multi.s6_addr32[3] |=
19496 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19497 
19498 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19499 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19500 			    NULL);
19501 			if (err != 0) {
19502 				ip0dbg(("ipif_multicast_up: solicited MC"
19503 				    " failed %d\n", err));
19504 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19505 				    ill, ill->ill_phyint->phyint_ifindex,
19506 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19507 				return;
19508 			}
19509 		}
19510 	} else {
19511 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19512 			return;
19513 
19514 		/* Join the all hosts multicast address */
19515 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19516 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19517 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19518 		if (err) {
19519 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19520 			return;
19521 		}
19522 	}
19523 	ipif->ipif_multicast_up = 1;
19524 }
19525 
19526 /*
19527  * Blow away any multicast groups that we joined in ipif_multicast_up().
19528  * (Explicit memberships are blown away in ill_leave_multicast() when the
19529  * ill is brought down.)
19530  */
19531 static void
19532 ipif_multicast_down(ipif_t *ipif)
19533 {
19534 	int err;
19535 
19536 	ASSERT(IAM_WRITER_IPIF(ipif));
19537 
19538 	ip1dbg(("ipif_multicast_down\n"));
19539 	if (!ipif->ipif_multicast_up)
19540 		return;
19541 
19542 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19543 
19544 	if (!ipif->ipif_isv6) {
19545 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19546 		    B_TRUE);
19547 		if (err != 0)
19548 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19549 
19550 		ipif->ipif_multicast_up = 0;
19551 		return;
19552 	}
19553 
19554 	/*
19555 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19556 	 * we should look for ilms on this ill rather than the ones that have
19557 	 * been failed over here.  They are here temporarily. As
19558 	 * ipif_multicast_up has joined on this ill, we should delete only
19559 	 * from this ill.
19560 	 */
19561 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19562 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19563 	    B_TRUE, B_TRUE);
19564 	if (err != 0) {
19565 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19566 		    err));
19567 	}
19568 	/*
19569 	 * Disable multicast for the solicited node multicast address
19570 	 */
19571 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19572 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19573 
19574 		ipv6_multi.s6_addr32[3] |=
19575 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19576 
19577 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19578 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19579 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19580 
19581 		if (err != 0) {
19582 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19583 			    err));
19584 		}
19585 	}
19586 
19587 	ipif->ipif_multicast_up = 0;
19588 }
19589 
19590 /*
19591  * Used when an interface comes up to recreate any extra routes on this
19592  * interface.
19593  */
19594 static ire_t **
19595 ipif_recover_ire(ipif_t *ipif)
19596 {
19597 	mblk_t	*mp;
19598 	ire_t	**ipif_saved_irep;
19599 	ire_t	**irep;
19600 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19601 
19602 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19603 	    ipif->ipif_id));
19604 
19605 	mutex_enter(&ipif->ipif_saved_ire_lock);
19606 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19607 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19608 	if (ipif_saved_irep == NULL) {
19609 		mutex_exit(&ipif->ipif_saved_ire_lock);
19610 		return (NULL);
19611 	}
19612 
19613 	irep = ipif_saved_irep;
19614 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19615 		ire_t		*ire;
19616 		queue_t		*rfq;
19617 		queue_t		*stq;
19618 		ifrt_t		*ifrt;
19619 		uchar_t		*src_addr;
19620 		uchar_t		*gateway_addr;
19621 		ushort_t	type;
19622 
19623 		/*
19624 		 * When the ire was initially created and then added in
19625 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19626 		 * in the case of a traditional interface route, or as one of
19627 		 * the IRE_OFFSUBNET types (with the exception of
19628 		 * IRE_HOST types ire which is created by icmp_redirect() and
19629 		 * which we don't need to save or recover).  In the case where
19630 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19631 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19632 		 * to satisfy software like GateD and Sun Cluster which creates
19633 		 * routes using the the loopback interface's address as a
19634 		 * gateway.
19635 		 *
19636 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19637 		 * ire_create() will be called in the same way here as
19638 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19639 		 * the route looks like a traditional interface route (where
19640 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19641 		 * the saved ifrt->ifrt_type.  This means that in the case where
19642 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19643 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19644 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19645 		 */
19646 		ifrt = (ifrt_t *)mp->b_rptr;
19647 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19648 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19649 			rfq = NULL;
19650 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19651 			    ? ipif->ipif_rq : ipif->ipif_wq;
19652 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19653 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19654 			    : (uint8_t *)&ipif->ipif_src_addr;
19655 			gateway_addr = NULL;
19656 			type = ipif->ipif_net_type;
19657 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19658 			/* Recover multiroute broadcast IRE. */
19659 			rfq = ipif->ipif_rq;
19660 			stq = ipif->ipif_wq;
19661 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19662 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19663 			    : (uint8_t *)&ipif->ipif_src_addr;
19664 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19665 			type = ifrt->ifrt_type;
19666 		} else {
19667 			rfq = NULL;
19668 			stq = NULL;
19669 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19670 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19671 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19672 			type = ifrt->ifrt_type;
19673 		}
19674 
19675 		/*
19676 		 * Create a copy of the IRE with the saved address and netmask.
19677 		 */
19678 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19679 		    "0x%x/0x%x\n",
19680 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19681 		    ntohl(ifrt->ifrt_addr),
19682 		    ntohl(ifrt->ifrt_mask)));
19683 		ire = ire_create(
19684 		    (uint8_t *)&ifrt->ifrt_addr,
19685 		    (uint8_t *)&ifrt->ifrt_mask,
19686 		    src_addr,
19687 		    gateway_addr,
19688 		    &ifrt->ifrt_max_frag,
19689 		    NULL,
19690 		    rfq,
19691 		    stq,
19692 		    type,
19693 		    ipif,
19694 		    0,
19695 		    0,
19696 		    0,
19697 		    ifrt->ifrt_flags,
19698 		    &ifrt->ifrt_iulp_info,
19699 		    NULL,
19700 		    NULL,
19701 		    ipst);
19702 
19703 		if (ire == NULL) {
19704 			mutex_exit(&ipif->ipif_saved_ire_lock);
19705 			kmem_free(ipif_saved_irep,
19706 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19707 			return (NULL);
19708 		}
19709 
19710 		/*
19711 		 * Some software (for example, GateD and Sun Cluster) attempts
19712 		 * to create (what amount to) IRE_PREFIX routes with the
19713 		 * loopback address as the gateway.  This is primarily done to
19714 		 * set up prefixes with the RTF_REJECT flag set (for example,
19715 		 * when generating aggregate routes.)
19716 		 *
19717 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19718 		 * IRE_LOOPBACK, then we map the request into a
19719 		 * IRE_IF_NORESOLVER.
19720 		 */
19721 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19722 			ire->ire_type = IRE_IF_NORESOLVER;
19723 		/*
19724 		 * ire held by ire_add, will be refreled' towards the
19725 		 * the end of ipif_up_done
19726 		 */
19727 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19728 		*irep = ire;
19729 		irep++;
19730 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19731 	}
19732 	mutex_exit(&ipif->ipif_saved_ire_lock);
19733 	return (ipif_saved_irep);
19734 }
19735 
19736 /*
19737  * Used to set the netmask and broadcast address to default values when the
19738  * interface is brought up.  (Always called as writer.)
19739  */
19740 static void
19741 ipif_set_default(ipif_t *ipif)
19742 {
19743 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19744 
19745 	if (!ipif->ipif_isv6) {
19746 		/*
19747 		 * Interface holds an IPv4 address. Default
19748 		 * mask is the natural netmask.
19749 		 */
19750 		if (!ipif->ipif_net_mask) {
19751 			ipaddr_t	v4mask;
19752 
19753 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19754 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19755 		}
19756 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19757 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19758 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19759 		} else {
19760 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19761 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19762 		}
19763 		/*
19764 		 * NOTE: SunOS 4.X does this even if the broadcast address
19765 		 * has been already set thus we do the same here.
19766 		 */
19767 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19768 			ipaddr_t	v4addr;
19769 
19770 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19771 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19772 		}
19773 	} else {
19774 		/*
19775 		 * Interface holds an IPv6-only address.  Default
19776 		 * mask is all-ones.
19777 		 */
19778 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19779 			ipif->ipif_v6net_mask = ipv6_all_ones;
19780 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19781 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19782 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19783 		} else {
19784 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19785 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19786 		}
19787 	}
19788 }
19789 
19790 /*
19791  * Return 0 if this address can be used as local address without causing
19792  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19793  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19794  * Special checks are needed to allow the same IPv6 link-local address
19795  * on different ills.
19796  * TODO: allowing the same site-local address on different ill's.
19797  */
19798 int
19799 ip_addr_availability_check(ipif_t *new_ipif)
19800 {
19801 	in6_addr_t our_v6addr;
19802 	ill_t *ill;
19803 	ipif_t *ipif;
19804 	ill_walk_context_t ctx;
19805 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19806 
19807 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19808 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19809 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19810 
19811 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19812 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19813 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19814 		return (0);
19815 
19816 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19817 
19818 	if (new_ipif->ipif_isv6)
19819 		ill = ILL_START_WALK_V6(&ctx, ipst);
19820 	else
19821 		ill = ILL_START_WALK_V4(&ctx, ipst);
19822 
19823 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19824 		for (ipif = ill->ill_ipif; ipif != NULL;
19825 		    ipif = ipif->ipif_next) {
19826 			if ((ipif == new_ipif) ||
19827 			    !(ipif->ipif_flags & IPIF_UP) ||
19828 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19829 				continue;
19830 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19831 			    &our_v6addr)) {
19832 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19833 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19834 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19835 					ipif->ipif_flags |= IPIF_UNNUMBERED;
19836 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19837 				    new_ipif->ipif_ill != ill)
19838 					continue;
19839 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19840 				    new_ipif->ipif_ill != ill)
19841 					continue;
19842 				else if (new_ipif->ipif_zoneid !=
19843 				    ipif->ipif_zoneid &&
19844 				    ipif->ipif_zoneid != ALL_ZONES &&
19845 				    IS_LOOPBACK(ill))
19846 					continue;
19847 				else if (new_ipif->ipif_ill == ill)
19848 					return (EADDRINUSE);
19849 				else
19850 					return (EADDRNOTAVAIL);
19851 			}
19852 		}
19853 	}
19854 
19855 	return (0);
19856 }
19857 
19858 /*
19859  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19860  * IREs for the ipif.
19861  * When the routine returns EINPROGRESS then mp has been consumed and
19862  * the ioctl will be acked from ip_rput_dlpi.
19863  */
19864 static int
19865 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19866 {
19867 	ill_t	*ill = ipif->ipif_ill;
19868 	boolean_t isv6 = ipif->ipif_isv6;
19869 	int	err = 0;
19870 	boolean_t success;
19871 
19872 	ASSERT(IAM_WRITER_IPIF(ipif));
19873 
19874 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19875 
19876 	/* Shouldn't get here if it is already up. */
19877 	if (ipif->ipif_flags & IPIF_UP)
19878 		return (EALREADY);
19879 
19880 	/* Skip arp/ndp for any loopback interface. */
19881 	if (ill->ill_wq != NULL) {
19882 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19883 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19884 
19885 		if (!ill->ill_dl_up) {
19886 			/*
19887 			 * ill_dl_up is not yet set. i.e. we are yet to
19888 			 * DL_BIND with the driver and this is the first
19889 			 * logical interface on the ill to become "up".
19890 			 * Tell the driver to get going (via DL_BIND_REQ).
19891 			 * Note that changing "significant" IFF_ flags
19892 			 * address/netmask etc cause a down/up dance, but
19893 			 * does not cause an unbind (DL_UNBIND) with the driver
19894 			 */
19895 			return (ill_dl_up(ill, ipif, mp, q));
19896 		}
19897 
19898 		/*
19899 		 * ipif_resolver_up may end up sending an
19900 		 * AR_INTERFACE_UP message to ARP, which would, in
19901 		 * turn send a DLPI message to the driver. ioctls are
19902 		 * serialized and so we cannot send more than one
19903 		 * interface up message at a time. If ipif_resolver_up
19904 		 * does send an interface up message to ARP, we get
19905 		 * EINPROGRESS and we will complete in ip_arp_done.
19906 		 */
19907 
19908 		ASSERT(connp != NULL || !CONN_Q(q));
19909 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19910 		if (connp != NULL)
19911 			mutex_enter(&connp->conn_lock);
19912 		mutex_enter(&ill->ill_lock);
19913 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19914 		mutex_exit(&ill->ill_lock);
19915 		if (connp != NULL)
19916 			mutex_exit(&connp->conn_lock);
19917 		if (!success)
19918 			return (EINTR);
19919 
19920 		/*
19921 		 * Crank up IPv6 neighbor discovery
19922 		 * Unlike ARP, this should complete when
19923 		 * ipif_ndp_up returns. However, for
19924 		 * ILLF_XRESOLV interfaces we also send a
19925 		 * AR_INTERFACE_UP to the external resolver.
19926 		 * That ioctl will complete in ip_rput.
19927 		 */
19928 		if (isv6) {
19929 			err = ipif_ndp_up(ipif);
19930 			if (err != 0) {
19931 				if (err != EINPROGRESS)
19932 					mp = ipsq_pending_mp_get(ipsq, &connp);
19933 				return (err);
19934 			}
19935 		}
19936 		/* Now, ARP */
19937 		err = ipif_resolver_up(ipif, Res_act_initial);
19938 		if (err == EINPROGRESS) {
19939 			/* We will complete it in ip_arp_done */
19940 			return (err);
19941 		}
19942 		mp = ipsq_pending_mp_get(ipsq, &connp);
19943 		ASSERT(mp != NULL);
19944 		if (err != 0)
19945 			return (err);
19946 	} else {
19947 		/*
19948 		 * Interfaces without underlying hardware don't do duplicate
19949 		 * address detection.
19950 		 */
19951 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
19952 		ipif->ipif_addr_ready = 1;
19953 	}
19954 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
19955 }
19956 
19957 /*
19958  * Perform a bind for the physical device.
19959  * When the routine returns EINPROGRESS then mp has been consumed and
19960  * the ioctl will be acked from ip_rput_dlpi.
19961  * Allocate an unbind message and save it until ipif_down.
19962  */
19963 static int
19964 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
19965 {
19966 	areq_t	*areq;
19967 	mblk_t	*areq_mp = NULL;
19968 	mblk_t	*bind_mp = NULL;
19969 	mblk_t	*unbind_mp = NULL;
19970 	conn_t	*connp;
19971 	boolean_t success;
19972 	uint16_t sap_addr;
19973 
19974 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
19975 	ASSERT(IAM_WRITER_ILL(ill));
19976 	ASSERT(mp != NULL);
19977 
19978 	/* Create a resolver cookie for ARP */
19979 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
19980 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
19981 		if (areq_mp == NULL)
19982 			return (ENOMEM);
19983 
19984 		freemsg(ill->ill_resolver_mp);
19985 		ill->ill_resolver_mp = areq_mp;
19986 		areq = (areq_t *)areq_mp->b_rptr;
19987 		sap_addr = ill->ill_sap;
19988 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
19989 	}
19990 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
19991 	    DL_BIND_REQ);
19992 	if (bind_mp == NULL)
19993 		goto bad;
19994 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
19995 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
19996 
19997 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
19998 	if (unbind_mp == NULL)
19999 		goto bad;
20000 
20001 	/*
20002 	 * Record state needed to complete this operation when the
20003 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
20004 	 */
20005 	ASSERT(WR(q)->q_next == NULL);
20006 	connp = Q_TO_CONN(q);
20007 
20008 	mutex_enter(&connp->conn_lock);
20009 	mutex_enter(&ipif->ipif_ill->ill_lock);
20010 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
20011 	mutex_exit(&ipif->ipif_ill->ill_lock);
20012 	mutex_exit(&connp->conn_lock);
20013 	if (!success)
20014 		goto bad;
20015 
20016 	/*
20017 	 * Save the unbind message for ill_dl_down(); it will be consumed when
20018 	 * the interface goes down.
20019 	 */
20020 	ASSERT(ill->ill_unbind_mp == NULL);
20021 	ill->ill_unbind_mp = unbind_mp;
20022 
20023 	ill_dlpi_send(ill, bind_mp);
20024 	/* Send down link-layer capabilities probe if not already done. */
20025 	ill_capability_probe(ill);
20026 
20027 	/*
20028 	 * Sysid used to rely on the fact that netboots set domainname
20029 	 * and the like. Now that miniroot boots aren't strictly netboots
20030 	 * and miniroot network configuration is driven from userland
20031 	 * these things still need to be set. This situation can be detected
20032 	 * by comparing the interface being configured here to the one
20033 	 * dhcifname was set to reference by the boot loader. Once sysid is
20034 	 * converted to use dhcp_ipc_getinfo() this call can go away.
20035 	 */
20036 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
20037 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
20038 	    (strlen(srpc_domain) == 0)) {
20039 		if (dhcpinit() != 0)
20040 			cmn_err(CE_WARN, "no cached dhcp response");
20041 	}
20042 
20043 	/*
20044 	 * This operation will complete in ip_rput_dlpi with either
20045 	 * a DL_BIND_ACK or DL_ERROR_ACK.
20046 	 */
20047 	return (EINPROGRESS);
20048 bad:
20049 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
20050 	/*
20051 	 * We don't have to check for possible removal from illgrp
20052 	 * as we have not yet inserted in illgrp. For groups
20053 	 * without names, this ipif is still not UP and hence
20054 	 * this could not have possibly had any influence in forming
20055 	 * groups.
20056 	 */
20057 
20058 	freemsg(bind_mp);
20059 	freemsg(unbind_mp);
20060 	return (ENOMEM);
20061 }
20062 
20063 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
20064 
20065 /*
20066  * DLPI and ARP is up.
20067  * Create all the IREs associated with an interface bring up multicast.
20068  * Set the interface flag and finish other initialization
20069  * that potentially had to be differed to after DL_BIND_ACK.
20070  */
20071 int
20072 ipif_up_done(ipif_t *ipif)
20073 {
20074 	ire_t	*ire_array[20];
20075 	ire_t	**irep = ire_array;
20076 	ire_t	**irep1;
20077 	ipaddr_t net_mask = 0;
20078 	ipaddr_t subnet_mask, route_mask;
20079 	ill_t	*ill = ipif->ipif_ill;
20080 	queue_t	*stq;
20081 	ipif_t	 *src_ipif;
20082 	ipif_t   *tmp_ipif;
20083 	boolean_t	flush_ire_cache = B_TRUE;
20084 	int	err = 0;
20085 	phyint_t *phyi;
20086 	ire_t	**ipif_saved_irep = NULL;
20087 	int ipif_saved_ire_cnt;
20088 	int	cnt;
20089 	boolean_t	src_ipif_held = B_FALSE;
20090 	boolean_t	ire_added = B_FALSE;
20091 	boolean_t	loopback = B_FALSE;
20092 	ip_stack_t	*ipst = ill->ill_ipst;
20093 
20094 	ip1dbg(("ipif_up_done(%s:%u)\n",
20095 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
20096 	/* Check if this is a loopback interface */
20097 	if (ipif->ipif_ill->ill_wq == NULL)
20098 		loopback = B_TRUE;
20099 
20100 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20101 	/*
20102 	 * If all other interfaces for this ill are down or DEPRECATED,
20103 	 * or otherwise unsuitable for source address selection, remove
20104 	 * any IRE_CACHE entries for this ill to make sure source
20105 	 * address selection gets to take this new ipif into account.
20106 	 * No need to hold ill_lock while traversing the ipif list since
20107 	 * we are writer
20108 	 */
20109 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20110 	    tmp_ipif = tmp_ipif->ipif_next) {
20111 		if (((tmp_ipif->ipif_flags &
20112 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20113 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20114 		    (tmp_ipif == ipif))
20115 			continue;
20116 		/* first useable pre-existing interface */
20117 		flush_ire_cache = B_FALSE;
20118 		break;
20119 	}
20120 	if (flush_ire_cache)
20121 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20122 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20123 
20124 	/*
20125 	 * Figure out which way the send-to queue should go.  Only
20126 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20127 	 * should show up here.
20128 	 */
20129 	switch (ill->ill_net_type) {
20130 	case IRE_IF_RESOLVER:
20131 		stq = ill->ill_rq;
20132 		break;
20133 	case IRE_IF_NORESOLVER:
20134 	case IRE_LOOPBACK:
20135 		stq = ill->ill_wq;
20136 		break;
20137 	default:
20138 		return (EINVAL);
20139 	}
20140 
20141 	if (IS_LOOPBACK(ill)) {
20142 		/*
20143 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20144 		 * ipif_lookup_on_name(), but in the case of zones we can have
20145 		 * several loopback addresses on lo0. So all the interfaces with
20146 		 * loopback addresses need to be marked IRE_LOOPBACK.
20147 		 */
20148 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20149 		    htonl(INADDR_LOOPBACK))
20150 			ipif->ipif_ire_type = IRE_LOOPBACK;
20151 		else
20152 			ipif->ipif_ire_type = IRE_LOCAL;
20153 	}
20154 
20155 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20156 		/*
20157 		 * Can't use our source address. Select a different
20158 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20159 		 */
20160 		src_ipif = ipif_select_source(ipif->ipif_ill,
20161 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20162 		if (src_ipif == NULL)
20163 			src_ipif = ipif;	/* Last resort */
20164 		else
20165 			src_ipif_held = B_TRUE;
20166 	} else {
20167 		src_ipif = ipif;
20168 	}
20169 
20170 	/* Create all the IREs associated with this interface */
20171 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20172 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20173 
20174 		/*
20175 		 * If we're on a labeled system then make sure that zone-
20176 		 * private addresses have proper remote host database entries.
20177 		 */
20178 		if (is_system_labeled() &&
20179 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20180 		    !tsol_check_interface_address(ipif))
20181 			return (EINVAL);
20182 
20183 		/* Register the source address for __sin6_src_id */
20184 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20185 		    ipif->ipif_zoneid, ipst);
20186 		if (err != 0) {
20187 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20188 			return (err);
20189 		}
20190 
20191 		/* If the interface address is set, create the local IRE. */
20192 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20193 		    (void *)ipif,
20194 		    ipif->ipif_ire_type,
20195 		    ntohl(ipif->ipif_lcl_addr)));
20196 		*irep++ = ire_create(
20197 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20198 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20199 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20200 		    NULL,				/* no gateway */
20201 		    &ip_loopback_mtuplus,		/* max frag size */
20202 		    NULL,
20203 		    ipif->ipif_rq,			/* recv-from queue */
20204 		    NULL,				/* no send-to queue */
20205 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20206 		    ipif,
20207 		    0,
20208 		    0,
20209 		    0,
20210 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20211 		    RTF_PRIVATE : 0,
20212 		    &ire_uinfo_null,
20213 		    NULL,
20214 		    NULL,
20215 		    ipst);
20216 	} else {
20217 		ip1dbg((
20218 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20219 		    ipif->ipif_ire_type,
20220 		    ntohl(ipif->ipif_lcl_addr),
20221 		    (uint_t)ipif->ipif_flags));
20222 	}
20223 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20224 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20225 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20226 	} else {
20227 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20228 	}
20229 
20230 	subnet_mask = ipif->ipif_net_mask;
20231 
20232 	/*
20233 	 * If mask was not specified, use natural netmask of
20234 	 * interface address. Also, store this mask back into the
20235 	 * ipif struct.
20236 	 */
20237 	if (subnet_mask == 0) {
20238 		subnet_mask = net_mask;
20239 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20240 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20241 		    ipif->ipif_v6subnet);
20242 	}
20243 
20244 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20245 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20246 	    ipif->ipif_subnet != INADDR_ANY) {
20247 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20248 
20249 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20250 			route_mask = IP_HOST_MASK;
20251 		} else {
20252 			route_mask = subnet_mask;
20253 		}
20254 
20255 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20256 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20257 		    (void *)ipif, (void *)ill,
20258 		    ill->ill_net_type,
20259 		    ntohl(ipif->ipif_subnet)));
20260 		*irep++ = ire_create(
20261 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20262 		    (uchar_t *)&route_mask,		/* mask */
20263 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20264 		    NULL,				/* no gateway */
20265 		    &ipif->ipif_mtu,			/* max frag */
20266 		    NULL,
20267 		    NULL,				/* no recv queue */
20268 		    stq,				/* send-to queue */
20269 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20270 		    ipif,
20271 		    0,
20272 		    0,
20273 		    0,
20274 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20275 		    &ire_uinfo_null,
20276 		    NULL,
20277 		    NULL,
20278 		    ipst);
20279 	}
20280 
20281 	/*
20282 	 * Create any necessary broadcast IREs.
20283 	 */
20284 	if (ipif->ipif_flags & IPIF_BROADCAST)
20285 		irep = ipif_create_bcast_ires(ipif, irep);
20286 
20287 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20288 
20289 	/* If an earlier ire_create failed, get out now */
20290 	for (irep1 = irep; irep1 > ire_array; ) {
20291 		irep1--;
20292 		if (*irep1 == NULL) {
20293 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20294 			err = ENOMEM;
20295 			goto bad;
20296 		}
20297 	}
20298 
20299 	/*
20300 	 * Need to atomically check for ip_addr_availablity_check
20301 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20302 	 * from group also.The ill_g_lock is grabbed as reader
20303 	 * just to make sure no new ills or new ipifs are being added
20304 	 * to the system while we are checking the uniqueness of addresses.
20305 	 */
20306 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20307 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20308 	/* Mark it up, and increment counters. */
20309 	ipif->ipif_flags |= IPIF_UP;
20310 	ill->ill_ipif_up_count++;
20311 	err = ip_addr_availability_check(ipif);
20312 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20313 	rw_exit(&ipst->ips_ill_g_lock);
20314 
20315 	if (err != 0) {
20316 		/*
20317 		 * Our address may already be up on the same ill. In this case,
20318 		 * the ARP entry for our ipif replaced the one for the other
20319 		 * ipif. So we don't want to delete it (otherwise the other ipif
20320 		 * would be unable to send packets).
20321 		 * ip_addr_availability_check() identifies this case for us and
20322 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20323 		 * which is the expected error code.
20324 		 */
20325 		if (err == EADDRINUSE) {
20326 			freemsg(ipif->ipif_arp_del_mp);
20327 			ipif->ipif_arp_del_mp = NULL;
20328 			err = EADDRNOTAVAIL;
20329 		}
20330 		ill->ill_ipif_up_count--;
20331 		ipif->ipif_flags &= ~IPIF_UP;
20332 		goto bad;
20333 	}
20334 
20335 	/*
20336 	 * Add in all newly created IREs.  ire_create_bcast() has
20337 	 * already checked for duplicates of the IRE_BROADCAST type.
20338 	 * We want to add before we call ifgrp_insert which wants
20339 	 * to know whether IRE_IF_RESOLVER exists or not.
20340 	 *
20341 	 * NOTE : We refrele the ire though we may branch to "bad"
20342 	 *	  later on where we do ire_delete. This is okay
20343 	 *	  because nobody can delete it as we are running
20344 	 *	  exclusively.
20345 	 */
20346 	for (irep1 = irep; irep1 > ire_array; ) {
20347 		irep1--;
20348 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20349 		/*
20350 		 * refheld by ire_add. refele towards the end of the func
20351 		 */
20352 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20353 	}
20354 	ire_added = B_TRUE;
20355 	/*
20356 	 * Form groups if possible.
20357 	 *
20358 	 * If we are supposed to be in a ill_group with a name, insert it
20359 	 * now as we know that at least one ipif is UP. Otherwise form
20360 	 * nameless groups.
20361 	 *
20362 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20363 	 * this ipif into the appropriate interface group, or create a
20364 	 * new one. If this is already in a nameless group, we try to form
20365 	 * a bigger group looking at other ills potentially sharing this
20366 	 * ipif's prefix.
20367 	 */
20368 	phyi = ill->ill_phyint;
20369 	if (phyi->phyint_groupname_len != 0) {
20370 		ASSERT(phyi->phyint_groupname != NULL);
20371 		if (ill->ill_ipif_up_count == 1) {
20372 			ASSERT(ill->ill_group == NULL);
20373 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20374 			    phyi->phyint_groupname, NULL, B_TRUE);
20375 			if (err != 0) {
20376 				ip1dbg(("ipif_up_done: illgrp allocation "
20377 				    "failed, error %d\n", err));
20378 				goto bad;
20379 			}
20380 		}
20381 		ASSERT(ill->ill_group != NULL);
20382 	}
20383 
20384 	/*
20385 	 * When this is part of group, we need to make sure that
20386 	 * any broadcast ires created because of this ipif coming
20387 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20388 	 * so that we don't receive duplicate broadcast packets.
20389 	 */
20390 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20391 		ipif_renominate_bcast(ipif);
20392 
20393 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20394 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20395 	ipif_saved_irep = ipif_recover_ire(ipif);
20396 
20397 	if (!loopback) {
20398 		/*
20399 		 * If the broadcast address has been set, make sure it makes
20400 		 * sense based on the interface address.
20401 		 * Only match on ill since we are sharing broadcast addresses.
20402 		 */
20403 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20404 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20405 			ire_t	*ire;
20406 
20407 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20408 			    IRE_BROADCAST, ipif, ALL_ZONES,
20409 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20410 
20411 			if (ire == NULL) {
20412 				/*
20413 				 * If there isn't a matching broadcast IRE,
20414 				 * revert to the default for this netmask.
20415 				 */
20416 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20417 				mutex_enter(&ipif->ipif_ill->ill_lock);
20418 				ipif_set_default(ipif);
20419 				mutex_exit(&ipif->ipif_ill->ill_lock);
20420 			} else {
20421 				ire_refrele(ire);
20422 			}
20423 		}
20424 
20425 	}
20426 
20427 	/* This is the first interface on this ill */
20428 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20429 		/*
20430 		 * Need to recover all multicast memberships in the driver.
20431 		 * This had to be deferred until we had attached.
20432 		 */
20433 		ill_recover_multicast(ill);
20434 	}
20435 	/* Join the allhosts multicast address */
20436 	ipif_multicast_up(ipif);
20437 
20438 	if (!loopback) {
20439 		/*
20440 		 * See whether anybody else would benefit from the
20441 		 * new ipif that we added. We call this always rather
20442 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20443 		 * ipif is for the benefit of illgrp_insert (done above)
20444 		 * which does not do source address selection as it does
20445 		 * not want to re-create interface routes that we are
20446 		 * having reference to it here.
20447 		 */
20448 		ill_update_source_selection(ill);
20449 	}
20450 
20451 	for (irep1 = irep; irep1 > ire_array; ) {
20452 		irep1--;
20453 		if (*irep1 != NULL) {
20454 			/* was held in ire_add */
20455 			ire_refrele(*irep1);
20456 		}
20457 	}
20458 
20459 	cnt = ipif_saved_ire_cnt;
20460 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20461 		if (*irep1 != NULL) {
20462 			/* was held in ire_add */
20463 			ire_refrele(*irep1);
20464 		}
20465 	}
20466 
20467 	if (!loopback && ipif->ipif_addr_ready) {
20468 		/* Broadcast an address mask reply. */
20469 		ipif_mask_reply(ipif);
20470 	}
20471 	if (ipif_saved_irep != NULL) {
20472 		kmem_free(ipif_saved_irep,
20473 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20474 	}
20475 	if (src_ipif_held)
20476 		ipif_refrele(src_ipif);
20477 
20478 	/*
20479 	 * This had to be deferred until we had bound.  Tell routing sockets and
20480 	 * others that this interface is up if it looks like the address has
20481 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20482 	 * duplicate address detection to do its thing.
20483 	 */
20484 	if (ipif->ipif_addr_ready) {
20485 		ip_rts_ifmsg(ipif);
20486 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20487 		/* Let SCTP update the status for this ipif */
20488 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20489 	}
20490 	return (0);
20491 
20492 bad:
20493 	ip1dbg(("ipif_up_done: FAILED \n"));
20494 	/*
20495 	 * We don't have to bother removing from ill groups because
20496 	 *
20497 	 * 1) For groups with names, we insert only when the first ipif
20498 	 *    comes up. In that case if it fails, it will not be in any
20499 	 *    group. So, we need not try to remove for that case.
20500 	 *
20501 	 * 2) For groups without names, either we tried to insert ipif_ill
20502 	 *    in a group as singleton or found some other group to become
20503 	 *    a bigger group. For the former, if it fails we don't have
20504 	 *    anything to do as ipif_ill is not in the group and for the
20505 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20506 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20507 	 */
20508 	while (irep > ire_array) {
20509 		irep--;
20510 		if (*irep != NULL) {
20511 			ire_delete(*irep);
20512 			if (ire_added)
20513 				ire_refrele(*irep);
20514 		}
20515 	}
20516 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20517 
20518 	if (ipif_saved_irep != NULL) {
20519 		kmem_free(ipif_saved_irep,
20520 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20521 	}
20522 	if (src_ipif_held)
20523 		ipif_refrele(src_ipif);
20524 
20525 	ipif_arp_down(ipif);
20526 	return (err);
20527 }
20528 
20529 /*
20530  * Turn off the ARP with the ILLF_NOARP flag.
20531  */
20532 static int
20533 ill_arp_off(ill_t *ill)
20534 {
20535 	mblk_t	*arp_off_mp = NULL;
20536 	mblk_t	*arp_on_mp = NULL;
20537 
20538 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20539 
20540 	ASSERT(IAM_WRITER_ILL(ill));
20541 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20542 
20543 	/*
20544 	 * If the on message is still around we've already done
20545 	 * an arp_off without doing an arp_on thus there is no
20546 	 * work needed.
20547 	 */
20548 	if (ill->ill_arp_on_mp != NULL)
20549 		return (0);
20550 
20551 	/*
20552 	 * Allocate an ARP on message (to be saved) and an ARP off message
20553 	 */
20554 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20555 	if (!arp_off_mp)
20556 		return (ENOMEM);
20557 
20558 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20559 	if (!arp_on_mp)
20560 		goto failed;
20561 
20562 	ASSERT(ill->ill_arp_on_mp == NULL);
20563 	ill->ill_arp_on_mp = arp_on_mp;
20564 
20565 	/* Send an AR_INTERFACE_OFF request */
20566 	putnext(ill->ill_rq, arp_off_mp);
20567 	return (0);
20568 failed:
20569 
20570 	if (arp_off_mp)
20571 		freemsg(arp_off_mp);
20572 	return (ENOMEM);
20573 }
20574 
20575 /*
20576  * Turn on ARP by turning off the ILLF_NOARP flag.
20577  */
20578 static int
20579 ill_arp_on(ill_t *ill)
20580 {
20581 	mblk_t	*mp;
20582 
20583 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20584 
20585 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20586 
20587 	ASSERT(IAM_WRITER_ILL(ill));
20588 	/*
20589 	 * Send an AR_INTERFACE_ON request if we have already done
20590 	 * an arp_off (which allocated the message).
20591 	 */
20592 	if (ill->ill_arp_on_mp != NULL) {
20593 		mp = ill->ill_arp_on_mp;
20594 		ill->ill_arp_on_mp = NULL;
20595 		putnext(ill->ill_rq, mp);
20596 	}
20597 	return (0);
20598 }
20599 
20600 /*
20601  * Called after either deleting ill from the group or when setting
20602  * FAILED or STANDBY on the interface.
20603  */
20604 static void
20605 illgrp_reset_schednext(ill_t *ill)
20606 {
20607 	ill_group_t *illgrp;
20608 	ill_t *save_ill;
20609 
20610 	ASSERT(IAM_WRITER_ILL(ill));
20611 	/*
20612 	 * When called from illgrp_delete, ill_group will be non-NULL.
20613 	 * But when called from ip_sioctl_flags, it could be NULL if
20614 	 * somebody is setting FAILED/INACTIVE on some interface which
20615 	 * is not part of a group.
20616 	 */
20617 	illgrp = ill->ill_group;
20618 	if (illgrp == NULL)
20619 		return;
20620 	if (illgrp->illgrp_ill_schednext != ill)
20621 		return;
20622 
20623 	illgrp->illgrp_ill_schednext = NULL;
20624 	save_ill = ill;
20625 	/*
20626 	 * Choose a good ill to be the next one for
20627 	 * outbound traffic. As the flags FAILED/STANDBY is
20628 	 * not yet marked when called from ip_sioctl_flags,
20629 	 * we check for ill separately.
20630 	 */
20631 	for (ill = illgrp->illgrp_ill; ill != NULL;
20632 	    ill = ill->ill_group_next) {
20633 		if ((ill != save_ill) &&
20634 		    !(ill->ill_phyint->phyint_flags &
20635 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20636 			illgrp->illgrp_ill_schednext = ill;
20637 			return;
20638 		}
20639 	}
20640 }
20641 
20642 /*
20643  * Given an ill, find the next ill in the group to be scheduled.
20644  * (This should be called by ip_newroute() before ire_create().)
20645  * The passed in ill may be pulled out of the group, after we have picked
20646  * up a different outgoing ill from the same group. However ire add will
20647  * atomically check this.
20648  */
20649 ill_t *
20650 illgrp_scheduler(ill_t *ill)
20651 {
20652 	ill_t *retill;
20653 	ill_group_t *illgrp;
20654 	int illcnt;
20655 	int i;
20656 	uint64_t flags;
20657 	ip_stack_t	*ipst = ill->ill_ipst;
20658 
20659 	/*
20660 	 * We don't use a lock to check for the ill_group. If this ill
20661 	 * is currently being inserted we may end up just returning this
20662 	 * ill itself. That is ok.
20663 	 */
20664 	if (ill->ill_group == NULL) {
20665 		ill_refhold(ill);
20666 		return (ill);
20667 	}
20668 
20669 	/*
20670 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20671 	 * a set of stable ills. No ill can be added or deleted or change
20672 	 * group while we hold the reader lock.
20673 	 */
20674 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20675 	if ((illgrp = ill->ill_group) == NULL) {
20676 		rw_exit(&ipst->ips_ill_g_lock);
20677 		ill_refhold(ill);
20678 		return (ill);
20679 	}
20680 
20681 	illcnt = illgrp->illgrp_ill_count;
20682 	mutex_enter(&illgrp->illgrp_lock);
20683 	retill = illgrp->illgrp_ill_schednext;
20684 
20685 	if (retill == NULL)
20686 		retill = illgrp->illgrp_ill;
20687 
20688 	/*
20689 	 * We do a circular search beginning at illgrp_ill_schednext
20690 	 * or illgrp_ill. We don't check the flags against the ill lock
20691 	 * since it can change anytime. The ire creation will be atomic
20692 	 * and will fail if the ill is FAILED or OFFLINE.
20693 	 */
20694 	for (i = 0; i < illcnt; i++) {
20695 		flags = retill->ill_phyint->phyint_flags;
20696 
20697 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20698 		    ILL_CAN_LOOKUP(retill)) {
20699 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20700 			ill_refhold(retill);
20701 			break;
20702 		}
20703 		retill = retill->ill_group_next;
20704 		if (retill == NULL)
20705 			retill = illgrp->illgrp_ill;
20706 	}
20707 	mutex_exit(&illgrp->illgrp_lock);
20708 	rw_exit(&ipst->ips_ill_g_lock);
20709 
20710 	return (i == illcnt ? NULL : retill);
20711 }
20712 
20713 /*
20714  * Checks for availbility of a usable source address (if there is one) when the
20715  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20716  * this selection is done regardless of the destination.
20717  */
20718 boolean_t
20719 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20720 {
20721 	uint_t	ifindex;
20722 	ipif_t	*ipif = NULL;
20723 	ill_t	*uill;
20724 	boolean_t isv6;
20725 	ip_stack_t	*ipst = ill->ill_ipst;
20726 
20727 	ASSERT(ill != NULL);
20728 
20729 	isv6 = ill->ill_isv6;
20730 	ifindex = ill->ill_usesrc_ifindex;
20731 	if (ifindex != 0) {
20732 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20733 		    NULL, ipst);
20734 		if (uill == NULL)
20735 			return (NULL);
20736 		mutex_enter(&uill->ill_lock);
20737 		for (ipif = uill->ill_ipif; ipif != NULL;
20738 		    ipif = ipif->ipif_next) {
20739 			if (!IPIF_CAN_LOOKUP(ipif))
20740 				continue;
20741 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20742 				continue;
20743 			if (!(ipif->ipif_flags & IPIF_UP))
20744 				continue;
20745 			if (ipif->ipif_zoneid != zoneid)
20746 				continue;
20747 			if ((isv6 &&
20748 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20749 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20750 				continue;
20751 			mutex_exit(&uill->ill_lock);
20752 			ill_refrele(uill);
20753 			return (B_TRUE);
20754 		}
20755 		mutex_exit(&uill->ill_lock);
20756 		ill_refrele(uill);
20757 	}
20758 	return (B_FALSE);
20759 }
20760 
20761 /*
20762  * Determine the best source address given a destination address and an ill.
20763  * Prefers non-deprecated over deprecated but will return a deprecated
20764  * address if there is no other choice. If there is a usable source address
20765  * on the interface pointed to by ill_usesrc_ifindex then that is given
20766  * first preference.
20767  *
20768  * Returns NULL if there is no suitable source address for the ill.
20769  * This only occurs when there is no valid source address for the ill.
20770  */
20771 ipif_t *
20772 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20773 {
20774 	ipif_t *ipif;
20775 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20776 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20777 	int index = 0;
20778 	boolean_t wrapped = B_FALSE;
20779 	boolean_t same_subnet_only = B_FALSE;
20780 	boolean_t ipif_same_found, ipif_other_found;
20781 	boolean_t specific_found;
20782 	ill_t	*till, *usill = NULL;
20783 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20784 	ip_stack_t	*ipst = ill->ill_ipst;
20785 
20786 	if (ill->ill_usesrc_ifindex != 0) {
20787 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20788 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20789 		if (usill != NULL)
20790 			ill = usill;	/* Select source from usesrc ILL */
20791 		else
20792 			return (NULL);
20793 	}
20794 
20795 	/*
20796 	 * If we're dealing with an unlabeled destination on a labeled system,
20797 	 * make sure that we ignore source addresses that are incompatible with
20798 	 * the destination's default label.  That destination's default label
20799 	 * must dominate the minimum label on the source address.
20800 	 */
20801 	dst_rhtp = NULL;
20802 	if (is_system_labeled()) {
20803 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20804 		if (dst_rhtp == NULL)
20805 			return (NULL);
20806 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20807 			TPC_RELE(dst_rhtp);
20808 			dst_rhtp = NULL;
20809 		}
20810 	}
20811 
20812 	/*
20813 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20814 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20815 	 * After selecting the right ipif, under ill_lock make sure ipif is
20816 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20817 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20818 	 * but not under a lock.
20819 	 */
20820 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20821 
20822 retry:
20823 	till = ill;
20824 	ipif_arr[0] = NULL;
20825 
20826 	if (till->ill_group != NULL)
20827 		till = till->ill_group->illgrp_ill;
20828 
20829 	/*
20830 	 * Choose one good source address from each ill across the group.
20831 	 * If possible choose a source address in the same subnet as
20832 	 * the destination address.
20833 	 *
20834 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20835 	 * This is okay because of the following.
20836 	 *
20837 	 *    If PHYI_FAILED is set and we still have non-deprecated
20838 	 *    addresses, it means the addresses have not yet been
20839 	 *    failed over to a different interface. We potentially
20840 	 *    select them to create IRE_CACHES, which will be later
20841 	 *    flushed when the addresses move over.
20842 	 *
20843 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20844 	 *    addresses, it means either the user has configured them
20845 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20846 	 *    been moved over. For the former, in.mpathd does a failover
20847 	 *    when the interface becomes INACTIVE and hence we should
20848 	 *    not find them. Once INACTIVE is set, we don't allow them
20849 	 *    to create logical interfaces anymore. For the latter, a
20850 	 *    flush will happen when INACTIVE is cleared which will
20851 	 *    flush the IRE_CACHES.
20852 	 *
20853 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20854 	 *    over soon. We potentially select them to create IRE_CACHEs,
20855 	 *    which will be later flushed when the addresses move over.
20856 	 *
20857 	 * NOTE : As ipif_select_source is called to borrow source address
20858 	 * for an ipif that is part of a group, source address selection
20859 	 * will be re-done whenever the group changes i.e either an
20860 	 * insertion/deletion in the group.
20861 	 *
20862 	 * Fill ipif_arr[] with source addresses, using these rules:
20863 	 *
20864 	 *	1. At most one source address from a given ill ends up
20865 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20866 	 *	   associated with a given ill ends up in ipif_arr[].
20867 	 *
20868 	 *	2. If there is at least one non-deprecated ipif in the
20869 	 *	   IPMP group with a source address on the same subnet as
20870 	 *	   our destination, then fill ipif_arr[] only with
20871 	 *	   source addresses on the same subnet as our destination.
20872 	 *	   Note that because of (1), only the first
20873 	 *	   non-deprecated ipif found with a source address
20874 	 *	   matching the destination ends up in ipif_arr[].
20875 	 *
20876 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20877 	 *	   addresses not in the same subnet as our destination.
20878 	 *	   Again, because of (1), only the first off-subnet source
20879 	 *	   address will be chosen.
20880 	 *
20881 	 *	4. If there are no non-deprecated ipifs, then just use
20882 	 *	   the source address associated with the last deprecated
20883 	 *	   one we find that happens to be on the same subnet,
20884 	 *	   otherwise the first one not in the same subnet.
20885 	 */
20886 	specific_found = B_FALSE;
20887 	for (; till != NULL; till = till->ill_group_next) {
20888 		ipif_same_found = B_FALSE;
20889 		ipif_other_found = B_FALSE;
20890 		for (ipif = till->ill_ipif; ipif != NULL;
20891 		    ipif = ipif->ipif_next) {
20892 			if (!IPIF_CAN_LOOKUP(ipif))
20893 				continue;
20894 			/* Always skip NOLOCAL and ANYCAST interfaces */
20895 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20896 				continue;
20897 			if (!(ipif->ipif_flags & IPIF_UP) ||
20898 			    !ipif->ipif_addr_ready)
20899 				continue;
20900 			if (ipif->ipif_zoneid != zoneid &&
20901 			    ipif->ipif_zoneid != ALL_ZONES)
20902 				continue;
20903 			/*
20904 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20905 			 * but are not valid as source addresses.
20906 			 */
20907 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20908 				continue;
20909 
20910 			/*
20911 			 * Check compatibility of local address for
20912 			 * destination's default label if we're on a labeled
20913 			 * system.  Incompatible addresses can't be used at
20914 			 * all.
20915 			 */
20916 			if (dst_rhtp != NULL) {
20917 				boolean_t incompat;
20918 
20919 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20920 				    IPV4_VERSION, B_FALSE);
20921 				if (src_rhtp == NULL)
20922 					continue;
20923 				incompat =
20924 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20925 				    src_rhtp->tpc_tp.tp_doi !=
20926 				    dst_rhtp->tpc_tp.tp_doi ||
20927 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20928 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20929 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20930 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20931 				TPC_RELE(src_rhtp);
20932 				if (incompat)
20933 					continue;
20934 			}
20935 
20936 			/*
20937 			 * We prefer not to use all all-zones addresses, if we
20938 			 * can avoid it, as they pose problems with unlabeled
20939 			 * destinations.
20940 			 */
20941 			if (ipif->ipif_zoneid != ALL_ZONES) {
20942 				if (!specific_found &&
20943 				    (!same_subnet_only ||
20944 				    (ipif->ipif_net_mask & dst) ==
20945 				    ipif->ipif_subnet)) {
20946 					index = 0;
20947 					specific_found = B_TRUE;
20948 					ipif_other_found = B_FALSE;
20949 				}
20950 			} else {
20951 				if (specific_found)
20952 					continue;
20953 			}
20954 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
20955 				if (ipif_dep == NULL ||
20956 				    (ipif->ipif_net_mask & dst) ==
20957 				    ipif->ipif_subnet)
20958 					ipif_dep = ipif;
20959 				continue;
20960 			}
20961 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
20962 				/* found a source address in the same subnet */
20963 				if (!same_subnet_only) {
20964 					same_subnet_only = B_TRUE;
20965 					index = 0;
20966 				}
20967 				ipif_same_found = B_TRUE;
20968 			} else {
20969 				if (same_subnet_only || ipif_other_found)
20970 					continue;
20971 				ipif_other_found = B_TRUE;
20972 			}
20973 			ipif_arr[index++] = ipif;
20974 			if (index == MAX_IPIF_SELECT_SOURCE) {
20975 				wrapped = B_TRUE;
20976 				index = 0;
20977 			}
20978 			if (ipif_same_found)
20979 				break;
20980 		}
20981 	}
20982 
20983 	if (ipif_arr[0] == NULL) {
20984 		ipif = ipif_dep;
20985 	} else {
20986 		if (wrapped)
20987 			index = MAX_IPIF_SELECT_SOURCE;
20988 		ipif = ipif_arr[ipif_rand(ipst) % index];
20989 		ASSERT(ipif != NULL);
20990 	}
20991 
20992 	if (ipif != NULL) {
20993 		mutex_enter(&ipif->ipif_ill->ill_lock);
20994 		if (!IPIF_CAN_LOOKUP(ipif)) {
20995 			mutex_exit(&ipif->ipif_ill->ill_lock);
20996 			goto retry;
20997 		}
20998 		ipif_refhold_locked(ipif);
20999 		mutex_exit(&ipif->ipif_ill->ill_lock);
21000 	}
21001 
21002 	rw_exit(&ipst->ips_ill_g_lock);
21003 	if (usill != NULL)
21004 		ill_refrele(usill);
21005 	if (dst_rhtp != NULL)
21006 		TPC_RELE(dst_rhtp);
21007 
21008 #ifdef DEBUG
21009 	if (ipif == NULL) {
21010 		char buf1[INET6_ADDRSTRLEN];
21011 
21012 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
21013 		    ill->ill_name,
21014 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
21015 	} else {
21016 		char buf1[INET6_ADDRSTRLEN];
21017 		char buf2[INET6_ADDRSTRLEN];
21018 
21019 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
21020 		    ipif->ipif_ill->ill_name,
21021 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
21022 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
21023 		    buf2, sizeof (buf2))));
21024 	}
21025 #endif /* DEBUG */
21026 	return (ipif);
21027 }
21028 
21029 
21030 /*
21031  * If old_ipif is not NULL, see if ipif was derived from old
21032  * ipif and if so, recreate the interface route by re-doing
21033  * source address selection. This happens when ipif_down ->
21034  * ipif_update_other_ipifs calls us.
21035  *
21036  * If old_ipif is NULL, just redo the source address selection
21037  * if needed. This happens when illgrp_insert or ipif_up_done
21038  * calls us.
21039  */
21040 static void
21041 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
21042 {
21043 	ire_t *ire;
21044 	ire_t *ipif_ire;
21045 	queue_t *stq;
21046 	ipif_t *nipif;
21047 	ill_t *ill;
21048 	boolean_t need_rele = B_FALSE;
21049 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21050 
21051 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
21052 	ASSERT(IAM_WRITER_IPIF(ipif));
21053 
21054 	ill = ipif->ipif_ill;
21055 	if (!(ipif->ipif_flags &
21056 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
21057 		/*
21058 		 * Can't possibly have borrowed the source
21059 		 * from old_ipif.
21060 		 */
21061 		return;
21062 	}
21063 
21064 	/*
21065 	 * Is there any work to be done? No work if the address
21066 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21067 	 * ipif_select_source() does not borrow addresses from
21068 	 * NOLOCAL and ANYCAST interfaces).
21069 	 */
21070 	if ((old_ipif != NULL) &&
21071 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21072 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21073 	    (old_ipif->ipif_flags &
21074 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21075 		return;
21076 	}
21077 
21078 	/*
21079 	 * Perform the same checks as when creating the
21080 	 * IRE_INTERFACE in ipif_up_done.
21081 	 */
21082 	if (!(ipif->ipif_flags & IPIF_UP))
21083 		return;
21084 
21085 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21086 	    (ipif->ipif_subnet == INADDR_ANY))
21087 		return;
21088 
21089 	ipif_ire = ipif_to_ire(ipif);
21090 	if (ipif_ire == NULL)
21091 		return;
21092 
21093 	/*
21094 	 * We know that ipif uses some other source for its
21095 	 * IRE_INTERFACE. Is it using the source of this
21096 	 * old_ipif?
21097 	 */
21098 	if (old_ipif != NULL &&
21099 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21100 		ire_refrele(ipif_ire);
21101 		return;
21102 	}
21103 	if (ip_debug > 2) {
21104 		/* ip1dbg */
21105 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21106 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21107 	}
21108 
21109 	stq = ipif_ire->ire_stq;
21110 
21111 	/*
21112 	 * Can't use our source address. Select a different
21113 	 * source address for the IRE_INTERFACE.
21114 	 */
21115 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21116 	if (nipif == NULL) {
21117 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21118 		nipif = ipif;
21119 	} else {
21120 		need_rele = B_TRUE;
21121 	}
21122 
21123 	ire = ire_create(
21124 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21125 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21126 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21127 	    NULL,				/* no gateway */
21128 	    &ipif->ipif_mtu,			/* max frag */
21129 	    NULL,				/* no src nce */
21130 	    NULL,				/* no recv from queue */
21131 	    stq,				/* send-to queue */
21132 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21133 	    ipif,
21134 	    0,
21135 	    0,
21136 	    0,
21137 	    0,
21138 	    &ire_uinfo_null,
21139 	    NULL,
21140 	    NULL,
21141 	    ipst);
21142 
21143 	if (ire != NULL) {
21144 		ire_t *ret_ire;
21145 		int error;
21146 
21147 		/*
21148 		 * We don't need ipif_ire anymore. We need to delete
21149 		 * before we add so that ire_add does not detect
21150 		 * duplicates.
21151 		 */
21152 		ire_delete(ipif_ire);
21153 		ret_ire = ire;
21154 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21155 		ASSERT(error == 0);
21156 		ASSERT(ire == ret_ire);
21157 		/* Held in ire_add */
21158 		ire_refrele(ret_ire);
21159 	}
21160 	/*
21161 	 * Either we are falling through from above or could not
21162 	 * allocate a replacement.
21163 	 */
21164 	ire_refrele(ipif_ire);
21165 	if (need_rele)
21166 		ipif_refrele(nipif);
21167 }
21168 
21169 /*
21170  * This old_ipif is going away.
21171  *
21172  * Determine if any other ipif's is using our address as
21173  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21174  * IPIF_DEPRECATED).
21175  * Find the IRE_INTERFACE for such ipifs and recreate them
21176  * to use an different source address following the rules in
21177  * ipif_up_done.
21178  *
21179  * This function takes an illgrp as an argument so that illgrp_delete
21180  * can call this to update source address even after deleting the
21181  * old_ipif->ipif_ill from the ill group.
21182  */
21183 static void
21184 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21185 {
21186 	ipif_t *ipif;
21187 	ill_t *ill;
21188 	char	buf[INET6_ADDRSTRLEN];
21189 
21190 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21191 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21192 
21193 	ill = old_ipif->ipif_ill;
21194 
21195 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21196 	    ill->ill_name,
21197 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21198 	    buf, sizeof (buf))));
21199 	/*
21200 	 * If this part of a group, look at all ills as ipif_select_source
21201 	 * borrows source address across all the ills in the group.
21202 	 */
21203 	if (illgrp != NULL)
21204 		ill = illgrp->illgrp_ill;
21205 
21206 	for (; ill != NULL; ill = ill->ill_group_next) {
21207 		for (ipif = ill->ill_ipif; ipif != NULL;
21208 		    ipif = ipif->ipif_next) {
21209 
21210 			if (ipif == old_ipif)
21211 				continue;
21212 
21213 			ipif_recreate_interface_routes(old_ipif, ipif);
21214 		}
21215 	}
21216 }
21217 
21218 /* ARGSUSED */
21219 int
21220 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21221 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21222 {
21223 	/*
21224 	 * ill_phyint_reinit merged the v4 and v6 into a single
21225 	 * ipsq. Could also have become part of a ipmp group in the
21226 	 * process, and we might not have been able to complete the
21227 	 * operation in ipif_set_values, if we could not become
21228 	 * exclusive.  If so restart it here.
21229 	 */
21230 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21231 }
21232 
21233 /*
21234  * Can operate on either a module or a driver queue.
21235  * Returns an error if not a module queue.
21236  */
21237 /* ARGSUSED */
21238 int
21239 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21240     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21241 {
21242 	queue_t		*q1 = q;
21243 	char 		*cp;
21244 	char		interf_name[LIFNAMSIZ];
21245 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21246 
21247 	if (q->q_next == NULL) {
21248 		ip1dbg((
21249 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21250 		return (EINVAL);
21251 	}
21252 
21253 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21254 		return (EALREADY);
21255 
21256 	do {
21257 		q1 = q1->q_next;
21258 	} while (q1->q_next);
21259 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21260 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21261 
21262 	/*
21263 	 * Here we are not going to delay the ioack until after
21264 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21265 	 * original ioctl message before sending the requests.
21266 	 */
21267 	return (ipif_set_values(q, mp, interf_name, &ppa));
21268 }
21269 
21270 /* ARGSUSED */
21271 int
21272 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21273     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21274 {
21275 	return (ENXIO);
21276 }
21277 
21278 /*
21279  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21280  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21281  * ire_check_and_create_bcast()).
21282  */
21283 static ire_t **
21284 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21285 {
21286 	ipaddr_t addr;
21287 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21288 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21289 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21290 
21291 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21292 
21293 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21294 
21295 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21296 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21297 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21298 
21299 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21300 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21301 
21302 	/*
21303 	 * For backward compatibility, we create net broadcast IREs based on
21304 	 * the old "IP address class system", since some old machines only
21305 	 * respond to these class derived net broadcast.  However, we must not
21306 	 * create these net broadcast IREs if the subnetmask is shorter than
21307 	 * the IP address class based derived netmask.  Otherwise, we may
21308 	 * create a net broadcast address which is the same as an IP address
21309 	 * on the subnet -- and then TCP will refuse to talk to that address.
21310 	 */
21311 	if (netmask < subnetmask) {
21312 		addr = netmask & ipif->ipif_subnet;
21313 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21314 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21315 		    flags);
21316 	}
21317 
21318 	/*
21319 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21320 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21321 	 * created.  Creating these broadcast IREs will only create confusion
21322 	 * as `addr' will be the same as the IP address.
21323 	 */
21324 	if (subnetmask != 0xFFFFFFFF) {
21325 		addr = ipif->ipif_subnet;
21326 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21327 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21328 		    irep, flags);
21329 	}
21330 
21331 	return (irep);
21332 }
21333 
21334 /*
21335  * Broadcast IRE info structure used in the functions below.  Since we
21336  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21337  */
21338 typedef struct bcast_ireinfo {
21339 	uchar_t		bi_type;	/* BCAST_* value from below */
21340 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21341 			bi_needrep:1,	/* do we need to replace it? */
21342 			bi_haverep:1,	/* have we replaced it? */
21343 			bi_pad:5;
21344 	ipaddr_t	bi_addr;	/* IRE address */
21345 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21346 } bcast_ireinfo_t;
21347 
21348 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21349 
21350 /*
21351  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21352  * return B_TRUE if it should immediately be used to recreate the IRE.
21353  */
21354 static boolean_t
21355 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21356 {
21357 	ipaddr_t addr;
21358 
21359 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21360 
21361 	switch (bireinfop->bi_type) {
21362 	case BCAST_NET:
21363 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21364 		if (addr != bireinfop->bi_addr)
21365 			return (B_FALSE);
21366 		break;
21367 	case BCAST_SUBNET:
21368 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21369 			return (B_FALSE);
21370 		break;
21371 	}
21372 
21373 	bireinfop->bi_needrep = 1;
21374 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21375 		if (bireinfop->bi_backup == NULL)
21376 			bireinfop->bi_backup = ipif;
21377 		return (B_FALSE);
21378 	}
21379 	return (B_TRUE);
21380 }
21381 
21382 /*
21383  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21384  * them ala ire_check_and_create_bcast().
21385  */
21386 static ire_t **
21387 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21388 {
21389 	ipaddr_t mask, addr;
21390 
21391 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21392 
21393 	addr = bireinfop->bi_addr;
21394 	irep = ire_create_bcast(ipif, addr, irep);
21395 
21396 	switch (bireinfop->bi_type) {
21397 	case BCAST_NET:
21398 		mask = ip_net_mask(ipif->ipif_subnet);
21399 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21400 		break;
21401 	case BCAST_SUBNET:
21402 		mask = ipif->ipif_net_mask;
21403 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21404 		break;
21405 	}
21406 
21407 	bireinfop->bi_haverep = 1;
21408 	return (irep);
21409 }
21410 
21411 /*
21412  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21413  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21414  * that are going away are still needed.  If so, have ipif_create_bcast()
21415  * recreate them (except for the deprecated case, as explained below).
21416  */
21417 static ire_t **
21418 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21419     ire_t **irep)
21420 {
21421 	int i;
21422 	ipif_t *ipif;
21423 
21424 	ASSERT(!ill->ill_isv6);
21425 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21426 		/*
21427 		 * Skip this ipif if it's (a) the one being taken down, (b)
21428 		 * not in the same zone, or (c) has no valid local address.
21429 		 */
21430 		if (ipif == test_ipif ||
21431 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21432 		    ipif->ipif_subnet == 0 ||
21433 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21434 		    (IPIF_UP|IPIF_BROADCAST))
21435 			continue;
21436 
21437 		/*
21438 		 * For each dying IRE that hasn't yet been replaced, see if
21439 		 * `ipif' needs it and whether the IRE should be recreated on
21440 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21441 		 * will return B_FALSE even if `ipif' needs the IRE on the
21442 		 * hopes that we'll later find a needy non-deprecated ipif.
21443 		 * However, the ipif is recorded in bi_backup for possible
21444 		 * subsequent use by ipif_check_bcast_ires().
21445 		 */
21446 		for (i = 0; i < BCAST_COUNT; i++) {
21447 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21448 				continue;
21449 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21450 				continue;
21451 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21452 		}
21453 
21454 		/*
21455 		 * If we've replaced all of the broadcast IREs that are going
21456 		 * to be taken down, we know we're done.
21457 		 */
21458 		for (i = 0; i < BCAST_COUNT; i++) {
21459 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21460 				break;
21461 		}
21462 		if (i == BCAST_COUNT)
21463 			break;
21464 	}
21465 	return (irep);
21466 }
21467 
21468 /*
21469  * Check if `test_ipif' (which is going away) is associated with any existing
21470  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21471  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21472  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21473  *
21474  * This is necessary because broadcast IREs are shared.  In particular, a
21475  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21476  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21477  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21478  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21479  * same zone, they will share the same set of broadcast IREs.
21480  *
21481  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21482  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21483  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21484  */
21485 static void
21486 ipif_check_bcast_ires(ipif_t *test_ipif)
21487 {
21488 	ill_t		*ill = test_ipif->ipif_ill;
21489 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21490 	ire_t		**irep1, **irep = &ire_array[0];
21491 	uint_t 		i, willdie;
21492 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21493 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21494 
21495 	ASSERT(!test_ipif->ipif_isv6);
21496 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21497 
21498 	/*
21499 	 * No broadcast IREs for the LOOPBACK interface
21500 	 * or others such as point to point and IPIF_NOXMIT.
21501 	 */
21502 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21503 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21504 		return;
21505 
21506 	bzero(bireinfo, sizeof (bireinfo));
21507 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21508 	bireinfo[0].bi_addr = 0;
21509 
21510 	bireinfo[1].bi_type = BCAST_ALLONES;
21511 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21512 
21513 	bireinfo[2].bi_type = BCAST_NET;
21514 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21515 
21516 	if (test_ipif->ipif_net_mask != 0)
21517 		mask = test_ipif->ipif_net_mask;
21518 	bireinfo[3].bi_type = BCAST_SUBNET;
21519 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21520 
21521 	/*
21522 	 * Figure out what (if any) broadcast IREs will die as a result of
21523 	 * `test_ipif' going away.  If none will die, we're done.
21524 	 */
21525 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21526 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21527 		    test_ipif, ALL_ZONES, NULL,
21528 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21529 		if (ire != NULL) {
21530 			willdie++;
21531 			bireinfo[i].bi_willdie = 1;
21532 			ire_refrele(ire);
21533 		}
21534 	}
21535 
21536 	if (willdie == 0)
21537 		return;
21538 
21539 	/*
21540 	 * Walk through all the ipifs that will be affected by the dying IREs,
21541 	 * and recreate the IREs as necessary.
21542 	 */
21543 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21544 
21545 	/*
21546 	 * Scan through the set of broadcast IREs and see if there are any
21547 	 * that we need to replace that have not yet been replaced.  If so,
21548 	 * replace them using the appropriate backup ipif.
21549 	 */
21550 	for (i = 0; i < BCAST_COUNT; i++) {
21551 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21552 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21553 			    &bireinfo[i], irep);
21554 	}
21555 
21556 	/*
21557 	 * If we can't create all of them, don't add any of them.  (Code in
21558 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21559 	 * non-loopback copy and loopback copy for a given address.)
21560 	 */
21561 	for (irep1 = irep; irep1 > ire_array; ) {
21562 		irep1--;
21563 		if (*irep1 == NULL) {
21564 			ip0dbg(("ipif_check_bcast_ires: can't create "
21565 			    "IRE_BROADCAST, memory allocation failure\n"));
21566 			while (irep > ire_array) {
21567 				irep--;
21568 				if (*irep != NULL)
21569 					ire_delete(*irep);
21570 			}
21571 			return;
21572 		}
21573 	}
21574 
21575 	for (irep1 = irep; irep1 > ire_array; ) {
21576 		irep1--;
21577 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21578 			ire_refrele(*irep1);		/* Held in ire_add */
21579 	}
21580 }
21581 
21582 /*
21583  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21584  * from lifr_flags and the name from lifr_name.
21585  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21586  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21587  * Returns EINPROGRESS when mp has been consumed by queueing it on
21588  * ill_pending_mp and the ioctl will complete in ip_rput.
21589  *
21590  * Can operate on either a module or a driver queue.
21591  * Returns an error if not a module queue.
21592  */
21593 /* ARGSUSED */
21594 int
21595 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21596     ip_ioctl_cmd_t *ipip, void *if_req)
21597 {
21598 	ill_t	*ill = q->q_ptr;
21599 	phyint_t *phyi;
21600 	ip_stack_t *ipst;
21601 	struct lifreq *lifr = if_req;
21602 
21603 	ASSERT(ipif != NULL);
21604 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21605 
21606 	if (q->q_next == NULL) {
21607 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21608 		return (EINVAL);
21609 	}
21610 
21611 	/*
21612 	 * If we are not writer on 'q' then this interface exists already
21613 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
21614 	 * so return EALREADY.
21615 	 */
21616 	if (ill != ipif->ipif_ill)
21617 		return (EALREADY);
21618 
21619 	if (ill->ill_name[0] != '\0')
21620 		return (EALREADY);
21621 
21622 	/*
21623 	 * Set all the flags. Allows all kinds of override. Provide some
21624 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21625 	 * unless there is either multicast/broadcast support in the driver
21626 	 * or it is a pt-pt link.
21627 	 */
21628 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21629 		/* Meaningless to IP thus don't allow them to be set. */
21630 		ip1dbg(("ip_setname: EINVAL 1\n"));
21631 		return (EINVAL);
21632 	}
21633 
21634 	/*
21635 	 * If there's another ill already with the requested name, ensure
21636 	 * that it's of the same type.	Otherwise, ill_phyint_reinit() will
21637 	 * fuse together two unrelated ills, which will cause chaos.
21638 	 */
21639 	ipst = ill->ill_ipst;
21640 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
21641 	    lifr->lifr_name, NULL);
21642 	if (phyi != NULL) {
21643 		ill_t *ill_mate = phyi->phyint_illv4;
21644 
21645 		if (ill_mate == NULL)
21646 			ill_mate = phyi->phyint_illv6;
21647 		ASSERT(ill_mate != NULL);
21648 
21649 		if (ill_mate->ill_media->ip_m_mac_type !=
21650 		    ill->ill_media->ip_m_mac_type) {
21651 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
21652 			    "use the same ill name on differing media\n"));
21653 			return (EINVAL);
21654 		}
21655 	}
21656 
21657 	/*
21658 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21659 	 * ill_bcast_addr_length info.
21660 	 */
21661 	if (!ill->ill_needs_attach &&
21662 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21663 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21664 	    ill->ill_bcast_addr_length == 0)) {
21665 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21666 		ip1dbg(("ip_setname: EINVAL 2\n"));
21667 		return (EINVAL);
21668 	}
21669 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21670 	    ((lifr->lifr_flags & IFF_IPV6) ||
21671 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21672 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21673 		ip1dbg(("ip_setname: EINVAL 3\n"));
21674 		return (EINVAL);
21675 	}
21676 	if (lifr->lifr_flags & IFF_UP) {
21677 		/* Can only be set with SIOCSLIFFLAGS */
21678 		ip1dbg(("ip_setname: EINVAL 4\n"));
21679 		return (EINVAL);
21680 	}
21681 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21682 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21683 		ip1dbg(("ip_setname: EINVAL 5\n"));
21684 		return (EINVAL);
21685 	}
21686 	/*
21687 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21688 	 */
21689 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21690 	    !(lifr->lifr_flags & IFF_IPV6) &&
21691 	    !(ipif->ipif_isv6)) {
21692 		ip1dbg(("ip_setname: EINVAL 6\n"));
21693 		return (EINVAL);
21694 	}
21695 
21696 	/*
21697 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21698 	 * we have all the flags here. So, we assign rather than we OR.
21699 	 * We can't OR the flags here because we don't want to set
21700 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21701 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21702 	 * on lifr_flags value here.
21703 	 */
21704 	/*
21705 	 * This ill has not been inserted into the global list.
21706 	 * So we are still single threaded and don't need any lock
21707 	 */
21708 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS & ~IFF_DUPLICATE;
21709 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21710 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21711 
21712 	/* We started off as V4. */
21713 	if (ill->ill_flags & ILLF_IPV6) {
21714 		ill->ill_phyint->phyint_illv6 = ill;
21715 		ill->ill_phyint->phyint_illv4 = NULL;
21716 	}
21717 
21718 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
21719 }
21720 
21721 /* ARGSUSED */
21722 int
21723 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21724     ip_ioctl_cmd_t *ipip, void *if_req)
21725 {
21726 	/*
21727 	 * ill_phyint_reinit merged the v4 and v6 into a single
21728 	 * ipsq. Could also have become part of a ipmp group in the
21729 	 * process, and we might not have been able to complete the
21730 	 * slifname in ipif_set_values, if we could not become
21731 	 * exclusive.  If so restart it here
21732 	 */
21733 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21734 }
21735 
21736 /*
21737  * Return a pointer to the ipif which matches the index, IP version type and
21738  * zoneid.
21739  */
21740 ipif_t *
21741 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21742     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21743 {
21744 	ill_t	*ill;
21745 	ipif_t	*ipif = NULL;
21746 
21747 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21748 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21749 
21750 	if (err != NULL)
21751 		*err = 0;
21752 
21753 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
21754 	if (ill != NULL) {
21755 		mutex_enter(&ill->ill_lock);
21756 		for (ipif = ill->ill_ipif; ipif != NULL;
21757 		    ipif = ipif->ipif_next) {
21758 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
21759 			    zoneid == ipif->ipif_zoneid ||
21760 			    ipif->ipif_zoneid == ALL_ZONES)) {
21761 				ipif_refhold_locked(ipif);
21762 				break;
21763 			}
21764 		}
21765 		mutex_exit(&ill->ill_lock);
21766 		ill_refrele(ill);
21767 		if (ipif == NULL && err != NULL)
21768 			*err = ENXIO;
21769 	}
21770 	return (ipif);
21771 }
21772 
21773 typedef struct conn_change_s {
21774 	uint_t cc_old_ifindex;
21775 	uint_t cc_new_ifindex;
21776 } conn_change_t;
21777 
21778 /*
21779  * ipcl_walk function for changing interface index.
21780  */
21781 static void
21782 conn_change_ifindex(conn_t *connp, caddr_t arg)
21783 {
21784 	conn_change_t *connc;
21785 	uint_t old_ifindex;
21786 	uint_t new_ifindex;
21787 	int i;
21788 	ilg_t *ilg;
21789 
21790 	connc = (conn_change_t *)arg;
21791 	old_ifindex = connc->cc_old_ifindex;
21792 	new_ifindex = connc->cc_new_ifindex;
21793 
21794 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21795 		connp->conn_orig_bound_ifindex = new_ifindex;
21796 
21797 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21798 		connp->conn_orig_multicast_ifindex = new_ifindex;
21799 
21800 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21801 		ilg = &connp->conn_ilg[i];
21802 		if (ilg->ilg_orig_ifindex == old_ifindex)
21803 			ilg->ilg_orig_ifindex = new_ifindex;
21804 	}
21805 }
21806 
21807 /*
21808  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21809  * to new_index if it matches the old_index.
21810  *
21811  * Failovers typically happen within a group of ills. But somebody
21812  * can remove an ill from the group after a failover happened. If
21813  * we are setting the ifindex after this, we potentially need to
21814  * look at all the ills rather than just the ones in the group.
21815  * We cut down the work by looking at matching ill_net_types
21816  * and ill_types as we could not possibly grouped them together.
21817  */
21818 static void
21819 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21820 {
21821 	ill_t *ill;
21822 	ipif_t *ipif;
21823 	uint_t old_ifindex;
21824 	uint_t new_ifindex;
21825 	ilm_t *ilm;
21826 	ill_walk_context_t ctx;
21827 	ip_stack_t	*ipst = ill_orig->ill_ipst;
21828 
21829 	old_ifindex = connc->cc_old_ifindex;
21830 	new_ifindex = connc->cc_new_ifindex;
21831 
21832 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21833 	ill = ILL_START_WALK_ALL(&ctx, ipst);
21834 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21835 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21836 		    (ill_orig->ill_type != ill->ill_type)) {
21837 			continue;
21838 		}
21839 		for (ipif = ill->ill_ipif; ipif != NULL;
21840 		    ipif = ipif->ipif_next) {
21841 			if (ipif->ipif_orig_ifindex == old_ifindex)
21842 				ipif->ipif_orig_ifindex = new_ifindex;
21843 		}
21844 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21845 			if (ilm->ilm_orig_ifindex == old_ifindex)
21846 				ilm->ilm_orig_ifindex = new_ifindex;
21847 		}
21848 	}
21849 	rw_exit(&ipst->ips_ill_g_lock);
21850 }
21851 
21852 /*
21853  * We first need to ensure that the new index is unique, and
21854  * then carry the change across both v4 and v6 ill representation
21855  * of the physical interface.
21856  */
21857 /* ARGSUSED */
21858 int
21859 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21860     ip_ioctl_cmd_t *ipip, void *ifreq)
21861 {
21862 	ill_t		*ill;
21863 	ill_t		*ill_other;
21864 	phyint_t	*phyi;
21865 	int		old_index;
21866 	conn_change_t	connc;
21867 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21868 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21869 	uint_t	index;
21870 	ill_t	*ill_v4;
21871 	ill_t	*ill_v6;
21872 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21873 
21874 	if (ipip->ipi_cmd_type == IF_CMD)
21875 		index = ifr->ifr_index;
21876 	else
21877 		index = lifr->lifr_index;
21878 
21879 	/*
21880 	 * Only allow on physical interface. Also, index zero is illegal.
21881 	 *
21882 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21883 	 *
21884 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21885 	 *    implies a possible failback might have to happen. As failback
21886 	 *    depends on the old index, we should fail setting the index.
21887 	 *
21888 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21889 	 *    any addresses or multicast memberships are failed over to
21890 	 *    a non-STANDBY interface. As failback depends on the old
21891 	 *    index, we should fail setting the index for this case also.
21892 	 *
21893 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21894 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21895 	 */
21896 	ill = ipif->ipif_ill;
21897 	phyi = ill->ill_phyint;
21898 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21899 	    ipif->ipif_id != 0 || index == 0) {
21900 		return (EINVAL);
21901 	}
21902 	old_index = phyi->phyint_ifindex;
21903 
21904 	/* If the index is not changing, no work to do */
21905 	if (old_index == index)
21906 		return (0);
21907 
21908 	/*
21909 	 * Use ill_lookup_on_ifindex to determine if the
21910 	 * new index is unused and if so allow the change.
21911 	 */
21912 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
21913 	    ipst);
21914 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
21915 	    ipst);
21916 	if (ill_v6 != NULL || ill_v4 != NULL) {
21917 		if (ill_v4 != NULL)
21918 			ill_refrele(ill_v4);
21919 		if (ill_v6 != NULL)
21920 			ill_refrele(ill_v6);
21921 		return (EBUSY);
21922 	}
21923 
21924 	/*
21925 	 * The new index is unused. Set it in the phyint.
21926 	 * Locate the other ill so that we can send a routing
21927 	 * sockets message.
21928 	 */
21929 	if (ill->ill_isv6) {
21930 		ill_other = phyi->phyint_illv4;
21931 	} else {
21932 		ill_other = phyi->phyint_illv6;
21933 	}
21934 
21935 	phyi->phyint_ifindex = index;
21936 
21937 	/* Update SCTP's ILL list */
21938 	sctp_ill_reindex(ill, old_index);
21939 
21940 	connc.cc_old_ifindex = old_index;
21941 	connc.cc_new_ifindex = index;
21942 	ip_change_ifindex(ill, &connc);
21943 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
21944 
21945 	/* Send the routing sockets message */
21946 	ip_rts_ifmsg(ipif);
21947 	if (ill_other != NULL)
21948 		ip_rts_ifmsg(ill_other->ill_ipif);
21949 
21950 	return (0);
21951 }
21952 
21953 /* ARGSUSED */
21954 int
21955 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21956     ip_ioctl_cmd_t *ipip, void *ifreq)
21957 {
21958 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21959 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21960 
21961 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
21962 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21963 	/* Get the interface index */
21964 	if (ipip->ipi_cmd_type == IF_CMD) {
21965 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21966 	} else {
21967 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21968 	}
21969 	return (0);
21970 }
21971 
21972 /* ARGSUSED */
21973 int
21974 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21975     ip_ioctl_cmd_t *ipip, void *ifreq)
21976 {
21977 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21978 
21979 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
21980 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21981 	/* Get the interface zone */
21982 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21983 	lifr->lifr_zoneid = ipif->ipif_zoneid;
21984 	return (0);
21985 }
21986 
21987 /*
21988  * Set the zoneid of an interface.
21989  */
21990 /* ARGSUSED */
21991 int
21992 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21993     ip_ioctl_cmd_t *ipip, void *ifreq)
21994 {
21995 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21996 	int err = 0;
21997 	boolean_t need_up = B_FALSE;
21998 	zone_t *zptr;
21999 	zone_status_t status;
22000 	zoneid_t zoneid;
22001 
22002 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22003 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
22004 		if (!is_system_labeled())
22005 			return (ENOTSUP);
22006 		zoneid = GLOBAL_ZONEID;
22007 	}
22008 
22009 	/* cannot assign instance zero to a non-global zone */
22010 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
22011 		return (ENOTSUP);
22012 
22013 	/*
22014 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
22015 	 * the event of a race with the zone shutdown processing, since IP
22016 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
22017 	 * interface will be cleaned up even if the zone is shut down
22018 	 * immediately after the status check. If the interface can't be brought
22019 	 * down right away, and the zone is shut down before the restart
22020 	 * function is called, we resolve the possible races by rechecking the
22021 	 * zone status in the restart function.
22022 	 */
22023 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
22024 		return (EINVAL);
22025 	status = zone_status_get(zptr);
22026 	zone_rele(zptr);
22027 
22028 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
22029 		return (EINVAL);
22030 
22031 	if (ipif->ipif_flags & IPIF_UP) {
22032 		/*
22033 		 * If the interface is already marked up,
22034 		 * we call ipif_down which will take care
22035 		 * of ditching any IREs that have been set
22036 		 * up based on the old interface address.
22037 		 */
22038 		err = ipif_logical_down(ipif, q, mp);
22039 		if (err == EINPROGRESS)
22040 			return (err);
22041 		ipif_down_tail(ipif);
22042 		need_up = B_TRUE;
22043 	}
22044 
22045 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
22046 	return (err);
22047 }
22048 
22049 static int
22050 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
22051     queue_t *q, mblk_t *mp, boolean_t need_up)
22052 {
22053 	int	err = 0;
22054 	ip_stack_t	*ipst;
22055 
22056 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
22057 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22058 
22059 	if (CONN_Q(q))
22060 		ipst = CONNQ_TO_IPST(q);
22061 	else
22062 		ipst = ILLQ_TO_IPST(q);
22063 
22064 	/*
22065 	 * For exclusive stacks we don't allow a different zoneid than
22066 	 * global.
22067 	 */
22068 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
22069 	    zoneid != GLOBAL_ZONEID)
22070 		return (EINVAL);
22071 
22072 	/* Set the new zone id. */
22073 	ipif->ipif_zoneid = zoneid;
22074 
22075 	/* Update sctp list */
22076 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22077 
22078 	if (need_up) {
22079 		/*
22080 		 * Now bring the interface back up.  If this
22081 		 * is the only IPIF for the ILL, ipif_up
22082 		 * will have to re-bind to the device, so
22083 		 * we may get back EINPROGRESS, in which
22084 		 * case, this IOCTL will get completed in
22085 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22086 		 */
22087 		err = ipif_up(ipif, q, mp);
22088 	}
22089 	return (err);
22090 }
22091 
22092 /* ARGSUSED */
22093 int
22094 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22095     ip_ioctl_cmd_t *ipip, void *if_req)
22096 {
22097 	struct lifreq *lifr = (struct lifreq *)if_req;
22098 	zoneid_t zoneid;
22099 	zone_t *zptr;
22100 	zone_status_t status;
22101 
22102 	ASSERT(ipif->ipif_id != 0);
22103 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22104 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22105 		zoneid = GLOBAL_ZONEID;
22106 
22107 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22108 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22109 
22110 	/*
22111 	 * We recheck the zone status to resolve the following race condition:
22112 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22113 	 * 2) hme0:1 is up and can't be brought down right away;
22114 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22115 	 * 3) zone "myzone" is halted; the zone status switches to
22116 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22117 	 * the interfaces to remove - hme0:1 is not returned because it's not
22118 	 * yet in "myzone", so it won't be removed;
22119 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22120 	 * status check here, we would have hme0:1 in "myzone" after it's been
22121 	 * destroyed.
22122 	 * Note that if the status check fails, we need to bring the interface
22123 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22124 	 * ipif_up_done[_v6]().
22125 	 */
22126 	status = ZONE_IS_UNINITIALIZED;
22127 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22128 		status = zone_status_get(zptr);
22129 		zone_rele(zptr);
22130 	}
22131 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22132 		if (ipif->ipif_isv6) {
22133 			(void) ipif_up_done_v6(ipif);
22134 		} else {
22135 			(void) ipif_up_done(ipif);
22136 		}
22137 		return (EINVAL);
22138 	}
22139 
22140 	ipif_down_tail(ipif);
22141 
22142 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22143 	    B_TRUE));
22144 }
22145 
22146 /* ARGSUSED */
22147 int
22148 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22149 	ip_ioctl_cmd_t *ipip, void *ifreq)
22150 {
22151 	struct lifreq	*lifr = ifreq;
22152 
22153 	ASSERT(q->q_next == NULL);
22154 	ASSERT(CONN_Q(q));
22155 
22156 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22157 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22158 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22159 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22160 
22161 	return (0);
22162 }
22163 
22164 /* Find the previous ILL in this usesrc group */
22165 static ill_t *
22166 ill_prev_usesrc(ill_t *uill)
22167 {
22168 	ill_t *ill;
22169 
22170 	for (ill = uill->ill_usesrc_grp_next;
22171 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22172 	    ill = ill->ill_usesrc_grp_next)
22173 		/* do nothing */;
22174 	return (ill);
22175 }
22176 
22177 /*
22178  * Release all members of the usesrc group. This routine is called
22179  * from ill_delete when the interface being unplumbed is the
22180  * group head.
22181  */
22182 static void
22183 ill_disband_usesrc_group(ill_t *uill)
22184 {
22185 	ill_t *next_ill, *tmp_ill;
22186 	ip_stack_t	*ipst = uill->ill_ipst;
22187 
22188 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22189 	next_ill = uill->ill_usesrc_grp_next;
22190 
22191 	do {
22192 		ASSERT(next_ill != NULL);
22193 		tmp_ill = next_ill->ill_usesrc_grp_next;
22194 		ASSERT(tmp_ill != NULL);
22195 		next_ill->ill_usesrc_grp_next = NULL;
22196 		next_ill->ill_usesrc_ifindex = 0;
22197 		next_ill = tmp_ill;
22198 	} while (next_ill->ill_usesrc_ifindex != 0);
22199 	uill->ill_usesrc_grp_next = NULL;
22200 }
22201 
22202 /*
22203  * Remove the client usesrc ILL from the list and relink to a new list
22204  */
22205 int
22206 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22207 {
22208 	ill_t *ill, *tmp_ill;
22209 	ip_stack_t	*ipst = ucill->ill_ipst;
22210 
22211 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22212 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22213 
22214 	/*
22215 	 * Check if the usesrc client ILL passed in is not already
22216 	 * in use as a usesrc ILL i.e one whose source address is
22217 	 * in use OR a usesrc ILL is not already in use as a usesrc
22218 	 * client ILL
22219 	 */
22220 	if ((ucill->ill_usesrc_ifindex == 0) ||
22221 	    (uill->ill_usesrc_ifindex != 0)) {
22222 		return (-1);
22223 	}
22224 
22225 	ill = ill_prev_usesrc(ucill);
22226 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22227 
22228 	/* Remove from the current list */
22229 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22230 		/* Only two elements in the list */
22231 		ASSERT(ill->ill_usesrc_ifindex == 0);
22232 		ill->ill_usesrc_grp_next = NULL;
22233 	} else {
22234 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22235 	}
22236 
22237 	if (ifindex == 0) {
22238 		ucill->ill_usesrc_ifindex = 0;
22239 		ucill->ill_usesrc_grp_next = NULL;
22240 		return (0);
22241 	}
22242 
22243 	ucill->ill_usesrc_ifindex = ifindex;
22244 	tmp_ill = uill->ill_usesrc_grp_next;
22245 	uill->ill_usesrc_grp_next = ucill;
22246 	ucill->ill_usesrc_grp_next =
22247 	    (tmp_ill != NULL) ? tmp_ill : uill;
22248 	return (0);
22249 }
22250 
22251 /*
22252  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22253  * ip.c for locking details.
22254  */
22255 /* ARGSUSED */
22256 int
22257 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22258     ip_ioctl_cmd_t *ipip, void *ifreq)
22259 {
22260 	struct lifreq *lifr = (struct lifreq *)ifreq;
22261 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22262 	    ill_flag_changed = B_FALSE;
22263 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22264 	int err = 0, ret;
22265 	uint_t ifindex;
22266 	phyint_t *us_phyint, *us_cli_phyint;
22267 	ipsq_t *ipsq = NULL;
22268 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22269 
22270 	ASSERT(IAM_WRITER_IPIF(ipif));
22271 	ASSERT(q->q_next == NULL);
22272 	ASSERT(CONN_Q(q));
22273 
22274 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22275 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22276 
22277 	ASSERT(us_cli_phyint != NULL);
22278 
22279 	/*
22280 	 * If the client ILL is being used for IPMP, abort.
22281 	 * Note, this can be done before ipsq_try_enter since we are already
22282 	 * exclusive on this ILL
22283 	 */
22284 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22285 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22286 		return (EINVAL);
22287 	}
22288 
22289 	ifindex = lifr->lifr_index;
22290 	if (ifindex == 0) {
22291 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22292 			/* non usesrc group interface, nothing to reset */
22293 			return (0);
22294 		}
22295 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22296 		/* valid reset request */
22297 		reset_flg = B_TRUE;
22298 	}
22299 
22300 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22301 	    ip_process_ioctl, &err, ipst);
22302 
22303 	if (usesrc_ill == NULL) {
22304 		return (err);
22305 	}
22306 
22307 	/*
22308 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22309 	 * group nor can either of the interfaces be used for standy. So
22310 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22311 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22312 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22313 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22314 	 * the usesrc_cli_ill
22315 	 */
22316 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22317 	    NEW_OP, B_TRUE);
22318 	if (ipsq == NULL) {
22319 		err = EINPROGRESS;
22320 		/* Operation enqueued on the ipsq of the usesrc ILL */
22321 		goto done;
22322 	}
22323 
22324 	/* Check if the usesrc_ill is used for IPMP */
22325 	us_phyint = usesrc_ill->ill_phyint;
22326 	if ((us_phyint->phyint_groupname != NULL) ||
22327 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22328 		err = EINVAL;
22329 		goto done;
22330 	}
22331 
22332 	/*
22333 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22334 	 * already a client then return EINVAL
22335 	 */
22336 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22337 		err = EINVAL;
22338 		goto done;
22339 	}
22340 
22341 	/*
22342 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22343 	 * be then this is a duplicate operation.
22344 	 */
22345 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22346 		err = 0;
22347 		goto done;
22348 	}
22349 
22350 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22351 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22352 	    usesrc_ill->ill_isv6));
22353 
22354 	/*
22355 	 * The next step ensures that no new ires will be created referencing
22356 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22357 	 * we go through an ire walk deleting all ire caches that reference
22358 	 * the client ill. New ires referencing the client ill that are added
22359 	 * to the ire table before the ILL_CHANGING flag is set, will be
22360 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22361 	 * the client ill while the ILL_CHANGING flag is set will be failed
22362 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22363 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22364 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22365 	 * belong to the same usesrc group.
22366 	 */
22367 	mutex_enter(&usesrc_cli_ill->ill_lock);
22368 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22369 	mutex_exit(&usesrc_cli_ill->ill_lock);
22370 	ill_flag_changed = B_TRUE;
22371 
22372 	if (ipif->ipif_isv6)
22373 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22374 		    ALL_ZONES, ipst);
22375 	else
22376 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22377 		    ALL_ZONES, ipst);
22378 
22379 	/*
22380 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22381 	 * and the ill_usesrc_ifindex fields
22382 	 */
22383 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22384 
22385 	if (reset_flg) {
22386 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22387 		if (ret != 0) {
22388 			err = EINVAL;
22389 		}
22390 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22391 		goto done;
22392 	}
22393 
22394 	/*
22395 	 * Four possibilities to consider:
22396 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22397 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22398 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22399 	 * 4. Both are part of their respective usesrc groups
22400 	 */
22401 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22402 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22403 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22404 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22405 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22406 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22407 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22408 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22409 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22410 		/* Insert at head of list */
22411 		usesrc_cli_ill->ill_usesrc_grp_next =
22412 		    usesrc_ill->ill_usesrc_grp_next;
22413 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22414 	} else {
22415 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22416 		    ifindex);
22417 		if (ret != 0)
22418 			err = EINVAL;
22419 	}
22420 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22421 
22422 done:
22423 	if (ill_flag_changed) {
22424 		mutex_enter(&usesrc_cli_ill->ill_lock);
22425 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22426 		mutex_exit(&usesrc_cli_ill->ill_lock);
22427 	}
22428 	if (ipsq != NULL)
22429 		ipsq_exit(ipsq);
22430 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22431 	ill_refrele(usesrc_ill);
22432 	return (err);
22433 }
22434 
22435 /*
22436  * comparison function used by avl.
22437  */
22438 static int
22439 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22440 {
22441 
22442 	uint_t index;
22443 
22444 	ASSERT(phyip != NULL && index_ptr != NULL);
22445 
22446 	index = *((uint_t *)index_ptr);
22447 	/*
22448 	 * let the phyint with the lowest index be on top.
22449 	 */
22450 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22451 		return (1);
22452 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22453 		return (-1);
22454 	return (0);
22455 }
22456 
22457 /*
22458  * comparison function used by avl.
22459  */
22460 static int
22461 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22462 {
22463 	ill_t *ill;
22464 	int res = 0;
22465 
22466 	ASSERT(phyip != NULL && name_ptr != NULL);
22467 
22468 	if (((phyint_t *)phyip)->phyint_illv4)
22469 		ill = ((phyint_t *)phyip)->phyint_illv4;
22470 	else
22471 		ill = ((phyint_t *)phyip)->phyint_illv6;
22472 	ASSERT(ill != NULL);
22473 
22474 	res = strcmp(ill->ill_name, (char *)name_ptr);
22475 	if (res > 0)
22476 		return (1);
22477 	else if (res < 0)
22478 		return (-1);
22479 	return (0);
22480 }
22481 /*
22482  * This function is called from ill_delete when the ill is being
22483  * unplumbed. We remove the reference from the phyint and we also
22484  * free the phyint when there are no more references to it.
22485  */
22486 static void
22487 ill_phyint_free(ill_t *ill)
22488 {
22489 	phyint_t *phyi;
22490 	phyint_t *next_phyint;
22491 	ipsq_t *cur_ipsq;
22492 	ip_stack_t	*ipst = ill->ill_ipst;
22493 
22494 	ASSERT(ill->ill_phyint != NULL);
22495 
22496 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22497 	phyi = ill->ill_phyint;
22498 	ill->ill_phyint = NULL;
22499 	/*
22500 	 * ill_init allocates a phyint always to store the copy
22501 	 * of flags relevant to phyint. At that point in time, we could
22502 	 * not assign the name and hence phyint_illv4/v6 could not be
22503 	 * initialized. Later in ipif_set_values, we assign the name to
22504 	 * the ill, at which point in time we assign phyint_illv4/v6.
22505 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22506 	 */
22507 	if (ill->ill_flags & ILLF_IPV6) {
22508 		phyi->phyint_illv6 = NULL;
22509 	} else {
22510 		phyi->phyint_illv4 = NULL;
22511 	}
22512 	/*
22513 	 * ipif_down removes it from the group when the last ipif goes
22514 	 * down.
22515 	 */
22516 	ASSERT(ill->ill_group == NULL);
22517 
22518 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22519 		return;
22520 
22521 	/*
22522 	 * Make sure this phyint was put in the list.
22523 	 */
22524 	if (phyi->phyint_ifindex > 0) {
22525 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22526 		    phyi);
22527 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22528 		    phyi);
22529 	}
22530 	/*
22531 	 * remove phyint from the ipsq list.
22532 	 */
22533 	cur_ipsq = phyi->phyint_ipsq;
22534 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22535 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22536 	} else {
22537 		next_phyint = cur_ipsq->ipsq_phyint_list;
22538 		while (next_phyint != NULL) {
22539 			if (next_phyint->phyint_ipsq_next == phyi) {
22540 				next_phyint->phyint_ipsq_next =
22541 				    phyi->phyint_ipsq_next;
22542 				break;
22543 			}
22544 			next_phyint = next_phyint->phyint_ipsq_next;
22545 		}
22546 		ASSERT(next_phyint != NULL);
22547 	}
22548 	IPSQ_DEC_REF(cur_ipsq, ipst);
22549 
22550 	if (phyi->phyint_groupname_len != 0) {
22551 		ASSERT(phyi->phyint_groupname != NULL);
22552 		mi_free(phyi->phyint_groupname);
22553 	}
22554 	mi_free(phyi);
22555 }
22556 
22557 /*
22558  * Attach the ill to the phyint structure which can be shared by both
22559  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22560  * function is called from ipif_set_values and ill_lookup_on_name (for
22561  * loopback) where we know the name of the ill. We lookup the ill and if
22562  * there is one present already with the name use that phyint. Otherwise
22563  * reuse the one allocated by ill_init.
22564  */
22565 static void
22566 ill_phyint_reinit(ill_t *ill)
22567 {
22568 	boolean_t isv6 = ill->ill_isv6;
22569 	phyint_t *phyi_old;
22570 	phyint_t *phyi;
22571 	avl_index_t where = 0;
22572 	ill_t	*ill_other = NULL;
22573 	ipsq_t	*ipsq;
22574 	ip_stack_t	*ipst = ill->ill_ipst;
22575 
22576 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22577 
22578 	phyi_old = ill->ill_phyint;
22579 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22580 	    phyi_old->phyint_illv6 == NULL));
22581 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22582 	    phyi_old->phyint_illv4 == NULL));
22583 	ASSERT(phyi_old->phyint_ifindex == 0);
22584 
22585 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22586 	    ill->ill_name, &where);
22587 
22588 	/*
22589 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22590 	 *    the global list of ills. So no other thread could have located
22591 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22592 	 * 2. Now locate the other protocol instance of this ill.
22593 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22594 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22595 	 *    of neither ill can change.
22596 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22597 	 *    other ill.
22598 	 * 5. Release all locks.
22599 	 */
22600 
22601 	/*
22602 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22603 	 * we are initializing IPv4.
22604 	 */
22605 	if (phyi != NULL) {
22606 		ill_other = (isv6) ? phyi->phyint_illv4 :
22607 		    phyi->phyint_illv6;
22608 		ASSERT(ill_other->ill_phyint != NULL);
22609 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22610 		    (!isv6 && ill_other->ill_isv6));
22611 		GRAB_ILL_LOCKS(ill, ill_other);
22612 		/*
22613 		 * We are potentially throwing away phyint_flags which
22614 		 * could be different from the one that we obtain from
22615 		 * ill_other->ill_phyint. But it is okay as we are assuming
22616 		 * that the state maintained within IP is correct.
22617 		 */
22618 		mutex_enter(&phyi->phyint_lock);
22619 		if (isv6) {
22620 			ASSERT(phyi->phyint_illv6 == NULL);
22621 			phyi->phyint_illv6 = ill;
22622 		} else {
22623 			ASSERT(phyi->phyint_illv4 == NULL);
22624 			phyi->phyint_illv4 = ill;
22625 		}
22626 		/*
22627 		 * This is a new ill, currently undergoing SLIFNAME
22628 		 * So we could not have joined an IPMP group until now.
22629 		 */
22630 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22631 		    phyi_old->phyint_groupname == NULL);
22632 
22633 		/*
22634 		 * This phyi_old is going away. Decref ipsq_refs and
22635 		 * assert it is zero. The ipsq itself will be freed in
22636 		 * ipsq_exit
22637 		 */
22638 		ipsq = phyi_old->phyint_ipsq;
22639 		IPSQ_DEC_REF(ipsq, ipst);
22640 		ASSERT(ipsq->ipsq_refs == 0);
22641 		/* Get the singleton phyint out of the ipsq list */
22642 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22643 		ipsq->ipsq_phyint_list = NULL;
22644 		phyi_old->phyint_illv4 = NULL;
22645 		phyi_old->phyint_illv6 = NULL;
22646 		mi_free(phyi_old);
22647 	} else {
22648 		mutex_enter(&ill->ill_lock);
22649 		/*
22650 		 * We don't need to acquire any lock, since
22651 		 * the ill is not yet visible globally  and we
22652 		 * have not yet released the ill_g_lock.
22653 		 */
22654 		phyi = phyi_old;
22655 		mutex_enter(&phyi->phyint_lock);
22656 		/* XXX We need a recovery strategy here. */
22657 		if (!phyint_assign_ifindex(phyi, ipst))
22658 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22659 
22660 		/* No IPMP group yet, thus the hook uses the ifindex */
22661 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22662 
22663 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22664 		    (void *)phyi, where);
22665 
22666 		(void) avl_find(&ipst->ips_phyint_g_list->
22667 		    phyint_list_avl_by_index,
22668 		    &phyi->phyint_ifindex, &where);
22669 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22670 		    (void *)phyi, where);
22671 	}
22672 
22673 	/*
22674 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22675 	 * pending mp is not affected because that is per ill basis.
22676 	 */
22677 	ill->ill_phyint = phyi;
22678 
22679 	/*
22680 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22681 	 * We do this here as when the first ipif was allocated,
22682 	 * ipif_allocate does not know the right interface index.
22683 	 */
22684 
22685 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22686 	/*
22687 	 * Now that the phyint's ifindex has been assigned, complete the
22688 	 * remaining
22689 	 */
22690 
22691 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22692 	if (ill->ill_isv6) {
22693 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22694 		    ill->ill_phyint->phyint_ifindex;
22695 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22696 	} else {
22697 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22698 	}
22699 
22700 	/*
22701 	 * Generate an event within the hooks framework to indicate that
22702 	 * a new interface has just been added to IP.  For this event to
22703 	 * be generated, the network interface must, at least, have an
22704 	 * ifindex assigned to it.
22705 	 *
22706 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22707 	 * that the ordering of delivered events to listeners matches the
22708 	 * order of them in the kernel.
22709 	 *
22710 	 * This function could be called from ill_lookup_on_name. In that case
22711 	 * the interface is loopback "lo", which will not generate a NIC event.
22712 	 */
22713 	if (ill->ill_name_length <= 2 ||
22714 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22715 		/*
22716 		 * Generate nic plumb event for ill_name even if
22717 		 * ipmp_hook_emulation is set. That avoids generating events
22718 		 * for the ill_names should ipmp_hook_emulation be turned on
22719 		 * later.
22720 		 */
22721 		ill_nic_info_plumb(ill, B_FALSE);
22722 	}
22723 	RELEASE_ILL_LOCKS(ill, ill_other);
22724 	mutex_exit(&phyi->phyint_lock);
22725 }
22726 
22727 /*
22728  * Allocate a NE_PLUMB nic info event and store in the ill.
22729  * If 'group' is set we do it for the group name, otherwise the ill name.
22730  * It will be sent when we leave the ipsq.
22731  */
22732 void
22733 ill_nic_info_plumb(ill_t *ill, boolean_t group)
22734 {
22735 	phyint_t	*phyi = ill->ill_phyint;
22736 	char		*name;
22737 	int		namelen;
22738 
22739 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22740 
22741 	if (group) {
22742 		ASSERT(phyi->phyint_groupname_len != 0);
22743 		namelen = phyi->phyint_groupname_len;
22744 		name = phyi->phyint_groupname;
22745 	} else {
22746 		namelen = ill->ill_name_length;
22747 		name = ill->ill_name;
22748 	}
22749 
22750 	(void) ill_hook_event_create(ill, 0, NE_PLUMB, name, namelen);
22751 }
22752 
22753 /*
22754  * Unhook the nic event message from the ill and enqueue it
22755  * into the nic event taskq.
22756  */
22757 void
22758 ill_nic_info_dispatch(ill_t *ill)
22759 {
22760 	hook_nic_event_int_t *info;
22761 
22762 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22763 
22764 	if ((info = ill->ill_nic_event_info) != NULL) {
22765 		if (ddi_taskq_dispatch(eventq_queue_nic,
22766 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
22767 			ip2dbg(("ill_nic_info_dispatch: "
22768 			    "ddi_taskq_dispatch failed\n"));
22769 			if (info->hnei_event.hne_data != NULL) {
22770 				kmem_free(info->hnei_event.hne_data,
22771 				    info->hnei_event.hne_datalen);
22772 			}
22773 			kmem_free(info, sizeof (*info));
22774 		}
22775 		ill->ill_nic_event_info = NULL;
22776 	}
22777 }
22778 
22779 /*
22780  * Notify any downstream modules of the name of this interface.
22781  * An M_IOCTL is used even though we don't expect a successful reply.
22782  * Any reply message from the driver (presumably an M_IOCNAK) will
22783  * eventually get discarded somewhere upstream.  The message format is
22784  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22785  * to IP.
22786  */
22787 static void
22788 ip_ifname_notify(ill_t *ill, queue_t *q)
22789 {
22790 	mblk_t *mp1, *mp2;
22791 	struct iocblk *iocp;
22792 	struct lifreq *lifr;
22793 
22794 	mp1 = mkiocb(SIOCSLIFNAME);
22795 	if (mp1 == NULL)
22796 		return;
22797 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22798 	if (mp2 == NULL) {
22799 		freeb(mp1);
22800 		return;
22801 	}
22802 
22803 	mp1->b_cont = mp2;
22804 	iocp = (struct iocblk *)mp1->b_rptr;
22805 	iocp->ioc_count = sizeof (struct lifreq);
22806 
22807 	lifr = (struct lifreq *)mp2->b_rptr;
22808 	mp2->b_wptr += sizeof (struct lifreq);
22809 	bzero(lifr, sizeof (struct lifreq));
22810 
22811 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22812 	lifr->lifr_ppa = ill->ill_ppa;
22813 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22814 
22815 	putnext(q, mp1);
22816 }
22817 
22818 static int
22819 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22820 {
22821 	int err;
22822 	ip_stack_t	*ipst = ill->ill_ipst;
22823 
22824 	/* Set the obsolete NDD per-interface forwarding name. */
22825 	err = ill_set_ndd_name(ill);
22826 	if (err != 0) {
22827 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22828 		    err);
22829 	}
22830 
22831 	/* Tell downstream modules where they are. */
22832 	ip_ifname_notify(ill, q);
22833 
22834 	/*
22835 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22836 	 * Error cases are ENOMEM ...
22837 	 */
22838 	err = ill_dl_phys(ill, ipif, mp, q);
22839 
22840 	/*
22841 	 * If there is no IRE expiration timer running, get one started.
22842 	 * igmp and mld timers will be triggered by the first multicast
22843 	 */
22844 	if (ipst->ips_ip_ire_expire_id == 0) {
22845 		/*
22846 		 * acquire the lock and check again.
22847 		 */
22848 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
22849 		if (ipst->ips_ip_ire_expire_id == 0) {
22850 			ipst->ips_ip_ire_expire_id = timeout(
22851 			    ip_trash_timer_expire, ipst,
22852 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
22853 		}
22854 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
22855 	}
22856 
22857 	if (ill->ill_isv6) {
22858 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
22859 		if (ipst->ips_mld_slowtimeout_id == 0) {
22860 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
22861 			    (void *)ipst,
22862 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22863 		}
22864 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
22865 	} else {
22866 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
22867 		if (ipst->ips_igmp_slowtimeout_id == 0) {
22868 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
22869 			    (void *)ipst,
22870 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22871 		}
22872 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
22873 	}
22874 
22875 	return (err);
22876 }
22877 
22878 /*
22879  * Common routine for ppa and ifname setting. Should be called exclusive.
22880  *
22881  * Returns EINPROGRESS when mp has been consumed by queueing it on
22882  * ill_pending_mp and the ioctl will complete in ip_rput.
22883  *
22884  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22885  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22886  * For SLIFNAME, we pass these values back to the userland.
22887  */
22888 static int
22889 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22890 {
22891 	ill_t	*ill;
22892 	ipif_t	*ipif;
22893 	ipsq_t	*ipsq;
22894 	char	*ppa_ptr;
22895 	char	*old_ptr;
22896 	char	old_char;
22897 	int	error;
22898 	ip_stack_t	*ipst;
22899 
22900 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
22901 	ASSERT(q->q_next != NULL);
22902 	ASSERT(interf_name != NULL);
22903 
22904 	ill = (ill_t *)q->q_ptr;
22905 	ipst = ill->ill_ipst;
22906 
22907 	ASSERT(ill->ill_ipst != NULL);
22908 	ASSERT(ill->ill_name[0] == '\0');
22909 	ASSERT(IAM_WRITER_ILL(ill));
22910 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
22911 	ASSERT(ill->ill_ppa == UINT_MAX);
22912 
22913 	/* The ppa is sent down by ifconfig or is chosen */
22914 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
22915 		return (EINVAL);
22916 	}
22917 
22918 	/*
22919 	 * make sure ppa passed in is same as ppa in the name.
22920 	 * This check is not made when ppa == UINT_MAX in that case ppa
22921 	 * in the name could be anything. System will choose a ppa and
22922 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
22923 	 */
22924 	if (*new_ppa_ptr != UINT_MAX) {
22925 		/* stoi changes the pointer */
22926 		old_ptr = ppa_ptr;
22927 		/*
22928 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
22929 		 * (they don't have an externally visible ppa).  We assign one
22930 		 * here so that we can manage the interface.  Note that in
22931 		 * the past this value was always 0 for DLPI 1 drivers.
22932 		 */
22933 		if (*new_ppa_ptr == 0)
22934 			*new_ppa_ptr = stoi(&old_ptr);
22935 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
22936 			return (EINVAL);
22937 	}
22938 	/*
22939 	 * terminate string before ppa
22940 	 * save char at that location.
22941 	 */
22942 	old_char = ppa_ptr[0];
22943 	ppa_ptr[0] = '\0';
22944 
22945 	ill->ill_ppa = *new_ppa_ptr;
22946 	/*
22947 	 * Finish as much work now as possible before calling ill_glist_insert
22948 	 * which makes the ill globally visible and also merges it with the
22949 	 * other protocol instance of this phyint. The remaining work is
22950 	 * done after entering the ipsq which may happen sometime later.
22951 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
22952 	 */
22953 	ipif = ill->ill_ipif;
22954 
22955 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
22956 	ipif_assign_seqid(ipif);
22957 
22958 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
22959 		ill->ill_flags |= ILLF_IPV4;
22960 
22961 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
22962 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
22963 
22964 	if (ill->ill_flags & ILLF_IPV6) {
22965 
22966 		ill->ill_isv6 = B_TRUE;
22967 		if (ill->ill_rq != NULL) {
22968 			ill->ill_rq->q_qinfo = &iprinitv6;
22969 			ill->ill_wq->q_qinfo = &ipwinitv6;
22970 		}
22971 
22972 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
22973 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
22974 		ipif->ipif_v6src_addr = ipv6_all_zeros;
22975 		ipif->ipif_v6subnet = ipv6_all_zeros;
22976 		ipif->ipif_v6net_mask = ipv6_all_zeros;
22977 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
22978 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
22979 		/*
22980 		 * point-to-point or Non-mulicast capable
22981 		 * interfaces won't do NUD unless explicitly
22982 		 * configured to do so.
22983 		 */
22984 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
22985 		    !(ill->ill_flags & ILLF_MULTICAST)) {
22986 			ill->ill_flags |= ILLF_NONUD;
22987 		}
22988 		/* Make sure IPv4 specific flag is not set on IPv6 if */
22989 		if (ill->ill_flags & ILLF_NOARP) {
22990 			/*
22991 			 * Note: xresolv interfaces will eventually need
22992 			 * NOARP set here as well, but that will require
22993 			 * those external resolvers to have some
22994 			 * knowledge of that flag and act appropriately.
22995 			 * Not to be changed at present.
22996 			 */
22997 			ill->ill_flags &= ~ILLF_NOARP;
22998 		}
22999 		/*
23000 		 * Set the ILLF_ROUTER flag according to the global
23001 		 * IPv6 forwarding policy.
23002 		 */
23003 		if (ipst->ips_ipv6_forward != 0)
23004 			ill->ill_flags |= ILLF_ROUTER;
23005 	} else if (ill->ill_flags & ILLF_IPV4) {
23006 		ill->ill_isv6 = B_FALSE;
23007 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
23008 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
23009 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
23010 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
23011 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
23012 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
23013 		/*
23014 		 * Set the ILLF_ROUTER flag according to the global
23015 		 * IPv4 forwarding policy.
23016 		 */
23017 		if (ipst->ips_ip_g_forward != 0)
23018 			ill->ill_flags |= ILLF_ROUTER;
23019 	}
23020 
23021 	ASSERT(ill->ill_phyint != NULL);
23022 
23023 	/*
23024 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
23025 	 * be completed in ill_glist_insert -> ill_phyint_reinit
23026 	 */
23027 	if (!ill_allocate_mibs(ill))
23028 		return (ENOMEM);
23029 
23030 	/*
23031 	 * Pick a default sap until we get the DL_INFO_ACK back from
23032 	 * the driver.
23033 	 */
23034 	if (ill->ill_sap == 0) {
23035 		if (ill->ill_isv6)
23036 			ill->ill_sap  = IP6_DL_SAP;
23037 		else
23038 			ill->ill_sap  = IP_DL_SAP;
23039 	}
23040 
23041 	ill->ill_ifname_pending = 1;
23042 	ill->ill_ifname_pending_err = 0;
23043 
23044 	ill_refhold(ill);
23045 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
23046 	if ((error = ill_glist_insert(ill, interf_name,
23047 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
23048 		ill->ill_ppa = UINT_MAX;
23049 		ill->ill_name[0] = '\0';
23050 		/*
23051 		 * undo null termination done above.
23052 		 */
23053 		ppa_ptr[0] = old_char;
23054 		rw_exit(&ipst->ips_ill_g_lock);
23055 		ill_refrele(ill);
23056 		return (error);
23057 	}
23058 
23059 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
23060 
23061 	/*
23062 	 * When we return the buffer pointed to by interf_name should contain
23063 	 * the same name as in ill_name.
23064 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
23065 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
23066 	 * so copy full name and update the ppa ptr.
23067 	 * When ppa passed in != UINT_MAX all values are correct just undo
23068 	 * null termination, this saves a bcopy.
23069 	 */
23070 	if (*new_ppa_ptr == UINT_MAX) {
23071 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23072 		*new_ppa_ptr = ill->ill_ppa;
23073 	} else {
23074 		/*
23075 		 * undo null termination done above.
23076 		 */
23077 		ppa_ptr[0] = old_char;
23078 	}
23079 
23080 	/* Let SCTP know about this ILL */
23081 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23082 
23083 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23084 	    B_TRUE);
23085 
23086 	rw_exit(&ipst->ips_ill_g_lock);
23087 	ill_refrele(ill);
23088 	if (ipsq == NULL)
23089 		return (EINPROGRESS);
23090 
23091 	/*
23092 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23093 	 */
23094 	if (ipsq->ipsq_current_ipif == NULL)
23095 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23096 	else
23097 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23098 
23099 	error = ipif_set_values_tail(ill, ipif, mp, q);
23100 	ipsq_exit(ipsq);
23101 	if (error != 0 && error != EINPROGRESS) {
23102 		/*
23103 		 * restore previous values
23104 		 */
23105 		ill->ill_isv6 = B_FALSE;
23106 	}
23107 	return (error);
23108 }
23109 
23110 
23111 void
23112 ipif_init(ip_stack_t *ipst)
23113 {
23114 	hrtime_t hrt;
23115 	int i;
23116 
23117 	/*
23118 	 * Can't call drv_getparm here as it is too early in the boot.
23119 	 * As we use ipif_src_random just for picking a different
23120 	 * source address everytime, this need not be really random.
23121 	 */
23122 	hrt = gethrtime();
23123 	ipst->ips_ipif_src_random =
23124 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23125 
23126 	for (i = 0; i < MAX_G_HEADS; i++) {
23127 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23128 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23129 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23130 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23131 	}
23132 
23133 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23134 	    ill_phyint_compare_index,
23135 	    sizeof (phyint_t),
23136 	    offsetof(struct phyint, phyint_avl_by_index));
23137 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23138 	    ill_phyint_compare_name,
23139 	    sizeof (phyint_t),
23140 	    offsetof(struct phyint, phyint_avl_by_name));
23141 }
23142 
23143 /*
23144  * Lookup the ipif corresponding to the onlink destination address. For
23145  * point-to-point interfaces, it matches with remote endpoint destination
23146  * address. For point-to-multipoint interfaces it only tries to match the
23147  * destination with the interface's subnet address. The longest, most specific
23148  * match is found to take care of such rare network configurations like -
23149  * le0: 129.146.1.1/16
23150  * le1: 129.146.2.2/24
23151  * It is used only by SO_DONTROUTE at the moment.
23152  */
23153 ipif_t *
23154 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23155 {
23156 	ipif_t	*ipif, *best_ipif;
23157 	ill_t	*ill;
23158 	ill_walk_context_t ctx;
23159 
23160 	ASSERT(zoneid != ALL_ZONES);
23161 	best_ipif = NULL;
23162 
23163 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23164 	ill = ILL_START_WALK_V4(&ctx, ipst);
23165 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23166 		mutex_enter(&ill->ill_lock);
23167 		for (ipif = ill->ill_ipif; ipif != NULL;
23168 		    ipif = ipif->ipif_next) {
23169 			if (!IPIF_CAN_LOOKUP(ipif))
23170 				continue;
23171 			if (ipif->ipif_zoneid != zoneid &&
23172 			    ipif->ipif_zoneid != ALL_ZONES)
23173 				continue;
23174 			/*
23175 			 * Point-to-point case. Look for exact match with
23176 			 * destination address.
23177 			 */
23178 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23179 				if (ipif->ipif_pp_dst_addr == addr) {
23180 					ipif_refhold_locked(ipif);
23181 					mutex_exit(&ill->ill_lock);
23182 					rw_exit(&ipst->ips_ill_g_lock);
23183 					if (best_ipif != NULL)
23184 						ipif_refrele(best_ipif);
23185 					return (ipif);
23186 				}
23187 			} else if (ipif->ipif_subnet == (addr &
23188 			    ipif->ipif_net_mask)) {
23189 				/*
23190 				 * Point-to-multipoint case. Looping through to
23191 				 * find the most specific match. If there are
23192 				 * multiple best match ipif's then prefer ipif's
23193 				 * that are UP. If there is only one best match
23194 				 * ipif and it is DOWN we must still return it.
23195 				 */
23196 				if ((best_ipif == NULL) ||
23197 				    (ipif->ipif_net_mask >
23198 				    best_ipif->ipif_net_mask) ||
23199 				    ((ipif->ipif_net_mask ==
23200 				    best_ipif->ipif_net_mask) &&
23201 				    ((ipif->ipif_flags & IPIF_UP) &&
23202 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23203 					ipif_refhold_locked(ipif);
23204 					mutex_exit(&ill->ill_lock);
23205 					rw_exit(&ipst->ips_ill_g_lock);
23206 					if (best_ipif != NULL)
23207 						ipif_refrele(best_ipif);
23208 					best_ipif = ipif;
23209 					rw_enter(&ipst->ips_ill_g_lock,
23210 					    RW_READER);
23211 					mutex_enter(&ill->ill_lock);
23212 				}
23213 			}
23214 		}
23215 		mutex_exit(&ill->ill_lock);
23216 	}
23217 	rw_exit(&ipst->ips_ill_g_lock);
23218 	return (best_ipif);
23219 }
23220 
23221 /*
23222  * Save enough information so that we can recreate the IRE if
23223  * the interface goes down and then up.
23224  */
23225 static void
23226 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23227 {
23228 	mblk_t	*save_mp;
23229 
23230 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23231 	if (save_mp != NULL) {
23232 		ifrt_t	*ifrt;
23233 
23234 		save_mp->b_wptr += sizeof (ifrt_t);
23235 		ifrt = (ifrt_t *)save_mp->b_rptr;
23236 		bzero(ifrt, sizeof (ifrt_t));
23237 		ifrt->ifrt_type = ire->ire_type;
23238 		ifrt->ifrt_addr = ire->ire_addr;
23239 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23240 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23241 		ifrt->ifrt_mask = ire->ire_mask;
23242 		ifrt->ifrt_flags = ire->ire_flags;
23243 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23244 		mutex_enter(&ipif->ipif_saved_ire_lock);
23245 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23246 		ipif->ipif_saved_ire_mp = save_mp;
23247 		ipif->ipif_saved_ire_cnt++;
23248 		mutex_exit(&ipif->ipif_saved_ire_lock);
23249 	}
23250 }
23251 
23252 static void
23253 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23254 {
23255 	mblk_t	**mpp;
23256 	mblk_t	*mp;
23257 	ifrt_t	*ifrt;
23258 
23259 	/* Remove from ipif_saved_ire_mp list if it is there */
23260 	mutex_enter(&ipif->ipif_saved_ire_lock);
23261 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23262 	    mpp = &(*mpp)->b_cont) {
23263 		/*
23264 		 * On a given ipif, the triple of address, gateway and
23265 		 * mask is unique for each saved IRE (in the case of
23266 		 * ordinary interface routes, the gateway address is
23267 		 * all-zeroes).
23268 		 */
23269 		mp = *mpp;
23270 		ifrt = (ifrt_t *)mp->b_rptr;
23271 		if (ifrt->ifrt_addr == ire->ire_addr &&
23272 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23273 		    ifrt->ifrt_mask == ire->ire_mask) {
23274 			*mpp = mp->b_cont;
23275 			ipif->ipif_saved_ire_cnt--;
23276 			freeb(mp);
23277 			break;
23278 		}
23279 	}
23280 	mutex_exit(&ipif->ipif_saved_ire_lock);
23281 }
23282 
23283 /*
23284  * IP multirouting broadcast routes handling
23285  * Append CGTP broadcast IREs to regular ones created
23286  * at ifconfig time.
23287  */
23288 static void
23289 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23290 {
23291 	ire_t *ire_prim;
23292 
23293 	ASSERT(ire != NULL);
23294 	ASSERT(ire_dst != NULL);
23295 
23296 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23297 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23298 	if (ire_prim != NULL) {
23299 		/*
23300 		 * We are in the special case of broadcasts for
23301 		 * CGTP. We add an IRE_BROADCAST that holds
23302 		 * the RTF_MULTIRT flag, the destination
23303 		 * address of ire_dst and the low level
23304 		 * info of ire_prim. In other words, CGTP
23305 		 * broadcast is added to the redundant ipif.
23306 		 */
23307 		ipif_t *ipif_prim;
23308 		ire_t  *bcast_ire;
23309 
23310 		ipif_prim = ire_prim->ire_ipif;
23311 
23312 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23313 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23314 		    (void *)ire_dst, (void *)ire_prim,
23315 		    (void *)ipif_prim));
23316 
23317 		bcast_ire = ire_create(
23318 		    (uchar_t *)&ire->ire_addr,
23319 		    (uchar_t *)&ip_g_all_ones,
23320 		    (uchar_t *)&ire_dst->ire_src_addr,
23321 		    (uchar_t *)&ire->ire_gateway_addr,
23322 		    &ipif_prim->ipif_mtu,
23323 		    NULL,
23324 		    ipif_prim->ipif_rq,
23325 		    ipif_prim->ipif_wq,
23326 		    IRE_BROADCAST,
23327 		    ipif_prim,
23328 		    0,
23329 		    0,
23330 		    0,
23331 		    ire->ire_flags,
23332 		    &ire_uinfo_null,
23333 		    NULL,
23334 		    NULL,
23335 		    ipst);
23336 
23337 		if (bcast_ire != NULL) {
23338 
23339 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23340 			    B_FALSE) == 0) {
23341 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23342 				    "added bcast_ire %p\n",
23343 				    (void *)bcast_ire));
23344 
23345 				ipif_save_ire(bcast_ire->ire_ipif,
23346 				    bcast_ire);
23347 				ire_refrele(bcast_ire);
23348 			}
23349 		}
23350 		ire_refrele(ire_prim);
23351 	}
23352 }
23353 
23354 
23355 /*
23356  * IP multirouting broadcast routes handling
23357  * Remove the broadcast ire
23358  */
23359 static void
23360 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23361 {
23362 	ire_t *ire_dst;
23363 
23364 	ASSERT(ire != NULL);
23365 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23366 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23367 	if (ire_dst != NULL) {
23368 		ire_t *ire_prim;
23369 
23370 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23371 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23372 		if (ire_prim != NULL) {
23373 			ipif_t *ipif_prim;
23374 			ire_t  *bcast_ire;
23375 
23376 			ipif_prim = ire_prim->ire_ipif;
23377 
23378 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23379 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23380 			    (void *)ire_dst, (void *)ire_prim,
23381 			    (void *)ipif_prim));
23382 
23383 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23384 			    ire->ire_gateway_addr,
23385 			    IRE_BROADCAST,
23386 			    ipif_prim, ALL_ZONES,
23387 			    NULL,
23388 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23389 			    MATCH_IRE_MASK, ipst);
23390 
23391 			if (bcast_ire != NULL) {
23392 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23393 				    "looked up bcast_ire %p\n",
23394 				    (void *)bcast_ire));
23395 				ipif_remove_ire(bcast_ire->ire_ipif,
23396 				    bcast_ire);
23397 				ire_delete(bcast_ire);
23398 				ire_refrele(bcast_ire);
23399 			}
23400 			ire_refrele(ire_prim);
23401 		}
23402 		ire_refrele(ire_dst);
23403 	}
23404 }
23405 
23406 /*
23407  * IPsec hardware acceleration capabilities related functions.
23408  */
23409 
23410 /*
23411  * Free a per-ill IPsec capabilities structure.
23412  */
23413 static void
23414 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23415 {
23416 	if (capab->auth_hw_algs != NULL)
23417 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23418 	if (capab->encr_hw_algs != NULL)
23419 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23420 	if (capab->encr_algparm != NULL)
23421 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23422 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23423 }
23424 
23425 /*
23426  * Allocate a new per-ill IPsec capabilities structure. This structure
23427  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23428  * an array which specifies, for each algorithm, whether this algorithm
23429  * is supported by the ill or not.
23430  */
23431 static ill_ipsec_capab_t *
23432 ill_ipsec_capab_alloc(void)
23433 {
23434 	ill_ipsec_capab_t *capab;
23435 	uint_t nelems;
23436 
23437 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23438 	if (capab == NULL)
23439 		return (NULL);
23440 
23441 	/* we need one bit per algorithm */
23442 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23443 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23444 
23445 	/* allocate memory to store algorithm flags */
23446 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23447 	if (capab->encr_hw_algs == NULL)
23448 		goto nomem;
23449 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23450 	if (capab->auth_hw_algs == NULL)
23451 		goto nomem;
23452 	/*
23453 	 * Leave encr_algparm NULL for now since we won't need it half
23454 	 * the time
23455 	 */
23456 	return (capab);
23457 
23458 nomem:
23459 	ill_ipsec_capab_free(capab);
23460 	return (NULL);
23461 }
23462 
23463 /*
23464  * Resize capability array.  Since we're exclusive, this is OK.
23465  */
23466 static boolean_t
23467 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23468 {
23469 	ipsec_capab_algparm_t *nalp, *oalp;
23470 	uint32_t olen, nlen;
23471 
23472 	oalp = capab->encr_algparm;
23473 	olen = capab->encr_algparm_size;
23474 
23475 	if (oalp != NULL) {
23476 		if (algid < capab->encr_algparm_end)
23477 			return (B_TRUE);
23478 	}
23479 
23480 	nlen = (algid + 1) * sizeof (*nalp);
23481 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23482 	if (nalp == NULL)
23483 		return (B_FALSE);
23484 
23485 	if (oalp != NULL) {
23486 		bcopy(oalp, nalp, olen);
23487 		kmem_free(oalp, olen);
23488 	}
23489 	capab->encr_algparm = nalp;
23490 	capab->encr_algparm_size = nlen;
23491 	capab->encr_algparm_end = algid + 1;
23492 
23493 	return (B_TRUE);
23494 }
23495 
23496 /*
23497  * Compare the capabilities of the specified ill with the protocol
23498  * and algorithms specified by the SA passed as argument.
23499  * If they match, returns B_TRUE, B_FALSE if they do not match.
23500  *
23501  * The ill can be passed as a pointer to it, or by specifying its index
23502  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23503  *
23504  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23505  * packet is eligible for hardware acceleration, and by
23506  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23507  * to a particular ill.
23508  */
23509 boolean_t
23510 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23511     ipsa_t *sa, netstack_t *ns)
23512 {
23513 	boolean_t sa_isv6;
23514 	uint_t algid;
23515 	struct ill_ipsec_capab_s *cpp;
23516 	boolean_t need_refrele = B_FALSE;
23517 	ip_stack_t	*ipst = ns->netstack_ip;
23518 
23519 	if (ill == NULL) {
23520 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23521 		    NULL, NULL, NULL, ipst);
23522 		if (ill == NULL) {
23523 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23524 			return (B_FALSE);
23525 		}
23526 		need_refrele = B_TRUE;
23527 	}
23528 
23529 	/*
23530 	 * Use the address length specified by the SA to determine
23531 	 * if it corresponds to a IPv6 address, and fail the matching
23532 	 * if the isv6 flag passed as argument does not match.
23533 	 * Note: this check is used for SADB capability checking before
23534 	 * sending SA information to an ill.
23535 	 */
23536 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23537 	if (sa_isv6 != ill_isv6)
23538 		/* protocol mismatch */
23539 		goto done;
23540 
23541 	/*
23542 	 * Check if the ill supports the protocol, algorithm(s) and
23543 	 * key size(s) specified by the SA, and get the pointers to
23544 	 * the algorithms supported by the ill.
23545 	 */
23546 	switch (sa->ipsa_type) {
23547 
23548 	case SADB_SATYPE_ESP:
23549 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23550 			/* ill does not support ESP acceleration */
23551 			goto done;
23552 		cpp = ill->ill_ipsec_capab_esp;
23553 		algid = sa->ipsa_auth_alg;
23554 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23555 			goto done;
23556 		algid = sa->ipsa_encr_alg;
23557 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23558 			goto done;
23559 		if (algid < cpp->encr_algparm_end) {
23560 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23561 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23562 				goto done;
23563 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23564 				goto done;
23565 		}
23566 		break;
23567 
23568 	case SADB_SATYPE_AH:
23569 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23570 			/* ill does not support AH acceleration */
23571 			goto done;
23572 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23573 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23574 			goto done;
23575 		break;
23576 	}
23577 
23578 	if (need_refrele)
23579 		ill_refrele(ill);
23580 	return (B_TRUE);
23581 done:
23582 	if (need_refrele)
23583 		ill_refrele(ill);
23584 	return (B_FALSE);
23585 }
23586 
23587 /*
23588  * Add a new ill to the list of IPsec capable ills.
23589  * Called from ill_capability_ipsec_ack() when an ACK was received
23590  * indicating that IPsec hardware processing was enabled for an ill.
23591  *
23592  * ill must point to the ill for which acceleration was enabled.
23593  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23594  */
23595 static void
23596 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23597 {
23598 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23599 	uint_t sa_type;
23600 	uint_t ipproto;
23601 	ip_stack_t	*ipst = ill->ill_ipst;
23602 
23603 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23604 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23605 
23606 	switch (dl_cap) {
23607 	case DL_CAPAB_IPSEC_AH:
23608 		sa_type = SADB_SATYPE_AH;
23609 		ills = &ipst->ips_ipsec_capab_ills_ah;
23610 		ipproto = IPPROTO_AH;
23611 		break;
23612 	case DL_CAPAB_IPSEC_ESP:
23613 		sa_type = SADB_SATYPE_ESP;
23614 		ills = &ipst->ips_ipsec_capab_ills_esp;
23615 		ipproto = IPPROTO_ESP;
23616 		break;
23617 	}
23618 
23619 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23620 
23621 	/*
23622 	 * Add ill index to list of hardware accelerators. If
23623 	 * already in list, do nothing.
23624 	 */
23625 	for (cur_ill = *ills; cur_ill != NULL &&
23626 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23627 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23628 		;
23629 
23630 	if (cur_ill == NULL) {
23631 		/* if this is a new entry for this ill */
23632 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23633 		if (new_ill == NULL) {
23634 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23635 			return;
23636 		}
23637 
23638 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23639 		new_ill->ill_isv6 = ill->ill_isv6;
23640 		new_ill->next = *ills;
23641 		*ills = new_ill;
23642 	} else if (!sadb_resync) {
23643 		/* not resync'ing SADB and an entry exists for this ill */
23644 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23645 		return;
23646 	}
23647 
23648 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23649 
23650 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23651 		/*
23652 		 * IPsec module for protocol loaded, initiate dump
23653 		 * of the SADB to this ill.
23654 		 */
23655 		sadb_ill_download(ill, sa_type);
23656 }
23657 
23658 /*
23659  * Remove an ill from the list of IPsec capable ills.
23660  */
23661 static void
23662 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23663 {
23664 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23665 	ip_stack_t	*ipst = ill->ill_ipst;
23666 
23667 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23668 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23669 
23670 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
23671 	    &ipst->ips_ipsec_capab_ills_esp;
23672 
23673 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23674 
23675 	prev_ill = NULL;
23676 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23677 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23678 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23679 		;
23680 	if (cur_ill == NULL) {
23681 		/* entry not found */
23682 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23683 		return;
23684 	}
23685 	if (prev_ill == NULL) {
23686 		/* entry at front of list */
23687 		*ills = NULL;
23688 	} else {
23689 		prev_ill->next = cur_ill->next;
23690 	}
23691 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23692 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23693 }
23694 
23695 /*
23696  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23697  * supporting the specified IPsec protocol acceleration.
23698  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23699  * We free the mblk and, if sa is non-null, release the held referece.
23700  */
23701 void
23702 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
23703     netstack_t *ns)
23704 {
23705 	ipsec_capab_ill_t *ici, *cur_ici;
23706 	ill_t *ill;
23707 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23708 	ip_stack_t	*ipst = ns->netstack_ip;
23709 
23710 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
23711 	    ipst->ips_ipsec_capab_ills_esp;
23712 
23713 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
23714 
23715 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23716 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23717 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
23718 
23719 		/*
23720 		 * Handle the case where the ill goes away while the SADB is
23721 		 * attempting to send messages.  If it's going away, it's
23722 		 * nuking its shadow SADB, so we don't care..
23723 		 */
23724 
23725 		if (ill == NULL)
23726 			continue;
23727 
23728 		if (sa != NULL) {
23729 			/*
23730 			 * Make sure capabilities match before
23731 			 * sending SA to ill.
23732 			 */
23733 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23734 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
23735 				ill_refrele(ill);
23736 				continue;
23737 			}
23738 
23739 			mutex_enter(&sa->ipsa_lock);
23740 			sa->ipsa_flags |= IPSA_F_HW;
23741 			mutex_exit(&sa->ipsa_lock);
23742 		}
23743 
23744 		/*
23745 		 * Copy template message, and add it to the front
23746 		 * of the mblk ship list. We want to avoid holding
23747 		 * the ipsec_capab_ills_lock while sending the
23748 		 * message to the ills.
23749 		 *
23750 		 * The b_next and b_prev are temporarily used
23751 		 * to build a list of mblks to be sent down, and to
23752 		 * save the ill to which they must be sent.
23753 		 */
23754 		nmp = copymsg(mp);
23755 		if (nmp == NULL) {
23756 			ill_refrele(ill);
23757 			continue;
23758 		}
23759 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23760 		nmp->b_next = mp_ship_list;
23761 		mp_ship_list = nmp;
23762 		nmp->b_prev = (mblk_t *)ill;
23763 	}
23764 
23765 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23766 
23767 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
23768 		/* restore the mblk to a sane state */
23769 		next_mp = nmp->b_next;
23770 		nmp->b_next = NULL;
23771 		ill = (ill_t *)nmp->b_prev;
23772 		nmp->b_prev = NULL;
23773 
23774 		ill_dlpi_send(ill, nmp);
23775 		ill_refrele(ill);
23776 	}
23777 
23778 	if (sa != NULL)
23779 		IPSA_REFRELE(sa);
23780 	freemsg(mp);
23781 }
23782 
23783 /*
23784  * Derive an interface id from the link layer address.
23785  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23786  */
23787 static boolean_t
23788 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23789 {
23790 	char		*addr;
23791 
23792 	if (phys_length != ETHERADDRL)
23793 		return (B_FALSE);
23794 
23795 	/* Form EUI-64 like address */
23796 	addr = (char *)&v6addr->s6_addr32[2];
23797 	bcopy((char *)phys_addr, addr, 3);
23798 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23799 	addr[3] = (char)0xff;
23800 	addr[4] = (char)0xfe;
23801 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23802 	return (B_TRUE);
23803 }
23804 
23805 /* ARGSUSED */
23806 static boolean_t
23807 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23808 {
23809 	return (B_FALSE);
23810 }
23811 
23812 /* ARGSUSED */
23813 static boolean_t
23814 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23815     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23816 {
23817 	/*
23818 	 * Multicast address mappings used over Ethernet/802.X.
23819 	 * This address is used as a base for mappings.
23820 	 */
23821 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23822 	    0x00, 0x00, 0x00};
23823 
23824 	/*
23825 	 * Extract low order 32 bits from IPv6 multicast address.
23826 	 * Or that into the link layer address, starting from the
23827 	 * second byte.
23828 	 */
23829 	*hw_start = 2;
23830 	v6_extract_mask->s6_addr32[0] = 0;
23831 	v6_extract_mask->s6_addr32[1] = 0;
23832 	v6_extract_mask->s6_addr32[2] = 0;
23833 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23834 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23835 	return (B_TRUE);
23836 }
23837 
23838 /*
23839  * Indicate by return value whether multicast is supported. If not,
23840  * this code should not touch/change any parameters.
23841  */
23842 /* ARGSUSED */
23843 static boolean_t
23844 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23845     uint32_t *hw_start, ipaddr_t *extract_mask)
23846 {
23847 	/*
23848 	 * Multicast address mappings used over Ethernet/802.X.
23849 	 * This address is used as a base for mappings.
23850 	 */
23851 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23852 	    0x00, 0x00, 0x00 };
23853 
23854 	if (phys_length != ETHERADDRL)
23855 		return (B_FALSE);
23856 
23857 	*extract_mask = htonl(0x007fffff);
23858 	*hw_start = 2;
23859 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23860 	return (B_TRUE);
23861 }
23862 
23863 /*
23864  * Derive IPoIB interface id from the link layer address.
23865  */
23866 static boolean_t
23867 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23868 {
23869 	char		*addr;
23870 
23871 	if (phys_length != 20)
23872 		return (B_FALSE);
23873 	addr = (char *)&v6addr->s6_addr32[2];
23874 	bcopy(phys_addr + 12, addr, 8);
23875 	/*
23876 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23877 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23878 	 * rules. In these cases, the IBA considers these GUIDs to be in
23879 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23880 	 * required; vendors are required not to assign global EUI-64's
23881 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23882 	 * of the interface identifier. Whether the GUID is in modified
23883 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23884 	 * bit set to 1.
23885 	 */
23886 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23887 	return (B_TRUE);
23888 }
23889 
23890 /*
23891  * Note on mapping from multicast IP addresses to IPoIB multicast link
23892  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
23893  * The format of an IPoIB multicast address is:
23894  *
23895  *  4 byte QPN      Scope Sign.  Pkey
23896  * +--------------------------------------------+
23897  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
23898  * +--------------------------------------------+
23899  *
23900  * The Scope and Pkey components are properties of the IBA port and
23901  * network interface. They can be ascertained from the broadcast address.
23902  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
23903  */
23904 
23905 static boolean_t
23906 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23907     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23908 {
23909 	/*
23910 	 * Base IPoIB IPv6 multicast address used for mappings.
23911 	 * Does not contain the IBA scope/Pkey values.
23912 	 */
23913 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23914 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
23915 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23916 
23917 	/*
23918 	 * Extract low order 80 bits from IPv6 multicast address.
23919 	 * Or that into the link layer address, starting from the
23920 	 * sixth byte.
23921 	 */
23922 	*hw_start = 6;
23923 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
23924 
23925 	/*
23926 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23927 	 */
23928 	*(maddr + 5) = *(bphys_addr + 5);
23929 	*(maddr + 8) = *(bphys_addr + 8);
23930 	*(maddr + 9) = *(bphys_addr + 9);
23931 
23932 	v6_extract_mask->s6_addr32[0] = 0;
23933 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
23934 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
23935 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23936 	return (B_TRUE);
23937 }
23938 
23939 static boolean_t
23940 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23941     uint32_t *hw_start, ipaddr_t *extract_mask)
23942 {
23943 	/*
23944 	 * Base IPoIB IPv4 multicast address used for mappings.
23945 	 * Does not contain the IBA scope/Pkey values.
23946 	 */
23947 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23948 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
23949 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23950 
23951 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
23952 		return (B_FALSE);
23953 
23954 	/*
23955 	 * Extract low order 28 bits from IPv4 multicast address.
23956 	 * Or that into the link layer address, starting from the
23957 	 * sixteenth byte.
23958 	 */
23959 	*extract_mask = htonl(0x0fffffff);
23960 	*hw_start = 16;
23961 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
23962 
23963 	/*
23964 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23965 	 */
23966 	*(maddr + 5) = *(bphys_addr + 5);
23967 	*(maddr + 8) = *(bphys_addr + 8);
23968 	*(maddr + 9) = *(bphys_addr + 9);
23969 	return (B_TRUE);
23970 }
23971 
23972 /*
23973  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
23974  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
23975  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
23976  * the link-local address is preferred.
23977  */
23978 boolean_t
23979 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23980 {
23981 	ipif_t	*ipif;
23982 	ipif_t	*maybe_ipif = NULL;
23983 
23984 	mutex_enter(&ill->ill_lock);
23985 	if (ill->ill_state_flags & ILL_CONDEMNED) {
23986 		mutex_exit(&ill->ill_lock);
23987 		if (ipifp != NULL)
23988 			*ipifp = NULL;
23989 		return (B_FALSE);
23990 	}
23991 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23992 		if (!IPIF_CAN_LOOKUP(ipif))
23993 			continue;
23994 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
23995 		    ipif->ipif_zoneid != ALL_ZONES)
23996 			continue;
23997 		if ((ipif->ipif_flags & flags) != flags)
23998 			continue;
23999 
24000 		if (ipifp == NULL) {
24001 			mutex_exit(&ill->ill_lock);
24002 			ASSERT(maybe_ipif == NULL);
24003 			return (B_TRUE);
24004 		}
24005 		if (!ill->ill_isv6 ||
24006 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
24007 			ipif_refhold_locked(ipif);
24008 			mutex_exit(&ill->ill_lock);
24009 			*ipifp = ipif;
24010 			return (B_TRUE);
24011 		}
24012 		if (maybe_ipif == NULL)
24013 			maybe_ipif = ipif;
24014 	}
24015 	if (ipifp != NULL) {
24016 		if (maybe_ipif != NULL)
24017 			ipif_refhold_locked(maybe_ipif);
24018 		*ipifp = maybe_ipif;
24019 	}
24020 	mutex_exit(&ill->ill_lock);
24021 	return (maybe_ipif != NULL);
24022 }
24023 
24024 /*
24025  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
24026  */
24027 boolean_t
24028 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24029 {
24030 	ill_t *illg;
24031 	ip_stack_t	*ipst = ill->ill_ipst;
24032 
24033 	/*
24034 	 * We look at the passed-in ill first without grabbing ill_g_lock.
24035 	 */
24036 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
24037 		return (B_TRUE);
24038 	}
24039 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
24040 	if (ill->ill_group == NULL) {
24041 		/* ill not in a group */
24042 		rw_exit(&ipst->ips_ill_g_lock);
24043 		return (B_FALSE);
24044 	}
24045 
24046 	/*
24047 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24048 	 * group. We need to look for an ipif in the zone on all the ills in the
24049 	 * group.
24050 	 */
24051 	illg = ill->ill_group->illgrp_ill;
24052 	do {
24053 		/*
24054 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24055 		 * that it's not there.
24056 		 */
24057 		if (illg != ill &&
24058 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24059 			break;
24060 		}
24061 	} while ((illg = illg->ill_group_next) != NULL);
24062 	rw_exit(&ipst->ips_ill_g_lock);
24063 	return (illg != NULL);
24064 }
24065 
24066 /*
24067  * Check if this ill is only being used to send ICMP probes for IPMP
24068  */
24069 boolean_t
24070 ill_is_probeonly(ill_t *ill)
24071 {
24072 	/*
24073 	 * Check if the interface is FAILED, or INACTIVE
24074 	 */
24075 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24076 		return (B_TRUE);
24077 
24078 	return (B_FALSE);
24079 }
24080 
24081 /*
24082  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24083  * If a pointer to an ipif_t is returned then the caller will need to do
24084  * an ill_refrele().
24085  *
24086  * If there is no real interface which matches the ifindex, then it looks
24087  * for a group that has a matching index. In the case of a group match the
24088  * lifidx must be zero. We don't need emulate the logical interfaces
24089  * since IP Filter's use of netinfo doesn't use that.
24090  */
24091 ipif_t *
24092 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24093     ip_stack_t *ipst)
24094 {
24095 	ipif_t *ipif;
24096 	ill_t *ill;
24097 
24098 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24099 	    ipst);
24100 
24101 	if (ill == NULL) {
24102 		/* Fallback to group names only if hook_emulation set */
24103 		if (!ipst->ips_ipmp_hook_emulation)
24104 			return (NULL);
24105 
24106 		if (lifidx != 0)
24107 			return (NULL);
24108 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24109 		if (ill == NULL)
24110 			return (NULL);
24111 	}
24112 
24113 	mutex_enter(&ill->ill_lock);
24114 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24115 		mutex_exit(&ill->ill_lock);
24116 		ill_refrele(ill);
24117 		return (NULL);
24118 	}
24119 
24120 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24121 		if (!IPIF_CAN_LOOKUP(ipif))
24122 			continue;
24123 		if (lifidx == ipif->ipif_id) {
24124 			ipif_refhold_locked(ipif);
24125 			break;
24126 		}
24127 	}
24128 
24129 	mutex_exit(&ill->ill_lock);
24130 	ill_refrele(ill);
24131 	return (ipif);
24132 }
24133 
24134 /*
24135  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24136  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24137  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24138  * for details.
24139  */
24140 void
24141 ill_fastpath_flush(ill_t *ill)
24142 {
24143 	ip_stack_t *ipst = ill->ill_ipst;
24144 
24145 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24146 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24147 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24148 }
24149 
24150 /*
24151  * Set the physical address information for `ill' to the contents of the
24152  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24153  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24154  * EINPROGRESS will be returned.
24155  */
24156 int
24157 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24158 {
24159 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24160 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24161 
24162 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24163 
24164 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24165 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24166 		/* Changing DL_IPV6_TOKEN is not yet supported */
24167 		return (0);
24168 	}
24169 
24170 	/*
24171 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24172 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24173 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24174 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24175 	 */
24176 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24177 		freemsg(mp);
24178 		return (ENOMEM);
24179 	}
24180 
24181 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24182 
24183 	/*
24184 	 * If we can quiesce the ill, then set the address.  If not, then
24185 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24186 	 */
24187 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24188 	mutex_enter(&ill->ill_lock);
24189 	if (!ill_is_quiescent(ill)) {
24190 		/* call cannot fail since `conn_t *' argument is NULL */
24191 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24192 		    mp, ILL_DOWN);
24193 		mutex_exit(&ill->ill_lock);
24194 		return (EINPROGRESS);
24195 	}
24196 	mutex_exit(&ill->ill_lock);
24197 
24198 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24199 	return (0);
24200 }
24201 
24202 /*
24203  * Once the ill associated with `q' has quiesced, set its physical address
24204  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24205  * are passed (linked by b_cont), since we sometimes need to save two distinct
24206  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24207  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24208  * is quiesced, we know any stale IREs with the old address information have
24209  * already been removed, so we don't need to call ill_fastpath_flush().
24210  */
24211 /* ARGSUSED */
24212 static void
24213 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24214 {
24215 	ill_t		*ill = q->q_ptr;
24216 	mblk_t		*addrmp2 = unlinkb(addrmp);
24217 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24218 	uint_t		addrlen, addroff;
24219 
24220 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24221 
24222 	addroff	= dlindp->dl_addr_offset;
24223 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24224 
24225 	switch (dlindp->dl_data) {
24226 	case DL_IPV6_LINK_LAYER_ADDR:
24227 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24228 		freemsg(addrmp2);
24229 		break;
24230 
24231 	case DL_CURR_PHYS_ADDR:
24232 		freemsg(ill->ill_phys_addr_mp);
24233 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24234 		ill->ill_phys_addr_mp = addrmp;
24235 		ill->ill_phys_addr_length = addrlen;
24236 
24237 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24238 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24239 		else
24240 			freemsg(addrmp2);
24241 		break;
24242 	default:
24243 		ASSERT(0);
24244 	}
24245 
24246 	/*
24247 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24248 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24249 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24250 	 * brought up.
24251 	 */
24252 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24253 		ipsq_current_finish(ipsq);
24254 }
24255 
24256 /*
24257  * Helper routine for setting the ill_nd_lla fields.
24258  */
24259 void
24260 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24261 {
24262 	freemsg(ill->ill_nd_lla_mp);
24263 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24264 	ill->ill_nd_lla_mp = ndmp;
24265 	ill->ill_nd_lla_len = addrlen;
24266 }
24267 
24268 major_t IP_MAJ;
24269 #define	IP	"ip"
24270 
24271 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24272 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24273 
24274 /*
24275  * Issue REMOVEIF ioctls to have the loopback interfaces
24276  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24277  * the former going away when the user-level processes in the zone
24278  * are killed  * and the latter are cleaned up by the stream head
24279  * str_stack_shutdown callback that undoes all I_PLINKs.
24280  */
24281 void
24282 ip_loopback_cleanup(ip_stack_t *ipst)
24283 {
24284 	int error;
24285 	ldi_handle_t	lh = NULL;
24286 	ldi_ident_t	li = NULL;
24287 	int		rval;
24288 	cred_t		*cr;
24289 	struct strioctl iocb;
24290 	struct lifreq	lifreq;
24291 
24292 	IP_MAJ = ddi_name_to_major(IP);
24293 
24294 #ifdef NS_DEBUG
24295 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24296 	    ipst->ips_netstack->netstack_stackid);
24297 #endif
24298 
24299 	bzero(&lifreq, sizeof (lifreq));
24300 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24301 
24302 	error = ldi_ident_from_major(IP_MAJ, &li);
24303 	if (error) {
24304 #ifdef DEBUG
24305 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24306 		    error);
24307 #endif
24308 		return;
24309 	}
24310 
24311 	cr = zone_get_kcred(netstackid_to_zoneid(
24312 	    ipst->ips_netstack->netstack_stackid));
24313 	ASSERT(cr != NULL);
24314 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24315 	if (error) {
24316 #ifdef DEBUG
24317 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24318 		    error);
24319 #endif
24320 		goto out;
24321 	}
24322 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24323 	iocb.ic_timout = 15;
24324 	iocb.ic_len = sizeof (lifreq);
24325 	iocb.ic_dp = (char *)&lifreq;
24326 
24327 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24328 	/* LINTED - statement has no consequent */
24329 	if (error) {
24330 #ifdef NS_DEBUG
24331 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24332 		    "UDP6 error %d\n", error);
24333 #endif
24334 	}
24335 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24336 	lh = NULL;
24337 
24338 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24339 	if (error) {
24340 #ifdef NS_DEBUG
24341 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24342 		    error);
24343 #endif
24344 		goto out;
24345 	}
24346 
24347 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24348 	iocb.ic_timout = 15;
24349 	iocb.ic_len = sizeof (lifreq);
24350 	iocb.ic_dp = (char *)&lifreq;
24351 
24352 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24353 	/* LINTED - statement has no consequent */
24354 	if (error) {
24355 #ifdef NS_DEBUG
24356 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24357 		    "UDP error %d\n", error);
24358 #endif
24359 	}
24360 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24361 	lh = NULL;
24362 
24363 out:
24364 	/* Close layered handles */
24365 	if (lh)
24366 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24367 	if (li)
24368 		ldi_ident_release(li);
24369 
24370 	crfree(cr);
24371 }
24372 
24373 /*
24374  * This needs to be in-sync with nic_event_t definition
24375  */
24376 static const char *
24377 ill_hook_event2str(nic_event_t event)
24378 {
24379 	switch (event) {
24380 	case NE_PLUMB:
24381 		return ("PLUMB");
24382 	case NE_UNPLUMB:
24383 		return ("UNPLUMB");
24384 	case NE_UP:
24385 		return ("UP");
24386 	case NE_DOWN:
24387 		return ("DOWN");
24388 	case NE_ADDRESS_CHANGE:
24389 		return ("ADDRESS_CHANGE");
24390 	default:
24391 		return ("UNKNOWN");
24392 	}
24393 }
24394 
24395 static void
24396 ill_hook_event_destroy(ill_t *ill)
24397 {
24398 	hook_nic_event_int_t	*info;
24399 
24400 	if ((info = ill->ill_nic_event_info) != NULL) {
24401 		if (info->hnei_event.hne_data != NULL) {
24402 			kmem_free(info->hnei_event.hne_data,
24403 			    info->hnei_event.hne_datalen);
24404 		}
24405 		kmem_free(info, sizeof (*info));
24406 
24407 		ill->ill_nic_event_info = NULL;
24408 	}
24409 
24410 }
24411 
24412 boolean_t
24413 ill_hook_event_create(ill_t *ill, lif_if_t lif, nic_event_t event,
24414     nic_event_data_t data, size_t datalen)
24415 {
24416 	ip_stack_t		*ipst = ill->ill_ipst;
24417 	hook_nic_event_int_t	*info;
24418 	const char		*str = NULL;
24419 
24420 	/* destroy nic event info if it exists */
24421 	if ((info = ill->ill_nic_event_info) != NULL) {
24422 		str = ill_hook_event2str(info->hnei_event.hne_event);
24423 		ip2dbg(("ill_hook_event_create: unexpected nic event %s "
24424 		    "attached for %s\n", str, ill->ill_name));
24425 		ill_hook_event_destroy(ill);
24426 	}
24427 
24428 	/* create a new nic event info */
24429 	info = kmem_alloc(sizeof (*info), KM_NOSLEEP);
24430 	if (info == NULL)
24431 		goto fail;
24432 
24433 	ill->ill_nic_event_info = info;
24434 
24435 	if (event == NE_UNPLUMB)
24436 		info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
24437 	else
24438 		info->hnei_event.hne_nic = ill->ill_phyint->phyint_hook_ifindex;
24439 	info->hnei_event.hne_lif = lif;
24440 	info->hnei_event.hne_event = event;
24441 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
24442 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
24443 	info->hnei_event.hne_data = NULL;
24444 	info->hnei_event.hne_datalen = 0;
24445 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
24446 
24447 	if (data != NULL && datalen != 0) {
24448 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
24449 		if (info->hnei_event.hne_data != NULL) {
24450 			bcopy(data, info->hnei_event.hne_data, datalen);
24451 			info->hnei_event.hne_datalen = datalen;
24452 		} else {
24453 			ill_hook_event_destroy(ill);
24454 			goto fail;
24455 		}
24456 	}
24457 
24458 	return (B_TRUE);
24459 fail:
24460 	str = ill_hook_event2str(event);
24461 	ip2dbg(("ill_hook_event_create: could not attach %s nic event "
24462 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
24463 	return (B_FALSE);
24464 }
24465