xref: /titanic_41/usr/src/uts/common/inet/ip/ip_if.c (revision 60c582727d131e0112b386fc9d678dc1fd460c3a)
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This file contains the interface control functions for IP.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/stream.h>
35 #include <sys/dlpi.h>
36 #include <sys/stropts.h>
37 #include <sys/strsun.h>
38 #include <sys/sysmacros.h>
39 #include <sys/strlog.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #include <sys/cmn_err.h>
43 #include <sys/kstat.h>
44 #include <sys/debug.h>
45 #include <sys/zone.h>
46 #include <sys/sunldi.h>
47 #include <sys/file.h>
48 
49 #include <sys/kmem.h>
50 #include <sys/systm.h>
51 #include <sys/param.h>
52 #include <sys/socket.h>
53 #include <sys/isa_defs.h>
54 #include <net/if.h>
55 #include <net/if_arp.h>
56 #include <net/if_types.h>
57 #include <net/if_dl.h>
58 #include <net/route.h>
59 #include <sys/sockio.h>
60 #include <netinet/in.h>
61 #include <netinet/ip6.h>
62 #include <netinet/icmp6.h>
63 #include <netinet/igmp_var.h>
64 #include <sys/strsun.h>
65 #include <sys/policy.h>
66 #include <sys/ethernet.h>
67 
68 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
69 #include <inet/mi.h>
70 #include <inet/nd.h>
71 #include <inet/arp.h>
72 #include <inet/mib2.h>
73 #include <inet/ip.h>
74 #include <inet/ip6.h>
75 #include <inet/ip6_asp.h>
76 #include <inet/tcp.h>
77 #include <inet/ip_multi.h>
78 #include <inet/ip_ire.h>
79 #include <inet/ip_ftable.h>
80 #include <inet/ip_rts.h>
81 #include <inet/ip_ndp.h>
82 #include <inet/ip_if.h>
83 #include <inet/ip_impl.h>
84 #include <inet/tun.h>
85 #include <inet/sctp_ip.h>
86 #include <inet/ip_netinfo.h>
87 #include <inet/mib2.h>
88 
89 #include <net/pfkeyv2.h>
90 #include <inet/ipsec_info.h>
91 #include <inet/sadb.h>
92 #include <inet/ipsec_impl.h>
93 #include <sys/iphada.h>
94 
95 
96 #include <netinet/igmp.h>
97 #include <inet/ip_listutils.h>
98 #include <inet/ipclassifier.h>
99 #include <sys/mac.h>
100 
101 #include <sys/systeminfo.h>
102 #include <sys/bootconf.h>
103 
104 #include <sys/tsol/tndb.h>
105 #include <sys/tsol/tnet.h>
106 
107 /* The character which tells where the ill_name ends */
108 #define	IPIF_SEPARATOR_CHAR	':'
109 
110 /* IP ioctl function table entry */
111 typedef struct ipft_s {
112 	int	ipft_cmd;
113 	pfi_t	ipft_pfi;
114 	int	ipft_min_size;
115 	int	ipft_flags;
116 } ipft_t;
117 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
118 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
119 
120 typedef struct ip_sock_ar_s {
121 	union {
122 		area_t	ip_sock_area;
123 		ared_t	ip_sock_ared;
124 		areq_t	ip_sock_areq;
125 	} ip_sock_ar_u;
126 	queue_t	*ip_sock_ar_q;
127 } ip_sock_ar_t;
128 
129 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
130 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
131 		    char *value, caddr_t cp, cred_t *ioc_cr);
132 
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, boolean_t need_up);
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_arp_common(ill_t *ill, queue_t *q, mblk_t *mp,
148     sin_t *sin, boolean_t x_arp_ioctl, boolean_t if_arp_ioctl);
149 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
150     int ioccmd, struct linkblk *li, boolean_t doconsist);
151 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
152 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
153 static void	ipsq_flush(ill_t *ill);
154 
155 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
156     queue_t *q, mblk_t *mp, boolean_t need_up);
157 static void	ipsq_delete(ipsq_t *);
158 
159 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
160 		    boolean_t initialize);
161 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
162 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
163 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
164 		    boolean_t isv6);
165 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
166 static void	ipif_delete_cache_ire(ire_t *, char *);
167 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
168 static void	ipif_free(ipif_t *ipif);
169 static void	ipif_free_tail(ipif_t *ipif);
170 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
171 static void	ipif_multicast_down(ipif_t *ipif);
172 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
173 static void	ipif_set_default(ipif_t *ipif);
174 static int	ipif_set_values(queue_t *q, mblk_t *mp,
175     char *interf_name, uint_t *ppa);
176 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
177     queue_t *q);
178 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
179     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
180     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
181 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
182 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
183 
184 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
185 static int	ill_arp_off(ill_t *ill);
186 static int	ill_arp_on(ill_t *ill);
187 static void	ill_delete_interface_type(ill_if_t *);
188 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
189 static void	ill_dl_down(ill_t *ill);
190 static void	ill_down(ill_t *ill);
191 static void	ill_downi(ire_t *ire, char *ill_arg);
192 static void	ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg);
193 static void	ill_down_tail(ill_t *ill);
194 static void	ill_free_mib(ill_t *ill);
195 static void	ill_glist_delete(ill_t *);
196 static boolean_t ill_has_usable_ipif(ill_t *);
197 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
198 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
199 static void	ill_phyint_free(ill_t *ill);
200 static void	ill_phyint_reinit(ill_t *ill);
201 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
202 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
203 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
204 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
205 static void	ill_stq_cache_delete(ire_t *, char *);
206 
207 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
208 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
209 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
210     in6_addr_t *);
211 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
212     ipaddr_t *);
213 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
214 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
215     in6_addr_t *);
216 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
217     ipaddr_t *);
218 
219 static void	ipif_save_ire(ipif_t *, ire_t *);
220 static void	ipif_remove_ire(ipif_t *, ire_t *);
221 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
222 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
223 
224 /*
225  * Per-ill IPsec capabilities management.
226  */
227 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
228 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
229 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
230 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
231 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
232 static void ill_capability_proto(ill_t *, int, mblk_t *);
233 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
234     boolean_t);
235 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
236 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
237 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
238 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
239 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
240 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
241 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
242 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
243     dl_capability_sub_t *);
244 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
245 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
246 static void ill_capability_lso_reset(ill_t *, mblk_t **);
247 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
248 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
249 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
250 static void	ill_capability_dls_disable(ill_t *);
251 
252 static void	illgrp_cache_delete(ire_t *, char *);
253 static void	illgrp_delete(ill_t *ill);
254 static void	illgrp_reset_schednext(ill_t *ill);
255 
256 static ill_t	*ill_prev_usesrc(ill_t *);
257 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
258 static void	ill_disband_usesrc_group(ill_t *);
259 
260 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
261 
262 /*
263  * if we go over the memory footprint limit more than once in this msec
264  * interval, we'll start pruning aggressively.
265  */
266 int ip_min_frag_prune_time = 0;
267 
268 /*
269  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
270  * and the IPsec DOI
271  */
272 #define	MAX_IPSEC_ALGS	256
273 
274 #define	BITSPERBYTE	8
275 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
276 
277 #define	IPSEC_ALG_ENABLE(algs, algid) \
278 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
279 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
280 
281 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
282 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
283 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
284 
285 typedef uint8_t ipsec_capab_elem_t;
286 
287 /*
288  * Per-algorithm parameters.  Note that at present, only encryption
289  * algorithms have variable keysize (IKE does not provide a way to negotiate
290  * auth algorithm keysize).
291  *
292  * All sizes here are in bits.
293  */
294 typedef struct
295 {
296 	uint16_t	minkeylen;
297 	uint16_t	maxkeylen;
298 } ipsec_capab_algparm_t;
299 
300 /*
301  * Per-ill capabilities.
302  */
303 struct ill_ipsec_capab_s {
304 	ipsec_capab_elem_t *encr_hw_algs;
305 	ipsec_capab_elem_t *auth_hw_algs;
306 	uint32_t algs_size;	/* size of _hw_algs in bytes */
307 	/* algorithm key lengths */
308 	ipsec_capab_algparm_t *encr_algparm;
309 	uint32_t encr_algparm_size;
310 	uint32_t encr_algparm_end;
311 };
312 
313 /*
314  * The field values are larger than strictly necessary for simple
315  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
316  */
317 static area_t	ip_area_template = {
318 	AR_ENTRY_ADD,			/* area_cmd */
319 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
320 					/* area_name_offset */
321 	/* area_name_length temporarily holds this structure length */
322 	sizeof (area_t),			/* area_name_length */
323 	IP_ARP_PROTO_TYPE,		/* area_proto */
324 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
325 	IP_ADDR_LEN,			/* area_proto_addr_length */
326 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
327 					/* area_proto_mask_offset */
328 	0,				/* area_flags */
329 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
330 					/* area_hw_addr_offset */
331 	/* Zero length hw_addr_length means 'use your idea of the address' */
332 	0				/* area_hw_addr_length */
333 };
334 
335 /*
336  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
337  * support
338  */
339 static area_t	ip6_area_template = {
340 	AR_ENTRY_ADD,			/* area_cmd */
341 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
342 					/* area_name_offset */
343 	/* area_name_length temporarily holds this structure length */
344 	sizeof (area_t),			/* area_name_length */
345 	IP_ARP_PROTO_TYPE,		/* area_proto */
346 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
347 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
348 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
349 					/* area_proto_mask_offset */
350 	0,				/* area_flags */
351 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
352 					/* area_hw_addr_offset */
353 	/* Zero length hw_addr_length means 'use your idea of the address' */
354 	0				/* area_hw_addr_length */
355 };
356 
357 static ared_t	ip_ared_template = {
358 	AR_ENTRY_DELETE,
359 	sizeof (ared_t) + IP_ADDR_LEN,
360 	sizeof (ared_t),
361 	IP_ARP_PROTO_TYPE,
362 	sizeof (ared_t),
363 	IP_ADDR_LEN
364 };
365 
366 static ared_t	ip6_ared_template = {
367 	AR_ENTRY_DELETE,
368 	sizeof (ared_t) + IPV6_ADDR_LEN,
369 	sizeof (ared_t),
370 	IP_ARP_PROTO_TYPE,
371 	sizeof (ared_t),
372 	IPV6_ADDR_LEN
373 };
374 
375 /*
376  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
377  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
378  * areq is used).
379  */
380 static areq_t	ip_areq_template = {
381 	AR_ENTRY_QUERY,			/* cmd */
382 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
383 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
384 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
385 	sizeof (areq_t),			/* target addr offset */
386 	IP_ADDR_LEN,			/* target addr_length */
387 	0,				/* flags */
388 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
389 	IP_ADDR_LEN,			/* sender addr length */
390 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
391 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
392 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
393 	/* anything else filled in by the code */
394 };
395 
396 static arc_t	ip_aru_template = {
397 	AR_INTERFACE_UP,
398 	sizeof (arc_t),		/* Name offset */
399 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
400 };
401 
402 static arc_t	ip_ard_template = {
403 	AR_INTERFACE_DOWN,
404 	sizeof (arc_t),		/* Name offset */
405 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
406 };
407 
408 static arc_t	ip_aron_template = {
409 	AR_INTERFACE_ON,
410 	sizeof (arc_t),		/* Name offset */
411 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
412 };
413 
414 static arc_t	ip_aroff_template = {
415 	AR_INTERFACE_OFF,
416 	sizeof (arc_t),		/* Name offset */
417 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
418 };
419 
420 
421 static arma_t	ip_arma_multi_template = {
422 	AR_MAPPING_ADD,
423 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
424 				/* Name offset */
425 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
426 	IP_ARP_PROTO_TYPE,
427 	sizeof (arma_t),			/* proto_addr_offset */
428 	IP_ADDR_LEN,				/* proto_addr_length */
429 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
430 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
431 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
432 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
433 	IP_MAX_HW_LEN,				/* hw_addr_length */
434 	0,					/* hw_mapping_start */
435 };
436 
437 static ipft_t	ip_ioctl_ftbl[] = {
438 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
439 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
440 		IPFT_F_NO_REPLY },
441 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
442 		IPFT_F_NO_REPLY },
443 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
444 	{ 0 }
445 };
446 
447 /* Simple ICMP IP Header Template */
448 static ipha_t icmp_ipha = {
449 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
450 };
451 
452 /* Flag descriptors for ip_ipif_report */
453 static nv_t	ipif_nv_tbl[] = {
454 	{ IPIF_UP,		"UP" },
455 	{ IPIF_BROADCAST,	"BROADCAST" },
456 	{ ILLF_DEBUG,		"DEBUG" },
457 	{ PHYI_LOOPBACK,	"LOOPBACK" },
458 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
459 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
460 	{ PHYI_RUNNING,		"RUNNING" },
461 	{ ILLF_NOARP,		"NOARP" },
462 	{ PHYI_PROMISC,		"PROMISC" },
463 	{ PHYI_ALLMULTI,	"ALLMULTI" },
464 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
465 	{ ILLF_MULTICAST,	"MULTICAST" },
466 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
467 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
468 	{ IPIF_DHCPRUNNING,	"DHCP" },
469 	{ IPIF_PRIVATE,		"PRIVATE" },
470 	{ IPIF_NOXMIT,		"NOXMIT" },
471 	{ IPIF_NOLOCAL,		"NOLOCAL" },
472 	{ IPIF_DEPRECATED,	"DEPRECATED" },
473 	{ IPIF_PREFERRED,	"PREFERRED" },
474 	{ IPIF_TEMPORARY,	"TEMPORARY" },
475 	{ IPIF_ADDRCONF,	"ADDRCONF" },
476 	{ PHYI_VIRTUAL,		"VIRTUAL" },
477 	{ ILLF_ROUTER,		"ROUTER" },
478 	{ ILLF_NONUD,		"NONUD" },
479 	{ IPIF_ANYCAST,		"ANYCAST" },
480 	{ ILLF_NORTEXCH,	"NORTEXCH" },
481 	{ ILLF_IPV4,		"IPV4" },
482 	{ ILLF_IPV6,		"IPV6" },
483 	{ IPIF_MIPRUNNING,	"MIP" },
484 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
485 	{ PHYI_FAILED,		"FAILED" },
486 	{ PHYI_STANDBY,		"STANDBY" },
487 	{ PHYI_INACTIVE,	"INACTIVE" },
488 	{ PHYI_OFFLINE,		"OFFLINE" },
489 };
490 
491 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
492 
493 static ip_m_t	ip_m_tbl[] = {
494 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
495 	    ip_ether_v6intfid },
496 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
497 	    ip_nodef_v6intfid },
498 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
499 	    ip_nodef_v6intfid },
500 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
501 	    ip_nodef_v6intfid },
502 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
503 	    ip_ether_v6intfid },
504 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
505 	    ip_ib_v6intfid },
506 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
507 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
508 	    ip_nodef_v6intfid }
509 };
510 
511 static ill_t	ill_null;		/* Empty ILL for init. */
512 char	ipif_loopback_name[] = "lo0";
513 static char *ipv4_forward_suffix = ":ip_forwarding";
514 static char *ipv6_forward_suffix = ":ip6_forwarding";
515 static	sin6_t	sin6_null;	/* Zero address for quick clears */
516 static	sin_t	sin_null;	/* Zero address for quick clears */
517 
518 /* When set search for unused ipif_seqid */
519 static ipif_t	ipif_zero;
520 
521 /*
522  * ppa arena is created after these many
523  * interfaces have been plumbed.
524  */
525 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
526 
527 /*
528  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
529  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
530  * set through platform specific code (Niagara/Ontario).
531  */
532 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
533 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
534 
535 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
536 
537 static uint_t
538 ipif_rand(ip_stack_t *ipst)
539 {
540 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
541 	    12345;
542 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
543 }
544 
545 /*
546  * Allocate per-interface mibs.
547  * Returns true if ok. False otherwise.
548  *  ipsq  may not yet be allocated (loopback case ).
549  */
550 static boolean_t
551 ill_allocate_mibs(ill_t *ill)
552 {
553 	/* Already allocated? */
554 	if (ill->ill_ip_mib != NULL) {
555 		if (ill->ill_isv6)
556 			ASSERT(ill->ill_icmp6_mib != NULL);
557 		return (B_TRUE);
558 	}
559 
560 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
561 	    KM_NOSLEEP);
562 	if (ill->ill_ip_mib == NULL) {
563 		return (B_FALSE);
564 	}
565 
566 	/* Setup static information */
567 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
568 	    sizeof (mib2_ipIfStatsEntry_t));
569 	if (ill->ill_isv6) {
570 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
571 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
572 		    sizeof (mib2_ipv6AddrEntry_t));
573 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
574 		    sizeof (mib2_ipv6RouteEntry_t));
575 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
576 		    sizeof (mib2_ipv6NetToMediaEntry_t));
577 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
578 		    sizeof (ipv6_member_t));
579 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
580 		    sizeof (ipv6_grpsrc_t));
581 	} else {
582 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
583 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
584 		    sizeof (mib2_ipAddrEntry_t));
585 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
586 		    sizeof (mib2_ipRouteEntry_t));
587 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
588 		    sizeof (mib2_ipNetToMediaEntry_t));
589 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
590 		    sizeof (ip_member_t));
591 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
592 		    sizeof (ip_grpsrc_t));
593 
594 		/*
595 		 * For a v4 ill, we are done at this point, because per ill
596 		 * icmp mibs are only used for v6.
597 		 */
598 		return (B_TRUE);
599 	}
600 
601 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
602 	    KM_NOSLEEP);
603 	if (ill->ill_icmp6_mib == NULL) {
604 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
605 		ill->ill_ip_mib = NULL;
606 		return (B_FALSE);
607 	}
608 	/* static icmp info */
609 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
610 	    sizeof (mib2_ipv6IfIcmpEntry_t);
611 	/*
612 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
613 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
614 	 * -> ill_phyint_reinit
615 	 */
616 	return (B_TRUE);
617 }
618 
619 /*
620  * Common code for preparation of ARP commands.  Two points to remember:
621  * 	1) The ill_name is tacked on at the end of the allocated space so
622  *	   the templates name_offset field must contain the total space
623  *	   to allocate less the name length.
624  *
625  *	2) The templates name_length field should contain the *template*
626  *	   length.  We use it as a parameter to bcopy() and then write
627  *	   the real ill_name_length into the name_length field of the copy.
628  * (Always called as writer.)
629  */
630 mblk_t *
631 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
632 {
633 	arc_t	*arc = (arc_t *)template;
634 	char	*cp;
635 	int	len;
636 	mblk_t	*mp;
637 	uint_t	name_length = ill->ill_name_length;
638 	uint_t	template_len = arc->arc_name_length;
639 
640 	len = arc->arc_name_offset + name_length;
641 	mp = allocb(len, BPRI_HI);
642 	if (mp == NULL)
643 		return (NULL);
644 	cp = (char *)mp->b_rptr;
645 	mp->b_wptr = (uchar_t *)&cp[len];
646 	if (template_len)
647 		bcopy(template, cp, template_len);
648 	if (len > template_len)
649 		bzero(&cp[template_len], len - template_len);
650 	mp->b_datap->db_type = M_PROTO;
651 
652 	arc = (arc_t *)cp;
653 	arc->arc_name_length = name_length;
654 	cp = (char *)arc + arc->arc_name_offset;
655 	bcopy(ill->ill_name, cp, name_length);
656 
657 	if (addr) {
658 		area_t	*area = (area_t *)mp->b_rptr;
659 
660 		cp = (char *)area + area->area_proto_addr_offset;
661 		bcopy(addr, cp, area->area_proto_addr_length);
662 		if (area->area_cmd == AR_ENTRY_ADD) {
663 			cp = (char *)area;
664 			len = area->area_proto_addr_length;
665 			if (area->area_proto_mask_offset)
666 				cp += area->area_proto_mask_offset;
667 			else
668 				cp += area->area_proto_addr_offset + len;
669 			while (len-- > 0)
670 				*cp++ = (char)~0;
671 		}
672 	}
673 	return (mp);
674 }
675 
676 mblk_t *
677 ipif_area_alloc(ipif_t *ipif)
678 {
679 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
680 	    (char *)&ipif->ipif_lcl_addr));
681 }
682 
683 mblk_t *
684 ipif_ared_alloc(ipif_t *ipif)
685 {
686 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
687 	    (char *)&ipif->ipif_lcl_addr));
688 }
689 
690 mblk_t *
691 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
692 {
693 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
694 	    (char *)&addr));
695 }
696 
697 /*
698  * Completely vaporize a lower level tap and all associated interfaces.
699  * ill_delete is called only out of ip_close when the device control
700  * stream is being closed.
701  */
702 void
703 ill_delete(ill_t *ill)
704 {
705 	ipif_t	*ipif;
706 	ill_t	*prev_ill;
707 	ip_stack_t	*ipst = ill->ill_ipst;
708 
709 	/*
710 	 * ill_delete may be forcibly entering the ipsq. The previous
711 	 * ioctl may not have completed and may need to be aborted.
712 	 * ipsq_flush takes care of it. If we don't need to enter the
713 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
714 	 * ill_delete_tail is sufficient.
715 	 */
716 	ipsq_flush(ill);
717 
718 	/*
719 	 * Nuke all interfaces.  ipif_free will take down the interface,
720 	 * remove it from the list, and free the data structure.
721 	 * Walk down the ipif list and remove the logical interfaces
722 	 * first before removing the main ipif. We can't unplumb
723 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
724 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
725 	 * POINTOPOINT.
726 	 *
727 	 * If ill_ipif was not properly initialized (i.e low on memory),
728 	 * then no interfaces to clean up. In this case just clean up the
729 	 * ill.
730 	 */
731 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
732 		ipif_free(ipif);
733 
734 	/*
735 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
736 	 * So nobody can be using this mp now. Free the mp allocated for
737 	 * honoring ILLF_NOARP
738 	 */
739 	freemsg(ill->ill_arp_on_mp);
740 	ill->ill_arp_on_mp = NULL;
741 
742 	/* Clean up msgs on pending upcalls for mrouted */
743 	reset_mrt_ill(ill);
744 
745 	/*
746 	 * ipif_free -> reset_conn_ipif will remove all multicast
747 	 * references for IPv4. For IPv6, we need to do it here as
748 	 * it points only at ills.
749 	 */
750 	reset_conn_ill(ill);
751 
752 	/*
753 	 * ill_down will arrange to blow off any IRE's dependent on this
754 	 * ILL, and shut down fragmentation reassembly.
755 	 */
756 	ill_down(ill);
757 
758 	/* Let SCTP know, so that it can remove this from its list. */
759 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
760 
761 	/*
762 	 * If an address on this ILL is being used as a source address then
763 	 * clear out the pointers in other ILLs that point to this ILL.
764 	 */
765 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
766 	if (ill->ill_usesrc_grp_next != NULL) {
767 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
768 			ill_disband_usesrc_group(ill);
769 		} else {	/* consumer of the usesrc ILL */
770 			prev_ill = ill_prev_usesrc(ill);
771 			prev_ill->ill_usesrc_grp_next =
772 			    ill->ill_usesrc_grp_next;
773 		}
774 	}
775 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
776 }
777 
778 static void
779 ipif_non_duplicate(ipif_t *ipif)
780 {
781 	ill_t *ill = ipif->ipif_ill;
782 	mutex_enter(&ill->ill_lock);
783 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
784 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
785 		ASSERT(ill->ill_ipif_dup_count > 0);
786 		ill->ill_ipif_dup_count--;
787 	}
788 	mutex_exit(&ill->ill_lock);
789 }
790 
791 /*
792  * ill_delete_tail is called from ip_modclose after all references
793  * to the closing ill are gone. The wait is done in ip_modclose
794  */
795 void
796 ill_delete_tail(ill_t *ill)
797 {
798 	mblk_t	**mpp;
799 	ipif_t	*ipif;
800 	ip_stack_t	*ipst = ill->ill_ipst;
801 
802 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
803 		ipif_non_duplicate(ipif);
804 		ipif_down_tail(ipif);
805 	}
806 
807 	ASSERT(ill->ill_ipif_dup_count == 0 &&
808 	    ill->ill_arp_down_mp == NULL &&
809 	    ill->ill_arp_del_mapping_mp == NULL);
810 
811 	/*
812 	 * If polling capability is enabled (which signifies direct
813 	 * upcall into IP and driver has ill saved as a handle),
814 	 * we need to make sure that unbind has completed before we
815 	 * let the ill disappear and driver no longer has any reference
816 	 * to this ill.
817 	 */
818 	mutex_enter(&ill->ill_lock);
819 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
820 		cv_wait(&ill->ill_cv, &ill->ill_lock);
821 	mutex_exit(&ill->ill_lock);
822 
823 	/*
824 	 * Clean up polling and soft ring capabilities
825 	 */
826 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
827 		ill_capability_dls_disable(ill);
828 
829 	if (ill->ill_net_type != IRE_LOOPBACK)
830 		qprocsoff(ill->ill_rq);
831 
832 	/*
833 	 * We do an ipsq_flush once again now. New messages could have
834 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
835 	 * could also have landed up if an ioctl thread had looked up
836 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
837 	 * enqueued the ioctl when we did the ipsq_flush last time.
838 	 */
839 	ipsq_flush(ill);
840 
841 	/*
842 	 * Free capabilities.
843 	 */
844 	if (ill->ill_ipsec_capab_ah != NULL) {
845 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
846 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
847 		ill->ill_ipsec_capab_ah = NULL;
848 	}
849 
850 	if (ill->ill_ipsec_capab_esp != NULL) {
851 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
852 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
853 		ill->ill_ipsec_capab_esp = NULL;
854 	}
855 
856 	if (ill->ill_mdt_capab != NULL) {
857 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
858 		ill->ill_mdt_capab = NULL;
859 	}
860 
861 	if (ill->ill_hcksum_capab != NULL) {
862 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
863 		ill->ill_hcksum_capab = NULL;
864 	}
865 
866 	if (ill->ill_zerocopy_capab != NULL) {
867 		kmem_free(ill->ill_zerocopy_capab,
868 		    sizeof (ill_zerocopy_capab_t));
869 		ill->ill_zerocopy_capab = NULL;
870 	}
871 
872 	if (ill->ill_lso_capab != NULL) {
873 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
874 		ill->ill_lso_capab = NULL;
875 	}
876 
877 	if (ill->ill_dls_capab != NULL) {
878 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
879 		ill->ill_dls_capab->ill_unbind_conn = NULL;
880 		kmem_free(ill->ill_dls_capab,
881 		    sizeof (ill_dls_capab_t) +
882 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
883 		ill->ill_dls_capab = NULL;
884 	}
885 
886 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
887 
888 	while (ill->ill_ipif != NULL)
889 		ipif_free_tail(ill->ill_ipif);
890 
891 	ill_down_tail(ill);
892 
893 	/*
894 	 * We have removed all references to ilm from conn and the ones joined
895 	 * within the kernel.
896 	 *
897 	 * We don't walk conns, mrts and ires because
898 	 *
899 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
900 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
901 	 *    ill references.
902 	 */
903 	ASSERT(ilm_walk_ill(ill) == 0);
904 	/*
905 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
906 	 * could free the phyint. No more reference to the phyint after this
907 	 * point.
908 	 */
909 	(void) ill_glist_delete(ill);
910 
911 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
912 	if (ill->ill_ndd_name != NULL)
913 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
914 	rw_exit(&ipst->ips_ip_g_nd_lock);
915 
916 
917 	if (ill->ill_frag_ptr != NULL) {
918 		uint_t count;
919 
920 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
921 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
922 		}
923 		mi_free(ill->ill_frag_ptr);
924 		ill->ill_frag_ptr = NULL;
925 		ill->ill_frag_hash_tbl = NULL;
926 	}
927 
928 	freemsg(ill->ill_nd_lla_mp);
929 	/* Free all retained control messages. */
930 	mpp = &ill->ill_first_mp_to_free;
931 	do {
932 		while (mpp[0]) {
933 			mblk_t  *mp;
934 			mblk_t  *mp1;
935 
936 			mp = mpp[0];
937 			mpp[0] = mp->b_next;
938 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
939 				mp1->b_next = NULL;
940 				mp1->b_prev = NULL;
941 			}
942 			freemsg(mp);
943 		}
944 	} while (mpp++ != &ill->ill_last_mp_to_free);
945 
946 	ill_free_mib(ill);
947 	/* Drop refcnt here */
948 	netstack_rele(ill->ill_ipst->ips_netstack);
949 	ill->ill_ipst = NULL;
950 
951 	ILL_TRACE_CLEANUP(ill);
952 }
953 
954 static void
955 ill_free_mib(ill_t *ill)
956 {
957 	ip_stack_t *ipst = ill->ill_ipst;
958 
959 	/*
960 	 * MIB statistics must not be lost, so when an interface
961 	 * goes away the counter values will be added to the global
962 	 * MIBs.
963 	 */
964 	if (ill->ill_ip_mib != NULL) {
965 		if (ill->ill_isv6) {
966 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
967 			    ill->ill_ip_mib);
968 		} else {
969 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
970 			    ill->ill_ip_mib);
971 		}
972 
973 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
974 		ill->ill_ip_mib = NULL;
975 	}
976 	if (ill->ill_icmp6_mib != NULL) {
977 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
978 		    ill->ill_icmp6_mib);
979 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
980 		ill->ill_icmp6_mib = NULL;
981 	}
982 }
983 
984 /*
985  * Concatenate together a physical address and a sap.
986  *
987  * Sap_lengths are interpreted as follows:
988  *   sap_length == 0	==>	no sap
989  *   sap_length > 0	==>	sap is at the head of the dlpi address
990  *   sap_length < 0	==>	sap is at the tail of the dlpi address
991  */
992 static void
993 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
994     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
995 {
996 	uint16_t sap_addr = (uint16_t)sap_src;
997 
998 	if (sap_length == 0) {
999 		if (phys_src == NULL)
1000 			bzero(dst, phys_length);
1001 		else
1002 			bcopy(phys_src, dst, phys_length);
1003 	} else if (sap_length < 0) {
1004 		if (phys_src == NULL)
1005 			bzero(dst, phys_length);
1006 		else
1007 			bcopy(phys_src, dst, phys_length);
1008 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1009 	} else {
1010 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1011 		if (phys_src == NULL)
1012 			bzero((char *)dst + sap_length, phys_length);
1013 		else
1014 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1015 	}
1016 }
1017 
1018 /*
1019  * Generate a dl_unitdata_req mblk for the device and address given.
1020  * addr_length is the length of the physical portion of the address.
1021  * If addr is NULL include an all zero address of the specified length.
1022  * TRUE? In any case, addr_length is taken to be the entire length of the
1023  * dlpi address, including the absolute value of sap_length.
1024  */
1025 mblk_t *
1026 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1027 		t_scalar_t sap_length)
1028 {
1029 	dl_unitdata_req_t *dlur;
1030 	mblk_t	*mp;
1031 	t_scalar_t	abs_sap_length;		/* absolute value */
1032 
1033 	abs_sap_length = ABS(sap_length);
1034 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1035 	    DL_UNITDATA_REQ);
1036 	if (mp == NULL)
1037 		return (NULL);
1038 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1039 	/* HACK: accomodate incompatible DLPI drivers */
1040 	if (addr_length == 8)
1041 		addr_length = 6;
1042 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1043 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1044 	dlur->dl_priority.dl_min = 0;
1045 	dlur->dl_priority.dl_max = 0;
1046 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1047 	    (uchar_t *)&dlur[1]);
1048 	return (mp);
1049 }
1050 
1051 /*
1052  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1053  * Return an error if we already have 1 or more ioctls in progress.
1054  * This is used only for non-exclusive ioctls. Currently this is used
1055  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1056  * and thus need to use ipsq_pending_mp_add.
1057  */
1058 boolean_t
1059 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1060 {
1061 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1062 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1063 	/*
1064 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1065 	 */
1066 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1067 	    (add_mp->b_datap->db_type == M_IOCTL));
1068 
1069 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1070 	/*
1071 	 * Return error if the conn has started closing. The conn
1072 	 * could have finished cleaning up the pending mp list,
1073 	 * If so we should not add another mp to the list negating
1074 	 * the cleanup.
1075 	 */
1076 	if (connp->conn_state_flags & CONN_CLOSING)
1077 		return (B_FALSE);
1078 	/*
1079 	 * Add the pending mp to the head of the list, chained by b_next.
1080 	 * Note down the conn on which the ioctl request came, in b_prev.
1081 	 * This will be used to later get the conn, when we get a response
1082 	 * on the ill queue, from some other module (typically arp)
1083 	 */
1084 	add_mp->b_next = (void *)ill->ill_pending_mp;
1085 	add_mp->b_queue = CONNP_TO_WQ(connp);
1086 	ill->ill_pending_mp = add_mp;
1087 	if (connp != NULL)
1088 		connp->conn_oper_pending_ill = ill;
1089 	return (B_TRUE);
1090 }
1091 
1092 /*
1093  * Retrieve the ill_pending_mp and return it. We have to walk the list
1094  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1095  */
1096 mblk_t *
1097 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1098 {
1099 	mblk_t	*prev = NULL;
1100 	mblk_t	*curr = NULL;
1101 	uint_t	id;
1102 	conn_t	*connp;
1103 
1104 	/*
1105 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1106 	 * up the pending mp, but it does not know the ioc_id and
1107 	 * passes in a zero for it.
1108 	 */
1109 	mutex_enter(&ill->ill_lock);
1110 	if (ioc_id != 0)
1111 		*connpp = NULL;
1112 
1113 	/* Search the list for the appropriate ioctl based on ioc_id */
1114 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1115 	    prev = curr, curr = curr->b_next) {
1116 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1117 		connp = Q_TO_CONN(curr->b_queue);
1118 		/* Match based on the ioc_id or based on the conn */
1119 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1120 			break;
1121 	}
1122 
1123 	if (curr != NULL) {
1124 		/* Unlink the mblk from the pending mp list */
1125 		if (prev != NULL) {
1126 			prev->b_next = curr->b_next;
1127 		} else {
1128 			ASSERT(ill->ill_pending_mp == curr);
1129 			ill->ill_pending_mp = curr->b_next;
1130 		}
1131 
1132 		/*
1133 		 * conn refcnt must have been bumped up at the start of
1134 		 * the ioctl. So we can safely access the conn.
1135 		 */
1136 		ASSERT(CONN_Q(curr->b_queue));
1137 		*connpp = Q_TO_CONN(curr->b_queue);
1138 		curr->b_next = NULL;
1139 		curr->b_queue = NULL;
1140 	}
1141 
1142 	mutex_exit(&ill->ill_lock);
1143 
1144 	return (curr);
1145 }
1146 
1147 /*
1148  * Add the pending mp to the list. There can be only 1 pending mp
1149  * in the list. Any exclusive ioctl that needs to wait for a response
1150  * from another module or driver needs to use this function to set
1151  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1152  * the other module/driver. This is also used while waiting for the
1153  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1154  */
1155 boolean_t
1156 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1157     int waitfor)
1158 {
1159 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1160 
1161 	ASSERT(IAM_WRITER_IPIF(ipif));
1162 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1163 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1164 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1165 	/*
1166 	 * The caller may be using a different ipif than the one passed into
1167 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1168 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1169 	 * that `ipsq_current_ipif == ipif'.
1170 	 */
1171 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1172 
1173 	/*
1174 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1175 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1176 	 */
1177 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1178 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1179 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1180 
1181 	if (connp != NULL) {
1182 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1183 		/*
1184 		 * Return error if the conn has started closing. The conn
1185 		 * could have finished cleaning up the pending mp list,
1186 		 * If so we should not add another mp to the list negating
1187 		 * the cleanup.
1188 		 */
1189 		if (connp->conn_state_flags & CONN_CLOSING)
1190 			return (B_FALSE);
1191 	}
1192 	mutex_enter(&ipsq->ipsq_lock);
1193 	ipsq->ipsq_pending_ipif = ipif;
1194 	/*
1195 	 * Note down the queue in b_queue. This will be returned by
1196 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1197 	 * the processing
1198 	 */
1199 	add_mp->b_next = NULL;
1200 	add_mp->b_queue = q;
1201 	ipsq->ipsq_pending_mp = add_mp;
1202 	ipsq->ipsq_waitfor = waitfor;
1203 
1204 	if (connp != NULL)
1205 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1206 	mutex_exit(&ipsq->ipsq_lock);
1207 	return (B_TRUE);
1208 }
1209 
1210 /*
1211  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1212  * queued in the list.
1213  */
1214 mblk_t *
1215 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1216 {
1217 	mblk_t	*curr = NULL;
1218 
1219 	mutex_enter(&ipsq->ipsq_lock);
1220 	*connpp = NULL;
1221 	if (ipsq->ipsq_pending_mp == NULL) {
1222 		mutex_exit(&ipsq->ipsq_lock);
1223 		return (NULL);
1224 	}
1225 
1226 	/* There can be only 1 such excl message */
1227 	curr = ipsq->ipsq_pending_mp;
1228 	ASSERT(curr != NULL && curr->b_next == NULL);
1229 	ipsq->ipsq_pending_ipif = NULL;
1230 	ipsq->ipsq_pending_mp = NULL;
1231 	ipsq->ipsq_waitfor = 0;
1232 	mutex_exit(&ipsq->ipsq_lock);
1233 
1234 	if (CONN_Q(curr->b_queue)) {
1235 		/*
1236 		 * This mp did a refhold on the conn, at the start of the ioctl.
1237 		 * So we can safely return a pointer to the conn to the caller.
1238 		 */
1239 		*connpp = Q_TO_CONN(curr->b_queue);
1240 	} else {
1241 		*connpp = NULL;
1242 	}
1243 	curr->b_next = NULL;
1244 	curr->b_prev = NULL;
1245 	return (curr);
1246 }
1247 
1248 /*
1249  * Cleanup the ioctl mp queued in ipsq_pending_mp
1250  * - Called in the ill_delete path
1251  * - Called in the M_ERROR or M_HANGUP path on the ill.
1252  * - Called in the conn close path.
1253  */
1254 boolean_t
1255 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1256 {
1257 	mblk_t	*mp;
1258 	ipsq_t	*ipsq;
1259 	queue_t	*q;
1260 	ipif_t	*ipif;
1261 
1262 	ASSERT(IAM_WRITER_ILL(ill));
1263 	ipsq = ill->ill_phyint->phyint_ipsq;
1264 	mutex_enter(&ipsq->ipsq_lock);
1265 	/*
1266 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1267 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1268 	 * even if it is meant for another ill, since we have to enqueue
1269 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1270 	 * If connp is non-null we are called from the conn close path.
1271 	 */
1272 	mp = ipsq->ipsq_pending_mp;
1273 	if (mp == NULL || (connp != NULL &&
1274 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1275 		mutex_exit(&ipsq->ipsq_lock);
1276 		return (B_FALSE);
1277 	}
1278 	/* Now remove from the ipsq_pending_mp */
1279 	ipsq->ipsq_pending_mp = NULL;
1280 	q = mp->b_queue;
1281 	mp->b_next = NULL;
1282 	mp->b_prev = NULL;
1283 	mp->b_queue = NULL;
1284 
1285 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1286 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1287 	if (ill->ill_move_in_progress) {
1288 		ILL_CLEAR_MOVE(ill);
1289 	} else if (ill->ill_up_ipifs) {
1290 		ill_group_cleanup(ill);
1291 	}
1292 
1293 	ipif = ipsq->ipsq_pending_ipif;
1294 	ipsq->ipsq_pending_ipif = NULL;
1295 	ipsq->ipsq_waitfor = 0;
1296 	ipsq->ipsq_current_ipif = NULL;
1297 	ipsq->ipsq_current_ioctl = 0;
1298 	mutex_exit(&ipsq->ipsq_lock);
1299 
1300 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1301 		if (connp == NULL) {
1302 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1303 		} else {
1304 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1305 			mutex_enter(&ipif->ipif_ill->ill_lock);
1306 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1307 			mutex_exit(&ipif->ipif_ill->ill_lock);
1308 		}
1309 	} else {
1310 		/*
1311 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1312 		 * be just inet_freemsg. we have to restart it
1313 		 * otherwise the thread will be stuck.
1314 		 */
1315 		inet_freemsg(mp);
1316 	}
1317 	return (B_TRUE);
1318 }
1319 
1320 /*
1321  * The ill is closing. Cleanup all the pending mps. Called exclusively
1322  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1323  * knows this ill, and hence nobody can add an mp to this list
1324  */
1325 static void
1326 ill_pending_mp_cleanup(ill_t *ill)
1327 {
1328 	mblk_t	*mp;
1329 	queue_t	*q;
1330 
1331 	ASSERT(IAM_WRITER_ILL(ill));
1332 
1333 	mutex_enter(&ill->ill_lock);
1334 	/*
1335 	 * Every mp on the pending mp list originating from an ioctl
1336 	 * added 1 to the conn refcnt, at the start of the ioctl.
1337 	 * So bump it down now.  See comments in ip_wput_nondata()
1338 	 */
1339 	while (ill->ill_pending_mp != NULL) {
1340 		mp = ill->ill_pending_mp;
1341 		ill->ill_pending_mp = mp->b_next;
1342 		mutex_exit(&ill->ill_lock);
1343 
1344 		q = mp->b_queue;
1345 		ASSERT(CONN_Q(q));
1346 		mp->b_next = NULL;
1347 		mp->b_prev = NULL;
1348 		mp->b_queue = NULL;
1349 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1350 		mutex_enter(&ill->ill_lock);
1351 	}
1352 	ill->ill_pending_ipif = NULL;
1353 
1354 	mutex_exit(&ill->ill_lock);
1355 }
1356 
1357 /*
1358  * Called in the conn close path and ill delete path
1359  */
1360 static void
1361 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1362 {
1363 	ipsq_t	*ipsq;
1364 	mblk_t	*prev;
1365 	mblk_t	*curr;
1366 	mblk_t	*next;
1367 	queue_t	*q;
1368 	mblk_t	*tmp_list = NULL;
1369 
1370 	ASSERT(IAM_WRITER_ILL(ill));
1371 	if (connp != NULL)
1372 		q = CONNP_TO_WQ(connp);
1373 	else
1374 		q = ill->ill_wq;
1375 
1376 	ipsq = ill->ill_phyint->phyint_ipsq;
1377 	/*
1378 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1379 	 * In the case of ioctl from a conn, there can be only 1 mp
1380 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1381 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1382 	 * ioctls meant for this ill form conn's are not flushed. They will
1383 	 * be processed during ipsq_exit and will not find the ill and will
1384 	 * return error.
1385 	 */
1386 	mutex_enter(&ipsq->ipsq_lock);
1387 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1388 	    curr = next) {
1389 		next = curr->b_next;
1390 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1391 			/* Unlink the mblk from the pending mp list */
1392 			if (prev != NULL) {
1393 				prev->b_next = curr->b_next;
1394 			} else {
1395 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1396 				ipsq->ipsq_xopq_mphead = curr->b_next;
1397 			}
1398 			if (ipsq->ipsq_xopq_mptail == curr)
1399 				ipsq->ipsq_xopq_mptail = prev;
1400 			/*
1401 			 * Create a temporary list and release the ipsq lock
1402 			 * New elements are added to the head of the tmp_list
1403 			 */
1404 			curr->b_next = tmp_list;
1405 			tmp_list = curr;
1406 		} else {
1407 			prev = curr;
1408 		}
1409 	}
1410 	mutex_exit(&ipsq->ipsq_lock);
1411 
1412 	while (tmp_list != NULL) {
1413 		curr = tmp_list;
1414 		tmp_list = curr->b_next;
1415 		curr->b_next = NULL;
1416 		curr->b_prev = NULL;
1417 		curr->b_queue = NULL;
1418 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1419 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1420 			    CONN_CLOSE : NO_COPYOUT, NULL);
1421 		} else {
1422 			/*
1423 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1424 			 * this can't be just inet_freemsg. we have to
1425 			 * restart it otherwise the thread will be stuck.
1426 			 */
1427 			inet_freemsg(curr);
1428 		}
1429 	}
1430 }
1431 
1432 /*
1433  * This conn has started closing. Cleanup any pending ioctl from this conn.
1434  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1435  */
1436 void
1437 conn_ioctl_cleanup(conn_t *connp)
1438 {
1439 	mblk_t *curr;
1440 	ipsq_t	*ipsq;
1441 	ill_t	*ill;
1442 	boolean_t refheld;
1443 
1444 	/*
1445 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1446 	 * ioctl has not yet started, the mp is pending in the list headed by
1447 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1448 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1449 	 * is currently executing now the mp is not queued anywhere but
1450 	 * conn_oper_pending_ill is null. The conn close will wait
1451 	 * till the conn_ref drops to zero.
1452 	 */
1453 	mutex_enter(&connp->conn_lock);
1454 	ill = connp->conn_oper_pending_ill;
1455 	if (ill == NULL) {
1456 		mutex_exit(&connp->conn_lock);
1457 		return;
1458 	}
1459 
1460 	curr = ill_pending_mp_get(ill, &connp, 0);
1461 	if (curr != NULL) {
1462 		mutex_exit(&connp->conn_lock);
1463 		CONN_DEC_REF(connp);
1464 		inet_freemsg(curr);
1465 		return;
1466 	}
1467 	/*
1468 	 * We may not be able to refhold the ill if the ill/ipif
1469 	 * is changing. But we need to make sure that the ill will
1470 	 * not vanish. So we just bump up the ill_waiter count.
1471 	 */
1472 	refheld = ill_waiter_inc(ill);
1473 	mutex_exit(&connp->conn_lock);
1474 	if (refheld) {
1475 		if (ipsq_enter(ill, B_TRUE)) {
1476 			ill_waiter_dcr(ill);
1477 			/*
1478 			 * Check whether this ioctl has started and is
1479 			 * pending now in ipsq_pending_mp. If it is not
1480 			 * found there then check whether this ioctl has
1481 			 * not even started and is in the ipsq_xopq list.
1482 			 */
1483 			if (!ipsq_pending_mp_cleanup(ill, connp))
1484 				ipsq_xopq_mp_cleanup(ill, connp);
1485 			ipsq = ill->ill_phyint->phyint_ipsq;
1486 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1487 			return;
1488 		}
1489 	}
1490 
1491 	/*
1492 	 * The ill is also closing and we could not bump up the
1493 	 * ill_waiter_count or we could not enter the ipsq. Leave
1494 	 * the cleanup to ill_delete
1495 	 */
1496 	mutex_enter(&connp->conn_lock);
1497 	while (connp->conn_oper_pending_ill != NULL)
1498 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1499 	mutex_exit(&connp->conn_lock);
1500 	if (refheld)
1501 		ill_waiter_dcr(ill);
1502 }
1503 
1504 /*
1505  * ipcl_walk function for cleaning up conn_*_ill fields.
1506  */
1507 static void
1508 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1509 {
1510 	ill_t	*ill = (ill_t *)arg;
1511 	ire_t	*ire;
1512 
1513 	mutex_enter(&connp->conn_lock);
1514 	if (connp->conn_multicast_ill == ill) {
1515 		/* Revert to late binding */
1516 		connp->conn_multicast_ill = NULL;
1517 		connp->conn_orig_multicast_ifindex = 0;
1518 	}
1519 	if (connp->conn_incoming_ill == ill)
1520 		connp->conn_incoming_ill = NULL;
1521 	if (connp->conn_outgoing_ill == ill)
1522 		connp->conn_outgoing_ill = NULL;
1523 	if (connp->conn_outgoing_pill == ill)
1524 		connp->conn_outgoing_pill = NULL;
1525 	if (connp->conn_nofailover_ill == ill)
1526 		connp->conn_nofailover_ill = NULL;
1527 	if (connp->conn_xmit_if_ill == ill)
1528 		connp->conn_xmit_if_ill = NULL;
1529 	if (connp->conn_ire_cache != NULL) {
1530 		ire = connp->conn_ire_cache;
1531 		/*
1532 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1533 		 * interface X and ipif coming from interface Y, if interface
1534 		 * X and Y are part of the same IPMPgroup. Thus whenever
1535 		 * interface X goes down, remove all references to it by
1536 		 * checking both on ire_ipif and ire_stq.
1537 		 */
1538 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1539 		    (ire->ire_type == IRE_CACHE &&
1540 		    ire->ire_stq == ill->ill_wq)) {
1541 			connp->conn_ire_cache = NULL;
1542 			mutex_exit(&connp->conn_lock);
1543 			ire_refrele_notr(ire);
1544 			return;
1545 		}
1546 	}
1547 	mutex_exit(&connp->conn_lock);
1548 
1549 }
1550 
1551 /* ARGSUSED */
1552 void
1553 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1554 {
1555 	ill_t	*ill = q->q_ptr;
1556 	ipif_t	*ipif;
1557 
1558 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1559 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1560 		ipif_non_duplicate(ipif);
1561 		ipif_down_tail(ipif);
1562 	}
1563 	ill_down_tail(ill);
1564 	freemsg(mp);
1565 	ipsq_current_finish(ipsq);
1566 }
1567 
1568 /*
1569  * ill_down_start is called when we want to down this ill and bring it up again
1570  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1571  * all interfaces, but don't tear down any plumbing.
1572  */
1573 boolean_t
1574 ill_down_start(queue_t *q, mblk_t *mp)
1575 {
1576 	ill_t	*ill = q->q_ptr;
1577 	ipif_t	*ipif;
1578 
1579 	ASSERT(IAM_WRITER_ILL(ill));
1580 
1581 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1582 		(void) ipif_down(ipif, NULL, NULL);
1583 
1584 	ill_down(ill);
1585 
1586 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1587 
1588 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1589 
1590 	/*
1591 	 * Atomically test and add the pending mp if references are active.
1592 	 */
1593 	mutex_enter(&ill->ill_lock);
1594 	if (!ill_is_quiescent(ill)) {
1595 		/* call cannot fail since `conn_t *' argument is NULL */
1596 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1597 		    mp, ILL_DOWN);
1598 		mutex_exit(&ill->ill_lock);
1599 		return (B_FALSE);
1600 	}
1601 	mutex_exit(&ill->ill_lock);
1602 	return (B_TRUE);
1603 }
1604 
1605 static void
1606 ill_down(ill_t *ill)
1607 {
1608 	ip_stack_t	*ipst = ill->ill_ipst;
1609 
1610 	/* Blow off any IREs dependent on this ILL. */
1611 	ire_walk(ill_downi, (char *)ill, ipst);
1612 
1613 	mutex_enter(&ipst->ips_ire_mrtun_lock);
1614 	if (ipst->ips_ire_mrtun_count != 0) {
1615 		mutex_exit(&ipst->ips_ire_mrtun_lock);
1616 		ire_walk_ill_mrtun(0, 0, ill_downi_mrtun_srcif,
1617 		    (char *)ill, NULL, ipst);
1618 	} else {
1619 		mutex_exit(&ipst->ips_ire_mrtun_lock);
1620 	}
1621 
1622 	/*
1623 	 * If any interface based forwarding table exists
1624 	 * Blow off the ires there dependent on this ill
1625 	 */
1626 	mutex_enter(&ipst->ips_ire_srcif_table_lock);
1627 	if (ipst->ips_ire_srcif_table_count > 0) {
1628 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
1629 		ire_walk_srcif_table_v4(ill_downi_mrtun_srcif, (char *)ill,
1630 		    ipst);
1631 	} else {
1632 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
1633 	}
1634 
1635 	/* Remove any conn_*_ill depending on this ill */
1636 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1637 
1638 	if (ill->ill_group != NULL) {
1639 		illgrp_delete(ill);
1640 	}
1641 }
1642 
1643 static void
1644 ill_down_tail(ill_t *ill)
1645 {
1646 	int	i;
1647 
1648 	/* Destroy ill_srcif_table if it exists */
1649 	/* Lock not reqd really because nobody should be able to access */
1650 	mutex_enter(&ill->ill_lock);
1651 	if (ill->ill_srcif_table != NULL) {
1652 		ill->ill_srcif_refcnt = 0;
1653 		for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
1654 			rw_destroy(&ill->ill_srcif_table[i].irb_lock);
1655 		}
1656 		kmem_free(ill->ill_srcif_table,
1657 		    IP_SRCIF_TABLE_SIZE * sizeof (irb_t));
1658 		ill->ill_srcif_table = NULL;
1659 		ill->ill_srcif_refcnt = 0;
1660 		ill->ill_mrtun_refcnt = 0;
1661 	}
1662 	mutex_exit(&ill->ill_lock);
1663 }
1664 
1665 /*
1666  * ire_walk routine used to delete every IRE that depends on queues
1667  * associated with 'ill'.  (Always called as writer.)
1668  */
1669 static void
1670 ill_downi(ire_t *ire, char *ill_arg)
1671 {
1672 	ill_t	*ill = (ill_t *)ill_arg;
1673 
1674 	/*
1675 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1676 	 * interface X and ipif coming from interface Y, if interface
1677 	 * X and Y are part of the same IPMP group. Thus whenever interface
1678 	 * X goes down, remove all references to it by checking both
1679 	 * on ire_ipif and ire_stq.
1680 	 */
1681 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1682 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1683 		ire_delete(ire);
1684 	}
1685 }
1686 
1687 /*
1688  * A seperate routine for deleting revtun and srcif based routes
1689  * are needed because the ires only deleted when the interface
1690  * is unplumbed. Also these ires have ire_in_ill non-null as well.
1691  * we want to keep mobile IP specific code separate.
1692  */
1693 static void
1694 ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg)
1695 {
1696 	ill_t   *ill = (ill_t *)ill_arg;
1697 
1698 	ASSERT(ire->ire_in_ill != NULL);
1699 
1700 	if ((ire->ire_in_ill != NULL && ire->ire_in_ill == ill) ||
1701 	    (ire->ire_stq == ill->ill_wq) || (ire->ire_stq == ill->ill_rq)) {
1702 		ire_delete(ire);
1703 	}
1704 }
1705 
1706 /*
1707  * Remove ire/nce from the fastpath list.
1708  */
1709 void
1710 ill_fastpath_nack(ill_t *ill)
1711 {
1712 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1713 }
1714 
1715 /* Consume an M_IOCACK of the fastpath probe. */
1716 void
1717 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1718 {
1719 	mblk_t	*mp1 = mp;
1720 
1721 	/*
1722 	 * If this was the first attempt turn on the fastpath probing.
1723 	 */
1724 	mutex_enter(&ill->ill_lock);
1725 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1726 		ill->ill_dlpi_fastpath_state = IDS_OK;
1727 	mutex_exit(&ill->ill_lock);
1728 
1729 	/* Free the M_IOCACK mblk, hold on to the data */
1730 	mp = mp->b_cont;
1731 	freeb(mp1);
1732 	if (mp == NULL)
1733 		return;
1734 	if (mp->b_cont != NULL) {
1735 		/*
1736 		 * Update all IRE's or NCE's that are waiting for
1737 		 * fastpath update.
1738 		 */
1739 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1740 		mp1 = mp->b_cont;
1741 		freeb(mp);
1742 		mp = mp1;
1743 	} else {
1744 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1745 	}
1746 
1747 	freeb(mp);
1748 }
1749 
1750 /*
1751  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1752  * The data portion of the request is a dl_unitdata_req_t template for
1753  * what we would send downstream in the absence of a fastpath confirmation.
1754  */
1755 int
1756 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1757 {
1758 	struct iocblk	*ioc;
1759 	mblk_t	*mp;
1760 
1761 	if (dlur_mp == NULL)
1762 		return (EINVAL);
1763 
1764 	mutex_enter(&ill->ill_lock);
1765 	switch (ill->ill_dlpi_fastpath_state) {
1766 	case IDS_FAILED:
1767 		/*
1768 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1769 		 * support it.
1770 		 */
1771 		mutex_exit(&ill->ill_lock);
1772 		return (ENOTSUP);
1773 	case IDS_UNKNOWN:
1774 		/* This is the first probe */
1775 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1776 		break;
1777 	default:
1778 		break;
1779 	}
1780 	mutex_exit(&ill->ill_lock);
1781 
1782 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1783 		return (EAGAIN);
1784 
1785 	mp->b_cont = copyb(dlur_mp);
1786 	if (mp->b_cont == NULL) {
1787 		freeb(mp);
1788 		return (EAGAIN);
1789 	}
1790 
1791 	ioc = (struct iocblk *)mp->b_rptr;
1792 	ioc->ioc_count = msgdsize(mp->b_cont);
1793 
1794 	putnext(ill->ill_wq, mp);
1795 	return (0);
1796 }
1797 
1798 void
1799 ill_capability_probe(ill_t *ill)
1800 {
1801 	/*
1802 	 * Do so only if negotiation is enabled, capabilities are unknown,
1803 	 * and a capability negotiation is not already in progress.
1804 	 */
1805 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN &&
1806 	    ill->ill_dlpi_capab_state != IDS_RENEG)
1807 		return;
1808 
1809 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1810 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1811 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1812 }
1813 
1814 void
1815 ill_capability_reset(ill_t *ill)
1816 {
1817 	mblk_t *sc_mp = NULL;
1818 	mblk_t *tmp;
1819 
1820 	/*
1821 	 * Note here that we reset the state to UNKNOWN, and later send
1822 	 * down the DL_CAPABILITY_REQ without first setting the state to
1823 	 * INPROGRESS.  We do this in order to distinguish the
1824 	 * DL_CAPABILITY_ACK response which may come back in response to
1825 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1826 	 * also handle the case where the driver doesn't send us back
1827 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1828 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1829 	 * features are turned off until the state reaches IDS_OK.
1830 	 */
1831 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1832 
1833 	/*
1834 	 * Disable sub-capabilities and request a list of sub-capability
1835 	 * messages which will be sent down to the driver.  Each handler
1836 	 * allocates the corresponding dl_capability_sub_t inside an
1837 	 * mblk, and links it to the existing sc_mp mblk, or return it
1838 	 * as sc_mp if it's the first sub-capability (the passed in
1839 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1840 	 * sc_mp will be pulled-up, before passing it downstream.
1841 	 */
1842 	ill_capability_mdt_reset(ill, &sc_mp);
1843 	ill_capability_hcksum_reset(ill, &sc_mp);
1844 	ill_capability_zerocopy_reset(ill, &sc_mp);
1845 	ill_capability_ipsec_reset(ill, &sc_mp);
1846 	ill_capability_dls_reset(ill, &sc_mp);
1847 	ill_capability_lso_reset(ill, &sc_mp);
1848 
1849 	/* Nothing to send down in order to disable the capabilities? */
1850 	if (sc_mp == NULL)
1851 		return;
1852 
1853 	tmp = msgpullup(sc_mp, -1);
1854 	freemsg(sc_mp);
1855 	if ((sc_mp = tmp) == NULL) {
1856 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1857 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1858 		return;
1859 	}
1860 
1861 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1862 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1863 }
1864 
1865 /*
1866  * Request or set new-style hardware capabilities supported by DLS provider.
1867  */
1868 static void
1869 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1870 {
1871 	mblk_t *mp;
1872 	dl_capability_req_t *capb;
1873 	size_t size = 0;
1874 	uint8_t *ptr;
1875 
1876 	if (reqp != NULL)
1877 		size = MBLKL(reqp);
1878 
1879 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1880 	if (mp == NULL) {
1881 		freemsg(reqp);
1882 		return;
1883 	}
1884 	ptr = mp->b_rptr;
1885 
1886 	capb = (dl_capability_req_t *)ptr;
1887 	ptr += sizeof (dl_capability_req_t);
1888 
1889 	if (reqp != NULL) {
1890 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1891 		capb->dl_sub_length = size;
1892 		bcopy(reqp->b_rptr, ptr, size);
1893 		ptr += size;
1894 		mp->b_cont = reqp->b_cont;
1895 		freeb(reqp);
1896 	}
1897 	ASSERT(ptr == mp->b_wptr);
1898 
1899 	ill_dlpi_send(ill, mp);
1900 }
1901 
1902 static void
1903 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1904 {
1905 	dl_capab_id_t *id_ic;
1906 	uint_t sub_dl_cap = outers->dl_cap;
1907 	dl_capability_sub_t *inners;
1908 	uint8_t *capend;
1909 
1910 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1911 
1912 	/*
1913 	 * Note: range checks here are not absolutely sufficient to
1914 	 * make us robust against malformed messages sent by drivers;
1915 	 * this is in keeping with the rest of IP's dlpi handling.
1916 	 * (Remember, it's coming from something else in the kernel
1917 	 * address space)
1918 	 */
1919 
1920 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1921 	if (capend > mp->b_wptr) {
1922 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1923 		    "malformed sub-capability too long for mblk");
1924 		return;
1925 	}
1926 
1927 	id_ic = (dl_capab_id_t *)(outers + 1);
1928 
1929 	if (outers->dl_length < sizeof (*id_ic) ||
1930 	    (inners = &id_ic->id_subcap,
1931 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1932 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1933 		    "encapsulated capab type %d too long for mblk",
1934 		    inners->dl_cap);
1935 		return;
1936 	}
1937 
1938 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1939 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1940 		    "isn't as expected; pass-thru module(s) detected, "
1941 		    "discarding capability\n", inners->dl_cap));
1942 		return;
1943 	}
1944 
1945 	/* Process the encapsulated sub-capability */
1946 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1947 }
1948 
1949 /*
1950  * Process Multidata Transmit capability negotiation ack received from a
1951  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1952  * DL_CAPABILITY_ACK message.
1953  */
1954 static void
1955 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1956 {
1957 	mblk_t *nmp = NULL;
1958 	dl_capability_req_t *oc;
1959 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1960 	ill_mdt_capab_t **ill_mdt_capab;
1961 	uint_t sub_dl_cap = isub->dl_cap;
1962 	uint8_t *capend;
1963 
1964 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1965 
1966 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1967 
1968 	/*
1969 	 * Note: range checks here are not absolutely sufficient to
1970 	 * make us robust against malformed messages sent by drivers;
1971 	 * this is in keeping with the rest of IP's dlpi handling.
1972 	 * (Remember, it's coming from something else in the kernel
1973 	 * address space)
1974 	 */
1975 
1976 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1977 	if (capend > mp->b_wptr) {
1978 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1979 		    "malformed sub-capability too long for mblk");
1980 		return;
1981 	}
1982 
1983 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1984 
1985 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1986 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1987 		    "unsupported MDT sub-capability (version %d, expected %d)",
1988 		    mdt_ic->mdt_version, MDT_VERSION_2);
1989 		return;
1990 	}
1991 
1992 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1993 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1994 		    "capability isn't as expected; pass-thru module(s) "
1995 		    "detected, discarding capability\n"));
1996 		return;
1997 	}
1998 
1999 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
2000 
2001 		if (*ill_mdt_capab == NULL) {
2002 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
2003 			    KM_NOSLEEP);
2004 
2005 			if (*ill_mdt_capab == NULL) {
2006 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2007 				    "could not enable MDT version %d "
2008 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
2009 				    ill->ill_name);
2010 				return;
2011 			}
2012 		}
2013 
2014 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
2015 		    "MDT version %d (%d bytes leading, %d bytes trailing "
2016 		    "header spaces, %d max pld bufs, %d span limit)\n",
2017 		    ill->ill_name, MDT_VERSION_2,
2018 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
2019 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
2020 
2021 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
2022 		(*ill_mdt_capab)->ill_mdt_on = 1;
2023 		/*
2024 		 * Round the following values to the nearest 32-bit; ULP
2025 		 * may further adjust them to accomodate for additional
2026 		 * protocol headers.  We pass these values to ULP during
2027 		 * bind time.
2028 		 */
2029 		(*ill_mdt_capab)->ill_mdt_hdr_head =
2030 		    roundup(mdt_ic->mdt_hdr_head, 4);
2031 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
2032 		    roundup(mdt_ic->mdt_hdr_tail, 4);
2033 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
2034 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
2035 
2036 		ill->ill_capabilities |= ILL_CAPAB_MDT;
2037 	} else {
2038 		uint_t size;
2039 		uchar_t *rptr;
2040 
2041 		size = sizeof (dl_capability_req_t) +
2042 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
2043 
2044 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2045 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2046 			    "could not enable MDT for %s (ENOMEM)\n",
2047 			    ill->ill_name);
2048 			return;
2049 		}
2050 
2051 		rptr = nmp->b_rptr;
2052 		/* initialize dl_capability_req_t */
2053 		oc = (dl_capability_req_t *)nmp->b_rptr;
2054 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2055 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2056 		    sizeof (dl_capab_mdt_t);
2057 		nmp->b_rptr += sizeof (dl_capability_req_t);
2058 
2059 		/* initialize dl_capability_sub_t */
2060 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2061 		nmp->b_rptr += sizeof (*isub);
2062 
2063 		/* initialize dl_capab_mdt_t */
2064 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2065 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2066 
2067 		nmp->b_rptr = rptr;
2068 
2069 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2070 		    "to enable MDT version %d\n", ill->ill_name,
2071 		    MDT_VERSION_2));
2072 
2073 		/* set ENABLE flag */
2074 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2075 
2076 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2077 		ill_dlpi_send(ill, nmp);
2078 	}
2079 }
2080 
2081 static void
2082 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2083 {
2084 	mblk_t *mp;
2085 	dl_capab_mdt_t *mdt_subcap;
2086 	dl_capability_sub_t *dl_subcap;
2087 	int size;
2088 
2089 	if (!ILL_MDT_CAPABLE(ill))
2090 		return;
2091 
2092 	ASSERT(ill->ill_mdt_capab != NULL);
2093 	/*
2094 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2095 	 * structure since it's possible that another thread is still
2096 	 * referring to it.  The structure only gets deallocated when
2097 	 * we destroy the ill.
2098 	 */
2099 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2100 
2101 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2102 
2103 	mp = allocb(size, BPRI_HI);
2104 	if (mp == NULL) {
2105 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2106 		    "request to disable MDT\n"));
2107 		return;
2108 	}
2109 
2110 	mp->b_wptr = mp->b_rptr + size;
2111 
2112 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2113 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2114 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2115 
2116 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2117 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2118 	mdt_subcap->mdt_flags = 0;
2119 	mdt_subcap->mdt_hdr_head = 0;
2120 	mdt_subcap->mdt_hdr_tail = 0;
2121 
2122 	if (*sc_mp != NULL)
2123 		linkb(*sc_mp, mp);
2124 	else
2125 		*sc_mp = mp;
2126 }
2127 
2128 /*
2129  * Send a DL_NOTIFY_REQ to the specified ill to enable
2130  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2131  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2132  * acceleration.
2133  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2134  */
2135 static boolean_t
2136 ill_enable_promisc_notify(ill_t *ill)
2137 {
2138 	mblk_t *mp;
2139 	dl_notify_req_t *req;
2140 
2141 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2142 
2143 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2144 	if (mp == NULL)
2145 		return (B_FALSE);
2146 
2147 	req = (dl_notify_req_t *)mp->b_rptr;
2148 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2149 	    DL_NOTE_PROMISC_OFF_PHYS;
2150 
2151 	ill_dlpi_send(ill, mp);
2152 
2153 	return (B_TRUE);
2154 }
2155 
2156 
2157 /*
2158  * Allocate an IPsec capability request which will be filled by our
2159  * caller to turn on support for one or more algorithms.
2160  */
2161 static mblk_t *
2162 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2163 {
2164 	mblk_t *nmp;
2165 	dl_capability_req_t	*ocap;
2166 	dl_capab_ipsec_t	*ocip;
2167 	dl_capab_ipsec_t	*icip;
2168 	uint8_t			*ptr;
2169 	icip = (dl_capab_ipsec_t *)(isub + 1);
2170 
2171 	/*
2172 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2173 	 * PROMISC_ON/OFF notification from the provider. We need to
2174 	 * do this before enabling the algorithms to avoid leakage of
2175 	 * cleartext packets.
2176 	 */
2177 
2178 	if (!ill_enable_promisc_notify(ill))
2179 		return (NULL);
2180 
2181 	/*
2182 	 * Allocate new mblk which will contain a new capability
2183 	 * request to enable the capabilities.
2184 	 */
2185 
2186 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2187 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2188 	if (nmp == NULL)
2189 		return (NULL);
2190 
2191 	ptr = nmp->b_rptr;
2192 
2193 	/* initialize dl_capability_req_t */
2194 	ocap = (dl_capability_req_t *)ptr;
2195 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2196 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2197 	ptr += sizeof (dl_capability_req_t);
2198 
2199 	/* initialize dl_capability_sub_t */
2200 	bcopy(isub, ptr, sizeof (*isub));
2201 	ptr += sizeof (*isub);
2202 
2203 	/* initialize dl_capab_ipsec_t */
2204 	ocip = (dl_capab_ipsec_t *)ptr;
2205 	bcopy(icip, ocip, sizeof (*icip));
2206 
2207 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2208 	return (nmp);
2209 }
2210 
2211 /*
2212  * Process an IPsec capability negotiation ack received from a DLS Provider.
2213  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2214  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2215  */
2216 static void
2217 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2218 {
2219 	dl_capab_ipsec_t	*icip;
2220 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2221 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2222 	uint_t cipher, nciphers;
2223 	mblk_t *nmp;
2224 	uint_t alg_len;
2225 	boolean_t need_sadb_dump;
2226 	uint_t sub_dl_cap = isub->dl_cap;
2227 	ill_ipsec_capab_t **ill_capab;
2228 	uint64_t ill_capab_flag;
2229 	uint8_t *capend, *ciphend;
2230 	boolean_t sadb_resync;
2231 
2232 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2233 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2234 
2235 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2236 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2237 		ill_capab_flag = ILL_CAPAB_AH;
2238 	} else {
2239 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2240 		ill_capab_flag = ILL_CAPAB_ESP;
2241 	}
2242 
2243 	/*
2244 	 * If the ill capability structure exists, then this incoming
2245 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2246 	 * If this is so, then we'd need to resynchronize the SADB
2247 	 * after re-enabling the offloaded ciphers.
2248 	 */
2249 	sadb_resync = (*ill_capab != NULL);
2250 
2251 	/*
2252 	 * Note: range checks here are not absolutely sufficient to
2253 	 * make us robust against malformed messages sent by drivers;
2254 	 * this is in keeping with the rest of IP's dlpi handling.
2255 	 * (Remember, it's coming from something else in the kernel
2256 	 * address space)
2257 	 */
2258 
2259 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2260 	if (capend > mp->b_wptr) {
2261 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2262 		    "malformed sub-capability too long for mblk");
2263 		return;
2264 	}
2265 
2266 	/*
2267 	 * There are two types of acks we process here:
2268 	 * 1. acks in reply to a (first form) generic capability req
2269 	 *    (no ENABLE flag set)
2270 	 * 2. acks in reply to a ENABLE capability req.
2271 	 *    (ENABLE flag set)
2272 	 *
2273 	 * We process the subcapability passed as argument as follows:
2274 	 * 1 do initializations
2275 	 *   1.1 initialize nmp = NULL
2276 	 *   1.2 set need_sadb_dump to B_FALSE
2277 	 * 2 for each cipher in subcapability:
2278 	 *   2.1 if ENABLE flag is set:
2279 	 *	2.1.1 update per-ill ipsec capabilities info
2280 	 *	2.1.2 set need_sadb_dump to B_TRUE
2281 	 *   2.2 if ENABLE flag is not set:
2282 	 *	2.2.1 if nmp is NULL:
2283 	 *		2.2.1.1 allocate and initialize nmp
2284 	 *		2.2.1.2 init current pos in nmp
2285 	 *	2.2.2 copy current cipher to current pos in nmp
2286 	 *	2.2.3 set ENABLE flag in nmp
2287 	 *	2.2.4 update current pos
2288 	 * 3 if nmp is not equal to NULL, send enable request
2289 	 *   3.1 send capability request
2290 	 * 4 if need_sadb_dump is B_TRUE
2291 	 *   4.1 enable promiscuous on/off notifications
2292 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2293 	 *	AH or ESP SA's to interface.
2294 	 */
2295 
2296 	nmp = NULL;
2297 	oalg = NULL;
2298 	need_sadb_dump = B_FALSE;
2299 	icip = (dl_capab_ipsec_t *)(isub + 1);
2300 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2301 
2302 	nciphers = icip->cip_nciphers;
2303 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2304 
2305 	if (ciphend > capend) {
2306 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2307 		    "too many ciphers for sub-capability len");
2308 		return;
2309 	}
2310 
2311 	for (cipher = 0; cipher < nciphers; cipher++) {
2312 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2313 
2314 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2315 			/*
2316 			 * TBD: when we provide a way to disable capabilities
2317 			 * from above, need to manage the request-pending state
2318 			 * and fail if we were not expecting this ACK.
2319 			 */
2320 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2321 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2322 
2323 			/*
2324 			 * Update IPsec capabilities for this ill
2325 			 */
2326 
2327 			if (*ill_capab == NULL) {
2328 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2329 				    ("ill_capability_ipsec_ack: "
2330 				    "allocating ipsec_capab for ill\n"));
2331 				*ill_capab = ill_ipsec_capab_alloc();
2332 
2333 				if (*ill_capab == NULL) {
2334 					cmn_err(CE_WARN,
2335 					    "ill_capability_ipsec_ack: "
2336 					    "could not enable IPsec Hardware "
2337 					    "acceleration for %s (ENOMEM)\n",
2338 					    ill->ill_name);
2339 					return;
2340 				}
2341 			}
2342 
2343 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2344 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2345 
2346 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2347 				cmn_err(CE_WARN,
2348 				    "ill_capability_ipsec_ack: "
2349 				    "malformed IPsec algorithm id %d",
2350 				    ialg->alg_prim);
2351 				continue;
2352 			}
2353 
2354 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2355 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2356 				    ialg->alg_prim);
2357 			} else {
2358 				ipsec_capab_algparm_t *alp;
2359 
2360 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2361 				    ialg->alg_prim);
2362 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2363 				    ialg->alg_prim)) {
2364 					cmn_err(CE_WARN,
2365 					    "ill_capability_ipsec_ack: "
2366 					    "no space for IPsec alg id %d",
2367 					    ialg->alg_prim);
2368 					continue;
2369 				}
2370 				alp = &((*ill_capab)->encr_algparm[
2371 				    ialg->alg_prim]);
2372 				alp->minkeylen = ialg->alg_minbits;
2373 				alp->maxkeylen = ialg->alg_maxbits;
2374 			}
2375 			ill->ill_capabilities |= ill_capab_flag;
2376 			/*
2377 			 * indicate that a capability was enabled, which
2378 			 * will be used below to kick off a SADB dump
2379 			 * to the ill.
2380 			 */
2381 			need_sadb_dump = B_TRUE;
2382 		} else {
2383 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2384 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2385 			    ialg->alg_prim));
2386 
2387 			if (nmp == NULL) {
2388 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2389 				if (nmp == NULL) {
2390 					/*
2391 					 * Sending the PROMISC_ON/OFF
2392 					 * notification request failed.
2393 					 * We cannot enable the algorithms
2394 					 * since the Provider will not
2395 					 * notify IP of promiscous mode
2396 					 * changes, which could lead
2397 					 * to leakage of packets.
2398 					 */
2399 					cmn_err(CE_WARN,
2400 					    "ill_capability_ipsec_ack: "
2401 					    "could not enable IPsec Hardware "
2402 					    "acceleration for %s (ENOMEM)\n",
2403 					    ill->ill_name);
2404 					return;
2405 				}
2406 				/* ptr to current output alg specifier */
2407 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2408 			}
2409 
2410 			/*
2411 			 * Copy current alg specifier, set ENABLE
2412 			 * flag, and advance to next output alg.
2413 			 * For now we enable all IPsec capabilities.
2414 			 */
2415 			ASSERT(oalg != NULL);
2416 			bcopy(ialg, oalg, alg_len);
2417 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2418 			nmp->b_wptr += alg_len;
2419 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2420 		}
2421 
2422 		/* move to next input algorithm specifier */
2423 		ialg = (dl_capab_ipsec_alg_t *)
2424 		    ((char *)ialg + alg_len);
2425 	}
2426 
2427 	if (nmp != NULL)
2428 		/*
2429 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2430 		 * IPsec hardware acceleration.
2431 		 */
2432 		ill_dlpi_send(ill, nmp);
2433 
2434 	if (need_sadb_dump)
2435 		/*
2436 		 * An acknowledgement corresponding to a request to
2437 		 * enable acceleration was received, notify SADB.
2438 		 */
2439 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2440 }
2441 
2442 /*
2443  * Given an mblk with enough space in it, create sub-capability entries for
2444  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2445  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2446  * in preparation for the reset the DL_CAPABILITY_REQ message.
2447  */
2448 static void
2449 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2450     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2451 {
2452 	dl_capab_ipsec_t *oipsec;
2453 	dl_capab_ipsec_alg_t *oalg;
2454 	dl_capability_sub_t *dl_subcap;
2455 	int i, k;
2456 
2457 	ASSERT(nciphers > 0);
2458 	ASSERT(ill_cap != NULL);
2459 	ASSERT(mp != NULL);
2460 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2461 
2462 	/* dl_capability_sub_t for "stype" */
2463 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2464 	dl_subcap->dl_cap = stype;
2465 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2466 	mp->b_wptr += sizeof (dl_capability_sub_t);
2467 
2468 	/* dl_capab_ipsec_t for "stype" */
2469 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2470 	oipsec->cip_version = 1;
2471 	oipsec->cip_nciphers = nciphers;
2472 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2473 
2474 	/* create entries for "stype" AUTH ciphers */
2475 	for (i = 0; i < ill_cap->algs_size; i++) {
2476 		for (k = 0; k < BITSPERBYTE; k++) {
2477 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2478 				continue;
2479 
2480 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2481 			bzero((void *)oalg, sizeof (*oalg));
2482 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2483 			oalg->alg_prim = k + (BITSPERBYTE * i);
2484 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2485 		}
2486 	}
2487 	/* create entries for "stype" ENCR ciphers */
2488 	for (i = 0; i < ill_cap->algs_size; i++) {
2489 		for (k = 0; k < BITSPERBYTE; k++) {
2490 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2491 				continue;
2492 
2493 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2494 			bzero((void *)oalg, sizeof (*oalg));
2495 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2496 			oalg->alg_prim = k + (BITSPERBYTE * i);
2497 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2498 		}
2499 	}
2500 }
2501 
2502 /*
2503  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2504  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2505  * POPC instruction, but our macro is more flexible for an arbitrary length
2506  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2507  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2508  * stays that way, we can reduce the number of iterations required.
2509  */
2510 #define	COUNT_1S(val, sum) {					\
2511 	uint8_t x = val & 0xff;					\
2512 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2513 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2514 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2515 }
2516 
2517 /* ARGSUSED */
2518 static void
2519 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2520 {
2521 	mblk_t *mp;
2522 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2523 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2524 	uint64_t ill_capabilities = ill->ill_capabilities;
2525 	int ah_cnt = 0, esp_cnt = 0;
2526 	int ah_len = 0, esp_len = 0;
2527 	int i, size = 0;
2528 
2529 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2530 		return;
2531 
2532 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2533 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2534 
2535 	/* Find out the number of ciphers for AH */
2536 	if (cap_ah != NULL) {
2537 		for (i = 0; i < cap_ah->algs_size; i++) {
2538 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2539 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2540 		}
2541 		if (ah_cnt > 0) {
2542 			size += sizeof (dl_capability_sub_t) +
2543 			    sizeof (dl_capab_ipsec_t);
2544 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2545 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2546 			size += ah_len;
2547 		}
2548 	}
2549 
2550 	/* Find out the number of ciphers for ESP */
2551 	if (cap_esp != NULL) {
2552 		for (i = 0; i < cap_esp->algs_size; i++) {
2553 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2554 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2555 		}
2556 		if (esp_cnt > 0) {
2557 			size += sizeof (dl_capability_sub_t) +
2558 			    sizeof (dl_capab_ipsec_t);
2559 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2560 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2561 			size += esp_len;
2562 		}
2563 	}
2564 
2565 	if (size == 0) {
2566 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2567 		    "there's nothing to reset\n"));
2568 		return;
2569 	}
2570 
2571 	mp = allocb(size, BPRI_HI);
2572 	if (mp == NULL) {
2573 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2574 		    "request to disable IPSEC Hardware Acceleration\n"));
2575 		return;
2576 	}
2577 
2578 	/*
2579 	 * Clear the capability flags for IPSec HA but retain the ill
2580 	 * capability structures since it's possible that another thread
2581 	 * is still referring to them.  The structures only get deallocated
2582 	 * when we destroy the ill.
2583 	 *
2584 	 * Various places check the flags to see if the ill is capable of
2585 	 * hardware acceleration, and by clearing them we ensure that new
2586 	 * outbound IPSec packets are sent down encrypted.
2587 	 */
2588 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2589 
2590 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2591 	if (ah_cnt > 0) {
2592 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2593 		    cap_ah, mp);
2594 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2595 	}
2596 
2597 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2598 	if (esp_cnt > 0) {
2599 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2600 		    cap_esp, mp);
2601 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2602 	}
2603 
2604 	/*
2605 	 * At this point we've composed a bunch of sub-capabilities to be
2606 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2607 	 * by the caller.  Upon receiving this reset message, the driver
2608 	 * must stop inbound decryption (by destroying all inbound SAs)
2609 	 * and let the corresponding packets come in encrypted.
2610 	 */
2611 
2612 	if (*sc_mp != NULL)
2613 		linkb(*sc_mp, mp);
2614 	else
2615 		*sc_mp = mp;
2616 }
2617 
2618 static void
2619 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2620     boolean_t encapsulated)
2621 {
2622 	boolean_t legacy = B_FALSE;
2623 
2624 	/*
2625 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2626 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2627 	 * instructed the driver to disable its advertised capabilities,
2628 	 * so there's no point in accepting any response at this moment.
2629 	 */
2630 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2631 		return;
2632 
2633 	/*
2634 	 * Note that only the following two sub-capabilities may be
2635 	 * considered as "legacy", since their original definitions
2636 	 * do not incorporate the dl_mid_t module ID token, and hence
2637 	 * may require the use of the wrapper sub-capability.
2638 	 */
2639 	switch (subp->dl_cap) {
2640 	case DL_CAPAB_IPSEC_AH:
2641 	case DL_CAPAB_IPSEC_ESP:
2642 		legacy = B_TRUE;
2643 		break;
2644 	}
2645 
2646 	/*
2647 	 * For legacy sub-capabilities which don't incorporate a queue_t
2648 	 * pointer in their structures, discard them if we detect that
2649 	 * there are intermediate modules in between IP and the driver.
2650 	 */
2651 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2652 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2653 		    "%d discarded; %d module(s) present below IP\n",
2654 		    subp->dl_cap, ill->ill_lmod_cnt));
2655 		return;
2656 	}
2657 
2658 	switch (subp->dl_cap) {
2659 	case DL_CAPAB_IPSEC_AH:
2660 	case DL_CAPAB_IPSEC_ESP:
2661 		ill_capability_ipsec_ack(ill, mp, subp);
2662 		break;
2663 	case DL_CAPAB_MDT:
2664 		ill_capability_mdt_ack(ill, mp, subp);
2665 		break;
2666 	case DL_CAPAB_HCKSUM:
2667 		ill_capability_hcksum_ack(ill, mp, subp);
2668 		break;
2669 	case DL_CAPAB_ZEROCOPY:
2670 		ill_capability_zerocopy_ack(ill, mp, subp);
2671 		break;
2672 	case DL_CAPAB_POLL:
2673 		if (!SOFT_RINGS_ENABLED())
2674 			ill_capability_dls_ack(ill, mp, subp);
2675 		break;
2676 	case DL_CAPAB_SOFT_RING:
2677 		if (SOFT_RINGS_ENABLED())
2678 			ill_capability_dls_ack(ill, mp, subp);
2679 		break;
2680 	case DL_CAPAB_LSO:
2681 		ill_capability_lso_ack(ill, mp, subp);
2682 		break;
2683 	default:
2684 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2685 		    subp->dl_cap));
2686 	}
2687 }
2688 
2689 /*
2690  * As part of negotiating polling capability, the driver tells us
2691  * the default (or normal) blanking interval and packet threshold
2692  * (the receive timer fires if blanking interval is reached or
2693  * the packet threshold is reached).
2694  *
2695  * As part of manipulating the polling interval, we always use our
2696  * estimated interval (avg service time * number of packets queued
2697  * on the squeue) but we try to blank for a minimum of
2698  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2699  * packet threshold during this time. When we are not in polling mode
2700  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2701  * rr_min_blank_ratio but up the packet cnt by a ratio of
2702  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2703  * possible although for a shorter interval.
2704  */
2705 #define	RR_MAX_BLANK_RATIO	20
2706 #define	RR_MIN_BLANK_RATIO	10
2707 #define	RR_MAX_PKT_CNT_RATIO	3
2708 #define	RR_MIN_PKT_CNT_RATIO	3
2709 
2710 /*
2711  * These can be tuned via /etc/system.
2712  */
2713 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2714 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2715 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2716 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2717 
2718 static mac_resource_handle_t
2719 ill_ring_add(void *arg, mac_resource_t *mrp)
2720 {
2721 	ill_t			*ill = (ill_t *)arg;
2722 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2723 	ill_rx_ring_t		*rx_ring;
2724 	int			ip_rx_index;
2725 
2726 	ASSERT(mrp != NULL);
2727 	if (mrp->mr_type != MAC_RX_FIFO) {
2728 		return (NULL);
2729 	}
2730 	ASSERT(ill != NULL);
2731 	ASSERT(ill->ill_dls_capab != NULL);
2732 
2733 	mutex_enter(&ill->ill_lock);
2734 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2735 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2736 		ASSERT(rx_ring != NULL);
2737 
2738 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2739 			time_t normal_blank_time =
2740 			    mrfp->mrf_normal_blank_time;
2741 			uint_t normal_pkt_cnt =
2742 			    mrfp->mrf_normal_pkt_count;
2743 
2744 	bzero(rx_ring, sizeof (ill_rx_ring_t));
2745 
2746 	rx_ring->rr_blank = mrfp->mrf_blank;
2747 	rx_ring->rr_handle = mrfp->mrf_arg;
2748 	rx_ring->rr_ill = ill;
2749 	rx_ring->rr_normal_blank_time = normal_blank_time;
2750 	rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2751 
2752 			rx_ring->rr_max_blank_time =
2753 			    normal_blank_time * rr_max_blank_ratio;
2754 			rx_ring->rr_min_blank_time =
2755 			    normal_blank_time * rr_min_blank_ratio;
2756 			rx_ring->rr_max_pkt_cnt =
2757 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2758 			rx_ring->rr_min_pkt_cnt =
2759 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2760 
2761 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2762 			mutex_exit(&ill->ill_lock);
2763 
2764 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2765 			    (int), ip_rx_index);
2766 			return ((mac_resource_handle_t)rx_ring);
2767 		}
2768 	}
2769 
2770 	/*
2771 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2772 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2773 	 * should be made configurable. Meanwhile it cause no panic because
2774 	 * driver will pass ip_input a NULL handle which will make
2775 	 * IP allocate the default squeue and Polling mode will not
2776 	 * be used for this ring.
2777 	 */
2778 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2779 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2780 
2781 	mutex_exit(&ill->ill_lock);
2782 	return (NULL);
2783 }
2784 
2785 static boolean_t
2786 ill_capability_dls_init(ill_t *ill)
2787 {
2788 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2789 	conn_t 			*connp;
2790 	size_t			sz;
2791 	ip_stack_t *ipst = ill->ill_ipst;
2792 
2793 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2794 		if (ill_dls == NULL) {
2795 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2796 			    "soft_ring enabled for ill=%s (%p) but data "
2797 			    "structs uninitialized\n", ill->ill_name,
2798 			    (void *)ill);
2799 		}
2800 		return (B_TRUE);
2801 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2802 		if (ill_dls == NULL) {
2803 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2804 			    "polling enabled for ill=%s (%p) but data "
2805 			    "structs uninitialized\n", ill->ill_name,
2806 			    (void *)ill);
2807 		}
2808 		return (B_TRUE);
2809 	}
2810 
2811 	if (ill_dls != NULL) {
2812 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2813 		/* Soft_Ring or polling is being re-enabled */
2814 
2815 		connp = ill_dls->ill_unbind_conn;
2816 		ASSERT(rx_ring != NULL);
2817 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2818 		bzero((void *)rx_ring,
2819 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2820 		ill_dls->ill_ring_tbl = rx_ring;
2821 		ill_dls->ill_unbind_conn = connp;
2822 		return (B_TRUE);
2823 	}
2824 
2825 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
2826 	    ipst->ips_netstack)) == NULL)
2827 		return (B_FALSE);
2828 
2829 	sz = sizeof (ill_dls_capab_t);
2830 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2831 
2832 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2833 	if (ill_dls == NULL) {
2834 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2835 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2836 		    (void *)ill);
2837 		CONN_DEC_REF(connp);
2838 		return (B_FALSE);
2839 	}
2840 
2841 	/* Allocate space to hold ring table */
2842 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2843 	ill->ill_dls_capab = ill_dls;
2844 	ill_dls->ill_unbind_conn = connp;
2845 	return (B_TRUE);
2846 }
2847 
2848 /*
2849  * ill_capability_dls_disable: disable soft_ring and/or polling
2850  * capability. Since any of the rings might already be in use, need
2851  * to call ip_squeue_clean_all() which gets behind the squeue to disable
2852  * direct calls if necessary.
2853  */
2854 static void
2855 ill_capability_dls_disable(ill_t *ill)
2856 {
2857 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2858 
2859 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2860 		ip_squeue_clean_all(ill);
2861 		ill_dls->ill_tx = NULL;
2862 		ill_dls->ill_tx_handle = NULL;
2863 		ill_dls->ill_dls_change_status = NULL;
2864 		ill_dls->ill_dls_bind = NULL;
2865 		ill_dls->ill_dls_unbind = NULL;
2866 	}
2867 
2868 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2869 }
2870 
2871 static void
2872 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2873     dl_capability_sub_t *isub)
2874 {
2875 	uint_t			size;
2876 	uchar_t			*rptr;
2877 	dl_capab_dls_t	dls, *odls;
2878 	ill_dls_capab_t	*ill_dls;
2879 	mblk_t			*nmp = NULL;
2880 	dl_capability_req_t	*ocap;
2881 	uint_t			sub_dl_cap = isub->dl_cap;
2882 
2883 	if (!ill_capability_dls_init(ill))
2884 		return;
2885 	ill_dls = ill->ill_dls_capab;
2886 
2887 	/* Copy locally to get the members aligned */
2888 	bcopy((void *)idls, (void *)&dls,
2889 	    sizeof (dl_capab_dls_t));
2890 
2891 	/* Get the tx function and handle from dld */
2892 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2893 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2894 
2895 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2896 		ill_dls->ill_dls_change_status =
2897 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2898 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2899 		ill_dls->ill_dls_unbind =
2900 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2901 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2902 	}
2903 
2904 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2905 	    isub->dl_length;
2906 
2907 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2908 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2909 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2910 		    ill->ill_name, (void *)ill);
2911 		return;
2912 	}
2913 
2914 	/* initialize dl_capability_req_t */
2915 	rptr = nmp->b_rptr;
2916 	ocap = (dl_capability_req_t *)rptr;
2917 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2918 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2919 	rptr += sizeof (dl_capability_req_t);
2920 
2921 	/* initialize dl_capability_sub_t */
2922 	bcopy(isub, rptr, sizeof (*isub));
2923 	rptr += sizeof (*isub);
2924 
2925 	odls = (dl_capab_dls_t *)rptr;
2926 	rptr += sizeof (dl_capab_dls_t);
2927 
2928 	/* initialize dl_capab_dls_t to be sent down */
2929 	dls.dls_rx_handle = (uintptr_t)ill;
2930 	dls.dls_rx = (uintptr_t)ip_input;
2931 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2932 
2933 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2934 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2935 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2936 		dls.dls_flags = SOFT_RING_ENABLE;
2937 	} else {
2938 		dls.dls_flags = POLL_ENABLE;
2939 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2940 		    "to enable polling\n", ill->ill_name));
2941 	}
2942 	bcopy((void *)&dls, (void *)odls,
2943 	    sizeof (dl_capab_dls_t));
2944 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2945 	/*
2946 	 * nmp points to a DL_CAPABILITY_REQ message to
2947 	 * enable either soft_ring or polling
2948 	 */
2949 	ill_dlpi_send(ill, nmp);
2950 }
2951 
2952 static void
2953 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2954 {
2955 	mblk_t *mp;
2956 	dl_capab_dls_t *idls;
2957 	dl_capability_sub_t *dl_subcap;
2958 	int size;
2959 
2960 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2961 		return;
2962 
2963 	ASSERT(ill->ill_dls_capab != NULL);
2964 
2965 	size = sizeof (*dl_subcap) + sizeof (*idls);
2966 
2967 	mp = allocb(size, BPRI_HI);
2968 	if (mp == NULL) {
2969 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2970 		    "request to disable soft_ring\n"));
2971 		return;
2972 	}
2973 
2974 	mp->b_wptr = mp->b_rptr + size;
2975 
2976 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2977 	dl_subcap->dl_length = sizeof (*idls);
2978 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2979 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2980 	else
2981 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2982 
2983 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2984 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2985 		idls->dls_flags = SOFT_RING_DISABLE;
2986 	else
2987 		idls->dls_flags = POLL_DISABLE;
2988 
2989 	if (*sc_mp != NULL)
2990 		linkb(*sc_mp, mp);
2991 	else
2992 		*sc_mp = mp;
2993 }
2994 
2995 /*
2996  * Process a soft_ring/poll capability negotiation ack received
2997  * from a DLS Provider.isub must point to the sub-capability
2998  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
2999  */
3000 static void
3001 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3002 {
3003 	dl_capab_dls_t		*idls;
3004 	uint_t			sub_dl_cap = isub->dl_cap;
3005 	uint8_t			*capend;
3006 
3007 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
3008 	    sub_dl_cap == DL_CAPAB_POLL);
3009 
3010 	if (ill->ill_isv6)
3011 		return;
3012 
3013 	/*
3014 	 * Note: range checks here are not absolutely sufficient to
3015 	 * make us robust against malformed messages sent by drivers;
3016 	 * this is in keeping with the rest of IP's dlpi handling.
3017 	 * (Remember, it's coming from something else in the kernel
3018 	 * address space)
3019 	 */
3020 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3021 	if (capend > mp->b_wptr) {
3022 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
3023 		    "malformed sub-capability too long for mblk");
3024 		return;
3025 	}
3026 
3027 	/*
3028 	 * There are two types of acks we process here:
3029 	 * 1. acks in reply to a (first form) generic capability req
3030 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
3031 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
3032 	 *    capability req.
3033 	 */
3034 	idls = (dl_capab_dls_t *)(isub + 1);
3035 
3036 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
3037 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
3038 		    "capability isn't as expected; pass-thru "
3039 		    "module(s) detected, discarding capability\n"));
3040 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
3041 			/*
3042 			 * This is a capability renegotitation case.
3043 			 * The interface better be unusable at this
3044 			 * point other wise bad things will happen
3045 			 * if we disable direct calls on a running
3046 			 * and up interface.
3047 			 */
3048 			ill_capability_dls_disable(ill);
3049 		}
3050 		return;
3051 	}
3052 
3053 	switch (idls->dls_flags) {
3054 	default:
3055 		/* Disable if unknown flag */
3056 	case SOFT_RING_DISABLE:
3057 	case POLL_DISABLE:
3058 		ill_capability_dls_disable(ill);
3059 		break;
3060 	case SOFT_RING_CAPABLE:
3061 	case POLL_CAPABLE:
3062 		/*
3063 		 * If the capability was already enabled, its safe
3064 		 * to disable it first to get rid of stale information
3065 		 * and then start enabling it again.
3066 		 */
3067 		ill_capability_dls_disable(ill);
3068 		ill_capability_dls_capable(ill, idls, isub);
3069 		break;
3070 	case SOFT_RING_ENABLE:
3071 	case POLL_ENABLE:
3072 		mutex_enter(&ill->ill_lock);
3073 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3074 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3075 			ASSERT(ill->ill_dls_capab != NULL);
3076 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3077 		}
3078 		if (sub_dl_cap == DL_CAPAB_POLL &&
3079 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3080 			ASSERT(ill->ill_dls_capab != NULL);
3081 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3082 			ip1dbg(("ill_capability_dls_ack: interface %s "
3083 			    "has enabled polling\n", ill->ill_name));
3084 		}
3085 		mutex_exit(&ill->ill_lock);
3086 		break;
3087 	}
3088 }
3089 
3090 /*
3091  * Process a hardware checksum offload capability negotiation ack received
3092  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3093  * of a DL_CAPABILITY_ACK message.
3094  */
3095 static void
3096 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3097 {
3098 	dl_capability_req_t	*ocap;
3099 	dl_capab_hcksum_t	*ihck, *ohck;
3100 	ill_hcksum_capab_t	**ill_hcksum;
3101 	mblk_t			*nmp = NULL;
3102 	uint_t			sub_dl_cap = isub->dl_cap;
3103 	uint8_t			*capend;
3104 
3105 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3106 
3107 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3108 
3109 	/*
3110 	 * Note: range checks here are not absolutely sufficient to
3111 	 * make us robust against malformed messages sent by drivers;
3112 	 * this is in keeping with the rest of IP's dlpi handling.
3113 	 * (Remember, it's coming from something else in the kernel
3114 	 * address space)
3115 	 */
3116 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3117 	if (capend > mp->b_wptr) {
3118 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3119 		    "malformed sub-capability too long for mblk");
3120 		return;
3121 	}
3122 
3123 	/*
3124 	 * There are two types of acks we process here:
3125 	 * 1. acks in reply to a (first form) generic capability req
3126 	 *    (no ENABLE flag set)
3127 	 * 2. acks in reply to a ENABLE capability req.
3128 	 *    (ENABLE flag set)
3129 	 */
3130 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3131 
3132 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3133 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3134 		    "unsupported hardware checksum "
3135 		    "sub-capability (version %d, expected %d)",
3136 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3137 		return;
3138 	}
3139 
3140 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3141 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3142 		    "checksum capability isn't as expected; pass-thru "
3143 		    "module(s) detected, discarding capability\n"));
3144 		return;
3145 	}
3146 
3147 #define	CURR_HCKSUM_CAPAB				\
3148 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3149 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3150 
3151 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3152 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3153 		/* do ENABLE processing */
3154 		if (*ill_hcksum == NULL) {
3155 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3156 			    KM_NOSLEEP);
3157 
3158 			if (*ill_hcksum == NULL) {
3159 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3160 				    "could not enable hcksum version %d "
3161 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3162 				    ill->ill_name);
3163 				return;
3164 			}
3165 		}
3166 
3167 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3168 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3169 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3170 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3171 		    "has enabled hardware checksumming\n ",
3172 		    ill->ill_name));
3173 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3174 		/*
3175 		 * Enabling hardware checksum offload
3176 		 * Currently IP supports {TCP,UDP}/IPv4
3177 		 * partial and full cksum offload and
3178 		 * IPv4 header checksum offload.
3179 		 * Allocate new mblk which will
3180 		 * contain a new capability request
3181 		 * to enable hardware checksum offload.
3182 		 */
3183 		uint_t	size;
3184 		uchar_t	*rptr;
3185 
3186 		size = sizeof (dl_capability_req_t) +
3187 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3188 
3189 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3190 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3191 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3192 			    ill->ill_name);
3193 			return;
3194 		}
3195 
3196 		rptr = nmp->b_rptr;
3197 		/* initialize dl_capability_req_t */
3198 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3199 		ocap->dl_sub_offset =
3200 		    sizeof (dl_capability_req_t);
3201 		ocap->dl_sub_length =
3202 		    sizeof (dl_capability_sub_t) +
3203 		    isub->dl_length;
3204 		nmp->b_rptr += sizeof (dl_capability_req_t);
3205 
3206 		/* initialize dl_capability_sub_t */
3207 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3208 		nmp->b_rptr += sizeof (*isub);
3209 
3210 		/* initialize dl_capab_hcksum_t */
3211 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3212 		bcopy(ihck, ohck, sizeof (*ihck));
3213 
3214 		nmp->b_rptr = rptr;
3215 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3216 
3217 		/* Set ENABLE flag */
3218 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3219 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3220 
3221 		/*
3222 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3223 		 * hardware checksum acceleration.
3224 		 */
3225 		ill_dlpi_send(ill, nmp);
3226 	} else {
3227 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3228 		    "advertised %x hardware checksum capability flags\n",
3229 		    ill->ill_name, ihck->hcksum_txflags));
3230 	}
3231 }
3232 
3233 static void
3234 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3235 {
3236 	mblk_t *mp;
3237 	dl_capab_hcksum_t *hck_subcap;
3238 	dl_capability_sub_t *dl_subcap;
3239 	int size;
3240 
3241 	if (!ILL_HCKSUM_CAPABLE(ill))
3242 		return;
3243 
3244 	ASSERT(ill->ill_hcksum_capab != NULL);
3245 	/*
3246 	 * Clear the capability flag for hardware checksum offload but
3247 	 * retain the ill_hcksum_capab structure since it's possible that
3248 	 * another thread is still referring to it.  The structure only
3249 	 * gets deallocated when we destroy the ill.
3250 	 */
3251 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3252 
3253 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3254 
3255 	mp = allocb(size, BPRI_HI);
3256 	if (mp == NULL) {
3257 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3258 		    "request to disable hardware checksum offload\n"));
3259 		return;
3260 	}
3261 
3262 	mp->b_wptr = mp->b_rptr + size;
3263 
3264 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3265 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3266 	dl_subcap->dl_length = sizeof (*hck_subcap);
3267 
3268 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3269 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3270 	hck_subcap->hcksum_txflags = 0;
3271 
3272 	if (*sc_mp != NULL)
3273 		linkb(*sc_mp, mp);
3274 	else
3275 		*sc_mp = mp;
3276 }
3277 
3278 static void
3279 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3280 {
3281 	mblk_t *nmp = NULL;
3282 	dl_capability_req_t *oc;
3283 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3284 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3285 	uint_t sub_dl_cap = isub->dl_cap;
3286 	uint8_t *capend;
3287 
3288 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3289 
3290 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3291 
3292 	/*
3293 	 * Note: range checks here are not absolutely sufficient to
3294 	 * make us robust against malformed messages sent by drivers;
3295 	 * this is in keeping with the rest of IP's dlpi handling.
3296 	 * (Remember, it's coming from something else in the kernel
3297 	 * address space)
3298 	 */
3299 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3300 	if (capend > mp->b_wptr) {
3301 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3302 		    "malformed sub-capability too long for mblk");
3303 		return;
3304 	}
3305 
3306 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3307 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3308 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3309 		    "unsupported ZEROCOPY sub-capability (version %d, "
3310 		    "expected %d)", zc_ic->zerocopy_version,
3311 		    ZEROCOPY_VERSION_1);
3312 		return;
3313 	}
3314 
3315 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3316 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3317 		    "capability isn't as expected; pass-thru module(s) "
3318 		    "detected, discarding capability\n"));
3319 		return;
3320 	}
3321 
3322 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3323 		if (*ill_zerocopy_capab == NULL) {
3324 			*ill_zerocopy_capab =
3325 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3326 			    KM_NOSLEEP);
3327 
3328 			if (*ill_zerocopy_capab == NULL) {
3329 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3330 				    "could not enable Zero-copy version %d "
3331 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3332 				    ill->ill_name);
3333 				return;
3334 			}
3335 		}
3336 
3337 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3338 		    "supports Zero-copy version %d\n", ill->ill_name,
3339 		    ZEROCOPY_VERSION_1));
3340 
3341 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3342 		    zc_ic->zerocopy_version;
3343 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3344 		    zc_ic->zerocopy_flags;
3345 
3346 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3347 	} else {
3348 		uint_t size;
3349 		uchar_t *rptr;
3350 
3351 		size = sizeof (dl_capability_req_t) +
3352 		    sizeof (dl_capability_sub_t) +
3353 		    sizeof (dl_capab_zerocopy_t);
3354 
3355 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3356 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3357 			    "could not enable zerocopy for %s (ENOMEM)\n",
3358 			    ill->ill_name);
3359 			return;
3360 		}
3361 
3362 		rptr = nmp->b_rptr;
3363 		/* initialize dl_capability_req_t */
3364 		oc = (dl_capability_req_t *)rptr;
3365 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3366 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3367 		    sizeof (dl_capab_zerocopy_t);
3368 		rptr += sizeof (dl_capability_req_t);
3369 
3370 		/* initialize dl_capability_sub_t */
3371 		bcopy(isub, rptr, sizeof (*isub));
3372 		rptr += sizeof (*isub);
3373 
3374 		/* initialize dl_capab_zerocopy_t */
3375 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3376 		*zc_oc = *zc_ic;
3377 
3378 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3379 		    "to enable zero-copy version %d\n", ill->ill_name,
3380 		    ZEROCOPY_VERSION_1));
3381 
3382 		/* set VMSAFE_MEM flag */
3383 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3384 
3385 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3386 		ill_dlpi_send(ill, nmp);
3387 	}
3388 }
3389 
3390 static void
3391 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3392 {
3393 	mblk_t *mp;
3394 	dl_capab_zerocopy_t *zerocopy_subcap;
3395 	dl_capability_sub_t *dl_subcap;
3396 	int size;
3397 
3398 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3399 		return;
3400 
3401 	ASSERT(ill->ill_zerocopy_capab != NULL);
3402 	/*
3403 	 * Clear the capability flag for Zero-copy but retain the
3404 	 * ill_zerocopy_capab structure since it's possible that another
3405 	 * thread is still referring to it.  The structure only gets
3406 	 * deallocated when we destroy the ill.
3407 	 */
3408 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3409 
3410 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3411 
3412 	mp = allocb(size, BPRI_HI);
3413 	if (mp == NULL) {
3414 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3415 		    "request to disable Zero-copy\n"));
3416 		return;
3417 	}
3418 
3419 	mp->b_wptr = mp->b_rptr + size;
3420 
3421 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3422 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3423 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3424 
3425 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3426 	zerocopy_subcap->zerocopy_version =
3427 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3428 	zerocopy_subcap->zerocopy_flags = 0;
3429 
3430 	if (*sc_mp != NULL)
3431 		linkb(*sc_mp, mp);
3432 	else
3433 		*sc_mp = mp;
3434 }
3435 
3436 /*
3437  * Process Large Segment Offload capability negotiation ack received from a
3438  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3439  * DL_CAPABILITY_ACK message.
3440  */
3441 static void
3442 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3443 {
3444 	mblk_t *nmp = NULL;
3445 	dl_capability_req_t *oc;
3446 	dl_capab_lso_t *lso_ic, *lso_oc;
3447 	ill_lso_capab_t **ill_lso_capab;
3448 	uint_t sub_dl_cap = isub->dl_cap;
3449 	uint8_t *capend;
3450 
3451 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3452 
3453 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3454 
3455 	/*
3456 	 * Note: range checks here are not absolutely sufficient to
3457 	 * make us robust against malformed messages sent by drivers;
3458 	 * this is in keeping with the rest of IP's dlpi handling.
3459 	 * (Remember, it's coming from something else in the kernel
3460 	 * address space)
3461 	 */
3462 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3463 	if (capend > mp->b_wptr) {
3464 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3465 		    "malformed sub-capability too long for mblk");
3466 		return;
3467 	}
3468 
3469 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3470 
3471 	if (lso_ic->lso_version != LSO_VERSION_1) {
3472 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3473 		    "unsupported LSO sub-capability (version %d, expected %d)",
3474 		    lso_ic->lso_version, LSO_VERSION_1);
3475 		return;
3476 	}
3477 
3478 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3479 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3480 		    "capability isn't as expected; pass-thru module(s) "
3481 		    "detected, discarding capability\n"));
3482 		return;
3483 	}
3484 
3485 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3486 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3487 		if (*ill_lso_capab == NULL) {
3488 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3489 			    KM_NOSLEEP);
3490 
3491 			if (*ill_lso_capab == NULL) {
3492 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3493 				    "could not enable LSO version %d "
3494 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3495 				    ill->ill_name);
3496 				return;
3497 			}
3498 		}
3499 
3500 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3501 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3502 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3503 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3504 
3505 		ip1dbg(("ill_capability_lso_ack: interface %s "
3506 		    "has enabled LSO\n ", ill->ill_name));
3507 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3508 		uint_t size;
3509 		uchar_t *rptr;
3510 
3511 		size = sizeof (dl_capability_req_t) +
3512 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3513 
3514 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3515 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3516 			    "could not enable LSO for %s (ENOMEM)\n",
3517 			    ill->ill_name);
3518 			return;
3519 		}
3520 
3521 		rptr = nmp->b_rptr;
3522 		/* initialize dl_capability_req_t */
3523 		oc = (dl_capability_req_t *)nmp->b_rptr;
3524 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3525 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3526 		    sizeof (dl_capab_lso_t);
3527 		nmp->b_rptr += sizeof (dl_capability_req_t);
3528 
3529 		/* initialize dl_capability_sub_t */
3530 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3531 		nmp->b_rptr += sizeof (*isub);
3532 
3533 		/* initialize dl_capab_lso_t */
3534 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3535 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3536 
3537 		nmp->b_rptr = rptr;
3538 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3539 
3540 		/* set ENABLE flag */
3541 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3542 
3543 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3544 		ill_dlpi_send(ill, nmp);
3545 	} else {
3546 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3547 		    "advertised %x LSO capability flags\n",
3548 		    ill->ill_name, lso_ic->lso_flags));
3549 	}
3550 }
3551 
3552 
3553 static void
3554 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3555 {
3556 	mblk_t *mp;
3557 	dl_capab_lso_t *lso_subcap;
3558 	dl_capability_sub_t *dl_subcap;
3559 	int size;
3560 
3561 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3562 		return;
3563 
3564 	ASSERT(ill->ill_lso_capab != NULL);
3565 	/*
3566 	 * Clear the capability flag for LSO but retain the
3567 	 * ill_lso_capab structure since it's possible that another
3568 	 * thread is still referring to it.  The structure only gets
3569 	 * deallocated when we destroy the ill.
3570 	 */
3571 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3572 
3573 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3574 
3575 	mp = allocb(size, BPRI_HI);
3576 	if (mp == NULL) {
3577 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3578 		    "request to disable LSO\n"));
3579 		return;
3580 	}
3581 
3582 	mp->b_wptr = mp->b_rptr + size;
3583 
3584 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3585 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3586 	dl_subcap->dl_length = sizeof (*lso_subcap);
3587 
3588 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3589 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3590 	lso_subcap->lso_flags = 0;
3591 
3592 	if (*sc_mp != NULL)
3593 		linkb(*sc_mp, mp);
3594 	else
3595 		*sc_mp = mp;
3596 }
3597 
3598 /*
3599  * Consume a new-style hardware capabilities negotiation ack.
3600  * Called from ip_rput_dlpi_writer().
3601  */
3602 void
3603 ill_capability_ack(ill_t *ill, mblk_t *mp)
3604 {
3605 	dl_capability_ack_t *capp;
3606 	dl_capability_sub_t *subp, *endp;
3607 
3608 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3609 		ill->ill_dlpi_capab_state = IDS_OK;
3610 
3611 	capp = (dl_capability_ack_t *)mp->b_rptr;
3612 
3613 	if (capp->dl_sub_length == 0)
3614 		/* no new-style capabilities */
3615 		return;
3616 
3617 	/* make sure the driver supplied correct dl_sub_length */
3618 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3619 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3620 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3621 		return;
3622 	}
3623 
3624 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3625 	/*
3626 	 * There are sub-capabilities. Process the ones we know about.
3627 	 * Loop until we don't have room for another sub-cap header..
3628 	 */
3629 	for (subp = SC(capp, capp->dl_sub_offset),
3630 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3631 	    subp <= endp;
3632 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3633 
3634 		switch (subp->dl_cap) {
3635 		case DL_CAPAB_ID_WRAPPER:
3636 			ill_capability_id_ack(ill, mp, subp);
3637 			break;
3638 		default:
3639 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3640 			break;
3641 		}
3642 	}
3643 #undef SC
3644 }
3645 
3646 /*
3647  * This routine is called to scan the fragmentation reassembly table for
3648  * the specified ILL for any packets that are starting to smell.
3649  * dead_interval is the maximum time in seconds that will be tolerated.  It
3650  * will either be the value specified in ip_g_frag_timeout, or zero if the
3651  * ILL is shutting down and it is time to blow everything off.
3652  *
3653  * It returns the number of seconds (as a time_t) that the next frag timer
3654  * should be scheduled for, 0 meaning that the timer doesn't need to be
3655  * re-started.  Note that the method of calculating next_timeout isn't
3656  * entirely accurate since time will flow between the time we grab
3657  * current_time and the time we schedule the next timeout.  This isn't a
3658  * big problem since this is the timer for sending an ICMP reassembly time
3659  * exceeded messages, and it doesn't have to be exactly accurate.
3660  *
3661  * This function is
3662  * sometimes called as writer, although this is not required.
3663  */
3664 time_t
3665 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3666 {
3667 	ipfb_t	*ipfb;
3668 	ipfb_t	*endp;
3669 	ipf_t	*ipf;
3670 	ipf_t	*ipfnext;
3671 	mblk_t	*mp;
3672 	time_t	current_time = gethrestime_sec();
3673 	time_t	next_timeout = 0;
3674 	uint32_t	hdr_length;
3675 	mblk_t	*send_icmp_head;
3676 	mblk_t	*send_icmp_head_v6;
3677 	zoneid_t zoneid;
3678 	ip_stack_t *ipst = ill->ill_ipst;
3679 
3680 	ipfb = ill->ill_frag_hash_tbl;
3681 	if (ipfb == NULL)
3682 		return (B_FALSE);
3683 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3684 	/* Walk the frag hash table. */
3685 	for (; ipfb < endp; ipfb++) {
3686 		send_icmp_head = NULL;
3687 		send_icmp_head_v6 = NULL;
3688 		mutex_enter(&ipfb->ipfb_lock);
3689 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3690 			time_t frag_time = current_time - ipf->ipf_timestamp;
3691 			time_t frag_timeout;
3692 
3693 			if (frag_time < dead_interval) {
3694 				/*
3695 				 * There are some outstanding fragments
3696 				 * that will timeout later.  Make note of
3697 				 * the time so that we can reschedule the
3698 				 * next timeout appropriately.
3699 				 */
3700 				frag_timeout = dead_interval - frag_time;
3701 				if (next_timeout == 0 ||
3702 				    frag_timeout < next_timeout) {
3703 					next_timeout = frag_timeout;
3704 				}
3705 				break;
3706 			}
3707 			/* Time's up.  Get it out of here. */
3708 			hdr_length = ipf->ipf_nf_hdr_len;
3709 			ipfnext = ipf->ipf_hash_next;
3710 			if (ipfnext)
3711 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3712 			*ipf->ipf_ptphn = ipfnext;
3713 			mp = ipf->ipf_mp->b_cont;
3714 			for (; mp; mp = mp->b_cont) {
3715 				/* Extra points for neatness. */
3716 				IP_REASS_SET_START(mp, 0);
3717 				IP_REASS_SET_END(mp, 0);
3718 			}
3719 			mp = ipf->ipf_mp->b_cont;
3720 			ill->ill_frag_count -= ipf->ipf_count;
3721 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3722 			ipfb->ipfb_count -= ipf->ipf_count;
3723 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3724 			ipfb->ipfb_frag_pkts--;
3725 			/*
3726 			 * We do not send any icmp message from here because
3727 			 * we currently are holding the ipfb_lock for this
3728 			 * hash chain. If we try and send any icmp messages
3729 			 * from here we may end up via a put back into ip
3730 			 * trying to get the same lock, causing a recursive
3731 			 * mutex panic. Instead we build a list and send all
3732 			 * the icmp messages after we have dropped the lock.
3733 			 */
3734 			if (ill->ill_isv6) {
3735 				if (hdr_length != 0) {
3736 					mp->b_next = send_icmp_head_v6;
3737 					send_icmp_head_v6 = mp;
3738 				} else {
3739 					freemsg(mp);
3740 				}
3741 			} else {
3742 				if (hdr_length != 0) {
3743 					mp->b_next = send_icmp_head;
3744 					send_icmp_head = mp;
3745 				} else {
3746 					freemsg(mp);
3747 				}
3748 			}
3749 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3750 			freeb(ipf->ipf_mp);
3751 		}
3752 		mutex_exit(&ipfb->ipfb_lock);
3753 		/*
3754 		 * Now need to send any icmp messages that we delayed from
3755 		 * above.
3756 		 */
3757 		while (send_icmp_head_v6 != NULL) {
3758 			ip6_t *ip6h;
3759 
3760 			mp = send_icmp_head_v6;
3761 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3762 			mp->b_next = NULL;
3763 			if (mp->b_datap->db_type == M_CTL)
3764 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3765 			else
3766 				ip6h = (ip6_t *)mp->b_rptr;
3767 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3768 			    ill, ipst);
3769 			if (zoneid == ALL_ZONES) {
3770 				freemsg(mp);
3771 			} else {
3772 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3773 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3774 				    B_FALSE, zoneid, ipst);
3775 			}
3776 		}
3777 		while (send_icmp_head != NULL) {
3778 			ipaddr_t dst;
3779 
3780 			mp = send_icmp_head;
3781 			send_icmp_head = send_icmp_head->b_next;
3782 			mp->b_next = NULL;
3783 
3784 			if (mp->b_datap->db_type == M_CTL)
3785 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3786 			else
3787 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3788 
3789 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3790 			if (zoneid == ALL_ZONES) {
3791 				freemsg(mp);
3792 			} else {
3793 				icmp_time_exceeded(ill->ill_wq, mp,
3794 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3795 				    ipst);
3796 			}
3797 		}
3798 	}
3799 	/*
3800 	 * A non-dying ILL will use the return value to decide whether to
3801 	 * restart the frag timer, and for how long.
3802 	 */
3803 	return (next_timeout);
3804 }
3805 
3806 /*
3807  * This routine is called when the approximate count of mblk memory used
3808  * for the specified ILL has exceeded max_count.
3809  */
3810 void
3811 ill_frag_prune(ill_t *ill, uint_t max_count)
3812 {
3813 	ipfb_t	*ipfb;
3814 	ipf_t	*ipf;
3815 	size_t	count;
3816 
3817 	/*
3818 	 * If we are here within ip_min_frag_prune_time msecs remove
3819 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3820 	 * ill_frag_free_num_pkts.
3821 	 */
3822 	mutex_enter(&ill->ill_lock);
3823 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3824 	    (ip_min_frag_prune_time != 0 ?
3825 	    ip_min_frag_prune_time : msec_per_tick)) {
3826 
3827 		ill->ill_frag_free_num_pkts++;
3828 
3829 	} else {
3830 		ill->ill_frag_free_num_pkts = 0;
3831 	}
3832 	ill->ill_last_frag_clean_time = lbolt;
3833 	mutex_exit(&ill->ill_lock);
3834 
3835 	/*
3836 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3837 	 */
3838 	if (ill->ill_frag_free_num_pkts != 0) {
3839 		int ix;
3840 
3841 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3842 			ipfb = &ill->ill_frag_hash_tbl[ix];
3843 			mutex_enter(&ipfb->ipfb_lock);
3844 			if (ipfb->ipfb_ipf != NULL) {
3845 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3846 				    ill->ill_frag_free_num_pkts);
3847 			}
3848 			mutex_exit(&ipfb->ipfb_lock);
3849 		}
3850 	}
3851 	/*
3852 	 * While the reassembly list for this ILL is too big, prune a fragment
3853 	 * queue by age, oldest first.  Note that the per ILL count is
3854 	 * approximate, while the per frag hash bucket counts are accurate.
3855 	 */
3856 	while (ill->ill_frag_count > max_count) {
3857 		int	ix;
3858 		ipfb_t	*oipfb = NULL;
3859 		uint_t	oldest = UINT_MAX;
3860 
3861 		count = 0;
3862 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3863 			ipfb = &ill->ill_frag_hash_tbl[ix];
3864 			mutex_enter(&ipfb->ipfb_lock);
3865 			ipf = ipfb->ipfb_ipf;
3866 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3867 				oldest = ipf->ipf_gen;
3868 				oipfb = ipfb;
3869 			}
3870 			count += ipfb->ipfb_count;
3871 			mutex_exit(&ipfb->ipfb_lock);
3872 		}
3873 		/* Refresh the per ILL count */
3874 		ill->ill_frag_count = count;
3875 		if (oipfb == NULL) {
3876 			ill->ill_frag_count = 0;
3877 			break;
3878 		}
3879 		if (count <= max_count)
3880 			return;	/* Somebody beat us to it, nothing to do */
3881 		mutex_enter(&oipfb->ipfb_lock);
3882 		ipf = oipfb->ipfb_ipf;
3883 		if (ipf != NULL) {
3884 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3885 		}
3886 		mutex_exit(&oipfb->ipfb_lock);
3887 	}
3888 }
3889 
3890 /*
3891  * free 'free_cnt' fragmented packets starting at ipf.
3892  */
3893 void
3894 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3895 {
3896 	size_t	count;
3897 	mblk_t	*mp;
3898 	mblk_t	*tmp;
3899 	ipf_t **ipfp = ipf->ipf_ptphn;
3900 
3901 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3902 	ASSERT(ipfp != NULL);
3903 	ASSERT(ipf != NULL);
3904 
3905 	while (ipf != NULL && free_cnt-- > 0) {
3906 		count = ipf->ipf_count;
3907 		mp = ipf->ipf_mp;
3908 		ipf = ipf->ipf_hash_next;
3909 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3910 			IP_REASS_SET_START(tmp, 0);
3911 			IP_REASS_SET_END(tmp, 0);
3912 		}
3913 		ill->ill_frag_count -= count;
3914 		ASSERT(ipfb->ipfb_count >= count);
3915 		ipfb->ipfb_count -= count;
3916 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3917 		ipfb->ipfb_frag_pkts--;
3918 		freemsg(mp);
3919 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3920 	}
3921 
3922 	if (ipf)
3923 		ipf->ipf_ptphn = ipfp;
3924 	ipfp[0] = ipf;
3925 }
3926 
3927 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3928 	"obsolete and may be removed in a future release of Solaris.  Use " \
3929 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3930 
3931 /*
3932  * For obsolete per-interface forwarding configuration;
3933  * called in response to ND_GET.
3934  */
3935 /* ARGSUSED */
3936 static int
3937 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3938 {
3939 	ill_t *ill = (ill_t *)cp;
3940 
3941 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3942 
3943 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3944 	return (0);
3945 }
3946 
3947 /*
3948  * For obsolete per-interface forwarding configuration;
3949  * called in response to ND_SET.
3950  */
3951 /* ARGSUSED */
3952 static int
3953 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3954     cred_t *ioc_cr)
3955 {
3956 	long value;
3957 	int retval;
3958 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3959 
3960 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3961 
3962 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3963 	    value < 0 || value > 1) {
3964 		return (EINVAL);
3965 	}
3966 
3967 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3968 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3969 	rw_exit(&ipst->ips_ill_g_lock);
3970 	return (retval);
3971 }
3972 
3973 /*
3974  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3975  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3976  * up RTS_IFINFO routing socket messages for each interface whose flags we
3977  * change.
3978  */
3979 int
3980 ill_forward_set(ill_t *ill, boolean_t enable)
3981 {
3982 	ill_group_t *illgrp;
3983 	ip_stack_t	*ipst = ill->ill_ipst;
3984 
3985 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3986 
3987 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3988 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3989 		return (0);
3990 
3991 	if (IS_LOOPBACK(ill))
3992 		return (EINVAL);
3993 
3994 	/*
3995 	 * If the ill is in an IPMP group, set the forwarding policy on all
3996 	 * members of the group to the same value.
3997 	 */
3998 	illgrp = ill->ill_group;
3999 	if (illgrp != NULL) {
4000 		ill_t *tmp_ill;
4001 
4002 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
4003 		    tmp_ill = tmp_ill->ill_group_next) {
4004 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
4005 			    (enable ? "Enabling" : "Disabling"),
4006 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
4007 			    tmp_ill->ill_name));
4008 			mutex_enter(&tmp_ill->ill_lock);
4009 			if (enable)
4010 				tmp_ill->ill_flags |= ILLF_ROUTER;
4011 			else
4012 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
4013 			mutex_exit(&tmp_ill->ill_lock);
4014 			if (tmp_ill->ill_isv6)
4015 				ill_set_nce_router_flags(tmp_ill, enable);
4016 			/* Notify routing socket listeners of this change. */
4017 			ip_rts_ifmsg(tmp_ill->ill_ipif);
4018 		}
4019 	} else {
4020 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
4021 		    (enable ? "Enabling" : "Disabling"),
4022 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
4023 		mutex_enter(&ill->ill_lock);
4024 		if (enable)
4025 			ill->ill_flags |= ILLF_ROUTER;
4026 		else
4027 			ill->ill_flags &= ~ILLF_ROUTER;
4028 		mutex_exit(&ill->ill_lock);
4029 		if (ill->ill_isv6)
4030 			ill_set_nce_router_flags(ill, enable);
4031 		/* Notify routing socket listeners of this change. */
4032 		ip_rts_ifmsg(ill->ill_ipif);
4033 	}
4034 
4035 	return (0);
4036 }
4037 
4038 /*
4039  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
4040  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
4041  * set or clear.
4042  */
4043 static void
4044 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
4045 {
4046 	ipif_t *ipif;
4047 	nce_t *nce;
4048 
4049 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4050 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
4051 		if (nce != NULL) {
4052 			mutex_enter(&nce->nce_lock);
4053 			if (enable)
4054 				nce->nce_flags |= NCE_F_ISROUTER;
4055 			else
4056 				nce->nce_flags &= ~NCE_F_ISROUTER;
4057 			mutex_exit(&nce->nce_lock);
4058 			NCE_REFRELE(nce);
4059 		}
4060 	}
4061 }
4062 
4063 /*
4064  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
4065  * for this ill.  Make sure the v6/v4 question has been answered about this
4066  * ill.  The creation of this ndd variable is only for backwards compatibility.
4067  * The preferred way to control per-interface IP forwarding is through the
4068  * ILLF_ROUTER interface flag.
4069  */
4070 static int
4071 ill_set_ndd_name(ill_t *ill)
4072 {
4073 	char *suffix;
4074 	ip_stack_t	*ipst = ill->ill_ipst;
4075 
4076 	ASSERT(IAM_WRITER_ILL(ill));
4077 
4078 	if (ill->ill_isv6)
4079 		suffix = ipv6_forward_suffix;
4080 	else
4081 		suffix = ipv4_forward_suffix;
4082 
4083 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4084 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4085 	/*
4086 	 * Copies over the '\0'.
4087 	 * Note that strlen(suffix) is always bounded.
4088 	 */
4089 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4090 	    strlen(suffix) + 1);
4091 
4092 	/*
4093 	 * Use of the nd table requires holding the reader lock.
4094 	 * Modifying the nd table thru nd_load/nd_unload requires
4095 	 * the writer lock.
4096 	 */
4097 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
4098 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4099 	    nd_ill_forward_set, (caddr_t)ill)) {
4100 		/*
4101 		 * If the nd_load failed, it only meant that it could not
4102 		 * allocate a new bunch of room for further NDD expansion.
4103 		 * Because of that, the ill_ndd_name will be set to 0, and
4104 		 * this interface is at the mercy of the global ip_forwarding
4105 		 * variable.
4106 		 */
4107 		rw_exit(&ipst->ips_ip_g_nd_lock);
4108 		ill->ill_ndd_name = NULL;
4109 		return (ENOMEM);
4110 	}
4111 	rw_exit(&ipst->ips_ip_g_nd_lock);
4112 	return (0);
4113 }
4114 
4115 /*
4116  * Intializes the context structure and returns the first ill in the list
4117  * cuurently start_list and end_list can have values:
4118  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4119  * IP_V4_G_HEAD		Traverse IPV4 list only.
4120  * IP_V6_G_HEAD		Traverse IPV6 list only.
4121  */
4122 
4123 /*
4124  * We don't check for CONDEMNED ills here. Caller must do that if
4125  * necessary under the ill lock.
4126  */
4127 ill_t *
4128 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
4129     ip_stack_t *ipst)
4130 {
4131 	ill_if_t *ifp;
4132 	ill_t *ill;
4133 	avl_tree_t *avl_tree;
4134 
4135 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4136 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4137 
4138 	/*
4139 	 * setup the lists to search
4140 	 */
4141 	if (end_list != MAX_G_HEADS) {
4142 		ctx->ctx_current_list = start_list;
4143 		ctx->ctx_last_list = end_list;
4144 	} else {
4145 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4146 		ctx->ctx_current_list = 0;
4147 	}
4148 
4149 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4150 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4151 		if (ifp != (ill_if_t *)
4152 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4153 			avl_tree = &ifp->illif_avl_by_ppa;
4154 			ill = avl_first(avl_tree);
4155 			/*
4156 			 * ill is guaranteed to be non NULL or ifp should have
4157 			 * not existed.
4158 			 */
4159 			ASSERT(ill != NULL);
4160 			return (ill);
4161 		}
4162 		ctx->ctx_current_list++;
4163 	}
4164 
4165 	return (NULL);
4166 }
4167 
4168 /*
4169  * returns the next ill in the list. ill_first() must have been called
4170  * before calling ill_next() or bad things will happen.
4171  */
4172 
4173 /*
4174  * We don't check for CONDEMNED ills here. Caller must do that if
4175  * necessary under the ill lock.
4176  */
4177 ill_t *
4178 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4179 {
4180 	ill_if_t *ifp;
4181 	ill_t *ill;
4182 	ip_stack_t	*ipst = lastill->ill_ipst;
4183 
4184 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4185 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
4186 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4187 	    AVL_AFTER)) != NULL) {
4188 		return (ill);
4189 	}
4190 
4191 	/* goto next ill_ifp in the list. */
4192 	ifp = lastill->ill_ifptr->illif_next;
4193 
4194 	/* make sure not at end of circular list */
4195 	while (ifp ==
4196 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4197 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4198 			return (NULL);
4199 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4200 	}
4201 
4202 	return (avl_first(&ifp->illif_avl_by_ppa));
4203 }
4204 
4205 /*
4206  * Check interface name for correct format which is name+ppa.
4207  * name can contain characters and digits, the right most digits
4208  * make up the ppa number. use of octal is not allowed, name must contain
4209  * a ppa, return pointer to the start of ppa.
4210  * In case of error return NULL.
4211  */
4212 static char *
4213 ill_get_ppa_ptr(char *name)
4214 {
4215 	int namelen = mi_strlen(name);
4216 
4217 	int len = namelen;
4218 
4219 	name += len;
4220 	while (len > 0) {
4221 		name--;
4222 		if (*name < '0' || *name > '9')
4223 			break;
4224 		len--;
4225 	}
4226 
4227 	/* empty string, all digits, or no trailing digits */
4228 	if (len == 0 || len == (int)namelen)
4229 		return (NULL);
4230 
4231 	name++;
4232 	/* check for attempted use of octal */
4233 	if (*name == '0' && len != (int)namelen - 1)
4234 		return (NULL);
4235 	return (name);
4236 }
4237 
4238 /*
4239  * use avl tree to locate the ill.
4240  */
4241 static ill_t *
4242 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4243     ipsq_func_t func, int *error, ip_stack_t *ipst)
4244 {
4245 	char *ppa_ptr = NULL;
4246 	int len;
4247 	uint_t ppa;
4248 	ill_t *ill = NULL;
4249 	ill_if_t *ifp;
4250 	int list;
4251 	ipsq_t *ipsq;
4252 
4253 	if (error != NULL)
4254 		*error = 0;
4255 
4256 	/*
4257 	 * get ppa ptr
4258 	 */
4259 	if (isv6)
4260 		list = IP_V6_G_HEAD;
4261 	else
4262 		list = IP_V4_G_HEAD;
4263 
4264 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4265 		if (error != NULL)
4266 			*error = ENXIO;
4267 		return (NULL);
4268 	}
4269 
4270 	len = ppa_ptr - name + 1;
4271 
4272 	ppa = stoi(&ppa_ptr);
4273 
4274 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4275 
4276 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4277 		/*
4278 		 * match is done on len - 1 as the name is not null
4279 		 * terminated it contains ppa in addition to the interface
4280 		 * name.
4281 		 */
4282 		if ((ifp->illif_name_len == len) &&
4283 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4284 			break;
4285 		} else {
4286 			ifp = ifp->illif_next;
4287 		}
4288 	}
4289 
4290 
4291 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4292 		/*
4293 		 * Even the interface type does not exist.
4294 		 */
4295 		if (error != NULL)
4296 			*error = ENXIO;
4297 		return (NULL);
4298 	}
4299 
4300 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4301 	if (ill != NULL) {
4302 		/*
4303 		 * The block comment at the start of ipif_down
4304 		 * explains the use of the macros used below
4305 		 */
4306 		GRAB_CONN_LOCK(q);
4307 		mutex_enter(&ill->ill_lock);
4308 		if (ILL_CAN_LOOKUP(ill)) {
4309 			ill_refhold_locked(ill);
4310 			mutex_exit(&ill->ill_lock);
4311 			RELEASE_CONN_LOCK(q);
4312 			return (ill);
4313 		} else if (ILL_CAN_WAIT(ill, q)) {
4314 			ipsq = ill->ill_phyint->phyint_ipsq;
4315 			mutex_enter(&ipsq->ipsq_lock);
4316 			mutex_exit(&ill->ill_lock);
4317 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4318 			mutex_exit(&ipsq->ipsq_lock);
4319 			RELEASE_CONN_LOCK(q);
4320 			*error = EINPROGRESS;
4321 			return (NULL);
4322 		}
4323 		mutex_exit(&ill->ill_lock);
4324 		RELEASE_CONN_LOCK(q);
4325 	}
4326 	if (error != NULL)
4327 		*error = ENXIO;
4328 	return (NULL);
4329 }
4330 
4331 /*
4332  * comparison function for use with avl.
4333  */
4334 static int
4335 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4336 {
4337 	uint_t ppa;
4338 	uint_t ill_ppa;
4339 
4340 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4341 
4342 	ppa = *((uint_t *)ppa_ptr);
4343 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4344 	/*
4345 	 * We want the ill with the lowest ppa to be on the
4346 	 * top.
4347 	 */
4348 	if (ill_ppa < ppa)
4349 		return (1);
4350 	if (ill_ppa > ppa)
4351 		return (-1);
4352 	return (0);
4353 }
4354 
4355 /*
4356  * remove an interface type from the global list.
4357  */
4358 static void
4359 ill_delete_interface_type(ill_if_t *interface)
4360 {
4361 	ASSERT(interface != NULL);
4362 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4363 
4364 	avl_destroy(&interface->illif_avl_by_ppa);
4365 	if (interface->illif_ppa_arena != NULL)
4366 		vmem_destroy(interface->illif_ppa_arena);
4367 
4368 	remque(interface);
4369 
4370 	mi_free(interface);
4371 }
4372 
4373 /* Defined in ip_netinfo.c */
4374 extern ddi_taskq_t	*eventq_queue_nic;
4375 
4376 /*
4377  * remove ill from the global list.
4378  */
4379 static void
4380 ill_glist_delete(ill_t *ill)
4381 {
4382 	char *nicname;
4383 	size_t nicnamelen;
4384 	hook_nic_event_t *info;
4385 	ip_stack_t	*ipst;
4386 
4387 	if (ill == NULL)
4388 		return;
4389 	ipst = ill->ill_ipst;
4390 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4391 
4392 	if (ill->ill_name != NULL) {
4393 		nicname = kmem_alloc(ill->ill_name_length, KM_NOSLEEP);
4394 		if (nicname != NULL) {
4395 			bcopy(ill->ill_name, nicname, ill->ill_name_length);
4396 			nicnamelen = ill->ill_name_length;
4397 		}
4398 	} else {
4399 		nicname = NULL;
4400 		nicnamelen = 0;
4401 	}
4402 
4403 	/*
4404 	 * If the ill was never inserted into the AVL tree
4405 	 * we skip the if branch.
4406 	 */
4407 	if (ill->ill_ifptr != NULL) {
4408 		/*
4409 		 * remove from AVL tree and free ppa number
4410 		 */
4411 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4412 
4413 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4414 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4415 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4416 		}
4417 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4418 			ill_delete_interface_type(ill->ill_ifptr);
4419 		}
4420 
4421 		/*
4422 		 * Indicate ill is no longer in the list.
4423 		 */
4424 		ill->ill_ifptr = NULL;
4425 		ill->ill_name_length = 0;
4426 		ill->ill_name[0] = '\0';
4427 		ill->ill_ppa = UINT_MAX;
4428 	}
4429 
4430 	/*
4431 	 * Run the unplumb hook after the NIC has disappeared from being
4432 	 * visible so that attempts to revalidate its existance will fail.
4433 	 *
4434 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4435 	 * that the ordering of delivered events to listeners matches the
4436 	 * order of them in the kernel.
4437 	 */
4438 	if ((info = ill->ill_nic_event_info) != NULL) {
4439 		if (info->hne_event != NE_DOWN) {
4440 			ip2dbg(("ill_glist_delete: unexpected nic event %d "
4441 			    "attached for %s\n", info->hne_event,
4442 			    ill->ill_name));
4443 			if (info->hne_data != NULL)
4444 				kmem_free(info->hne_data, info->hne_datalen);
4445 			kmem_free(info, sizeof (hook_nic_event_t));
4446 		} else {
4447 			if (ddi_taskq_dispatch(eventq_queue_nic,
4448 			    ip_ne_queue_func, (void *)info, DDI_SLEEP)
4449 			    == DDI_FAILURE) {
4450 				ip2dbg(("ill_glist_delete: ddi_taskq_dispatch "
4451 				    "failed\n"));
4452 				if (info->hne_data != NULL)
4453 					kmem_free(info->hne_data,
4454 					    info->hne_datalen);
4455 				kmem_free(info, sizeof (hook_nic_event_t));
4456 			}
4457 		}
4458 	}
4459 
4460 	/* Generate NE_UNPLUMB event for ill_name. */
4461 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
4462 	if (info != NULL) {
4463 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
4464 		info->hne_lif = 0;
4465 		info->hne_event = NE_UNPLUMB;
4466 		info->hne_data = nicname;
4467 		info->hne_datalen = nicnamelen;
4468 		info->hne_family = ill->ill_isv6 ?
4469 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
4470 	} else {
4471 		ip2dbg(("ill_glist_delete: could not attach UNPLUMB nic event "
4472 		    "information for %s (ENOMEM)\n", ill->ill_name));
4473 		if (nicname != NULL)
4474 			kmem_free(nicname, nicnamelen);
4475 	}
4476 
4477 	ill->ill_nic_event_info = info;
4478 
4479 	ill_phyint_free(ill);
4480 	rw_exit(&ipst->ips_ill_g_lock);
4481 }
4482 
4483 /*
4484  * allocate a ppa, if the number of plumbed interfaces of this type are
4485  * less than ill_no_arena do a linear search to find a unused ppa.
4486  * When the number goes beyond ill_no_arena switch to using an arena.
4487  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4488  * is the return value for an error condition, so allocation starts at one
4489  * and is decremented by one.
4490  */
4491 static int
4492 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4493 {
4494 	ill_t *tmp_ill;
4495 	uint_t start, end;
4496 	int ppa;
4497 
4498 	if (ifp->illif_ppa_arena == NULL &&
4499 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4500 		/*
4501 		 * Create an arena.
4502 		 */
4503 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4504 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4505 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4506 			/* allocate what has already been assigned */
4507 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4508 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4509 		    tmp_ill, AVL_AFTER)) {
4510 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4511 			    1,		/* size */
4512 			    1,		/* align/quantum */
4513 			    0,		/* phase */
4514 			    0,		/* nocross */
4515 			    /* minaddr */
4516 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4517 			    /* maxaddr */
4518 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4519 			    VM_NOSLEEP|VM_FIRSTFIT);
4520 			if (ppa == 0) {
4521 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4522 				    " failed while switching"));
4523 				vmem_destroy(ifp->illif_ppa_arena);
4524 				ifp->illif_ppa_arena = NULL;
4525 				break;
4526 			}
4527 		}
4528 	}
4529 
4530 	if (ifp->illif_ppa_arena != NULL) {
4531 		if (ill->ill_ppa == UINT_MAX) {
4532 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4533 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4534 			if (ppa == 0)
4535 				return (EAGAIN);
4536 			ill->ill_ppa = --ppa;
4537 		} else {
4538 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4539 			    1, 		/* size */
4540 			    1, 		/* align/quantum */
4541 			    0, 		/* phase */
4542 			    0, 		/* nocross */
4543 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4544 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4545 			    VM_NOSLEEP|VM_FIRSTFIT);
4546 			/*
4547 			 * Most likely the allocation failed because
4548 			 * the requested ppa was in use.
4549 			 */
4550 			if (ppa == 0)
4551 				return (EEXIST);
4552 		}
4553 		return (0);
4554 	}
4555 
4556 	/*
4557 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4558 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4559 	 */
4560 	if (ill->ill_ppa == UINT_MAX) {
4561 		end = UINT_MAX - 1;
4562 		start = 0;
4563 	} else {
4564 		end = start = ill->ill_ppa;
4565 	}
4566 
4567 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4568 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4569 		if (start++ >= end) {
4570 			if (ill->ill_ppa == UINT_MAX)
4571 				return (EAGAIN);
4572 			else
4573 				return (EEXIST);
4574 		}
4575 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4576 	}
4577 	ill->ill_ppa = start;
4578 	return (0);
4579 }
4580 
4581 /*
4582  * Insert ill into the list of configured ill's. Once this function completes,
4583  * the ill is globally visible and is available through lookups. More precisely
4584  * this happens after the caller drops the ill_g_lock.
4585  */
4586 static int
4587 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4588 {
4589 	ill_if_t *ill_interface;
4590 	avl_index_t where = 0;
4591 	int error;
4592 	int name_length;
4593 	int index;
4594 	boolean_t check_length = B_FALSE;
4595 	ip_stack_t	*ipst = ill->ill_ipst;
4596 
4597 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4598 
4599 	name_length = mi_strlen(name) + 1;
4600 
4601 	if (isv6)
4602 		index = IP_V6_G_HEAD;
4603 	else
4604 		index = IP_V4_G_HEAD;
4605 
4606 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4607 	/*
4608 	 * Search for interface type based on name
4609 	 */
4610 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4611 		if ((ill_interface->illif_name_len == name_length) &&
4612 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4613 			break;
4614 		}
4615 		ill_interface = ill_interface->illif_next;
4616 	}
4617 
4618 	/*
4619 	 * Interface type not found, create one.
4620 	 */
4621 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4622 
4623 		ill_g_head_t ghead;
4624 
4625 		/*
4626 		 * allocate ill_if_t structure
4627 		 */
4628 
4629 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4630 		if (ill_interface == NULL) {
4631 			return (ENOMEM);
4632 		}
4633 
4634 
4635 
4636 		(void) strcpy(ill_interface->illif_name, name);
4637 		ill_interface->illif_name_len = name_length;
4638 
4639 		avl_create(&ill_interface->illif_avl_by_ppa,
4640 		    ill_compare_ppa, sizeof (ill_t),
4641 		    offsetof(struct ill_s, ill_avl_byppa));
4642 
4643 		/*
4644 		 * link the structure in the back to maintain order
4645 		 * of configuration for ifconfig output.
4646 		 */
4647 		ghead = ipst->ips_ill_g_heads[index];
4648 		insque(ill_interface, ghead.ill_g_list_tail);
4649 
4650 	}
4651 
4652 	if (ill->ill_ppa == UINT_MAX)
4653 		check_length = B_TRUE;
4654 
4655 	error = ill_alloc_ppa(ill_interface, ill);
4656 	if (error != 0) {
4657 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4658 			ill_delete_interface_type(ill->ill_ifptr);
4659 		return (error);
4660 	}
4661 
4662 	/*
4663 	 * When the ppa is choosen by the system, check that there is
4664 	 * enough space to insert ppa. if a specific ppa was passed in this
4665 	 * check is not required as the interface name passed in will have
4666 	 * the right ppa in it.
4667 	 */
4668 	if (check_length) {
4669 		/*
4670 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4671 		 */
4672 		char buf[sizeof (uint_t) * 3];
4673 
4674 		/*
4675 		 * convert ppa to string to calculate the amount of space
4676 		 * required for it in the name.
4677 		 */
4678 		numtos(ill->ill_ppa, buf);
4679 
4680 		/* Do we have enough space to insert ppa ? */
4681 
4682 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4683 			/* Free ppa and interface type struct */
4684 			if (ill_interface->illif_ppa_arena != NULL) {
4685 				vmem_free(ill_interface->illif_ppa_arena,
4686 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4687 			}
4688 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4689 			    0) {
4690 				ill_delete_interface_type(ill->ill_ifptr);
4691 			}
4692 
4693 			return (EINVAL);
4694 		}
4695 	}
4696 
4697 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4698 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4699 
4700 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4701 	    &where);
4702 	ill->ill_ifptr = ill_interface;
4703 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4704 
4705 	ill_phyint_reinit(ill);
4706 	return (0);
4707 }
4708 
4709 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4710 static boolean_t
4711 ipsq_init(ill_t *ill)
4712 {
4713 	ipsq_t  *ipsq;
4714 
4715 	/* Init the ipsq and impicitly enter as writer */
4716 	ill->ill_phyint->phyint_ipsq =
4717 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4718 	if (ill->ill_phyint->phyint_ipsq == NULL)
4719 		return (B_FALSE);
4720 	ipsq = ill->ill_phyint->phyint_ipsq;
4721 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4722 	ill->ill_phyint->phyint_ipsq_next = NULL;
4723 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4724 	ipsq->ipsq_refs = 1;
4725 	ipsq->ipsq_writer = curthread;
4726 	ipsq->ipsq_reentry_cnt = 1;
4727 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4728 #ifdef ILL_DEBUG
4729 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack, IP_STACK_DEPTH);
4730 #endif
4731 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4732 	return (B_TRUE);
4733 }
4734 
4735 /*
4736  * ill_init is called by ip_open when a device control stream is opened.
4737  * It does a few initializations, and shoots a DL_INFO_REQ message down
4738  * to the driver.  The response is later picked up in ip_rput_dlpi and
4739  * used to set up default mechanisms for talking to the driver.  (Always
4740  * called as writer.)
4741  *
4742  * If this function returns error, ip_open will call ip_close which in
4743  * turn will call ill_delete to clean up any memory allocated here that
4744  * is not yet freed.
4745  */
4746 int
4747 ill_init(queue_t *q, ill_t *ill)
4748 {
4749 	int	count;
4750 	dl_info_req_t	*dlir;
4751 	mblk_t	*info_mp;
4752 	uchar_t *frag_ptr;
4753 
4754 	/*
4755 	 * The ill is initialized to zero by mi_alloc*(). In addition
4756 	 * some fields already contain valid values, initialized in
4757 	 * ip_open(), before we reach here.
4758 	 */
4759 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4760 
4761 	ill->ill_rq = q;
4762 	ill->ill_wq = WR(q);
4763 
4764 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4765 	    BPRI_HI);
4766 	if (info_mp == NULL)
4767 		return (ENOMEM);
4768 
4769 	/*
4770 	 * Allocate sufficient space to contain our fragment hash table and
4771 	 * the device name.
4772 	 */
4773 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4774 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4775 	if (frag_ptr == NULL) {
4776 		freemsg(info_mp);
4777 		return (ENOMEM);
4778 	}
4779 	ill->ill_frag_ptr = frag_ptr;
4780 	ill->ill_frag_free_num_pkts = 0;
4781 	ill->ill_last_frag_clean_time = 0;
4782 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4783 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4784 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4785 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4786 		    NULL, MUTEX_DEFAULT, NULL);
4787 	}
4788 
4789 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4790 	if (ill->ill_phyint == NULL) {
4791 		freemsg(info_mp);
4792 		mi_free(frag_ptr);
4793 		return (ENOMEM);
4794 	}
4795 
4796 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4797 	/*
4798 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4799 	 * at this point because of the following reason. If we can't
4800 	 * enter the ipsq at some point and cv_wait, the writer that
4801 	 * wakes us up tries to locate us using the list of all phyints
4802 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4803 	 * If we don't set it now, we risk a missed wakeup.
4804 	 */
4805 	ill->ill_phyint->phyint_illv4 = ill;
4806 	ill->ill_ppa = UINT_MAX;
4807 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4808 
4809 	if (!ipsq_init(ill)) {
4810 		freemsg(info_mp);
4811 		mi_free(frag_ptr);
4812 		mi_free(ill->ill_phyint);
4813 		return (ENOMEM);
4814 	}
4815 
4816 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4817 
4818 
4819 	/* Frag queue limit stuff */
4820 	ill->ill_frag_count = 0;
4821 	ill->ill_ipf_gen = 0;
4822 
4823 	ill->ill_global_timer = INFINITY;
4824 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
4825 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4826 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4827 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4828 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4829 
4830 	/*
4831 	 * Initialize IPv6 configuration variables.  The IP module is always
4832 	 * opened as an IPv4 module.  Instead tracking down the cases where
4833 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4834 	 * here for convenience, this has no effect until the ill is set to do
4835 	 * IPv6.
4836 	 */
4837 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4838 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4839 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4840 	ill->ill_max_buf = ND_MAX_Q;
4841 	ill->ill_refcnt = 0;
4842 
4843 	/* Send down the Info Request to the driver. */
4844 	info_mp->b_datap->db_type = M_PCPROTO;
4845 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4846 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4847 	dlir->dl_primitive = DL_INFO_REQ;
4848 
4849 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4850 
4851 	qprocson(q);
4852 	ill_dlpi_send(ill, info_mp);
4853 
4854 	return (0);
4855 }
4856 
4857 /*
4858  * ill_dls_info
4859  * creates datalink socket info from the device.
4860  */
4861 int
4862 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4863 {
4864 	size_t	len;
4865 	ill_t	*ill = ipif->ipif_ill;
4866 
4867 	sdl->sdl_family = AF_LINK;
4868 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4869 	sdl->sdl_type = ill->ill_type;
4870 	(void) ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4871 	len = strlen(sdl->sdl_data);
4872 	ASSERT(len < 256);
4873 	sdl->sdl_nlen = (uchar_t)len;
4874 	sdl->sdl_alen = ill->ill_phys_addr_length;
4875 	sdl->sdl_slen = 0;
4876 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4877 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4878 
4879 	return (sizeof (struct sockaddr_dl));
4880 }
4881 
4882 /*
4883  * ill_xarp_info
4884  * creates xarp info from the device.
4885  */
4886 static int
4887 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4888 {
4889 	sdl->sdl_family = AF_LINK;
4890 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4891 	sdl->sdl_type = ill->ill_type;
4892 	(void) ipif_get_name(ill->ill_ipif, sdl->sdl_data,
4893 	    sizeof (sdl->sdl_data));
4894 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4895 	sdl->sdl_alen = ill->ill_phys_addr_length;
4896 	sdl->sdl_slen = 0;
4897 	return (sdl->sdl_nlen);
4898 }
4899 
4900 static int
4901 loopback_kstat_update(kstat_t *ksp, int rw)
4902 {
4903 	kstat_named_t *kn;
4904 	netstackid_t	stackid;
4905 	netstack_t	*ns;
4906 	ip_stack_t	*ipst;
4907 
4908 	if (ksp == NULL || ksp->ks_data == NULL)
4909 		return (EIO);
4910 
4911 	if (rw == KSTAT_WRITE)
4912 		return (EACCES);
4913 
4914 	kn = KSTAT_NAMED_PTR(ksp);
4915 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4916 
4917 	ns = netstack_find_by_stackid(stackid);
4918 	if (ns == NULL)
4919 		return (-1);
4920 
4921 	ipst = ns->netstack_ip;
4922 	if (ipst == NULL) {
4923 		netstack_rele(ns);
4924 		return (-1);
4925 	}
4926 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4927 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4928 	netstack_rele(ns);
4929 	return (0);
4930 }
4931 
4932 
4933 /*
4934  * Has ifindex been plumbed already.
4935  * Compares both phyint_ifindex and phyint_group_ifindex.
4936  */
4937 static boolean_t
4938 phyint_exists(uint_t index, ip_stack_t *ipst)
4939 {
4940 	phyint_t *phyi;
4941 
4942 	ASSERT(index != 0);
4943 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4944 	/*
4945 	 * Indexes are stored in the phyint - a common structure
4946 	 * to both IPv4 and IPv6.
4947 	 */
4948 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4949 	for (; phyi != NULL;
4950 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4951 	    phyi, AVL_AFTER)) {
4952 		if (phyi->phyint_ifindex == index ||
4953 		    phyi->phyint_group_ifindex == index)
4954 			return (B_TRUE);
4955 	}
4956 	return (B_FALSE);
4957 }
4958 
4959 /* Pick a unique ifindex */
4960 boolean_t
4961 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4962 {
4963 	uint_t starting_index;
4964 
4965 	if (!ipst->ips_ill_index_wrap) {
4966 		*indexp = ipst->ips_ill_index++;
4967 		if (ipst->ips_ill_index == 0) {
4968 			/* Reached the uint_t limit Next time wrap  */
4969 			ipst->ips_ill_index_wrap = B_TRUE;
4970 		}
4971 		return (B_TRUE);
4972 	}
4973 
4974 	/*
4975 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4976 	 * at this point and don't want to call any function that attempts
4977 	 * to get the lock again.
4978 	 */
4979 	starting_index = ipst->ips_ill_index++;
4980 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4981 		if (ipst->ips_ill_index != 0 &&
4982 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4983 			/* found unused index - use it */
4984 			*indexp = ipst->ips_ill_index;
4985 			return (B_TRUE);
4986 		}
4987 	}
4988 
4989 	/*
4990 	 * all interface indicies are inuse.
4991 	 */
4992 	return (B_FALSE);
4993 }
4994 
4995 /*
4996  * Assign a unique interface index for the phyint.
4997  */
4998 static boolean_t
4999 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
5000 {
5001 	ASSERT(phyi->phyint_ifindex == 0);
5002 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
5003 }
5004 
5005 /*
5006  * Return a pointer to the ill which matches the supplied name.  Note that
5007  * the ill name length includes the null termination character.  (May be
5008  * called as writer.)
5009  * If do_alloc and the interface is "lo0" it will be automatically created.
5010  * Cannot bump up reference on condemned ills. So dup detect can't be done
5011  * using this func.
5012  */
5013 ill_t *
5014 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
5015     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
5016     ip_stack_t *ipst)
5017 {
5018 	ill_t	*ill;
5019 	ipif_t	*ipif;
5020 	kstat_named_t	*kn;
5021 	boolean_t isloopback;
5022 	ipsq_t *old_ipsq;
5023 	in6_addr_t ov6addr;
5024 
5025 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
5026 
5027 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5028 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
5029 	rw_exit(&ipst->ips_ill_g_lock);
5030 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
5031 		return (ill);
5032 
5033 	/*
5034 	 * Couldn't find it.  Does this happen to be a lookup for the
5035 	 * loopback device and are we allowed to allocate it?
5036 	 */
5037 	if (!isloopback || !do_alloc)
5038 		return (NULL);
5039 
5040 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
5041 
5042 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
5043 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
5044 		rw_exit(&ipst->ips_ill_g_lock);
5045 		return (ill);
5046 	}
5047 
5048 	/* Create the loopback device on demand */
5049 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
5050 	    sizeof (ipif_loopback_name), BPRI_MED));
5051 	if (ill == NULL)
5052 		goto done;
5053 
5054 	*ill = ill_null;
5055 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
5056 	ill->ill_ipst = ipst;
5057 	netstack_hold(ipst->ips_netstack);
5058 	/*
5059 	 * For exclusive stacks we set the zoneid to zero
5060 	 * to make IP operate as if in the global zone.
5061 	 */
5062 	ill->ill_zoneid = GLOBAL_ZONEID;
5063 
5064 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
5065 	if (ill->ill_phyint == NULL)
5066 		goto done;
5067 
5068 	if (isv6)
5069 		ill->ill_phyint->phyint_illv6 = ill;
5070 	else
5071 		ill->ill_phyint->phyint_illv4 = ill;
5072 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
5073 	ill->ill_max_frag = IP_LOOPBACK_MTU;
5074 	/* Add room for tcp+ip headers */
5075 	if (isv6) {
5076 		ill->ill_isv6 = B_TRUE;
5077 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
5078 	} else {
5079 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
5080 	}
5081 	if (!ill_allocate_mibs(ill))
5082 		goto done;
5083 	ill->ill_max_mtu = ill->ill_max_frag;
5084 	/*
5085 	 * ipif_loopback_name can't be pointed at directly because its used
5086 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
5087 	 * from the glist, ill_glist_delete() sets the first character of
5088 	 * ill_name to '\0'.
5089 	 */
5090 	ill->ill_name = (char *)ill + sizeof (*ill);
5091 	(void) strcpy(ill->ill_name, ipif_loopback_name);
5092 	ill->ill_name_length = sizeof (ipif_loopback_name);
5093 	/* Set ill_name_set for ill_phyint_reinit to work properly */
5094 
5095 	ill->ill_global_timer = INFINITY;
5096 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
5097 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
5098 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
5099 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
5100 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
5101 
5102 	/* No resolver here. */
5103 	ill->ill_net_type = IRE_LOOPBACK;
5104 
5105 	/* Initialize the ipsq */
5106 	if (!ipsq_init(ill))
5107 		goto done;
5108 
5109 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
5110 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
5111 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
5112 #ifdef ILL_DEBUG
5113 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
5114 #endif
5115 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
5116 	if (ipif == NULL)
5117 		goto done;
5118 
5119 	ill->ill_flags = ILLF_MULTICAST;
5120 
5121 	ov6addr = ipif->ipif_v6lcl_addr;
5122 	/* Set up default loopback address and mask. */
5123 	if (!isv6) {
5124 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5125 
5126 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5127 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5128 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5129 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5130 		    ipif->ipif_v6subnet);
5131 		ill->ill_flags |= ILLF_IPV4;
5132 	} else {
5133 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5134 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5135 		ipif->ipif_v6net_mask = ipv6_all_ones;
5136 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5137 		    ipif->ipif_v6subnet);
5138 		ill->ill_flags |= ILLF_IPV6;
5139 	}
5140 
5141 	/*
5142 	 * Chain us in at the end of the ill list. hold the ill
5143 	 * before we make it globally visible. 1 for the lookup.
5144 	 */
5145 	ill->ill_refcnt = 0;
5146 	ill_refhold(ill);
5147 
5148 	ill->ill_frag_count = 0;
5149 	ill->ill_frag_free_num_pkts = 0;
5150 	ill->ill_last_frag_clean_time = 0;
5151 
5152 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5153 
5154 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5155 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5156 
5157 	/* Let SCTP know so that it can add this to its list */
5158 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5159 
5160 	/*
5161 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5162 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5163 	 * requires to be after ill_glist_insert() since we need the
5164 	 * ill_index set. Pass on ipv6_loopback as the old address.
5165 	 */
5166 	sctp_update_ipif_addr(ipif, ov6addr);
5167 
5168 	/*
5169 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5170 	 */
5171 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5172 		/* Loopback ills aren't in any IPMP group */
5173 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5174 		ipsq_delete(old_ipsq);
5175 	}
5176 
5177 	/*
5178 	 * Delay this till the ipif is allocated as ipif_allocate
5179 	 * de-references ill_phyint for getting the ifindex. We
5180 	 * can't do this before ipif_allocate because ill_phyint_reinit
5181 	 * -> phyint_assign_ifindex expects ipif to be present.
5182 	 */
5183 	mutex_enter(&ill->ill_phyint->phyint_lock);
5184 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5185 	mutex_exit(&ill->ill_phyint->phyint_lock);
5186 
5187 	if (ipst->ips_loopback_ksp == NULL) {
5188 		/* Export loopback interface statistics */
5189 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5190 		    ipif_loopback_name, "net",
5191 		    KSTAT_TYPE_NAMED, 2, 0,
5192 		    ipst->ips_netstack->netstack_stackid);
5193 		if (ipst->ips_loopback_ksp != NULL) {
5194 			ipst->ips_loopback_ksp->ks_update =
5195 			    loopback_kstat_update;
5196 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5197 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5198 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5199 			ipst->ips_loopback_ksp->ks_private =
5200 			    (void *)(uintptr_t)ipst->ips_netstack->
5201 			    netstack_stackid;
5202 			kstat_install(ipst->ips_loopback_ksp);
5203 		}
5204 	}
5205 
5206 	if (error != NULL)
5207 		*error = 0;
5208 	*did_alloc = B_TRUE;
5209 	rw_exit(&ipst->ips_ill_g_lock);
5210 	return (ill);
5211 done:
5212 	if (ill != NULL) {
5213 		if (ill->ill_phyint != NULL) {
5214 			ipsq_t	*ipsq;
5215 
5216 			ipsq = ill->ill_phyint->phyint_ipsq;
5217 			if (ipsq != NULL) {
5218 				ipsq->ipsq_ipst = NULL;
5219 				kmem_free(ipsq, sizeof (ipsq_t));
5220 			}
5221 			mi_free(ill->ill_phyint);
5222 		}
5223 		ill_free_mib(ill);
5224 		if (ill->ill_ipst != NULL)
5225 			netstack_rele(ill->ill_ipst->ips_netstack);
5226 		mi_free(ill);
5227 	}
5228 	rw_exit(&ipst->ips_ill_g_lock);
5229 	if (error != NULL)
5230 		*error = ENOMEM;
5231 	return (NULL);
5232 }
5233 
5234 /*
5235  * For IPP calls - use the ip_stack_t for global stack.
5236  */
5237 ill_t *
5238 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5239     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5240 {
5241 	ip_stack_t	*ipst;
5242 	ill_t		*ill;
5243 
5244 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5245 	if (ipst == NULL) {
5246 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5247 		return (NULL);
5248 	}
5249 
5250 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5251 	netstack_rele(ipst->ips_netstack);
5252 	return (ill);
5253 }
5254 
5255 /*
5256  * Return a pointer to the ill which matches the index and IP version type.
5257  */
5258 ill_t *
5259 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5260     ipsq_func_t func, int *err, ip_stack_t *ipst)
5261 {
5262 	ill_t	*ill;
5263 	ipsq_t  *ipsq;
5264 	phyint_t *phyi;
5265 
5266 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5267 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5268 
5269 	if (err != NULL)
5270 		*err = 0;
5271 
5272 	/*
5273 	 * Indexes are stored in the phyint - a common structure
5274 	 * to both IPv4 and IPv6.
5275 	 */
5276 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5277 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5278 	    (void *) &index, NULL);
5279 	if (phyi != NULL) {
5280 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5281 		if (ill != NULL) {
5282 			/*
5283 			 * The block comment at the start of ipif_down
5284 			 * explains the use of the macros used below
5285 			 */
5286 			GRAB_CONN_LOCK(q);
5287 			mutex_enter(&ill->ill_lock);
5288 			if (ILL_CAN_LOOKUP(ill)) {
5289 				ill_refhold_locked(ill);
5290 				mutex_exit(&ill->ill_lock);
5291 				RELEASE_CONN_LOCK(q);
5292 				rw_exit(&ipst->ips_ill_g_lock);
5293 				return (ill);
5294 			} else if (ILL_CAN_WAIT(ill, q)) {
5295 				ipsq = ill->ill_phyint->phyint_ipsq;
5296 				mutex_enter(&ipsq->ipsq_lock);
5297 				rw_exit(&ipst->ips_ill_g_lock);
5298 				mutex_exit(&ill->ill_lock);
5299 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5300 				mutex_exit(&ipsq->ipsq_lock);
5301 				RELEASE_CONN_LOCK(q);
5302 				*err = EINPROGRESS;
5303 				return (NULL);
5304 			}
5305 			RELEASE_CONN_LOCK(q);
5306 			mutex_exit(&ill->ill_lock);
5307 		}
5308 	}
5309 	rw_exit(&ipst->ips_ill_g_lock);
5310 	if (err != NULL)
5311 		*err = ENXIO;
5312 	return (NULL);
5313 }
5314 
5315 /*
5316  * Return the ifindex next in sequence after the passed in ifindex.
5317  * If there is no next ifindex for the given protocol, return 0.
5318  */
5319 uint_t
5320 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5321 {
5322 	phyint_t *phyi;
5323 	phyint_t *phyi_initial;
5324 	uint_t   ifindex;
5325 
5326 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5327 
5328 	if (index == 0) {
5329 		phyi = avl_first(
5330 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5331 	} else {
5332 		phyi = phyi_initial = avl_find(
5333 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5334 		    (void *) &index, NULL);
5335 	}
5336 
5337 	for (; phyi != NULL;
5338 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5339 	    phyi, AVL_AFTER)) {
5340 		/*
5341 		 * If we're not returning the first interface in the tree
5342 		 * and we still haven't moved past the phyint_t that
5343 		 * corresponds to index, avl_walk needs to be called again
5344 		 */
5345 		if (!((index != 0) && (phyi == phyi_initial))) {
5346 			if (isv6) {
5347 				if ((phyi->phyint_illv6) &&
5348 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5349 				    (phyi->phyint_illv6->ill_isv6 == 1))
5350 					break;
5351 			} else {
5352 				if ((phyi->phyint_illv4) &&
5353 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5354 				    (phyi->phyint_illv4->ill_isv6 == 0))
5355 					break;
5356 			}
5357 		}
5358 	}
5359 
5360 	rw_exit(&ipst->ips_ill_g_lock);
5361 
5362 	if (phyi != NULL)
5363 		ifindex = phyi->phyint_ifindex;
5364 	else
5365 		ifindex = 0;
5366 
5367 	return (ifindex);
5368 }
5369 
5370 
5371 /*
5372  * Return the ifindex for the named interface.
5373  * If there is no next ifindex for the interface, return 0.
5374  */
5375 uint_t
5376 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5377 {
5378 	phyint_t	*phyi;
5379 	avl_index_t	where = 0;
5380 	uint_t		ifindex;
5381 
5382 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5383 
5384 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5385 	    name, &where)) == NULL) {
5386 		rw_exit(&ipst->ips_ill_g_lock);
5387 		return (0);
5388 	}
5389 
5390 	ifindex = phyi->phyint_ifindex;
5391 
5392 	rw_exit(&ipst->ips_ill_g_lock);
5393 
5394 	return (ifindex);
5395 }
5396 
5397 
5398 /*
5399  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5400  * that gives a running thread a reference to the ill. This reference must be
5401  * released by the thread when it is done accessing the ill and related
5402  * objects. ill_refcnt can not be used to account for static references
5403  * such as other structures pointing to an ill. Callers must generally
5404  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5405  * or be sure that the ill is not being deleted or changing state before
5406  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5407  * ill won't change any of its critical state such as address, netmask etc.
5408  */
5409 void
5410 ill_refhold(ill_t *ill)
5411 {
5412 	mutex_enter(&ill->ill_lock);
5413 	ill->ill_refcnt++;
5414 	ILL_TRACE_REF(ill);
5415 	mutex_exit(&ill->ill_lock);
5416 }
5417 
5418 void
5419 ill_refhold_locked(ill_t *ill)
5420 {
5421 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5422 	ill->ill_refcnt++;
5423 	ILL_TRACE_REF(ill);
5424 }
5425 
5426 int
5427 ill_check_and_refhold(ill_t *ill)
5428 {
5429 	mutex_enter(&ill->ill_lock);
5430 	if (ILL_CAN_LOOKUP(ill)) {
5431 		ill_refhold_locked(ill);
5432 		mutex_exit(&ill->ill_lock);
5433 		return (0);
5434 	}
5435 	mutex_exit(&ill->ill_lock);
5436 	return (ILL_LOOKUP_FAILED);
5437 }
5438 
5439 /*
5440  * Must not be called while holding any locks. Otherwise if this is
5441  * the last reference to be released, there is a chance of recursive mutex
5442  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5443  * to restart an ioctl.
5444  */
5445 void
5446 ill_refrele(ill_t *ill)
5447 {
5448 	mutex_enter(&ill->ill_lock);
5449 	ASSERT(ill->ill_refcnt != 0);
5450 	ill->ill_refcnt--;
5451 	ILL_UNTRACE_REF(ill);
5452 	if (ill->ill_refcnt != 0) {
5453 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5454 		mutex_exit(&ill->ill_lock);
5455 		return;
5456 	}
5457 
5458 	/* Drops the ill_lock */
5459 	ipif_ill_refrele_tail(ill);
5460 }
5461 
5462 /*
5463  * Obtain a weak reference count on the ill. This reference ensures the
5464  * ill won't be freed, but the ill may change any of its critical state
5465  * such as netmask, address etc. Returns an error if the ill has started
5466  * closing.
5467  */
5468 boolean_t
5469 ill_waiter_inc(ill_t *ill)
5470 {
5471 	mutex_enter(&ill->ill_lock);
5472 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5473 		mutex_exit(&ill->ill_lock);
5474 		return (B_FALSE);
5475 	}
5476 	ill->ill_waiters++;
5477 	mutex_exit(&ill->ill_lock);
5478 	return (B_TRUE);
5479 }
5480 
5481 void
5482 ill_waiter_dcr(ill_t *ill)
5483 {
5484 	mutex_enter(&ill->ill_lock);
5485 	ill->ill_waiters--;
5486 	if (ill->ill_waiters == 0)
5487 		cv_broadcast(&ill->ill_cv);
5488 	mutex_exit(&ill->ill_lock);
5489 }
5490 
5491 /*
5492  * Named Dispatch routine to produce a formatted report on all ILLs.
5493  * This report is accessed by using the ndd utility to "get" ND variable
5494  * "ip_ill_status".
5495  */
5496 /* ARGSUSED */
5497 int
5498 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5499 {
5500 	ill_t		*ill;
5501 	ill_walk_context_t ctx;
5502 	ip_stack_t	*ipst;
5503 
5504 	ipst = CONNQ_TO_IPST(q);
5505 
5506 	(void) mi_mpprintf(mp,
5507 	    "ILL      " MI_COL_HDRPAD_STR
5508 	/*   01234567[89ABCDEF] */
5509 	    "rq       " MI_COL_HDRPAD_STR
5510 	/*   01234567[89ABCDEF] */
5511 	    "wq       " MI_COL_HDRPAD_STR
5512 	/*   01234567[89ABCDEF] */
5513 	    "upcnt mxfrg err name");
5514 	/*   12345 12345 123 xxxxxxxx  */
5515 
5516 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5517 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5518 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5519 		(void) mi_mpprintf(mp,
5520 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5521 		    "%05u %05u %03d %s",
5522 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5523 		    ill->ill_ipif_up_count,
5524 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5525 	}
5526 	rw_exit(&ipst->ips_ill_g_lock);
5527 
5528 	return (0);
5529 }
5530 
5531 /*
5532  * Named Dispatch routine to produce a formatted report on all IPIFs.
5533  * This report is accessed by using the ndd utility to "get" ND variable
5534  * "ip_ipif_status".
5535  */
5536 /* ARGSUSED */
5537 int
5538 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5539 {
5540 	char	buf1[INET6_ADDRSTRLEN];
5541 	char	buf2[INET6_ADDRSTRLEN];
5542 	char	buf3[INET6_ADDRSTRLEN];
5543 	char	buf4[INET6_ADDRSTRLEN];
5544 	char	buf5[INET6_ADDRSTRLEN];
5545 	char	buf6[INET6_ADDRSTRLEN];
5546 	char	buf[LIFNAMSIZ];
5547 	ill_t	*ill;
5548 	ipif_t	*ipif;
5549 	nv_t	*nvp;
5550 	uint64_t flags;
5551 	zoneid_t zoneid;
5552 	ill_walk_context_t ctx;
5553 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5554 
5555 	(void) mi_mpprintf(mp,
5556 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5557 	    "\tlocal address\n"
5558 	    "\tsrc address\n"
5559 	    "\tsubnet\n"
5560 	    "\tmask\n"
5561 	    "\tbroadcast\n"
5562 	    "\tp-p-dst");
5563 
5564 	ASSERT(q->q_next == NULL);
5565 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5566 
5567 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5568 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5569 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5570 		for (ipif = ill->ill_ipif; ipif != NULL;
5571 		    ipif = ipif->ipif_next) {
5572 			if (zoneid != GLOBAL_ZONEID &&
5573 			    zoneid != ipif->ipif_zoneid &&
5574 			    ipif->ipif_zoneid != ALL_ZONES)
5575 				continue;
5576 			(void) mi_mpprintf(mp,
5577 			    MI_COL_PTRFMT_STR
5578 			    "%04u %05u %u/%u/%u %s %d",
5579 			    (void *)ipif,
5580 			    ipif->ipif_metric, ipif->ipif_mtu,
5581 			    ipif->ipif_ib_pkt_count,
5582 			    ipif->ipif_ob_pkt_count,
5583 			    ipif->ipif_fo_pkt_count,
5584 			    ipif_get_name(ipif, buf, sizeof (buf)),
5585 			    ipif->ipif_zoneid);
5586 
5587 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5588 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5589 
5590 		/* Tack on text strings for any flags. */
5591 		nvp = ipif_nv_tbl;
5592 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5593 			if (nvp->nv_value & flags)
5594 				(void) mi_mpprintf_nr(mp, " %s",
5595 				    nvp->nv_name);
5596 		}
5597 		(void) mi_mpprintf(mp,
5598 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5599 		    inet_ntop(AF_INET6,
5600 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5601 		    inet_ntop(AF_INET6,
5602 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5603 		    inet_ntop(AF_INET6,
5604 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5605 		    inet_ntop(AF_INET6,
5606 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5607 		    inet_ntop(AF_INET6,
5608 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5609 		    inet_ntop(AF_INET6,
5610 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5611 		}
5612 	}
5613 	rw_exit(&ipst->ips_ill_g_lock);
5614 	return (0);
5615 }
5616 
5617 /*
5618  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5619  * driver.  We construct best guess defaults for lower level information that
5620  * we need.  If an interface is brought up without injection of any overriding
5621  * information from outside, we have to be ready to go with these defaults.
5622  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5623  * we primarely want the dl_provider_style.
5624  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5625  * at which point we assume the other part of the information is valid.
5626  */
5627 void
5628 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5629 {
5630 	uchar_t		*brdcst_addr;
5631 	uint_t		brdcst_addr_length, phys_addr_length;
5632 	t_scalar_t	sap_length;
5633 	dl_info_ack_t	*dlia;
5634 	ip_m_t		*ipm;
5635 	dl_qos_cl_sel1_t *sel1;
5636 
5637 	ASSERT(IAM_WRITER_ILL(ill));
5638 
5639 	/*
5640 	 * Till the ill is fully up ILL_CHANGING will be set and
5641 	 * the ill is not globally visible. So no need for a lock.
5642 	 */
5643 	dlia = (dl_info_ack_t *)mp->b_rptr;
5644 	ill->ill_mactype = dlia->dl_mac_type;
5645 
5646 	ipm = ip_m_lookup(dlia->dl_mac_type);
5647 	if (ipm == NULL) {
5648 		ipm = ip_m_lookup(DL_OTHER);
5649 		ASSERT(ipm != NULL);
5650 	}
5651 	ill->ill_media = ipm;
5652 
5653 	/*
5654 	 * When the new DLPI stuff is ready we'll pull lengths
5655 	 * from dlia.
5656 	 */
5657 	if (dlia->dl_version == DL_VERSION_2) {
5658 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5659 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5660 		    brdcst_addr_length);
5661 		if (brdcst_addr == NULL) {
5662 			brdcst_addr_length = 0;
5663 		}
5664 		sap_length = dlia->dl_sap_length;
5665 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5666 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5667 		    brdcst_addr_length, sap_length, phys_addr_length));
5668 	} else {
5669 		brdcst_addr_length = 6;
5670 		brdcst_addr = ip_six_byte_all_ones;
5671 		sap_length = -2;
5672 		phys_addr_length = brdcst_addr_length;
5673 	}
5674 
5675 	ill->ill_bcast_addr_length = brdcst_addr_length;
5676 	ill->ill_phys_addr_length = phys_addr_length;
5677 	ill->ill_sap_length = sap_length;
5678 	ill->ill_max_frag = dlia->dl_max_sdu;
5679 	ill->ill_max_mtu = ill->ill_max_frag;
5680 
5681 	ill->ill_type = ipm->ip_m_type;
5682 
5683 	if (!ill->ill_dlpi_style_set) {
5684 		if (dlia->dl_provider_style == DL_STYLE2)
5685 			ill->ill_needs_attach = 1;
5686 
5687 		/*
5688 		 * Allocate the first ipif on this ill. We don't delay it
5689 		 * further as ioctl handling assumes atleast one ipif to
5690 		 * be present.
5691 		 *
5692 		 * At this point we don't know whether the ill is v4 or v6.
5693 		 * We will know this whan the SIOCSLIFNAME happens and
5694 		 * the correct value for ill_isv6 will be assigned in
5695 		 * ipif_set_values(). We need to hold the ill lock and
5696 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5697 		 * the wakeup.
5698 		 */
5699 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5700 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5701 		mutex_enter(&ill->ill_lock);
5702 		ASSERT(ill->ill_dlpi_style_set == 0);
5703 		ill->ill_dlpi_style_set = 1;
5704 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5705 		cv_broadcast(&ill->ill_cv);
5706 		mutex_exit(&ill->ill_lock);
5707 		freemsg(mp);
5708 		return;
5709 	}
5710 	ASSERT(ill->ill_ipif != NULL);
5711 	/*
5712 	 * We know whether it is IPv4 or IPv6 now, as this is the
5713 	 * second DL_INFO_ACK we are recieving in response to the
5714 	 * DL_INFO_REQ sent in ipif_set_values.
5715 	 */
5716 	if (ill->ill_isv6)
5717 		ill->ill_sap = IP6_DL_SAP;
5718 	else
5719 		ill->ill_sap = IP_DL_SAP;
5720 	/*
5721 	 * Set ipif_mtu which is used to set the IRE's
5722 	 * ire_max_frag value. The driver could have sent
5723 	 * a different mtu from what it sent last time. No
5724 	 * need to call ipif_mtu_change because IREs have
5725 	 * not yet been created.
5726 	 */
5727 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5728 	/*
5729 	 * Clear all the flags that were set based on ill_bcast_addr_length
5730 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5731 	 * changed now and we need to re-evaluate.
5732 	 */
5733 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5734 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5735 
5736 	/*
5737 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5738 	 * changed now.
5739 	 */
5740 	if (ill->ill_bcast_addr_length == 0) {
5741 		if (ill->ill_resolver_mp != NULL)
5742 			freemsg(ill->ill_resolver_mp);
5743 		if (ill->ill_bcast_mp != NULL)
5744 			freemsg(ill->ill_bcast_mp);
5745 		if (ill->ill_flags & ILLF_XRESOLV)
5746 			ill->ill_net_type = IRE_IF_RESOLVER;
5747 		else
5748 			ill->ill_net_type = IRE_IF_NORESOLVER;
5749 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5750 		    ill->ill_phys_addr_length,
5751 		    ill->ill_sap,
5752 		    ill->ill_sap_length);
5753 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5754 
5755 		if (ill->ill_isv6)
5756 			/*
5757 			 * Note: xresolv interfaces will eventually need NOARP
5758 			 * set here as well, but that will require those
5759 			 * external resolvers to have some knowledge of
5760 			 * that flag and act appropriately. Not to be changed
5761 			 * at present.
5762 			 */
5763 			ill->ill_flags |= ILLF_NONUD;
5764 		else
5765 			ill->ill_flags |= ILLF_NOARP;
5766 
5767 		if (ill->ill_phys_addr_length == 0) {
5768 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5769 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5770 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5771 			} else {
5772 				/* pt-pt supports multicast. */
5773 				ill->ill_flags |= ILLF_MULTICAST;
5774 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5775 			}
5776 		}
5777 	} else {
5778 		ill->ill_net_type = IRE_IF_RESOLVER;
5779 		if (ill->ill_bcast_mp != NULL)
5780 			freemsg(ill->ill_bcast_mp);
5781 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5782 		    ill->ill_bcast_addr_length, ill->ill_sap,
5783 		    ill->ill_sap_length);
5784 		/*
5785 		 * Later detect lack of DLPI driver multicast
5786 		 * capability by catching DL_ENABMULTI errors in
5787 		 * ip_rput_dlpi.
5788 		 */
5789 		ill->ill_flags |= ILLF_MULTICAST;
5790 		if (!ill->ill_isv6)
5791 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5792 	}
5793 	/* By default an interface does not support any CoS marking */
5794 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5795 
5796 	/*
5797 	 * If we get QoS information in DL_INFO_ACK, the device supports
5798 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5799 	 */
5800 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5801 	    dlia->dl_qos_length);
5802 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5803 		ill->ill_flags |= ILLF_COS_ENABLED;
5804 	}
5805 
5806 	/* Clear any previous error indication. */
5807 	ill->ill_error = 0;
5808 	freemsg(mp);
5809 }
5810 
5811 /*
5812  * Perform various checks to verify that an address would make sense as a
5813  * local, remote, or subnet interface address.
5814  */
5815 static boolean_t
5816 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5817 {
5818 	ipaddr_t	net_mask;
5819 
5820 	/*
5821 	 * Don't allow all zeroes, all ones or experimental address, but allow
5822 	 * all ones netmask.
5823 	 */
5824 	if ((net_mask = ip_net_mask(addr)) == 0)
5825 		return (B_FALSE);
5826 	/* A given netmask overrides the "guess" netmask */
5827 	if (subnet_mask != 0)
5828 		net_mask = subnet_mask;
5829 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5830 	    (addr == (addr | ~net_mask)))) {
5831 		return (B_FALSE);
5832 	}
5833 	if (CLASSD(addr))
5834 		return (B_FALSE);
5835 
5836 	return (B_TRUE);
5837 }
5838 
5839 #define	V6_IPIF_LINKLOCAL(p)	\
5840 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5841 
5842 /*
5843  * Compare two given ipifs and check if the second one is better than
5844  * the first one using the order of preference (not taking deprecated
5845  * into acount) specified in ipif_lookup_multicast().
5846  */
5847 static boolean_t
5848 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5849 {
5850 	/* Check the least preferred first. */
5851 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5852 		/* If both ipifs are the same, use the first one. */
5853 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5854 			return (B_FALSE);
5855 		else
5856 			return (B_TRUE);
5857 	}
5858 
5859 	/* For IPv6, check for link local address. */
5860 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5861 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5862 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5863 			/* The second one is equal or less preferred. */
5864 			return (B_FALSE);
5865 		} else {
5866 			return (B_TRUE);
5867 		}
5868 	}
5869 
5870 	/* Then check for point to point interface. */
5871 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5872 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5873 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5874 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5875 			return (B_FALSE);
5876 		} else {
5877 			return (B_TRUE);
5878 		}
5879 	}
5880 
5881 	/* old_ipif is a normal interface, so no need to use the new one. */
5882 	return (B_FALSE);
5883 }
5884 
5885 /*
5886  * Find any non-virtual, not condemned, and up multicast capable interface
5887  * given an IP instance and zoneid.  Order of preference is:
5888  *
5889  * 1. normal
5890  * 1.1 normal, but deprecated
5891  * 2. point to point
5892  * 2.1 point to point, but deprecated
5893  * 3. link local
5894  * 3.1 link local, but deprecated
5895  * 4. loopback.
5896  */
5897 ipif_t *
5898 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5899 {
5900 	ill_t			*ill;
5901 	ill_walk_context_t	ctx;
5902 	ipif_t			*ipif;
5903 	ipif_t			*saved_ipif = NULL;
5904 	ipif_t			*dep_ipif = NULL;
5905 
5906 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5907 	if (isv6)
5908 		ill = ILL_START_WALK_V6(&ctx, ipst);
5909 	else
5910 		ill = ILL_START_WALK_V4(&ctx, ipst);
5911 
5912 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5913 		mutex_enter(&ill->ill_lock);
5914 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5915 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5916 			mutex_exit(&ill->ill_lock);
5917 			continue;
5918 		}
5919 		for (ipif = ill->ill_ipif; ipif != NULL;
5920 		    ipif = ipif->ipif_next) {
5921 			if (zoneid != ipif->ipif_zoneid &&
5922 			    zoneid != ALL_ZONES &&
5923 			    ipif->ipif_zoneid != ALL_ZONES) {
5924 				continue;
5925 			}
5926 			if (!(ipif->ipif_flags & IPIF_UP) ||
5927 			    !IPIF_CAN_LOOKUP(ipif)) {
5928 				continue;
5929 			}
5930 
5931 			/*
5932 			 * Found one candidate.  If it is deprecated,
5933 			 * remember it in dep_ipif.  If it is not deprecated,
5934 			 * remember it in saved_ipif.
5935 			 */
5936 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5937 				if (dep_ipif == NULL) {
5938 					dep_ipif = ipif;
5939 				} else if (ipif_comp_multi(dep_ipif, ipif,
5940 				    isv6)) {
5941 					/*
5942 					 * If the previous dep_ipif does not
5943 					 * belong to the same ill, we've done
5944 					 * a ipif_refhold() on it.  So we need
5945 					 * to release it.
5946 					 */
5947 					if (dep_ipif->ipif_ill != ill)
5948 						ipif_refrele(dep_ipif);
5949 					dep_ipif = ipif;
5950 				}
5951 				continue;
5952 			}
5953 			if (saved_ipif == NULL) {
5954 				saved_ipif = ipif;
5955 			} else {
5956 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5957 					if (saved_ipif->ipif_ill != ill)
5958 						ipif_refrele(saved_ipif);
5959 					saved_ipif = ipif;
5960 				}
5961 			}
5962 		}
5963 		/*
5964 		 * Before going to the next ill, do a ipif_refhold() on the
5965 		 * saved ones.
5966 		 */
5967 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5968 			ipif_refhold_locked(saved_ipif);
5969 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5970 			ipif_refhold_locked(dep_ipif);
5971 		mutex_exit(&ill->ill_lock);
5972 	}
5973 	rw_exit(&ipst->ips_ill_g_lock);
5974 
5975 	/*
5976 	 * If we have only the saved_ipif, return it.  But if we have both
5977 	 * saved_ipif and dep_ipif, check to see which one is better.
5978 	 */
5979 	if (saved_ipif != NULL) {
5980 		if (dep_ipif != NULL) {
5981 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5982 				ipif_refrele(saved_ipif);
5983 				return (dep_ipif);
5984 			} else {
5985 				ipif_refrele(dep_ipif);
5986 				return (saved_ipif);
5987 			}
5988 		}
5989 		return (saved_ipif);
5990 	} else {
5991 		return (dep_ipif);
5992 	}
5993 }
5994 
5995 /*
5996  * This function is called when an application does not specify an interface
5997  * to be used for multicast traffic (joining a group/sending data).  It
5998  * calls ire_lookup_multi() to look for an interface route for the
5999  * specified multicast group.  Doing this allows the administrator to add
6000  * prefix routes for multicast to indicate which interface to be used for
6001  * multicast traffic in the above scenario.  The route could be for all
6002  * multicast (224.0/4), for a single multicast group (a /32 route) or
6003  * anything in between.  If there is no such multicast route, we just find
6004  * any multicast capable interface and return it.  The returned ipif
6005  * is refhold'ed.
6006  */
6007 ipif_t *
6008 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
6009 {
6010 	ire_t			*ire;
6011 	ipif_t			*ipif;
6012 
6013 	ire = ire_lookup_multi(group, zoneid, ipst);
6014 	if (ire != NULL) {
6015 		ipif = ire->ire_ipif;
6016 		ipif_refhold(ipif);
6017 		ire_refrele(ire);
6018 		return (ipif);
6019 	}
6020 
6021 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
6022 }
6023 
6024 /*
6025  * Look for an ipif with the specified interface address and destination.
6026  * The destination address is used only for matching point-to-point interfaces.
6027  */
6028 ipif_t *
6029 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
6030     ipsq_func_t func, int *error, ip_stack_t *ipst)
6031 {
6032 	ipif_t	*ipif;
6033 	ill_t	*ill;
6034 	ill_walk_context_t ctx;
6035 	ipsq_t	*ipsq;
6036 
6037 	if (error != NULL)
6038 		*error = 0;
6039 
6040 	/*
6041 	 * First match all the point-to-point interfaces
6042 	 * before looking at non-point-to-point interfaces.
6043 	 * This is done to avoid returning non-point-to-point
6044 	 * ipif instead of unnumbered point-to-point ipif.
6045 	 */
6046 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6047 	ill = ILL_START_WALK_V4(&ctx, ipst);
6048 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6049 		GRAB_CONN_LOCK(q);
6050 		mutex_enter(&ill->ill_lock);
6051 		for (ipif = ill->ill_ipif; ipif != NULL;
6052 		    ipif = ipif->ipif_next) {
6053 			/* Allow the ipif to be down */
6054 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
6055 			    (ipif->ipif_lcl_addr == if_addr) &&
6056 			    (ipif->ipif_pp_dst_addr == dst)) {
6057 				/*
6058 				 * The block comment at the start of ipif_down
6059 				 * explains the use of the macros used below
6060 				 */
6061 				if (IPIF_CAN_LOOKUP(ipif)) {
6062 					ipif_refhold_locked(ipif);
6063 					mutex_exit(&ill->ill_lock);
6064 					RELEASE_CONN_LOCK(q);
6065 					rw_exit(&ipst->ips_ill_g_lock);
6066 					return (ipif);
6067 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6068 					ipsq = ill->ill_phyint->phyint_ipsq;
6069 					mutex_enter(&ipsq->ipsq_lock);
6070 					mutex_exit(&ill->ill_lock);
6071 					rw_exit(&ipst->ips_ill_g_lock);
6072 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6073 					    ill);
6074 					mutex_exit(&ipsq->ipsq_lock);
6075 					RELEASE_CONN_LOCK(q);
6076 					*error = EINPROGRESS;
6077 					return (NULL);
6078 				}
6079 			}
6080 		}
6081 		mutex_exit(&ill->ill_lock);
6082 		RELEASE_CONN_LOCK(q);
6083 	}
6084 	rw_exit(&ipst->ips_ill_g_lock);
6085 
6086 	/* lookup the ipif based on interface address */
6087 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
6088 	    ipst);
6089 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
6090 	return (ipif);
6091 }
6092 
6093 /*
6094  * Look for an ipif with the specified address. For point-point links
6095  * we look for matches on either the destination address and the local
6096  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6097  * is set.
6098  * Matches on a specific ill if match_ill is set.
6099  */
6100 ipif_t *
6101 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
6102     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
6103 {
6104 	ipif_t  *ipif;
6105 	ill_t   *ill;
6106 	boolean_t ptp = B_FALSE;
6107 	ipsq_t	*ipsq;
6108 	ill_walk_context_t	ctx;
6109 
6110 	if (error != NULL)
6111 		*error = 0;
6112 
6113 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6114 	/*
6115 	 * Repeat twice, first based on local addresses and
6116 	 * next time for pointopoint.
6117 	 */
6118 repeat:
6119 	ill = ILL_START_WALK_V4(&ctx, ipst);
6120 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6121 		if (match_ill != NULL && ill != match_ill) {
6122 			continue;
6123 		}
6124 		GRAB_CONN_LOCK(q);
6125 		mutex_enter(&ill->ill_lock);
6126 		for (ipif = ill->ill_ipif; ipif != NULL;
6127 		    ipif = ipif->ipif_next) {
6128 			if (zoneid != ALL_ZONES &&
6129 			    zoneid != ipif->ipif_zoneid &&
6130 			    ipif->ipif_zoneid != ALL_ZONES)
6131 				continue;
6132 			/* Allow the ipif to be down */
6133 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6134 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6135 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6136 			    (ipif->ipif_pp_dst_addr == addr))) {
6137 				/*
6138 				 * The block comment at the start of ipif_down
6139 				 * explains the use of the macros used below
6140 				 */
6141 				if (IPIF_CAN_LOOKUP(ipif)) {
6142 					ipif_refhold_locked(ipif);
6143 					mutex_exit(&ill->ill_lock);
6144 					RELEASE_CONN_LOCK(q);
6145 					rw_exit(&ipst->ips_ill_g_lock);
6146 					return (ipif);
6147 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6148 					ipsq = ill->ill_phyint->phyint_ipsq;
6149 					mutex_enter(&ipsq->ipsq_lock);
6150 					mutex_exit(&ill->ill_lock);
6151 					rw_exit(&ipst->ips_ill_g_lock);
6152 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6153 					    ill);
6154 					mutex_exit(&ipsq->ipsq_lock);
6155 					RELEASE_CONN_LOCK(q);
6156 					*error = EINPROGRESS;
6157 					return (NULL);
6158 				}
6159 			}
6160 		}
6161 		mutex_exit(&ill->ill_lock);
6162 		RELEASE_CONN_LOCK(q);
6163 	}
6164 
6165 	/* If we already did the ptp case, then we are done */
6166 	if (ptp) {
6167 		rw_exit(&ipst->ips_ill_g_lock);
6168 		if (error != NULL)
6169 			*error = ENXIO;
6170 		return (NULL);
6171 	}
6172 	ptp = B_TRUE;
6173 	goto repeat;
6174 }
6175 
6176 /*
6177  * Look for an ipif with the specified address. For point-point links
6178  * we look for matches on either the destination address and the local
6179  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6180  * is set.
6181  * Matches on a specific ill if match_ill is set.
6182  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6183  */
6184 zoneid_t
6185 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6186 {
6187 	zoneid_t zoneid;
6188 	ipif_t  *ipif;
6189 	ill_t   *ill;
6190 	boolean_t ptp = B_FALSE;
6191 	ill_walk_context_t	ctx;
6192 
6193 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6194 	/*
6195 	 * Repeat twice, first based on local addresses and
6196 	 * next time for pointopoint.
6197 	 */
6198 repeat:
6199 	ill = ILL_START_WALK_V4(&ctx, ipst);
6200 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6201 		if (match_ill != NULL && ill != match_ill) {
6202 			continue;
6203 		}
6204 		mutex_enter(&ill->ill_lock);
6205 		for (ipif = ill->ill_ipif; ipif != NULL;
6206 		    ipif = ipif->ipif_next) {
6207 			/* Allow the ipif to be down */
6208 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6209 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6210 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6211 			    (ipif->ipif_pp_dst_addr == addr)) &&
6212 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6213 				zoneid = ipif->ipif_zoneid;
6214 				mutex_exit(&ill->ill_lock);
6215 				rw_exit(&ipst->ips_ill_g_lock);
6216 				/*
6217 				 * If ipif_zoneid was ALL_ZONES then we have
6218 				 * a trusted extensions shared IP address.
6219 				 * In that case GLOBAL_ZONEID works to send.
6220 				 */
6221 				if (zoneid == ALL_ZONES)
6222 					zoneid = GLOBAL_ZONEID;
6223 				return (zoneid);
6224 			}
6225 		}
6226 		mutex_exit(&ill->ill_lock);
6227 	}
6228 
6229 	/* If we already did the ptp case, then we are done */
6230 	if (ptp) {
6231 		rw_exit(&ipst->ips_ill_g_lock);
6232 		return (ALL_ZONES);
6233 	}
6234 	ptp = B_TRUE;
6235 	goto repeat;
6236 }
6237 
6238 /*
6239  * Look for an ipif that matches the specified remote address i.e. the
6240  * ipif that would receive the specified packet.
6241  * First look for directly connected interfaces and then do a recursive
6242  * IRE lookup and pick the first ipif corresponding to the source address in the
6243  * ire.
6244  * Returns: held ipif
6245  */
6246 ipif_t *
6247 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6248 {
6249 	ipif_t	*ipif;
6250 	ire_t	*ire;
6251 	ip_stack_t	*ipst = ill->ill_ipst;
6252 
6253 	ASSERT(!ill->ill_isv6);
6254 
6255 	/*
6256 	 * Someone could be changing this ipif currently or change it
6257 	 * after we return this. Thus  a few packets could use the old
6258 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6259 	 * will atomically be updated or cleaned up with the new value
6260 	 * Thus we don't need a lock to check the flags or other attrs below.
6261 	 */
6262 	mutex_enter(&ill->ill_lock);
6263 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6264 		if (!IPIF_CAN_LOOKUP(ipif))
6265 			continue;
6266 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6267 		    ipif->ipif_zoneid != ALL_ZONES)
6268 			continue;
6269 		/* Allow the ipif to be down */
6270 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6271 			if ((ipif->ipif_pp_dst_addr == addr) ||
6272 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6273 			    ipif->ipif_lcl_addr == addr)) {
6274 				ipif_refhold_locked(ipif);
6275 				mutex_exit(&ill->ill_lock);
6276 				return (ipif);
6277 			}
6278 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6279 			ipif_refhold_locked(ipif);
6280 			mutex_exit(&ill->ill_lock);
6281 			return (ipif);
6282 		}
6283 	}
6284 	mutex_exit(&ill->ill_lock);
6285 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6286 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6287 	if (ire != NULL) {
6288 		/*
6289 		 * The callers of this function wants to know the
6290 		 * interface on which they have to send the replies
6291 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6292 		 * derived from different ills, we really don't care
6293 		 * what we return here.
6294 		 */
6295 		ipif = ire->ire_ipif;
6296 		if (ipif != NULL) {
6297 			ipif_refhold(ipif);
6298 			ire_refrele(ire);
6299 			return (ipif);
6300 		}
6301 		ire_refrele(ire);
6302 	}
6303 	/* Pick the first interface */
6304 	ipif = ipif_get_next_ipif(NULL, ill);
6305 	return (ipif);
6306 }
6307 
6308 /*
6309  * This func does not prevent refcnt from increasing. But if
6310  * the caller has taken steps to that effect, then this func
6311  * can be used to determine whether the ill has become quiescent
6312  */
6313 boolean_t
6314 ill_is_quiescent(ill_t *ill)
6315 {
6316 	ipif_t	*ipif;
6317 
6318 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6319 
6320 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6321 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6322 			return (B_FALSE);
6323 		}
6324 	}
6325 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
6326 	    ill->ill_nce_cnt != 0 || ill->ill_srcif_refcnt != 0 ||
6327 	    ill->ill_mrtun_refcnt != 0) {
6328 		return (B_FALSE);
6329 	}
6330 	return (B_TRUE);
6331 }
6332 
6333 /*
6334  * This func does not prevent refcnt from increasing. But if
6335  * the caller has taken steps to that effect, then this func
6336  * can be used to determine whether the ipif has become quiescent
6337  */
6338 static boolean_t
6339 ipif_is_quiescent(ipif_t *ipif)
6340 {
6341 	ill_t *ill;
6342 
6343 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6344 
6345 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6346 		return (B_FALSE);
6347 	}
6348 
6349 	ill = ipif->ipif_ill;
6350 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6351 	    ill->ill_logical_down) {
6352 		return (B_TRUE);
6353 	}
6354 
6355 	/* This is the last ipif going down or being deleted on this ill */
6356 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
6357 		return (B_FALSE);
6358 	}
6359 
6360 	return (B_TRUE);
6361 }
6362 
6363 /*
6364  * This func does not prevent refcnt from increasing. But if
6365  * the caller has taken steps to that effect, then this func
6366  * can be used to determine whether the ipifs marked with IPIF_MOVING
6367  * have become quiescent and can be moved in a failover/failback.
6368  */
6369 static ipif_t *
6370 ill_quiescent_to_move(ill_t *ill)
6371 {
6372 	ipif_t  *ipif;
6373 
6374 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6375 
6376 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6377 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6378 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6379 				return (ipif);
6380 			}
6381 		}
6382 	}
6383 	return (NULL);
6384 }
6385 
6386 /*
6387  * The ipif/ill/ire has been refreled. Do the tail processing.
6388  * Determine if the ipif or ill in question has become quiescent and if so
6389  * wakeup close and/or restart any queued pending ioctl that is waiting
6390  * for the ipif_down (or ill_down)
6391  */
6392 void
6393 ipif_ill_refrele_tail(ill_t *ill)
6394 {
6395 	mblk_t	*mp;
6396 	conn_t	*connp;
6397 	ipsq_t	*ipsq;
6398 	ipif_t	*ipif;
6399 	dl_notify_ind_t *dlindp;
6400 
6401 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6402 
6403 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6404 	    ill_is_quiescent(ill)) {
6405 		/* ill_close may be waiting */
6406 		cv_broadcast(&ill->ill_cv);
6407 	}
6408 
6409 	/* ipsq can't change because ill_lock  is held */
6410 	ipsq = ill->ill_phyint->phyint_ipsq;
6411 	if (ipsq->ipsq_waitfor == 0) {
6412 		/* Not waiting for anything, just return. */
6413 		mutex_exit(&ill->ill_lock);
6414 		return;
6415 	}
6416 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6417 	    ipsq->ipsq_pending_ipif != NULL);
6418 	/*
6419 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6420 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6421 	 * be zero for restarting an ioctl that ends up downing the ill.
6422 	 */
6423 	ipif = ipsq->ipsq_pending_ipif;
6424 	if (ipif->ipif_ill != ill) {
6425 		/* The ioctl is pending on some other ill. */
6426 		mutex_exit(&ill->ill_lock);
6427 		return;
6428 	}
6429 
6430 	switch (ipsq->ipsq_waitfor) {
6431 	case IPIF_DOWN:
6432 	case IPIF_FREE:
6433 		if (!ipif_is_quiescent(ipif)) {
6434 			mutex_exit(&ill->ill_lock);
6435 			return;
6436 		}
6437 		break;
6438 
6439 	case ILL_DOWN:
6440 	case ILL_FREE:
6441 		/*
6442 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6443 		 * waits synchronously in ip_close, and no message is queued in
6444 		 * ipsq_pending_mp at all in this case
6445 		 */
6446 		if (!ill_is_quiescent(ill)) {
6447 			mutex_exit(&ill->ill_lock);
6448 			return;
6449 		}
6450 
6451 		break;
6452 
6453 	case ILL_MOVE_OK:
6454 		if (ill_quiescent_to_move(ill) != NULL) {
6455 			mutex_exit(&ill->ill_lock);
6456 			return;
6457 		}
6458 
6459 		break;
6460 	default:
6461 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6462 		    (void *)ipsq, ipsq->ipsq_waitfor);
6463 	}
6464 
6465 	/*
6466 	 * Incr refcnt for the qwriter_ip call below which
6467 	 * does a refrele
6468 	 */
6469 	ill_refhold_locked(ill);
6470 	mutex_exit(&ill->ill_lock);
6471 
6472 	mp = ipsq_pending_mp_get(ipsq, &connp);
6473 	ASSERT(mp != NULL);
6474 
6475 	/*
6476 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6477 	 * we can only get here when the current operation decides it
6478 	 * it needs to quiesce via ipsq_pending_mp_add().
6479 	 */
6480 	switch (mp->b_datap->db_type) {
6481 	case M_PCPROTO:
6482 	case M_PROTO:
6483 		/*
6484 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6485 		 */
6486 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6487 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6488 
6489 		switch (dlindp->dl_notification) {
6490 		case DL_NOTE_PHYS_ADDR:
6491 			qwriter_ip(ill, ill->ill_rq, mp,
6492 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6493 			return;
6494 		default:
6495 			ASSERT(0);
6496 		}
6497 		break;
6498 
6499 	case M_ERROR:
6500 	case M_HANGUP:
6501 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6502 		    B_TRUE);
6503 		return;
6504 
6505 	case M_IOCTL:
6506 	case M_IOCDATA:
6507 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6508 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6509 		return;
6510 
6511 	default:
6512 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6513 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6514 	}
6515 }
6516 
6517 #ifdef ILL_DEBUG
6518 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6519 void
6520 th_trace_rrecord(th_trace_t *th_trace)
6521 {
6522 	tr_buf_t *tr_buf;
6523 	uint_t lastref;
6524 
6525 	lastref = th_trace->th_trace_lastref;
6526 	lastref++;
6527 	if (lastref == TR_BUF_MAX)
6528 		lastref = 0;
6529 	th_trace->th_trace_lastref = lastref;
6530 	tr_buf = &th_trace->th_trbuf[lastref];
6531 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, IP_STACK_DEPTH);
6532 }
6533 
6534 th_trace_t *
6535 th_trace_ipif_lookup(ipif_t *ipif)
6536 {
6537 	int bucket_id;
6538 	th_trace_t *th_trace;
6539 
6540 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6541 
6542 	bucket_id = IP_TR_HASH(curthread);
6543 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6544 
6545 	for (th_trace = ipif->ipif_trace[bucket_id]; th_trace != NULL;
6546 	    th_trace = th_trace->th_next) {
6547 		if (th_trace->th_id == curthread)
6548 			return (th_trace);
6549 	}
6550 	return (NULL);
6551 }
6552 
6553 void
6554 ipif_trace_ref(ipif_t *ipif)
6555 {
6556 	int bucket_id;
6557 	th_trace_t *th_trace;
6558 
6559 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6560 
6561 	if (ipif->ipif_trace_disable)
6562 		return;
6563 
6564 	/*
6565 	 * Attempt to locate the trace buffer for the curthread.
6566 	 * If it does not exist, then allocate a new trace buffer
6567 	 * and link it in list of trace bufs for this ipif, at the head
6568 	 */
6569 	th_trace = th_trace_ipif_lookup(ipif);
6570 	if (th_trace == NULL) {
6571 		bucket_id = IP_TR_HASH(curthread);
6572 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6573 		    KM_NOSLEEP);
6574 		if (th_trace == NULL) {
6575 			ipif->ipif_trace_disable = B_TRUE;
6576 			ipif_trace_cleanup(ipif);
6577 			return;
6578 		}
6579 		th_trace->th_id = curthread;
6580 		th_trace->th_next = ipif->ipif_trace[bucket_id];
6581 		th_trace->th_prev = &ipif->ipif_trace[bucket_id];
6582 		if (th_trace->th_next != NULL)
6583 			th_trace->th_next->th_prev = &th_trace->th_next;
6584 		ipif->ipif_trace[bucket_id] = th_trace;
6585 	}
6586 	ASSERT(th_trace->th_refcnt >= 0 &&
6587 	    th_trace->th_refcnt < TR_BUF_MAX -1);
6588 	th_trace->th_refcnt++;
6589 	th_trace_rrecord(th_trace);
6590 }
6591 
6592 void
6593 ipif_untrace_ref(ipif_t *ipif)
6594 {
6595 	th_trace_t *th_trace;
6596 
6597 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6598 
6599 	if (ipif->ipif_trace_disable)
6600 		return;
6601 	th_trace = th_trace_ipif_lookup(ipif);
6602 	ASSERT(th_trace != NULL);
6603 	ASSERT(th_trace->th_refcnt > 0);
6604 
6605 	th_trace->th_refcnt--;
6606 	th_trace_rrecord(th_trace);
6607 }
6608 
6609 th_trace_t *
6610 th_trace_ill_lookup(ill_t *ill)
6611 {
6612 	th_trace_t *th_trace;
6613 	int bucket_id;
6614 
6615 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6616 
6617 	bucket_id = IP_TR_HASH(curthread);
6618 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6619 
6620 	for (th_trace = ill->ill_trace[bucket_id]; th_trace != NULL;
6621 	    th_trace = th_trace->th_next) {
6622 		if (th_trace->th_id == curthread)
6623 			return (th_trace);
6624 	}
6625 	return (NULL);
6626 }
6627 
6628 void
6629 ill_trace_ref(ill_t *ill)
6630 {
6631 	int bucket_id;
6632 	th_trace_t *th_trace;
6633 
6634 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6635 	if (ill->ill_trace_disable)
6636 		return;
6637 	/*
6638 	 * Attempt to locate the trace buffer for the curthread.
6639 	 * If it does not exist, then allocate a new trace buffer
6640 	 * and link it in list of trace bufs for this ill, at the head
6641 	 */
6642 	th_trace = th_trace_ill_lookup(ill);
6643 	if (th_trace == NULL) {
6644 		bucket_id = IP_TR_HASH(curthread);
6645 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6646 		    KM_NOSLEEP);
6647 		if (th_trace == NULL) {
6648 			ill->ill_trace_disable = B_TRUE;
6649 			ill_trace_cleanup(ill);
6650 			return;
6651 		}
6652 		th_trace->th_id = curthread;
6653 		th_trace->th_next = ill->ill_trace[bucket_id];
6654 		th_trace->th_prev = &ill->ill_trace[bucket_id];
6655 		if (th_trace->th_next != NULL)
6656 			th_trace->th_next->th_prev = &th_trace->th_next;
6657 		ill->ill_trace[bucket_id] = th_trace;
6658 	}
6659 	ASSERT(th_trace->th_refcnt >= 0 &&
6660 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6661 
6662 	th_trace->th_refcnt++;
6663 	th_trace_rrecord(th_trace);
6664 }
6665 
6666 void
6667 ill_untrace_ref(ill_t *ill)
6668 {
6669 	th_trace_t *th_trace;
6670 
6671 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6672 
6673 	if (ill->ill_trace_disable)
6674 		return;
6675 	th_trace = th_trace_ill_lookup(ill);
6676 	ASSERT(th_trace != NULL);
6677 	ASSERT(th_trace->th_refcnt > 0);
6678 
6679 	th_trace->th_refcnt--;
6680 	th_trace_rrecord(th_trace);
6681 }
6682 
6683 /*
6684  * Verify that this thread has no refs to the ipif and free
6685  * the trace buffers
6686  */
6687 /* ARGSUSED */
6688 void
6689 ipif_thread_exit(ipif_t *ipif, void *dummy)
6690 {
6691 	th_trace_t *th_trace;
6692 
6693 	mutex_enter(&ipif->ipif_ill->ill_lock);
6694 
6695 	th_trace = th_trace_ipif_lookup(ipif);
6696 	if (th_trace == NULL) {
6697 		mutex_exit(&ipif->ipif_ill->ill_lock);
6698 		return;
6699 	}
6700 	ASSERT(th_trace->th_refcnt == 0);
6701 	/* unlink th_trace and free it */
6702 	*th_trace->th_prev = th_trace->th_next;
6703 	if (th_trace->th_next != NULL)
6704 		th_trace->th_next->th_prev = th_trace->th_prev;
6705 	th_trace->th_next = NULL;
6706 	th_trace->th_prev = NULL;
6707 	kmem_free(th_trace, sizeof (th_trace_t));
6708 
6709 	mutex_exit(&ipif->ipif_ill->ill_lock);
6710 }
6711 
6712 /*
6713  * Verify that this thread has no refs to the ill and free
6714  * the trace buffers
6715  */
6716 /* ARGSUSED */
6717 void
6718 ill_thread_exit(ill_t *ill, void *dummy)
6719 {
6720 	th_trace_t *th_trace;
6721 
6722 	mutex_enter(&ill->ill_lock);
6723 
6724 	th_trace = th_trace_ill_lookup(ill);
6725 	if (th_trace == NULL) {
6726 		mutex_exit(&ill->ill_lock);
6727 		return;
6728 	}
6729 	ASSERT(th_trace->th_refcnt == 0);
6730 	/* unlink th_trace and free it */
6731 	*th_trace->th_prev = th_trace->th_next;
6732 	if (th_trace->th_next != NULL)
6733 		th_trace->th_next->th_prev = th_trace->th_prev;
6734 	th_trace->th_next = NULL;
6735 	th_trace->th_prev = NULL;
6736 	kmem_free(th_trace, sizeof (th_trace_t));
6737 
6738 	mutex_exit(&ill->ill_lock);
6739 }
6740 #endif
6741 
6742 #ifdef ILL_DEBUG
6743 void
6744 ip_thread_exit_stack(ip_stack_t *ipst)
6745 {
6746 	ill_t	*ill;
6747 	ipif_t	*ipif;
6748 	ill_walk_context_t	ctx;
6749 
6750 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6751 	ill = ILL_START_WALK_ALL(&ctx, ipst);
6752 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6753 		for (ipif = ill->ill_ipif; ipif != NULL;
6754 		    ipif = ipif->ipif_next) {
6755 			ipif_thread_exit(ipif, NULL);
6756 		}
6757 		ill_thread_exit(ill, NULL);
6758 	}
6759 	rw_exit(&ipst->ips_ill_g_lock);
6760 
6761 	ire_walk(ire_thread_exit, NULL, ipst);
6762 	ndp_walk_common(ipst->ips_ndp4, NULL, nce_thread_exit, NULL, B_FALSE);
6763 	ndp_walk_common(ipst->ips_ndp6, NULL, nce_thread_exit, NULL, B_FALSE);
6764 }
6765 
6766 /*
6767  * This is a function which is called from thread_exit
6768  * that can be used to debug reference count issues in IP. See comment in
6769  * <inet/ip.h> on how it is used.
6770  */
6771 void
6772 ip_thread_exit(void)
6773 {
6774 	netstack_t *ns;
6775 
6776 	ns = netstack_get_current();
6777 	if (ns != NULL) {
6778 		ip_thread_exit_stack(ns->netstack_ip);
6779 		netstack_rele(ns);
6780 	}
6781 }
6782 
6783 /*
6784  * Called when ipif is unplumbed or when memory alloc fails
6785  */
6786 void
6787 ipif_trace_cleanup(ipif_t *ipif)
6788 {
6789 	int	i;
6790 	th_trace_t	*th_trace;
6791 	th_trace_t	*th_trace_next;
6792 
6793 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6794 		for (th_trace = ipif->ipif_trace[i]; th_trace != NULL;
6795 		    th_trace = th_trace_next) {
6796 			th_trace_next = th_trace->th_next;
6797 			kmem_free(th_trace, sizeof (th_trace_t));
6798 		}
6799 		ipif->ipif_trace[i] = NULL;
6800 	}
6801 }
6802 
6803 /*
6804  * Called when ill is unplumbed or when memory alloc fails
6805  */
6806 void
6807 ill_trace_cleanup(ill_t *ill)
6808 {
6809 	int	i;
6810 	th_trace_t	*th_trace;
6811 	th_trace_t	*th_trace_next;
6812 
6813 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6814 		for (th_trace = ill->ill_trace[i]; th_trace != NULL;
6815 		    th_trace = th_trace_next) {
6816 			th_trace_next = th_trace->th_next;
6817 			kmem_free(th_trace, sizeof (th_trace_t));
6818 		}
6819 		ill->ill_trace[i] = NULL;
6820 	}
6821 }
6822 
6823 #else
6824 void ip_thread_exit(void) {}
6825 #endif
6826 
6827 void
6828 ipif_refhold_locked(ipif_t *ipif)
6829 {
6830 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6831 	ipif->ipif_refcnt++;
6832 	IPIF_TRACE_REF(ipif);
6833 }
6834 
6835 void
6836 ipif_refhold(ipif_t *ipif)
6837 {
6838 	ill_t	*ill;
6839 
6840 	ill = ipif->ipif_ill;
6841 	mutex_enter(&ill->ill_lock);
6842 	ipif->ipif_refcnt++;
6843 	IPIF_TRACE_REF(ipif);
6844 	mutex_exit(&ill->ill_lock);
6845 }
6846 
6847 /*
6848  * Must not be called while holding any locks. Otherwise if this is
6849  * the last reference to be released there is a chance of recursive mutex
6850  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6851  * to restart an ioctl.
6852  */
6853 void
6854 ipif_refrele(ipif_t *ipif)
6855 {
6856 	ill_t	*ill;
6857 
6858 	ill = ipif->ipif_ill;
6859 
6860 	mutex_enter(&ill->ill_lock);
6861 	ASSERT(ipif->ipif_refcnt != 0);
6862 	ipif->ipif_refcnt--;
6863 	IPIF_UNTRACE_REF(ipif);
6864 	if (ipif->ipif_refcnt != 0) {
6865 		mutex_exit(&ill->ill_lock);
6866 		return;
6867 	}
6868 
6869 	/* Drops the ill_lock */
6870 	ipif_ill_refrele_tail(ill);
6871 }
6872 
6873 ipif_t *
6874 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6875 {
6876 	ipif_t	*ipif;
6877 
6878 	mutex_enter(&ill->ill_lock);
6879 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6880 	    ipif != NULL; ipif = ipif->ipif_next) {
6881 		if (!IPIF_CAN_LOOKUP(ipif))
6882 			continue;
6883 		ipif_refhold_locked(ipif);
6884 		mutex_exit(&ill->ill_lock);
6885 		return (ipif);
6886 	}
6887 	mutex_exit(&ill->ill_lock);
6888 	return (NULL);
6889 }
6890 
6891 /*
6892  * TODO: make this table extendible at run time
6893  * Return a pointer to the mac type info for 'mac_type'
6894  */
6895 static ip_m_t *
6896 ip_m_lookup(t_uscalar_t mac_type)
6897 {
6898 	ip_m_t	*ipm;
6899 
6900 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6901 		if (ipm->ip_m_mac_type == mac_type)
6902 			return (ipm);
6903 	return (NULL);
6904 }
6905 
6906 /*
6907  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6908  * ipif_arg is passed in to associate it with the correct interface.
6909  * We may need to restart this operation if the ipif cannot be looked up
6910  * due to an exclusive operation that is currently in progress. The restart
6911  * entry point is specified by 'func'
6912  */
6913 int
6914 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6915     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6916     ire_t **ire_arg, boolean_t ioctl_msg, queue_t *q, mblk_t *mp,
6917     ipsq_func_t func, struct rtsa_s *sp, ip_stack_t *ipst)
6918 {
6919 	ire_t	*ire;
6920 	ire_t	*gw_ire = NULL;
6921 	ipif_t	*ipif = NULL;
6922 	boolean_t ipif_refheld = B_FALSE;
6923 	uint_t	type;
6924 	int	match_flags = MATCH_IRE_TYPE;
6925 	int	error;
6926 	tsol_gc_t *gc = NULL;
6927 	tsol_gcgrp_t *gcgrp = NULL;
6928 	boolean_t gcgrp_xtraref = B_FALSE;
6929 
6930 	ip1dbg(("ip_rt_add:"));
6931 
6932 	if (ire_arg != NULL)
6933 		*ire_arg = NULL;
6934 
6935 	/*
6936 	 * If this is the case of RTF_HOST being set, then we set the netmask
6937 	 * to all ones (regardless if one was supplied).
6938 	 */
6939 	if (flags & RTF_HOST)
6940 		mask = IP_HOST_MASK;
6941 
6942 	/*
6943 	 * Prevent routes with a zero gateway from being created (since
6944 	 * interfaces can currently be plumbed and brought up no assigned
6945 	 * address).
6946 	 * For routes with RTA_SRCIFP, the gateway address can be 0.0.0.0.
6947 	 */
6948 	if (gw_addr == 0 && src_ipif == NULL)
6949 		return (ENETUNREACH);
6950 	/*
6951 	 * Get the ipif, if any, corresponding to the gw_addr
6952 	 */
6953 	if (gw_addr != 0) {
6954 		ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func,
6955 		    &error, ipst);
6956 		if (ipif != NULL) {
6957 			if (IS_VNI(ipif->ipif_ill)) {
6958 				ipif_refrele(ipif);
6959 				return (EINVAL);
6960 			}
6961 			ipif_refheld = B_TRUE;
6962 		} else if (error == EINPROGRESS) {
6963 			ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6964 			return (EINPROGRESS);
6965 		} else {
6966 			error = 0;
6967 		}
6968 	}
6969 
6970 	if (ipif != NULL) {
6971 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6972 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6973 	} else {
6974 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6975 	}
6976 
6977 	/*
6978 	 * GateD will attempt to create routes with a loopback interface
6979 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6980 	 * these routes to be added, but create them as interface routes
6981 	 * since the gateway is an interface address.
6982 	 */
6983 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6984 		flags &= ~RTF_GATEWAY;
6985 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6986 		    mask == IP_HOST_MASK) {
6987 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6988 			    ALL_ZONES, NULL, match_flags, ipst);
6989 			if (ire != NULL) {
6990 				ire_refrele(ire);
6991 				if (ipif_refheld)
6992 					ipif_refrele(ipif);
6993 				return (EEXIST);
6994 			}
6995 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6996 			    "for 0x%x\n", (void *)ipif,
6997 			    ipif->ipif_ire_type,
6998 			    ntohl(ipif->ipif_lcl_addr)));
6999 			ire = ire_create(
7000 			    (uchar_t *)&dst_addr,	/* dest address */
7001 			    (uchar_t *)&mask,		/* mask */
7002 			    (uchar_t *)&ipif->ipif_src_addr,
7003 			    NULL,			/* no gateway */
7004 			    NULL,
7005 			    &ipif->ipif_mtu,
7006 			    NULL,
7007 			    ipif->ipif_rq,		/* recv-from queue */
7008 			    NULL,			/* no send-to queue */
7009 			    ipif->ipif_ire_type,	/* LOOPBACK */
7010 			    ipif,
7011 			    NULL,
7012 			    0,
7013 			    0,
7014 			    0,
7015 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
7016 			    RTF_PRIVATE : 0,
7017 			    &ire_uinfo_null,
7018 			    NULL,
7019 			    NULL,
7020 			    ipst);
7021 
7022 			if (ire == NULL) {
7023 				if (ipif_refheld)
7024 					ipif_refrele(ipif);
7025 				return (ENOMEM);
7026 			}
7027 			error = ire_add(&ire, q, mp, func, B_FALSE);
7028 			if (error == 0)
7029 				goto save_ire;
7030 			if (ipif_refheld)
7031 				ipif_refrele(ipif);
7032 			return (error);
7033 
7034 		}
7035 	}
7036 
7037 	/*
7038 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
7039 	 * and the gateway address provided is one of the system's interface
7040 	 * addresses.  By using the routing socket interface and supplying an
7041 	 * RTA_IFP sockaddr with an interface index, an alternate method of
7042 	 * specifying an interface route to be created is available which uses
7043 	 * the interface index that specifies the outgoing interface rather than
7044 	 * the address of an outgoing interface (which may not be able to
7045 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
7046 	 * flag, routes can be specified which not only specify the next-hop to
7047 	 * be used when routing to a certain prefix, but also which outgoing
7048 	 * interface should be used.
7049 	 *
7050 	 * Previously, interfaces would have unique addresses assigned to them
7051 	 * and so the address assigned to a particular interface could be used
7052 	 * to identify a particular interface.  One exception to this was the
7053 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
7054 	 *
7055 	 * With the advent of IPv6 and its link-local addresses, this
7056 	 * restriction was relaxed and interfaces could share addresses between
7057 	 * themselves.  In fact, typically all of the link-local interfaces on
7058 	 * an IPv6 node or router will have the same link-local address.  In
7059 	 * order to differentiate between these interfaces, the use of an
7060 	 * interface index is necessary and this index can be carried inside a
7061 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
7062 	 * of using the interface index, however, is that all of the ipif's that
7063 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
7064 	 * cannot be used to differentiate between ipif's (or logical
7065 	 * interfaces) that belong to the same ill (physical interface).
7066 	 *
7067 	 * For example, in the following case involving IPv4 interfaces and
7068 	 * logical interfaces
7069 	 *
7070 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
7071 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
7072 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
7073 	 *
7074 	 * the ipif's corresponding to each of these interface routes can be
7075 	 * uniquely identified by the "gateway" (actually interface address).
7076 	 *
7077 	 * In this case involving multiple IPv6 default routes to a particular
7078 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
7079 	 * default route is of interest:
7080 	 *
7081 	 *	default		fe80::123:4567:89ab:cdef	U	if0
7082 	 *	default		fe80::123:4567:89ab:cdef	U	if1
7083 	 */
7084 
7085 	/* RTF_GATEWAY not set */
7086 	if (!(flags & RTF_GATEWAY)) {
7087 		queue_t	*stq;
7088 		queue_t	*rfq = NULL;
7089 		ill_t	*in_ill = NULL;
7090 
7091 		if (sp != NULL) {
7092 			ip2dbg(("ip_rt_add: gateway security attributes "
7093 			    "cannot be set with interface route\n"));
7094 			if (ipif_refheld)
7095 				ipif_refrele(ipif);
7096 			return (EINVAL);
7097 		}
7098 
7099 		/*
7100 		 * As the interface index specified with the RTA_IFP sockaddr is
7101 		 * the same for all ipif's off of an ill, the matching logic
7102 		 * below uses MATCH_IRE_ILL if such an index was specified.
7103 		 * This means that routes sharing the same prefix when added
7104 		 * using a RTA_IFP sockaddr must have distinct interface
7105 		 * indices (namely, they must be on distinct ill's).
7106 		 *
7107 		 * On the other hand, since the gateway address will usually be
7108 		 * different for each ipif on the system, the matching logic
7109 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
7110 		 * route.  This means that interface routes for the same prefix
7111 		 * can be created if they belong to distinct ipif's and if a
7112 		 * RTA_IFP sockaddr is not present.
7113 		 */
7114 		if (ipif_arg != NULL) {
7115 			if (ipif_refheld)  {
7116 				ipif_refrele(ipif);
7117 				ipif_refheld = B_FALSE;
7118 			}
7119 			ipif = ipif_arg;
7120 			match_flags |= MATCH_IRE_ILL;
7121 		} else {
7122 			/*
7123 			 * Check the ipif corresponding to the gw_addr
7124 			 */
7125 			if (ipif == NULL)
7126 				return (ENETUNREACH);
7127 			match_flags |= MATCH_IRE_IPIF;
7128 		}
7129 		ASSERT(ipif != NULL);
7130 		/*
7131 		 * If src_ipif is not NULL, we have to create
7132 		 * an ire with non-null ire_in_ill value
7133 		 */
7134 		if (src_ipif != NULL) {
7135 			in_ill = src_ipif->ipif_ill;
7136 		}
7137 
7138 		/*
7139 		 * We check for an existing entry at this point.
7140 		 *
7141 		 * Since a netmask isn't passed in via the ioctl interface
7142 		 * (SIOCADDRT), we don't check for a matching netmask in that
7143 		 * case.
7144 		 */
7145 		if (!ioctl_msg)
7146 			match_flags |= MATCH_IRE_MASK;
7147 		if (src_ipif != NULL) {
7148 			/* Look up in the special table */
7149 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
7150 			    ipif, src_ipif->ipif_ill, match_flags);
7151 		} else {
7152 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7153 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
7154 			    NULL, match_flags, ipst);
7155 		}
7156 		if (ire != NULL) {
7157 			ire_refrele(ire);
7158 			if (ipif_refheld)
7159 				ipif_refrele(ipif);
7160 			return (EEXIST);
7161 		}
7162 
7163 		if (src_ipif != NULL) {
7164 			/*
7165 			 * Create the special ire for the IRE table
7166 			 * which hangs out of ire_in_ill. This ire
7167 			 * is in-between IRE_CACHE and IRE_INTERFACE.
7168 			 * Thus rfq is non-NULL.
7169 			 */
7170 			rfq = ipif->ipif_rq;
7171 		}
7172 		/* Create the usual interface ires */
7173 
7174 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
7175 		    ? ipif->ipif_rq : ipif->ipif_wq;
7176 
7177 		/*
7178 		 * Create a copy of the IRE_LOOPBACK,
7179 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
7180 		 * the modified address and netmask.
7181 		 */
7182 		ire = ire_create(
7183 		    (uchar_t *)&dst_addr,
7184 		    (uint8_t *)&mask,
7185 		    (uint8_t *)&ipif->ipif_src_addr,
7186 		    NULL,
7187 		    NULL,
7188 		    &ipif->ipif_mtu,
7189 		    NULL,
7190 		    rfq,
7191 		    stq,
7192 		    ipif->ipif_net_type,
7193 		    ipif,
7194 		    in_ill,
7195 		    0,
7196 		    0,
7197 		    0,
7198 		    flags,
7199 		    &ire_uinfo_null,
7200 		    NULL,
7201 		    NULL,
7202 		    ipst);
7203 		if (ire == NULL) {
7204 			if (ipif_refheld)
7205 				ipif_refrele(ipif);
7206 			return (ENOMEM);
7207 		}
7208 
7209 		/*
7210 		 * Some software (for example, GateD and Sun Cluster) attempts
7211 		 * to create (what amount to) IRE_PREFIX routes with the
7212 		 * loopback address as the gateway.  This is primarily done to
7213 		 * set up prefixes with the RTF_REJECT flag set (for example,
7214 		 * when generating aggregate routes.)
7215 		 *
7216 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7217 		 * IRE_LOOPBACK, then we map the request into a
7218 		 * IRE_IF_NORESOLVER.
7219 		 *
7220 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7221 		 * routine, but rather using ire_create() directly.
7222 		 *
7223 		 */
7224 		if (ipif->ipif_net_type == IRE_LOOPBACK)
7225 			ire->ire_type = IRE_IF_NORESOLVER;
7226 
7227 		error = ire_add(&ire, q, mp, func, B_FALSE);
7228 		if (error == 0)
7229 			goto save_ire;
7230 
7231 		/*
7232 		 * In the result of failure, ire_add() will have already
7233 		 * deleted the ire in question, so there is no need to
7234 		 * do that here.
7235 		 */
7236 		if (ipif_refheld)
7237 			ipif_refrele(ipif);
7238 		return (error);
7239 	}
7240 	if (ipif_refheld) {
7241 		ipif_refrele(ipif);
7242 		ipif_refheld = B_FALSE;
7243 	}
7244 
7245 	if (src_ipif != NULL) {
7246 		/* RTA_SRCIFP is not supported on RTF_GATEWAY */
7247 		ip2dbg(("ip_rt_add: SRCIF cannot be set with gateway route\n"));
7248 		return (EINVAL);
7249 	}
7250 	/*
7251 	 * Get an interface IRE for the specified gateway.
7252 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7253 	 * gateway, it is currently unreachable and we fail the request
7254 	 * accordingly.
7255 	 */
7256 	ipif = ipif_arg;
7257 	if (ipif_arg != NULL)
7258 		match_flags |= MATCH_IRE_ILL;
7259 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7260 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7261 	if (gw_ire == NULL)
7262 		return (ENETUNREACH);
7263 
7264 	/*
7265 	 * We create one of three types of IREs as a result of this request
7266 	 * based on the netmask.  A netmask of all ones (which is automatically
7267 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7268 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7269 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7270 	 * destination prefix.
7271 	 */
7272 	if (mask == IP_HOST_MASK)
7273 		type = IRE_HOST;
7274 	else if (mask == 0)
7275 		type = IRE_DEFAULT;
7276 	else
7277 		type = IRE_PREFIX;
7278 
7279 	/* check for a duplicate entry */
7280 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7281 	    NULL, ALL_ZONES, 0, NULL,
7282 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7283 	if (ire != NULL) {
7284 		ire_refrele(gw_ire);
7285 		ire_refrele(ire);
7286 		return (EEXIST);
7287 	}
7288 
7289 	/* Security attribute exists */
7290 	if (sp != NULL) {
7291 		tsol_gcgrp_addr_t ga;
7292 
7293 		/* find or create the gateway credentials group */
7294 		ga.ga_af = AF_INET;
7295 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7296 
7297 		/* we hold reference to it upon success */
7298 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7299 		if (gcgrp == NULL) {
7300 			ire_refrele(gw_ire);
7301 			return (ENOMEM);
7302 		}
7303 
7304 		/*
7305 		 * Create and add the security attribute to the group; a
7306 		 * reference to the group is made upon allocating a new
7307 		 * entry successfully.  If it finds an already-existing
7308 		 * entry for the security attribute in the group, it simply
7309 		 * returns it and no new reference is made to the group.
7310 		 */
7311 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7312 		if (gc == NULL) {
7313 			/* release reference held by gcgrp_lookup */
7314 			GCGRP_REFRELE(gcgrp);
7315 			ire_refrele(gw_ire);
7316 			return (ENOMEM);
7317 		}
7318 	}
7319 
7320 	/* Create the IRE. */
7321 	ire = ire_create(
7322 	    (uchar_t *)&dst_addr,		/* dest address */
7323 	    (uchar_t *)&mask,			/* mask */
7324 	    /* src address assigned by the caller? */
7325 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7326 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7327 	    (uchar_t *)&gw_addr,		/* gateway address */
7328 	    NULL,				/* no in-srcaddress */
7329 	    &gw_ire->ire_max_frag,
7330 	    NULL,				/* no src nce */
7331 	    NULL,				/* no recv-from queue */
7332 	    NULL,				/* no send-to queue */
7333 	    (ushort_t)type,			/* IRE type */
7334 	    ipif_arg,
7335 	    NULL,
7336 	    0,
7337 	    0,
7338 	    0,
7339 	    flags,
7340 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7341 	    gc,					/* security attribute */
7342 	    NULL,
7343 	    ipst);
7344 
7345 	/*
7346 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7347 	 * reference to the 'gcgrp'. We can now release the extra reference
7348 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7349 	 */
7350 	if (gcgrp_xtraref)
7351 		GCGRP_REFRELE(gcgrp);
7352 	if (ire == NULL) {
7353 		if (gc != NULL)
7354 			GC_REFRELE(gc);
7355 		ire_refrele(gw_ire);
7356 		return (ENOMEM);
7357 	}
7358 
7359 	/*
7360 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7361 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7362 	 */
7363 
7364 	/* Add the new IRE. */
7365 	error = ire_add(&ire, q, mp, func, B_FALSE);
7366 	if (error != 0) {
7367 		/*
7368 		 * In the result of failure, ire_add() will have already
7369 		 * deleted the ire in question, so there is no need to
7370 		 * do that here.
7371 		 */
7372 		ire_refrele(gw_ire);
7373 		return (error);
7374 	}
7375 
7376 	if (flags & RTF_MULTIRT) {
7377 		/*
7378 		 * Invoke the CGTP (multirouting) filtering module
7379 		 * to add the dst address in the filtering database.
7380 		 * Replicated inbound packets coming from that address
7381 		 * will be filtered to discard the duplicates.
7382 		 * It is not necessary to call the CGTP filter hook
7383 		 * when the dst address is a broadcast or multicast,
7384 		 * because an IP source address cannot be a broadcast
7385 		 * or a multicast.
7386 		 */
7387 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7388 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7389 		if (ire_dst != NULL) {
7390 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7391 			ire_refrele(ire_dst);
7392 			goto save_ire;
7393 		}
7394 		if ((ip_cgtp_filter_ops != NULL) && !CLASSD(ire->ire_addr) &&
7395 		    ipst->ips_netstack->netstack_stackid == GLOBAL_NETSTACKID) {
7396 			int res = ip_cgtp_filter_ops->cfo_add_dest_v4(
7397 			    ire->ire_addr,
7398 			    ire->ire_gateway_addr,
7399 			    ire->ire_src_addr,
7400 			    gw_ire->ire_src_addr);
7401 			if (res != 0) {
7402 				ire_refrele(gw_ire);
7403 				ire_delete(ire);
7404 				return (res);
7405 			}
7406 		}
7407 	}
7408 
7409 	/*
7410 	 * Now that the prefix IRE entry has been created, delete any
7411 	 * existing gateway IRE cache entries as well as any IRE caches
7412 	 * using the gateway, and force them to be created through
7413 	 * ip_newroute.
7414 	 */
7415 	if (gc != NULL) {
7416 		ASSERT(gcgrp != NULL);
7417 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7418 	}
7419 
7420 save_ire:
7421 	if (gw_ire != NULL) {
7422 		ire_refrele(gw_ire);
7423 	}
7424 	/*
7425 	 * We do not do save_ire for the routes added with RTA_SRCIFP
7426 	 * flag. This route is only added and deleted by mipagent.
7427 	 * So, for simplicity of design, we refrain from saving
7428 	 * ires that are created with srcif value. This may change
7429 	 * in future if we find more usage of srcifp feature.
7430 	 */
7431 	if (ipif != NULL && src_ipif == NULL) {
7432 		/*
7433 		 * Save enough information so that we can recreate the IRE if
7434 		 * the interface goes down and then up.  The metrics associated
7435 		 * with the route will be saved as well when rts_setmetrics() is
7436 		 * called after the IRE has been created.  In the case where
7437 		 * memory cannot be allocated, none of this information will be
7438 		 * saved.
7439 		 */
7440 		ipif_save_ire(ipif, ire);
7441 	}
7442 	if (ioctl_msg)
7443 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7444 	if (ire_arg != NULL) {
7445 		/*
7446 		 * Store the ire that was successfully added into where ire_arg
7447 		 * points to so that callers don't have to look it up
7448 		 * themselves (but they are responsible for ire_refrele()ing
7449 		 * the ire when they are finished with it).
7450 		 */
7451 		*ire_arg = ire;
7452 	} else {
7453 		ire_refrele(ire);		/* Held in ire_add */
7454 	}
7455 	if (ipif_refheld)
7456 		ipif_refrele(ipif);
7457 	return (0);
7458 }
7459 
7460 /*
7461  * ip_rt_delete is called to delete an IPv4 route.
7462  * ipif_arg is passed in to associate it with the correct interface.
7463  * src_ipif is passed to associate the incoming interface of the packet.
7464  * We may need to restart this operation if the ipif cannot be looked up
7465  * due to an exclusive operation that is currently in progress. The restart
7466  * entry point is specified by 'func'
7467  */
7468 /* ARGSUSED4 */
7469 int
7470 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7471     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
7472     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
7473     ip_stack_t *ipst)
7474 {
7475 	ire_t	*ire = NULL;
7476 	ipif_t	*ipif;
7477 	boolean_t ipif_refheld = B_FALSE;
7478 	uint_t	type;
7479 	uint_t	match_flags = MATCH_IRE_TYPE;
7480 	int	err = 0;
7481 
7482 	ip1dbg(("ip_rt_delete:"));
7483 	/*
7484 	 * If this is the case of RTF_HOST being set, then we set the netmask
7485 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7486 	 */
7487 	if (flags & RTF_HOST) {
7488 		mask = IP_HOST_MASK;
7489 		match_flags |= MATCH_IRE_MASK;
7490 	} else if (rtm_addrs & RTA_NETMASK) {
7491 		match_flags |= MATCH_IRE_MASK;
7492 	}
7493 
7494 	/*
7495 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7496 	 * we check if the gateway address is one of our interfaces first,
7497 	 * and fall back on RTF_GATEWAY routes.
7498 	 *
7499 	 * This makes it possible to delete an original
7500 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7501 	 *
7502 	 * As the interface index specified with the RTA_IFP sockaddr is the
7503 	 * same for all ipif's off of an ill, the matching logic below uses
7504 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7505 	 * sharing the same prefix and interface index as the the route
7506 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7507 	 * is specified in the request.
7508 	 *
7509 	 * On the other hand, since the gateway address will usually be
7510 	 * different for each ipif on the system, the matching logic
7511 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7512 	 * route.  This means that interface routes for the same prefix can be
7513 	 * uniquely identified if they belong to distinct ipif's and if a
7514 	 * RTA_IFP sockaddr is not present.
7515 	 *
7516 	 * For more detail on specifying routes by gateway address and by
7517 	 * interface index, see the comments in ip_rt_add().
7518 	 * gw_addr could be zero in some cases when both RTA_SRCIFP and
7519 	 * RTA_IFP are specified. If RTA_SRCIFP is specified and  both
7520 	 * RTA_IFP and gateway_addr are NULL/zero, then delete will not
7521 	 * succeed.
7522 	 */
7523 	if (src_ipif != NULL) {
7524 		if (ipif_arg == NULL && gw_addr != 0) {
7525 			ipif_arg = ipif_lookup_interface(gw_addr, dst_addr,
7526 			    q, mp, func, &err, ipst);
7527 			if (ipif_arg != NULL)
7528 				ipif_refheld = B_TRUE;
7529 		}
7530 		if (ipif_arg == NULL) {
7531 			err = (err == EINPROGRESS) ? err : ESRCH;
7532 			return (err);
7533 		}
7534 		ipif = ipif_arg;
7535 	} else {
7536 		ipif = ipif_lookup_interface(gw_addr, dst_addr,
7537 		    q, mp, func, &err, ipst);
7538 		if (ipif != NULL)
7539 			ipif_refheld = B_TRUE;
7540 		else if (err == EINPROGRESS)
7541 			return (err);
7542 		else
7543 			err = 0;
7544 	}
7545 	if (ipif != NULL) {
7546 		if (ipif_arg != NULL) {
7547 			if (ipif_refheld) {
7548 				ipif_refrele(ipif);
7549 				ipif_refheld = B_FALSE;
7550 			}
7551 			ipif = ipif_arg;
7552 			match_flags |= MATCH_IRE_ILL;
7553 		} else {
7554 			match_flags |= MATCH_IRE_IPIF;
7555 		}
7556 		if (src_ipif != NULL) {
7557 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
7558 			    ipif, src_ipif->ipif_ill, match_flags);
7559 		} else {
7560 			if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7561 				ire = ire_ctable_lookup(dst_addr, 0,
7562 				    IRE_LOOPBACK, ipif, ALL_ZONES, NULL,
7563 				    match_flags, ipst);
7564 			}
7565 			if (ire == NULL) {
7566 				ire = ire_ftable_lookup(dst_addr, mask, 0,
7567 				    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
7568 				    NULL, match_flags, ipst);
7569 			}
7570 		}
7571 	}
7572 
7573 	if (ire == NULL) {
7574 		/*
7575 		 * At this point, the gateway address is not one of our own
7576 		 * addresses or a matching interface route was not found.  We
7577 		 * set the IRE type to lookup based on whether
7578 		 * this is a host route, a default route or just a prefix.
7579 		 *
7580 		 * If an ipif_arg was passed in, then the lookup is based on an
7581 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7582 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7583 		 * set as the route being looked up is not a traditional
7584 		 * interface route.
7585 		 * Since we do not add gateway route with srcipif, we don't
7586 		 * expect to find it either.
7587 		 */
7588 		if (src_ipif != NULL) {
7589 			if (ipif_refheld)
7590 				ipif_refrele(ipif);
7591 			return (ESRCH);
7592 		} else {
7593 			match_flags &= ~MATCH_IRE_IPIF;
7594 			match_flags |= MATCH_IRE_GW;
7595 			if (ipif_arg != NULL)
7596 				match_flags |= MATCH_IRE_ILL;
7597 			if (mask == IP_HOST_MASK)
7598 				type = IRE_HOST;
7599 			else if (mask == 0)
7600 				type = IRE_DEFAULT;
7601 			else
7602 				type = IRE_PREFIX;
7603 			ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type,
7604 			    ipif_arg, NULL, ALL_ZONES, 0, NULL, match_flags,
7605 			    ipst);
7606 		}
7607 	}
7608 
7609 	if (ipif_refheld)
7610 		ipif_refrele(ipif);
7611 
7612 	/* ipif is not refheld anymore */
7613 	if (ire == NULL)
7614 		return (ESRCH);
7615 
7616 	if (ire->ire_flags & RTF_MULTIRT) {
7617 		/*
7618 		 * Invoke the CGTP (multirouting) filtering module
7619 		 * to remove the dst address from the filtering database.
7620 		 * Packets coming from that address will no longer be
7621 		 * filtered to remove duplicates.
7622 		 */
7623 		if (ip_cgtp_filter_ops != NULL &&
7624 		    ipst->ips_netstack->netstack_stackid == GLOBAL_NETSTACKID) {
7625 			err = ip_cgtp_filter_ops->cfo_del_dest_v4(
7626 			    ire->ire_addr, ire->ire_gateway_addr);
7627 		}
7628 		ip_cgtp_bcast_delete(ire, ipst);
7629 	}
7630 
7631 	ipif = ire->ire_ipif;
7632 	/*
7633 	 * Removing from ipif_saved_ire_mp is not necessary
7634 	 * when src_ipif being non-NULL. ip_rt_add does not
7635 	 * save the ires which src_ipif being non-NULL.
7636 	 */
7637 	if (ipif != NULL && src_ipif == NULL) {
7638 		ipif_remove_ire(ipif, ire);
7639 	}
7640 	if (ioctl_msg)
7641 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7642 	ire_delete(ire);
7643 	ire_refrele(ire);
7644 	return (err);
7645 }
7646 
7647 /*
7648  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7649  */
7650 /* ARGSUSED */
7651 int
7652 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7653     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7654 {
7655 	ipaddr_t dst_addr;
7656 	ipaddr_t gw_addr;
7657 	ipaddr_t mask;
7658 	int error = 0;
7659 	mblk_t *mp1;
7660 	struct rtentry *rt;
7661 	ipif_t *ipif = NULL;
7662 	ip_stack_t	*ipst;
7663 
7664 	ASSERT(q->q_next == NULL);
7665 	ipst = CONNQ_TO_IPST(q);
7666 
7667 	ip1dbg(("ip_siocaddrt:"));
7668 	/* Existence of mp1 verified in ip_wput_nondata */
7669 	mp1 = mp->b_cont->b_cont;
7670 	rt = (struct rtentry *)mp1->b_rptr;
7671 
7672 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7673 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7674 
7675 	/*
7676 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7677 	 * to a particular host address.  In this case, we set the netmask to
7678 	 * all ones for the particular destination address.  Otherwise,
7679 	 * determine the netmask to be used based on dst_addr and the interfaces
7680 	 * in use.
7681 	 */
7682 	if (rt->rt_flags & RTF_HOST) {
7683 		mask = IP_HOST_MASK;
7684 	} else {
7685 		/*
7686 		 * Note that ip_subnet_mask returns a zero mask in the case of
7687 		 * default (an all-zeroes address).
7688 		 */
7689 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7690 	}
7691 
7692 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7693 	    NULL, B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7694 	if (ipif != NULL)
7695 		ipif_refrele(ipif);
7696 	return (error);
7697 }
7698 
7699 /*
7700  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7701  */
7702 /* ARGSUSED */
7703 int
7704 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7705     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7706 {
7707 	ipaddr_t dst_addr;
7708 	ipaddr_t gw_addr;
7709 	ipaddr_t mask;
7710 	int error;
7711 	mblk_t *mp1;
7712 	struct rtentry *rt;
7713 	ipif_t *ipif = NULL;
7714 	ip_stack_t	*ipst;
7715 
7716 	ASSERT(q->q_next == NULL);
7717 	ipst = CONNQ_TO_IPST(q);
7718 
7719 	ip1dbg(("ip_siocdelrt:"));
7720 	/* Existence of mp1 verified in ip_wput_nondata */
7721 	mp1 = mp->b_cont->b_cont;
7722 	rt = (struct rtentry *)mp1->b_rptr;
7723 
7724 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7725 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7726 
7727 	/*
7728 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7729 	 * to a particular host address.  In this case, we set the netmask to
7730 	 * all ones for the particular destination address.  Otherwise,
7731 	 * determine the netmask to be used based on dst_addr and the interfaces
7732 	 * in use.
7733 	 */
7734 	if (rt->rt_flags & RTF_HOST) {
7735 		mask = IP_HOST_MASK;
7736 	} else {
7737 		/*
7738 		 * Note that ip_subnet_mask returns a zero mask in the case of
7739 		 * default (an all-zeroes address).
7740 		 */
7741 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7742 	}
7743 
7744 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7745 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, NULL,
7746 	    B_TRUE, q, mp, ip_process_ioctl, ipst);
7747 	if (ipif != NULL)
7748 		ipif_refrele(ipif);
7749 	return (error);
7750 }
7751 
7752 /*
7753  * Enqueue the mp onto the ipsq, chained by b_next.
7754  * b_prev stores the function to be executed later, and b_queue the queue
7755  * where this mp originated.
7756  */
7757 void
7758 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7759     ill_t *pending_ill)
7760 {
7761 	conn_t	*connp = NULL;
7762 
7763 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7764 	ASSERT(func != NULL);
7765 
7766 	mp->b_queue = q;
7767 	mp->b_prev = (void *)func;
7768 	mp->b_next = NULL;
7769 
7770 	switch (type) {
7771 	case CUR_OP:
7772 		if (ipsq->ipsq_mptail != NULL) {
7773 			ASSERT(ipsq->ipsq_mphead != NULL);
7774 			ipsq->ipsq_mptail->b_next = mp;
7775 		} else {
7776 			ASSERT(ipsq->ipsq_mphead == NULL);
7777 			ipsq->ipsq_mphead = mp;
7778 		}
7779 		ipsq->ipsq_mptail = mp;
7780 		break;
7781 
7782 	case NEW_OP:
7783 		if (ipsq->ipsq_xopq_mptail != NULL) {
7784 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7785 			ipsq->ipsq_xopq_mptail->b_next = mp;
7786 		} else {
7787 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7788 			ipsq->ipsq_xopq_mphead = mp;
7789 		}
7790 		ipsq->ipsq_xopq_mptail = mp;
7791 		break;
7792 	default:
7793 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7794 	}
7795 
7796 	if (CONN_Q(q) && pending_ill != NULL) {
7797 		connp = Q_TO_CONN(q);
7798 
7799 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7800 		connp->conn_oper_pending_ill = pending_ill;
7801 	}
7802 }
7803 
7804 /*
7805  * Return the mp at the head of the ipsq. After emptying the ipsq
7806  * look at the next ioctl, if this ioctl is complete. Otherwise
7807  * return, we will resume when we complete the current ioctl.
7808  * The current ioctl will wait till it gets a response from the
7809  * driver below.
7810  */
7811 static mblk_t *
7812 ipsq_dq(ipsq_t *ipsq)
7813 {
7814 	mblk_t	*mp;
7815 
7816 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7817 
7818 	mp = ipsq->ipsq_mphead;
7819 	if (mp != NULL) {
7820 		ipsq->ipsq_mphead = mp->b_next;
7821 		if (ipsq->ipsq_mphead == NULL)
7822 			ipsq->ipsq_mptail = NULL;
7823 		mp->b_next = NULL;
7824 		return (mp);
7825 	}
7826 	if (ipsq->ipsq_current_ipif != NULL)
7827 		return (NULL);
7828 	mp = ipsq->ipsq_xopq_mphead;
7829 	if (mp != NULL) {
7830 		ipsq->ipsq_xopq_mphead = mp->b_next;
7831 		if (ipsq->ipsq_xopq_mphead == NULL)
7832 			ipsq->ipsq_xopq_mptail = NULL;
7833 		mp->b_next = NULL;
7834 		return (mp);
7835 	}
7836 	return (NULL);
7837 }
7838 
7839 /*
7840  * Enter the ipsq corresponding to ill, by waiting synchronously till
7841  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7842  * will have to drain completely before ipsq_enter returns success.
7843  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7844  * and the ipsq_exit logic will start the next enqueued ioctl after
7845  * completion of the current ioctl. If 'force' is used, we don't wait
7846  * for the enqueued ioctls. This is needed when a conn_close wants to
7847  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7848  * of an ill can also use this option. But we dont' use it currently.
7849  */
7850 #define	ENTER_SQ_WAIT_TICKS 100
7851 boolean_t
7852 ipsq_enter(ill_t *ill, boolean_t force)
7853 {
7854 	ipsq_t	*ipsq;
7855 	boolean_t waited_enough = B_FALSE;
7856 
7857 	/*
7858 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7859 	 * Since the <ill-ipsq> assocs could change while we wait for the
7860 	 * writer, it is easier to wait on a fixed global rather than try to
7861 	 * cv_wait on a changing ipsq.
7862 	 */
7863 	mutex_enter(&ill->ill_lock);
7864 	for (;;) {
7865 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7866 			mutex_exit(&ill->ill_lock);
7867 			return (B_FALSE);
7868 		}
7869 
7870 		ipsq = ill->ill_phyint->phyint_ipsq;
7871 		mutex_enter(&ipsq->ipsq_lock);
7872 		if (ipsq->ipsq_writer == NULL &&
7873 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7874 			break;
7875 		} else if (ipsq->ipsq_writer != NULL) {
7876 			mutex_exit(&ipsq->ipsq_lock);
7877 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7878 		} else {
7879 			mutex_exit(&ipsq->ipsq_lock);
7880 			if (force) {
7881 				(void) cv_timedwait(&ill->ill_cv,
7882 				    &ill->ill_lock,
7883 				    lbolt + ENTER_SQ_WAIT_TICKS);
7884 				waited_enough = B_TRUE;
7885 				continue;
7886 			} else {
7887 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7888 			}
7889 		}
7890 	}
7891 
7892 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7893 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7894 	ipsq->ipsq_writer = curthread;
7895 	ipsq->ipsq_reentry_cnt++;
7896 #ifdef ILL_DEBUG
7897 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7898 #endif
7899 	mutex_exit(&ipsq->ipsq_lock);
7900 	mutex_exit(&ill->ill_lock);
7901 	return (B_TRUE);
7902 }
7903 
7904 /*
7905  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7906  * certain critical operations like plumbing (i.e. most set ioctls),
7907  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7908  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7909  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7910  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7911  * threads executing in the ipsq. Responses from the driver pertain to the
7912  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7913  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7914  *
7915  * If a thread does not want to reenter the ipsq when it is already writer,
7916  * it must make sure that the specified reentry point to be called later
7917  * when the ipsq is empty, nor any code path starting from the specified reentry
7918  * point must never ever try to enter the ipsq again. Otherwise it can lead
7919  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7920  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7921  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7922  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7923  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7924  * ioctl if the current ioctl has completed. If the current ioctl is still
7925  * in progress it simply returns. The current ioctl could be waiting for
7926  * a response from another module (arp_ or the driver or could be waiting for
7927  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7928  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7929  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7930  * ipsq_current_ipif is clear which happens only on ioctl completion.
7931  */
7932 
7933 /*
7934  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7935  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7936  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7937  * completion.
7938  */
7939 ipsq_t *
7940 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7941     ipsq_func_t func, int type, boolean_t reentry_ok)
7942 {
7943 	ipsq_t	*ipsq;
7944 
7945 	/* Only 1 of ipif or ill can be specified */
7946 	ASSERT((ipif != NULL) ^ (ill != NULL));
7947 	if (ipif != NULL)
7948 		ill = ipif->ipif_ill;
7949 
7950 	/*
7951 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7952 	 * ipsq of an ill can't change when ill_lock is held.
7953 	 */
7954 	GRAB_CONN_LOCK(q);
7955 	mutex_enter(&ill->ill_lock);
7956 	ipsq = ill->ill_phyint->phyint_ipsq;
7957 	mutex_enter(&ipsq->ipsq_lock);
7958 
7959 	/*
7960 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7961 	 *    (Note: If the caller does not specify reentry_ok then neither
7962 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7963 	 *    again. Otherwise it can lead to an infinite loop
7964 	 * 2. Enter the ipsq if there is no current writer and this attempted
7965 	 *    entry is part of the current ioctl or operation
7966 	 * 3. Enter the ipsq if there is no current writer and this is a new
7967 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7968 	 *    empty and there is no ioctl (or operation) currently in progress
7969 	 */
7970 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7971 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7972 	    ipsq->ipsq_current_ipif == NULL))) ||
7973 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7974 		/* Success. */
7975 		ipsq->ipsq_reentry_cnt++;
7976 		ipsq->ipsq_writer = curthread;
7977 		mutex_exit(&ipsq->ipsq_lock);
7978 		mutex_exit(&ill->ill_lock);
7979 		RELEASE_CONN_LOCK(q);
7980 #ifdef ILL_DEBUG
7981 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7982 #endif
7983 		return (ipsq);
7984 	}
7985 
7986 	ipsq_enq(ipsq, q, mp, func, type, ill);
7987 
7988 	mutex_exit(&ipsq->ipsq_lock);
7989 	mutex_exit(&ill->ill_lock);
7990 	RELEASE_CONN_LOCK(q);
7991 	return (NULL);
7992 }
7993 
7994 /*
7995  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7996  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7997  * cannot be entered, the mp is queued for completion.
7998  */
7999 void
8000 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
8001     boolean_t reentry_ok)
8002 {
8003 	ipsq_t	*ipsq;
8004 
8005 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
8006 
8007 	/*
8008 	 * Drop the caller's refhold on the ill.  This is safe since we either
8009 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
8010 	 * IPSQ, in which case we return without accessing ill anymore.  This
8011 	 * is needed because func needs to see the correct refcount.
8012 	 * e.g. removeif can work only then.
8013 	 */
8014 	ill_refrele(ill);
8015 	if (ipsq != NULL) {
8016 		(*func)(ipsq, q, mp, NULL);
8017 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
8018 	}
8019 }
8020 
8021 /*
8022  * If there are more than ILL_GRP_CNT ills in a group,
8023  * we use kmem alloc'd buffers, else use the stack
8024  */
8025 #define	ILL_GRP_CNT	14
8026 /*
8027  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
8028  * Called by a thread that is currently exclusive on this ipsq.
8029  */
8030 void
8031 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
8032 {
8033 	queue_t	*q;
8034 	mblk_t	*mp;
8035 	ipsq_func_t	func;
8036 	int	next;
8037 	ill_t	**ill_list = NULL;
8038 	size_t	ill_list_size = 0;
8039 	int	cnt = 0;
8040 	boolean_t need_ipsq_free = B_FALSE;
8041 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
8042 
8043 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8044 	mutex_enter(&ipsq->ipsq_lock);
8045 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
8046 	if (ipsq->ipsq_reentry_cnt != 1) {
8047 		ipsq->ipsq_reentry_cnt--;
8048 		mutex_exit(&ipsq->ipsq_lock);
8049 		return;
8050 	}
8051 
8052 	mp = ipsq_dq(ipsq);
8053 	while (mp != NULL) {
8054 again:
8055 		mutex_exit(&ipsq->ipsq_lock);
8056 		func = (ipsq_func_t)mp->b_prev;
8057 		q = (queue_t *)mp->b_queue;
8058 		mp->b_prev = NULL;
8059 		mp->b_queue = NULL;
8060 
8061 		/*
8062 		 * If 'q' is an conn queue, it is valid, since we did a
8063 		 * a refhold on the connp, at the start of the ioctl.
8064 		 * If 'q' is an ill queue, it is valid, since close of an
8065 		 * ill will clean up the 'ipsq'.
8066 		 */
8067 		(*func)(ipsq, q, mp, NULL);
8068 
8069 		mutex_enter(&ipsq->ipsq_lock);
8070 		mp = ipsq_dq(ipsq);
8071 	}
8072 
8073 	mutex_exit(&ipsq->ipsq_lock);
8074 
8075 	/*
8076 	 * Need to grab the locks in the right order. Need to
8077 	 * atomically check (under ipsq_lock) that there are no
8078 	 * messages before relinquishing the ipsq. Also need to
8079 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
8080 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
8081 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
8082 	 * to grab ill_g_lock as writer.
8083 	 */
8084 	rw_enter(&ipst->ips_ill_g_lock,
8085 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
8086 
8087 	/* ipsq_refs can't change while ill_g_lock is held as reader */
8088 	if (ipsq->ipsq_refs != 0) {
8089 		/* At most 2 ills v4/v6 per phyint */
8090 		cnt = ipsq->ipsq_refs << 1;
8091 		ill_list_size = cnt * sizeof (ill_t *);
8092 		/*
8093 		 * If memory allocation fails, we will do the split
8094 		 * the next time ipsq_exit is called for whatever reason.
8095 		 * As long as the ipsq_split flag is set the need to
8096 		 * split is remembered.
8097 		 */
8098 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
8099 		if (ill_list != NULL)
8100 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
8101 	}
8102 	mutex_enter(&ipsq->ipsq_lock);
8103 	mp = ipsq_dq(ipsq);
8104 	if (mp != NULL) {
8105 		/* oops, some message has landed up, we can't get out */
8106 		if (ill_list != NULL)
8107 			ill_unlock_ills(ill_list, cnt);
8108 		rw_exit(&ipst->ips_ill_g_lock);
8109 		if (ill_list != NULL)
8110 			kmem_free(ill_list, ill_list_size);
8111 		ill_list = NULL;
8112 		ill_list_size = 0;
8113 		cnt = 0;
8114 		goto again;
8115 	}
8116 
8117 	/*
8118 	 * Split only if no ioctl is pending and if memory alloc succeeded
8119 	 * above.
8120 	 */
8121 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
8122 	    ill_list != NULL) {
8123 		/*
8124 		 * No new ill can join this ipsq since we are holding the
8125 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
8126 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
8127 		 * If so we will retry on the next ipsq_exit.
8128 		 */
8129 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
8130 	}
8131 
8132 	/*
8133 	 * We are holding the ipsq lock, hence no new messages can
8134 	 * land up on the ipsq, and there are no messages currently.
8135 	 * Now safe to get out. Wake up waiters and relinquish ipsq
8136 	 * atomically while holding ill locks.
8137 	 */
8138 	ipsq->ipsq_writer = NULL;
8139 	ipsq->ipsq_reentry_cnt--;
8140 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
8141 #ifdef ILL_DEBUG
8142 	ipsq->ipsq_depth = 0;
8143 #endif
8144 	mutex_exit(&ipsq->ipsq_lock);
8145 	/*
8146 	 * For IPMP this should wake up all ills in this ipsq.
8147 	 * We need to hold the ill_lock while waking up waiters to
8148 	 * avoid missed wakeups. But there is no need to acquire all
8149 	 * the ill locks and then wakeup. If we have not acquired all
8150 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
8151 	 * wakes up ills one at a time after getting the right ill_lock
8152 	 */
8153 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
8154 	if (ill_list != NULL)
8155 		ill_unlock_ills(ill_list, cnt);
8156 	if (ipsq->ipsq_refs == 0)
8157 		need_ipsq_free = B_TRUE;
8158 	rw_exit(&ipst->ips_ill_g_lock);
8159 	if (ill_list != 0)
8160 		kmem_free(ill_list, ill_list_size);
8161 
8162 	if (need_ipsq_free) {
8163 		/*
8164 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
8165 		 * looked up. ipsq can be looked up only thru ill or phyint
8166 		 * and there are no ills/phyint on this ipsq.
8167 		 */
8168 		ipsq_delete(ipsq);
8169 	}
8170 	/*
8171 	 * Now start any igmp or mld timers that could not be started
8172 	 * while inside the ipsq. The timers can't be started while inside
8173 	 * the ipsq, since igmp_start_timers may need to call untimeout()
8174 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
8175 	 * there could be a deadlock since the timeout handlers
8176 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
8177 	 * wait in ipsq_enter() trying to get the ipsq.
8178 	 *
8179 	 * However there is one exception to the above. If this thread is
8180 	 * itself the igmp/mld timeout handler thread, then we don't want
8181 	 * to start any new timer until the current handler is done. The
8182 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
8183 	 * all others pass B_TRUE.
8184 	 */
8185 	if (start_igmp_timer) {
8186 		mutex_enter(&ipst->ips_igmp_timer_lock);
8187 		next = ipst->ips_igmp_deferred_next;
8188 		ipst->ips_igmp_deferred_next = INFINITY;
8189 		mutex_exit(&ipst->ips_igmp_timer_lock);
8190 
8191 		if (next != INFINITY)
8192 			igmp_start_timers(next, ipst);
8193 	}
8194 
8195 	if (start_mld_timer) {
8196 		mutex_enter(&ipst->ips_mld_timer_lock);
8197 		next = ipst->ips_mld_deferred_next;
8198 		ipst->ips_mld_deferred_next = INFINITY;
8199 		mutex_exit(&ipst->ips_mld_timer_lock);
8200 
8201 		if (next != INFINITY)
8202 			mld_start_timers(next, ipst);
8203 	}
8204 }
8205 
8206 /*
8207  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
8208  * and `ioccmd'.
8209  */
8210 void
8211 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
8212 {
8213 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8214 
8215 	mutex_enter(&ipsq->ipsq_lock);
8216 	ASSERT(ipsq->ipsq_current_ipif == NULL);
8217 	ASSERT(ipsq->ipsq_current_ioctl == 0);
8218 	ipsq->ipsq_current_ipif = ipif;
8219 	ipsq->ipsq_current_ioctl = ioccmd;
8220 	mutex_exit(&ipsq->ipsq_lock);
8221 }
8222 
8223 /*
8224  * Finish the current exclusive operation on `ipsq'.  Note that other
8225  * operations will not be able to proceed until an ipsq_exit() is done.
8226  */
8227 void
8228 ipsq_current_finish(ipsq_t *ipsq)
8229 {
8230 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8231 
8232 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8233 
8234 	/*
8235 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8236 	 * (but we're careful to never set IPIF_CHANGING in that case).
8237 	 */
8238 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8239 		mutex_enter(&ipif->ipif_ill->ill_lock);
8240 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8241 
8242 		/* Send any queued event */
8243 		ill_nic_info_dispatch(ipif->ipif_ill);
8244 		mutex_exit(&ipif->ipif_ill->ill_lock);
8245 	}
8246 
8247 	mutex_enter(&ipsq->ipsq_lock);
8248 	ASSERT(ipsq->ipsq_current_ipif != NULL);
8249 	ipsq->ipsq_current_ipif = NULL;
8250 	ipsq->ipsq_current_ioctl = 0;
8251 	mutex_exit(&ipsq->ipsq_lock);
8252 }
8253 
8254 /*
8255  * The ill is closing. Flush all messages on the ipsq that originated
8256  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8257  * for this ill since ipsq_enter could not have entered until then.
8258  * New messages can't be queued since the CONDEMNED flag is set.
8259  */
8260 static void
8261 ipsq_flush(ill_t *ill)
8262 {
8263 	queue_t	*q;
8264 	mblk_t	*prev;
8265 	mblk_t	*mp;
8266 	mblk_t	*mp_next;
8267 	ipsq_t	*ipsq;
8268 
8269 	ASSERT(IAM_WRITER_ILL(ill));
8270 	ipsq = ill->ill_phyint->phyint_ipsq;
8271 	/*
8272 	 * Flush any messages sent up by the driver.
8273 	 */
8274 	mutex_enter(&ipsq->ipsq_lock);
8275 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8276 		mp_next = mp->b_next;
8277 		q = mp->b_queue;
8278 		if (q == ill->ill_rq || q == ill->ill_wq) {
8279 			/* Remove the mp from the ipsq */
8280 			if (prev == NULL)
8281 				ipsq->ipsq_mphead = mp->b_next;
8282 			else
8283 				prev->b_next = mp->b_next;
8284 			if (ipsq->ipsq_mptail == mp) {
8285 				ASSERT(mp_next == NULL);
8286 				ipsq->ipsq_mptail = prev;
8287 			}
8288 			inet_freemsg(mp);
8289 		} else {
8290 			prev = mp;
8291 		}
8292 	}
8293 	mutex_exit(&ipsq->ipsq_lock);
8294 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8295 	ipsq_xopq_mp_cleanup(ill, NULL);
8296 	ill_pending_mp_cleanup(ill);
8297 }
8298 
8299 /* ARGSUSED */
8300 int
8301 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8302     ip_ioctl_cmd_t *ipip, void *ifreq)
8303 {
8304 	ill_t	*ill;
8305 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8306 	boolean_t isv6;
8307 	conn_t	*connp;
8308 	ip_stack_t	*ipst;
8309 
8310 	connp = Q_TO_CONN(q);
8311 	ipst = connp->conn_netstack->netstack_ip;
8312 	isv6 = connp->conn_af_isv6;
8313 	/*
8314 	 * Set original index.
8315 	 * Failover and failback move logical interfaces
8316 	 * from one physical interface to another.  The
8317 	 * original index indicates the parent of a logical
8318 	 * interface, in other words, the physical interface
8319 	 * the logical interface will be moved back to on
8320 	 * failback.
8321 	 */
8322 
8323 	/*
8324 	 * Don't allow the original index to be changed
8325 	 * for non-failover addresses, autoconfigured
8326 	 * addresses, or IPv6 link local addresses.
8327 	 */
8328 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8329 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8330 		return (EINVAL);
8331 	}
8332 	/*
8333 	 * The new original index must be in use by some
8334 	 * physical interface.
8335 	 */
8336 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8337 	    NULL, NULL, ipst);
8338 	if (ill == NULL)
8339 		return (ENXIO);
8340 	ill_refrele(ill);
8341 
8342 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8343 	/*
8344 	 * When this ipif gets failed back, don't
8345 	 * preserve the original id, as it is no
8346 	 * longer applicable.
8347 	 */
8348 	ipif->ipif_orig_ipifid = 0;
8349 	/*
8350 	 * For IPv4, change the original index of any
8351 	 * multicast addresses associated with the
8352 	 * ipif to the new value.
8353 	 */
8354 	if (!isv6) {
8355 		ilm_t *ilm;
8356 
8357 		mutex_enter(&ipif->ipif_ill->ill_lock);
8358 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8359 		    ilm = ilm->ilm_next) {
8360 			if (ilm->ilm_ipif == ipif) {
8361 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8362 			}
8363 		}
8364 		mutex_exit(&ipif->ipif_ill->ill_lock);
8365 	}
8366 	return (0);
8367 }
8368 
8369 /* ARGSUSED */
8370 int
8371 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8372     ip_ioctl_cmd_t *ipip, void *ifreq)
8373 {
8374 	struct lifreq *lifr = (struct lifreq *)ifreq;
8375 
8376 	/*
8377 	 * Get the original interface index i.e the one
8378 	 * before FAILOVER if it ever happened.
8379 	 */
8380 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8381 	return (0);
8382 }
8383 
8384 /*
8385  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8386  * refhold and return the associated ipif
8387  */
8388 int
8389 ip_extract_tunreq(queue_t *q, mblk_t *mp, ipif_t **ipifp, ipsq_func_t func)
8390 {
8391 	boolean_t exists;
8392 	struct iftun_req *ta;
8393 	ipif_t	*ipif;
8394 	ill_t	*ill;
8395 	boolean_t isv6;
8396 	mblk_t	*mp1;
8397 	int	error;
8398 	conn_t	*connp;
8399 	ip_stack_t	*ipst;
8400 
8401 	/* Existence verified in ip_wput_nondata */
8402 	mp1 = mp->b_cont->b_cont;
8403 	ta = (struct iftun_req *)mp1->b_rptr;
8404 	/*
8405 	 * Null terminate the string to protect against buffer
8406 	 * overrun. String was generated by user code and may not
8407 	 * be trusted.
8408 	 */
8409 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8410 
8411 	connp = Q_TO_CONN(q);
8412 	isv6 = connp->conn_af_isv6;
8413 	ipst = connp->conn_netstack->netstack_ip;
8414 
8415 	/* Disallows implicit create */
8416 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8417 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8418 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8419 	if (ipif == NULL)
8420 		return (error);
8421 
8422 	if (ipif->ipif_id != 0) {
8423 		/*
8424 		 * We really don't want to set/get tunnel parameters
8425 		 * on virtual tunnel interfaces.  Only allow the
8426 		 * base tunnel to do these.
8427 		 */
8428 		ipif_refrele(ipif);
8429 		return (EINVAL);
8430 	}
8431 
8432 	/*
8433 	 * Send down to tunnel mod for ioctl processing.
8434 	 * Will finish ioctl in ip_rput_other().
8435 	 */
8436 	ill = ipif->ipif_ill;
8437 	if (ill->ill_net_type == IRE_LOOPBACK) {
8438 		ipif_refrele(ipif);
8439 		return (EOPNOTSUPP);
8440 	}
8441 
8442 	if (ill->ill_wq == NULL) {
8443 		ipif_refrele(ipif);
8444 		return (ENXIO);
8445 	}
8446 	/*
8447 	 * Mark the ioctl as coming from an IPv6 interface for
8448 	 * tun's convenience.
8449 	 */
8450 	if (ill->ill_isv6)
8451 		ta->ifta_flags |= 0x80000000;
8452 	*ipifp = ipif;
8453 	return (0);
8454 }
8455 
8456 /*
8457  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8458  * and return the associated ipif.
8459  * Return value:
8460  *	Non zero: An error has occurred. ci may not be filled out.
8461  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8462  *	a held ipif in ci.ci_ipif.
8463  */
8464 int
8465 ip_extract_lifreq_cmn(queue_t *q, mblk_t *mp, int cmd_type, int flags,
8466     cmd_info_t *ci, ipsq_func_t func)
8467 {
8468 	sin_t		*sin;
8469 	sin6_t		*sin6;
8470 	char		*name;
8471 	struct ifreq    *ifr;
8472 	struct lifreq    *lifr;
8473 	ipif_t		*ipif = NULL;
8474 	ill_t		*ill;
8475 	conn_t		*connp;
8476 	boolean_t	isv6;
8477 	struct iocblk   *iocp = (struct iocblk *)mp->b_rptr;
8478 	boolean_t	exists;
8479 	int		err;
8480 	mblk_t		*mp1;
8481 	zoneid_t	zoneid;
8482 	ip_stack_t	*ipst;
8483 
8484 	if (q->q_next != NULL) {
8485 		ill = (ill_t *)q->q_ptr;
8486 		isv6 = ill->ill_isv6;
8487 		connp = NULL;
8488 		zoneid = ALL_ZONES;
8489 		ipst = ill->ill_ipst;
8490 	} else {
8491 		ill = NULL;
8492 		connp = Q_TO_CONN(q);
8493 		isv6 = connp->conn_af_isv6;
8494 		zoneid = connp->conn_zoneid;
8495 		if (zoneid == GLOBAL_ZONEID) {
8496 			/* global zone can access ipifs in all zones */
8497 			zoneid = ALL_ZONES;
8498 		}
8499 		ipst = connp->conn_netstack->netstack_ip;
8500 	}
8501 
8502 	/* Has been checked in ip_wput_nondata */
8503 	mp1 = mp->b_cont->b_cont;
8504 
8505 
8506 	if (cmd_type == IF_CMD) {
8507 		/* This a old style SIOC[GS]IF* command */
8508 		ifr = (struct ifreq *)mp1->b_rptr;
8509 		/*
8510 		 * Null terminate the string to protect against buffer
8511 		 * overrun. String was generated by user code and may not
8512 		 * be trusted.
8513 		 */
8514 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8515 		sin = (sin_t *)&ifr->ifr_addr;
8516 		name = ifr->ifr_name;
8517 		ci->ci_sin = sin;
8518 		ci->ci_sin6 = NULL;
8519 		ci->ci_lifr = (struct lifreq *)ifr;
8520 	} else {
8521 		/* This a new style SIOC[GS]LIF* command */
8522 		ASSERT(cmd_type == LIF_CMD);
8523 		lifr = (struct lifreq *)mp1->b_rptr;
8524 		/*
8525 		 * Null terminate the string to protect against buffer
8526 		 * overrun. String was generated by user code and may not
8527 		 * be trusted.
8528 		 */
8529 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8530 		name = lifr->lifr_name;
8531 		sin = (sin_t *)&lifr->lifr_addr;
8532 		sin6 = (sin6_t *)&lifr->lifr_addr;
8533 		if (iocp->ioc_cmd == SIOCSLIFGROUPNAME) {
8534 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8535 			    LIFNAMSIZ);
8536 		}
8537 		ci->ci_sin = sin;
8538 		ci->ci_sin6 = sin6;
8539 		ci->ci_lifr = lifr;
8540 	}
8541 
8542 	if (iocp->ioc_cmd == SIOCSLIFNAME) {
8543 		/*
8544 		 * The ioctl will be failed if the ioctl comes down
8545 		 * an conn stream
8546 		 */
8547 		if (ill == NULL) {
8548 			/*
8549 			 * Not an ill queue, return EINVAL same as the
8550 			 * old error code.
8551 			 */
8552 			return (ENXIO);
8553 		}
8554 		ipif = ill->ill_ipif;
8555 		ipif_refhold(ipif);
8556 	} else {
8557 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8558 		    &exists, isv6, zoneid,
8559 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8560 		    ipst);
8561 		if (ipif == NULL) {
8562 			if (err == EINPROGRESS)
8563 				return (err);
8564 			if (iocp->ioc_cmd == SIOCLIFFAILOVER ||
8565 			    iocp->ioc_cmd == SIOCLIFFAILBACK) {
8566 				/*
8567 				 * Need to try both v4 and v6 since this
8568 				 * ioctl can come down either v4 or v6
8569 				 * socket. The lifreq.lifr_family passed
8570 				 * down by this ioctl is AF_UNSPEC.
8571 				 */
8572 				ipif = ipif_lookup_on_name(name,
8573 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8574 				    zoneid, (connp == NULL) ? q :
8575 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8576 				if (err == EINPROGRESS)
8577 					return (err);
8578 			}
8579 			err = 0;	/* Ensure we don't use it below */
8580 		}
8581 	}
8582 
8583 	/*
8584 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8585 	 */
8586 	if (ipif != NULL && ipif->ipif_isv6 && cmd_type == IF_CMD) {
8587 		ipif_refrele(ipif);
8588 		return (ENXIO);
8589 	}
8590 
8591 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8592 	    name[0] == '\0') {
8593 		/*
8594 		 * Handle a or a SIOC?IF* with a null name
8595 		 * during plumb (on the ill queue before the I_PLINK).
8596 		 */
8597 		ipif = ill->ill_ipif;
8598 		ipif_refhold(ipif);
8599 	}
8600 
8601 	if (ipif == NULL)
8602 		return (ENXIO);
8603 
8604 	/*
8605 	 * Allow only GET operations if this ipif has been created
8606 	 * temporarily due to a MOVE operation.
8607 	 */
8608 	if (ipif->ipif_replace_zero && !(flags & IPI_REPL)) {
8609 		ipif_refrele(ipif);
8610 		return (EINVAL);
8611 	}
8612 
8613 	ci->ci_ipif = ipif;
8614 	return (0);
8615 }
8616 
8617 /*
8618  * Return the total number of ipifs.
8619  */
8620 static uint_t
8621 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8622 {
8623 	uint_t numifs = 0;
8624 	ill_t	*ill;
8625 	ill_walk_context_t	ctx;
8626 	ipif_t	*ipif;
8627 
8628 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8629 	ill = ILL_START_WALK_V4(&ctx, ipst);
8630 
8631 	while (ill != NULL) {
8632 		for (ipif = ill->ill_ipif; ipif != NULL;
8633 		    ipif = ipif->ipif_next) {
8634 			if (ipif->ipif_zoneid == zoneid ||
8635 			    ipif->ipif_zoneid == ALL_ZONES)
8636 				numifs++;
8637 		}
8638 		ill = ill_next(&ctx, ill);
8639 	}
8640 	rw_exit(&ipst->ips_ill_g_lock);
8641 	return (numifs);
8642 }
8643 
8644 /*
8645  * Return the total number of ipifs.
8646  */
8647 static uint_t
8648 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8649 {
8650 	uint_t numifs = 0;
8651 	ill_t	*ill;
8652 	ipif_t	*ipif;
8653 	ill_walk_context_t	ctx;
8654 
8655 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8656 
8657 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8658 	if (family == AF_INET)
8659 		ill = ILL_START_WALK_V4(&ctx, ipst);
8660 	else if (family == AF_INET6)
8661 		ill = ILL_START_WALK_V6(&ctx, ipst);
8662 	else
8663 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8664 
8665 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8666 		for (ipif = ill->ill_ipif; ipif != NULL;
8667 		    ipif = ipif->ipif_next) {
8668 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8669 			    !(lifn_flags & LIFC_NOXMIT))
8670 				continue;
8671 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8672 			    !(lifn_flags & LIFC_TEMPORARY))
8673 				continue;
8674 			if (((ipif->ipif_flags &
8675 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8676 			    IPIF_DEPRECATED)) ||
8677 			    IS_LOOPBACK(ill) ||
8678 			    !(ipif->ipif_flags & IPIF_UP)) &&
8679 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8680 				continue;
8681 
8682 			if (zoneid != ipif->ipif_zoneid &&
8683 			    ipif->ipif_zoneid != ALL_ZONES &&
8684 			    (zoneid != GLOBAL_ZONEID ||
8685 			    !(lifn_flags & LIFC_ALLZONES)))
8686 				continue;
8687 
8688 			numifs++;
8689 		}
8690 	}
8691 	rw_exit(&ipst->ips_ill_g_lock);
8692 	return (numifs);
8693 }
8694 
8695 uint_t
8696 ip_get_lifsrcofnum(ill_t *ill)
8697 {
8698 	uint_t numifs = 0;
8699 	ill_t	*ill_head = ill;
8700 	ip_stack_t	*ipst = ill->ill_ipst;
8701 
8702 	/*
8703 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8704 	 * other thread may be trying to relink the ILLs in this usesrc group
8705 	 * and adjusting the ill_usesrc_grp_next pointers
8706 	 */
8707 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8708 	if ((ill->ill_usesrc_ifindex == 0) &&
8709 	    (ill->ill_usesrc_grp_next != NULL)) {
8710 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8711 		    ill = ill->ill_usesrc_grp_next)
8712 			numifs++;
8713 	}
8714 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8715 
8716 	return (numifs);
8717 }
8718 
8719 /* Null values are passed in for ipif, sin, and ifreq */
8720 /* ARGSUSED */
8721 int
8722 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8723     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8724 {
8725 	int *nump;
8726 	conn_t *connp = Q_TO_CONN(q);
8727 
8728 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8729 
8730 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8731 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8732 
8733 	*nump = ip_get_numifs(connp->conn_zoneid,
8734 	    connp->conn_netstack->netstack_ip);
8735 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8736 	return (0);
8737 }
8738 
8739 /* Null values are passed in for ipif, sin, and ifreq */
8740 /* ARGSUSED */
8741 int
8742 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8743     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8744 {
8745 	struct lifnum *lifn;
8746 	mblk_t	*mp1;
8747 	conn_t *connp = Q_TO_CONN(q);
8748 
8749 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8750 
8751 	/* Existence checked in ip_wput_nondata */
8752 	mp1 = mp->b_cont->b_cont;
8753 
8754 	lifn = (struct lifnum *)mp1->b_rptr;
8755 	switch (lifn->lifn_family) {
8756 	case AF_UNSPEC:
8757 	case AF_INET:
8758 	case AF_INET6:
8759 		break;
8760 	default:
8761 		return (EAFNOSUPPORT);
8762 	}
8763 
8764 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8765 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8766 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8767 	return (0);
8768 }
8769 
8770 /* ARGSUSED */
8771 int
8772 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8773     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8774 {
8775 	STRUCT_HANDLE(ifconf, ifc);
8776 	mblk_t *mp1;
8777 	struct iocblk *iocp;
8778 	struct ifreq *ifr;
8779 	ill_walk_context_t	ctx;
8780 	ill_t	*ill;
8781 	ipif_t	*ipif;
8782 	struct sockaddr_in *sin;
8783 	int32_t	ifclen;
8784 	zoneid_t zoneid;
8785 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8786 
8787 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8788 
8789 	ip1dbg(("ip_sioctl_get_ifconf"));
8790 	/* Existence verified in ip_wput_nondata */
8791 	mp1 = mp->b_cont->b_cont;
8792 	iocp = (struct iocblk *)mp->b_rptr;
8793 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8794 
8795 	/*
8796 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8797 	 * the user buffer address and length into which the list of struct
8798 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8799 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8800 	 * the SIOCGIFCONF operation was redefined to simply provide
8801 	 * a large output buffer into which we are supposed to jam the ifreq
8802 	 * array.  The same ioctl command code was used, despite the fact that
8803 	 * both the applications and the kernel code had to change, thus making
8804 	 * it impossible to support both interfaces.
8805 	 *
8806 	 * For reasons not good enough to try to explain, the following
8807 	 * algorithm is used for deciding what to do with one of these:
8808 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8809 	 * form with the output buffer coming down as the continuation message.
8810 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8811 	 * and we have to copy in the ifconf structure to find out how big the
8812 	 * output buffer is and where to copy out to.  Sure no problem...
8813 	 *
8814 	 */
8815 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8816 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8817 		int numifs = 0;
8818 		size_t ifc_bufsize;
8819 
8820 		/*
8821 		 * Must be (better be!) continuation of a TRANSPARENT
8822 		 * IOCTL.  We just copied in the ifconf structure.
8823 		 */
8824 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8825 		    (struct ifconf *)mp1->b_rptr);
8826 
8827 		/*
8828 		 * Allocate a buffer to hold requested information.
8829 		 *
8830 		 * If ifc_len is larger than what is needed, we only
8831 		 * allocate what we will use.
8832 		 *
8833 		 * If ifc_len is smaller than what is needed, return
8834 		 * EINVAL.
8835 		 *
8836 		 * XXX: the ill_t structure can hava 2 counters, for
8837 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8838 		 * number of interfaces for a device, so we don't need
8839 		 * to count them here...
8840 		 */
8841 		numifs = ip_get_numifs(zoneid, ipst);
8842 
8843 		ifclen = STRUCT_FGET(ifc, ifc_len);
8844 		ifc_bufsize = numifs * sizeof (struct ifreq);
8845 		if (ifc_bufsize > ifclen) {
8846 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8847 				/* old behaviour */
8848 				return (EINVAL);
8849 			} else {
8850 				ifc_bufsize = ifclen;
8851 			}
8852 		}
8853 
8854 		mp1 = mi_copyout_alloc(q, mp,
8855 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8856 		if (mp1 == NULL)
8857 			return (ENOMEM);
8858 
8859 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8860 	}
8861 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8862 	/*
8863 	 * the SIOCGIFCONF ioctl only knows about
8864 	 * IPv4 addresses, so don't try to tell
8865 	 * it about interfaces with IPv6-only
8866 	 * addresses. (Last parm 'isv6' is B_FALSE)
8867 	 */
8868 
8869 	ifr = (struct ifreq *)mp1->b_rptr;
8870 
8871 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8872 	ill = ILL_START_WALK_V4(&ctx, ipst);
8873 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8874 		for (ipif = ill->ill_ipif; ipif != NULL;
8875 		    ipif = ipif->ipif_next) {
8876 			if (zoneid != ipif->ipif_zoneid &&
8877 			    ipif->ipif_zoneid != ALL_ZONES)
8878 				continue;
8879 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8880 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8881 					/* old behaviour */
8882 					rw_exit(&ipst->ips_ill_g_lock);
8883 					return (EINVAL);
8884 				} else {
8885 					goto if_copydone;
8886 				}
8887 			}
8888 			(void) ipif_get_name(ipif,
8889 			    ifr->ifr_name,
8890 			    sizeof (ifr->ifr_name));
8891 			sin = (sin_t *)&ifr->ifr_addr;
8892 			*sin = sin_null;
8893 			sin->sin_family = AF_INET;
8894 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8895 			ifr++;
8896 		}
8897 	}
8898 if_copydone:
8899 	rw_exit(&ipst->ips_ill_g_lock);
8900 	mp1->b_wptr = (uchar_t *)ifr;
8901 
8902 	if (STRUCT_BUF(ifc) != NULL) {
8903 		STRUCT_FSET(ifc, ifc_len,
8904 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8905 	}
8906 	return (0);
8907 }
8908 
8909 /*
8910  * Get the interfaces using the address hosted on the interface passed in,
8911  * as a source adddress
8912  */
8913 /* ARGSUSED */
8914 int
8915 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8916     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8917 {
8918 	mblk_t *mp1;
8919 	ill_t	*ill, *ill_head;
8920 	ipif_t	*ipif, *orig_ipif;
8921 	int	numlifs = 0;
8922 	size_t	lifs_bufsize, lifsmaxlen;
8923 	struct	lifreq *lifr;
8924 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8925 	uint_t	ifindex;
8926 	zoneid_t zoneid;
8927 	int err = 0;
8928 	boolean_t isv6 = B_FALSE;
8929 	struct	sockaddr_in	*sin;
8930 	struct	sockaddr_in6	*sin6;
8931 	STRUCT_HANDLE(lifsrcof, lifs);
8932 	ip_stack_t		*ipst;
8933 
8934 	ipst = CONNQ_TO_IPST(q);
8935 
8936 	ASSERT(q->q_next == NULL);
8937 
8938 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8939 
8940 	/* Existence verified in ip_wput_nondata */
8941 	mp1 = mp->b_cont->b_cont;
8942 
8943 	/*
8944 	 * Must be (better be!) continuation of a TRANSPARENT
8945 	 * IOCTL.  We just copied in the lifsrcof structure.
8946 	 */
8947 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8948 	    (struct lifsrcof *)mp1->b_rptr);
8949 
8950 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8951 		return (EINVAL);
8952 
8953 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8954 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8955 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8956 	    ip_process_ioctl, &err, ipst);
8957 	if (ipif == NULL) {
8958 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8959 		    ifindex));
8960 		return (err);
8961 	}
8962 
8963 
8964 	/* Allocate a buffer to hold requested information */
8965 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8966 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8967 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8968 	/* The actual size needed is always returned in lifs_len */
8969 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8970 
8971 	/* If the amount we need is more than what is passed in, abort */
8972 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8973 		ipif_refrele(ipif);
8974 		return (0);
8975 	}
8976 
8977 	mp1 = mi_copyout_alloc(q, mp,
8978 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8979 	if (mp1 == NULL) {
8980 		ipif_refrele(ipif);
8981 		return (ENOMEM);
8982 	}
8983 
8984 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8985 	bzero(mp1->b_rptr, lifs_bufsize);
8986 
8987 	lifr = (struct lifreq *)mp1->b_rptr;
8988 
8989 	ill = ill_head = ipif->ipif_ill;
8990 	orig_ipif = ipif;
8991 
8992 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8993 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8994 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8995 
8996 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8997 	for (; (ill != NULL) && (ill != ill_head);
8998 	    ill = ill->ill_usesrc_grp_next) {
8999 
9000 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
9001 			break;
9002 
9003 		ipif = ill->ill_ipif;
9004 		(void) ipif_get_name(ipif,
9005 		    lifr->lifr_name, sizeof (lifr->lifr_name));
9006 		if (ipif->ipif_isv6) {
9007 			sin6 = (sin6_t *)&lifr->lifr_addr;
9008 			*sin6 = sin6_null;
9009 			sin6->sin6_family = AF_INET6;
9010 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
9011 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
9012 			    &ipif->ipif_v6net_mask);
9013 		} else {
9014 			sin = (sin_t *)&lifr->lifr_addr;
9015 			*sin = sin_null;
9016 			sin->sin_family = AF_INET;
9017 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
9018 			lifr->lifr_addrlen = ip_mask_to_plen(
9019 			    ipif->ipif_net_mask);
9020 		}
9021 		lifr++;
9022 	}
9023 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
9024 	rw_exit(&ipst->ips_ill_g_lock);
9025 	ipif_refrele(orig_ipif);
9026 	mp1->b_wptr = (uchar_t *)lifr;
9027 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
9028 
9029 	return (0);
9030 }
9031 
9032 /* ARGSUSED */
9033 int
9034 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
9035     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9036 {
9037 	mblk_t *mp1;
9038 	int	list;
9039 	ill_t	*ill;
9040 	ipif_t	*ipif;
9041 	int	flags;
9042 	int	numlifs = 0;
9043 	size_t	lifc_bufsize;
9044 	struct	lifreq *lifr;
9045 	sa_family_t	family;
9046 	struct	sockaddr_in	*sin;
9047 	struct	sockaddr_in6	*sin6;
9048 	ill_walk_context_t	ctx;
9049 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9050 	int32_t	lifclen;
9051 	zoneid_t zoneid;
9052 	STRUCT_HANDLE(lifconf, lifc);
9053 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
9054 
9055 	ip1dbg(("ip_sioctl_get_lifconf"));
9056 
9057 	ASSERT(q->q_next == NULL);
9058 
9059 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9060 
9061 	/* Existence verified in ip_wput_nondata */
9062 	mp1 = mp->b_cont->b_cont;
9063 
9064 	/*
9065 	 * An extended version of SIOCGIFCONF that takes an
9066 	 * additional address family and flags field.
9067 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
9068 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
9069 	 * interfaces are omitted.
9070 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
9071 	 * unless LIFC_TEMPORARY is specified.
9072 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
9073 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
9074 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
9075 	 * has priority over LIFC_NOXMIT.
9076 	 */
9077 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
9078 
9079 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
9080 		return (EINVAL);
9081 
9082 	/*
9083 	 * Must be (better be!) continuation of a TRANSPARENT
9084 	 * IOCTL.  We just copied in the lifconf structure.
9085 	 */
9086 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
9087 
9088 	family = STRUCT_FGET(lifc, lifc_family);
9089 	flags = STRUCT_FGET(lifc, lifc_flags);
9090 
9091 	switch (family) {
9092 	case AF_UNSPEC:
9093 		/*
9094 		 * walk all ILL's.
9095 		 */
9096 		list = MAX_G_HEADS;
9097 		break;
9098 	case AF_INET:
9099 		/*
9100 		 * walk only IPV4 ILL's.
9101 		 */
9102 		list = IP_V4_G_HEAD;
9103 		break;
9104 	case AF_INET6:
9105 		/*
9106 		 * walk only IPV6 ILL's.
9107 		 */
9108 		list = IP_V6_G_HEAD;
9109 		break;
9110 	default:
9111 		return (EAFNOSUPPORT);
9112 	}
9113 
9114 	/*
9115 	 * Allocate a buffer to hold requested information.
9116 	 *
9117 	 * If lifc_len is larger than what is needed, we only
9118 	 * allocate what we will use.
9119 	 *
9120 	 * If lifc_len is smaller than what is needed, return
9121 	 * EINVAL.
9122 	 */
9123 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
9124 	lifc_bufsize = numlifs * sizeof (struct lifreq);
9125 	lifclen = STRUCT_FGET(lifc, lifc_len);
9126 	if (lifc_bufsize > lifclen) {
9127 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
9128 			return (EINVAL);
9129 		else
9130 			lifc_bufsize = lifclen;
9131 	}
9132 
9133 	mp1 = mi_copyout_alloc(q, mp,
9134 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
9135 	if (mp1 == NULL)
9136 		return (ENOMEM);
9137 
9138 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
9139 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
9140 
9141 	lifr = (struct lifreq *)mp1->b_rptr;
9142 
9143 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9144 	ill = ill_first(list, list, &ctx, ipst);
9145 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
9146 		for (ipif = ill->ill_ipif; ipif != NULL;
9147 		    ipif = ipif->ipif_next) {
9148 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
9149 			    !(flags & LIFC_NOXMIT))
9150 				continue;
9151 
9152 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
9153 			    !(flags & LIFC_TEMPORARY))
9154 				continue;
9155 
9156 			if (((ipif->ipif_flags &
9157 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
9158 			    IPIF_DEPRECATED)) ||
9159 			    IS_LOOPBACK(ill) ||
9160 			    !(ipif->ipif_flags & IPIF_UP)) &&
9161 			    (flags & LIFC_EXTERNAL_SOURCE))
9162 				continue;
9163 
9164 			if (zoneid != ipif->ipif_zoneid &&
9165 			    ipif->ipif_zoneid != ALL_ZONES &&
9166 			    (zoneid != GLOBAL_ZONEID ||
9167 			    !(flags & LIFC_ALLZONES)))
9168 				continue;
9169 
9170 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
9171 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
9172 					rw_exit(&ipst->ips_ill_g_lock);
9173 					return (EINVAL);
9174 				} else {
9175 					goto lif_copydone;
9176 				}
9177 			}
9178 
9179 			(void) ipif_get_name(ipif, lifr->lifr_name,
9180 			    sizeof (lifr->lifr_name));
9181 			if (ipif->ipif_isv6) {
9182 				sin6 = (sin6_t *)&lifr->lifr_addr;
9183 				*sin6 = sin6_null;
9184 				sin6->sin6_family = AF_INET6;
9185 				sin6->sin6_addr =
9186 				    ipif->ipif_v6lcl_addr;
9187 				lifr->lifr_addrlen =
9188 				    ip_mask_to_plen_v6(
9189 				    &ipif->ipif_v6net_mask);
9190 			} else {
9191 				sin = (sin_t *)&lifr->lifr_addr;
9192 				*sin = sin_null;
9193 				sin->sin_family = AF_INET;
9194 				sin->sin_addr.s_addr =
9195 				    ipif->ipif_lcl_addr;
9196 				lifr->lifr_addrlen =
9197 				    ip_mask_to_plen(
9198 				    ipif->ipif_net_mask);
9199 			}
9200 			lifr++;
9201 		}
9202 	}
9203 lif_copydone:
9204 	rw_exit(&ipst->ips_ill_g_lock);
9205 
9206 	mp1->b_wptr = (uchar_t *)lifr;
9207 	if (STRUCT_BUF(lifc) != NULL) {
9208 		STRUCT_FSET(lifc, lifc_len,
9209 		    (int)((uchar_t *)lifr - mp1->b_rptr));
9210 	}
9211 	return (0);
9212 }
9213 
9214 /* ARGSUSED */
9215 int
9216 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
9217     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9218 {
9219 	ip_stack_t	*ipst;
9220 
9221 	if (q->q_next == NULL)
9222 		ipst = CONNQ_TO_IPST(q);
9223 	else
9224 		ipst = ILLQ_TO_IPST(q);
9225 
9226 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9227 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9228 	return (0);
9229 }
9230 
9231 static void
9232 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9233 {
9234 	ip6_asp_t *table;
9235 	size_t table_size;
9236 	mblk_t *data_mp;
9237 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9238 	ip_stack_t	*ipst;
9239 
9240 	if (q->q_next == NULL)
9241 		ipst = CONNQ_TO_IPST(q);
9242 	else
9243 		ipst = ILLQ_TO_IPST(q);
9244 
9245 	/* These two ioctls are I_STR only */
9246 	if (iocp->ioc_count == TRANSPARENT) {
9247 		miocnak(q, mp, 0, EINVAL);
9248 		return;
9249 	}
9250 
9251 	data_mp = mp->b_cont;
9252 	if (data_mp == NULL) {
9253 		/* The user passed us a NULL argument */
9254 		table = NULL;
9255 		table_size = iocp->ioc_count;
9256 	} else {
9257 		/*
9258 		 * The user provided a table.  The stream head
9259 		 * may have copied in the user data in chunks,
9260 		 * so make sure everything is pulled up
9261 		 * properly.
9262 		 */
9263 		if (MBLKL(data_mp) < iocp->ioc_count) {
9264 			mblk_t *new_data_mp;
9265 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9266 			    NULL) {
9267 				miocnak(q, mp, 0, ENOMEM);
9268 				return;
9269 			}
9270 			freemsg(data_mp);
9271 			data_mp = new_data_mp;
9272 			mp->b_cont = data_mp;
9273 		}
9274 		table = (ip6_asp_t *)data_mp->b_rptr;
9275 		table_size = iocp->ioc_count;
9276 	}
9277 
9278 	switch (iocp->ioc_cmd) {
9279 	case SIOCGIP6ADDRPOLICY:
9280 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9281 		if (iocp->ioc_rval == -1)
9282 			iocp->ioc_error = EINVAL;
9283 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9284 		else if (table != NULL &&
9285 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9286 			ip6_asp_t *src = table;
9287 			ip6_asp32_t *dst = (void *)table;
9288 			int count = table_size / sizeof (ip6_asp_t);
9289 			int i;
9290 
9291 			/*
9292 			 * We need to do an in-place shrink of the array
9293 			 * to match the alignment attributes of the
9294 			 * 32-bit ABI looking at it.
9295 			 */
9296 			/* LINTED: logical expression always true: op "||" */
9297 			ASSERT(sizeof (*src) > sizeof (*dst));
9298 			for (i = 1; i < count; i++)
9299 				bcopy(src + i, dst + i, sizeof (*dst));
9300 		}
9301 #endif
9302 		break;
9303 
9304 	case SIOCSIP6ADDRPOLICY:
9305 		ASSERT(mp->b_prev == NULL);
9306 		mp->b_prev = (void *)q;
9307 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9308 		/*
9309 		 * We pass in the datamodel here so that the ip6_asp_replace()
9310 		 * routine can handle converting from 32-bit to native formats
9311 		 * where necessary.
9312 		 *
9313 		 * A better way to handle this might be to convert the inbound
9314 		 * data structure here, and hang it off a new 'mp'; thus the
9315 		 * ip6_asp_replace() logic would always be dealing with native
9316 		 * format data structures..
9317 		 *
9318 		 * (An even simpler way to handle these ioctls is to just
9319 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9320 		 * and just recompile everything that depends on it.)
9321 		 */
9322 #endif
9323 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9324 		    iocp->ioc_flag & IOC_MODELS);
9325 		return;
9326 	}
9327 
9328 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9329 	qreply(q, mp);
9330 }
9331 
9332 static void
9333 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9334 {
9335 	mblk_t 		*data_mp;
9336 	struct dstinforeq	*dir;
9337 	uint8_t		*end, *cur;
9338 	in6_addr_t	*daddr, *saddr;
9339 	ipaddr_t	v4daddr;
9340 	ire_t		*ire;
9341 	char		*slabel, *dlabel;
9342 	boolean_t	isipv4;
9343 	int		match_ire;
9344 	ill_t		*dst_ill;
9345 	ipif_t		*src_ipif, *ire_ipif;
9346 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9347 	zoneid_t	zoneid;
9348 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9349 
9350 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9351 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9352 
9353 	/*
9354 	 * This ioctl is I_STR only, and must have a
9355 	 * data mblk following the M_IOCTL mblk.
9356 	 */
9357 	data_mp = mp->b_cont;
9358 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9359 		miocnak(q, mp, 0, EINVAL);
9360 		return;
9361 	}
9362 
9363 	if (MBLKL(data_mp) < iocp->ioc_count) {
9364 		mblk_t *new_data_mp;
9365 
9366 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9367 			miocnak(q, mp, 0, ENOMEM);
9368 			return;
9369 		}
9370 		freemsg(data_mp);
9371 		data_mp = new_data_mp;
9372 		mp->b_cont = data_mp;
9373 	}
9374 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9375 
9376 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9377 	    end - cur >= sizeof (struct dstinforeq);
9378 	    cur += sizeof (struct dstinforeq)) {
9379 		dir = (struct dstinforeq *)cur;
9380 		daddr = &dir->dir_daddr;
9381 		saddr = &dir->dir_saddr;
9382 
9383 		/*
9384 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9385 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9386 		 * and ipif_select_source[_v6]() do not.
9387 		 */
9388 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9389 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9390 
9391 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9392 		if (isipv4) {
9393 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9394 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9395 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9396 		} else {
9397 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9398 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9399 		}
9400 		if (ire == NULL) {
9401 			dir->dir_dreachable = 0;
9402 
9403 			/* move on to next dst addr */
9404 			continue;
9405 		}
9406 		dir->dir_dreachable = 1;
9407 
9408 		ire_ipif = ire->ire_ipif;
9409 		if (ire_ipif == NULL)
9410 			goto next_dst;
9411 
9412 		/*
9413 		 * We expect to get back an interface ire or a
9414 		 * gateway ire cache entry.  For both types, the
9415 		 * output interface is ire_ipif->ipif_ill.
9416 		 */
9417 		dst_ill = ire_ipif->ipif_ill;
9418 		dir->dir_dmactype = dst_ill->ill_mactype;
9419 
9420 		if (isipv4) {
9421 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9422 		} else {
9423 			src_ipif = ipif_select_source_v6(dst_ill,
9424 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9425 			    zoneid);
9426 		}
9427 		if (src_ipif == NULL)
9428 			goto next_dst;
9429 
9430 		*saddr = src_ipif->ipif_v6lcl_addr;
9431 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9432 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9433 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9434 		dir->dir_sdeprecated =
9435 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9436 		ipif_refrele(src_ipif);
9437 next_dst:
9438 		ire_refrele(ire);
9439 	}
9440 	miocack(q, mp, iocp->ioc_count, 0);
9441 }
9442 
9443 
9444 /*
9445  * Check if this is an address assigned to this machine.
9446  * Skips interfaces that are down by using ire checks.
9447  * Translates mapped addresses to v4 addresses and then
9448  * treats them as such, returning true if the v4 address
9449  * associated with this mapped address is configured.
9450  * Note: Applications will have to be careful what they do
9451  * with the response; use of mapped addresses limits
9452  * what can be done with the socket, especially with
9453  * respect to socket options and ioctls - neither IPv4
9454  * options nor IPv6 sticky options/ancillary data options
9455  * may be used.
9456  */
9457 /* ARGSUSED */
9458 int
9459 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9460     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9461 {
9462 	struct sioc_addrreq *sia;
9463 	sin_t *sin;
9464 	ire_t *ire;
9465 	mblk_t *mp1;
9466 	zoneid_t zoneid;
9467 	ip_stack_t	*ipst;
9468 
9469 	ip1dbg(("ip_sioctl_tmyaddr"));
9470 
9471 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9472 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9473 	ipst = CONNQ_TO_IPST(q);
9474 
9475 	/* Existence verified in ip_wput_nondata */
9476 	mp1 = mp->b_cont->b_cont;
9477 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9478 	sin = (sin_t *)&sia->sa_addr;
9479 	switch (sin->sin_family) {
9480 	case AF_INET6: {
9481 		sin6_t *sin6 = (sin6_t *)sin;
9482 
9483 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9484 			ipaddr_t v4_addr;
9485 
9486 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9487 			    v4_addr);
9488 			ire = ire_ctable_lookup(v4_addr, 0,
9489 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9490 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9491 		} else {
9492 			in6_addr_t v6addr;
9493 
9494 			v6addr = sin6->sin6_addr;
9495 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9496 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9497 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9498 		}
9499 		break;
9500 	}
9501 	case AF_INET: {
9502 		ipaddr_t v4addr;
9503 
9504 		v4addr = sin->sin_addr.s_addr;
9505 		ire = ire_ctable_lookup(v4addr, 0,
9506 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9507 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9508 		break;
9509 	}
9510 	default:
9511 		return (EAFNOSUPPORT);
9512 	}
9513 	if (ire != NULL) {
9514 		sia->sa_res = 1;
9515 		ire_refrele(ire);
9516 	} else {
9517 		sia->sa_res = 0;
9518 	}
9519 	return (0);
9520 }
9521 
9522 /*
9523  * Check if this is an address assigned on-link i.e. neighbor,
9524  * and makes sure it's reachable from the current zone.
9525  * Returns true for my addresses as well.
9526  * Translates mapped addresses to v4 addresses and then
9527  * treats them as such, returning true if the v4 address
9528  * associated with this mapped address is configured.
9529  * Note: Applications will have to be careful what they do
9530  * with the response; use of mapped addresses limits
9531  * what can be done with the socket, especially with
9532  * respect to socket options and ioctls - neither IPv4
9533  * options nor IPv6 sticky options/ancillary data options
9534  * may be used.
9535  */
9536 /* ARGSUSED */
9537 int
9538 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9539     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9540 {
9541 	struct sioc_addrreq *sia;
9542 	sin_t *sin;
9543 	mblk_t	*mp1;
9544 	ire_t *ire = NULL;
9545 	zoneid_t zoneid;
9546 	ip_stack_t	*ipst;
9547 
9548 	ip1dbg(("ip_sioctl_tonlink"));
9549 
9550 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9551 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9552 	ipst = CONNQ_TO_IPST(q);
9553 
9554 	/* Existence verified in ip_wput_nondata */
9555 	mp1 = mp->b_cont->b_cont;
9556 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9557 	sin = (sin_t *)&sia->sa_addr;
9558 
9559 	/*
9560 	 * Match addresses with a zero gateway field to avoid
9561 	 * routes going through a router.
9562 	 * Exclude broadcast and multicast addresses.
9563 	 */
9564 	switch (sin->sin_family) {
9565 	case AF_INET6: {
9566 		sin6_t *sin6 = (sin6_t *)sin;
9567 
9568 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9569 			ipaddr_t v4_addr;
9570 
9571 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9572 			    v4_addr);
9573 			if (!CLASSD(v4_addr)) {
9574 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9575 				    NULL, NULL, zoneid, NULL,
9576 				    MATCH_IRE_GW, ipst);
9577 			}
9578 		} else {
9579 			in6_addr_t v6addr;
9580 			in6_addr_t v6gw;
9581 
9582 			v6addr = sin6->sin6_addr;
9583 			v6gw = ipv6_all_zeros;
9584 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9585 				ire = ire_route_lookup_v6(&v6addr, 0,
9586 				    &v6gw, 0, NULL, NULL, zoneid,
9587 				    NULL, MATCH_IRE_GW, ipst);
9588 			}
9589 		}
9590 		break;
9591 	}
9592 	case AF_INET: {
9593 		ipaddr_t v4addr;
9594 
9595 		v4addr = sin->sin_addr.s_addr;
9596 		if (!CLASSD(v4addr)) {
9597 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9598 			    NULL, NULL, zoneid, NULL,
9599 			    MATCH_IRE_GW, ipst);
9600 		}
9601 		break;
9602 	}
9603 	default:
9604 		return (EAFNOSUPPORT);
9605 	}
9606 	sia->sa_res = 0;
9607 	if (ire != NULL) {
9608 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9609 		    IRE_LOCAL|IRE_LOOPBACK)) {
9610 			sia->sa_res = 1;
9611 		}
9612 		ire_refrele(ire);
9613 	}
9614 	return (0);
9615 }
9616 
9617 /*
9618  * TBD: implement when kernel maintaines a list of site prefixes.
9619  */
9620 /* ARGSUSED */
9621 int
9622 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9623     ip_ioctl_cmd_t *ipip, void *ifreq)
9624 {
9625 	return (ENXIO);
9626 }
9627 
9628 /* ARGSUSED */
9629 int
9630 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9631     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9632 {
9633 	ill_t  		*ill;
9634 	mblk_t		*mp1;
9635 	conn_t		*connp;
9636 	boolean_t	success;
9637 
9638 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9639 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9640 	/* ioctl comes down on an conn */
9641 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9642 	connp = Q_TO_CONN(q);
9643 
9644 	mp->b_datap->db_type = M_IOCTL;
9645 
9646 	/*
9647 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9648 	 * The original mp contains contaminated b_next values due to 'mi',
9649 	 * which is needed to do the mi_copy_done. Unfortunately if we
9650 	 * send down the original mblk itself and if we are popped due to an
9651 	 * an unplumb before the response comes back from tunnel,
9652 	 * the streamhead (which does a freemsg) will see this contaminated
9653 	 * message and the assertion in freemsg about non-null b_next/b_prev
9654 	 * will panic a DEBUG kernel.
9655 	 */
9656 	mp1 = copymsg(mp);
9657 	if (mp1 == NULL)
9658 		return (ENOMEM);
9659 
9660 	ill = ipif->ipif_ill;
9661 	mutex_enter(&connp->conn_lock);
9662 	mutex_enter(&ill->ill_lock);
9663 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9664 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9665 		    mp, 0);
9666 	} else {
9667 		success = ill_pending_mp_add(ill, connp, mp);
9668 	}
9669 	mutex_exit(&ill->ill_lock);
9670 	mutex_exit(&connp->conn_lock);
9671 
9672 	if (success) {
9673 		ip1dbg(("sending down tunparam request "));
9674 		putnext(ill->ill_wq, mp1);
9675 		return (EINPROGRESS);
9676 	} else {
9677 		/* The conn has started closing */
9678 		freemsg(mp1);
9679 		return (EINTR);
9680 	}
9681 }
9682 
9683 static int
9684 ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp, sin_t *sin,
9685     boolean_t x_arp_ioctl, boolean_t if_arp_ioctl)
9686 {
9687 	mblk_t *mp1;
9688 	mblk_t *mp2;
9689 	mblk_t *pending_mp;
9690 	ipaddr_t ipaddr;
9691 	area_t *area;
9692 	struct iocblk *iocp;
9693 	conn_t *connp;
9694 	struct arpreq *ar;
9695 	struct xarpreq *xar;
9696 	boolean_t success;
9697 	int flags, alength;
9698 	char *lladdr;
9699 	ip_stack_t	*ipst;
9700 
9701 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9702 	connp = Q_TO_CONN(q);
9703 	ipst = connp->conn_netstack->netstack_ip;
9704 
9705 	iocp = (struct iocblk *)mp->b_rptr;
9706 	/*
9707 	 * ill has already been set depending on whether
9708 	 * bsd style or interface style ioctl.
9709 	 */
9710 	ASSERT(ill != NULL);
9711 
9712 	/*
9713 	 * Is this one of the new SIOC*XARP ioctls?
9714 	 */
9715 	if (x_arp_ioctl) {
9716 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9717 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9718 		ar = NULL;
9719 
9720 		flags = xar->xarp_flags;
9721 		lladdr = LLADDR(&xar->xarp_ha);
9722 		/*
9723 		 * Validate against user's link layer address length
9724 		 * input and name and addr length limits.
9725 		 */
9726 		alength = ill->ill_phys_addr_length;
9727 		if (iocp->ioc_cmd == SIOCSXARP) {
9728 			if (alength != xar->xarp_ha.sdl_alen ||
9729 			    (alength + xar->xarp_ha.sdl_nlen >
9730 			    sizeof (xar->xarp_ha.sdl_data)))
9731 				return (EINVAL);
9732 		}
9733 	} else {
9734 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9735 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9736 		xar = NULL;
9737 
9738 		flags = ar->arp_flags;
9739 		lladdr = ar->arp_ha.sa_data;
9740 		/*
9741 		 * Theoretically, the sa_family could tell us what link
9742 		 * layer type this operation is trying to deal with. By
9743 		 * common usage AF_UNSPEC means ethernet. We'll assume
9744 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9745 		 * for now. Our new SIOC*XARP ioctls can be used more
9746 		 * generally.
9747 		 *
9748 		 * If the underlying media happens to have a non 6 byte
9749 		 * address, arp module will fail set/get, but the del
9750 		 * operation will succeed.
9751 		 */
9752 		alength = 6;
9753 		if ((iocp->ioc_cmd != SIOCDARP) &&
9754 		    (alength != ill->ill_phys_addr_length)) {
9755 			return (EINVAL);
9756 		}
9757 	}
9758 
9759 	/*
9760 	 * We are going to pass up to ARP a packet chain that looks
9761 	 * like:
9762 	 *
9763 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9764 	 *
9765 	 * Get a copy of the original IOCTL mblk to head the chain,
9766 	 * to be sent up (in mp1). Also get another copy to store
9767 	 * in the ill_pending_mp list, for matching the response
9768 	 * when it comes back from ARP.
9769 	 */
9770 	mp1 = copyb(mp);
9771 	pending_mp = copymsg(mp);
9772 	if (mp1 == NULL || pending_mp == NULL) {
9773 		if (mp1 != NULL)
9774 			freeb(mp1);
9775 		if (pending_mp != NULL)
9776 			inet_freemsg(pending_mp);
9777 		return (ENOMEM);
9778 	}
9779 
9780 	ipaddr = sin->sin_addr.s_addr;
9781 
9782 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9783 	    (caddr_t)&ipaddr);
9784 	if (mp2 == NULL) {
9785 		freeb(mp1);
9786 		inet_freemsg(pending_mp);
9787 		return (ENOMEM);
9788 	}
9789 	/* Put together the chain. */
9790 	mp1->b_cont = mp2;
9791 	mp1->b_datap->db_type = M_IOCTL;
9792 	mp2->b_cont = mp;
9793 	mp2->b_datap->db_type = M_DATA;
9794 
9795 	iocp = (struct iocblk *)mp1->b_rptr;
9796 
9797 	/*
9798 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9799 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9800 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9801 	 * ioc_count field; set ioc_count to be correct.
9802 	 */
9803 	iocp->ioc_count = MBLKL(mp1->b_cont);
9804 
9805 	/*
9806 	 * Set the proper command in the ARP message.
9807 	 * Convert the SIOC{G|S|D}ARP calls into our
9808 	 * AR_ENTRY_xxx calls.
9809 	 */
9810 	area = (area_t *)mp2->b_rptr;
9811 	switch (iocp->ioc_cmd) {
9812 	case SIOCDARP:
9813 	case SIOCDXARP:
9814 		/*
9815 		 * We defer deleting the corresponding IRE until
9816 		 * we return from arp.
9817 		 */
9818 		area->area_cmd = AR_ENTRY_DELETE;
9819 		area->area_proto_mask_offset = 0;
9820 		break;
9821 	case SIOCGARP:
9822 	case SIOCGXARP:
9823 		area->area_cmd = AR_ENTRY_SQUERY;
9824 		area->area_proto_mask_offset = 0;
9825 		break;
9826 	case SIOCSARP:
9827 	case SIOCSXARP: {
9828 		/*
9829 		 * Delete the corresponding ire to make sure IP will
9830 		 * pick up any change from arp.
9831 		 */
9832 		if (!if_arp_ioctl) {
9833 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9834 			break;
9835 		} else {
9836 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9837 			if (ipif != NULL) {
9838 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9839 				    ipst);
9840 				ipif_refrele(ipif);
9841 			}
9842 			break;
9843 		}
9844 	}
9845 	}
9846 	iocp->ioc_cmd = area->area_cmd;
9847 
9848 	/*
9849 	 * Before sending 'mp' to ARP, we have to clear the b_next
9850 	 * and b_prev. Otherwise if STREAMS encounters such a message
9851 	 * in freemsg(), (because ARP can close any time) it can cause
9852 	 * a panic. But mi code needs the b_next and b_prev values of
9853 	 * mp->b_cont, to complete the ioctl. So we store it here
9854 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9855 	 * when the response comes down from ARP.
9856 	 */
9857 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9858 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9859 	mp->b_cont->b_next = NULL;
9860 	mp->b_cont->b_prev = NULL;
9861 
9862 	mutex_enter(&connp->conn_lock);
9863 	mutex_enter(&ill->ill_lock);
9864 	/* conn has not yet started closing, hence this can't fail */
9865 	success = ill_pending_mp_add(ill, connp, pending_mp);
9866 	ASSERT(success);
9867 	mutex_exit(&ill->ill_lock);
9868 	mutex_exit(&connp->conn_lock);
9869 
9870 	/*
9871 	 * Fill in the rest of the ARP operation fields.
9872 	 */
9873 	area->area_hw_addr_length = alength;
9874 	bcopy(lladdr,
9875 	    (char *)area + area->area_hw_addr_offset,
9876 	    area->area_hw_addr_length);
9877 	/* Translate the flags. */
9878 	if (flags & ATF_PERM)
9879 		area->area_flags |= ACE_F_PERMANENT;
9880 	if (flags & ATF_PUBL)
9881 		area->area_flags |= ACE_F_PUBLISH;
9882 	if (flags & ATF_AUTHORITY)
9883 		area->area_flags |= ACE_F_AUTHORITY;
9884 
9885 	/*
9886 	 * Up to ARP it goes.  The response will come
9887 	 * back in ip_wput as an M_IOCACK message, and
9888 	 * will be handed to ip_sioctl_iocack for
9889 	 * completion.
9890 	 */
9891 	putnext(ill->ill_rq, mp1);
9892 	return (EINPROGRESS);
9893 }
9894 
9895 /* ARGSUSED */
9896 int
9897 ip_sioctl_xarp(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9898     ip_ioctl_cmd_t *ipip, void *ifreq)
9899 {
9900 	struct xarpreq *xar;
9901 	boolean_t isv6;
9902 	mblk_t	*mp1;
9903 	int	err;
9904 	conn_t	*connp;
9905 	int ifnamelen;
9906 	ire_t	*ire = NULL;
9907 	ill_t	*ill = NULL;
9908 	struct sockaddr_in *sin;
9909 	boolean_t if_arp_ioctl = B_FALSE;
9910 	ip_stack_t	*ipst;
9911 
9912 	/* ioctl comes down on an conn */
9913 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9914 	connp = Q_TO_CONN(q);
9915 	isv6 = connp->conn_af_isv6;
9916 	ipst = connp->conn_netstack->netstack_ip;
9917 
9918 	/* Existance verified in ip_wput_nondata */
9919 	mp1 = mp->b_cont->b_cont;
9920 
9921 	ASSERT(MBLKL(mp1) >= sizeof (*xar));
9922 	xar = (struct xarpreq *)mp1->b_rptr;
9923 	sin = (sin_t *)&xar->xarp_pa;
9924 
9925 	if (isv6 || (xar->xarp_ha.sdl_family != AF_LINK) ||
9926 	    (xar->xarp_pa.ss_family != AF_INET))
9927 		return (ENXIO);
9928 
9929 	ifnamelen = xar->xarp_ha.sdl_nlen;
9930 	if (ifnamelen != 0) {
9931 		char	*cptr, cval;
9932 
9933 		if (ifnamelen >= LIFNAMSIZ)
9934 			return (EINVAL);
9935 
9936 		/*
9937 		 * Instead of bcopying a bunch of bytes,
9938 		 * null-terminate the string in-situ.
9939 		 */
9940 		cptr = xar->xarp_ha.sdl_data + ifnamelen;
9941 		cval = *cptr;
9942 		*cptr = '\0';
9943 		ill = ill_lookup_on_name(xar->xarp_ha.sdl_data,
9944 		    B_FALSE, isv6, CONNP_TO_WQ(connp), mp, ip_process_ioctl,
9945 		    &err, NULL, ipst);
9946 		*cptr = cval;
9947 		if (ill == NULL)
9948 			return (err);
9949 		if (ill->ill_net_type != IRE_IF_RESOLVER) {
9950 			ill_refrele(ill);
9951 			return (ENXIO);
9952 		}
9953 
9954 		if_arp_ioctl = B_TRUE;
9955 	} else {
9956 		/*
9957 		 * PSARC 2003/088 states that if sdl_nlen == 0, it behaves
9958 		 * as an extended BSD ioctl. The kernel uses the IP address
9959 		 * to figure out the network interface.
9960 		 */
9961 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9962 		    ipst);
9963 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9964 		    ((ill = ire_to_ill(ire)) == NULL) ||
9965 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9966 			if (ire != NULL)
9967 				ire_refrele(ire);
9968 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9969 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9970 			    NULL, MATCH_IRE_TYPE, ipst);
9971 			if ((ire == NULL) ||
9972 			    ((ill = ire_to_ill(ire)) == NULL)) {
9973 				if (ire != NULL)
9974 					ire_refrele(ire);
9975 				return (ENXIO);
9976 			}
9977 		}
9978 		ASSERT(ire != NULL && ill != NULL);
9979 	}
9980 
9981 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_TRUE, if_arp_ioctl);
9982 	if (if_arp_ioctl)
9983 		ill_refrele(ill);
9984 	if (ire != NULL)
9985 		ire_refrele(ire);
9986 
9987 	return (err);
9988 }
9989 
9990 /*
9991  * ARP IOCTLs.
9992  * How does IP get in the business of fronting ARP configuration/queries?
9993  * Well its like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9994  * are by tradition passed in through a datagram socket.  That lands in IP.
9995  * As it happens, this is just as well since the interface is quite crude in
9996  * that it passes in no information about protocol or hardware types, or
9997  * interface association.  After making the protocol assumption, IP is in
9998  * the position to look up the name of the ILL, which ARP will need, and
9999  * format a request that can be handled by ARP.	 The request is passed up
10000  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
10001  * back a response.  ARP supports its own set of more general IOCTLs, in
10002  * case anyone is interested.
10003  */
10004 /* ARGSUSED */
10005 int
10006 ip_sioctl_arp(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10007     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
10008 {
10009 	struct arpreq *ar;
10010 	struct sockaddr_in *sin;
10011 	ire_t	*ire;
10012 	boolean_t isv6;
10013 	mblk_t	*mp1;
10014 	int	err;
10015 	conn_t	*connp;
10016 	ill_t	*ill;
10017 	ip_stack_t	*ipst;
10018 
10019 	/* ioctl comes down on an conn */
10020 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
10021 	connp = Q_TO_CONN(q);
10022 	ipst = CONNQ_TO_IPST(q);
10023 	isv6 = connp->conn_af_isv6;
10024 	if (isv6)
10025 		return (ENXIO);
10026 
10027 	/* Existance verified in ip_wput_nondata */
10028 	mp1 = mp->b_cont->b_cont;
10029 
10030 	ar = (struct arpreq *)mp1->b_rptr;
10031 	sin = (sin_t *)&ar->arp_pa;
10032 
10033 	/*
10034 	 * We need to let ARP know on which interface the IP
10035 	 * address has an ARP mapping. In the IPMP case, a
10036 	 * simple forwarding table lookup will return the
10037 	 * IRE_IF_RESOLVER for the first interface in the group,
10038 	 * which might not be the interface on which the
10039 	 * requested IP address was resolved due to the ill
10040 	 * selection algorithm (see ip_newroute_get_dst_ill()).
10041 	 * So we do a cache table lookup first: if the IRE cache
10042 	 * entry for the IP address is still there, it will
10043 	 * contain the ill pointer for the right interface, so
10044 	 * we use that. If the cache entry has been flushed, we
10045 	 * fall back to the forwarding table lookup. This should
10046 	 * be rare enough since IRE cache entries have a longer
10047 	 * life expectancy than ARP cache entries.
10048 	 */
10049 	ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL, ipst);
10050 	if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
10051 	    ((ill = ire_to_ill(ire)) == NULL)) {
10052 		if (ire != NULL)
10053 			ire_refrele(ire);
10054 		ire = ire_ftable_lookup(sin->sin_addr.s_addr,
10055 		    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
10056 		    NULL, MATCH_IRE_TYPE, ipst);
10057 		if ((ire == NULL) || ((ill = ire_to_ill(ire)) == NULL)) {
10058 			if (ire != NULL)
10059 				ire_refrele(ire);
10060 			return (ENXIO);
10061 		}
10062 	}
10063 	ASSERT(ire != NULL && ill != NULL);
10064 
10065 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_FALSE, B_FALSE);
10066 	ire_refrele(ire);
10067 	return (err);
10068 }
10069 
10070 /*
10071  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
10072  * atomically set/clear the muxids. Also complete the ioctl by acking or
10073  * naking it.  Note that the code is structured such that the link type,
10074  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
10075  * its clones use the persistent link, while pppd(1M) and perhaps many
10076  * other daemons may use non-persistent link.  When combined with some
10077  * ill_t states, linking and unlinking lower streams may be used as
10078  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
10079  */
10080 /* ARGSUSED */
10081 void
10082 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
10083 {
10084 	mblk_t		*mp1, *mp2;
10085 	struct linkblk	*li;
10086 	struct ipmx_s	*ipmxp;
10087 	ill_t		*ill;
10088 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
10089 	int		err = 0;
10090 	boolean_t	entered_ipsq = B_FALSE;
10091 	boolean_t	islink;
10092 	ip_stack_t	*ipst;
10093 
10094 	if (CONN_Q(q))
10095 		ipst = CONNQ_TO_IPST(q);
10096 	else
10097 		ipst = ILLQ_TO_IPST(q);
10098 
10099 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
10100 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
10101 
10102 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
10103 
10104 	mp1 = mp->b_cont;	/* This is the linkblk info */
10105 	li = (struct linkblk *)mp1->b_rptr;
10106 
10107 	/*
10108 	 * ARP has added this special mblk, and the utility is asking us
10109 	 * to perform consistency checks, and also atomically set the
10110 	 * muxid. Ifconfig is an example.  It achieves this by using
10111 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
10112 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
10113 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
10114 	 * and other comments in this routine for more details.
10115 	 */
10116 	mp2 = mp1->b_cont;	/* This is added by ARP */
10117 
10118 	/*
10119 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
10120 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
10121 	 * get the special mblk above.  For backward compatibility, we
10122 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
10123 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
10124 	 * not atomic, and can leave the streams unplumbable if the utility
10125 	 * is interrupted before it does the SIOCSLIFMUXID.
10126 	 */
10127 	if (mp2 == NULL) {
10128 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
10129 		if (err == EINPROGRESS)
10130 			return;
10131 		goto done;
10132 	}
10133 
10134 	/*
10135 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
10136 	 * ARP has appended this last mblk to tell us whether the lower stream
10137 	 * is an arp-dev stream or an IP module stream.
10138 	 */
10139 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
10140 	if (ipmxp->ipmx_arpdev_stream) {
10141 		/*
10142 		 * The lower stream is the arp-dev stream.
10143 		 */
10144 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
10145 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
10146 		if (ill == NULL) {
10147 			if (err == EINPROGRESS)
10148 				return;
10149 			err = EINVAL;
10150 			goto done;
10151 		}
10152 
10153 		if (ipsq == NULL) {
10154 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
10155 			    NEW_OP, B_TRUE);
10156 			if (ipsq == NULL) {
10157 				ill_refrele(ill);
10158 				return;
10159 			}
10160 			entered_ipsq = B_TRUE;
10161 		}
10162 		ASSERT(IAM_WRITER_ILL(ill));
10163 		ill_refrele(ill);
10164 
10165 		/*
10166 		 * To ensure consistency between IP and ARP, the following
10167 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
10168 		 * This is because the muxid's are stored in the IP stream on
10169 		 * the ill.
10170 		 *
10171 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
10172 		 * the ARP stream. On an arp-dev stream, IP checks that it is
10173 		 * not yet plinked, and it also checks that the corresponding
10174 		 * IP stream is already plinked.
10175 		 *
10176 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
10177 		 * punlinking the IP stream. IP does not allow punlink of the
10178 		 * IP stream unless the arp stream has been punlinked.
10179 		 */
10180 		if ((islink &&
10181 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
10182 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
10183 			err = EINVAL;
10184 			goto done;
10185 		}
10186 		ill->ill_arp_muxid = islink ? li->l_index : 0;
10187 	} else {
10188 		/*
10189 		 * The lower stream is probably an IP module stream.  Do
10190 		 * consistency checking.
10191 		 */
10192 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
10193 		if (err == EINPROGRESS)
10194 			return;
10195 	}
10196 done:
10197 	if (err == 0)
10198 		miocack(q, mp, 0, 0);
10199 	else
10200 		miocnak(q, mp, 0, err);
10201 
10202 	/* Conn was refheld in ip_sioctl_copyin_setup */
10203 	if (CONN_Q(q))
10204 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
10205 	if (entered_ipsq)
10206 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
10207 }
10208 
10209 /*
10210  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
10211  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
10212  * module stream).  If `doconsist' is set, then do the extended consistency
10213  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
10214  * Returns zero on success, EINPROGRESS if the operation is still pending, or
10215  * an error code on failure.
10216  */
10217 static int
10218 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
10219     struct linkblk *li, boolean_t doconsist)
10220 {
10221 	ill_t  		*ill;
10222 	queue_t		*ipwq, *dwq;
10223 	const char	*name;
10224 	struct qinit	*qinfo;
10225 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
10226 
10227 	/*
10228 	 * Walk the lower stream to verify it's the IP module stream.
10229 	 * The IP module is identified by its name, wput function,
10230 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
10231 	 * (li->l_qbot) will not vanish until this ioctl completes.
10232 	 */
10233 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
10234 		qinfo = ipwq->q_qinfo;
10235 		name = qinfo->qi_minfo->mi_idname;
10236 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
10237 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
10238 			break;
10239 		}
10240 	}
10241 
10242 	/*
10243 	 * If this isn't an IP module stream, bail.
10244 	 */
10245 	if (ipwq == NULL)
10246 		return (0);
10247 
10248 	ill = ipwq->q_ptr;
10249 	ASSERT(ill != NULL);
10250 
10251 	if (ipsq == NULL) {
10252 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
10253 		    NEW_OP, B_TRUE);
10254 		if (ipsq == NULL)
10255 			return (EINPROGRESS);
10256 	}
10257 	ASSERT(IAM_WRITER_ILL(ill));
10258 
10259 	if (doconsist) {
10260 		/*
10261 		 * Consistency checking requires that I_{P}LINK occurs
10262 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
10263 		 * occurs prior to clearing ill_arp_muxid.
10264 		 */
10265 		if ((islink && ill->ill_ip_muxid != 0) ||
10266 		    (!islink && ill->ill_arp_muxid != 0)) {
10267 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
10268 			return (EINVAL);
10269 		}
10270 	}
10271 
10272 	/*
10273 	 * As part of I_{P}LINKing, stash the number of downstream modules and
10274 	 * the read queue of the module immediately below IP in the ill.
10275 	 * These are used during the capability negotiation below.
10276 	 */
10277 	ill->ill_lmod_rq = NULL;
10278 	ill->ill_lmod_cnt = 0;
10279 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
10280 		ill->ill_lmod_rq = RD(dwq);
10281 		for (; dwq != NULL; dwq = dwq->q_next)
10282 			ill->ill_lmod_cnt++;
10283 	}
10284 
10285 	if (doconsist)
10286 		ill->ill_ip_muxid = islink ? li->l_index : 0;
10287 
10288 	/*
10289 	 * If there's at least one up ipif on this ill, then we're bound to
10290 	 * the underlying driver via DLPI.  In that case, renegotiate
10291 	 * capabilities to account for any possible change in modules
10292 	 * interposed between IP and the driver.
10293 	 */
10294 	if (ill->ill_ipif_up_count > 0) {
10295 		if (islink)
10296 			ill_capability_probe(ill);
10297 		else
10298 			ill_capability_reset(ill);
10299 	}
10300 
10301 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10302 	return (0);
10303 }
10304 
10305 /*
10306  * Search the ioctl command in the ioctl tables and return a pointer
10307  * to the ioctl command information. The ioctl command tables are
10308  * static and fully populated at compile time.
10309  */
10310 ip_ioctl_cmd_t *
10311 ip_sioctl_lookup(int ioc_cmd)
10312 {
10313 	int index;
10314 	ip_ioctl_cmd_t *ipip;
10315 	ip_ioctl_cmd_t *ipip_end;
10316 
10317 	if (ioc_cmd == IPI_DONTCARE)
10318 		return (NULL);
10319 
10320 	/*
10321 	 * Do a 2 step search. First search the indexed table
10322 	 * based on the least significant byte of the ioctl cmd.
10323 	 * If we don't find a match, then search the misc table
10324 	 * serially.
10325 	 */
10326 	index = ioc_cmd & 0xFF;
10327 	if (index < ip_ndx_ioctl_count) {
10328 		ipip = &ip_ndx_ioctl_table[index];
10329 		if (ipip->ipi_cmd == ioc_cmd) {
10330 			/* Found a match in the ndx table */
10331 			return (ipip);
10332 		}
10333 	}
10334 
10335 	/* Search the misc table */
10336 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10337 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10338 		if (ipip->ipi_cmd == ioc_cmd)
10339 			/* Found a match in the misc table */
10340 			return (ipip);
10341 	}
10342 
10343 	return (NULL);
10344 }
10345 
10346 /*
10347  * Wrapper function for resuming deferred ioctl processing
10348  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10349  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10350  */
10351 /* ARGSUSED */
10352 void
10353 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10354     void *dummy_arg)
10355 {
10356 	ip_sioctl_copyin_setup(q, mp);
10357 }
10358 
10359 /*
10360  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10361  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10362  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10363  * We establish here the size of the block to be copied in.  mi_copyin
10364  * arranges for this to happen, an processing continues in ip_wput with
10365  * an M_IOCDATA message.
10366  */
10367 void
10368 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10369 {
10370 	int	copyin_size;
10371 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10372 	ip_ioctl_cmd_t *ipip;
10373 	cred_t *cr;
10374 	ip_stack_t	*ipst;
10375 
10376 	if (CONN_Q(q))
10377 		ipst = CONNQ_TO_IPST(q);
10378 	else
10379 		ipst = ILLQ_TO_IPST(q);
10380 
10381 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10382 	if (ipip == NULL) {
10383 		/*
10384 		 * The ioctl is not one we understand or own.
10385 		 * Pass it along to be processed down stream,
10386 		 * if this is a module instance of IP, else nak
10387 		 * the ioctl.
10388 		 */
10389 		if (q->q_next == NULL) {
10390 			goto nak;
10391 		} else {
10392 			putnext(q, mp);
10393 			return;
10394 		}
10395 	}
10396 
10397 	/*
10398 	 * If this is deferred, then we will do all the checks when we
10399 	 * come back.
10400 	 */
10401 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10402 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10403 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10404 		return;
10405 	}
10406 
10407 	/*
10408 	 * Only allow a very small subset of IP ioctls on this stream if
10409 	 * IP is a module and not a driver. Allowing ioctls to be processed
10410 	 * in this case may cause assert failures or data corruption.
10411 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10412 	 * ioctls allowed on an IP module stream, after which this stream
10413 	 * normally becomes a multiplexor (at which time the stream head
10414 	 * will fail all ioctls).
10415 	 */
10416 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10417 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10418 			/*
10419 			 * Pass common Streams ioctls which the IP
10420 			 * module does not own or consume along to
10421 			 * be processed down stream.
10422 			 */
10423 			putnext(q, mp);
10424 			return;
10425 		} else {
10426 			goto nak;
10427 		}
10428 	}
10429 
10430 	/* Make sure we have ioctl data to process. */
10431 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10432 		goto nak;
10433 
10434 	/*
10435 	 * Prefer dblk credential over ioctl credential; some synthesized
10436 	 * ioctls have kcred set because there's no way to crhold()
10437 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10438 	 * the framework; the caller of ioctl needs to hold the reference
10439 	 * for the duration of the call).
10440 	 */
10441 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10442 
10443 	/* Make sure normal users don't send down privileged ioctls */
10444 	if ((ipip->ipi_flags & IPI_PRIV) &&
10445 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10446 		/* We checked the privilege earlier but log it here */
10447 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10448 		return;
10449 	}
10450 
10451 	/*
10452 	 * The ioctl command tables can only encode fixed length
10453 	 * ioctl data. If the length is variable, the table will
10454 	 * encode the length as zero. Such special cases are handled
10455 	 * below in the switch.
10456 	 */
10457 	if (ipip->ipi_copyin_size != 0) {
10458 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10459 		return;
10460 	}
10461 
10462 	switch (iocp->ioc_cmd) {
10463 	case O_SIOCGIFCONF:
10464 	case SIOCGIFCONF:
10465 		/*
10466 		 * This IOCTL is hilarious.  See comments in
10467 		 * ip_sioctl_get_ifconf for the story.
10468 		 */
10469 		if (iocp->ioc_count == TRANSPARENT)
10470 			copyin_size = SIZEOF_STRUCT(ifconf,
10471 			    iocp->ioc_flag);
10472 		else
10473 			copyin_size = iocp->ioc_count;
10474 		mi_copyin(q, mp, NULL, copyin_size);
10475 		return;
10476 
10477 	case O_SIOCGLIFCONF:
10478 	case SIOCGLIFCONF:
10479 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10480 		mi_copyin(q, mp, NULL, copyin_size);
10481 		return;
10482 
10483 	case SIOCGLIFSRCOF:
10484 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10485 		mi_copyin(q, mp, NULL, copyin_size);
10486 		return;
10487 	case SIOCGIP6ADDRPOLICY:
10488 		ip_sioctl_ip6addrpolicy(q, mp);
10489 		ip6_asp_table_refrele(ipst);
10490 		return;
10491 
10492 	case SIOCSIP6ADDRPOLICY:
10493 		ip_sioctl_ip6addrpolicy(q, mp);
10494 		return;
10495 
10496 	case SIOCGDSTINFO:
10497 		ip_sioctl_dstinfo(q, mp);
10498 		ip6_asp_table_refrele(ipst);
10499 		return;
10500 
10501 	case I_PLINK:
10502 	case I_PUNLINK:
10503 	case I_LINK:
10504 	case I_UNLINK:
10505 		/*
10506 		 * We treat non-persistent link similarly as the persistent
10507 		 * link case, in terms of plumbing/unplumbing, as well as
10508 		 * dynamic re-plumbing events indicator.  See comments
10509 		 * in ip_sioctl_plink() for more.
10510 		 *
10511 		 * Request can be enqueued in the 'ipsq' while waiting
10512 		 * to become exclusive. So bump up the conn ref.
10513 		 */
10514 		if (CONN_Q(q))
10515 			CONN_INC_REF(Q_TO_CONN(q));
10516 		ip_sioctl_plink(NULL, q, mp, NULL);
10517 		return;
10518 
10519 	case ND_GET:
10520 	case ND_SET:
10521 		/*
10522 		 * Use of the nd table requires holding the reader lock.
10523 		 * Modifying the nd table thru nd_load/nd_unload requires
10524 		 * the writer lock.
10525 		 */
10526 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10527 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10528 			rw_exit(&ipst->ips_ip_g_nd_lock);
10529 
10530 			if (iocp->ioc_error)
10531 				iocp->ioc_count = 0;
10532 			mp->b_datap->db_type = M_IOCACK;
10533 			qreply(q, mp);
10534 			return;
10535 		}
10536 		rw_exit(&ipst->ips_ip_g_nd_lock);
10537 		/*
10538 		 * We don't understand this subioctl of ND_GET / ND_SET.
10539 		 * Maybe intended for some driver / module below us
10540 		 */
10541 		if (q->q_next) {
10542 			putnext(q, mp);
10543 		} else {
10544 			iocp->ioc_error = ENOENT;
10545 			mp->b_datap->db_type = M_IOCNAK;
10546 			iocp->ioc_count = 0;
10547 			qreply(q, mp);
10548 		}
10549 		return;
10550 
10551 	case IP_IOCTL:
10552 		ip_wput_ioctl(q, mp);
10553 		return;
10554 	default:
10555 		cmn_err(CE_PANIC, "should not happen ");
10556 	}
10557 nak:
10558 	if (mp->b_cont != NULL) {
10559 		freemsg(mp->b_cont);
10560 		mp->b_cont = NULL;
10561 	}
10562 	iocp->ioc_error = EINVAL;
10563 	mp->b_datap->db_type = M_IOCNAK;
10564 	iocp->ioc_count = 0;
10565 	qreply(q, mp);
10566 }
10567 
10568 /* ip_wput hands off ARP IOCTL responses to us */
10569 void
10570 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10571 {
10572 	struct arpreq *ar;
10573 	struct xarpreq *xar;
10574 	area_t	*area;
10575 	mblk_t	*area_mp;
10576 	struct iocblk *iocp;
10577 	mblk_t	*orig_ioc_mp, *tmp;
10578 	struct iocblk	*orig_iocp;
10579 	ill_t *ill;
10580 	conn_t *connp = NULL;
10581 	uint_t ioc_id;
10582 	mblk_t *pending_mp;
10583 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10584 	int *flagsp;
10585 	char *storage = NULL;
10586 	sin_t *sin;
10587 	ipaddr_t addr;
10588 	int err;
10589 	ip_stack_t *ipst;
10590 
10591 	ill = q->q_ptr;
10592 	ASSERT(ill != NULL);
10593 	ipst = ill->ill_ipst;
10594 
10595 	/*
10596 	 * We should get back from ARP a packet chain that looks like:
10597 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10598 	 */
10599 	if (!(area_mp = mp->b_cont) ||
10600 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10601 	    !(orig_ioc_mp = area_mp->b_cont) ||
10602 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10603 		freemsg(mp);
10604 		return;
10605 	}
10606 
10607 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10608 
10609 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10610 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10611 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10612 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10613 		x_arp_ioctl = B_TRUE;
10614 		xar = (struct xarpreq *)tmp->b_rptr;
10615 		sin = (sin_t *)&xar->xarp_pa;
10616 		flagsp = &xar->xarp_flags;
10617 		storage = xar->xarp_ha.sdl_data;
10618 		if (xar->xarp_ha.sdl_nlen != 0)
10619 			ifx_arp_ioctl = B_TRUE;
10620 	} else {
10621 		ar = (struct arpreq *)tmp->b_rptr;
10622 		sin = (sin_t *)&ar->arp_pa;
10623 		flagsp = &ar->arp_flags;
10624 		storage = ar->arp_ha.sa_data;
10625 	}
10626 
10627 	iocp = (struct iocblk *)mp->b_rptr;
10628 
10629 	/*
10630 	 * Pick out the originating queue based on the ioc_id.
10631 	 */
10632 	ioc_id = iocp->ioc_id;
10633 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10634 	if (pending_mp == NULL) {
10635 		ASSERT(connp == NULL);
10636 		inet_freemsg(mp);
10637 		return;
10638 	}
10639 	ASSERT(connp != NULL);
10640 	q = CONNP_TO_WQ(connp);
10641 
10642 	/* Uncouple the internally generated IOCTL from the original one */
10643 	area = (area_t *)area_mp->b_rptr;
10644 	area_mp->b_cont = NULL;
10645 
10646 	/*
10647 	 * Restore the b_next and b_prev used by mi code. This is needed
10648 	 * to complete the ioctl using mi* functions. We stored them in
10649 	 * the pending mp prior to sending the request to ARP.
10650 	 */
10651 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10652 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10653 	inet_freemsg(pending_mp);
10654 
10655 	/*
10656 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10657 	 * Catch the case where there is an IRE_CACHE by no entry in the
10658 	 * arp table.
10659 	 */
10660 	addr = sin->sin_addr.s_addr;
10661 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10662 		ire_t			*ire;
10663 		dl_unitdata_req_t	*dlup;
10664 		mblk_t			*llmp;
10665 		int			addr_len;
10666 		ill_t			*ipsqill = NULL;
10667 
10668 		if (ifx_arp_ioctl) {
10669 			/*
10670 			 * There's no need to lookup the ill, since
10671 			 * we've already done that when we started
10672 			 * processing the ioctl and sent the message
10673 			 * to ARP on that ill.  So use the ill that
10674 			 * is stored in q->q_ptr.
10675 			 */
10676 			ipsqill = ill;
10677 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10678 			    ipsqill->ill_ipif, ALL_ZONES,
10679 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10680 		} else {
10681 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10682 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10683 			if (ire != NULL)
10684 				ipsqill = ire_to_ill(ire);
10685 		}
10686 
10687 		if ((x_arp_ioctl) && (ipsqill != NULL))
10688 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10689 
10690 		if (ire != NULL) {
10691 			/*
10692 			 * Since the ire obtained from cachetable is used for
10693 			 * mac addr copying below, treat an incomplete ire as if
10694 			 * as if we never found it.
10695 			 */
10696 			if (ire->ire_nce != NULL &&
10697 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10698 				ire_refrele(ire);
10699 				ire = NULL;
10700 				ipsqill = NULL;
10701 				goto errack;
10702 			}
10703 			*flagsp = ATF_INUSE;
10704 			llmp = (ire->ire_nce != NULL ?
10705 			    ire->ire_nce->nce_res_mp : NULL);
10706 			if (llmp != NULL && ipsqill != NULL) {
10707 				uchar_t *macaddr;
10708 
10709 				addr_len = ipsqill->ill_phys_addr_length;
10710 				if (x_arp_ioctl && ((addr_len +
10711 				    ipsqill->ill_name_length) >
10712 				    sizeof (xar->xarp_ha.sdl_data))) {
10713 					ire_refrele(ire);
10714 					freemsg(mp);
10715 					ip_ioctl_finish(q, orig_ioc_mp,
10716 					    EINVAL, NO_COPYOUT, NULL);
10717 					return;
10718 				}
10719 				*flagsp |= ATF_COM;
10720 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10721 				if (ipsqill->ill_sap_length < 0)
10722 					macaddr = llmp->b_rptr +
10723 					    dlup->dl_dest_addr_offset;
10724 				else
10725 					macaddr = llmp->b_rptr +
10726 					    dlup->dl_dest_addr_offset +
10727 					    ipsqill->ill_sap_length;
10728 				/*
10729 				 * For SIOCGARP, MAC address length
10730 				 * validation has already been done
10731 				 * before the ioctl was issued to ARP to
10732 				 * allow it to progress only on 6 byte
10733 				 * addressable (ethernet like) media. Thus
10734 				 * the mac address copying can not overwrite
10735 				 * the sa_data area below.
10736 				 */
10737 				bcopy(macaddr, storage, addr_len);
10738 			}
10739 			/* Ditch the internal IOCTL. */
10740 			freemsg(mp);
10741 			ire_refrele(ire);
10742 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10743 			return;
10744 		}
10745 	}
10746 
10747 	/*
10748 	 * Delete the coresponding IRE_CACHE if any.
10749 	 * Reset the error if there was one (in case there was no entry
10750 	 * in arp.)
10751 	 */
10752 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10753 		ipif_t *ipintf = NULL;
10754 
10755 		if (ifx_arp_ioctl) {
10756 			/*
10757 			 * There's no need to lookup the ill, since
10758 			 * we've already done that when we started
10759 			 * processing the ioctl and sent the message
10760 			 * to ARP on that ill.  So use the ill that
10761 			 * is stored in q->q_ptr.
10762 			 */
10763 			ipintf = ill->ill_ipif;
10764 		}
10765 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10766 			/*
10767 			 * The address in "addr" may be an entry for a
10768 			 * router. If that's true, then any off-net
10769 			 * IRE_CACHE entries that go through the router
10770 			 * with address "addr" must be clobbered. Use
10771 			 * ire_walk to achieve this goal.
10772 			 */
10773 			if (ifx_arp_ioctl)
10774 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10775 				    ire_delete_cache_gw, (char *)&addr, ill);
10776 			else
10777 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10778 				    ALL_ZONES, ipst);
10779 			iocp->ioc_error = 0;
10780 		}
10781 	}
10782 errack:
10783 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10784 		err = iocp->ioc_error;
10785 		freemsg(mp);
10786 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10787 		return;
10788 	}
10789 
10790 	/*
10791 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10792 	 * the area_t into the struct {x}arpreq.
10793 	 */
10794 	if (x_arp_ioctl) {
10795 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10796 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10797 		    sizeof (xar->xarp_ha.sdl_data)) {
10798 			freemsg(mp);
10799 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10800 			    NULL);
10801 			return;
10802 		}
10803 	}
10804 	*flagsp = ATF_INUSE;
10805 	if (area->area_flags & ACE_F_PERMANENT)
10806 		*flagsp |= ATF_PERM;
10807 	if (area->area_flags & ACE_F_PUBLISH)
10808 		*flagsp |= ATF_PUBL;
10809 	if (area->area_flags & ACE_F_AUTHORITY)
10810 		*flagsp |= ATF_AUTHORITY;
10811 	if (area->area_hw_addr_length != 0) {
10812 		*flagsp |= ATF_COM;
10813 		/*
10814 		 * For SIOCGARP, MAC address length validation has
10815 		 * already been done before the ioctl was issued to ARP
10816 		 * to allow it to progress only on 6 byte addressable
10817 		 * (ethernet like) media. Thus the mac address copying
10818 		 * can not overwrite the sa_data area below.
10819 		 */
10820 		bcopy((char *)area + area->area_hw_addr_offset,
10821 		    storage, area->area_hw_addr_length);
10822 	}
10823 
10824 	/* Ditch the internal IOCTL. */
10825 	freemsg(mp);
10826 	/* Complete the original. */
10827 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10828 }
10829 
10830 /*
10831  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10832  * interface) create the next available logical interface for this
10833  * physical interface.
10834  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10835  * ipif with the specified name.
10836  *
10837  * If the address family is not AF_UNSPEC then set the address as well.
10838  *
10839  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10840  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10841  *
10842  * Executed as a writer on the ill or ill group.
10843  * So no lock is needed to traverse the ipif chain, or examine the
10844  * phyint flags.
10845  */
10846 /* ARGSUSED */
10847 int
10848 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10849     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10850 {
10851 	mblk_t	*mp1;
10852 	struct lifreq *lifr;
10853 	boolean_t	isv6;
10854 	boolean_t	exists;
10855 	char 	*name;
10856 	char	*endp;
10857 	char	*cp;
10858 	int	namelen;
10859 	ipif_t	*ipif;
10860 	long	id;
10861 	ipsq_t	*ipsq;
10862 	ill_t	*ill;
10863 	sin_t	*sin;
10864 	int	err = 0;
10865 	boolean_t found_sep = B_FALSE;
10866 	conn_t	*connp;
10867 	zoneid_t zoneid;
10868 	int	orig_ifindex = 0;
10869 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10870 
10871 	ASSERT(q->q_next == NULL);
10872 	ip1dbg(("ip_sioctl_addif\n"));
10873 	/* Existence of mp1 has been checked in ip_wput_nondata */
10874 	mp1 = mp->b_cont->b_cont;
10875 	/*
10876 	 * Null terminate the string to protect against buffer
10877 	 * overrun. String was generated by user code and may not
10878 	 * be trusted.
10879 	 */
10880 	lifr = (struct lifreq *)mp1->b_rptr;
10881 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10882 	name = lifr->lifr_name;
10883 	ASSERT(CONN_Q(q));
10884 	connp = Q_TO_CONN(q);
10885 	isv6 = connp->conn_af_isv6;
10886 	zoneid = connp->conn_zoneid;
10887 	namelen = mi_strlen(name);
10888 	if (namelen == 0)
10889 		return (EINVAL);
10890 
10891 	exists = B_FALSE;
10892 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10893 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10894 		/*
10895 		 * Allow creating lo0 using SIOCLIFADDIF.
10896 		 * can't be any other writer thread. So can pass null below
10897 		 * for the last 4 args to ipif_lookup_name.
10898 		 */
10899 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10900 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10901 		/* Prevent any further action */
10902 		if (ipif == NULL) {
10903 			return (ENOBUFS);
10904 		} else if (!exists) {
10905 			/* We created the ipif now and as writer */
10906 			ipif_refrele(ipif);
10907 			return (0);
10908 		} else {
10909 			ill = ipif->ipif_ill;
10910 			ill_refhold(ill);
10911 			ipif_refrele(ipif);
10912 		}
10913 	} else {
10914 		/* Look for a colon in the name. */
10915 		endp = &name[namelen];
10916 		for (cp = endp; --cp > name; ) {
10917 			if (*cp == IPIF_SEPARATOR_CHAR) {
10918 				found_sep = B_TRUE;
10919 				/*
10920 				 * Reject any non-decimal aliases for plumbing
10921 				 * of logical interfaces. Aliases with leading
10922 				 * zeroes are also rejected as they introduce
10923 				 * ambiguity in the naming of the interfaces.
10924 				 * Comparing with "0" takes care of all such
10925 				 * cases.
10926 				 */
10927 				if ((strncmp("0", cp+1, 1)) == 0)
10928 					return (EINVAL);
10929 
10930 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10931 				    id <= 0 || *endp != '\0') {
10932 					return (EINVAL);
10933 				}
10934 				*cp = '\0';
10935 				break;
10936 			}
10937 		}
10938 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10939 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10940 		if (found_sep)
10941 			*cp = IPIF_SEPARATOR_CHAR;
10942 		if (ill == NULL)
10943 			return (err);
10944 	}
10945 
10946 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10947 	    B_TRUE);
10948 
10949 	/*
10950 	 * Release the refhold due to the lookup, now that we are excl
10951 	 * or we are just returning
10952 	 */
10953 	ill_refrele(ill);
10954 
10955 	if (ipsq == NULL)
10956 		return (EINPROGRESS);
10957 
10958 	/*
10959 	 * If the interface is failed, inactive or offlined, look for a working
10960 	 * interface in the ill group and create the ipif there. If we can't
10961 	 * find a good interface, create the ipif anyway so that in.mpathd can
10962 	 * move it to the first repaired interface.
10963 	 */
10964 	if ((ill->ill_phyint->phyint_flags &
10965 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10966 	    ill->ill_phyint->phyint_groupname_len != 0) {
10967 		phyint_t *phyi;
10968 		char *groupname = ill->ill_phyint->phyint_groupname;
10969 
10970 		/*
10971 		 * We're looking for a working interface, but it doesn't matter
10972 		 * if it's up or down; so instead of following the group lists,
10973 		 * we look at each physical interface and compare the groupname.
10974 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10975 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10976 		 * Otherwise we create the ipif on the failed interface.
10977 		 */
10978 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10979 		phyi = avl_first(&ipst->ips_phyint_g_list->
10980 		    phyint_list_avl_by_index);
10981 		for (; phyi != NULL;
10982 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10983 		    phyint_list_avl_by_index,
10984 		    phyi, AVL_AFTER)) {
10985 			if (phyi->phyint_groupname_len == 0)
10986 				continue;
10987 			ASSERT(phyi->phyint_groupname != NULL);
10988 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10989 			    !(phyi->phyint_flags &
10990 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10991 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10992 			    (phyi->phyint_illv4 != NULL))) {
10993 				break;
10994 			}
10995 		}
10996 		rw_exit(&ipst->ips_ill_g_lock);
10997 
10998 		if (phyi != NULL) {
10999 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
11000 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
11001 			    phyi->phyint_illv4);
11002 		}
11003 	}
11004 
11005 	/*
11006 	 * We are now exclusive on the ipsq, so an ill move will be serialized
11007 	 * before or after us.
11008 	 */
11009 	ASSERT(IAM_WRITER_ILL(ill));
11010 	ASSERT(ill->ill_move_in_progress == B_FALSE);
11011 
11012 	if (found_sep && orig_ifindex == 0) {
11013 		/* Now see if there is an IPIF with this unit number. */
11014 		for (ipif = ill->ill_ipif; ipif != NULL;
11015 		    ipif = ipif->ipif_next) {
11016 			if (ipif->ipif_id == id) {
11017 				err = EEXIST;
11018 				goto done;
11019 			}
11020 		}
11021 	}
11022 
11023 	/*
11024 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
11025 	 * of lo0. We never come here when we plumb lo0:0. It
11026 	 * happens in ipif_lookup_on_name.
11027 	 * The specified unit number is ignored when we create the ipif on a
11028 	 * different interface. However, we save it in ipif_orig_ipifid below so
11029 	 * that the ipif fails back to the right position.
11030 	 */
11031 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
11032 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
11033 		err = ENOBUFS;
11034 		goto done;
11035 	}
11036 
11037 	/* Return created name with ioctl */
11038 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
11039 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
11040 	ip1dbg(("created %s\n", lifr->lifr_name));
11041 
11042 	/* Set address */
11043 	sin = (sin_t *)&lifr->lifr_addr;
11044 	if (sin->sin_family != AF_UNSPEC) {
11045 		err = ip_sioctl_addr(ipif, sin, q, mp,
11046 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
11047 	}
11048 
11049 	/* Set ifindex and unit number for failback */
11050 	if (err == 0 && orig_ifindex != 0) {
11051 		ipif->ipif_orig_ifindex = orig_ifindex;
11052 		if (found_sep) {
11053 			ipif->ipif_orig_ipifid = id;
11054 		}
11055 	}
11056 
11057 done:
11058 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
11059 	return (err);
11060 }
11061 
11062 /*
11063  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
11064  * interface) delete it based on the IP address (on this physical interface).
11065  * Otherwise delete it based on the ipif_id.
11066  * Also, special handling to allow a removeif of lo0.
11067  */
11068 /* ARGSUSED */
11069 int
11070 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11071     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11072 {
11073 	conn_t		*connp;
11074 	ill_t		*ill = ipif->ipif_ill;
11075 	boolean_t	 success;
11076 	ip_stack_t	*ipst;
11077 
11078 	ipst = CONNQ_TO_IPST(q);
11079 
11080 	ASSERT(q->q_next == NULL);
11081 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
11082 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11083 	ASSERT(IAM_WRITER_IPIF(ipif));
11084 
11085 	connp = Q_TO_CONN(q);
11086 	/*
11087 	 * Special case for unplumbing lo0 (the loopback physical interface).
11088 	 * If unplumbing lo0, the incoming address structure has been
11089 	 * initialized to all zeros. When unplumbing lo0, all its logical
11090 	 * interfaces must be removed too.
11091 	 *
11092 	 * Note that this interface may be called to remove a specific
11093 	 * loopback logical interface (eg, lo0:1). But in that case
11094 	 * ipif->ipif_id != 0 so that the code path for that case is the
11095 	 * same as any other interface (meaning it skips the code directly
11096 	 * below).
11097 	 */
11098 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11099 		if (sin->sin_family == AF_UNSPEC &&
11100 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
11101 			/*
11102 			 * Mark it condemned. No new ref. will be made to ill.
11103 			 */
11104 			mutex_enter(&ill->ill_lock);
11105 			ill->ill_state_flags |= ILL_CONDEMNED;
11106 			for (ipif = ill->ill_ipif; ipif != NULL;
11107 			    ipif = ipif->ipif_next) {
11108 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
11109 			}
11110 			mutex_exit(&ill->ill_lock);
11111 
11112 			ipif = ill->ill_ipif;
11113 			/* unplumb the loopback interface */
11114 			ill_delete(ill);
11115 			mutex_enter(&connp->conn_lock);
11116 			mutex_enter(&ill->ill_lock);
11117 			ASSERT(ill->ill_group == NULL);
11118 
11119 			/* Are any references to this ill active */
11120 			if (ill_is_quiescent(ill)) {
11121 				mutex_exit(&ill->ill_lock);
11122 				mutex_exit(&connp->conn_lock);
11123 				ill_delete_tail(ill);
11124 				mi_free(ill);
11125 				return (0);
11126 			}
11127 			success = ipsq_pending_mp_add(connp, ipif,
11128 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
11129 			mutex_exit(&connp->conn_lock);
11130 			mutex_exit(&ill->ill_lock);
11131 			if (success)
11132 				return (EINPROGRESS);
11133 			else
11134 				return (EINTR);
11135 		}
11136 	}
11137 
11138 	/*
11139 	 * We are exclusive on the ipsq, so an ill move will be serialized
11140 	 * before or after us.
11141 	 */
11142 	ASSERT(ill->ill_move_in_progress == B_FALSE);
11143 
11144 	if (ipif->ipif_id == 0) {
11145 		/* Find based on address */
11146 		if (ipif->ipif_isv6) {
11147 			sin6_t *sin6;
11148 
11149 			if (sin->sin_family != AF_INET6)
11150 				return (EAFNOSUPPORT);
11151 
11152 			sin6 = (sin6_t *)sin;
11153 			/* We are a writer, so we should be able to lookup */
11154 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
11155 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
11156 			if (ipif == NULL) {
11157 				/*
11158 				 * Maybe the address in on another interface in
11159 				 * the same IPMP group? We check this below.
11160 				 */
11161 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
11162 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
11163 				    ipst);
11164 			}
11165 		} else {
11166 			ipaddr_t addr;
11167 
11168 			if (sin->sin_family != AF_INET)
11169 				return (EAFNOSUPPORT);
11170 
11171 			addr = sin->sin_addr.s_addr;
11172 			/* We are a writer, so we should be able to lookup */
11173 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
11174 			    NULL, NULL, NULL, ipst);
11175 			if (ipif == NULL) {
11176 				/*
11177 				 * Maybe the address in on another interface in
11178 				 * the same IPMP group? We check this below.
11179 				 */
11180 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
11181 				    NULL, NULL, NULL, NULL, ipst);
11182 			}
11183 		}
11184 		if (ipif == NULL) {
11185 			return (EADDRNOTAVAIL);
11186 		}
11187 		/*
11188 		 * When the address to be removed is hosted on a different
11189 		 * interface, we check if the interface is in the same IPMP
11190 		 * group as the specified one; if so we proceed with the
11191 		 * removal.
11192 		 * ill->ill_group is NULL when the ill is down, so we have to
11193 		 * compare the group names instead.
11194 		 */
11195 		if (ipif->ipif_ill != ill &&
11196 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
11197 		    ill->ill_phyint->phyint_groupname_len == 0 ||
11198 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
11199 		    ill->ill_phyint->phyint_groupname) != 0)) {
11200 			ipif_refrele(ipif);
11201 			return (EADDRNOTAVAIL);
11202 		}
11203 
11204 		/* This is a writer */
11205 		ipif_refrele(ipif);
11206 	}
11207 
11208 	/*
11209 	 * Can not delete instance zero since it is tied to the ill.
11210 	 */
11211 	if (ipif->ipif_id == 0)
11212 		return (EBUSY);
11213 
11214 	mutex_enter(&ill->ill_lock);
11215 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
11216 	mutex_exit(&ill->ill_lock);
11217 
11218 	ipif_free(ipif);
11219 
11220 	mutex_enter(&connp->conn_lock);
11221 	mutex_enter(&ill->ill_lock);
11222 
11223 	/* Are any references to this ipif active */
11224 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
11225 		mutex_exit(&ill->ill_lock);
11226 		mutex_exit(&connp->conn_lock);
11227 		ipif_non_duplicate(ipif);
11228 		ipif_down_tail(ipif);
11229 		ipif_free_tail(ipif);
11230 		return (0);
11231 	}
11232 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
11233 	    IPIF_FREE);
11234 	mutex_exit(&ill->ill_lock);
11235 	mutex_exit(&connp->conn_lock);
11236 	if (success)
11237 		return (EINPROGRESS);
11238 	else
11239 		return (EINTR);
11240 }
11241 
11242 /*
11243  * Restart the removeif ioctl. The refcnt has gone down to 0.
11244  * The ipif is already condemned. So can't find it thru lookups.
11245  */
11246 /* ARGSUSED */
11247 int
11248 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
11249     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11250 {
11251 	ill_t *ill;
11252 
11253 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
11254 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11255 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11256 		ill = ipif->ipif_ill;
11257 		ASSERT(IAM_WRITER_ILL(ill));
11258 		ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) &&
11259 		    (ill->ill_state_flags & IPIF_CONDEMNED));
11260 		ill_delete_tail(ill);
11261 		mi_free(ill);
11262 		return (0);
11263 	}
11264 
11265 	ill = ipif->ipif_ill;
11266 	ASSERT(IAM_WRITER_IPIF(ipif));
11267 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
11268 
11269 	ipif_non_duplicate(ipif);
11270 	ipif_down_tail(ipif);
11271 	ipif_free_tail(ipif);
11272 
11273 	ILL_UNMARK_CHANGING(ill);
11274 	return (0);
11275 }
11276 
11277 /*
11278  * Set the local interface address.
11279  * Allow an address of all zero when the interface is down.
11280  */
11281 /* ARGSUSED */
11282 int
11283 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11284     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11285 {
11286 	int err = 0;
11287 	in6_addr_t v6addr;
11288 	boolean_t need_up = B_FALSE;
11289 
11290 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11291 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11292 
11293 	ASSERT(IAM_WRITER_IPIF(ipif));
11294 
11295 	if (ipif->ipif_isv6) {
11296 		sin6_t *sin6;
11297 		ill_t *ill;
11298 		phyint_t *phyi;
11299 
11300 		if (sin->sin_family != AF_INET6)
11301 			return (EAFNOSUPPORT);
11302 
11303 		sin6 = (sin6_t *)sin;
11304 		v6addr = sin6->sin6_addr;
11305 		ill = ipif->ipif_ill;
11306 		phyi = ill->ill_phyint;
11307 
11308 		/*
11309 		 * Enforce that true multicast interfaces have a link-local
11310 		 * address for logical unit 0.
11311 		 */
11312 		if (ipif->ipif_id == 0 &&
11313 		    (ill->ill_flags & ILLF_MULTICAST) &&
11314 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11315 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11316 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11317 			return (EADDRNOTAVAIL);
11318 		}
11319 
11320 		/*
11321 		 * up interfaces shouldn't have the unspecified address
11322 		 * unless they also have the IPIF_NOLOCAL flags set and
11323 		 * have a subnet assigned.
11324 		 */
11325 		if ((ipif->ipif_flags & IPIF_UP) &&
11326 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11327 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11328 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11329 			return (EADDRNOTAVAIL);
11330 		}
11331 
11332 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11333 			return (EADDRNOTAVAIL);
11334 	} else {
11335 		ipaddr_t addr;
11336 
11337 		if (sin->sin_family != AF_INET)
11338 			return (EAFNOSUPPORT);
11339 
11340 		addr = sin->sin_addr.s_addr;
11341 
11342 		/* Allow 0 as the local address. */
11343 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11344 			return (EADDRNOTAVAIL);
11345 
11346 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11347 	}
11348 
11349 
11350 	/*
11351 	 * Even if there is no change we redo things just to rerun
11352 	 * ipif_set_default.
11353 	 */
11354 	if (ipif->ipif_flags & IPIF_UP) {
11355 		/*
11356 		 * Setting a new local address, make sure
11357 		 * we have net and subnet bcast ire's for
11358 		 * the old address if we need them.
11359 		 */
11360 		if (!ipif->ipif_isv6)
11361 			ipif_check_bcast_ires(ipif);
11362 		/*
11363 		 * If the interface is already marked up,
11364 		 * we call ipif_down which will take care
11365 		 * of ditching any IREs that have been set
11366 		 * up based on the old interface address.
11367 		 */
11368 		err = ipif_logical_down(ipif, q, mp);
11369 		if (err == EINPROGRESS)
11370 			return (err);
11371 		ipif_down_tail(ipif);
11372 		need_up = 1;
11373 	}
11374 
11375 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11376 	return (err);
11377 }
11378 
11379 int
11380 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11381     boolean_t need_up)
11382 {
11383 	in6_addr_t v6addr;
11384 	in6_addr_t ov6addr;
11385 	ipaddr_t addr;
11386 	sin6_t	*sin6;
11387 	int	sinlen;
11388 	int	err = 0;
11389 	ill_t	*ill = ipif->ipif_ill;
11390 	boolean_t need_dl_down;
11391 	boolean_t need_arp_down;
11392 	struct iocblk *iocp;
11393 
11394 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11395 
11396 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11397 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11398 	ASSERT(IAM_WRITER_IPIF(ipif));
11399 
11400 	/* Must cancel any pending timer before taking the ill_lock */
11401 	if (ipif->ipif_recovery_id != 0)
11402 		(void) untimeout(ipif->ipif_recovery_id);
11403 	ipif->ipif_recovery_id = 0;
11404 
11405 	if (ipif->ipif_isv6) {
11406 		sin6 = (sin6_t *)sin;
11407 		v6addr = sin6->sin6_addr;
11408 		sinlen = sizeof (struct sockaddr_in6);
11409 	} else {
11410 		addr = sin->sin_addr.s_addr;
11411 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11412 		sinlen = sizeof (struct sockaddr_in);
11413 	}
11414 	mutex_enter(&ill->ill_lock);
11415 	ov6addr = ipif->ipif_v6lcl_addr;
11416 	ipif->ipif_v6lcl_addr = v6addr;
11417 	sctp_update_ipif_addr(ipif, ov6addr);
11418 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11419 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11420 	} else {
11421 		ipif->ipif_v6src_addr = v6addr;
11422 	}
11423 	ipif->ipif_addr_ready = 0;
11424 
11425 	/*
11426 	 * If the interface was previously marked as a duplicate, then since
11427 	 * we've now got a "new" address, it should no longer be considered a
11428 	 * duplicate -- even if the "new" address is the same as the old one.
11429 	 * Note that if all ipifs are down, we may have a pending ARP down
11430 	 * event to handle.  This is because we want to recover from duplicates
11431 	 * and thus delay tearing down ARP until the duplicates have been
11432 	 * removed or disabled.
11433 	 */
11434 	need_dl_down = need_arp_down = B_FALSE;
11435 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11436 		need_arp_down = !need_up;
11437 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11438 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11439 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11440 			need_dl_down = B_TRUE;
11441 		}
11442 	}
11443 
11444 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11445 	    !ill->ill_is_6to4tun) {
11446 		queue_t *wqp = ill->ill_wq;
11447 
11448 		/*
11449 		 * The local address of this interface is a 6to4 address,
11450 		 * check if this interface is in fact a 6to4 tunnel or just
11451 		 * an interface configured with a 6to4 address.  We are only
11452 		 * interested in the former.
11453 		 */
11454 		if (wqp != NULL) {
11455 			while ((wqp->q_next != NULL) &&
11456 			    (wqp->q_next->q_qinfo != NULL) &&
11457 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11458 
11459 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11460 				    == TUN6TO4_MODID) {
11461 					/* set for use in IP */
11462 					ill->ill_is_6to4tun = 1;
11463 					break;
11464 				}
11465 				wqp = wqp->q_next;
11466 			}
11467 		}
11468 	}
11469 
11470 	ipif_set_default(ipif);
11471 
11472 	/*
11473 	 * When publishing an interface address change event, we only notify
11474 	 * the event listeners of the new address.  It is assumed that if they
11475 	 * actively care about the addresses assigned that they will have
11476 	 * already discovered the previous address assigned (if there was one.)
11477 	 *
11478 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11479 	 */
11480 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11481 		hook_nic_event_t *info;
11482 		if ((info = ipif->ipif_ill->ill_nic_event_info) != NULL) {
11483 			ip2dbg(("ip_sioctl_addr_tail: unexpected nic event %d "
11484 			    "attached for %s\n", info->hne_event,
11485 			    ill->ill_name));
11486 			if (info->hne_data != NULL)
11487 				kmem_free(info->hne_data, info->hne_datalen);
11488 			kmem_free(info, sizeof (hook_nic_event_t));
11489 		}
11490 
11491 		info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
11492 		if (info != NULL) {
11493 			ip_stack_t	*ipst = ill->ill_ipst;
11494 
11495 			info->hne_nic =
11496 			    ipif->ipif_ill->ill_phyint->phyint_hook_ifindex;
11497 			info->hne_lif = MAP_IPIF_ID(ipif->ipif_id);
11498 			info->hne_event = NE_ADDRESS_CHANGE;
11499 			info->hne_family = ipif->ipif_isv6 ?
11500 			    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
11501 			info->hne_data = kmem_alloc(sinlen, KM_NOSLEEP);
11502 			if (info->hne_data != NULL) {
11503 				info->hne_datalen = sinlen;
11504 				bcopy(sin, info->hne_data, sinlen);
11505 			} else {
11506 				ip2dbg(("ip_sioctl_addr_tail: could not attach "
11507 				    "address information for ADDRESS_CHANGE nic"
11508 				    " event of %s (ENOMEM)\n",
11509 				    ipif->ipif_ill->ill_name));
11510 				kmem_free(info, sizeof (hook_nic_event_t));
11511 			}
11512 		} else
11513 			ip2dbg(("ip_sioctl_addr_tail: could not attach "
11514 			    "ADDRESS_CHANGE nic event information for %s "
11515 			    "(ENOMEM)\n", ipif->ipif_ill->ill_name));
11516 
11517 		ipif->ipif_ill->ill_nic_event_info = info;
11518 	}
11519 
11520 	mutex_exit(&ill->ill_lock);
11521 
11522 	if (need_up) {
11523 		/*
11524 		 * Now bring the interface back up.  If this
11525 		 * is the only IPIF for the ILL, ipif_up
11526 		 * will have to re-bind to the device, so
11527 		 * we may get back EINPROGRESS, in which
11528 		 * case, this IOCTL will get completed in
11529 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11530 		 */
11531 		err = ipif_up(ipif, q, mp);
11532 	}
11533 
11534 	if (need_dl_down)
11535 		ill_dl_down(ill);
11536 	if (need_arp_down)
11537 		ipif_arp_down(ipif);
11538 
11539 	return (err);
11540 }
11541 
11542 
11543 /*
11544  * Restart entry point to restart the address set operation after the
11545  * refcounts have dropped to zero.
11546  */
11547 /* ARGSUSED */
11548 int
11549 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11550     ip_ioctl_cmd_t *ipip, void *ifreq)
11551 {
11552 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11553 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11554 	ASSERT(IAM_WRITER_IPIF(ipif));
11555 	ipif_down_tail(ipif);
11556 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11557 }
11558 
11559 /* ARGSUSED */
11560 int
11561 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11562     ip_ioctl_cmd_t *ipip, void *if_req)
11563 {
11564 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11565 	struct lifreq *lifr = (struct lifreq *)if_req;
11566 
11567 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11568 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11569 	/*
11570 	 * The net mask and address can't change since we have a
11571 	 * reference to the ipif. So no lock is necessary.
11572 	 */
11573 	if (ipif->ipif_isv6) {
11574 		*sin6 = sin6_null;
11575 		sin6->sin6_family = AF_INET6;
11576 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11577 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11578 		lifr->lifr_addrlen =
11579 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11580 	} else {
11581 		*sin = sin_null;
11582 		sin->sin_family = AF_INET;
11583 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11584 		if (ipip->ipi_cmd_type == LIF_CMD) {
11585 			lifr->lifr_addrlen =
11586 			    ip_mask_to_plen(ipif->ipif_net_mask);
11587 		}
11588 	}
11589 	return (0);
11590 }
11591 
11592 /*
11593  * Set the destination address for a pt-pt interface.
11594  */
11595 /* ARGSUSED */
11596 int
11597 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11598     ip_ioctl_cmd_t *ipip, void *if_req)
11599 {
11600 	int err = 0;
11601 	in6_addr_t v6addr;
11602 	boolean_t need_up = B_FALSE;
11603 
11604 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11605 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11606 	ASSERT(IAM_WRITER_IPIF(ipif));
11607 
11608 	if (ipif->ipif_isv6) {
11609 		sin6_t *sin6;
11610 
11611 		if (sin->sin_family != AF_INET6)
11612 			return (EAFNOSUPPORT);
11613 
11614 		sin6 = (sin6_t *)sin;
11615 		v6addr = sin6->sin6_addr;
11616 
11617 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11618 			return (EADDRNOTAVAIL);
11619 	} else {
11620 		ipaddr_t addr;
11621 
11622 		if (sin->sin_family != AF_INET)
11623 			return (EAFNOSUPPORT);
11624 
11625 		addr = sin->sin_addr.s_addr;
11626 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11627 			return (EADDRNOTAVAIL);
11628 
11629 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11630 	}
11631 
11632 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11633 		return (0);	/* No change */
11634 
11635 	if (ipif->ipif_flags & IPIF_UP) {
11636 		/*
11637 		 * If the interface is already marked up,
11638 		 * we call ipif_down which will take care
11639 		 * of ditching any IREs that have been set
11640 		 * up based on the old pp dst address.
11641 		 */
11642 		err = ipif_logical_down(ipif, q, mp);
11643 		if (err == EINPROGRESS)
11644 			return (err);
11645 		ipif_down_tail(ipif);
11646 		need_up = B_TRUE;
11647 	}
11648 	/*
11649 	 * could return EINPROGRESS. If so ioctl will complete in
11650 	 * ip_rput_dlpi_writer
11651 	 */
11652 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11653 	return (err);
11654 }
11655 
11656 static int
11657 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11658     boolean_t need_up)
11659 {
11660 	in6_addr_t v6addr;
11661 	ill_t	*ill = ipif->ipif_ill;
11662 	int	err = 0;
11663 	boolean_t need_dl_down;
11664 	boolean_t need_arp_down;
11665 
11666 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11667 	    ipif->ipif_id, (void *)ipif));
11668 
11669 	/* Must cancel any pending timer before taking the ill_lock */
11670 	if (ipif->ipif_recovery_id != 0)
11671 		(void) untimeout(ipif->ipif_recovery_id);
11672 	ipif->ipif_recovery_id = 0;
11673 
11674 	if (ipif->ipif_isv6) {
11675 		sin6_t *sin6;
11676 
11677 		sin6 = (sin6_t *)sin;
11678 		v6addr = sin6->sin6_addr;
11679 	} else {
11680 		ipaddr_t addr;
11681 
11682 		addr = sin->sin_addr.s_addr;
11683 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11684 	}
11685 	mutex_enter(&ill->ill_lock);
11686 	/* Set point to point destination address. */
11687 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11688 		/*
11689 		 * Allow this as a means of creating logical
11690 		 * pt-pt interfaces on top of e.g. an Ethernet.
11691 		 * XXX Undocumented HACK for testing.
11692 		 * pt-pt interfaces are created with NUD disabled.
11693 		 */
11694 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11695 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11696 		if (ipif->ipif_isv6)
11697 			ill->ill_flags |= ILLF_NONUD;
11698 	}
11699 
11700 	/*
11701 	 * If the interface was previously marked as a duplicate, then since
11702 	 * we've now got a "new" address, it should no longer be considered a
11703 	 * duplicate -- even if the "new" address is the same as the old one.
11704 	 * Note that if all ipifs are down, we may have a pending ARP down
11705 	 * event to handle.
11706 	 */
11707 	need_dl_down = need_arp_down = B_FALSE;
11708 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11709 		need_arp_down = !need_up;
11710 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11711 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11712 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11713 			need_dl_down = B_TRUE;
11714 		}
11715 	}
11716 
11717 	/* Set the new address. */
11718 	ipif->ipif_v6pp_dst_addr = v6addr;
11719 	/* Make sure subnet tracks pp_dst */
11720 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11721 	mutex_exit(&ill->ill_lock);
11722 
11723 	if (need_up) {
11724 		/*
11725 		 * Now bring the interface back up.  If this
11726 		 * is the only IPIF for the ILL, ipif_up
11727 		 * will have to re-bind to the device, so
11728 		 * we may get back EINPROGRESS, in which
11729 		 * case, this IOCTL will get completed in
11730 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11731 		 */
11732 		err = ipif_up(ipif, q, mp);
11733 	}
11734 
11735 	if (need_dl_down)
11736 		ill_dl_down(ill);
11737 
11738 	if (need_arp_down)
11739 		ipif_arp_down(ipif);
11740 	return (err);
11741 }
11742 
11743 /*
11744  * Restart entry point to restart the dstaddress set operation after the
11745  * refcounts have dropped to zero.
11746  */
11747 /* ARGSUSED */
11748 int
11749 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11750     ip_ioctl_cmd_t *ipip, void *ifreq)
11751 {
11752 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11753 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11754 	ipif_down_tail(ipif);
11755 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11756 }
11757 
11758 /* ARGSUSED */
11759 int
11760 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11761     ip_ioctl_cmd_t *ipip, void *if_req)
11762 {
11763 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11764 
11765 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11766 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11767 	/*
11768 	 * Get point to point destination address. The addresses can't
11769 	 * change since we hold a reference to the ipif.
11770 	 */
11771 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11772 		return (EADDRNOTAVAIL);
11773 
11774 	if (ipif->ipif_isv6) {
11775 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11776 		*sin6 = sin6_null;
11777 		sin6->sin6_family = AF_INET6;
11778 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11779 	} else {
11780 		*sin = sin_null;
11781 		sin->sin_family = AF_INET;
11782 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11783 	}
11784 	return (0);
11785 }
11786 
11787 /*
11788  * part of ipmp, make this func return the active/inactive state and
11789  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11790  */
11791 /*
11792  * This function either sets or clears the IFF_INACTIVE flag.
11793  *
11794  * As long as there are some addresses or multicast memberships on the
11795  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11796  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11797  * will be used for outbound packets.
11798  *
11799  * Caller needs to verify the validity of setting IFF_INACTIVE.
11800  */
11801 static void
11802 phyint_inactive(phyint_t *phyi)
11803 {
11804 	ill_t *ill_v4;
11805 	ill_t *ill_v6;
11806 	ipif_t *ipif;
11807 	ilm_t *ilm;
11808 
11809 	ill_v4 = phyi->phyint_illv4;
11810 	ill_v6 = phyi->phyint_illv6;
11811 
11812 	/*
11813 	 * No need for a lock while traversing the list since iam
11814 	 * a writer
11815 	 */
11816 	if (ill_v4 != NULL) {
11817 		ASSERT(IAM_WRITER_ILL(ill_v4));
11818 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11819 		    ipif = ipif->ipif_next) {
11820 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11821 				mutex_enter(&phyi->phyint_lock);
11822 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11823 				mutex_exit(&phyi->phyint_lock);
11824 				return;
11825 			}
11826 		}
11827 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11828 		    ilm = ilm->ilm_next) {
11829 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11830 				mutex_enter(&phyi->phyint_lock);
11831 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11832 				mutex_exit(&phyi->phyint_lock);
11833 				return;
11834 			}
11835 		}
11836 	}
11837 	if (ill_v6 != NULL) {
11838 		ill_v6 = phyi->phyint_illv6;
11839 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11840 		    ipif = ipif->ipif_next) {
11841 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11842 				mutex_enter(&phyi->phyint_lock);
11843 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11844 				mutex_exit(&phyi->phyint_lock);
11845 				return;
11846 			}
11847 		}
11848 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11849 		    ilm = ilm->ilm_next) {
11850 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11851 				mutex_enter(&phyi->phyint_lock);
11852 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11853 				mutex_exit(&phyi->phyint_lock);
11854 				return;
11855 			}
11856 		}
11857 	}
11858 	mutex_enter(&phyi->phyint_lock);
11859 	phyi->phyint_flags |= PHYI_INACTIVE;
11860 	mutex_exit(&phyi->phyint_lock);
11861 }
11862 
11863 /*
11864  * This function is called only when the phyint flags change. Currently
11865  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11866  * that we can select a good ill.
11867  */
11868 static void
11869 ip_redo_nomination(phyint_t *phyi)
11870 {
11871 	ill_t *ill_v4;
11872 
11873 	ill_v4 = phyi->phyint_illv4;
11874 
11875 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11876 		ASSERT(IAM_WRITER_ILL(ill_v4));
11877 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11878 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11879 	}
11880 }
11881 
11882 /*
11883  * Heuristic to check if ill is INACTIVE.
11884  * Checks if ill has an ipif with an usable ip address.
11885  *
11886  * Return values:
11887  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11888  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11889  */
11890 static boolean_t
11891 ill_is_inactive(ill_t *ill)
11892 {
11893 	ipif_t *ipif;
11894 
11895 	/* Check whether it is in an IPMP group */
11896 	if (ill->ill_phyint->phyint_groupname == NULL)
11897 		return (B_FALSE);
11898 
11899 	if (ill->ill_ipif_up_count == 0)
11900 		return (B_TRUE);
11901 
11902 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11903 		uint64_t flags = ipif->ipif_flags;
11904 
11905 		/*
11906 		 * This ipif is usable if it is IPIF_UP and not a
11907 		 * dedicated test address.  A dedicated test address
11908 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11909 		 * (note in particular that V6 test addresses are
11910 		 * link-local data addresses and thus are marked
11911 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11912 		 */
11913 		if ((flags & IPIF_UP) &&
11914 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11915 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11916 			return (B_FALSE);
11917 	}
11918 	return (B_TRUE);
11919 }
11920 
11921 /*
11922  * Set interface flags.
11923  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11924  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11925  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11926  *
11927  * NOTE : We really don't enforce that ipif_id zero should be used
11928  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11929  *	  is because applications generally does SICGLIFFLAGS and
11930  *	  ORs in the new flags (that affects the logical) and does a
11931  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11932  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11933  *	  flags that will be turned on is correct with respect to
11934  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11935  */
11936 /* ARGSUSED */
11937 int
11938 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11939     ip_ioctl_cmd_t *ipip, void *if_req)
11940 {
11941 	uint64_t turn_on;
11942 	uint64_t turn_off;
11943 	int	err;
11944 	boolean_t need_up = B_FALSE;
11945 	phyint_t *phyi;
11946 	ill_t *ill;
11947 	uint64_t intf_flags;
11948 	boolean_t phyint_flags_modified = B_FALSE;
11949 	uint64_t flags;
11950 	struct ifreq *ifr;
11951 	struct lifreq *lifr;
11952 	boolean_t set_linklocal = B_FALSE;
11953 	boolean_t zero_source = B_FALSE;
11954 	ip_stack_t *ipst;
11955 
11956 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11957 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11958 
11959 	ASSERT(IAM_WRITER_IPIF(ipif));
11960 
11961 	ill = ipif->ipif_ill;
11962 	phyi = ill->ill_phyint;
11963 	ipst = ill->ill_ipst;
11964 
11965 	if (ipip->ipi_cmd_type == IF_CMD) {
11966 		ifr = (struct ifreq *)if_req;
11967 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11968 	} else {
11969 		lifr = (struct lifreq *)if_req;
11970 		flags = lifr->lifr_flags;
11971 	}
11972 
11973 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11974 
11975 	/*
11976 	 * Has the flags been set correctly till now ?
11977 	 */
11978 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11979 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11980 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11981 	/*
11982 	 * Compare the new flags to the old, and partition
11983 	 * into those coming on and those going off.
11984 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11985 	 */
11986 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11987 		flags |= intf_flags & ~0xFFFF;
11988 
11989 	/*
11990 	 * First check which bits will change and then which will
11991 	 * go on and off
11992 	 */
11993 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11994 	if (!turn_on)
11995 		return (0);	/* No change */
11996 
11997 	turn_off = intf_flags & turn_on;
11998 	turn_on ^= turn_off;
11999 	err = 0;
12000 
12001 	/*
12002 	 * Don't allow any bits belonging to the logical interface
12003 	 * to be set or cleared on the replacement ipif that was
12004 	 * created temporarily during a MOVE.
12005 	 */
12006 	if (ipif->ipif_replace_zero &&
12007 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
12008 		return (EINVAL);
12009 	}
12010 
12011 	/*
12012 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
12013 	 * IPv6 interfaces.
12014 	 */
12015 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
12016 		return (EINVAL);
12017 
12018 	/*
12019 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
12020 	 */
12021 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
12022 		return (EINVAL);
12023 
12024 	/*
12025 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
12026 	 * interfaces.  It makes no sense in that context.
12027 	 */
12028 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
12029 		return (EINVAL);
12030 
12031 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
12032 		zero_source = B_TRUE;
12033 
12034 	/*
12035 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
12036 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
12037 	 * If the link local address isn't set, and can be set, it will get
12038 	 * set later on in this function.
12039 	 */
12040 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
12041 	    (flags & IFF_UP) && !zero_source &&
12042 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
12043 		if (ipif_cant_setlinklocal(ipif))
12044 			return (EINVAL);
12045 		set_linklocal = B_TRUE;
12046 	}
12047 
12048 	/*
12049 	 * ILL cannot be part of a usesrc group and and IPMP group at the
12050 	 * same time. No need to grab ill_g_usesrc_lock here, see
12051 	 * synchronization notes in ip.c
12052 	 */
12053 	if (turn_on & PHYI_STANDBY &&
12054 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
12055 		return (EINVAL);
12056 	}
12057 
12058 	/*
12059 	 * If we modify physical interface flags, we'll potentially need to
12060 	 * send up two routing socket messages for the changes (one for the
12061 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
12062 	 */
12063 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
12064 		phyint_flags_modified = B_TRUE;
12065 
12066 	/*
12067 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
12068 	 * we need to flush the IRE_CACHES belonging to this ill.
12069 	 * We handle this case here without doing the DOWN/UP dance
12070 	 * like it is done for other flags. If some other flags are
12071 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
12072 	 * below will handle it by bringing it down and then
12073 	 * bringing it UP.
12074 	 */
12075 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
12076 		ill_t *ill_v4, *ill_v6;
12077 
12078 		ill_v4 = phyi->phyint_illv4;
12079 		ill_v6 = phyi->phyint_illv6;
12080 
12081 		/*
12082 		 * First set the INACTIVE flag if needed. Then delete the ires.
12083 		 * ire_add will atomically prevent creating new IRE_CACHEs
12084 		 * unless hidden flag is set.
12085 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
12086 		 */
12087 		if ((turn_on & PHYI_FAILED) &&
12088 		    ((intf_flags & PHYI_STANDBY) ||
12089 		    !ipst->ips_ipmp_enable_failback)) {
12090 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
12091 			phyi->phyint_flags &= ~PHYI_INACTIVE;
12092 		}
12093 		if ((turn_off & PHYI_FAILED) &&
12094 		    ((intf_flags & PHYI_STANDBY) ||
12095 		    (!ipst->ips_ipmp_enable_failback &&
12096 		    ill_is_inactive(ill)))) {
12097 			phyint_inactive(phyi);
12098 		}
12099 
12100 		if (turn_on & PHYI_STANDBY) {
12101 			/*
12102 			 * We implicitly set INACTIVE only when STANDBY is set.
12103 			 * INACTIVE is also set on non-STANDBY phyint when user
12104 			 * disables FAILBACK using configuration file.
12105 			 * Do not allow STANDBY to be set on such INACTIVE
12106 			 * phyint
12107 			 */
12108 			if (phyi->phyint_flags & PHYI_INACTIVE)
12109 				return (EINVAL);
12110 			if (!(phyi->phyint_flags & PHYI_FAILED))
12111 				phyint_inactive(phyi);
12112 		}
12113 		if (turn_off & PHYI_STANDBY) {
12114 			if (ipst->ips_ipmp_enable_failback) {
12115 				/*
12116 				 * Reset PHYI_INACTIVE.
12117 				 */
12118 				phyi->phyint_flags &= ~PHYI_INACTIVE;
12119 			} else if (ill_is_inactive(ill) &&
12120 			    !(phyi->phyint_flags & PHYI_FAILED)) {
12121 				/*
12122 				 * Need to set INACTIVE, when user sets
12123 				 * STANDBY on a non-STANDBY phyint and
12124 				 * later resets STANDBY
12125 				 */
12126 				phyint_inactive(phyi);
12127 			}
12128 		}
12129 		/*
12130 		 * We should always send up a message so that the
12131 		 * daemons come to know of it. Note that the zeroth
12132 		 * interface can be down and the check below for IPIF_UP
12133 		 * will not make sense as we are actually setting
12134 		 * a phyint flag here. We assume that the ipif used
12135 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
12136 		 * send up any message for non-zero ipifs).
12137 		 */
12138 		phyint_flags_modified = B_TRUE;
12139 
12140 		if (ill_v4 != NULL) {
12141 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
12142 			    IRE_CACHE, ill_stq_cache_delete,
12143 			    (char *)ill_v4, ill_v4);
12144 			illgrp_reset_schednext(ill_v4);
12145 		}
12146 		if (ill_v6 != NULL) {
12147 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
12148 			    IRE_CACHE, ill_stq_cache_delete,
12149 			    (char *)ill_v6, ill_v6);
12150 			illgrp_reset_schednext(ill_v6);
12151 		}
12152 	}
12153 
12154 	/*
12155 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
12156 	 * status of the interface and, if the interface is part of an IPMP
12157 	 * group, all other interfaces that are part of the same IPMP
12158 	 * group.
12159 	 */
12160 	if ((turn_on | turn_off) & ILLF_ROUTER)
12161 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
12162 
12163 	/*
12164 	 * If the interface is not UP and we are not going to
12165 	 * bring it UP, record the flags and return. When the
12166 	 * interface comes UP later, the right actions will be
12167 	 * taken.
12168 	 */
12169 	if (!(ipif->ipif_flags & IPIF_UP) &&
12170 	    !(turn_on & IPIF_UP)) {
12171 		/* Record new flags in their respective places. */
12172 		mutex_enter(&ill->ill_lock);
12173 		mutex_enter(&ill->ill_phyint->phyint_lock);
12174 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12175 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12176 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12177 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12178 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12179 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12180 		mutex_exit(&ill->ill_lock);
12181 		mutex_exit(&ill->ill_phyint->phyint_lock);
12182 
12183 		/*
12184 		 * We do the broadcast and nomination here rather
12185 		 * than waiting for a FAILOVER/FAILBACK to happen. In
12186 		 * the case of FAILBACK from INACTIVE standby to the
12187 		 * interface that has been repaired, PHYI_FAILED has not
12188 		 * been cleared yet. If there are only two interfaces in
12189 		 * that group, all we have is a FAILED and INACTIVE
12190 		 * interface. If we do the nomination soon after a failback,
12191 		 * the broadcast nomination code would select the
12192 		 * INACTIVE interface for receiving broadcasts as FAILED is
12193 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
12194 		 * receive broadcast packets, we need to redo nomination
12195 		 * when the FAILED is cleared here. Thus, in general we
12196 		 * always do the nomination here for FAILED, STANDBY
12197 		 * and OFFLINE.
12198 		 */
12199 		if (((turn_on | turn_off) &
12200 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
12201 			ip_redo_nomination(phyi);
12202 		}
12203 		if (phyint_flags_modified) {
12204 			if (phyi->phyint_illv4 != NULL) {
12205 				ip_rts_ifmsg(phyi->phyint_illv4->
12206 				    ill_ipif);
12207 			}
12208 			if (phyi->phyint_illv6 != NULL) {
12209 				ip_rts_ifmsg(phyi->phyint_illv6->
12210 				    ill_ipif);
12211 			}
12212 		}
12213 		return (0);
12214 	} else if (set_linklocal || zero_source) {
12215 		mutex_enter(&ill->ill_lock);
12216 		if (set_linklocal)
12217 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
12218 		if (zero_source)
12219 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
12220 		mutex_exit(&ill->ill_lock);
12221 	}
12222 
12223 	/*
12224 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
12225 	 * or point-to-point interfaces with an unspecified destination. We do
12226 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
12227 	 * have a subnet assigned, which is how in.ndpd currently manages its
12228 	 * onlink prefix list when no addresses are configured with those
12229 	 * prefixes.
12230 	 */
12231 	if (ipif->ipif_isv6 &&
12232 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
12233 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
12234 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
12235 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12236 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
12237 		return (EINVAL);
12238 	}
12239 
12240 	/*
12241 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
12242 	 * from being brought up.
12243 	 */
12244 	if (!ipif->ipif_isv6 &&
12245 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12246 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
12247 		return (EINVAL);
12248 	}
12249 
12250 	/*
12251 	 * The only flag changes that we currently take specific action on
12252 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
12253 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
12254 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
12255 	 * the flags and bringing it back up again.
12256 	 */
12257 	if ((turn_on|turn_off) &
12258 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
12259 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
12260 		/*
12261 		 * Taking this ipif down, make sure we have
12262 		 * valid net and subnet bcast ire's for other
12263 		 * logical interfaces, if we need them.
12264 		 */
12265 		if (!ipif->ipif_isv6)
12266 			ipif_check_bcast_ires(ipif);
12267 
12268 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
12269 		    !(turn_off & IPIF_UP)) {
12270 			need_up = B_TRUE;
12271 			if (ipif->ipif_flags & IPIF_UP)
12272 				ill->ill_logical_down = 1;
12273 			turn_on &= ~IPIF_UP;
12274 		}
12275 		err = ipif_down(ipif, q, mp);
12276 		ip1dbg(("ipif_down returns %d err ", err));
12277 		if (err == EINPROGRESS)
12278 			return (err);
12279 		ipif_down_tail(ipif);
12280 	}
12281 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
12282 }
12283 
12284 static int
12285 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
12286     boolean_t need_up)
12287 {
12288 	ill_t	*ill;
12289 	phyint_t *phyi;
12290 	uint64_t turn_on;
12291 	uint64_t turn_off;
12292 	uint64_t intf_flags;
12293 	boolean_t phyint_flags_modified = B_FALSE;
12294 	int	err = 0;
12295 	boolean_t set_linklocal = B_FALSE;
12296 	boolean_t zero_source = B_FALSE;
12297 
12298 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12299 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12300 
12301 	ASSERT(IAM_WRITER_IPIF(ipif));
12302 
12303 	ill = ipif->ipif_ill;
12304 	phyi = ill->ill_phyint;
12305 
12306 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12307 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12308 
12309 	turn_off = intf_flags & turn_on;
12310 	turn_on ^= turn_off;
12311 
12312 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12313 		phyint_flags_modified = B_TRUE;
12314 
12315 	/*
12316 	 * Now we change the flags. Track current value of
12317 	 * other flags in their respective places.
12318 	 */
12319 	mutex_enter(&ill->ill_lock);
12320 	mutex_enter(&phyi->phyint_lock);
12321 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12322 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12323 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12324 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12325 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12326 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12327 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12328 		set_linklocal = B_TRUE;
12329 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12330 	}
12331 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12332 		zero_source = B_TRUE;
12333 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12334 	}
12335 	mutex_exit(&ill->ill_lock);
12336 	mutex_exit(&phyi->phyint_lock);
12337 
12338 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12339 		ip_redo_nomination(phyi);
12340 
12341 	if (set_linklocal)
12342 		(void) ipif_setlinklocal(ipif);
12343 
12344 	if (zero_source)
12345 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12346 	else
12347 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12348 
12349 	if (need_up) {
12350 		/*
12351 		 * XXX ipif_up really does not know whether a phyint flags
12352 		 * was modified or not. So, it sends up information on
12353 		 * only one routing sockets message. As we don't bring up
12354 		 * the interface and also set STANDBY/FAILED simultaneously
12355 		 * it should be okay.
12356 		 */
12357 		err = ipif_up(ipif, q, mp);
12358 	} else {
12359 		/*
12360 		 * Make sure routing socket sees all changes to the flags.
12361 		 * ipif_up_done* handles this when we use ipif_up.
12362 		 */
12363 		if (phyint_flags_modified) {
12364 			if (phyi->phyint_illv4 != NULL) {
12365 				ip_rts_ifmsg(phyi->phyint_illv4->
12366 				    ill_ipif);
12367 			}
12368 			if (phyi->phyint_illv6 != NULL) {
12369 				ip_rts_ifmsg(phyi->phyint_illv6->
12370 				    ill_ipif);
12371 			}
12372 		} else {
12373 			ip_rts_ifmsg(ipif);
12374 		}
12375 		/*
12376 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12377 		 * this in need_up case.
12378 		 */
12379 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12380 	}
12381 	return (err);
12382 }
12383 
12384 /*
12385  * Restart entry point to restart the flags restart operation after the
12386  * refcounts have dropped to zero.
12387  */
12388 /* ARGSUSED */
12389 int
12390 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12391     ip_ioctl_cmd_t *ipip, void *if_req)
12392 {
12393 	int	err;
12394 	struct ifreq *ifr = (struct ifreq *)if_req;
12395 	struct lifreq *lifr = (struct lifreq *)if_req;
12396 
12397 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12398 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12399 
12400 	ipif_down_tail(ipif);
12401 	if (ipip->ipi_cmd_type == IF_CMD) {
12402 		/*
12403 		 * Since ip_sioctl_flags expects an int and ifr_flags
12404 		 * is a short we need to cast ifr_flags into an int
12405 		 * to avoid having sign extension cause bits to get
12406 		 * set that should not be.
12407 		 */
12408 		err = ip_sioctl_flags_tail(ipif,
12409 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
12410 		    q, mp, B_TRUE);
12411 	} else {
12412 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
12413 		    q, mp, B_TRUE);
12414 	}
12415 	return (err);
12416 }
12417 
12418 /*
12419  * Can operate on either a module or a driver queue.
12420  */
12421 /* ARGSUSED */
12422 int
12423 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12424     ip_ioctl_cmd_t *ipip, void *if_req)
12425 {
12426 	/*
12427 	 * Has the flags been set correctly till now ?
12428 	 */
12429 	ill_t *ill = ipif->ipif_ill;
12430 	phyint_t *phyi = ill->ill_phyint;
12431 
12432 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12433 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12434 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12435 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12436 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12437 
12438 	/*
12439 	 * Need a lock since some flags can be set even when there are
12440 	 * references to the ipif.
12441 	 */
12442 	mutex_enter(&ill->ill_lock);
12443 	if (ipip->ipi_cmd_type == IF_CMD) {
12444 		struct ifreq *ifr = (struct ifreq *)if_req;
12445 
12446 		/* Get interface flags (low 16 only). */
12447 		ifr->ifr_flags = ((ipif->ipif_flags |
12448 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12449 	} else {
12450 		struct lifreq *lifr = (struct lifreq *)if_req;
12451 
12452 		/* Get interface flags. */
12453 		lifr->lifr_flags = ipif->ipif_flags |
12454 		    ill->ill_flags | phyi->phyint_flags;
12455 	}
12456 	mutex_exit(&ill->ill_lock);
12457 	return (0);
12458 }
12459 
12460 /* ARGSUSED */
12461 int
12462 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12463     ip_ioctl_cmd_t *ipip, void *if_req)
12464 {
12465 	int mtu;
12466 	int ip_min_mtu;
12467 	struct ifreq	*ifr;
12468 	struct lifreq *lifr;
12469 	ire_t	*ire;
12470 	ip_stack_t *ipst;
12471 
12472 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12473 	    ipif->ipif_id, (void *)ipif));
12474 	if (ipip->ipi_cmd_type == IF_CMD) {
12475 		ifr = (struct ifreq *)if_req;
12476 		mtu = ifr->ifr_metric;
12477 	} else {
12478 		lifr = (struct lifreq *)if_req;
12479 		mtu = lifr->lifr_mtu;
12480 	}
12481 
12482 	if (ipif->ipif_isv6)
12483 		ip_min_mtu = IPV6_MIN_MTU;
12484 	else
12485 		ip_min_mtu = IP_MIN_MTU;
12486 
12487 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12488 		return (EINVAL);
12489 
12490 	/*
12491 	 * Change the MTU size in all relevant ire's.
12492 	 * Mtu change Vs. new ire creation - protocol below.
12493 	 * First change ipif_mtu and the ire_max_frag of the
12494 	 * interface ire. Then do an ire walk and change the
12495 	 * ire_max_frag of all affected ires. During ire_add
12496 	 * under the bucket lock, set the ire_max_frag of the
12497 	 * new ire being created from the ipif/ire from which
12498 	 * it is being derived. If an mtu change happens after
12499 	 * the ire is added, the new ire will be cleaned up.
12500 	 * Conversely if the mtu change happens before the ire
12501 	 * is added, ire_add will see the new value of the mtu.
12502 	 */
12503 	ipif->ipif_mtu = mtu;
12504 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12505 
12506 	if (ipif->ipif_isv6)
12507 		ire = ipif_to_ire_v6(ipif);
12508 	else
12509 		ire = ipif_to_ire(ipif);
12510 	if (ire != NULL) {
12511 		ire->ire_max_frag = ipif->ipif_mtu;
12512 		ire_refrele(ire);
12513 	}
12514 	ipst = ipif->ipif_ill->ill_ipst;
12515 	if (ipif->ipif_flags & IPIF_UP) {
12516 		if (ipif->ipif_isv6)
12517 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12518 			    ipst);
12519 		else
12520 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12521 			    ipst);
12522 	}
12523 	/* Update the MTU in SCTP's list */
12524 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12525 	return (0);
12526 }
12527 
12528 /* Get interface MTU. */
12529 /* ARGSUSED */
12530 int
12531 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12532 	ip_ioctl_cmd_t *ipip, void *if_req)
12533 {
12534 	struct ifreq	*ifr;
12535 	struct lifreq	*lifr;
12536 
12537 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12538 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12539 	if (ipip->ipi_cmd_type == IF_CMD) {
12540 		ifr = (struct ifreq *)if_req;
12541 		ifr->ifr_metric = ipif->ipif_mtu;
12542 	} else {
12543 		lifr = (struct lifreq *)if_req;
12544 		lifr->lifr_mtu = ipif->ipif_mtu;
12545 	}
12546 	return (0);
12547 }
12548 
12549 /* Set interface broadcast address. */
12550 /* ARGSUSED2 */
12551 int
12552 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12553 	ip_ioctl_cmd_t *ipip, void *if_req)
12554 {
12555 	ipaddr_t addr;
12556 	ire_t	*ire;
12557 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12558 
12559 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12560 	    ipif->ipif_id));
12561 
12562 	ASSERT(IAM_WRITER_IPIF(ipif));
12563 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12564 		return (EADDRNOTAVAIL);
12565 
12566 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12567 
12568 	if (sin->sin_family != AF_INET)
12569 		return (EAFNOSUPPORT);
12570 
12571 	addr = sin->sin_addr.s_addr;
12572 	if (ipif->ipif_flags & IPIF_UP) {
12573 		/*
12574 		 * If we are already up, make sure the new
12575 		 * broadcast address makes sense.  If it does,
12576 		 * there should be an IRE for it already.
12577 		 * Don't match on ipif, only on the ill
12578 		 * since we are sharing these now. Don't use
12579 		 * MATCH_IRE_ILL_GROUP as we are looking for
12580 		 * the broadcast ire on this ill and each ill
12581 		 * in the group has its own broadcast ire.
12582 		 */
12583 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12584 		    ipif, ALL_ZONES, NULL,
12585 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12586 		if (ire == NULL) {
12587 			return (EINVAL);
12588 		} else {
12589 			ire_refrele(ire);
12590 		}
12591 	}
12592 	/*
12593 	 * Changing the broadcast addr for this ipif.
12594 	 * Make sure we have valid net and subnet bcast
12595 	 * ire's for other logical interfaces, if needed.
12596 	 */
12597 	if (addr != ipif->ipif_brd_addr)
12598 		ipif_check_bcast_ires(ipif);
12599 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12600 	return (0);
12601 }
12602 
12603 /* Get interface broadcast address. */
12604 /* ARGSUSED */
12605 int
12606 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12607     ip_ioctl_cmd_t *ipip, void *if_req)
12608 {
12609 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12610 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12611 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12612 		return (EADDRNOTAVAIL);
12613 
12614 	/* IPIF_BROADCAST not possible with IPv6 */
12615 	ASSERT(!ipif->ipif_isv6);
12616 	*sin = sin_null;
12617 	sin->sin_family = AF_INET;
12618 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12619 	return (0);
12620 }
12621 
12622 /*
12623  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12624  */
12625 /* ARGSUSED */
12626 int
12627 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12628     ip_ioctl_cmd_t *ipip, void *if_req)
12629 {
12630 	int err = 0;
12631 	in6_addr_t v6mask;
12632 
12633 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12634 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12635 
12636 	ASSERT(IAM_WRITER_IPIF(ipif));
12637 
12638 	if (ipif->ipif_isv6) {
12639 		sin6_t *sin6;
12640 
12641 		if (sin->sin_family != AF_INET6)
12642 			return (EAFNOSUPPORT);
12643 
12644 		sin6 = (sin6_t *)sin;
12645 		v6mask = sin6->sin6_addr;
12646 	} else {
12647 		ipaddr_t mask;
12648 
12649 		if (sin->sin_family != AF_INET)
12650 			return (EAFNOSUPPORT);
12651 
12652 		mask = sin->sin_addr.s_addr;
12653 		V4MASK_TO_V6(mask, v6mask);
12654 	}
12655 
12656 	/*
12657 	 * No big deal if the interface isn't already up, or the mask
12658 	 * isn't really changing, or this is pt-pt.
12659 	 */
12660 	if (!(ipif->ipif_flags & IPIF_UP) ||
12661 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12662 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12663 		ipif->ipif_v6net_mask = v6mask;
12664 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12665 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12666 			    ipif->ipif_v6net_mask,
12667 			    ipif->ipif_v6subnet);
12668 		}
12669 		return (0);
12670 	}
12671 	/*
12672 	 * Make sure we have valid net and subnet broadcast ire's
12673 	 * for the old netmask, if needed by other logical interfaces.
12674 	 */
12675 	if (!ipif->ipif_isv6)
12676 		ipif_check_bcast_ires(ipif);
12677 
12678 	err = ipif_logical_down(ipif, q, mp);
12679 	if (err == EINPROGRESS)
12680 		return (err);
12681 	ipif_down_tail(ipif);
12682 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12683 	return (err);
12684 }
12685 
12686 static int
12687 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12688 {
12689 	in6_addr_t v6mask;
12690 	int err = 0;
12691 
12692 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12693 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12694 
12695 	if (ipif->ipif_isv6) {
12696 		sin6_t *sin6;
12697 
12698 		sin6 = (sin6_t *)sin;
12699 		v6mask = sin6->sin6_addr;
12700 	} else {
12701 		ipaddr_t mask;
12702 
12703 		mask = sin->sin_addr.s_addr;
12704 		V4MASK_TO_V6(mask, v6mask);
12705 	}
12706 
12707 	ipif->ipif_v6net_mask = v6mask;
12708 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12709 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12710 		    ipif->ipif_v6subnet);
12711 	}
12712 	err = ipif_up(ipif, q, mp);
12713 
12714 	if (err == 0 || err == EINPROGRESS) {
12715 		/*
12716 		 * The interface must be DL_BOUND if this packet has to
12717 		 * go out on the wire. Since we only go through a logical
12718 		 * down and are bound with the driver during an internal
12719 		 * down/up that is satisfied.
12720 		 */
12721 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12722 			/* Potentially broadcast an address mask reply. */
12723 			ipif_mask_reply(ipif);
12724 		}
12725 	}
12726 	return (err);
12727 }
12728 
12729 /* ARGSUSED */
12730 int
12731 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12732     ip_ioctl_cmd_t *ipip, void *if_req)
12733 {
12734 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12735 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12736 	ipif_down_tail(ipif);
12737 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12738 }
12739 
12740 /* Get interface net mask. */
12741 /* ARGSUSED */
12742 int
12743 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12744     ip_ioctl_cmd_t *ipip, void *if_req)
12745 {
12746 	struct lifreq *lifr = (struct lifreq *)if_req;
12747 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12748 
12749 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12750 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12751 
12752 	/*
12753 	 * net mask can't change since we have a reference to the ipif.
12754 	 */
12755 	if (ipif->ipif_isv6) {
12756 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12757 		*sin6 = sin6_null;
12758 		sin6->sin6_family = AF_INET6;
12759 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12760 		lifr->lifr_addrlen =
12761 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12762 	} else {
12763 		*sin = sin_null;
12764 		sin->sin_family = AF_INET;
12765 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12766 		if (ipip->ipi_cmd_type == LIF_CMD) {
12767 			lifr->lifr_addrlen =
12768 			    ip_mask_to_plen(ipif->ipif_net_mask);
12769 		}
12770 	}
12771 	return (0);
12772 }
12773 
12774 /* ARGSUSED */
12775 int
12776 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12777     ip_ioctl_cmd_t *ipip, void *if_req)
12778 {
12779 
12780 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12781 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12782 	/*
12783 	 * Set interface metric.  We don't use this for
12784 	 * anything but we keep track of it in case it is
12785 	 * important to routing applications or such.
12786 	 */
12787 	if (ipip->ipi_cmd_type == IF_CMD) {
12788 		struct ifreq    *ifr;
12789 
12790 		ifr = (struct ifreq *)if_req;
12791 		ipif->ipif_metric = ifr->ifr_metric;
12792 	} else {
12793 		struct lifreq   *lifr;
12794 
12795 		lifr = (struct lifreq *)if_req;
12796 		ipif->ipif_metric = lifr->lifr_metric;
12797 	}
12798 	return (0);
12799 }
12800 
12801 
12802 /* ARGSUSED */
12803 int
12804 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12805     ip_ioctl_cmd_t *ipip, void *if_req)
12806 {
12807 
12808 	/* Get interface metric. */
12809 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12810 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12811 	if (ipip->ipi_cmd_type == IF_CMD) {
12812 		struct ifreq    *ifr;
12813 
12814 		ifr = (struct ifreq *)if_req;
12815 		ifr->ifr_metric = ipif->ipif_metric;
12816 	} else {
12817 		struct lifreq   *lifr;
12818 
12819 		lifr = (struct lifreq *)if_req;
12820 		lifr->lifr_metric = ipif->ipif_metric;
12821 	}
12822 
12823 	return (0);
12824 }
12825 
12826 /* ARGSUSED */
12827 int
12828 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12829     ip_ioctl_cmd_t *ipip, void *if_req)
12830 {
12831 
12832 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12833 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12834 	/*
12835 	 * Set the muxid returned from I_PLINK.
12836 	 */
12837 	if (ipip->ipi_cmd_type == IF_CMD) {
12838 		struct ifreq *ifr = (struct ifreq *)if_req;
12839 
12840 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12841 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12842 	} else {
12843 		struct lifreq *lifr = (struct lifreq *)if_req;
12844 
12845 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12846 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12847 	}
12848 	return (0);
12849 }
12850 
12851 /* ARGSUSED */
12852 int
12853 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12854     ip_ioctl_cmd_t *ipip, void *if_req)
12855 {
12856 
12857 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12858 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12859 	/*
12860 	 * Get the muxid saved in ill for I_PUNLINK.
12861 	 */
12862 	if (ipip->ipi_cmd_type == IF_CMD) {
12863 		struct ifreq *ifr = (struct ifreq *)if_req;
12864 
12865 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12866 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12867 	} else {
12868 		struct lifreq *lifr = (struct lifreq *)if_req;
12869 
12870 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12871 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12872 	}
12873 	return (0);
12874 }
12875 
12876 /*
12877  * Set the subnet prefix. Does not modify the broadcast address.
12878  */
12879 /* ARGSUSED */
12880 int
12881 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12882     ip_ioctl_cmd_t *ipip, void *if_req)
12883 {
12884 	int err = 0;
12885 	in6_addr_t v6addr;
12886 	in6_addr_t v6mask;
12887 	boolean_t need_up = B_FALSE;
12888 	int addrlen;
12889 
12890 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12891 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12892 
12893 	ASSERT(IAM_WRITER_IPIF(ipif));
12894 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12895 
12896 	if (ipif->ipif_isv6) {
12897 		sin6_t *sin6;
12898 
12899 		if (sin->sin_family != AF_INET6)
12900 			return (EAFNOSUPPORT);
12901 
12902 		sin6 = (sin6_t *)sin;
12903 		v6addr = sin6->sin6_addr;
12904 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12905 			return (EADDRNOTAVAIL);
12906 	} else {
12907 		ipaddr_t addr;
12908 
12909 		if (sin->sin_family != AF_INET)
12910 			return (EAFNOSUPPORT);
12911 
12912 		addr = sin->sin_addr.s_addr;
12913 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12914 			return (EADDRNOTAVAIL);
12915 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12916 		/* Add 96 bits */
12917 		addrlen += IPV6_ABITS - IP_ABITS;
12918 	}
12919 
12920 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12921 		return (EINVAL);
12922 
12923 	/* Check if bits in the address is set past the mask */
12924 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12925 		return (EINVAL);
12926 
12927 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12928 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12929 		return (0);	/* No change */
12930 
12931 	if (ipif->ipif_flags & IPIF_UP) {
12932 		/*
12933 		 * If the interface is already marked up,
12934 		 * we call ipif_down which will take care
12935 		 * of ditching any IREs that have been set
12936 		 * up based on the old interface address.
12937 		 */
12938 		err = ipif_logical_down(ipif, q, mp);
12939 		if (err == EINPROGRESS)
12940 			return (err);
12941 		ipif_down_tail(ipif);
12942 		need_up = B_TRUE;
12943 	}
12944 
12945 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12946 	return (err);
12947 }
12948 
12949 static int
12950 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12951     queue_t *q, mblk_t *mp, boolean_t need_up)
12952 {
12953 	ill_t	*ill = ipif->ipif_ill;
12954 	int	err = 0;
12955 
12956 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12957 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12958 
12959 	/* Set the new address. */
12960 	mutex_enter(&ill->ill_lock);
12961 	ipif->ipif_v6net_mask = v6mask;
12962 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12963 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12964 		    ipif->ipif_v6subnet);
12965 	}
12966 	mutex_exit(&ill->ill_lock);
12967 
12968 	if (need_up) {
12969 		/*
12970 		 * Now bring the interface back up.  If this
12971 		 * is the only IPIF for the ILL, ipif_up
12972 		 * will have to re-bind to the device, so
12973 		 * we may get back EINPROGRESS, in which
12974 		 * case, this IOCTL will get completed in
12975 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12976 		 */
12977 		err = ipif_up(ipif, q, mp);
12978 		if (err == EINPROGRESS)
12979 			return (err);
12980 	}
12981 	return (err);
12982 }
12983 
12984 /* ARGSUSED */
12985 int
12986 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12987     ip_ioctl_cmd_t *ipip, void *if_req)
12988 {
12989 	int	addrlen;
12990 	in6_addr_t v6addr;
12991 	in6_addr_t v6mask;
12992 	struct lifreq *lifr = (struct lifreq *)if_req;
12993 
12994 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12995 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12996 	ipif_down_tail(ipif);
12997 
12998 	addrlen = lifr->lifr_addrlen;
12999 	if (ipif->ipif_isv6) {
13000 		sin6_t *sin6;
13001 
13002 		sin6 = (sin6_t *)sin;
13003 		v6addr = sin6->sin6_addr;
13004 	} else {
13005 		ipaddr_t addr;
13006 
13007 		addr = sin->sin_addr.s_addr;
13008 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
13009 		addrlen += IPV6_ABITS - IP_ABITS;
13010 	}
13011 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
13012 
13013 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
13014 }
13015 
13016 /* ARGSUSED */
13017 int
13018 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13019     ip_ioctl_cmd_t *ipip, void *if_req)
13020 {
13021 	struct lifreq *lifr = (struct lifreq *)if_req;
13022 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
13023 
13024 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
13025 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13026 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
13027 
13028 	if (ipif->ipif_isv6) {
13029 		*sin6 = sin6_null;
13030 		sin6->sin6_family = AF_INET6;
13031 		sin6->sin6_addr = ipif->ipif_v6subnet;
13032 		lifr->lifr_addrlen =
13033 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
13034 	} else {
13035 		*sin = sin_null;
13036 		sin->sin_family = AF_INET;
13037 		sin->sin_addr.s_addr = ipif->ipif_subnet;
13038 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
13039 	}
13040 	return (0);
13041 }
13042 
13043 /*
13044  * Set the IPv6 address token.
13045  */
13046 /* ARGSUSED */
13047 int
13048 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13049     ip_ioctl_cmd_t *ipi, void *if_req)
13050 {
13051 	ill_t *ill = ipif->ipif_ill;
13052 	int err;
13053 	in6_addr_t v6addr;
13054 	in6_addr_t v6mask;
13055 	boolean_t need_up = B_FALSE;
13056 	int i;
13057 	sin6_t *sin6 = (sin6_t *)sin;
13058 	struct lifreq *lifr = (struct lifreq *)if_req;
13059 	int addrlen;
13060 
13061 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
13062 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13063 	ASSERT(IAM_WRITER_IPIF(ipif));
13064 
13065 	addrlen = lifr->lifr_addrlen;
13066 	/* Only allow for logical unit zero i.e. not on "le0:17" */
13067 	if (ipif->ipif_id != 0)
13068 		return (EINVAL);
13069 
13070 	if (!ipif->ipif_isv6)
13071 		return (EINVAL);
13072 
13073 	if (addrlen > IPV6_ABITS)
13074 		return (EINVAL);
13075 
13076 	v6addr = sin6->sin6_addr;
13077 
13078 	/*
13079 	 * The length of the token is the length from the end.  To get
13080 	 * the proper mask for this, compute the mask of the bits not
13081 	 * in the token; ie. the prefix, and then xor to get the mask.
13082 	 */
13083 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
13084 		return (EINVAL);
13085 	for (i = 0; i < 4; i++) {
13086 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
13087 	}
13088 
13089 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
13090 	    ill->ill_token_length == addrlen)
13091 		return (0);	/* No change */
13092 
13093 	if (ipif->ipif_flags & IPIF_UP) {
13094 		err = ipif_logical_down(ipif, q, mp);
13095 		if (err == EINPROGRESS)
13096 			return (err);
13097 		ipif_down_tail(ipif);
13098 		need_up = B_TRUE;
13099 	}
13100 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
13101 	return (err);
13102 }
13103 
13104 static int
13105 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
13106     mblk_t *mp, boolean_t need_up)
13107 {
13108 	in6_addr_t v6addr;
13109 	in6_addr_t v6mask;
13110 	ill_t	*ill = ipif->ipif_ill;
13111 	int	i;
13112 	int	err = 0;
13113 
13114 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
13115 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13116 	v6addr = sin6->sin6_addr;
13117 	/*
13118 	 * The length of the token is the length from the end.  To get
13119 	 * the proper mask for this, compute the mask of the bits not
13120 	 * in the token; ie. the prefix, and then xor to get the mask.
13121 	 */
13122 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
13123 	for (i = 0; i < 4; i++)
13124 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
13125 
13126 	mutex_enter(&ill->ill_lock);
13127 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
13128 	ill->ill_token_length = addrlen;
13129 	mutex_exit(&ill->ill_lock);
13130 
13131 	if (need_up) {
13132 		/*
13133 		 * Now bring the interface back up.  If this
13134 		 * is the only IPIF for the ILL, ipif_up
13135 		 * will have to re-bind to the device, so
13136 		 * we may get back EINPROGRESS, in which
13137 		 * case, this IOCTL will get completed in
13138 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
13139 		 */
13140 		err = ipif_up(ipif, q, mp);
13141 		if (err == EINPROGRESS)
13142 			return (err);
13143 	}
13144 	return (err);
13145 }
13146 
13147 /* ARGSUSED */
13148 int
13149 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13150     ip_ioctl_cmd_t *ipi, void *if_req)
13151 {
13152 	ill_t *ill;
13153 	sin6_t *sin6 = (sin6_t *)sin;
13154 	struct lifreq *lifr = (struct lifreq *)if_req;
13155 
13156 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
13157 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13158 	if (ipif->ipif_id != 0)
13159 		return (EINVAL);
13160 
13161 	ill = ipif->ipif_ill;
13162 	if (!ill->ill_isv6)
13163 		return (ENXIO);
13164 
13165 	*sin6 = sin6_null;
13166 	sin6->sin6_family = AF_INET6;
13167 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
13168 	sin6->sin6_addr = ill->ill_token;
13169 	lifr->lifr_addrlen = ill->ill_token_length;
13170 	return (0);
13171 }
13172 
13173 /*
13174  * Set (hardware) link specific information that might override
13175  * what was acquired through the DL_INFO_ACK.
13176  * The logic is as follows.
13177  *
13178  * become exclusive
13179  * set CHANGING flag
13180  * change mtu on affected IREs
13181  * clear CHANGING flag
13182  *
13183  * An ire add that occurs before the CHANGING flag is set will have its mtu
13184  * changed by the ip_sioctl_lnkinfo.
13185  *
13186  * During the time the CHANGING flag is set, no new ires will be added to the
13187  * bucket, and ire add will fail (due the CHANGING flag).
13188  *
13189  * An ire add that occurs after the CHANGING flag is set will have the right mtu
13190  * before it is added to the bucket.
13191  *
13192  * Obviously only 1 thread can set the CHANGING flag and we need to become
13193  * exclusive to set the flag.
13194  */
13195 /* ARGSUSED */
13196 int
13197 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13198     ip_ioctl_cmd_t *ipi, void *if_req)
13199 {
13200 	ill_t		*ill = ipif->ipif_ill;
13201 	ipif_t		*nipif;
13202 	int		ip_min_mtu;
13203 	boolean_t	mtu_walk = B_FALSE;
13204 	struct lifreq	*lifr = (struct lifreq *)if_req;
13205 	lif_ifinfo_req_t *lir;
13206 	ire_t		*ire;
13207 
13208 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
13209 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13210 	lir = &lifr->lifr_ifinfo;
13211 	ASSERT(IAM_WRITER_IPIF(ipif));
13212 
13213 	/* Only allow for logical unit zero i.e. not on "le0:17" */
13214 	if (ipif->ipif_id != 0)
13215 		return (EINVAL);
13216 
13217 	/* Set interface MTU. */
13218 	if (ipif->ipif_isv6)
13219 		ip_min_mtu = IPV6_MIN_MTU;
13220 	else
13221 		ip_min_mtu = IP_MIN_MTU;
13222 
13223 	/*
13224 	 * Verify values before we set anything. Allow zero to
13225 	 * mean unspecified.
13226 	 */
13227 	if (lir->lir_maxmtu != 0 &&
13228 	    (lir->lir_maxmtu > ill->ill_max_frag ||
13229 	    lir->lir_maxmtu < ip_min_mtu))
13230 		return (EINVAL);
13231 	if (lir->lir_reachtime != 0 &&
13232 	    lir->lir_reachtime > ND_MAX_REACHTIME)
13233 		return (EINVAL);
13234 	if (lir->lir_reachretrans != 0 &&
13235 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
13236 		return (EINVAL);
13237 
13238 	mutex_enter(&ill->ill_lock);
13239 	ill->ill_state_flags |= ILL_CHANGING;
13240 	for (nipif = ill->ill_ipif; nipif != NULL;
13241 	    nipif = nipif->ipif_next) {
13242 		nipif->ipif_state_flags |= IPIF_CHANGING;
13243 	}
13244 
13245 	mutex_exit(&ill->ill_lock);
13246 
13247 	if (lir->lir_maxmtu != 0) {
13248 		ill->ill_max_mtu = lir->lir_maxmtu;
13249 		ill->ill_mtu_userspecified = 1;
13250 		mtu_walk = B_TRUE;
13251 	}
13252 
13253 	if (lir->lir_reachtime != 0)
13254 		ill->ill_reachable_time = lir->lir_reachtime;
13255 
13256 	if (lir->lir_reachretrans != 0)
13257 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
13258 
13259 	ill->ill_max_hops = lir->lir_maxhops;
13260 
13261 	ill->ill_max_buf = ND_MAX_Q;
13262 
13263 	if (mtu_walk) {
13264 		/*
13265 		 * Set the MTU on all ipifs associated with this ill except
13266 		 * for those whose MTU was fixed via SIOCSLIFMTU.
13267 		 */
13268 		for (nipif = ill->ill_ipif; nipif != NULL;
13269 		    nipif = nipif->ipif_next) {
13270 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
13271 				continue;
13272 
13273 			nipif->ipif_mtu = ill->ill_max_mtu;
13274 
13275 			if (!(nipif->ipif_flags & IPIF_UP))
13276 				continue;
13277 
13278 			if (nipif->ipif_isv6)
13279 				ire = ipif_to_ire_v6(nipif);
13280 			else
13281 				ire = ipif_to_ire(nipif);
13282 			if (ire != NULL) {
13283 				ire->ire_max_frag = ipif->ipif_mtu;
13284 				ire_refrele(ire);
13285 			}
13286 			if (ill->ill_isv6) {
13287 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
13288 				    ipif_mtu_change, (char *)nipif,
13289 				    ill);
13290 			} else {
13291 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
13292 				    ipif_mtu_change, (char *)nipif,
13293 				    ill);
13294 			}
13295 		}
13296 	}
13297 
13298 	mutex_enter(&ill->ill_lock);
13299 	for (nipif = ill->ill_ipif; nipif != NULL;
13300 	    nipif = nipif->ipif_next) {
13301 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
13302 	}
13303 	ILL_UNMARK_CHANGING(ill);
13304 	mutex_exit(&ill->ill_lock);
13305 
13306 	return (0);
13307 }
13308 
13309 /* ARGSUSED */
13310 int
13311 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13312     ip_ioctl_cmd_t *ipi, void *if_req)
13313 {
13314 	struct lif_ifinfo_req *lir;
13315 	ill_t *ill = ipif->ipif_ill;
13316 
13317 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13318 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13319 	if (ipif->ipif_id != 0)
13320 		return (EINVAL);
13321 
13322 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13323 	lir->lir_maxhops = ill->ill_max_hops;
13324 	lir->lir_reachtime = ill->ill_reachable_time;
13325 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13326 	lir->lir_maxmtu = ill->ill_max_mtu;
13327 
13328 	return (0);
13329 }
13330 
13331 /*
13332  * Return best guess as to the subnet mask for the specified address.
13333  * Based on the subnet masks for all the configured interfaces.
13334  *
13335  * We end up returning a zero mask in the case of default, multicast or
13336  * experimental.
13337  */
13338 static ipaddr_t
13339 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13340 {
13341 	ipaddr_t net_mask;
13342 	ill_t	*ill;
13343 	ipif_t	*ipif;
13344 	ill_walk_context_t ctx;
13345 	ipif_t	*fallback_ipif = NULL;
13346 
13347 	net_mask = ip_net_mask(addr);
13348 	if (net_mask == 0) {
13349 		*ipifp = NULL;
13350 		return (0);
13351 	}
13352 
13353 	/* Let's check to see if this is maybe a local subnet route. */
13354 	/* this function only applies to IPv4 interfaces */
13355 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13356 	ill = ILL_START_WALK_V4(&ctx, ipst);
13357 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13358 		mutex_enter(&ill->ill_lock);
13359 		for (ipif = ill->ill_ipif; ipif != NULL;
13360 		    ipif = ipif->ipif_next) {
13361 			if (!IPIF_CAN_LOOKUP(ipif))
13362 				continue;
13363 			if (!(ipif->ipif_flags & IPIF_UP))
13364 				continue;
13365 			if ((ipif->ipif_subnet & net_mask) ==
13366 			    (addr & net_mask)) {
13367 				/*
13368 				 * Don't trust pt-pt interfaces if there are
13369 				 * other interfaces.
13370 				 */
13371 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13372 					if (fallback_ipif == NULL) {
13373 						ipif_refhold_locked(ipif);
13374 						fallback_ipif = ipif;
13375 					}
13376 					continue;
13377 				}
13378 
13379 				/*
13380 				 * Fine. Just assume the same net mask as the
13381 				 * directly attached subnet interface is using.
13382 				 */
13383 				ipif_refhold_locked(ipif);
13384 				mutex_exit(&ill->ill_lock);
13385 				rw_exit(&ipst->ips_ill_g_lock);
13386 				if (fallback_ipif != NULL)
13387 					ipif_refrele(fallback_ipif);
13388 				*ipifp = ipif;
13389 				return (ipif->ipif_net_mask);
13390 			}
13391 		}
13392 		mutex_exit(&ill->ill_lock);
13393 	}
13394 	rw_exit(&ipst->ips_ill_g_lock);
13395 
13396 	*ipifp = fallback_ipif;
13397 	return ((fallback_ipif != NULL) ?
13398 	    fallback_ipif->ipif_net_mask : net_mask);
13399 }
13400 
13401 /*
13402  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13403  */
13404 static void
13405 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13406 {
13407 	IOCP	iocp;
13408 	ipft_t	*ipft;
13409 	ipllc_t	*ipllc;
13410 	mblk_t	*mp1;
13411 	cred_t	*cr;
13412 	int	error = 0;
13413 	conn_t	*connp;
13414 
13415 	ip1dbg(("ip_wput_ioctl"));
13416 	iocp = (IOCP)mp->b_rptr;
13417 	mp1 = mp->b_cont;
13418 	if (mp1 == NULL) {
13419 		iocp->ioc_error = EINVAL;
13420 		mp->b_datap->db_type = M_IOCNAK;
13421 		iocp->ioc_count = 0;
13422 		qreply(q, mp);
13423 		return;
13424 	}
13425 
13426 	/*
13427 	 * These IOCTLs provide various control capabilities to
13428 	 * upstream agents such as ULPs and processes.	There
13429 	 * are currently two such IOCTLs implemented.  They
13430 	 * are used by TCP to provide update information for
13431 	 * existing IREs and to forcibly delete an IRE for a
13432 	 * host that is not responding, thereby forcing an
13433 	 * attempt at a new route.
13434 	 */
13435 	iocp->ioc_error = EINVAL;
13436 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13437 		goto done;
13438 
13439 	ipllc = (ipllc_t *)mp1->b_rptr;
13440 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13441 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13442 			break;
13443 	}
13444 	/*
13445 	 * prefer credential from mblk over ioctl;
13446 	 * see ip_sioctl_copyin_setup
13447 	 */
13448 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13449 
13450 	/*
13451 	 * Refhold the conn in case the request gets queued up in some lookup
13452 	 */
13453 	ASSERT(CONN_Q(q));
13454 	connp = Q_TO_CONN(q);
13455 	CONN_INC_REF(connp);
13456 	if (ipft->ipft_pfi &&
13457 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13458 	    pullupmsg(mp1, ipft->ipft_min_size))) {
13459 		error = (*ipft->ipft_pfi)(q,
13460 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13461 	}
13462 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13463 		/*
13464 		 * CONN_OPER_PENDING_DONE happens in the function called
13465 		 * through ipft_pfi above.
13466 		 */
13467 		return;
13468 	}
13469 
13470 	CONN_OPER_PENDING_DONE(connp);
13471 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13472 		freemsg(mp);
13473 		return;
13474 	}
13475 	iocp->ioc_error = error;
13476 
13477 done:
13478 	mp->b_datap->db_type = M_IOCACK;
13479 	if (iocp->ioc_error)
13480 		iocp->ioc_count = 0;
13481 	qreply(q, mp);
13482 }
13483 
13484 /*
13485  * Lookup an ipif using the sequence id (ipif_seqid)
13486  */
13487 ipif_t *
13488 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13489 {
13490 	ipif_t *ipif;
13491 
13492 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13493 
13494 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13495 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13496 			return (ipif);
13497 	}
13498 	return (NULL);
13499 }
13500 
13501 /*
13502  * Assign a unique id for the ipif. This is used later when we send
13503  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13504  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13505  * IRE is added, we verify that ipif has not disappeared.
13506  */
13507 
13508 static void
13509 ipif_assign_seqid(ipif_t *ipif)
13510 {
13511 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13512 
13513 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13514 }
13515 
13516 /*
13517  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13518  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13519  * be inserted into the first space available in the list. The value of
13520  * ipif_id will then be set to the appropriate value for its position.
13521  */
13522 static int
13523 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13524 {
13525 	ill_t *ill;
13526 	ipif_t *tipif;
13527 	ipif_t **tipifp;
13528 	int id;
13529 	ip_stack_t	*ipst;
13530 
13531 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13532 	    IAM_WRITER_IPIF(ipif));
13533 
13534 	ill = ipif->ipif_ill;
13535 	ASSERT(ill != NULL);
13536 	ipst = ill->ill_ipst;
13537 
13538 	/*
13539 	 * In the case of lo0:0 we already hold the ill_g_lock.
13540 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13541 	 * ipif_insert. Another such caller is ipif_move.
13542 	 */
13543 	if (acquire_g_lock)
13544 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13545 	if (acquire_ill_lock)
13546 		mutex_enter(&ill->ill_lock);
13547 	id = ipif->ipif_id;
13548 	tipifp = &(ill->ill_ipif);
13549 	if (id == -1) {	/* need to find a real id */
13550 		id = 0;
13551 		while ((tipif = *tipifp) != NULL) {
13552 			ASSERT(tipif->ipif_id >= id);
13553 			if (tipif->ipif_id != id)
13554 				break; /* non-consecutive id */
13555 			id++;
13556 			tipifp = &(tipif->ipif_next);
13557 		}
13558 		/* limit number of logical interfaces */
13559 		if (id >= ipst->ips_ip_addrs_per_if) {
13560 			if (acquire_ill_lock)
13561 				mutex_exit(&ill->ill_lock);
13562 			if (acquire_g_lock)
13563 				rw_exit(&ipst->ips_ill_g_lock);
13564 			return (-1);
13565 		}
13566 		ipif->ipif_id = id; /* assign new id */
13567 	} else if (id < ipst->ips_ip_addrs_per_if) {
13568 		/* we have a real id; insert ipif in the right place */
13569 		while ((tipif = *tipifp) != NULL) {
13570 			ASSERT(tipif->ipif_id != id);
13571 			if (tipif->ipif_id > id)
13572 				break; /* found correct location */
13573 			tipifp = &(tipif->ipif_next);
13574 		}
13575 	} else {
13576 		if (acquire_ill_lock)
13577 			mutex_exit(&ill->ill_lock);
13578 		if (acquire_g_lock)
13579 			rw_exit(&ipst->ips_ill_g_lock);
13580 		return (-1);
13581 	}
13582 
13583 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13584 
13585 	ipif->ipif_next = tipif;
13586 	*tipifp = ipif;
13587 	if (acquire_ill_lock)
13588 		mutex_exit(&ill->ill_lock);
13589 	if (acquire_g_lock)
13590 		rw_exit(&ipst->ips_ill_g_lock);
13591 	return (0);
13592 }
13593 
13594 static void
13595 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13596 {
13597 	ipif_t	**ipifp;
13598 	ill_t	*ill = ipif->ipif_ill;
13599 
13600 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13601 	if (acquire_ill_lock)
13602 		mutex_enter(&ill->ill_lock);
13603 	else
13604 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13605 
13606 	ipifp = &ill->ill_ipif;
13607 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13608 		if (*ipifp == ipif) {
13609 			*ipifp = ipif->ipif_next;
13610 			break;
13611 		}
13612 	}
13613 
13614 	if (acquire_ill_lock)
13615 		mutex_exit(&ill->ill_lock);
13616 }
13617 
13618 /*
13619  * Allocate and initialize a new interface control structure.  (Always
13620  * called as writer.)
13621  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13622  * is not part of the global linked list of ills. ipif_seqid is unique
13623  * in the system and to preserve the uniqueness, it is assigned only
13624  * when ill becomes part of the global list. At that point ill will
13625  * have a name. If it doesn't get assigned here, it will get assigned
13626  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13627  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13628  * the interface flags or any other information from the DL_INFO_ACK for
13629  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13630  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13631  * second DL_INFO_ACK comes in from the driver.
13632  */
13633 static ipif_t *
13634 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13635 {
13636 	ipif_t	*ipif;
13637 	phyint_t *phyi;
13638 
13639 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13640 	    ill->ill_name, id, (void *)ill));
13641 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13642 
13643 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13644 		return (NULL);
13645 	*ipif = ipif_zero;	/* start clean */
13646 
13647 	ipif->ipif_ill = ill;
13648 	ipif->ipif_id = id;	/* could be -1 */
13649 	/*
13650 	 * Inherit the zoneid from the ill; for the shared stack instance
13651 	 * this is always the global zone
13652 	 */
13653 	ipif->ipif_zoneid = ill->ill_zoneid;
13654 
13655 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13656 
13657 	ipif->ipif_refcnt = 0;
13658 	ipif->ipif_saved_ire_cnt = 0;
13659 
13660 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13661 		mi_free(ipif);
13662 		return (NULL);
13663 	}
13664 	/* -1 id should have been replaced by real id */
13665 	id = ipif->ipif_id;
13666 	ASSERT(id >= 0);
13667 
13668 	if (ill->ill_name[0] != '\0')
13669 		ipif_assign_seqid(ipif);
13670 
13671 	/*
13672 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13673 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13674 	 * ioctl sets ipif_orig_ipifid to zero.
13675 	 */
13676 	ipif->ipif_orig_ipifid = id;
13677 
13678 	/*
13679 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13680 	 * The ipif is still not up and can't be looked up until the
13681 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13682 	 */
13683 	mutex_enter(&ill->ill_lock);
13684 	mutex_enter(&ill->ill_phyint->phyint_lock);
13685 	/*
13686 	 * Set the running flag when logical interface zero is created.
13687 	 * For subsequent logical interfaces, a DLPI link down
13688 	 * notification message may have cleared the running flag to
13689 	 * indicate the link is down, so we shouldn't just blindly set it.
13690 	 */
13691 	if (id == 0)
13692 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13693 	ipif->ipif_ire_type = ire_type;
13694 	phyi = ill->ill_phyint;
13695 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13696 
13697 	if (ipif->ipif_isv6) {
13698 		ill->ill_flags |= ILLF_IPV6;
13699 	} else {
13700 		ipaddr_t inaddr_any = INADDR_ANY;
13701 
13702 		ill->ill_flags |= ILLF_IPV4;
13703 
13704 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13705 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13706 		    &ipif->ipif_v6lcl_addr);
13707 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13708 		    &ipif->ipif_v6src_addr);
13709 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13710 		    &ipif->ipif_v6subnet);
13711 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13712 		    &ipif->ipif_v6net_mask);
13713 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13714 		    &ipif->ipif_v6brd_addr);
13715 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13716 		    &ipif->ipif_v6pp_dst_addr);
13717 	}
13718 
13719 	/*
13720 	 * Don't set the interface flags etc. now, will do it in
13721 	 * ip_ll_subnet_defaults.
13722 	 */
13723 	if (!initialize) {
13724 		mutex_exit(&ill->ill_lock);
13725 		mutex_exit(&ill->ill_phyint->phyint_lock);
13726 		return (ipif);
13727 	}
13728 	ipif->ipif_mtu = ill->ill_max_mtu;
13729 
13730 	if (ill->ill_bcast_addr_length != 0) {
13731 		/*
13732 		 * Later detect lack of DLPI driver multicast
13733 		 * capability by catching DL_ENABMULTI errors in
13734 		 * ip_rput_dlpi.
13735 		 */
13736 		ill->ill_flags |= ILLF_MULTICAST;
13737 		if (!ipif->ipif_isv6)
13738 			ipif->ipif_flags |= IPIF_BROADCAST;
13739 	} else {
13740 		if (ill->ill_net_type != IRE_LOOPBACK) {
13741 			if (ipif->ipif_isv6)
13742 				/*
13743 				 * Note: xresolv interfaces will eventually need
13744 				 * NOARP set here as well, but that will require
13745 				 * those external resolvers to have some
13746 				 * knowledge of that flag and act appropriately.
13747 				 * Not to be changed at present.
13748 				 */
13749 				ill->ill_flags |= ILLF_NONUD;
13750 			else
13751 				ill->ill_flags |= ILLF_NOARP;
13752 		}
13753 		if (ill->ill_phys_addr_length == 0) {
13754 			if (ill->ill_media &&
13755 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13756 				ipif->ipif_flags |= IPIF_NOXMIT;
13757 				phyi->phyint_flags |= PHYI_VIRTUAL;
13758 			} else {
13759 				/* pt-pt supports multicast. */
13760 				ill->ill_flags |= ILLF_MULTICAST;
13761 				if (ill->ill_net_type == IRE_LOOPBACK) {
13762 					phyi->phyint_flags |=
13763 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13764 				} else {
13765 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13766 				}
13767 			}
13768 		}
13769 	}
13770 	mutex_exit(&ill->ill_lock);
13771 	mutex_exit(&ill->ill_phyint->phyint_lock);
13772 	return (ipif);
13773 }
13774 
13775 /*
13776  * If appropriate, send a message up to the resolver delete the entry
13777  * for the address of this interface which is going out of business.
13778  * (Always called as writer).
13779  *
13780  * NOTE : We need to check for NULL mps as some of the fields are
13781  *	  initialized only for some interface types. See ipif_resolver_up()
13782  *	  for details.
13783  */
13784 void
13785 ipif_arp_down(ipif_t *ipif)
13786 {
13787 	mblk_t	*mp;
13788 	ill_t	*ill = ipif->ipif_ill;
13789 
13790 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13791 	ASSERT(IAM_WRITER_IPIF(ipif));
13792 
13793 	/* Delete the mapping for the local address */
13794 	mp = ipif->ipif_arp_del_mp;
13795 	if (mp != NULL) {
13796 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13797 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13798 		putnext(ill->ill_rq, mp);
13799 		ipif->ipif_arp_del_mp = NULL;
13800 	}
13801 
13802 	/*
13803 	 * If this is the last ipif that is going down and there are no
13804 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13805 	 * clean up ARP completely.
13806 	 */
13807 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13808 
13809 		/* Send up AR_INTERFACE_DOWN message */
13810 		mp = ill->ill_arp_down_mp;
13811 		if (mp != NULL) {
13812 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13813 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13814 			    ipif->ipif_id));
13815 			putnext(ill->ill_rq, mp);
13816 			ill->ill_arp_down_mp = NULL;
13817 		}
13818 
13819 		/* Tell ARP to delete the multicast mappings */
13820 		mp = ill->ill_arp_del_mapping_mp;
13821 		if (mp != NULL) {
13822 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13823 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13824 			    ipif->ipif_id));
13825 			putnext(ill->ill_rq, mp);
13826 			ill->ill_arp_del_mapping_mp = NULL;
13827 		}
13828 	}
13829 }
13830 
13831 /*
13832  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13833  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13834  * that it wants the add_mp allocated in this function to be returned
13835  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13836  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13837  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13838  * as it does a ipif_arp_down after calling this function - which will
13839  * remove what we add here.
13840  *
13841  * Returns -1 on failures and 0 on success.
13842  */
13843 int
13844 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13845 {
13846 	mblk_t	*del_mp = NULL;
13847 	mblk_t *add_mp = NULL;
13848 	mblk_t *mp;
13849 	ill_t	*ill = ipif->ipif_ill;
13850 	phyint_t *phyi = ill->ill_phyint;
13851 	ipaddr_t addr, mask, extract_mask = 0;
13852 	arma_t	*arma;
13853 	uint8_t *maddr, *bphys_addr;
13854 	uint32_t hw_start;
13855 	dl_unitdata_req_t *dlur;
13856 
13857 	ASSERT(IAM_WRITER_IPIF(ipif));
13858 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13859 		return (0);
13860 
13861 	/*
13862 	 * Delete the existing mapping from ARP. Normally ipif_down
13863 	 * -> ipif_arp_down should send this up to ARP. The only
13864 	 * reason we would find this when we are switching from
13865 	 * Multicast to Broadcast where we did not do a down.
13866 	 */
13867 	mp = ill->ill_arp_del_mapping_mp;
13868 	if (mp != NULL) {
13869 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13870 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13871 		putnext(ill->ill_rq, mp);
13872 		ill->ill_arp_del_mapping_mp = NULL;
13873 	}
13874 
13875 	if (arp_add_mapping_mp != NULL)
13876 		*arp_add_mapping_mp = NULL;
13877 
13878 	/*
13879 	 * Check that the address is not to long for the constant
13880 	 * length reserved in the template arma_t.
13881 	 */
13882 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13883 		return (-1);
13884 
13885 	/* Add mapping mblk */
13886 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13887 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13888 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13889 	    (caddr_t)&addr);
13890 	if (add_mp == NULL)
13891 		return (-1);
13892 	arma = (arma_t *)add_mp->b_rptr;
13893 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13894 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13895 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13896 
13897 	/*
13898 	 * Determine the broadcast address.
13899 	 */
13900 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13901 	if (ill->ill_sap_length < 0)
13902 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13903 	else
13904 		bphys_addr = (uchar_t *)dlur +
13905 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13906 	/*
13907 	 * Check PHYI_MULTI_BCAST and length of physical
13908 	 * address to determine if we use the mapping or the
13909 	 * broadcast address.
13910 	 */
13911 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13912 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13913 		    bphys_addr, maddr, &hw_start, &extract_mask))
13914 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13915 
13916 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13917 	    (ill->ill_flags & ILLF_MULTICAST)) {
13918 		/* Make sure this will not match the "exact" entry. */
13919 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13920 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13921 		    (caddr_t)&addr);
13922 		if (del_mp == NULL) {
13923 			freemsg(add_mp);
13924 			return (-1);
13925 		}
13926 		bcopy(&extract_mask, (char *)arma +
13927 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13928 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13929 			/* Use link-layer broadcast address for MULTI_BCAST */
13930 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13931 			ip2dbg(("ipif_arp_setup_multicast: adding"
13932 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13933 		} else {
13934 			arma->arma_hw_mapping_start = hw_start;
13935 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13936 			    " ARP setup for %s\n", ill->ill_name));
13937 		}
13938 	} else {
13939 		freemsg(add_mp);
13940 		ASSERT(del_mp == NULL);
13941 		/* It is neither MULTICAST nor MULTI_BCAST */
13942 		return (0);
13943 	}
13944 	ASSERT(add_mp != NULL && del_mp != NULL);
13945 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13946 	ill->ill_arp_del_mapping_mp = del_mp;
13947 	if (arp_add_mapping_mp != NULL) {
13948 		/* The caller just wants the mblks allocated */
13949 		*arp_add_mapping_mp = add_mp;
13950 	} else {
13951 		/* The caller wants us to send it to arp */
13952 		putnext(ill->ill_rq, add_mp);
13953 	}
13954 	return (0);
13955 }
13956 
13957 /*
13958  * Get the resolver set up for a new interface address.
13959  * (Always called as writer.)
13960  * Called both for IPv4 and IPv6 interfaces,
13961  * though it only sets up the resolver for v6
13962  * if it's an xresolv interface (one using an external resolver).
13963  * Honors ILLF_NOARP.
13964  * The enumerated value res_act is used to tune the behavior.
13965  * If set to Res_act_initial, then we set up all the resolver
13966  * structures for a new interface.  If set to Res_act_move, then
13967  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13968  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13969  * asynchronous hardware address change notification.  If set to
13970  * Res_act_defend, then we tell ARP that it needs to send a single
13971  * gratuitous message in defense of the address.
13972  * Returns error on failure.
13973  */
13974 int
13975 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13976 {
13977 	caddr_t	addr;
13978 	mblk_t	*arp_up_mp = NULL;
13979 	mblk_t	*arp_down_mp = NULL;
13980 	mblk_t	*arp_add_mp = NULL;
13981 	mblk_t	*arp_del_mp = NULL;
13982 	mblk_t	*arp_add_mapping_mp = NULL;
13983 	mblk_t	*arp_del_mapping_mp = NULL;
13984 	ill_t	*ill = ipif->ipif_ill;
13985 	uchar_t	*area_p = NULL;
13986 	uchar_t	*ared_p = NULL;
13987 	int	err = ENOMEM;
13988 	boolean_t was_dup;
13989 
13990 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13991 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13992 	ASSERT(IAM_WRITER_IPIF(ipif));
13993 
13994 	was_dup = B_FALSE;
13995 	if (res_act == Res_act_initial) {
13996 		ipif->ipif_addr_ready = 0;
13997 		/*
13998 		 * We're bringing an interface up here.  There's no way that we
13999 		 * should need to shut down ARP now.
14000 		 */
14001 		mutex_enter(&ill->ill_lock);
14002 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
14003 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
14004 			ill->ill_ipif_dup_count--;
14005 			was_dup = B_TRUE;
14006 		}
14007 		mutex_exit(&ill->ill_lock);
14008 	}
14009 	if (ipif->ipif_recovery_id != 0)
14010 		(void) untimeout(ipif->ipif_recovery_id);
14011 	ipif->ipif_recovery_id = 0;
14012 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
14013 		ipif->ipif_addr_ready = 1;
14014 		return (0);
14015 	}
14016 	/* NDP will set the ipif_addr_ready flag when it's ready */
14017 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
14018 		return (0);
14019 
14020 	if (ill->ill_isv6) {
14021 		/*
14022 		 * External resolver for IPv6
14023 		 */
14024 		ASSERT(res_act == Res_act_initial);
14025 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
14026 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
14027 			area_p = (uchar_t *)&ip6_area_template;
14028 			ared_p = (uchar_t *)&ip6_ared_template;
14029 		}
14030 	} else {
14031 		/*
14032 		 * IPv4 arp case. If the ARP stream has already started
14033 		 * closing, fail this request for ARP bringup. Else
14034 		 * record the fact that an ARP bringup is pending.
14035 		 */
14036 		mutex_enter(&ill->ill_lock);
14037 		if (ill->ill_arp_closing) {
14038 			mutex_exit(&ill->ill_lock);
14039 			err = EINVAL;
14040 			goto failed;
14041 		} else {
14042 			if (ill->ill_ipif_up_count == 0 &&
14043 			    ill->ill_ipif_dup_count == 0 && !was_dup)
14044 				ill->ill_arp_bringup_pending = 1;
14045 			mutex_exit(&ill->ill_lock);
14046 		}
14047 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
14048 			addr = (caddr_t)&ipif->ipif_lcl_addr;
14049 			area_p = (uchar_t *)&ip_area_template;
14050 			ared_p = (uchar_t *)&ip_ared_template;
14051 		}
14052 	}
14053 
14054 	/*
14055 	 * Add an entry for the local address in ARP only if it
14056 	 * is not UNNUMBERED and the address is not INADDR_ANY.
14057 	 */
14058 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
14059 		area_t *area;
14060 
14061 		/* Now ask ARP to publish our address. */
14062 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
14063 		if (arp_add_mp == NULL)
14064 			goto failed;
14065 		area = (area_t *)arp_add_mp->b_rptr;
14066 		if (res_act != Res_act_initial) {
14067 			/*
14068 			 * Copy the new hardware address and length into
14069 			 * arp_add_mp to be sent to ARP.
14070 			 */
14071 			area->area_hw_addr_length = ill->ill_phys_addr_length;
14072 			bcopy(ill->ill_phys_addr,
14073 			    ((char *)area + area->area_hw_addr_offset),
14074 			    area->area_hw_addr_length);
14075 		}
14076 
14077 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
14078 		    ACE_F_MYADDR;
14079 
14080 		if (res_act == Res_act_defend) {
14081 			area->area_flags |= ACE_F_DEFEND;
14082 			/*
14083 			 * If we're just defending our address now, then
14084 			 * there's no need to set up ARP multicast mappings.
14085 			 * The publish command is enough.
14086 			 */
14087 			goto done;
14088 		}
14089 
14090 		if (res_act != Res_act_initial)
14091 			goto arp_setup_multicast;
14092 
14093 		/*
14094 		 * Allocate an ARP deletion message so we know we can tell ARP
14095 		 * when the interface goes down.
14096 		 */
14097 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
14098 		if (arp_del_mp == NULL)
14099 			goto failed;
14100 
14101 	} else {
14102 		if (res_act != Res_act_initial)
14103 			goto done;
14104 	}
14105 	/*
14106 	 * Need to bring up ARP or setup multicast mapping only
14107 	 * when the first interface is coming UP.
14108 	 */
14109 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
14110 	    was_dup) {
14111 		goto done;
14112 	}
14113 
14114 	/*
14115 	 * Allocate an ARP down message (to be saved) and an ARP up
14116 	 * message.
14117 	 */
14118 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
14119 	if (arp_down_mp == NULL)
14120 		goto failed;
14121 
14122 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
14123 	if (arp_up_mp == NULL)
14124 		goto failed;
14125 
14126 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
14127 		goto done;
14128 
14129 arp_setup_multicast:
14130 	/*
14131 	 * Setup the multicast mappings. This function initializes
14132 	 * ill_arp_del_mapping_mp also. This does not need to be done for
14133 	 * IPv6.
14134 	 */
14135 	if (!ill->ill_isv6) {
14136 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
14137 		if (err != 0)
14138 			goto failed;
14139 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
14140 		ASSERT(arp_add_mapping_mp != NULL);
14141 	}
14142 
14143 done:
14144 	if (arp_del_mp != NULL) {
14145 		ASSERT(ipif->ipif_arp_del_mp == NULL);
14146 		ipif->ipif_arp_del_mp = arp_del_mp;
14147 	}
14148 	if (arp_down_mp != NULL) {
14149 		ASSERT(ill->ill_arp_down_mp == NULL);
14150 		ill->ill_arp_down_mp = arp_down_mp;
14151 	}
14152 	if (arp_del_mapping_mp != NULL) {
14153 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
14154 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
14155 	}
14156 	if (arp_up_mp != NULL) {
14157 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
14158 		    ill->ill_name, ipif->ipif_id));
14159 		putnext(ill->ill_rq, arp_up_mp);
14160 	}
14161 	if (arp_add_mp != NULL) {
14162 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
14163 		    ill->ill_name, ipif->ipif_id));
14164 		/*
14165 		 * If it's an extended ARP implementation, then we'll wait to
14166 		 * hear that DAD has finished before using the interface.
14167 		 */
14168 		if (!ill->ill_arp_extend)
14169 			ipif->ipif_addr_ready = 1;
14170 		putnext(ill->ill_rq, arp_add_mp);
14171 	} else {
14172 		ipif->ipif_addr_ready = 1;
14173 	}
14174 	if (arp_add_mapping_mp != NULL) {
14175 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
14176 		    ill->ill_name, ipif->ipif_id));
14177 		putnext(ill->ill_rq, arp_add_mapping_mp);
14178 	}
14179 	if (res_act != Res_act_initial)
14180 		return (0);
14181 
14182 	if (ill->ill_flags & ILLF_NOARP)
14183 		err = ill_arp_off(ill);
14184 	else
14185 		err = ill_arp_on(ill);
14186 	if (err != 0) {
14187 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
14188 		freemsg(ipif->ipif_arp_del_mp);
14189 		freemsg(ill->ill_arp_down_mp);
14190 		freemsg(ill->ill_arp_del_mapping_mp);
14191 		ipif->ipif_arp_del_mp = NULL;
14192 		ill->ill_arp_down_mp = NULL;
14193 		ill->ill_arp_del_mapping_mp = NULL;
14194 		return (err);
14195 	}
14196 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
14197 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
14198 
14199 failed:
14200 	ip1dbg(("ipif_resolver_up: FAILED\n"));
14201 	freemsg(arp_add_mp);
14202 	freemsg(arp_del_mp);
14203 	freemsg(arp_add_mapping_mp);
14204 	freemsg(arp_up_mp);
14205 	freemsg(arp_down_mp);
14206 	ill->ill_arp_bringup_pending = 0;
14207 	return (err);
14208 }
14209 
14210 /*
14211  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
14212  * just gone back up.
14213  */
14214 static void
14215 ipif_arp_start_dad(ipif_t *ipif)
14216 {
14217 	ill_t *ill = ipif->ipif_ill;
14218 	mblk_t *arp_add_mp;
14219 	area_t *area;
14220 
14221 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
14222 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
14223 	    ipif->ipif_lcl_addr == INADDR_ANY ||
14224 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
14225 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
14226 		/*
14227 		 * If we can't contact ARP for some reason, that's not really a
14228 		 * problem.  Just send out the routing socket notification that
14229 		 * DAD completion would have done, and continue.
14230 		 */
14231 		ipif_mask_reply(ipif);
14232 		ip_rts_ifmsg(ipif);
14233 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14234 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14235 		ipif->ipif_addr_ready = 1;
14236 		return;
14237 	}
14238 
14239 	/* Setting the 'unverified' flag restarts DAD */
14240 	area = (area_t *)arp_add_mp->b_rptr;
14241 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
14242 	    ACE_F_UNVERIFIED;
14243 	putnext(ill->ill_rq, arp_add_mp);
14244 }
14245 
14246 static void
14247 ipif_ndp_start_dad(ipif_t *ipif)
14248 {
14249 	nce_t *nce;
14250 
14251 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
14252 	if (nce == NULL)
14253 		return;
14254 
14255 	if (!ndp_restart_dad(nce)) {
14256 		/*
14257 		 * If we can't restart DAD for some reason, that's not really a
14258 		 * problem.  Just send out the routing socket notification that
14259 		 * DAD completion would have done, and continue.
14260 		 */
14261 		ip_rts_ifmsg(ipif);
14262 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14263 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14264 		ipif->ipif_addr_ready = 1;
14265 	}
14266 	NCE_REFRELE(nce);
14267 }
14268 
14269 /*
14270  * Restart duplicate address detection on all interfaces on the given ill.
14271  *
14272  * This is called when an interface transitions from down to up
14273  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
14274  *
14275  * Note that since the underlying physical link has transitioned, we must cause
14276  * at least one routing socket message to be sent here, either via DAD
14277  * completion or just by default on the first ipif.  (If we don't do this, then
14278  * in.mpathd will see long delays when doing link-based failure recovery.)
14279  */
14280 void
14281 ill_restart_dad(ill_t *ill, boolean_t went_up)
14282 {
14283 	ipif_t *ipif;
14284 
14285 	if (ill == NULL)
14286 		return;
14287 
14288 	/*
14289 	 * If layer two doesn't support duplicate address detection, then just
14290 	 * send the routing socket message now and be done with it.
14291 	 */
14292 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
14293 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
14294 		ip_rts_ifmsg(ill->ill_ipif);
14295 		return;
14296 	}
14297 
14298 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14299 		if (went_up) {
14300 			if (ipif->ipif_flags & IPIF_UP) {
14301 				if (ill->ill_isv6)
14302 					ipif_ndp_start_dad(ipif);
14303 				else
14304 					ipif_arp_start_dad(ipif);
14305 			} else if (ill->ill_isv6 &&
14306 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14307 				/*
14308 				 * For IPv4, the ARP module itself will
14309 				 * automatically start the DAD process when it
14310 				 * sees DL_NOTE_LINK_UP.  We respond to the
14311 				 * AR_CN_READY at the completion of that task.
14312 				 * For IPv6, we must kick off the bring-up
14313 				 * process now.
14314 				 */
14315 				ndp_do_recovery(ipif);
14316 			} else {
14317 				/*
14318 				 * Unfortunately, the first ipif is "special"
14319 				 * and represents the underlying ill in the
14320 				 * routing socket messages.  Thus, when this
14321 				 * one ipif is down, we must still notify so
14322 				 * that the user knows the IFF_RUNNING status
14323 				 * change.  (If the first ipif is up, then
14324 				 * we'll handle eventual routing socket
14325 				 * notification via DAD completion.)
14326 				 */
14327 				if (ipif == ill->ill_ipif)
14328 					ip_rts_ifmsg(ill->ill_ipif);
14329 			}
14330 		} else {
14331 			/*
14332 			 * After link down, we'll need to send a new routing
14333 			 * message when the link comes back, so clear
14334 			 * ipif_addr_ready.
14335 			 */
14336 			ipif->ipif_addr_ready = 0;
14337 		}
14338 	}
14339 
14340 	/*
14341 	 * If we've torn down links, then notify the user right away.
14342 	 */
14343 	if (!went_up)
14344 		ip_rts_ifmsg(ill->ill_ipif);
14345 }
14346 
14347 /*
14348  * Wakeup all threads waiting to enter the ipsq, and sleeping
14349  * on any of the ills in this ipsq. The ill_lock of the ill
14350  * must be held so that waiters don't miss wakeups
14351  */
14352 static void
14353 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14354 {
14355 	phyint_t *phyint;
14356 
14357 	phyint = ipsq->ipsq_phyint_list;
14358 	while (phyint != NULL) {
14359 		if (phyint->phyint_illv4) {
14360 			if (!caller_holds_lock)
14361 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14362 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14363 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14364 			if (!caller_holds_lock)
14365 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14366 		}
14367 		if (phyint->phyint_illv6) {
14368 			if (!caller_holds_lock)
14369 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14370 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14371 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14372 			if (!caller_holds_lock)
14373 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14374 		}
14375 		phyint = phyint->phyint_ipsq_next;
14376 	}
14377 }
14378 
14379 static ipsq_t *
14380 ipsq_create(char *groupname, ip_stack_t *ipst)
14381 {
14382 	ipsq_t	*ipsq;
14383 
14384 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14385 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14386 	if (ipsq == NULL) {
14387 		return (NULL);
14388 	}
14389 
14390 	if (groupname != NULL)
14391 		(void) strcpy(ipsq->ipsq_name, groupname);
14392 	else
14393 		ipsq->ipsq_name[0] = '\0';
14394 
14395 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14396 	ipsq->ipsq_flags |= IPSQ_GROUP;
14397 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14398 	ipst->ips_ipsq_g_head = ipsq;
14399 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14400 	return (ipsq);
14401 }
14402 
14403 /*
14404  * Return an ipsq correspoding to the groupname. If 'create' is true
14405  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14406  * uniquely with an IPMP group. However during IPMP groupname operations,
14407  * multiple IPMP groups may be associated with a single ipsq. But no
14408  * IPMP group can be associated with more than 1 ipsq at any time.
14409  * For example
14410  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14411  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14412  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14413  *
14414  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14415  * status shown below during the execution of the above command.
14416  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14417  *
14418  * After the completion of the above groupname command we return to the stable
14419  * state shown below.
14420  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14421  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14422  *
14423  * Because of the above, we don't search based on the ipsq_name since that
14424  * would miss the correct ipsq during certain windows as shown above.
14425  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14426  * natural state.
14427  */
14428 static ipsq_t *
14429 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14430     ip_stack_t *ipst)
14431 {
14432 	ipsq_t	*ipsq;
14433 	int	group_len;
14434 	phyint_t *phyint;
14435 
14436 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14437 
14438 	group_len = strlen(groupname);
14439 	ASSERT(group_len != 0);
14440 	group_len++;
14441 
14442 	for (ipsq = ipst->ips_ipsq_g_head;
14443 	    ipsq != NULL;
14444 	    ipsq = ipsq->ipsq_next) {
14445 		/*
14446 		 * When an ipsq is being split, and ill_split_ipsq
14447 		 * calls this function, we exclude it from being considered.
14448 		 */
14449 		if (ipsq == exclude_ipsq)
14450 			continue;
14451 
14452 		/*
14453 		 * Compare against the ipsq_name. The groupname change happens
14454 		 * in 2 phases. The 1st phase merges the from group into
14455 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14456 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14457 		 * ipsq_name. At this point the phyint_groupname has not been
14458 		 * updated.
14459 		 */
14460 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14461 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14462 			/*
14463 			 * Verify that an ipmp groupname is exactly
14464 			 * part of 1 ipsq and is not found in any other
14465 			 * ipsq.
14466 			 */
14467 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14468 			    NULL);
14469 			return (ipsq);
14470 		}
14471 
14472 		/*
14473 		 * Comparison against ipsq_name alone is not sufficient.
14474 		 * In the case when groups are currently being
14475 		 * merged, the ipsq could hold other IPMP groups temporarily.
14476 		 * so we walk the phyint list and compare against the
14477 		 * phyint_groupname as well.
14478 		 */
14479 		phyint = ipsq->ipsq_phyint_list;
14480 		while (phyint != NULL) {
14481 			if ((group_len == phyint->phyint_groupname_len) &&
14482 			    (bcmp(phyint->phyint_groupname, groupname,
14483 			    group_len) == 0)) {
14484 				/*
14485 				 * Verify that an ipmp groupname is exactly
14486 				 * part of 1 ipsq and is not found in any other
14487 				 * ipsq.
14488 				 */
14489 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14490 				    ipst) == NULL);
14491 				return (ipsq);
14492 			}
14493 			phyint = phyint->phyint_ipsq_next;
14494 		}
14495 	}
14496 	if (create)
14497 		ipsq = ipsq_create(groupname, ipst);
14498 	return (ipsq);
14499 }
14500 
14501 static void
14502 ipsq_delete(ipsq_t *ipsq)
14503 {
14504 	ipsq_t *nipsq;
14505 	ipsq_t *pipsq = NULL;
14506 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14507 
14508 	/*
14509 	 * We don't hold the ipsq lock, but we are sure no new
14510 	 * messages can land up, since the ipsq_refs is zero.
14511 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14512 	 * is associated with this ipsq. (Lookups are based on ill_name
14513 	 * or phyint_groupname)
14514 	 */
14515 	ASSERT(ipsq->ipsq_refs == 0);
14516 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14517 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14518 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14519 		/*
14520 		 * This is not the ipsq of an IPMP group.
14521 		 */
14522 		ipsq->ipsq_ipst = NULL;
14523 		kmem_free(ipsq, sizeof (ipsq_t));
14524 		return;
14525 	}
14526 
14527 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14528 
14529 	/*
14530 	 * Locate the ipsq  before we can remove it from
14531 	 * the singly linked list of ipsq's.
14532 	 */
14533 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14534 	    nipsq = nipsq->ipsq_next) {
14535 		if (nipsq == ipsq) {
14536 			break;
14537 		}
14538 		pipsq = nipsq;
14539 	}
14540 
14541 	ASSERT(nipsq == ipsq);
14542 
14543 	/* unlink ipsq from the list */
14544 	if (pipsq != NULL)
14545 		pipsq->ipsq_next = ipsq->ipsq_next;
14546 	else
14547 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14548 	ipsq->ipsq_ipst = NULL;
14549 	kmem_free(ipsq, sizeof (ipsq_t));
14550 	rw_exit(&ipst->ips_ill_g_lock);
14551 }
14552 
14553 static void
14554 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14555     queue_t *q)
14556 {
14557 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14558 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14559 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14560 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14561 	ASSERT(current_mp != NULL);
14562 
14563 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14564 	    NEW_OP, NULL);
14565 
14566 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14567 	    new_ipsq->ipsq_xopq_mphead != NULL);
14568 
14569 	/*
14570 	 * move from old ipsq to the new ipsq.
14571 	 */
14572 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14573 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14574 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14575 
14576 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14577 }
14578 
14579 void
14580 ill_group_cleanup(ill_t *ill)
14581 {
14582 	ill_t *ill_v4;
14583 	ill_t *ill_v6;
14584 	ipif_t *ipif;
14585 
14586 	ill_v4 = ill->ill_phyint->phyint_illv4;
14587 	ill_v6 = ill->ill_phyint->phyint_illv6;
14588 
14589 	if (ill_v4 != NULL) {
14590 		mutex_enter(&ill_v4->ill_lock);
14591 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14592 		    ipif = ipif->ipif_next) {
14593 			IPIF_UNMARK_MOVING(ipif);
14594 		}
14595 		ill_v4->ill_up_ipifs = B_FALSE;
14596 		mutex_exit(&ill_v4->ill_lock);
14597 	}
14598 
14599 	if (ill_v6 != NULL) {
14600 		mutex_enter(&ill_v6->ill_lock);
14601 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14602 		    ipif = ipif->ipif_next) {
14603 			IPIF_UNMARK_MOVING(ipif);
14604 		}
14605 		ill_v6->ill_up_ipifs = B_FALSE;
14606 		mutex_exit(&ill_v6->ill_lock);
14607 	}
14608 }
14609 /*
14610  * This function is called when an ill has had a change in its group status
14611  * to bring up all the ipifs that were up before the change.
14612  */
14613 int
14614 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14615 {
14616 	ipif_t *ipif;
14617 	ill_t *ill_v4;
14618 	ill_t *ill_v6;
14619 	ill_t *from_ill;
14620 	int err = 0;
14621 
14622 
14623 	ASSERT(IAM_WRITER_ILL(ill));
14624 
14625 	/*
14626 	 * Except for ipif_state_flags and ill_state_flags the other
14627 	 * fields of the ipif/ill that are modified below are protected
14628 	 * implicitly since we are a writer. We would have tried to down
14629 	 * even an ipif that was already down, in ill_down_ipifs. So we
14630 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14631 	 */
14632 	ill_v4 = ill->ill_phyint->phyint_illv4;
14633 	ill_v6 = ill->ill_phyint->phyint_illv6;
14634 	if (ill_v4 != NULL) {
14635 		ill_v4->ill_up_ipifs = B_TRUE;
14636 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14637 		    ipif = ipif->ipif_next) {
14638 			mutex_enter(&ill_v4->ill_lock);
14639 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14640 			IPIF_UNMARK_MOVING(ipif);
14641 			mutex_exit(&ill_v4->ill_lock);
14642 			if (ipif->ipif_was_up) {
14643 				if (!(ipif->ipif_flags & IPIF_UP))
14644 					err = ipif_up(ipif, q, mp);
14645 				ipif->ipif_was_up = B_FALSE;
14646 				if (err != 0) {
14647 					/*
14648 					 * Can there be any other error ?
14649 					 */
14650 					ASSERT(err == EINPROGRESS);
14651 					return (err);
14652 				}
14653 			}
14654 		}
14655 		mutex_enter(&ill_v4->ill_lock);
14656 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14657 		mutex_exit(&ill_v4->ill_lock);
14658 		ill_v4->ill_up_ipifs = B_FALSE;
14659 		if (ill_v4->ill_move_in_progress) {
14660 			ASSERT(ill_v4->ill_move_peer != NULL);
14661 			ill_v4->ill_move_in_progress = B_FALSE;
14662 			from_ill = ill_v4->ill_move_peer;
14663 			from_ill->ill_move_in_progress = B_FALSE;
14664 			from_ill->ill_move_peer = NULL;
14665 			mutex_enter(&from_ill->ill_lock);
14666 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14667 			mutex_exit(&from_ill->ill_lock);
14668 			if (ill_v6 == NULL) {
14669 				if (from_ill->ill_phyint->phyint_flags &
14670 				    PHYI_STANDBY) {
14671 					phyint_inactive(from_ill->ill_phyint);
14672 				}
14673 				if (ill_v4->ill_phyint->phyint_flags &
14674 				    PHYI_STANDBY) {
14675 					phyint_inactive(ill_v4->ill_phyint);
14676 				}
14677 			}
14678 			ill_v4->ill_move_peer = NULL;
14679 		}
14680 	}
14681 
14682 	if (ill_v6 != NULL) {
14683 		ill_v6->ill_up_ipifs = B_TRUE;
14684 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14685 		    ipif = ipif->ipif_next) {
14686 			mutex_enter(&ill_v6->ill_lock);
14687 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14688 			IPIF_UNMARK_MOVING(ipif);
14689 			mutex_exit(&ill_v6->ill_lock);
14690 			if (ipif->ipif_was_up) {
14691 				if (!(ipif->ipif_flags & IPIF_UP))
14692 					err = ipif_up(ipif, q, mp);
14693 				ipif->ipif_was_up = B_FALSE;
14694 				if (err != 0) {
14695 					/*
14696 					 * Can there be any other error ?
14697 					 */
14698 					ASSERT(err == EINPROGRESS);
14699 					return (err);
14700 				}
14701 			}
14702 		}
14703 		mutex_enter(&ill_v6->ill_lock);
14704 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14705 		mutex_exit(&ill_v6->ill_lock);
14706 		ill_v6->ill_up_ipifs = B_FALSE;
14707 		if (ill_v6->ill_move_in_progress) {
14708 			ASSERT(ill_v6->ill_move_peer != NULL);
14709 			ill_v6->ill_move_in_progress = B_FALSE;
14710 			from_ill = ill_v6->ill_move_peer;
14711 			from_ill->ill_move_in_progress = B_FALSE;
14712 			from_ill->ill_move_peer = NULL;
14713 			mutex_enter(&from_ill->ill_lock);
14714 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14715 			mutex_exit(&from_ill->ill_lock);
14716 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14717 				phyint_inactive(from_ill->ill_phyint);
14718 			}
14719 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14720 				phyint_inactive(ill_v6->ill_phyint);
14721 			}
14722 			ill_v6->ill_move_peer = NULL;
14723 		}
14724 	}
14725 	return (0);
14726 }
14727 
14728 /*
14729  * bring down all the approriate ipifs.
14730  */
14731 /* ARGSUSED */
14732 static void
14733 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14734 {
14735 	ipif_t *ipif;
14736 
14737 	ASSERT(IAM_WRITER_ILL(ill));
14738 
14739 	/*
14740 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14741 	 * are modified below are protected implicitly since we are a writer
14742 	 */
14743 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14744 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14745 			continue;
14746 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14747 			/*
14748 			 * We go through the ipif_down logic even if the ipif
14749 			 * is already down, since routes can be added based
14750 			 * on down ipifs. Going through ipif_down once again
14751 			 * will delete any IREs created based on these routes.
14752 			 */
14753 			if (ipif->ipif_flags & IPIF_UP)
14754 				ipif->ipif_was_up = B_TRUE;
14755 			/*
14756 			 * If called with chk_nofailover true ipif is moving.
14757 			 */
14758 			mutex_enter(&ill->ill_lock);
14759 			if (chk_nofailover) {
14760 				ipif->ipif_state_flags |=
14761 				    IPIF_MOVING | IPIF_CHANGING;
14762 			} else {
14763 				ipif->ipif_state_flags |= IPIF_CHANGING;
14764 			}
14765 			mutex_exit(&ill->ill_lock);
14766 			/*
14767 			 * Need to re-create net/subnet bcast ires if
14768 			 * they are dependent on ipif.
14769 			 */
14770 			if (!ipif->ipif_isv6)
14771 				ipif_check_bcast_ires(ipif);
14772 			(void) ipif_logical_down(ipif, NULL, NULL);
14773 			ipif_non_duplicate(ipif);
14774 			ipif_down_tail(ipif);
14775 		}
14776 	}
14777 }
14778 
14779 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14780 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14781 	(ipsq)->ipsq_refs++;				\
14782 }
14783 
14784 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14785 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14786 	(ipsq)->ipsq_refs--;				\
14787 	if ((ipsq)->ipsq_refs == 0)				\
14788 		(ipsq)->ipsq_name[0] = '\0'; 		\
14789 }
14790 
14791 /*
14792  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14793  * new_ipsq.
14794  */
14795 static void
14796 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14797 {
14798 	phyint_t *phyint;
14799 	phyint_t *next_phyint;
14800 
14801 	/*
14802 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14803 	 * writer and the ill_lock of the ill in question. Also the dest
14804 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14805 	 */
14806 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14807 
14808 	phyint = cur_ipsq->ipsq_phyint_list;
14809 	cur_ipsq->ipsq_phyint_list = NULL;
14810 	while (phyint != NULL) {
14811 		next_phyint = phyint->phyint_ipsq_next;
14812 		IPSQ_DEC_REF(cur_ipsq, ipst);
14813 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14814 		new_ipsq->ipsq_phyint_list = phyint;
14815 		IPSQ_INC_REF(new_ipsq, ipst);
14816 		phyint->phyint_ipsq = new_ipsq;
14817 		phyint = next_phyint;
14818 	}
14819 }
14820 
14821 #define	SPLIT_SUCCESS		0
14822 #define	SPLIT_NOT_NEEDED	1
14823 #define	SPLIT_FAILED		2
14824 
14825 int
14826 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14827     ip_stack_t *ipst)
14828 {
14829 	ipsq_t *newipsq = NULL;
14830 
14831 	/*
14832 	 * Assertions denote pre-requisites for changing the ipsq of
14833 	 * a phyint
14834 	 */
14835 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14836 	/*
14837 	 * <ill-phyint> assocs can't change while ill_g_lock
14838 	 * is held as writer. See ill_phyint_reinit()
14839 	 */
14840 	ASSERT(phyint->phyint_illv4 == NULL ||
14841 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14842 	ASSERT(phyint->phyint_illv6 == NULL ||
14843 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14844 
14845 	if ((phyint->phyint_groupname_len !=
14846 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14847 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14848 	    phyint->phyint_groupname_len) != 0)) {
14849 		/*
14850 		 * Once we fail in creating a new ipsq due to memory shortage,
14851 		 * don't attempt to create new ipsq again, based on another
14852 		 * phyint, since we want all phyints belonging to an IPMP group
14853 		 * to be in the same ipsq even in the event of mem alloc fails.
14854 		 */
14855 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14856 		    cur_ipsq, ipst);
14857 		if (newipsq == NULL) {
14858 			/* Memory allocation failure */
14859 			return (SPLIT_FAILED);
14860 		} else {
14861 			/* ipsq_refs protected by ill_g_lock (writer) */
14862 			IPSQ_DEC_REF(cur_ipsq, ipst);
14863 			phyint->phyint_ipsq = newipsq;
14864 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14865 			newipsq->ipsq_phyint_list = phyint;
14866 			IPSQ_INC_REF(newipsq, ipst);
14867 			return (SPLIT_SUCCESS);
14868 		}
14869 	}
14870 	return (SPLIT_NOT_NEEDED);
14871 }
14872 
14873 /*
14874  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14875  * to do this split
14876  */
14877 static int
14878 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14879 {
14880 	ipsq_t *newipsq;
14881 
14882 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14883 	/*
14884 	 * <ill-phyint> assocs can't change while ill_g_lock
14885 	 * is held as writer. See ill_phyint_reinit()
14886 	 */
14887 
14888 	ASSERT(phyint->phyint_illv4 == NULL ||
14889 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14890 	ASSERT(phyint->phyint_illv6 == NULL ||
14891 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14892 
14893 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14894 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14895 		/*
14896 		 * ipsq_init failed due to no memory
14897 		 * caller will use the same ipsq
14898 		 */
14899 		return (SPLIT_FAILED);
14900 	}
14901 
14902 	/* ipsq_ref is protected by ill_g_lock (writer) */
14903 	IPSQ_DEC_REF(cur_ipsq, ipst);
14904 
14905 	/*
14906 	 * This is a new ipsq that is unknown to the world.
14907 	 * So we don't need to hold ipsq_lock,
14908 	 */
14909 	newipsq = phyint->phyint_ipsq;
14910 	newipsq->ipsq_writer = NULL;
14911 	newipsq->ipsq_reentry_cnt--;
14912 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14913 #ifdef ILL_DEBUG
14914 	newipsq->ipsq_depth = 0;
14915 #endif
14916 
14917 	return (SPLIT_SUCCESS);
14918 }
14919 
14920 /*
14921  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14922  * ipsq's representing their individual groups or themselves. Return
14923  * whether split needs to be retried again later.
14924  */
14925 static boolean_t
14926 ill_split_ipsq(ipsq_t *cur_ipsq)
14927 {
14928 	phyint_t *phyint;
14929 	phyint_t *next_phyint;
14930 	int	error;
14931 	boolean_t need_retry = B_FALSE;
14932 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14933 
14934 	phyint = cur_ipsq->ipsq_phyint_list;
14935 	cur_ipsq->ipsq_phyint_list = NULL;
14936 	while (phyint != NULL) {
14937 		next_phyint = phyint->phyint_ipsq_next;
14938 		/*
14939 		 * 'created' will tell us whether the callee actually
14940 		 * created an ipsq. Lack of memory may force the callee
14941 		 * to return without creating an ipsq.
14942 		 */
14943 		if (phyint->phyint_groupname == NULL) {
14944 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14945 		} else {
14946 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14947 			    need_retry, ipst);
14948 		}
14949 
14950 		switch (error) {
14951 		case SPLIT_FAILED:
14952 			need_retry = B_TRUE;
14953 			/* FALLTHRU */
14954 		case SPLIT_NOT_NEEDED:
14955 			/*
14956 			 * Keep it on the list.
14957 			 */
14958 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14959 			cur_ipsq->ipsq_phyint_list = phyint;
14960 			break;
14961 		case SPLIT_SUCCESS:
14962 			break;
14963 		default:
14964 			ASSERT(0);
14965 		}
14966 
14967 		phyint = next_phyint;
14968 	}
14969 	return (need_retry);
14970 }
14971 
14972 /*
14973  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14974  * and return the ills in the list. This list will be
14975  * needed to unlock all the ills later on by the caller.
14976  * The <ill-ipsq> associations could change between the
14977  * lock and unlock. Hence the unlock can't traverse the
14978  * ipsq to get the list of ills.
14979  */
14980 static int
14981 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14982 {
14983 	int	cnt = 0;
14984 	phyint_t	*phyint;
14985 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14986 
14987 	/*
14988 	 * The caller holds ill_g_lock to ensure that the ill memberships
14989 	 * of the ipsq don't change
14990 	 */
14991 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14992 
14993 	phyint = ipsq->ipsq_phyint_list;
14994 	while (phyint != NULL) {
14995 		if (phyint->phyint_illv4 != NULL) {
14996 			ASSERT(cnt < list_max);
14997 			list[cnt++] = phyint->phyint_illv4;
14998 		}
14999 		if (phyint->phyint_illv6 != NULL) {
15000 			ASSERT(cnt < list_max);
15001 			list[cnt++] = phyint->phyint_illv6;
15002 		}
15003 		phyint = phyint->phyint_ipsq_next;
15004 	}
15005 	ill_lock_ills(list, cnt);
15006 	return (cnt);
15007 }
15008 
15009 void
15010 ill_lock_ills(ill_t **list, int cnt)
15011 {
15012 	int	i;
15013 
15014 	if (cnt > 1) {
15015 		boolean_t try_again;
15016 		do {
15017 			try_again = B_FALSE;
15018 			for (i = 0; i < cnt - 1; i++) {
15019 				if (list[i] < list[i + 1]) {
15020 					ill_t	*tmp;
15021 
15022 					/* swap the elements */
15023 					tmp = list[i];
15024 					list[i] = list[i + 1];
15025 					list[i + 1] = tmp;
15026 					try_again = B_TRUE;
15027 				}
15028 			}
15029 		} while (try_again);
15030 	}
15031 
15032 	for (i = 0; i < cnt; i++) {
15033 		if (i == 0) {
15034 			if (list[i] != NULL)
15035 				mutex_enter(&list[i]->ill_lock);
15036 			else
15037 				return;
15038 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
15039 			mutex_enter(&list[i]->ill_lock);
15040 		}
15041 	}
15042 }
15043 
15044 void
15045 ill_unlock_ills(ill_t **list, int cnt)
15046 {
15047 	int	i;
15048 
15049 	for (i = 0; i < cnt; i++) {
15050 		if ((i == 0) && (list[i] != NULL)) {
15051 			mutex_exit(&list[i]->ill_lock);
15052 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
15053 			mutex_exit(&list[i]->ill_lock);
15054 		}
15055 	}
15056 }
15057 
15058 /*
15059  * Merge all the ills from 1 ipsq group into another ipsq group.
15060  * The source ipsq group is specified by the ipsq associated with
15061  * 'from_ill'. The destination ipsq group is specified by the ipsq
15062  * associated with 'to_ill' or 'groupname' respectively.
15063  * Note that ipsq itself does not have a reference count mechanism
15064  * and functions don't look up an ipsq and pass it around. Instead
15065  * functions pass around an ill or groupname, and the ipsq is looked
15066  * up from the ill or groupname and the required operation performed
15067  * atomically with the lookup on the ipsq.
15068  */
15069 static int
15070 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
15071     queue_t *q)
15072 {
15073 	ipsq_t *old_ipsq;
15074 	ipsq_t *new_ipsq;
15075 	ill_t	**ill_list;
15076 	int	cnt;
15077 	size_t	ill_list_size;
15078 	boolean_t became_writer_on_new_sq = B_FALSE;
15079 	ip_stack_t	*ipst = from_ill->ill_ipst;
15080 
15081 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
15082 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
15083 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
15084 
15085 	/*
15086 	 * Need to hold ill_g_lock as writer and also the ill_lock to
15087 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
15088 	 * ipsq_lock to prevent new messages from landing on an ipsq.
15089 	 */
15090 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15091 
15092 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
15093 	if (groupname != NULL)
15094 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
15095 	else {
15096 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
15097 	}
15098 
15099 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
15100 
15101 	/*
15102 	 * both groups are on the same ipsq.
15103 	 */
15104 	if (old_ipsq == new_ipsq) {
15105 		rw_exit(&ipst->ips_ill_g_lock);
15106 		return (0);
15107 	}
15108 
15109 	cnt = old_ipsq->ipsq_refs << 1;
15110 	ill_list_size = cnt * sizeof (ill_t *);
15111 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
15112 	if (ill_list == NULL) {
15113 		rw_exit(&ipst->ips_ill_g_lock);
15114 		return (ENOMEM);
15115 	}
15116 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
15117 
15118 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
15119 	mutex_enter(&new_ipsq->ipsq_lock);
15120 	if ((new_ipsq->ipsq_writer == NULL &&
15121 	    new_ipsq->ipsq_current_ipif == NULL) ||
15122 	    (new_ipsq->ipsq_writer == curthread)) {
15123 		new_ipsq->ipsq_writer = curthread;
15124 		new_ipsq->ipsq_reentry_cnt++;
15125 		became_writer_on_new_sq = B_TRUE;
15126 	}
15127 
15128 	/*
15129 	 * We are holding ill_g_lock as writer and all the ill locks of
15130 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
15131 	 * message can land up on the old ipsq even though we don't hold the
15132 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
15133 	 */
15134 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
15135 
15136 	/*
15137 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
15138 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
15139 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
15140 	 */
15141 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
15142 
15143 	/*
15144 	 * Mark the new ipsq as needing a split since it is currently
15145 	 * being shared by more than 1 IPMP group. The split will
15146 	 * occur at the end of ipsq_exit
15147 	 */
15148 	new_ipsq->ipsq_split = B_TRUE;
15149 
15150 	/* Now release all the locks */
15151 	mutex_exit(&new_ipsq->ipsq_lock);
15152 	ill_unlock_ills(ill_list, cnt);
15153 	rw_exit(&ipst->ips_ill_g_lock);
15154 
15155 	kmem_free(ill_list, ill_list_size);
15156 
15157 	/*
15158 	 * If we succeeded in becoming writer on the new ipsq, then
15159 	 * drain the new ipsq and start processing  all enqueued messages
15160 	 * including the current ioctl we are processing which is either
15161 	 * a set groupname or failover/failback.
15162 	 */
15163 	if (became_writer_on_new_sq)
15164 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
15165 
15166 	/*
15167 	 * syncq has been changed and all the messages have been moved.
15168 	 */
15169 	mutex_enter(&old_ipsq->ipsq_lock);
15170 	old_ipsq->ipsq_current_ipif = NULL;
15171 	old_ipsq->ipsq_current_ioctl = 0;
15172 	mutex_exit(&old_ipsq->ipsq_lock);
15173 	return (EINPROGRESS);
15174 }
15175 
15176 /*
15177  * Delete and add the loopback copy and non-loopback copy of
15178  * the BROADCAST ire corresponding to ill and addr. Used to
15179  * group broadcast ires together when ill becomes part of
15180  * a group.
15181  *
15182  * This function is also called when ill is leaving the group
15183  * so that the ires belonging to the group gets re-grouped.
15184  */
15185 static void
15186 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
15187 {
15188 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
15189 	ire_t **ire_ptpn = &ire_head;
15190 	ip_stack_t	*ipst = ill->ill_ipst;
15191 
15192 	/*
15193 	 * The loopback and non-loopback IREs are inserted in the order in which
15194 	 * they're found, on the basis that they are correctly ordered (loopback
15195 	 * first).
15196 	 */
15197 	for (;;) {
15198 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15199 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15200 		if (ire == NULL)
15201 			break;
15202 
15203 		/*
15204 		 * we are passing in KM_SLEEP because it is not easy to
15205 		 * go back to a sane state in case of memory failure.
15206 		 */
15207 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
15208 		ASSERT(nire != NULL);
15209 		bzero(nire, sizeof (ire_t));
15210 		/*
15211 		 * Don't use ire_max_frag directly since we don't
15212 		 * hold on to 'ire' until we add the new ire 'nire' and
15213 		 * we don't want the new ire to have a dangling reference
15214 		 * to 'ire'. The ire_max_frag of a broadcast ire must
15215 		 * be in sync with the ipif_mtu of the associate ipif.
15216 		 * For eg. this happens as a result of SIOCSLIFNAME,
15217 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
15218 		 * the driver. A change in ire_max_frag triggered as
15219 		 * as a result of path mtu discovery, or due to an
15220 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
15221 		 * route change -mtu command does not apply to broadcast ires.
15222 		 *
15223 		 * XXX We need a recovery strategy here if ire_init fails
15224 		 */
15225 		if (ire_init(nire,
15226 		    (uchar_t *)&ire->ire_addr,
15227 		    (uchar_t *)&ire->ire_mask,
15228 		    (uchar_t *)&ire->ire_src_addr,
15229 		    (uchar_t *)&ire->ire_gateway_addr,
15230 		    (uchar_t *)&ire->ire_in_src_addr,
15231 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
15232 		    &ire->ire_ipif->ipif_mtu,
15233 		    ire->ire_nce,
15234 		    ire->ire_rfq,
15235 		    ire->ire_stq,
15236 		    ire->ire_type,
15237 		    ire->ire_ipif,
15238 		    ire->ire_in_ill,
15239 		    ire->ire_cmask,
15240 		    ire->ire_phandle,
15241 		    ire->ire_ihandle,
15242 		    ire->ire_flags,
15243 		    &ire->ire_uinfo,
15244 		    NULL,
15245 		    NULL,
15246 		    ipst) == NULL) {
15247 			cmn_err(CE_PANIC, "ire_init() failed");
15248 		}
15249 		ire_delete(ire);
15250 		ire_refrele(ire);
15251 
15252 		/*
15253 		 * The newly created IREs are inserted at the tail of the list
15254 		 * starting with ire_head. As we've just allocated them no one
15255 		 * knows about them so it's safe.
15256 		 */
15257 		*ire_ptpn = nire;
15258 		ire_ptpn = &nire->ire_next;
15259 	}
15260 
15261 	for (nire = ire_head; nire != NULL; nire = nire_next) {
15262 		int error;
15263 		ire_t *oire;
15264 		/* unlink the IRE from our list before calling ire_add() */
15265 		nire_next = nire->ire_next;
15266 		nire->ire_next = NULL;
15267 
15268 		/* ire_add adds the ire at the right place in the list */
15269 		oire = nire;
15270 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
15271 		ASSERT(error == 0);
15272 		ASSERT(oire == nire);
15273 		ire_refrele(nire);	/* Held in ire_add */
15274 	}
15275 }
15276 
15277 /*
15278  * This function is usually called when an ill is inserted in
15279  * a group and all the ipifs are already UP. As all the ipifs
15280  * are already UP, the broadcast ires have already been created
15281  * and been inserted. But, ire_add_v4 would not have grouped properly.
15282  * We need to re-group for the benefit of ip_wput_ire which
15283  * expects BROADCAST ires to be grouped properly to avoid sending
15284  * more than one copy of the broadcast packet per group.
15285  *
15286  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
15287  *	  because when ipif_up_done ends up calling this, ires have
15288  *        already been added before illgrp_insert i.e before ill_group
15289  *	  has been initialized.
15290  */
15291 static void
15292 ill_group_bcast_for_xmit(ill_t *ill)
15293 {
15294 	ill_group_t *illgrp;
15295 	ipif_t *ipif;
15296 	ipaddr_t addr;
15297 	ipaddr_t net_mask;
15298 	ipaddr_t subnet_netmask;
15299 
15300 	illgrp = ill->ill_group;
15301 
15302 	/*
15303 	 * This function is called even when an ill is deleted from
15304 	 * the group. Hence, illgrp could be null.
15305 	 */
15306 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15307 		return;
15308 
15309 	/*
15310 	 * Delete all the BROADCAST ires matching this ill and add
15311 	 * them back. This time, ire_add_v4 should take care of
15312 	 * grouping them with others because ill is part of the
15313 	 * group.
15314 	 */
15315 	ill_bcast_delete_and_add(ill, 0);
15316 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15317 
15318 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15319 
15320 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15321 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15322 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15323 		} else {
15324 			net_mask = htonl(IN_CLASSA_NET);
15325 		}
15326 		addr = net_mask & ipif->ipif_subnet;
15327 		ill_bcast_delete_and_add(ill, addr);
15328 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15329 
15330 		subnet_netmask = ipif->ipif_net_mask;
15331 		addr = ipif->ipif_subnet;
15332 		ill_bcast_delete_and_add(ill, addr);
15333 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15334 	}
15335 }
15336 
15337 /*
15338  * This function is called from illgrp_delete when ill is being deleted
15339  * from the group.
15340  *
15341  * As ill is not there in the group anymore, any address belonging
15342  * to this ill should be cleared of IRE_MARK_NORECV.
15343  */
15344 static void
15345 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15346 {
15347 	ire_t *ire;
15348 	irb_t *irb;
15349 	ip_stack_t	*ipst = ill->ill_ipst;
15350 
15351 	ASSERT(ill->ill_group == NULL);
15352 
15353 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15354 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15355 
15356 	if (ire != NULL) {
15357 		/*
15358 		 * IPMP and plumbing operations are serialized on the ipsq, so
15359 		 * no one will insert or delete a broadcast ire under our feet.
15360 		 */
15361 		irb = ire->ire_bucket;
15362 		rw_enter(&irb->irb_lock, RW_READER);
15363 		ire_refrele(ire);
15364 
15365 		for (; ire != NULL; ire = ire->ire_next) {
15366 			if (ire->ire_addr != addr)
15367 				break;
15368 			if (ire_to_ill(ire) != ill)
15369 				continue;
15370 
15371 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15372 			ire->ire_marks &= ~IRE_MARK_NORECV;
15373 		}
15374 		rw_exit(&irb->irb_lock);
15375 	}
15376 }
15377 
15378 /*
15379  * This function must be called only after the broadcast ires
15380  * have been grouped together. For a given address addr, nominate
15381  * only one of the ires whose interface is not FAILED or OFFLINE.
15382  *
15383  * This is also called when an ipif goes down, so that we can nominate
15384  * a different ire with the same address for receiving.
15385  */
15386 static void
15387 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15388 {
15389 	irb_t *irb;
15390 	ire_t *ire;
15391 	ire_t *ire1;
15392 	ire_t *save_ire;
15393 	ire_t **irep = NULL;
15394 	boolean_t first = B_TRUE;
15395 	ire_t *clear_ire = NULL;
15396 	ire_t *start_ire = NULL;
15397 	ire_t	*new_lb_ire;
15398 	ire_t	*new_nlb_ire;
15399 	boolean_t new_lb_ire_used = B_FALSE;
15400 	boolean_t new_nlb_ire_used = B_FALSE;
15401 	uint64_t match_flags;
15402 	uint64_t phyi_flags;
15403 	boolean_t fallback = B_FALSE;
15404 	uint_t	max_frag;
15405 
15406 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15407 	    NULL, MATCH_IRE_TYPE, ipst);
15408 	/*
15409 	 * We may not be able to find some ires if a previous
15410 	 * ire_create failed. This happens when an ipif goes
15411 	 * down and we are unable to create BROADCAST ires due
15412 	 * to memory failure. Thus, we have to check for NULL
15413 	 * below. This should handle the case for LOOPBACK,
15414 	 * POINTOPOINT and interfaces with some POINTOPOINT
15415 	 * logicals for which there are no BROADCAST ires.
15416 	 */
15417 	if (ire == NULL)
15418 		return;
15419 	/*
15420 	 * Currently IRE_BROADCASTS are deleted when an ipif
15421 	 * goes down which runs exclusively. Thus, setting
15422 	 * IRE_MARK_RCVD should not race with ire_delete marking
15423 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15424 	 * be consistent with other parts of the code that walks
15425 	 * a given bucket.
15426 	 */
15427 	save_ire = ire;
15428 	irb = ire->ire_bucket;
15429 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15430 	if (new_lb_ire == NULL) {
15431 		ire_refrele(ire);
15432 		return;
15433 	}
15434 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15435 	if (new_nlb_ire == NULL) {
15436 		ire_refrele(ire);
15437 		kmem_cache_free(ire_cache, new_lb_ire);
15438 		return;
15439 	}
15440 	IRB_REFHOLD(irb);
15441 	rw_enter(&irb->irb_lock, RW_WRITER);
15442 	/*
15443 	 * Get to the first ire matching the address and the
15444 	 * group. If the address does not match we are done
15445 	 * as we could not find the IRE. If the address matches
15446 	 * we should get to the first one matching the group.
15447 	 */
15448 	while (ire != NULL) {
15449 		if (ire->ire_addr != addr ||
15450 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15451 			break;
15452 		}
15453 		ire = ire->ire_next;
15454 	}
15455 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15456 	start_ire = ire;
15457 redo:
15458 	while (ire != NULL && ire->ire_addr == addr &&
15459 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15460 		/*
15461 		 * The first ire for any address within a group
15462 		 * should always be the one with IRE_MARK_NORECV cleared
15463 		 * so that ip_wput_ire can avoid searching for one.
15464 		 * Note down the insertion point which will be used
15465 		 * later.
15466 		 */
15467 		if (first && (irep == NULL))
15468 			irep = ire->ire_ptpn;
15469 		/*
15470 		 * PHYI_FAILED is set when the interface fails.
15471 		 * This interface might have become good, but the
15472 		 * daemon has not yet detected. We should still
15473 		 * not receive on this. PHYI_OFFLINE should never
15474 		 * be picked as this has been offlined and soon
15475 		 * be removed.
15476 		 */
15477 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15478 		if (phyi_flags & PHYI_OFFLINE) {
15479 			ire->ire_marks |= IRE_MARK_NORECV;
15480 			ire = ire->ire_next;
15481 			continue;
15482 		}
15483 		if (phyi_flags & match_flags) {
15484 			ire->ire_marks |= IRE_MARK_NORECV;
15485 			ire = ire->ire_next;
15486 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15487 			    PHYI_INACTIVE) {
15488 				fallback = B_TRUE;
15489 			}
15490 			continue;
15491 		}
15492 		if (first) {
15493 			/*
15494 			 * We will move this to the front of the list later
15495 			 * on.
15496 			 */
15497 			clear_ire = ire;
15498 			ire->ire_marks &= ~IRE_MARK_NORECV;
15499 		} else {
15500 			ire->ire_marks |= IRE_MARK_NORECV;
15501 		}
15502 		first = B_FALSE;
15503 		ire = ire->ire_next;
15504 	}
15505 	/*
15506 	 * If we never nominated anybody, try nominating at least
15507 	 * an INACTIVE, if we found one. Do it only once though.
15508 	 */
15509 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15510 	    fallback) {
15511 		match_flags = PHYI_FAILED;
15512 		ire = start_ire;
15513 		irep = NULL;
15514 		goto redo;
15515 	}
15516 	ire_refrele(save_ire);
15517 
15518 	/*
15519 	 * irep non-NULL indicates that we entered the while loop
15520 	 * above. If clear_ire is at the insertion point, we don't
15521 	 * have to do anything. clear_ire will be NULL if all the
15522 	 * interfaces are failed.
15523 	 *
15524 	 * We cannot unlink and reinsert the ire at the right place
15525 	 * in the list since there can be other walkers of this bucket.
15526 	 * Instead we delete and recreate the ire
15527 	 */
15528 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15529 		ire_t *clear_ire_stq = NULL;
15530 
15531 		bzero(new_lb_ire, sizeof (ire_t));
15532 		/* XXX We need a recovery strategy here. */
15533 		if (ire_init(new_lb_ire,
15534 		    (uchar_t *)&clear_ire->ire_addr,
15535 		    (uchar_t *)&clear_ire->ire_mask,
15536 		    (uchar_t *)&clear_ire->ire_src_addr,
15537 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15538 		    (uchar_t *)&clear_ire->ire_in_src_addr,
15539 		    &clear_ire->ire_max_frag,
15540 		    NULL, /* let ire_nce_init derive the resolver info */
15541 		    clear_ire->ire_rfq,
15542 		    clear_ire->ire_stq,
15543 		    clear_ire->ire_type,
15544 		    clear_ire->ire_ipif,
15545 		    clear_ire->ire_in_ill,
15546 		    clear_ire->ire_cmask,
15547 		    clear_ire->ire_phandle,
15548 		    clear_ire->ire_ihandle,
15549 		    clear_ire->ire_flags,
15550 		    &clear_ire->ire_uinfo,
15551 		    NULL,
15552 		    NULL,
15553 		    ipst) == NULL)
15554 			cmn_err(CE_PANIC, "ire_init() failed");
15555 		if (clear_ire->ire_stq == NULL) {
15556 			ire_t *ire_next = clear_ire->ire_next;
15557 			if (ire_next != NULL &&
15558 			    ire_next->ire_stq != NULL &&
15559 			    ire_next->ire_addr == clear_ire->ire_addr &&
15560 			    ire_next->ire_ipif->ipif_ill ==
15561 			    clear_ire->ire_ipif->ipif_ill) {
15562 				clear_ire_stq = ire_next;
15563 
15564 				bzero(new_nlb_ire, sizeof (ire_t));
15565 				/* XXX We need a recovery strategy here. */
15566 				if (ire_init(new_nlb_ire,
15567 				    (uchar_t *)&clear_ire_stq->ire_addr,
15568 				    (uchar_t *)&clear_ire_stq->ire_mask,
15569 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15570 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15571 				    (uchar_t *)&clear_ire_stq->ire_in_src_addr,
15572 				    &clear_ire_stq->ire_max_frag,
15573 				    NULL,
15574 				    clear_ire_stq->ire_rfq,
15575 				    clear_ire_stq->ire_stq,
15576 				    clear_ire_stq->ire_type,
15577 				    clear_ire_stq->ire_ipif,
15578 				    clear_ire_stq->ire_in_ill,
15579 				    clear_ire_stq->ire_cmask,
15580 				    clear_ire_stq->ire_phandle,
15581 				    clear_ire_stq->ire_ihandle,
15582 				    clear_ire_stq->ire_flags,
15583 				    &clear_ire_stq->ire_uinfo,
15584 				    NULL,
15585 				    NULL,
15586 				    ipst) == NULL)
15587 					cmn_err(CE_PANIC, "ire_init() failed");
15588 			}
15589 		}
15590 
15591 		/*
15592 		 * Delete the ire. We can't call ire_delete() since
15593 		 * we are holding the bucket lock. We can't release the
15594 		 * bucket lock since we can't allow irep to change. So just
15595 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15596 		 * ire from the list and do the refrele.
15597 		 */
15598 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15599 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15600 
15601 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15602 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15603 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15604 		}
15605 
15606 		/*
15607 		 * Also take care of otherfields like ib/ob pkt count
15608 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15609 		 */
15610 
15611 		/* Set the max_frag before adding the ire */
15612 		max_frag = *new_lb_ire->ire_max_fragp;
15613 		new_lb_ire->ire_max_fragp = NULL;
15614 		new_lb_ire->ire_max_frag = max_frag;
15615 
15616 		/* Add the new ire's. Insert at *irep */
15617 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15618 		ire1 = *irep;
15619 		if (ire1 != NULL)
15620 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15621 		new_lb_ire->ire_next = ire1;
15622 		/* Link the new one in. */
15623 		new_lb_ire->ire_ptpn = irep;
15624 		membar_producer();
15625 		*irep = new_lb_ire;
15626 		new_lb_ire_used = B_TRUE;
15627 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15628 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15629 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15630 
15631 		if (clear_ire_stq != NULL) {
15632 			/* Set the max_frag before adding the ire */
15633 			max_frag = *new_nlb_ire->ire_max_fragp;
15634 			new_nlb_ire->ire_max_fragp = NULL;
15635 			new_nlb_ire->ire_max_frag = max_frag;
15636 
15637 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15638 			irep = &new_lb_ire->ire_next;
15639 			/* Add the new ire. Insert at *irep */
15640 			ire1 = *irep;
15641 			if (ire1 != NULL)
15642 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15643 			new_nlb_ire->ire_next = ire1;
15644 			/* Link the new one in. */
15645 			new_nlb_ire->ire_ptpn = irep;
15646 			membar_producer();
15647 			*irep = new_nlb_ire;
15648 			new_nlb_ire_used = B_TRUE;
15649 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15650 			    ire_stats_inserted);
15651 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15652 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15653 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
15654 		}
15655 	}
15656 	rw_exit(&irb->irb_lock);
15657 	if (!new_lb_ire_used)
15658 		kmem_cache_free(ire_cache, new_lb_ire);
15659 	if (!new_nlb_ire_used)
15660 		kmem_cache_free(ire_cache, new_nlb_ire);
15661 	IRB_REFRELE(irb);
15662 }
15663 
15664 /*
15665  * Whenever an ipif goes down we have to renominate a different
15666  * broadcast ire to receive. Whenever an ipif comes up, we need
15667  * to make sure that we have only one nominated to receive.
15668  */
15669 static void
15670 ipif_renominate_bcast(ipif_t *ipif)
15671 {
15672 	ill_t *ill = ipif->ipif_ill;
15673 	ipaddr_t subnet_addr;
15674 	ipaddr_t net_addr;
15675 	ipaddr_t net_mask = 0;
15676 	ipaddr_t subnet_netmask;
15677 	ipaddr_t addr;
15678 	ill_group_t *illgrp;
15679 	ip_stack_t	*ipst = ill->ill_ipst;
15680 
15681 	illgrp = ill->ill_group;
15682 	/*
15683 	 * If this is the last ipif going down, it might take
15684 	 * the ill out of the group. In that case ipif_down ->
15685 	 * illgrp_delete takes care of doing the nomination.
15686 	 * ipif_down does not call for this case.
15687 	 */
15688 	ASSERT(illgrp != NULL);
15689 
15690 	/* There could not have been any ires associated with this */
15691 	if (ipif->ipif_subnet == 0)
15692 		return;
15693 
15694 	ill_mark_bcast(illgrp, 0, ipst);
15695 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15696 
15697 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15698 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15699 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15700 	} else {
15701 		net_mask = htonl(IN_CLASSA_NET);
15702 	}
15703 	addr = net_mask & ipif->ipif_subnet;
15704 	ill_mark_bcast(illgrp, addr, ipst);
15705 
15706 	net_addr = ~net_mask | addr;
15707 	ill_mark_bcast(illgrp, net_addr, ipst);
15708 
15709 	subnet_netmask = ipif->ipif_net_mask;
15710 	addr = ipif->ipif_subnet;
15711 	ill_mark_bcast(illgrp, addr, ipst);
15712 
15713 	subnet_addr = ~subnet_netmask | addr;
15714 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15715 }
15716 
15717 /*
15718  * Whenever we form or delete ill groups, we need to nominate one set of
15719  * BROADCAST ires for receiving in the group.
15720  *
15721  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15722  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15723  *    for ill_ipif_up_count to be non-zero. This is the only case where
15724  *    ill_ipif_up_count is zero and we would still find the ires.
15725  *
15726  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15727  *    ipif is UP and we just have to do the nomination.
15728  *
15729  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15730  *    from the group. So, we have to do the nomination.
15731  *
15732  * Because of (3), there could be just one ill in the group. But we have
15733  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15734  * Thus, this function does not optimize when there is only one ill as
15735  * it is not correct for (3).
15736  */
15737 static void
15738 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15739 {
15740 	ill_t *ill;
15741 	ipif_t *ipif;
15742 	ipaddr_t subnet_addr;
15743 	ipaddr_t prev_subnet_addr = 0;
15744 	ipaddr_t net_addr;
15745 	ipaddr_t prev_net_addr = 0;
15746 	ipaddr_t net_mask = 0;
15747 	ipaddr_t subnet_netmask;
15748 	ipaddr_t addr;
15749 	ip_stack_t	*ipst;
15750 
15751 	/*
15752 	 * When the last memeber is leaving, there is nothing to
15753 	 * nominate.
15754 	 */
15755 	if (illgrp->illgrp_ill_count == 0) {
15756 		ASSERT(illgrp->illgrp_ill == NULL);
15757 		return;
15758 	}
15759 
15760 	ill = illgrp->illgrp_ill;
15761 	ASSERT(!ill->ill_isv6);
15762 	ipst = ill->ill_ipst;
15763 	/*
15764 	 * We assume that ires with same address and belonging to the
15765 	 * same group, has been grouped together. Nominating a *single*
15766 	 * ill in the group for sending and receiving broadcast is done
15767 	 * by making sure that the first BROADCAST ire (which will be
15768 	 * the one returned by ire_ctable_lookup for ip_rput and the
15769 	 * one that will be used in ip_wput_ire) will be the one that
15770 	 * will not have IRE_MARK_NORECV set.
15771 	 *
15772 	 * 1) ip_rput checks and discards packets received on ires marked
15773 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15774 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15775 	 *    first ire in the group for every broadcast address in the group.
15776 	 *    ip_rput will accept packets only on the first ire i.e only
15777 	 *    one copy of the ill.
15778 	 *
15779 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15780 	 *    packet for the whole group. It needs to send out on the ill
15781 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15782 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15783 	 *    the copy echoed back on other port where the ire is not marked
15784 	 *    with IRE_MARK_NORECV.
15785 	 *
15786 	 * Note that we just need to have the first IRE either loopback or
15787 	 * non-loopback (either of them may not exist if ire_create failed
15788 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15789 	 * always hit the first one and hence will always accept one copy.
15790 	 *
15791 	 * We have a broadcast ire per ill for all the unique prefixes
15792 	 * hosted on that ill. As we don't have a way of knowing the
15793 	 * unique prefixes on a given ill and hence in the whole group,
15794 	 * we just call ill_mark_bcast on all the prefixes that exist
15795 	 * in the group. For the common case of one prefix, the code
15796 	 * below optimizes by remebering the last address used for
15797 	 * markng. In the case of multiple prefixes, this will still
15798 	 * optimize depending the order of prefixes.
15799 	 *
15800 	 * The only unique address across the whole group is 0.0.0.0 and
15801 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15802 	 * the first ire in the bucket for receiving and disables the
15803 	 * others.
15804 	 */
15805 	ill_mark_bcast(illgrp, 0, ipst);
15806 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15807 	for (; ill != NULL; ill = ill->ill_group_next) {
15808 
15809 		for (ipif = ill->ill_ipif; ipif != NULL;
15810 		    ipif = ipif->ipif_next) {
15811 
15812 			if (!(ipif->ipif_flags & IPIF_UP) ||
15813 			    ipif->ipif_subnet == 0) {
15814 				continue;
15815 			}
15816 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15817 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15818 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15819 			} else {
15820 				net_mask = htonl(IN_CLASSA_NET);
15821 			}
15822 			addr = net_mask & ipif->ipif_subnet;
15823 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15824 				ill_mark_bcast(illgrp, addr, ipst);
15825 				net_addr = ~net_mask | addr;
15826 				ill_mark_bcast(illgrp, net_addr, ipst);
15827 			}
15828 			prev_net_addr = addr;
15829 
15830 			subnet_netmask = ipif->ipif_net_mask;
15831 			addr = ipif->ipif_subnet;
15832 			if (prev_subnet_addr == 0 ||
15833 			    prev_subnet_addr != addr) {
15834 				ill_mark_bcast(illgrp, addr, ipst);
15835 				subnet_addr = ~subnet_netmask | addr;
15836 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15837 			}
15838 			prev_subnet_addr = addr;
15839 		}
15840 	}
15841 }
15842 
15843 /*
15844  * This function is called while forming ill groups.
15845  *
15846  * Currently, we handle only allmulti groups. We want to join
15847  * allmulti on only one of the ills in the groups. In future,
15848  * when we have link aggregation, we may have to join normal
15849  * multicast groups on multiple ills as switch does inbound load
15850  * balancing. Following are the functions that calls this
15851  * function :
15852  *
15853  * 1) ill_recover_multicast : Interface is coming back UP.
15854  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15855  *    will call ill_recover_multicast to recover all the multicast
15856  *    groups. We need to make sure that only one member is joined
15857  *    in the ill group.
15858  *
15859  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15860  *    Somebody is joining allmulti. We need to make sure that only one
15861  *    member is joined in the group.
15862  *
15863  * 3) illgrp_insert : If allmulti has already joined, we need to make
15864  *    sure that only one member is joined in the group.
15865  *
15866  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15867  *    allmulti who we have nominated. We need to pick someother ill.
15868  *
15869  * 5) illgrp_delete : The ill we nominated is leaving the group,
15870  *    we need to pick a new ill to join the group.
15871  *
15872  * For (1), (2), (5) - we just have to check whether there is
15873  * a good ill joined in the group. If we could not find any ills
15874  * joined the group, we should join.
15875  *
15876  * For (4), the one that was nominated to receive, left the group.
15877  * There could be nobody joined in the group when this function is
15878  * called.
15879  *
15880  * For (3) - we need to explicitly check whether there are multiple
15881  * ills joined in the group.
15882  *
15883  * For simplicity, we don't differentiate any of the above cases. We
15884  * just leave the group if it is joined on any of them and join on
15885  * the first good ill.
15886  */
15887 int
15888 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15889 {
15890 	ilm_t *ilm;
15891 	ill_t *ill;
15892 	ill_t *fallback_inactive_ill = NULL;
15893 	ill_t *fallback_failed_ill = NULL;
15894 	int ret = 0;
15895 
15896 	/*
15897 	 * Leave the allmulti on all the ills and start fresh.
15898 	 */
15899 	for (ill = illgrp->illgrp_ill; ill != NULL;
15900 	    ill = ill->ill_group_next) {
15901 		if (ill->ill_join_allmulti)
15902 			(void) ip_leave_allmulti(ill->ill_ipif);
15903 	}
15904 
15905 	/*
15906 	 * Choose a good ill. Fallback to inactive or failed if
15907 	 * none available. We need to fallback to FAILED in the
15908 	 * case where we have 2 interfaces in a group - where
15909 	 * one of them is failed and another is a good one and
15910 	 * the good one (not marked inactive) is leaving the group.
15911 	 */
15912 	ret = 0;
15913 	for (ill = illgrp->illgrp_ill; ill != NULL;
15914 	    ill = ill->ill_group_next) {
15915 		/* Never pick an offline interface */
15916 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15917 			continue;
15918 
15919 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15920 			fallback_failed_ill = ill;
15921 			continue;
15922 		}
15923 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15924 			fallback_inactive_ill = ill;
15925 			continue;
15926 		}
15927 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15928 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15929 				ret = ip_join_allmulti(ill->ill_ipif);
15930 				/*
15931 				 * ip_join_allmulti can fail because of memory
15932 				 * failures. So, make sure we join at least
15933 				 * on one ill.
15934 				 */
15935 				if (ill->ill_join_allmulti)
15936 					return (0);
15937 			}
15938 		}
15939 	}
15940 	if (ret != 0) {
15941 		/*
15942 		 * If we tried nominating above and failed to do so,
15943 		 * return error. We might have tried multiple times.
15944 		 * But, return the latest error.
15945 		 */
15946 		return (ret);
15947 	}
15948 	if ((ill = fallback_inactive_ill) != NULL) {
15949 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15950 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15951 				ret = ip_join_allmulti(ill->ill_ipif);
15952 				return (ret);
15953 			}
15954 		}
15955 	} else if ((ill = fallback_failed_ill) != NULL) {
15956 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15957 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15958 				ret = ip_join_allmulti(ill->ill_ipif);
15959 				return (ret);
15960 			}
15961 		}
15962 	}
15963 	return (0);
15964 }
15965 
15966 /*
15967  * This function is called from illgrp_delete after it is
15968  * deleted from the group to reschedule responsibilities
15969  * to a different ill.
15970  */
15971 static void
15972 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15973 {
15974 	ilm_t	*ilm;
15975 	ipif_t	*ipif;
15976 	ipaddr_t subnet_addr;
15977 	ipaddr_t net_addr;
15978 	ipaddr_t net_mask = 0;
15979 	ipaddr_t subnet_netmask;
15980 	ipaddr_t addr;
15981 	ip_stack_t *ipst = ill->ill_ipst;
15982 
15983 	ASSERT(ill->ill_group == NULL);
15984 	/*
15985 	 * Broadcast Responsibility:
15986 	 *
15987 	 * 1. If this ill has been nominated for receiving broadcast
15988 	 * packets, we need to find a new one. Before we find a new
15989 	 * one, we need to re-group the ires that are part of this new
15990 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15991 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15992 	 * thing for us.
15993 	 *
15994 	 * 2. If this ill was not nominated for receiving broadcast
15995 	 * packets, we need to clear the IRE_MARK_NORECV flag
15996 	 * so that we continue to send up broadcast packets.
15997 	 */
15998 	if (!ill->ill_isv6) {
15999 		/*
16000 		 * Case 1 above : No optimization here. Just redo the
16001 		 * nomination.
16002 		 */
16003 		ill_group_bcast_for_xmit(ill);
16004 		ill_nominate_bcast_rcv(illgrp);
16005 
16006 		/*
16007 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
16008 		 */
16009 		ill_clear_bcast_mark(ill, 0);
16010 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
16011 
16012 		for (ipif = ill->ill_ipif; ipif != NULL;
16013 		    ipif = ipif->ipif_next) {
16014 
16015 			if (!(ipif->ipif_flags & IPIF_UP) ||
16016 			    ipif->ipif_subnet == 0) {
16017 				continue;
16018 			}
16019 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
16020 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
16021 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
16022 			} else {
16023 				net_mask = htonl(IN_CLASSA_NET);
16024 			}
16025 			addr = net_mask & ipif->ipif_subnet;
16026 			ill_clear_bcast_mark(ill, addr);
16027 
16028 			net_addr = ~net_mask | addr;
16029 			ill_clear_bcast_mark(ill, net_addr);
16030 
16031 			subnet_netmask = ipif->ipif_net_mask;
16032 			addr = ipif->ipif_subnet;
16033 			ill_clear_bcast_mark(ill, addr);
16034 
16035 			subnet_addr = ~subnet_netmask | addr;
16036 			ill_clear_bcast_mark(ill, subnet_addr);
16037 		}
16038 	}
16039 
16040 	/*
16041 	 * Multicast Responsibility.
16042 	 *
16043 	 * If we have joined allmulti on this one, find a new member
16044 	 * in the group to join allmulti. As this ill is already part
16045 	 * of allmulti, we don't have to join on this one.
16046 	 *
16047 	 * If we have not joined allmulti on this one, there is no
16048 	 * responsibility to handoff. But we need to take new
16049 	 * responsibility i.e, join allmulti on this one if we need
16050 	 * to.
16051 	 */
16052 	if (ill->ill_join_allmulti) {
16053 		(void) ill_nominate_mcast_rcv(illgrp);
16054 	} else {
16055 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16056 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16057 				(void) ip_join_allmulti(ill->ill_ipif);
16058 				break;
16059 			}
16060 		}
16061 	}
16062 
16063 	/*
16064 	 * We intentionally do the flushing of IRE_CACHES only matching
16065 	 * on the ill and not on groups. Note that we are already deleted
16066 	 * from the group.
16067 	 *
16068 	 * This will make sure that all IRE_CACHES whose stq is pointing
16069 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
16070 	 * deleted and IRE_CACHES that are not pointing at this ill will
16071 	 * be left alone.
16072 	 */
16073 	if (ill->ill_isv6) {
16074 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
16075 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
16076 	} else {
16077 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
16078 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
16079 	}
16080 
16081 	/*
16082 	 * Some conn may have cached one of the IREs deleted above. By removing
16083 	 * the ire reference, we clean up the extra reference to the ill held in
16084 	 * ire->ire_stq.
16085 	 */
16086 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
16087 
16088 	/*
16089 	 * Re-do source address selection for all the members in the
16090 	 * group, if they borrowed source address from one of the ipifs
16091 	 * in this ill.
16092 	 */
16093 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
16094 		if (ill->ill_isv6) {
16095 			ipif_update_other_ipifs_v6(ipif, illgrp);
16096 		} else {
16097 			ipif_update_other_ipifs(ipif, illgrp);
16098 		}
16099 	}
16100 }
16101 
16102 /*
16103  * Delete the ill from the group. The caller makes sure that it is
16104  * in a group and it okay to delete from the group. So, we always
16105  * delete here.
16106  */
16107 static void
16108 illgrp_delete(ill_t *ill)
16109 {
16110 	ill_group_t *illgrp;
16111 	ill_group_t *tmpg;
16112 	ill_t *tmp_ill;
16113 	ip_stack_t	*ipst = ill->ill_ipst;
16114 
16115 	/*
16116 	 * Reset illgrp_ill_schednext if it was pointing at us.
16117 	 * We need to do this before we set ill_group to NULL.
16118 	 */
16119 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16120 	mutex_enter(&ill->ill_lock);
16121 
16122 	illgrp_reset_schednext(ill);
16123 
16124 	illgrp = ill->ill_group;
16125 
16126 	/* Delete the ill from illgrp. */
16127 	if (illgrp->illgrp_ill == ill) {
16128 		illgrp->illgrp_ill = ill->ill_group_next;
16129 	} else {
16130 		tmp_ill = illgrp->illgrp_ill;
16131 		while (tmp_ill->ill_group_next != ill) {
16132 			tmp_ill = tmp_ill->ill_group_next;
16133 			ASSERT(tmp_ill != NULL);
16134 		}
16135 		tmp_ill->ill_group_next = ill->ill_group_next;
16136 	}
16137 	ill->ill_group = NULL;
16138 	ill->ill_group_next = NULL;
16139 
16140 	illgrp->illgrp_ill_count--;
16141 	mutex_exit(&ill->ill_lock);
16142 	rw_exit(&ipst->ips_ill_g_lock);
16143 
16144 	/*
16145 	 * As this ill is leaving the group, we need to hand off
16146 	 * the responsibilities to the other ills in the group, if
16147 	 * this ill had some responsibilities.
16148 	 */
16149 
16150 	ill_handoff_responsibility(ill, illgrp);
16151 
16152 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16153 
16154 	if (illgrp->illgrp_ill_count == 0) {
16155 
16156 		ASSERT(illgrp->illgrp_ill == NULL);
16157 		if (ill->ill_isv6) {
16158 			if (illgrp == ipst->ips_illgrp_head_v6) {
16159 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
16160 			} else {
16161 				tmpg = ipst->ips_illgrp_head_v6;
16162 				while (tmpg->illgrp_next != illgrp) {
16163 					tmpg = tmpg->illgrp_next;
16164 					ASSERT(tmpg != NULL);
16165 				}
16166 				tmpg->illgrp_next = illgrp->illgrp_next;
16167 			}
16168 		} else {
16169 			if (illgrp == ipst->ips_illgrp_head_v4) {
16170 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
16171 			} else {
16172 				tmpg = ipst->ips_illgrp_head_v4;
16173 				while (tmpg->illgrp_next != illgrp) {
16174 					tmpg = tmpg->illgrp_next;
16175 					ASSERT(tmpg != NULL);
16176 				}
16177 				tmpg->illgrp_next = illgrp->illgrp_next;
16178 			}
16179 		}
16180 		mutex_destroy(&illgrp->illgrp_lock);
16181 		mi_free(illgrp);
16182 	}
16183 	rw_exit(&ipst->ips_ill_g_lock);
16184 
16185 	/*
16186 	 * Even though the ill is out of the group its not necessary
16187 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
16188 	 * We will split the ipsq when phyint_groupname is set to NULL.
16189 	 */
16190 
16191 	/*
16192 	 * Send a routing sockets message if we are deleting from
16193 	 * groups with names.
16194 	 */
16195 	if (ill->ill_phyint->phyint_groupname_len != 0)
16196 		ip_rts_ifmsg(ill->ill_ipif);
16197 }
16198 
16199 /*
16200  * Re-do source address selection. This is normally called when
16201  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
16202  * ipif comes up.
16203  */
16204 void
16205 ill_update_source_selection(ill_t *ill)
16206 {
16207 	ipif_t *ipif;
16208 
16209 	ASSERT(IAM_WRITER_ILL(ill));
16210 
16211 	if (ill->ill_group != NULL)
16212 		ill = ill->ill_group->illgrp_ill;
16213 
16214 	for (; ill != NULL; ill = ill->ill_group_next) {
16215 		for (ipif = ill->ill_ipif; ipif != NULL;
16216 		    ipif = ipif->ipif_next) {
16217 			if (ill->ill_isv6)
16218 				ipif_recreate_interface_routes_v6(NULL, ipif);
16219 			else
16220 				ipif_recreate_interface_routes(NULL, ipif);
16221 		}
16222 	}
16223 }
16224 
16225 /*
16226  * Insert ill in a group headed by illgrp_head. The caller can either
16227  * pass a groupname in which case we search for a group with the
16228  * same name to insert in or pass a group to insert in. This function
16229  * would only search groups with names.
16230  *
16231  * NOTE : The caller should make sure that there is at least one ipif
16232  *	  UP on this ill so that illgrp_scheduler can pick this ill
16233  *	  for outbound packets. If ill_ipif_up_count is zero, we have
16234  *	  already sent a DL_UNBIND to the driver and we don't want to
16235  *	  send anymore packets. We don't assert for ipif_up_count
16236  *	  to be greater than zero, because ipif_up_done wants to call
16237  *	  this function before bumping up the ipif_up_count. See
16238  *	  ipif_up_done() for details.
16239  */
16240 int
16241 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
16242     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
16243 {
16244 	ill_group_t *illgrp;
16245 	ill_t *prev_ill;
16246 	phyint_t *phyi;
16247 	ip_stack_t	*ipst = ill->ill_ipst;
16248 
16249 	ASSERT(ill->ill_group == NULL);
16250 
16251 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16252 	mutex_enter(&ill->ill_lock);
16253 
16254 	if (groupname != NULL) {
16255 		/*
16256 		 * Look for a group with a matching groupname to insert.
16257 		 */
16258 		for (illgrp = *illgrp_head; illgrp != NULL;
16259 		    illgrp = illgrp->illgrp_next) {
16260 
16261 			ill_t *tmp_ill;
16262 
16263 			/*
16264 			 * If we have an ill_group_t in the list which has
16265 			 * no ill_t assigned then we must be in the process of
16266 			 * removing this group. We skip this as illgrp_delete()
16267 			 * will remove it from the list.
16268 			 */
16269 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
16270 				ASSERT(illgrp->illgrp_ill_count == 0);
16271 				continue;
16272 			}
16273 
16274 			ASSERT(tmp_ill->ill_phyint != NULL);
16275 			phyi = tmp_ill->ill_phyint;
16276 			/*
16277 			 * Look at groups which has names only.
16278 			 */
16279 			if (phyi->phyint_groupname_len == 0)
16280 				continue;
16281 			/*
16282 			 * Names are stored in the phyint common to both
16283 			 * IPv4 and IPv6.
16284 			 */
16285 			if (mi_strcmp(phyi->phyint_groupname,
16286 			    groupname) == 0) {
16287 				break;
16288 			}
16289 		}
16290 	} else {
16291 		/*
16292 		 * If the caller passes in a NULL "grp_to_insert", we
16293 		 * allocate one below and insert this singleton.
16294 		 */
16295 		illgrp = grp_to_insert;
16296 	}
16297 
16298 	ill->ill_group_next = NULL;
16299 
16300 	if (illgrp == NULL) {
16301 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16302 		if (illgrp == NULL) {
16303 			return (ENOMEM);
16304 		}
16305 		illgrp->illgrp_next = *illgrp_head;
16306 		*illgrp_head = illgrp;
16307 		illgrp->illgrp_ill = ill;
16308 		illgrp->illgrp_ill_count = 1;
16309 		ill->ill_group = illgrp;
16310 		/*
16311 		 * Used in illgrp_scheduler to protect multiple threads
16312 		 * from traversing the list.
16313 		 */
16314 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16315 	} else {
16316 		ASSERT(ill->ill_net_type ==
16317 		    illgrp->illgrp_ill->ill_net_type);
16318 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16319 
16320 		/* Insert ill at tail of this group */
16321 		prev_ill = illgrp->illgrp_ill;
16322 		while (prev_ill->ill_group_next != NULL)
16323 			prev_ill = prev_ill->ill_group_next;
16324 		prev_ill->ill_group_next = ill;
16325 		ill->ill_group = illgrp;
16326 		illgrp->illgrp_ill_count++;
16327 		/*
16328 		 * Inherit group properties. Currently only forwarding
16329 		 * is the property we try to keep the same with all the
16330 		 * ills. When there are more, we will abstract this into
16331 		 * a function.
16332 		 */
16333 		ill->ill_flags &= ~ILLF_ROUTER;
16334 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16335 	}
16336 	mutex_exit(&ill->ill_lock);
16337 	rw_exit(&ipst->ips_ill_g_lock);
16338 
16339 	/*
16340 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16341 	 *    may be zero as it has not yet been bumped. But the ires
16342 	 *    have already been added. So, we do the nomination here
16343 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16344 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16345 	 *    ill_ipif_up_count here while nominating broadcast ires for
16346 	 *    receive.
16347 	 *
16348 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16349 	 *    to group them properly as ire_add() has already happened
16350 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16351 	 *    case, we need to do it here anyway.
16352 	 */
16353 	if (!ill->ill_isv6) {
16354 		ill_group_bcast_for_xmit(ill);
16355 		ill_nominate_bcast_rcv(illgrp);
16356 	}
16357 
16358 	if (!ipif_is_coming_up) {
16359 		/*
16360 		 * When ipif_up_done() calls this function, the multicast
16361 		 * groups have not been joined yet. So, there is no point in
16362 		 * nomination. ip_join_allmulti will handle groups when
16363 		 * ill_recover_multicast is called from ipif_up_done() later.
16364 		 */
16365 		(void) ill_nominate_mcast_rcv(illgrp);
16366 		/*
16367 		 * ipif_up_done calls ill_update_source_selection
16368 		 * anyway. Moreover, we don't want to re-create
16369 		 * interface routes while ipif_up_done() still has reference
16370 		 * to them. Refer to ipif_up_done() for more details.
16371 		 */
16372 		ill_update_source_selection(ill);
16373 	}
16374 
16375 	/*
16376 	 * Send a routing sockets message if we are inserting into
16377 	 * groups with names.
16378 	 */
16379 	if (groupname != NULL)
16380 		ip_rts_ifmsg(ill->ill_ipif);
16381 	return (0);
16382 }
16383 
16384 /*
16385  * Return the first phyint matching the groupname. There could
16386  * be more than one when there are ill groups.
16387  *
16388  * If 'usable' is set, then we exclude ones that are marked with any of
16389  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16390  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16391  * emulation of ipmp.
16392  */
16393 phyint_t *
16394 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16395 {
16396 	phyint_t *phyi;
16397 
16398 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16399 	/*
16400 	 * Group names are stored in the phyint - a common structure
16401 	 * to both IPv4 and IPv6.
16402 	 */
16403 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16404 	for (; phyi != NULL;
16405 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16406 	    phyi, AVL_AFTER)) {
16407 		if (phyi->phyint_groupname_len == 0)
16408 			continue;
16409 		/*
16410 		 * Skip the ones that should not be used since the callers
16411 		 * sometime use this for sending packets.
16412 		 */
16413 		if (usable && (phyi->phyint_flags &
16414 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16415 			continue;
16416 
16417 		ASSERT(phyi->phyint_groupname != NULL);
16418 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16419 			return (phyi);
16420 	}
16421 	return (NULL);
16422 }
16423 
16424 
16425 /*
16426  * Return the first usable phyint matching the group index. By 'usable'
16427  * we exclude ones that are marked ununsable with any of
16428  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16429  *
16430  * Used only for the ipmp/netinfo emulation of ipmp.
16431  */
16432 phyint_t *
16433 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16434 {
16435 	phyint_t *phyi;
16436 
16437 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16438 
16439 	if (!ipst->ips_ipmp_hook_emulation)
16440 		return (NULL);
16441 
16442 	/*
16443 	 * Group indicies are stored in the phyint - a common structure
16444 	 * to both IPv4 and IPv6.
16445 	 */
16446 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16447 	for (; phyi != NULL;
16448 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16449 	    phyi, AVL_AFTER)) {
16450 		/* Ignore the ones that do not have a group */
16451 		if (phyi->phyint_groupname_len == 0)
16452 			continue;
16453 
16454 		ASSERT(phyi->phyint_group_ifindex != 0);
16455 		/*
16456 		 * Skip the ones that should not be used since the callers
16457 		 * sometime use this for sending packets.
16458 		 */
16459 		if (phyi->phyint_flags &
16460 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16461 			continue;
16462 		if (phyi->phyint_group_ifindex == group_ifindex)
16463 			return (phyi);
16464 	}
16465 	return (NULL);
16466 }
16467 
16468 
16469 /*
16470  * MT notes on creation and deletion of IPMP groups
16471  *
16472  * Creation and deletion of IPMP groups introduce the need to merge or
16473  * split the associated serialization objects i.e the ipsq's. Normally all
16474  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16475  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16476  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16477  * is a need to change the <ill-ipsq> association and we have to operate on both
16478  * the source and destination IPMP groups. For eg. attempting to set the
16479  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16480  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16481  * source or destination IPMP group are mapped to a single ipsq for executing
16482  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16483  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16484  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16485  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16486  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16487  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16488  *
16489  * In the above example the ioctl handling code locates the current ipsq of hme0
16490  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16491  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16492  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16493  * the destination ipsq. If the destination ipsq is not busy, it also enters
16494  * the destination ipsq exclusively. Now the actual groupname setting operation
16495  * can proceed. If the destination ipsq is busy, the operation is enqueued
16496  * on the destination (merged) ipsq and will be handled in the unwind from
16497  * ipsq_exit.
16498  *
16499  * To prevent other threads accessing the ill while the group name change is
16500  * in progres, we bring down the ipifs which also removes the ill from the
16501  * group. The group is changed in phyint and when the first ipif on the ill
16502  * is brought up, the ill is inserted into the right IPMP group by
16503  * illgrp_insert.
16504  */
16505 /* ARGSUSED */
16506 int
16507 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16508     ip_ioctl_cmd_t *ipip, void *ifreq)
16509 {
16510 	int i;
16511 	char *tmp;
16512 	int namelen;
16513 	ill_t *ill = ipif->ipif_ill;
16514 	ill_t *ill_v4, *ill_v6;
16515 	int err = 0;
16516 	phyint_t *phyi;
16517 	phyint_t *phyi_tmp;
16518 	struct lifreq *lifr;
16519 	mblk_t	*mp1;
16520 	char *groupname;
16521 	ipsq_t *ipsq;
16522 	ip_stack_t	*ipst = ill->ill_ipst;
16523 
16524 	ASSERT(IAM_WRITER_IPIF(ipif));
16525 
16526 	/* Existance verified in ip_wput_nondata */
16527 	mp1 = mp->b_cont->b_cont;
16528 	lifr = (struct lifreq *)mp1->b_rptr;
16529 	groupname = lifr->lifr_groupname;
16530 
16531 	if (ipif->ipif_id != 0)
16532 		return (EINVAL);
16533 
16534 	phyi = ill->ill_phyint;
16535 	ASSERT(phyi != NULL);
16536 
16537 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16538 		return (EINVAL);
16539 
16540 	tmp = groupname;
16541 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16542 		;
16543 
16544 	if (i == LIFNAMSIZ) {
16545 		/* no null termination */
16546 		return (EINVAL);
16547 	}
16548 
16549 	/*
16550 	 * Calculate the namelen exclusive of the null
16551 	 * termination character.
16552 	 */
16553 	namelen = tmp - groupname;
16554 
16555 	ill_v4 = phyi->phyint_illv4;
16556 	ill_v6 = phyi->phyint_illv6;
16557 
16558 	/*
16559 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16560 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16561 	 * synchronization notes in ip.c
16562 	 */
16563 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16564 		return (EINVAL);
16565 	}
16566 
16567 	/*
16568 	 * mark the ill as changing.
16569 	 * this should queue all new requests on the syncq.
16570 	 */
16571 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16572 
16573 	if (ill_v4 != NULL)
16574 		ill_v4->ill_state_flags |= ILL_CHANGING;
16575 	if (ill_v6 != NULL)
16576 		ill_v6->ill_state_flags |= ILL_CHANGING;
16577 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16578 
16579 	if (namelen == 0) {
16580 		/*
16581 		 * Null string means remove this interface from the
16582 		 * existing group.
16583 		 */
16584 		if (phyi->phyint_groupname_len == 0) {
16585 			/*
16586 			 * Never was in a group.
16587 			 */
16588 			err = 0;
16589 			goto done;
16590 		}
16591 
16592 		/*
16593 		 * IPv4 or IPv6 may be temporarily out of the group when all
16594 		 * the ipifs are down. Thus, we need to check for ill_group to
16595 		 * be non-NULL.
16596 		 */
16597 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16598 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16599 			mutex_enter(&ill_v4->ill_lock);
16600 			if (!ill_is_quiescent(ill_v4)) {
16601 				/*
16602 				 * ipsq_pending_mp_add will not fail since
16603 				 * connp is NULL
16604 				 */
16605 				(void) ipsq_pending_mp_add(NULL,
16606 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16607 				mutex_exit(&ill_v4->ill_lock);
16608 				err = EINPROGRESS;
16609 				goto done;
16610 			}
16611 			mutex_exit(&ill_v4->ill_lock);
16612 		}
16613 
16614 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16615 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16616 			mutex_enter(&ill_v6->ill_lock);
16617 			if (!ill_is_quiescent(ill_v6)) {
16618 				(void) ipsq_pending_mp_add(NULL,
16619 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16620 				mutex_exit(&ill_v6->ill_lock);
16621 				err = EINPROGRESS;
16622 				goto done;
16623 			}
16624 			mutex_exit(&ill_v6->ill_lock);
16625 		}
16626 
16627 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16628 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16629 		mutex_enter(&phyi->phyint_lock);
16630 		ASSERT(phyi->phyint_groupname != NULL);
16631 		mi_free(phyi->phyint_groupname);
16632 		phyi->phyint_groupname = NULL;
16633 		phyi->phyint_groupname_len = 0;
16634 
16635 		/* Restore the ifindex used to be the per interface one */
16636 		phyi->phyint_group_ifindex = 0;
16637 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16638 		mutex_exit(&phyi->phyint_lock);
16639 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16640 		rw_exit(&ipst->ips_ill_g_lock);
16641 		err = ill_up_ipifs(ill, q, mp);
16642 
16643 		/*
16644 		 * set the split flag so that the ipsq can be split
16645 		 */
16646 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16647 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16648 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16649 
16650 	} else {
16651 		if (phyi->phyint_groupname_len != 0) {
16652 			ASSERT(phyi->phyint_groupname != NULL);
16653 			/* Are we inserting in the same group ? */
16654 			if (mi_strcmp(groupname,
16655 			    phyi->phyint_groupname) == 0) {
16656 				err = 0;
16657 				goto done;
16658 			}
16659 		}
16660 
16661 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16662 		/*
16663 		 * Merge ipsq for the group's.
16664 		 * This check is here as multiple groups/ills might be
16665 		 * sharing the same ipsq.
16666 		 * If we have to merege than the operation is restarted
16667 		 * on the new ipsq.
16668 		 */
16669 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16670 		if (phyi->phyint_ipsq != ipsq) {
16671 			rw_exit(&ipst->ips_ill_g_lock);
16672 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16673 			goto done;
16674 		}
16675 		/*
16676 		 * Running exclusive on new ipsq.
16677 		 */
16678 
16679 		ASSERT(ipsq != NULL);
16680 		ASSERT(ipsq->ipsq_writer == curthread);
16681 
16682 		/*
16683 		 * Check whether the ill_type and ill_net_type matches before
16684 		 * we allocate any memory so that the cleanup is easier.
16685 		 *
16686 		 * We can't group dissimilar ones as we can't load spread
16687 		 * packets across the group because of potential link-level
16688 		 * header differences.
16689 		 */
16690 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16691 		if (phyi_tmp != NULL) {
16692 			if ((ill_v4 != NULL &&
16693 			    phyi_tmp->phyint_illv4 != NULL) &&
16694 			    ((ill_v4->ill_net_type !=
16695 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16696 			    (ill_v4->ill_type !=
16697 			    phyi_tmp->phyint_illv4->ill_type))) {
16698 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16699 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16700 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16701 				rw_exit(&ipst->ips_ill_g_lock);
16702 				return (EINVAL);
16703 			}
16704 			if ((ill_v6 != NULL &&
16705 			    phyi_tmp->phyint_illv6 != NULL) &&
16706 			    ((ill_v6->ill_net_type !=
16707 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16708 			    (ill_v6->ill_type !=
16709 			    phyi_tmp->phyint_illv6->ill_type))) {
16710 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16711 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16712 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16713 				rw_exit(&ipst->ips_ill_g_lock);
16714 				return (EINVAL);
16715 			}
16716 		}
16717 
16718 		rw_exit(&ipst->ips_ill_g_lock);
16719 
16720 		/*
16721 		 * bring down all v4 ipifs.
16722 		 */
16723 		if (ill_v4 != NULL) {
16724 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16725 		}
16726 
16727 		/*
16728 		 * bring down all v6 ipifs.
16729 		 */
16730 		if (ill_v6 != NULL) {
16731 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16732 		}
16733 
16734 		/*
16735 		 * make sure all ipifs are down and there are no active
16736 		 * references. Call to ipsq_pending_mp_add will not fail
16737 		 * since connp is NULL.
16738 		 */
16739 		if (ill_v4 != NULL) {
16740 			mutex_enter(&ill_v4->ill_lock);
16741 			if (!ill_is_quiescent(ill_v4)) {
16742 				(void) ipsq_pending_mp_add(NULL,
16743 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16744 				mutex_exit(&ill_v4->ill_lock);
16745 				err = EINPROGRESS;
16746 				goto done;
16747 			}
16748 			mutex_exit(&ill_v4->ill_lock);
16749 		}
16750 
16751 		if (ill_v6 != NULL) {
16752 			mutex_enter(&ill_v6->ill_lock);
16753 			if (!ill_is_quiescent(ill_v6)) {
16754 				(void) ipsq_pending_mp_add(NULL,
16755 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16756 				mutex_exit(&ill_v6->ill_lock);
16757 				err = EINPROGRESS;
16758 				goto done;
16759 			}
16760 			mutex_exit(&ill_v6->ill_lock);
16761 		}
16762 
16763 		/*
16764 		 * allocate including space for null terminator
16765 		 * before we insert.
16766 		 */
16767 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16768 		if (tmp == NULL)
16769 			return (ENOMEM);
16770 
16771 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16772 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16773 		mutex_enter(&phyi->phyint_lock);
16774 		if (phyi->phyint_groupname_len != 0) {
16775 			ASSERT(phyi->phyint_groupname != NULL);
16776 			mi_free(phyi->phyint_groupname);
16777 		}
16778 
16779 		/*
16780 		 * setup the new group name.
16781 		 */
16782 		phyi->phyint_groupname = tmp;
16783 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16784 		phyi->phyint_groupname_len = namelen + 1;
16785 
16786 		if (ipst->ips_ipmp_hook_emulation) {
16787 			/*
16788 			 * If the group already exists we use the existing
16789 			 * group_ifindex, otherwise we pick a new index here.
16790 			 */
16791 			if (phyi_tmp != NULL) {
16792 				phyi->phyint_group_ifindex =
16793 				    phyi_tmp->phyint_group_ifindex;
16794 			} else {
16795 				/* XXX We need a recovery strategy here. */
16796 				if (!ip_assign_ifindex(
16797 				    &phyi->phyint_group_ifindex, ipst))
16798 					cmn_err(CE_PANIC,
16799 					    "ip_assign_ifindex() failed");
16800 			}
16801 		}
16802 		/*
16803 		 * Select whether the netinfo and hook use the per-interface
16804 		 * or per-group ifindex.
16805 		 */
16806 		if (ipst->ips_ipmp_hook_emulation)
16807 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16808 		else
16809 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16810 
16811 		if (ipst->ips_ipmp_hook_emulation &&
16812 		    phyi_tmp != NULL) {
16813 			/* First phyint in group - group PLUMB event */
16814 			ill_nic_info_plumb(ill, B_TRUE);
16815 		}
16816 		mutex_exit(&phyi->phyint_lock);
16817 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16818 		rw_exit(&ipst->ips_ill_g_lock);
16819 
16820 		err = ill_up_ipifs(ill, q, mp);
16821 	}
16822 
16823 done:
16824 	/*
16825 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16826 	 */
16827 	if (err != EINPROGRESS) {
16828 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16829 		if (ill_v4 != NULL)
16830 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16831 		if (ill_v6 != NULL)
16832 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16833 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16834 	}
16835 	return (err);
16836 }
16837 
16838 /* ARGSUSED */
16839 int
16840 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16841     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16842 {
16843 	ill_t *ill;
16844 	phyint_t *phyi;
16845 	struct lifreq *lifr;
16846 	mblk_t	*mp1;
16847 
16848 	/* Existence verified in ip_wput_nondata */
16849 	mp1 = mp->b_cont->b_cont;
16850 	lifr = (struct lifreq *)mp1->b_rptr;
16851 	ill = ipif->ipif_ill;
16852 	phyi = ill->ill_phyint;
16853 
16854 	lifr->lifr_groupname[0] = '\0';
16855 	/*
16856 	 * ill_group may be null if all the interfaces
16857 	 * are down. But still, the phyint should always
16858 	 * hold the name.
16859 	 */
16860 	if (phyi->phyint_groupname_len != 0) {
16861 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16862 		    phyi->phyint_groupname_len);
16863 	}
16864 
16865 	return (0);
16866 }
16867 
16868 
16869 typedef struct conn_move_s {
16870 	ill_t	*cm_from_ill;
16871 	ill_t	*cm_to_ill;
16872 	int	cm_ifindex;
16873 } conn_move_t;
16874 
16875 /*
16876  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16877  */
16878 static void
16879 conn_move(conn_t *connp, caddr_t arg)
16880 {
16881 	conn_move_t *connm;
16882 	int ifindex;
16883 	int i;
16884 	ill_t *from_ill;
16885 	ill_t *to_ill;
16886 	ilg_t *ilg;
16887 	ilm_t *ret_ilm;
16888 
16889 	connm = (conn_move_t *)arg;
16890 	ifindex = connm->cm_ifindex;
16891 	from_ill = connm->cm_from_ill;
16892 	to_ill = connm->cm_to_ill;
16893 
16894 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16895 
16896 	/* All multicast fields protected by conn_lock */
16897 	mutex_enter(&connp->conn_lock);
16898 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16899 	if ((connp->conn_outgoing_ill == from_ill) &&
16900 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16901 		connp->conn_outgoing_ill = to_ill;
16902 		connp->conn_incoming_ill = to_ill;
16903 	}
16904 
16905 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16906 
16907 	if ((connp->conn_multicast_ill == from_ill) &&
16908 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16909 		connp->conn_multicast_ill = connm->cm_to_ill;
16910 	}
16911 
16912 	/* Change IP_XMIT_IF associations */
16913 	if ((connp->conn_xmit_if_ill == from_ill) &&
16914 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
16915 		connp->conn_xmit_if_ill = to_ill;
16916 	}
16917 	/*
16918 	 * Change the ilg_ill to point to the new one. This assumes
16919 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16920 	 * has been told to receive packets on this interface.
16921 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16922 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16923 	 * some ilms may not have moved. We check to see whether
16924 	 * the ilms have moved to to_ill. We can't check on from_ill
16925 	 * as in the process of moving, we could have split an ilm
16926 	 * in to two - which has the same orig_ifindex and v6group.
16927 	 *
16928 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16929 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16930 	 */
16931 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16932 		ilg = &connp->conn_ilg[i];
16933 		if ((ilg->ilg_ill == from_ill) &&
16934 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16935 			/* ifindex != 0 indicates failback */
16936 			if (ifindex != 0) {
16937 				connp->conn_ilg[i].ilg_ill = to_ill;
16938 				continue;
16939 			}
16940 
16941 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16942 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16943 			    connp->conn_zoneid);
16944 
16945 			if (ret_ilm != NULL)
16946 				connp->conn_ilg[i].ilg_ill = to_ill;
16947 		}
16948 	}
16949 	mutex_exit(&connp->conn_lock);
16950 }
16951 
16952 static void
16953 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16954 {
16955 	conn_move_t connm;
16956 	ip_stack_t	*ipst = from_ill->ill_ipst;
16957 
16958 	connm.cm_from_ill = from_ill;
16959 	connm.cm_to_ill = to_ill;
16960 	connm.cm_ifindex = ifindex;
16961 
16962 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16963 }
16964 
16965 /*
16966  * ilm has been moved from from_ill to to_ill.
16967  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16968  * appropriately.
16969  *
16970  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16971  *	  the code there de-references ipif_ill to get the ill to
16972  *	  send multicast requests. It does not work as ipif is on its
16973  *	  move and already moved when this function is called.
16974  *	  Thus, we need to use from_ill and to_ill send down multicast
16975  *	  requests.
16976  */
16977 static void
16978 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16979 {
16980 	ipif_t *ipif;
16981 	ilm_t *ilm;
16982 
16983 	/*
16984 	 * See whether we need to send down DL_ENABMULTI_REQ on
16985 	 * to_ill as ilm has just been added.
16986 	 */
16987 	ASSERT(IAM_WRITER_ILL(to_ill));
16988 	ASSERT(IAM_WRITER_ILL(from_ill));
16989 
16990 	ILM_WALKER_HOLD(to_ill);
16991 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16992 
16993 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16994 			continue;
16995 		/*
16996 		 * no locks held, ill/ipif cannot dissappear as long
16997 		 * as we are writer.
16998 		 */
16999 		ipif = to_ill->ill_ipif;
17000 		/*
17001 		 * No need to hold any lock as we are the writer and this
17002 		 * can only be changed by a writer.
17003 		 */
17004 		ilm->ilm_is_new = B_FALSE;
17005 
17006 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
17007 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
17008 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
17009 			    "resolver\n"));
17010 			continue;		/* Must be IRE_IF_NORESOLVER */
17011 		}
17012 
17013 
17014 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
17015 			ip1dbg(("ilm_send_multicast_reqs: "
17016 			    "to_ill MULTI_BCAST\n"));
17017 			goto from;
17018 		}
17019 
17020 		if (to_ill->ill_isv6)
17021 			mld_joingroup(ilm);
17022 		else
17023 			igmp_joingroup(ilm);
17024 
17025 		if (to_ill->ill_ipif_up_count == 0) {
17026 			/*
17027 			 * Nobody there. All multicast addresses will be
17028 			 * re-joined when we get the DL_BIND_ACK bringing the
17029 			 * interface up.
17030 			 */
17031 			ilm->ilm_notify_driver = B_FALSE;
17032 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
17033 			goto from;
17034 		}
17035 
17036 		/*
17037 		 * For allmulti address, we want to join on only one interface.
17038 		 * Checking for ilm_numentries_v6 is not correct as you may
17039 		 * find an ilm with zero address on to_ill, but we may not
17040 		 * have nominated to_ill for receiving. Thus, if we have
17041 		 * nominated from_ill (ill_join_allmulti is set), nominate
17042 		 * only if to_ill is not already nominated (to_ill normally
17043 		 * should not have been nominated if "from_ill" has already
17044 		 * been nominated. As we don't prevent failovers from happening
17045 		 * across groups, we don't assert).
17046 		 */
17047 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
17048 			/*
17049 			 * There is no need to hold ill locks as we are
17050 			 * writer on both ills and when ill_join_allmulti
17051 			 * is changed the thread is always a writer.
17052 			 */
17053 			if (from_ill->ill_join_allmulti &&
17054 			    !to_ill->ill_join_allmulti) {
17055 				(void) ip_join_allmulti(to_ill->ill_ipif);
17056 			}
17057 		} else if (ilm->ilm_notify_driver) {
17058 
17059 			/*
17060 			 * This is a newly moved ilm so we need to tell the
17061 			 * driver about the new group. There can be more than
17062 			 * one ilm's for the same group in the list each with a
17063 			 * different orig_ifindex. We have to inform the driver
17064 			 * once. In ilm_move_v[4,6] we only set the flag
17065 			 * ilm_notify_driver for the first ilm.
17066 			 */
17067 
17068 			(void) ip_ll_send_enabmulti_req(to_ill,
17069 			    &ilm->ilm_v6addr);
17070 		}
17071 
17072 		ilm->ilm_notify_driver = B_FALSE;
17073 
17074 		/*
17075 		 * See whether we need to send down DL_DISABMULTI_REQ on
17076 		 * from_ill as ilm has just been removed.
17077 		 */
17078 from:
17079 		ipif = from_ill->ill_ipif;
17080 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
17081 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
17082 			ip1dbg(("ilm_send_multicast_reqs: "
17083 			    "from_ill not resolver\n"));
17084 			continue;		/* Must be IRE_IF_NORESOLVER */
17085 		}
17086 
17087 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
17088 			ip1dbg(("ilm_send_multicast_reqs: "
17089 			    "from_ill MULTI_BCAST\n"));
17090 			continue;
17091 		}
17092 
17093 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
17094 			if (from_ill->ill_join_allmulti)
17095 				(void) ip_leave_allmulti(from_ill->ill_ipif);
17096 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
17097 			(void) ip_ll_send_disabmulti_req(from_ill,
17098 			    &ilm->ilm_v6addr);
17099 		}
17100 	}
17101 	ILM_WALKER_RELE(to_ill);
17102 }
17103 
17104 /*
17105  * This function is called when all multicast memberships needs
17106  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
17107  * called only once unlike the IPv4 counterpart where it is called after
17108  * every logical interface is moved. The reason is due to multicast
17109  * memberships are joined using an interface address in IPv4 while in
17110  * IPv6, interface index is used.
17111  */
17112 static void
17113 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
17114 {
17115 	ilm_t	*ilm;
17116 	ilm_t	*ilm_next;
17117 	ilm_t	*new_ilm;
17118 	ilm_t	**ilmp;
17119 	int	count;
17120 	char buf[INET6_ADDRSTRLEN];
17121 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
17122 	ip_stack_t	*ipst = from_ill->ill_ipst;
17123 
17124 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17125 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17126 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17127 
17128 	if (ifindex == 0) {
17129 		/*
17130 		 * Form the solicited node mcast address which is used later.
17131 		 */
17132 		ipif_t *ipif;
17133 
17134 		ipif = from_ill->ill_ipif;
17135 		ASSERT(ipif->ipif_id == 0);
17136 
17137 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
17138 	}
17139 
17140 	ilmp = &from_ill->ill_ilm;
17141 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17142 		ilm_next = ilm->ilm_next;
17143 
17144 		if (ilm->ilm_flags & ILM_DELETED) {
17145 			ilmp = &ilm->ilm_next;
17146 			continue;
17147 		}
17148 
17149 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
17150 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
17151 		ASSERT(ilm->ilm_orig_ifindex != 0);
17152 		if (ilm->ilm_orig_ifindex == ifindex) {
17153 			/*
17154 			 * We are failing back multicast memberships.
17155 			 * If the same ilm exists in to_ill, it means somebody
17156 			 * has joined the same group there e.g. ff02::1
17157 			 * is joined within the kernel when the interfaces
17158 			 * came UP.
17159 			 */
17160 			ASSERT(ilm->ilm_ipif == NULL);
17161 			if (new_ilm != NULL) {
17162 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17163 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17164 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17165 					new_ilm->ilm_is_new = B_TRUE;
17166 				}
17167 			} else {
17168 				/*
17169 				 * check if we can just move the ilm
17170 				 */
17171 				if (from_ill->ill_ilm_walker_cnt != 0) {
17172 					/*
17173 					 * We have walkers we cannot move
17174 					 * the ilm, so allocate a new ilm,
17175 					 * this (old) ilm will be marked
17176 					 * ILM_DELETED at the end of the loop
17177 					 * and will be freed when the
17178 					 * last walker exits.
17179 					 */
17180 					new_ilm = (ilm_t *)mi_zalloc
17181 					    (sizeof (ilm_t));
17182 					if (new_ilm == NULL) {
17183 						ip0dbg(("ilm_move_v6: "
17184 						    "FAILBACK of IPv6"
17185 						    " multicast address %s : "
17186 						    "from %s to"
17187 						    " %s failed : ENOMEM \n",
17188 						    inet_ntop(AF_INET6,
17189 						    &ilm->ilm_v6addr, buf,
17190 						    sizeof (buf)),
17191 						    from_ill->ill_name,
17192 						    to_ill->ill_name));
17193 
17194 							ilmp = &ilm->ilm_next;
17195 							continue;
17196 					}
17197 					*new_ilm = *ilm;
17198 					/*
17199 					 * we don't want new_ilm linked to
17200 					 * ilm's filter list.
17201 					 */
17202 					new_ilm->ilm_filter = NULL;
17203 				} else {
17204 					/*
17205 					 * No walkers we can move the ilm.
17206 					 * lets take it out of the list.
17207 					 */
17208 					*ilmp = ilm->ilm_next;
17209 					ilm->ilm_next = NULL;
17210 					new_ilm = ilm;
17211 				}
17212 
17213 				/*
17214 				 * if this is the first ilm for the group
17215 				 * set ilm_notify_driver so that we notify the
17216 				 * driver in ilm_send_multicast_reqs.
17217 				 */
17218 				if (ilm_lookup_ill_v6(to_ill,
17219 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17220 					new_ilm->ilm_notify_driver = B_TRUE;
17221 
17222 				new_ilm->ilm_ill = to_ill;
17223 				/* Add to the to_ill's list */
17224 				new_ilm->ilm_next = to_ill->ill_ilm;
17225 				to_ill->ill_ilm = new_ilm;
17226 				/*
17227 				 * set the flag so that mld_joingroup is
17228 				 * called in ilm_send_multicast_reqs().
17229 				 */
17230 				new_ilm->ilm_is_new = B_TRUE;
17231 			}
17232 			goto bottom;
17233 		} else if (ifindex != 0) {
17234 			/*
17235 			 * If this is FAILBACK (ifindex != 0) and the ifindex
17236 			 * has not matched above, look at the next ilm.
17237 			 */
17238 			ilmp = &ilm->ilm_next;
17239 			continue;
17240 		}
17241 		/*
17242 		 * If we are here, it means ifindex is 0. Failover
17243 		 * everything.
17244 		 *
17245 		 * We need to handle solicited node mcast address
17246 		 * and all_nodes mcast address differently as they
17247 		 * are joined witin the kenrel (ipif_multicast_up)
17248 		 * and potentially from the userland. We are called
17249 		 * after the ipifs of from_ill has been moved.
17250 		 * If we still find ilms on ill with solicited node
17251 		 * mcast address or all_nodes mcast address, it must
17252 		 * belong to the UP interface that has not moved e.g.
17253 		 * ipif_id 0 with the link local prefix does not move.
17254 		 * We join this on the new ill accounting for all the
17255 		 * userland memberships so that applications don't
17256 		 * see any failure.
17257 		 *
17258 		 * We need to make sure that we account only for the
17259 		 * solicited node and all node multicast addresses
17260 		 * that was brought UP on these. In the case of
17261 		 * a failover from A to B, we might have ilms belonging
17262 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
17263 		 * for the membership from the userland. If we are failing
17264 		 * over from B to C now, we will find the ones belonging
17265 		 * to A on B. These don't account for the ill_ipif_up_count.
17266 		 * They just move from B to C. The check below on
17267 		 * ilm_orig_ifindex ensures that.
17268 		 */
17269 		if ((ilm->ilm_orig_ifindex ==
17270 		    from_ill->ill_phyint->phyint_ifindex) &&
17271 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
17272 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
17273 		    &ilm->ilm_v6addr))) {
17274 			ASSERT(ilm->ilm_refcnt > 0);
17275 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
17276 			/*
17277 			 * For indentation reasons, we are not using a
17278 			 * "else" here.
17279 			 */
17280 			if (count == 0) {
17281 				ilmp = &ilm->ilm_next;
17282 				continue;
17283 			}
17284 			ilm->ilm_refcnt -= count;
17285 			if (new_ilm != NULL) {
17286 				/*
17287 				 * Can find one with the same
17288 				 * ilm_orig_ifindex, if we are failing
17289 				 * over to a STANDBY. This happens
17290 				 * when somebody wants to join a group
17291 				 * on a STANDBY interface and we
17292 				 * internally join on a different one.
17293 				 * If we had joined on from_ill then, a
17294 				 * failover now will find a new ilm
17295 				 * with this index.
17296 				 */
17297 				ip1dbg(("ilm_move_v6: FAILOVER, found"
17298 				    " new ilm on %s, group address %s\n",
17299 				    to_ill->ill_name,
17300 				    inet_ntop(AF_INET6,
17301 				    &ilm->ilm_v6addr, buf,
17302 				    sizeof (buf))));
17303 				new_ilm->ilm_refcnt += count;
17304 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17305 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17306 					new_ilm->ilm_is_new = B_TRUE;
17307 				}
17308 			} else {
17309 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17310 				if (new_ilm == NULL) {
17311 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17312 					    " multicast address %s : from %s to"
17313 					    " %s failed : ENOMEM \n",
17314 					    inet_ntop(AF_INET6,
17315 					    &ilm->ilm_v6addr, buf,
17316 					    sizeof (buf)), from_ill->ill_name,
17317 					    to_ill->ill_name));
17318 					ilmp = &ilm->ilm_next;
17319 					continue;
17320 				}
17321 				*new_ilm = *ilm;
17322 				new_ilm->ilm_filter = NULL;
17323 				new_ilm->ilm_refcnt = count;
17324 				new_ilm->ilm_timer = INFINITY;
17325 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17326 				new_ilm->ilm_is_new = B_TRUE;
17327 				/*
17328 				 * If the to_ill has not joined this
17329 				 * group we need to tell the driver in
17330 				 * ill_send_multicast_reqs.
17331 				 */
17332 				if (ilm_lookup_ill_v6(to_ill,
17333 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17334 					new_ilm->ilm_notify_driver = B_TRUE;
17335 
17336 				new_ilm->ilm_ill = to_ill;
17337 				/* Add to the to_ill's list */
17338 				new_ilm->ilm_next = to_ill->ill_ilm;
17339 				to_ill->ill_ilm = new_ilm;
17340 				ASSERT(new_ilm->ilm_ipif == NULL);
17341 			}
17342 			if (ilm->ilm_refcnt == 0) {
17343 				goto bottom;
17344 			} else {
17345 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17346 				CLEAR_SLIST(new_ilm->ilm_filter);
17347 				ilmp = &ilm->ilm_next;
17348 			}
17349 			continue;
17350 		} else {
17351 			/*
17352 			 * ifindex = 0 means, move everything pointing at
17353 			 * from_ill. We are doing this becuase ill has
17354 			 * either FAILED or became INACTIVE.
17355 			 *
17356 			 * As we would like to move things later back to
17357 			 * from_ill, we want to retain the identity of this
17358 			 * ilm. Thus, we don't blindly increment the reference
17359 			 * count on the ilms matching the address alone. We
17360 			 * need to match on the ilm_orig_index also. new_ilm
17361 			 * was obtained by matching ilm_orig_index also.
17362 			 */
17363 			if (new_ilm != NULL) {
17364 				/*
17365 				 * This is possible only if a previous restore
17366 				 * was incomplete i.e restore to
17367 				 * ilm_orig_ifindex left some ilms because
17368 				 * of some failures. Thus when we are failing
17369 				 * again, we might find our old friends there.
17370 				 */
17371 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17372 				    " on %s, group address %s\n",
17373 				    to_ill->ill_name,
17374 				    inet_ntop(AF_INET6,
17375 				    &ilm->ilm_v6addr, buf,
17376 				    sizeof (buf))));
17377 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17378 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17379 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17380 					new_ilm->ilm_is_new = B_TRUE;
17381 				}
17382 			} else {
17383 				if (from_ill->ill_ilm_walker_cnt != 0) {
17384 					new_ilm = (ilm_t *)
17385 					    mi_zalloc(sizeof (ilm_t));
17386 					if (new_ilm == NULL) {
17387 						ip0dbg(("ilm_move_v6: "
17388 						    "FAILOVER of IPv6"
17389 						    " multicast address %s : "
17390 						    "from %s to"
17391 						    " %s failed : ENOMEM \n",
17392 						    inet_ntop(AF_INET6,
17393 						    &ilm->ilm_v6addr, buf,
17394 						    sizeof (buf)),
17395 						    from_ill->ill_name,
17396 						    to_ill->ill_name));
17397 
17398 							ilmp = &ilm->ilm_next;
17399 							continue;
17400 					}
17401 					*new_ilm = *ilm;
17402 					new_ilm->ilm_filter = NULL;
17403 				} else {
17404 					*ilmp = ilm->ilm_next;
17405 					new_ilm = ilm;
17406 				}
17407 				/*
17408 				 * If the to_ill has not joined this
17409 				 * group we need to tell the driver in
17410 				 * ill_send_multicast_reqs.
17411 				 */
17412 				if (ilm_lookup_ill_v6(to_ill,
17413 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17414 					new_ilm->ilm_notify_driver = B_TRUE;
17415 
17416 				/* Add to the to_ill's list */
17417 				new_ilm->ilm_next = to_ill->ill_ilm;
17418 				to_ill->ill_ilm = new_ilm;
17419 				ASSERT(ilm->ilm_ipif == NULL);
17420 				new_ilm->ilm_ill = to_ill;
17421 				new_ilm->ilm_is_new = B_TRUE;
17422 			}
17423 
17424 		}
17425 
17426 bottom:
17427 		/*
17428 		 * Revert multicast filter state to (EXCLUDE, NULL).
17429 		 * new_ilm->ilm_is_new should already be set if needed.
17430 		 */
17431 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17432 		CLEAR_SLIST(new_ilm->ilm_filter);
17433 		/*
17434 		 * We allocated/got a new ilm, free the old one.
17435 		 */
17436 		if (new_ilm != ilm) {
17437 			if (from_ill->ill_ilm_walker_cnt == 0) {
17438 				*ilmp = ilm->ilm_next;
17439 				ilm->ilm_next = NULL;
17440 				FREE_SLIST(ilm->ilm_filter);
17441 				FREE_SLIST(ilm->ilm_pendsrcs);
17442 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17443 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17444 				mi_free((char *)ilm);
17445 			} else {
17446 				ilm->ilm_flags |= ILM_DELETED;
17447 				from_ill->ill_ilm_cleanup_reqd = 1;
17448 				ilmp = &ilm->ilm_next;
17449 			}
17450 		}
17451 	}
17452 }
17453 
17454 /*
17455  * Move all the multicast memberships to to_ill. Called when
17456  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17457  * different from IPv6 counterpart as multicast memberships are associated
17458  * with ills in IPv6. This function is called after every ipif is moved
17459  * unlike IPv6, where it is moved only once.
17460  */
17461 static void
17462 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17463 {
17464 	ilm_t	*ilm;
17465 	ilm_t	*ilm_next;
17466 	ilm_t	*new_ilm;
17467 	ilm_t	**ilmp;
17468 	ip_stack_t	*ipst = from_ill->ill_ipst;
17469 
17470 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17471 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17472 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17473 
17474 	ilmp = &from_ill->ill_ilm;
17475 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17476 		ilm_next = ilm->ilm_next;
17477 
17478 		if (ilm->ilm_flags & ILM_DELETED) {
17479 			ilmp = &ilm->ilm_next;
17480 			continue;
17481 		}
17482 
17483 		ASSERT(ilm->ilm_ipif != NULL);
17484 
17485 		if (ilm->ilm_ipif != ipif) {
17486 			ilmp = &ilm->ilm_next;
17487 			continue;
17488 		}
17489 
17490 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17491 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17492 			new_ilm = ilm_lookup_ipif(ipif,
17493 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17494 			if (new_ilm != NULL) {
17495 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17496 				/*
17497 				 * We still need to deal with the from_ill.
17498 				 */
17499 				new_ilm->ilm_is_new = B_TRUE;
17500 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17501 				CLEAR_SLIST(new_ilm->ilm_filter);
17502 				goto delete_ilm;
17503 			}
17504 			/*
17505 			 * If we could not find one e.g. ipif is
17506 			 * still down on to_ill, we add this ilm
17507 			 * on ill_new to preserve the reference
17508 			 * count.
17509 			 */
17510 		}
17511 		/*
17512 		 * When ipifs move, ilms always move with it
17513 		 * to the NEW ill. Thus we should never be
17514 		 * able to find ilm till we really move it here.
17515 		 */
17516 		ASSERT(ilm_lookup_ipif(ipif,
17517 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17518 
17519 		if (from_ill->ill_ilm_walker_cnt != 0) {
17520 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17521 			if (new_ilm == NULL) {
17522 				char buf[INET6_ADDRSTRLEN];
17523 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17524 				    " multicast address %s : "
17525 				    "from %s to"
17526 				    " %s failed : ENOMEM \n",
17527 				    inet_ntop(AF_INET,
17528 				    &ilm->ilm_v6addr, buf,
17529 				    sizeof (buf)),
17530 				    from_ill->ill_name,
17531 				    to_ill->ill_name));
17532 
17533 				ilmp = &ilm->ilm_next;
17534 				continue;
17535 			}
17536 			*new_ilm = *ilm;
17537 			/* We don't want new_ilm linked to ilm's filter list */
17538 			new_ilm->ilm_filter = NULL;
17539 		} else {
17540 			/* Remove from the list */
17541 			*ilmp = ilm->ilm_next;
17542 			new_ilm = ilm;
17543 		}
17544 
17545 		/*
17546 		 * If we have never joined this group on the to_ill
17547 		 * make sure we tell the driver.
17548 		 */
17549 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17550 		    ALL_ZONES) == NULL)
17551 			new_ilm->ilm_notify_driver = B_TRUE;
17552 
17553 		/* Add to the to_ill's list */
17554 		new_ilm->ilm_next = to_ill->ill_ilm;
17555 		to_ill->ill_ilm = new_ilm;
17556 		new_ilm->ilm_is_new = B_TRUE;
17557 
17558 		/*
17559 		 * Revert multicast filter state to (EXCLUDE, NULL)
17560 		 */
17561 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17562 		CLEAR_SLIST(new_ilm->ilm_filter);
17563 
17564 		/*
17565 		 * Delete only if we have allocated a new ilm.
17566 		 */
17567 		if (new_ilm != ilm) {
17568 delete_ilm:
17569 			if (from_ill->ill_ilm_walker_cnt == 0) {
17570 				/* Remove from the list */
17571 				*ilmp = ilm->ilm_next;
17572 				ilm->ilm_next = NULL;
17573 				FREE_SLIST(ilm->ilm_filter);
17574 				FREE_SLIST(ilm->ilm_pendsrcs);
17575 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17576 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17577 				mi_free((char *)ilm);
17578 			} else {
17579 				ilm->ilm_flags |= ILM_DELETED;
17580 				from_ill->ill_ilm_cleanup_reqd = 1;
17581 				ilmp = &ilm->ilm_next;
17582 			}
17583 		}
17584 	}
17585 }
17586 
17587 static uint_t
17588 ipif_get_id(ill_t *ill, uint_t id)
17589 {
17590 	uint_t	unit;
17591 	ipif_t	*tipif;
17592 	boolean_t found = B_FALSE;
17593 	ip_stack_t	*ipst = ill->ill_ipst;
17594 
17595 	/*
17596 	 * During failback, we want to go back to the same id
17597 	 * instead of the smallest id so that the original
17598 	 * configuration is maintained. id is non-zero in that
17599 	 * case.
17600 	 */
17601 	if (id != 0) {
17602 		/*
17603 		 * While failing back, if we still have an ipif with
17604 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17605 		 * as soon as we return from this function. It was
17606 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17607 		 * we can choose the smallest id. Thus we return zero
17608 		 * in that case ignoring the hint.
17609 		 */
17610 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17611 			return (0);
17612 		for (tipif = ill->ill_ipif; tipif != NULL;
17613 		    tipif = tipif->ipif_next) {
17614 			if (tipif->ipif_id == id) {
17615 				found = B_TRUE;
17616 				break;
17617 			}
17618 		}
17619 		/*
17620 		 * If somebody already plumbed another logical
17621 		 * with the same id, we won't be able to find it.
17622 		 */
17623 		if (!found)
17624 			return (id);
17625 	}
17626 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17627 		found = B_FALSE;
17628 		for (tipif = ill->ill_ipif; tipif != NULL;
17629 		    tipif = tipif->ipif_next) {
17630 			if (tipif->ipif_id == unit) {
17631 				found = B_TRUE;
17632 				break;
17633 			}
17634 		}
17635 		if (!found)
17636 			break;
17637 	}
17638 	return (unit);
17639 }
17640 
17641 /* ARGSUSED */
17642 static int
17643 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17644     ipif_t **rep_ipif_ptr)
17645 {
17646 	ill_t	*from_ill;
17647 	ipif_t	*rep_ipif;
17648 	uint_t	unit;
17649 	int err = 0;
17650 	ipif_t	*to_ipif;
17651 	struct iocblk	*iocp;
17652 	boolean_t failback_cmd;
17653 	boolean_t remove_ipif;
17654 	int	rc;
17655 	ip_stack_t	*ipst;
17656 
17657 	ASSERT(IAM_WRITER_ILL(to_ill));
17658 	ASSERT(IAM_WRITER_IPIF(ipif));
17659 
17660 	iocp = (struct iocblk *)mp->b_rptr;
17661 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17662 	remove_ipif = B_FALSE;
17663 
17664 	from_ill = ipif->ipif_ill;
17665 	ipst = from_ill->ill_ipst;
17666 
17667 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17668 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17669 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17670 
17671 	/*
17672 	 * Don't move LINK LOCAL addresses as they are tied to
17673 	 * physical interface.
17674 	 */
17675 	if (from_ill->ill_isv6 &&
17676 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17677 		ipif->ipif_was_up = B_FALSE;
17678 		IPIF_UNMARK_MOVING(ipif);
17679 		return (0);
17680 	}
17681 
17682 	/*
17683 	 * We set the ipif_id to maximum so that the search for
17684 	 * ipif_id will pick the lowest number i.e 0 in the
17685 	 * following 2 cases :
17686 	 *
17687 	 * 1) We have a replacement ipif at the head of to_ill.
17688 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17689 	 *    on to_ill and hence the MOVE might fail. We want to
17690 	 *    remove it only if we could move the ipif. Thus, by
17691 	 *    setting it to the MAX value, we make the search in
17692 	 *    ipif_get_id return the zeroth id.
17693 	 *
17694 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17695 	 *    we might just have a zero address plumbed on the ipif
17696 	 *    with zero id in the case of IPv4. We remove that while
17697 	 *    doing the failback. We want to remove it only if we
17698 	 *    could move the ipif. Thus, by setting it to the MAX
17699 	 *    value, we make the search in ipif_get_id return the
17700 	 *    zeroth id.
17701 	 *
17702 	 * Both (1) and (2) are done only when when we are moving
17703 	 * an ipif (either due to failover/failback) which originally
17704 	 * belonged to this interface i.e the ipif_orig_ifindex is
17705 	 * the same as to_ill's ifindex. This is needed so that
17706 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17707 	 * from B -> A (B is being removed from the group) and
17708 	 * FAILBACK from A -> B restores the original configuration.
17709 	 * Without the check for orig_ifindex, the second FAILOVER
17710 	 * could make the ipif belonging to B replace the A's zeroth
17711 	 * ipif and the subsequent failback re-creating the replacement
17712 	 * ipif again.
17713 	 *
17714 	 * NOTE : We created the replacement ipif when we did a
17715 	 * FAILOVER (See below). We could check for FAILBACK and
17716 	 * then look for replacement ipif to be removed. But we don't
17717 	 * want to do that because we wan't to allow the possibility
17718 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17719 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17720 	 * from B -> A.
17721 	 */
17722 	to_ipif = to_ill->ill_ipif;
17723 	if ((to_ill->ill_phyint->phyint_ifindex ==
17724 	    ipif->ipif_orig_ifindex) &&
17725 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17726 		ASSERT(to_ipif->ipif_id == 0);
17727 		remove_ipif = B_TRUE;
17728 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17729 	}
17730 	/*
17731 	 * Find the lowest logical unit number on the to_ill.
17732 	 * If we are failing back, try to get the original id
17733 	 * rather than the lowest one so that the original
17734 	 * configuration is maintained.
17735 	 *
17736 	 * XXX need a better scheme for this.
17737 	 */
17738 	if (failback_cmd) {
17739 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17740 	} else {
17741 		unit = ipif_get_id(to_ill, 0);
17742 	}
17743 
17744 	/* Reset back to zero in case we fail below */
17745 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17746 		to_ipif->ipif_id = 0;
17747 
17748 	if (unit == ipst->ips_ip_addrs_per_if) {
17749 		ipif->ipif_was_up = B_FALSE;
17750 		IPIF_UNMARK_MOVING(ipif);
17751 		return (EINVAL);
17752 	}
17753 
17754 	/*
17755 	 * ipif is ready to move from "from_ill" to "to_ill".
17756 	 *
17757 	 * 1) If we are moving ipif with id zero, create a
17758 	 *    replacement ipif for this ipif on from_ill. If this fails
17759 	 *    fail the MOVE operation.
17760 	 *
17761 	 * 2) Remove the replacement ipif on to_ill if any.
17762 	 *    We could remove the replacement ipif when we are moving
17763 	 *    the ipif with id zero. But what if somebody already
17764 	 *    unplumbed it ? Thus we always remove it if it is present.
17765 	 *    We want to do it only if we are sure we are going to
17766 	 *    move the ipif to to_ill which is why there are no
17767 	 *    returns due to error till ipif is linked to to_ill.
17768 	 *    Note that the first ipif that we failback will always
17769 	 *    be zero if it is present.
17770 	 */
17771 	if (ipif->ipif_id == 0) {
17772 		ipaddr_t inaddr_any = INADDR_ANY;
17773 
17774 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17775 		if (rep_ipif == NULL) {
17776 			ipif->ipif_was_up = B_FALSE;
17777 			IPIF_UNMARK_MOVING(ipif);
17778 			return (ENOMEM);
17779 		}
17780 		*rep_ipif = ipif_zero;
17781 		/*
17782 		 * Before we put the ipif on the list, store the addresses
17783 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17784 		 * assumes so. This logic is not any different from what
17785 		 * ipif_allocate does.
17786 		 */
17787 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17788 		    &rep_ipif->ipif_v6lcl_addr);
17789 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17790 		    &rep_ipif->ipif_v6src_addr);
17791 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17792 		    &rep_ipif->ipif_v6subnet);
17793 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17794 		    &rep_ipif->ipif_v6net_mask);
17795 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17796 		    &rep_ipif->ipif_v6brd_addr);
17797 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17798 		    &rep_ipif->ipif_v6pp_dst_addr);
17799 		/*
17800 		 * We mark IPIF_NOFAILOVER so that this can never
17801 		 * move.
17802 		 */
17803 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17804 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17805 		rep_ipif->ipif_replace_zero = B_TRUE;
17806 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17807 		    MUTEX_DEFAULT, NULL);
17808 		rep_ipif->ipif_id = 0;
17809 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17810 		rep_ipif->ipif_ill = from_ill;
17811 		rep_ipif->ipif_orig_ifindex =
17812 		    from_ill->ill_phyint->phyint_ifindex;
17813 		/* Insert at head */
17814 		rep_ipif->ipif_next = from_ill->ill_ipif;
17815 		from_ill->ill_ipif = rep_ipif;
17816 		/*
17817 		 * We don't really care to let apps know about
17818 		 * this interface.
17819 		 */
17820 	}
17821 
17822 	if (remove_ipif) {
17823 		/*
17824 		 * We set to a max value above for this case to get
17825 		 * id zero. ASSERT that we did get one.
17826 		 */
17827 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17828 		rep_ipif = to_ipif;
17829 		to_ill->ill_ipif = rep_ipif->ipif_next;
17830 		rep_ipif->ipif_next = NULL;
17831 		/*
17832 		 * If some apps scanned and find this interface,
17833 		 * it is time to let them know, so that they can
17834 		 * delete it.
17835 		 */
17836 
17837 		*rep_ipif_ptr = rep_ipif;
17838 	}
17839 
17840 	/* Get it out of the ILL interface list. */
17841 	ipif_remove(ipif, B_FALSE);
17842 
17843 	/* Assign the new ill */
17844 	ipif->ipif_ill = to_ill;
17845 	ipif->ipif_id = unit;
17846 	/* id has already been checked */
17847 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17848 	ASSERT(rc == 0);
17849 	/* Let SCTP update its list */
17850 	sctp_move_ipif(ipif, from_ill, to_ill);
17851 	/*
17852 	 * Handle the failover and failback of ipif_t between
17853 	 * ill_t that have differing maximum mtu values.
17854 	 */
17855 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17856 		if (ipif->ipif_saved_mtu == 0) {
17857 			/*
17858 			 * As this ipif_t is moving to an ill_t
17859 			 * that has a lower ill_max_mtu, its
17860 			 * ipif_mtu needs to be saved so it can
17861 			 * be restored during failback or during
17862 			 * failover to an ill_t which has a
17863 			 * higher ill_max_mtu.
17864 			 */
17865 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17866 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17867 		} else {
17868 			/*
17869 			 * The ipif_t is, once again, moving to
17870 			 * an ill_t that has a lower maximum mtu
17871 			 * value.
17872 			 */
17873 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17874 		}
17875 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17876 	    ipif->ipif_saved_mtu != 0) {
17877 		/*
17878 		 * The mtu of this ipif_t had to be reduced
17879 		 * during an earlier failover; this is an
17880 		 * opportunity for it to be increased (either as
17881 		 * part of another failover or a failback).
17882 		 */
17883 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17884 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17885 			ipif->ipif_saved_mtu = 0;
17886 		} else {
17887 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17888 		}
17889 	}
17890 
17891 	/*
17892 	 * We preserve all the other fields of the ipif including
17893 	 * ipif_saved_ire_mp. The routes that are saved here will
17894 	 * be recreated on the new interface and back on the old
17895 	 * interface when we move back.
17896 	 */
17897 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17898 
17899 	return (err);
17900 }
17901 
17902 static int
17903 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17904     int ifindex, ipif_t **rep_ipif_ptr)
17905 {
17906 	ipif_t *mipif;
17907 	ipif_t *ipif_next;
17908 	int err;
17909 
17910 	/*
17911 	 * We don't really try to MOVE back things if some of the
17912 	 * operations fail. The daemon will take care of moving again
17913 	 * later on.
17914 	 */
17915 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17916 		ipif_next = mipif->ipif_next;
17917 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17918 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17919 
17920 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17921 
17922 			/*
17923 			 * When the MOVE fails, it is the job of the
17924 			 * application to take care of this properly
17925 			 * i.e try again if it is ENOMEM.
17926 			 */
17927 			if (mipif->ipif_ill != from_ill) {
17928 				/*
17929 				 * ipif has moved.
17930 				 *
17931 				 * Move the multicast memberships associated
17932 				 * with this ipif to the new ill. For IPv6, we
17933 				 * do it once after all the ipifs are moved
17934 				 * (in ill_move) as they are not associated
17935 				 * with ipifs.
17936 				 *
17937 				 * We need to move the ilms as the ipif has
17938 				 * already been moved to a new ill even
17939 				 * in the case of errors. Neither
17940 				 * ilm_free(ipif) will find the ilm
17941 				 * when somebody unplumbs this ipif nor
17942 				 * ilm_delete(ilm) will be able to find the
17943 				 * ilm, if we don't move now.
17944 				 */
17945 				if (!from_ill->ill_isv6)
17946 					ilm_move_v4(from_ill, to_ill, mipif);
17947 			}
17948 
17949 			if (err != 0)
17950 				return (err);
17951 		}
17952 	}
17953 	return (0);
17954 }
17955 
17956 static int
17957 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17958 {
17959 	int ifindex;
17960 	int err;
17961 	struct iocblk	*iocp;
17962 	ipif_t	*ipif;
17963 	ipif_t *rep_ipif_ptr = NULL;
17964 	ipif_t	*from_ipif = NULL;
17965 	boolean_t check_rep_if = B_FALSE;
17966 	ip_stack_t	*ipst = from_ill->ill_ipst;
17967 
17968 	iocp = (struct iocblk *)mp->b_rptr;
17969 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17970 		/*
17971 		 * Move everything pointing at from_ill to to_ill.
17972 		 * We acheive this by passing in 0 as ifindex.
17973 		 */
17974 		ifindex = 0;
17975 	} else {
17976 		/*
17977 		 * Move everything pointing at from_ill whose original
17978 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17979 		 * We acheive this by passing in ifindex rather than 0.
17980 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17981 		 */
17982 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17983 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17984 	}
17985 
17986 	/*
17987 	 * Determine if there is at least one ipif that would move from
17988 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17989 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17990 	 * the move, in which case we need to quiesce the replacement ipif also.
17991 	 */
17992 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17993 	    from_ipif = from_ipif->ipif_next) {
17994 		if (((ifindex == 0) ||
17995 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17996 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17997 			check_rep_if = B_TRUE;
17998 			break;
17999 		}
18000 	}
18001 
18002 
18003 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
18004 
18005 	GRAB_ILL_LOCKS(from_ill, to_ill);
18006 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
18007 		(void) ipsq_pending_mp_add(NULL, ipif, q,
18008 		    mp, ILL_MOVE_OK);
18009 		RELEASE_ILL_LOCKS(from_ill, to_ill);
18010 		return (EINPROGRESS);
18011 	}
18012 
18013 	/* Check if the replacement ipif is quiescent to delete */
18014 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
18015 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
18016 		to_ill->ill_ipif->ipif_state_flags |=
18017 		    IPIF_MOVING | IPIF_CHANGING;
18018 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
18019 			(void) ipsq_pending_mp_add(NULL, ipif, q,
18020 			    mp, ILL_MOVE_OK);
18021 			RELEASE_ILL_LOCKS(from_ill, to_ill);
18022 			return (EINPROGRESS);
18023 		}
18024 	}
18025 	RELEASE_ILL_LOCKS(from_ill, to_ill);
18026 
18027 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
18028 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
18029 	GRAB_ILL_LOCKS(from_ill, to_ill);
18030 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
18031 
18032 	/* ilm_move is done inside ipif_move for IPv4 */
18033 	if (err == 0 && from_ill->ill_isv6)
18034 		ilm_move_v6(from_ill, to_ill, ifindex);
18035 
18036 	RELEASE_ILL_LOCKS(from_ill, to_ill);
18037 	rw_exit(&ipst->ips_ill_g_lock);
18038 
18039 	/*
18040 	 * send rts messages and multicast messages.
18041 	 */
18042 	if (rep_ipif_ptr != NULL) {
18043 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
18044 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
18045 			rep_ipif_ptr->ipif_recovery_id = 0;
18046 		}
18047 		ip_rts_ifmsg(rep_ipif_ptr);
18048 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
18049 		IPIF_TRACE_CLEANUP(rep_ipif_ptr);
18050 		mi_free(rep_ipif_ptr);
18051 	}
18052 
18053 	conn_move_ill(from_ill, to_ill, ifindex);
18054 
18055 	return (err);
18056 }
18057 
18058 /*
18059  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
18060  * Also checks for the validity of the arguments.
18061  * Note: We are already exclusive inside the from group.
18062  * It is upto the caller to release refcnt on the to_ill's.
18063  */
18064 static int
18065 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
18066     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
18067 {
18068 	int dst_index;
18069 	ipif_t *ipif_v4, *ipif_v6;
18070 	struct lifreq *lifr;
18071 	mblk_t *mp1;
18072 	boolean_t exists;
18073 	sin_t	*sin;
18074 	int	err = 0;
18075 	ip_stack_t	*ipst;
18076 
18077 	if (CONN_Q(q))
18078 		ipst = CONNQ_TO_IPST(q);
18079 	else
18080 		ipst = ILLQ_TO_IPST(q);
18081 
18082 
18083 	if ((mp1 = mp->b_cont) == NULL)
18084 		return (EPROTO);
18085 
18086 	if ((mp1 = mp1->b_cont) == NULL)
18087 		return (EPROTO);
18088 
18089 	lifr = (struct lifreq *)mp1->b_rptr;
18090 	sin = (sin_t *)&lifr->lifr_addr;
18091 
18092 	/*
18093 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
18094 	 * specific operations.
18095 	 */
18096 	if (sin->sin_family != AF_UNSPEC)
18097 		return (EINVAL);
18098 
18099 	/*
18100 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
18101 	 * NULLs for the last 4 args and we know the lookup won't fail
18102 	 * with EINPROGRESS.
18103 	 */
18104 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
18105 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
18106 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
18107 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
18108 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
18109 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
18110 
18111 	if (ipif_v4 == NULL && ipif_v6 == NULL)
18112 		return (ENXIO);
18113 
18114 	if (ipif_v4 != NULL) {
18115 		ASSERT(ipif_v4->ipif_refcnt != 0);
18116 		if (ipif_v4->ipif_id != 0) {
18117 			err = EINVAL;
18118 			goto done;
18119 		}
18120 
18121 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
18122 		*ill_from_v4 = ipif_v4->ipif_ill;
18123 	}
18124 
18125 	if (ipif_v6 != NULL) {
18126 		ASSERT(ipif_v6->ipif_refcnt != 0);
18127 		if (ipif_v6->ipif_id != 0) {
18128 			err = EINVAL;
18129 			goto done;
18130 		}
18131 
18132 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
18133 		*ill_from_v6 = ipif_v6->ipif_ill;
18134 	}
18135 
18136 	err = 0;
18137 	dst_index = lifr->lifr_movetoindex;
18138 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
18139 	    q, mp, ip_process_ioctl, &err, ipst);
18140 	if (err != 0) {
18141 		/*
18142 		 * There could be only v6.
18143 		 */
18144 		if (err != ENXIO)
18145 			goto done;
18146 		err = 0;
18147 	}
18148 
18149 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
18150 	    q, mp, ip_process_ioctl, &err, ipst);
18151 	if (err != 0) {
18152 		if (err != ENXIO)
18153 			goto done;
18154 		if (*ill_to_v4 == NULL) {
18155 			err = ENXIO;
18156 			goto done;
18157 		}
18158 		err = 0;
18159 	}
18160 
18161 	/*
18162 	 * If we have something to MOVE i.e "from" not NULL,
18163 	 * "to" should be non-NULL.
18164 	 */
18165 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
18166 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
18167 		err = EINVAL;
18168 	}
18169 
18170 done:
18171 	if (ipif_v4 != NULL)
18172 		ipif_refrele(ipif_v4);
18173 	if (ipif_v6 != NULL)
18174 		ipif_refrele(ipif_v6);
18175 	return (err);
18176 }
18177 
18178 /*
18179  * FAILOVER and FAILBACK are modelled as MOVE operations.
18180  *
18181  * We don't check whether the MOVE is within the same group or
18182  * not, because this ioctl can be used as a generic mechanism
18183  * to failover from interface A to B, though things will function
18184  * only if they are really part of the same group. Moreover,
18185  * all ipifs may be down and hence temporarily out of the group.
18186  *
18187  * ipif's that need to be moved are first brought down; V4 ipifs are brought
18188  * down first and then V6.  For each we wait for the ipif's to become quiescent.
18189  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
18190  * have been deleted and there are no active references. Once quiescent the
18191  * ipif's are moved and brought up on the new ill.
18192  *
18193  * Normally the source ill and destination ill belong to the same IPMP group
18194  * and hence the same ipsq_t. In the event they don't belong to the same
18195  * same group the two ipsq's are first merged into one ipsq - that of the
18196  * to_ill. The multicast memberships on the source and destination ill cannot
18197  * change during the move operation since multicast joins/leaves also have to
18198  * execute on the same ipsq and are hence serialized.
18199  */
18200 /* ARGSUSED */
18201 int
18202 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18203     ip_ioctl_cmd_t *ipip, void *ifreq)
18204 {
18205 	ill_t *ill_to_v4 = NULL;
18206 	ill_t *ill_to_v6 = NULL;
18207 	ill_t *ill_from_v4 = NULL;
18208 	ill_t *ill_from_v6 = NULL;
18209 	int err = 0;
18210 
18211 	/*
18212 	 * setup from and to ill's, we can get EINPROGRESS only for
18213 	 * to_ill's.
18214 	 */
18215 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
18216 	    &ill_to_v4, &ill_to_v6);
18217 
18218 	if (err != 0) {
18219 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
18220 		goto done;
18221 	}
18222 
18223 	/*
18224 	 * nothing to do.
18225 	 */
18226 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
18227 		goto done;
18228 	}
18229 
18230 	/*
18231 	 * nothing to do.
18232 	 */
18233 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
18234 		goto done;
18235 	}
18236 
18237 	/*
18238 	 * Mark the ill as changing.
18239 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
18240 	 * in ill_up_ipifs in case of error they are cleared below.
18241 	 */
18242 
18243 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18244 	if (ill_from_v4 != NULL)
18245 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
18246 	if (ill_from_v6 != NULL)
18247 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
18248 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18249 
18250 	/*
18251 	 * Make sure that both src and dst are
18252 	 * in the same syncq group. If not make it happen.
18253 	 * We are not holding any locks because we are the writer
18254 	 * on the from_ipsq and we will hold locks in ill_merge_groups
18255 	 * to protect to_ipsq against changing.
18256 	 */
18257 	if (ill_from_v4 != NULL) {
18258 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
18259 		    ill_to_v4->ill_phyint->phyint_ipsq) {
18260 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
18261 			    NULL, mp, q);
18262 			goto err_ret;
18263 
18264 		}
18265 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
18266 	} else {
18267 
18268 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
18269 		    ill_to_v6->ill_phyint->phyint_ipsq) {
18270 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
18271 			    NULL, mp, q);
18272 			goto err_ret;
18273 
18274 		}
18275 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
18276 	}
18277 
18278 	/*
18279 	 * Now that the ipsq's have been merged and we are the writer
18280 	 * lets mark to_ill as changing as well.
18281 	 */
18282 
18283 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18284 	if (ill_to_v4 != NULL)
18285 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
18286 	if (ill_to_v6 != NULL)
18287 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
18288 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18289 
18290 	/*
18291 	 * Its ok for us to proceed with the move even if
18292 	 * ill_pending_mp is non null on one of the from ill's as the reply
18293 	 * should not be looking at the ipif, it should only care about the
18294 	 * ill itself.
18295 	 */
18296 
18297 	/*
18298 	 * lets move ipv4 first.
18299 	 */
18300 	if (ill_from_v4 != NULL) {
18301 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18302 		ill_from_v4->ill_move_in_progress = B_TRUE;
18303 		ill_to_v4->ill_move_in_progress = B_TRUE;
18304 		ill_to_v4->ill_move_peer = ill_from_v4;
18305 		ill_from_v4->ill_move_peer = ill_to_v4;
18306 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18307 	}
18308 
18309 	/*
18310 	 * Now lets move ipv6.
18311 	 */
18312 	if (err == 0 && ill_from_v6 != NULL) {
18313 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18314 		ill_from_v6->ill_move_in_progress = B_TRUE;
18315 		ill_to_v6->ill_move_in_progress = B_TRUE;
18316 		ill_to_v6->ill_move_peer = ill_from_v6;
18317 		ill_from_v6->ill_move_peer = ill_to_v6;
18318 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18319 	}
18320 
18321 err_ret:
18322 	/*
18323 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18324 	 * moved to become quiescent.
18325 	 */
18326 	if (err == EINPROGRESS) {
18327 		goto done;
18328 	}
18329 
18330 	/*
18331 	 * if err is set ill_up_ipifs will not be called
18332 	 * lets clear the flags.
18333 	 */
18334 
18335 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18336 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18337 	/*
18338 	 * Some of the clearing may be redundant. But it is simple
18339 	 * not making any extra checks.
18340 	 */
18341 	if (ill_from_v6 != NULL) {
18342 		ill_from_v6->ill_move_in_progress = B_FALSE;
18343 		ill_from_v6->ill_move_peer = NULL;
18344 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18345 	}
18346 	if (ill_from_v4 != NULL) {
18347 		ill_from_v4->ill_move_in_progress = B_FALSE;
18348 		ill_from_v4->ill_move_peer = NULL;
18349 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18350 	}
18351 	if (ill_to_v6 != NULL) {
18352 		ill_to_v6->ill_move_in_progress = B_FALSE;
18353 		ill_to_v6->ill_move_peer = NULL;
18354 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18355 	}
18356 	if (ill_to_v4 != NULL) {
18357 		ill_to_v4->ill_move_in_progress = B_FALSE;
18358 		ill_to_v4->ill_move_peer = NULL;
18359 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18360 	}
18361 
18362 	/*
18363 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18364 	 * Do this always to maintain proper state i.e even in case of errors.
18365 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18366 	 * we need not call on both v4 and v6 interfaces.
18367 	 */
18368 	if (ill_from_v4 != NULL) {
18369 		if ((ill_from_v4->ill_phyint->phyint_flags &
18370 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18371 			phyint_inactive(ill_from_v4->ill_phyint);
18372 		}
18373 	} else if (ill_from_v6 != NULL) {
18374 		if ((ill_from_v6->ill_phyint->phyint_flags &
18375 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18376 			phyint_inactive(ill_from_v6->ill_phyint);
18377 		}
18378 	}
18379 
18380 	if (ill_to_v4 != NULL) {
18381 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18382 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18383 		}
18384 	} else if (ill_to_v6 != NULL) {
18385 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18386 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18387 		}
18388 	}
18389 
18390 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18391 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18392 
18393 no_err:
18394 	/*
18395 	 * lets bring the interfaces up on the to_ill.
18396 	 */
18397 	if (err == 0) {
18398 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18399 		    q, mp);
18400 	}
18401 
18402 	if (err == 0) {
18403 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18404 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18405 
18406 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18407 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18408 	}
18409 done:
18410 
18411 	if (ill_to_v4 != NULL) {
18412 		ill_refrele(ill_to_v4);
18413 	}
18414 	if (ill_to_v6 != NULL) {
18415 		ill_refrele(ill_to_v6);
18416 	}
18417 
18418 	return (err);
18419 }
18420 
18421 static void
18422 ill_dl_down(ill_t *ill)
18423 {
18424 	/*
18425 	 * The ill is down; unbind but stay attached since we're still
18426 	 * associated with a PPA. If we have negotiated DLPI capabilites
18427 	 * with the data link service provider (IDS_OK) then reset them.
18428 	 * The interval between unbinding and rebinding is potentially
18429 	 * unbounded hence we cannot assume things will be the same.
18430 	 * The DLPI capabilities will be probed again when the data link
18431 	 * is brought up.
18432 	 */
18433 	mblk_t	*mp = ill->ill_unbind_mp;
18434 	hook_nic_event_t *info;
18435 
18436 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18437 
18438 	ill->ill_unbind_mp = NULL;
18439 	if (mp != NULL) {
18440 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18441 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18442 		    ill->ill_name));
18443 		mutex_enter(&ill->ill_lock);
18444 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18445 		mutex_exit(&ill->ill_lock);
18446 		if (ill->ill_dlpi_capab_state == IDS_OK)
18447 			ill_capability_reset(ill);
18448 		ill_dlpi_send(ill, mp);
18449 	}
18450 
18451 	/*
18452 	 * Toss all of our multicast memberships.  We could keep them, but
18453 	 * then we'd have to do bookkeeping of any joins and leaves performed
18454 	 * by the application while the the interface is down (we can't just
18455 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18456 	 * on a downed interface).
18457 	 */
18458 	ill_leave_multicast(ill);
18459 
18460 	mutex_enter(&ill->ill_lock);
18461 
18462 	ill->ill_dl_up = 0;
18463 
18464 	if ((info = ill->ill_nic_event_info) != NULL) {
18465 		ip2dbg(("ill_dl_down:unexpected nic event %d attached for %s\n",
18466 		    info->hne_event, ill->ill_name));
18467 		if (info->hne_data != NULL)
18468 			kmem_free(info->hne_data, info->hne_datalen);
18469 		kmem_free(info, sizeof (hook_nic_event_t));
18470 	}
18471 
18472 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
18473 	if (info != NULL) {
18474 		ip_stack_t	*ipst = ill->ill_ipst;
18475 
18476 		info->hne_nic = ill->ill_phyint->phyint_hook_ifindex;
18477 		info->hne_lif = 0;
18478 		info->hne_event = NE_DOWN;
18479 		info->hne_data = NULL;
18480 		info->hne_datalen = 0;
18481 		info->hne_family = ill->ill_isv6 ?
18482 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
18483 	} else
18484 		ip2dbg(("ill_dl_down: could not attach DOWN nic event "
18485 		    "information for %s (ENOMEM)\n", ill->ill_name));
18486 
18487 	ill->ill_nic_event_info = info;
18488 
18489 	mutex_exit(&ill->ill_lock);
18490 }
18491 
18492 static void
18493 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18494 {
18495 	union DL_primitives *dlp;
18496 	t_uscalar_t prim;
18497 
18498 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18499 
18500 	dlp = (union DL_primitives *)mp->b_rptr;
18501 	prim = dlp->dl_primitive;
18502 
18503 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18504 	    dlpi_prim_str(prim), prim, ill->ill_name));
18505 
18506 	switch (prim) {
18507 	case DL_PHYS_ADDR_REQ:
18508 	{
18509 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18510 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18511 		break;
18512 	}
18513 	case DL_BIND_REQ:
18514 		mutex_enter(&ill->ill_lock);
18515 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18516 		mutex_exit(&ill->ill_lock);
18517 		break;
18518 	}
18519 
18520 	/*
18521 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18522 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18523 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18524 	 */
18525 	mutex_enter(&ill->ill_lock);
18526 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18527 	    (prim == DL_UNBIND_REQ)) {
18528 		ill->ill_dlpi_pending = prim;
18529 	}
18530 	mutex_exit(&ill->ill_lock);
18531 
18532 	putnext(ill->ill_wq, mp);
18533 }
18534 
18535 /*
18536  * Helper function for ill_dlpi_send().
18537  */
18538 /* ARGSUSED */
18539 static void
18540 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18541 {
18542 	ill_dlpi_send((ill_t *)q->q_ptr, mp);
18543 }
18544 
18545 /*
18546  * Send a DLPI control message to the driver but make sure there
18547  * is only one outstanding message. Uses ill_dlpi_pending to tell
18548  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18549  * when an ACK or a NAK is received to process the next queued message.
18550  */
18551 void
18552 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18553 {
18554 	mblk_t **mpp;
18555 
18556 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18557 
18558 	/*
18559 	 * To ensure that any DLPI requests for current exclusive operation
18560 	 * are always completely sent before any DLPI messages for other
18561 	 * operations, require writer access before enqueuing.
18562 	 */
18563 	if (!IAM_WRITER_ILL(ill)) {
18564 		ill_refhold(ill);
18565 		/* qwriter_ip() does the ill_refrele() */
18566 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18567 		    NEW_OP, B_TRUE);
18568 		return;
18569 	}
18570 
18571 	mutex_enter(&ill->ill_lock);
18572 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18573 		/* Must queue message. Tail insertion */
18574 		mpp = &ill->ill_dlpi_deferred;
18575 		while (*mpp != NULL)
18576 			mpp = &((*mpp)->b_next);
18577 
18578 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18579 		    ill->ill_name));
18580 
18581 		*mpp = mp;
18582 		mutex_exit(&ill->ill_lock);
18583 		return;
18584 	}
18585 	mutex_exit(&ill->ill_lock);
18586 	ill_dlpi_dispatch(ill, mp);
18587 }
18588 
18589 /*
18590  * Send all deferred DLPI messages without waiting for their ACKs.
18591  */
18592 void
18593 ill_dlpi_send_deferred(ill_t *ill)
18594 {
18595 	mblk_t *mp, *nextmp;
18596 
18597 	/*
18598 	 * Clear ill_dlpi_pending so that the message is not queued in
18599 	 * ill_dlpi_send().
18600 	 */
18601 	mutex_enter(&ill->ill_lock);
18602 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18603 	mp = ill->ill_dlpi_deferred;
18604 	ill->ill_dlpi_deferred = NULL;
18605 	mutex_exit(&ill->ill_lock);
18606 
18607 	for (; mp != NULL; mp = nextmp) {
18608 		nextmp = mp->b_next;
18609 		mp->b_next = NULL;
18610 		ill_dlpi_send(ill, mp);
18611 	}
18612 }
18613 
18614 /*
18615  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18616  */
18617 boolean_t
18618 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18619 {
18620 	t_uscalar_t prim_pending;
18621 
18622 	mutex_enter(&ill->ill_lock);
18623 	prim_pending = ill->ill_dlpi_pending;
18624 	mutex_exit(&ill->ill_lock);
18625 
18626 	/*
18627 	 * During teardown, ill_dlpi_send_deferred() will send requests
18628 	 * without waiting; don't bother printing any warnings in that case.
18629 	 */
18630 	if (!(ill->ill_flags & ILL_CONDEMNED) && prim_pending != prim) {
18631 		if (prim_pending == DL_PRIM_INVAL) {
18632 			(void) mi_strlog(ill->ill_rq, 1,
18633 			    SL_CONSOLE|SL_ERROR|SL_TRACE, "ip: received "
18634 			    "unsolicited ack for %s on %s\n",
18635 			    dlpi_prim_str(prim), ill->ill_name);
18636 		} else {
18637 			(void) mi_strlog(ill->ill_rq, 1,
18638 			    SL_CONSOLE|SL_ERROR|SL_TRACE, "ip: received "
18639 			    "unexpected ack for %s on %s (expecting %s)\n",
18640 			    dlpi_prim_str(prim), ill->ill_name,
18641 			    dlpi_prim_str(prim_pending));
18642 		}
18643 	}
18644 	return (prim_pending == prim);
18645 }
18646 
18647 /*
18648  * Called when an DLPI control message has been acked or nacked to
18649  * send down the next queued message (if any).
18650  */
18651 void
18652 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18653 {
18654 	mblk_t *mp;
18655 
18656 	ASSERT(IAM_WRITER_ILL(ill));
18657 	mutex_enter(&ill->ill_lock);
18658 
18659 	ASSERT(prim != DL_PRIM_INVAL);
18660 	ASSERT(ill->ill_dlpi_pending == prim);
18661 
18662 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18663 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18664 
18665 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18666 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18667 		cv_signal(&ill->ill_cv);
18668 		mutex_exit(&ill->ill_lock);
18669 		return;
18670 	}
18671 
18672 	ill->ill_dlpi_deferred = mp->b_next;
18673 	mp->b_next = NULL;
18674 	mutex_exit(&ill->ill_lock);
18675 
18676 	ill_dlpi_dispatch(ill, mp);
18677 }
18678 
18679 void
18680 conn_delete_ire(conn_t *connp, caddr_t arg)
18681 {
18682 	ipif_t	*ipif = (ipif_t *)arg;
18683 	ire_t	*ire;
18684 
18685 	/*
18686 	 * Look at the cached ires on conns which has pointers to ipifs.
18687 	 * We just call ire_refrele which clears up the reference
18688 	 * to ire. Called when a conn closes. Also called from ipif_free
18689 	 * to cleanup indirect references to the stale ipif via the cached ire.
18690 	 */
18691 	mutex_enter(&connp->conn_lock);
18692 	ire = connp->conn_ire_cache;
18693 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18694 		connp->conn_ire_cache = NULL;
18695 		mutex_exit(&connp->conn_lock);
18696 		IRE_REFRELE_NOTR(ire);
18697 		return;
18698 	}
18699 	mutex_exit(&connp->conn_lock);
18700 
18701 }
18702 
18703 /*
18704  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18705  * of IREs. Those IREs may have been previously cached in the conn structure.
18706  * This ipcl_walk() walker function releases all references to such IREs based
18707  * on the condemned flag.
18708  */
18709 /* ARGSUSED */
18710 void
18711 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18712 {
18713 	ire_t	*ire;
18714 
18715 	mutex_enter(&connp->conn_lock);
18716 	ire = connp->conn_ire_cache;
18717 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18718 		connp->conn_ire_cache = NULL;
18719 		mutex_exit(&connp->conn_lock);
18720 		IRE_REFRELE_NOTR(ire);
18721 		return;
18722 	}
18723 	mutex_exit(&connp->conn_lock);
18724 }
18725 
18726 /*
18727  * Take down a specific interface, but don't lose any information about it.
18728  * Also delete interface from its interface group (ifgrp).
18729  * (Always called as writer.)
18730  * This function goes through the down sequence even if the interface is
18731  * already down. There are 2 reasons.
18732  * a. Currently we permit interface routes that depend on down interfaces
18733  *    to be added. This behaviour itself is questionable. However it appears
18734  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18735  *    time. We go thru the cleanup in order to remove these routes.
18736  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18737  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18738  *    down, but we need to cleanup i.e. do ill_dl_down and
18739  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18740  *
18741  * IP-MT notes:
18742  *
18743  * Model of reference to interfaces.
18744  *
18745  * The following members in ipif_t track references to the ipif.
18746  *	int     ipif_refcnt;    Active reference count
18747  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18748  * The following members in ill_t track references to the ill.
18749  *	int             ill_refcnt;     active refcnt
18750  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18751  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18752  *
18753  * Reference to an ipif or ill can be obtained in any of the following ways.
18754  *
18755  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18756  * Pointers to ipif / ill from other data structures viz ire and conn.
18757  * Implicit reference to the ipif / ill by holding a reference to the ire.
18758  *
18759  * The ipif/ill lookup functions return a reference held ipif / ill.
18760  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18761  * This is a purely dynamic reference count associated with threads holding
18762  * references to the ipif / ill. Pointers from other structures do not
18763  * count towards this reference count.
18764  *
18765  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
18766  * ipif/ill. This is incremented whenever a new ire is created referencing the
18767  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
18768  * actually added to the ire hash table. The count is decremented in
18769  * ire_inactive where the ire is destroyed.
18770  *
18771  * nce's reference ill's thru nce_ill and the count of nce's associated with
18772  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18773  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18774  * table. Similarly it is decremented in ndp_inactive() where the nce
18775  * is destroyed.
18776  *
18777  * Flow of ioctls involving interface down/up
18778  *
18779  * The following is the sequence of an attempt to set some critical flags on an
18780  * up interface.
18781  * ip_sioctl_flags
18782  * ipif_down
18783  * wait for ipif to be quiescent
18784  * ipif_down_tail
18785  * ip_sioctl_flags_tail
18786  *
18787  * All set ioctls that involve down/up sequence would have a skeleton similar
18788  * to the above. All the *tail functions are called after the refcounts have
18789  * dropped to the appropriate values.
18790  *
18791  * The mechanism to quiesce an ipif is as follows.
18792  *
18793  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18794  * on the ipif. Callers either pass a flag requesting wait or the lookup
18795  *  functions will return NULL.
18796  *
18797  * Delete all ires referencing this ipif
18798  *
18799  * Any thread attempting to do an ipif_refhold on an ipif that has been
18800  * obtained thru a cached pointer will first make sure that
18801  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18802  * increment the refcount.
18803  *
18804  * The above guarantees that the ipif refcount will eventually come down to
18805  * zero and the ipif will quiesce, once all threads that currently hold a
18806  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18807  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18808  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
18809  * drop to zero.
18810  *
18811  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18812  *
18813  * Threads trying to lookup an ipif or ill can pass a flag requesting
18814  * wait and restart if the ipif / ill cannot be looked up currently.
18815  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18816  * failure if the ipif is currently undergoing an exclusive operation, and
18817  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18818  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18819  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18820  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18821  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18822  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18823  * until we release the ipsq_lock, even though the the ill/ipif state flags
18824  * can change after we drop the ill_lock.
18825  *
18826  * An attempt to send out a packet using an ipif that is currently
18827  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18828  * operation and restart it later when the exclusive condition on the ipif ends.
18829  * This is an example of not passing the wait flag to the lookup functions. For
18830  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18831  * out a multicast packet on that ipif will fail while the ipif is
18832  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18833  * currently IPIF_CHANGING will also fail.
18834  */
18835 int
18836 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18837 {
18838 	ill_t		*ill = ipif->ipif_ill;
18839 	phyint_t	*phyi;
18840 	conn_t		*connp;
18841 	boolean_t	success;
18842 	boolean_t	ipif_was_up = B_FALSE;
18843 	ip_stack_t	*ipst = ill->ill_ipst;
18844 
18845 	ASSERT(IAM_WRITER_IPIF(ipif));
18846 
18847 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18848 
18849 	if (ipif->ipif_flags & IPIF_UP) {
18850 		mutex_enter(&ill->ill_lock);
18851 		ipif->ipif_flags &= ~IPIF_UP;
18852 		ASSERT(ill->ill_ipif_up_count > 0);
18853 		--ill->ill_ipif_up_count;
18854 		mutex_exit(&ill->ill_lock);
18855 		ipif_was_up = B_TRUE;
18856 		/* Update status in SCTP's list */
18857 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18858 	}
18859 
18860 	/*
18861 	 * Blow away memberships we established in ipif_multicast_up().
18862 	 */
18863 	ipif_multicast_down(ipif);
18864 
18865 	/*
18866 	 * Remove from the mapping for __sin6_src_id. We insert only
18867 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18868 	 * stored as mapped addresses, we need to check for mapped
18869 	 * INADDR_ANY also.
18870 	 */
18871 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18872 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18873 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18874 		int err;
18875 
18876 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18877 		    ipif->ipif_zoneid, ipst);
18878 		if (err != 0) {
18879 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18880 		}
18881 	}
18882 
18883 	/*
18884 	 * Before we delete the ill from the group (if any), we need
18885 	 * to make sure that we delete all the routes dependent on
18886 	 * this and also any ipifs dependent on this ipif for
18887 	 * source address. We need to do before we delete from
18888 	 * the group because
18889 	 *
18890 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18891 	 *
18892 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18893 	 *    for re-doing source address selection. Note that
18894 	 *    ipif_select_source[_v6] called from
18895 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18896 	 *    because we have already marked down here i.e cleared
18897 	 *    IPIF_UP.
18898 	 */
18899 	if (ipif->ipif_isv6) {
18900 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18901 		    ipst);
18902 	} else {
18903 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18904 		    ipst);
18905 	}
18906 
18907 	/*
18908 	 * Need to add these also to be saved and restored when the
18909 	 * ipif is brought down and up
18910 	 */
18911 	mutex_enter(&ipst->ips_ire_mrtun_lock);
18912 	if (ipst->ips_ire_mrtun_count != 0) {
18913 		mutex_exit(&ipst->ips_ire_mrtun_lock);
18914 		ire_walk_ill_mrtun(0, 0, ipif_down_delete_ire,
18915 		    (char *)ipif, NULL, ipst);
18916 	} else {
18917 		mutex_exit(&ipst->ips_ire_mrtun_lock);
18918 	}
18919 
18920 	mutex_enter(&ipst->ips_ire_srcif_table_lock);
18921 	if (ipst->ips_ire_srcif_table_count > 0) {
18922 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
18923 		ire_walk_srcif_table_v4(ipif_down_delete_ire, (char *)ipif,
18924 		    ipst);
18925 	} else {
18926 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
18927 	}
18928 
18929 	/*
18930 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18931 	 * ires have been deleted above. Otherwise a thread could end up
18932 	 * caching an ire in a conn after we have finished the cleanup of the
18933 	 * conn. The caching is done after making sure that the ire is not yet
18934 	 * condemned. Also documented in the block comment above ip_output
18935 	 */
18936 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18937 	/* Also, delete the ires cached in SCTP */
18938 	sctp_ire_cache_flush(ipif);
18939 
18940 	/* Resolve any IPsec/IKE NAT-T instances that depend on this ipif. */
18941 	nattymod_clean_ipif(ipif);
18942 
18943 	/*
18944 	 * Update any other ipifs which have used "our" local address as
18945 	 * a source address. This entails removing and recreating IRE_INTERFACE
18946 	 * entries for such ipifs.
18947 	 */
18948 	if (ipif->ipif_isv6)
18949 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18950 	else
18951 		ipif_update_other_ipifs(ipif, ill->ill_group);
18952 
18953 	if (ipif_was_up) {
18954 		/*
18955 		 * Check whether it is last ipif to leave this group.
18956 		 * If this is the last ipif to leave, we should remove
18957 		 * this ill from the group as ipif_select_source will not
18958 		 * be able to find any useful ipifs if this ill is selected
18959 		 * for load balancing.
18960 		 *
18961 		 * For nameless groups, we should call ifgrp_delete if this
18962 		 * belongs to some group. As this ipif is going down, we may
18963 		 * need to reconstruct groups.
18964 		 */
18965 		phyi = ill->ill_phyint;
18966 		/*
18967 		 * If the phyint_groupname_len is 0, it may or may not
18968 		 * be in the nameless group. If the phyint_groupname_len is
18969 		 * not 0, then this ill should be part of some group.
18970 		 * As we always insert this ill in the group if
18971 		 * phyint_groupname_len is not zero when the first ipif
18972 		 * comes up (in ipif_up_done), it should be in a group
18973 		 * when the namelen is not 0.
18974 		 *
18975 		 * NOTE : When we delete the ill from the group,it will
18976 		 * blow away all the IRE_CACHES pointing either at this ipif or
18977 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18978 		 * should be pointing at this ill.
18979 		 */
18980 		ASSERT(phyi->phyint_groupname_len == 0 ||
18981 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18982 
18983 		if (phyi->phyint_groupname_len != 0) {
18984 			if (ill->ill_ipif_up_count == 0)
18985 				illgrp_delete(ill);
18986 		}
18987 
18988 		/*
18989 		 * If we have deleted some of the broadcast ires associated
18990 		 * with this ipif, we need to re-nominate somebody else if
18991 		 * the ires that we deleted were the nominated ones.
18992 		 */
18993 		if (ill->ill_group != NULL && !ill->ill_isv6)
18994 			ipif_renominate_bcast(ipif);
18995 	}
18996 
18997 	/*
18998 	 * neighbor-discovery or arp entries for this interface.
18999 	 */
19000 	ipif_ndp_down(ipif);
19001 
19002 	/*
19003 	 * If mp is NULL the caller will wait for the appropriate refcnt.
19004 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
19005 	 * and ill_delete -> ipif_free -> ipif_down
19006 	 */
19007 	if (mp == NULL) {
19008 		ASSERT(q == NULL);
19009 		return (0);
19010 	}
19011 
19012 	if (CONN_Q(q)) {
19013 		connp = Q_TO_CONN(q);
19014 		mutex_enter(&connp->conn_lock);
19015 	} else {
19016 		connp = NULL;
19017 	}
19018 	mutex_enter(&ill->ill_lock);
19019 	/*
19020 	 * Are there any ire's pointing to this ipif that are still active ?
19021 	 * If this is the last ipif going down, are there any ire's pointing
19022 	 * to this ill that are still active ?
19023 	 */
19024 	if (ipif_is_quiescent(ipif)) {
19025 		mutex_exit(&ill->ill_lock);
19026 		if (connp != NULL)
19027 			mutex_exit(&connp->conn_lock);
19028 		return (0);
19029 	}
19030 
19031 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
19032 	    ill->ill_name, (void *)ill));
19033 	/*
19034 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
19035 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
19036 	 * which in turn is called by the last refrele on the ipif/ill/ire.
19037 	 */
19038 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
19039 	if (!success) {
19040 		/* The conn is closing. So just return */
19041 		ASSERT(connp != NULL);
19042 		mutex_exit(&ill->ill_lock);
19043 		mutex_exit(&connp->conn_lock);
19044 		return (EINTR);
19045 	}
19046 
19047 	mutex_exit(&ill->ill_lock);
19048 	if (connp != NULL)
19049 		mutex_exit(&connp->conn_lock);
19050 	return (EINPROGRESS);
19051 }
19052 
19053 void
19054 ipif_down_tail(ipif_t *ipif)
19055 {
19056 	ill_t	*ill = ipif->ipif_ill;
19057 
19058 	/*
19059 	 * Skip any loopback interface (null wq).
19060 	 * If this is the last logical interface on the ill
19061 	 * have ill_dl_down tell the driver we are gone (unbind)
19062 	 * Note that lun 0 can ipif_down even though
19063 	 * there are other logical units that are up.
19064 	 * This occurs e.g. when we change a "significant" IFF_ flag.
19065 	 */
19066 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
19067 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
19068 	    ill->ill_dl_up) {
19069 		ill_dl_down(ill);
19070 	}
19071 	ill->ill_logical_down = 0;
19072 
19073 	/*
19074 	 * Have to be after removing the routes in ipif_down_delete_ire.
19075 	 */
19076 	if (ipif->ipif_isv6) {
19077 		if (ill->ill_flags & ILLF_XRESOLV)
19078 			ipif_arp_down(ipif);
19079 	} else {
19080 		ipif_arp_down(ipif);
19081 	}
19082 
19083 	ip_rts_ifmsg(ipif);
19084 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
19085 }
19086 
19087 /*
19088  * Bring interface logically down without bringing the physical interface
19089  * down e.g. when the netmask is changed. This avoids long lasting link
19090  * negotiations between an ethernet interface and a certain switches.
19091  */
19092 static int
19093 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
19094 {
19095 	/*
19096 	 * The ill_logical_down flag is a transient flag. It is set here
19097 	 * and is cleared once the down has completed in ipif_down_tail.
19098 	 * This flag does not indicate whether the ill stream is in the
19099 	 * DL_BOUND state with the driver. Instead this flag is used by
19100 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
19101 	 * the driver. The state of the ill stream i.e. whether it is
19102 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
19103 	 */
19104 	ipif->ipif_ill->ill_logical_down = 1;
19105 	return (ipif_down(ipif, q, mp));
19106 }
19107 
19108 /*
19109  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
19110  * If the usesrc client ILL is already part of a usesrc group or not,
19111  * in either case a ire_stq with the matching usesrc client ILL will
19112  * locate the IRE's that need to be deleted. We want IREs to be created
19113  * with the new source address.
19114  */
19115 static void
19116 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
19117 {
19118 	ill_t	*ucill = (ill_t *)ill_arg;
19119 
19120 	ASSERT(IAM_WRITER_ILL(ucill));
19121 
19122 	if (ire->ire_stq == NULL)
19123 		return;
19124 
19125 	if ((ire->ire_type == IRE_CACHE) &&
19126 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
19127 		ire_delete(ire);
19128 }
19129 
19130 /*
19131  * ire_walk routine to delete every IRE dependent on the interface
19132  * address that is going down.	(Always called as writer.)
19133  * Works for both v4 and v6.
19134  * In addition for checking for ire_ipif matches it also checks for
19135  * IRE_CACHE entries which have the same source address as the
19136  * disappearing ipif since ipif_select_source might have picked
19137  * that source. Note that ipif_down/ipif_update_other_ipifs takes
19138  * care of any IRE_INTERFACE with the disappearing source address.
19139  */
19140 static void
19141 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
19142 {
19143 	ipif_t	*ipif = (ipif_t *)ipif_arg;
19144 	ill_t *ire_ill;
19145 	ill_t *ipif_ill;
19146 
19147 	ASSERT(IAM_WRITER_IPIF(ipif));
19148 	if (ire->ire_ipif == NULL)
19149 		return;
19150 
19151 	/*
19152 	 * For IPv4, we derive source addresses for an IRE from ipif's
19153 	 * belonging to the same IPMP group as the IRE's outgoing
19154 	 * interface.  If an IRE's outgoing interface isn't in the
19155 	 * same IPMP group as a particular ipif, then that ipif
19156 	 * couldn't have been used as a source address for this IRE.
19157 	 *
19158 	 * For IPv6, source addresses are only restricted to the IPMP group
19159 	 * if the IRE is for a link-local address or a multicast address.
19160 	 * Otherwise, source addresses for an IRE can be chosen from
19161 	 * interfaces other than the the outgoing interface for that IRE.
19162 	 *
19163 	 * For source address selection details, see ipif_select_source()
19164 	 * and ipif_select_source_v6().
19165 	 */
19166 	if (ire->ire_ipversion == IPV4_VERSION ||
19167 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
19168 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
19169 		ire_ill = ire->ire_ipif->ipif_ill;
19170 		ipif_ill = ipif->ipif_ill;
19171 
19172 		if (ire_ill->ill_group != ipif_ill->ill_group) {
19173 			return;
19174 		}
19175 	}
19176 
19177 
19178 	if (ire->ire_ipif != ipif) {
19179 		/*
19180 		 * Look for a matching source address.
19181 		 */
19182 		if (ire->ire_type != IRE_CACHE)
19183 			return;
19184 		if (ipif->ipif_flags & IPIF_NOLOCAL)
19185 			return;
19186 
19187 		if (ire->ire_ipversion == IPV4_VERSION) {
19188 			if (ire->ire_src_addr != ipif->ipif_src_addr)
19189 				return;
19190 		} else {
19191 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
19192 			    &ipif->ipif_v6lcl_addr))
19193 				return;
19194 		}
19195 		ire_delete(ire);
19196 		return;
19197 	}
19198 	/*
19199 	 * ire_delete() will do an ire_flush_cache which will delete
19200 	 * all ire_ipif matches
19201 	 */
19202 	ire_delete(ire);
19203 }
19204 
19205 /*
19206  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
19207  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
19208  * 2) when an interface is brought up or down (on that ill).
19209  * This ensures that the IRE_CACHE entries don't retain stale source
19210  * address selection results.
19211  */
19212 void
19213 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
19214 {
19215 	ill_t	*ill = (ill_t *)ill_arg;
19216 	ill_t	*ipif_ill;
19217 
19218 	ASSERT(IAM_WRITER_ILL(ill));
19219 	/*
19220 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19221 	 * Hence this should be IRE_CACHE.
19222 	 */
19223 	ASSERT(ire->ire_type == IRE_CACHE);
19224 
19225 	/*
19226 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
19227 	 * We are only interested in IRE_CACHES that has borrowed
19228 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
19229 	 * for which we need to look at ire_ipif->ipif_ill match
19230 	 * with ill.
19231 	 */
19232 	ASSERT(ire->ire_ipif != NULL);
19233 	ipif_ill = ire->ire_ipif->ipif_ill;
19234 	if (ipif_ill == ill || (ill->ill_group != NULL &&
19235 	    ipif_ill->ill_group == ill->ill_group)) {
19236 		ire_delete(ire);
19237 	}
19238 }
19239 
19240 /*
19241  * Delete all the ire whose stq references ill_arg.
19242  */
19243 static void
19244 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
19245 {
19246 	ill_t	*ill = (ill_t *)ill_arg;
19247 	ill_t	*ire_ill;
19248 
19249 	ASSERT(IAM_WRITER_ILL(ill));
19250 	/*
19251 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19252 	 * Hence this should be IRE_CACHE.
19253 	 */
19254 	ASSERT(ire->ire_type == IRE_CACHE);
19255 
19256 	/*
19257 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19258 	 * matches ill. We are only interested in IRE_CACHES that
19259 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
19260 	 * filtering here.
19261 	 */
19262 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
19263 
19264 	if (ire_ill == ill)
19265 		ire_delete(ire);
19266 }
19267 
19268 /*
19269  * This is called when an ill leaves the group. We want to delete
19270  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
19271  * pointing at ill.
19272  */
19273 static void
19274 illgrp_cache_delete(ire_t *ire, char *ill_arg)
19275 {
19276 	ill_t	*ill = (ill_t *)ill_arg;
19277 
19278 	ASSERT(IAM_WRITER_ILL(ill));
19279 	ASSERT(ill->ill_group == NULL);
19280 	/*
19281 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19282 	 * Hence this should be IRE_CACHE.
19283 	 */
19284 	ASSERT(ire->ire_type == IRE_CACHE);
19285 	/*
19286 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19287 	 * matches ill. We are interested in both.
19288 	 */
19289 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
19290 	    (ire->ire_ipif->ipif_ill == ill));
19291 
19292 	ire_delete(ire);
19293 }
19294 
19295 /*
19296  * Initiate deallocate of an IPIF. Always called as writer. Called by
19297  * ill_delete or ip_sioctl_removeif.
19298  */
19299 static void
19300 ipif_free(ipif_t *ipif)
19301 {
19302 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19303 
19304 	ASSERT(IAM_WRITER_IPIF(ipif));
19305 
19306 	if (ipif->ipif_recovery_id != 0)
19307 		(void) untimeout(ipif->ipif_recovery_id);
19308 	ipif->ipif_recovery_id = 0;
19309 
19310 	/* Remove conn references */
19311 	reset_conn_ipif(ipif);
19312 
19313 	/*
19314 	 * Make sure we have valid net and subnet broadcast ire's for the
19315 	 * other ipif's which share them with this ipif.
19316 	 */
19317 	if (!ipif->ipif_isv6)
19318 		ipif_check_bcast_ires(ipif);
19319 
19320 	/*
19321 	 * Take down the interface. We can be called either from ill_delete
19322 	 * or from ip_sioctl_removeif.
19323 	 */
19324 	(void) ipif_down(ipif, NULL, NULL);
19325 
19326 	/*
19327 	 * Now that the interface is down, there's no chance it can still
19328 	 * become a duplicate.  Cancel any timer that may have been set while
19329 	 * tearing down.
19330 	 */
19331 	if (ipif->ipif_recovery_id != 0)
19332 		(void) untimeout(ipif->ipif_recovery_id);
19333 	ipif->ipif_recovery_id = 0;
19334 
19335 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19336 	/* Remove pointers to this ill in the multicast routing tables */
19337 	reset_mrt_vif_ipif(ipif);
19338 	rw_exit(&ipst->ips_ill_g_lock);
19339 }
19340 
19341 /*
19342  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19343  * also ill_move().
19344  */
19345 static void
19346 ipif_free_tail(ipif_t *ipif)
19347 {
19348 	mblk_t	*mp;
19349 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19350 
19351 	/*
19352 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19353 	 */
19354 	mutex_enter(&ipif->ipif_saved_ire_lock);
19355 	mp = ipif->ipif_saved_ire_mp;
19356 	ipif->ipif_saved_ire_mp = NULL;
19357 	mutex_exit(&ipif->ipif_saved_ire_lock);
19358 	freemsg(mp);
19359 
19360 	/*
19361 	 * Need to hold both ill_g_lock and ill_lock while
19362 	 * inserting or removing an ipif from the linked list
19363 	 * of ipifs hanging off the ill.
19364 	 */
19365 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19366 	/*
19367 	 * Remove all IPv4 multicast memberships on the interface now.
19368 	 * IPv6 is not handled here as the multicast memberships are
19369 	 * tied to the ill rather than the ipif.
19370 	 */
19371 	ilm_free(ipif);
19372 
19373 	/*
19374 	 * Since we held the ill_g_lock while doing the ilm_free above,
19375 	 * we can assert the ilms were really deleted and not just marked
19376 	 * ILM_DELETED.
19377 	 */
19378 	ASSERT(ilm_walk_ipif(ipif) == 0);
19379 
19380 	IPIF_TRACE_CLEANUP(ipif);
19381 
19382 	/* Ask SCTP to take it out of it list */
19383 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19384 
19385 	/* Get it out of the ILL interface list. */
19386 	ipif_remove(ipif, B_TRUE);
19387 	rw_exit(&ipst->ips_ill_g_lock);
19388 
19389 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19390 
19391 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19392 	ASSERT(ipif->ipif_recovery_id == 0);
19393 
19394 	/* Free the memory. */
19395 	mi_free(ipif);
19396 }
19397 
19398 /*
19399  * Returns an ipif name in the form "ill_name/unit" if ipif_id is not zero,
19400  * "ill_name" otherwise.
19401  */
19402 char *
19403 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19404 {
19405 	char	lbuf[32];
19406 	char	*name;
19407 	size_t	name_len;
19408 
19409 	buf[0] = '\0';
19410 	if (!ipif)
19411 		return (buf);
19412 	name = ipif->ipif_ill->ill_name;
19413 	name_len = ipif->ipif_ill->ill_name_length;
19414 	if (ipif->ipif_id != 0) {
19415 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19416 		    ipif->ipif_id);
19417 		name = lbuf;
19418 		name_len = mi_strlen(name) + 1;
19419 	}
19420 	len -= 1;
19421 	buf[len] = '\0';
19422 	len = MIN(len, name_len);
19423 	bcopy(name, buf, len);
19424 	return (buf);
19425 }
19426 
19427 /*
19428  * Find an IPIF based on the name passed in.  Names can be of the
19429  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19430  * The <phys> string can have forms like <dev><#> (e.g., le0),
19431  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19432  * When there is no colon, the implied unit id is zero. <phys> must
19433  * correspond to the name of an ILL.  (May be called as writer.)
19434  */
19435 static ipif_t *
19436 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19437     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19438     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19439 {
19440 	char	*cp;
19441 	char	*endp;
19442 	long	id;
19443 	ill_t	*ill;
19444 	ipif_t	*ipif;
19445 	uint_t	ire_type;
19446 	boolean_t did_alloc = B_FALSE;
19447 	ipsq_t	*ipsq;
19448 
19449 	if (error != NULL)
19450 		*error = 0;
19451 
19452 	/*
19453 	 * If the caller wants to us to create the ipif, make sure we have a
19454 	 * valid zoneid
19455 	 */
19456 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19457 
19458 	if (namelen == 0) {
19459 		if (error != NULL)
19460 			*error = ENXIO;
19461 		return (NULL);
19462 	}
19463 
19464 	*exists = B_FALSE;
19465 	/* Look for a colon in the name. */
19466 	endp = &name[namelen];
19467 	for (cp = endp; --cp > name; ) {
19468 		if (*cp == IPIF_SEPARATOR_CHAR)
19469 			break;
19470 	}
19471 
19472 	if (*cp == IPIF_SEPARATOR_CHAR) {
19473 		/*
19474 		 * Reject any non-decimal aliases for logical
19475 		 * interfaces. Aliases with leading zeroes
19476 		 * are also rejected as they introduce ambiguity
19477 		 * in the naming of the interfaces.
19478 		 * In order to confirm with existing semantics,
19479 		 * and to not break any programs/script relying
19480 		 * on that behaviour, if<0>:0 is considered to be
19481 		 * a valid interface.
19482 		 *
19483 		 * If alias has two or more digits and the first
19484 		 * is zero, fail.
19485 		 */
19486 		if (&cp[2] < endp && cp[1] == '0')
19487 			return (NULL);
19488 	}
19489 
19490 	if (cp <= name) {
19491 		cp = endp;
19492 	} else {
19493 		*cp = '\0';
19494 	}
19495 
19496 	/*
19497 	 * Look up the ILL, based on the portion of the name
19498 	 * before the slash. ill_lookup_on_name returns a held ill.
19499 	 * Temporary to check whether ill exists already. If so
19500 	 * ill_lookup_on_name will clear it.
19501 	 */
19502 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19503 	    q, mp, func, error, &did_alloc, ipst);
19504 	if (cp != endp)
19505 		*cp = IPIF_SEPARATOR_CHAR;
19506 	if (ill == NULL)
19507 		return (NULL);
19508 
19509 	/* Establish the unit number in the name. */
19510 	id = 0;
19511 	if (cp < endp && *endp == '\0') {
19512 		/* If there was a colon, the unit number follows. */
19513 		cp++;
19514 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19515 			ill_refrele(ill);
19516 			if (error != NULL)
19517 				*error = ENXIO;
19518 			return (NULL);
19519 		}
19520 	}
19521 
19522 	GRAB_CONN_LOCK(q);
19523 	mutex_enter(&ill->ill_lock);
19524 	/* Now see if there is an IPIF with this unit number. */
19525 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19526 		if (ipif->ipif_id == id) {
19527 			if (zoneid != ALL_ZONES &&
19528 			    zoneid != ipif->ipif_zoneid &&
19529 			    ipif->ipif_zoneid != ALL_ZONES) {
19530 				mutex_exit(&ill->ill_lock);
19531 				RELEASE_CONN_LOCK(q);
19532 				ill_refrele(ill);
19533 				if (error != NULL)
19534 					*error = ENXIO;
19535 				return (NULL);
19536 			}
19537 			/*
19538 			 * The block comment at the start of ipif_down
19539 			 * explains the use of the macros used below
19540 			 */
19541 			if (IPIF_CAN_LOOKUP(ipif)) {
19542 				ipif_refhold_locked(ipif);
19543 				mutex_exit(&ill->ill_lock);
19544 				if (!did_alloc)
19545 					*exists = B_TRUE;
19546 				/*
19547 				 * Drop locks before calling ill_refrele
19548 				 * since it can potentially call into
19549 				 * ipif_ill_refrele_tail which can end up
19550 				 * in trying to acquire any lock.
19551 				 */
19552 				RELEASE_CONN_LOCK(q);
19553 				ill_refrele(ill);
19554 				return (ipif);
19555 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19556 				ipsq = ill->ill_phyint->phyint_ipsq;
19557 				mutex_enter(&ipsq->ipsq_lock);
19558 				mutex_exit(&ill->ill_lock);
19559 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19560 				mutex_exit(&ipsq->ipsq_lock);
19561 				RELEASE_CONN_LOCK(q);
19562 				ill_refrele(ill);
19563 				*error = EINPROGRESS;
19564 				return (NULL);
19565 			}
19566 		}
19567 	}
19568 	RELEASE_CONN_LOCK(q);
19569 
19570 	if (!do_alloc) {
19571 		mutex_exit(&ill->ill_lock);
19572 		ill_refrele(ill);
19573 		if (error != NULL)
19574 			*error = ENXIO;
19575 		return (NULL);
19576 	}
19577 
19578 	/*
19579 	 * If none found, atomically allocate and return a new one.
19580 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19581 	 * to support "receive only" use of lo0:1 etc. as is still done
19582 	 * below as an initial guess.
19583 	 * However, this is now likely to be overriden later in ipif_up_done()
19584 	 * when we know for sure what address has been configured on the
19585 	 * interface, since we might have more than one loopback interface
19586 	 * with a loopback address, e.g. in the case of zones, and all the
19587 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19588 	 */
19589 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19590 		ire_type = IRE_LOOPBACK;
19591 	else
19592 		ire_type = IRE_LOCAL;
19593 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19594 	if (ipif != NULL)
19595 		ipif_refhold_locked(ipif);
19596 	else if (error != NULL)
19597 		*error = ENOMEM;
19598 	mutex_exit(&ill->ill_lock);
19599 	ill_refrele(ill);
19600 	return (ipif);
19601 }
19602 
19603 /*
19604  * This routine is called whenever a new address comes up on an ipif.  If
19605  * we are configured to respond to address mask requests, then we are supposed
19606  * to broadcast an address mask reply at this time.  This routine is also
19607  * called if we are already up, but a netmask change is made.  This is legal
19608  * but might not make the system manager very popular.	(May be called
19609  * as writer.)
19610  */
19611 void
19612 ipif_mask_reply(ipif_t *ipif)
19613 {
19614 	icmph_t	*icmph;
19615 	ipha_t	*ipha;
19616 	mblk_t	*mp;
19617 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19618 
19619 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19620 
19621 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19622 		return;
19623 
19624 	/* ICMP mask reply is IPv4 only */
19625 	ASSERT(!ipif->ipif_isv6);
19626 	/* ICMP mask reply is not for a loopback interface */
19627 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19628 
19629 	mp = allocb(REPLY_LEN, BPRI_HI);
19630 	if (mp == NULL)
19631 		return;
19632 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19633 
19634 	ipha = (ipha_t *)mp->b_rptr;
19635 	bzero(ipha, REPLY_LEN);
19636 	*ipha = icmp_ipha;
19637 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19638 	ipha->ipha_src = ipif->ipif_src_addr;
19639 	ipha->ipha_dst = ipif->ipif_brd_addr;
19640 	ipha->ipha_length = htons(REPLY_LEN);
19641 	ipha->ipha_ident = 0;
19642 
19643 	icmph = (icmph_t *)&ipha[1];
19644 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19645 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19646 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19647 
19648 	put(ipif->ipif_wq, mp);
19649 
19650 #undef	REPLY_LEN
19651 }
19652 
19653 /*
19654  * When the mtu in the ipif changes, we call this routine through ire_walk
19655  * to update all the relevant IREs.
19656  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19657  */
19658 static void
19659 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19660 {
19661 	ipif_t *ipif = (ipif_t *)ipif_arg;
19662 
19663 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19664 		return;
19665 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19666 }
19667 
19668 /*
19669  * When the mtu in the ill changes, we call this routine through ire_walk
19670  * to update all the relevant IREs.
19671  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19672  */
19673 void
19674 ill_mtu_change(ire_t *ire, char *ill_arg)
19675 {
19676 	ill_t	*ill = (ill_t *)ill_arg;
19677 
19678 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19679 		return;
19680 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19681 }
19682 
19683 /*
19684  * Join the ipif specific multicast groups.
19685  * Must be called after a mapping has been set up in the resolver.  (Always
19686  * called as writer.)
19687  */
19688 void
19689 ipif_multicast_up(ipif_t *ipif)
19690 {
19691 	int err, index;
19692 	ill_t *ill;
19693 
19694 	ASSERT(IAM_WRITER_IPIF(ipif));
19695 
19696 	ill = ipif->ipif_ill;
19697 	index = ill->ill_phyint->phyint_ifindex;
19698 
19699 	ip1dbg(("ipif_multicast_up\n"));
19700 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19701 		return;
19702 
19703 	if (ipif->ipif_isv6) {
19704 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19705 			return;
19706 
19707 		/* Join the all hosts multicast address */
19708 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19709 		/*
19710 		 * Passing B_TRUE means we have to join the multicast
19711 		 * membership on this interface even though this is
19712 		 * FAILED. If we join on a different one in the group,
19713 		 * we will not be able to delete the membership later
19714 		 * as we currently don't track where we join when we
19715 		 * join within the kernel unlike applications where
19716 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19717 		 * for more on this.
19718 		 */
19719 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19720 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19721 		if (err != 0) {
19722 			ip0dbg(("ipif_multicast_up: "
19723 			    "all_hosts_mcast failed %d\n",
19724 			    err));
19725 			return;
19726 		}
19727 		/*
19728 		 * Enable multicast for the solicited node multicast address
19729 		 */
19730 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19731 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19732 
19733 			ipv6_multi.s6_addr32[3] |=
19734 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19735 
19736 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19737 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19738 			    NULL);
19739 			if (err != 0) {
19740 				ip0dbg(("ipif_multicast_up: solicited MC"
19741 				    " failed %d\n", err));
19742 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19743 				    ill, ill->ill_phyint->phyint_ifindex,
19744 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19745 				return;
19746 			}
19747 		}
19748 	} else {
19749 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19750 			return;
19751 
19752 		/* Join the all hosts multicast address */
19753 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19754 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19755 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19756 		if (err) {
19757 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19758 			return;
19759 		}
19760 	}
19761 	ipif->ipif_multicast_up = 1;
19762 }
19763 
19764 /*
19765  * Blow away any multicast groups that we joined in ipif_multicast_up().
19766  * (Explicit memberships are blown away in ill_leave_multicast() when the
19767  * ill is brought down.)
19768  */
19769 static void
19770 ipif_multicast_down(ipif_t *ipif)
19771 {
19772 	int err;
19773 
19774 	ASSERT(IAM_WRITER_IPIF(ipif));
19775 
19776 	ip1dbg(("ipif_multicast_down\n"));
19777 	if (!ipif->ipif_multicast_up)
19778 		return;
19779 
19780 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19781 
19782 	if (!ipif->ipif_isv6) {
19783 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19784 		    B_TRUE);
19785 		if (err != 0)
19786 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19787 
19788 		ipif->ipif_multicast_up = 0;
19789 		return;
19790 	}
19791 
19792 	/*
19793 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19794 	 * we should look for ilms on this ill rather than the ones that have
19795 	 * been failed over here.  They are here temporarily. As
19796 	 * ipif_multicast_up has joined on this ill, we should delete only
19797 	 * from this ill.
19798 	 */
19799 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19800 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19801 	    B_TRUE, B_TRUE);
19802 	if (err != 0) {
19803 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19804 		    err));
19805 	}
19806 	/*
19807 	 * Disable multicast for the solicited node multicast address
19808 	 */
19809 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19810 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19811 
19812 		ipv6_multi.s6_addr32[3] |=
19813 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19814 
19815 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19816 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19817 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19818 
19819 		if (err != 0) {
19820 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19821 			    err));
19822 		}
19823 	}
19824 
19825 	ipif->ipif_multicast_up = 0;
19826 }
19827 
19828 /*
19829  * Used when an interface comes up to recreate any extra routes on this
19830  * interface.
19831  */
19832 static ire_t **
19833 ipif_recover_ire(ipif_t *ipif)
19834 {
19835 	mblk_t	*mp;
19836 	ire_t	**ipif_saved_irep;
19837 	ire_t	**irep;
19838 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19839 
19840 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19841 	    ipif->ipif_id));
19842 
19843 	mutex_enter(&ipif->ipif_saved_ire_lock);
19844 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19845 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19846 	if (ipif_saved_irep == NULL) {
19847 		mutex_exit(&ipif->ipif_saved_ire_lock);
19848 		return (NULL);
19849 	}
19850 
19851 	irep = ipif_saved_irep;
19852 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19853 		ire_t		*ire;
19854 		queue_t		*rfq;
19855 		queue_t		*stq;
19856 		ifrt_t		*ifrt;
19857 		uchar_t		*src_addr;
19858 		uchar_t		*gateway_addr;
19859 		ushort_t	type;
19860 
19861 		/*
19862 		 * When the ire was initially created and then added in
19863 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19864 		 * in the case of a traditional interface route, or as one of
19865 		 * the IRE_OFFSUBNET types (with the exception of
19866 		 * IRE_HOST types ire which is created by icmp_redirect() and
19867 		 * which we don't need to save or recover).  In the case where
19868 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19869 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19870 		 * to satisfy software like GateD and Sun Cluster which creates
19871 		 * routes using the the loopback interface's address as a
19872 		 * gateway.
19873 		 *
19874 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19875 		 * ire_create() will be called in the same way here as
19876 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19877 		 * the route looks like a traditional interface route (where
19878 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19879 		 * the saved ifrt->ifrt_type.  This means that in the case where
19880 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19881 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19882 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19883 		 */
19884 		ifrt = (ifrt_t *)mp->b_rptr;
19885 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19886 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19887 			rfq = NULL;
19888 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19889 			    ? ipif->ipif_rq : ipif->ipif_wq;
19890 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19891 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19892 			    : (uint8_t *)&ipif->ipif_src_addr;
19893 			gateway_addr = NULL;
19894 			type = ipif->ipif_net_type;
19895 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19896 			/* Recover multiroute broadcast IRE. */
19897 			rfq = ipif->ipif_rq;
19898 			stq = ipif->ipif_wq;
19899 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19900 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19901 			    : (uint8_t *)&ipif->ipif_src_addr;
19902 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19903 			type = ifrt->ifrt_type;
19904 		} else {
19905 			rfq = NULL;
19906 			stq = NULL;
19907 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19908 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19909 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19910 			type = ifrt->ifrt_type;
19911 		}
19912 
19913 		/*
19914 		 * Create a copy of the IRE with the saved address and netmask.
19915 		 */
19916 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19917 		    "0x%x/0x%x\n",
19918 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19919 		    ntohl(ifrt->ifrt_addr),
19920 		    ntohl(ifrt->ifrt_mask)));
19921 		ire = ire_create(
19922 		    (uint8_t *)&ifrt->ifrt_addr,
19923 		    (uint8_t *)&ifrt->ifrt_mask,
19924 		    src_addr,
19925 		    gateway_addr,
19926 		    NULL,
19927 		    &ifrt->ifrt_max_frag,
19928 		    NULL,
19929 		    rfq,
19930 		    stq,
19931 		    type,
19932 		    ipif,
19933 		    NULL,
19934 		    0,
19935 		    0,
19936 		    0,
19937 		    ifrt->ifrt_flags,
19938 		    &ifrt->ifrt_iulp_info,
19939 		    NULL,
19940 		    NULL,
19941 		    ipst);
19942 
19943 		if (ire == NULL) {
19944 			mutex_exit(&ipif->ipif_saved_ire_lock);
19945 			kmem_free(ipif_saved_irep,
19946 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19947 			return (NULL);
19948 		}
19949 
19950 		/*
19951 		 * Some software (for example, GateD and Sun Cluster) attempts
19952 		 * to create (what amount to) IRE_PREFIX routes with the
19953 		 * loopback address as the gateway.  This is primarily done to
19954 		 * set up prefixes with the RTF_REJECT flag set (for example,
19955 		 * when generating aggregate routes.)
19956 		 *
19957 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19958 		 * IRE_LOOPBACK, then we map the request into a
19959 		 * IRE_IF_NORESOLVER.
19960 		 */
19961 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19962 			ire->ire_type = IRE_IF_NORESOLVER;
19963 		/*
19964 		 * ire held by ire_add, will be refreled' towards the
19965 		 * the end of ipif_up_done
19966 		 */
19967 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19968 		*irep = ire;
19969 		irep++;
19970 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19971 	}
19972 	mutex_exit(&ipif->ipif_saved_ire_lock);
19973 	return (ipif_saved_irep);
19974 }
19975 
19976 /*
19977  * Used to set the netmask and broadcast address to default values when the
19978  * interface is brought up.  (Always called as writer.)
19979  */
19980 static void
19981 ipif_set_default(ipif_t *ipif)
19982 {
19983 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19984 
19985 	if (!ipif->ipif_isv6) {
19986 		/*
19987 		 * Interface holds an IPv4 address. Default
19988 		 * mask is the natural netmask.
19989 		 */
19990 		if (!ipif->ipif_net_mask) {
19991 			ipaddr_t	v4mask;
19992 
19993 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19994 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19995 		}
19996 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19997 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19998 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19999 		} else {
20000 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
20001 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
20002 		}
20003 		/*
20004 		 * NOTE: SunOS 4.X does this even if the broadcast address
20005 		 * has been already set thus we do the same here.
20006 		 */
20007 		if (ipif->ipif_flags & IPIF_BROADCAST) {
20008 			ipaddr_t	v4addr;
20009 
20010 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
20011 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
20012 		}
20013 	} else {
20014 		/*
20015 		 * Interface holds an IPv6-only address.  Default
20016 		 * mask is all-ones.
20017 		 */
20018 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
20019 			ipif->ipif_v6net_mask = ipv6_all_ones;
20020 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20021 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20022 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
20023 		} else {
20024 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
20025 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
20026 		}
20027 	}
20028 }
20029 
20030 /*
20031  * Return 0 if this address can be used as local address without causing
20032  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
20033  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
20034  * Special checks are needed to allow the same IPv6 link-local address
20035  * on different ills.
20036  * TODO: allowing the same site-local address on different ill's.
20037  */
20038 int
20039 ip_addr_availability_check(ipif_t *new_ipif)
20040 {
20041 	in6_addr_t our_v6addr;
20042 	ill_t *ill;
20043 	ipif_t *ipif;
20044 	ill_walk_context_t ctx;
20045 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
20046 
20047 	ASSERT(IAM_WRITER_IPIF(new_ipif));
20048 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
20049 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
20050 
20051 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
20052 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
20053 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
20054 		return (0);
20055 
20056 	our_v6addr = new_ipif->ipif_v6lcl_addr;
20057 
20058 	if (new_ipif->ipif_isv6)
20059 		ill = ILL_START_WALK_V6(&ctx, ipst);
20060 	else
20061 		ill = ILL_START_WALK_V4(&ctx, ipst);
20062 
20063 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
20064 		for (ipif = ill->ill_ipif; ipif != NULL;
20065 		    ipif = ipif->ipif_next) {
20066 			if ((ipif == new_ipif) ||
20067 			    !(ipif->ipif_flags & IPIF_UP) ||
20068 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
20069 				continue;
20070 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
20071 			    &our_v6addr)) {
20072 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
20073 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
20074 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
20075 					ipif->ipif_flags |= IPIF_UNNUMBERED;
20076 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
20077 				    new_ipif->ipif_ill != ill)
20078 					continue;
20079 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
20080 				    new_ipif->ipif_ill != ill)
20081 					continue;
20082 				else if (new_ipif->ipif_zoneid !=
20083 				    ipif->ipif_zoneid &&
20084 				    ipif->ipif_zoneid != ALL_ZONES &&
20085 				    IS_LOOPBACK(ill))
20086 					continue;
20087 				else if (new_ipif->ipif_ill == ill)
20088 					return (EADDRINUSE);
20089 				else
20090 					return (EADDRNOTAVAIL);
20091 			}
20092 		}
20093 	}
20094 
20095 	return (0);
20096 }
20097 
20098 /*
20099  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
20100  * IREs for the ipif.
20101  * When the routine returns EINPROGRESS then mp has been consumed and
20102  * the ioctl will be acked from ip_rput_dlpi.
20103  */
20104 static int
20105 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
20106 {
20107 	ill_t	*ill = ipif->ipif_ill;
20108 	boolean_t isv6 = ipif->ipif_isv6;
20109 	int	err = 0;
20110 	boolean_t success;
20111 
20112 	ASSERT(IAM_WRITER_IPIF(ipif));
20113 
20114 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
20115 
20116 	/* Shouldn't get here if it is already up. */
20117 	if (ipif->ipif_flags & IPIF_UP)
20118 		return (EALREADY);
20119 
20120 	/* Skip arp/ndp for any loopback interface. */
20121 	if (ill->ill_wq != NULL) {
20122 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
20123 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
20124 
20125 		if (!ill->ill_dl_up) {
20126 			/*
20127 			 * ill_dl_up is not yet set. i.e. we are yet to
20128 			 * DL_BIND with the driver and this is the first
20129 			 * logical interface on the ill to become "up".
20130 			 * Tell the driver to get going (via DL_BIND_REQ).
20131 			 * Note that changing "significant" IFF_ flags
20132 			 * address/netmask etc cause a down/up dance, but
20133 			 * does not cause an unbind (DL_UNBIND) with the driver
20134 			 */
20135 			return (ill_dl_up(ill, ipif, mp, q));
20136 		}
20137 
20138 		/*
20139 		 * ipif_resolver_up may end up sending an
20140 		 * AR_INTERFACE_UP message to ARP, which would, in
20141 		 * turn send a DLPI message to the driver. ioctls are
20142 		 * serialized and so we cannot send more than one
20143 		 * interface up message at a time. If ipif_resolver_up
20144 		 * does send an interface up message to ARP, we get
20145 		 * EINPROGRESS and we will complete in ip_arp_done.
20146 		 */
20147 
20148 		ASSERT(connp != NULL || !CONN_Q(q));
20149 		ASSERT(ipsq->ipsq_pending_mp == NULL);
20150 		if (connp != NULL)
20151 			mutex_enter(&connp->conn_lock);
20152 		mutex_enter(&ill->ill_lock);
20153 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
20154 		mutex_exit(&ill->ill_lock);
20155 		if (connp != NULL)
20156 			mutex_exit(&connp->conn_lock);
20157 		if (!success)
20158 			return (EINTR);
20159 
20160 		/*
20161 		 * Crank up IPv6 neighbor discovery
20162 		 * Unlike ARP, this should complete when
20163 		 * ipif_ndp_up returns. However, for
20164 		 * ILLF_XRESOLV interfaces we also send a
20165 		 * AR_INTERFACE_UP to the external resolver.
20166 		 * That ioctl will complete in ip_rput.
20167 		 */
20168 		if (isv6) {
20169 			err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr);
20170 			if (err != 0) {
20171 				if (err != EINPROGRESS)
20172 					mp = ipsq_pending_mp_get(ipsq, &connp);
20173 				return (err);
20174 			}
20175 		}
20176 		/* Now, ARP */
20177 		err = ipif_resolver_up(ipif, Res_act_initial);
20178 		if (err == EINPROGRESS) {
20179 			/* We will complete it in ip_arp_done */
20180 			return (err);
20181 		}
20182 		mp = ipsq_pending_mp_get(ipsq, &connp);
20183 		ASSERT(mp != NULL);
20184 		if (err != 0)
20185 			return (err);
20186 	} else {
20187 		/*
20188 		 * Interfaces without underlying hardware don't do duplicate
20189 		 * address detection.
20190 		 */
20191 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
20192 		ipif->ipif_addr_ready = 1;
20193 	}
20194 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
20195 }
20196 
20197 /*
20198  * Perform a bind for the physical device.
20199  * When the routine returns EINPROGRESS then mp has been consumed and
20200  * the ioctl will be acked from ip_rput_dlpi.
20201  * Allocate an unbind message and save it until ipif_down.
20202  */
20203 static int
20204 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
20205 {
20206 	areq_t	*areq;
20207 	mblk_t	*areq_mp = NULL;
20208 	mblk_t	*bind_mp = NULL;
20209 	mblk_t	*unbind_mp = NULL;
20210 	conn_t	*connp;
20211 	boolean_t success;
20212 	uint16_t sap_addr;
20213 
20214 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
20215 	ASSERT(IAM_WRITER_ILL(ill));
20216 	ASSERT(mp != NULL);
20217 
20218 	/* Create a resolver cookie for ARP */
20219 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
20220 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
20221 		if (areq_mp == NULL)
20222 			return (ENOMEM);
20223 
20224 		freemsg(ill->ill_resolver_mp);
20225 		ill->ill_resolver_mp = areq_mp;
20226 		areq = (areq_t *)areq_mp->b_rptr;
20227 		sap_addr = ill->ill_sap;
20228 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
20229 	}
20230 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
20231 	    DL_BIND_REQ);
20232 	if (bind_mp == NULL)
20233 		goto bad;
20234 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
20235 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
20236 
20237 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
20238 	if (unbind_mp == NULL)
20239 		goto bad;
20240 
20241 	/*
20242 	 * Record state needed to complete this operation when the
20243 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
20244 	 */
20245 	ASSERT(WR(q)->q_next == NULL);
20246 	connp = Q_TO_CONN(q);
20247 
20248 	mutex_enter(&connp->conn_lock);
20249 	mutex_enter(&ipif->ipif_ill->ill_lock);
20250 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
20251 	mutex_exit(&ipif->ipif_ill->ill_lock);
20252 	mutex_exit(&connp->conn_lock);
20253 	if (!success)
20254 		goto bad;
20255 
20256 	/*
20257 	 * Save the unbind message for ill_dl_down(); it will be consumed when
20258 	 * the interface goes down.
20259 	 */
20260 	ASSERT(ill->ill_unbind_mp == NULL);
20261 	ill->ill_unbind_mp = unbind_mp;
20262 
20263 	ill_dlpi_send(ill, bind_mp);
20264 	/* Send down link-layer capabilities probe if not already done. */
20265 	ill_capability_probe(ill);
20266 
20267 	/*
20268 	 * Sysid used to rely on the fact that netboots set domainname
20269 	 * and the like. Now that miniroot boots aren't strictly netboots
20270 	 * and miniroot network configuration is driven from userland
20271 	 * these things still need to be set. This situation can be detected
20272 	 * by comparing the interface being configured here to the one
20273 	 * dhcack was set to reference by the boot loader. Once sysid is
20274 	 * converted to use dhcp_ipc_getinfo() this call can go away.
20275 	 */
20276 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
20277 	    (strcmp(ill->ill_name, dhcack) == 0) &&
20278 	    (strlen(srpc_domain) == 0)) {
20279 		if (dhcpinit() != 0)
20280 			cmn_err(CE_WARN, "no cached dhcp response");
20281 	}
20282 
20283 	/*
20284 	 * This operation will complete in ip_rput_dlpi with either
20285 	 * a DL_BIND_ACK or DL_ERROR_ACK.
20286 	 */
20287 	return (EINPROGRESS);
20288 bad:
20289 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
20290 	/*
20291 	 * We don't have to check for possible removal from illgrp
20292 	 * as we have not yet inserted in illgrp. For groups
20293 	 * without names, this ipif is still not UP and hence
20294 	 * this could not have possibly had any influence in forming
20295 	 * groups.
20296 	 */
20297 
20298 	freemsg(bind_mp);
20299 	freemsg(unbind_mp);
20300 	return (ENOMEM);
20301 }
20302 
20303 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
20304 
20305 /*
20306  * DLPI and ARP is up.
20307  * Create all the IREs associated with an interface bring up multicast.
20308  * Set the interface flag and finish other initialization
20309  * that potentially had to be differed to after DL_BIND_ACK.
20310  */
20311 int
20312 ipif_up_done(ipif_t *ipif)
20313 {
20314 	ire_t	*ire_array[20];
20315 	ire_t	**irep = ire_array;
20316 	ire_t	**irep1;
20317 	ipaddr_t net_mask = 0;
20318 	ipaddr_t subnet_mask, route_mask;
20319 	ill_t	*ill = ipif->ipif_ill;
20320 	queue_t	*stq;
20321 	ipif_t	 *src_ipif;
20322 	ipif_t   *tmp_ipif;
20323 	boolean_t	flush_ire_cache = B_TRUE;
20324 	int	err = 0;
20325 	phyint_t *phyi;
20326 	ire_t	**ipif_saved_irep = NULL;
20327 	int ipif_saved_ire_cnt;
20328 	int	cnt;
20329 	boolean_t	src_ipif_held = B_FALSE;
20330 	boolean_t	ire_added = B_FALSE;
20331 	boolean_t	loopback = B_FALSE;
20332 	ip_stack_t	*ipst = ill->ill_ipst;
20333 
20334 	ip1dbg(("ipif_up_done(%s:%u)\n",
20335 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
20336 	/* Check if this is a loopback interface */
20337 	if (ipif->ipif_ill->ill_wq == NULL)
20338 		loopback = B_TRUE;
20339 
20340 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20341 	/*
20342 	 * If all other interfaces for this ill are down or DEPRECATED,
20343 	 * or otherwise unsuitable for source address selection, remove
20344 	 * any IRE_CACHE entries for this ill to make sure source
20345 	 * address selection gets to take this new ipif into account.
20346 	 * No need to hold ill_lock while traversing the ipif list since
20347 	 * we are writer
20348 	 */
20349 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20350 	    tmp_ipif = tmp_ipif->ipif_next) {
20351 		if (((tmp_ipif->ipif_flags &
20352 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20353 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20354 		    (tmp_ipif == ipif))
20355 			continue;
20356 		/* first useable pre-existing interface */
20357 		flush_ire_cache = B_FALSE;
20358 		break;
20359 	}
20360 	if (flush_ire_cache)
20361 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20362 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20363 
20364 	/*
20365 	 * Figure out which way the send-to queue should go.  Only
20366 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20367 	 * should show up here.
20368 	 */
20369 	switch (ill->ill_net_type) {
20370 	case IRE_IF_RESOLVER:
20371 		stq = ill->ill_rq;
20372 		break;
20373 	case IRE_IF_NORESOLVER:
20374 	case IRE_LOOPBACK:
20375 		stq = ill->ill_wq;
20376 		break;
20377 	default:
20378 		return (EINVAL);
20379 	}
20380 
20381 	if (IS_LOOPBACK(ill)) {
20382 		/*
20383 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20384 		 * ipif_lookup_on_name(), but in the case of zones we can have
20385 		 * several loopback addresses on lo0. So all the interfaces with
20386 		 * loopback addresses need to be marked IRE_LOOPBACK.
20387 		 */
20388 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20389 		    htonl(INADDR_LOOPBACK))
20390 			ipif->ipif_ire_type = IRE_LOOPBACK;
20391 		else
20392 			ipif->ipif_ire_type = IRE_LOCAL;
20393 	}
20394 
20395 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20396 		/*
20397 		 * Can't use our source address. Select a different
20398 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20399 		 */
20400 		src_ipif = ipif_select_source(ipif->ipif_ill,
20401 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20402 		if (src_ipif == NULL)
20403 			src_ipif = ipif;	/* Last resort */
20404 		else
20405 			src_ipif_held = B_TRUE;
20406 	} else {
20407 		src_ipif = ipif;
20408 	}
20409 
20410 	/* Create all the IREs associated with this interface */
20411 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20412 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20413 
20414 		/*
20415 		 * If we're on a labeled system then make sure that zone-
20416 		 * private addresses have proper remote host database entries.
20417 		 */
20418 		if (is_system_labeled() &&
20419 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20420 		    !tsol_check_interface_address(ipif))
20421 			return (EINVAL);
20422 
20423 		/* Register the source address for __sin6_src_id */
20424 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20425 		    ipif->ipif_zoneid, ipst);
20426 		if (err != 0) {
20427 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20428 			return (err);
20429 		}
20430 
20431 		/* If the interface address is set, create the local IRE. */
20432 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20433 		    (void *)ipif,
20434 		    ipif->ipif_ire_type,
20435 		    ntohl(ipif->ipif_lcl_addr)));
20436 		*irep++ = ire_create(
20437 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20438 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20439 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20440 		    NULL,				/* no gateway */
20441 		    NULL,
20442 		    &ip_loopback_mtuplus,		/* max frag size */
20443 		    NULL,
20444 		    ipif->ipif_rq,			/* recv-from queue */
20445 		    NULL,				/* no send-to queue */
20446 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20447 		    ipif,
20448 		    NULL,
20449 		    0,
20450 		    0,
20451 		    0,
20452 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20453 		    RTF_PRIVATE : 0,
20454 		    &ire_uinfo_null,
20455 		    NULL,
20456 		    NULL,
20457 		    ipst);
20458 	} else {
20459 		ip1dbg((
20460 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20461 		    ipif->ipif_ire_type,
20462 		    ntohl(ipif->ipif_lcl_addr),
20463 		    (uint_t)ipif->ipif_flags));
20464 	}
20465 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20466 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20467 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20468 	} else {
20469 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20470 	}
20471 
20472 	subnet_mask = ipif->ipif_net_mask;
20473 
20474 	/*
20475 	 * If mask was not specified, use natural netmask of
20476 	 * interface address. Also, store this mask back into the
20477 	 * ipif struct.
20478 	 */
20479 	if (subnet_mask == 0) {
20480 		subnet_mask = net_mask;
20481 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20482 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20483 		    ipif->ipif_v6subnet);
20484 	}
20485 
20486 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20487 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20488 	    ipif->ipif_subnet != INADDR_ANY) {
20489 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20490 
20491 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20492 			route_mask = IP_HOST_MASK;
20493 		} else {
20494 			route_mask = subnet_mask;
20495 		}
20496 
20497 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20498 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20499 		    (void *)ipif, (void *)ill,
20500 		    ill->ill_net_type,
20501 		    ntohl(ipif->ipif_subnet)));
20502 		*irep++ = ire_create(
20503 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20504 		    (uchar_t *)&route_mask,		/* mask */
20505 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20506 		    NULL,				/* no gateway */
20507 		    NULL,
20508 		    &ipif->ipif_mtu,			/* max frag */
20509 		    NULL,
20510 		    NULL,				/* no recv queue */
20511 		    stq,				/* send-to queue */
20512 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20513 		    ipif,
20514 		    NULL,
20515 		    0,
20516 		    0,
20517 		    0,
20518 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20519 		    &ire_uinfo_null,
20520 		    NULL,
20521 		    NULL,
20522 		    ipst);
20523 	}
20524 
20525 	/*
20526 	 * Create any necessary broadcast IREs.
20527 	 */
20528 	if ((ipif->ipif_subnet != INADDR_ANY) &&
20529 	    (ipif->ipif_flags & IPIF_BROADCAST))
20530 		irep = ipif_create_bcast_ires(ipif, irep);
20531 
20532 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20533 
20534 	/* If an earlier ire_create failed, get out now */
20535 	for (irep1 = irep; irep1 > ire_array; ) {
20536 		irep1--;
20537 		if (*irep1 == NULL) {
20538 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20539 			err = ENOMEM;
20540 			goto bad;
20541 		}
20542 	}
20543 
20544 	/*
20545 	 * Need to atomically check for ip_addr_availablity_check
20546 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20547 	 * from group also.The ill_g_lock is grabbed as reader
20548 	 * just to make sure no new ills or new ipifs are being added
20549 	 * to the system while we are checking the uniqueness of addresses.
20550 	 */
20551 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20552 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20553 	/* Mark it up, and increment counters. */
20554 	ipif->ipif_flags |= IPIF_UP;
20555 	ill->ill_ipif_up_count++;
20556 	err = ip_addr_availability_check(ipif);
20557 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20558 	rw_exit(&ipst->ips_ill_g_lock);
20559 
20560 	if (err != 0) {
20561 		/*
20562 		 * Our address may already be up on the same ill. In this case,
20563 		 * the ARP entry for our ipif replaced the one for the other
20564 		 * ipif. So we don't want to delete it (otherwise the other ipif
20565 		 * would be unable to send packets).
20566 		 * ip_addr_availability_check() identifies this case for us and
20567 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20568 		 * which is the expected error code.
20569 		 */
20570 		if (err == EADDRINUSE) {
20571 			freemsg(ipif->ipif_arp_del_mp);
20572 			ipif->ipif_arp_del_mp = NULL;
20573 			err = EADDRNOTAVAIL;
20574 		}
20575 		ill->ill_ipif_up_count--;
20576 		ipif->ipif_flags &= ~IPIF_UP;
20577 		goto bad;
20578 	}
20579 
20580 	/*
20581 	 * Add in all newly created IREs.  ire_create_bcast() has
20582 	 * already checked for duplicates of the IRE_BROADCAST type.
20583 	 * We want to add before we call ifgrp_insert which wants
20584 	 * to know whether IRE_IF_RESOLVER exists or not.
20585 	 *
20586 	 * NOTE : We refrele the ire though we may branch to "bad"
20587 	 *	  later on where we do ire_delete. This is okay
20588 	 *	  because nobody can delete it as we are running
20589 	 *	  exclusively.
20590 	 */
20591 	for (irep1 = irep; irep1 > ire_array; ) {
20592 		irep1--;
20593 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20594 		/*
20595 		 * refheld by ire_add. refele towards the end of the func
20596 		 */
20597 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20598 	}
20599 	ire_added = B_TRUE;
20600 	/*
20601 	 * Form groups if possible.
20602 	 *
20603 	 * If we are supposed to be in a ill_group with a name, insert it
20604 	 * now as we know that at least one ipif is UP. Otherwise form
20605 	 * nameless groups.
20606 	 *
20607 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20608 	 * this ipif into the appropriate interface group, or create a
20609 	 * new one. If this is already in a nameless group, we try to form
20610 	 * a bigger group looking at other ills potentially sharing this
20611 	 * ipif's prefix.
20612 	 */
20613 	phyi = ill->ill_phyint;
20614 	if (phyi->phyint_groupname_len != 0) {
20615 		ASSERT(phyi->phyint_groupname != NULL);
20616 		if (ill->ill_ipif_up_count == 1) {
20617 			ASSERT(ill->ill_group == NULL);
20618 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20619 			    phyi->phyint_groupname, NULL, B_TRUE);
20620 			if (err != 0) {
20621 				ip1dbg(("ipif_up_done: illgrp allocation "
20622 				    "failed, error %d\n", err));
20623 				goto bad;
20624 			}
20625 		}
20626 		ASSERT(ill->ill_group != NULL);
20627 	}
20628 
20629 	/*
20630 	 * When this is part of group, we need to make sure that
20631 	 * any broadcast ires created because of this ipif coming
20632 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20633 	 * so that we don't receive duplicate broadcast packets.
20634 	 */
20635 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20636 		ipif_renominate_bcast(ipif);
20637 
20638 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20639 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20640 	ipif_saved_irep = ipif_recover_ire(ipif);
20641 
20642 	if (!loopback) {
20643 		/*
20644 		 * If the broadcast address has been set, make sure it makes
20645 		 * sense based on the interface address.
20646 		 * Only match on ill since we are sharing broadcast addresses.
20647 		 */
20648 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20649 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20650 			ire_t	*ire;
20651 
20652 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20653 			    IRE_BROADCAST, ipif, ALL_ZONES,
20654 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20655 
20656 			if (ire == NULL) {
20657 				/*
20658 				 * If there isn't a matching broadcast IRE,
20659 				 * revert to the default for this netmask.
20660 				 */
20661 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20662 				mutex_enter(&ipif->ipif_ill->ill_lock);
20663 				ipif_set_default(ipif);
20664 				mutex_exit(&ipif->ipif_ill->ill_lock);
20665 			} else {
20666 				ire_refrele(ire);
20667 			}
20668 		}
20669 
20670 	}
20671 
20672 	/* This is the first interface on this ill */
20673 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20674 		/*
20675 		 * Need to recover all multicast memberships in the driver.
20676 		 * This had to be deferred until we had attached.
20677 		 */
20678 		ill_recover_multicast(ill);
20679 	}
20680 	/* Join the allhosts multicast address */
20681 	ipif_multicast_up(ipif);
20682 
20683 	if (!loopback) {
20684 		/*
20685 		 * See whether anybody else would benefit from the
20686 		 * new ipif that we added. We call this always rather
20687 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20688 		 * ipif is for the benefit of illgrp_insert (done above)
20689 		 * which does not do source address selection as it does
20690 		 * not want to re-create interface routes that we are
20691 		 * having reference to it here.
20692 		 */
20693 		ill_update_source_selection(ill);
20694 	}
20695 
20696 	for (irep1 = irep; irep1 > ire_array; ) {
20697 		irep1--;
20698 		if (*irep1 != NULL) {
20699 			/* was held in ire_add */
20700 			ire_refrele(*irep1);
20701 		}
20702 	}
20703 
20704 	cnt = ipif_saved_ire_cnt;
20705 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20706 		if (*irep1 != NULL) {
20707 			/* was held in ire_add */
20708 			ire_refrele(*irep1);
20709 		}
20710 	}
20711 
20712 	if (!loopback && ipif->ipif_addr_ready) {
20713 		/* Broadcast an address mask reply. */
20714 		ipif_mask_reply(ipif);
20715 	}
20716 	if (ipif_saved_irep != NULL) {
20717 		kmem_free(ipif_saved_irep,
20718 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20719 	}
20720 	if (src_ipif_held)
20721 		ipif_refrele(src_ipif);
20722 
20723 	/*
20724 	 * This had to be deferred until we had bound.  Tell routing sockets and
20725 	 * others that this interface is up if it looks like the address has
20726 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20727 	 * duplicate address detection to do its thing.
20728 	 */
20729 	if (ipif->ipif_addr_ready) {
20730 		ip_rts_ifmsg(ipif);
20731 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20732 		/* Let SCTP update the status for this ipif */
20733 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20734 	}
20735 	return (0);
20736 
20737 bad:
20738 	ip1dbg(("ipif_up_done: FAILED \n"));
20739 	/*
20740 	 * We don't have to bother removing from ill groups because
20741 	 *
20742 	 * 1) For groups with names, we insert only when the first ipif
20743 	 *    comes up. In that case if it fails, it will not be in any
20744 	 *    group. So, we need not try to remove for that case.
20745 	 *
20746 	 * 2) For groups without names, either we tried to insert ipif_ill
20747 	 *    in a group as singleton or found some other group to become
20748 	 *    a bigger group. For the former, if it fails we don't have
20749 	 *    anything to do as ipif_ill is not in the group and for the
20750 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20751 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20752 	 */
20753 	while (irep > ire_array) {
20754 		irep--;
20755 		if (*irep != NULL) {
20756 			ire_delete(*irep);
20757 			if (ire_added)
20758 				ire_refrele(*irep);
20759 		}
20760 	}
20761 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20762 
20763 	if (ipif_saved_irep != NULL) {
20764 		kmem_free(ipif_saved_irep,
20765 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20766 	}
20767 	if (src_ipif_held)
20768 		ipif_refrele(src_ipif);
20769 
20770 	ipif_arp_down(ipif);
20771 	return (err);
20772 }
20773 
20774 /*
20775  * Turn off the ARP with the ILLF_NOARP flag.
20776  */
20777 static int
20778 ill_arp_off(ill_t *ill)
20779 {
20780 	mblk_t	*arp_off_mp = NULL;
20781 	mblk_t	*arp_on_mp = NULL;
20782 
20783 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20784 
20785 	ASSERT(IAM_WRITER_ILL(ill));
20786 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20787 
20788 	/*
20789 	 * If the on message is still around we've already done
20790 	 * an arp_off without doing an arp_on thus there is no
20791 	 * work needed.
20792 	 */
20793 	if (ill->ill_arp_on_mp != NULL)
20794 		return (0);
20795 
20796 	/*
20797 	 * Allocate an ARP on message (to be saved) and an ARP off message
20798 	 */
20799 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20800 	if (!arp_off_mp)
20801 		return (ENOMEM);
20802 
20803 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20804 	if (!arp_on_mp)
20805 		goto failed;
20806 
20807 	ASSERT(ill->ill_arp_on_mp == NULL);
20808 	ill->ill_arp_on_mp = arp_on_mp;
20809 
20810 	/* Send an AR_INTERFACE_OFF request */
20811 	putnext(ill->ill_rq, arp_off_mp);
20812 	return (0);
20813 failed:
20814 
20815 	if (arp_off_mp)
20816 		freemsg(arp_off_mp);
20817 	return (ENOMEM);
20818 }
20819 
20820 /*
20821  * Turn on ARP by turning off the ILLF_NOARP flag.
20822  */
20823 static int
20824 ill_arp_on(ill_t *ill)
20825 {
20826 	mblk_t	*mp;
20827 
20828 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20829 
20830 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20831 
20832 	ASSERT(IAM_WRITER_ILL(ill));
20833 	/*
20834 	 * Send an AR_INTERFACE_ON request if we have already done
20835 	 * an arp_off (which allocated the message).
20836 	 */
20837 	if (ill->ill_arp_on_mp != NULL) {
20838 		mp = ill->ill_arp_on_mp;
20839 		ill->ill_arp_on_mp = NULL;
20840 		putnext(ill->ill_rq, mp);
20841 	}
20842 	return (0);
20843 }
20844 
20845 /*
20846  * Called after either deleting ill from the group or when setting
20847  * FAILED or STANDBY on the interface.
20848  */
20849 static void
20850 illgrp_reset_schednext(ill_t *ill)
20851 {
20852 	ill_group_t *illgrp;
20853 	ill_t *save_ill;
20854 
20855 	ASSERT(IAM_WRITER_ILL(ill));
20856 	/*
20857 	 * When called from illgrp_delete, ill_group will be non-NULL.
20858 	 * But when called from ip_sioctl_flags, it could be NULL if
20859 	 * somebody is setting FAILED/INACTIVE on some interface which
20860 	 * is not part of a group.
20861 	 */
20862 	illgrp = ill->ill_group;
20863 	if (illgrp == NULL)
20864 		return;
20865 	if (illgrp->illgrp_ill_schednext != ill)
20866 		return;
20867 
20868 	illgrp->illgrp_ill_schednext = NULL;
20869 	save_ill = ill;
20870 	/*
20871 	 * Choose a good ill to be the next one for
20872 	 * outbound traffic. As the flags FAILED/STANDBY is
20873 	 * not yet marked when called from ip_sioctl_flags,
20874 	 * we check for ill separately.
20875 	 */
20876 	for (ill = illgrp->illgrp_ill; ill != NULL;
20877 	    ill = ill->ill_group_next) {
20878 		if ((ill != save_ill) &&
20879 		    !(ill->ill_phyint->phyint_flags &
20880 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20881 			illgrp->illgrp_ill_schednext = ill;
20882 			return;
20883 		}
20884 	}
20885 }
20886 
20887 /*
20888  * Given an ill, find the next ill in the group to be scheduled.
20889  * (This should be called by ip_newroute() before ire_create().)
20890  * The passed in ill may be pulled out of the group, after we have picked
20891  * up a different outgoing ill from the same group. However ire add will
20892  * atomically check this.
20893  */
20894 ill_t *
20895 illgrp_scheduler(ill_t *ill)
20896 {
20897 	ill_t *retill;
20898 	ill_group_t *illgrp;
20899 	int illcnt;
20900 	int i;
20901 	uint64_t flags;
20902 	ip_stack_t	*ipst = ill->ill_ipst;
20903 
20904 	/*
20905 	 * We don't use a lock to check for the ill_group. If this ill
20906 	 * is currently being inserted we may end up just returning this
20907 	 * ill itself. That is ok.
20908 	 */
20909 	if (ill->ill_group == NULL) {
20910 		ill_refhold(ill);
20911 		return (ill);
20912 	}
20913 
20914 	/*
20915 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20916 	 * a set of stable ills. No ill can be added or deleted or change
20917 	 * group while we hold the reader lock.
20918 	 */
20919 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20920 	if ((illgrp = ill->ill_group) == NULL) {
20921 		rw_exit(&ipst->ips_ill_g_lock);
20922 		ill_refhold(ill);
20923 		return (ill);
20924 	}
20925 
20926 	illcnt = illgrp->illgrp_ill_count;
20927 	mutex_enter(&illgrp->illgrp_lock);
20928 	retill = illgrp->illgrp_ill_schednext;
20929 
20930 	if (retill == NULL)
20931 		retill = illgrp->illgrp_ill;
20932 
20933 	/*
20934 	 * We do a circular search beginning at illgrp_ill_schednext
20935 	 * or illgrp_ill. We don't check the flags against the ill lock
20936 	 * since it can change anytime. The ire creation will be atomic
20937 	 * and will fail if the ill is FAILED or OFFLINE.
20938 	 */
20939 	for (i = 0; i < illcnt; i++) {
20940 		flags = retill->ill_phyint->phyint_flags;
20941 
20942 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20943 		    ILL_CAN_LOOKUP(retill)) {
20944 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20945 			ill_refhold(retill);
20946 			break;
20947 		}
20948 		retill = retill->ill_group_next;
20949 		if (retill == NULL)
20950 			retill = illgrp->illgrp_ill;
20951 	}
20952 	mutex_exit(&illgrp->illgrp_lock);
20953 	rw_exit(&ipst->ips_ill_g_lock);
20954 
20955 	return (i == illcnt ? NULL : retill);
20956 }
20957 
20958 /*
20959  * Checks for availbility of a usable source address (if there is one) when the
20960  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20961  * this selection is done regardless of the destination.
20962  */
20963 boolean_t
20964 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20965 {
20966 	uint_t	ifindex;
20967 	ipif_t	*ipif = NULL;
20968 	ill_t	*uill;
20969 	boolean_t isv6;
20970 	ip_stack_t	*ipst = ill->ill_ipst;
20971 
20972 	ASSERT(ill != NULL);
20973 
20974 	isv6 = ill->ill_isv6;
20975 	ifindex = ill->ill_usesrc_ifindex;
20976 	if (ifindex != 0) {
20977 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20978 		    NULL, ipst);
20979 		if (uill == NULL)
20980 			return (NULL);
20981 		mutex_enter(&uill->ill_lock);
20982 		for (ipif = uill->ill_ipif; ipif != NULL;
20983 		    ipif = ipif->ipif_next) {
20984 			if (!IPIF_CAN_LOOKUP(ipif))
20985 				continue;
20986 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20987 				continue;
20988 			if (!(ipif->ipif_flags & IPIF_UP))
20989 				continue;
20990 			if (ipif->ipif_zoneid != zoneid)
20991 				continue;
20992 			if ((isv6 &&
20993 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20994 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20995 				continue;
20996 			mutex_exit(&uill->ill_lock);
20997 			ill_refrele(uill);
20998 			return (B_TRUE);
20999 		}
21000 		mutex_exit(&uill->ill_lock);
21001 		ill_refrele(uill);
21002 	}
21003 	return (B_FALSE);
21004 }
21005 
21006 /*
21007  * Determine the best source address given a destination address and an ill.
21008  * Prefers non-deprecated over deprecated but will return a deprecated
21009  * address if there is no other choice. If there is a usable source address
21010  * on the interface pointed to by ill_usesrc_ifindex then that is given
21011  * first preference.
21012  *
21013  * Returns NULL if there is no suitable source address for the ill.
21014  * This only occurs when there is no valid source address for the ill.
21015  */
21016 ipif_t *
21017 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
21018 {
21019 	ipif_t *ipif;
21020 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
21021 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
21022 	int index = 0;
21023 	boolean_t wrapped = B_FALSE;
21024 	boolean_t same_subnet_only = B_FALSE;
21025 	boolean_t ipif_same_found, ipif_other_found;
21026 	boolean_t specific_found;
21027 	ill_t	*till, *usill = NULL;
21028 	tsol_tpc_t *src_rhtp, *dst_rhtp;
21029 	ip_stack_t	*ipst = ill->ill_ipst;
21030 
21031 	if (ill->ill_usesrc_ifindex != 0) {
21032 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
21033 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
21034 		if (usill != NULL)
21035 			ill = usill;	/* Select source from usesrc ILL */
21036 		else
21037 			return (NULL);
21038 	}
21039 
21040 	/*
21041 	 * If we're dealing with an unlabeled destination on a labeled system,
21042 	 * make sure that we ignore source addresses that are incompatible with
21043 	 * the destination's default label.  That destination's default label
21044 	 * must dominate the minimum label on the source address.
21045 	 */
21046 	dst_rhtp = NULL;
21047 	if (is_system_labeled()) {
21048 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
21049 		if (dst_rhtp == NULL)
21050 			return (NULL);
21051 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
21052 			TPC_RELE(dst_rhtp);
21053 			dst_rhtp = NULL;
21054 		}
21055 	}
21056 
21057 	/*
21058 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
21059 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
21060 	 * After selecting the right ipif, under ill_lock make sure ipif is
21061 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
21062 	 * we retry. Inside the loop we still need to check for CONDEMNED,
21063 	 * but not under a lock.
21064 	 */
21065 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21066 
21067 retry:
21068 	till = ill;
21069 	ipif_arr[0] = NULL;
21070 
21071 	if (till->ill_group != NULL)
21072 		till = till->ill_group->illgrp_ill;
21073 
21074 	/*
21075 	 * Choose one good source address from each ill across the group.
21076 	 * If possible choose a source address in the same subnet as
21077 	 * the destination address.
21078 	 *
21079 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
21080 	 * This is okay because of the following.
21081 	 *
21082 	 *    If PHYI_FAILED is set and we still have non-deprecated
21083 	 *    addresses, it means the addresses have not yet been
21084 	 *    failed over to a different interface. We potentially
21085 	 *    select them to create IRE_CACHES, which will be later
21086 	 *    flushed when the addresses move over.
21087 	 *
21088 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
21089 	 *    addresses, it means either the user has configured them
21090 	 *    or PHYI_INACTIVE has not been cleared after the addresses
21091 	 *    been moved over. For the former, in.mpathd does a failover
21092 	 *    when the interface becomes INACTIVE and hence we should
21093 	 *    not find them. Once INACTIVE is set, we don't allow them
21094 	 *    to create logical interfaces anymore. For the latter, a
21095 	 *    flush will happen when INACTIVE is cleared which will
21096 	 *    flush the IRE_CACHES.
21097 	 *
21098 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
21099 	 *    over soon. We potentially select them to create IRE_CACHEs,
21100 	 *    which will be later flushed when the addresses move over.
21101 	 *
21102 	 * NOTE : As ipif_select_source is called to borrow source address
21103 	 * for an ipif that is part of a group, source address selection
21104 	 * will be re-done whenever the group changes i.e either an
21105 	 * insertion/deletion in the group.
21106 	 *
21107 	 * Fill ipif_arr[] with source addresses, using these rules:
21108 	 *
21109 	 *	1. At most one source address from a given ill ends up
21110 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
21111 	 *	   associated with a given ill ends up in ipif_arr[].
21112 	 *
21113 	 *	2. If there is at least one non-deprecated ipif in the
21114 	 *	   IPMP group with a source address on the same subnet as
21115 	 *	   our destination, then fill ipif_arr[] only with
21116 	 *	   source addresses on the same subnet as our destination.
21117 	 *	   Note that because of (1), only the first
21118 	 *	   non-deprecated ipif found with a source address
21119 	 *	   matching the destination ends up in ipif_arr[].
21120 	 *
21121 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
21122 	 *	   addresses not in the same subnet as our destination.
21123 	 *	   Again, because of (1), only the first off-subnet source
21124 	 *	   address will be chosen.
21125 	 *
21126 	 *	4. If there are no non-deprecated ipifs, then just use
21127 	 *	   the source address associated with the last deprecated
21128 	 *	   one we find that happens to be on the same subnet,
21129 	 *	   otherwise the first one not in the same subnet.
21130 	 */
21131 	specific_found = B_FALSE;
21132 	for (; till != NULL; till = till->ill_group_next) {
21133 		ipif_same_found = B_FALSE;
21134 		ipif_other_found = B_FALSE;
21135 		for (ipif = till->ill_ipif; ipif != NULL;
21136 		    ipif = ipif->ipif_next) {
21137 			if (!IPIF_CAN_LOOKUP(ipif))
21138 				continue;
21139 			/* Always skip NOLOCAL and ANYCAST interfaces */
21140 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
21141 				continue;
21142 			if (!(ipif->ipif_flags & IPIF_UP) ||
21143 			    !ipif->ipif_addr_ready)
21144 				continue;
21145 			if (ipif->ipif_zoneid != zoneid &&
21146 			    ipif->ipif_zoneid != ALL_ZONES)
21147 				continue;
21148 			/*
21149 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
21150 			 * but are not valid as source addresses.
21151 			 */
21152 			if (ipif->ipif_lcl_addr == INADDR_ANY)
21153 				continue;
21154 
21155 			/*
21156 			 * Check compatibility of local address for
21157 			 * destination's default label if we're on a labeled
21158 			 * system.  Incompatible addresses can't be used at
21159 			 * all.
21160 			 */
21161 			if (dst_rhtp != NULL) {
21162 				boolean_t incompat;
21163 
21164 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
21165 				    IPV4_VERSION, B_FALSE);
21166 				if (src_rhtp == NULL)
21167 					continue;
21168 				incompat =
21169 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
21170 				    src_rhtp->tpc_tp.tp_doi !=
21171 				    dst_rhtp->tpc_tp.tp_doi ||
21172 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
21173 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
21174 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
21175 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
21176 				TPC_RELE(src_rhtp);
21177 				if (incompat)
21178 					continue;
21179 			}
21180 
21181 			/*
21182 			 * We prefer not to use all all-zones addresses, if we
21183 			 * can avoid it, as they pose problems with unlabeled
21184 			 * destinations.
21185 			 */
21186 			if (ipif->ipif_zoneid != ALL_ZONES) {
21187 				if (!specific_found &&
21188 				    (!same_subnet_only ||
21189 				    (ipif->ipif_net_mask & dst) ==
21190 				    ipif->ipif_subnet)) {
21191 					index = 0;
21192 					specific_found = B_TRUE;
21193 					ipif_other_found = B_FALSE;
21194 				}
21195 			} else {
21196 				if (specific_found)
21197 					continue;
21198 			}
21199 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
21200 				if (ipif_dep == NULL ||
21201 				    (ipif->ipif_net_mask & dst) ==
21202 				    ipif->ipif_subnet)
21203 					ipif_dep = ipif;
21204 				continue;
21205 			}
21206 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
21207 				/* found a source address in the same subnet */
21208 				if (!same_subnet_only) {
21209 					same_subnet_only = B_TRUE;
21210 					index = 0;
21211 				}
21212 				ipif_same_found = B_TRUE;
21213 			} else {
21214 				if (same_subnet_only || ipif_other_found)
21215 					continue;
21216 				ipif_other_found = B_TRUE;
21217 			}
21218 			ipif_arr[index++] = ipif;
21219 			if (index == MAX_IPIF_SELECT_SOURCE) {
21220 				wrapped = B_TRUE;
21221 				index = 0;
21222 			}
21223 			if (ipif_same_found)
21224 				break;
21225 		}
21226 	}
21227 
21228 	if (ipif_arr[0] == NULL) {
21229 		ipif = ipif_dep;
21230 	} else {
21231 		if (wrapped)
21232 			index = MAX_IPIF_SELECT_SOURCE;
21233 		ipif = ipif_arr[ipif_rand(ipst) % index];
21234 		ASSERT(ipif != NULL);
21235 	}
21236 
21237 	if (ipif != NULL) {
21238 		mutex_enter(&ipif->ipif_ill->ill_lock);
21239 		if (!IPIF_CAN_LOOKUP(ipif)) {
21240 			mutex_exit(&ipif->ipif_ill->ill_lock);
21241 			goto retry;
21242 		}
21243 		ipif_refhold_locked(ipif);
21244 		mutex_exit(&ipif->ipif_ill->ill_lock);
21245 	}
21246 
21247 	rw_exit(&ipst->ips_ill_g_lock);
21248 	if (usill != NULL)
21249 		ill_refrele(usill);
21250 	if (dst_rhtp != NULL)
21251 		TPC_RELE(dst_rhtp);
21252 
21253 #ifdef DEBUG
21254 	if (ipif == NULL) {
21255 		char buf1[INET6_ADDRSTRLEN];
21256 
21257 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
21258 		    ill->ill_name,
21259 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
21260 	} else {
21261 		char buf1[INET6_ADDRSTRLEN];
21262 		char buf2[INET6_ADDRSTRLEN];
21263 
21264 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
21265 		    ipif->ipif_ill->ill_name,
21266 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
21267 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
21268 		    buf2, sizeof (buf2))));
21269 	}
21270 #endif /* DEBUG */
21271 	return (ipif);
21272 }
21273 
21274 
21275 /*
21276  * If old_ipif is not NULL, see if ipif was derived from old
21277  * ipif and if so, recreate the interface route by re-doing
21278  * source address selection. This happens when ipif_down ->
21279  * ipif_update_other_ipifs calls us.
21280  *
21281  * If old_ipif is NULL, just redo the source address selection
21282  * if needed. This happens when illgrp_insert or ipif_up_done
21283  * calls us.
21284  */
21285 static void
21286 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
21287 {
21288 	ire_t *ire;
21289 	ire_t *ipif_ire;
21290 	queue_t *stq;
21291 	ipif_t *nipif;
21292 	ill_t *ill;
21293 	boolean_t need_rele = B_FALSE;
21294 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21295 
21296 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
21297 	ASSERT(IAM_WRITER_IPIF(ipif));
21298 
21299 	ill = ipif->ipif_ill;
21300 	if (!(ipif->ipif_flags &
21301 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
21302 		/*
21303 		 * Can't possibly have borrowed the source
21304 		 * from old_ipif.
21305 		 */
21306 		return;
21307 	}
21308 
21309 	/*
21310 	 * Is there any work to be done? No work if the address
21311 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21312 	 * ipif_select_source() does not borrow addresses from
21313 	 * NOLOCAL and ANYCAST interfaces).
21314 	 */
21315 	if ((old_ipif != NULL) &&
21316 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21317 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21318 	    (old_ipif->ipif_flags &
21319 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21320 		return;
21321 	}
21322 
21323 	/*
21324 	 * Perform the same checks as when creating the
21325 	 * IRE_INTERFACE in ipif_up_done.
21326 	 */
21327 	if (!(ipif->ipif_flags & IPIF_UP))
21328 		return;
21329 
21330 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21331 	    (ipif->ipif_subnet == INADDR_ANY))
21332 		return;
21333 
21334 	ipif_ire = ipif_to_ire(ipif);
21335 	if (ipif_ire == NULL)
21336 		return;
21337 
21338 	/*
21339 	 * We know that ipif uses some other source for its
21340 	 * IRE_INTERFACE. Is it using the source of this
21341 	 * old_ipif?
21342 	 */
21343 	if (old_ipif != NULL &&
21344 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21345 		ire_refrele(ipif_ire);
21346 		return;
21347 	}
21348 	if (ip_debug > 2) {
21349 		/* ip1dbg */
21350 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21351 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21352 	}
21353 
21354 	stq = ipif_ire->ire_stq;
21355 
21356 	/*
21357 	 * Can't use our source address. Select a different
21358 	 * source address for the IRE_INTERFACE.
21359 	 */
21360 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21361 	if (nipif == NULL) {
21362 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21363 		nipif = ipif;
21364 	} else {
21365 		need_rele = B_TRUE;
21366 	}
21367 
21368 	ire = ire_create(
21369 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21370 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21371 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21372 	    NULL,				/* no gateway */
21373 	    NULL,
21374 	    &ipif->ipif_mtu,			/* max frag */
21375 	    NULL,				/* no src nce */
21376 	    NULL,				/* no recv from queue */
21377 	    stq,				/* send-to queue */
21378 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21379 	    ipif,
21380 	    NULL,
21381 	    0,
21382 	    0,
21383 	    0,
21384 	    0,
21385 	    &ire_uinfo_null,
21386 	    NULL,
21387 	    NULL,
21388 	    ipst);
21389 
21390 	if (ire != NULL) {
21391 		ire_t *ret_ire;
21392 		int error;
21393 
21394 		/*
21395 		 * We don't need ipif_ire anymore. We need to delete
21396 		 * before we add so that ire_add does not detect
21397 		 * duplicates.
21398 		 */
21399 		ire_delete(ipif_ire);
21400 		ret_ire = ire;
21401 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21402 		ASSERT(error == 0);
21403 		ASSERT(ire == ret_ire);
21404 		/* Held in ire_add */
21405 		ire_refrele(ret_ire);
21406 	}
21407 	/*
21408 	 * Either we are falling through from above or could not
21409 	 * allocate a replacement.
21410 	 */
21411 	ire_refrele(ipif_ire);
21412 	if (need_rele)
21413 		ipif_refrele(nipif);
21414 }
21415 
21416 /*
21417  * This old_ipif is going away.
21418  *
21419  * Determine if any other ipif's is using our address as
21420  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21421  * IPIF_DEPRECATED).
21422  * Find the IRE_INTERFACE for such ipifs and recreate them
21423  * to use an different source address following the rules in
21424  * ipif_up_done.
21425  *
21426  * This function takes an illgrp as an argument so that illgrp_delete
21427  * can call this to update source address even after deleting the
21428  * old_ipif->ipif_ill from the ill group.
21429  */
21430 static void
21431 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21432 {
21433 	ipif_t *ipif;
21434 	ill_t *ill;
21435 	char	buf[INET6_ADDRSTRLEN];
21436 
21437 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21438 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21439 
21440 	ill = old_ipif->ipif_ill;
21441 
21442 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21443 	    ill->ill_name,
21444 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21445 	    buf, sizeof (buf))));
21446 	/*
21447 	 * If this part of a group, look at all ills as ipif_select_source
21448 	 * borrows source address across all the ills in the group.
21449 	 */
21450 	if (illgrp != NULL)
21451 		ill = illgrp->illgrp_ill;
21452 
21453 	for (; ill != NULL; ill = ill->ill_group_next) {
21454 		for (ipif = ill->ill_ipif; ipif != NULL;
21455 		    ipif = ipif->ipif_next) {
21456 
21457 			if (ipif == old_ipif)
21458 				continue;
21459 
21460 			ipif_recreate_interface_routes(old_ipif, ipif);
21461 		}
21462 	}
21463 }
21464 
21465 /* ARGSUSED */
21466 int
21467 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21468 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21469 {
21470 	/*
21471 	 * ill_phyint_reinit merged the v4 and v6 into a single
21472 	 * ipsq. Could also have become part of a ipmp group in the
21473 	 * process, and we might not have been able to complete the
21474 	 * operation in ipif_set_values, if we could not become
21475 	 * exclusive.  If so restart it here.
21476 	 */
21477 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21478 }
21479 
21480 
21481 /*
21482  * Can operate on either a module or a driver queue.
21483  * Returns an error if not a module queue.
21484  */
21485 /* ARGSUSED */
21486 int
21487 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21488     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21489 {
21490 	queue_t		*q1 = q;
21491 	char 		*cp;
21492 	char		interf_name[LIFNAMSIZ];
21493 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21494 
21495 	if (q->q_next == NULL) {
21496 		ip1dbg((
21497 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21498 		return (EINVAL);
21499 	}
21500 
21501 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21502 		return (EALREADY);
21503 
21504 	do {
21505 		q1 = q1->q_next;
21506 	} while (q1->q_next);
21507 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21508 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21509 
21510 	/*
21511 	 * Here we are not going to delay the ioack until after
21512 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21513 	 * original ioctl message before sending the requests.
21514 	 */
21515 	return (ipif_set_values(q, mp, interf_name, &ppa));
21516 }
21517 
21518 /* ARGSUSED */
21519 int
21520 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21521     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21522 {
21523 	return (ENXIO);
21524 }
21525 
21526 /*
21527  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21528  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21529  * ire_check_and_create_bcast()).
21530  */
21531 static ire_t **
21532 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21533 {
21534 	ipaddr_t addr;
21535 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21536 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21537 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21538 
21539 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21540 
21541 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21542 
21543 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21544 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21545 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21546 
21547 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21548 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21549 
21550 	/*
21551 	 * For backward compatibility, we create net broadcast IREs based on
21552 	 * the old "IP address class system", since some old machines only
21553 	 * respond to these class derived net broadcast.  However, we must not
21554 	 * create these net broadcast IREs if the subnetmask is shorter than
21555 	 * the IP address class based derived netmask.  Otherwise, we may
21556 	 * create a net broadcast address which is the same as an IP address
21557 	 * on the subnet -- and then TCP will refuse to talk to that address.
21558 	 */
21559 	if (netmask < subnetmask) {
21560 		addr = netmask & ipif->ipif_subnet;
21561 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21562 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21563 		    flags);
21564 	}
21565 
21566 	/*
21567 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21568 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21569 	 * created.  Creating these broadcast IREs will only create confusion
21570 	 * as `addr' will be the same as the IP address.
21571 	 */
21572 	if (subnetmask != 0xFFFFFFFF) {
21573 		addr = ipif->ipif_subnet;
21574 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21575 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21576 		    irep, flags);
21577 	}
21578 
21579 	return (irep);
21580 }
21581 
21582 /*
21583  * Broadcast IRE info structure used in the functions below.  Since we
21584  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21585  */
21586 typedef struct bcast_ireinfo {
21587 	uchar_t		bi_type;	/* BCAST_* value from below */
21588 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21589 			bi_needrep:1,	/* do we need to replace it? */
21590 			bi_haverep:1,	/* have we replaced it? */
21591 			bi_pad:5;
21592 	ipaddr_t	bi_addr;	/* IRE address */
21593 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21594 } bcast_ireinfo_t;
21595 
21596 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21597 
21598 /*
21599  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21600  * return B_TRUE if it should immediately be used to recreate the IRE.
21601  */
21602 static boolean_t
21603 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21604 {
21605 	ipaddr_t addr;
21606 
21607 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21608 
21609 	switch (bireinfop->bi_type) {
21610 	case BCAST_NET:
21611 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21612 		if (addr != bireinfop->bi_addr)
21613 			return (B_FALSE);
21614 		break;
21615 	case BCAST_SUBNET:
21616 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21617 			return (B_FALSE);
21618 		break;
21619 	}
21620 
21621 	bireinfop->bi_needrep = 1;
21622 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21623 		if (bireinfop->bi_backup == NULL)
21624 			bireinfop->bi_backup = ipif;
21625 		return (B_FALSE);
21626 	}
21627 	return (B_TRUE);
21628 }
21629 
21630 /*
21631  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21632  * them ala ire_check_and_create_bcast().
21633  */
21634 static ire_t **
21635 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21636 {
21637 	ipaddr_t mask, addr;
21638 
21639 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21640 
21641 	addr = bireinfop->bi_addr;
21642 	irep = ire_create_bcast(ipif, addr, irep);
21643 
21644 	switch (bireinfop->bi_type) {
21645 	case BCAST_NET:
21646 		mask = ip_net_mask(ipif->ipif_subnet);
21647 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21648 		break;
21649 	case BCAST_SUBNET:
21650 		mask = ipif->ipif_net_mask;
21651 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21652 		break;
21653 	}
21654 
21655 	bireinfop->bi_haverep = 1;
21656 	return (irep);
21657 }
21658 
21659 /*
21660  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21661  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21662  * that are going away are still needed.  If so, have ipif_create_bcast()
21663  * recreate them (except for the deprecated case, as explained below).
21664  */
21665 static ire_t **
21666 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21667     ire_t **irep)
21668 {
21669 	int i;
21670 	ipif_t *ipif;
21671 
21672 	ASSERT(!ill->ill_isv6);
21673 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21674 		/*
21675 		 * Skip this ipif if it's (a) the one being taken down, (b)
21676 		 * not in the same zone, or (c) has no valid local address.
21677 		 */
21678 		if (ipif == test_ipif ||
21679 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21680 		    ipif->ipif_subnet == 0 ||
21681 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21682 		    (IPIF_UP|IPIF_BROADCAST))
21683 			continue;
21684 
21685 		/*
21686 		 * For each dying IRE that hasn't yet been replaced, see if
21687 		 * `ipif' needs it and whether the IRE should be recreated on
21688 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21689 		 * will return B_FALSE even if `ipif' needs the IRE on the
21690 		 * hopes that we'll later find a needy non-deprecated ipif.
21691 		 * However, the ipif is recorded in bi_backup for possible
21692 		 * subsequent use by ipif_check_bcast_ires().
21693 		 */
21694 		for (i = 0; i < BCAST_COUNT; i++) {
21695 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21696 				continue;
21697 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21698 				continue;
21699 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21700 		}
21701 
21702 		/*
21703 		 * If we've replaced all of the broadcast IREs that are going
21704 		 * to be taken down, we know we're done.
21705 		 */
21706 		for (i = 0; i < BCAST_COUNT; i++) {
21707 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21708 				break;
21709 		}
21710 		if (i == BCAST_COUNT)
21711 			break;
21712 	}
21713 	return (irep);
21714 }
21715 
21716 /*
21717  * Check if `test_ipif' (which is going away) is associated with any existing
21718  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21719  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21720  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21721  *
21722  * This is necessary because broadcast IREs are shared.  In particular, a
21723  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21724  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21725  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21726  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21727  * same zone, they will share the same set of broadcast IREs.
21728  *
21729  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21730  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21731  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21732  */
21733 static void
21734 ipif_check_bcast_ires(ipif_t *test_ipif)
21735 {
21736 	ill_t		*ill = test_ipif->ipif_ill;
21737 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21738 	ire_t		**irep1, **irep = &ire_array[0];
21739 	uint_t 		i, willdie;
21740 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21741 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21742 
21743 	ASSERT(!test_ipif->ipif_isv6);
21744 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21745 
21746 	/*
21747 	 * No broadcast IREs for the LOOPBACK interface
21748 	 * or others such as point to point and IPIF_NOXMIT.
21749 	 */
21750 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21751 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21752 		return;
21753 
21754 	bzero(bireinfo, sizeof (bireinfo));
21755 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21756 	bireinfo[0].bi_addr = 0;
21757 
21758 	bireinfo[1].bi_type = BCAST_ALLONES;
21759 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21760 
21761 	bireinfo[2].bi_type = BCAST_NET;
21762 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21763 
21764 	if (test_ipif->ipif_net_mask != 0)
21765 		mask = test_ipif->ipif_net_mask;
21766 	bireinfo[3].bi_type = BCAST_SUBNET;
21767 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21768 
21769 	/*
21770 	 * Figure out what (if any) broadcast IREs will die as a result of
21771 	 * `test_ipif' going away.  If none will die, we're done.
21772 	 */
21773 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21774 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21775 		    test_ipif, ALL_ZONES, NULL,
21776 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21777 		if (ire != NULL) {
21778 			willdie++;
21779 			bireinfo[i].bi_willdie = 1;
21780 			ire_refrele(ire);
21781 		}
21782 	}
21783 
21784 	if (willdie == 0)
21785 		return;
21786 
21787 	/*
21788 	 * Walk through all the ipifs that will be affected by the dying IREs,
21789 	 * and recreate the IREs as necessary.
21790 	 */
21791 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21792 
21793 	/*
21794 	 * Scan through the set of broadcast IREs and see if there are any
21795 	 * that we need to replace that have not yet been replaced.  If so,
21796 	 * replace them using the appropriate backup ipif.
21797 	 */
21798 	for (i = 0; i < BCAST_COUNT; i++) {
21799 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21800 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21801 			    &bireinfo[i], irep);
21802 	}
21803 
21804 	/*
21805 	 * If we can't create all of them, don't add any of them.  (Code in
21806 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21807 	 * non-loopback copy and loopback copy for a given address.)
21808 	 */
21809 	for (irep1 = irep; irep1 > ire_array; ) {
21810 		irep1--;
21811 		if (*irep1 == NULL) {
21812 			ip0dbg(("ipif_check_bcast_ires: can't create "
21813 			    "IRE_BROADCAST, memory allocation failure\n"));
21814 			while (irep > ire_array) {
21815 				irep--;
21816 				if (*irep != NULL)
21817 					ire_delete(*irep);
21818 			}
21819 			return;
21820 		}
21821 	}
21822 
21823 	for (irep1 = irep; irep1 > ire_array; ) {
21824 		irep1--;
21825 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21826 			ire_refrele(*irep1);		/* Held in ire_add */
21827 	}
21828 }
21829 
21830 /*
21831  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21832  * from lifr_flags and the name from lifr_name.
21833  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21834  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21835  * Returns EINPROGRESS when mp has been consumed by queueing it on
21836  * ill_pending_mp and the ioctl will complete in ip_rput.
21837  *
21838  * Can operate on either a module or a driver queue.
21839  * Returns an error if not a module queue.
21840  */
21841 /* ARGSUSED */
21842 int
21843 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21844     ip_ioctl_cmd_t *ipip, void *if_req)
21845 {
21846 	int	err;
21847 	ill_t	*ill;
21848 	struct lifreq *lifr = (struct lifreq *)if_req;
21849 
21850 	ASSERT(ipif != NULL);
21851 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21852 
21853 	if (q->q_next == NULL) {
21854 		ip1dbg((
21855 		    "if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21856 		return (EINVAL);
21857 	}
21858 
21859 	ill = (ill_t *)q->q_ptr;
21860 	/*
21861 	 * If we are not writer on 'q' then this interface exists already
21862 	 * and previous lookups (ipif_extract_lifreq_cmn) found this ipif.
21863 	 * So return EALREADY
21864 	 */
21865 	if (ill != ipif->ipif_ill)
21866 		return (EALREADY);
21867 
21868 	if (ill->ill_name[0] != '\0')
21869 		return (EALREADY);
21870 
21871 	/*
21872 	 * Set all the flags. Allows all kinds of override. Provide some
21873 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21874 	 * unless there is either multicast/broadcast support in the driver
21875 	 * or it is a pt-pt link.
21876 	 */
21877 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21878 		/* Meaningless to IP thus don't allow them to be set. */
21879 		ip1dbg(("ip_setname: EINVAL 1\n"));
21880 		return (EINVAL);
21881 	}
21882 	/*
21883 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21884 	 * ill_bcast_addr_length info.
21885 	 */
21886 	if (!ill->ill_needs_attach &&
21887 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21888 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21889 	    ill->ill_bcast_addr_length == 0)) {
21890 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21891 		ip1dbg(("ip_setname: EINVAL 2\n"));
21892 		return (EINVAL);
21893 	}
21894 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21895 	    ((lifr->lifr_flags & IFF_IPV6) ||
21896 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21897 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21898 		ip1dbg(("ip_setname: EINVAL 3\n"));
21899 		return (EINVAL);
21900 	}
21901 	if (lifr->lifr_flags & IFF_UP) {
21902 		/* Can only be set with SIOCSLIFFLAGS */
21903 		ip1dbg(("ip_setname: EINVAL 4\n"));
21904 		return (EINVAL);
21905 	}
21906 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21907 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21908 		ip1dbg(("ip_setname: EINVAL 5\n"));
21909 		return (EINVAL);
21910 	}
21911 	/*
21912 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21913 	 */
21914 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21915 	    !(lifr->lifr_flags & IFF_IPV6) &&
21916 	    !(ipif->ipif_isv6)) {
21917 		ip1dbg(("ip_setname: EINVAL 6\n"));
21918 		return (EINVAL);
21919 	}
21920 
21921 	/*
21922 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21923 	 * we have all the flags here. So, we assign rather than we OR.
21924 	 * We can't OR the flags here because we don't want to set
21925 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21926 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21927 	 * on lifr_flags value here.
21928 	 */
21929 	/*
21930 	 * This ill has not been inserted into the global list.
21931 	 * So we are still single threaded and don't need any lock
21932 	 */
21933 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS &
21934 	    ~IFF_DUPLICATE;
21935 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21936 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21937 
21938 	/* We started off as V4. */
21939 	if (ill->ill_flags & ILLF_IPV6) {
21940 		ill->ill_phyint->phyint_illv6 = ill;
21941 		ill->ill_phyint->phyint_illv4 = NULL;
21942 	}
21943 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
21944 	return (err);
21945 }
21946 
21947 /* ARGSUSED */
21948 int
21949 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21950     ip_ioctl_cmd_t *ipip, void *if_req)
21951 {
21952 	/*
21953 	 * ill_phyint_reinit merged the v4 and v6 into a single
21954 	 * ipsq. Could also have become part of a ipmp group in the
21955 	 * process, and we might not have been able to complete the
21956 	 * slifname in ipif_set_values, if we could not become
21957 	 * exclusive.  If so restart it here
21958 	 */
21959 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21960 }
21961 
21962 /*
21963  * Return a pointer to the ipif which matches the index, IP version type and
21964  * zoneid.
21965  */
21966 ipif_t *
21967 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21968     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21969 {
21970 	ill_t	*ill;
21971 	ipsq_t  *ipsq;
21972 	phyint_t *phyi;
21973 	ipif_t	*ipif;
21974 
21975 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21976 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21977 
21978 	if (err != NULL)
21979 		*err = 0;
21980 
21981 	/*
21982 	 * Indexes are stored in the phyint - a common structure
21983 	 * to both IPv4 and IPv6.
21984 	 */
21985 
21986 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21987 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
21988 	    (void *) &index, NULL);
21989 	if (phyi != NULL) {
21990 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
21991 		if (ill == NULL) {
21992 			rw_exit(&ipst->ips_ill_g_lock);
21993 			if (err != NULL)
21994 				*err = ENXIO;
21995 			return (NULL);
21996 		}
21997 		GRAB_CONN_LOCK(q);
21998 		mutex_enter(&ill->ill_lock);
21999 		if (ILL_CAN_LOOKUP(ill)) {
22000 			for (ipif = ill->ill_ipif; ipif != NULL;
22001 			    ipif = ipif->ipif_next) {
22002 				if (IPIF_CAN_LOOKUP(ipif) &&
22003 				    (zoneid == ALL_ZONES ||
22004 				    zoneid == ipif->ipif_zoneid ||
22005 				    ipif->ipif_zoneid == ALL_ZONES)) {
22006 					ipif_refhold_locked(ipif);
22007 					mutex_exit(&ill->ill_lock);
22008 					RELEASE_CONN_LOCK(q);
22009 					rw_exit(&ipst->ips_ill_g_lock);
22010 					return (ipif);
22011 				}
22012 			}
22013 		} else if (ILL_CAN_WAIT(ill, q)) {
22014 			ipsq = ill->ill_phyint->phyint_ipsq;
22015 			mutex_enter(&ipsq->ipsq_lock);
22016 			rw_exit(&ipst->ips_ill_g_lock);
22017 			mutex_exit(&ill->ill_lock);
22018 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
22019 			mutex_exit(&ipsq->ipsq_lock);
22020 			RELEASE_CONN_LOCK(q);
22021 			*err = EINPROGRESS;
22022 			return (NULL);
22023 		}
22024 		mutex_exit(&ill->ill_lock);
22025 		RELEASE_CONN_LOCK(q);
22026 	}
22027 	rw_exit(&ipst->ips_ill_g_lock);
22028 	if (err != NULL)
22029 		*err = ENXIO;
22030 	return (NULL);
22031 }
22032 
22033 typedef struct conn_change_s {
22034 	uint_t cc_old_ifindex;
22035 	uint_t cc_new_ifindex;
22036 } conn_change_t;
22037 
22038 /*
22039  * ipcl_walk function for changing interface index.
22040  */
22041 static void
22042 conn_change_ifindex(conn_t *connp, caddr_t arg)
22043 {
22044 	conn_change_t *connc;
22045 	uint_t old_ifindex;
22046 	uint_t new_ifindex;
22047 	int i;
22048 	ilg_t *ilg;
22049 
22050 	connc = (conn_change_t *)arg;
22051 	old_ifindex = connc->cc_old_ifindex;
22052 	new_ifindex = connc->cc_new_ifindex;
22053 
22054 	if (connp->conn_orig_bound_ifindex == old_ifindex)
22055 		connp->conn_orig_bound_ifindex = new_ifindex;
22056 
22057 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
22058 		connp->conn_orig_multicast_ifindex = new_ifindex;
22059 
22060 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
22061 		connp->conn_orig_xmit_ifindex = new_ifindex;
22062 
22063 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
22064 		ilg = &connp->conn_ilg[i];
22065 		if (ilg->ilg_orig_ifindex == old_ifindex)
22066 			ilg->ilg_orig_ifindex = new_ifindex;
22067 	}
22068 }
22069 
22070 /*
22071  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
22072  * to new_index if it matches the old_index.
22073  *
22074  * Failovers typically happen within a group of ills. But somebody
22075  * can remove an ill from the group after a failover happened. If
22076  * we are setting the ifindex after this, we potentially need to
22077  * look at all the ills rather than just the ones in the group.
22078  * We cut down the work by looking at matching ill_net_types
22079  * and ill_types as we could not possibly grouped them together.
22080  */
22081 static void
22082 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
22083 {
22084 	ill_t *ill;
22085 	ipif_t *ipif;
22086 	uint_t old_ifindex;
22087 	uint_t new_ifindex;
22088 	ilm_t *ilm;
22089 	ill_walk_context_t ctx;
22090 	ip_stack_t	*ipst = ill_orig->ill_ipst;
22091 
22092 	old_ifindex = connc->cc_old_ifindex;
22093 	new_ifindex = connc->cc_new_ifindex;
22094 
22095 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
22096 	ill = ILL_START_WALK_ALL(&ctx, ipst);
22097 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
22098 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
22099 		    (ill_orig->ill_type != ill->ill_type)) {
22100 			continue;
22101 		}
22102 		for (ipif = ill->ill_ipif; ipif != NULL;
22103 		    ipif = ipif->ipif_next) {
22104 			if (ipif->ipif_orig_ifindex == old_ifindex)
22105 				ipif->ipif_orig_ifindex = new_ifindex;
22106 		}
22107 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
22108 			if (ilm->ilm_orig_ifindex == old_ifindex)
22109 				ilm->ilm_orig_ifindex = new_ifindex;
22110 		}
22111 	}
22112 	rw_exit(&ipst->ips_ill_g_lock);
22113 }
22114 
22115 /*
22116  * We first need to ensure that the new index is unique, and
22117  * then carry the change across both v4 and v6 ill representation
22118  * of the physical interface.
22119  */
22120 /* ARGSUSED */
22121 int
22122 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22123     ip_ioctl_cmd_t *ipip, void *ifreq)
22124 {
22125 	ill_t		*ill;
22126 	ill_t		*ill_other;
22127 	phyint_t	*phyi;
22128 	int		old_index;
22129 	conn_change_t	connc;
22130 	struct ifreq	*ifr = (struct ifreq *)ifreq;
22131 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22132 	uint_t	index;
22133 	ill_t	*ill_v4;
22134 	ill_t	*ill_v6;
22135 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22136 
22137 	if (ipip->ipi_cmd_type == IF_CMD)
22138 		index = ifr->ifr_index;
22139 	else
22140 		index = lifr->lifr_index;
22141 
22142 	/*
22143 	 * Only allow on physical interface. Also, index zero is illegal.
22144 	 *
22145 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
22146 	 *
22147 	 * 1) If PHYI_FAILED is set, a failover could have happened which
22148 	 *    implies a possible failback might have to happen. As failback
22149 	 *    depends on the old index, we should fail setting the index.
22150 	 *
22151 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
22152 	 *    any addresses or multicast memberships are failed over to
22153 	 *    a non-STANDBY interface. As failback depends on the old
22154 	 *    index, we should fail setting the index for this case also.
22155 	 *
22156 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
22157 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
22158 	 */
22159 	ill = ipif->ipif_ill;
22160 	phyi = ill->ill_phyint;
22161 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
22162 	    ipif->ipif_id != 0 || index == 0) {
22163 		return (EINVAL);
22164 	}
22165 	old_index = phyi->phyint_ifindex;
22166 
22167 	/* If the index is not changing, no work to do */
22168 	if (old_index == index)
22169 		return (0);
22170 
22171 	/*
22172 	 * Use ill_lookup_on_ifindex to determine if the
22173 	 * new index is unused and if so allow the change.
22174 	 */
22175 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
22176 	    ipst);
22177 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
22178 	    ipst);
22179 	if (ill_v6 != NULL || ill_v4 != NULL) {
22180 		if (ill_v4 != NULL)
22181 			ill_refrele(ill_v4);
22182 		if (ill_v6 != NULL)
22183 			ill_refrele(ill_v6);
22184 		return (EBUSY);
22185 	}
22186 
22187 	/*
22188 	 * The new index is unused. Set it in the phyint.
22189 	 * Locate the other ill so that we can send a routing
22190 	 * sockets message.
22191 	 */
22192 	if (ill->ill_isv6) {
22193 		ill_other = phyi->phyint_illv4;
22194 	} else {
22195 		ill_other = phyi->phyint_illv6;
22196 	}
22197 
22198 	phyi->phyint_ifindex = index;
22199 
22200 	/* Update SCTP's ILL list */
22201 	sctp_ill_reindex(ill, old_index);
22202 
22203 	connc.cc_old_ifindex = old_index;
22204 	connc.cc_new_ifindex = index;
22205 	ip_change_ifindex(ill, &connc);
22206 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
22207 
22208 	/* Send the routing sockets message */
22209 	ip_rts_ifmsg(ipif);
22210 	if (ill_other != NULL)
22211 		ip_rts_ifmsg(ill_other->ill_ipif);
22212 
22213 	return (0);
22214 }
22215 
22216 /* ARGSUSED */
22217 int
22218 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22219     ip_ioctl_cmd_t *ipip, void *ifreq)
22220 {
22221 	struct ifreq	*ifr = (struct ifreq *)ifreq;
22222 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22223 
22224 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
22225 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22226 	/* Get the interface index */
22227 	if (ipip->ipi_cmd_type == IF_CMD) {
22228 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
22229 	} else {
22230 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
22231 	}
22232 	return (0);
22233 }
22234 
22235 /* ARGSUSED */
22236 int
22237 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22238     ip_ioctl_cmd_t *ipip, void *ifreq)
22239 {
22240 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22241 
22242 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
22243 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22244 	/* Get the interface zone */
22245 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22246 	lifr->lifr_zoneid = ipif->ipif_zoneid;
22247 	return (0);
22248 }
22249 
22250 /*
22251  * Set the zoneid of an interface.
22252  */
22253 /* ARGSUSED */
22254 int
22255 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22256     ip_ioctl_cmd_t *ipip, void *ifreq)
22257 {
22258 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22259 	int err = 0;
22260 	boolean_t need_up = B_FALSE;
22261 	zone_t *zptr;
22262 	zone_status_t status;
22263 	zoneid_t zoneid;
22264 
22265 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22266 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
22267 		if (!is_system_labeled())
22268 			return (ENOTSUP);
22269 		zoneid = GLOBAL_ZONEID;
22270 	}
22271 
22272 	/* cannot assign instance zero to a non-global zone */
22273 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
22274 		return (ENOTSUP);
22275 
22276 	/*
22277 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
22278 	 * the event of a race with the zone shutdown processing, since IP
22279 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
22280 	 * interface will be cleaned up even if the zone is shut down
22281 	 * immediately after the status check. If the interface can't be brought
22282 	 * down right away, and the zone is shut down before the restart
22283 	 * function is called, we resolve the possible races by rechecking the
22284 	 * zone status in the restart function.
22285 	 */
22286 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
22287 		return (EINVAL);
22288 	status = zone_status_get(zptr);
22289 	zone_rele(zptr);
22290 
22291 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
22292 		return (EINVAL);
22293 
22294 	if (ipif->ipif_flags & IPIF_UP) {
22295 		/*
22296 		 * If the interface is already marked up,
22297 		 * we call ipif_down which will take care
22298 		 * of ditching any IREs that have been set
22299 		 * up based on the old interface address.
22300 		 */
22301 		err = ipif_logical_down(ipif, q, mp);
22302 		if (err == EINPROGRESS)
22303 			return (err);
22304 		ipif_down_tail(ipif);
22305 		need_up = B_TRUE;
22306 	}
22307 
22308 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
22309 	return (err);
22310 }
22311 
22312 static int
22313 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
22314     queue_t *q, mblk_t *mp, boolean_t need_up)
22315 {
22316 	int	err = 0;
22317 	ip_stack_t	*ipst;
22318 
22319 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
22320 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22321 
22322 	if (CONN_Q(q))
22323 		ipst = CONNQ_TO_IPST(q);
22324 	else
22325 		ipst = ILLQ_TO_IPST(q);
22326 
22327 	/*
22328 	 * For exclusive stacks we don't allow a different zoneid than
22329 	 * global.
22330 	 */
22331 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
22332 	    zoneid != GLOBAL_ZONEID)
22333 		return (EINVAL);
22334 
22335 	/* Set the new zone id. */
22336 	ipif->ipif_zoneid = zoneid;
22337 
22338 	/* Update sctp list */
22339 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22340 
22341 	if (need_up) {
22342 		/*
22343 		 * Now bring the interface back up.  If this
22344 		 * is the only IPIF for the ILL, ipif_up
22345 		 * will have to re-bind to the device, so
22346 		 * we may get back EINPROGRESS, in which
22347 		 * case, this IOCTL will get completed in
22348 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22349 		 */
22350 		err = ipif_up(ipif, q, mp);
22351 	}
22352 	return (err);
22353 }
22354 
22355 /* ARGSUSED */
22356 int
22357 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22358     ip_ioctl_cmd_t *ipip, void *if_req)
22359 {
22360 	struct lifreq *lifr = (struct lifreq *)if_req;
22361 	zoneid_t zoneid;
22362 	zone_t *zptr;
22363 	zone_status_t status;
22364 
22365 	ASSERT(ipif->ipif_id != 0);
22366 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22367 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22368 		zoneid = GLOBAL_ZONEID;
22369 
22370 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22371 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22372 
22373 	/*
22374 	 * We recheck the zone status to resolve the following race condition:
22375 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22376 	 * 2) hme0:1 is up and can't be brought down right away;
22377 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22378 	 * 3) zone "myzone" is halted; the zone status switches to
22379 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22380 	 * the interfaces to remove - hme0:1 is not returned because it's not
22381 	 * yet in "myzone", so it won't be removed;
22382 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22383 	 * status check here, we would have hme0:1 in "myzone" after it's been
22384 	 * destroyed.
22385 	 * Note that if the status check fails, we need to bring the interface
22386 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22387 	 * ipif_up_done[_v6]().
22388 	 */
22389 	status = ZONE_IS_UNINITIALIZED;
22390 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22391 		status = zone_status_get(zptr);
22392 		zone_rele(zptr);
22393 	}
22394 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22395 		if (ipif->ipif_isv6) {
22396 			(void) ipif_up_done_v6(ipif);
22397 		} else {
22398 			(void) ipif_up_done(ipif);
22399 		}
22400 		return (EINVAL);
22401 	}
22402 
22403 	ipif_down_tail(ipif);
22404 
22405 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22406 	    B_TRUE));
22407 }
22408 
22409 /* ARGSUSED */
22410 int
22411 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22412 	ip_ioctl_cmd_t *ipip, void *ifreq)
22413 {
22414 	struct lifreq	*lifr = ifreq;
22415 
22416 	ASSERT(q->q_next == NULL);
22417 	ASSERT(CONN_Q(q));
22418 
22419 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22420 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22421 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22422 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22423 
22424 	return (0);
22425 }
22426 
22427 
22428 /* Find the previous ILL in this usesrc group */
22429 static ill_t *
22430 ill_prev_usesrc(ill_t *uill)
22431 {
22432 	ill_t *ill;
22433 
22434 	for (ill = uill->ill_usesrc_grp_next;
22435 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22436 	    ill = ill->ill_usesrc_grp_next)
22437 		/* do nothing */;
22438 	return (ill);
22439 }
22440 
22441 /*
22442  * Release all members of the usesrc group. This routine is called
22443  * from ill_delete when the interface being unplumbed is the
22444  * group head.
22445  */
22446 static void
22447 ill_disband_usesrc_group(ill_t *uill)
22448 {
22449 	ill_t *next_ill, *tmp_ill;
22450 	ip_stack_t	*ipst = uill->ill_ipst;
22451 
22452 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22453 	next_ill = uill->ill_usesrc_grp_next;
22454 
22455 	do {
22456 		ASSERT(next_ill != NULL);
22457 		tmp_ill = next_ill->ill_usesrc_grp_next;
22458 		ASSERT(tmp_ill != NULL);
22459 		next_ill->ill_usesrc_grp_next = NULL;
22460 		next_ill->ill_usesrc_ifindex = 0;
22461 		next_ill = tmp_ill;
22462 	} while (next_ill->ill_usesrc_ifindex != 0);
22463 	uill->ill_usesrc_grp_next = NULL;
22464 }
22465 
22466 /*
22467  * Remove the client usesrc ILL from the list and relink to a new list
22468  */
22469 int
22470 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22471 {
22472 	ill_t *ill, *tmp_ill;
22473 	ip_stack_t	*ipst = ucill->ill_ipst;
22474 
22475 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22476 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22477 
22478 	/*
22479 	 * Check if the usesrc client ILL passed in is not already
22480 	 * in use as a usesrc ILL i.e one whose source address is
22481 	 * in use OR a usesrc ILL is not already in use as a usesrc
22482 	 * client ILL
22483 	 */
22484 	if ((ucill->ill_usesrc_ifindex == 0) ||
22485 	    (uill->ill_usesrc_ifindex != 0)) {
22486 		return (-1);
22487 	}
22488 
22489 	ill = ill_prev_usesrc(ucill);
22490 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22491 
22492 	/* Remove from the current list */
22493 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22494 		/* Only two elements in the list */
22495 		ASSERT(ill->ill_usesrc_ifindex == 0);
22496 		ill->ill_usesrc_grp_next = NULL;
22497 	} else {
22498 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22499 	}
22500 
22501 	if (ifindex == 0) {
22502 		ucill->ill_usesrc_ifindex = 0;
22503 		ucill->ill_usesrc_grp_next = NULL;
22504 		return (0);
22505 	}
22506 
22507 	ucill->ill_usesrc_ifindex = ifindex;
22508 	tmp_ill = uill->ill_usesrc_grp_next;
22509 	uill->ill_usesrc_grp_next = ucill;
22510 	ucill->ill_usesrc_grp_next =
22511 	    (tmp_ill != NULL) ? tmp_ill : uill;
22512 	return (0);
22513 }
22514 
22515 /*
22516  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22517  * ip.c for locking details.
22518  */
22519 /* ARGSUSED */
22520 int
22521 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22522     ip_ioctl_cmd_t *ipip, void *ifreq)
22523 {
22524 	struct lifreq *lifr = (struct lifreq *)ifreq;
22525 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22526 	    ill_flag_changed = B_FALSE;
22527 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22528 	int err = 0, ret;
22529 	uint_t ifindex;
22530 	phyint_t *us_phyint, *us_cli_phyint;
22531 	ipsq_t *ipsq = NULL;
22532 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22533 
22534 	ASSERT(IAM_WRITER_IPIF(ipif));
22535 	ASSERT(q->q_next == NULL);
22536 	ASSERT(CONN_Q(q));
22537 
22538 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22539 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22540 
22541 	ASSERT(us_cli_phyint != NULL);
22542 
22543 	/*
22544 	 * If the client ILL is being used for IPMP, abort.
22545 	 * Note, this can be done before ipsq_try_enter since we are already
22546 	 * exclusive on this ILL
22547 	 */
22548 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22549 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22550 		return (EINVAL);
22551 	}
22552 
22553 	ifindex = lifr->lifr_index;
22554 	if (ifindex == 0) {
22555 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22556 			/* non usesrc group interface, nothing to reset */
22557 			return (0);
22558 		}
22559 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22560 		/* valid reset request */
22561 		reset_flg = B_TRUE;
22562 	}
22563 
22564 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22565 	    ip_process_ioctl, &err, ipst);
22566 
22567 	if (usesrc_ill == NULL) {
22568 		return (err);
22569 	}
22570 
22571 	/*
22572 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22573 	 * group nor can either of the interfaces be used for standy. So
22574 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22575 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22576 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22577 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22578 	 * the usesrc_cli_ill
22579 	 */
22580 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22581 	    NEW_OP, B_TRUE);
22582 	if (ipsq == NULL) {
22583 		err = EINPROGRESS;
22584 		/* Operation enqueued on the ipsq of the usesrc ILL */
22585 		goto done;
22586 	}
22587 
22588 	/* Check if the usesrc_ill is used for IPMP */
22589 	us_phyint = usesrc_ill->ill_phyint;
22590 	if ((us_phyint->phyint_groupname != NULL) ||
22591 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22592 		err = EINVAL;
22593 		goto done;
22594 	}
22595 
22596 	/*
22597 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22598 	 * already a client then return EINVAL
22599 	 */
22600 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22601 		err = EINVAL;
22602 		goto done;
22603 	}
22604 
22605 	/*
22606 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22607 	 * be then this is a duplicate operation.
22608 	 */
22609 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22610 		err = 0;
22611 		goto done;
22612 	}
22613 
22614 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22615 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22616 	    usesrc_ill->ill_isv6));
22617 
22618 	/*
22619 	 * The next step ensures that no new ires will be created referencing
22620 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22621 	 * we go through an ire walk deleting all ire caches that reference
22622 	 * the client ill. New ires referencing the client ill that are added
22623 	 * to the ire table before the ILL_CHANGING flag is set, will be
22624 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22625 	 * the client ill while the ILL_CHANGING flag is set will be failed
22626 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22627 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22628 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22629 	 * belong to the same usesrc group.
22630 	 */
22631 	mutex_enter(&usesrc_cli_ill->ill_lock);
22632 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22633 	mutex_exit(&usesrc_cli_ill->ill_lock);
22634 	ill_flag_changed = B_TRUE;
22635 
22636 	if (ipif->ipif_isv6)
22637 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22638 		    ALL_ZONES, ipst);
22639 	else
22640 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22641 		    ALL_ZONES, ipst);
22642 
22643 	/*
22644 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22645 	 * and the ill_usesrc_ifindex fields
22646 	 */
22647 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22648 
22649 	if (reset_flg) {
22650 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22651 		if (ret != 0) {
22652 			err = EINVAL;
22653 		}
22654 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22655 		goto done;
22656 	}
22657 
22658 	/*
22659 	 * Four possibilities to consider:
22660 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22661 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22662 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22663 	 * 4. Both are part of their respective usesrc groups
22664 	 */
22665 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22666 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22667 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22668 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22669 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22670 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22671 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22672 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22673 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22674 		/* Insert at head of list */
22675 		usesrc_cli_ill->ill_usesrc_grp_next =
22676 		    usesrc_ill->ill_usesrc_grp_next;
22677 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22678 	} else {
22679 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22680 		    ifindex);
22681 		if (ret != 0)
22682 			err = EINVAL;
22683 	}
22684 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22685 
22686 done:
22687 	if (ill_flag_changed) {
22688 		mutex_enter(&usesrc_cli_ill->ill_lock);
22689 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22690 		mutex_exit(&usesrc_cli_ill->ill_lock);
22691 	}
22692 	if (ipsq != NULL)
22693 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
22694 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22695 	ill_refrele(usesrc_ill);
22696 	return (err);
22697 }
22698 
22699 /*
22700  * comparison function used by avl.
22701  */
22702 static int
22703 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22704 {
22705 
22706 	uint_t index;
22707 
22708 	ASSERT(phyip != NULL && index_ptr != NULL);
22709 
22710 	index = *((uint_t *)index_ptr);
22711 	/*
22712 	 * let the phyint with the lowest index be on top.
22713 	 */
22714 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22715 		return (1);
22716 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22717 		return (-1);
22718 	return (0);
22719 }
22720 
22721 /*
22722  * comparison function used by avl.
22723  */
22724 static int
22725 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22726 {
22727 	ill_t *ill;
22728 	int res = 0;
22729 
22730 	ASSERT(phyip != NULL && name_ptr != NULL);
22731 
22732 	if (((phyint_t *)phyip)->phyint_illv4)
22733 		ill = ((phyint_t *)phyip)->phyint_illv4;
22734 	else
22735 		ill = ((phyint_t *)phyip)->phyint_illv6;
22736 	ASSERT(ill != NULL);
22737 
22738 	res = strcmp(ill->ill_name, (char *)name_ptr);
22739 	if (res > 0)
22740 		return (1);
22741 	else if (res < 0)
22742 		return (-1);
22743 	return (0);
22744 }
22745 /*
22746  * This function is called from ill_delete when the ill is being
22747  * unplumbed. We remove the reference from the phyint and we also
22748  * free the phyint when there are no more references to it.
22749  */
22750 static void
22751 ill_phyint_free(ill_t *ill)
22752 {
22753 	phyint_t *phyi;
22754 	phyint_t *next_phyint;
22755 	ipsq_t *cur_ipsq;
22756 	ip_stack_t	*ipst = ill->ill_ipst;
22757 
22758 	ASSERT(ill->ill_phyint != NULL);
22759 
22760 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22761 	phyi = ill->ill_phyint;
22762 	ill->ill_phyint = NULL;
22763 	/*
22764 	 * ill_init allocates a phyint always to store the copy
22765 	 * of flags relevant to phyint. At that point in time, we could
22766 	 * not assign the name and hence phyint_illv4/v6 could not be
22767 	 * initialized. Later in ipif_set_values, we assign the name to
22768 	 * the ill, at which point in time we assign phyint_illv4/v6.
22769 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22770 	 */
22771 	if (ill->ill_flags & ILLF_IPV6) {
22772 		phyi->phyint_illv6 = NULL;
22773 	} else {
22774 		phyi->phyint_illv4 = NULL;
22775 	}
22776 	/*
22777 	 * ipif_down removes it from the group when the last ipif goes
22778 	 * down.
22779 	 */
22780 	ASSERT(ill->ill_group == NULL);
22781 
22782 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22783 		return;
22784 
22785 	/*
22786 	 * Make sure this phyint was put in the list.
22787 	 */
22788 	if (phyi->phyint_ifindex > 0) {
22789 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22790 		    phyi);
22791 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22792 		    phyi);
22793 	}
22794 	/*
22795 	 * remove phyint from the ipsq list.
22796 	 */
22797 	cur_ipsq = phyi->phyint_ipsq;
22798 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22799 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22800 	} else {
22801 		next_phyint = cur_ipsq->ipsq_phyint_list;
22802 		while (next_phyint != NULL) {
22803 			if (next_phyint->phyint_ipsq_next == phyi) {
22804 				next_phyint->phyint_ipsq_next =
22805 				    phyi->phyint_ipsq_next;
22806 				break;
22807 			}
22808 			next_phyint = next_phyint->phyint_ipsq_next;
22809 		}
22810 		ASSERT(next_phyint != NULL);
22811 	}
22812 	IPSQ_DEC_REF(cur_ipsq, ipst);
22813 
22814 	if (phyi->phyint_groupname_len != 0) {
22815 		ASSERT(phyi->phyint_groupname != NULL);
22816 		mi_free(phyi->phyint_groupname);
22817 	}
22818 	mi_free(phyi);
22819 }
22820 
22821 /*
22822  * Attach the ill to the phyint structure which can be shared by both
22823  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22824  * function is called from ipif_set_values and ill_lookup_on_name (for
22825  * loopback) where we know the name of the ill. We lookup the ill and if
22826  * there is one present already with the name use that phyint. Otherwise
22827  * reuse the one allocated by ill_init.
22828  */
22829 static void
22830 ill_phyint_reinit(ill_t *ill)
22831 {
22832 	boolean_t isv6 = ill->ill_isv6;
22833 	phyint_t *phyi_old;
22834 	phyint_t *phyi;
22835 	avl_index_t where = 0;
22836 	ill_t	*ill_other = NULL;
22837 	ipsq_t	*ipsq;
22838 	ip_stack_t	*ipst = ill->ill_ipst;
22839 
22840 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22841 
22842 	phyi_old = ill->ill_phyint;
22843 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22844 	    phyi_old->phyint_illv6 == NULL));
22845 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22846 	    phyi_old->phyint_illv4 == NULL));
22847 	ASSERT(phyi_old->phyint_ifindex == 0);
22848 
22849 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22850 	    ill->ill_name, &where);
22851 
22852 	/*
22853 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22854 	 *    the global list of ills. So no other thread could have located
22855 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22856 	 * 2. Now locate the other protocol instance of this ill.
22857 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22858 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22859 	 *    of neither ill can change.
22860 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22861 	 *    other ill.
22862 	 * 5. Release all locks.
22863 	 */
22864 
22865 	/*
22866 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22867 	 * we are initializing IPv4.
22868 	 */
22869 	if (phyi != NULL) {
22870 		ill_other = (isv6) ? phyi->phyint_illv4 :
22871 		    phyi->phyint_illv6;
22872 		ASSERT(ill_other->ill_phyint != NULL);
22873 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22874 		    (!isv6 && ill_other->ill_isv6));
22875 		GRAB_ILL_LOCKS(ill, ill_other);
22876 		/*
22877 		 * We are potentially throwing away phyint_flags which
22878 		 * could be different from the one that we obtain from
22879 		 * ill_other->ill_phyint. But it is okay as we are assuming
22880 		 * that the state maintained within IP is correct.
22881 		 */
22882 		mutex_enter(&phyi->phyint_lock);
22883 		if (isv6) {
22884 			ASSERT(phyi->phyint_illv6 == NULL);
22885 			phyi->phyint_illv6 = ill;
22886 		} else {
22887 			ASSERT(phyi->phyint_illv4 == NULL);
22888 			phyi->phyint_illv4 = ill;
22889 		}
22890 		/*
22891 		 * This is a new ill, currently undergoing SLIFNAME
22892 		 * So we could not have joined an IPMP group until now.
22893 		 */
22894 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22895 		    phyi_old->phyint_groupname == NULL);
22896 
22897 		/*
22898 		 * This phyi_old is going away. Decref ipsq_refs and
22899 		 * assert it is zero. The ipsq itself will be freed in
22900 		 * ipsq_exit
22901 		 */
22902 		ipsq = phyi_old->phyint_ipsq;
22903 		IPSQ_DEC_REF(ipsq, ipst);
22904 		ASSERT(ipsq->ipsq_refs == 0);
22905 		/* Get the singleton phyint out of the ipsq list */
22906 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22907 		ipsq->ipsq_phyint_list = NULL;
22908 		phyi_old->phyint_illv4 = NULL;
22909 		phyi_old->phyint_illv6 = NULL;
22910 		mi_free(phyi_old);
22911 	} else {
22912 		mutex_enter(&ill->ill_lock);
22913 		/*
22914 		 * We don't need to acquire any lock, since
22915 		 * the ill is not yet visible globally  and we
22916 		 * have not yet released the ill_g_lock.
22917 		 */
22918 		phyi = phyi_old;
22919 		mutex_enter(&phyi->phyint_lock);
22920 		/* XXX We need a recovery strategy here. */
22921 		if (!phyint_assign_ifindex(phyi, ipst))
22922 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22923 
22924 		/* No IPMP group yet, thus the hook uses the ifindex */
22925 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22926 
22927 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22928 		    (void *)phyi, where);
22929 
22930 		(void) avl_find(&ipst->ips_phyint_g_list->
22931 		    phyint_list_avl_by_index,
22932 		    &phyi->phyint_ifindex, &where);
22933 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22934 		    (void *)phyi, where);
22935 	}
22936 
22937 	/*
22938 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22939 	 * pending mp is not affected because that is per ill basis.
22940 	 */
22941 	ill->ill_phyint = phyi;
22942 
22943 	/*
22944 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22945 	 * We do this here as when the first ipif was allocated,
22946 	 * ipif_allocate does not know the right interface index.
22947 	 */
22948 
22949 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22950 	/*
22951 	 * Now that the phyint's ifindex has been assigned, complete the
22952 	 * remaining
22953 	 */
22954 
22955 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22956 	if (ill->ill_isv6) {
22957 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22958 		    ill->ill_phyint->phyint_ifindex;
22959 	}
22960 
22961 	/*
22962 	 * Generate an event within the hooks framework to indicate that
22963 	 * a new interface has just been added to IP.  For this event to
22964 	 * be generated, the network interface must, at least, have an
22965 	 * ifindex assigned to it.
22966 	 *
22967 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22968 	 * that the ordering of delivered events to listeners matches the
22969 	 * order of them in the kernel.
22970 	 *
22971 	 * This function could be called from ill_lookup_on_name. In that case
22972 	 * the interface is loopback "lo", which will not generate a NIC event.
22973 	 */
22974 	if (ill->ill_name_length <= 2 ||
22975 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22976 		/*
22977 		 * Generate nic plumb event for ill_name even if
22978 		 * ipmp_hook_emulation is set. That avoids generating events
22979 		 * for the ill_names should ipmp_hook_emulation be turned on
22980 		 * later.
22981 		 */
22982 		ill_nic_info_plumb(ill, B_FALSE);
22983 	}
22984 	RELEASE_ILL_LOCKS(ill, ill_other);
22985 	mutex_exit(&phyi->phyint_lock);
22986 }
22987 
22988 /*
22989  * Allocate a NE_PLUMB nic info event and store in the ill.
22990  * If 'group' is set we do it for the group name, otherwise the ill name.
22991  * It will be sent when we leave the ipsq.
22992  */
22993 void
22994 ill_nic_info_plumb(ill_t *ill, boolean_t group)
22995 {
22996 	phyint_t	*phyi = ill->ill_phyint;
22997 	ip_stack_t	*ipst = ill->ill_ipst;
22998 	hook_nic_event_t *info;
22999 	char		*name;
23000 	int		namelen;
23001 
23002 	ASSERT(MUTEX_HELD(&ill->ill_lock));
23003 
23004 	if ((info = ill->ill_nic_event_info) != NULL) {
23005 		ip2dbg(("ill_nic_info_plumb: unexpected nic event %d "
23006 		    "attached for %s\n", info->hne_event,
23007 		    ill->ill_name));
23008 		if (info->hne_data != NULL)
23009 			kmem_free(info->hne_data, info->hne_datalen);
23010 		kmem_free(info, sizeof (hook_nic_event_t));
23011 		ill->ill_nic_event_info = NULL;
23012 	}
23013 
23014 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
23015 	if (info == NULL) {
23016 		ip2dbg(("ill_nic_info_plumb: could not attach PLUMB nic "
23017 		    "event information for %s (ENOMEM)\n",
23018 		    ill->ill_name));
23019 		return;
23020 	}
23021 
23022 	if (group) {
23023 		ASSERT(phyi->phyint_groupname_len != 0);
23024 		namelen = phyi->phyint_groupname_len;
23025 		name = phyi->phyint_groupname;
23026 	} else {
23027 		namelen = ill->ill_name_length;
23028 		name = ill->ill_name;
23029 	}
23030 
23031 	info->hne_nic = phyi->phyint_hook_ifindex;
23032 	info->hne_lif = 0;
23033 	info->hne_event = NE_PLUMB;
23034 	info->hne_family = ill->ill_isv6 ?
23035 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
23036 
23037 	info->hne_data = kmem_alloc(namelen, KM_NOSLEEP);
23038 	if (info->hne_data != NULL) {
23039 		info->hne_datalen = namelen;
23040 		bcopy(name, info->hne_data, info->hne_datalen);
23041 	} else {
23042 		ip2dbg(("ill_nic_info_plumb: could not attach "
23043 		    "name information for PLUMB nic event "
23044 		    "of %s (ENOMEM)\n", name));
23045 		kmem_free(info, sizeof (hook_nic_event_t));
23046 		info = NULL;
23047 	}
23048 	ill->ill_nic_event_info = info;
23049 }
23050 
23051 /*
23052  * Unhook the nic event message from the ill and enqueue it
23053  * into the nic event taskq.
23054  */
23055 void
23056 ill_nic_info_dispatch(ill_t *ill)
23057 {
23058 	hook_nic_event_t *info;
23059 
23060 	ASSERT(MUTEX_HELD(&ill->ill_lock));
23061 
23062 	if ((info = ill->ill_nic_event_info) != NULL) {
23063 		if (ddi_taskq_dispatch(eventq_queue_nic,
23064 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
23065 			ip2dbg(("ill_nic_info_dispatch: "
23066 			    "ddi_taskq_dispatch failed\n"));
23067 			if (info->hne_data != NULL)
23068 				kmem_free(info->hne_data, info->hne_datalen);
23069 			kmem_free(info, sizeof (hook_nic_event_t));
23070 		}
23071 		ill->ill_nic_event_info = NULL;
23072 	}
23073 }
23074 
23075 /*
23076  * Notify any downstream modules of the name of this interface.
23077  * An M_IOCTL is used even though we don't expect a successful reply.
23078  * Any reply message from the driver (presumably an M_IOCNAK) will
23079  * eventually get discarded somewhere upstream.  The message format is
23080  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
23081  * to IP.
23082  */
23083 static void
23084 ip_ifname_notify(ill_t *ill, queue_t *q)
23085 {
23086 	mblk_t *mp1, *mp2;
23087 	struct iocblk *iocp;
23088 	struct lifreq *lifr;
23089 
23090 	mp1 = mkiocb(SIOCSLIFNAME);
23091 	if (mp1 == NULL)
23092 		return;
23093 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
23094 	if (mp2 == NULL) {
23095 		freeb(mp1);
23096 		return;
23097 	}
23098 
23099 	mp1->b_cont = mp2;
23100 	iocp = (struct iocblk *)mp1->b_rptr;
23101 	iocp->ioc_count = sizeof (struct lifreq);
23102 
23103 	lifr = (struct lifreq *)mp2->b_rptr;
23104 	mp2->b_wptr += sizeof (struct lifreq);
23105 	bzero(lifr, sizeof (struct lifreq));
23106 
23107 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
23108 	lifr->lifr_ppa = ill->ill_ppa;
23109 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
23110 
23111 	putnext(q, mp1);
23112 }
23113 
23114 static int
23115 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
23116 {
23117 	int err;
23118 	ip_stack_t	*ipst = ill->ill_ipst;
23119 
23120 	/* Set the obsolete NDD per-interface forwarding name. */
23121 	err = ill_set_ndd_name(ill);
23122 	if (err != 0) {
23123 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
23124 		    err);
23125 	}
23126 
23127 	/* Tell downstream modules where they are. */
23128 	ip_ifname_notify(ill, q);
23129 
23130 	/*
23131 	 * ill_dl_phys returns EINPROGRESS in the usual case.
23132 	 * Error cases are ENOMEM ...
23133 	 */
23134 	err = ill_dl_phys(ill, ipif, mp, q);
23135 
23136 	/*
23137 	 * If there is no IRE expiration timer running, get one started.
23138 	 * igmp and mld timers will be triggered by the first multicast
23139 	 */
23140 	if (ipst->ips_ip_ire_expire_id == 0) {
23141 		/*
23142 		 * acquire the lock and check again.
23143 		 */
23144 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
23145 		if (ipst->ips_ip_ire_expire_id == 0) {
23146 			ipst->ips_ip_ire_expire_id = timeout(
23147 			    ip_trash_timer_expire, ipst,
23148 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
23149 		}
23150 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
23151 	}
23152 
23153 	if (ill->ill_isv6) {
23154 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
23155 		if (ipst->ips_mld_slowtimeout_id == 0) {
23156 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
23157 			    (void *)ipst,
23158 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
23159 		}
23160 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
23161 	} else {
23162 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
23163 		if (ipst->ips_igmp_slowtimeout_id == 0) {
23164 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
23165 			    (void *)ipst,
23166 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
23167 		}
23168 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
23169 	}
23170 
23171 	return (err);
23172 }
23173 
23174 /*
23175  * Common routine for ppa and ifname setting. Should be called exclusive.
23176  *
23177  * Returns EINPROGRESS when mp has been consumed by queueing it on
23178  * ill_pending_mp and the ioctl will complete in ip_rput.
23179  *
23180  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
23181  * the new name and new ppa in lifr_name and lifr_ppa respectively.
23182  * For SLIFNAME, we pass these values back to the userland.
23183  */
23184 static int
23185 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
23186 {
23187 	ill_t	*ill;
23188 	ipif_t	*ipif;
23189 	ipsq_t	*ipsq;
23190 	char	*ppa_ptr;
23191 	char	*old_ptr;
23192 	char	old_char;
23193 	int	error;
23194 	ip_stack_t	*ipst;
23195 
23196 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
23197 	ASSERT(q->q_next != NULL);
23198 	ASSERT(interf_name != NULL);
23199 
23200 	ill = (ill_t *)q->q_ptr;
23201 	ipst = ill->ill_ipst;
23202 
23203 	ASSERT(ill->ill_ipst != NULL);
23204 	ASSERT(ill->ill_name[0] == '\0');
23205 	ASSERT(IAM_WRITER_ILL(ill));
23206 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
23207 	ASSERT(ill->ill_ppa == UINT_MAX);
23208 
23209 	/* The ppa is sent down by ifconfig or is chosen */
23210 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
23211 		return (EINVAL);
23212 	}
23213 
23214 	/*
23215 	 * make sure ppa passed in is same as ppa in the name.
23216 	 * This check is not made when ppa == UINT_MAX in that case ppa
23217 	 * in the name could be anything. System will choose a ppa and
23218 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
23219 	 */
23220 	if (*new_ppa_ptr != UINT_MAX) {
23221 		/* stoi changes the pointer */
23222 		old_ptr = ppa_ptr;
23223 		/*
23224 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
23225 		 * (they don't have an externally visible ppa).  We assign one
23226 		 * here so that we can manage the interface.  Note that in
23227 		 * the past this value was always 0 for DLPI 1 drivers.
23228 		 */
23229 		if (*new_ppa_ptr == 0)
23230 			*new_ppa_ptr = stoi(&old_ptr);
23231 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
23232 			return (EINVAL);
23233 	}
23234 	/*
23235 	 * terminate string before ppa
23236 	 * save char at that location.
23237 	 */
23238 	old_char = ppa_ptr[0];
23239 	ppa_ptr[0] = '\0';
23240 
23241 	ill->ill_ppa = *new_ppa_ptr;
23242 	/*
23243 	 * Finish as much work now as possible before calling ill_glist_insert
23244 	 * which makes the ill globally visible and also merges it with the
23245 	 * other protocol instance of this phyint. The remaining work is
23246 	 * done after entering the ipsq which may happen sometime later.
23247 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
23248 	 */
23249 	ipif = ill->ill_ipif;
23250 
23251 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
23252 	ipif_assign_seqid(ipif);
23253 
23254 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
23255 		ill->ill_flags |= ILLF_IPV4;
23256 
23257 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
23258 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
23259 
23260 	if (ill->ill_flags & ILLF_IPV6) {
23261 
23262 		ill->ill_isv6 = B_TRUE;
23263 		if (ill->ill_rq != NULL) {
23264 			ill->ill_rq->q_qinfo = &rinit_ipv6;
23265 			ill->ill_wq->q_qinfo = &winit_ipv6;
23266 		}
23267 
23268 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
23269 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
23270 		ipif->ipif_v6src_addr = ipv6_all_zeros;
23271 		ipif->ipif_v6subnet = ipv6_all_zeros;
23272 		ipif->ipif_v6net_mask = ipv6_all_zeros;
23273 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
23274 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
23275 		/*
23276 		 * point-to-point or Non-mulicast capable
23277 		 * interfaces won't do NUD unless explicitly
23278 		 * configured to do so.
23279 		 */
23280 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
23281 		    !(ill->ill_flags & ILLF_MULTICAST)) {
23282 			ill->ill_flags |= ILLF_NONUD;
23283 		}
23284 		/* Make sure IPv4 specific flag is not set on IPv6 if */
23285 		if (ill->ill_flags & ILLF_NOARP) {
23286 			/*
23287 			 * Note: xresolv interfaces will eventually need
23288 			 * NOARP set here as well, but that will require
23289 			 * those external resolvers to have some
23290 			 * knowledge of that flag and act appropriately.
23291 			 * Not to be changed at present.
23292 			 */
23293 			ill->ill_flags &= ~ILLF_NOARP;
23294 		}
23295 		/*
23296 		 * Set the ILLF_ROUTER flag according to the global
23297 		 * IPv6 forwarding policy.
23298 		 */
23299 		if (ipst->ips_ipv6_forward != 0)
23300 			ill->ill_flags |= ILLF_ROUTER;
23301 	} else if (ill->ill_flags & ILLF_IPV4) {
23302 		ill->ill_isv6 = B_FALSE;
23303 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
23304 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
23305 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
23306 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
23307 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
23308 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
23309 		/*
23310 		 * Set the ILLF_ROUTER flag according to the global
23311 		 * IPv4 forwarding policy.
23312 		 */
23313 		if (ipst->ips_ip_g_forward != 0)
23314 			ill->ill_flags |= ILLF_ROUTER;
23315 	}
23316 
23317 	ASSERT(ill->ill_phyint != NULL);
23318 
23319 	/*
23320 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
23321 	 * be completed in ill_glist_insert -> ill_phyint_reinit
23322 	 */
23323 	if (!ill_allocate_mibs(ill))
23324 		return (ENOMEM);
23325 
23326 	/*
23327 	 * Pick a default sap until we get the DL_INFO_ACK back from
23328 	 * the driver.
23329 	 */
23330 	if (ill->ill_sap == 0) {
23331 		if (ill->ill_isv6)
23332 			ill->ill_sap  = IP6_DL_SAP;
23333 		else
23334 			ill->ill_sap  = IP_DL_SAP;
23335 	}
23336 
23337 	ill->ill_ifname_pending = 1;
23338 	ill->ill_ifname_pending_err = 0;
23339 
23340 	ill_refhold(ill);
23341 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
23342 	if ((error = ill_glist_insert(ill, interf_name,
23343 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
23344 		ill->ill_ppa = UINT_MAX;
23345 		ill->ill_name[0] = '\0';
23346 		/*
23347 		 * undo null termination done above.
23348 		 */
23349 		ppa_ptr[0] = old_char;
23350 		rw_exit(&ipst->ips_ill_g_lock);
23351 		ill_refrele(ill);
23352 		return (error);
23353 	}
23354 
23355 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
23356 
23357 	/*
23358 	 * When we return the buffer pointed to by interf_name should contain
23359 	 * the same name as in ill_name.
23360 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
23361 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
23362 	 * so copy full name and update the ppa ptr.
23363 	 * When ppa passed in != UINT_MAX all values are correct just undo
23364 	 * null termination, this saves a bcopy.
23365 	 */
23366 	if (*new_ppa_ptr == UINT_MAX) {
23367 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23368 		*new_ppa_ptr = ill->ill_ppa;
23369 	} else {
23370 		/*
23371 		 * undo null termination done above.
23372 		 */
23373 		ppa_ptr[0] = old_char;
23374 	}
23375 
23376 	/* Let SCTP know about this ILL */
23377 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23378 
23379 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23380 	    B_TRUE);
23381 
23382 	rw_exit(&ipst->ips_ill_g_lock);
23383 	ill_refrele(ill);
23384 	if (ipsq == NULL)
23385 		return (EINPROGRESS);
23386 
23387 	/*
23388 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23389 	 */
23390 	if (ipsq->ipsq_current_ipif == NULL)
23391 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23392 	else
23393 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23394 
23395 	error = ipif_set_values_tail(ill, ipif, mp, q);
23396 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
23397 	if (error != 0 && error != EINPROGRESS) {
23398 		/*
23399 		 * restore previous values
23400 		 */
23401 		ill->ill_isv6 = B_FALSE;
23402 	}
23403 	return (error);
23404 }
23405 
23406 
23407 void
23408 ipif_init(ip_stack_t *ipst)
23409 {
23410 	hrtime_t hrt;
23411 	int i;
23412 
23413 	/*
23414 	 * Can't call drv_getparm here as it is too early in the boot.
23415 	 * As we use ipif_src_random just for picking a different
23416 	 * source address everytime, this need not be really random.
23417 	 */
23418 	hrt = gethrtime();
23419 	ipst->ips_ipif_src_random =
23420 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23421 
23422 	for (i = 0; i < MAX_G_HEADS; i++) {
23423 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23424 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23425 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23426 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23427 	}
23428 
23429 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23430 	    ill_phyint_compare_index,
23431 	    sizeof (phyint_t),
23432 	    offsetof(struct phyint, phyint_avl_by_index));
23433 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23434 	    ill_phyint_compare_name,
23435 	    sizeof (phyint_t),
23436 	    offsetof(struct phyint, phyint_avl_by_name));
23437 }
23438 
23439 /*
23440  * This is called by ip_rt_add when src_addr value is other than zero.
23441  * src_addr signifies the source address of the incoming packet. For
23442  * reverse tunnel route we need to create a source addr based routing
23443  * table. This routine creates ip_mrtun_table if it's empty and then
23444  * it adds the route entry hashed by source address. It verifies that
23445  * the outgoing interface is always a non-resolver interface (tunnel).
23446  */
23447 int
23448 ip_mrtun_rt_add(ipaddr_t in_src_addr, int flags, ipif_t *ipif_arg,
23449     ipif_t *src_ipif, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func,
23450     ip_stack_t *ipst)
23451 {
23452 	ire_t   *ire;
23453 	ire_t	*save_ire;
23454 	ipif_t  *ipif;
23455 	ill_t   *in_ill = NULL;
23456 	ill_t	*out_ill;
23457 	queue_t	*stq;
23458 	mblk_t	*dlureq_mp;
23459 	int	error;
23460 
23461 	if (ire_arg != NULL)
23462 		*ire_arg = NULL;
23463 	ASSERT(in_src_addr != INADDR_ANY);
23464 
23465 	ipif = ipif_arg;
23466 	if (ipif != NULL) {
23467 		out_ill = ipif->ipif_ill;
23468 	} else {
23469 		ip1dbg(("ip_mrtun_rt_add: ipif is NULL\n"));
23470 		return (EINVAL);
23471 	}
23472 
23473 	if (src_ipif == NULL) {
23474 		ip1dbg(("ip_mrtun_rt_add: src_ipif is NULL\n"));
23475 		return (EINVAL);
23476 	}
23477 	in_ill = src_ipif->ipif_ill;
23478 
23479 	/*
23480 	 * Check for duplicates. We don't need to
23481 	 * match out_ill, because the uniqueness of
23482 	 * a route is only dependent on src_addr and
23483 	 * in_ill.
23484 	 */
23485 	ire = ire_mrtun_lookup(in_src_addr, in_ill);
23486 	if (ire != NULL) {
23487 		ire_refrele(ire);
23488 		return (EEXIST);
23489 	}
23490 	if (ipif->ipif_net_type != IRE_IF_NORESOLVER) {
23491 		ip2dbg(("ip_mrtun_rt_add: outgoing interface is type %d\n",
23492 		    ipif->ipif_net_type));
23493 		return (EINVAL);
23494 	}
23495 
23496 	stq = ipif->ipif_wq;
23497 	ASSERT(stq != NULL);
23498 
23499 	/*
23500 	 * The outgoing interface must be non-resolver
23501 	 * interface.
23502 	 */
23503 	dlureq_mp = ill_dlur_gen(NULL,
23504 	    out_ill->ill_phys_addr_length, out_ill->ill_sap,
23505 	    out_ill->ill_sap_length);
23506 
23507 	if (dlureq_mp == NULL) {
23508 		ip1dbg(("ip_newroute: dlureq_mp NULL\n"));
23509 		return (ENOMEM);
23510 	}
23511 
23512 	/* Create the IRE. */
23513 
23514 	ire = ire_create(
23515 	    NULL,				/* Zero dst addr */
23516 	    NULL,				/* Zero mask */
23517 	    NULL,				/* Zero gateway addr */
23518 	    NULL,				/* Zero ipif_src addr */
23519 	    (uint8_t *)&in_src_addr,		/* in_src-addr */
23520 	    &ipif->ipif_mtu,
23521 	    NULL,
23522 	    NULL,				/* rfq */
23523 	    stq,
23524 	    IRE_MIPRTUN,
23525 	    ipif,
23526 	    in_ill,
23527 	    0,
23528 	    0,
23529 	    0,
23530 	    flags,
23531 	    &ire_uinfo_null,
23532 	    NULL,
23533 	    NULL,
23534 	    ipst);
23535 
23536 	if (ire == NULL) {
23537 		freeb(dlureq_mp);
23538 		return (ENOMEM);
23539 	}
23540 	ip2dbg(("ip_mrtun_rt_add: mrtun route is created with type %d\n",
23541 	    ire->ire_type));
23542 	save_ire = ire;
23543 	ASSERT(save_ire != NULL);
23544 	error = ire_add_mrtun(&ire, q, mp, func);
23545 	/*
23546 	 * If ire_add_mrtun() failed, the ire passed in was freed
23547 	 * so there is no need to do so here.
23548 	 */
23549 	if (error != 0) {
23550 		return (error);
23551 	}
23552 
23553 	/* Duplicate check */
23554 	if (ire != save_ire) {
23555 		/* route already exists by now */
23556 		ire_refrele(ire);
23557 		return (EEXIST);
23558 	}
23559 
23560 	if (ire_arg != NULL) {
23561 		/*
23562 		 * Store the ire that was just added. the caller
23563 		 * ip_rts_request responsible for doing ire_refrele()
23564 		 * on it.
23565 		 */
23566 		*ire_arg = ire;
23567 	} else {
23568 		ire_refrele(ire);	/* held in ire_add_mrtun */
23569 	}
23570 
23571 	return (0);
23572 }
23573 
23574 /*
23575  * It is called by ip_rt_delete() only when mipagent requests to delete
23576  * a reverse tunnel route that was added by ip_mrtun_rt_add() before.
23577  */
23578 
23579 int
23580 ip_mrtun_rt_delete(ipaddr_t in_src_addr, ipif_t *src_ipif)
23581 {
23582 	ire_t   *ire = NULL;
23583 
23584 	if (in_src_addr == INADDR_ANY)
23585 		return (EINVAL);
23586 	if (src_ipif == NULL)
23587 		return (EINVAL);
23588 
23589 	/* search if this route exists in the ip_mrtun_table */
23590 	ire = ire_mrtun_lookup(in_src_addr, src_ipif->ipif_ill);
23591 	if (ire == NULL) {
23592 		ip2dbg(("ip_mrtun_rt_delete: ire not found\n"));
23593 		return (ESRCH);
23594 	}
23595 	ire_delete(ire);
23596 	ire_refrele(ire);
23597 	return (0);
23598 }
23599 
23600 /*
23601  * Lookup the ipif corresponding to the onlink destination address. For
23602  * point-to-point interfaces, it matches with remote endpoint destination
23603  * address. For point-to-multipoint interfaces it only tries to match the
23604  * destination with the interface's subnet address. The longest, most specific
23605  * match is found to take care of such rare network configurations like -
23606  * le0: 129.146.1.1/16
23607  * le1: 129.146.2.2/24
23608  * It is used only by SO_DONTROUTE at the moment.
23609  */
23610 ipif_t *
23611 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23612 {
23613 	ipif_t	*ipif, *best_ipif;
23614 	ill_t	*ill;
23615 	ill_walk_context_t ctx;
23616 
23617 	ASSERT(zoneid != ALL_ZONES);
23618 	best_ipif = NULL;
23619 
23620 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23621 	ill = ILL_START_WALK_V4(&ctx, ipst);
23622 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23623 		mutex_enter(&ill->ill_lock);
23624 		for (ipif = ill->ill_ipif; ipif != NULL;
23625 		    ipif = ipif->ipif_next) {
23626 			if (!IPIF_CAN_LOOKUP(ipif))
23627 				continue;
23628 			if (ipif->ipif_zoneid != zoneid &&
23629 			    ipif->ipif_zoneid != ALL_ZONES)
23630 				continue;
23631 			/*
23632 			 * Point-to-point case. Look for exact match with
23633 			 * destination address.
23634 			 */
23635 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23636 				if (ipif->ipif_pp_dst_addr == addr) {
23637 					ipif_refhold_locked(ipif);
23638 					mutex_exit(&ill->ill_lock);
23639 					rw_exit(&ipst->ips_ill_g_lock);
23640 					if (best_ipif != NULL)
23641 						ipif_refrele(best_ipif);
23642 					return (ipif);
23643 				}
23644 			} else if (ipif->ipif_subnet == (addr &
23645 			    ipif->ipif_net_mask)) {
23646 				/*
23647 				 * Point-to-multipoint case. Looping through to
23648 				 * find the most specific match. If there are
23649 				 * multiple best match ipif's then prefer ipif's
23650 				 * that are UP. If there is only one best match
23651 				 * ipif and it is DOWN we must still return it.
23652 				 */
23653 				if ((best_ipif == NULL) ||
23654 				    (ipif->ipif_net_mask >
23655 				    best_ipif->ipif_net_mask) ||
23656 				    ((ipif->ipif_net_mask ==
23657 				    best_ipif->ipif_net_mask) &&
23658 				    ((ipif->ipif_flags & IPIF_UP) &&
23659 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23660 					ipif_refhold_locked(ipif);
23661 					mutex_exit(&ill->ill_lock);
23662 					rw_exit(&ipst->ips_ill_g_lock);
23663 					if (best_ipif != NULL)
23664 						ipif_refrele(best_ipif);
23665 					best_ipif = ipif;
23666 					rw_enter(&ipst->ips_ill_g_lock,
23667 					    RW_READER);
23668 					mutex_enter(&ill->ill_lock);
23669 				}
23670 			}
23671 		}
23672 		mutex_exit(&ill->ill_lock);
23673 	}
23674 	rw_exit(&ipst->ips_ill_g_lock);
23675 	return (best_ipif);
23676 }
23677 
23678 
23679 /*
23680  * Save enough information so that we can recreate the IRE if
23681  * the interface goes down and then up.
23682  */
23683 static void
23684 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23685 {
23686 	mblk_t	*save_mp;
23687 
23688 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23689 	if (save_mp != NULL) {
23690 		ifrt_t	*ifrt;
23691 
23692 		save_mp->b_wptr += sizeof (ifrt_t);
23693 		ifrt = (ifrt_t *)save_mp->b_rptr;
23694 		bzero(ifrt, sizeof (ifrt_t));
23695 		ifrt->ifrt_type = ire->ire_type;
23696 		ifrt->ifrt_addr = ire->ire_addr;
23697 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23698 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23699 		ifrt->ifrt_mask = ire->ire_mask;
23700 		ifrt->ifrt_flags = ire->ire_flags;
23701 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23702 		mutex_enter(&ipif->ipif_saved_ire_lock);
23703 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23704 		ipif->ipif_saved_ire_mp = save_mp;
23705 		ipif->ipif_saved_ire_cnt++;
23706 		mutex_exit(&ipif->ipif_saved_ire_lock);
23707 	}
23708 }
23709 
23710 
23711 static void
23712 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23713 {
23714 	mblk_t	**mpp;
23715 	mblk_t	*mp;
23716 	ifrt_t	*ifrt;
23717 
23718 	/* Remove from ipif_saved_ire_mp list if it is there */
23719 	mutex_enter(&ipif->ipif_saved_ire_lock);
23720 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23721 	    mpp = &(*mpp)->b_cont) {
23722 		/*
23723 		 * On a given ipif, the triple of address, gateway and
23724 		 * mask is unique for each saved IRE (in the case of
23725 		 * ordinary interface routes, the gateway address is
23726 		 * all-zeroes).
23727 		 */
23728 		mp = *mpp;
23729 		ifrt = (ifrt_t *)mp->b_rptr;
23730 		if (ifrt->ifrt_addr == ire->ire_addr &&
23731 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23732 		    ifrt->ifrt_mask == ire->ire_mask) {
23733 			*mpp = mp->b_cont;
23734 			ipif->ipif_saved_ire_cnt--;
23735 			freeb(mp);
23736 			break;
23737 		}
23738 	}
23739 	mutex_exit(&ipif->ipif_saved_ire_lock);
23740 }
23741 
23742 
23743 /*
23744  * IP multirouting broadcast routes handling
23745  * Append CGTP broadcast IREs to regular ones created
23746  * at ifconfig time.
23747  */
23748 static void
23749 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23750 {
23751 	ire_t *ire_prim;
23752 
23753 	ASSERT(ire != NULL);
23754 	ASSERT(ire_dst != NULL);
23755 
23756 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23757 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23758 	if (ire_prim != NULL) {
23759 		/*
23760 		 * We are in the special case of broadcasts for
23761 		 * CGTP. We add an IRE_BROADCAST that holds
23762 		 * the RTF_MULTIRT flag, the destination
23763 		 * address of ire_dst and the low level
23764 		 * info of ire_prim. In other words, CGTP
23765 		 * broadcast is added to the redundant ipif.
23766 		 */
23767 		ipif_t *ipif_prim;
23768 		ire_t  *bcast_ire;
23769 
23770 		ipif_prim = ire_prim->ire_ipif;
23771 
23772 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23773 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23774 		    (void *)ire_dst, (void *)ire_prim,
23775 		    (void *)ipif_prim));
23776 
23777 		bcast_ire = ire_create(
23778 		    (uchar_t *)&ire->ire_addr,
23779 		    (uchar_t *)&ip_g_all_ones,
23780 		    (uchar_t *)&ire_dst->ire_src_addr,
23781 		    (uchar_t *)&ire->ire_gateway_addr,
23782 		    NULL,
23783 		    &ipif_prim->ipif_mtu,
23784 		    NULL,
23785 		    ipif_prim->ipif_rq,
23786 		    ipif_prim->ipif_wq,
23787 		    IRE_BROADCAST,
23788 		    ipif_prim,
23789 		    NULL,
23790 		    0,
23791 		    0,
23792 		    0,
23793 		    ire->ire_flags,
23794 		    &ire_uinfo_null,
23795 		    NULL,
23796 		    NULL,
23797 		    ipst);
23798 
23799 		if (bcast_ire != NULL) {
23800 
23801 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23802 			    B_FALSE) == 0) {
23803 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23804 				    "added bcast_ire %p\n",
23805 				    (void *)bcast_ire));
23806 
23807 				ipif_save_ire(bcast_ire->ire_ipif,
23808 				    bcast_ire);
23809 				ire_refrele(bcast_ire);
23810 			}
23811 		}
23812 		ire_refrele(ire_prim);
23813 	}
23814 }
23815 
23816 
23817 /*
23818  * IP multirouting broadcast routes handling
23819  * Remove the broadcast ire
23820  */
23821 static void
23822 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23823 {
23824 	ire_t *ire_dst;
23825 
23826 	ASSERT(ire != NULL);
23827 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23828 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23829 	if (ire_dst != NULL) {
23830 		ire_t *ire_prim;
23831 
23832 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23833 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23834 		if (ire_prim != NULL) {
23835 			ipif_t *ipif_prim;
23836 			ire_t  *bcast_ire;
23837 
23838 			ipif_prim = ire_prim->ire_ipif;
23839 
23840 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23841 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23842 			    (void *)ire_dst, (void *)ire_prim,
23843 			    (void *)ipif_prim));
23844 
23845 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23846 			    ire->ire_gateway_addr,
23847 			    IRE_BROADCAST,
23848 			    ipif_prim, ALL_ZONES,
23849 			    NULL,
23850 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23851 			    MATCH_IRE_MASK, ipst);
23852 
23853 			if (bcast_ire != NULL) {
23854 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23855 				    "looked up bcast_ire %p\n",
23856 				    (void *)bcast_ire));
23857 				ipif_remove_ire(bcast_ire->ire_ipif,
23858 				    bcast_ire);
23859 				ire_delete(bcast_ire);
23860 			}
23861 			ire_refrele(ire_prim);
23862 		}
23863 		ire_refrele(ire_dst);
23864 	}
23865 }
23866 
23867 /*
23868  * IPsec hardware acceleration capabilities related functions.
23869  */
23870 
23871 /*
23872  * Free a per-ill IPsec capabilities structure.
23873  */
23874 static void
23875 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23876 {
23877 	if (capab->auth_hw_algs != NULL)
23878 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23879 	if (capab->encr_hw_algs != NULL)
23880 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23881 	if (capab->encr_algparm != NULL)
23882 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23883 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23884 }
23885 
23886 /*
23887  * Allocate a new per-ill IPsec capabilities structure. This structure
23888  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23889  * an array which specifies, for each algorithm, whether this algorithm
23890  * is supported by the ill or not.
23891  */
23892 static ill_ipsec_capab_t *
23893 ill_ipsec_capab_alloc(void)
23894 {
23895 	ill_ipsec_capab_t *capab;
23896 	uint_t nelems;
23897 
23898 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23899 	if (capab == NULL)
23900 		return (NULL);
23901 
23902 	/* we need one bit per algorithm */
23903 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23904 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23905 
23906 	/* allocate memory to store algorithm flags */
23907 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23908 	if (capab->encr_hw_algs == NULL)
23909 		goto nomem;
23910 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23911 	if (capab->auth_hw_algs == NULL)
23912 		goto nomem;
23913 	/*
23914 	 * Leave encr_algparm NULL for now since we won't need it half
23915 	 * the time
23916 	 */
23917 	return (capab);
23918 
23919 nomem:
23920 	ill_ipsec_capab_free(capab);
23921 	return (NULL);
23922 }
23923 
23924 /*
23925  * Resize capability array.  Since we're exclusive, this is OK.
23926  */
23927 static boolean_t
23928 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23929 {
23930 	ipsec_capab_algparm_t *nalp, *oalp;
23931 	uint32_t olen, nlen;
23932 
23933 	oalp = capab->encr_algparm;
23934 	olen = capab->encr_algparm_size;
23935 
23936 	if (oalp != NULL) {
23937 		if (algid < capab->encr_algparm_end)
23938 			return (B_TRUE);
23939 	}
23940 
23941 	nlen = (algid + 1) * sizeof (*nalp);
23942 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23943 	if (nalp == NULL)
23944 		return (B_FALSE);
23945 
23946 	if (oalp != NULL) {
23947 		bcopy(oalp, nalp, olen);
23948 		kmem_free(oalp, olen);
23949 	}
23950 	capab->encr_algparm = nalp;
23951 	capab->encr_algparm_size = nlen;
23952 	capab->encr_algparm_end = algid + 1;
23953 
23954 	return (B_TRUE);
23955 }
23956 
23957 /*
23958  * Compare the capabilities of the specified ill with the protocol
23959  * and algorithms specified by the SA passed as argument.
23960  * If they match, returns B_TRUE, B_FALSE if they do not match.
23961  *
23962  * The ill can be passed as a pointer to it, or by specifying its index
23963  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23964  *
23965  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23966  * packet is eligible for hardware acceleration, and by
23967  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23968  * to a particular ill.
23969  */
23970 boolean_t
23971 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23972     ipsa_t *sa, netstack_t *ns)
23973 {
23974 	boolean_t sa_isv6;
23975 	uint_t algid;
23976 	struct ill_ipsec_capab_s *cpp;
23977 	boolean_t need_refrele = B_FALSE;
23978 	ip_stack_t	*ipst = ns->netstack_ip;
23979 
23980 	if (ill == NULL) {
23981 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23982 		    NULL, NULL, NULL, ipst);
23983 		if (ill == NULL) {
23984 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23985 			return (B_FALSE);
23986 		}
23987 		need_refrele = B_TRUE;
23988 	}
23989 
23990 	/*
23991 	 * Use the address length specified by the SA to determine
23992 	 * if it corresponds to a IPv6 address, and fail the matching
23993 	 * if the isv6 flag passed as argument does not match.
23994 	 * Note: this check is used for SADB capability checking before
23995 	 * sending SA information to an ill.
23996 	 */
23997 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23998 	if (sa_isv6 != ill_isv6)
23999 		/* protocol mismatch */
24000 		goto done;
24001 
24002 	/*
24003 	 * Check if the ill supports the protocol, algorithm(s) and
24004 	 * key size(s) specified by the SA, and get the pointers to
24005 	 * the algorithms supported by the ill.
24006 	 */
24007 	switch (sa->ipsa_type) {
24008 
24009 	case SADB_SATYPE_ESP:
24010 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
24011 			/* ill does not support ESP acceleration */
24012 			goto done;
24013 		cpp = ill->ill_ipsec_capab_esp;
24014 		algid = sa->ipsa_auth_alg;
24015 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
24016 			goto done;
24017 		algid = sa->ipsa_encr_alg;
24018 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
24019 			goto done;
24020 		if (algid < cpp->encr_algparm_end) {
24021 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
24022 			if (sa->ipsa_encrkeybits < alp->minkeylen)
24023 				goto done;
24024 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
24025 				goto done;
24026 		}
24027 		break;
24028 
24029 	case SADB_SATYPE_AH:
24030 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
24031 			/* ill does not support AH acceleration */
24032 			goto done;
24033 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
24034 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
24035 			goto done;
24036 		break;
24037 	}
24038 
24039 	if (need_refrele)
24040 		ill_refrele(ill);
24041 	return (B_TRUE);
24042 done:
24043 	if (need_refrele)
24044 		ill_refrele(ill);
24045 	return (B_FALSE);
24046 }
24047 
24048 
24049 /*
24050  * Add a new ill to the list of IPsec capable ills.
24051  * Called from ill_capability_ipsec_ack() when an ACK was received
24052  * indicating that IPsec hardware processing was enabled for an ill.
24053  *
24054  * ill must point to the ill for which acceleration was enabled.
24055  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
24056  */
24057 static void
24058 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
24059 {
24060 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
24061 	uint_t sa_type;
24062 	uint_t ipproto;
24063 	ip_stack_t	*ipst = ill->ill_ipst;
24064 
24065 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
24066 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
24067 
24068 	switch (dl_cap) {
24069 	case DL_CAPAB_IPSEC_AH:
24070 		sa_type = SADB_SATYPE_AH;
24071 		ills = &ipst->ips_ipsec_capab_ills_ah;
24072 		ipproto = IPPROTO_AH;
24073 		break;
24074 	case DL_CAPAB_IPSEC_ESP:
24075 		sa_type = SADB_SATYPE_ESP;
24076 		ills = &ipst->ips_ipsec_capab_ills_esp;
24077 		ipproto = IPPROTO_ESP;
24078 		break;
24079 	}
24080 
24081 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
24082 
24083 	/*
24084 	 * Add ill index to list of hardware accelerators. If
24085 	 * already in list, do nothing.
24086 	 */
24087 	for (cur_ill = *ills; cur_ill != NULL &&
24088 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
24089 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
24090 		;
24091 
24092 	if (cur_ill == NULL) {
24093 		/* if this is a new entry for this ill */
24094 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
24095 		if (new_ill == NULL) {
24096 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24097 			return;
24098 		}
24099 
24100 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
24101 		new_ill->ill_isv6 = ill->ill_isv6;
24102 		new_ill->next = *ills;
24103 		*ills = new_ill;
24104 	} else if (!sadb_resync) {
24105 		/* not resync'ing SADB and an entry exists for this ill */
24106 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24107 		return;
24108 	}
24109 
24110 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24111 
24112 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
24113 		/*
24114 		 * IPsec module for protocol loaded, initiate dump
24115 		 * of the SADB to this ill.
24116 		 */
24117 		sadb_ill_download(ill, sa_type);
24118 }
24119 
24120 /*
24121  * Remove an ill from the list of IPsec capable ills.
24122  */
24123 static void
24124 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
24125 {
24126 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
24127 	ip_stack_t	*ipst = ill->ill_ipst;
24128 
24129 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
24130 	    dl_cap == DL_CAPAB_IPSEC_ESP);
24131 
24132 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
24133 	    &ipst->ips_ipsec_capab_ills_esp;
24134 
24135 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
24136 
24137 	prev_ill = NULL;
24138 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
24139 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
24140 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
24141 		;
24142 	if (cur_ill == NULL) {
24143 		/* entry not found */
24144 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24145 		return;
24146 	}
24147 	if (prev_ill == NULL) {
24148 		/* entry at front of list */
24149 		*ills = NULL;
24150 	} else {
24151 		prev_ill->next = cur_ill->next;
24152 	}
24153 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
24154 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24155 }
24156 
24157 /*
24158  * Called by SADB to send a DL_CONTROL_REQ message to every ill
24159  * supporting the specified IPsec protocol acceleration.
24160  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
24161  * We free the mblk and, if sa is non-null, release the held referece.
24162  */
24163 void
24164 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
24165     netstack_t *ns)
24166 {
24167 	ipsec_capab_ill_t *ici, *cur_ici;
24168 	ill_t *ill;
24169 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
24170 	ip_stack_t	*ipst = ns->netstack_ip;
24171 
24172 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
24173 	    ipst->ips_ipsec_capab_ills_esp;
24174 
24175 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
24176 
24177 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
24178 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
24179 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
24180 
24181 		/*
24182 		 * Handle the case where the ill goes away while the SADB is
24183 		 * attempting to send messages.  If it's going away, it's
24184 		 * nuking its shadow SADB, so we don't care..
24185 		 */
24186 
24187 		if (ill == NULL)
24188 			continue;
24189 
24190 		if (sa != NULL) {
24191 			/*
24192 			 * Make sure capabilities match before
24193 			 * sending SA to ill.
24194 			 */
24195 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
24196 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
24197 				ill_refrele(ill);
24198 				continue;
24199 			}
24200 
24201 			mutex_enter(&sa->ipsa_lock);
24202 			sa->ipsa_flags |= IPSA_F_HW;
24203 			mutex_exit(&sa->ipsa_lock);
24204 		}
24205 
24206 		/*
24207 		 * Copy template message, and add it to the front
24208 		 * of the mblk ship list. We want to avoid holding
24209 		 * the ipsec_capab_ills_lock while sending the
24210 		 * message to the ills.
24211 		 *
24212 		 * The b_next and b_prev are temporarily used
24213 		 * to build a list of mblks to be sent down, and to
24214 		 * save the ill to which they must be sent.
24215 		 */
24216 		nmp = copymsg(mp);
24217 		if (nmp == NULL) {
24218 			ill_refrele(ill);
24219 			continue;
24220 		}
24221 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
24222 		nmp->b_next = mp_ship_list;
24223 		mp_ship_list = nmp;
24224 		nmp->b_prev = (mblk_t *)ill;
24225 	}
24226 
24227 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24228 
24229 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
24230 		/* restore the mblk to a sane state */
24231 		next_mp = nmp->b_next;
24232 		nmp->b_next = NULL;
24233 		ill = (ill_t *)nmp->b_prev;
24234 		nmp->b_prev = NULL;
24235 
24236 		ill_dlpi_send(ill, nmp);
24237 		ill_refrele(ill);
24238 	}
24239 
24240 	if (sa != NULL)
24241 		IPSA_REFRELE(sa);
24242 	freemsg(mp);
24243 }
24244 
24245 /*
24246  * Derive an interface id from the link layer address.
24247  * Knows about IEEE 802 and IEEE EUI-64 mappings.
24248  */
24249 static boolean_t
24250 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
24251 {
24252 	char		*addr;
24253 
24254 	if (phys_length != ETHERADDRL)
24255 		return (B_FALSE);
24256 
24257 	/* Form EUI-64 like address */
24258 	addr = (char *)&v6addr->s6_addr32[2];
24259 	bcopy((char *)phys_addr, addr, 3);
24260 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
24261 	addr[3] = (char)0xff;
24262 	addr[4] = (char)0xfe;
24263 	bcopy((char *)phys_addr + 3, addr + 5, 3);
24264 	return (B_TRUE);
24265 }
24266 
24267 /* ARGSUSED */
24268 static boolean_t
24269 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
24270 {
24271 	return (B_FALSE);
24272 }
24273 
24274 /* ARGSUSED */
24275 static boolean_t
24276 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
24277     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
24278 {
24279 	/*
24280 	 * Multicast address mappings used over Ethernet/802.X.
24281 	 * This address is used as a base for mappings.
24282 	 */
24283 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
24284 	    0x00, 0x00, 0x00};
24285 
24286 	/*
24287 	 * Extract low order 32 bits from IPv6 multicast address.
24288 	 * Or that into the link layer address, starting from the
24289 	 * second byte.
24290 	 */
24291 	*hw_start = 2;
24292 	v6_extract_mask->s6_addr32[0] = 0;
24293 	v6_extract_mask->s6_addr32[1] = 0;
24294 	v6_extract_mask->s6_addr32[2] = 0;
24295 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
24296 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
24297 	return (B_TRUE);
24298 }
24299 
24300 /*
24301  * Indicate by return value whether multicast is supported. If not,
24302  * this code should not touch/change any parameters.
24303  */
24304 /* ARGSUSED */
24305 static boolean_t
24306 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
24307     uint32_t *hw_start, ipaddr_t *extract_mask)
24308 {
24309 	/*
24310 	 * Multicast address mappings used over Ethernet/802.X.
24311 	 * This address is used as a base for mappings.
24312 	 */
24313 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
24314 	    0x00, 0x00, 0x00 };
24315 
24316 	if (phys_length != ETHERADDRL)
24317 		return (B_FALSE);
24318 
24319 	*extract_mask = htonl(0x007fffff);
24320 	*hw_start = 2;
24321 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
24322 	return (B_TRUE);
24323 }
24324 
24325 /*
24326  * Derive IPoIB interface id from the link layer address.
24327  */
24328 static boolean_t
24329 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
24330 {
24331 	char		*addr;
24332 
24333 	if (phys_length != 20)
24334 		return (B_FALSE);
24335 	addr = (char *)&v6addr->s6_addr32[2];
24336 	bcopy(phys_addr + 12, addr, 8);
24337 	/*
24338 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
24339 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
24340 	 * rules. In these cases, the IBA considers these GUIDs to be in
24341 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
24342 	 * required; vendors are required not to assign global EUI-64's
24343 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
24344 	 * of the interface identifier. Whether the GUID is in modified
24345 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
24346 	 * bit set to 1.
24347 	 */
24348 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
24349 	return (B_TRUE);
24350 }
24351 
24352 /*
24353  * Note on mapping from multicast IP addresses to IPoIB multicast link
24354  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
24355  * The format of an IPoIB multicast address is:
24356  *
24357  *  4 byte QPN      Scope Sign.  Pkey
24358  * +--------------------------------------------+
24359  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
24360  * +--------------------------------------------+
24361  *
24362  * The Scope and Pkey components are properties of the IBA port and
24363  * network interface. They can be ascertained from the broadcast address.
24364  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
24365  */
24366 
24367 static boolean_t
24368 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
24369     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
24370 {
24371 	/*
24372 	 * Base IPoIB IPv6 multicast address used for mappings.
24373 	 * Does not contain the IBA scope/Pkey values.
24374 	 */
24375 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
24376 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
24377 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24378 
24379 	/*
24380 	 * Extract low order 80 bits from IPv6 multicast address.
24381 	 * Or that into the link layer address, starting from the
24382 	 * sixth byte.
24383 	 */
24384 	*hw_start = 6;
24385 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
24386 
24387 	/*
24388 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24389 	 */
24390 	*(maddr + 5) = *(bphys_addr + 5);
24391 	*(maddr + 8) = *(bphys_addr + 8);
24392 	*(maddr + 9) = *(bphys_addr + 9);
24393 
24394 	v6_extract_mask->s6_addr32[0] = 0;
24395 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
24396 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
24397 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
24398 	return (B_TRUE);
24399 }
24400 
24401 static boolean_t
24402 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
24403     uint32_t *hw_start, ipaddr_t *extract_mask)
24404 {
24405 	/*
24406 	 * Base IPoIB IPv4 multicast address used for mappings.
24407 	 * Does not contain the IBA scope/Pkey values.
24408 	 */
24409 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
24410 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
24411 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24412 
24413 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
24414 		return (B_FALSE);
24415 
24416 	/*
24417 	 * Extract low order 28 bits from IPv4 multicast address.
24418 	 * Or that into the link layer address, starting from the
24419 	 * sixteenth byte.
24420 	 */
24421 	*extract_mask = htonl(0x0fffffff);
24422 	*hw_start = 16;
24423 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
24424 
24425 	/*
24426 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24427 	 */
24428 	*(maddr + 5) = *(bphys_addr + 5);
24429 	*(maddr + 8) = *(bphys_addr + 8);
24430 	*(maddr + 9) = *(bphys_addr + 9);
24431 	return (B_TRUE);
24432 }
24433 
24434 /*
24435  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
24436  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
24437  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
24438  * the link-local address is preferred.
24439  */
24440 boolean_t
24441 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24442 {
24443 	ipif_t	*ipif;
24444 	ipif_t	*maybe_ipif = NULL;
24445 
24446 	mutex_enter(&ill->ill_lock);
24447 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24448 		mutex_exit(&ill->ill_lock);
24449 		if (ipifp != NULL)
24450 			*ipifp = NULL;
24451 		return (B_FALSE);
24452 	}
24453 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24454 		if (!IPIF_CAN_LOOKUP(ipif))
24455 			continue;
24456 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
24457 		    ipif->ipif_zoneid != ALL_ZONES)
24458 			continue;
24459 		if ((ipif->ipif_flags & flags) != flags)
24460 			continue;
24461 
24462 		if (ipifp == NULL) {
24463 			mutex_exit(&ill->ill_lock);
24464 			ASSERT(maybe_ipif == NULL);
24465 			return (B_TRUE);
24466 		}
24467 		if (!ill->ill_isv6 ||
24468 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
24469 			ipif_refhold_locked(ipif);
24470 			mutex_exit(&ill->ill_lock);
24471 			*ipifp = ipif;
24472 			return (B_TRUE);
24473 		}
24474 		if (maybe_ipif == NULL)
24475 			maybe_ipif = ipif;
24476 	}
24477 	if (ipifp != NULL) {
24478 		if (maybe_ipif != NULL)
24479 			ipif_refhold_locked(maybe_ipif);
24480 		*ipifp = maybe_ipif;
24481 	}
24482 	mutex_exit(&ill->ill_lock);
24483 	return (maybe_ipif != NULL);
24484 }
24485 
24486 /*
24487  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
24488  */
24489 boolean_t
24490 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24491 {
24492 	ill_t *illg;
24493 	ip_stack_t	*ipst = ill->ill_ipst;
24494 
24495 	/*
24496 	 * We look at the passed-in ill first without grabbing ill_g_lock.
24497 	 */
24498 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
24499 		return (B_TRUE);
24500 	}
24501 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
24502 	if (ill->ill_group == NULL) {
24503 		/* ill not in a group */
24504 		rw_exit(&ipst->ips_ill_g_lock);
24505 		return (B_FALSE);
24506 	}
24507 
24508 	/*
24509 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24510 	 * group. We need to look for an ipif in the zone on all the ills in the
24511 	 * group.
24512 	 */
24513 	illg = ill->ill_group->illgrp_ill;
24514 	do {
24515 		/*
24516 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24517 		 * that it's not there.
24518 		 */
24519 		if (illg != ill &&
24520 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24521 			break;
24522 		}
24523 	} while ((illg = illg->ill_group_next) != NULL);
24524 	rw_exit(&ipst->ips_ill_g_lock);
24525 	return (illg != NULL);
24526 }
24527 
24528 /*
24529  * Check if this ill is only being used to send ICMP probes for IPMP
24530  */
24531 boolean_t
24532 ill_is_probeonly(ill_t *ill)
24533 {
24534 	/*
24535 	 * Check if the interface is FAILED, or INACTIVE
24536 	 */
24537 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24538 		return (B_TRUE);
24539 
24540 	return (B_FALSE);
24541 }
24542 
24543 /*
24544  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24545  * If a pointer to an ipif_t is returned then the caller will need to do
24546  * an ill_refrele().
24547  *
24548  * If there is no real interface which matches the ifindex, then it looks
24549  * for a group that has a matching index. In the case of a group match the
24550  * lifidx must be zero. We don't need emulate the logical interfaces
24551  * since IP Filter's use of netinfo doesn't use that.
24552  */
24553 ipif_t *
24554 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24555     ip_stack_t *ipst)
24556 {
24557 	ipif_t *ipif;
24558 	ill_t *ill;
24559 
24560 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24561 	    ipst);
24562 
24563 	if (ill == NULL) {
24564 		/* Fallback to group names only if hook_emulation set */
24565 		if (!ipst->ips_ipmp_hook_emulation)
24566 			return (NULL);
24567 
24568 		if (lifidx != 0)
24569 			return (NULL);
24570 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24571 		if (ill == NULL)
24572 			return (NULL);
24573 	}
24574 
24575 	mutex_enter(&ill->ill_lock);
24576 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24577 		mutex_exit(&ill->ill_lock);
24578 		ill_refrele(ill);
24579 		return (NULL);
24580 	}
24581 
24582 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24583 		if (!IPIF_CAN_LOOKUP(ipif))
24584 			continue;
24585 		if (lifidx == ipif->ipif_id) {
24586 			ipif_refhold_locked(ipif);
24587 			break;
24588 		}
24589 	}
24590 
24591 	mutex_exit(&ill->ill_lock);
24592 	ill_refrele(ill);
24593 	return (ipif);
24594 }
24595 
24596 /*
24597  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24598  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24599  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24600  * for details.
24601  */
24602 void
24603 ill_fastpath_flush(ill_t *ill)
24604 {
24605 	ip_stack_t *ipst = ill->ill_ipst;
24606 
24607 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24608 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24609 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24610 }
24611 
24612 /*
24613  * Set the physical address information for `ill' to the contents of the
24614  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24615  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24616  * EINPROGRESS will be returned.
24617  */
24618 int
24619 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24620 {
24621 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24622 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24623 
24624 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24625 
24626 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24627 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24628 		/* Changing DL_IPV6_TOKEN is not yet supported */
24629 		return (0);
24630 	}
24631 
24632 	/*
24633 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24634 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24635 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24636 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24637 	 */
24638 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24639 		freemsg(mp);
24640 		return (ENOMEM);
24641 	}
24642 
24643 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24644 
24645 	/*
24646 	 * If we can quiesce the ill, then set the address.  If not, then
24647 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24648 	 */
24649 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24650 	mutex_enter(&ill->ill_lock);
24651 	if (!ill_is_quiescent(ill)) {
24652 		/* call cannot fail since `conn_t *' argument is NULL */
24653 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24654 		    mp, ILL_DOWN);
24655 		mutex_exit(&ill->ill_lock);
24656 		return (EINPROGRESS);
24657 	}
24658 	mutex_exit(&ill->ill_lock);
24659 
24660 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24661 	return (0);
24662 }
24663 
24664 /*
24665  * Once the ill associated with `q' has quiesced, set its physical address
24666  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24667  * are passed (linked by b_cont), since we sometimes need to save two distinct
24668  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24669  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24670  * is quiesced, we know any stale IREs with the old address information have
24671  * already been removed, so we don't need to call ill_fastpath_flush().
24672  */
24673 /* ARGSUSED */
24674 static void
24675 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24676 {
24677 	ill_t		*ill = q->q_ptr;
24678 	mblk_t		*addrmp2 = unlinkb(addrmp);
24679 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24680 	uint_t		addrlen, addroff;
24681 
24682 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24683 
24684 	addroff	= dlindp->dl_addr_offset;
24685 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24686 
24687 	switch (dlindp->dl_data) {
24688 	case DL_IPV6_LINK_LAYER_ADDR:
24689 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24690 		freemsg(addrmp2);
24691 		break;
24692 
24693 	case DL_CURR_PHYS_ADDR:
24694 		freemsg(ill->ill_phys_addr_mp);
24695 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24696 		ill->ill_phys_addr_mp = addrmp;
24697 		ill->ill_phys_addr_length = addrlen;
24698 
24699 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24700 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24701 		else
24702 			freemsg(addrmp2);
24703 		break;
24704 	default:
24705 		ASSERT(0);
24706 	}
24707 
24708 	/*
24709 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24710 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24711 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24712 	 * brought up.
24713 	 */
24714 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24715 		ipsq_current_finish(ipsq);
24716 }
24717 
24718 /*
24719  * Helper routine for setting the ill_nd_lla fields.
24720  */
24721 void
24722 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24723 {
24724 	freemsg(ill->ill_nd_lla_mp);
24725 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24726 	ill->ill_nd_lla_mp = ndmp;
24727 	ill->ill_nd_lla_len = addrlen;
24728 }
24729 
24730 major_t IP_MAJ;
24731 #define	IP	"ip"
24732 
24733 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24734 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24735 
24736 /*
24737  * Issue REMOVEIF ioctls to have the loopback interfaces
24738  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24739  * the former going away when the user-level processes in the zone
24740  * are killed  * and the latter are cleaned up by the stream head
24741  * str_stack_shutdown callback that undoes all I_PLINKs.
24742  */
24743 void
24744 ip_loopback_cleanup(ip_stack_t *ipst)
24745 {
24746 	int error;
24747 	ldi_handle_t	lh = NULL;
24748 	ldi_ident_t	li = NULL;
24749 	int		rval;
24750 	cred_t		*cr;
24751 	struct strioctl iocb;
24752 	struct lifreq	lifreq;
24753 
24754 	IP_MAJ = ddi_name_to_major(IP);
24755 
24756 #ifdef NS_DEBUG
24757 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24758 	    ipst->ips_netstack->netstack_stackid);
24759 #endif
24760 
24761 	bzero(&lifreq, sizeof (lifreq));
24762 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24763 
24764 	error = ldi_ident_from_major(IP_MAJ, &li);
24765 	if (error) {
24766 #ifdef DEBUG
24767 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24768 		    error);
24769 #endif
24770 		return;
24771 	}
24772 
24773 	cr = zone_get_kcred(netstackid_to_zoneid(
24774 	    ipst->ips_netstack->netstack_stackid));
24775 	ASSERT(cr != NULL);
24776 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24777 	if (error) {
24778 #ifdef DEBUG
24779 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24780 		    error);
24781 #endif
24782 		goto out;
24783 	}
24784 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24785 	iocb.ic_timout = 15;
24786 	iocb.ic_len = sizeof (lifreq);
24787 	iocb.ic_dp = (char *)&lifreq;
24788 
24789 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24790 	/* LINTED - statement has no consequent */
24791 	if (error) {
24792 #ifdef NS_DEBUG
24793 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24794 		    "UDP6 error %d\n", error);
24795 #endif
24796 	}
24797 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24798 	lh = NULL;
24799 
24800 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24801 	if (error) {
24802 #ifdef NS_DEBUG
24803 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24804 		    error);
24805 #endif
24806 		goto out;
24807 	}
24808 
24809 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24810 	iocb.ic_timout = 15;
24811 	iocb.ic_len = sizeof (lifreq);
24812 	iocb.ic_dp = (char *)&lifreq;
24813 
24814 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24815 	/* LINTED - statement has no consequent */
24816 	if (error) {
24817 #ifdef NS_DEBUG
24818 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24819 		    "UDP error %d\n", error);
24820 #endif
24821 	}
24822 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24823 	lh = NULL;
24824 
24825 out:
24826 	/* Close layered handles */
24827 	if (lh)
24828 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24829 	if (li)
24830 		ldi_ident_release(li);
24831 
24832 	crfree(cr);
24833 }
24834