xref: /titanic_51/usr/src/uts/common/inet/ip/ip_if.c (revision f875b4ebb1dd9fdbeb043557cab38ab3bf7f6e01)
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 ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
150 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
151 static void	ipsq_flush(ill_t *ill);
152 
153 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
154     queue_t *q, mblk_t *mp, boolean_t need_up);
155 static void	ipsq_delete(ipsq_t *);
156 
157 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
158 		    boolean_t initialize);
159 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
160 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
161 		    boolean_t isv6);
162 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
163 static void	ipif_delete_cache_ire(ire_t *, char *);
164 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
165 static void	ipif_free(ipif_t *ipif);
166 static void	ipif_free_tail(ipif_t *ipif);
167 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
168 static void	ipif_multicast_down(ipif_t *ipif);
169 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
170 static void	ipif_set_default(ipif_t *ipif);
171 static int	ipif_set_values(queue_t *q, mblk_t *mp,
172     char *interf_name, uint_t *ppa);
173 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
174     queue_t *q);
175 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
176     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
177     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
178 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
179 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
180 
181 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
182 static int	ill_arp_off(ill_t *ill);
183 static int	ill_arp_on(ill_t *ill);
184 static void	ill_delete_interface_type(ill_if_t *);
185 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
186 static void	ill_dl_down(ill_t *ill);
187 static void	ill_down(ill_t *ill);
188 static void	ill_downi(ire_t *ire, char *ill_arg);
189 static void	ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg);
190 static void	ill_down_tail(ill_t *ill);
191 static void	ill_free_mib(ill_t *ill);
192 static void	ill_glist_delete(ill_t *);
193 static boolean_t ill_has_usable_ipif(ill_t *);
194 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
195 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
196 static void	ill_phyint_free(ill_t *ill);
197 static void	ill_phyint_reinit(ill_t *ill);
198 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
199 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
200 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
201 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
202 static void	ill_stq_cache_delete(ire_t *, char *);
203 
204 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
205 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
206 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
207     in6_addr_t *);
208 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
209     ipaddr_t *);
210 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
211 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
212     in6_addr_t *);
213 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
214     ipaddr_t *);
215 
216 static void	ipif_save_ire(ipif_t *, ire_t *);
217 static void	ipif_remove_ire(ipif_t *, ire_t *);
218 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
219 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
220 
221 /*
222  * Per-ill IPsec capabilities management.
223  */
224 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
225 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
226 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
227 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
228 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
229 static void ill_capability_proto(ill_t *, int, mblk_t *);
230 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
231     boolean_t);
232 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
233 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
234 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
235 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
236 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
237 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
238 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
239 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
240     dl_capability_sub_t *);
241 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
242 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
243 static void ill_capability_lso_reset(ill_t *, mblk_t **);
244 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
245 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
246 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
247 static void	ill_capability_dls_disable(ill_t *);
248 
249 static void	illgrp_cache_delete(ire_t *, char *);
250 static void	illgrp_delete(ill_t *ill);
251 static void	illgrp_reset_schednext(ill_t *ill);
252 
253 static ill_t	*ill_prev_usesrc(ill_t *);
254 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
255 static void	ill_disband_usesrc_group(ill_t *);
256 
257 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
258 
259 /*
260  * if we go over the memory footprint limit more than once in this msec
261  * interval, we'll start pruning aggressively.
262  */
263 int ip_min_frag_prune_time = 0;
264 
265 /*
266  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
267  * and the IPsec DOI
268  */
269 #define	MAX_IPSEC_ALGS	256
270 
271 #define	BITSPERBYTE	8
272 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
273 
274 #define	IPSEC_ALG_ENABLE(algs, algid) \
275 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
276 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
277 
278 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
279 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
280 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
281 
282 typedef uint8_t ipsec_capab_elem_t;
283 
284 /*
285  * Per-algorithm parameters.  Note that at present, only encryption
286  * algorithms have variable keysize (IKE does not provide a way to negotiate
287  * auth algorithm keysize).
288  *
289  * All sizes here are in bits.
290  */
291 typedef struct
292 {
293 	uint16_t	minkeylen;
294 	uint16_t	maxkeylen;
295 } ipsec_capab_algparm_t;
296 
297 /*
298  * Per-ill capabilities.
299  */
300 struct ill_ipsec_capab_s {
301 	ipsec_capab_elem_t *encr_hw_algs;
302 	ipsec_capab_elem_t *auth_hw_algs;
303 	uint32_t algs_size;	/* size of _hw_algs in bytes */
304 	/* algorithm key lengths */
305 	ipsec_capab_algparm_t *encr_algparm;
306 	uint32_t encr_algparm_size;
307 	uint32_t encr_algparm_end;
308 };
309 
310 /*
311  * The field values are larger than strictly necessary for simple
312  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
313  */
314 static area_t	ip_area_template = {
315 	AR_ENTRY_ADD,			/* area_cmd */
316 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
317 					/* area_name_offset */
318 	/* area_name_length temporarily holds this structure length */
319 	sizeof (area_t),			/* area_name_length */
320 	IP_ARP_PROTO_TYPE,		/* area_proto */
321 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
322 	IP_ADDR_LEN,			/* area_proto_addr_length */
323 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
324 					/* area_proto_mask_offset */
325 	0,				/* area_flags */
326 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
327 					/* area_hw_addr_offset */
328 	/* Zero length hw_addr_length means 'use your idea of the address' */
329 	0				/* area_hw_addr_length */
330 };
331 
332 /*
333  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
334  * support
335  */
336 static area_t	ip6_area_template = {
337 	AR_ENTRY_ADD,			/* area_cmd */
338 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
339 					/* area_name_offset */
340 	/* area_name_length temporarily holds this structure length */
341 	sizeof (area_t),			/* area_name_length */
342 	IP_ARP_PROTO_TYPE,		/* area_proto */
343 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
344 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
345 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
346 					/* area_proto_mask_offset */
347 	0,				/* area_flags */
348 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
349 					/* area_hw_addr_offset */
350 	/* Zero length hw_addr_length means 'use your idea of the address' */
351 	0				/* area_hw_addr_length */
352 };
353 
354 static ared_t	ip_ared_template = {
355 	AR_ENTRY_DELETE,
356 	sizeof (ared_t) + IP_ADDR_LEN,
357 	sizeof (ared_t),
358 	IP_ARP_PROTO_TYPE,
359 	sizeof (ared_t),
360 	IP_ADDR_LEN
361 };
362 
363 static ared_t	ip6_ared_template = {
364 	AR_ENTRY_DELETE,
365 	sizeof (ared_t) + IPV6_ADDR_LEN,
366 	sizeof (ared_t),
367 	IP_ARP_PROTO_TYPE,
368 	sizeof (ared_t),
369 	IPV6_ADDR_LEN
370 };
371 
372 /*
373  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
374  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
375  * areq is used).
376  */
377 static areq_t	ip_areq_template = {
378 	AR_ENTRY_QUERY,			/* cmd */
379 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
380 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
381 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
382 	sizeof (areq_t),			/* target addr offset */
383 	IP_ADDR_LEN,			/* target addr_length */
384 	0,				/* flags */
385 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
386 	IP_ADDR_LEN,			/* sender addr length */
387 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
388 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
389 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
390 	/* anything else filled in by the code */
391 };
392 
393 static arc_t	ip_aru_template = {
394 	AR_INTERFACE_UP,
395 	sizeof (arc_t),		/* Name offset */
396 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
397 };
398 
399 static arc_t	ip_ard_template = {
400 	AR_INTERFACE_DOWN,
401 	sizeof (arc_t),		/* Name offset */
402 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
403 };
404 
405 static arc_t	ip_aron_template = {
406 	AR_INTERFACE_ON,
407 	sizeof (arc_t),		/* Name offset */
408 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
409 };
410 
411 static arc_t	ip_aroff_template = {
412 	AR_INTERFACE_OFF,
413 	sizeof (arc_t),		/* Name offset */
414 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
415 };
416 
417 
418 static arma_t	ip_arma_multi_template = {
419 	AR_MAPPING_ADD,
420 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
421 				/* Name offset */
422 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
423 	IP_ARP_PROTO_TYPE,
424 	sizeof (arma_t),			/* proto_addr_offset */
425 	IP_ADDR_LEN,				/* proto_addr_length */
426 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
427 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
428 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
429 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
430 	IP_MAX_HW_LEN,				/* hw_addr_length */
431 	0,					/* hw_mapping_start */
432 };
433 
434 static ipft_t	ip_ioctl_ftbl[] = {
435 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
436 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
437 		IPFT_F_NO_REPLY },
438 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
439 		IPFT_F_NO_REPLY },
440 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
441 	{ 0 }
442 };
443 
444 /* Simple ICMP IP Header Template */
445 static ipha_t icmp_ipha = {
446 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
447 };
448 
449 /* Flag descriptors for ip_ipif_report */
450 static nv_t	ipif_nv_tbl[] = {
451 	{ IPIF_UP,		"UP" },
452 	{ IPIF_BROADCAST,	"BROADCAST" },
453 	{ ILLF_DEBUG,		"DEBUG" },
454 	{ PHYI_LOOPBACK,	"LOOPBACK" },
455 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
456 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
457 	{ PHYI_RUNNING,		"RUNNING" },
458 	{ ILLF_NOARP,		"NOARP" },
459 	{ PHYI_PROMISC,		"PROMISC" },
460 	{ PHYI_ALLMULTI,	"ALLMULTI" },
461 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
462 	{ ILLF_MULTICAST,	"MULTICAST" },
463 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
464 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
465 	{ IPIF_DHCPRUNNING,	"DHCP" },
466 	{ IPIF_PRIVATE,		"PRIVATE" },
467 	{ IPIF_NOXMIT,		"NOXMIT" },
468 	{ IPIF_NOLOCAL,		"NOLOCAL" },
469 	{ IPIF_DEPRECATED,	"DEPRECATED" },
470 	{ IPIF_PREFERRED,	"PREFERRED" },
471 	{ IPIF_TEMPORARY,	"TEMPORARY" },
472 	{ IPIF_ADDRCONF,	"ADDRCONF" },
473 	{ PHYI_VIRTUAL,		"VIRTUAL" },
474 	{ ILLF_ROUTER,		"ROUTER" },
475 	{ ILLF_NONUD,		"NONUD" },
476 	{ IPIF_ANYCAST,		"ANYCAST" },
477 	{ ILLF_NORTEXCH,	"NORTEXCH" },
478 	{ ILLF_IPV4,		"IPV4" },
479 	{ ILLF_IPV6,		"IPV6" },
480 	{ IPIF_MIPRUNNING,	"MIP" },
481 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
482 	{ PHYI_FAILED,		"FAILED" },
483 	{ PHYI_STANDBY,		"STANDBY" },
484 	{ PHYI_INACTIVE,	"INACTIVE" },
485 	{ PHYI_OFFLINE,		"OFFLINE" },
486 };
487 
488 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
489 
490 static ip_m_t	ip_m_tbl[] = {
491 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
492 	    ip_ether_v6intfid },
493 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
494 	    ip_nodef_v6intfid },
495 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
496 	    ip_nodef_v6intfid },
497 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
498 	    ip_nodef_v6intfid },
499 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
500 	    ip_ether_v6intfid },
501 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
502 	    ip_ib_v6intfid },
503 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
504 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
505 	    ip_nodef_v6intfid }
506 };
507 
508 static ill_t	ill_null;		/* Empty ILL for init. */
509 char	ipif_loopback_name[] = "lo0";
510 static char *ipv4_forward_suffix = ":ip_forwarding";
511 static char *ipv6_forward_suffix = ":ip6_forwarding";
512 static	sin6_t	sin6_null;	/* Zero address for quick clears */
513 static	sin_t	sin_null;	/* Zero address for quick clears */
514 
515 /* When set search for unused ipif_seqid */
516 static ipif_t	ipif_zero;
517 
518 /*
519  * ppa arena is created after these many
520  * interfaces have been plumbed.
521  */
522 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
523 
524 /*
525  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
526  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
527  * set through platform specific code (Niagara/Ontario).
528  */
529 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
530 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
531 
532 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
533 
534 static uint_t
535 ipif_rand(ip_stack_t *ipst)
536 {
537 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
538 	    12345;
539 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
540 }
541 
542 /*
543  * Allocate per-interface mibs.
544  * Returns true if ok. False otherwise.
545  *  ipsq  may not yet be allocated (loopback case ).
546  */
547 static boolean_t
548 ill_allocate_mibs(ill_t *ill)
549 {
550 	/* Already allocated? */
551 	if (ill->ill_ip_mib != NULL) {
552 		if (ill->ill_isv6)
553 			ASSERT(ill->ill_icmp6_mib != NULL);
554 		return (B_TRUE);
555 	}
556 
557 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
558 	    KM_NOSLEEP);
559 	if (ill->ill_ip_mib == NULL) {
560 		return (B_FALSE);
561 	}
562 
563 	/* Setup static information */
564 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
565 	    sizeof (mib2_ipIfStatsEntry_t));
566 	if (ill->ill_isv6) {
567 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
568 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
569 		    sizeof (mib2_ipv6AddrEntry_t));
570 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
571 		    sizeof (mib2_ipv6RouteEntry_t));
572 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
573 		    sizeof (mib2_ipv6NetToMediaEntry_t));
574 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
575 		    sizeof (ipv6_member_t));
576 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
577 		    sizeof (ipv6_grpsrc_t));
578 	} else {
579 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
580 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
581 		    sizeof (mib2_ipAddrEntry_t));
582 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
583 		    sizeof (mib2_ipRouteEntry_t));
584 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
585 		    sizeof (mib2_ipNetToMediaEntry_t));
586 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
587 		    sizeof (ip_member_t));
588 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
589 		    sizeof (ip_grpsrc_t));
590 
591 		/*
592 		 * For a v4 ill, we are done at this point, because per ill
593 		 * icmp mibs are only used for v6.
594 		 */
595 		return (B_TRUE);
596 	}
597 
598 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
599 	    KM_NOSLEEP);
600 	if (ill->ill_icmp6_mib == NULL) {
601 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
602 		ill->ill_ip_mib = NULL;
603 		return (B_FALSE);
604 	}
605 	/* static icmp info */
606 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
607 	    sizeof (mib2_ipv6IfIcmpEntry_t);
608 	/*
609 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
610 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
611 	 * -> ill_phyint_reinit
612 	 */
613 	return (B_TRUE);
614 }
615 
616 /*
617  * Common code for preparation of ARP commands.  Two points to remember:
618  * 	1) The ill_name is tacked on at the end of the allocated space so
619  *	   the templates name_offset field must contain the total space
620  *	   to allocate less the name length.
621  *
622  *	2) The templates name_length field should contain the *template*
623  *	   length.  We use it as a parameter to bcopy() and then write
624  *	   the real ill_name_length into the name_length field of the copy.
625  * (Always called as writer.)
626  */
627 mblk_t *
628 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
629 {
630 	arc_t	*arc = (arc_t *)template;
631 	char	*cp;
632 	int	len;
633 	mblk_t	*mp;
634 	uint_t	name_length = ill->ill_name_length;
635 	uint_t	template_len = arc->arc_name_length;
636 
637 	len = arc->arc_name_offset + name_length;
638 	mp = allocb(len, BPRI_HI);
639 	if (mp == NULL)
640 		return (NULL);
641 	cp = (char *)mp->b_rptr;
642 	mp->b_wptr = (uchar_t *)&cp[len];
643 	if (template_len)
644 		bcopy(template, cp, template_len);
645 	if (len > template_len)
646 		bzero(&cp[template_len], len - template_len);
647 	mp->b_datap->db_type = M_PROTO;
648 
649 	arc = (arc_t *)cp;
650 	arc->arc_name_length = name_length;
651 	cp = (char *)arc + arc->arc_name_offset;
652 	bcopy(ill->ill_name, cp, name_length);
653 
654 	if (addr) {
655 		area_t	*area = (area_t *)mp->b_rptr;
656 
657 		cp = (char *)area + area->area_proto_addr_offset;
658 		bcopy(addr, cp, area->area_proto_addr_length);
659 		if (area->area_cmd == AR_ENTRY_ADD) {
660 			cp = (char *)area;
661 			len = area->area_proto_addr_length;
662 			if (area->area_proto_mask_offset)
663 				cp += area->area_proto_mask_offset;
664 			else
665 				cp += area->area_proto_addr_offset + len;
666 			while (len-- > 0)
667 				*cp++ = (char)~0;
668 		}
669 	}
670 	return (mp);
671 }
672 
673 mblk_t *
674 ipif_area_alloc(ipif_t *ipif)
675 {
676 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
677 	    (char *)&ipif->ipif_lcl_addr));
678 }
679 
680 mblk_t *
681 ipif_ared_alloc(ipif_t *ipif)
682 {
683 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
684 	    (char *)&ipif->ipif_lcl_addr));
685 }
686 
687 mblk_t *
688 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
689 {
690 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
691 	    (char *)&addr));
692 }
693 
694 /*
695  * Completely vaporize a lower level tap and all associated interfaces.
696  * ill_delete is called only out of ip_close when the device control
697  * stream is being closed.
698  */
699 void
700 ill_delete(ill_t *ill)
701 {
702 	ipif_t	*ipif;
703 	ill_t	*prev_ill;
704 	ip_stack_t	*ipst = ill->ill_ipst;
705 
706 	/*
707 	 * ill_delete may be forcibly entering the ipsq. The previous
708 	 * ioctl may not have completed and may need to be aborted.
709 	 * ipsq_flush takes care of it. If we don't need to enter the
710 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
711 	 * ill_delete_tail is sufficient.
712 	 */
713 	ipsq_flush(ill);
714 
715 	/*
716 	 * Nuke all interfaces.  ipif_free will take down the interface,
717 	 * remove it from the list, and free the data structure.
718 	 * Walk down the ipif list and remove the logical interfaces
719 	 * first before removing the main ipif. We can't unplumb
720 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
721 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
722 	 * POINTOPOINT.
723 	 *
724 	 * If ill_ipif was not properly initialized (i.e low on memory),
725 	 * then no interfaces to clean up. In this case just clean up the
726 	 * ill.
727 	 */
728 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
729 		ipif_free(ipif);
730 
731 	/*
732 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
733 	 * So nobody can be using this mp now. Free the mp allocated for
734 	 * honoring ILLF_NOARP
735 	 */
736 	freemsg(ill->ill_arp_on_mp);
737 	ill->ill_arp_on_mp = NULL;
738 
739 	/* Clean up msgs on pending upcalls for mrouted */
740 	reset_mrt_ill(ill);
741 
742 	/*
743 	 * ipif_free -> reset_conn_ipif will remove all multicast
744 	 * references for IPv4. For IPv6, we need to do it here as
745 	 * it points only at ills.
746 	 */
747 	reset_conn_ill(ill);
748 
749 	/*
750 	 * ill_down will arrange to blow off any IRE's dependent on this
751 	 * ILL, and shut down fragmentation reassembly.
752 	 */
753 	ill_down(ill);
754 
755 	/* Let SCTP know, so that it can remove this from its list. */
756 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
757 
758 	/*
759 	 * If an address on this ILL is being used as a source address then
760 	 * clear out the pointers in other ILLs that point to this ILL.
761 	 */
762 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
763 	if (ill->ill_usesrc_grp_next != NULL) {
764 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
765 			ill_disband_usesrc_group(ill);
766 		} else {	/* consumer of the usesrc ILL */
767 			prev_ill = ill_prev_usesrc(ill);
768 			prev_ill->ill_usesrc_grp_next =
769 			    ill->ill_usesrc_grp_next;
770 		}
771 	}
772 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
773 }
774 
775 static void
776 ipif_non_duplicate(ipif_t *ipif)
777 {
778 	ill_t *ill = ipif->ipif_ill;
779 	mutex_enter(&ill->ill_lock);
780 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
781 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
782 		ASSERT(ill->ill_ipif_dup_count > 0);
783 		ill->ill_ipif_dup_count--;
784 	}
785 	mutex_exit(&ill->ill_lock);
786 }
787 
788 /*
789  * ill_delete_tail is called from ip_modclose after all references
790  * to the closing ill are gone. The wait is done in ip_modclose
791  */
792 void
793 ill_delete_tail(ill_t *ill)
794 {
795 	mblk_t	**mpp;
796 	ipif_t	*ipif;
797 	ip_stack_t	*ipst = ill->ill_ipst;
798 
799 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
800 		ipif_non_duplicate(ipif);
801 		ipif_down_tail(ipif);
802 	}
803 
804 	ASSERT(ill->ill_ipif_dup_count == 0 &&
805 	    ill->ill_arp_down_mp == NULL &&
806 	    ill->ill_arp_del_mapping_mp == NULL);
807 
808 	/*
809 	 * If polling capability is enabled (which signifies direct
810 	 * upcall into IP and driver has ill saved as a handle),
811 	 * we need to make sure that unbind has completed before we
812 	 * let the ill disappear and driver no longer has any reference
813 	 * to this ill.
814 	 */
815 	mutex_enter(&ill->ill_lock);
816 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
817 		cv_wait(&ill->ill_cv, &ill->ill_lock);
818 	mutex_exit(&ill->ill_lock);
819 
820 	/*
821 	 * Clean up polling and soft ring capabilities
822 	 */
823 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
824 		ill_capability_dls_disable(ill);
825 
826 	if (ill->ill_net_type != IRE_LOOPBACK)
827 		qprocsoff(ill->ill_rq);
828 
829 	/*
830 	 * We do an ipsq_flush once again now. New messages could have
831 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
832 	 * could also have landed up if an ioctl thread had looked up
833 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
834 	 * enqueued the ioctl when we did the ipsq_flush last time.
835 	 */
836 	ipsq_flush(ill);
837 
838 	/*
839 	 * Free capabilities.
840 	 */
841 	if (ill->ill_ipsec_capab_ah != NULL) {
842 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
843 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
844 		ill->ill_ipsec_capab_ah = NULL;
845 	}
846 
847 	if (ill->ill_ipsec_capab_esp != NULL) {
848 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
849 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
850 		ill->ill_ipsec_capab_esp = NULL;
851 	}
852 
853 	if (ill->ill_mdt_capab != NULL) {
854 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
855 		ill->ill_mdt_capab = NULL;
856 	}
857 
858 	if (ill->ill_hcksum_capab != NULL) {
859 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
860 		ill->ill_hcksum_capab = NULL;
861 	}
862 
863 	if (ill->ill_zerocopy_capab != NULL) {
864 		kmem_free(ill->ill_zerocopy_capab,
865 		    sizeof (ill_zerocopy_capab_t));
866 		ill->ill_zerocopy_capab = NULL;
867 	}
868 
869 	if (ill->ill_lso_capab != NULL) {
870 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
871 		ill->ill_lso_capab = NULL;
872 	}
873 
874 	if (ill->ill_dls_capab != NULL) {
875 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
876 		ill->ill_dls_capab->ill_unbind_conn = NULL;
877 		kmem_free(ill->ill_dls_capab,
878 		    sizeof (ill_dls_capab_t) +
879 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
880 		ill->ill_dls_capab = NULL;
881 	}
882 
883 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
884 
885 	while (ill->ill_ipif != NULL)
886 		ipif_free_tail(ill->ill_ipif);
887 
888 	ill_down_tail(ill);
889 
890 	/*
891 	 * We have removed all references to ilm from conn and the ones joined
892 	 * within the kernel.
893 	 *
894 	 * We don't walk conns, mrts and ires because
895 	 *
896 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
897 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
898 	 *    ill references.
899 	 */
900 	ASSERT(ilm_walk_ill(ill) == 0);
901 	/*
902 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
903 	 * could free the phyint. No more reference to the phyint after this
904 	 * point.
905 	 */
906 	(void) ill_glist_delete(ill);
907 
908 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
909 	if (ill->ill_ndd_name != NULL)
910 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
911 	rw_exit(&ipst->ips_ip_g_nd_lock);
912 
913 
914 	if (ill->ill_frag_ptr != NULL) {
915 		uint_t count;
916 
917 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
918 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
919 		}
920 		mi_free(ill->ill_frag_ptr);
921 		ill->ill_frag_ptr = NULL;
922 		ill->ill_frag_hash_tbl = NULL;
923 	}
924 
925 	freemsg(ill->ill_nd_lla_mp);
926 	/* Free all retained control messages. */
927 	mpp = &ill->ill_first_mp_to_free;
928 	do {
929 		while (mpp[0]) {
930 			mblk_t  *mp;
931 			mblk_t  *mp1;
932 
933 			mp = mpp[0];
934 			mpp[0] = mp->b_next;
935 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
936 				mp1->b_next = NULL;
937 				mp1->b_prev = NULL;
938 			}
939 			freemsg(mp);
940 		}
941 	} while (mpp++ != &ill->ill_last_mp_to_free);
942 
943 	ill_free_mib(ill);
944 	/* Drop refcnt here */
945 	netstack_rele(ill->ill_ipst->ips_netstack);
946 	ill->ill_ipst = NULL;
947 
948 	ILL_TRACE_CLEANUP(ill);
949 }
950 
951 static void
952 ill_free_mib(ill_t *ill)
953 {
954 	ip_stack_t *ipst = ill->ill_ipst;
955 
956 	/*
957 	 * MIB statistics must not be lost, so when an interface
958 	 * goes away the counter values will be added to the global
959 	 * MIBs.
960 	 */
961 	if (ill->ill_ip_mib != NULL) {
962 		if (ill->ill_isv6) {
963 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
964 			    ill->ill_ip_mib);
965 		} else {
966 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
967 			    ill->ill_ip_mib);
968 		}
969 
970 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
971 		ill->ill_ip_mib = NULL;
972 	}
973 	if (ill->ill_icmp6_mib != NULL) {
974 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
975 		    ill->ill_icmp6_mib);
976 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
977 		ill->ill_icmp6_mib = NULL;
978 	}
979 }
980 
981 /*
982  * Concatenate together a physical address and a sap.
983  *
984  * Sap_lengths are interpreted as follows:
985  *   sap_length == 0	==>	no sap
986  *   sap_length > 0	==>	sap is at the head of the dlpi address
987  *   sap_length < 0	==>	sap is at the tail of the dlpi address
988  */
989 static void
990 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
991     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
992 {
993 	uint16_t sap_addr = (uint16_t)sap_src;
994 
995 	if (sap_length == 0) {
996 		if (phys_src == NULL)
997 			bzero(dst, phys_length);
998 		else
999 			bcopy(phys_src, dst, phys_length);
1000 	} else if (sap_length < 0) {
1001 		if (phys_src == NULL)
1002 			bzero(dst, phys_length);
1003 		else
1004 			bcopy(phys_src, dst, phys_length);
1005 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1006 	} else {
1007 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1008 		if (phys_src == NULL)
1009 			bzero((char *)dst + sap_length, phys_length);
1010 		else
1011 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1012 	}
1013 }
1014 
1015 /*
1016  * Generate a dl_unitdata_req mblk for the device and address given.
1017  * addr_length is the length of the physical portion of the address.
1018  * If addr is NULL include an all zero address of the specified length.
1019  * TRUE? In any case, addr_length is taken to be the entire length of the
1020  * dlpi address, including the absolute value of sap_length.
1021  */
1022 mblk_t *
1023 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1024 		t_scalar_t sap_length)
1025 {
1026 	dl_unitdata_req_t *dlur;
1027 	mblk_t	*mp;
1028 	t_scalar_t	abs_sap_length;		/* absolute value */
1029 
1030 	abs_sap_length = ABS(sap_length);
1031 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1032 	    DL_UNITDATA_REQ);
1033 	if (mp == NULL)
1034 		return (NULL);
1035 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1036 	/* HACK: accomodate incompatible DLPI drivers */
1037 	if (addr_length == 8)
1038 		addr_length = 6;
1039 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1040 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1041 	dlur->dl_priority.dl_min = 0;
1042 	dlur->dl_priority.dl_max = 0;
1043 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1044 	    (uchar_t *)&dlur[1]);
1045 	return (mp);
1046 }
1047 
1048 /*
1049  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1050  * Return an error if we already have 1 or more ioctls in progress.
1051  * This is used only for non-exclusive ioctls. Currently this is used
1052  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1053  * and thus need to use ipsq_pending_mp_add.
1054  */
1055 boolean_t
1056 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1057 {
1058 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1059 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1060 	/*
1061 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1062 	 */
1063 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1064 	    (add_mp->b_datap->db_type == M_IOCTL));
1065 
1066 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1067 	/*
1068 	 * Return error if the conn has started closing. The conn
1069 	 * could have finished cleaning up the pending mp list,
1070 	 * If so we should not add another mp to the list negating
1071 	 * the cleanup.
1072 	 */
1073 	if (connp->conn_state_flags & CONN_CLOSING)
1074 		return (B_FALSE);
1075 	/*
1076 	 * Add the pending mp to the head of the list, chained by b_next.
1077 	 * Note down the conn on which the ioctl request came, in b_prev.
1078 	 * This will be used to later get the conn, when we get a response
1079 	 * on the ill queue, from some other module (typically arp)
1080 	 */
1081 	add_mp->b_next = (void *)ill->ill_pending_mp;
1082 	add_mp->b_queue = CONNP_TO_WQ(connp);
1083 	ill->ill_pending_mp = add_mp;
1084 	if (connp != NULL)
1085 		connp->conn_oper_pending_ill = ill;
1086 	return (B_TRUE);
1087 }
1088 
1089 /*
1090  * Retrieve the ill_pending_mp and return it. We have to walk the list
1091  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1092  */
1093 mblk_t *
1094 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1095 {
1096 	mblk_t	*prev = NULL;
1097 	mblk_t	*curr = NULL;
1098 	uint_t	id;
1099 	conn_t	*connp;
1100 
1101 	/*
1102 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1103 	 * up the pending mp, but it does not know the ioc_id and
1104 	 * passes in a zero for it.
1105 	 */
1106 	mutex_enter(&ill->ill_lock);
1107 	if (ioc_id != 0)
1108 		*connpp = NULL;
1109 
1110 	/* Search the list for the appropriate ioctl based on ioc_id */
1111 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1112 	    prev = curr, curr = curr->b_next) {
1113 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1114 		connp = Q_TO_CONN(curr->b_queue);
1115 		/* Match based on the ioc_id or based on the conn */
1116 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1117 			break;
1118 	}
1119 
1120 	if (curr != NULL) {
1121 		/* Unlink the mblk from the pending mp list */
1122 		if (prev != NULL) {
1123 			prev->b_next = curr->b_next;
1124 		} else {
1125 			ASSERT(ill->ill_pending_mp == curr);
1126 			ill->ill_pending_mp = curr->b_next;
1127 		}
1128 
1129 		/*
1130 		 * conn refcnt must have been bumped up at the start of
1131 		 * the ioctl. So we can safely access the conn.
1132 		 */
1133 		ASSERT(CONN_Q(curr->b_queue));
1134 		*connpp = Q_TO_CONN(curr->b_queue);
1135 		curr->b_next = NULL;
1136 		curr->b_queue = NULL;
1137 	}
1138 
1139 	mutex_exit(&ill->ill_lock);
1140 
1141 	return (curr);
1142 }
1143 
1144 /*
1145  * Add the pending mp to the list. There can be only 1 pending mp
1146  * in the list. Any exclusive ioctl that needs to wait for a response
1147  * from another module or driver needs to use this function to set
1148  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1149  * the other module/driver. This is also used while waiting for the
1150  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1151  */
1152 boolean_t
1153 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1154     int waitfor)
1155 {
1156 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1157 
1158 	ASSERT(IAM_WRITER_IPIF(ipif));
1159 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1160 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1161 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1162 	/*
1163 	 * The caller may be using a different ipif than the one passed into
1164 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1165 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1166 	 * that `ipsq_current_ipif == ipif'.
1167 	 */
1168 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1169 
1170 	/*
1171 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1172 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1173 	 */
1174 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1175 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1176 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1177 
1178 	if (connp != NULL) {
1179 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1180 		/*
1181 		 * Return error if the conn has started closing. The conn
1182 		 * could have finished cleaning up the pending mp list,
1183 		 * If so we should not add another mp to the list negating
1184 		 * the cleanup.
1185 		 */
1186 		if (connp->conn_state_flags & CONN_CLOSING)
1187 			return (B_FALSE);
1188 	}
1189 	mutex_enter(&ipsq->ipsq_lock);
1190 	ipsq->ipsq_pending_ipif = ipif;
1191 	/*
1192 	 * Note down the queue in b_queue. This will be returned by
1193 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1194 	 * the processing
1195 	 */
1196 	add_mp->b_next = NULL;
1197 	add_mp->b_queue = q;
1198 	ipsq->ipsq_pending_mp = add_mp;
1199 	ipsq->ipsq_waitfor = waitfor;
1200 
1201 	if (connp != NULL)
1202 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1203 	mutex_exit(&ipsq->ipsq_lock);
1204 	return (B_TRUE);
1205 }
1206 
1207 /*
1208  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1209  * queued in the list.
1210  */
1211 mblk_t *
1212 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1213 {
1214 	mblk_t	*curr = NULL;
1215 
1216 	mutex_enter(&ipsq->ipsq_lock);
1217 	*connpp = NULL;
1218 	if (ipsq->ipsq_pending_mp == NULL) {
1219 		mutex_exit(&ipsq->ipsq_lock);
1220 		return (NULL);
1221 	}
1222 
1223 	/* There can be only 1 such excl message */
1224 	curr = ipsq->ipsq_pending_mp;
1225 	ASSERT(curr != NULL && curr->b_next == NULL);
1226 	ipsq->ipsq_pending_ipif = NULL;
1227 	ipsq->ipsq_pending_mp = NULL;
1228 	ipsq->ipsq_waitfor = 0;
1229 	mutex_exit(&ipsq->ipsq_lock);
1230 
1231 	if (CONN_Q(curr->b_queue)) {
1232 		/*
1233 		 * This mp did a refhold on the conn, at the start of the ioctl.
1234 		 * So we can safely return a pointer to the conn to the caller.
1235 		 */
1236 		*connpp = Q_TO_CONN(curr->b_queue);
1237 	} else {
1238 		*connpp = NULL;
1239 	}
1240 	curr->b_next = NULL;
1241 	curr->b_prev = NULL;
1242 	return (curr);
1243 }
1244 
1245 /*
1246  * Cleanup the ioctl mp queued in ipsq_pending_mp
1247  * - Called in the ill_delete path
1248  * - Called in the M_ERROR or M_HANGUP path on the ill.
1249  * - Called in the conn close path.
1250  */
1251 boolean_t
1252 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1253 {
1254 	mblk_t	*mp;
1255 	ipsq_t	*ipsq;
1256 	queue_t	*q;
1257 	ipif_t	*ipif;
1258 
1259 	ASSERT(IAM_WRITER_ILL(ill));
1260 	ipsq = ill->ill_phyint->phyint_ipsq;
1261 	mutex_enter(&ipsq->ipsq_lock);
1262 	/*
1263 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1264 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1265 	 * even if it is meant for another ill, since we have to enqueue
1266 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1267 	 * If connp is non-null we are called from the conn close path.
1268 	 */
1269 	mp = ipsq->ipsq_pending_mp;
1270 	if (mp == NULL || (connp != NULL &&
1271 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1272 		mutex_exit(&ipsq->ipsq_lock);
1273 		return (B_FALSE);
1274 	}
1275 	/* Now remove from the ipsq_pending_mp */
1276 	ipsq->ipsq_pending_mp = NULL;
1277 	q = mp->b_queue;
1278 	mp->b_next = NULL;
1279 	mp->b_prev = NULL;
1280 	mp->b_queue = NULL;
1281 
1282 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1283 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1284 	if (ill->ill_move_in_progress) {
1285 		ILL_CLEAR_MOVE(ill);
1286 	} else if (ill->ill_up_ipifs) {
1287 		ill_group_cleanup(ill);
1288 	}
1289 
1290 	ipif = ipsq->ipsq_pending_ipif;
1291 	ipsq->ipsq_pending_ipif = NULL;
1292 	ipsq->ipsq_waitfor = 0;
1293 	ipsq->ipsq_current_ipif = NULL;
1294 	ipsq->ipsq_current_ioctl = 0;
1295 	mutex_exit(&ipsq->ipsq_lock);
1296 
1297 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1298 		if (connp == NULL) {
1299 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1300 		} else {
1301 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1302 			mutex_enter(&ipif->ipif_ill->ill_lock);
1303 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1304 			mutex_exit(&ipif->ipif_ill->ill_lock);
1305 		}
1306 	} else {
1307 		/*
1308 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1309 		 * be just inet_freemsg. we have to restart it
1310 		 * otherwise the thread will be stuck.
1311 		 */
1312 		inet_freemsg(mp);
1313 	}
1314 	return (B_TRUE);
1315 }
1316 
1317 /*
1318  * The ill is closing. Cleanup all the pending mps. Called exclusively
1319  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1320  * knows this ill, and hence nobody can add an mp to this list
1321  */
1322 static void
1323 ill_pending_mp_cleanup(ill_t *ill)
1324 {
1325 	mblk_t	*mp;
1326 	queue_t	*q;
1327 
1328 	ASSERT(IAM_WRITER_ILL(ill));
1329 
1330 	mutex_enter(&ill->ill_lock);
1331 	/*
1332 	 * Every mp on the pending mp list originating from an ioctl
1333 	 * added 1 to the conn refcnt, at the start of the ioctl.
1334 	 * So bump it down now.  See comments in ip_wput_nondata()
1335 	 */
1336 	while (ill->ill_pending_mp != NULL) {
1337 		mp = ill->ill_pending_mp;
1338 		ill->ill_pending_mp = mp->b_next;
1339 		mutex_exit(&ill->ill_lock);
1340 
1341 		q = mp->b_queue;
1342 		ASSERT(CONN_Q(q));
1343 		mp->b_next = NULL;
1344 		mp->b_prev = NULL;
1345 		mp->b_queue = NULL;
1346 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1347 		mutex_enter(&ill->ill_lock);
1348 	}
1349 	ill->ill_pending_ipif = NULL;
1350 
1351 	mutex_exit(&ill->ill_lock);
1352 }
1353 
1354 /*
1355  * Called in the conn close path and ill delete path
1356  */
1357 static void
1358 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1359 {
1360 	ipsq_t	*ipsq;
1361 	mblk_t	*prev;
1362 	mblk_t	*curr;
1363 	mblk_t	*next;
1364 	queue_t	*q;
1365 	mblk_t	*tmp_list = NULL;
1366 
1367 	ASSERT(IAM_WRITER_ILL(ill));
1368 	if (connp != NULL)
1369 		q = CONNP_TO_WQ(connp);
1370 	else
1371 		q = ill->ill_wq;
1372 
1373 	ipsq = ill->ill_phyint->phyint_ipsq;
1374 	/*
1375 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1376 	 * In the case of ioctl from a conn, there can be only 1 mp
1377 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1378 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1379 	 * ioctls meant for this ill form conn's are not flushed. They will
1380 	 * be processed during ipsq_exit and will not find the ill and will
1381 	 * return error.
1382 	 */
1383 	mutex_enter(&ipsq->ipsq_lock);
1384 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1385 	    curr = next) {
1386 		next = curr->b_next;
1387 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1388 			/* Unlink the mblk from the pending mp list */
1389 			if (prev != NULL) {
1390 				prev->b_next = curr->b_next;
1391 			} else {
1392 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1393 				ipsq->ipsq_xopq_mphead = curr->b_next;
1394 			}
1395 			if (ipsq->ipsq_xopq_mptail == curr)
1396 				ipsq->ipsq_xopq_mptail = prev;
1397 			/*
1398 			 * Create a temporary list and release the ipsq lock
1399 			 * New elements are added to the head of the tmp_list
1400 			 */
1401 			curr->b_next = tmp_list;
1402 			tmp_list = curr;
1403 		} else {
1404 			prev = curr;
1405 		}
1406 	}
1407 	mutex_exit(&ipsq->ipsq_lock);
1408 
1409 	while (tmp_list != NULL) {
1410 		curr = tmp_list;
1411 		tmp_list = curr->b_next;
1412 		curr->b_next = NULL;
1413 		curr->b_prev = NULL;
1414 		curr->b_queue = NULL;
1415 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1416 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1417 			    CONN_CLOSE : NO_COPYOUT, NULL);
1418 		} else {
1419 			/*
1420 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1421 			 * this can't be just inet_freemsg. we have to
1422 			 * restart it otherwise the thread will be stuck.
1423 			 */
1424 			inet_freemsg(curr);
1425 		}
1426 	}
1427 }
1428 
1429 /*
1430  * This conn has started closing. Cleanup any pending ioctl from this conn.
1431  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1432  */
1433 void
1434 conn_ioctl_cleanup(conn_t *connp)
1435 {
1436 	mblk_t *curr;
1437 	ipsq_t	*ipsq;
1438 	ill_t	*ill;
1439 	boolean_t refheld;
1440 
1441 	/*
1442 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1443 	 * ioctl has not yet started, the mp is pending in the list headed by
1444 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1445 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1446 	 * is currently executing now the mp is not queued anywhere but
1447 	 * conn_oper_pending_ill is null. The conn close will wait
1448 	 * till the conn_ref drops to zero.
1449 	 */
1450 	mutex_enter(&connp->conn_lock);
1451 	ill = connp->conn_oper_pending_ill;
1452 	if (ill == NULL) {
1453 		mutex_exit(&connp->conn_lock);
1454 		return;
1455 	}
1456 
1457 	curr = ill_pending_mp_get(ill, &connp, 0);
1458 	if (curr != NULL) {
1459 		mutex_exit(&connp->conn_lock);
1460 		CONN_DEC_REF(connp);
1461 		inet_freemsg(curr);
1462 		return;
1463 	}
1464 	/*
1465 	 * We may not be able to refhold the ill if the ill/ipif
1466 	 * is changing. But we need to make sure that the ill will
1467 	 * not vanish. So we just bump up the ill_waiter count.
1468 	 */
1469 	refheld = ill_waiter_inc(ill);
1470 	mutex_exit(&connp->conn_lock);
1471 	if (refheld) {
1472 		if (ipsq_enter(ill, B_TRUE)) {
1473 			ill_waiter_dcr(ill);
1474 			/*
1475 			 * Check whether this ioctl has started and is
1476 			 * pending now in ipsq_pending_mp. If it is not
1477 			 * found there then check whether this ioctl has
1478 			 * not even started and is in the ipsq_xopq list.
1479 			 */
1480 			if (!ipsq_pending_mp_cleanup(ill, connp))
1481 				ipsq_xopq_mp_cleanup(ill, connp);
1482 			ipsq = ill->ill_phyint->phyint_ipsq;
1483 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1484 			return;
1485 		}
1486 	}
1487 
1488 	/*
1489 	 * The ill is also closing and we could not bump up the
1490 	 * ill_waiter_count or we could not enter the ipsq. Leave
1491 	 * the cleanup to ill_delete
1492 	 */
1493 	mutex_enter(&connp->conn_lock);
1494 	while (connp->conn_oper_pending_ill != NULL)
1495 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1496 	mutex_exit(&connp->conn_lock);
1497 	if (refheld)
1498 		ill_waiter_dcr(ill);
1499 }
1500 
1501 /*
1502  * ipcl_walk function for cleaning up conn_*_ill fields.
1503  */
1504 static void
1505 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1506 {
1507 	ill_t	*ill = (ill_t *)arg;
1508 	ire_t	*ire;
1509 
1510 	mutex_enter(&connp->conn_lock);
1511 	if (connp->conn_multicast_ill == ill) {
1512 		/* Revert to late binding */
1513 		connp->conn_multicast_ill = NULL;
1514 		connp->conn_orig_multicast_ifindex = 0;
1515 	}
1516 	if (connp->conn_incoming_ill == ill)
1517 		connp->conn_incoming_ill = NULL;
1518 	if (connp->conn_outgoing_ill == ill)
1519 		connp->conn_outgoing_ill = NULL;
1520 	if (connp->conn_outgoing_pill == ill)
1521 		connp->conn_outgoing_pill = NULL;
1522 	if (connp->conn_nofailover_ill == ill)
1523 		connp->conn_nofailover_ill = NULL;
1524 	if (connp->conn_xmit_if_ill == ill)
1525 		connp->conn_xmit_if_ill = NULL;
1526 	if (connp->conn_ire_cache != NULL) {
1527 		ire = connp->conn_ire_cache;
1528 		/*
1529 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1530 		 * interface X and ipif coming from interface Y, if interface
1531 		 * X and Y are part of the same IPMPgroup. Thus whenever
1532 		 * interface X goes down, remove all references to it by
1533 		 * checking both on ire_ipif and ire_stq.
1534 		 */
1535 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1536 		    (ire->ire_type == IRE_CACHE &&
1537 		    ire->ire_stq == ill->ill_wq)) {
1538 			connp->conn_ire_cache = NULL;
1539 			mutex_exit(&connp->conn_lock);
1540 			ire_refrele_notr(ire);
1541 			return;
1542 		}
1543 	}
1544 	mutex_exit(&connp->conn_lock);
1545 
1546 }
1547 
1548 /* ARGSUSED */
1549 void
1550 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1551 {
1552 	ill_t	*ill = q->q_ptr;
1553 	ipif_t	*ipif;
1554 
1555 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1556 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1557 		ipif_non_duplicate(ipif);
1558 		ipif_down_tail(ipif);
1559 	}
1560 	ill_down_tail(ill);
1561 	freemsg(mp);
1562 	ipsq_current_finish(ipsq);
1563 }
1564 
1565 /*
1566  * ill_down_start is called when we want to down this ill and bring it up again
1567  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1568  * all interfaces, but don't tear down any plumbing.
1569  */
1570 boolean_t
1571 ill_down_start(queue_t *q, mblk_t *mp)
1572 {
1573 	ill_t	*ill = q->q_ptr;
1574 	ipif_t	*ipif;
1575 
1576 	ASSERT(IAM_WRITER_ILL(ill));
1577 
1578 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1579 		(void) ipif_down(ipif, NULL, NULL);
1580 
1581 	ill_down(ill);
1582 
1583 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1584 
1585 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1586 
1587 	/*
1588 	 * Atomically test and add the pending mp if references are active.
1589 	 */
1590 	mutex_enter(&ill->ill_lock);
1591 	if (!ill_is_quiescent(ill)) {
1592 		/* call cannot fail since `conn_t *' argument is NULL */
1593 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1594 		    mp, ILL_DOWN);
1595 		mutex_exit(&ill->ill_lock);
1596 		return (B_FALSE);
1597 	}
1598 	mutex_exit(&ill->ill_lock);
1599 	return (B_TRUE);
1600 }
1601 
1602 static void
1603 ill_down(ill_t *ill)
1604 {
1605 	ip_stack_t	*ipst = ill->ill_ipst;
1606 
1607 	/* Blow off any IREs dependent on this ILL. */
1608 	ire_walk(ill_downi, (char *)ill, ipst);
1609 
1610 	mutex_enter(&ipst->ips_ire_mrtun_lock);
1611 	if (ipst->ips_ire_mrtun_count != 0) {
1612 		mutex_exit(&ipst->ips_ire_mrtun_lock);
1613 		ire_walk_ill_mrtun(0, 0, ill_downi_mrtun_srcif,
1614 		    (char *)ill, NULL, ipst);
1615 	} else {
1616 		mutex_exit(&ipst->ips_ire_mrtun_lock);
1617 	}
1618 
1619 	/*
1620 	 * If any interface based forwarding table exists
1621 	 * Blow off the ires there dependent on this ill
1622 	 */
1623 	mutex_enter(&ipst->ips_ire_srcif_table_lock);
1624 	if (ipst->ips_ire_srcif_table_count > 0) {
1625 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
1626 		ire_walk_srcif_table_v4(ill_downi_mrtun_srcif, (char *)ill,
1627 		    ipst);
1628 	} else {
1629 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
1630 	}
1631 
1632 	/* Remove any conn_*_ill depending on this ill */
1633 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1634 
1635 	if (ill->ill_group != NULL) {
1636 		illgrp_delete(ill);
1637 	}
1638 }
1639 
1640 static void
1641 ill_down_tail(ill_t *ill)
1642 {
1643 	int	i;
1644 
1645 	/* Destroy ill_srcif_table if it exists */
1646 	/* Lock not reqd really because nobody should be able to access */
1647 	mutex_enter(&ill->ill_lock);
1648 	if (ill->ill_srcif_table != NULL) {
1649 		ill->ill_srcif_refcnt = 0;
1650 		for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
1651 			rw_destroy(&ill->ill_srcif_table[i].irb_lock);
1652 		}
1653 		kmem_free(ill->ill_srcif_table,
1654 		    IP_SRCIF_TABLE_SIZE * sizeof (irb_t));
1655 		ill->ill_srcif_table = NULL;
1656 		ill->ill_srcif_refcnt = 0;
1657 		ill->ill_mrtun_refcnt = 0;
1658 	}
1659 	mutex_exit(&ill->ill_lock);
1660 }
1661 
1662 /*
1663  * ire_walk routine used to delete every IRE that depends on queues
1664  * associated with 'ill'.  (Always called as writer.)
1665  */
1666 static void
1667 ill_downi(ire_t *ire, char *ill_arg)
1668 {
1669 	ill_t	*ill = (ill_t *)ill_arg;
1670 
1671 	/*
1672 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1673 	 * interface X and ipif coming from interface Y, if interface
1674 	 * X and Y are part of the same IPMP group. Thus whenever interface
1675 	 * X goes down, remove all references to it by checking both
1676 	 * on ire_ipif and ire_stq.
1677 	 */
1678 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1679 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1680 		ire_delete(ire);
1681 	}
1682 }
1683 
1684 /*
1685  * A seperate routine for deleting revtun and srcif based routes
1686  * are needed because the ires only deleted when the interface
1687  * is unplumbed. Also these ires have ire_in_ill non-null as well.
1688  * we want to keep mobile IP specific code separate.
1689  */
1690 static void
1691 ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg)
1692 {
1693 	ill_t   *ill = (ill_t *)ill_arg;
1694 
1695 	ASSERT(ire->ire_in_ill != NULL);
1696 
1697 	if ((ire->ire_in_ill != NULL && ire->ire_in_ill == ill) ||
1698 	    (ire->ire_stq == ill->ill_wq) || (ire->ire_stq == ill->ill_rq)) {
1699 		ire_delete(ire);
1700 	}
1701 }
1702 
1703 /*
1704  * Remove ire/nce from the fastpath list.
1705  */
1706 void
1707 ill_fastpath_nack(ill_t *ill)
1708 {
1709 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1710 }
1711 
1712 /* Consume an M_IOCACK of the fastpath probe. */
1713 void
1714 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1715 {
1716 	mblk_t	*mp1 = mp;
1717 
1718 	/*
1719 	 * If this was the first attempt turn on the fastpath probing.
1720 	 */
1721 	mutex_enter(&ill->ill_lock);
1722 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1723 		ill->ill_dlpi_fastpath_state = IDS_OK;
1724 	mutex_exit(&ill->ill_lock);
1725 
1726 	/* Free the M_IOCACK mblk, hold on to the data */
1727 	mp = mp->b_cont;
1728 	freeb(mp1);
1729 	if (mp == NULL)
1730 		return;
1731 	if (mp->b_cont != NULL) {
1732 		/*
1733 		 * Update all IRE's or NCE's that are waiting for
1734 		 * fastpath update.
1735 		 */
1736 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1737 		mp1 = mp->b_cont;
1738 		freeb(mp);
1739 		mp = mp1;
1740 	} else {
1741 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1742 	}
1743 
1744 	freeb(mp);
1745 }
1746 
1747 /*
1748  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1749  * The data portion of the request is a dl_unitdata_req_t template for
1750  * what we would send downstream in the absence of a fastpath confirmation.
1751  */
1752 int
1753 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1754 {
1755 	struct iocblk	*ioc;
1756 	mblk_t	*mp;
1757 
1758 	if (dlur_mp == NULL)
1759 		return (EINVAL);
1760 
1761 	mutex_enter(&ill->ill_lock);
1762 	switch (ill->ill_dlpi_fastpath_state) {
1763 	case IDS_FAILED:
1764 		/*
1765 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1766 		 * support it.
1767 		 */
1768 		mutex_exit(&ill->ill_lock);
1769 		return (ENOTSUP);
1770 	case IDS_UNKNOWN:
1771 		/* This is the first probe */
1772 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1773 		break;
1774 	default:
1775 		break;
1776 	}
1777 	mutex_exit(&ill->ill_lock);
1778 
1779 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1780 		return (EAGAIN);
1781 
1782 	mp->b_cont = copyb(dlur_mp);
1783 	if (mp->b_cont == NULL) {
1784 		freeb(mp);
1785 		return (EAGAIN);
1786 	}
1787 
1788 	ioc = (struct iocblk *)mp->b_rptr;
1789 	ioc->ioc_count = msgdsize(mp->b_cont);
1790 
1791 	putnext(ill->ill_wq, mp);
1792 	return (0);
1793 }
1794 
1795 void
1796 ill_capability_probe(ill_t *ill)
1797 {
1798 	/*
1799 	 * Do so only if negotiation is enabled, capabilities are unknown,
1800 	 * and a capability negotiation is not already in progress.
1801 	 */
1802 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN &&
1803 	    ill->ill_dlpi_capab_state != IDS_RENEG)
1804 		return;
1805 
1806 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1807 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1808 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1809 }
1810 
1811 void
1812 ill_capability_reset(ill_t *ill)
1813 {
1814 	mblk_t *sc_mp = NULL;
1815 	mblk_t *tmp;
1816 
1817 	/*
1818 	 * Note here that we reset the state to UNKNOWN, and later send
1819 	 * down the DL_CAPABILITY_REQ without first setting the state to
1820 	 * INPROGRESS.  We do this in order to distinguish the
1821 	 * DL_CAPABILITY_ACK response which may come back in response to
1822 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1823 	 * also handle the case where the driver doesn't send us back
1824 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1825 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1826 	 * features are turned off until the state reaches IDS_OK.
1827 	 */
1828 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1829 
1830 	/*
1831 	 * Disable sub-capabilities and request a list of sub-capability
1832 	 * messages which will be sent down to the driver.  Each handler
1833 	 * allocates the corresponding dl_capability_sub_t inside an
1834 	 * mblk, and links it to the existing sc_mp mblk, or return it
1835 	 * as sc_mp if it's the first sub-capability (the passed in
1836 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1837 	 * sc_mp will be pulled-up, before passing it downstream.
1838 	 */
1839 	ill_capability_mdt_reset(ill, &sc_mp);
1840 	ill_capability_hcksum_reset(ill, &sc_mp);
1841 	ill_capability_zerocopy_reset(ill, &sc_mp);
1842 	ill_capability_ipsec_reset(ill, &sc_mp);
1843 	ill_capability_dls_reset(ill, &sc_mp);
1844 	ill_capability_lso_reset(ill, &sc_mp);
1845 
1846 	/* Nothing to send down in order to disable the capabilities? */
1847 	if (sc_mp == NULL)
1848 		return;
1849 
1850 	tmp = msgpullup(sc_mp, -1);
1851 	freemsg(sc_mp);
1852 	if ((sc_mp = tmp) == NULL) {
1853 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1854 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1855 		return;
1856 	}
1857 
1858 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1859 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1860 }
1861 
1862 /*
1863  * Request or set new-style hardware capabilities supported by DLS provider.
1864  */
1865 static void
1866 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1867 {
1868 	mblk_t *mp;
1869 	dl_capability_req_t *capb;
1870 	size_t size = 0;
1871 	uint8_t *ptr;
1872 
1873 	if (reqp != NULL)
1874 		size = MBLKL(reqp);
1875 
1876 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1877 	if (mp == NULL) {
1878 		freemsg(reqp);
1879 		return;
1880 	}
1881 	ptr = mp->b_rptr;
1882 
1883 	capb = (dl_capability_req_t *)ptr;
1884 	ptr += sizeof (dl_capability_req_t);
1885 
1886 	if (reqp != NULL) {
1887 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1888 		capb->dl_sub_length = size;
1889 		bcopy(reqp->b_rptr, ptr, size);
1890 		ptr += size;
1891 		mp->b_cont = reqp->b_cont;
1892 		freeb(reqp);
1893 	}
1894 	ASSERT(ptr == mp->b_wptr);
1895 
1896 	ill_dlpi_send(ill, mp);
1897 }
1898 
1899 static void
1900 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1901 {
1902 	dl_capab_id_t *id_ic;
1903 	uint_t sub_dl_cap = outers->dl_cap;
1904 	dl_capability_sub_t *inners;
1905 	uint8_t *capend;
1906 
1907 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1908 
1909 	/*
1910 	 * Note: range checks here are not absolutely sufficient to
1911 	 * make us robust against malformed messages sent by drivers;
1912 	 * this is in keeping with the rest of IP's dlpi handling.
1913 	 * (Remember, it's coming from something else in the kernel
1914 	 * address space)
1915 	 */
1916 
1917 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1918 	if (capend > mp->b_wptr) {
1919 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1920 		    "malformed sub-capability too long for mblk");
1921 		return;
1922 	}
1923 
1924 	id_ic = (dl_capab_id_t *)(outers + 1);
1925 
1926 	if (outers->dl_length < sizeof (*id_ic) ||
1927 	    (inners = &id_ic->id_subcap,
1928 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1929 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1930 		    "encapsulated capab type %d too long for mblk",
1931 		    inners->dl_cap);
1932 		return;
1933 	}
1934 
1935 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1936 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1937 		    "isn't as expected; pass-thru module(s) detected, "
1938 		    "discarding capability\n", inners->dl_cap));
1939 		return;
1940 	}
1941 
1942 	/* Process the encapsulated sub-capability */
1943 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1944 }
1945 
1946 /*
1947  * Process Multidata Transmit capability negotiation ack received from a
1948  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1949  * DL_CAPABILITY_ACK message.
1950  */
1951 static void
1952 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1953 {
1954 	mblk_t *nmp = NULL;
1955 	dl_capability_req_t *oc;
1956 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1957 	ill_mdt_capab_t **ill_mdt_capab;
1958 	uint_t sub_dl_cap = isub->dl_cap;
1959 	uint8_t *capend;
1960 
1961 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1962 
1963 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1964 
1965 	/*
1966 	 * Note: range checks here are not absolutely sufficient to
1967 	 * make us robust against malformed messages sent by drivers;
1968 	 * this is in keeping with the rest of IP's dlpi handling.
1969 	 * (Remember, it's coming from something else in the kernel
1970 	 * address space)
1971 	 */
1972 
1973 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1974 	if (capend > mp->b_wptr) {
1975 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1976 		    "malformed sub-capability too long for mblk");
1977 		return;
1978 	}
1979 
1980 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1981 
1982 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1983 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1984 		    "unsupported MDT sub-capability (version %d, expected %d)",
1985 		    mdt_ic->mdt_version, MDT_VERSION_2);
1986 		return;
1987 	}
1988 
1989 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1990 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1991 		    "capability isn't as expected; pass-thru module(s) "
1992 		    "detected, discarding capability\n"));
1993 		return;
1994 	}
1995 
1996 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1997 
1998 		if (*ill_mdt_capab == NULL) {
1999 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
2000 			    KM_NOSLEEP);
2001 
2002 			if (*ill_mdt_capab == NULL) {
2003 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2004 				    "could not enable MDT version %d "
2005 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
2006 				    ill->ill_name);
2007 				return;
2008 			}
2009 		}
2010 
2011 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
2012 		    "MDT version %d (%d bytes leading, %d bytes trailing "
2013 		    "header spaces, %d max pld bufs, %d span limit)\n",
2014 		    ill->ill_name, MDT_VERSION_2,
2015 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
2016 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
2017 
2018 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
2019 		(*ill_mdt_capab)->ill_mdt_on = 1;
2020 		/*
2021 		 * Round the following values to the nearest 32-bit; ULP
2022 		 * may further adjust them to accomodate for additional
2023 		 * protocol headers.  We pass these values to ULP during
2024 		 * bind time.
2025 		 */
2026 		(*ill_mdt_capab)->ill_mdt_hdr_head =
2027 		    roundup(mdt_ic->mdt_hdr_head, 4);
2028 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
2029 		    roundup(mdt_ic->mdt_hdr_tail, 4);
2030 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
2031 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
2032 
2033 		ill->ill_capabilities |= ILL_CAPAB_MDT;
2034 	} else {
2035 		uint_t size;
2036 		uchar_t *rptr;
2037 
2038 		size = sizeof (dl_capability_req_t) +
2039 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
2040 
2041 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2042 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2043 			    "could not enable MDT for %s (ENOMEM)\n",
2044 			    ill->ill_name);
2045 			return;
2046 		}
2047 
2048 		rptr = nmp->b_rptr;
2049 		/* initialize dl_capability_req_t */
2050 		oc = (dl_capability_req_t *)nmp->b_rptr;
2051 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2052 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2053 		    sizeof (dl_capab_mdt_t);
2054 		nmp->b_rptr += sizeof (dl_capability_req_t);
2055 
2056 		/* initialize dl_capability_sub_t */
2057 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2058 		nmp->b_rptr += sizeof (*isub);
2059 
2060 		/* initialize dl_capab_mdt_t */
2061 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2062 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2063 
2064 		nmp->b_rptr = rptr;
2065 
2066 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2067 		    "to enable MDT version %d\n", ill->ill_name,
2068 		    MDT_VERSION_2));
2069 
2070 		/* set ENABLE flag */
2071 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2072 
2073 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2074 		ill_dlpi_send(ill, nmp);
2075 	}
2076 }
2077 
2078 static void
2079 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2080 {
2081 	mblk_t *mp;
2082 	dl_capab_mdt_t *mdt_subcap;
2083 	dl_capability_sub_t *dl_subcap;
2084 	int size;
2085 
2086 	if (!ILL_MDT_CAPABLE(ill))
2087 		return;
2088 
2089 	ASSERT(ill->ill_mdt_capab != NULL);
2090 	/*
2091 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2092 	 * structure since it's possible that another thread is still
2093 	 * referring to it.  The structure only gets deallocated when
2094 	 * we destroy the ill.
2095 	 */
2096 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2097 
2098 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2099 
2100 	mp = allocb(size, BPRI_HI);
2101 	if (mp == NULL) {
2102 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2103 		    "request to disable MDT\n"));
2104 		return;
2105 	}
2106 
2107 	mp->b_wptr = mp->b_rptr + size;
2108 
2109 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2110 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2111 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2112 
2113 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2114 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2115 	mdt_subcap->mdt_flags = 0;
2116 	mdt_subcap->mdt_hdr_head = 0;
2117 	mdt_subcap->mdt_hdr_tail = 0;
2118 
2119 	if (*sc_mp != NULL)
2120 		linkb(*sc_mp, mp);
2121 	else
2122 		*sc_mp = mp;
2123 }
2124 
2125 /*
2126  * Send a DL_NOTIFY_REQ to the specified ill to enable
2127  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2128  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2129  * acceleration.
2130  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2131  */
2132 static boolean_t
2133 ill_enable_promisc_notify(ill_t *ill)
2134 {
2135 	mblk_t *mp;
2136 	dl_notify_req_t *req;
2137 
2138 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2139 
2140 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2141 	if (mp == NULL)
2142 		return (B_FALSE);
2143 
2144 	req = (dl_notify_req_t *)mp->b_rptr;
2145 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2146 	    DL_NOTE_PROMISC_OFF_PHYS;
2147 
2148 	ill_dlpi_send(ill, mp);
2149 
2150 	return (B_TRUE);
2151 }
2152 
2153 
2154 /*
2155  * Allocate an IPsec capability request which will be filled by our
2156  * caller to turn on support for one or more algorithms.
2157  */
2158 static mblk_t *
2159 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2160 {
2161 	mblk_t *nmp;
2162 	dl_capability_req_t	*ocap;
2163 	dl_capab_ipsec_t	*ocip;
2164 	dl_capab_ipsec_t	*icip;
2165 	uint8_t			*ptr;
2166 	icip = (dl_capab_ipsec_t *)(isub + 1);
2167 
2168 	/*
2169 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2170 	 * PROMISC_ON/OFF notification from the provider. We need to
2171 	 * do this before enabling the algorithms to avoid leakage of
2172 	 * cleartext packets.
2173 	 */
2174 
2175 	if (!ill_enable_promisc_notify(ill))
2176 		return (NULL);
2177 
2178 	/*
2179 	 * Allocate new mblk which will contain a new capability
2180 	 * request to enable the capabilities.
2181 	 */
2182 
2183 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2184 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2185 	if (nmp == NULL)
2186 		return (NULL);
2187 
2188 	ptr = nmp->b_rptr;
2189 
2190 	/* initialize dl_capability_req_t */
2191 	ocap = (dl_capability_req_t *)ptr;
2192 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2193 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2194 	ptr += sizeof (dl_capability_req_t);
2195 
2196 	/* initialize dl_capability_sub_t */
2197 	bcopy(isub, ptr, sizeof (*isub));
2198 	ptr += sizeof (*isub);
2199 
2200 	/* initialize dl_capab_ipsec_t */
2201 	ocip = (dl_capab_ipsec_t *)ptr;
2202 	bcopy(icip, ocip, sizeof (*icip));
2203 
2204 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2205 	return (nmp);
2206 }
2207 
2208 /*
2209  * Process an IPsec capability negotiation ack received from a DLS Provider.
2210  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2211  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2212  */
2213 static void
2214 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2215 {
2216 	dl_capab_ipsec_t	*icip;
2217 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2218 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2219 	uint_t cipher, nciphers;
2220 	mblk_t *nmp;
2221 	uint_t alg_len;
2222 	boolean_t need_sadb_dump;
2223 	uint_t sub_dl_cap = isub->dl_cap;
2224 	ill_ipsec_capab_t **ill_capab;
2225 	uint64_t ill_capab_flag;
2226 	uint8_t *capend, *ciphend;
2227 	boolean_t sadb_resync;
2228 
2229 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2230 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2231 
2232 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2233 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2234 		ill_capab_flag = ILL_CAPAB_AH;
2235 	} else {
2236 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2237 		ill_capab_flag = ILL_CAPAB_ESP;
2238 	}
2239 
2240 	/*
2241 	 * If the ill capability structure exists, then this incoming
2242 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2243 	 * If this is so, then we'd need to resynchronize the SADB
2244 	 * after re-enabling the offloaded ciphers.
2245 	 */
2246 	sadb_resync = (*ill_capab != NULL);
2247 
2248 	/*
2249 	 * Note: range checks here are not absolutely sufficient to
2250 	 * make us robust against malformed messages sent by drivers;
2251 	 * this is in keeping with the rest of IP's dlpi handling.
2252 	 * (Remember, it's coming from something else in the kernel
2253 	 * address space)
2254 	 */
2255 
2256 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2257 	if (capend > mp->b_wptr) {
2258 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2259 		    "malformed sub-capability too long for mblk");
2260 		return;
2261 	}
2262 
2263 	/*
2264 	 * There are two types of acks we process here:
2265 	 * 1. acks in reply to a (first form) generic capability req
2266 	 *    (no ENABLE flag set)
2267 	 * 2. acks in reply to a ENABLE capability req.
2268 	 *    (ENABLE flag set)
2269 	 *
2270 	 * We process the subcapability passed as argument as follows:
2271 	 * 1 do initializations
2272 	 *   1.1 initialize nmp = NULL
2273 	 *   1.2 set need_sadb_dump to B_FALSE
2274 	 * 2 for each cipher in subcapability:
2275 	 *   2.1 if ENABLE flag is set:
2276 	 *	2.1.1 update per-ill ipsec capabilities info
2277 	 *	2.1.2 set need_sadb_dump to B_TRUE
2278 	 *   2.2 if ENABLE flag is not set:
2279 	 *	2.2.1 if nmp is NULL:
2280 	 *		2.2.1.1 allocate and initialize nmp
2281 	 *		2.2.1.2 init current pos in nmp
2282 	 *	2.2.2 copy current cipher to current pos in nmp
2283 	 *	2.2.3 set ENABLE flag in nmp
2284 	 *	2.2.4 update current pos
2285 	 * 3 if nmp is not equal to NULL, send enable request
2286 	 *   3.1 send capability request
2287 	 * 4 if need_sadb_dump is B_TRUE
2288 	 *   4.1 enable promiscuous on/off notifications
2289 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2290 	 *	AH or ESP SA's to interface.
2291 	 */
2292 
2293 	nmp = NULL;
2294 	oalg = NULL;
2295 	need_sadb_dump = B_FALSE;
2296 	icip = (dl_capab_ipsec_t *)(isub + 1);
2297 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2298 
2299 	nciphers = icip->cip_nciphers;
2300 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2301 
2302 	if (ciphend > capend) {
2303 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2304 		    "too many ciphers for sub-capability len");
2305 		return;
2306 	}
2307 
2308 	for (cipher = 0; cipher < nciphers; cipher++) {
2309 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2310 
2311 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2312 			/*
2313 			 * TBD: when we provide a way to disable capabilities
2314 			 * from above, need to manage the request-pending state
2315 			 * and fail if we were not expecting this ACK.
2316 			 */
2317 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2318 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2319 
2320 			/*
2321 			 * Update IPsec capabilities for this ill
2322 			 */
2323 
2324 			if (*ill_capab == NULL) {
2325 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2326 				    ("ill_capability_ipsec_ack: "
2327 				    "allocating ipsec_capab for ill\n"));
2328 				*ill_capab = ill_ipsec_capab_alloc();
2329 
2330 				if (*ill_capab == NULL) {
2331 					cmn_err(CE_WARN,
2332 					    "ill_capability_ipsec_ack: "
2333 					    "could not enable IPsec Hardware "
2334 					    "acceleration for %s (ENOMEM)\n",
2335 					    ill->ill_name);
2336 					return;
2337 				}
2338 			}
2339 
2340 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2341 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2342 
2343 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2344 				cmn_err(CE_WARN,
2345 				    "ill_capability_ipsec_ack: "
2346 				    "malformed IPsec algorithm id %d",
2347 				    ialg->alg_prim);
2348 				continue;
2349 			}
2350 
2351 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2352 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2353 				    ialg->alg_prim);
2354 			} else {
2355 				ipsec_capab_algparm_t *alp;
2356 
2357 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2358 				    ialg->alg_prim);
2359 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2360 				    ialg->alg_prim)) {
2361 					cmn_err(CE_WARN,
2362 					    "ill_capability_ipsec_ack: "
2363 					    "no space for IPsec alg id %d",
2364 					    ialg->alg_prim);
2365 					continue;
2366 				}
2367 				alp = &((*ill_capab)->encr_algparm[
2368 				    ialg->alg_prim]);
2369 				alp->minkeylen = ialg->alg_minbits;
2370 				alp->maxkeylen = ialg->alg_maxbits;
2371 			}
2372 			ill->ill_capabilities |= ill_capab_flag;
2373 			/*
2374 			 * indicate that a capability was enabled, which
2375 			 * will be used below to kick off a SADB dump
2376 			 * to the ill.
2377 			 */
2378 			need_sadb_dump = B_TRUE;
2379 		} else {
2380 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2381 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2382 			    ialg->alg_prim));
2383 
2384 			if (nmp == NULL) {
2385 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2386 				if (nmp == NULL) {
2387 					/*
2388 					 * Sending the PROMISC_ON/OFF
2389 					 * notification request failed.
2390 					 * We cannot enable the algorithms
2391 					 * since the Provider will not
2392 					 * notify IP of promiscous mode
2393 					 * changes, which could lead
2394 					 * to leakage of packets.
2395 					 */
2396 					cmn_err(CE_WARN,
2397 					    "ill_capability_ipsec_ack: "
2398 					    "could not enable IPsec Hardware "
2399 					    "acceleration for %s (ENOMEM)\n",
2400 					    ill->ill_name);
2401 					return;
2402 				}
2403 				/* ptr to current output alg specifier */
2404 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2405 			}
2406 
2407 			/*
2408 			 * Copy current alg specifier, set ENABLE
2409 			 * flag, and advance to next output alg.
2410 			 * For now we enable all IPsec capabilities.
2411 			 */
2412 			ASSERT(oalg != NULL);
2413 			bcopy(ialg, oalg, alg_len);
2414 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2415 			nmp->b_wptr += alg_len;
2416 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2417 		}
2418 
2419 		/* move to next input algorithm specifier */
2420 		ialg = (dl_capab_ipsec_alg_t *)
2421 		    ((char *)ialg + alg_len);
2422 	}
2423 
2424 	if (nmp != NULL)
2425 		/*
2426 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2427 		 * IPsec hardware acceleration.
2428 		 */
2429 		ill_dlpi_send(ill, nmp);
2430 
2431 	if (need_sadb_dump)
2432 		/*
2433 		 * An acknowledgement corresponding to a request to
2434 		 * enable acceleration was received, notify SADB.
2435 		 */
2436 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2437 }
2438 
2439 /*
2440  * Given an mblk with enough space in it, create sub-capability entries for
2441  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2442  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2443  * in preparation for the reset the DL_CAPABILITY_REQ message.
2444  */
2445 static void
2446 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2447     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2448 {
2449 	dl_capab_ipsec_t *oipsec;
2450 	dl_capab_ipsec_alg_t *oalg;
2451 	dl_capability_sub_t *dl_subcap;
2452 	int i, k;
2453 
2454 	ASSERT(nciphers > 0);
2455 	ASSERT(ill_cap != NULL);
2456 	ASSERT(mp != NULL);
2457 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2458 
2459 	/* dl_capability_sub_t for "stype" */
2460 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2461 	dl_subcap->dl_cap = stype;
2462 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2463 	mp->b_wptr += sizeof (dl_capability_sub_t);
2464 
2465 	/* dl_capab_ipsec_t for "stype" */
2466 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2467 	oipsec->cip_version = 1;
2468 	oipsec->cip_nciphers = nciphers;
2469 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2470 
2471 	/* create entries for "stype" AUTH ciphers */
2472 	for (i = 0; i < ill_cap->algs_size; i++) {
2473 		for (k = 0; k < BITSPERBYTE; k++) {
2474 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2475 				continue;
2476 
2477 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2478 			bzero((void *)oalg, sizeof (*oalg));
2479 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2480 			oalg->alg_prim = k + (BITSPERBYTE * i);
2481 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2482 		}
2483 	}
2484 	/* create entries for "stype" ENCR ciphers */
2485 	for (i = 0; i < ill_cap->algs_size; i++) {
2486 		for (k = 0; k < BITSPERBYTE; k++) {
2487 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2488 				continue;
2489 
2490 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2491 			bzero((void *)oalg, sizeof (*oalg));
2492 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2493 			oalg->alg_prim = k + (BITSPERBYTE * i);
2494 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2495 		}
2496 	}
2497 }
2498 
2499 /*
2500  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2501  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2502  * POPC instruction, but our macro is more flexible for an arbitrary length
2503  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2504  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2505  * stays that way, we can reduce the number of iterations required.
2506  */
2507 #define	COUNT_1S(val, sum) {					\
2508 	uint8_t x = val & 0xff;					\
2509 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2510 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2511 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2512 }
2513 
2514 /* ARGSUSED */
2515 static void
2516 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2517 {
2518 	mblk_t *mp;
2519 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2520 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2521 	uint64_t ill_capabilities = ill->ill_capabilities;
2522 	int ah_cnt = 0, esp_cnt = 0;
2523 	int ah_len = 0, esp_len = 0;
2524 	int i, size = 0;
2525 
2526 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2527 		return;
2528 
2529 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2530 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2531 
2532 	/* Find out the number of ciphers for AH */
2533 	if (cap_ah != NULL) {
2534 		for (i = 0; i < cap_ah->algs_size; i++) {
2535 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2536 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2537 		}
2538 		if (ah_cnt > 0) {
2539 			size += sizeof (dl_capability_sub_t) +
2540 			    sizeof (dl_capab_ipsec_t);
2541 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2542 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2543 			size += ah_len;
2544 		}
2545 	}
2546 
2547 	/* Find out the number of ciphers for ESP */
2548 	if (cap_esp != NULL) {
2549 		for (i = 0; i < cap_esp->algs_size; i++) {
2550 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2551 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2552 		}
2553 		if (esp_cnt > 0) {
2554 			size += sizeof (dl_capability_sub_t) +
2555 			    sizeof (dl_capab_ipsec_t);
2556 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2557 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2558 			size += esp_len;
2559 		}
2560 	}
2561 
2562 	if (size == 0) {
2563 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2564 		    "there's nothing to reset\n"));
2565 		return;
2566 	}
2567 
2568 	mp = allocb(size, BPRI_HI);
2569 	if (mp == NULL) {
2570 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2571 		    "request to disable IPSEC Hardware Acceleration\n"));
2572 		return;
2573 	}
2574 
2575 	/*
2576 	 * Clear the capability flags for IPSec HA but retain the ill
2577 	 * capability structures since it's possible that another thread
2578 	 * is still referring to them.  The structures only get deallocated
2579 	 * when we destroy the ill.
2580 	 *
2581 	 * Various places check the flags to see if the ill is capable of
2582 	 * hardware acceleration, and by clearing them we ensure that new
2583 	 * outbound IPSec packets are sent down encrypted.
2584 	 */
2585 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2586 
2587 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2588 	if (ah_cnt > 0) {
2589 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2590 		    cap_ah, mp);
2591 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2592 	}
2593 
2594 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2595 	if (esp_cnt > 0) {
2596 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2597 		    cap_esp, mp);
2598 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2599 	}
2600 
2601 	/*
2602 	 * At this point we've composed a bunch of sub-capabilities to be
2603 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2604 	 * by the caller.  Upon receiving this reset message, the driver
2605 	 * must stop inbound decryption (by destroying all inbound SAs)
2606 	 * and let the corresponding packets come in encrypted.
2607 	 */
2608 
2609 	if (*sc_mp != NULL)
2610 		linkb(*sc_mp, mp);
2611 	else
2612 		*sc_mp = mp;
2613 }
2614 
2615 static void
2616 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2617     boolean_t encapsulated)
2618 {
2619 	boolean_t legacy = B_FALSE;
2620 
2621 	/*
2622 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2623 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2624 	 * instructed the driver to disable its advertised capabilities,
2625 	 * so there's no point in accepting any response at this moment.
2626 	 */
2627 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2628 		return;
2629 
2630 	/*
2631 	 * Note that only the following two sub-capabilities may be
2632 	 * considered as "legacy", since their original definitions
2633 	 * do not incorporate the dl_mid_t module ID token, and hence
2634 	 * may require the use of the wrapper sub-capability.
2635 	 */
2636 	switch (subp->dl_cap) {
2637 	case DL_CAPAB_IPSEC_AH:
2638 	case DL_CAPAB_IPSEC_ESP:
2639 		legacy = B_TRUE;
2640 		break;
2641 	}
2642 
2643 	/*
2644 	 * For legacy sub-capabilities which don't incorporate a queue_t
2645 	 * pointer in their structures, discard them if we detect that
2646 	 * there are intermediate modules in between IP and the driver.
2647 	 */
2648 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2649 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2650 		    "%d discarded; %d module(s) present below IP\n",
2651 		    subp->dl_cap, ill->ill_lmod_cnt));
2652 		return;
2653 	}
2654 
2655 	switch (subp->dl_cap) {
2656 	case DL_CAPAB_IPSEC_AH:
2657 	case DL_CAPAB_IPSEC_ESP:
2658 		ill_capability_ipsec_ack(ill, mp, subp);
2659 		break;
2660 	case DL_CAPAB_MDT:
2661 		ill_capability_mdt_ack(ill, mp, subp);
2662 		break;
2663 	case DL_CAPAB_HCKSUM:
2664 		ill_capability_hcksum_ack(ill, mp, subp);
2665 		break;
2666 	case DL_CAPAB_ZEROCOPY:
2667 		ill_capability_zerocopy_ack(ill, mp, subp);
2668 		break;
2669 	case DL_CAPAB_POLL:
2670 		if (!SOFT_RINGS_ENABLED())
2671 			ill_capability_dls_ack(ill, mp, subp);
2672 		break;
2673 	case DL_CAPAB_SOFT_RING:
2674 		if (SOFT_RINGS_ENABLED())
2675 			ill_capability_dls_ack(ill, mp, subp);
2676 		break;
2677 	case DL_CAPAB_LSO:
2678 		ill_capability_lso_ack(ill, mp, subp);
2679 		break;
2680 	default:
2681 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2682 		    subp->dl_cap));
2683 	}
2684 }
2685 
2686 /*
2687  * As part of negotiating polling capability, the driver tells us
2688  * the default (or normal) blanking interval and packet threshold
2689  * (the receive timer fires if blanking interval is reached or
2690  * the packet threshold is reached).
2691  *
2692  * As part of manipulating the polling interval, we always use our
2693  * estimated interval (avg service time * number of packets queued
2694  * on the squeue) but we try to blank for a minimum of
2695  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2696  * packet threshold during this time. When we are not in polling mode
2697  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2698  * rr_min_blank_ratio but up the packet cnt by a ratio of
2699  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2700  * possible although for a shorter interval.
2701  */
2702 #define	RR_MAX_BLANK_RATIO	20
2703 #define	RR_MIN_BLANK_RATIO	10
2704 #define	RR_MAX_PKT_CNT_RATIO	3
2705 #define	RR_MIN_PKT_CNT_RATIO	3
2706 
2707 /*
2708  * These can be tuned via /etc/system.
2709  */
2710 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2711 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2712 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2713 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2714 
2715 static mac_resource_handle_t
2716 ill_ring_add(void *arg, mac_resource_t *mrp)
2717 {
2718 	ill_t			*ill = (ill_t *)arg;
2719 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2720 	ill_rx_ring_t		*rx_ring;
2721 	int			ip_rx_index;
2722 
2723 	ASSERT(mrp != NULL);
2724 	if (mrp->mr_type != MAC_RX_FIFO) {
2725 		return (NULL);
2726 	}
2727 	ASSERT(ill != NULL);
2728 	ASSERT(ill->ill_dls_capab != NULL);
2729 
2730 	mutex_enter(&ill->ill_lock);
2731 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2732 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2733 		ASSERT(rx_ring != NULL);
2734 
2735 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2736 			time_t normal_blank_time =
2737 			    mrfp->mrf_normal_blank_time;
2738 			uint_t normal_pkt_cnt =
2739 			    mrfp->mrf_normal_pkt_count;
2740 
2741 	bzero(rx_ring, sizeof (ill_rx_ring_t));
2742 
2743 	rx_ring->rr_blank = mrfp->mrf_blank;
2744 	rx_ring->rr_handle = mrfp->mrf_arg;
2745 	rx_ring->rr_ill = ill;
2746 	rx_ring->rr_normal_blank_time = normal_blank_time;
2747 	rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2748 
2749 			rx_ring->rr_max_blank_time =
2750 			    normal_blank_time * rr_max_blank_ratio;
2751 			rx_ring->rr_min_blank_time =
2752 			    normal_blank_time * rr_min_blank_ratio;
2753 			rx_ring->rr_max_pkt_cnt =
2754 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2755 			rx_ring->rr_min_pkt_cnt =
2756 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2757 
2758 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2759 			mutex_exit(&ill->ill_lock);
2760 
2761 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2762 			    (int), ip_rx_index);
2763 			return ((mac_resource_handle_t)rx_ring);
2764 		}
2765 	}
2766 
2767 	/*
2768 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2769 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2770 	 * should be made configurable. Meanwhile it cause no panic because
2771 	 * driver will pass ip_input a NULL handle which will make
2772 	 * IP allocate the default squeue and Polling mode will not
2773 	 * be used for this ring.
2774 	 */
2775 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2776 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2777 
2778 	mutex_exit(&ill->ill_lock);
2779 	return (NULL);
2780 }
2781 
2782 static boolean_t
2783 ill_capability_dls_init(ill_t *ill)
2784 {
2785 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2786 	conn_t 			*connp;
2787 	size_t			sz;
2788 	ip_stack_t *ipst = ill->ill_ipst;
2789 
2790 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2791 		if (ill_dls == NULL) {
2792 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2793 			    "soft_ring enabled for ill=%s (%p) but data "
2794 			    "structs uninitialized\n", ill->ill_name,
2795 			    (void *)ill);
2796 		}
2797 		return (B_TRUE);
2798 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2799 		if (ill_dls == NULL) {
2800 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2801 			    "polling enabled for ill=%s (%p) but data "
2802 			    "structs uninitialized\n", ill->ill_name,
2803 			    (void *)ill);
2804 		}
2805 		return (B_TRUE);
2806 	}
2807 
2808 	if (ill_dls != NULL) {
2809 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2810 		/* Soft_Ring or polling is being re-enabled */
2811 
2812 		connp = ill_dls->ill_unbind_conn;
2813 		ASSERT(rx_ring != NULL);
2814 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2815 		bzero((void *)rx_ring,
2816 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2817 		ill_dls->ill_ring_tbl = rx_ring;
2818 		ill_dls->ill_unbind_conn = connp;
2819 		return (B_TRUE);
2820 	}
2821 
2822 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
2823 	    ipst->ips_netstack)) == NULL)
2824 		return (B_FALSE);
2825 
2826 	sz = sizeof (ill_dls_capab_t);
2827 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2828 
2829 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2830 	if (ill_dls == NULL) {
2831 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2832 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2833 		    (void *)ill);
2834 		CONN_DEC_REF(connp);
2835 		return (B_FALSE);
2836 	}
2837 
2838 	/* Allocate space to hold ring table */
2839 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2840 	ill->ill_dls_capab = ill_dls;
2841 	ill_dls->ill_unbind_conn = connp;
2842 	return (B_TRUE);
2843 }
2844 
2845 /*
2846  * ill_capability_dls_disable: disable soft_ring and/or polling
2847  * capability. Since any of the rings might already be in use, need
2848  * to call ip_squeue_clean_all() which gets behind the squeue to disable
2849  * direct calls if necessary.
2850  */
2851 static void
2852 ill_capability_dls_disable(ill_t *ill)
2853 {
2854 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2855 
2856 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2857 		ip_squeue_clean_all(ill);
2858 		ill_dls->ill_tx = NULL;
2859 		ill_dls->ill_tx_handle = NULL;
2860 		ill_dls->ill_dls_change_status = NULL;
2861 		ill_dls->ill_dls_bind = NULL;
2862 		ill_dls->ill_dls_unbind = NULL;
2863 	}
2864 
2865 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2866 }
2867 
2868 static void
2869 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2870     dl_capability_sub_t *isub)
2871 {
2872 	uint_t			size;
2873 	uchar_t			*rptr;
2874 	dl_capab_dls_t	dls, *odls;
2875 	ill_dls_capab_t	*ill_dls;
2876 	mblk_t			*nmp = NULL;
2877 	dl_capability_req_t	*ocap;
2878 	uint_t			sub_dl_cap = isub->dl_cap;
2879 
2880 	if (!ill_capability_dls_init(ill))
2881 		return;
2882 	ill_dls = ill->ill_dls_capab;
2883 
2884 	/* Copy locally to get the members aligned */
2885 	bcopy((void *)idls, (void *)&dls,
2886 	    sizeof (dl_capab_dls_t));
2887 
2888 	/* Get the tx function and handle from dld */
2889 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2890 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2891 
2892 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2893 		ill_dls->ill_dls_change_status =
2894 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2895 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2896 		ill_dls->ill_dls_unbind =
2897 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2898 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2899 	}
2900 
2901 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2902 	    isub->dl_length;
2903 
2904 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2905 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2906 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2907 		    ill->ill_name, (void *)ill);
2908 		return;
2909 	}
2910 
2911 	/* initialize dl_capability_req_t */
2912 	rptr = nmp->b_rptr;
2913 	ocap = (dl_capability_req_t *)rptr;
2914 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2915 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2916 	rptr += sizeof (dl_capability_req_t);
2917 
2918 	/* initialize dl_capability_sub_t */
2919 	bcopy(isub, rptr, sizeof (*isub));
2920 	rptr += sizeof (*isub);
2921 
2922 	odls = (dl_capab_dls_t *)rptr;
2923 	rptr += sizeof (dl_capab_dls_t);
2924 
2925 	/* initialize dl_capab_dls_t to be sent down */
2926 	dls.dls_rx_handle = (uintptr_t)ill;
2927 	dls.dls_rx = (uintptr_t)ip_input;
2928 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2929 
2930 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2931 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2932 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2933 		dls.dls_flags = SOFT_RING_ENABLE;
2934 	} else {
2935 		dls.dls_flags = POLL_ENABLE;
2936 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2937 		    "to enable polling\n", ill->ill_name));
2938 	}
2939 	bcopy((void *)&dls, (void *)odls,
2940 	    sizeof (dl_capab_dls_t));
2941 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2942 	/*
2943 	 * nmp points to a DL_CAPABILITY_REQ message to
2944 	 * enable either soft_ring or polling
2945 	 */
2946 	ill_dlpi_send(ill, nmp);
2947 }
2948 
2949 static void
2950 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2951 {
2952 	mblk_t *mp;
2953 	dl_capab_dls_t *idls;
2954 	dl_capability_sub_t *dl_subcap;
2955 	int size;
2956 
2957 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2958 		return;
2959 
2960 	ASSERT(ill->ill_dls_capab != NULL);
2961 
2962 	size = sizeof (*dl_subcap) + sizeof (*idls);
2963 
2964 	mp = allocb(size, BPRI_HI);
2965 	if (mp == NULL) {
2966 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2967 		    "request to disable soft_ring\n"));
2968 		return;
2969 	}
2970 
2971 	mp->b_wptr = mp->b_rptr + size;
2972 
2973 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2974 	dl_subcap->dl_length = sizeof (*idls);
2975 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2976 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2977 	else
2978 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2979 
2980 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2981 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2982 		idls->dls_flags = SOFT_RING_DISABLE;
2983 	else
2984 		idls->dls_flags = POLL_DISABLE;
2985 
2986 	if (*sc_mp != NULL)
2987 		linkb(*sc_mp, mp);
2988 	else
2989 		*sc_mp = mp;
2990 }
2991 
2992 /*
2993  * Process a soft_ring/poll capability negotiation ack received
2994  * from a DLS Provider.isub must point to the sub-capability
2995  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
2996  */
2997 static void
2998 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2999 {
3000 	dl_capab_dls_t		*idls;
3001 	uint_t			sub_dl_cap = isub->dl_cap;
3002 	uint8_t			*capend;
3003 
3004 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
3005 	    sub_dl_cap == DL_CAPAB_POLL);
3006 
3007 	if (ill->ill_isv6)
3008 		return;
3009 
3010 	/*
3011 	 * Note: range checks here are not absolutely sufficient to
3012 	 * make us robust against malformed messages sent by drivers;
3013 	 * this is in keeping with the rest of IP's dlpi handling.
3014 	 * (Remember, it's coming from something else in the kernel
3015 	 * address space)
3016 	 */
3017 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3018 	if (capend > mp->b_wptr) {
3019 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
3020 		    "malformed sub-capability too long for mblk");
3021 		return;
3022 	}
3023 
3024 	/*
3025 	 * There are two types of acks we process here:
3026 	 * 1. acks in reply to a (first form) generic capability req
3027 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
3028 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
3029 	 *    capability req.
3030 	 */
3031 	idls = (dl_capab_dls_t *)(isub + 1);
3032 
3033 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
3034 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
3035 		    "capability isn't as expected; pass-thru "
3036 		    "module(s) detected, discarding capability\n"));
3037 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
3038 			/*
3039 			 * This is a capability renegotitation case.
3040 			 * The interface better be unusable at this
3041 			 * point other wise bad things will happen
3042 			 * if we disable direct calls on a running
3043 			 * and up interface.
3044 			 */
3045 			ill_capability_dls_disable(ill);
3046 		}
3047 		return;
3048 	}
3049 
3050 	switch (idls->dls_flags) {
3051 	default:
3052 		/* Disable if unknown flag */
3053 	case SOFT_RING_DISABLE:
3054 	case POLL_DISABLE:
3055 		ill_capability_dls_disable(ill);
3056 		break;
3057 	case SOFT_RING_CAPABLE:
3058 	case POLL_CAPABLE:
3059 		/*
3060 		 * If the capability was already enabled, its safe
3061 		 * to disable it first to get rid of stale information
3062 		 * and then start enabling it again.
3063 		 */
3064 		ill_capability_dls_disable(ill);
3065 		ill_capability_dls_capable(ill, idls, isub);
3066 		break;
3067 	case SOFT_RING_ENABLE:
3068 	case POLL_ENABLE:
3069 		mutex_enter(&ill->ill_lock);
3070 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3071 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3072 			ASSERT(ill->ill_dls_capab != NULL);
3073 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3074 		}
3075 		if (sub_dl_cap == DL_CAPAB_POLL &&
3076 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3077 			ASSERT(ill->ill_dls_capab != NULL);
3078 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3079 			ip1dbg(("ill_capability_dls_ack: interface %s "
3080 			    "has enabled polling\n", ill->ill_name));
3081 		}
3082 		mutex_exit(&ill->ill_lock);
3083 		break;
3084 	}
3085 }
3086 
3087 /*
3088  * Process a hardware checksum offload capability negotiation ack received
3089  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3090  * of a DL_CAPABILITY_ACK message.
3091  */
3092 static void
3093 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3094 {
3095 	dl_capability_req_t	*ocap;
3096 	dl_capab_hcksum_t	*ihck, *ohck;
3097 	ill_hcksum_capab_t	**ill_hcksum;
3098 	mblk_t			*nmp = NULL;
3099 	uint_t			sub_dl_cap = isub->dl_cap;
3100 	uint8_t			*capend;
3101 
3102 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3103 
3104 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3105 
3106 	/*
3107 	 * Note: range checks here are not absolutely sufficient to
3108 	 * make us robust against malformed messages sent by drivers;
3109 	 * this is in keeping with the rest of IP's dlpi handling.
3110 	 * (Remember, it's coming from something else in the kernel
3111 	 * address space)
3112 	 */
3113 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3114 	if (capend > mp->b_wptr) {
3115 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3116 		    "malformed sub-capability too long for mblk");
3117 		return;
3118 	}
3119 
3120 	/*
3121 	 * There are two types of acks we process here:
3122 	 * 1. acks in reply to a (first form) generic capability req
3123 	 *    (no ENABLE flag set)
3124 	 * 2. acks in reply to a ENABLE capability req.
3125 	 *    (ENABLE flag set)
3126 	 */
3127 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3128 
3129 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3130 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3131 		    "unsupported hardware checksum "
3132 		    "sub-capability (version %d, expected %d)",
3133 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3134 		return;
3135 	}
3136 
3137 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3138 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3139 		    "checksum capability isn't as expected; pass-thru "
3140 		    "module(s) detected, discarding capability\n"));
3141 		return;
3142 	}
3143 
3144 #define	CURR_HCKSUM_CAPAB				\
3145 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3146 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3147 
3148 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3149 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3150 		/* do ENABLE processing */
3151 		if (*ill_hcksum == NULL) {
3152 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3153 			    KM_NOSLEEP);
3154 
3155 			if (*ill_hcksum == NULL) {
3156 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3157 				    "could not enable hcksum version %d "
3158 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3159 				    ill->ill_name);
3160 				return;
3161 			}
3162 		}
3163 
3164 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3165 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3166 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3167 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3168 		    "has enabled hardware checksumming\n ",
3169 		    ill->ill_name));
3170 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3171 		/*
3172 		 * Enabling hardware checksum offload
3173 		 * Currently IP supports {TCP,UDP}/IPv4
3174 		 * partial and full cksum offload and
3175 		 * IPv4 header checksum offload.
3176 		 * Allocate new mblk which will
3177 		 * contain a new capability request
3178 		 * to enable hardware checksum offload.
3179 		 */
3180 		uint_t	size;
3181 		uchar_t	*rptr;
3182 
3183 		size = sizeof (dl_capability_req_t) +
3184 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3185 
3186 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3187 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3188 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3189 			    ill->ill_name);
3190 			return;
3191 		}
3192 
3193 		rptr = nmp->b_rptr;
3194 		/* initialize dl_capability_req_t */
3195 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3196 		ocap->dl_sub_offset =
3197 		    sizeof (dl_capability_req_t);
3198 		ocap->dl_sub_length =
3199 		    sizeof (dl_capability_sub_t) +
3200 		    isub->dl_length;
3201 		nmp->b_rptr += sizeof (dl_capability_req_t);
3202 
3203 		/* initialize dl_capability_sub_t */
3204 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3205 		nmp->b_rptr += sizeof (*isub);
3206 
3207 		/* initialize dl_capab_hcksum_t */
3208 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3209 		bcopy(ihck, ohck, sizeof (*ihck));
3210 
3211 		nmp->b_rptr = rptr;
3212 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3213 
3214 		/* Set ENABLE flag */
3215 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3216 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3217 
3218 		/*
3219 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3220 		 * hardware checksum acceleration.
3221 		 */
3222 		ill_dlpi_send(ill, nmp);
3223 	} else {
3224 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3225 		    "advertised %x hardware checksum capability flags\n",
3226 		    ill->ill_name, ihck->hcksum_txflags));
3227 	}
3228 }
3229 
3230 static void
3231 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3232 {
3233 	mblk_t *mp;
3234 	dl_capab_hcksum_t *hck_subcap;
3235 	dl_capability_sub_t *dl_subcap;
3236 	int size;
3237 
3238 	if (!ILL_HCKSUM_CAPABLE(ill))
3239 		return;
3240 
3241 	ASSERT(ill->ill_hcksum_capab != NULL);
3242 	/*
3243 	 * Clear the capability flag for hardware checksum offload but
3244 	 * retain the ill_hcksum_capab structure since it's possible that
3245 	 * another thread is still referring to it.  The structure only
3246 	 * gets deallocated when we destroy the ill.
3247 	 */
3248 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3249 
3250 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3251 
3252 	mp = allocb(size, BPRI_HI);
3253 	if (mp == NULL) {
3254 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3255 		    "request to disable hardware checksum offload\n"));
3256 		return;
3257 	}
3258 
3259 	mp->b_wptr = mp->b_rptr + size;
3260 
3261 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3262 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3263 	dl_subcap->dl_length = sizeof (*hck_subcap);
3264 
3265 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3266 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3267 	hck_subcap->hcksum_txflags = 0;
3268 
3269 	if (*sc_mp != NULL)
3270 		linkb(*sc_mp, mp);
3271 	else
3272 		*sc_mp = mp;
3273 }
3274 
3275 static void
3276 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3277 {
3278 	mblk_t *nmp = NULL;
3279 	dl_capability_req_t *oc;
3280 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3281 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3282 	uint_t sub_dl_cap = isub->dl_cap;
3283 	uint8_t *capend;
3284 
3285 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3286 
3287 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3288 
3289 	/*
3290 	 * Note: range checks here are not absolutely sufficient to
3291 	 * make us robust against malformed messages sent by drivers;
3292 	 * this is in keeping with the rest of IP's dlpi handling.
3293 	 * (Remember, it's coming from something else in the kernel
3294 	 * address space)
3295 	 */
3296 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3297 	if (capend > mp->b_wptr) {
3298 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3299 		    "malformed sub-capability too long for mblk");
3300 		return;
3301 	}
3302 
3303 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3304 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3305 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3306 		    "unsupported ZEROCOPY sub-capability (version %d, "
3307 		    "expected %d)", zc_ic->zerocopy_version,
3308 		    ZEROCOPY_VERSION_1);
3309 		return;
3310 	}
3311 
3312 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3313 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3314 		    "capability isn't as expected; pass-thru module(s) "
3315 		    "detected, discarding capability\n"));
3316 		return;
3317 	}
3318 
3319 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3320 		if (*ill_zerocopy_capab == NULL) {
3321 			*ill_zerocopy_capab =
3322 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3323 			    KM_NOSLEEP);
3324 
3325 			if (*ill_zerocopy_capab == NULL) {
3326 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3327 				    "could not enable Zero-copy version %d "
3328 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3329 				    ill->ill_name);
3330 				return;
3331 			}
3332 		}
3333 
3334 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3335 		    "supports Zero-copy version %d\n", ill->ill_name,
3336 		    ZEROCOPY_VERSION_1));
3337 
3338 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3339 		    zc_ic->zerocopy_version;
3340 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3341 		    zc_ic->zerocopy_flags;
3342 
3343 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3344 	} else {
3345 		uint_t size;
3346 		uchar_t *rptr;
3347 
3348 		size = sizeof (dl_capability_req_t) +
3349 		    sizeof (dl_capability_sub_t) +
3350 		    sizeof (dl_capab_zerocopy_t);
3351 
3352 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3353 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3354 			    "could not enable zerocopy for %s (ENOMEM)\n",
3355 			    ill->ill_name);
3356 			return;
3357 		}
3358 
3359 		rptr = nmp->b_rptr;
3360 		/* initialize dl_capability_req_t */
3361 		oc = (dl_capability_req_t *)rptr;
3362 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3363 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3364 		    sizeof (dl_capab_zerocopy_t);
3365 		rptr += sizeof (dl_capability_req_t);
3366 
3367 		/* initialize dl_capability_sub_t */
3368 		bcopy(isub, rptr, sizeof (*isub));
3369 		rptr += sizeof (*isub);
3370 
3371 		/* initialize dl_capab_zerocopy_t */
3372 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3373 		*zc_oc = *zc_ic;
3374 
3375 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3376 		    "to enable zero-copy version %d\n", ill->ill_name,
3377 		    ZEROCOPY_VERSION_1));
3378 
3379 		/* set VMSAFE_MEM flag */
3380 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3381 
3382 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3383 		ill_dlpi_send(ill, nmp);
3384 	}
3385 }
3386 
3387 static void
3388 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3389 {
3390 	mblk_t *mp;
3391 	dl_capab_zerocopy_t *zerocopy_subcap;
3392 	dl_capability_sub_t *dl_subcap;
3393 	int size;
3394 
3395 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3396 		return;
3397 
3398 	ASSERT(ill->ill_zerocopy_capab != NULL);
3399 	/*
3400 	 * Clear the capability flag for Zero-copy but retain the
3401 	 * ill_zerocopy_capab structure since it's possible that another
3402 	 * thread is still referring to it.  The structure only gets
3403 	 * deallocated when we destroy the ill.
3404 	 */
3405 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3406 
3407 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3408 
3409 	mp = allocb(size, BPRI_HI);
3410 	if (mp == NULL) {
3411 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3412 		    "request to disable Zero-copy\n"));
3413 		return;
3414 	}
3415 
3416 	mp->b_wptr = mp->b_rptr + size;
3417 
3418 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3419 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3420 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3421 
3422 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3423 	zerocopy_subcap->zerocopy_version =
3424 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3425 	zerocopy_subcap->zerocopy_flags = 0;
3426 
3427 	if (*sc_mp != NULL)
3428 		linkb(*sc_mp, mp);
3429 	else
3430 		*sc_mp = mp;
3431 }
3432 
3433 /*
3434  * Process Large Segment Offload capability negotiation ack received from a
3435  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3436  * DL_CAPABILITY_ACK message.
3437  */
3438 static void
3439 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3440 {
3441 	mblk_t *nmp = NULL;
3442 	dl_capability_req_t *oc;
3443 	dl_capab_lso_t *lso_ic, *lso_oc;
3444 	ill_lso_capab_t **ill_lso_capab;
3445 	uint_t sub_dl_cap = isub->dl_cap;
3446 	uint8_t *capend;
3447 
3448 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3449 
3450 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3451 
3452 	/*
3453 	 * Note: range checks here are not absolutely sufficient to
3454 	 * make us robust against malformed messages sent by drivers;
3455 	 * this is in keeping with the rest of IP's dlpi handling.
3456 	 * (Remember, it's coming from something else in the kernel
3457 	 * address space)
3458 	 */
3459 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3460 	if (capend > mp->b_wptr) {
3461 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3462 		    "malformed sub-capability too long for mblk");
3463 		return;
3464 	}
3465 
3466 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3467 
3468 	if (lso_ic->lso_version != LSO_VERSION_1) {
3469 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3470 		    "unsupported LSO sub-capability (version %d, expected %d)",
3471 		    lso_ic->lso_version, LSO_VERSION_1);
3472 		return;
3473 	}
3474 
3475 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3476 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3477 		    "capability isn't as expected; pass-thru module(s) "
3478 		    "detected, discarding capability\n"));
3479 		return;
3480 	}
3481 
3482 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3483 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3484 		if (*ill_lso_capab == NULL) {
3485 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3486 			    KM_NOSLEEP);
3487 
3488 			if (*ill_lso_capab == NULL) {
3489 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3490 				    "could not enable LSO version %d "
3491 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3492 				    ill->ill_name);
3493 				return;
3494 			}
3495 		}
3496 
3497 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3498 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3499 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3500 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3501 
3502 		ip1dbg(("ill_capability_lso_ack: interface %s "
3503 		    "has enabled LSO\n ", ill->ill_name));
3504 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3505 		uint_t size;
3506 		uchar_t *rptr;
3507 
3508 		size = sizeof (dl_capability_req_t) +
3509 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3510 
3511 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3512 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3513 			    "could not enable LSO for %s (ENOMEM)\n",
3514 			    ill->ill_name);
3515 			return;
3516 		}
3517 
3518 		rptr = nmp->b_rptr;
3519 		/* initialize dl_capability_req_t */
3520 		oc = (dl_capability_req_t *)nmp->b_rptr;
3521 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3522 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3523 		    sizeof (dl_capab_lso_t);
3524 		nmp->b_rptr += sizeof (dl_capability_req_t);
3525 
3526 		/* initialize dl_capability_sub_t */
3527 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3528 		nmp->b_rptr += sizeof (*isub);
3529 
3530 		/* initialize dl_capab_lso_t */
3531 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3532 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3533 
3534 		nmp->b_rptr = rptr;
3535 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3536 
3537 		/* set ENABLE flag */
3538 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3539 
3540 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3541 		ill_dlpi_send(ill, nmp);
3542 	} else {
3543 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3544 		    "advertised %x LSO capability flags\n",
3545 		    ill->ill_name, lso_ic->lso_flags));
3546 	}
3547 }
3548 
3549 
3550 static void
3551 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3552 {
3553 	mblk_t *mp;
3554 	dl_capab_lso_t *lso_subcap;
3555 	dl_capability_sub_t *dl_subcap;
3556 	int size;
3557 
3558 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3559 		return;
3560 
3561 	ASSERT(ill->ill_lso_capab != NULL);
3562 	/*
3563 	 * Clear the capability flag for LSO but retain the
3564 	 * ill_lso_capab structure since it's possible that another
3565 	 * thread is still referring to it.  The structure only gets
3566 	 * deallocated when we destroy the ill.
3567 	 */
3568 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3569 
3570 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3571 
3572 	mp = allocb(size, BPRI_HI);
3573 	if (mp == NULL) {
3574 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3575 		    "request to disable LSO\n"));
3576 		return;
3577 	}
3578 
3579 	mp->b_wptr = mp->b_rptr + size;
3580 
3581 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3582 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3583 	dl_subcap->dl_length = sizeof (*lso_subcap);
3584 
3585 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3586 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3587 	lso_subcap->lso_flags = 0;
3588 
3589 	if (*sc_mp != NULL)
3590 		linkb(*sc_mp, mp);
3591 	else
3592 		*sc_mp = mp;
3593 }
3594 
3595 /*
3596  * Consume a new-style hardware capabilities negotiation ack.
3597  * Called from ip_rput_dlpi_writer().
3598  */
3599 void
3600 ill_capability_ack(ill_t *ill, mblk_t *mp)
3601 {
3602 	dl_capability_ack_t *capp;
3603 	dl_capability_sub_t *subp, *endp;
3604 
3605 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3606 		ill->ill_dlpi_capab_state = IDS_OK;
3607 
3608 	capp = (dl_capability_ack_t *)mp->b_rptr;
3609 
3610 	if (capp->dl_sub_length == 0)
3611 		/* no new-style capabilities */
3612 		return;
3613 
3614 	/* make sure the driver supplied correct dl_sub_length */
3615 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3616 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3617 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3618 		return;
3619 	}
3620 
3621 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3622 	/*
3623 	 * There are sub-capabilities. Process the ones we know about.
3624 	 * Loop until we don't have room for another sub-cap header..
3625 	 */
3626 	for (subp = SC(capp, capp->dl_sub_offset),
3627 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3628 	    subp <= endp;
3629 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3630 
3631 		switch (subp->dl_cap) {
3632 		case DL_CAPAB_ID_WRAPPER:
3633 			ill_capability_id_ack(ill, mp, subp);
3634 			break;
3635 		default:
3636 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3637 			break;
3638 		}
3639 	}
3640 #undef SC
3641 }
3642 
3643 /*
3644  * This routine is called to scan the fragmentation reassembly table for
3645  * the specified ILL for any packets that are starting to smell.
3646  * dead_interval is the maximum time in seconds that will be tolerated.  It
3647  * will either be the value specified in ip_g_frag_timeout, or zero if the
3648  * ILL is shutting down and it is time to blow everything off.
3649  *
3650  * It returns the number of seconds (as a time_t) that the next frag timer
3651  * should be scheduled for, 0 meaning that the timer doesn't need to be
3652  * re-started.  Note that the method of calculating next_timeout isn't
3653  * entirely accurate since time will flow between the time we grab
3654  * current_time and the time we schedule the next timeout.  This isn't a
3655  * big problem since this is the timer for sending an ICMP reassembly time
3656  * exceeded messages, and it doesn't have to be exactly accurate.
3657  *
3658  * This function is
3659  * sometimes called as writer, although this is not required.
3660  */
3661 time_t
3662 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3663 {
3664 	ipfb_t	*ipfb;
3665 	ipfb_t	*endp;
3666 	ipf_t	*ipf;
3667 	ipf_t	*ipfnext;
3668 	mblk_t	*mp;
3669 	time_t	current_time = gethrestime_sec();
3670 	time_t	next_timeout = 0;
3671 	uint32_t	hdr_length;
3672 	mblk_t	*send_icmp_head;
3673 	mblk_t	*send_icmp_head_v6;
3674 	zoneid_t zoneid;
3675 	ip_stack_t *ipst = ill->ill_ipst;
3676 
3677 	ipfb = ill->ill_frag_hash_tbl;
3678 	if (ipfb == NULL)
3679 		return (B_FALSE);
3680 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3681 	/* Walk the frag hash table. */
3682 	for (; ipfb < endp; ipfb++) {
3683 		send_icmp_head = NULL;
3684 		send_icmp_head_v6 = NULL;
3685 		mutex_enter(&ipfb->ipfb_lock);
3686 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3687 			time_t frag_time = current_time - ipf->ipf_timestamp;
3688 			time_t frag_timeout;
3689 
3690 			if (frag_time < dead_interval) {
3691 				/*
3692 				 * There are some outstanding fragments
3693 				 * that will timeout later.  Make note of
3694 				 * the time so that we can reschedule the
3695 				 * next timeout appropriately.
3696 				 */
3697 				frag_timeout = dead_interval - frag_time;
3698 				if (next_timeout == 0 ||
3699 				    frag_timeout < next_timeout) {
3700 					next_timeout = frag_timeout;
3701 				}
3702 				break;
3703 			}
3704 			/* Time's up.  Get it out of here. */
3705 			hdr_length = ipf->ipf_nf_hdr_len;
3706 			ipfnext = ipf->ipf_hash_next;
3707 			if (ipfnext)
3708 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3709 			*ipf->ipf_ptphn = ipfnext;
3710 			mp = ipf->ipf_mp->b_cont;
3711 			for (; mp; mp = mp->b_cont) {
3712 				/* Extra points for neatness. */
3713 				IP_REASS_SET_START(mp, 0);
3714 				IP_REASS_SET_END(mp, 0);
3715 			}
3716 			mp = ipf->ipf_mp->b_cont;
3717 			ill->ill_frag_count -= ipf->ipf_count;
3718 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3719 			ipfb->ipfb_count -= ipf->ipf_count;
3720 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3721 			ipfb->ipfb_frag_pkts--;
3722 			/*
3723 			 * We do not send any icmp message from here because
3724 			 * we currently are holding the ipfb_lock for this
3725 			 * hash chain. If we try and send any icmp messages
3726 			 * from here we may end up via a put back into ip
3727 			 * trying to get the same lock, causing a recursive
3728 			 * mutex panic. Instead we build a list and send all
3729 			 * the icmp messages after we have dropped the lock.
3730 			 */
3731 			if (ill->ill_isv6) {
3732 				if (hdr_length != 0) {
3733 					mp->b_next = send_icmp_head_v6;
3734 					send_icmp_head_v6 = mp;
3735 				} else {
3736 					freemsg(mp);
3737 				}
3738 			} else {
3739 				if (hdr_length != 0) {
3740 					mp->b_next = send_icmp_head;
3741 					send_icmp_head = mp;
3742 				} else {
3743 					freemsg(mp);
3744 				}
3745 			}
3746 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3747 			freeb(ipf->ipf_mp);
3748 		}
3749 		mutex_exit(&ipfb->ipfb_lock);
3750 		/*
3751 		 * Now need to send any icmp messages that we delayed from
3752 		 * above.
3753 		 */
3754 		while (send_icmp_head_v6 != NULL) {
3755 			ip6_t *ip6h;
3756 
3757 			mp = send_icmp_head_v6;
3758 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3759 			mp->b_next = NULL;
3760 			if (mp->b_datap->db_type == M_CTL)
3761 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3762 			else
3763 				ip6h = (ip6_t *)mp->b_rptr;
3764 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3765 			    ill, ipst);
3766 			if (zoneid == ALL_ZONES) {
3767 				freemsg(mp);
3768 			} else {
3769 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3770 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3771 				    B_FALSE, zoneid, ipst);
3772 			}
3773 		}
3774 		while (send_icmp_head != NULL) {
3775 			ipaddr_t dst;
3776 
3777 			mp = send_icmp_head;
3778 			send_icmp_head = send_icmp_head->b_next;
3779 			mp->b_next = NULL;
3780 
3781 			if (mp->b_datap->db_type == M_CTL)
3782 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3783 			else
3784 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3785 
3786 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3787 			if (zoneid == ALL_ZONES) {
3788 				freemsg(mp);
3789 			} else {
3790 				icmp_time_exceeded(ill->ill_wq, mp,
3791 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3792 				    ipst);
3793 			}
3794 		}
3795 	}
3796 	/*
3797 	 * A non-dying ILL will use the return value to decide whether to
3798 	 * restart the frag timer, and for how long.
3799 	 */
3800 	return (next_timeout);
3801 }
3802 
3803 /*
3804  * This routine is called when the approximate count of mblk memory used
3805  * for the specified ILL has exceeded max_count.
3806  */
3807 void
3808 ill_frag_prune(ill_t *ill, uint_t max_count)
3809 {
3810 	ipfb_t	*ipfb;
3811 	ipf_t	*ipf;
3812 	size_t	count;
3813 
3814 	/*
3815 	 * If we are here within ip_min_frag_prune_time msecs remove
3816 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3817 	 * ill_frag_free_num_pkts.
3818 	 */
3819 	mutex_enter(&ill->ill_lock);
3820 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3821 	    (ip_min_frag_prune_time != 0 ?
3822 	    ip_min_frag_prune_time : msec_per_tick)) {
3823 
3824 		ill->ill_frag_free_num_pkts++;
3825 
3826 	} else {
3827 		ill->ill_frag_free_num_pkts = 0;
3828 	}
3829 	ill->ill_last_frag_clean_time = lbolt;
3830 	mutex_exit(&ill->ill_lock);
3831 
3832 	/*
3833 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3834 	 */
3835 	if (ill->ill_frag_free_num_pkts != 0) {
3836 		int ix;
3837 
3838 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3839 			ipfb = &ill->ill_frag_hash_tbl[ix];
3840 			mutex_enter(&ipfb->ipfb_lock);
3841 			if (ipfb->ipfb_ipf != NULL) {
3842 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3843 				    ill->ill_frag_free_num_pkts);
3844 			}
3845 			mutex_exit(&ipfb->ipfb_lock);
3846 		}
3847 	}
3848 	/*
3849 	 * While the reassembly list for this ILL is too big, prune a fragment
3850 	 * queue by age, oldest first.  Note that the per ILL count is
3851 	 * approximate, while the per frag hash bucket counts are accurate.
3852 	 */
3853 	while (ill->ill_frag_count > max_count) {
3854 		int	ix;
3855 		ipfb_t	*oipfb = NULL;
3856 		uint_t	oldest = UINT_MAX;
3857 
3858 		count = 0;
3859 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3860 			ipfb = &ill->ill_frag_hash_tbl[ix];
3861 			mutex_enter(&ipfb->ipfb_lock);
3862 			ipf = ipfb->ipfb_ipf;
3863 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3864 				oldest = ipf->ipf_gen;
3865 				oipfb = ipfb;
3866 			}
3867 			count += ipfb->ipfb_count;
3868 			mutex_exit(&ipfb->ipfb_lock);
3869 		}
3870 		/* Refresh the per ILL count */
3871 		ill->ill_frag_count = count;
3872 		if (oipfb == NULL) {
3873 			ill->ill_frag_count = 0;
3874 			break;
3875 		}
3876 		if (count <= max_count)
3877 			return;	/* Somebody beat us to it, nothing to do */
3878 		mutex_enter(&oipfb->ipfb_lock);
3879 		ipf = oipfb->ipfb_ipf;
3880 		if (ipf != NULL) {
3881 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3882 		}
3883 		mutex_exit(&oipfb->ipfb_lock);
3884 	}
3885 }
3886 
3887 /*
3888  * free 'free_cnt' fragmented packets starting at ipf.
3889  */
3890 void
3891 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3892 {
3893 	size_t	count;
3894 	mblk_t	*mp;
3895 	mblk_t	*tmp;
3896 	ipf_t **ipfp = ipf->ipf_ptphn;
3897 
3898 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3899 	ASSERT(ipfp != NULL);
3900 	ASSERT(ipf != NULL);
3901 
3902 	while (ipf != NULL && free_cnt-- > 0) {
3903 		count = ipf->ipf_count;
3904 		mp = ipf->ipf_mp;
3905 		ipf = ipf->ipf_hash_next;
3906 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3907 			IP_REASS_SET_START(tmp, 0);
3908 			IP_REASS_SET_END(tmp, 0);
3909 		}
3910 		ill->ill_frag_count -= count;
3911 		ASSERT(ipfb->ipfb_count >= count);
3912 		ipfb->ipfb_count -= count;
3913 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3914 		ipfb->ipfb_frag_pkts--;
3915 		freemsg(mp);
3916 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3917 	}
3918 
3919 	if (ipf)
3920 		ipf->ipf_ptphn = ipfp;
3921 	ipfp[0] = ipf;
3922 }
3923 
3924 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3925 	"obsolete and may be removed in a future release of Solaris.  Use " \
3926 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3927 
3928 /*
3929  * For obsolete per-interface forwarding configuration;
3930  * called in response to ND_GET.
3931  */
3932 /* ARGSUSED */
3933 static int
3934 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3935 {
3936 	ill_t *ill = (ill_t *)cp;
3937 
3938 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3939 
3940 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3941 	return (0);
3942 }
3943 
3944 /*
3945  * For obsolete per-interface forwarding configuration;
3946  * called in response to ND_SET.
3947  */
3948 /* ARGSUSED */
3949 static int
3950 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3951     cred_t *ioc_cr)
3952 {
3953 	long value;
3954 	int retval;
3955 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3956 
3957 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3958 
3959 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3960 	    value < 0 || value > 1) {
3961 		return (EINVAL);
3962 	}
3963 
3964 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3965 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3966 	rw_exit(&ipst->ips_ill_g_lock);
3967 	return (retval);
3968 }
3969 
3970 /*
3971  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3972  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3973  * up RTS_IFINFO routing socket messages for each interface whose flags we
3974  * change.
3975  */
3976 int
3977 ill_forward_set(ill_t *ill, boolean_t enable)
3978 {
3979 	ill_group_t *illgrp;
3980 	ip_stack_t	*ipst = ill->ill_ipst;
3981 
3982 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3983 
3984 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3985 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3986 		return (0);
3987 
3988 	if (IS_LOOPBACK(ill))
3989 		return (EINVAL);
3990 
3991 	/*
3992 	 * If the ill is in an IPMP group, set the forwarding policy on all
3993 	 * members of the group to the same value.
3994 	 */
3995 	illgrp = ill->ill_group;
3996 	if (illgrp != NULL) {
3997 		ill_t *tmp_ill;
3998 
3999 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
4000 		    tmp_ill = tmp_ill->ill_group_next) {
4001 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
4002 			    (enable ? "Enabling" : "Disabling"),
4003 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
4004 			    tmp_ill->ill_name));
4005 			mutex_enter(&tmp_ill->ill_lock);
4006 			if (enable)
4007 				tmp_ill->ill_flags |= ILLF_ROUTER;
4008 			else
4009 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
4010 			mutex_exit(&tmp_ill->ill_lock);
4011 			if (tmp_ill->ill_isv6)
4012 				ill_set_nce_router_flags(tmp_ill, enable);
4013 			/* Notify routing socket listeners of this change. */
4014 			ip_rts_ifmsg(tmp_ill->ill_ipif);
4015 		}
4016 	} else {
4017 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
4018 		    (enable ? "Enabling" : "Disabling"),
4019 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
4020 		mutex_enter(&ill->ill_lock);
4021 		if (enable)
4022 			ill->ill_flags |= ILLF_ROUTER;
4023 		else
4024 			ill->ill_flags &= ~ILLF_ROUTER;
4025 		mutex_exit(&ill->ill_lock);
4026 		if (ill->ill_isv6)
4027 			ill_set_nce_router_flags(ill, enable);
4028 		/* Notify routing socket listeners of this change. */
4029 		ip_rts_ifmsg(ill->ill_ipif);
4030 	}
4031 
4032 	return (0);
4033 }
4034 
4035 /*
4036  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
4037  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
4038  * set or clear.
4039  */
4040 static void
4041 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
4042 {
4043 	ipif_t *ipif;
4044 	nce_t *nce;
4045 
4046 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4047 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
4048 		if (nce != NULL) {
4049 			mutex_enter(&nce->nce_lock);
4050 			if (enable)
4051 				nce->nce_flags |= NCE_F_ISROUTER;
4052 			else
4053 				nce->nce_flags &= ~NCE_F_ISROUTER;
4054 			mutex_exit(&nce->nce_lock);
4055 			NCE_REFRELE(nce);
4056 		}
4057 	}
4058 }
4059 
4060 /*
4061  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
4062  * for this ill.  Make sure the v6/v4 question has been answered about this
4063  * ill.  The creation of this ndd variable is only for backwards compatibility.
4064  * The preferred way to control per-interface IP forwarding is through the
4065  * ILLF_ROUTER interface flag.
4066  */
4067 static int
4068 ill_set_ndd_name(ill_t *ill)
4069 {
4070 	char *suffix;
4071 	ip_stack_t	*ipst = ill->ill_ipst;
4072 
4073 	ASSERT(IAM_WRITER_ILL(ill));
4074 
4075 	if (ill->ill_isv6)
4076 		suffix = ipv6_forward_suffix;
4077 	else
4078 		suffix = ipv4_forward_suffix;
4079 
4080 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4081 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4082 	/*
4083 	 * Copies over the '\0'.
4084 	 * Note that strlen(suffix) is always bounded.
4085 	 */
4086 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4087 	    strlen(suffix) + 1);
4088 
4089 	/*
4090 	 * Use of the nd table requires holding the reader lock.
4091 	 * Modifying the nd table thru nd_load/nd_unload requires
4092 	 * the writer lock.
4093 	 */
4094 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
4095 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4096 	    nd_ill_forward_set, (caddr_t)ill)) {
4097 		/*
4098 		 * If the nd_load failed, it only meant that it could not
4099 		 * allocate a new bunch of room for further NDD expansion.
4100 		 * Because of that, the ill_ndd_name will be set to 0, and
4101 		 * this interface is at the mercy of the global ip_forwarding
4102 		 * variable.
4103 		 */
4104 		rw_exit(&ipst->ips_ip_g_nd_lock);
4105 		ill->ill_ndd_name = NULL;
4106 		return (ENOMEM);
4107 	}
4108 	rw_exit(&ipst->ips_ip_g_nd_lock);
4109 	return (0);
4110 }
4111 
4112 /*
4113  * Intializes the context structure and returns the first ill in the list
4114  * cuurently start_list and end_list can have values:
4115  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4116  * IP_V4_G_HEAD		Traverse IPV4 list only.
4117  * IP_V6_G_HEAD		Traverse IPV6 list only.
4118  */
4119 
4120 /*
4121  * We don't check for CONDEMNED ills here. Caller must do that if
4122  * necessary under the ill lock.
4123  */
4124 ill_t *
4125 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
4126     ip_stack_t *ipst)
4127 {
4128 	ill_if_t *ifp;
4129 	ill_t *ill;
4130 	avl_tree_t *avl_tree;
4131 
4132 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4133 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4134 
4135 	/*
4136 	 * setup the lists to search
4137 	 */
4138 	if (end_list != MAX_G_HEADS) {
4139 		ctx->ctx_current_list = start_list;
4140 		ctx->ctx_last_list = end_list;
4141 	} else {
4142 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4143 		ctx->ctx_current_list = 0;
4144 	}
4145 
4146 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4147 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4148 		if (ifp != (ill_if_t *)
4149 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4150 			avl_tree = &ifp->illif_avl_by_ppa;
4151 			ill = avl_first(avl_tree);
4152 			/*
4153 			 * ill is guaranteed to be non NULL or ifp should have
4154 			 * not existed.
4155 			 */
4156 			ASSERT(ill != NULL);
4157 			return (ill);
4158 		}
4159 		ctx->ctx_current_list++;
4160 	}
4161 
4162 	return (NULL);
4163 }
4164 
4165 /*
4166  * returns the next ill in the list. ill_first() must have been called
4167  * before calling ill_next() or bad things will happen.
4168  */
4169 
4170 /*
4171  * We don't check for CONDEMNED ills here. Caller must do that if
4172  * necessary under the ill lock.
4173  */
4174 ill_t *
4175 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4176 {
4177 	ill_if_t *ifp;
4178 	ill_t *ill;
4179 	ip_stack_t	*ipst = lastill->ill_ipst;
4180 
4181 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4182 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
4183 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4184 	    AVL_AFTER)) != NULL) {
4185 		return (ill);
4186 	}
4187 
4188 	/* goto next ill_ifp in the list. */
4189 	ifp = lastill->ill_ifptr->illif_next;
4190 
4191 	/* make sure not at end of circular list */
4192 	while (ifp ==
4193 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4194 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4195 			return (NULL);
4196 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4197 	}
4198 
4199 	return (avl_first(&ifp->illif_avl_by_ppa));
4200 }
4201 
4202 /*
4203  * Check interface name for correct format which is name+ppa.
4204  * name can contain characters and digits, the right most digits
4205  * make up the ppa number. use of octal is not allowed, name must contain
4206  * a ppa, return pointer to the start of ppa.
4207  * In case of error return NULL.
4208  */
4209 static char *
4210 ill_get_ppa_ptr(char *name)
4211 {
4212 	int namelen = mi_strlen(name);
4213 
4214 	int len = namelen;
4215 
4216 	name += len;
4217 	while (len > 0) {
4218 		name--;
4219 		if (*name < '0' || *name > '9')
4220 			break;
4221 		len--;
4222 	}
4223 
4224 	/* empty string, all digits, or no trailing digits */
4225 	if (len == 0 || len == (int)namelen)
4226 		return (NULL);
4227 
4228 	name++;
4229 	/* check for attempted use of octal */
4230 	if (*name == '0' && len != (int)namelen - 1)
4231 		return (NULL);
4232 	return (name);
4233 }
4234 
4235 /*
4236  * use avl tree to locate the ill.
4237  */
4238 static ill_t *
4239 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4240     ipsq_func_t func, int *error, ip_stack_t *ipst)
4241 {
4242 	char *ppa_ptr = NULL;
4243 	int len;
4244 	uint_t ppa;
4245 	ill_t *ill = NULL;
4246 	ill_if_t *ifp;
4247 	int list;
4248 	ipsq_t *ipsq;
4249 
4250 	if (error != NULL)
4251 		*error = 0;
4252 
4253 	/*
4254 	 * get ppa ptr
4255 	 */
4256 	if (isv6)
4257 		list = IP_V6_G_HEAD;
4258 	else
4259 		list = IP_V4_G_HEAD;
4260 
4261 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4262 		if (error != NULL)
4263 			*error = ENXIO;
4264 		return (NULL);
4265 	}
4266 
4267 	len = ppa_ptr - name + 1;
4268 
4269 	ppa = stoi(&ppa_ptr);
4270 
4271 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4272 
4273 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4274 		/*
4275 		 * match is done on len - 1 as the name is not null
4276 		 * terminated it contains ppa in addition to the interface
4277 		 * name.
4278 		 */
4279 		if ((ifp->illif_name_len == len) &&
4280 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4281 			break;
4282 		} else {
4283 			ifp = ifp->illif_next;
4284 		}
4285 	}
4286 
4287 
4288 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4289 		/*
4290 		 * Even the interface type does not exist.
4291 		 */
4292 		if (error != NULL)
4293 			*error = ENXIO;
4294 		return (NULL);
4295 	}
4296 
4297 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4298 	if (ill != NULL) {
4299 		/*
4300 		 * The block comment at the start of ipif_down
4301 		 * explains the use of the macros used below
4302 		 */
4303 		GRAB_CONN_LOCK(q);
4304 		mutex_enter(&ill->ill_lock);
4305 		if (ILL_CAN_LOOKUP(ill)) {
4306 			ill_refhold_locked(ill);
4307 			mutex_exit(&ill->ill_lock);
4308 			RELEASE_CONN_LOCK(q);
4309 			return (ill);
4310 		} else if (ILL_CAN_WAIT(ill, q)) {
4311 			ipsq = ill->ill_phyint->phyint_ipsq;
4312 			mutex_enter(&ipsq->ipsq_lock);
4313 			mutex_exit(&ill->ill_lock);
4314 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4315 			mutex_exit(&ipsq->ipsq_lock);
4316 			RELEASE_CONN_LOCK(q);
4317 			*error = EINPROGRESS;
4318 			return (NULL);
4319 		}
4320 		mutex_exit(&ill->ill_lock);
4321 		RELEASE_CONN_LOCK(q);
4322 	}
4323 	if (error != NULL)
4324 		*error = ENXIO;
4325 	return (NULL);
4326 }
4327 
4328 /*
4329  * comparison function for use with avl.
4330  */
4331 static int
4332 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4333 {
4334 	uint_t ppa;
4335 	uint_t ill_ppa;
4336 
4337 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4338 
4339 	ppa = *((uint_t *)ppa_ptr);
4340 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4341 	/*
4342 	 * We want the ill with the lowest ppa to be on the
4343 	 * top.
4344 	 */
4345 	if (ill_ppa < ppa)
4346 		return (1);
4347 	if (ill_ppa > ppa)
4348 		return (-1);
4349 	return (0);
4350 }
4351 
4352 /*
4353  * remove an interface type from the global list.
4354  */
4355 static void
4356 ill_delete_interface_type(ill_if_t *interface)
4357 {
4358 	ASSERT(interface != NULL);
4359 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4360 
4361 	avl_destroy(&interface->illif_avl_by_ppa);
4362 	if (interface->illif_ppa_arena != NULL)
4363 		vmem_destroy(interface->illif_ppa_arena);
4364 
4365 	remque(interface);
4366 
4367 	mi_free(interface);
4368 }
4369 
4370 /* Defined in ip_netinfo.c */
4371 extern ddi_taskq_t	*eventq_queue_nic;
4372 
4373 /*
4374  * remove ill from the global list.
4375  */
4376 static void
4377 ill_glist_delete(ill_t *ill)
4378 {
4379 	char *nicname;
4380 	size_t nicnamelen;
4381 	hook_nic_event_t *info;
4382 	ip_stack_t	*ipst;
4383 
4384 	if (ill == NULL)
4385 		return;
4386 	ipst = ill->ill_ipst;
4387 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4388 
4389 	if (ill->ill_name != NULL) {
4390 		nicname = kmem_alloc(ill->ill_name_length, KM_NOSLEEP);
4391 		if (nicname != NULL) {
4392 			bcopy(ill->ill_name, nicname, ill->ill_name_length);
4393 			nicnamelen = ill->ill_name_length;
4394 		}
4395 	} else {
4396 		nicname = NULL;
4397 		nicnamelen = 0;
4398 	}
4399 
4400 	/*
4401 	 * If the ill was never inserted into the AVL tree
4402 	 * we skip the if branch.
4403 	 */
4404 	if (ill->ill_ifptr != NULL) {
4405 		/*
4406 		 * remove from AVL tree and free ppa number
4407 		 */
4408 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4409 
4410 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4411 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4412 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4413 		}
4414 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4415 			ill_delete_interface_type(ill->ill_ifptr);
4416 		}
4417 
4418 		/*
4419 		 * Indicate ill is no longer in the list.
4420 		 */
4421 		ill->ill_ifptr = NULL;
4422 		ill->ill_name_length = 0;
4423 		ill->ill_name[0] = '\0';
4424 		ill->ill_ppa = UINT_MAX;
4425 	}
4426 
4427 	/*
4428 	 * Run the unplumb hook after the NIC has disappeared from being
4429 	 * visible so that attempts to revalidate its existance will fail.
4430 	 *
4431 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4432 	 * that the ordering of delivered events to listeners matches the
4433 	 * order of them in the kernel.
4434 	 */
4435 	if ((info = ill->ill_nic_event_info) != NULL) {
4436 		if (info->hne_event != NE_DOWN) {
4437 			ip2dbg(("ill_glist_delete: unexpected nic event %d "
4438 			    "attached for %s\n", info->hne_event,
4439 			    ill->ill_name));
4440 			if (info->hne_data != NULL)
4441 				kmem_free(info->hne_data, info->hne_datalen);
4442 			kmem_free(info, sizeof (hook_nic_event_t));
4443 		} else {
4444 			if (ddi_taskq_dispatch(eventq_queue_nic,
4445 			    ip_ne_queue_func, (void *)info, DDI_SLEEP)
4446 			    == DDI_FAILURE) {
4447 				ip2dbg(("ill_glist_delete: ddi_taskq_dispatch "
4448 				    "failed\n"));
4449 				if (info->hne_data != NULL)
4450 					kmem_free(info->hne_data,
4451 					    info->hne_datalen);
4452 				kmem_free(info, sizeof (hook_nic_event_t));
4453 			}
4454 		}
4455 	}
4456 
4457 	/* Generate NE_UNPLUMB event for ill_name. */
4458 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
4459 	if (info != NULL) {
4460 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
4461 		info->hne_lif = 0;
4462 		info->hne_event = NE_UNPLUMB;
4463 		info->hne_data = nicname;
4464 		info->hne_datalen = nicnamelen;
4465 		info->hne_family = ill->ill_isv6 ?
4466 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
4467 	} else {
4468 		ip2dbg(("ill_glist_delete: could not attach UNPLUMB nic event "
4469 		    "information for %s (ENOMEM)\n", ill->ill_name));
4470 		if (nicname != NULL)
4471 			kmem_free(nicname, nicnamelen);
4472 	}
4473 
4474 	ill->ill_nic_event_info = info;
4475 
4476 	ill_phyint_free(ill);
4477 	rw_exit(&ipst->ips_ill_g_lock);
4478 }
4479 
4480 /*
4481  * allocate a ppa, if the number of plumbed interfaces of this type are
4482  * less than ill_no_arena do a linear search to find a unused ppa.
4483  * When the number goes beyond ill_no_arena switch to using an arena.
4484  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4485  * is the return value for an error condition, so allocation starts at one
4486  * and is decremented by one.
4487  */
4488 static int
4489 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4490 {
4491 	ill_t *tmp_ill;
4492 	uint_t start, end;
4493 	int ppa;
4494 
4495 	if (ifp->illif_ppa_arena == NULL &&
4496 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4497 		/*
4498 		 * Create an arena.
4499 		 */
4500 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4501 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4502 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4503 			/* allocate what has already been assigned */
4504 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4505 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4506 		    tmp_ill, AVL_AFTER)) {
4507 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4508 			    1,		/* size */
4509 			    1,		/* align/quantum */
4510 			    0,		/* phase */
4511 			    0,		/* nocross */
4512 			    /* minaddr */
4513 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4514 			    /* maxaddr */
4515 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4516 			    VM_NOSLEEP|VM_FIRSTFIT);
4517 			if (ppa == 0) {
4518 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4519 				    " failed while switching"));
4520 				vmem_destroy(ifp->illif_ppa_arena);
4521 				ifp->illif_ppa_arena = NULL;
4522 				break;
4523 			}
4524 		}
4525 	}
4526 
4527 	if (ifp->illif_ppa_arena != NULL) {
4528 		if (ill->ill_ppa == UINT_MAX) {
4529 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4530 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4531 			if (ppa == 0)
4532 				return (EAGAIN);
4533 			ill->ill_ppa = --ppa;
4534 		} else {
4535 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4536 			    1, 		/* size */
4537 			    1, 		/* align/quantum */
4538 			    0, 		/* phase */
4539 			    0, 		/* nocross */
4540 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4541 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4542 			    VM_NOSLEEP|VM_FIRSTFIT);
4543 			/*
4544 			 * Most likely the allocation failed because
4545 			 * the requested ppa was in use.
4546 			 */
4547 			if (ppa == 0)
4548 				return (EEXIST);
4549 		}
4550 		return (0);
4551 	}
4552 
4553 	/*
4554 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4555 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4556 	 */
4557 	if (ill->ill_ppa == UINT_MAX) {
4558 		end = UINT_MAX - 1;
4559 		start = 0;
4560 	} else {
4561 		end = start = ill->ill_ppa;
4562 	}
4563 
4564 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4565 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4566 		if (start++ >= end) {
4567 			if (ill->ill_ppa == UINT_MAX)
4568 				return (EAGAIN);
4569 			else
4570 				return (EEXIST);
4571 		}
4572 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4573 	}
4574 	ill->ill_ppa = start;
4575 	return (0);
4576 }
4577 
4578 /*
4579  * Insert ill into the list of configured ill's. Once this function completes,
4580  * the ill is globally visible and is available through lookups. More precisely
4581  * this happens after the caller drops the ill_g_lock.
4582  */
4583 static int
4584 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4585 {
4586 	ill_if_t *ill_interface;
4587 	avl_index_t where = 0;
4588 	int error;
4589 	int name_length;
4590 	int index;
4591 	boolean_t check_length = B_FALSE;
4592 	ip_stack_t	*ipst = ill->ill_ipst;
4593 
4594 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4595 
4596 	name_length = mi_strlen(name) + 1;
4597 
4598 	if (isv6)
4599 		index = IP_V6_G_HEAD;
4600 	else
4601 		index = IP_V4_G_HEAD;
4602 
4603 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4604 	/*
4605 	 * Search for interface type based on name
4606 	 */
4607 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4608 		if ((ill_interface->illif_name_len == name_length) &&
4609 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4610 			break;
4611 		}
4612 		ill_interface = ill_interface->illif_next;
4613 	}
4614 
4615 	/*
4616 	 * Interface type not found, create one.
4617 	 */
4618 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4619 
4620 		ill_g_head_t ghead;
4621 
4622 		/*
4623 		 * allocate ill_if_t structure
4624 		 */
4625 
4626 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4627 		if (ill_interface == NULL) {
4628 			return (ENOMEM);
4629 		}
4630 
4631 
4632 
4633 		(void) strcpy(ill_interface->illif_name, name);
4634 		ill_interface->illif_name_len = name_length;
4635 
4636 		avl_create(&ill_interface->illif_avl_by_ppa,
4637 		    ill_compare_ppa, sizeof (ill_t),
4638 		    offsetof(struct ill_s, ill_avl_byppa));
4639 
4640 		/*
4641 		 * link the structure in the back to maintain order
4642 		 * of configuration for ifconfig output.
4643 		 */
4644 		ghead = ipst->ips_ill_g_heads[index];
4645 		insque(ill_interface, ghead.ill_g_list_tail);
4646 
4647 	}
4648 
4649 	if (ill->ill_ppa == UINT_MAX)
4650 		check_length = B_TRUE;
4651 
4652 	error = ill_alloc_ppa(ill_interface, ill);
4653 	if (error != 0) {
4654 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4655 			ill_delete_interface_type(ill->ill_ifptr);
4656 		return (error);
4657 	}
4658 
4659 	/*
4660 	 * When the ppa is choosen by the system, check that there is
4661 	 * enough space to insert ppa. if a specific ppa was passed in this
4662 	 * check is not required as the interface name passed in will have
4663 	 * the right ppa in it.
4664 	 */
4665 	if (check_length) {
4666 		/*
4667 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4668 		 */
4669 		char buf[sizeof (uint_t) * 3];
4670 
4671 		/*
4672 		 * convert ppa to string to calculate the amount of space
4673 		 * required for it in the name.
4674 		 */
4675 		numtos(ill->ill_ppa, buf);
4676 
4677 		/* Do we have enough space to insert ppa ? */
4678 
4679 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4680 			/* Free ppa and interface type struct */
4681 			if (ill_interface->illif_ppa_arena != NULL) {
4682 				vmem_free(ill_interface->illif_ppa_arena,
4683 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4684 			}
4685 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4686 			    0) {
4687 				ill_delete_interface_type(ill->ill_ifptr);
4688 			}
4689 
4690 			return (EINVAL);
4691 		}
4692 	}
4693 
4694 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4695 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4696 
4697 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4698 	    &where);
4699 	ill->ill_ifptr = ill_interface;
4700 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4701 
4702 	ill_phyint_reinit(ill);
4703 	return (0);
4704 }
4705 
4706 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4707 static boolean_t
4708 ipsq_init(ill_t *ill)
4709 {
4710 	ipsq_t  *ipsq;
4711 
4712 	/* Init the ipsq and impicitly enter as writer */
4713 	ill->ill_phyint->phyint_ipsq =
4714 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4715 	if (ill->ill_phyint->phyint_ipsq == NULL)
4716 		return (B_FALSE);
4717 	ipsq = ill->ill_phyint->phyint_ipsq;
4718 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4719 	ill->ill_phyint->phyint_ipsq_next = NULL;
4720 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4721 	ipsq->ipsq_refs = 1;
4722 	ipsq->ipsq_writer = curthread;
4723 	ipsq->ipsq_reentry_cnt = 1;
4724 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4725 #ifdef ILL_DEBUG
4726 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack, IP_STACK_DEPTH);
4727 #endif
4728 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4729 	return (B_TRUE);
4730 }
4731 
4732 /*
4733  * ill_init is called by ip_open when a device control stream is opened.
4734  * It does a few initializations, and shoots a DL_INFO_REQ message down
4735  * to the driver.  The response is later picked up in ip_rput_dlpi and
4736  * used to set up default mechanisms for talking to the driver.  (Always
4737  * called as writer.)
4738  *
4739  * If this function returns error, ip_open will call ip_close which in
4740  * turn will call ill_delete to clean up any memory allocated here that
4741  * is not yet freed.
4742  */
4743 int
4744 ill_init(queue_t *q, ill_t *ill)
4745 {
4746 	int	count;
4747 	dl_info_req_t	*dlir;
4748 	mblk_t	*info_mp;
4749 	uchar_t *frag_ptr;
4750 
4751 	/*
4752 	 * The ill is initialized to zero by mi_alloc*(). In addition
4753 	 * some fields already contain valid values, initialized in
4754 	 * ip_open(), before we reach here.
4755 	 */
4756 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4757 
4758 	ill->ill_rq = q;
4759 	ill->ill_wq = WR(q);
4760 
4761 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4762 	    BPRI_HI);
4763 	if (info_mp == NULL)
4764 		return (ENOMEM);
4765 
4766 	/*
4767 	 * Allocate sufficient space to contain our fragment hash table and
4768 	 * the device name.
4769 	 */
4770 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4771 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4772 	if (frag_ptr == NULL) {
4773 		freemsg(info_mp);
4774 		return (ENOMEM);
4775 	}
4776 	ill->ill_frag_ptr = frag_ptr;
4777 	ill->ill_frag_free_num_pkts = 0;
4778 	ill->ill_last_frag_clean_time = 0;
4779 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4780 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4781 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4782 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4783 		    NULL, MUTEX_DEFAULT, NULL);
4784 	}
4785 
4786 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4787 	if (ill->ill_phyint == NULL) {
4788 		freemsg(info_mp);
4789 		mi_free(frag_ptr);
4790 		return (ENOMEM);
4791 	}
4792 
4793 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4794 	/*
4795 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4796 	 * at this point because of the following reason. If we can't
4797 	 * enter the ipsq at some point and cv_wait, the writer that
4798 	 * wakes us up tries to locate us using the list of all phyints
4799 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4800 	 * If we don't set it now, we risk a missed wakeup.
4801 	 */
4802 	ill->ill_phyint->phyint_illv4 = ill;
4803 	ill->ill_ppa = UINT_MAX;
4804 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4805 
4806 	if (!ipsq_init(ill)) {
4807 		freemsg(info_mp);
4808 		mi_free(frag_ptr);
4809 		mi_free(ill->ill_phyint);
4810 		return (ENOMEM);
4811 	}
4812 
4813 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4814 
4815 
4816 	/* Frag queue limit stuff */
4817 	ill->ill_frag_count = 0;
4818 	ill->ill_ipf_gen = 0;
4819 
4820 	ill->ill_global_timer = INFINITY;
4821 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
4822 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4823 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4824 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4825 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4826 
4827 	/*
4828 	 * Initialize IPv6 configuration variables.  The IP module is always
4829 	 * opened as an IPv4 module.  Instead tracking down the cases where
4830 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4831 	 * here for convenience, this has no effect until the ill is set to do
4832 	 * IPv6.
4833 	 */
4834 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4835 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4836 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4837 	ill->ill_max_buf = ND_MAX_Q;
4838 	ill->ill_refcnt = 0;
4839 
4840 	/* Send down the Info Request to the driver. */
4841 	info_mp->b_datap->db_type = M_PCPROTO;
4842 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4843 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4844 	dlir->dl_primitive = DL_INFO_REQ;
4845 
4846 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4847 
4848 	qprocson(q);
4849 	ill_dlpi_send(ill, info_mp);
4850 
4851 	return (0);
4852 }
4853 
4854 /*
4855  * ill_dls_info
4856  * creates datalink socket info from the device.
4857  */
4858 int
4859 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4860 {
4861 	size_t	len;
4862 	ill_t	*ill = ipif->ipif_ill;
4863 
4864 	sdl->sdl_family = AF_LINK;
4865 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4866 	sdl->sdl_type = ill->ill_type;
4867 	(void) ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4868 	len = strlen(sdl->sdl_data);
4869 	ASSERT(len < 256);
4870 	sdl->sdl_nlen = (uchar_t)len;
4871 	sdl->sdl_alen = ill->ill_phys_addr_length;
4872 	sdl->sdl_slen = 0;
4873 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4874 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4875 
4876 	return (sizeof (struct sockaddr_dl));
4877 }
4878 
4879 /*
4880  * ill_xarp_info
4881  * creates xarp info from the device.
4882  */
4883 static int
4884 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4885 {
4886 	sdl->sdl_family = AF_LINK;
4887 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4888 	sdl->sdl_type = ill->ill_type;
4889 	(void) ipif_get_name(ill->ill_ipif, sdl->sdl_data,
4890 	    sizeof (sdl->sdl_data));
4891 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4892 	sdl->sdl_alen = ill->ill_phys_addr_length;
4893 	sdl->sdl_slen = 0;
4894 	return (sdl->sdl_nlen);
4895 }
4896 
4897 static int
4898 loopback_kstat_update(kstat_t *ksp, int rw)
4899 {
4900 	kstat_named_t *kn;
4901 	netstackid_t	stackid;
4902 	netstack_t	*ns;
4903 	ip_stack_t	*ipst;
4904 
4905 	if (ksp == NULL || ksp->ks_data == NULL)
4906 		return (EIO);
4907 
4908 	if (rw == KSTAT_WRITE)
4909 		return (EACCES);
4910 
4911 	kn = KSTAT_NAMED_PTR(ksp);
4912 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4913 
4914 	ns = netstack_find_by_stackid(stackid);
4915 	if (ns == NULL)
4916 		return (-1);
4917 
4918 	ipst = ns->netstack_ip;
4919 	if (ipst == NULL) {
4920 		netstack_rele(ns);
4921 		return (-1);
4922 	}
4923 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4924 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4925 	netstack_rele(ns);
4926 	return (0);
4927 }
4928 
4929 
4930 /*
4931  * Has ifindex been plumbed already.
4932  * Compares both phyint_ifindex and phyint_group_ifindex.
4933  */
4934 static boolean_t
4935 phyint_exists(uint_t index, ip_stack_t *ipst)
4936 {
4937 	phyint_t *phyi;
4938 
4939 	ASSERT(index != 0);
4940 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4941 	/*
4942 	 * Indexes are stored in the phyint - a common structure
4943 	 * to both IPv4 and IPv6.
4944 	 */
4945 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4946 	for (; phyi != NULL;
4947 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4948 	    phyi, AVL_AFTER)) {
4949 		if (phyi->phyint_ifindex == index ||
4950 		    phyi->phyint_group_ifindex == index)
4951 			return (B_TRUE);
4952 	}
4953 	return (B_FALSE);
4954 }
4955 
4956 /* Pick a unique ifindex */
4957 boolean_t
4958 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4959 {
4960 	uint_t starting_index;
4961 
4962 	if (!ipst->ips_ill_index_wrap) {
4963 		*indexp = ipst->ips_ill_index++;
4964 		if (ipst->ips_ill_index == 0) {
4965 			/* Reached the uint_t limit Next time wrap  */
4966 			ipst->ips_ill_index_wrap = B_TRUE;
4967 		}
4968 		return (B_TRUE);
4969 	}
4970 
4971 	/*
4972 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4973 	 * at this point and don't want to call any function that attempts
4974 	 * to get the lock again.
4975 	 */
4976 	starting_index = ipst->ips_ill_index++;
4977 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4978 		if (ipst->ips_ill_index != 0 &&
4979 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4980 			/* found unused index - use it */
4981 			*indexp = ipst->ips_ill_index;
4982 			return (B_TRUE);
4983 		}
4984 	}
4985 
4986 	/*
4987 	 * all interface indicies are inuse.
4988 	 */
4989 	return (B_FALSE);
4990 }
4991 
4992 /*
4993  * Assign a unique interface index for the phyint.
4994  */
4995 static boolean_t
4996 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4997 {
4998 	ASSERT(phyi->phyint_ifindex == 0);
4999 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
5000 }
5001 
5002 /*
5003  * Return a pointer to the ill which matches the supplied name.  Note that
5004  * the ill name length includes the null termination character.  (May be
5005  * called as writer.)
5006  * If do_alloc and the interface is "lo0" it will be automatically created.
5007  * Cannot bump up reference on condemned ills. So dup detect can't be done
5008  * using this func.
5009  */
5010 ill_t *
5011 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
5012     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
5013     ip_stack_t *ipst)
5014 {
5015 	ill_t	*ill;
5016 	ipif_t	*ipif;
5017 	kstat_named_t	*kn;
5018 	boolean_t isloopback;
5019 	ipsq_t *old_ipsq;
5020 	in6_addr_t ov6addr;
5021 
5022 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
5023 
5024 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5025 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
5026 	rw_exit(&ipst->ips_ill_g_lock);
5027 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
5028 		return (ill);
5029 
5030 	/*
5031 	 * Couldn't find it.  Does this happen to be a lookup for the
5032 	 * loopback device and are we allowed to allocate it?
5033 	 */
5034 	if (!isloopback || !do_alloc)
5035 		return (NULL);
5036 
5037 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
5038 
5039 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
5040 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
5041 		rw_exit(&ipst->ips_ill_g_lock);
5042 		return (ill);
5043 	}
5044 
5045 	/* Create the loopback device on demand */
5046 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
5047 	    sizeof (ipif_loopback_name), BPRI_MED));
5048 	if (ill == NULL)
5049 		goto done;
5050 
5051 	*ill = ill_null;
5052 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
5053 	ill->ill_ipst = ipst;
5054 	netstack_hold(ipst->ips_netstack);
5055 	/*
5056 	 * For exclusive stacks we set the zoneid to zero
5057 	 * to make IP operate as if in the global zone.
5058 	 */
5059 	ill->ill_zoneid = GLOBAL_ZONEID;
5060 
5061 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
5062 	if (ill->ill_phyint == NULL)
5063 		goto done;
5064 
5065 	if (isv6)
5066 		ill->ill_phyint->phyint_illv6 = ill;
5067 	else
5068 		ill->ill_phyint->phyint_illv4 = ill;
5069 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
5070 	ill->ill_max_frag = IP_LOOPBACK_MTU;
5071 	/* Add room for tcp+ip headers */
5072 	if (isv6) {
5073 		ill->ill_isv6 = B_TRUE;
5074 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
5075 	} else {
5076 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
5077 	}
5078 	if (!ill_allocate_mibs(ill))
5079 		goto done;
5080 	ill->ill_max_mtu = ill->ill_max_frag;
5081 	/*
5082 	 * ipif_loopback_name can't be pointed at directly because its used
5083 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
5084 	 * from the glist, ill_glist_delete() sets the first character of
5085 	 * ill_name to '\0'.
5086 	 */
5087 	ill->ill_name = (char *)ill + sizeof (*ill);
5088 	(void) strcpy(ill->ill_name, ipif_loopback_name);
5089 	ill->ill_name_length = sizeof (ipif_loopback_name);
5090 	/* Set ill_name_set for ill_phyint_reinit to work properly */
5091 
5092 	ill->ill_global_timer = INFINITY;
5093 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
5094 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
5095 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
5096 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
5097 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
5098 
5099 	/* No resolver here. */
5100 	ill->ill_net_type = IRE_LOOPBACK;
5101 
5102 	/* Initialize the ipsq */
5103 	if (!ipsq_init(ill))
5104 		goto done;
5105 
5106 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
5107 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
5108 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
5109 #ifdef ILL_DEBUG
5110 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
5111 #endif
5112 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
5113 	if (ipif == NULL)
5114 		goto done;
5115 
5116 	ill->ill_flags = ILLF_MULTICAST;
5117 
5118 	ov6addr = ipif->ipif_v6lcl_addr;
5119 	/* Set up default loopback address and mask. */
5120 	if (!isv6) {
5121 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5122 
5123 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5124 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5125 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5126 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5127 		    ipif->ipif_v6subnet);
5128 		ill->ill_flags |= ILLF_IPV4;
5129 	} else {
5130 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5131 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5132 		ipif->ipif_v6net_mask = ipv6_all_ones;
5133 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5134 		    ipif->ipif_v6subnet);
5135 		ill->ill_flags |= ILLF_IPV6;
5136 	}
5137 
5138 	/*
5139 	 * Chain us in at the end of the ill list. hold the ill
5140 	 * before we make it globally visible. 1 for the lookup.
5141 	 */
5142 	ill->ill_refcnt = 0;
5143 	ill_refhold(ill);
5144 
5145 	ill->ill_frag_count = 0;
5146 	ill->ill_frag_free_num_pkts = 0;
5147 	ill->ill_last_frag_clean_time = 0;
5148 
5149 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5150 
5151 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5152 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5153 
5154 	/* Let SCTP know so that it can add this to its list */
5155 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5156 
5157 	/*
5158 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5159 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5160 	 * requires to be after ill_glist_insert() since we need the
5161 	 * ill_index set. Pass on ipv6_loopback as the old address.
5162 	 */
5163 	sctp_update_ipif_addr(ipif, ov6addr);
5164 
5165 	/*
5166 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5167 	 */
5168 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5169 		/* Loopback ills aren't in any IPMP group */
5170 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5171 		ipsq_delete(old_ipsq);
5172 	}
5173 
5174 	/*
5175 	 * Delay this till the ipif is allocated as ipif_allocate
5176 	 * de-references ill_phyint for getting the ifindex. We
5177 	 * can't do this before ipif_allocate because ill_phyint_reinit
5178 	 * -> phyint_assign_ifindex expects ipif to be present.
5179 	 */
5180 	mutex_enter(&ill->ill_phyint->phyint_lock);
5181 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5182 	mutex_exit(&ill->ill_phyint->phyint_lock);
5183 
5184 	if (ipst->ips_loopback_ksp == NULL) {
5185 		/* Export loopback interface statistics */
5186 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5187 		    ipif_loopback_name, "net",
5188 		    KSTAT_TYPE_NAMED, 2, 0,
5189 		    ipst->ips_netstack->netstack_stackid);
5190 		if (ipst->ips_loopback_ksp != NULL) {
5191 			ipst->ips_loopback_ksp->ks_update =
5192 			    loopback_kstat_update;
5193 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5194 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5195 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5196 			ipst->ips_loopback_ksp->ks_private =
5197 			    (void *)(uintptr_t)ipst->ips_netstack->
5198 			    netstack_stackid;
5199 			kstat_install(ipst->ips_loopback_ksp);
5200 		}
5201 	}
5202 
5203 	if (error != NULL)
5204 		*error = 0;
5205 	*did_alloc = B_TRUE;
5206 	rw_exit(&ipst->ips_ill_g_lock);
5207 	return (ill);
5208 done:
5209 	if (ill != NULL) {
5210 		if (ill->ill_phyint != NULL) {
5211 			ipsq_t	*ipsq;
5212 
5213 			ipsq = ill->ill_phyint->phyint_ipsq;
5214 			if (ipsq != NULL) {
5215 				ipsq->ipsq_ipst = NULL;
5216 				kmem_free(ipsq, sizeof (ipsq_t));
5217 			}
5218 			mi_free(ill->ill_phyint);
5219 		}
5220 		ill_free_mib(ill);
5221 		if (ill->ill_ipst != NULL)
5222 			netstack_rele(ill->ill_ipst->ips_netstack);
5223 		mi_free(ill);
5224 	}
5225 	rw_exit(&ipst->ips_ill_g_lock);
5226 	if (error != NULL)
5227 		*error = ENOMEM;
5228 	return (NULL);
5229 }
5230 
5231 /*
5232  * For IPP calls - use the ip_stack_t for global stack.
5233  */
5234 ill_t *
5235 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5236     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5237 {
5238 	ip_stack_t	*ipst;
5239 	ill_t		*ill;
5240 
5241 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5242 	if (ipst == NULL) {
5243 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5244 		return (NULL);
5245 	}
5246 
5247 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5248 	netstack_rele(ipst->ips_netstack);
5249 	return (ill);
5250 }
5251 
5252 /*
5253  * Return a pointer to the ill which matches the index and IP version type.
5254  */
5255 ill_t *
5256 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5257     ipsq_func_t func, int *err, ip_stack_t *ipst)
5258 {
5259 	ill_t	*ill;
5260 	ipsq_t  *ipsq;
5261 	phyint_t *phyi;
5262 
5263 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5264 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5265 
5266 	if (err != NULL)
5267 		*err = 0;
5268 
5269 	/*
5270 	 * Indexes are stored in the phyint - a common structure
5271 	 * to both IPv4 and IPv6.
5272 	 */
5273 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5274 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5275 	    (void *) &index, NULL);
5276 	if (phyi != NULL) {
5277 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5278 		if (ill != NULL) {
5279 			/*
5280 			 * The block comment at the start of ipif_down
5281 			 * explains the use of the macros used below
5282 			 */
5283 			GRAB_CONN_LOCK(q);
5284 			mutex_enter(&ill->ill_lock);
5285 			if (ILL_CAN_LOOKUP(ill)) {
5286 				ill_refhold_locked(ill);
5287 				mutex_exit(&ill->ill_lock);
5288 				RELEASE_CONN_LOCK(q);
5289 				rw_exit(&ipst->ips_ill_g_lock);
5290 				return (ill);
5291 			} else if (ILL_CAN_WAIT(ill, q)) {
5292 				ipsq = ill->ill_phyint->phyint_ipsq;
5293 				mutex_enter(&ipsq->ipsq_lock);
5294 				rw_exit(&ipst->ips_ill_g_lock);
5295 				mutex_exit(&ill->ill_lock);
5296 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5297 				mutex_exit(&ipsq->ipsq_lock);
5298 				RELEASE_CONN_LOCK(q);
5299 				*err = EINPROGRESS;
5300 				return (NULL);
5301 			}
5302 			RELEASE_CONN_LOCK(q);
5303 			mutex_exit(&ill->ill_lock);
5304 		}
5305 	}
5306 	rw_exit(&ipst->ips_ill_g_lock);
5307 	if (err != NULL)
5308 		*err = ENXIO;
5309 	return (NULL);
5310 }
5311 
5312 /*
5313  * Return the ifindex next in sequence after the passed in ifindex.
5314  * If there is no next ifindex for the given protocol, return 0.
5315  */
5316 uint_t
5317 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5318 {
5319 	phyint_t *phyi;
5320 	phyint_t *phyi_initial;
5321 	uint_t   ifindex;
5322 
5323 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5324 
5325 	if (index == 0) {
5326 		phyi = avl_first(
5327 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5328 	} else {
5329 		phyi = phyi_initial = avl_find(
5330 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5331 		    (void *) &index, NULL);
5332 	}
5333 
5334 	for (; phyi != NULL;
5335 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5336 	    phyi, AVL_AFTER)) {
5337 		/*
5338 		 * If we're not returning the first interface in the tree
5339 		 * and we still haven't moved past the phyint_t that
5340 		 * corresponds to index, avl_walk needs to be called again
5341 		 */
5342 		if (!((index != 0) && (phyi == phyi_initial))) {
5343 			if (isv6) {
5344 				if ((phyi->phyint_illv6) &&
5345 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5346 				    (phyi->phyint_illv6->ill_isv6 == 1))
5347 					break;
5348 			} else {
5349 				if ((phyi->phyint_illv4) &&
5350 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5351 				    (phyi->phyint_illv4->ill_isv6 == 0))
5352 					break;
5353 			}
5354 		}
5355 	}
5356 
5357 	rw_exit(&ipst->ips_ill_g_lock);
5358 
5359 	if (phyi != NULL)
5360 		ifindex = phyi->phyint_ifindex;
5361 	else
5362 		ifindex = 0;
5363 
5364 	return (ifindex);
5365 }
5366 
5367 
5368 /*
5369  * Return the ifindex for the named interface.
5370  * If there is no next ifindex for the interface, return 0.
5371  */
5372 uint_t
5373 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5374 {
5375 	phyint_t	*phyi;
5376 	avl_index_t	where = 0;
5377 	uint_t		ifindex;
5378 
5379 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5380 
5381 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5382 	    name, &where)) == NULL) {
5383 		rw_exit(&ipst->ips_ill_g_lock);
5384 		return (0);
5385 	}
5386 
5387 	ifindex = phyi->phyint_ifindex;
5388 
5389 	rw_exit(&ipst->ips_ill_g_lock);
5390 
5391 	return (ifindex);
5392 }
5393 
5394 
5395 /*
5396  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5397  * that gives a running thread a reference to the ill. This reference must be
5398  * released by the thread when it is done accessing the ill and related
5399  * objects. ill_refcnt can not be used to account for static references
5400  * such as other structures pointing to an ill. Callers must generally
5401  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5402  * or be sure that the ill is not being deleted or changing state before
5403  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5404  * ill won't change any of its critical state such as address, netmask etc.
5405  */
5406 void
5407 ill_refhold(ill_t *ill)
5408 {
5409 	mutex_enter(&ill->ill_lock);
5410 	ill->ill_refcnt++;
5411 	ILL_TRACE_REF(ill);
5412 	mutex_exit(&ill->ill_lock);
5413 }
5414 
5415 void
5416 ill_refhold_locked(ill_t *ill)
5417 {
5418 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5419 	ill->ill_refcnt++;
5420 	ILL_TRACE_REF(ill);
5421 }
5422 
5423 int
5424 ill_check_and_refhold(ill_t *ill)
5425 {
5426 	mutex_enter(&ill->ill_lock);
5427 	if (ILL_CAN_LOOKUP(ill)) {
5428 		ill_refhold_locked(ill);
5429 		mutex_exit(&ill->ill_lock);
5430 		return (0);
5431 	}
5432 	mutex_exit(&ill->ill_lock);
5433 	return (ILL_LOOKUP_FAILED);
5434 }
5435 
5436 /*
5437  * Must not be called while holding any locks. Otherwise if this is
5438  * the last reference to be released, there is a chance of recursive mutex
5439  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5440  * to restart an ioctl.
5441  */
5442 void
5443 ill_refrele(ill_t *ill)
5444 {
5445 	mutex_enter(&ill->ill_lock);
5446 	ASSERT(ill->ill_refcnt != 0);
5447 	ill->ill_refcnt--;
5448 	ILL_UNTRACE_REF(ill);
5449 	if (ill->ill_refcnt != 0) {
5450 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5451 		mutex_exit(&ill->ill_lock);
5452 		return;
5453 	}
5454 
5455 	/* Drops the ill_lock */
5456 	ipif_ill_refrele_tail(ill);
5457 }
5458 
5459 /*
5460  * Obtain a weak reference count on the ill. This reference ensures the
5461  * ill won't be freed, but the ill may change any of its critical state
5462  * such as netmask, address etc. Returns an error if the ill has started
5463  * closing.
5464  */
5465 boolean_t
5466 ill_waiter_inc(ill_t *ill)
5467 {
5468 	mutex_enter(&ill->ill_lock);
5469 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5470 		mutex_exit(&ill->ill_lock);
5471 		return (B_FALSE);
5472 	}
5473 	ill->ill_waiters++;
5474 	mutex_exit(&ill->ill_lock);
5475 	return (B_TRUE);
5476 }
5477 
5478 void
5479 ill_waiter_dcr(ill_t *ill)
5480 {
5481 	mutex_enter(&ill->ill_lock);
5482 	ill->ill_waiters--;
5483 	if (ill->ill_waiters == 0)
5484 		cv_broadcast(&ill->ill_cv);
5485 	mutex_exit(&ill->ill_lock);
5486 }
5487 
5488 /*
5489  * Named Dispatch routine to produce a formatted report on all ILLs.
5490  * This report is accessed by using the ndd utility to "get" ND variable
5491  * "ip_ill_status".
5492  */
5493 /* ARGSUSED */
5494 int
5495 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5496 {
5497 	ill_t		*ill;
5498 	ill_walk_context_t ctx;
5499 	ip_stack_t	*ipst;
5500 
5501 	ipst = CONNQ_TO_IPST(q);
5502 
5503 	(void) mi_mpprintf(mp,
5504 	    "ILL      " MI_COL_HDRPAD_STR
5505 	/*   01234567[89ABCDEF] */
5506 	    "rq       " MI_COL_HDRPAD_STR
5507 	/*   01234567[89ABCDEF] */
5508 	    "wq       " MI_COL_HDRPAD_STR
5509 	/*   01234567[89ABCDEF] */
5510 	    "upcnt mxfrg err name");
5511 	/*   12345 12345 123 xxxxxxxx  */
5512 
5513 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5514 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5515 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5516 		(void) mi_mpprintf(mp,
5517 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5518 		    "%05u %05u %03d %s",
5519 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5520 		    ill->ill_ipif_up_count,
5521 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5522 	}
5523 	rw_exit(&ipst->ips_ill_g_lock);
5524 
5525 	return (0);
5526 }
5527 
5528 /*
5529  * Named Dispatch routine to produce a formatted report on all IPIFs.
5530  * This report is accessed by using the ndd utility to "get" ND variable
5531  * "ip_ipif_status".
5532  */
5533 /* ARGSUSED */
5534 int
5535 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5536 {
5537 	char	buf1[INET6_ADDRSTRLEN];
5538 	char	buf2[INET6_ADDRSTRLEN];
5539 	char	buf3[INET6_ADDRSTRLEN];
5540 	char	buf4[INET6_ADDRSTRLEN];
5541 	char	buf5[INET6_ADDRSTRLEN];
5542 	char	buf6[INET6_ADDRSTRLEN];
5543 	char	buf[LIFNAMSIZ];
5544 	ill_t	*ill;
5545 	ipif_t	*ipif;
5546 	nv_t	*nvp;
5547 	uint64_t flags;
5548 	zoneid_t zoneid;
5549 	ill_walk_context_t ctx;
5550 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5551 
5552 	(void) mi_mpprintf(mp,
5553 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5554 	    "\tlocal address\n"
5555 	    "\tsrc address\n"
5556 	    "\tsubnet\n"
5557 	    "\tmask\n"
5558 	    "\tbroadcast\n"
5559 	    "\tp-p-dst");
5560 
5561 	ASSERT(q->q_next == NULL);
5562 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5563 
5564 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5565 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5566 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5567 		for (ipif = ill->ill_ipif; ipif != NULL;
5568 		    ipif = ipif->ipif_next) {
5569 			if (zoneid != GLOBAL_ZONEID &&
5570 			    zoneid != ipif->ipif_zoneid &&
5571 			    ipif->ipif_zoneid != ALL_ZONES)
5572 				continue;
5573 			(void) mi_mpprintf(mp,
5574 			    MI_COL_PTRFMT_STR
5575 			    "%04u %05u %u/%u/%u %s %d",
5576 			    (void *)ipif,
5577 			    ipif->ipif_metric, ipif->ipif_mtu,
5578 			    ipif->ipif_ib_pkt_count,
5579 			    ipif->ipif_ob_pkt_count,
5580 			    ipif->ipif_fo_pkt_count,
5581 			    ipif_get_name(ipif, buf, sizeof (buf)),
5582 			    ipif->ipif_zoneid);
5583 
5584 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5585 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5586 
5587 		/* Tack on text strings for any flags. */
5588 		nvp = ipif_nv_tbl;
5589 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5590 			if (nvp->nv_value & flags)
5591 				(void) mi_mpprintf_nr(mp, " %s",
5592 				    nvp->nv_name);
5593 		}
5594 		(void) mi_mpprintf(mp,
5595 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5596 		    inet_ntop(AF_INET6,
5597 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5598 		    inet_ntop(AF_INET6,
5599 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5600 		    inet_ntop(AF_INET6,
5601 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5602 		    inet_ntop(AF_INET6,
5603 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5604 		    inet_ntop(AF_INET6,
5605 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5606 		    inet_ntop(AF_INET6,
5607 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5608 		}
5609 	}
5610 	rw_exit(&ipst->ips_ill_g_lock);
5611 	return (0);
5612 }
5613 
5614 /*
5615  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5616  * driver.  We construct best guess defaults for lower level information that
5617  * we need.  If an interface is brought up without injection of any overriding
5618  * information from outside, we have to be ready to go with these defaults.
5619  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5620  * we primarely want the dl_provider_style.
5621  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5622  * at which point we assume the other part of the information is valid.
5623  */
5624 void
5625 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5626 {
5627 	uchar_t		*brdcst_addr;
5628 	uint_t		brdcst_addr_length, phys_addr_length;
5629 	t_scalar_t	sap_length;
5630 	dl_info_ack_t	*dlia;
5631 	ip_m_t		*ipm;
5632 	dl_qos_cl_sel1_t *sel1;
5633 
5634 	ASSERT(IAM_WRITER_ILL(ill));
5635 
5636 	/*
5637 	 * Till the ill is fully up ILL_CHANGING will be set and
5638 	 * the ill is not globally visible. So no need for a lock.
5639 	 */
5640 	dlia = (dl_info_ack_t *)mp->b_rptr;
5641 	ill->ill_mactype = dlia->dl_mac_type;
5642 
5643 	ipm = ip_m_lookup(dlia->dl_mac_type);
5644 	if (ipm == NULL) {
5645 		ipm = ip_m_lookup(DL_OTHER);
5646 		ASSERT(ipm != NULL);
5647 	}
5648 	ill->ill_media = ipm;
5649 
5650 	/*
5651 	 * When the new DLPI stuff is ready we'll pull lengths
5652 	 * from dlia.
5653 	 */
5654 	if (dlia->dl_version == DL_VERSION_2) {
5655 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5656 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5657 		    brdcst_addr_length);
5658 		if (brdcst_addr == NULL) {
5659 			brdcst_addr_length = 0;
5660 		}
5661 		sap_length = dlia->dl_sap_length;
5662 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5663 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5664 		    brdcst_addr_length, sap_length, phys_addr_length));
5665 	} else {
5666 		brdcst_addr_length = 6;
5667 		brdcst_addr = ip_six_byte_all_ones;
5668 		sap_length = -2;
5669 		phys_addr_length = brdcst_addr_length;
5670 	}
5671 
5672 	ill->ill_bcast_addr_length = brdcst_addr_length;
5673 	ill->ill_phys_addr_length = phys_addr_length;
5674 	ill->ill_sap_length = sap_length;
5675 	ill->ill_max_frag = dlia->dl_max_sdu;
5676 	ill->ill_max_mtu = ill->ill_max_frag;
5677 
5678 	ill->ill_type = ipm->ip_m_type;
5679 
5680 	if (!ill->ill_dlpi_style_set) {
5681 		if (dlia->dl_provider_style == DL_STYLE2)
5682 			ill->ill_needs_attach = 1;
5683 
5684 		/*
5685 		 * Allocate the first ipif on this ill. We don't delay it
5686 		 * further as ioctl handling assumes atleast one ipif to
5687 		 * be present.
5688 		 *
5689 		 * At this point we don't know whether the ill is v4 or v6.
5690 		 * We will know this whan the SIOCSLIFNAME happens and
5691 		 * the correct value for ill_isv6 will be assigned in
5692 		 * ipif_set_values(). We need to hold the ill lock and
5693 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5694 		 * the wakeup.
5695 		 */
5696 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5697 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5698 		mutex_enter(&ill->ill_lock);
5699 		ASSERT(ill->ill_dlpi_style_set == 0);
5700 		ill->ill_dlpi_style_set = 1;
5701 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5702 		cv_broadcast(&ill->ill_cv);
5703 		mutex_exit(&ill->ill_lock);
5704 		freemsg(mp);
5705 		return;
5706 	}
5707 	ASSERT(ill->ill_ipif != NULL);
5708 	/*
5709 	 * We know whether it is IPv4 or IPv6 now, as this is the
5710 	 * second DL_INFO_ACK we are recieving in response to the
5711 	 * DL_INFO_REQ sent in ipif_set_values.
5712 	 */
5713 	if (ill->ill_isv6)
5714 		ill->ill_sap = IP6_DL_SAP;
5715 	else
5716 		ill->ill_sap = IP_DL_SAP;
5717 	/*
5718 	 * Set ipif_mtu which is used to set the IRE's
5719 	 * ire_max_frag value. The driver could have sent
5720 	 * a different mtu from what it sent last time. No
5721 	 * need to call ipif_mtu_change because IREs have
5722 	 * not yet been created.
5723 	 */
5724 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5725 	/*
5726 	 * Clear all the flags that were set based on ill_bcast_addr_length
5727 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5728 	 * changed now and we need to re-evaluate.
5729 	 */
5730 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5731 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5732 
5733 	/*
5734 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5735 	 * changed now.
5736 	 */
5737 	if (ill->ill_bcast_addr_length == 0) {
5738 		if (ill->ill_resolver_mp != NULL)
5739 			freemsg(ill->ill_resolver_mp);
5740 		if (ill->ill_bcast_mp != NULL)
5741 			freemsg(ill->ill_bcast_mp);
5742 		if (ill->ill_flags & ILLF_XRESOLV)
5743 			ill->ill_net_type = IRE_IF_RESOLVER;
5744 		else
5745 			ill->ill_net_type = IRE_IF_NORESOLVER;
5746 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5747 		    ill->ill_phys_addr_length,
5748 		    ill->ill_sap,
5749 		    ill->ill_sap_length);
5750 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5751 
5752 		if (ill->ill_isv6)
5753 			/*
5754 			 * Note: xresolv interfaces will eventually need NOARP
5755 			 * set here as well, but that will require those
5756 			 * external resolvers to have some knowledge of
5757 			 * that flag and act appropriately. Not to be changed
5758 			 * at present.
5759 			 */
5760 			ill->ill_flags |= ILLF_NONUD;
5761 		else
5762 			ill->ill_flags |= ILLF_NOARP;
5763 
5764 		if (ill->ill_phys_addr_length == 0) {
5765 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5766 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5767 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5768 			} else {
5769 				/* pt-pt supports multicast. */
5770 				ill->ill_flags |= ILLF_MULTICAST;
5771 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5772 			}
5773 		}
5774 	} else {
5775 		ill->ill_net_type = IRE_IF_RESOLVER;
5776 		if (ill->ill_bcast_mp != NULL)
5777 			freemsg(ill->ill_bcast_mp);
5778 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5779 		    ill->ill_bcast_addr_length, ill->ill_sap,
5780 		    ill->ill_sap_length);
5781 		/*
5782 		 * Later detect lack of DLPI driver multicast
5783 		 * capability by catching DL_ENABMULTI errors in
5784 		 * ip_rput_dlpi.
5785 		 */
5786 		ill->ill_flags |= ILLF_MULTICAST;
5787 		if (!ill->ill_isv6)
5788 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5789 	}
5790 	/* By default an interface does not support any CoS marking */
5791 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5792 
5793 	/*
5794 	 * If we get QoS information in DL_INFO_ACK, the device supports
5795 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5796 	 */
5797 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5798 	    dlia->dl_qos_length);
5799 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5800 		ill->ill_flags |= ILLF_COS_ENABLED;
5801 	}
5802 
5803 	/* Clear any previous error indication. */
5804 	ill->ill_error = 0;
5805 	freemsg(mp);
5806 }
5807 
5808 /*
5809  * Perform various checks to verify that an address would make sense as a
5810  * local, remote, or subnet interface address.
5811  */
5812 static boolean_t
5813 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5814 {
5815 	ipaddr_t	net_mask;
5816 
5817 	/*
5818 	 * Don't allow all zeroes, all ones or experimental address, but allow
5819 	 * all ones netmask.
5820 	 */
5821 	if ((net_mask = ip_net_mask(addr)) == 0)
5822 		return (B_FALSE);
5823 	/* A given netmask overrides the "guess" netmask */
5824 	if (subnet_mask != 0)
5825 		net_mask = subnet_mask;
5826 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5827 	    (addr == (addr | ~net_mask)))) {
5828 		return (B_FALSE);
5829 	}
5830 	if (CLASSD(addr))
5831 		return (B_FALSE);
5832 
5833 	return (B_TRUE);
5834 }
5835 
5836 #define	V6_IPIF_LINKLOCAL(p)	\
5837 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5838 
5839 /*
5840  * Compare two given ipifs and check if the second one is better than
5841  * the first one using the order of preference (not taking deprecated
5842  * into acount) specified in ipif_lookup_multicast().
5843  */
5844 static boolean_t
5845 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5846 {
5847 	/* Check the least preferred first. */
5848 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5849 		/* If both ipifs are the same, use the first one. */
5850 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5851 			return (B_FALSE);
5852 		else
5853 			return (B_TRUE);
5854 	}
5855 
5856 	/* For IPv6, check for link local address. */
5857 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5858 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5859 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5860 			/* The second one is equal or less preferred. */
5861 			return (B_FALSE);
5862 		} else {
5863 			return (B_TRUE);
5864 		}
5865 	}
5866 
5867 	/* Then check for point to point interface. */
5868 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5869 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5870 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5871 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5872 			return (B_FALSE);
5873 		} else {
5874 			return (B_TRUE);
5875 		}
5876 	}
5877 
5878 	/* old_ipif is a normal interface, so no need to use the new one. */
5879 	return (B_FALSE);
5880 }
5881 
5882 /*
5883  * Find any non-virtual, not condemned, and up multicast capable interface
5884  * given an IP instance and zoneid.  Order of preference is:
5885  *
5886  * 1. normal
5887  * 1.1 normal, but deprecated
5888  * 2. point to point
5889  * 2.1 point to point, but deprecated
5890  * 3. link local
5891  * 3.1 link local, but deprecated
5892  * 4. loopback.
5893  */
5894 ipif_t *
5895 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5896 {
5897 	ill_t			*ill;
5898 	ill_walk_context_t	ctx;
5899 	ipif_t			*ipif;
5900 	ipif_t			*saved_ipif = NULL;
5901 	ipif_t			*dep_ipif = NULL;
5902 
5903 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5904 	if (isv6)
5905 		ill = ILL_START_WALK_V6(&ctx, ipst);
5906 	else
5907 		ill = ILL_START_WALK_V4(&ctx, ipst);
5908 
5909 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5910 		mutex_enter(&ill->ill_lock);
5911 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5912 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5913 			mutex_exit(&ill->ill_lock);
5914 			continue;
5915 		}
5916 		for (ipif = ill->ill_ipif; ipif != NULL;
5917 		    ipif = ipif->ipif_next) {
5918 			if (zoneid != ipif->ipif_zoneid &&
5919 			    zoneid != ALL_ZONES &&
5920 			    ipif->ipif_zoneid != ALL_ZONES) {
5921 				continue;
5922 			}
5923 			if (!(ipif->ipif_flags & IPIF_UP) ||
5924 			    !IPIF_CAN_LOOKUP(ipif)) {
5925 				continue;
5926 			}
5927 
5928 			/*
5929 			 * Found one candidate.  If it is deprecated,
5930 			 * remember it in dep_ipif.  If it is not deprecated,
5931 			 * remember it in saved_ipif.
5932 			 */
5933 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5934 				if (dep_ipif == NULL) {
5935 					dep_ipif = ipif;
5936 				} else if (ipif_comp_multi(dep_ipif, ipif,
5937 				    isv6)) {
5938 					/*
5939 					 * If the previous dep_ipif does not
5940 					 * belong to the same ill, we've done
5941 					 * a ipif_refhold() on it.  So we need
5942 					 * to release it.
5943 					 */
5944 					if (dep_ipif->ipif_ill != ill)
5945 						ipif_refrele(dep_ipif);
5946 					dep_ipif = ipif;
5947 				}
5948 				continue;
5949 			}
5950 			if (saved_ipif == NULL) {
5951 				saved_ipif = ipif;
5952 			} else {
5953 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5954 					if (saved_ipif->ipif_ill != ill)
5955 						ipif_refrele(saved_ipif);
5956 					saved_ipif = ipif;
5957 				}
5958 			}
5959 		}
5960 		/*
5961 		 * Before going to the next ill, do a ipif_refhold() on the
5962 		 * saved ones.
5963 		 */
5964 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5965 			ipif_refhold_locked(saved_ipif);
5966 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5967 			ipif_refhold_locked(dep_ipif);
5968 		mutex_exit(&ill->ill_lock);
5969 	}
5970 	rw_exit(&ipst->ips_ill_g_lock);
5971 
5972 	/*
5973 	 * If we have only the saved_ipif, return it.  But if we have both
5974 	 * saved_ipif and dep_ipif, check to see which one is better.
5975 	 */
5976 	if (saved_ipif != NULL) {
5977 		if (dep_ipif != NULL) {
5978 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5979 				ipif_refrele(saved_ipif);
5980 				return (dep_ipif);
5981 			} else {
5982 				ipif_refrele(dep_ipif);
5983 				return (saved_ipif);
5984 			}
5985 		}
5986 		return (saved_ipif);
5987 	} else {
5988 		return (dep_ipif);
5989 	}
5990 }
5991 
5992 /*
5993  * This function is called when an application does not specify an interface
5994  * to be used for multicast traffic (joining a group/sending data).  It
5995  * calls ire_lookup_multi() to look for an interface route for the
5996  * specified multicast group.  Doing this allows the administrator to add
5997  * prefix routes for multicast to indicate which interface to be used for
5998  * multicast traffic in the above scenario.  The route could be for all
5999  * multicast (224.0/4), for a single multicast group (a /32 route) or
6000  * anything in between.  If there is no such multicast route, we just find
6001  * any multicast capable interface and return it.  The returned ipif
6002  * is refhold'ed.
6003  */
6004 ipif_t *
6005 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
6006 {
6007 	ire_t			*ire;
6008 	ipif_t			*ipif;
6009 
6010 	ire = ire_lookup_multi(group, zoneid, ipst);
6011 	if (ire != NULL) {
6012 		ipif = ire->ire_ipif;
6013 		ipif_refhold(ipif);
6014 		ire_refrele(ire);
6015 		return (ipif);
6016 	}
6017 
6018 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
6019 }
6020 
6021 /*
6022  * Look for an ipif with the specified interface address and destination.
6023  * The destination address is used only for matching point-to-point interfaces.
6024  */
6025 ipif_t *
6026 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
6027     ipsq_func_t func, int *error, ip_stack_t *ipst)
6028 {
6029 	ipif_t	*ipif;
6030 	ill_t	*ill;
6031 	ill_walk_context_t ctx;
6032 	ipsq_t	*ipsq;
6033 
6034 	if (error != NULL)
6035 		*error = 0;
6036 
6037 	/*
6038 	 * First match all the point-to-point interfaces
6039 	 * before looking at non-point-to-point interfaces.
6040 	 * This is done to avoid returning non-point-to-point
6041 	 * ipif instead of unnumbered point-to-point ipif.
6042 	 */
6043 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6044 	ill = ILL_START_WALK_V4(&ctx, ipst);
6045 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6046 		GRAB_CONN_LOCK(q);
6047 		mutex_enter(&ill->ill_lock);
6048 		for (ipif = ill->ill_ipif; ipif != NULL;
6049 		    ipif = ipif->ipif_next) {
6050 			/* Allow the ipif to be down */
6051 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
6052 			    (ipif->ipif_lcl_addr == if_addr) &&
6053 			    (ipif->ipif_pp_dst_addr == dst)) {
6054 				/*
6055 				 * The block comment at the start of ipif_down
6056 				 * explains the use of the macros used below
6057 				 */
6058 				if (IPIF_CAN_LOOKUP(ipif)) {
6059 					ipif_refhold_locked(ipif);
6060 					mutex_exit(&ill->ill_lock);
6061 					RELEASE_CONN_LOCK(q);
6062 					rw_exit(&ipst->ips_ill_g_lock);
6063 					return (ipif);
6064 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6065 					ipsq = ill->ill_phyint->phyint_ipsq;
6066 					mutex_enter(&ipsq->ipsq_lock);
6067 					mutex_exit(&ill->ill_lock);
6068 					rw_exit(&ipst->ips_ill_g_lock);
6069 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6070 					    ill);
6071 					mutex_exit(&ipsq->ipsq_lock);
6072 					RELEASE_CONN_LOCK(q);
6073 					*error = EINPROGRESS;
6074 					return (NULL);
6075 				}
6076 			}
6077 		}
6078 		mutex_exit(&ill->ill_lock);
6079 		RELEASE_CONN_LOCK(q);
6080 	}
6081 	rw_exit(&ipst->ips_ill_g_lock);
6082 
6083 	/* lookup the ipif based on interface address */
6084 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
6085 	    ipst);
6086 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
6087 	return (ipif);
6088 }
6089 
6090 /*
6091  * Look for an ipif with the specified address. For point-point links
6092  * we look for matches on either the destination address and the local
6093  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6094  * is set.
6095  * Matches on a specific ill if match_ill is set.
6096  */
6097 ipif_t *
6098 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
6099     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
6100 {
6101 	ipif_t  *ipif;
6102 	ill_t   *ill;
6103 	boolean_t ptp = B_FALSE;
6104 	ipsq_t	*ipsq;
6105 	ill_walk_context_t	ctx;
6106 
6107 	if (error != NULL)
6108 		*error = 0;
6109 
6110 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6111 	/*
6112 	 * Repeat twice, first based on local addresses and
6113 	 * next time for pointopoint.
6114 	 */
6115 repeat:
6116 	ill = ILL_START_WALK_V4(&ctx, ipst);
6117 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6118 		if (match_ill != NULL && ill != match_ill) {
6119 			continue;
6120 		}
6121 		GRAB_CONN_LOCK(q);
6122 		mutex_enter(&ill->ill_lock);
6123 		for (ipif = ill->ill_ipif; ipif != NULL;
6124 		    ipif = ipif->ipif_next) {
6125 			if (zoneid != ALL_ZONES &&
6126 			    zoneid != ipif->ipif_zoneid &&
6127 			    ipif->ipif_zoneid != ALL_ZONES)
6128 				continue;
6129 			/* Allow the ipif to be down */
6130 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6131 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6132 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6133 			    (ipif->ipif_pp_dst_addr == addr))) {
6134 				/*
6135 				 * The block comment at the start of ipif_down
6136 				 * explains the use of the macros used below
6137 				 */
6138 				if (IPIF_CAN_LOOKUP(ipif)) {
6139 					ipif_refhold_locked(ipif);
6140 					mutex_exit(&ill->ill_lock);
6141 					RELEASE_CONN_LOCK(q);
6142 					rw_exit(&ipst->ips_ill_g_lock);
6143 					return (ipif);
6144 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6145 					ipsq = ill->ill_phyint->phyint_ipsq;
6146 					mutex_enter(&ipsq->ipsq_lock);
6147 					mutex_exit(&ill->ill_lock);
6148 					rw_exit(&ipst->ips_ill_g_lock);
6149 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6150 					    ill);
6151 					mutex_exit(&ipsq->ipsq_lock);
6152 					RELEASE_CONN_LOCK(q);
6153 					*error = EINPROGRESS;
6154 					return (NULL);
6155 				}
6156 			}
6157 		}
6158 		mutex_exit(&ill->ill_lock);
6159 		RELEASE_CONN_LOCK(q);
6160 	}
6161 
6162 	/* If we already did the ptp case, then we are done */
6163 	if (ptp) {
6164 		rw_exit(&ipst->ips_ill_g_lock);
6165 		if (error != NULL)
6166 			*error = ENXIO;
6167 		return (NULL);
6168 	}
6169 	ptp = B_TRUE;
6170 	goto repeat;
6171 }
6172 
6173 /*
6174  * Look for an ipif with the specified address. For point-point links
6175  * we look for matches on either the destination address and the local
6176  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6177  * is set.
6178  * Matches on a specific ill if match_ill is set.
6179  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6180  */
6181 zoneid_t
6182 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6183 {
6184 	zoneid_t zoneid;
6185 	ipif_t  *ipif;
6186 	ill_t   *ill;
6187 	boolean_t ptp = B_FALSE;
6188 	ill_walk_context_t	ctx;
6189 
6190 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6191 	/*
6192 	 * Repeat twice, first based on local addresses and
6193 	 * next time for pointopoint.
6194 	 */
6195 repeat:
6196 	ill = ILL_START_WALK_V4(&ctx, ipst);
6197 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6198 		if (match_ill != NULL && ill != match_ill) {
6199 			continue;
6200 		}
6201 		mutex_enter(&ill->ill_lock);
6202 		for (ipif = ill->ill_ipif; ipif != NULL;
6203 		    ipif = ipif->ipif_next) {
6204 			/* Allow the ipif to be down */
6205 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6206 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6207 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6208 			    (ipif->ipif_pp_dst_addr == addr)) &&
6209 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6210 				zoneid = ipif->ipif_zoneid;
6211 				mutex_exit(&ill->ill_lock);
6212 				rw_exit(&ipst->ips_ill_g_lock);
6213 				/*
6214 				 * If ipif_zoneid was ALL_ZONES then we have
6215 				 * a trusted extensions shared IP address.
6216 				 * In that case GLOBAL_ZONEID works to send.
6217 				 */
6218 				if (zoneid == ALL_ZONES)
6219 					zoneid = GLOBAL_ZONEID;
6220 				return (zoneid);
6221 			}
6222 		}
6223 		mutex_exit(&ill->ill_lock);
6224 	}
6225 
6226 	/* If we already did the ptp case, then we are done */
6227 	if (ptp) {
6228 		rw_exit(&ipst->ips_ill_g_lock);
6229 		return (ALL_ZONES);
6230 	}
6231 	ptp = B_TRUE;
6232 	goto repeat;
6233 }
6234 
6235 /*
6236  * Look for an ipif that matches the specified remote address i.e. the
6237  * ipif that would receive the specified packet.
6238  * First look for directly connected interfaces and then do a recursive
6239  * IRE lookup and pick the first ipif corresponding to the source address in the
6240  * ire.
6241  * Returns: held ipif
6242  */
6243 ipif_t *
6244 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6245 {
6246 	ipif_t	*ipif;
6247 	ire_t	*ire;
6248 	ip_stack_t	*ipst = ill->ill_ipst;
6249 
6250 	ASSERT(!ill->ill_isv6);
6251 
6252 	/*
6253 	 * Someone could be changing this ipif currently or change it
6254 	 * after we return this. Thus  a few packets could use the old
6255 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6256 	 * will atomically be updated or cleaned up with the new value
6257 	 * Thus we don't need a lock to check the flags or other attrs below.
6258 	 */
6259 	mutex_enter(&ill->ill_lock);
6260 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6261 		if (!IPIF_CAN_LOOKUP(ipif))
6262 			continue;
6263 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6264 		    ipif->ipif_zoneid != ALL_ZONES)
6265 			continue;
6266 		/* Allow the ipif to be down */
6267 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6268 			if ((ipif->ipif_pp_dst_addr == addr) ||
6269 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6270 			    ipif->ipif_lcl_addr == addr)) {
6271 				ipif_refhold_locked(ipif);
6272 				mutex_exit(&ill->ill_lock);
6273 				return (ipif);
6274 			}
6275 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6276 			ipif_refhold_locked(ipif);
6277 			mutex_exit(&ill->ill_lock);
6278 			return (ipif);
6279 		}
6280 	}
6281 	mutex_exit(&ill->ill_lock);
6282 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6283 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6284 	if (ire != NULL) {
6285 		/*
6286 		 * The callers of this function wants to know the
6287 		 * interface on which they have to send the replies
6288 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6289 		 * derived from different ills, we really don't care
6290 		 * what we return here.
6291 		 */
6292 		ipif = ire->ire_ipif;
6293 		if (ipif != NULL) {
6294 			ipif_refhold(ipif);
6295 			ire_refrele(ire);
6296 			return (ipif);
6297 		}
6298 		ire_refrele(ire);
6299 	}
6300 	/* Pick the first interface */
6301 	ipif = ipif_get_next_ipif(NULL, ill);
6302 	return (ipif);
6303 }
6304 
6305 /*
6306  * This func does not prevent refcnt from increasing. But if
6307  * the caller has taken steps to that effect, then this func
6308  * can be used to determine whether the ill has become quiescent
6309  */
6310 boolean_t
6311 ill_is_quiescent(ill_t *ill)
6312 {
6313 	ipif_t	*ipif;
6314 
6315 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6316 
6317 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6318 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6319 			return (B_FALSE);
6320 		}
6321 	}
6322 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
6323 	    ill->ill_nce_cnt != 0 || ill->ill_srcif_refcnt != 0 ||
6324 	    ill->ill_mrtun_refcnt != 0) {
6325 		return (B_FALSE);
6326 	}
6327 	return (B_TRUE);
6328 }
6329 
6330 /*
6331  * This func does not prevent refcnt from increasing. But if
6332  * the caller has taken steps to that effect, then this func
6333  * can be used to determine whether the ipif has become quiescent
6334  */
6335 static boolean_t
6336 ipif_is_quiescent(ipif_t *ipif)
6337 {
6338 	ill_t *ill;
6339 
6340 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6341 
6342 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6343 		return (B_FALSE);
6344 	}
6345 
6346 	ill = ipif->ipif_ill;
6347 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6348 	    ill->ill_logical_down) {
6349 		return (B_TRUE);
6350 	}
6351 
6352 	/* This is the last ipif going down or being deleted on this ill */
6353 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
6354 		return (B_FALSE);
6355 	}
6356 
6357 	return (B_TRUE);
6358 }
6359 
6360 /*
6361  * This func does not prevent refcnt from increasing. But if
6362  * the caller has taken steps to that effect, then this func
6363  * can be used to determine whether the ipifs marked with IPIF_MOVING
6364  * have become quiescent and can be moved in a failover/failback.
6365  */
6366 static ipif_t *
6367 ill_quiescent_to_move(ill_t *ill)
6368 {
6369 	ipif_t  *ipif;
6370 
6371 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6372 
6373 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6374 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6375 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6376 				return (ipif);
6377 			}
6378 		}
6379 	}
6380 	return (NULL);
6381 }
6382 
6383 /*
6384  * The ipif/ill/ire has been refreled. Do the tail processing.
6385  * Determine if the ipif or ill in question has become quiescent and if so
6386  * wakeup close and/or restart any queued pending ioctl that is waiting
6387  * for the ipif_down (or ill_down)
6388  */
6389 void
6390 ipif_ill_refrele_tail(ill_t *ill)
6391 {
6392 	mblk_t	*mp;
6393 	conn_t	*connp;
6394 	ipsq_t	*ipsq;
6395 	ipif_t	*ipif;
6396 	dl_notify_ind_t *dlindp;
6397 
6398 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6399 
6400 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6401 	    ill_is_quiescent(ill)) {
6402 		/* ill_close may be waiting */
6403 		cv_broadcast(&ill->ill_cv);
6404 	}
6405 
6406 	/* ipsq can't change because ill_lock  is held */
6407 	ipsq = ill->ill_phyint->phyint_ipsq;
6408 	if (ipsq->ipsq_waitfor == 0) {
6409 		/* Not waiting for anything, just return. */
6410 		mutex_exit(&ill->ill_lock);
6411 		return;
6412 	}
6413 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6414 	    ipsq->ipsq_pending_ipif != NULL);
6415 	/*
6416 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6417 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6418 	 * be zero for restarting an ioctl that ends up downing the ill.
6419 	 */
6420 	ipif = ipsq->ipsq_pending_ipif;
6421 	if (ipif->ipif_ill != ill) {
6422 		/* The ioctl is pending on some other ill. */
6423 		mutex_exit(&ill->ill_lock);
6424 		return;
6425 	}
6426 
6427 	switch (ipsq->ipsq_waitfor) {
6428 	case IPIF_DOWN:
6429 	case IPIF_FREE:
6430 		if (!ipif_is_quiescent(ipif)) {
6431 			mutex_exit(&ill->ill_lock);
6432 			return;
6433 		}
6434 		break;
6435 
6436 	case ILL_DOWN:
6437 	case ILL_FREE:
6438 		/*
6439 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6440 		 * waits synchronously in ip_close, and no message is queued in
6441 		 * ipsq_pending_mp at all in this case
6442 		 */
6443 		if (!ill_is_quiescent(ill)) {
6444 			mutex_exit(&ill->ill_lock);
6445 			return;
6446 		}
6447 
6448 		break;
6449 
6450 	case ILL_MOVE_OK:
6451 		if (ill_quiescent_to_move(ill) != NULL) {
6452 			mutex_exit(&ill->ill_lock);
6453 			return;
6454 		}
6455 
6456 		break;
6457 	default:
6458 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6459 		    (void *)ipsq, ipsq->ipsq_waitfor);
6460 	}
6461 
6462 	/*
6463 	 * Incr refcnt for the qwriter_ip call below which
6464 	 * does a refrele
6465 	 */
6466 	ill_refhold_locked(ill);
6467 	mutex_exit(&ill->ill_lock);
6468 
6469 	mp = ipsq_pending_mp_get(ipsq, &connp);
6470 	ASSERT(mp != NULL);
6471 
6472 	/*
6473 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6474 	 * we can only get here when the current operation decides it
6475 	 * it needs to quiesce via ipsq_pending_mp_add().
6476 	 */
6477 	switch (mp->b_datap->db_type) {
6478 	case M_PCPROTO:
6479 	case M_PROTO:
6480 		/*
6481 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6482 		 */
6483 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6484 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6485 
6486 		switch (dlindp->dl_notification) {
6487 		case DL_NOTE_PHYS_ADDR:
6488 			qwriter_ip(ill, ill->ill_rq, mp,
6489 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6490 			return;
6491 		default:
6492 			ASSERT(0);
6493 		}
6494 		break;
6495 
6496 	case M_ERROR:
6497 	case M_HANGUP:
6498 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6499 		    B_TRUE);
6500 		return;
6501 
6502 	case M_IOCTL:
6503 	case M_IOCDATA:
6504 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6505 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6506 		return;
6507 
6508 	default:
6509 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6510 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6511 	}
6512 }
6513 
6514 #ifdef ILL_DEBUG
6515 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6516 void
6517 th_trace_rrecord(th_trace_t *th_trace)
6518 {
6519 	tr_buf_t *tr_buf;
6520 	uint_t lastref;
6521 
6522 	lastref = th_trace->th_trace_lastref;
6523 	lastref++;
6524 	if (lastref == TR_BUF_MAX)
6525 		lastref = 0;
6526 	th_trace->th_trace_lastref = lastref;
6527 	tr_buf = &th_trace->th_trbuf[lastref];
6528 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, IP_STACK_DEPTH);
6529 }
6530 
6531 th_trace_t *
6532 th_trace_ipif_lookup(ipif_t *ipif)
6533 {
6534 	int bucket_id;
6535 	th_trace_t *th_trace;
6536 
6537 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6538 
6539 	bucket_id = IP_TR_HASH(curthread);
6540 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6541 
6542 	for (th_trace = ipif->ipif_trace[bucket_id]; th_trace != NULL;
6543 	    th_trace = th_trace->th_next) {
6544 		if (th_trace->th_id == curthread)
6545 			return (th_trace);
6546 	}
6547 	return (NULL);
6548 }
6549 
6550 void
6551 ipif_trace_ref(ipif_t *ipif)
6552 {
6553 	int bucket_id;
6554 	th_trace_t *th_trace;
6555 
6556 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6557 
6558 	if (ipif->ipif_trace_disable)
6559 		return;
6560 
6561 	/*
6562 	 * Attempt to locate the trace buffer for the curthread.
6563 	 * If it does not exist, then allocate a new trace buffer
6564 	 * and link it in list of trace bufs for this ipif, at the head
6565 	 */
6566 	th_trace = th_trace_ipif_lookup(ipif);
6567 	if (th_trace == NULL) {
6568 		bucket_id = IP_TR_HASH(curthread);
6569 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6570 		    KM_NOSLEEP);
6571 		if (th_trace == NULL) {
6572 			ipif->ipif_trace_disable = B_TRUE;
6573 			ipif_trace_cleanup(ipif);
6574 			return;
6575 		}
6576 		th_trace->th_id = curthread;
6577 		th_trace->th_next = ipif->ipif_trace[bucket_id];
6578 		th_trace->th_prev = &ipif->ipif_trace[bucket_id];
6579 		if (th_trace->th_next != NULL)
6580 			th_trace->th_next->th_prev = &th_trace->th_next;
6581 		ipif->ipif_trace[bucket_id] = th_trace;
6582 	}
6583 	ASSERT(th_trace->th_refcnt >= 0 &&
6584 	    th_trace->th_refcnt < TR_BUF_MAX -1);
6585 	th_trace->th_refcnt++;
6586 	th_trace_rrecord(th_trace);
6587 }
6588 
6589 void
6590 ipif_untrace_ref(ipif_t *ipif)
6591 {
6592 	th_trace_t *th_trace;
6593 
6594 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6595 
6596 	if (ipif->ipif_trace_disable)
6597 		return;
6598 	th_trace = th_trace_ipif_lookup(ipif);
6599 	ASSERT(th_trace != NULL);
6600 	ASSERT(th_trace->th_refcnt > 0);
6601 
6602 	th_trace->th_refcnt--;
6603 	th_trace_rrecord(th_trace);
6604 }
6605 
6606 th_trace_t *
6607 th_trace_ill_lookup(ill_t *ill)
6608 {
6609 	th_trace_t *th_trace;
6610 	int bucket_id;
6611 
6612 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6613 
6614 	bucket_id = IP_TR_HASH(curthread);
6615 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6616 
6617 	for (th_trace = ill->ill_trace[bucket_id]; th_trace != NULL;
6618 	    th_trace = th_trace->th_next) {
6619 		if (th_trace->th_id == curthread)
6620 			return (th_trace);
6621 	}
6622 	return (NULL);
6623 }
6624 
6625 void
6626 ill_trace_ref(ill_t *ill)
6627 {
6628 	int bucket_id;
6629 	th_trace_t *th_trace;
6630 
6631 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6632 	if (ill->ill_trace_disable)
6633 		return;
6634 	/*
6635 	 * Attempt to locate the trace buffer for the curthread.
6636 	 * If it does not exist, then allocate a new trace buffer
6637 	 * and link it in list of trace bufs for this ill, at the head
6638 	 */
6639 	th_trace = th_trace_ill_lookup(ill);
6640 	if (th_trace == NULL) {
6641 		bucket_id = IP_TR_HASH(curthread);
6642 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6643 		    KM_NOSLEEP);
6644 		if (th_trace == NULL) {
6645 			ill->ill_trace_disable = B_TRUE;
6646 			ill_trace_cleanup(ill);
6647 			return;
6648 		}
6649 		th_trace->th_id = curthread;
6650 		th_trace->th_next = ill->ill_trace[bucket_id];
6651 		th_trace->th_prev = &ill->ill_trace[bucket_id];
6652 		if (th_trace->th_next != NULL)
6653 			th_trace->th_next->th_prev = &th_trace->th_next;
6654 		ill->ill_trace[bucket_id] = th_trace;
6655 	}
6656 	ASSERT(th_trace->th_refcnt >= 0 &&
6657 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6658 
6659 	th_trace->th_refcnt++;
6660 	th_trace_rrecord(th_trace);
6661 }
6662 
6663 void
6664 ill_untrace_ref(ill_t *ill)
6665 {
6666 	th_trace_t *th_trace;
6667 
6668 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6669 
6670 	if (ill->ill_trace_disable)
6671 		return;
6672 	th_trace = th_trace_ill_lookup(ill);
6673 	ASSERT(th_trace != NULL);
6674 	ASSERT(th_trace->th_refcnt > 0);
6675 
6676 	th_trace->th_refcnt--;
6677 	th_trace_rrecord(th_trace);
6678 }
6679 
6680 /*
6681  * Verify that this thread has no refs to the ipif and free
6682  * the trace buffers
6683  */
6684 /* ARGSUSED */
6685 void
6686 ipif_thread_exit(ipif_t *ipif, void *dummy)
6687 {
6688 	th_trace_t *th_trace;
6689 
6690 	mutex_enter(&ipif->ipif_ill->ill_lock);
6691 
6692 	th_trace = th_trace_ipif_lookup(ipif);
6693 	if (th_trace == NULL) {
6694 		mutex_exit(&ipif->ipif_ill->ill_lock);
6695 		return;
6696 	}
6697 	ASSERT(th_trace->th_refcnt == 0);
6698 	/* unlink th_trace and free it */
6699 	*th_trace->th_prev = th_trace->th_next;
6700 	if (th_trace->th_next != NULL)
6701 		th_trace->th_next->th_prev = th_trace->th_prev;
6702 	th_trace->th_next = NULL;
6703 	th_trace->th_prev = NULL;
6704 	kmem_free(th_trace, sizeof (th_trace_t));
6705 
6706 	mutex_exit(&ipif->ipif_ill->ill_lock);
6707 }
6708 
6709 /*
6710  * Verify that this thread has no refs to the ill and free
6711  * the trace buffers
6712  */
6713 /* ARGSUSED */
6714 void
6715 ill_thread_exit(ill_t *ill, void *dummy)
6716 {
6717 	th_trace_t *th_trace;
6718 
6719 	mutex_enter(&ill->ill_lock);
6720 
6721 	th_trace = th_trace_ill_lookup(ill);
6722 	if (th_trace == NULL) {
6723 		mutex_exit(&ill->ill_lock);
6724 		return;
6725 	}
6726 	ASSERT(th_trace->th_refcnt == 0);
6727 	/* unlink th_trace and free it */
6728 	*th_trace->th_prev = th_trace->th_next;
6729 	if (th_trace->th_next != NULL)
6730 		th_trace->th_next->th_prev = th_trace->th_prev;
6731 	th_trace->th_next = NULL;
6732 	th_trace->th_prev = NULL;
6733 	kmem_free(th_trace, sizeof (th_trace_t));
6734 
6735 	mutex_exit(&ill->ill_lock);
6736 }
6737 #endif
6738 
6739 #ifdef ILL_DEBUG
6740 void
6741 ip_thread_exit_stack(ip_stack_t *ipst)
6742 {
6743 	ill_t	*ill;
6744 	ipif_t	*ipif;
6745 	ill_walk_context_t	ctx;
6746 
6747 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6748 	ill = ILL_START_WALK_ALL(&ctx, ipst);
6749 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6750 		for (ipif = ill->ill_ipif; ipif != NULL;
6751 		    ipif = ipif->ipif_next) {
6752 			ipif_thread_exit(ipif, NULL);
6753 		}
6754 		ill_thread_exit(ill, NULL);
6755 	}
6756 	rw_exit(&ipst->ips_ill_g_lock);
6757 
6758 	ire_walk(ire_thread_exit, NULL, ipst);
6759 	ndp_walk_common(ipst->ips_ndp4, NULL, nce_thread_exit, NULL, B_FALSE);
6760 	ndp_walk_common(ipst->ips_ndp6, NULL, nce_thread_exit, NULL, B_FALSE);
6761 }
6762 
6763 /*
6764  * This is a function which is called from thread_exit
6765  * that can be used to debug reference count issues in IP. See comment in
6766  * <inet/ip.h> on how it is used.
6767  */
6768 void
6769 ip_thread_exit(void)
6770 {
6771 	netstack_t *ns;
6772 
6773 	ns = netstack_get_current();
6774 	if (ns != NULL) {
6775 		ip_thread_exit_stack(ns->netstack_ip);
6776 		netstack_rele(ns);
6777 	}
6778 }
6779 
6780 /*
6781  * Called when ipif is unplumbed or when memory alloc fails
6782  */
6783 void
6784 ipif_trace_cleanup(ipif_t *ipif)
6785 {
6786 	int	i;
6787 	th_trace_t	*th_trace;
6788 	th_trace_t	*th_trace_next;
6789 
6790 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6791 		for (th_trace = ipif->ipif_trace[i]; th_trace != NULL;
6792 		    th_trace = th_trace_next) {
6793 			th_trace_next = th_trace->th_next;
6794 			kmem_free(th_trace, sizeof (th_trace_t));
6795 		}
6796 		ipif->ipif_trace[i] = NULL;
6797 	}
6798 }
6799 
6800 /*
6801  * Called when ill is unplumbed or when memory alloc fails
6802  */
6803 void
6804 ill_trace_cleanup(ill_t *ill)
6805 {
6806 	int	i;
6807 	th_trace_t	*th_trace;
6808 	th_trace_t	*th_trace_next;
6809 
6810 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6811 		for (th_trace = ill->ill_trace[i]; th_trace != NULL;
6812 		    th_trace = th_trace_next) {
6813 			th_trace_next = th_trace->th_next;
6814 			kmem_free(th_trace, sizeof (th_trace_t));
6815 		}
6816 		ill->ill_trace[i] = NULL;
6817 	}
6818 }
6819 
6820 #else
6821 void ip_thread_exit(void) {}
6822 #endif
6823 
6824 void
6825 ipif_refhold_locked(ipif_t *ipif)
6826 {
6827 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6828 	ipif->ipif_refcnt++;
6829 	IPIF_TRACE_REF(ipif);
6830 }
6831 
6832 void
6833 ipif_refhold(ipif_t *ipif)
6834 {
6835 	ill_t	*ill;
6836 
6837 	ill = ipif->ipif_ill;
6838 	mutex_enter(&ill->ill_lock);
6839 	ipif->ipif_refcnt++;
6840 	IPIF_TRACE_REF(ipif);
6841 	mutex_exit(&ill->ill_lock);
6842 }
6843 
6844 /*
6845  * Must not be called while holding any locks. Otherwise if this is
6846  * the last reference to be released there is a chance of recursive mutex
6847  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6848  * to restart an ioctl.
6849  */
6850 void
6851 ipif_refrele(ipif_t *ipif)
6852 {
6853 	ill_t	*ill;
6854 
6855 	ill = ipif->ipif_ill;
6856 
6857 	mutex_enter(&ill->ill_lock);
6858 	ASSERT(ipif->ipif_refcnt != 0);
6859 	ipif->ipif_refcnt--;
6860 	IPIF_UNTRACE_REF(ipif);
6861 	if (ipif->ipif_refcnt != 0) {
6862 		mutex_exit(&ill->ill_lock);
6863 		return;
6864 	}
6865 
6866 	/* Drops the ill_lock */
6867 	ipif_ill_refrele_tail(ill);
6868 }
6869 
6870 ipif_t *
6871 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6872 {
6873 	ipif_t	*ipif;
6874 
6875 	mutex_enter(&ill->ill_lock);
6876 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6877 	    ipif != NULL; ipif = ipif->ipif_next) {
6878 		if (!IPIF_CAN_LOOKUP(ipif))
6879 			continue;
6880 		ipif_refhold_locked(ipif);
6881 		mutex_exit(&ill->ill_lock);
6882 		return (ipif);
6883 	}
6884 	mutex_exit(&ill->ill_lock);
6885 	return (NULL);
6886 }
6887 
6888 /*
6889  * TODO: make this table extendible at run time
6890  * Return a pointer to the mac type info for 'mac_type'
6891  */
6892 static ip_m_t *
6893 ip_m_lookup(t_uscalar_t mac_type)
6894 {
6895 	ip_m_t	*ipm;
6896 
6897 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6898 		if (ipm->ip_m_mac_type == mac_type)
6899 			return (ipm);
6900 	return (NULL);
6901 }
6902 
6903 /*
6904  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6905  * ipif_arg is passed in to associate it with the correct interface.
6906  * We may need to restart this operation if the ipif cannot be looked up
6907  * due to an exclusive operation that is currently in progress. The restart
6908  * entry point is specified by 'func'
6909  */
6910 int
6911 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6912     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6913     ire_t **ire_arg, boolean_t ioctl_msg, queue_t *q, mblk_t *mp,
6914     ipsq_func_t func, struct rtsa_s *sp, ip_stack_t *ipst)
6915 {
6916 	ire_t	*ire;
6917 	ire_t	*gw_ire = NULL;
6918 	ipif_t	*ipif = NULL;
6919 	boolean_t ipif_refheld = B_FALSE;
6920 	uint_t	type;
6921 	int	match_flags = MATCH_IRE_TYPE;
6922 	int	error;
6923 	tsol_gc_t *gc = NULL;
6924 	tsol_gcgrp_t *gcgrp = NULL;
6925 	boolean_t gcgrp_xtraref = B_FALSE;
6926 
6927 	ip1dbg(("ip_rt_add:"));
6928 
6929 	if (ire_arg != NULL)
6930 		*ire_arg = NULL;
6931 
6932 	/*
6933 	 * If this is the case of RTF_HOST being set, then we set the netmask
6934 	 * to all ones (regardless if one was supplied).
6935 	 */
6936 	if (flags & RTF_HOST)
6937 		mask = IP_HOST_MASK;
6938 
6939 	/*
6940 	 * Prevent routes with a zero gateway from being created (since
6941 	 * interfaces can currently be plumbed and brought up no assigned
6942 	 * address).
6943 	 * For routes with RTA_SRCIFP, the gateway address can be 0.0.0.0.
6944 	 */
6945 	if (gw_addr == 0 && src_ipif == NULL)
6946 		return (ENETUNREACH);
6947 	/*
6948 	 * Get the ipif, if any, corresponding to the gw_addr
6949 	 */
6950 	if (gw_addr != 0) {
6951 		ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func,
6952 		    &error, ipst);
6953 		if (ipif != NULL) {
6954 			if (IS_VNI(ipif->ipif_ill)) {
6955 				ipif_refrele(ipif);
6956 				return (EINVAL);
6957 			}
6958 			ipif_refheld = B_TRUE;
6959 		} else if (error == EINPROGRESS) {
6960 			ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6961 			return (EINPROGRESS);
6962 		} else {
6963 			error = 0;
6964 		}
6965 	}
6966 
6967 	if (ipif != NULL) {
6968 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6969 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6970 	} else {
6971 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6972 	}
6973 
6974 	/*
6975 	 * GateD will attempt to create routes with a loopback interface
6976 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6977 	 * these routes to be added, but create them as interface routes
6978 	 * since the gateway is an interface address.
6979 	 */
6980 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6981 		flags &= ~RTF_GATEWAY;
6982 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6983 		    mask == IP_HOST_MASK) {
6984 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6985 			    ALL_ZONES, NULL, match_flags, ipst);
6986 			if (ire != NULL) {
6987 				ire_refrele(ire);
6988 				if (ipif_refheld)
6989 					ipif_refrele(ipif);
6990 				return (EEXIST);
6991 			}
6992 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6993 			    "for 0x%x\n", (void *)ipif,
6994 			    ipif->ipif_ire_type,
6995 			    ntohl(ipif->ipif_lcl_addr)));
6996 			ire = ire_create(
6997 			    (uchar_t *)&dst_addr,	/* dest address */
6998 			    (uchar_t *)&mask,		/* mask */
6999 			    (uchar_t *)&ipif->ipif_src_addr,
7000 			    NULL,			/* no gateway */
7001 			    NULL,
7002 			    &ipif->ipif_mtu,
7003 			    NULL,
7004 			    ipif->ipif_rq,		/* recv-from queue */
7005 			    NULL,			/* no send-to queue */
7006 			    ipif->ipif_ire_type,	/* LOOPBACK */
7007 			    ipif,
7008 			    NULL,
7009 			    0,
7010 			    0,
7011 			    0,
7012 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
7013 			    RTF_PRIVATE : 0,
7014 			    &ire_uinfo_null,
7015 			    NULL,
7016 			    NULL,
7017 			    ipst);
7018 
7019 			if (ire == NULL) {
7020 				if (ipif_refheld)
7021 					ipif_refrele(ipif);
7022 				return (ENOMEM);
7023 			}
7024 			error = ire_add(&ire, q, mp, func, B_FALSE);
7025 			if (error == 0)
7026 				goto save_ire;
7027 			if (ipif_refheld)
7028 				ipif_refrele(ipif);
7029 			return (error);
7030 
7031 		}
7032 	}
7033 
7034 	/*
7035 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
7036 	 * and the gateway address provided is one of the system's interface
7037 	 * addresses.  By using the routing socket interface and supplying an
7038 	 * RTA_IFP sockaddr with an interface index, an alternate method of
7039 	 * specifying an interface route to be created is available which uses
7040 	 * the interface index that specifies the outgoing interface rather than
7041 	 * the address of an outgoing interface (which may not be able to
7042 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
7043 	 * flag, routes can be specified which not only specify the next-hop to
7044 	 * be used when routing to a certain prefix, but also which outgoing
7045 	 * interface should be used.
7046 	 *
7047 	 * Previously, interfaces would have unique addresses assigned to them
7048 	 * and so the address assigned to a particular interface could be used
7049 	 * to identify a particular interface.  One exception to this was the
7050 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
7051 	 *
7052 	 * With the advent of IPv6 and its link-local addresses, this
7053 	 * restriction was relaxed and interfaces could share addresses between
7054 	 * themselves.  In fact, typically all of the link-local interfaces on
7055 	 * an IPv6 node or router will have the same link-local address.  In
7056 	 * order to differentiate between these interfaces, the use of an
7057 	 * interface index is necessary and this index can be carried inside a
7058 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
7059 	 * of using the interface index, however, is that all of the ipif's that
7060 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
7061 	 * cannot be used to differentiate between ipif's (or logical
7062 	 * interfaces) that belong to the same ill (physical interface).
7063 	 *
7064 	 * For example, in the following case involving IPv4 interfaces and
7065 	 * logical interfaces
7066 	 *
7067 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
7068 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
7069 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
7070 	 *
7071 	 * the ipif's corresponding to each of these interface routes can be
7072 	 * uniquely identified by the "gateway" (actually interface address).
7073 	 *
7074 	 * In this case involving multiple IPv6 default routes to a particular
7075 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
7076 	 * default route is of interest:
7077 	 *
7078 	 *	default		fe80::123:4567:89ab:cdef	U	if0
7079 	 *	default		fe80::123:4567:89ab:cdef	U	if1
7080 	 */
7081 
7082 	/* RTF_GATEWAY not set */
7083 	if (!(flags & RTF_GATEWAY)) {
7084 		queue_t	*stq;
7085 		queue_t	*rfq = NULL;
7086 		ill_t	*in_ill = NULL;
7087 
7088 		if (sp != NULL) {
7089 			ip2dbg(("ip_rt_add: gateway security attributes "
7090 			    "cannot be set with interface route\n"));
7091 			if (ipif_refheld)
7092 				ipif_refrele(ipif);
7093 			return (EINVAL);
7094 		}
7095 
7096 		/*
7097 		 * As the interface index specified with the RTA_IFP sockaddr is
7098 		 * the same for all ipif's off of an ill, the matching logic
7099 		 * below uses MATCH_IRE_ILL if such an index was specified.
7100 		 * This means that routes sharing the same prefix when added
7101 		 * using a RTA_IFP sockaddr must have distinct interface
7102 		 * indices (namely, they must be on distinct ill's).
7103 		 *
7104 		 * On the other hand, since the gateway address will usually be
7105 		 * different for each ipif on the system, the matching logic
7106 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
7107 		 * route.  This means that interface routes for the same prefix
7108 		 * can be created if they belong to distinct ipif's and if a
7109 		 * RTA_IFP sockaddr is not present.
7110 		 */
7111 		if (ipif_arg != NULL) {
7112 			if (ipif_refheld)  {
7113 				ipif_refrele(ipif);
7114 				ipif_refheld = B_FALSE;
7115 			}
7116 			ipif = ipif_arg;
7117 			match_flags |= MATCH_IRE_ILL;
7118 		} else {
7119 			/*
7120 			 * Check the ipif corresponding to the gw_addr
7121 			 */
7122 			if (ipif == NULL)
7123 				return (ENETUNREACH);
7124 			match_flags |= MATCH_IRE_IPIF;
7125 		}
7126 		ASSERT(ipif != NULL);
7127 		/*
7128 		 * If src_ipif is not NULL, we have to create
7129 		 * an ire with non-null ire_in_ill value
7130 		 */
7131 		if (src_ipif != NULL) {
7132 			in_ill = src_ipif->ipif_ill;
7133 		}
7134 
7135 		/*
7136 		 * We check for an existing entry at this point.
7137 		 *
7138 		 * Since a netmask isn't passed in via the ioctl interface
7139 		 * (SIOCADDRT), we don't check for a matching netmask in that
7140 		 * case.
7141 		 */
7142 		if (!ioctl_msg)
7143 			match_flags |= MATCH_IRE_MASK;
7144 		if (src_ipif != NULL) {
7145 			/* Look up in the special table */
7146 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
7147 			    ipif, src_ipif->ipif_ill, match_flags);
7148 		} else {
7149 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7150 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
7151 			    NULL, match_flags, ipst);
7152 		}
7153 		if (ire != NULL) {
7154 			ire_refrele(ire);
7155 			if (ipif_refheld)
7156 				ipif_refrele(ipif);
7157 			return (EEXIST);
7158 		}
7159 
7160 		if (src_ipif != NULL) {
7161 			/*
7162 			 * Create the special ire for the IRE table
7163 			 * which hangs out of ire_in_ill. This ire
7164 			 * is in-between IRE_CACHE and IRE_INTERFACE.
7165 			 * Thus rfq is non-NULL.
7166 			 */
7167 			rfq = ipif->ipif_rq;
7168 		}
7169 		/* Create the usual interface ires */
7170 
7171 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
7172 		    ? ipif->ipif_rq : ipif->ipif_wq;
7173 
7174 		/*
7175 		 * Create a copy of the IRE_LOOPBACK,
7176 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
7177 		 * the modified address and netmask.
7178 		 */
7179 		ire = ire_create(
7180 		    (uchar_t *)&dst_addr,
7181 		    (uint8_t *)&mask,
7182 		    (uint8_t *)&ipif->ipif_src_addr,
7183 		    NULL,
7184 		    NULL,
7185 		    &ipif->ipif_mtu,
7186 		    NULL,
7187 		    rfq,
7188 		    stq,
7189 		    ipif->ipif_net_type,
7190 		    ipif,
7191 		    in_ill,
7192 		    0,
7193 		    0,
7194 		    0,
7195 		    flags,
7196 		    &ire_uinfo_null,
7197 		    NULL,
7198 		    NULL,
7199 		    ipst);
7200 		if (ire == NULL) {
7201 			if (ipif_refheld)
7202 				ipif_refrele(ipif);
7203 			return (ENOMEM);
7204 		}
7205 
7206 		/*
7207 		 * Some software (for example, GateD and Sun Cluster) attempts
7208 		 * to create (what amount to) IRE_PREFIX routes with the
7209 		 * loopback address as the gateway.  This is primarily done to
7210 		 * set up prefixes with the RTF_REJECT flag set (for example,
7211 		 * when generating aggregate routes.)
7212 		 *
7213 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7214 		 * IRE_LOOPBACK, then we map the request into a
7215 		 * IRE_IF_NORESOLVER.
7216 		 *
7217 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7218 		 * routine, but rather using ire_create() directly.
7219 		 *
7220 		 */
7221 		if (ipif->ipif_net_type == IRE_LOOPBACK)
7222 			ire->ire_type = IRE_IF_NORESOLVER;
7223 
7224 		error = ire_add(&ire, q, mp, func, B_FALSE);
7225 		if (error == 0)
7226 			goto save_ire;
7227 
7228 		/*
7229 		 * In the result of failure, ire_add() will have already
7230 		 * deleted the ire in question, so there is no need to
7231 		 * do that here.
7232 		 */
7233 		if (ipif_refheld)
7234 			ipif_refrele(ipif);
7235 		return (error);
7236 	}
7237 	if (ipif_refheld) {
7238 		ipif_refrele(ipif);
7239 		ipif_refheld = B_FALSE;
7240 	}
7241 
7242 	if (src_ipif != NULL) {
7243 		/* RTA_SRCIFP is not supported on RTF_GATEWAY */
7244 		ip2dbg(("ip_rt_add: SRCIF cannot be set with gateway route\n"));
7245 		return (EINVAL);
7246 	}
7247 	/*
7248 	 * Get an interface IRE for the specified gateway.
7249 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7250 	 * gateway, it is currently unreachable and we fail the request
7251 	 * accordingly.
7252 	 */
7253 	ipif = ipif_arg;
7254 	if (ipif_arg != NULL)
7255 		match_flags |= MATCH_IRE_ILL;
7256 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7257 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7258 	if (gw_ire == NULL)
7259 		return (ENETUNREACH);
7260 
7261 	/*
7262 	 * We create one of three types of IREs as a result of this request
7263 	 * based on the netmask.  A netmask of all ones (which is automatically
7264 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7265 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7266 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7267 	 * destination prefix.
7268 	 */
7269 	if (mask == IP_HOST_MASK)
7270 		type = IRE_HOST;
7271 	else if (mask == 0)
7272 		type = IRE_DEFAULT;
7273 	else
7274 		type = IRE_PREFIX;
7275 
7276 	/* check for a duplicate entry */
7277 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7278 	    NULL, ALL_ZONES, 0, NULL,
7279 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7280 	if (ire != NULL) {
7281 		ire_refrele(gw_ire);
7282 		ire_refrele(ire);
7283 		return (EEXIST);
7284 	}
7285 
7286 	/* Security attribute exists */
7287 	if (sp != NULL) {
7288 		tsol_gcgrp_addr_t ga;
7289 
7290 		/* find or create the gateway credentials group */
7291 		ga.ga_af = AF_INET;
7292 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7293 
7294 		/* we hold reference to it upon success */
7295 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7296 		if (gcgrp == NULL) {
7297 			ire_refrele(gw_ire);
7298 			return (ENOMEM);
7299 		}
7300 
7301 		/*
7302 		 * Create and add the security attribute to the group; a
7303 		 * reference to the group is made upon allocating a new
7304 		 * entry successfully.  If it finds an already-existing
7305 		 * entry for the security attribute in the group, it simply
7306 		 * returns it and no new reference is made to the group.
7307 		 */
7308 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7309 		if (gc == NULL) {
7310 			/* release reference held by gcgrp_lookup */
7311 			GCGRP_REFRELE(gcgrp);
7312 			ire_refrele(gw_ire);
7313 			return (ENOMEM);
7314 		}
7315 	}
7316 
7317 	/* Create the IRE. */
7318 	ire = ire_create(
7319 	    (uchar_t *)&dst_addr,		/* dest address */
7320 	    (uchar_t *)&mask,			/* mask */
7321 	    /* src address assigned by the caller? */
7322 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7323 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7324 	    (uchar_t *)&gw_addr,		/* gateway address */
7325 	    NULL,				/* no in-srcaddress */
7326 	    &gw_ire->ire_max_frag,
7327 	    NULL,				/* no src nce */
7328 	    NULL,				/* no recv-from queue */
7329 	    NULL,				/* no send-to queue */
7330 	    (ushort_t)type,			/* IRE type */
7331 	    ipif_arg,
7332 	    NULL,
7333 	    0,
7334 	    0,
7335 	    0,
7336 	    flags,
7337 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7338 	    gc,					/* security attribute */
7339 	    NULL,
7340 	    ipst);
7341 
7342 	/*
7343 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7344 	 * reference to the 'gcgrp'. We can now release the extra reference
7345 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7346 	 */
7347 	if (gcgrp_xtraref)
7348 		GCGRP_REFRELE(gcgrp);
7349 	if (ire == NULL) {
7350 		if (gc != NULL)
7351 			GC_REFRELE(gc);
7352 		ire_refrele(gw_ire);
7353 		return (ENOMEM);
7354 	}
7355 
7356 	/*
7357 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7358 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7359 	 */
7360 
7361 	/* Add the new IRE. */
7362 	error = ire_add(&ire, q, mp, func, B_FALSE);
7363 	if (error != 0) {
7364 		/*
7365 		 * In the result of failure, ire_add() will have already
7366 		 * deleted the ire in question, so there is no need to
7367 		 * do that here.
7368 		 */
7369 		ire_refrele(gw_ire);
7370 		return (error);
7371 	}
7372 
7373 	if (flags & RTF_MULTIRT) {
7374 		/*
7375 		 * Invoke the CGTP (multirouting) filtering module
7376 		 * to add the dst address in the filtering database.
7377 		 * Replicated inbound packets coming from that address
7378 		 * will be filtered to discard the duplicates.
7379 		 * It is not necessary to call the CGTP filter hook
7380 		 * when the dst address is a broadcast or multicast,
7381 		 * because an IP source address cannot be a broadcast
7382 		 * or a multicast.
7383 		 */
7384 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7385 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7386 		if (ire_dst != NULL) {
7387 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7388 			ire_refrele(ire_dst);
7389 			goto save_ire;
7390 		}
7391 		if ((ip_cgtp_filter_ops != NULL) && !CLASSD(ire->ire_addr) &&
7392 		    ipst->ips_netstack->netstack_stackid == GLOBAL_NETSTACKID) {
7393 			int res = ip_cgtp_filter_ops->cfo_add_dest_v4(
7394 			    ire->ire_addr,
7395 			    ire->ire_gateway_addr,
7396 			    ire->ire_src_addr,
7397 			    gw_ire->ire_src_addr);
7398 			if (res != 0) {
7399 				ire_refrele(gw_ire);
7400 				ire_delete(ire);
7401 				return (res);
7402 			}
7403 		}
7404 	}
7405 
7406 	/*
7407 	 * Now that the prefix IRE entry has been created, delete any
7408 	 * existing gateway IRE cache entries as well as any IRE caches
7409 	 * using the gateway, and force them to be created through
7410 	 * ip_newroute.
7411 	 */
7412 	if (gc != NULL) {
7413 		ASSERT(gcgrp != NULL);
7414 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7415 	}
7416 
7417 save_ire:
7418 	if (gw_ire != NULL) {
7419 		ire_refrele(gw_ire);
7420 	}
7421 	/*
7422 	 * We do not do save_ire for the routes added with RTA_SRCIFP
7423 	 * flag. This route is only added and deleted by mipagent.
7424 	 * So, for simplicity of design, we refrain from saving
7425 	 * ires that are created with srcif value. This may change
7426 	 * in future if we find more usage of srcifp feature.
7427 	 */
7428 	if (ipif != NULL && src_ipif == NULL) {
7429 		/*
7430 		 * Save enough information so that we can recreate the IRE if
7431 		 * the interface goes down and then up.  The metrics associated
7432 		 * with the route will be saved as well when rts_setmetrics() is
7433 		 * called after the IRE has been created.  In the case where
7434 		 * memory cannot be allocated, none of this information will be
7435 		 * saved.
7436 		 */
7437 		ipif_save_ire(ipif, ire);
7438 	}
7439 	if (ioctl_msg)
7440 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7441 	if (ire_arg != NULL) {
7442 		/*
7443 		 * Store the ire that was successfully added into where ire_arg
7444 		 * points to so that callers don't have to look it up
7445 		 * themselves (but they are responsible for ire_refrele()ing
7446 		 * the ire when they are finished with it).
7447 		 */
7448 		*ire_arg = ire;
7449 	} else {
7450 		ire_refrele(ire);		/* Held in ire_add */
7451 	}
7452 	if (ipif_refheld)
7453 		ipif_refrele(ipif);
7454 	return (0);
7455 }
7456 
7457 /*
7458  * ip_rt_delete is called to delete an IPv4 route.
7459  * ipif_arg is passed in to associate it with the correct interface.
7460  * src_ipif is passed to associate the incoming interface of the packet.
7461  * We may need to restart this operation if the ipif cannot be looked up
7462  * due to an exclusive operation that is currently in progress. The restart
7463  * entry point is specified by 'func'
7464  */
7465 /* ARGSUSED4 */
7466 int
7467 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7468     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
7469     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
7470     ip_stack_t *ipst)
7471 {
7472 	ire_t	*ire = NULL;
7473 	ipif_t	*ipif;
7474 	boolean_t ipif_refheld = B_FALSE;
7475 	uint_t	type;
7476 	uint_t	match_flags = MATCH_IRE_TYPE;
7477 	int	err = 0;
7478 
7479 	ip1dbg(("ip_rt_delete:"));
7480 	/*
7481 	 * If this is the case of RTF_HOST being set, then we set the netmask
7482 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7483 	 */
7484 	if (flags & RTF_HOST) {
7485 		mask = IP_HOST_MASK;
7486 		match_flags |= MATCH_IRE_MASK;
7487 	} else if (rtm_addrs & RTA_NETMASK) {
7488 		match_flags |= MATCH_IRE_MASK;
7489 	}
7490 
7491 	/*
7492 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7493 	 * we check if the gateway address is one of our interfaces first,
7494 	 * and fall back on RTF_GATEWAY routes.
7495 	 *
7496 	 * This makes it possible to delete an original
7497 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7498 	 *
7499 	 * As the interface index specified with the RTA_IFP sockaddr is the
7500 	 * same for all ipif's off of an ill, the matching logic below uses
7501 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7502 	 * sharing the same prefix and interface index as the the route
7503 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7504 	 * is specified in the request.
7505 	 *
7506 	 * On the other hand, since the gateway address will usually be
7507 	 * different for each ipif on the system, the matching logic
7508 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7509 	 * route.  This means that interface routes for the same prefix can be
7510 	 * uniquely identified if they belong to distinct ipif's and if a
7511 	 * RTA_IFP sockaddr is not present.
7512 	 *
7513 	 * For more detail on specifying routes by gateway address and by
7514 	 * interface index, see the comments in ip_rt_add().
7515 	 * gw_addr could be zero in some cases when both RTA_SRCIFP and
7516 	 * RTA_IFP are specified. If RTA_SRCIFP is specified and  both
7517 	 * RTA_IFP and gateway_addr are NULL/zero, then delete will not
7518 	 * succeed.
7519 	 */
7520 	if (src_ipif != NULL) {
7521 		if (ipif_arg == NULL && gw_addr != 0) {
7522 			ipif_arg = ipif_lookup_interface(gw_addr, dst_addr,
7523 			    q, mp, func, &err, ipst);
7524 			if (ipif_arg != NULL)
7525 				ipif_refheld = B_TRUE;
7526 		}
7527 		if (ipif_arg == NULL) {
7528 			err = (err == EINPROGRESS) ? err : ESRCH;
7529 			return (err);
7530 		}
7531 		ipif = ipif_arg;
7532 	} else {
7533 		ipif = ipif_lookup_interface(gw_addr, dst_addr,
7534 		    q, mp, func, &err, ipst);
7535 		if (ipif != NULL)
7536 			ipif_refheld = B_TRUE;
7537 		else if (err == EINPROGRESS)
7538 			return (err);
7539 		else
7540 			err = 0;
7541 	}
7542 	if (ipif != NULL) {
7543 		if (ipif_arg != NULL) {
7544 			if (ipif_refheld) {
7545 				ipif_refrele(ipif);
7546 				ipif_refheld = B_FALSE;
7547 			}
7548 			ipif = ipif_arg;
7549 			match_flags |= MATCH_IRE_ILL;
7550 		} else {
7551 			match_flags |= MATCH_IRE_IPIF;
7552 		}
7553 		if (src_ipif != NULL) {
7554 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
7555 			    ipif, src_ipif->ipif_ill, match_flags);
7556 		} else {
7557 			if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7558 				ire = ire_ctable_lookup(dst_addr, 0,
7559 				    IRE_LOOPBACK, ipif, ALL_ZONES, NULL,
7560 				    match_flags, ipst);
7561 			}
7562 			if (ire == NULL) {
7563 				ire = ire_ftable_lookup(dst_addr, mask, 0,
7564 				    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
7565 				    NULL, match_flags, ipst);
7566 			}
7567 		}
7568 	}
7569 
7570 	if (ire == NULL) {
7571 		/*
7572 		 * At this point, the gateway address is not one of our own
7573 		 * addresses or a matching interface route was not found.  We
7574 		 * set the IRE type to lookup based on whether
7575 		 * this is a host route, a default route or just a prefix.
7576 		 *
7577 		 * If an ipif_arg was passed in, then the lookup is based on an
7578 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7579 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7580 		 * set as the route being looked up is not a traditional
7581 		 * interface route.
7582 		 * Since we do not add gateway route with srcipif, we don't
7583 		 * expect to find it either.
7584 		 */
7585 		if (src_ipif != NULL) {
7586 			if (ipif_refheld)
7587 				ipif_refrele(ipif);
7588 			return (ESRCH);
7589 		} else {
7590 			match_flags &= ~MATCH_IRE_IPIF;
7591 			match_flags |= MATCH_IRE_GW;
7592 			if (ipif_arg != NULL)
7593 				match_flags |= MATCH_IRE_ILL;
7594 			if (mask == IP_HOST_MASK)
7595 				type = IRE_HOST;
7596 			else if (mask == 0)
7597 				type = IRE_DEFAULT;
7598 			else
7599 				type = IRE_PREFIX;
7600 			ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type,
7601 			    ipif_arg, NULL, ALL_ZONES, 0, NULL, match_flags,
7602 			    ipst);
7603 		}
7604 	}
7605 
7606 	if (ipif_refheld)
7607 		ipif_refrele(ipif);
7608 
7609 	/* ipif is not refheld anymore */
7610 	if (ire == NULL)
7611 		return (ESRCH);
7612 
7613 	if (ire->ire_flags & RTF_MULTIRT) {
7614 		/*
7615 		 * Invoke the CGTP (multirouting) filtering module
7616 		 * to remove the dst address from the filtering database.
7617 		 * Packets coming from that address will no longer be
7618 		 * filtered to remove duplicates.
7619 		 */
7620 		if (ip_cgtp_filter_ops != NULL &&
7621 		    ipst->ips_netstack->netstack_stackid == GLOBAL_NETSTACKID) {
7622 			err = ip_cgtp_filter_ops->cfo_del_dest_v4(
7623 			    ire->ire_addr, ire->ire_gateway_addr);
7624 		}
7625 		ip_cgtp_bcast_delete(ire, ipst);
7626 	}
7627 
7628 	ipif = ire->ire_ipif;
7629 	/*
7630 	 * Removing from ipif_saved_ire_mp is not necessary
7631 	 * when src_ipif being non-NULL. ip_rt_add does not
7632 	 * save the ires which src_ipif being non-NULL.
7633 	 */
7634 	if (ipif != NULL && src_ipif == NULL) {
7635 		ipif_remove_ire(ipif, ire);
7636 	}
7637 	if (ioctl_msg)
7638 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7639 	ire_delete(ire);
7640 	ire_refrele(ire);
7641 	return (err);
7642 }
7643 
7644 /*
7645  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7646  */
7647 /* ARGSUSED */
7648 int
7649 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7650     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7651 {
7652 	ipaddr_t dst_addr;
7653 	ipaddr_t gw_addr;
7654 	ipaddr_t mask;
7655 	int error = 0;
7656 	mblk_t *mp1;
7657 	struct rtentry *rt;
7658 	ipif_t *ipif = NULL;
7659 	ip_stack_t	*ipst;
7660 
7661 	ASSERT(q->q_next == NULL);
7662 	ipst = CONNQ_TO_IPST(q);
7663 
7664 	ip1dbg(("ip_siocaddrt:"));
7665 	/* Existence of mp1 verified in ip_wput_nondata */
7666 	mp1 = mp->b_cont->b_cont;
7667 	rt = (struct rtentry *)mp1->b_rptr;
7668 
7669 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7670 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7671 
7672 	/*
7673 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7674 	 * to a particular host address.  In this case, we set the netmask to
7675 	 * all ones for the particular destination address.  Otherwise,
7676 	 * determine the netmask to be used based on dst_addr and the interfaces
7677 	 * in use.
7678 	 */
7679 	if (rt->rt_flags & RTF_HOST) {
7680 		mask = IP_HOST_MASK;
7681 	} else {
7682 		/*
7683 		 * Note that ip_subnet_mask returns a zero mask in the case of
7684 		 * default (an all-zeroes address).
7685 		 */
7686 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7687 	}
7688 
7689 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7690 	    NULL, B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7691 	if (ipif != NULL)
7692 		ipif_refrele(ipif);
7693 	return (error);
7694 }
7695 
7696 /*
7697  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7698  */
7699 /* ARGSUSED */
7700 int
7701 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7702     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7703 {
7704 	ipaddr_t dst_addr;
7705 	ipaddr_t gw_addr;
7706 	ipaddr_t mask;
7707 	int error;
7708 	mblk_t *mp1;
7709 	struct rtentry *rt;
7710 	ipif_t *ipif = NULL;
7711 	ip_stack_t	*ipst;
7712 
7713 	ASSERT(q->q_next == NULL);
7714 	ipst = CONNQ_TO_IPST(q);
7715 
7716 	ip1dbg(("ip_siocdelrt:"));
7717 	/* Existence of mp1 verified in ip_wput_nondata */
7718 	mp1 = mp->b_cont->b_cont;
7719 	rt = (struct rtentry *)mp1->b_rptr;
7720 
7721 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7722 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7723 
7724 	/*
7725 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7726 	 * to a particular host address.  In this case, we set the netmask to
7727 	 * all ones for the particular destination address.  Otherwise,
7728 	 * determine the netmask to be used based on dst_addr and the interfaces
7729 	 * in use.
7730 	 */
7731 	if (rt->rt_flags & RTF_HOST) {
7732 		mask = IP_HOST_MASK;
7733 	} else {
7734 		/*
7735 		 * Note that ip_subnet_mask returns a zero mask in the case of
7736 		 * default (an all-zeroes address).
7737 		 */
7738 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7739 	}
7740 
7741 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7742 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, NULL,
7743 	    B_TRUE, q, mp, ip_process_ioctl, ipst);
7744 	if (ipif != NULL)
7745 		ipif_refrele(ipif);
7746 	return (error);
7747 }
7748 
7749 /*
7750  * Enqueue the mp onto the ipsq, chained by b_next.
7751  * b_prev stores the function to be executed later, and b_queue the queue
7752  * where this mp originated.
7753  */
7754 void
7755 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7756     ill_t *pending_ill)
7757 {
7758 	conn_t	*connp = NULL;
7759 
7760 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7761 	ASSERT(func != NULL);
7762 
7763 	mp->b_queue = q;
7764 	mp->b_prev = (void *)func;
7765 	mp->b_next = NULL;
7766 
7767 	switch (type) {
7768 	case CUR_OP:
7769 		if (ipsq->ipsq_mptail != NULL) {
7770 			ASSERT(ipsq->ipsq_mphead != NULL);
7771 			ipsq->ipsq_mptail->b_next = mp;
7772 		} else {
7773 			ASSERT(ipsq->ipsq_mphead == NULL);
7774 			ipsq->ipsq_mphead = mp;
7775 		}
7776 		ipsq->ipsq_mptail = mp;
7777 		break;
7778 
7779 	case NEW_OP:
7780 		if (ipsq->ipsq_xopq_mptail != NULL) {
7781 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7782 			ipsq->ipsq_xopq_mptail->b_next = mp;
7783 		} else {
7784 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7785 			ipsq->ipsq_xopq_mphead = mp;
7786 		}
7787 		ipsq->ipsq_xopq_mptail = mp;
7788 		break;
7789 	default:
7790 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7791 	}
7792 
7793 	if (CONN_Q(q) && pending_ill != NULL) {
7794 		connp = Q_TO_CONN(q);
7795 
7796 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7797 		connp->conn_oper_pending_ill = pending_ill;
7798 	}
7799 }
7800 
7801 /*
7802  * Return the mp at the head of the ipsq. After emptying the ipsq
7803  * look at the next ioctl, if this ioctl is complete. Otherwise
7804  * return, we will resume when we complete the current ioctl.
7805  * The current ioctl will wait till it gets a response from the
7806  * driver below.
7807  */
7808 static mblk_t *
7809 ipsq_dq(ipsq_t *ipsq)
7810 {
7811 	mblk_t	*mp;
7812 
7813 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7814 
7815 	mp = ipsq->ipsq_mphead;
7816 	if (mp != NULL) {
7817 		ipsq->ipsq_mphead = mp->b_next;
7818 		if (ipsq->ipsq_mphead == NULL)
7819 			ipsq->ipsq_mptail = NULL;
7820 		mp->b_next = NULL;
7821 		return (mp);
7822 	}
7823 	if (ipsq->ipsq_current_ipif != NULL)
7824 		return (NULL);
7825 	mp = ipsq->ipsq_xopq_mphead;
7826 	if (mp != NULL) {
7827 		ipsq->ipsq_xopq_mphead = mp->b_next;
7828 		if (ipsq->ipsq_xopq_mphead == NULL)
7829 			ipsq->ipsq_xopq_mptail = NULL;
7830 		mp->b_next = NULL;
7831 		return (mp);
7832 	}
7833 	return (NULL);
7834 }
7835 
7836 /*
7837  * Enter the ipsq corresponding to ill, by waiting synchronously till
7838  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7839  * will have to drain completely before ipsq_enter returns success.
7840  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7841  * and the ipsq_exit logic will start the next enqueued ioctl after
7842  * completion of the current ioctl. If 'force' is used, we don't wait
7843  * for the enqueued ioctls. This is needed when a conn_close wants to
7844  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7845  * of an ill can also use this option. But we dont' use it currently.
7846  */
7847 #define	ENTER_SQ_WAIT_TICKS 100
7848 boolean_t
7849 ipsq_enter(ill_t *ill, boolean_t force)
7850 {
7851 	ipsq_t	*ipsq;
7852 	boolean_t waited_enough = B_FALSE;
7853 
7854 	/*
7855 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7856 	 * Since the <ill-ipsq> assocs could change while we wait for the
7857 	 * writer, it is easier to wait on a fixed global rather than try to
7858 	 * cv_wait on a changing ipsq.
7859 	 */
7860 	mutex_enter(&ill->ill_lock);
7861 	for (;;) {
7862 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7863 			mutex_exit(&ill->ill_lock);
7864 			return (B_FALSE);
7865 		}
7866 
7867 		ipsq = ill->ill_phyint->phyint_ipsq;
7868 		mutex_enter(&ipsq->ipsq_lock);
7869 		if (ipsq->ipsq_writer == NULL &&
7870 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7871 			break;
7872 		} else if (ipsq->ipsq_writer != NULL) {
7873 			mutex_exit(&ipsq->ipsq_lock);
7874 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7875 		} else {
7876 			mutex_exit(&ipsq->ipsq_lock);
7877 			if (force) {
7878 				(void) cv_timedwait(&ill->ill_cv,
7879 				    &ill->ill_lock,
7880 				    lbolt + ENTER_SQ_WAIT_TICKS);
7881 				waited_enough = B_TRUE;
7882 				continue;
7883 			} else {
7884 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7885 			}
7886 		}
7887 	}
7888 
7889 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7890 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7891 	ipsq->ipsq_writer = curthread;
7892 	ipsq->ipsq_reentry_cnt++;
7893 #ifdef ILL_DEBUG
7894 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7895 #endif
7896 	mutex_exit(&ipsq->ipsq_lock);
7897 	mutex_exit(&ill->ill_lock);
7898 	return (B_TRUE);
7899 }
7900 
7901 /*
7902  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7903  * certain critical operations like plumbing (i.e. most set ioctls),
7904  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7905  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7906  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7907  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7908  * threads executing in the ipsq. Responses from the driver pertain to the
7909  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7910  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7911  *
7912  * If a thread does not want to reenter the ipsq when it is already writer,
7913  * it must make sure that the specified reentry point to be called later
7914  * when the ipsq is empty, nor any code path starting from the specified reentry
7915  * point must never ever try to enter the ipsq again. Otherwise it can lead
7916  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7917  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7918  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7919  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7920  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7921  * ioctl if the current ioctl has completed. If the current ioctl is still
7922  * in progress it simply returns. The current ioctl could be waiting for
7923  * a response from another module (arp_ or the driver or could be waiting for
7924  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7925  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7926  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7927  * ipsq_current_ipif is clear which happens only on ioctl completion.
7928  */
7929 
7930 /*
7931  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7932  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7933  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7934  * completion.
7935  */
7936 ipsq_t *
7937 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7938     ipsq_func_t func, int type, boolean_t reentry_ok)
7939 {
7940 	ipsq_t	*ipsq;
7941 
7942 	/* Only 1 of ipif or ill can be specified */
7943 	ASSERT((ipif != NULL) ^ (ill != NULL));
7944 	if (ipif != NULL)
7945 		ill = ipif->ipif_ill;
7946 
7947 	/*
7948 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7949 	 * ipsq of an ill can't change when ill_lock is held.
7950 	 */
7951 	GRAB_CONN_LOCK(q);
7952 	mutex_enter(&ill->ill_lock);
7953 	ipsq = ill->ill_phyint->phyint_ipsq;
7954 	mutex_enter(&ipsq->ipsq_lock);
7955 
7956 	/*
7957 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7958 	 *    (Note: If the caller does not specify reentry_ok then neither
7959 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7960 	 *    again. Otherwise it can lead to an infinite loop
7961 	 * 2. Enter the ipsq if there is no current writer and this attempted
7962 	 *    entry is part of the current ioctl or operation
7963 	 * 3. Enter the ipsq if there is no current writer and this is a new
7964 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7965 	 *    empty and there is no ioctl (or operation) currently in progress
7966 	 */
7967 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7968 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7969 	    ipsq->ipsq_current_ipif == NULL))) ||
7970 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7971 		/* Success. */
7972 		ipsq->ipsq_reentry_cnt++;
7973 		ipsq->ipsq_writer = curthread;
7974 		mutex_exit(&ipsq->ipsq_lock);
7975 		mutex_exit(&ill->ill_lock);
7976 		RELEASE_CONN_LOCK(q);
7977 #ifdef ILL_DEBUG
7978 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7979 #endif
7980 		return (ipsq);
7981 	}
7982 
7983 	ipsq_enq(ipsq, q, mp, func, type, ill);
7984 
7985 	mutex_exit(&ipsq->ipsq_lock);
7986 	mutex_exit(&ill->ill_lock);
7987 	RELEASE_CONN_LOCK(q);
7988 	return (NULL);
7989 }
7990 
7991 /*
7992  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7993  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7994  * cannot be entered, the mp is queued for completion.
7995  */
7996 void
7997 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7998     boolean_t reentry_ok)
7999 {
8000 	ipsq_t	*ipsq;
8001 
8002 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
8003 
8004 	/*
8005 	 * Drop the caller's refhold on the ill.  This is safe since we either
8006 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
8007 	 * IPSQ, in which case we return without accessing ill anymore.  This
8008 	 * is needed because func needs to see the correct refcount.
8009 	 * e.g. removeif can work only then.
8010 	 */
8011 	ill_refrele(ill);
8012 	if (ipsq != NULL) {
8013 		(*func)(ipsq, q, mp, NULL);
8014 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
8015 	}
8016 }
8017 
8018 /*
8019  * If there are more than ILL_GRP_CNT ills in a group,
8020  * we use kmem alloc'd buffers, else use the stack
8021  */
8022 #define	ILL_GRP_CNT	14
8023 /*
8024  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
8025  * Called by a thread that is currently exclusive on this ipsq.
8026  */
8027 void
8028 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
8029 {
8030 	queue_t	*q;
8031 	mblk_t	*mp;
8032 	ipsq_func_t	func;
8033 	int	next;
8034 	ill_t	**ill_list = NULL;
8035 	size_t	ill_list_size = 0;
8036 	int	cnt = 0;
8037 	boolean_t need_ipsq_free = B_FALSE;
8038 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
8039 
8040 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8041 	mutex_enter(&ipsq->ipsq_lock);
8042 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
8043 	if (ipsq->ipsq_reentry_cnt != 1) {
8044 		ipsq->ipsq_reentry_cnt--;
8045 		mutex_exit(&ipsq->ipsq_lock);
8046 		return;
8047 	}
8048 
8049 	mp = ipsq_dq(ipsq);
8050 	while (mp != NULL) {
8051 again:
8052 		mutex_exit(&ipsq->ipsq_lock);
8053 		func = (ipsq_func_t)mp->b_prev;
8054 		q = (queue_t *)mp->b_queue;
8055 		mp->b_prev = NULL;
8056 		mp->b_queue = NULL;
8057 
8058 		/*
8059 		 * If 'q' is an conn queue, it is valid, since we did a
8060 		 * a refhold on the connp, at the start of the ioctl.
8061 		 * If 'q' is an ill queue, it is valid, since close of an
8062 		 * ill will clean up the 'ipsq'.
8063 		 */
8064 		(*func)(ipsq, q, mp, NULL);
8065 
8066 		mutex_enter(&ipsq->ipsq_lock);
8067 		mp = ipsq_dq(ipsq);
8068 	}
8069 
8070 	mutex_exit(&ipsq->ipsq_lock);
8071 
8072 	/*
8073 	 * Need to grab the locks in the right order. Need to
8074 	 * atomically check (under ipsq_lock) that there are no
8075 	 * messages before relinquishing the ipsq. Also need to
8076 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
8077 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
8078 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
8079 	 * to grab ill_g_lock as writer.
8080 	 */
8081 	rw_enter(&ipst->ips_ill_g_lock,
8082 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
8083 
8084 	/* ipsq_refs can't change while ill_g_lock is held as reader */
8085 	if (ipsq->ipsq_refs != 0) {
8086 		/* At most 2 ills v4/v6 per phyint */
8087 		cnt = ipsq->ipsq_refs << 1;
8088 		ill_list_size = cnt * sizeof (ill_t *);
8089 		/*
8090 		 * If memory allocation fails, we will do the split
8091 		 * the next time ipsq_exit is called for whatever reason.
8092 		 * As long as the ipsq_split flag is set the need to
8093 		 * split is remembered.
8094 		 */
8095 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
8096 		if (ill_list != NULL)
8097 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
8098 	}
8099 	mutex_enter(&ipsq->ipsq_lock);
8100 	mp = ipsq_dq(ipsq);
8101 	if (mp != NULL) {
8102 		/* oops, some message has landed up, we can't get out */
8103 		if (ill_list != NULL)
8104 			ill_unlock_ills(ill_list, cnt);
8105 		rw_exit(&ipst->ips_ill_g_lock);
8106 		if (ill_list != NULL)
8107 			kmem_free(ill_list, ill_list_size);
8108 		ill_list = NULL;
8109 		ill_list_size = 0;
8110 		cnt = 0;
8111 		goto again;
8112 	}
8113 
8114 	/*
8115 	 * Split only if no ioctl is pending and if memory alloc succeeded
8116 	 * above.
8117 	 */
8118 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
8119 	    ill_list != NULL) {
8120 		/*
8121 		 * No new ill can join this ipsq since we are holding the
8122 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
8123 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
8124 		 * If so we will retry on the next ipsq_exit.
8125 		 */
8126 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
8127 	}
8128 
8129 	/*
8130 	 * We are holding the ipsq lock, hence no new messages can
8131 	 * land up on the ipsq, and there are no messages currently.
8132 	 * Now safe to get out. Wake up waiters and relinquish ipsq
8133 	 * atomically while holding ill locks.
8134 	 */
8135 	ipsq->ipsq_writer = NULL;
8136 	ipsq->ipsq_reentry_cnt--;
8137 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
8138 #ifdef ILL_DEBUG
8139 	ipsq->ipsq_depth = 0;
8140 #endif
8141 	mutex_exit(&ipsq->ipsq_lock);
8142 	/*
8143 	 * For IPMP this should wake up all ills in this ipsq.
8144 	 * We need to hold the ill_lock while waking up waiters to
8145 	 * avoid missed wakeups. But there is no need to acquire all
8146 	 * the ill locks and then wakeup. If we have not acquired all
8147 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
8148 	 * wakes up ills one at a time after getting the right ill_lock
8149 	 */
8150 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
8151 	if (ill_list != NULL)
8152 		ill_unlock_ills(ill_list, cnt);
8153 	if (ipsq->ipsq_refs == 0)
8154 		need_ipsq_free = B_TRUE;
8155 	rw_exit(&ipst->ips_ill_g_lock);
8156 	if (ill_list != 0)
8157 		kmem_free(ill_list, ill_list_size);
8158 
8159 	if (need_ipsq_free) {
8160 		/*
8161 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
8162 		 * looked up. ipsq can be looked up only thru ill or phyint
8163 		 * and there are no ills/phyint on this ipsq.
8164 		 */
8165 		ipsq_delete(ipsq);
8166 	}
8167 	/*
8168 	 * Now start any igmp or mld timers that could not be started
8169 	 * while inside the ipsq. The timers can't be started while inside
8170 	 * the ipsq, since igmp_start_timers may need to call untimeout()
8171 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
8172 	 * there could be a deadlock since the timeout handlers
8173 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
8174 	 * wait in ipsq_enter() trying to get the ipsq.
8175 	 *
8176 	 * However there is one exception to the above. If this thread is
8177 	 * itself the igmp/mld timeout handler thread, then we don't want
8178 	 * to start any new timer until the current handler is done. The
8179 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
8180 	 * all others pass B_TRUE.
8181 	 */
8182 	if (start_igmp_timer) {
8183 		mutex_enter(&ipst->ips_igmp_timer_lock);
8184 		next = ipst->ips_igmp_deferred_next;
8185 		ipst->ips_igmp_deferred_next = INFINITY;
8186 		mutex_exit(&ipst->ips_igmp_timer_lock);
8187 
8188 		if (next != INFINITY)
8189 			igmp_start_timers(next, ipst);
8190 	}
8191 
8192 	if (start_mld_timer) {
8193 		mutex_enter(&ipst->ips_mld_timer_lock);
8194 		next = ipst->ips_mld_deferred_next;
8195 		ipst->ips_mld_deferred_next = INFINITY;
8196 		mutex_exit(&ipst->ips_mld_timer_lock);
8197 
8198 		if (next != INFINITY)
8199 			mld_start_timers(next, ipst);
8200 	}
8201 }
8202 
8203 /*
8204  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
8205  * and `ioccmd'.
8206  */
8207 void
8208 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
8209 {
8210 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8211 
8212 	mutex_enter(&ipsq->ipsq_lock);
8213 	ASSERT(ipsq->ipsq_current_ipif == NULL);
8214 	ASSERT(ipsq->ipsq_current_ioctl == 0);
8215 	ipsq->ipsq_current_ipif = ipif;
8216 	ipsq->ipsq_current_ioctl = ioccmd;
8217 	mutex_exit(&ipsq->ipsq_lock);
8218 }
8219 
8220 /*
8221  * Finish the current exclusive operation on `ipsq'.  Note that other
8222  * operations will not be able to proceed until an ipsq_exit() is done.
8223  */
8224 void
8225 ipsq_current_finish(ipsq_t *ipsq)
8226 {
8227 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8228 
8229 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8230 
8231 	/*
8232 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8233 	 * (but we're careful to never set IPIF_CHANGING in that case).
8234 	 */
8235 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8236 		mutex_enter(&ipif->ipif_ill->ill_lock);
8237 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8238 
8239 		/* Send any queued event */
8240 		ill_nic_info_dispatch(ipif->ipif_ill);
8241 		mutex_exit(&ipif->ipif_ill->ill_lock);
8242 	}
8243 
8244 	mutex_enter(&ipsq->ipsq_lock);
8245 	ASSERT(ipsq->ipsq_current_ipif != NULL);
8246 	ipsq->ipsq_current_ipif = NULL;
8247 	ipsq->ipsq_current_ioctl = 0;
8248 	mutex_exit(&ipsq->ipsq_lock);
8249 }
8250 
8251 /*
8252  * The ill is closing. Flush all messages on the ipsq that originated
8253  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8254  * for this ill since ipsq_enter could not have entered until then.
8255  * New messages can't be queued since the CONDEMNED flag is set.
8256  */
8257 static void
8258 ipsq_flush(ill_t *ill)
8259 {
8260 	queue_t	*q;
8261 	mblk_t	*prev;
8262 	mblk_t	*mp;
8263 	mblk_t	*mp_next;
8264 	ipsq_t	*ipsq;
8265 
8266 	ASSERT(IAM_WRITER_ILL(ill));
8267 	ipsq = ill->ill_phyint->phyint_ipsq;
8268 	/*
8269 	 * Flush any messages sent up by the driver.
8270 	 */
8271 	mutex_enter(&ipsq->ipsq_lock);
8272 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8273 		mp_next = mp->b_next;
8274 		q = mp->b_queue;
8275 		if (q == ill->ill_rq || q == ill->ill_wq) {
8276 			/* Remove the mp from the ipsq */
8277 			if (prev == NULL)
8278 				ipsq->ipsq_mphead = mp->b_next;
8279 			else
8280 				prev->b_next = mp->b_next;
8281 			if (ipsq->ipsq_mptail == mp) {
8282 				ASSERT(mp_next == NULL);
8283 				ipsq->ipsq_mptail = prev;
8284 			}
8285 			inet_freemsg(mp);
8286 		} else {
8287 			prev = mp;
8288 		}
8289 	}
8290 	mutex_exit(&ipsq->ipsq_lock);
8291 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8292 	ipsq_xopq_mp_cleanup(ill, NULL);
8293 	ill_pending_mp_cleanup(ill);
8294 }
8295 
8296 /* ARGSUSED */
8297 int
8298 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8299     ip_ioctl_cmd_t *ipip, void *ifreq)
8300 {
8301 	ill_t	*ill;
8302 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8303 	boolean_t isv6;
8304 	conn_t	*connp;
8305 	ip_stack_t	*ipst;
8306 
8307 	connp = Q_TO_CONN(q);
8308 	ipst = connp->conn_netstack->netstack_ip;
8309 	isv6 = connp->conn_af_isv6;
8310 	/*
8311 	 * Set original index.
8312 	 * Failover and failback move logical interfaces
8313 	 * from one physical interface to another.  The
8314 	 * original index indicates the parent of a logical
8315 	 * interface, in other words, the physical interface
8316 	 * the logical interface will be moved back to on
8317 	 * failback.
8318 	 */
8319 
8320 	/*
8321 	 * Don't allow the original index to be changed
8322 	 * for non-failover addresses, autoconfigured
8323 	 * addresses, or IPv6 link local addresses.
8324 	 */
8325 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8326 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8327 		return (EINVAL);
8328 	}
8329 	/*
8330 	 * The new original index must be in use by some
8331 	 * physical interface.
8332 	 */
8333 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8334 	    NULL, NULL, ipst);
8335 	if (ill == NULL)
8336 		return (ENXIO);
8337 	ill_refrele(ill);
8338 
8339 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8340 	/*
8341 	 * When this ipif gets failed back, don't
8342 	 * preserve the original id, as it is no
8343 	 * longer applicable.
8344 	 */
8345 	ipif->ipif_orig_ipifid = 0;
8346 	/*
8347 	 * For IPv4, change the original index of any
8348 	 * multicast addresses associated with the
8349 	 * ipif to the new value.
8350 	 */
8351 	if (!isv6) {
8352 		ilm_t *ilm;
8353 
8354 		mutex_enter(&ipif->ipif_ill->ill_lock);
8355 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8356 		    ilm = ilm->ilm_next) {
8357 			if (ilm->ilm_ipif == ipif) {
8358 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8359 			}
8360 		}
8361 		mutex_exit(&ipif->ipif_ill->ill_lock);
8362 	}
8363 	return (0);
8364 }
8365 
8366 /* ARGSUSED */
8367 int
8368 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8369     ip_ioctl_cmd_t *ipip, void *ifreq)
8370 {
8371 	struct lifreq *lifr = (struct lifreq *)ifreq;
8372 
8373 	/*
8374 	 * Get the original interface index i.e the one
8375 	 * before FAILOVER if it ever happened.
8376 	 */
8377 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8378 	return (0);
8379 }
8380 
8381 /*
8382  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8383  * refhold and return the associated ipif
8384  */
8385 int
8386 ip_extract_tunreq(queue_t *q, mblk_t *mp, ipif_t **ipifp, ipsq_func_t func)
8387 {
8388 	boolean_t exists;
8389 	struct iftun_req *ta;
8390 	ipif_t	*ipif;
8391 	ill_t	*ill;
8392 	boolean_t isv6;
8393 	mblk_t	*mp1;
8394 	int	error;
8395 	conn_t	*connp;
8396 	ip_stack_t	*ipst;
8397 
8398 	/* Existence verified in ip_wput_nondata */
8399 	mp1 = mp->b_cont->b_cont;
8400 	ta = (struct iftun_req *)mp1->b_rptr;
8401 	/*
8402 	 * Null terminate the string to protect against buffer
8403 	 * overrun. String was generated by user code and may not
8404 	 * be trusted.
8405 	 */
8406 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8407 
8408 	connp = Q_TO_CONN(q);
8409 	isv6 = connp->conn_af_isv6;
8410 	ipst = connp->conn_netstack->netstack_ip;
8411 
8412 	/* Disallows implicit create */
8413 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8414 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8415 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8416 	if (ipif == NULL)
8417 		return (error);
8418 
8419 	if (ipif->ipif_id != 0) {
8420 		/*
8421 		 * We really don't want to set/get tunnel parameters
8422 		 * on virtual tunnel interfaces.  Only allow the
8423 		 * base tunnel to do these.
8424 		 */
8425 		ipif_refrele(ipif);
8426 		return (EINVAL);
8427 	}
8428 
8429 	/*
8430 	 * Send down to tunnel mod for ioctl processing.
8431 	 * Will finish ioctl in ip_rput_other().
8432 	 */
8433 	ill = ipif->ipif_ill;
8434 	if (ill->ill_net_type == IRE_LOOPBACK) {
8435 		ipif_refrele(ipif);
8436 		return (EOPNOTSUPP);
8437 	}
8438 
8439 	if (ill->ill_wq == NULL) {
8440 		ipif_refrele(ipif);
8441 		return (ENXIO);
8442 	}
8443 	/*
8444 	 * Mark the ioctl as coming from an IPv6 interface for
8445 	 * tun's convenience.
8446 	 */
8447 	if (ill->ill_isv6)
8448 		ta->ifta_flags |= 0x80000000;
8449 	*ipifp = ipif;
8450 	return (0);
8451 }
8452 
8453 /*
8454  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8455  * and return the associated ipif.
8456  * Return value:
8457  *	Non zero: An error has occurred. ci may not be filled out.
8458  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8459  *	a held ipif in ci.ci_ipif.
8460  */
8461 int
8462 ip_extract_lifreq_cmn(queue_t *q, mblk_t *mp, int cmd_type, int flags,
8463     cmd_info_t *ci, ipsq_func_t func)
8464 {
8465 	sin_t		*sin;
8466 	sin6_t		*sin6;
8467 	char		*name;
8468 	struct ifreq    *ifr;
8469 	struct lifreq    *lifr;
8470 	ipif_t		*ipif = NULL;
8471 	ill_t		*ill;
8472 	conn_t		*connp;
8473 	boolean_t	isv6;
8474 	struct iocblk   *iocp = (struct iocblk *)mp->b_rptr;
8475 	boolean_t	exists;
8476 	int		err;
8477 	mblk_t		*mp1;
8478 	zoneid_t	zoneid;
8479 	ip_stack_t	*ipst;
8480 
8481 	if (q->q_next != NULL) {
8482 		ill = (ill_t *)q->q_ptr;
8483 		isv6 = ill->ill_isv6;
8484 		connp = NULL;
8485 		zoneid = ALL_ZONES;
8486 		ipst = ill->ill_ipst;
8487 	} else {
8488 		ill = NULL;
8489 		connp = Q_TO_CONN(q);
8490 		isv6 = connp->conn_af_isv6;
8491 		zoneid = connp->conn_zoneid;
8492 		if (zoneid == GLOBAL_ZONEID) {
8493 			/* global zone can access ipifs in all zones */
8494 			zoneid = ALL_ZONES;
8495 		}
8496 		ipst = connp->conn_netstack->netstack_ip;
8497 	}
8498 
8499 	/* Has been checked in ip_wput_nondata */
8500 	mp1 = mp->b_cont->b_cont;
8501 
8502 
8503 	if (cmd_type == IF_CMD) {
8504 		/* This a old style SIOC[GS]IF* command */
8505 		ifr = (struct ifreq *)mp1->b_rptr;
8506 		/*
8507 		 * Null terminate the string to protect against buffer
8508 		 * overrun. String was generated by user code and may not
8509 		 * be trusted.
8510 		 */
8511 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8512 		sin = (sin_t *)&ifr->ifr_addr;
8513 		name = ifr->ifr_name;
8514 		ci->ci_sin = sin;
8515 		ci->ci_sin6 = NULL;
8516 		ci->ci_lifr = (struct lifreq *)ifr;
8517 	} else {
8518 		/* This a new style SIOC[GS]LIF* command */
8519 		ASSERT(cmd_type == LIF_CMD);
8520 		lifr = (struct lifreq *)mp1->b_rptr;
8521 		/*
8522 		 * Null terminate the string to protect against buffer
8523 		 * overrun. String was generated by user code and may not
8524 		 * be trusted.
8525 		 */
8526 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8527 		name = lifr->lifr_name;
8528 		sin = (sin_t *)&lifr->lifr_addr;
8529 		sin6 = (sin6_t *)&lifr->lifr_addr;
8530 		if (iocp->ioc_cmd == SIOCSLIFGROUPNAME) {
8531 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8532 			    LIFNAMSIZ);
8533 		}
8534 		ci->ci_sin = sin;
8535 		ci->ci_sin6 = sin6;
8536 		ci->ci_lifr = lifr;
8537 	}
8538 
8539 	if (iocp->ioc_cmd == SIOCSLIFNAME) {
8540 		/*
8541 		 * The ioctl will be failed if the ioctl comes down
8542 		 * an conn stream
8543 		 */
8544 		if (ill == NULL) {
8545 			/*
8546 			 * Not an ill queue, return EINVAL same as the
8547 			 * old error code.
8548 			 */
8549 			return (ENXIO);
8550 		}
8551 		ipif = ill->ill_ipif;
8552 		ipif_refhold(ipif);
8553 	} else {
8554 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8555 		    &exists, isv6, zoneid,
8556 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8557 		    ipst);
8558 		if (ipif == NULL) {
8559 			if (err == EINPROGRESS)
8560 				return (err);
8561 			if (iocp->ioc_cmd == SIOCLIFFAILOVER ||
8562 			    iocp->ioc_cmd == SIOCLIFFAILBACK) {
8563 				/*
8564 				 * Need to try both v4 and v6 since this
8565 				 * ioctl can come down either v4 or v6
8566 				 * socket. The lifreq.lifr_family passed
8567 				 * down by this ioctl is AF_UNSPEC.
8568 				 */
8569 				ipif = ipif_lookup_on_name(name,
8570 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8571 				    zoneid, (connp == NULL) ? q :
8572 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8573 				if (err == EINPROGRESS)
8574 					return (err);
8575 			}
8576 			err = 0;	/* Ensure we don't use it below */
8577 		}
8578 	}
8579 
8580 	/*
8581 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8582 	 */
8583 	if (ipif != NULL && ipif->ipif_isv6 && cmd_type == IF_CMD) {
8584 		ipif_refrele(ipif);
8585 		return (ENXIO);
8586 	}
8587 
8588 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8589 	    name[0] == '\0') {
8590 		/*
8591 		 * Handle a or a SIOC?IF* with a null name
8592 		 * during plumb (on the ill queue before the I_PLINK).
8593 		 */
8594 		ipif = ill->ill_ipif;
8595 		ipif_refhold(ipif);
8596 	}
8597 
8598 	if (ipif == NULL)
8599 		return (ENXIO);
8600 
8601 	/*
8602 	 * Allow only GET operations if this ipif has been created
8603 	 * temporarily due to a MOVE operation.
8604 	 */
8605 	if (ipif->ipif_replace_zero && !(flags & IPI_REPL)) {
8606 		ipif_refrele(ipif);
8607 		return (EINVAL);
8608 	}
8609 
8610 	ci->ci_ipif = ipif;
8611 	return (0);
8612 }
8613 
8614 /*
8615  * Return the total number of ipifs.
8616  */
8617 static uint_t
8618 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8619 {
8620 	uint_t numifs = 0;
8621 	ill_t	*ill;
8622 	ill_walk_context_t	ctx;
8623 	ipif_t	*ipif;
8624 
8625 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8626 	ill = ILL_START_WALK_V4(&ctx, ipst);
8627 
8628 	while (ill != NULL) {
8629 		for (ipif = ill->ill_ipif; ipif != NULL;
8630 		    ipif = ipif->ipif_next) {
8631 			if (ipif->ipif_zoneid == zoneid ||
8632 			    ipif->ipif_zoneid == ALL_ZONES)
8633 				numifs++;
8634 		}
8635 		ill = ill_next(&ctx, ill);
8636 	}
8637 	rw_exit(&ipst->ips_ill_g_lock);
8638 	return (numifs);
8639 }
8640 
8641 /*
8642  * Return the total number of ipifs.
8643  */
8644 static uint_t
8645 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8646 {
8647 	uint_t numifs = 0;
8648 	ill_t	*ill;
8649 	ipif_t	*ipif;
8650 	ill_walk_context_t	ctx;
8651 
8652 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8653 
8654 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8655 	if (family == AF_INET)
8656 		ill = ILL_START_WALK_V4(&ctx, ipst);
8657 	else if (family == AF_INET6)
8658 		ill = ILL_START_WALK_V6(&ctx, ipst);
8659 	else
8660 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8661 
8662 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8663 		for (ipif = ill->ill_ipif; ipif != NULL;
8664 		    ipif = ipif->ipif_next) {
8665 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8666 			    !(lifn_flags & LIFC_NOXMIT))
8667 				continue;
8668 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8669 			    !(lifn_flags & LIFC_TEMPORARY))
8670 				continue;
8671 			if (((ipif->ipif_flags &
8672 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8673 			    IPIF_DEPRECATED)) ||
8674 			    IS_LOOPBACK(ill) ||
8675 			    !(ipif->ipif_flags & IPIF_UP)) &&
8676 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8677 				continue;
8678 
8679 			if (zoneid != ipif->ipif_zoneid &&
8680 			    ipif->ipif_zoneid != ALL_ZONES &&
8681 			    (zoneid != GLOBAL_ZONEID ||
8682 			    !(lifn_flags & LIFC_ALLZONES)))
8683 				continue;
8684 
8685 			numifs++;
8686 		}
8687 	}
8688 	rw_exit(&ipst->ips_ill_g_lock);
8689 	return (numifs);
8690 }
8691 
8692 uint_t
8693 ip_get_lifsrcofnum(ill_t *ill)
8694 {
8695 	uint_t numifs = 0;
8696 	ill_t	*ill_head = ill;
8697 	ip_stack_t	*ipst = ill->ill_ipst;
8698 
8699 	/*
8700 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8701 	 * other thread may be trying to relink the ILLs in this usesrc group
8702 	 * and adjusting the ill_usesrc_grp_next pointers
8703 	 */
8704 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8705 	if ((ill->ill_usesrc_ifindex == 0) &&
8706 	    (ill->ill_usesrc_grp_next != NULL)) {
8707 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8708 		    ill = ill->ill_usesrc_grp_next)
8709 			numifs++;
8710 	}
8711 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8712 
8713 	return (numifs);
8714 }
8715 
8716 /* Null values are passed in for ipif, sin, and ifreq */
8717 /* ARGSUSED */
8718 int
8719 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8720     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8721 {
8722 	int *nump;
8723 	conn_t *connp = Q_TO_CONN(q);
8724 
8725 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8726 
8727 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8728 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8729 
8730 	*nump = ip_get_numifs(connp->conn_zoneid,
8731 	    connp->conn_netstack->netstack_ip);
8732 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8733 	return (0);
8734 }
8735 
8736 /* Null values are passed in for ipif, sin, and ifreq */
8737 /* ARGSUSED */
8738 int
8739 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8740     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8741 {
8742 	struct lifnum *lifn;
8743 	mblk_t	*mp1;
8744 	conn_t *connp = Q_TO_CONN(q);
8745 
8746 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8747 
8748 	/* Existence checked in ip_wput_nondata */
8749 	mp1 = mp->b_cont->b_cont;
8750 
8751 	lifn = (struct lifnum *)mp1->b_rptr;
8752 	switch (lifn->lifn_family) {
8753 	case AF_UNSPEC:
8754 	case AF_INET:
8755 	case AF_INET6:
8756 		break;
8757 	default:
8758 		return (EAFNOSUPPORT);
8759 	}
8760 
8761 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8762 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8763 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8764 	return (0);
8765 }
8766 
8767 /* ARGSUSED */
8768 int
8769 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8770     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8771 {
8772 	STRUCT_HANDLE(ifconf, ifc);
8773 	mblk_t *mp1;
8774 	struct iocblk *iocp;
8775 	struct ifreq *ifr;
8776 	ill_walk_context_t	ctx;
8777 	ill_t	*ill;
8778 	ipif_t	*ipif;
8779 	struct sockaddr_in *sin;
8780 	int32_t	ifclen;
8781 	zoneid_t zoneid;
8782 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8783 
8784 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8785 
8786 	ip1dbg(("ip_sioctl_get_ifconf"));
8787 	/* Existence verified in ip_wput_nondata */
8788 	mp1 = mp->b_cont->b_cont;
8789 	iocp = (struct iocblk *)mp->b_rptr;
8790 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8791 
8792 	/*
8793 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8794 	 * the user buffer address and length into which the list of struct
8795 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8796 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8797 	 * the SIOCGIFCONF operation was redefined to simply provide
8798 	 * a large output buffer into which we are supposed to jam the ifreq
8799 	 * array.  The same ioctl command code was used, despite the fact that
8800 	 * both the applications and the kernel code had to change, thus making
8801 	 * it impossible to support both interfaces.
8802 	 *
8803 	 * For reasons not good enough to try to explain, the following
8804 	 * algorithm is used for deciding what to do with one of these:
8805 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8806 	 * form with the output buffer coming down as the continuation message.
8807 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8808 	 * and we have to copy in the ifconf structure to find out how big the
8809 	 * output buffer is and where to copy out to.  Sure no problem...
8810 	 *
8811 	 */
8812 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8813 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8814 		int numifs = 0;
8815 		size_t ifc_bufsize;
8816 
8817 		/*
8818 		 * Must be (better be!) continuation of a TRANSPARENT
8819 		 * IOCTL.  We just copied in the ifconf structure.
8820 		 */
8821 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8822 		    (struct ifconf *)mp1->b_rptr);
8823 
8824 		/*
8825 		 * Allocate a buffer to hold requested information.
8826 		 *
8827 		 * If ifc_len is larger than what is needed, we only
8828 		 * allocate what we will use.
8829 		 *
8830 		 * If ifc_len is smaller than what is needed, return
8831 		 * EINVAL.
8832 		 *
8833 		 * XXX: the ill_t structure can hava 2 counters, for
8834 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8835 		 * number of interfaces for a device, so we don't need
8836 		 * to count them here...
8837 		 */
8838 		numifs = ip_get_numifs(zoneid, ipst);
8839 
8840 		ifclen = STRUCT_FGET(ifc, ifc_len);
8841 		ifc_bufsize = numifs * sizeof (struct ifreq);
8842 		if (ifc_bufsize > ifclen) {
8843 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8844 				/* old behaviour */
8845 				return (EINVAL);
8846 			} else {
8847 				ifc_bufsize = ifclen;
8848 			}
8849 		}
8850 
8851 		mp1 = mi_copyout_alloc(q, mp,
8852 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8853 		if (mp1 == NULL)
8854 			return (ENOMEM);
8855 
8856 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8857 	}
8858 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8859 	/*
8860 	 * the SIOCGIFCONF ioctl only knows about
8861 	 * IPv4 addresses, so don't try to tell
8862 	 * it about interfaces with IPv6-only
8863 	 * addresses. (Last parm 'isv6' is B_FALSE)
8864 	 */
8865 
8866 	ifr = (struct ifreq *)mp1->b_rptr;
8867 
8868 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8869 	ill = ILL_START_WALK_V4(&ctx, ipst);
8870 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8871 		for (ipif = ill->ill_ipif; ipif != NULL;
8872 		    ipif = ipif->ipif_next) {
8873 			if (zoneid != ipif->ipif_zoneid &&
8874 			    ipif->ipif_zoneid != ALL_ZONES)
8875 				continue;
8876 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8877 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8878 					/* old behaviour */
8879 					rw_exit(&ipst->ips_ill_g_lock);
8880 					return (EINVAL);
8881 				} else {
8882 					goto if_copydone;
8883 				}
8884 			}
8885 			(void) ipif_get_name(ipif,
8886 			    ifr->ifr_name,
8887 			    sizeof (ifr->ifr_name));
8888 			sin = (sin_t *)&ifr->ifr_addr;
8889 			*sin = sin_null;
8890 			sin->sin_family = AF_INET;
8891 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8892 			ifr++;
8893 		}
8894 	}
8895 if_copydone:
8896 	rw_exit(&ipst->ips_ill_g_lock);
8897 	mp1->b_wptr = (uchar_t *)ifr;
8898 
8899 	if (STRUCT_BUF(ifc) != NULL) {
8900 		STRUCT_FSET(ifc, ifc_len,
8901 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8902 	}
8903 	return (0);
8904 }
8905 
8906 /*
8907  * Get the interfaces using the address hosted on the interface passed in,
8908  * as a source adddress
8909  */
8910 /* ARGSUSED */
8911 int
8912 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8913     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8914 {
8915 	mblk_t *mp1;
8916 	ill_t	*ill, *ill_head;
8917 	ipif_t	*ipif, *orig_ipif;
8918 	int	numlifs = 0;
8919 	size_t	lifs_bufsize, lifsmaxlen;
8920 	struct	lifreq *lifr;
8921 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8922 	uint_t	ifindex;
8923 	zoneid_t zoneid;
8924 	int err = 0;
8925 	boolean_t isv6 = B_FALSE;
8926 	struct	sockaddr_in	*sin;
8927 	struct	sockaddr_in6	*sin6;
8928 	STRUCT_HANDLE(lifsrcof, lifs);
8929 	ip_stack_t		*ipst;
8930 
8931 	ipst = CONNQ_TO_IPST(q);
8932 
8933 	ASSERT(q->q_next == NULL);
8934 
8935 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8936 
8937 	/* Existence verified in ip_wput_nondata */
8938 	mp1 = mp->b_cont->b_cont;
8939 
8940 	/*
8941 	 * Must be (better be!) continuation of a TRANSPARENT
8942 	 * IOCTL.  We just copied in the lifsrcof structure.
8943 	 */
8944 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8945 	    (struct lifsrcof *)mp1->b_rptr);
8946 
8947 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8948 		return (EINVAL);
8949 
8950 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8951 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8952 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8953 	    ip_process_ioctl, &err, ipst);
8954 	if (ipif == NULL) {
8955 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8956 		    ifindex));
8957 		return (err);
8958 	}
8959 
8960 
8961 	/* Allocate a buffer to hold requested information */
8962 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8963 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8964 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8965 	/* The actual size needed is always returned in lifs_len */
8966 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8967 
8968 	/* If the amount we need is more than what is passed in, abort */
8969 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8970 		ipif_refrele(ipif);
8971 		return (0);
8972 	}
8973 
8974 	mp1 = mi_copyout_alloc(q, mp,
8975 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8976 	if (mp1 == NULL) {
8977 		ipif_refrele(ipif);
8978 		return (ENOMEM);
8979 	}
8980 
8981 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8982 	bzero(mp1->b_rptr, lifs_bufsize);
8983 
8984 	lifr = (struct lifreq *)mp1->b_rptr;
8985 
8986 	ill = ill_head = ipif->ipif_ill;
8987 	orig_ipif = ipif;
8988 
8989 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8990 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8991 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8992 
8993 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8994 	for (; (ill != NULL) && (ill != ill_head);
8995 	    ill = ill->ill_usesrc_grp_next) {
8996 
8997 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8998 			break;
8999 
9000 		ipif = ill->ill_ipif;
9001 		(void) ipif_get_name(ipif,
9002 		    lifr->lifr_name, sizeof (lifr->lifr_name));
9003 		if (ipif->ipif_isv6) {
9004 			sin6 = (sin6_t *)&lifr->lifr_addr;
9005 			*sin6 = sin6_null;
9006 			sin6->sin6_family = AF_INET6;
9007 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
9008 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
9009 			    &ipif->ipif_v6net_mask);
9010 		} else {
9011 			sin = (sin_t *)&lifr->lifr_addr;
9012 			*sin = sin_null;
9013 			sin->sin_family = AF_INET;
9014 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
9015 			lifr->lifr_addrlen = ip_mask_to_plen(
9016 			    ipif->ipif_net_mask);
9017 		}
9018 		lifr++;
9019 	}
9020 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
9021 	rw_exit(&ipst->ips_ill_g_lock);
9022 	ipif_refrele(orig_ipif);
9023 	mp1->b_wptr = (uchar_t *)lifr;
9024 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
9025 
9026 	return (0);
9027 }
9028 
9029 /* ARGSUSED */
9030 int
9031 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
9032     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9033 {
9034 	mblk_t *mp1;
9035 	int	list;
9036 	ill_t	*ill;
9037 	ipif_t	*ipif;
9038 	int	flags;
9039 	int	numlifs = 0;
9040 	size_t	lifc_bufsize;
9041 	struct	lifreq *lifr;
9042 	sa_family_t	family;
9043 	struct	sockaddr_in	*sin;
9044 	struct	sockaddr_in6	*sin6;
9045 	ill_walk_context_t	ctx;
9046 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9047 	int32_t	lifclen;
9048 	zoneid_t zoneid;
9049 	STRUCT_HANDLE(lifconf, lifc);
9050 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
9051 
9052 	ip1dbg(("ip_sioctl_get_lifconf"));
9053 
9054 	ASSERT(q->q_next == NULL);
9055 
9056 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9057 
9058 	/* Existence verified in ip_wput_nondata */
9059 	mp1 = mp->b_cont->b_cont;
9060 
9061 	/*
9062 	 * An extended version of SIOCGIFCONF that takes an
9063 	 * additional address family and flags field.
9064 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
9065 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
9066 	 * interfaces are omitted.
9067 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
9068 	 * unless LIFC_TEMPORARY is specified.
9069 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
9070 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
9071 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
9072 	 * has priority over LIFC_NOXMIT.
9073 	 */
9074 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
9075 
9076 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
9077 		return (EINVAL);
9078 
9079 	/*
9080 	 * Must be (better be!) continuation of a TRANSPARENT
9081 	 * IOCTL.  We just copied in the lifconf structure.
9082 	 */
9083 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
9084 
9085 	family = STRUCT_FGET(lifc, lifc_family);
9086 	flags = STRUCT_FGET(lifc, lifc_flags);
9087 
9088 	switch (family) {
9089 	case AF_UNSPEC:
9090 		/*
9091 		 * walk all ILL's.
9092 		 */
9093 		list = MAX_G_HEADS;
9094 		break;
9095 	case AF_INET:
9096 		/*
9097 		 * walk only IPV4 ILL's.
9098 		 */
9099 		list = IP_V4_G_HEAD;
9100 		break;
9101 	case AF_INET6:
9102 		/*
9103 		 * walk only IPV6 ILL's.
9104 		 */
9105 		list = IP_V6_G_HEAD;
9106 		break;
9107 	default:
9108 		return (EAFNOSUPPORT);
9109 	}
9110 
9111 	/*
9112 	 * Allocate a buffer to hold requested information.
9113 	 *
9114 	 * If lifc_len is larger than what is needed, we only
9115 	 * allocate what we will use.
9116 	 *
9117 	 * If lifc_len is smaller than what is needed, return
9118 	 * EINVAL.
9119 	 */
9120 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
9121 	lifc_bufsize = numlifs * sizeof (struct lifreq);
9122 	lifclen = STRUCT_FGET(lifc, lifc_len);
9123 	if (lifc_bufsize > lifclen) {
9124 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
9125 			return (EINVAL);
9126 		else
9127 			lifc_bufsize = lifclen;
9128 	}
9129 
9130 	mp1 = mi_copyout_alloc(q, mp,
9131 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
9132 	if (mp1 == NULL)
9133 		return (ENOMEM);
9134 
9135 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
9136 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
9137 
9138 	lifr = (struct lifreq *)mp1->b_rptr;
9139 
9140 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9141 	ill = ill_first(list, list, &ctx, ipst);
9142 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
9143 		for (ipif = ill->ill_ipif; ipif != NULL;
9144 		    ipif = ipif->ipif_next) {
9145 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
9146 			    !(flags & LIFC_NOXMIT))
9147 				continue;
9148 
9149 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
9150 			    !(flags & LIFC_TEMPORARY))
9151 				continue;
9152 
9153 			if (((ipif->ipif_flags &
9154 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
9155 			    IPIF_DEPRECATED)) ||
9156 			    IS_LOOPBACK(ill) ||
9157 			    !(ipif->ipif_flags & IPIF_UP)) &&
9158 			    (flags & LIFC_EXTERNAL_SOURCE))
9159 				continue;
9160 
9161 			if (zoneid != ipif->ipif_zoneid &&
9162 			    ipif->ipif_zoneid != ALL_ZONES &&
9163 			    (zoneid != GLOBAL_ZONEID ||
9164 			    !(flags & LIFC_ALLZONES)))
9165 				continue;
9166 
9167 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
9168 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
9169 					rw_exit(&ipst->ips_ill_g_lock);
9170 					return (EINVAL);
9171 				} else {
9172 					goto lif_copydone;
9173 				}
9174 			}
9175 
9176 			(void) ipif_get_name(ipif, lifr->lifr_name,
9177 			    sizeof (lifr->lifr_name));
9178 			if (ipif->ipif_isv6) {
9179 				sin6 = (sin6_t *)&lifr->lifr_addr;
9180 				*sin6 = sin6_null;
9181 				sin6->sin6_family = AF_INET6;
9182 				sin6->sin6_addr =
9183 				    ipif->ipif_v6lcl_addr;
9184 				lifr->lifr_addrlen =
9185 				    ip_mask_to_plen_v6(
9186 				    &ipif->ipif_v6net_mask);
9187 			} else {
9188 				sin = (sin_t *)&lifr->lifr_addr;
9189 				*sin = sin_null;
9190 				sin->sin_family = AF_INET;
9191 				sin->sin_addr.s_addr =
9192 				    ipif->ipif_lcl_addr;
9193 				lifr->lifr_addrlen =
9194 				    ip_mask_to_plen(
9195 				    ipif->ipif_net_mask);
9196 			}
9197 			lifr++;
9198 		}
9199 	}
9200 lif_copydone:
9201 	rw_exit(&ipst->ips_ill_g_lock);
9202 
9203 	mp1->b_wptr = (uchar_t *)lifr;
9204 	if (STRUCT_BUF(lifc) != NULL) {
9205 		STRUCT_FSET(lifc, lifc_len,
9206 		    (int)((uchar_t *)lifr - mp1->b_rptr));
9207 	}
9208 	return (0);
9209 }
9210 
9211 /* ARGSUSED */
9212 int
9213 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
9214     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9215 {
9216 	ip_stack_t	*ipst;
9217 
9218 	if (q->q_next == NULL)
9219 		ipst = CONNQ_TO_IPST(q);
9220 	else
9221 		ipst = ILLQ_TO_IPST(q);
9222 
9223 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9224 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9225 	return (0);
9226 }
9227 
9228 static void
9229 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9230 {
9231 	ip6_asp_t *table;
9232 	size_t table_size;
9233 	mblk_t *data_mp;
9234 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9235 	ip_stack_t	*ipst;
9236 
9237 	if (q->q_next == NULL)
9238 		ipst = CONNQ_TO_IPST(q);
9239 	else
9240 		ipst = ILLQ_TO_IPST(q);
9241 
9242 	/* These two ioctls are I_STR only */
9243 	if (iocp->ioc_count == TRANSPARENT) {
9244 		miocnak(q, mp, 0, EINVAL);
9245 		return;
9246 	}
9247 
9248 	data_mp = mp->b_cont;
9249 	if (data_mp == NULL) {
9250 		/* The user passed us a NULL argument */
9251 		table = NULL;
9252 		table_size = iocp->ioc_count;
9253 	} else {
9254 		/*
9255 		 * The user provided a table.  The stream head
9256 		 * may have copied in the user data in chunks,
9257 		 * so make sure everything is pulled up
9258 		 * properly.
9259 		 */
9260 		if (MBLKL(data_mp) < iocp->ioc_count) {
9261 			mblk_t *new_data_mp;
9262 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9263 			    NULL) {
9264 				miocnak(q, mp, 0, ENOMEM);
9265 				return;
9266 			}
9267 			freemsg(data_mp);
9268 			data_mp = new_data_mp;
9269 			mp->b_cont = data_mp;
9270 		}
9271 		table = (ip6_asp_t *)data_mp->b_rptr;
9272 		table_size = iocp->ioc_count;
9273 	}
9274 
9275 	switch (iocp->ioc_cmd) {
9276 	case SIOCGIP6ADDRPOLICY:
9277 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9278 		if (iocp->ioc_rval == -1)
9279 			iocp->ioc_error = EINVAL;
9280 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9281 		else if (table != NULL &&
9282 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9283 			ip6_asp_t *src = table;
9284 			ip6_asp32_t *dst = (void *)table;
9285 			int count = table_size / sizeof (ip6_asp_t);
9286 			int i;
9287 
9288 			/*
9289 			 * We need to do an in-place shrink of the array
9290 			 * to match the alignment attributes of the
9291 			 * 32-bit ABI looking at it.
9292 			 */
9293 			/* LINTED: logical expression always true: op "||" */
9294 			ASSERT(sizeof (*src) > sizeof (*dst));
9295 			for (i = 1; i < count; i++)
9296 				bcopy(src + i, dst + i, sizeof (*dst));
9297 		}
9298 #endif
9299 		break;
9300 
9301 	case SIOCSIP6ADDRPOLICY:
9302 		ASSERT(mp->b_prev == NULL);
9303 		mp->b_prev = (void *)q;
9304 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9305 		/*
9306 		 * We pass in the datamodel here so that the ip6_asp_replace()
9307 		 * routine can handle converting from 32-bit to native formats
9308 		 * where necessary.
9309 		 *
9310 		 * A better way to handle this might be to convert the inbound
9311 		 * data structure here, and hang it off a new 'mp'; thus the
9312 		 * ip6_asp_replace() logic would always be dealing with native
9313 		 * format data structures..
9314 		 *
9315 		 * (An even simpler way to handle these ioctls is to just
9316 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9317 		 * and just recompile everything that depends on it.)
9318 		 */
9319 #endif
9320 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9321 		    iocp->ioc_flag & IOC_MODELS);
9322 		return;
9323 	}
9324 
9325 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9326 	qreply(q, mp);
9327 }
9328 
9329 static void
9330 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9331 {
9332 	mblk_t 		*data_mp;
9333 	struct dstinforeq	*dir;
9334 	uint8_t		*end, *cur;
9335 	in6_addr_t	*daddr, *saddr;
9336 	ipaddr_t	v4daddr;
9337 	ire_t		*ire;
9338 	char		*slabel, *dlabel;
9339 	boolean_t	isipv4;
9340 	int		match_ire;
9341 	ill_t		*dst_ill;
9342 	ipif_t		*src_ipif, *ire_ipif;
9343 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9344 	zoneid_t	zoneid;
9345 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9346 
9347 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9348 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9349 
9350 	/*
9351 	 * This ioctl is I_STR only, and must have a
9352 	 * data mblk following the M_IOCTL mblk.
9353 	 */
9354 	data_mp = mp->b_cont;
9355 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9356 		miocnak(q, mp, 0, EINVAL);
9357 		return;
9358 	}
9359 
9360 	if (MBLKL(data_mp) < iocp->ioc_count) {
9361 		mblk_t *new_data_mp;
9362 
9363 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9364 			miocnak(q, mp, 0, ENOMEM);
9365 			return;
9366 		}
9367 		freemsg(data_mp);
9368 		data_mp = new_data_mp;
9369 		mp->b_cont = data_mp;
9370 	}
9371 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9372 
9373 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9374 	    end - cur >= sizeof (struct dstinforeq);
9375 	    cur += sizeof (struct dstinforeq)) {
9376 		dir = (struct dstinforeq *)cur;
9377 		daddr = &dir->dir_daddr;
9378 		saddr = &dir->dir_saddr;
9379 
9380 		/*
9381 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9382 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9383 		 * and ipif_select_source[_v6]() do not.
9384 		 */
9385 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9386 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9387 
9388 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9389 		if (isipv4) {
9390 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9391 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9392 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9393 		} else {
9394 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9395 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9396 		}
9397 		if (ire == NULL) {
9398 			dir->dir_dreachable = 0;
9399 
9400 			/* move on to next dst addr */
9401 			continue;
9402 		}
9403 		dir->dir_dreachable = 1;
9404 
9405 		ire_ipif = ire->ire_ipif;
9406 		if (ire_ipif == NULL)
9407 			goto next_dst;
9408 
9409 		/*
9410 		 * We expect to get back an interface ire or a
9411 		 * gateway ire cache entry.  For both types, the
9412 		 * output interface is ire_ipif->ipif_ill.
9413 		 */
9414 		dst_ill = ire_ipif->ipif_ill;
9415 		dir->dir_dmactype = dst_ill->ill_mactype;
9416 
9417 		if (isipv4) {
9418 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9419 		} else {
9420 			src_ipif = ipif_select_source_v6(dst_ill,
9421 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9422 			    zoneid);
9423 		}
9424 		if (src_ipif == NULL)
9425 			goto next_dst;
9426 
9427 		*saddr = src_ipif->ipif_v6lcl_addr;
9428 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9429 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9430 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9431 		dir->dir_sdeprecated =
9432 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9433 		ipif_refrele(src_ipif);
9434 next_dst:
9435 		ire_refrele(ire);
9436 	}
9437 	miocack(q, mp, iocp->ioc_count, 0);
9438 }
9439 
9440 
9441 /*
9442  * Check if this is an address assigned to this machine.
9443  * Skips interfaces that are down by using ire checks.
9444  * Translates mapped addresses to v4 addresses and then
9445  * treats them as such, returning true if the v4 address
9446  * associated with this mapped address is configured.
9447  * Note: Applications will have to be careful what they do
9448  * with the response; use of mapped addresses limits
9449  * what can be done with the socket, especially with
9450  * respect to socket options and ioctls - neither IPv4
9451  * options nor IPv6 sticky options/ancillary data options
9452  * may be used.
9453  */
9454 /* ARGSUSED */
9455 int
9456 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9457     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9458 {
9459 	struct sioc_addrreq *sia;
9460 	sin_t *sin;
9461 	ire_t *ire;
9462 	mblk_t *mp1;
9463 	zoneid_t zoneid;
9464 	ip_stack_t	*ipst;
9465 
9466 	ip1dbg(("ip_sioctl_tmyaddr"));
9467 
9468 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9469 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9470 	ipst = CONNQ_TO_IPST(q);
9471 
9472 	/* Existence verified in ip_wput_nondata */
9473 	mp1 = mp->b_cont->b_cont;
9474 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9475 	sin = (sin_t *)&sia->sa_addr;
9476 	switch (sin->sin_family) {
9477 	case AF_INET6: {
9478 		sin6_t *sin6 = (sin6_t *)sin;
9479 
9480 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9481 			ipaddr_t v4_addr;
9482 
9483 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9484 			    v4_addr);
9485 			ire = ire_ctable_lookup(v4_addr, 0,
9486 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9487 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9488 		} else {
9489 			in6_addr_t v6addr;
9490 
9491 			v6addr = sin6->sin6_addr;
9492 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9493 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9494 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9495 		}
9496 		break;
9497 	}
9498 	case AF_INET: {
9499 		ipaddr_t v4addr;
9500 
9501 		v4addr = sin->sin_addr.s_addr;
9502 		ire = ire_ctable_lookup(v4addr, 0,
9503 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9504 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9505 		break;
9506 	}
9507 	default:
9508 		return (EAFNOSUPPORT);
9509 	}
9510 	if (ire != NULL) {
9511 		sia->sa_res = 1;
9512 		ire_refrele(ire);
9513 	} else {
9514 		sia->sa_res = 0;
9515 	}
9516 	return (0);
9517 }
9518 
9519 /*
9520  * Check if this is an address assigned on-link i.e. neighbor,
9521  * and makes sure it's reachable from the current zone.
9522  * Returns true for my addresses as well.
9523  * Translates mapped addresses to v4 addresses and then
9524  * treats them as such, returning true if the v4 address
9525  * associated with this mapped address is configured.
9526  * Note: Applications will have to be careful what they do
9527  * with the response; use of mapped addresses limits
9528  * what can be done with the socket, especially with
9529  * respect to socket options and ioctls - neither IPv4
9530  * options nor IPv6 sticky options/ancillary data options
9531  * may be used.
9532  */
9533 /* ARGSUSED */
9534 int
9535 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9536     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9537 {
9538 	struct sioc_addrreq *sia;
9539 	sin_t *sin;
9540 	mblk_t	*mp1;
9541 	ire_t *ire = NULL;
9542 	zoneid_t zoneid;
9543 	ip_stack_t	*ipst;
9544 
9545 	ip1dbg(("ip_sioctl_tonlink"));
9546 
9547 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9548 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9549 	ipst = CONNQ_TO_IPST(q);
9550 
9551 	/* Existence verified in ip_wput_nondata */
9552 	mp1 = mp->b_cont->b_cont;
9553 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9554 	sin = (sin_t *)&sia->sa_addr;
9555 
9556 	/*
9557 	 * Match addresses with a zero gateway field to avoid
9558 	 * routes going through a router.
9559 	 * Exclude broadcast and multicast addresses.
9560 	 */
9561 	switch (sin->sin_family) {
9562 	case AF_INET6: {
9563 		sin6_t *sin6 = (sin6_t *)sin;
9564 
9565 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9566 			ipaddr_t v4_addr;
9567 
9568 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9569 			    v4_addr);
9570 			if (!CLASSD(v4_addr)) {
9571 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9572 				    NULL, NULL, zoneid, NULL,
9573 				    MATCH_IRE_GW, ipst);
9574 			}
9575 		} else {
9576 			in6_addr_t v6addr;
9577 			in6_addr_t v6gw;
9578 
9579 			v6addr = sin6->sin6_addr;
9580 			v6gw = ipv6_all_zeros;
9581 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9582 				ire = ire_route_lookup_v6(&v6addr, 0,
9583 				    &v6gw, 0, NULL, NULL, zoneid,
9584 				    NULL, MATCH_IRE_GW, ipst);
9585 			}
9586 		}
9587 		break;
9588 	}
9589 	case AF_INET: {
9590 		ipaddr_t v4addr;
9591 
9592 		v4addr = sin->sin_addr.s_addr;
9593 		if (!CLASSD(v4addr)) {
9594 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9595 			    NULL, NULL, zoneid, NULL,
9596 			    MATCH_IRE_GW, ipst);
9597 		}
9598 		break;
9599 	}
9600 	default:
9601 		return (EAFNOSUPPORT);
9602 	}
9603 	sia->sa_res = 0;
9604 	if (ire != NULL) {
9605 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9606 		    IRE_LOCAL|IRE_LOOPBACK)) {
9607 			sia->sa_res = 1;
9608 		}
9609 		ire_refrele(ire);
9610 	}
9611 	return (0);
9612 }
9613 
9614 /*
9615  * TBD: implement when kernel maintaines a list of site prefixes.
9616  */
9617 /* ARGSUSED */
9618 int
9619 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9620     ip_ioctl_cmd_t *ipip, void *ifreq)
9621 {
9622 	return (ENXIO);
9623 }
9624 
9625 /* ARGSUSED */
9626 int
9627 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9628     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9629 {
9630 	ill_t  		*ill;
9631 	mblk_t		*mp1;
9632 	conn_t		*connp;
9633 	boolean_t	success;
9634 
9635 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9636 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9637 	/* ioctl comes down on an conn */
9638 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9639 	connp = Q_TO_CONN(q);
9640 
9641 	mp->b_datap->db_type = M_IOCTL;
9642 
9643 	/*
9644 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9645 	 * The original mp contains contaminated b_next values due to 'mi',
9646 	 * which is needed to do the mi_copy_done. Unfortunately if we
9647 	 * send down the original mblk itself and if we are popped due to an
9648 	 * an unplumb before the response comes back from tunnel,
9649 	 * the streamhead (which does a freemsg) will see this contaminated
9650 	 * message and the assertion in freemsg about non-null b_next/b_prev
9651 	 * will panic a DEBUG kernel.
9652 	 */
9653 	mp1 = copymsg(mp);
9654 	if (mp1 == NULL)
9655 		return (ENOMEM);
9656 
9657 	ill = ipif->ipif_ill;
9658 	mutex_enter(&connp->conn_lock);
9659 	mutex_enter(&ill->ill_lock);
9660 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9661 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9662 		    mp, 0);
9663 	} else {
9664 		success = ill_pending_mp_add(ill, connp, mp);
9665 	}
9666 	mutex_exit(&ill->ill_lock);
9667 	mutex_exit(&connp->conn_lock);
9668 
9669 	if (success) {
9670 		ip1dbg(("sending down tunparam request "));
9671 		putnext(ill->ill_wq, mp1);
9672 		return (EINPROGRESS);
9673 	} else {
9674 		/* The conn has started closing */
9675 		freemsg(mp1);
9676 		return (EINTR);
9677 	}
9678 }
9679 
9680 static int
9681 ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp, sin_t *sin,
9682     boolean_t x_arp_ioctl, boolean_t if_arp_ioctl)
9683 {
9684 	mblk_t *mp1;
9685 	mblk_t *mp2;
9686 	mblk_t *pending_mp;
9687 	ipaddr_t ipaddr;
9688 	area_t *area;
9689 	struct iocblk *iocp;
9690 	conn_t *connp;
9691 	struct arpreq *ar;
9692 	struct xarpreq *xar;
9693 	boolean_t success;
9694 	int flags, alength;
9695 	char *lladdr;
9696 	ip_stack_t	*ipst;
9697 
9698 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9699 	connp = Q_TO_CONN(q);
9700 	ipst = connp->conn_netstack->netstack_ip;
9701 
9702 	iocp = (struct iocblk *)mp->b_rptr;
9703 	/*
9704 	 * ill has already been set depending on whether
9705 	 * bsd style or interface style ioctl.
9706 	 */
9707 	ASSERT(ill != NULL);
9708 
9709 	/*
9710 	 * Is this one of the new SIOC*XARP ioctls?
9711 	 */
9712 	if (x_arp_ioctl) {
9713 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9714 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9715 		ar = NULL;
9716 
9717 		flags = xar->xarp_flags;
9718 		lladdr = LLADDR(&xar->xarp_ha);
9719 		/*
9720 		 * Validate against user's link layer address length
9721 		 * input and name and addr length limits.
9722 		 */
9723 		alength = ill->ill_phys_addr_length;
9724 		if (iocp->ioc_cmd == SIOCSXARP) {
9725 			if (alength != xar->xarp_ha.sdl_alen ||
9726 			    (alength + xar->xarp_ha.sdl_nlen >
9727 			    sizeof (xar->xarp_ha.sdl_data)))
9728 				return (EINVAL);
9729 		}
9730 	} else {
9731 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9732 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9733 		xar = NULL;
9734 
9735 		flags = ar->arp_flags;
9736 		lladdr = ar->arp_ha.sa_data;
9737 		/*
9738 		 * Theoretically, the sa_family could tell us what link
9739 		 * layer type this operation is trying to deal with. By
9740 		 * common usage AF_UNSPEC means ethernet. We'll assume
9741 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9742 		 * for now. Our new SIOC*XARP ioctls can be used more
9743 		 * generally.
9744 		 *
9745 		 * If the underlying media happens to have a non 6 byte
9746 		 * address, arp module will fail set/get, but the del
9747 		 * operation will succeed.
9748 		 */
9749 		alength = 6;
9750 		if ((iocp->ioc_cmd != SIOCDARP) &&
9751 		    (alength != ill->ill_phys_addr_length)) {
9752 			return (EINVAL);
9753 		}
9754 	}
9755 
9756 	/*
9757 	 * We are going to pass up to ARP a packet chain that looks
9758 	 * like:
9759 	 *
9760 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9761 	 *
9762 	 * Get a copy of the original IOCTL mblk to head the chain,
9763 	 * to be sent up (in mp1). Also get another copy to store
9764 	 * in the ill_pending_mp list, for matching the response
9765 	 * when it comes back from ARP.
9766 	 */
9767 	mp1 = copyb(mp);
9768 	pending_mp = copymsg(mp);
9769 	if (mp1 == NULL || pending_mp == NULL) {
9770 		if (mp1 != NULL)
9771 			freeb(mp1);
9772 		if (pending_mp != NULL)
9773 			inet_freemsg(pending_mp);
9774 		return (ENOMEM);
9775 	}
9776 
9777 	ipaddr = sin->sin_addr.s_addr;
9778 
9779 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9780 	    (caddr_t)&ipaddr);
9781 	if (mp2 == NULL) {
9782 		freeb(mp1);
9783 		inet_freemsg(pending_mp);
9784 		return (ENOMEM);
9785 	}
9786 	/* Put together the chain. */
9787 	mp1->b_cont = mp2;
9788 	mp1->b_datap->db_type = M_IOCTL;
9789 	mp2->b_cont = mp;
9790 	mp2->b_datap->db_type = M_DATA;
9791 
9792 	iocp = (struct iocblk *)mp1->b_rptr;
9793 
9794 	/*
9795 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9796 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9797 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9798 	 * ioc_count field; set ioc_count to be correct.
9799 	 */
9800 	iocp->ioc_count = MBLKL(mp1->b_cont);
9801 
9802 	/*
9803 	 * Set the proper command in the ARP message.
9804 	 * Convert the SIOC{G|S|D}ARP calls into our
9805 	 * AR_ENTRY_xxx calls.
9806 	 */
9807 	area = (area_t *)mp2->b_rptr;
9808 	switch (iocp->ioc_cmd) {
9809 	case SIOCDARP:
9810 	case SIOCDXARP:
9811 		/*
9812 		 * We defer deleting the corresponding IRE until
9813 		 * we return from arp.
9814 		 */
9815 		area->area_cmd = AR_ENTRY_DELETE;
9816 		area->area_proto_mask_offset = 0;
9817 		break;
9818 	case SIOCGARP:
9819 	case SIOCGXARP:
9820 		area->area_cmd = AR_ENTRY_SQUERY;
9821 		area->area_proto_mask_offset = 0;
9822 		break;
9823 	case SIOCSARP:
9824 	case SIOCSXARP: {
9825 		/*
9826 		 * Delete the corresponding ire to make sure IP will
9827 		 * pick up any change from arp.
9828 		 */
9829 		if (!if_arp_ioctl) {
9830 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9831 			break;
9832 		} else {
9833 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9834 			if (ipif != NULL) {
9835 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9836 				    ipst);
9837 				ipif_refrele(ipif);
9838 			}
9839 			break;
9840 		}
9841 	}
9842 	}
9843 	iocp->ioc_cmd = area->area_cmd;
9844 
9845 	/*
9846 	 * Before sending 'mp' to ARP, we have to clear the b_next
9847 	 * and b_prev. Otherwise if STREAMS encounters such a message
9848 	 * in freemsg(), (because ARP can close any time) it can cause
9849 	 * a panic. But mi code needs the b_next and b_prev values of
9850 	 * mp->b_cont, to complete the ioctl. So we store it here
9851 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9852 	 * when the response comes down from ARP.
9853 	 */
9854 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9855 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9856 	mp->b_cont->b_next = NULL;
9857 	mp->b_cont->b_prev = NULL;
9858 
9859 	mutex_enter(&connp->conn_lock);
9860 	mutex_enter(&ill->ill_lock);
9861 	/* conn has not yet started closing, hence this can't fail */
9862 	success = ill_pending_mp_add(ill, connp, pending_mp);
9863 	ASSERT(success);
9864 	mutex_exit(&ill->ill_lock);
9865 	mutex_exit(&connp->conn_lock);
9866 
9867 	/*
9868 	 * Fill in the rest of the ARP operation fields.
9869 	 */
9870 	area->area_hw_addr_length = alength;
9871 	bcopy(lladdr,
9872 	    (char *)area + area->area_hw_addr_offset,
9873 	    area->area_hw_addr_length);
9874 	/* Translate the flags. */
9875 	if (flags & ATF_PERM)
9876 		area->area_flags |= ACE_F_PERMANENT;
9877 	if (flags & ATF_PUBL)
9878 		area->area_flags |= ACE_F_PUBLISH;
9879 	if (flags & ATF_AUTHORITY)
9880 		area->area_flags |= ACE_F_AUTHORITY;
9881 
9882 	/*
9883 	 * Up to ARP it goes.  The response will come
9884 	 * back in ip_wput as an M_IOCACK message, and
9885 	 * will be handed to ip_sioctl_iocack for
9886 	 * completion.
9887 	 */
9888 	putnext(ill->ill_rq, mp1);
9889 	return (EINPROGRESS);
9890 }
9891 
9892 /* ARGSUSED */
9893 int
9894 ip_sioctl_xarp(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9895     ip_ioctl_cmd_t *ipip, void *ifreq)
9896 {
9897 	struct xarpreq *xar;
9898 	boolean_t isv6;
9899 	mblk_t	*mp1;
9900 	int	err;
9901 	conn_t	*connp;
9902 	int ifnamelen;
9903 	ire_t	*ire = NULL;
9904 	ill_t	*ill = NULL;
9905 	struct sockaddr_in *sin;
9906 	boolean_t if_arp_ioctl = B_FALSE;
9907 	ip_stack_t	*ipst;
9908 
9909 	/* ioctl comes down on an conn */
9910 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9911 	connp = Q_TO_CONN(q);
9912 	isv6 = connp->conn_af_isv6;
9913 	ipst = connp->conn_netstack->netstack_ip;
9914 
9915 	/* Existance verified in ip_wput_nondata */
9916 	mp1 = mp->b_cont->b_cont;
9917 
9918 	ASSERT(MBLKL(mp1) >= sizeof (*xar));
9919 	xar = (struct xarpreq *)mp1->b_rptr;
9920 	sin = (sin_t *)&xar->xarp_pa;
9921 
9922 	if (isv6 || (xar->xarp_ha.sdl_family != AF_LINK) ||
9923 	    (xar->xarp_pa.ss_family != AF_INET))
9924 		return (ENXIO);
9925 
9926 	ifnamelen = xar->xarp_ha.sdl_nlen;
9927 	if (ifnamelen != 0) {
9928 		char	*cptr, cval;
9929 
9930 		if (ifnamelen >= LIFNAMSIZ)
9931 			return (EINVAL);
9932 
9933 		/*
9934 		 * Instead of bcopying a bunch of bytes,
9935 		 * null-terminate the string in-situ.
9936 		 */
9937 		cptr = xar->xarp_ha.sdl_data + ifnamelen;
9938 		cval = *cptr;
9939 		*cptr = '\0';
9940 		ill = ill_lookup_on_name(xar->xarp_ha.sdl_data,
9941 		    B_FALSE, isv6, CONNP_TO_WQ(connp), mp, ip_process_ioctl,
9942 		    &err, NULL, ipst);
9943 		*cptr = cval;
9944 		if (ill == NULL)
9945 			return (err);
9946 		if (ill->ill_net_type != IRE_IF_RESOLVER) {
9947 			ill_refrele(ill);
9948 			return (ENXIO);
9949 		}
9950 
9951 		if_arp_ioctl = B_TRUE;
9952 	} else {
9953 		/*
9954 		 * PSARC 2003/088 states that if sdl_nlen == 0, it behaves
9955 		 * as an extended BSD ioctl. The kernel uses the IP address
9956 		 * to figure out the network interface.
9957 		 */
9958 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9959 		    ipst);
9960 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9961 		    ((ill = ire_to_ill(ire)) == NULL) ||
9962 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9963 			if (ire != NULL)
9964 				ire_refrele(ire);
9965 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9966 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9967 			    NULL, MATCH_IRE_TYPE, ipst);
9968 			if ((ire == NULL) ||
9969 			    ((ill = ire_to_ill(ire)) == NULL)) {
9970 				if (ire != NULL)
9971 					ire_refrele(ire);
9972 				return (ENXIO);
9973 			}
9974 		}
9975 		ASSERT(ire != NULL && ill != NULL);
9976 	}
9977 
9978 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_TRUE, if_arp_ioctl);
9979 	if (if_arp_ioctl)
9980 		ill_refrele(ill);
9981 	if (ire != NULL)
9982 		ire_refrele(ire);
9983 
9984 	return (err);
9985 }
9986 
9987 /*
9988  * ARP IOCTLs.
9989  * How does IP get in the business of fronting ARP configuration/queries?
9990  * Well its like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9991  * are by tradition passed in through a datagram socket.  That lands in IP.
9992  * As it happens, this is just as well since the interface is quite crude in
9993  * that it passes in no information about protocol or hardware types, or
9994  * interface association.  After making the protocol assumption, IP is in
9995  * the position to look up the name of the ILL, which ARP will need, and
9996  * format a request that can be handled by ARP.	 The request is passed up
9997  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9998  * back a response.  ARP supports its own set of more general IOCTLs, in
9999  * case anyone is interested.
10000  */
10001 /* ARGSUSED */
10002 int
10003 ip_sioctl_arp(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10004     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
10005 {
10006 	struct arpreq *ar;
10007 	struct sockaddr_in *sin;
10008 	ire_t	*ire;
10009 	boolean_t isv6;
10010 	mblk_t	*mp1;
10011 	int	err;
10012 	conn_t	*connp;
10013 	ill_t	*ill;
10014 	ip_stack_t	*ipst;
10015 
10016 	/* ioctl comes down on an conn */
10017 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
10018 	connp = Q_TO_CONN(q);
10019 	ipst = CONNQ_TO_IPST(q);
10020 	isv6 = connp->conn_af_isv6;
10021 	if (isv6)
10022 		return (ENXIO);
10023 
10024 	/* Existance verified in ip_wput_nondata */
10025 	mp1 = mp->b_cont->b_cont;
10026 
10027 	ar = (struct arpreq *)mp1->b_rptr;
10028 	sin = (sin_t *)&ar->arp_pa;
10029 
10030 	/*
10031 	 * We need to let ARP know on which interface the IP
10032 	 * address has an ARP mapping. In the IPMP case, a
10033 	 * simple forwarding table lookup will return the
10034 	 * IRE_IF_RESOLVER for the first interface in the group,
10035 	 * which might not be the interface on which the
10036 	 * requested IP address was resolved due to the ill
10037 	 * selection algorithm (see ip_newroute_get_dst_ill()).
10038 	 * So we do a cache table lookup first: if the IRE cache
10039 	 * entry for the IP address is still there, it will
10040 	 * contain the ill pointer for the right interface, so
10041 	 * we use that. If the cache entry has been flushed, we
10042 	 * fall back to the forwarding table lookup. This should
10043 	 * be rare enough since IRE cache entries have a longer
10044 	 * life expectancy than ARP cache entries.
10045 	 */
10046 	ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL, ipst);
10047 	if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
10048 	    ((ill = ire_to_ill(ire)) == NULL)) {
10049 		if (ire != NULL)
10050 			ire_refrele(ire);
10051 		ire = ire_ftable_lookup(sin->sin_addr.s_addr,
10052 		    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
10053 		    NULL, MATCH_IRE_TYPE, ipst);
10054 		if ((ire == NULL) || ((ill = ire_to_ill(ire)) == NULL)) {
10055 			if (ire != NULL)
10056 				ire_refrele(ire);
10057 			return (ENXIO);
10058 		}
10059 	}
10060 	ASSERT(ire != NULL && ill != NULL);
10061 
10062 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_FALSE, B_FALSE);
10063 	ire_refrele(ire);
10064 	return (err);
10065 }
10066 
10067 /*
10068  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
10069  * atomically set/clear the muxids. Also complete the ioctl by acking or
10070  * naking it.  Note that the code is structured such that the link type,
10071  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
10072  * its clones use the persistent link, while pppd(1M) and perhaps many
10073  * other daemons may use non-persistent link.  When combined with some
10074  * ill_t states, linking and unlinking lower streams may be used as
10075  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
10076  */
10077 /* ARGSUSED */
10078 void
10079 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
10080 {
10081 	mblk_t *mp1;
10082 	mblk_t *mp2;
10083 	struct linkblk *li;
10084 	queue_t	*ipwq;
10085 	char	*name;
10086 	struct qinit *qinfo;
10087 	struct ipmx_s *ipmxp;
10088 	ill_t	*ill = NULL;
10089 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10090 	int	err = 0;
10091 	boolean_t	entered_ipsq = B_FALSE;
10092 	boolean_t islink;
10093 	queue_t *dwq = NULL;
10094 	ip_stack_t	*ipst;
10095 
10096 	if (CONN_Q(q))
10097 		ipst = CONNQ_TO_IPST(q);
10098 	else
10099 		ipst = ILLQ_TO_IPST(q);
10100 
10101 	ASSERT(iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_PUNLINK ||
10102 	    iocp->ioc_cmd == I_LINK || iocp->ioc_cmd == I_UNLINK);
10103 
10104 	islink = (iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_LINK) ?
10105 	    B_TRUE : B_FALSE;
10106 
10107 	mp1 = mp->b_cont;	/* This is the linkblk info */
10108 	li = (struct linkblk *)mp1->b_rptr;
10109 
10110 	/*
10111 	 * ARP has added this special mblk, and the utility is asking us
10112 	 * to perform consistency checks, and also atomically set the
10113 	 * muxid. Ifconfig is an example.  It achieves this by using
10114 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
10115 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
10116 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
10117 	 * and other comments in this routine for more details.
10118 	 */
10119 	mp2 = mp1->b_cont;	/* This is added by ARP */
10120 
10121 	/*
10122 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
10123 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
10124 	 * get the special mblk above.  For backward compatibility, we just
10125 	 * return success.  The utility will use SIOCSLIFMUXID to store
10126 	 * the muxids.  This is not atomic, and can leave the streams
10127 	 * unplumbable if the utility is interrrupted, before it does the
10128 	 * SIOCSLIFMUXID.
10129 	 */
10130 	if (mp2 == NULL) {
10131 		/*
10132 		 * At this point we don't know whether or not this is the
10133 		 * IP module stream or the ARP device stream.  We need to
10134 		 * walk the lower stream in order to find this out, since
10135 		 * the capability negotiation is done only on the IP module
10136 		 * stream.  IP module instance is identified by the module
10137 		 * name IP, non-null q_next, and it's wput not being ip_lwput.
10138 		 * STREAMS ensures that the lower stream (l_qbot) will not
10139 		 * vanish until this ioctl completes. So we can safely walk
10140 		 * the stream or refer to the q_ptr.
10141 		 */
10142 		ipwq = li->l_qbot;
10143 		while (ipwq != NULL) {
10144 			qinfo = ipwq->q_qinfo;
10145 			name = qinfo->qi_minfo->mi_idname;
10146 			if (name != NULL && name[0] != NULL &&
10147 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
10148 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
10149 			    (ipwq->q_next != NULL)) {
10150 				break;
10151 			}
10152 			ipwq = ipwq->q_next;
10153 		}
10154 		/*
10155 		 * This looks like an IP module stream, so trigger
10156 		 * the capability reset or re-negotiation if necessary.
10157 		 */
10158 		if (ipwq != NULL) {
10159 			ill = ipwq->q_ptr;
10160 			ASSERT(ill != NULL);
10161 
10162 			if (ipsq == NULL) {
10163 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
10164 				    ip_sioctl_plink, NEW_OP, B_TRUE);
10165 				if (ipsq == NULL)
10166 					return;
10167 				entered_ipsq = B_TRUE;
10168 			}
10169 			ASSERT(IAM_WRITER_ILL(ill));
10170 			/*
10171 			 * Store the upper read queue of the module
10172 			 * immediately below IP, and count the total
10173 			 * number of lower modules.  Do this only
10174 			 * for I_PLINK or I_LINK event.
10175 			 */
10176 			ill->ill_lmod_rq = NULL;
10177 			ill->ill_lmod_cnt = 0;
10178 			if (islink && (dwq = ipwq->q_next) != NULL) {
10179 				ill->ill_lmod_rq = RD(dwq);
10180 
10181 				while (dwq != NULL) {
10182 					ill->ill_lmod_cnt++;
10183 					dwq = dwq->q_next;
10184 				}
10185 			}
10186 			/*
10187 			 * There's no point in resetting or re-negotiating if
10188 			 * we are not bound to the driver, so only do this if
10189 			 * the DLPI state is idle (up); we assume such state
10190 			 * since ill_ipif_up_count gets incremented in
10191 			 * ipif_up_done(), which is after we are bound to the
10192 			 * driver.  Note that in the case of logical
10193 			 * interfaces, IP won't rebind to the driver unless
10194 			 * the ill_ipif_up_count is 0, meaning that all other
10195 			 * IP interfaces (including the main ipif) are in the
10196 			 * down state.  Because of this, we use such counter
10197 			 * as an indicator, instead of relying on the IPIF_UP
10198 			 * flag, which is per ipif instance.
10199 			 */
10200 			if (ill->ill_ipif_up_count > 0) {
10201 				if (islink)
10202 					ill_capability_probe(ill);
10203 				else
10204 					ill_capability_reset(ill);
10205 			}
10206 		}
10207 		goto done;
10208 	}
10209 
10210 	/*
10211 	 * This is an I_{P}LINK sent down by ifconfig on
10212 	 * /dev/arp. ARP has appended this last (3rd) mblk,
10213 	 * giving more info. STREAMS ensures that the lower
10214 	 * stream (l_qbot) will not vanish until this ioctl
10215 	 * completes. So we can safely walk the stream or refer
10216 	 * to the q_ptr.
10217 	 */
10218 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
10219 	if (ipmxp->ipmx_arpdev_stream) {
10220 		/*
10221 		 * The operation is occuring on the arp-device
10222 		 * stream.
10223 		 */
10224 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
10225 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
10226 		if (ill == NULL) {
10227 			if (err == EINPROGRESS) {
10228 				return;
10229 			} else {
10230 				err = EINVAL;
10231 				goto done;
10232 			}
10233 		}
10234 
10235 		if (ipsq == NULL) {
10236 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
10237 			    NEW_OP, B_TRUE);
10238 			if (ipsq == NULL) {
10239 				ill_refrele(ill);
10240 				return;
10241 			}
10242 			entered_ipsq = B_TRUE;
10243 		}
10244 		ASSERT(IAM_WRITER_ILL(ill));
10245 		ill_refrele(ill);
10246 		/*
10247 		 * To ensure consistency between IP and ARP,
10248 		 * the following LIFO scheme is used in
10249 		 * plink/punlink. (IP first, ARP last).
10250 		 * This is because the muxid's are stored
10251 		 * in the IP stream on the ill.
10252 		 *
10253 		 * I_{P}LINK: ifconfig plinks the IP stream before
10254 		 * plinking the ARP stream. On an arp-dev
10255 		 * stream, IP checks that it is not yet
10256 		 * plinked, and it also checks that the
10257 		 * corresponding IP stream is already plinked.
10258 		 *
10259 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream
10260 		 * before punlinking the IP stream. IP does
10261 		 * not allow punlink of the IP stream unless
10262 		 * the arp stream has been punlinked.
10263 		 *
10264 		 */
10265 		if ((islink &&
10266 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
10267 		    (!islink &&
10268 		    ill->ill_arp_muxid != li->l_index)) {
10269 			err = EINVAL;
10270 			goto done;
10271 		}
10272 		if (islink) {
10273 			ill->ill_arp_muxid = li->l_index;
10274 		} else {
10275 			ill->ill_arp_muxid = 0;
10276 		}
10277 	} else {
10278 		/*
10279 		 * This must be the IP module stream with or
10280 		 * without arp. Walk the stream and locate the
10281 		 * IP module. An IP module instance is
10282 		 * identified by the module name IP, non-null
10283 		 * q_next, and it's wput not being ip_lwput.
10284 		 */
10285 		ipwq = li->l_qbot;
10286 		while (ipwq != NULL) {
10287 			qinfo = ipwq->q_qinfo;
10288 			name = qinfo->qi_minfo->mi_idname;
10289 			if (name != NULL && name[0] != NULL &&
10290 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
10291 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
10292 			    (ipwq->q_next != NULL)) {
10293 				break;
10294 			}
10295 			ipwq = ipwq->q_next;
10296 		}
10297 		if (ipwq != NULL) {
10298 			ill = ipwq->q_ptr;
10299 			ASSERT(ill != NULL);
10300 
10301 			if (ipsq == NULL) {
10302 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
10303 				    ip_sioctl_plink, NEW_OP, B_TRUE);
10304 				if (ipsq == NULL)
10305 					return;
10306 				entered_ipsq = B_TRUE;
10307 			}
10308 			ASSERT(IAM_WRITER_ILL(ill));
10309 			/*
10310 			 * Return error if the ip_mux_id is
10311 			 * non-zero and command is I_{P}LINK.
10312 			 * If command is I_{P}UNLINK, return
10313 			 * error if the arp-devstr is not
10314 			 * yet punlinked.
10315 			 */
10316 			if ((islink && ill->ill_ip_muxid != 0) ||
10317 			    (!islink && ill->ill_arp_muxid != 0)) {
10318 				err = EINVAL;
10319 				goto done;
10320 			}
10321 			ill->ill_lmod_rq = NULL;
10322 			ill->ill_lmod_cnt = 0;
10323 			if (islink) {
10324 				/*
10325 				 * Store the upper read queue of the module
10326 				 * immediately below IP, and count the total
10327 				 * number of lower modules.
10328 				 */
10329 				if ((dwq = ipwq->q_next) != NULL) {
10330 					ill->ill_lmod_rq = RD(dwq);
10331 
10332 					while (dwq != NULL) {
10333 						ill->ill_lmod_cnt++;
10334 						dwq = dwq->q_next;
10335 					}
10336 				}
10337 				ill->ill_ip_muxid = li->l_index;
10338 			} else {
10339 				ill->ill_ip_muxid = 0;
10340 			}
10341 
10342 			/*
10343 			 * See comments above about resetting/re-
10344 			 * negotiating driver sub-capabilities.
10345 			 */
10346 			if (ill->ill_ipif_up_count > 0) {
10347 				if (islink)
10348 					ill_capability_probe(ill);
10349 				else
10350 					ill_capability_reset(ill);
10351 			}
10352 		}
10353 	}
10354 done:
10355 	iocp->ioc_count = 0;
10356 	iocp->ioc_error = err;
10357 	if (err == 0)
10358 		mp->b_datap->db_type = M_IOCACK;
10359 	else
10360 		mp->b_datap->db_type = M_IOCNAK;
10361 	qreply(q, mp);
10362 
10363 	/* Conn was refheld in ip_sioctl_copyin_setup */
10364 	if (CONN_Q(q))
10365 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
10366 	if (entered_ipsq)
10367 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
10368 }
10369 
10370 /*
10371  * Search the ioctl command in the ioctl tables and return a pointer
10372  * to the ioctl command information. The ioctl command tables are
10373  * static and fully populated at compile time.
10374  */
10375 ip_ioctl_cmd_t *
10376 ip_sioctl_lookup(int ioc_cmd)
10377 {
10378 	int index;
10379 	ip_ioctl_cmd_t *ipip;
10380 	ip_ioctl_cmd_t *ipip_end;
10381 
10382 	if (ioc_cmd == IPI_DONTCARE)
10383 		return (NULL);
10384 
10385 	/*
10386 	 * Do a 2 step search. First search the indexed table
10387 	 * based on the least significant byte of the ioctl cmd.
10388 	 * If we don't find a match, then search the misc table
10389 	 * serially.
10390 	 */
10391 	index = ioc_cmd & 0xFF;
10392 	if (index < ip_ndx_ioctl_count) {
10393 		ipip = &ip_ndx_ioctl_table[index];
10394 		if (ipip->ipi_cmd == ioc_cmd) {
10395 			/* Found a match in the ndx table */
10396 			return (ipip);
10397 		}
10398 	}
10399 
10400 	/* Search the misc table */
10401 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10402 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10403 		if (ipip->ipi_cmd == ioc_cmd)
10404 			/* Found a match in the misc table */
10405 			return (ipip);
10406 	}
10407 
10408 	return (NULL);
10409 }
10410 
10411 /*
10412  * Wrapper function for resuming deferred ioctl processing
10413  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10414  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10415  */
10416 /* ARGSUSED */
10417 void
10418 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10419     void *dummy_arg)
10420 {
10421 	ip_sioctl_copyin_setup(q, mp);
10422 }
10423 
10424 /*
10425  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10426  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10427  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10428  * We establish here the size of the block to be copied in.  mi_copyin
10429  * arranges for this to happen, an processing continues in ip_wput with
10430  * an M_IOCDATA message.
10431  */
10432 void
10433 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10434 {
10435 	int	copyin_size;
10436 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10437 	ip_ioctl_cmd_t *ipip;
10438 	cred_t *cr;
10439 	ip_stack_t	*ipst;
10440 
10441 	if (CONN_Q(q))
10442 		ipst = CONNQ_TO_IPST(q);
10443 	else
10444 		ipst = ILLQ_TO_IPST(q);
10445 
10446 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10447 	if (ipip == NULL) {
10448 		/*
10449 		 * The ioctl is not one we understand or own.
10450 		 * Pass it along to be processed down stream,
10451 		 * if this is a module instance of IP, else nak
10452 		 * the ioctl.
10453 		 */
10454 		if (q->q_next == NULL) {
10455 			goto nak;
10456 		} else {
10457 			putnext(q, mp);
10458 			return;
10459 		}
10460 	}
10461 
10462 	/*
10463 	 * If this is deferred, then we will do all the checks when we
10464 	 * come back.
10465 	 */
10466 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10467 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10468 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10469 		return;
10470 	}
10471 
10472 	/*
10473 	 * Only allow a very small subset of IP ioctls on this stream if
10474 	 * IP is a module and not a driver. Allowing ioctls to be processed
10475 	 * in this case may cause assert failures or data corruption.
10476 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10477 	 * ioctls allowed on an IP module stream, after which this stream
10478 	 * normally becomes a multiplexor (at which time the stream head
10479 	 * will fail all ioctls).
10480 	 */
10481 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10482 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10483 			/*
10484 			 * Pass common Streams ioctls which the IP
10485 			 * module does not own or consume along to
10486 			 * be processed down stream.
10487 			 */
10488 			putnext(q, mp);
10489 			return;
10490 		} else {
10491 			goto nak;
10492 		}
10493 	}
10494 
10495 	/* Make sure we have ioctl data to process. */
10496 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10497 		goto nak;
10498 
10499 	/*
10500 	 * Prefer dblk credential over ioctl credential; some synthesized
10501 	 * ioctls have kcred set because there's no way to crhold()
10502 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10503 	 * the framework; the caller of ioctl needs to hold the reference
10504 	 * for the duration of the call).
10505 	 */
10506 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10507 
10508 	/* Make sure normal users don't send down privileged ioctls */
10509 	if ((ipip->ipi_flags & IPI_PRIV) &&
10510 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10511 		/* We checked the privilege earlier but log it here */
10512 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10513 		return;
10514 	}
10515 
10516 	/*
10517 	 * The ioctl command tables can only encode fixed length
10518 	 * ioctl data. If the length is variable, the table will
10519 	 * encode the length as zero. Such special cases are handled
10520 	 * below in the switch.
10521 	 */
10522 	if (ipip->ipi_copyin_size != 0) {
10523 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10524 		return;
10525 	}
10526 
10527 	switch (iocp->ioc_cmd) {
10528 	case O_SIOCGIFCONF:
10529 	case SIOCGIFCONF:
10530 		/*
10531 		 * This IOCTL is hilarious.  See comments in
10532 		 * ip_sioctl_get_ifconf for the story.
10533 		 */
10534 		if (iocp->ioc_count == TRANSPARENT)
10535 			copyin_size = SIZEOF_STRUCT(ifconf,
10536 			    iocp->ioc_flag);
10537 		else
10538 			copyin_size = iocp->ioc_count;
10539 		mi_copyin(q, mp, NULL, copyin_size);
10540 		return;
10541 
10542 	case O_SIOCGLIFCONF:
10543 	case SIOCGLIFCONF:
10544 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10545 		mi_copyin(q, mp, NULL, copyin_size);
10546 		return;
10547 
10548 	case SIOCGLIFSRCOF:
10549 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10550 		mi_copyin(q, mp, NULL, copyin_size);
10551 		return;
10552 	case SIOCGIP6ADDRPOLICY:
10553 		ip_sioctl_ip6addrpolicy(q, mp);
10554 		ip6_asp_table_refrele(ipst);
10555 		return;
10556 
10557 	case SIOCSIP6ADDRPOLICY:
10558 		ip_sioctl_ip6addrpolicy(q, mp);
10559 		return;
10560 
10561 	case SIOCGDSTINFO:
10562 		ip_sioctl_dstinfo(q, mp);
10563 		ip6_asp_table_refrele(ipst);
10564 		return;
10565 
10566 	case I_PLINK:
10567 	case I_PUNLINK:
10568 	case I_LINK:
10569 	case I_UNLINK:
10570 		/*
10571 		 * We treat non-persistent link similarly as the persistent
10572 		 * link case, in terms of plumbing/unplumbing, as well as
10573 		 * dynamic re-plumbing events indicator.  See comments
10574 		 * in ip_sioctl_plink() for more.
10575 		 *
10576 		 * Request can be enqueued in the 'ipsq' while waiting
10577 		 * to become exclusive. So bump up the conn ref.
10578 		 */
10579 		if (CONN_Q(q))
10580 			CONN_INC_REF(Q_TO_CONN(q));
10581 		ip_sioctl_plink(NULL, q, mp, NULL);
10582 		return;
10583 
10584 	case ND_GET:
10585 	case ND_SET:
10586 		/*
10587 		 * Use of the nd table requires holding the reader lock.
10588 		 * Modifying the nd table thru nd_load/nd_unload requires
10589 		 * the writer lock.
10590 		 */
10591 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10592 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10593 			rw_exit(&ipst->ips_ip_g_nd_lock);
10594 
10595 			if (iocp->ioc_error)
10596 				iocp->ioc_count = 0;
10597 			mp->b_datap->db_type = M_IOCACK;
10598 			qreply(q, mp);
10599 			return;
10600 		}
10601 		rw_exit(&ipst->ips_ip_g_nd_lock);
10602 		/*
10603 		 * We don't understand this subioctl of ND_GET / ND_SET.
10604 		 * Maybe intended for some driver / module below us
10605 		 */
10606 		if (q->q_next) {
10607 			putnext(q, mp);
10608 		} else {
10609 			iocp->ioc_error = ENOENT;
10610 			mp->b_datap->db_type = M_IOCNAK;
10611 			iocp->ioc_count = 0;
10612 			qreply(q, mp);
10613 		}
10614 		return;
10615 
10616 	case IP_IOCTL:
10617 		ip_wput_ioctl(q, mp);
10618 		return;
10619 	default:
10620 		cmn_err(CE_PANIC, "should not happen ");
10621 	}
10622 nak:
10623 	if (mp->b_cont != NULL) {
10624 		freemsg(mp->b_cont);
10625 		mp->b_cont = NULL;
10626 	}
10627 	iocp->ioc_error = EINVAL;
10628 	mp->b_datap->db_type = M_IOCNAK;
10629 	iocp->ioc_count = 0;
10630 	qreply(q, mp);
10631 }
10632 
10633 /* ip_wput hands off ARP IOCTL responses to us */
10634 void
10635 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10636 {
10637 	struct arpreq *ar;
10638 	struct xarpreq *xar;
10639 	area_t	*area;
10640 	mblk_t	*area_mp;
10641 	struct iocblk *iocp;
10642 	mblk_t	*orig_ioc_mp, *tmp;
10643 	struct iocblk	*orig_iocp;
10644 	ill_t *ill;
10645 	conn_t *connp = NULL;
10646 	uint_t ioc_id;
10647 	mblk_t *pending_mp;
10648 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10649 	int *flagsp;
10650 	char *storage = NULL;
10651 	sin_t *sin;
10652 	ipaddr_t addr;
10653 	int err;
10654 	ip_stack_t *ipst;
10655 
10656 	ill = q->q_ptr;
10657 	ASSERT(ill != NULL);
10658 	ipst = ill->ill_ipst;
10659 
10660 	/*
10661 	 * We should get back from ARP a packet chain that looks like:
10662 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10663 	 */
10664 	if (!(area_mp = mp->b_cont) ||
10665 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10666 	    !(orig_ioc_mp = area_mp->b_cont) ||
10667 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10668 		freemsg(mp);
10669 		return;
10670 	}
10671 
10672 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10673 
10674 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10675 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10676 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10677 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10678 		x_arp_ioctl = B_TRUE;
10679 		xar = (struct xarpreq *)tmp->b_rptr;
10680 		sin = (sin_t *)&xar->xarp_pa;
10681 		flagsp = &xar->xarp_flags;
10682 		storage = xar->xarp_ha.sdl_data;
10683 		if (xar->xarp_ha.sdl_nlen != 0)
10684 			ifx_arp_ioctl = B_TRUE;
10685 	} else {
10686 		ar = (struct arpreq *)tmp->b_rptr;
10687 		sin = (sin_t *)&ar->arp_pa;
10688 		flagsp = &ar->arp_flags;
10689 		storage = ar->arp_ha.sa_data;
10690 	}
10691 
10692 	iocp = (struct iocblk *)mp->b_rptr;
10693 
10694 	/*
10695 	 * Pick out the originating queue based on the ioc_id.
10696 	 */
10697 	ioc_id = iocp->ioc_id;
10698 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10699 	if (pending_mp == NULL) {
10700 		ASSERT(connp == NULL);
10701 		inet_freemsg(mp);
10702 		return;
10703 	}
10704 	ASSERT(connp != NULL);
10705 	q = CONNP_TO_WQ(connp);
10706 
10707 	/* Uncouple the internally generated IOCTL from the original one */
10708 	area = (area_t *)area_mp->b_rptr;
10709 	area_mp->b_cont = NULL;
10710 
10711 	/*
10712 	 * Restore the b_next and b_prev used by mi code. This is needed
10713 	 * to complete the ioctl using mi* functions. We stored them in
10714 	 * the pending mp prior to sending the request to ARP.
10715 	 */
10716 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10717 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10718 	inet_freemsg(pending_mp);
10719 
10720 	/*
10721 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10722 	 * Catch the case where there is an IRE_CACHE by no entry in the
10723 	 * arp table.
10724 	 */
10725 	addr = sin->sin_addr.s_addr;
10726 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10727 		ire_t			*ire;
10728 		dl_unitdata_req_t	*dlup;
10729 		mblk_t			*llmp;
10730 		int			addr_len;
10731 		ill_t			*ipsqill = NULL;
10732 
10733 		if (ifx_arp_ioctl) {
10734 			/*
10735 			 * There's no need to lookup the ill, since
10736 			 * we've already done that when we started
10737 			 * processing the ioctl and sent the message
10738 			 * to ARP on that ill.  So use the ill that
10739 			 * is stored in q->q_ptr.
10740 			 */
10741 			ipsqill = ill;
10742 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10743 			    ipsqill->ill_ipif, ALL_ZONES,
10744 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10745 		} else {
10746 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10747 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10748 			if (ire != NULL)
10749 				ipsqill = ire_to_ill(ire);
10750 		}
10751 
10752 		if ((x_arp_ioctl) && (ipsqill != NULL))
10753 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10754 
10755 		if (ire != NULL) {
10756 			/*
10757 			 * Since the ire obtained from cachetable is used for
10758 			 * mac addr copying below, treat an incomplete ire as if
10759 			 * as if we never found it.
10760 			 */
10761 			if (ire->ire_nce != NULL &&
10762 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10763 				ire_refrele(ire);
10764 				ire = NULL;
10765 				ipsqill = NULL;
10766 				goto errack;
10767 			}
10768 			*flagsp = ATF_INUSE;
10769 			llmp = (ire->ire_nce != NULL ?
10770 			    ire->ire_nce->nce_res_mp : NULL);
10771 			if (llmp != NULL && ipsqill != NULL) {
10772 				uchar_t *macaddr;
10773 
10774 				addr_len = ipsqill->ill_phys_addr_length;
10775 				if (x_arp_ioctl && ((addr_len +
10776 				    ipsqill->ill_name_length) >
10777 				    sizeof (xar->xarp_ha.sdl_data))) {
10778 					ire_refrele(ire);
10779 					freemsg(mp);
10780 					ip_ioctl_finish(q, orig_ioc_mp,
10781 					    EINVAL, NO_COPYOUT, NULL);
10782 					return;
10783 				}
10784 				*flagsp |= ATF_COM;
10785 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10786 				if (ipsqill->ill_sap_length < 0)
10787 					macaddr = llmp->b_rptr +
10788 					    dlup->dl_dest_addr_offset;
10789 				else
10790 					macaddr = llmp->b_rptr +
10791 					    dlup->dl_dest_addr_offset +
10792 					    ipsqill->ill_sap_length;
10793 				/*
10794 				 * For SIOCGARP, MAC address length
10795 				 * validation has already been done
10796 				 * before the ioctl was issued to ARP to
10797 				 * allow it to progress only on 6 byte
10798 				 * addressable (ethernet like) media. Thus
10799 				 * the mac address copying can not overwrite
10800 				 * the sa_data area below.
10801 				 */
10802 				bcopy(macaddr, storage, addr_len);
10803 			}
10804 			/* Ditch the internal IOCTL. */
10805 			freemsg(mp);
10806 			ire_refrele(ire);
10807 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10808 			return;
10809 		}
10810 	}
10811 
10812 	/*
10813 	 * Delete the coresponding IRE_CACHE if any.
10814 	 * Reset the error if there was one (in case there was no entry
10815 	 * in arp.)
10816 	 */
10817 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10818 		ipif_t *ipintf = NULL;
10819 
10820 		if (ifx_arp_ioctl) {
10821 			/*
10822 			 * There's no need to lookup the ill, since
10823 			 * we've already done that when we started
10824 			 * processing the ioctl and sent the message
10825 			 * to ARP on that ill.  So use the ill that
10826 			 * is stored in q->q_ptr.
10827 			 */
10828 			ipintf = ill->ill_ipif;
10829 		}
10830 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10831 			/*
10832 			 * The address in "addr" may be an entry for a
10833 			 * router. If that's true, then any off-net
10834 			 * IRE_CACHE entries that go through the router
10835 			 * with address "addr" must be clobbered. Use
10836 			 * ire_walk to achieve this goal.
10837 			 */
10838 			if (ifx_arp_ioctl)
10839 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10840 				    ire_delete_cache_gw, (char *)&addr, ill);
10841 			else
10842 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10843 				    ALL_ZONES, ipst);
10844 			iocp->ioc_error = 0;
10845 		}
10846 	}
10847 errack:
10848 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10849 		err = iocp->ioc_error;
10850 		freemsg(mp);
10851 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10852 		return;
10853 	}
10854 
10855 	/*
10856 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10857 	 * the area_t into the struct {x}arpreq.
10858 	 */
10859 	if (x_arp_ioctl) {
10860 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10861 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10862 		    sizeof (xar->xarp_ha.sdl_data)) {
10863 			freemsg(mp);
10864 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10865 			    NULL);
10866 			return;
10867 		}
10868 	}
10869 	*flagsp = ATF_INUSE;
10870 	if (area->area_flags & ACE_F_PERMANENT)
10871 		*flagsp |= ATF_PERM;
10872 	if (area->area_flags & ACE_F_PUBLISH)
10873 		*flagsp |= ATF_PUBL;
10874 	if (area->area_flags & ACE_F_AUTHORITY)
10875 		*flagsp |= ATF_AUTHORITY;
10876 	if (area->area_hw_addr_length != 0) {
10877 		*flagsp |= ATF_COM;
10878 		/*
10879 		 * For SIOCGARP, MAC address length validation has
10880 		 * already been done before the ioctl was issued to ARP
10881 		 * to allow it to progress only on 6 byte addressable
10882 		 * (ethernet like) media. Thus the mac address copying
10883 		 * can not overwrite the sa_data area below.
10884 		 */
10885 		bcopy((char *)area + area->area_hw_addr_offset,
10886 		    storage, area->area_hw_addr_length);
10887 	}
10888 
10889 	/* Ditch the internal IOCTL. */
10890 	freemsg(mp);
10891 	/* Complete the original. */
10892 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10893 }
10894 
10895 /*
10896  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10897  * interface) create the next available logical interface for this
10898  * physical interface.
10899  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10900  * ipif with the specified name.
10901  *
10902  * If the address family is not AF_UNSPEC then set the address as well.
10903  *
10904  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10905  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10906  *
10907  * Executed as a writer on the ill or ill group.
10908  * So no lock is needed to traverse the ipif chain, or examine the
10909  * phyint flags.
10910  */
10911 /* ARGSUSED */
10912 int
10913 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10914     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10915 {
10916 	mblk_t	*mp1;
10917 	struct lifreq *lifr;
10918 	boolean_t	isv6;
10919 	boolean_t	exists;
10920 	char 	*name;
10921 	char	*endp;
10922 	char	*cp;
10923 	int	namelen;
10924 	ipif_t	*ipif;
10925 	long	id;
10926 	ipsq_t	*ipsq;
10927 	ill_t	*ill;
10928 	sin_t	*sin;
10929 	int	err = 0;
10930 	boolean_t found_sep = B_FALSE;
10931 	conn_t	*connp;
10932 	zoneid_t zoneid;
10933 	int	orig_ifindex = 0;
10934 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10935 
10936 	ASSERT(q->q_next == NULL);
10937 	ip1dbg(("ip_sioctl_addif\n"));
10938 	/* Existence of mp1 has been checked in ip_wput_nondata */
10939 	mp1 = mp->b_cont->b_cont;
10940 	/*
10941 	 * Null terminate the string to protect against buffer
10942 	 * overrun. String was generated by user code and may not
10943 	 * be trusted.
10944 	 */
10945 	lifr = (struct lifreq *)mp1->b_rptr;
10946 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10947 	name = lifr->lifr_name;
10948 	ASSERT(CONN_Q(q));
10949 	connp = Q_TO_CONN(q);
10950 	isv6 = connp->conn_af_isv6;
10951 	zoneid = connp->conn_zoneid;
10952 	namelen = mi_strlen(name);
10953 	if (namelen == 0)
10954 		return (EINVAL);
10955 
10956 	exists = B_FALSE;
10957 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10958 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10959 		/*
10960 		 * Allow creating lo0 using SIOCLIFADDIF.
10961 		 * can't be any other writer thread. So can pass null below
10962 		 * for the last 4 args to ipif_lookup_name.
10963 		 */
10964 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10965 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10966 		/* Prevent any further action */
10967 		if (ipif == NULL) {
10968 			return (ENOBUFS);
10969 		} else if (!exists) {
10970 			/* We created the ipif now and as writer */
10971 			ipif_refrele(ipif);
10972 			return (0);
10973 		} else {
10974 			ill = ipif->ipif_ill;
10975 			ill_refhold(ill);
10976 			ipif_refrele(ipif);
10977 		}
10978 	} else {
10979 		/* Look for a colon in the name. */
10980 		endp = &name[namelen];
10981 		for (cp = endp; --cp > name; ) {
10982 			if (*cp == IPIF_SEPARATOR_CHAR) {
10983 				found_sep = B_TRUE;
10984 				/*
10985 				 * Reject any non-decimal aliases for plumbing
10986 				 * of logical interfaces. Aliases with leading
10987 				 * zeroes are also rejected as they introduce
10988 				 * ambiguity in the naming of the interfaces.
10989 				 * Comparing with "0" takes care of all such
10990 				 * cases.
10991 				 */
10992 				if ((strncmp("0", cp+1, 1)) == 0)
10993 					return (EINVAL);
10994 
10995 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10996 				    id <= 0 || *endp != '\0') {
10997 					return (EINVAL);
10998 				}
10999 				*cp = '\0';
11000 				break;
11001 			}
11002 		}
11003 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
11004 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
11005 		if (found_sep)
11006 			*cp = IPIF_SEPARATOR_CHAR;
11007 		if (ill == NULL)
11008 			return (err);
11009 	}
11010 
11011 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
11012 	    B_TRUE);
11013 
11014 	/*
11015 	 * Release the refhold due to the lookup, now that we are excl
11016 	 * or we are just returning
11017 	 */
11018 	ill_refrele(ill);
11019 
11020 	if (ipsq == NULL)
11021 		return (EINPROGRESS);
11022 
11023 	/*
11024 	 * If the interface is failed, inactive or offlined, look for a working
11025 	 * interface in the ill group and create the ipif there. If we can't
11026 	 * find a good interface, create the ipif anyway so that in.mpathd can
11027 	 * move it to the first repaired interface.
11028 	 */
11029 	if ((ill->ill_phyint->phyint_flags &
11030 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
11031 	    ill->ill_phyint->phyint_groupname_len != 0) {
11032 		phyint_t *phyi;
11033 		char *groupname = ill->ill_phyint->phyint_groupname;
11034 
11035 		/*
11036 		 * We're looking for a working interface, but it doesn't matter
11037 		 * if it's up or down; so instead of following the group lists,
11038 		 * we look at each physical interface and compare the groupname.
11039 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
11040 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
11041 		 * Otherwise we create the ipif on the failed interface.
11042 		 */
11043 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
11044 		phyi = avl_first(&ipst->ips_phyint_g_list->
11045 		    phyint_list_avl_by_index);
11046 		for (; phyi != NULL;
11047 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
11048 		    phyint_list_avl_by_index,
11049 		    phyi, AVL_AFTER)) {
11050 			if (phyi->phyint_groupname_len == 0)
11051 				continue;
11052 			ASSERT(phyi->phyint_groupname != NULL);
11053 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
11054 			    !(phyi->phyint_flags &
11055 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
11056 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
11057 			    (phyi->phyint_illv4 != NULL))) {
11058 				break;
11059 			}
11060 		}
11061 		rw_exit(&ipst->ips_ill_g_lock);
11062 
11063 		if (phyi != NULL) {
11064 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
11065 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
11066 			    phyi->phyint_illv4);
11067 		}
11068 	}
11069 
11070 	/*
11071 	 * We are now exclusive on the ipsq, so an ill move will be serialized
11072 	 * before or after us.
11073 	 */
11074 	ASSERT(IAM_WRITER_ILL(ill));
11075 	ASSERT(ill->ill_move_in_progress == B_FALSE);
11076 
11077 	if (found_sep && orig_ifindex == 0) {
11078 		/* Now see if there is an IPIF with this unit number. */
11079 		for (ipif = ill->ill_ipif; ipif != NULL;
11080 		    ipif = ipif->ipif_next) {
11081 			if (ipif->ipif_id == id) {
11082 				err = EEXIST;
11083 				goto done;
11084 			}
11085 		}
11086 	}
11087 
11088 	/*
11089 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
11090 	 * of lo0. We never come here when we plumb lo0:0. It
11091 	 * happens in ipif_lookup_on_name.
11092 	 * The specified unit number is ignored when we create the ipif on a
11093 	 * different interface. However, we save it in ipif_orig_ipifid below so
11094 	 * that the ipif fails back to the right position.
11095 	 */
11096 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
11097 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
11098 		err = ENOBUFS;
11099 		goto done;
11100 	}
11101 
11102 	/* Return created name with ioctl */
11103 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
11104 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
11105 	ip1dbg(("created %s\n", lifr->lifr_name));
11106 
11107 	/* Set address */
11108 	sin = (sin_t *)&lifr->lifr_addr;
11109 	if (sin->sin_family != AF_UNSPEC) {
11110 		err = ip_sioctl_addr(ipif, sin, q, mp,
11111 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
11112 	}
11113 
11114 	/* Set ifindex and unit number for failback */
11115 	if (err == 0 && orig_ifindex != 0) {
11116 		ipif->ipif_orig_ifindex = orig_ifindex;
11117 		if (found_sep) {
11118 			ipif->ipif_orig_ipifid = id;
11119 		}
11120 	}
11121 
11122 done:
11123 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
11124 	return (err);
11125 }
11126 
11127 /*
11128  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
11129  * interface) delete it based on the IP address (on this physical interface).
11130  * Otherwise delete it based on the ipif_id.
11131  * Also, special handling to allow a removeif of lo0.
11132  */
11133 /* ARGSUSED */
11134 int
11135 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11136     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11137 {
11138 	conn_t		*connp;
11139 	ill_t		*ill = ipif->ipif_ill;
11140 	boolean_t	 success;
11141 	ip_stack_t	*ipst;
11142 
11143 	ipst = CONNQ_TO_IPST(q);
11144 
11145 	ASSERT(q->q_next == NULL);
11146 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
11147 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11148 	ASSERT(IAM_WRITER_IPIF(ipif));
11149 
11150 	connp = Q_TO_CONN(q);
11151 	/*
11152 	 * Special case for unplumbing lo0 (the loopback physical interface).
11153 	 * If unplumbing lo0, the incoming address structure has been
11154 	 * initialized to all zeros. When unplumbing lo0, all its logical
11155 	 * interfaces must be removed too.
11156 	 *
11157 	 * Note that this interface may be called to remove a specific
11158 	 * loopback logical interface (eg, lo0:1). But in that case
11159 	 * ipif->ipif_id != 0 so that the code path for that case is the
11160 	 * same as any other interface (meaning it skips the code directly
11161 	 * below).
11162 	 */
11163 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11164 		if (sin->sin_family == AF_UNSPEC &&
11165 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
11166 			/*
11167 			 * Mark it condemned. No new ref. will be made to ill.
11168 			 */
11169 			mutex_enter(&ill->ill_lock);
11170 			ill->ill_state_flags |= ILL_CONDEMNED;
11171 			for (ipif = ill->ill_ipif; ipif != NULL;
11172 			    ipif = ipif->ipif_next) {
11173 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
11174 			}
11175 			mutex_exit(&ill->ill_lock);
11176 
11177 			ipif = ill->ill_ipif;
11178 			/* unplumb the loopback interface */
11179 			ill_delete(ill);
11180 			mutex_enter(&connp->conn_lock);
11181 			mutex_enter(&ill->ill_lock);
11182 			ASSERT(ill->ill_group == NULL);
11183 
11184 			/* Are any references to this ill active */
11185 			if (ill_is_quiescent(ill)) {
11186 				mutex_exit(&ill->ill_lock);
11187 				mutex_exit(&connp->conn_lock);
11188 				ill_delete_tail(ill);
11189 				mi_free(ill);
11190 				return (0);
11191 			}
11192 			success = ipsq_pending_mp_add(connp, ipif,
11193 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
11194 			mutex_exit(&connp->conn_lock);
11195 			mutex_exit(&ill->ill_lock);
11196 			if (success)
11197 				return (EINPROGRESS);
11198 			else
11199 				return (EINTR);
11200 		}
11201 	}
11202 
11203 	/*
11204 	 * We are exclusive on the ipsq, so an ill move will be serialized
11205 	 * before or after us.
11206 	 */
11207 	ASSERT(ill->ill_move_in_progress == B_FALSE);
11208 
11209 	if (ipif->ipif_id == 0) {
11210 		/* Find based on address */
11211 		if (ipif->ipif_isv6) {
11212 			sin6_t *sin6;
11213 
11214 			if (sin->sin_family != AF_INET6)
11215 				return (EAFNOSUPPORT);
11216 
11217 			sin6 = (sin6_t *)sin;
11218 			/* We are a writer, so we should be able to lookup */
11219 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
11220 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
11221 			if (ipif == NULL) {
11222 				/*
11223 				 * Maybe the address in on another interface in
11224 				 * the same IPMP group? We check this below.
11225 				 */
11226 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
11227 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
11228 				    ipst);
11229 			}
11230 		} else {
11231 			ipaddr_t addr;
11232 
11233 			if (sin->sin_family != AF_INET)
11234 				return (EAFNOSUPPORT);
11235 
11236 			addr = sin->sin_addr.s_addr;
11237 			/* We are a writer, so we should be able to lookup */
11238 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
11239 			    NULL, NULL, NULL, ipst);
11240 			if (ipif == NULL) {
11241 				/*
11242 				 * Maybe the address in on another interface in
11243 				 * the same IPMP group? We check this below.
11244 				 */
11245 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
11246 				    NULL, NULL, NULL, NULL, ipst);
11247 			}
11248 		}
11249 		if (ipif == NULL) {
11250 			return (EADDRNOTAVAIL);
11251 		}
11252 		/*
11253 		 * When the address to be removed is hosted on a different
11254 		 * interface, we check if the interface is in the same IPMP
11255 		 * group as the specified one; if so we proceed with the
11256 		 * removal.
11257 		 * ill->ill_group is NULL when the ill is down, so we have to
11258 		 * compare the group names instead.
11259 		 */
11260 		if (ipif->ipif_ill != ill &&
11261 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
11262 		    ill->ill_phyint->phyint_groupname_len == 0 ||
11263 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
11264 		    ill->ill_phyint->phyint_groupname) != 0)) {
11265 			ipif_refrele(ipif);
11266 			return (EADDRNOTAVAIL);
11267 		}
11268 
11269 		/* This is a writer */
11270 		ipif_refrele(ipif);
11271 	}
11272 
11273 	/*
11274 	 * Can not delete instance zero since it is tied to the ill.
11275 	 */
11276 	if (ipif->ipif_id == 0)
11277 		return (EBUSY);
11278 
11279 	mutex_enter(&ill->ill_lock);
11280 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
11281 	mutex_exit(&ill->ill_lock);
11282 
11283 	ipif_free(ipif);
11284 
11285 	mutex_enter(&connp->conn_lock);
11286 	mutex_enter(&ill->ill_lock);
11287 
11288 	/* Are any references to this ipif active */
11289 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
11290 		mutex_exit(&ill->ill_lock);
11291 		mutex_exit(&connp->conn_lock);
11292 		ipif_non_duplicate(ipif);
11293 		ipif_down_tail(ipif);
11294 		ipif_free_tail(ipif);
11295 		return (0);
11296 	}
11297 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
11298 	    IPIF_FREE);
11299 	mutex_exit(&ill->ill_lock);
11300 	mutex_exit(&connp->conn_lock);
11301 	if (success)
11302 		return (EINPROGRESS);
11303 	else
11304 		return (EINTR);
11305 }
11306 
11307 /*
11308  * Restart the removeif ioctl. The refcnt has gone down to 0.
11309  * The ipif is already condemned. So can't find it thru lookups.
11310  */
11311 /* ARGSUSED */
11312 int
11313 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
11314     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11315 {
11316 	ill_t *ill;
11317 
11318 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
11319 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11320 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11321 		ill = ipif->ipif_ill;
11322 		ASSERT(IAM_WRITER_ILL(ill));
11323 		ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) &&
11324 		    (ill->ill_state_flags & IPIF_CONDEMNED));
11325 		ill_delete_tail(ill);
11326 		mi_free(ill);
11327 		return (0);
11328 	}
11329 
11330 	ill = ipif->ipif_ill;
11331 	ASSERT(IAM_WRITER_IPIF(ipif));
11332 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
11333 
11334 	ipif_non_duplicate(ipif);
11335 	ipif_down_tail(ipif);
11336 	ipif_free_tail(ipif);
11337 
11338 	ILL_UNMARK_CHANGING(ill);
11339 	return (0);
11340 }
11341 
11342 /*
11343  * Set the local interface address.
11344  * Allow an address of all zero when the interface is down.
11345  */
11346 /* ARGSUSED */
11347 int
11348 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11349     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11350 {
11351 	int err = 0;
11352 	in6_addr_t v6addr;
11353 	boolean_t need_up = B_FALSE;
11354 
11355 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11356 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11357 
11358 	ASSERT(IAM_WRITER_IPIF(ipif));
11359 
11360 	if (ipif->ipif_isv6) {
11361 		sin6_t *sin6;
11362 		ill_t *ill;
11363 		phyint_t *phyi;
11364 
11365 		if (sin->sin_family != AF_INET6)
11366 			return (EAFNOSUPPORT);
11367 
11368 		sin6 = (sin6_t *)sin;
11369 		v6addr = sin6->sin6_addr;
11370 		ill = ipif->ipif_ill;
11371 		phyi = ill->ill_phyint;
11372 
11373 		/*
11374 		 * Enforce that true multicast interfaces have a link-local
11375 		 * address for logical unit 0.
11376 		 */
11377 		if (ipif->ipif_id == 0 &&
11378 		    (ill->ill_flags & ILLF_MULTICAST) &&
11379 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11380 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11381 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11382 			return (EADDRNOTAVAIL);
11383 		}
11384 
11385 		/*
11386 		 * up interfaces shouldn't have the unspecified address
11387 		 * unless they also have the IPIF_NOLOCAL flags set and
11388 		 * have a subnet assigned.
11389 		 */
11390 		if ((ipif->ipif_flags & IPIF_UP) &&
11391 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11392 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11393 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11394 			return (EADDRNOTAVAIL);
11395 		}
11396 
11397 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11398 			return (EADDRNOTAVAIL);
11399 	} else {
11400 		ipaddr_t addr;
11401 
11402 		if (sin->sin_family != AF_INET)
11403 			return (EAFNOSUPPORT);
11404 
11405 		addr = sin->sin_addr.s_addr;
11406 
11407 		/* Allow 0 as the local address. */
11408 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11409 			return (EADDRNOTAVAIL);
11410 
11411 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11412 	}
11413 
11414 
11415 	/*
11416 	 * Even if there is no change we redo things just to rerun
11417 	 * ipif_set_default.
11418 	 */
11419 	if (ipif->ipif_flags & IPIF_UP) {
11420 		/*
11421 		 * Setting a new local address, make sure
11422 		 * we have net and subnet bcast ire's for
11423 		 * the old address if we need them.
11424 		 */
11425 		if (!ipif->ipif_isv6)
11426 			ipif_check_bcast_ires(ipif);
11427 		/*
11428 		 * If the interface is already marked up,
11429 		 * we call ipif_down which will take care
11430 		 * of ditching any IREs that have been set
11431 		 * up based on the old interface address.
11432 		 */
11433 		err = ipif_logical_down(ipif, q, mp);
11434 		if (err == EINPROGRESS)
11435 			return (err);
11436 		ipif_down_tail(ipif);
11437 		need_up = 1;
11438 	}
11439 
11440 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11441 	return (err);
11442 }
11443 
11444 int
11445 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11446     boolean_t need_up)
11447 {
11448 	in6_addr_t v6addr;
11449 	in6_addr_t ov6addr;
11450 	ipaddr_t addr;
11451 	sin6_t	*sin6;
11452 	int	sinlen;
11453 	int	err = 0;
11454 	ill_t	*ill = ipif->ipif_ill;
11455 	boolean_t need_dl_down;
11456 	boolean_t need_arp_down;
11457 	struct iocblk *iocp;
11458 
11459 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11460 
11461 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11462 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11463 	ASSERT(IAM_WRITER_IPIF(ipif));
11464 
11465 	/* Must cancel any pending timer before taking the ill_lock */
11466 	if (ipif->ipif_recovery_id != 0)
11467 		(void) untimeout(ipif->ipif_recovery_id);
11468 	ipif->ipif_recovery_id = 0;
11469 
11470 	if (ipif->ipif_isv6) {
11471 		sin6 = (sin6_t *)sin;
11472 		v6addr = sin6->sin6_addr;
11473 		sinlen = sizeof (struct sockaddr_in6);
11474 	} else {
11475 		addr = sin->sin_addr.s_addr;
11476 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11477 		sinlen = sizeof (struct sockaddr_in);
11478 	}
11479 	mutex_enter(&ill->ill_lock);
11480 	ov6addr = ipif->ipif_v6lcl_addr;
11481 	ipif->ipif_v6lcl_addr = v6addr;
11482 	sctp_update_ipif_addr(ipif, ov6addr);
11483 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11484 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11485 	} else {
11486 		ipif->ipif_v6src_addr = v6addr;
11487 	}
11488 	ipif->ipif_addr_ready = 0;
11489 
11490 	/*
11491 	 * If the interface was previously marked as a duplicate, then since
11492 	 * we've now got a "new" address, it should no longer be considered a
11493 	 * duplicate -- even if the "new" address is the same as the old one.
11494 	 * Note that if all ipifs are down, we may have a pending ARP down
11495 	 * event to handle.  This is because we want to recover from duplicates
11496 	 * and thus delay tearing down ARP until the duplicates have been
11497 	 * removed or disabled.
11498 	 */
11499 	need_dl_down = need_arp_down = B_FALSE;
11500 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11501 		need_arp_down = !need_up;
11502 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11503 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11504 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11505 			need_dl_down = B_TRUE;
11506 		}
11507 	}
11508 
11509 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11510 	    !ill->ill_is_6to4tun) {
11511 		queue_t *wqp = ill->ill_wq;
11512 
11513 		/*
11514 		 * The local address of this interface is a 6to4 address,
11515 		 * check if this interface is in fact a 6to4 tunnel or just
11516 		 * an interface configured with a 6to4 address.  We are only
11517 		 * interested in the former.
11518 		 */
11519 		if (wqp != NULL) {
11520 			while ((wqp->q_next != NULL) &&
11521 			    (wqp->q_next->q_qinfo != NULL) &&
11522 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11523 
11524 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11525 				    == TUN6TO4_MODID) {
11526 					/* set for use in IP */
11527 					ill->ill_is_6to4tun = 1;
11528 					break;
11529 				}
11530 				wqp = wqp->q_next;
11531 			}
11532 		}
11533 	}
11534 
11535 	ipif_set_default(ipif);
11536 
11537 	/*
11538 	 * When publishing an interface address change event, we only notify
11539 	 * the event listeners of the new address.  It is assumed that if they
11540 	 * actively care about the addresses assigned that they will have
11541 	 * already discovered the previous address assigned (if there was one.)
11542 	 *
11543 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11544 	 */
11545 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11546 		hook_nic_event_t *info;
11547 		if ((info = ipif->ipif_ill->ill_nic_event_info) != NULL) {
11548 			ip2dbg(("ip_sioctl_addr_tail: unexpected nic event %d "
11549 			    "attached for %s\n", info->hne_event,
11550 			    ill->ill_name));
11551 			if (info->hne_data != NULL)
11552 				kmem_free(info->hne_data, info->hne_datalen);
11553 			kmem_free(info, sizeof (hook_nic_event_t));
11554 		}
11555 
11556 		info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
11557 		if (info != NULL) {
11558 			ip_stack_t	*ipst = ill->ill_ipst;
11559 
11560 			info->hne_nic =
11561 			    ipif->ipif_ill->ill_phyint->phyint_hook_ifindex;
11562 			info->hne_lif = MAP_IPIF_ID(ipif->ipif_id);
11563 			info->hne_event = NE_ADDRESS_CHANGE;
11564 			info->hne_family = ipif->ipif_isv6 ?
11565 			    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
11566 			info->hne_data = kmem_alloc(sinlen, KM_NOSLEEP);
11567 			if (info->hne_data != NULL) {
11568 				info->hne_datalen = sinlen;
11569 				bcopy(sin, info->hne_data, sinlen);
11570 			} else {
11571 				ip2dbg(("ip_sioctl_addr_tail: could not attach "
11572 				    "address information for ADDRESS_CHANGE nic"
11573 				    " event of %s (ENOMEM)\n",
11574 				    ipif->ipif_ill->ill_name));
11575 				kmem_free(info, sizeof (hook_nic_event_t));
11576 			}
11577 		} else
11578 			ip2dbg(("ip_sioctl_addr_tail: could not attach "
11579 			    "ADDRESS_CHANGE nic event information for %s "
11580 			    "(ENOMEM)\n", ipif->ipif_ill->ill_name));
11581 
11582 		ipif->ipif_ill->ill_nic_event_info = info;
11583 	}
11584 
11585 	mutex_exit(&ill->ill_lock);
11586 
11587 	if (need_up) {
11588 		/*
11589 		 * Now bring the interface back up.  If this
11590 		 * is the only IPIF for the ILL, ipif_up
11591 		 * will have to re-bind to the device, so
11592 		 * we may get back EINPROGRESS, in which
11593 		 * case, this IOCTL will get completed in
11594 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11595 		 */
11596 		err = ipif_up(ipif, q, mp);
11597 	}
11598 
11599 	if (need_dl_down)
11600 		ill_dl_down(ill);
11601 	if (need_arp_down)
11602 		ipif_arp_down(ipif);
11603 
11604 	return (err);
11605 }
11606 
11607 
11608 /*
11609  * Restart entry point to restart the address set operation after the
11610  * refcounts have dropped to zero.
11611  */
11612 /* ARGSUSED */
11613 int
11614 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11615     ip_ioctl_cmd_t *ipip, void *ifreq)
11616 {
11617 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11618 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11619 	ASSERT(IAM_WRITER_IPIF(ipif));
11620 	ipif_down_tail(ipif);
11621 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11622 }
11623 
11624 /* ARGSUSED */
11625 int
11626 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11627     ip_ioctl_cmd_t *ipip, void *if_req)
11628 {
11629 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11630 	struct lifreq *lifr = (struct lifreq *)if_req;
11631 
11632 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11633 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11634 	/*
11635 	 * The net mask and address can't change since we have a
11636 	 * reference to the ipif. So no lock is necessary.
11637 	 */
11638 	if (ipif->ipif_isv6) {
11639 		*sin6 = sin6_null;
11640 		sin6->sin6_family = AF_INET6;
11641 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11642 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11643 		lifr->lifr_addrlen =
11644 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11645 	} else {
11646 		*sin = sin_null;
11647 		sin->sin_family = AF_INET;
11648 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11649 		if (ipip->ipi_cmd_type == LIF_CMD) {
11650 			lifr->lifr_addrlen =
11651 			    ip_mask_to_plen(ipif->ipif_net_mask);
11652 		}
11653 	}
11654 	return (0);
11655 }
11656 
11657 /*
11658  * Set the destination address for a pt-pt interface.
11659  */
11660 /* ARGSUSED */
11661 int
11662 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11663     ip_ioctl_cmd_t *ipip, void *if_req)
11664 {
11665 	int err = 0;
11666 	in6_addr_t v6addr;
11667 	boolean_t need_up = B_FALSE;
11668 
11669 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11670 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11671 	ASSERT(IAM_WRITER_IPIF(ipif));
11672 
11673 	if (ipif->ipif_isv6) {
11674 		sin6_t *sin6;
11675 
11676 		if (sin->sin_family != AF_INET6)
11677 			return (EAFNOSUPPORT);
11678 
11679 		sin6 = (sin6_t *)sin;
11680 		v6addr = sin6->sin6_addr;
11681 
11682 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11683 			return (EADDRNOTAVAIL);
11684 	} else {
11685 		ipaddr_t addr;
11686 
11687 		if (sin->sin_family != AF_INET)
11688 			return (EAFNOSUPPORT);
11689 
11690 		addr = sin->sin_addr.s_addr;
11691 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11692 			return (EADDRNOTAVAIL);
11693 
11694 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11695 	}
11696 
11697 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11698 		return (0);	/* No change */
11699 
11700 	if (ipif->ipif_flags & IPIF_UP) {
11701 		/*
11702 		 * If the interface is already marked up,
11703 		 * we call ipif_down which will take care
11704 		 * of ditching any IREs that have been set
11705 		 * up based on the old pp dst address.
11706 		 */
11707 		err = ipif_logical_down(ipif, q, mp);
11708 		if (err == EINPROGRESS)
11709 			return (err);
11710 		ipif_down_tail(ipif);
11711 		need_up = B_TRUE;
11712 	}
11713 	/*
11714 	 * could return EINPROGRESS. If so ioctl will complete in
11715 	 * ip_rput_dlpi_writer
11716 	 */
11717 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11718 	return (err);
11719 }
11720 
11721 static int
11722 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11723     boolean_t need_up)
11724 {
11725 	in6_addr_t v6addr;
11726 	ill_t	*ill = ipif->ipif_ill;
11727 	int	err = 0;
11728 	boolean_t need_dl_down;
11729 	boolean_t need_arp_down;
11730 
11731 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11732 	    ipif->ipif_id, (void *)ipif));
11733 
11734 	/* Must cancel any pending timer before taking the ill_lock */
11735 	if (ipif->ipif_recovery_id != 0)
11736 		(void) untimeout(ipif->ipif_recovery_id);
11737 	ipif->ipif_recovery_id = 0;
11738 
11739 	if (ipif->ipif_isv6) {
11740 		sin6_t *sin6;
11741 
11742 		sin6 = (sin6_t *)sin;
11743 		v6addr = sin6->sin6_addr;
11744 	} else {
11745 		ipaddr_t addr;
11746 
11747 		addr = sin->sin_addr.s_addr;
11748 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11749 	}
11750 	mutex_enter(&ill->ill_lock);
11751 	/* Set point to point destination address. */
11752 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11753 		/*
11754 		 * Allow this as a means of creating logical
11755 		 * pt-pt interfaces on top of e.g. an Ethernet.
11756 		 * XXX Undocumented HACK for testing.
11757 		 * pt-pt interfaces are created with NUD disabled.
11758 		 */
11759 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11760 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11761 		if (ipif->ipif_isv6)
11762 			ill->ill_flags |= ILLF_NONUD;
11763 	}
11764 
11765 	/*
11766 	 * If the interface was previously marked as a duplicate, then since
11767 	 * we've now got a "new" address, it should no longer be considered a
11768 	 * duplicate -- even if the "new" address is the same as the old one.
11769 	 * Note that if all ipifs are down, we may have a pending ARP down
11770 	 * event to handle.
11771 	 */
11772 	need_dl_down = need_arp_down = B_FALSE;
11773 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11774 		need_arp_down = !need_up;
11775 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11776 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11777 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11778 			need_dl_down = B_TRUE;
11779 		}
11780 	}
11781 
11782 	/* Set the new address. */
11783 	ipif->ipif_v6pp_dst_addr = v6addr;
11784 	/* Make sure subnet tracks pp_dst */
11785 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11786 	mutex_exit(&ill->ill_lock);
11787 
11788 	if (need_up) {
11789 		/*
11790 		 * Now bring the interface back up.  If this
11791 		 * is the only IPIF for the ILL, ipif_up
11792 		 * will have to re-bind to the device, so
11793 		 * we may get back EINPROGRESS, in which
11794 		 * case, this IOCTL will get completed in
11795 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11796 		 */
11797 		err = ipif_up(ipif, q, mp);
11798 	}
11799 
11800 	if (need_dl_down)
11801 		ill_dl_down(ill);
11802 
11803 	if (need_arp_down)
11804 		ipif_arp_down(ipif);
11805 	return (err);
11806 }
11807 
11808 /*
11809  * Restart entry point to restart the dstaddress set operation after the
11810  * refcounts have dropped to zero.
11811  */
11812 /* ARGSUSED */
11813 int
11814 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11815     ip_ioctl_cmd_t *ipip, void *ifreq)
11816 {
11817 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11818 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11819 	ipif_down_tail(ipif);
11820 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11821 }
11822 
11823 /* ARGSUSED */
11824 int
11825 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11826     ip_ioctl_cmd_t *ipip, void *if_req)
11827 {
11828 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11829 
11830 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11831 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11832 	/*
11833 	 * Get point to point destination address. The addresses can't
11834 	 * change since we hold a reference to the ipif.
11835 	 */
11836 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11837 		return (EADDRNOTAVAIL);
11838 
11839 	if (ipif->ipif_isv6) {
11840 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11841 		*sin6 = sin6_null;
11842 		sin6->sin6_family = AF_INET6;
11843 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11844 	} else {
11845 		*sin = sin_null;
11846 		sin->sin_family = AF_INET;
11847 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11848 	}
11849 	return (0);
11850 }
11851 
11852 /*
11853  * part of ipmp, make this func return the active/inactive state and
11854  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11855  */
11856 /*
11857  * This function either sets or clears the IFF_INACTIVE flag.
11858  *
11859  * As long as there are some addresses or multicast memberships on the
11860  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11861  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11862  * will be used for outbound packets.
11863  *
11864  * Caller needs to verify the validity of setting IFF_INACTIVE.
11865  */
11866 static void
11867 phyint_inactive(phyint_t *phyi)
11868 {
11869 	ill_t *ill_v4;
11870 	ill_t *ill_v6;
11871 	ipif_t *ipif;
11872 	ilm_t *ilm;
11873 
11874 	ill_v4 = phyi->phyint_illv4;
11875 	ill_v6 = phyi->phyint_illv6;
11876 
11877 	/*
11878 	 * No need for a lock while traversing the list since iam
11879 	 * a writer
11880 	 */
11881 	if (ill_v4 != NULL) {
11882 		ASSERT(IAM_WRITER_ILL(ill_v4));
11883 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11884 		    ipif = ipif->ipif_next) {
11885 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11886 				mutex_enter(&phyi->phyint_lock);
11887 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11888 				mutex_exit(&phyi->phyint_lock);
11889 				return;
11890 			}
11891 		}
11892 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11893 		    ilm = ilm->ilm_next) {
11894 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11895 				mutex_enter(&phyi->phyint_lock);
11896 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11897 				mutex_exit(&phyi->phyint_lock);
11898 				return;
11899 			}
11900 		}
11901 	}
11902 	if (ill_v6 != NULL) {
11903 		ill_v6 = phyi->phyint_illv6;
11904 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11905 		    ipif = ipif->ipif_next) {
11906 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11907 				mutex_enter(&phyi->phyint_lock);
11908 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11909 				mutex_exit(&phyi->phyint_lock);
11910 				return;
11911 			}
11912 		}
11913 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11914 		    ilm = ilm->ilm_next) {
11915 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11916 				mutex_enter(&phyi->phyint_lock);
11917 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11918 				mutex_exit(&phyi->phyint_lock);
11919 				return;
11920 			}
11921 		}
11922 	}
11923 	mutex_enter(&phyi->phyint_lock);
11924 	phyi->phyint_flags |= PHYI_INACTIVE;
11925 	mutex_exit(&phyi->phyint_lock);
11926 }
11927 
11928 /*
11929  * This function is called only when the phyint flags change. Currently
11930  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11931  * that we can select a good ill.
11932  */
11933 static void
11934 ip_redo_nomination(phyint_t *phyi)
11935 {
11936 	ill_t *ill_v4;
11937 
11938 	ill_v4 = phyi->phyint_illv4;
11939 
11940 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11941 		ASSERT(IAM_WRITER_ILL(ill_v4));
11942 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11943 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11944 	}
11945 }
11946 
11947 /*
11948  * Heuristic to check if ill is INACTIVE.
11949  * Checks if ill has an ipif with an usable ip address.
11950  *
11951  * Return values:
11952  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11953  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11954  */
11955 static boolean_t
11956 ill_is_inactive(ill_t *ill)
11957 {
11958 	ipif_t *ipif;
11959 
11960 	/* Check whether it is in an IPMP group */
11961 	if (ill->ill_phyint->phyint_groupname == NULL)
11962 		return (B_FALSE);
11963 
11964 	if (ill->ill_ipif_up_count == 0)
11965 		return (B_TRUE);
11966 
11967 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11968 		uint64_t flags = ipif->ipif_flags;
11969 
11970 		/*
11971 		 * This ipif is usable if it is IPIF_UP and not a
11972 		 * dedicated test address.  A dedicated test address
11973 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11974 		 * (note in particular that V6 test addresses are
11975 		 * link-local data addresses and thus are marked
11976 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11977 		 */
11978 		if ((flags & IPIF_UP) &&
11979 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11980 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11981 			return (B_FALSE);
11982 	}
11983 	return (B_TRUE);
11984 }
11985 
11986 /*
11987  * Set interface flags.
11988  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11989  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11990  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11991  *
11992  * NOTE : We really don't enforce that ipif_id zero should be used
11993  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11994  *	  is because applications generally does SICGLIFFLAGS and
11995  *	  ORs in the new flags (that affects the logical) and does a
11996  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11997  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11998  *	  flags that will be turned on is correct with respect to
11999  *	  ipif_id 0. For backward compatibility reasons, it is not done.
12000  */
12001 /* ARGSUSED */
12002 int
12003 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12004     ip_ioctl_cmd_t *ipip, void *if_req)
12005 {
12006 	uint64_t turn_on;
12007 	uint64_t turn_off;
12008 	int	err;
12009 	boolean_t need_up = B_FALSE;
12010 	phyint_t *phyi;
12011 	ill_t *ill;
12012 	uint64_t intf_flags;
12013 	boolean_t phyint_flags_modified = B_FALSE;
12014 	uint64_t flags;
12015 	struct ifreq *ifr;
12016 	struct lifreq *lifr;
12017 	boolean_t set_linklocal = B_FALSE;
12018 	boolean_t zero_source = B_FALSE;
12019 	ip_stack_t *ipst;
12020 
12021 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
12022 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12023 
12024 	ASSERT(IAM_WRITER_IPIF(ipif));
12025 
12026 	ill = ipif->ipif_ill;
12027 	phyi = ill->ill_phyint;
12028 	ipst = ill->ill_ipst;
12029 
12030 	if (ipip->ipi_cmd_type == IF_CMD) {
12031 		ifr = (struct ifreq *)if_req;
12032 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
12033 	} else {
12034 		lifr = (struct lifreq *)if_req;
12035 		flags = lifr->lifr_flags;
12036 	}
12037 
12038 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12039 
12040 	/*
12041 	 * Has the flags been set correctly till now ?
12042 	 */
12043 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12044 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12045 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12046 	/*
12047 	 * Compare the new flags to the old, and partition
12048 	 * into those coming on and those going off.
12049 	 * For the 16 bit command keep the bits above bit 16 unchanged.
12050 	 */
12051 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
12052 		flags |= intf_flags & ~0xFFFF;
12053 
12054 	/*
12055 	 * First check which bits will change and then which will
12056 	 * go on and off
12057 	 */
12058 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
12059 	if (!turn_on)
12060 		return (0);	/* No change */
12061 
12062 	turn_off = intf_flags & turn_on;
12063 	turn_on ^= turn_off;
12064 	err = 0;
12065 
12066 	/*
12067 	 * Don't allow any bits belonging to the logical interface
12068 	 * to be set or cleared on the replacement ipif that was
12069 	 * created temporarily during a MOVE.
12070 	 */
12071 	if (ipif->ipif_replace_zero &&
12072 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
12073 		return (EINVAL);
12074 	}
12075 
12076 	/*
12077 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
12078 	 * IPv6 interfaces.
12079 	 */
12080 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
12081 		return (EINVAL);
12082 
12083 	/*
12084 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
12085 	 */
12086 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
12087 		return (EINVAL);
12088 
12089 	/*
12090 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
12091 	 * interfaces.  It makes no sense in that context.
12092 	 */
12093 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
12094 		return (EINVAL);
12095 
12096 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
12097 		zero_source = B_TRUE;
12098 
12099 	/*
12100 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
12101 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
12102 	 * If the link local address isn't set, and can be set, it will get
12103 	 * set later on in this function.
12104 	 */
12105 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
12106 	    (flags & IFF_UP) && !zero_source &&
12107 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
12108 		if (ipif_cant_setlinklocal(ipif))
12109 			return (EINVAL);
12110 		set_linklocal = B_TRUE;
12111 	}
12112 
12113 	/*
12114 	 * ILL cannot be part of a usesrc group and and IPMP group at the
12115 	 * same time. No need to grab ill_g_usesrc_lock here, see
12116 	 * synchronization notes in ip.c
12117 	 */
12118 	if (turn_on & PHYI_STANDBY &&
12119 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
12120 		return (EINVAL);
12121 	}
12122 
12123 	/*
12124 	 * If we modify physical interface flags, we'll potentially need to
12125 	 * send up two routing socket messages for the changes (one for the
12126 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
12127 	 */
12128 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
12129 		phyint_flags_modified = B_TRUE;
12130 
12131 	/*
12132 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
12133 	 * we need to flush the IRE_CACHES belonging to this ill.
12134 	 * We handle this case here without doing the DOWN/UP dance
12135 	 * like it is done for other flags. If some other flags are
12136 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
12137 	 * below will handle it by bringing it down and then
12138 	 * bringing it UP.
12139 	 */
12140 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
12141 		ill_t *ill_v4, *ill_v6;
12142 
12143 		ill_v4 = phyi->phyint_illv4;
12144 		ill_v6 = phyi->phyint_illv6;
12145 
12146 		/*
12147 		 * First set the INACTIVE flag if needed. Then delete the ires.
12148 		 * ire_add will atomically prevent creating new IRE_CACHEs
12149 		 * unless hidden flag is set.
12150 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
12151 		 */
12152 		if ((turn_on & PHYI_FAILED) &&
12153 		    ((intf_flags & PHYI_STANDBY) ||
12154 		    !ipst->ips_ipmp_enable_failback)) {
12155 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
12156 			phyi->phyint_flags &= ~PHYI_INACTIVE;
12157 		}
12158 		if ((turn_off & PHYI_FAILED) &&
12159 		    ((intf_flags & PHYI_STANDBY) ||
12160 		    (!ipst->ips_ipmp_enable_failback &&
12161 		    ill_is_inactive(ill)))) {
12162 			phyint_inactive(phyi);
12163 		}
12164 
12165 		if (turn_on & PHYI_STANDBY) {
12166 			/*
12167 			 * We implicitly set INACTIVE only when STANDBY is set.
12168 			 * INACTIVE is also set on non-STANDBY phyint when user
12169 			 * disables FAILBACK using configuration file.
12170 			 * Do not allow STANDBY to be set on such INACTIVE
12171 			 * phyint
12172 			 */
12173 			if (phyi->phyint_flags & PHYI_INACTIVE)
12174 				return (EINVAL);
12175 			if (!(phyi->phyint_flags & PHYI_FAILED))
12176 				phyint_inactive(phyi);
12177 		}
12178 		if (turn_off & PHYI_STANDBY) {
12179 			if (ipst->ips_ipmp_enable_failback) {
12180 				/*
12181 				 * Reset PHYI_INACTIVE.
12182 				 */
12183 				phyi->phyint_flags &= ~PHYI_INACTIVE;
12184 			} else if (ill_is_inactive(ill) &&
12185 			    !(phyi->phyint_flags & PHYI_FAILED)) {
12186 				/*
12187 				 * Need to set INACTIVE, when user sets
12188 				 * STANDBY on a non-STANDBY phyint and
12189 				 * later resets STANDBY
12190 				 */
12191 				phyint_inactive(phyi);
12192 			}
12193 		}
12194 		/*
12195 		 * We should always send up a message so that the
12196 		 * daemons come to know of it. Note that the zeroth
12197 		 * interface can be down and the check below for IPIF_UP
12198 		 * will not make sense as we are actually setting
12199 		 * a phyint flag here. We assume that the ipif used
12200 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
12201 		 * send up any message for non-zero ipifs).
12202 		 */
12203 		phyint_flags_modified = B_TRUE;
12204 
12205 		if (ill_v4 != NULL) {
12206 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
12207 			    IRE_CACHE, ill_stq_cache_delete,
12208 			    (char *)ill_v4, ill_v4);
12209 			illgrp_reset_schednext(ill_v4);
12210 		}
12211 		if (ill_v6 != NULL) {
12212 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
12213 			    IRE_CACHE, ill_stq_cache_delete,
12214 			    (char *)ill_v6, ill_v6);
12215 			illgrp_reset_schednext(ill_v6);
12216 		}
12217 	}
12218 
12219 	/*
12220 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
12221 	 * status of the interface and, if the interface is part of an IPMP
12222 	 * group, all other interfaces that are part of the same IPMP
12223 	 * group.
12224 	 */
12225 	if ((turn_on | turn_off) & ILLF_ROUTER)
12226 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
12227 
12228 	/*
12229 	 * If the interface is not UP and we are not going to
12230 	 * bring it UP, record the flags and return. When the
12231 	 * interface comes UP later, the right actions will be
12232 	 * taken.
12233 	 */
12234 	if (!(ipif->ipif_flags & IPIF_UP) &&
12235 	    !(turn_on & IPIF_UP)) {
12236 		/* Record new flags in their respective places. */
12237 		mutex_enter(&ill->ill_lock);
12238 		mutex_enter(&ill->ill_phyint->phyint_lock);
12239 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12240 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12241 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12242 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12243 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12244 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12245 		mutex_exit(&ill->ill_lock);
12246 		mutex_exit(&ill->ill_phyint->phyint_lock);
12247 
12248 		/*
12249 		 * We do the broadcast and nomination here rather
12250 		 * than waiting for a FAILOVER/FAILBACK to happen. In
12251 		 * the case of FAILBACK from INACTIVE standby to the
12252 		 * interface that has been repaired, PHYI_FAILED has not
12253 		 * been cleared yet. If there are only two interfaces in
12254 		 * that group, all we have is a FAILED and INACTIVE
12255 		 * interface. If we do the nomination soon after a failback,
12256 		 * the broadcast nomination code would select the
12257 		 * INACTIVE interface for receiving broadcasts as FAILED is
12258 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
12259 		 * receive broadcast packets, we need to redo nomination
12260 		 * when the FAILED is cleared here. Thus, in general we
12261 		 * always do the nomination here for FAILED, STANDBY
12262 		 * and OFFLINE.
12263 		 */
12264 		if (((turn_on | turn_off) &
12265 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
12266 			ip_redo_nomination(phyi);
12267 		}
12268 		if (phyint_flags_modified) {
12269 			if (phyi->phyint_illv4 != NULL) {
12270 				ip_rts_ifmsg(phyi->phyint_illv4->
12271 				    ill_ipif);
12272 			}
12273 			if (phyi->phyint_illv6 != NULL) {
12274 				ip_rts_ifmsg(phyi->phyint_illv6->
12275 				    ill_ipif);
12276 			}
12277 		}
12278 		return (0);
12279 	} else if (set_linklocal || zero_source) {
12280 		mutex_enter(&ill->ill_lock);
12281 		if (set_linklocal)
12282 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
12283 		if (zero_source)
12284 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
12285 		mutex_exit(&ill->ill_lock);
12286 	}
12287 
12288 	/*
12289 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
12290 	 * or point-to-point interfaces with an unspecified destination. We do
12291 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
12292 	 * have a subnet assigned, which is how in.ndpd currently manages its
12293 	 * onlink prefix list when no addresses are configured with those
12294 	 * prefixes.
12295 	 */
12296 	if (ipif->ipif_isv6 &&
12297 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
12298 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
12299 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
12300 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12301 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
12302 		return (EINVAL);
12303 	}
12304 
12305 	/*
12306 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
12307 	 * from being brought up.
12308 	 */
12309 	if (!ipif->ipif_isv6 &&
12310 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12311 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
12312 		return (EINVAL);
12313 	}
12314 
12315 	/*
12316 	 * The only flag changes that we currently take specific action on
12317 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
12318 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
12319 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
12320 	 * the flags and bringing it back up again.
12321 	 */
12322 	if ((turn_on|turn_off) &
12323 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
12324 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
12325 		/*
12326 		 * Taking this ipif down, make sure we have
12327 		 * valid net and subnet bcast ire's for other
12328 		 * logical interfaces, if we need them.
12329 		 */
12330 		if (!ipif->ipif_isv6)
12331 			ipif_check_bcast_ires(ipif);
12332 
12333 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
12334 		    !(turn_off & IPIF_UP)) {
12335 			need_up = B_TRUE;
12336 			if (ipif->ipif_flags & IPIF_UP)
12337 				ill->ill_logical_down = 1;
12338 			turn_on &= ~IPIF_UP;
12339 		}
12340 		err = ipif_down(ipif, q, mp);
12341 		ip1dbg(("ipif_down returns %d err ", err));
12342 		if (err == EINPROGRESS)
12343 			return (err);
12344 		ipif_down_tail(ipif);
12345 	}
12346 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
12347 }
12348 
12349 static int
12350 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
12351     boolean_t need_up)
12352 {
12353 	ill_t	*ill;
12354 	phyint_t *phyi;
12355 	uint64_t turn_on;
12356 	uint64_t turn_off;
12357 	uint64_t intf_flags;
12358 	boolean_t phyint_flags_modified = B_FALSE;
12359 	int	err = 0;
12360 	boolean_t set_linklocal = B_FALSE;
12361 	boolean_t zero_source = B_FALSE;
12362 
12363 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12364 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12365 
12366 	ASSERT(IAM_WRITER_IPIF(ipif));
12367 
12368 	ill = ipif->ipif_ill;
12369 	phyi = ill->ill_phyint;
12370 
12371 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12372 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12373 
12374 	turn_off = intf_flags & turn_on;
12375 	turn_on ^= turn_off;
12376 
12377 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12378 		phyint_flags_modified = B_TRUE;
12379 
12380 	/*
12381 	 * Now we change the flags. Track current value of
12382 	 * other flags in their respective places.
12383 	 */
12384 	mutex_enter(&ill->ill_lock);
12385 	mutex_enter(&phyi->phyint_lock);
12386 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12387 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12388 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12389 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12390 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12391 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12392 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12393 		set_linklocal = B_TRUE;
12394 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12395 	}
12396 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12397 		zero_source = B_TRUE;
12398 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12399 	}
12400 	mutex_exit(&ill->ill_lock);
12401 	mutex_exit(&phyi->phyint_lock);
12402 
12403 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12404 		ip_redo_nomination(phyi);
12405 
12406 	if (set_linklocal)
12407 		(void) ipif_setlinklocal(ipif);
12408 
12409 	if (zero_source)
12410 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12411 	else
12412 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12413 
12414 	if (need_up) {
12415 		/*
12416 		 * XXX ipif_up really does not know whether a phyint flags
12417 		 * was modified or not. So, it sends up information on
12418 		 * only one routing sockets message. As we don't bring up
12419 		 * the interface and also set STANDBY/FAILED simultaneously
12420 		 * it should be okay.
12421 		 */
12422 		err = ipif_up(ipif, q, mp);
12423 	} else {
12424 		/*
12425 		 * Make sure routing socket sees all changes to the flags.
12426 		 * ipif_up_done* handles this when we use ipif_up.
12427 		 */
12428 		if (phyint_flags_modified) {
12429 			if (phyi->phyint_illv4 != NULL) {
12430 				ip_rts_ifmsg(phyi->phyint_illv4->
12431 				    ill_ipif);
12432 			}
12433 			if (phyi->phyint_illv6 != NULL) {
12434 				ip_rts_ifmsg(phyi->phyint_illv6->
12435 				    ill_ipif);
12436 			}
12437 		} else {
12438 			ip_rts_ifmsg(ipif);
12439 		}
12440 		/*
12441 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12442 		 * this in need_up case.
12443 		 */
12444 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12445 	}
12446 	return (err);
12447 }
12448 
12449 /*
12450  * Restart entry point to restart the flags restart operation after the
12451  * refcounts have dropped to zero.
12452  */
12453 /* ARGSUSED */
12454 int
12455 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12456     ip_ioctl_cmd_t *ipip, void *if_req)
12457 {
12458 	int	err;
12459 	struct ifreq *ifr = (struct ifreq *)if_req;
12460 	struct lifreq *lifr = (struct lifreq *)if_req;
12461 
12462 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12463 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12464 
12465 	ipif_down_tail(ipif);
12466 	if (ipip->ipi_cmd_type == IF_CMD) {
12467 		/*
12468 		 * Since ip_sioctl_flags expects an int and ifr_flags
12469 		 * is a short we need to cast ifr_flags into an int
12470 		 * to avoid having sign extension cause bits to get
12471 		 * set that should not be.
12472 		 */
12473 		err = ip_sioctl_flags_tail(ipif,
12474 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
12475 		    q, mp, B_TRUE);
12476 	} else {
12477 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
12478 		    q, mp, B_TRUE);
12479 	}
12480 	return (err);
12481 }
12482 
12483 /*
12484  * Can operate on either a module or a driver queue.
12485  */
12486 /* ARGSUSED */
12487 int
12488 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12489     ip_ioctl_cmd_t *ipip, void *if_req)
12490 {
12491 	/*
12492 	 * Has the flags been set correctly till now ?
12493 	 */
12494 	ill_t *ill = ipif->ipif_ill;
12495 	phyint_t *phyi = ill->ill_phyint;
12496 
12497 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12498 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12499 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12500 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12501 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12502 
12503 	/*
12504 	 * Need a lock since some flags can be set even when there are
12505 	 * references to the ipif.
12506 	 */
12507 	mutex_enter(&ill->ill_lock);
12508 	if (ipip->ipi_cmd_type == IF_CMD) {
12509 		struct ifreq *ifr = (struct ifreq *)if_req;
12510 
12511 		/* Get interface flags (low 16 only). */
12512 		ifr->ifr_flags = ((ipif->ipif_flags |
12513 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12514 	} else {
12515 		struct lifreq *lifr = (struct lifreq *)if_req;
12516 
12517 		/* Get interface flags. */
12518 		lifr->lifr_flags = ipif->ipif_flags |
12519 		    ill->ill_flags | phyi->phyint_flags;
12520 	}
12521 	mutex_exit(&ill->ill_lock);
12522 	return (0);
12523 }
12524 
12525 /* ARGSUSED */
12526 int
12527 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12528     ip_ioctl_cmd_t *ipip, void *if_req)
12529 {
12530 	int mtu;
12531 	int ip_min_mtu;
12532 	struct ifreq	*ifr;
12533 	struct lifreq *lifr;
12534 	ire_t	*ire;
12535 	ip_stack_t *ipst;
12536 
12537 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12538 	    ipif->ipif_id, (void *)ipif));
12539 	if (ipip->ipi_cmd_type == IF_CMD) {
12540 		ifr = (struct ifreq *)if_req;
12541 		mtu = ifr->ifr_metric;
12542 	} else {
12543 		lifr = (struct lifreq *)if_req;
12544 		mtu = lifr->lifr_mtu;
12545 	}
12546 
12547 	if (ipif->ipif_isv6)
12548 		ip_min_mtu = IPV6_MIN_MTU;
12549 	else
12550 		ip_min_mtu = IP_MIN_MTU;
12551 
12552 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12553 		return (EINVAL);
12554 
12555 	/*
12556 	 * Change the MTU size in all relevant ire's.
12557 	 * Mtu change Vs. new ire creation - protocol below.
12558 	 * First change ipif_mtu and the ire_max_frag of the
12559 	 * interface ire. Then do an ire walk and change the
12560 	 * ire_max_frag of all affected ires. During ire_add
12561 	 * under the bucket lock, set the ire_max_frag of the
12562 	 * new ire being created from the ipif/ire from which
12563 	 * it is being derived. If an mtu change happens after
12564 	 * the ire is added, the new ire will be cleaned up.
12565 	 * Conversely if the mtu change happens before the ire
12566 	 * is added, ire_add will see the new value of the mtu.
12567 	 */
12568 	ipif->ipif_mtu = mtu;
12569 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12570 
12571 	if (ipif->ipif_isv6)
12572 		ire = ipif_to_ire_v6(ipif);
12573 	else
12574 		ire = ipif_to_ire(ipif);
12575 	if (ire != NULL) {
12576 		ire->ire_max_frag = ipif->ipif_mtu;
12577 		ire_refrele(ire);
12578 	}
12579 	ipst = ipif->ipif_ill->ill_ipst;
12580 	if (ipif->ipif_flags & IPIF_UP) {
12581 		if (ipif->ipif_isv6)
12582 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12583 			    ipst);
12584 		else
12585 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12586 			    ipst);
12587 	}
12588 	/* Update the MTU in SCTP's list */
12589 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12590 	return (0);
12591 }
12592 
12593 /* Get interface MTU. */
12594 /* ARGSUSED */
12595 int
12596 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12597 	ip_ioctl_cmd_t *ipip, void *if_req)
12598 {
12599 	struct ifreq	*ifr;
12600 	struct lifreq	*lifr;
12601 
12602 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12603 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12604 	if (ipip->ipi_cmd_type == IF_CMD) {
12605 		ifr = (struct ifreq *)if_req;
12606 		ifr->ifr_metric = ipif->ipif_mtu;
12607 	} else {
12608 		lifr = (struct lifreq *)if_req;
12609 		lifr->lifr_mtu = ipif->ipif_mtu;
12610 	}
12611 	return (0);
12612 }
12613 
12614 /* Set interface broadcast address. */
12615 /* ARGSUSED2 */
12616 int
12617 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12618 	ip_ioctl_cmd_t *ipip, void *if_req)
12619 {
12620 	ipaddr_t addr;
12621 	ire_t	*ire;
12622 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12623 
12624 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12625 	    ipif->ipif_id));
12626 
12627 	ASSERT(IAM_WRITER_IPIF(ipif));
12628 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12629 		return (EADDRNOTAVAIL);
12630 
12631 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12632 
12633 	if (sin->sin_family != AF_INET)
12634 		return (EAFNOSUPPORT);
12635 
12636 	addr = sin->sin_addr.s_addr;
12637 	if (ipif->ipif_flags & IPIF_UP) {
12638 		/*
12639 		 * If we are already up, make sure the new
12640 		 * broadcast address makes sense.  If it does,
12641 		 * there should be an IRE for it already.
12642 		 * Don't match on ipif, only on the ill
12643 		 * since we are sharing these now. Don't use
12644 		 * MATCH_IRE_ILL_GROUP as we are looking for
12645 		 * the broadcast ire on this ill and each ill
12646 		 * in the group has its own broadcast ire.
12647 		 */
12648 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12649 		    ipif, ALL_ZONES, NULL,
12650 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12651 		if (ire == NULL) {
12652 			return (EINVAL);
12653 		} else {
12654 			ire_refrele(ire);
12655 		}
12656 	}
12657 	/*
12658 	 * Changing the broadcast addr for this ipif.
12659 	 * Make sure we have valid net and subnet bcast
12660 	 * ire's for other logical interfaces, if needed.
12661 	 */
12662 	if (addr != ipif->ipif_brd_addr)
12663 		ipif_check_bcast_ires(ipif);
12664 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12665 	return (0);
12666 }
12667 
12668 /* Get interface broadcast address. */
12669 /* ARGSUSED */
12670 int
12671 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12672     ip_ioctl_cmd_t *ipip, void *if_req)
12673 {
12674 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12675 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12676 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12677 		return (EADDRNOTAVAIL);
12678 
12679 	/* IPIF_BROADCAST not possible with IPv6 */
12680 	ASSERT(!ipif->ipif_isv6);
12681 	*sin = sin_null;
12682 	sin->sin_family = AF_INET;
12683 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12684 	return (0);
12685 }
12686 
12687 /*
12688  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12689  */
12690 /* ARGSUSED */
12691 int
12692 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12693     ip_ioctl_cmd_t *ipip, void *if_req)
12694 {
12695 	int err = 0;
12696 	in6_addr_t v6mask;
12697 
12698 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12699 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12700 
12701 	ASSERT(IAM_WRITER_IPIF(ipif));
12702 
12703 	if (ipif->ipif_isv6) {
12704 		sin6_t *sin6;
12705 
12706 		if (sin->sin_family != AF_INET6)
12707 			return (EAFNOSUPPORT);
12708 
12709 		sin6 = (sin6_t *)sin;
12710 		v6mask = sin6->sin6_addr;
12711 	} else {
12712 		ipaddr_t mask;
12713 
12714 		if (sin->sin_family != AF_INET)
12715 			return (EAFNOSUPPORT);
12716 
12717 		mask = sin->sin_addr.s_addr;
12718 		V4MASK_TO_V6(mask, v6mask);
12719 	}
12720 
12721 	/*
12722 	 * No big deal if the interface isn't already up, or the mask
12723 	 * isn't really changing, or this is pt-pt.
12724 	 */
12725 	if (!(ipif->ipif_flags & IPIF_UP) ||
12726 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12727 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12728 		ipif->ipif_v6net_mask = v6mask;
12729 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12730 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12731 			    ipif->ipif_v6net_mask,
12732 			    ipif->ipif_v6subnet);
12733 		}
12734 		return (0);
12735 	}
12736 	/*
12737 	 * Make sure we have valid net and subnet broadcast ire's
12738 	 * for the old netmask, if needed by other logical interfaces.
12739 	 */
12740 	if (!ipif->ipif_isv6)
12741 		ipif_check_bcast_ires(ipif);
12742 
12743 	err = ipif_logical_down(ipif, q, mp);
12744 	if (err == EINPROGRESS)
12745 		return (err);
12746 	ipif_down_tail(ipif);
12747 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12748 	return (err);
12749 }
12750 
12751 static int
12752 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12753 {
12754 	in6_addr_t v6mask;
12755 	int err = 0;
12756 
12757 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12758 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12759 
12760 	if (ipif->ipif_isv6) {
12761 		sin6_t *sin6;
12762 
12763 		sin6 = (sin6_t *)sin;
12764 		v6mask = sin6->sin6_addr;
12765 	} else {
12766 		ipaddr_t mask;
12767 
12768 		mask = sin->sin_addr.s_addr;
12769 		V4MASK_TO_V6(mask, v6mask);
12770 	}
12771 
12772 	ipif->ipif_v6net_mask = v6mask;
12773 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12774 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12775 		    ipif->ipif_v6subnet);
12776 	}
12777 	err = ipif_up(ipif, q, mp);
12778 
12779 	if (err == 0 || err == EINPROGRESS) {
12780 		/*
12781 		 * The interface must be DL_BOUND if this packet has to
12782 		 * go out on the wire. Since we only go through a logical
12783 		 * down and are bound with the driver during an internal
12784 		 * down/up that is satisfied.
12785 		 */
12786 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12787 			/* Potentially broadcast an address mask reply. */
12788 			ipif_mask_reply(ipif);
12789 		}
12790 	}
12791 	return (err);
12792 }
12793 
12794 /* ARGSUSED */
12795 int
12796 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12797     ip_ioctl_cmd_t *ipip, void *if_req)
12798 {
12799 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12800 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12801 	ipif_down_tail(ipif);
12802 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12803 }
12804 
12805 /* Get interface net mask. */
12806 /* ARGSUSED */
12807 int
12808 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12809     ip_ioctl_cmd_t *ipip, void *if_req)
12810 {
12811 	struct lifreq *lifr = (struct lifreq *)if_req;
12812 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12813 
12814 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12815 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12816 
12817 	/*
12818 	 * net mask can't change since we have a reference to the ipif.
12819 	 */
12820 	if (ipif->ipif_isv6) {
12821 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12822 		*sin6 = sin6_null;
12823 		sin6->sin6_family = AF_INET6;
12824 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12825 		lifr->lifr_addrlen =
12826 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12827 	} else {
12828 		*sin = sin_null;
12829 		sin->sin_family = AF_INET;
12830 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12831 		if (ipip->ipi_cmd_type == LIF_CMD) {
12832 			lifr->lifr_addrlen =
12833 			    ip_mask_to_plen(ipif->ipif_net_mask);
12834 		}
12835 	}
12836 	return (0);
12837 }
12838 
12839 /* ARGSUSED */
12840 int
12841 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12842     ip_ioctl_cmd_t *ipip, void *if_req)
12843 {
12844 
12845 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12846 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12847 	/*
12848 	 * Set interface metric.  We don't use this for
12849 	 * anything but we keep track of it in case it is
12850 	 * important to routing applications or such.
12851 	 */
12852 	if (ipip->ipi_cmd_type == IF_CMD) {
12853 		struct ifreq    *ifr;
12854 
12855 		ifr = (struct ifreq *)if_req;
12856 		ipif->ipif_metric = ifr->ifr_metric;
12857 	} else {
12858 		struct lifreq   *lifr;
12859 
12860 		lifr = (struct lifreq *)if_req;
12861 		ipif->ipif_metric = lifr->lifr_metric;
12862 	}
12863 	return (0);
12864 }
12865 
12866 
12867 /* ARGSUSED */
12868 int
12869 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12870     ip_ioctl_cmd_t *ipip, void *if_req)
12871 {
12872 
12873 	/* Get interface metric. */
12874 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12875 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12876 	if (ipip->ipi_cmd_type == IF_CMD) {
12877 		struct ifreq    *ifr;
12878 
12879 		ifr = (struct ifreq *)if_req;
12880 		ifr->ifr_metric = ipif->ipif_metric;
12881 	} else {
12882 		struct lifreq   *lifr;
12883 
12884 		lifr = (struct lifreq *)if_req;
12885 		lifr->lifr_metric = ipif->ipif_metric;
12886 	}
12887 
12888 	return (0);
12889 }
12890 
12891 /* ARGSUSED */
12892 int
12893 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12894     ip_ioctl_cmd_t *ipip, void *if_req)
12895 {
12896 
12897 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12898 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12899 	/*
12900 	 * Set the muxid returned from I_PLINK.
12901 	 */
12902 	if (ipip->ipi_cmd_type == IF_CMD) {
12903 		struct ifreq *ifr = (struct ifreq *)if_req;
12904 
12905 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12906 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12907 	} else {
12908 		struct lifreq *lifr = (struct lifreq *)if_req;
12909 
12910 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12911 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12912 	}
12913 	return (0);
12914 }
12915 
12916 /* ARGSUSED */
12917 int
12918 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12919     ip_ioctl_cmd_t *ipip, void *if_req)
12920 {
12921 
12922 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12923 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12924 	/*
12925 	 * Get the muxid saved in ill for I_PUNLINK.
12926 	 */
12927 	if (ipip->ipi_cmd_type == IF_CMD) {
12928 		struct ifreq *ifr = (struct ifreq *)if_req;
12929 
12930 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12931 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12932 	} else {
12933 		struct lifreq *lifr = (struct lifreq *)if_req;
12934 
12935 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12936 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12937 	}
12938 	return (0);
12939 }
12940 
12941 /*
12942  * Set the subnet prefix. Does not modify the broadcast address.
12943  */
12944 /* ARGSUSED */
12945 int
12946 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12947     ip_ioctl_cmd_t *ipip, void *if_req)
12948 {
12949 	int err = 0;
12950 	in6_addr_t v6addr;
12951 	in6_addr_t v6mask;
12952 	boolean_t need_up = B_FALSE;
12953 	int addrlen;
12954 
12955 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12956 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12957 
12958 	ASSERT(IAM_WRITER_IPIF(ipif));
12959 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12960 
12961 	if (ipif->ipif_isv6) {
12962 		sin6_t *sin6;
12963 
12964 		if (sin->sin_family != AF_INET6)
12965 			return (EAFNOSUPPORT);
12966 
12967 		sin6 = (sin6_t *)sin;
12968 		v6addr = sin6->sin6_addr;
12969 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12970 			return (EADDRNOTAVAIL);
12971 	} else {
12972 		ipaddr_t addr;
12973 
12974 		if (sin->sin_family != AF_INET)
12975 			return (EAFNOSUPPORT);
12976 
12977 		addr = sin->sin_addr.s_addr;
12978 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12979 			return (EADDRNOTAVAIL);
12980 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12981 		/* Add 96 bits */
12982 		addrlen += IPV6_ABITS - IP_ABITS;
12983 	}
12984 
12985 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12986 		return (EINVAL);
12987 
12988 	/* Check if bits in the address is set past the mask */
12989 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12990 		return (EINVAL);
12991 
12992 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12993 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12994 		return (0);	/* No change */
12995 
12996 	if (ipif->ipif_flags & IPIF_UP) {
12997 		/*
12998 		 * If the interface is already marked up,
12999 		 * we call ipif_down which will take care
13000 		 * of ditching any IREs that have been set
13001 		 * up based on the old interface address.
13002 		 */
13003 		err = ipif_logical_down(ipif, q, mp);
13004 		if (err == EINPROGRESS)
13005 			return (err);
13006 		ipif_down_tail(ipif);
13007 		need_up = B_TRUE;
13008 	}
13009 
13010 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
13011 	return (err);
13012 }
13013 
13014 static int
13015 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
13016     queue_t *q, mblk_t *mp, boolean_t need_up)
13017 {
13018 	ill_t	*ill = ipif->ipif_ill;
13019 	int	err = 0;
13020 
13021 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
13022 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13023 
13024 	/* Set the new address. */
13025 	mutex_enter(&ill->ill_lock);
13026 	ipif->ipif_v6net_mask = v6mask;
13027 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
13028 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
13029 		    ipif->ipif_v6subnet);
13030 	}
13031 	mutex_exit(&ill->ill_lock);
13032 
13033 	if (need_up) {
13034 		/*
13035 		 * Now bring the interface back up.  If this
13036 		 * is the only IPIF for the ILL, ipif_up
13037 		 * will have to re-bind to the device, so
13038 		 * we may get back EINPROGRESS, in which
13039 		 * case, this IOCTL will get completed in
13040 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
13041 		 */
13042 		err = ipif_up(ipif, q, mp);
13043 		if (err == EINPROGRESS)
13044 			return (err);
13045 	}
13046 	return (err);
13047 }
13048 
13049 /* ARGSUSED */
13050 int
13051 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13052     ip_ioctl_cmd_t *ipip, void *if_req)
13053 {
13054 	int	addrlen;
13055 	in6_addr_t v6addr;
13056 	in6_addr_t v6mask;
13057 	struct lifreq *lifr = (struct lifreq *)if_req;
13058 
13059 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
13060 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13061 	ipif_down_tail(ipif);
13062 
13063 	addrlen = lifr->lifr_addrlen;
13064 	if (ipif->ipif_isv6) {
13065 		sin6_t *sin6;
13066 
13067 		sin6 = (sin6_t *)sin;
13068 		v6addr = sin6->sin6_addr;
13069 	} else {
13070 		ipaddr_t addr;
13071 
13072 		addr = sin->sin_addr.s_addr;
13073 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
13074 		addrlen += IPV6_ABITS - IP_ABITS;
13075 	}
13076 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
13077 
13078 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
13079 }
13080 
13081 /* ARGSUSED */
13082 int
13083 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13084     ip_ioctl_cmd_t *ipip, void *if_req)
13085 {
13086 	struct lifreq *lifr = (struct lifreq *)if_req;
13087 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
13088 
13089 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
13090 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13091 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
13092 
13093 	if (ipif->ipif_isv6) {
13094 		*sin6 = sin6_null;
13095 		sin6->sin6_family = AF_INET6;
13096 		sin6->sin6_addr = ipif->ipif_v6subnet;
13097 		lifr->lifr_addrlen =
13098 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
13099 	} else {
13100 		*sin = sin_null;
13101 		sin->sin_family = AF_INET;
13102 		sin->sin_addr.s_addr = ipif->ipif_subnet;
13103 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
13104 	}
13105 	return (0);
13106 }
13107 
13108 /*
13109  * Set the IPv6 address token.
13110  */
13111 /* ARGSUSED */
13112 int
13113 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13114     ip_ioctl_cmd_t *ipi, void *if_req)
13115 {
13116 	ill_t *ill = ipif->ipif_ill;
13117 	int err;
13118 	in6_addr_t v6addr;
13119 	in6_addr_t v6mask;
13120 	boolean_t need_up = B_FALSE;
13121 	int i;
13122 	sin6_t *sin6 = (sin6_t *)sin;
13123 	struct lifreq *lifr = (struct lifreq *)if_req;
13124 	int addrlen;
13125 
13126 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
13127 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13128 	ASSERT(IAM_WRITER_IPIF(ipif));
13129 
13130 	addrlen = lifr->lifr_addrlen;
13131 	/* Only allow for logical unit zero i.e. not on "le0:17" */
13132 	if (ipif->ipif_id != 0)
13133 		return (EINVAL);
13134 
13135 	if (!ipif->ipif_isv6)
13136 		return (EINVAL);
13137 
13138 	if (addrlen > IPV6_ABITS)
13139 		return (EINVAL);
13140 
13141 	v6addr = sin6->sin6_addr;
13142 
13143 	/*
13144 	 * The length of the token is the length from the end.  To get
13145 	 * the proper mask for this, compute the mask of the bits not
13146 	 * in the token; ie. the prefix, and then xor to get the mask.
13147 	 */
13148 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
13149 		return (EINVAL);
13150 	for (i = 0; i < 4; i++) {
13151 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
13152 	}
13153 
13154 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
13155 	    ill->ill_token_length == addrlen)
13156 		return (0);	/* No change */
13157 
13158 	if (ipif->ipif_flags & IPIF_UP) {
13159 		err = ipif_logical_down(ipif, q, mp);
13160 		if (err == EINPROGRESS)
13161 			return (err);
13162 		ipif_down_tail(ipif);
13163 		need_up = B_TRUE;
13164 	}
13165 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
13166 	return (err);
13167 }
13168 
13169 static int
13170 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
13171     mblk_t *mp, boolean_t need_up)
13172 {
13173 	in6_addr_t v6addr;
13174 	in6_addr_t v6mask;
13175 	ill_t	*ill = ipif->ipif_ill;
13176 	int	i;
13177 	int	err = 0;
13178 
13179 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
13180 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13181 	v6addr = sin6->sin6_addr;
13182 	/*
13183 	 * The length of the token is the length from the end.  To get
13184 	 * the proper mask for this, compute the mask of the bits not
13185 	 * in the token; ie. the prefix, and then xor to get the mask.
13186 	 */
13187 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
13188 	for (i = 0; i < 4; i++)
13189 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
13190 
13191 	mutex_enter(&ill->ill_lock);
13192 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
13193 	ill->ill_token_length = addrlen;
13194 	mutex_exit(&ill->ill_lock);
13195 
13196 	if (need_up) {
13197 		/*
13198 		 * Now bring the interface back up.  If this
13199 		 * is the only IPIF for the ILL, ipif_up
13200 		 * will have to re-bind to the device, so
13201 		 * we may get back EINPROGRESS, in which
13202 		 * case, this IOCTL will get completed in
13203 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
13204 		 */
13205 		err = ipif_up(ipif, q, mp);
13206 		if (err == EINPROGRESS)
13207 			return (err);
13208 	}
13209 	return (err);
13210 }
13211 
13212 /* ARGSUSED */
13213 int
13214 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13215     ip_ioctl_cmd_t *ipi, void *if_req)
13216 {
13217 	ill_t *ill;
13218 	sin6_t *sin6 = (sin6_t *)sin;
13219 	struct lifreq *lifr = (struct lifreq *)if_req;
13220 
13221 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
13222 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13223 	if (ipif->ipif_id != 0)
13224 		return (EINVAL);
13225 
13226 	ill = ipif->ipif_ill;
13227 	if (!ill->ill_isv6)
13228 		return (ENXIO);
13229 
13230 	*sin6 = sin6_null;
13231 	sin6->sin6_family = AF_INET6;
13232 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
13233 	sin6->sin6_addr = ill->ill_token;
13234 	lifr->lifr_addrlen = ill->ill_token_length;
13235 	return (0);
13236 }
13237 
13238 /*
13239  * Set (hardware) link specific information that might override
13240  * what was acquired through the DL_INFO_ACK.
13241  * The logic is as follows.
13242  *
13243  * become exclusive
13244  * set CHANGING flag
13245  * change mtu on affected IREs
13246  * clear CHANGING flag
13247  *
13248  * An ire add that occurs before the CHANGING flag is set will have its mtu
13249  * changed by the ip_sioctl_lnkinfo.
13250  *
13251  * During the time the CHANGING flag is set, no new ires will be added to the
13252  * bucket, and ire add will fail (due the CHANGING flag).
13253  *
13254  * An ire add that occurs after the CHANGING flag is set will have the right mtu
13255  * before it is added to the bucket.
13256  *
13257  * Obviously only 1 thread can set the CHANGING flag and we need to become
13258  * exclusive to set the flag.
13259  */
13260 /* ARGSUSED */
13261 int
13262 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13263     ip_ioctl_cmd_t *ipi, void *if_req)
13264 {
13265 	ill_t		*ill = ipif->ipif_ill;
13266 	ipif_t		*nipif;
13267 	int		ip_min_mtu;
13268 	boolean_t	mtu_walk = B_FALSE;
13269 	struct lifreq	*lifr = (struct lifreq *)if_req;
13270 	lif_ifinfo_req_t *lir;
13271 	ire_t		*ire;
13272 
13273 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
13274 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13275 	lir = &lifr->lifr_ifinfo;
13276 	ASSERT(IAM_WRITER_IPIF(ipif));
13277 
13278 	/* Only allow for logical unit zero i.e. not on "le0:17" */
13279 	if (ipif->ipif_id != 0)
13280 		return (EINVAL);
13281 
13282 	/* Set interface MTU. */
13283 	if (ipif->ipif_isv6)
13284 		ip_min_mtu = IPV6_MIN_MTU;
13285 	else
13286 		ip_min_mtu = IP_MIN_MTU;
13287 
13288 	/*
13289 	 * Verify values before we set anything. Allow zero to
13290 	 * mean unspecified.
13291 	 */
13292 	if (lir->lir_maxmtu != 0 &&
13293 	    (lir->lir_maxmtu > ill->ill_max_frag ||
13294 	    lir->lir_maxmtu < ip_min_mtu))
13295 		return (EINVAL);
13296 	if (lir->lir_reachtime != 0 &&
13297 	    lir->lir_reachtime > ND_MAX_REACHTIME)
13298 		return (EINVAL);
13299 	if (lir->lir_reachretrans != 0 &&
13300 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
13301 		return (EINVAL);
13302 
13303 	mutex_enter(&ill->ill_lock);
13304 	ill->ill_state_flags |= ILL_CHANGING;
13305 	for (nipif = ill->ill_ipif; nipif != NULL;
13306 	    nipif = nipif->ipif_next) {
13307 		nipif->ipif_state_flags |= IPIF_CHANGING;
13308 	}
13309 
13310 	mutex_exit(&ill->ill_lock);
13311 
13312 	if (lir->lir_maxmtu != 0) {
13313 		ill->ill_max_mtu = lir->lir_maxmtu;
13314 		ill->ill_mtu_userspecified = 1;
13315 		mtu_walk = B_TRUE;
13316 	}
13317 
13318 	if (lir->lir_reachtime != 0)
13319 		ill->ill_reachable_time = lir->lir_reachtime;
13320 
13321 	if (lir->lir_reachretrans != 0)
13322 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
13323 
13324 	ill->ill_max_hops = lir->lir_maxhops;
13325 
13326 	ill->ill_max_buf = ND_MAX_Q;
13327 
13328 	if (mtu_walk) {
13329 		/*
13330 		 * Set the MTU on all ipifs associated with this ill except
13331 		 * for those whose MTU was fixed via SIOCSLIFMTU.
13332 		 */
13333 		for (nipif = ill->ill_ipif; nipif != NULL;
13334 		    nipif = nipif->ipif_next) {
13335 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
13336 				continue;
13337 
13338 			nipif->ipif_mtu = ill->ill_max_mtu;
13339 
13340 			if (!(nipif->ipif_flags & IPIF_UP))
13341 				continue;
13342 
13343 			if (nipif->ipif_isv6)
13344 				ire = ipif_to_ire_v6(nipif);
13345 			else
13346 				ire = ipif_to_ire(nipif);
13347 			if (ire != NULL) {
13348 				ire->ire_max_frag = ipif->ipif_mtu;
13349 				ire_refrele(ire);
13350 			}
13351 			if (ill->ill_isv6) {
13352 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
13353 				    ipif_mtu_change, (char *)nipif,
13354 				    ill);
13355 			} else {
13356 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
13357 				    ipif_mtu_change, (char *)nipif,
13358 				    ill);
13359 			}
13360 		}
13361 	}
13362 
13363 	mutex_enter(&ill->ill_lock);
13364 	for (nipif = ill->ill_ipif; nipif != NULL;
13365 	    nipif = nipif->ipif_next) {
13366 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
13367 	}
13368 	ILL_UNMARK_CHANGING(ill);
13369 	mutex_exit(&ill->ill_lock);
13370 
13371 	return (0);
13372 }
13373 
13374 /* ARGSUSED */
13375 int
13376 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13377     ip_ioctl_cmd_t *ipi, void *if_req)
13378 {
13379 	struct lif_ifinfo_req *lir;
13380 	ill_t *ill = ipif->ipif_ill;
13381 
13382 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13383 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13384 	if (ipif->ipif_id != 0)
13385 		return (EINVAL);
13386 
13387 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13388 	lir->lir_maxhops = ill->ill_max_hops;
13389 	lir->lir_reachtime = ill->ill_reachable_time;
13390 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13391 	lir->lir_maxmtu = ill->ill_max_mtu;
13392 
13393 	return (0);
13394 }
13395 
13396 /*
13397  * Return best guess as to the subnet mask for the specified address.
13398  * Based on the subnet masks for all the configured interfaces.
13399  *
13400  * We end up returning a zero mask in the case of default, multicast or
13401  * experimental.
13402  */
13403 static ipaddr_t
13404 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13405 {
13406 	ipaddr_t net_mask;
13407 	ill_t	*ill;
13408 	ipif_t	*ipif;
13409 	ill_walk_context_t ctx;
13410 	ipif_t	*fallback_ipif = NULL;
13411 
13412 	net_mask = ip_net_mask(addr);
13413 	if (net_mask == 0) {
13414 		*ipifp = NULL;
13415 		return (0);
13416 	}
13417 
13418 	/* Let's check to see if this is maybe a local subnet route. */
13419 	/* this function only applies to IPv4 interfaces */
13420 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13421 	ill = ILL_START_WALK_V4(&ctx, ipst);
13422 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13423 		mutex_enter(&ill->ill_lock);
13424 		for (ipif = ill->ill_ipif; ipif != NULL;
13425 		    ipif = ipif->ipif_next) {
13426 			if (!IPIF_CAN_LOOKUP(ipif))
13427 				continue;
13428 			if (!(ipif->ipif_flags & IPIF_UP))
13429 				continue;
13430 			if ((ipif->ipif_subnet & net_mask) ==
13431 			    (addr & net_mask)) {
13432 				/*
13433 				 * Don't trust pt-pt interfaces if there are
13434 				 * other interfaces.
13435 				 */
13436 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13437 					if (fallback_ipif == NULL) {
13438 						ipif_refhold_locked(ipif);
13439 						fallback_ipif = ipif;
13440 					}
13441 					continue;
13442 				}
13443 
13444 				/*
13445 				 * Fine. Just assume the same net mask as the
13446 				 * directly attached subnet interface is using.
13447 				 */
13448 				ipif_refhold_locked(ipif);
13449 				mutex_exit(&ill->ill_lock);
13450 				rw_exit(&ipst->ips_ill_g_lock);
13451 				if (fallback_ipif != NULL)
13452 					ipif_refrele(fallback_ipif);
13453 				*ipifp = ipif;
13454 				return (ipif->ipif_net_mask);
13455 			}
13456 		}
13457 		mutex_exit(&ill->ill_lock);
13458 	}
13459 	rw_exit(&ipst->ips_ill_g_lock);
13460 
13461 	*ipifp = fallback_ipif;
13462 	return ((fallback_ipif != NULL) ?
13463 	    fallback_ipif->ipif_net_mask : net_mask);
13464 }
13465 
13466 /*
13467  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13468  */
13469 static void
13470 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13471 {
13472 	IOCP	iocp;
13473 	ipft_t	*ipft;
13474 	ipllc_t	*ipllc;
13475 	mblk_t	*mp1;
13476 	cred_t	*cr;
13477 	int	error = 0;
13478 	conn_t	*connp;
13479 
13480 	ip1dbg(("ip_wput_ioctl"));
13481 	iocp = (IOCP)mp->b_rptr;
13482 	mp1 = mp->b_cont;
13483 	if (mp1 == NULL) {
13484 		iocp->ioc_error = EINVAL;
13485 		mp->b_datap->db_type = M_IOCNAK;
13486 		iocp->ioc_count = 0;
13487 		qreply(q, mp);
13488 		return;
13489 	}
13490 
13491 	/*
13492 	 * These IOCTLs provide various control capabilities to
13493 	 * upstream agents such as ULPs and processes.	There
13494 	 * are currently two such IOCTLs implemented.  They
13495 	 * are used by TCP to provide update information for
13496 	 * existing IREs and to forcibly delete an IRE for a
13497 	 * host that is not responding, thereby forcing an
13498 	 * attempt at a new route.
13499 	 */
13500 	iocp->ioc_error = EINVAL;
13501 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13502 		goto done;
13503 
13504 	ipllc = (ipllc_t *)mp1->b_rptr;
13505 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13506 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13507 			break;
13508 	}
13509 	/*
13510 	 * prefer credential from mblk over ioctl;
13511 	 * see ip_sioctl_copyin_setup
13512 	 */
13513 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13514 
13515 	/*
13516 	 * Refhold the conn in case the request gets queued up in some lookup
13517 	 */
13518 	ASSERT(CONN_Q(q));
13519 	connp = Q_TO_CONN(q);
13520 	CONN_INC_REF(connp);
13521 	if (ipft->ipft_pfi &&
13522 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13523 	    pullupmsg(mp1, ipft->ipft_min_size))) {
13524 		error = (*ipft->ipft_pfi)(q,
13525 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13526 	}
13527 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13528 		/*
13529 		 * CONN_OPER_PENDING_DONE happens in the function called
13530 		 * through ipft_pfi above.
13531 		 */
13532 		return;
13533 	}
13534 
13535 	CONN_OPER_PENDING_DONE(connp);
13536 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13537 		freemsg(mp);
13538 		return;
13539 	}
13540 	iocp->ioc_error = error;
13541 
13542 done:
13543 	mp->b_datap->db_type = M_IOCACK;
13544 	if (iocp->ioc_error)
13545 		iocp->ioc_count = 0;
13546 	qreply(q, mp);
13547 }
13548 
13549 /*
13550  * Lookup an ipif using the sequence id (ipif_seqid)
13551  */
13552 ipif_t *
13553 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13554 {
13555 	ipif_t *ipif;
13556 
13557 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13558 
13559 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13560 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13561 			return (ipif);
13562 	}
13563 	return (NULL);
13564 }
13565 
13566 /*
13567  * Assign a unique id for the ipif. This is used later when we send
13568  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13569  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13570  * IRE is added, we verify that ipif has not disappeared.
13571  */
13572 
13573 static void
13574 ipif_assign_seqid(ipif_t *ipif)
13575 {
13576 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13577 
13578 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13579 }
13580 
13581 /*
13582  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13583  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13584  * be inserted into the first space available in the list. The value of
13585  * ipif_id will then be set to the appropriate value for its position.
13586  */
13587 static int
13588 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13589 {
13590 	ill_t *ill;
13591 	ipif_t *tipif;
13592 	ipif_t **tipifp;
13593 	int id;
13594 	ip_stack_t	*ipst;
13595 
13596 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13597 	    IAM_WRITER_IPIF(ipif));
13598 
13599 	ill = ipif->ipif_ill;
13600 	ASSERT(ill != NULL);
13601 	ipst = ill->ill_ipst;
13602 
13603 	/*
13604 	 * In the case of lo0:0 we already hold the ill_g_lock.
13605 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13606 	 * ipif_insert. Another such caller is ipif_move.
13607 	 */
13608 	if (acquire_g_lock)
13609 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13610 	if (acquire_ill_lock)
13611 		mutex_enter(&ill->ill_lock);
13612 	id = ipif->ipif_id;
13613 	tipifp = &(ill->ill_ipif);
13614 	if (id == -1) {	/* need to find a real id */
13615 		id = 0;
13616 		while ((tipif = *tipifp) != NULL) {
13617 			ASSERT(tipif->ipif_id >= id);
13618 			if (tipif->ipif_id != id)
13619 				break; /* non-consecutive id */
13620 			id++;
13621 			tipifp = &(tipif->ipif_next);
13622 		}
13623 		/* limit number of logical interfaces */
13624 		if (id >= ipst->ips_ip_addrs_per_if) {
13625 			if (acquire_ill_lock)
13626 				mutex_exit(&ill->ill_lock);
13627 			if (acquire_g_lock)
13628 				rw_exit(&ipst->ips_ill_g_lock);
13629 			return (-1);
13630 		}
13631 		ipif->ipif_id = id; /* assign new id */
13632 	} else if (id < ipst->ips_ip_addrs_per_if) {
13633 		/* we have a real id; insert ipif in the right place */
13634 		while ((tipif = *tipifp) != NULL) {
13635 			ASSERT(tipif->ipif_id != id);
13636 			if (tipif->ipif_id > id)
13637 				break; /* found correct location */
13638 			tipifp = &(tipif->ipif_next);
13639 		}
13640 	} else {
13641 		if (acquire_ill_lock)
13642 			mutex_exit(&ill->ill_lock);
13643 		if (acquire_g_lock)
13644 			rw_exit(&ipst->ips_ill_g_lock);
13645 		return (-1);
13646 	}
13647 
13648 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13649 
13650 	ipif->ipif_next = tipif;
13651 	*tipifp = ipif;
13652 	if (acquire_ill_lock)
13653 		mutex_exit(&ill->ill_lock);
13654 	if (acquire_g_lock)
13655 		rw_exit(&ipst->ips_ill_g_lock);
13656 	return (0);
13657 }
13658 
13659 static void
13660 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13661 {
13662 	ipif_t	**ipifp;
13663 	ill_t	*ill = ipif->ipif_ill;
13664 
13665 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13666 	if (acquire_ill_lock)
13667 		mutex_enter(&ill->ill_lock);
13668 	else
13669 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13670 
13671 	ipifp = &ill->ill_ipif;
13672 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13673 		if (*ipifp == ipif) {
13674 			*ipifp = ipif->ipif_next;
13675 			break;
13676 		}
13677 	}
13678 
13679 	if (acquire_ill_lock)
13680 		mutex_exit(&ill->ill_lock);
13681 }
13682 
13683 /*
13684  * Allocate and initialize a new interface control structure.  (Always
13685  * called as writer.)
13686  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13687  * is not part of the global linked list of ills. ipif_seqid is unique
13688  * in the system and to preserve the uniqueness, it is assigned only
13689  * when ill becomes part of the global list. At that point ill will
13690  * have a name. If it doesn't get assigned here, it will get assigned
13691  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13692  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13693  * the interface flags or any other information from the DL_INFO_ACK for
13694  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13695  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13696  * second DL_INFO_ACK comes in from the driver.
13697  */
13698 static ipif_t *
13699 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13700 {
13701 	ipif_t	*ipif;
13702 	phyint_t *phyi;
13703 
13704 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13705 	    ill->ill_name, id, (void *)ill));
13706 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13707 
13708 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13709 		return (NULL);
13710 	*ipif = ipif_zero;	/* start clean */
13711 
13712 	ipif->ipif_ill = ill;
13713 	ipif->ipif_id = id;	/* could be -1 */
13714 	/*
13715 	 * Inherit the zoneid from the ill; for the shared stack instance
13716 	 * this is always the global zone
13717 	 */
13718 	ipif->ipif_zoneid = ill->ill_zoneid;
13719 
13720 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13721 
13722 	ipif->ipif_refcnt = 0;
13723 	ipif->ipif_saved_ire_cnt = 0;
13724 
13725 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13726 		mi_free(ipif);
13727 		return (NULL);
13728 	}
13729 	/* -1 id should have been replaced by real id */
13730 	id = ipif->ipif_id;
13731 	ASSERT(id >= 0);
13732 
13733 	if (ill->ill_name[0] != '\0')
13734 		ipif_assign_seqid(ipif);
13735 
13736 	/*
13737 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13738 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13739 	 * ioctl sets ipif_orig_ipifid to zero.
13740 	 */
13741 	ipif->ipif_orig_ipifid = id;
13742 
13743 	/*
13744 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13745 	 * The ipif is still not up and can't be looked up until the
13746 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13747 	 */
13748 	mutex_enter(&ill->ill_lock);
13749 	mutex_enter(&ill->ill_phyint->phyint_lock);
13750 	/*
13751 	 * Set the running flag when logical interface zero is created.
13752 	 * For subsequent logical interfaces, a DLPI link down
13753 	 * notification message may have cleared the running flag to
13754 	 * indicate the link is down, so we shouldn't just blindly set it.
13755 	 */
13756 	if (id == 0)
13757 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13758 	ipif->ipif_ire_type = ire_type;
13759 	phyi = ill->ill_phyint;
13760 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13761 
13762 	if (ipif->ipif_isv6) {
13763 		ill->ill_flags |= ILLF_IPV6;
13764 	} else {
13765 		ipaddr_t inaddr_any = INADDR_ANY;
13766 
13767 		ill->ill_flags |= ILLF_IPV4;
13768 
13769 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13770 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13771 		    &ipif->ipif_v6lcl_addr);
13772 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13773 		    &ipif->ipif_v6src_addr);
13774 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13775 		    &ipif->ipif_v6subnet);
13776 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13777 		    &ipif->ipif_v6net_mask);
13778 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13779 		    &ipif->ipif_v6brd_addr);
13780 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13781 		    &ipif->ipif_v6pp_dst_addr);
13782 	}
13783 
13784 	/*
13785 	 * Don't set the interface flags etc. now, will do it in
13786 	 * ip_ll_subnet_defaults.
13787 	 */
13788 	if (!initialize) {
13789 		mutex_exit(&ill->ill_lock);
13790 		mutex_exit(&ill->ill_phyint->phyint_lock);
13791 		return (ipif);
13792 	}
13793 	ipif->ipif_mtu = ill->ill_max_mtu;
13794 
13795 	if (ill->ill_bcast_addr_length != 0) {
13796 		/*
13797 		 * Later detect lack of DLPI driver multicast
13798 		 * capability by catching DL_ENABMULTI errors in
13799 		 * ip_rput_dlpi.
13800 		 */
13801 		ill->ill_flags |= ILLF_MULTICAST;
13802 		if (!ipif->ipif_isv6)
13803 			ipif->ipif_flags |= IPIF_BROADCAST;
13804 	} else {
13805 		if (ill->ill_net_type != IRE_LOOPBACK) {
13806 			if (ipif->ipif_isv6)
13807 				/*
13808 				 * Note: xresolv interfaces will eventually need
13809 				 * NOARP set here as well, but that will require
13810 				 * those external resolvers to have some
13811 				 * knowledge of that flag and act appropriately.
13812 				 * Not to be changed at present.
13813 				 */
13814 				ill->ill_flags |= ILLF_NONUD;
13815 			else
13816 				ill->ill_flags |= ILLF_NOARP;
13817 		}
13818 		if (ill->ill_phys_addr_length == 0) {
13819 			if (ill->ill_media &&
13820 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13821 				ipif->ipif_flags |= IPIF_NOXMIT;
13822 				phyi->phyint_flags |= PHYI_VIRTUAL;
13823 			} else {
13824 				/* pt-pt supports multicast. */
13825 				ill->ill_flags |= ILLF_MULTICAST;
13826 				if (ill->ill_net_type == IRE_LOOPBACK) {
13827 					phyi->phyint_flags |=
13828 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13829 				} else {
13830 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13831 				}
13832 			}
13833 		}
13834 	}
13835 	mutex_exit(&ill->ill_lock);
13836 	mutex_exit(&ill->ill_phyint->phyint_lock);
13837 	return (ipif);
13838 }
13839 
13840 /*
13841  * If appropriate, send a message up to the resolver delete the entry
13842  * for the address of this interface which is going out of business.
13843  * (Always called as writer).
13844  *
13845  * NOTE : We need to check for NULL mps as some of the fields are
13846  *	  initialized only for some interface types. See ipif_resolver_up()
13847  *	  for details.
13848  */
13849 void
13850 ipif_arp_down(ipif_t *ipif)
13851 {
13852 	mblk_t	*mp;
13853 	ill_t	*ill = ipif->ipif_ill;
13854 
13855 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13856 	ASSERT(IAM_WRITER_IPIF(ipif));
13857 
13858 	/* Delete the mapping for the local address */
13859 	mp = ipif->ipif_arp_del_mp;
13860 	if (mp != NULL) {
13861 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13862 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13863 		putnext(ill->ill_rq, mp);
13864 		ipif->ipif_arp_del_mp = NULL;
13865 	}
13866 
13867 	/*
13868 	 * If this is the last ipif that is going down and there are no
13869 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13870 	 * clean up ARP completely.
13871 	 */
13872 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13873 
13874 		/* Send up AR_INTERFACE_DOWN message */
13875 		mp = ill->ill_arp_down_mp;
13876 		if (mp != NULL) {
13877 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13878 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13879 			    ipif->ipif_id));
13880 			putnext(ill->ill_rq, mp);
13881 			ill->ill_arp_down_mp = NULL;
13882 		}
13883 
13884 		/* Tell ARP to delete the multicast mappings */
13885 		mp = ill->ill_arp_del_mapping_mp;
13886 		if (mp != NULL) {
13887 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13888 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13889 			    ipif->ipif_id));
13890 			putnext(ill->ill_rq, mp);
13891 			ill->ill_arp_del_mapping_mp = NULL;
13892 		}
13893 	}
13894 }
13895 
13896 /*
13897  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13898  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13899  * that it wants the add_mp allocated in this function to be returned
13900  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13901  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13902  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13903  * as it does a ipif_arp_down after calling this function - which will
13904  * remove what we add here.
13905  *
13906  * Returns -1 on failures and 0 on success.
13907  */
13908 int
13909 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13910 {
13911 	mblk_t	*del_mp = NULL;
13912 	mblk_t *add_mp = NULL;
13913 	mblk_t *mp;
13914 	ill_t	*ill = ipif->ipif_ill;
13915 	phyint_t *phyi = ill->ill_phyint;
13916 	ipaddr_t addr, mask, extract_mask = 0;
13917 	arma_t	*arma;
13918 	uint8_t *maddr, *bphys_addr;
13919 	uint32_t hw_start;
13920 	dl_unitdata_req_t *dlur;
13921 
13922 	ASSERT(IAM_WRITER_IPIF(ipif));
13923 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13924 		return (0);
13925 
13926 	/*
13927 	 * Delete the existing mapping from ARP. Normally ipif_down
13928 	 * -> ipif_arp_down should send this up to ARP. The only
13929 	 * reason we would find this when we are switching from
13930 	 * Multicast to Broadcast where we did not do a down.
13931 	 */
13932 	mp = ill->ill_arp_del_mapping_mp;
13933 	if (mp != NULL) {
13934 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13935 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13936 		putnext(ill->ill_rq, mp);
13937 		ill->ill_arp_del_mapping_mp = NULL;
13938 	}
13939 
13940 	if (arp_add_mapping_mp != NULL)
13941 		*arp_add_mapping_mp = NULL;
13942 
13943 	/*
13944 	 * Check that the address is not to long for the constant
13945 	 * length reserved in the template arma_t.
13946 	 */
13947 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13948 		return (-1);
13949 
13950 	/* Add mapping mblk */
13951 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13952 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13953 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13954 	    (caddr_t)&addr);
13955 	if (add_mp == NULL)
13956 		return (-1);
13957 	arma = (arma_t *)add_mp->b_rptr;
13958 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13959 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13960 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13961 
13962 	/*
13963 	 * Determine the broadcast address.
13964 	 */
13965 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13966 	if (ill->ill_sap_length < 0)
13967 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13968 	else
13969 		bphys_addr = (uchar_t *)dlur +
13970 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13971 	/*
13972 	 * Check PHYI_MULTI_BCAST and length of physical
13973 	 * address to determine if we use the mapping or the
13974 	 * broadcast address.
13975 	 */
13976 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13977 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13978 		    bphys_addr, maddr, &hw_start, &extract_mask))
13979 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13980 
13981 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13982 	    (ill->ill_flags & ILLF_MULTICAST)) {
13983 		/* Make sure this will not match the "exact" entry. */
13984 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13985 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13986 		    (caddr_t)&addr);
13987 		if (del_mp == NULL) {
13988 			freemsg(add_mp);
13989 			return (-1);
13990 		}
13991 		bcopy(&extract_mask, (char *)arma +
13992 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13993 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13994 			/* Use link-layer broadcast address for MULTI_BCAST */
13995 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13996 			ip2dbg(("ipif_arp_setup_multicast: adding"
13997 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13998 		} else {
13999 			arma->arma_hw_mapping_start = hw_start;
14000 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
14001 			    " ARP setup for %s\n", ill->ill_name));
14002 		}
14003 	} else {
14004 		freemsg(add_mp);
14005 		ASSERT(del_mp == NULL);
14006 		/* It is neither MULTICAST nor MULTI_BCAST */
14007 		return (0);
14008 	}
14009 	ASSERT(add_mp != NULL && del_mp != NULL);
14010 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
14011 	ill->ill_arp_del_mapping_mp = del_mp;
14012 	if (arp_add_mapping_mp != NULL) {
14013 		/* The caller just wants the mblks allocated */
14014 		*arp_add_mapping_mp = add_mp;
14015 	} else {
14016 		/* The caller wants us to send it to arp */
14017 		putnext(ill->ill_rq, add_mp);
14018 	}
14019 	return (0);
14020 }
14021 
14022 /*
14023  * Get the resolver set up for a new interface address.
14024  * (Always called as writer.)
14025  * Called both for IPv4 and IPv6 interfaces,
14026  * though it only sets up the resolver for v6
14027  * if it's an xresolv interface (one using an external resolver).
14028  * Honors ILLF_NOARP.
14029  * The enumerated value res_act is used to tune the behavior.
14030  * If set to Res_act_initial, then we set up all the resolver
14031  * structures for a new interface.  If set to Res_act_move, then
14032  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
14033  * interfaces; this is called by ip_rput_dlpi_writer() to handle
14034  * asynchronous hardware address change notification.  If set to
14035  * Res_act_defend, then we tell ARP that it needs to send a single
14036  * gratuitous message in defense of the address.
14037  * Returns error on failure.
14038  */
14039 int
14040 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
14041 {
14042 	caddr_t	addr;
14043 	mblk_t	*arp_up_mp = NULL;
14044 	mblk_t	*arp_down_mp = NULL;
14045 	mblk_t	*arp_add_mp = NULL;
14046 	mblk_t	*arp_del_mp = NULL;
14047 	mblk_t	*arp_add_mapping_mp = NULL;
14048 	mblk_t	*arp_del_mapping_mp = NULL;
14049 	ill_t	*ill = ipif->ipif_ill;
14050 	uchar_t	*area_p = NULL;
14051 	uchar_t	*ared_p = NULL;
14052 	int	err = ENOMEM;
14053 	boolean_t was_dup;
14054 
14055 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
14056 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
14057 	ASSERT(IAM_WRITER_IPIF(ipif));
14058 
14059 	was_dup = B_FALSE;
14060 	if (res_act == Res_act_initial) {
14061 		ipif->ipif_addr_ready = 0;
14062 		/*
14063 		 * We're bringing an interface up here.  There's no way that we
14064 		 * should need to shut down ARP now.
14065 		 */
14066 		mutex_enter(&ill->ill_lock);
14067 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
14068 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
14069 			ill->ill_ipif_dup_count--;
14070 			was_dup = B_TRUE;
14071 		}
14072 		mutex_exit(&ill->ill_lock);
14073 	}
14074 	if (ipif->ipif_recovery_id != 0)
14075 		(void) untimeout(ipif->ipif_recovery_id);
14076 	ipif->ipif_recovery_id = 0;
14077 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
14078 		ipif->ipif_addr_ready = 1;
14079 		return (0);
14080 	}
14081 	/* NDP will set the ipif_addr_ready flag when it's ready */
14082 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
14083 		return (0);
14084 
14085 	if (ill->ill_isv6) {
14086 		/*
14087 		 * External resolver for IPv6
14088 		 */
14089 		ASSERT(res_act == Res_act_initial);
14090 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
14091 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
14092 			area_p = (uchar_t *)&ip6_area_template;
14093 			ared_p = (uchar_t *)&ip6_ared_template;
14094 		}
14095 	} else {
14096 		/*
14097 		 * IPv4 arp case. If the ARP stream has already started
14098 		 * closing, fail this request for ARP bringup. Else
14099 		 * record the fact that an ARP bringup is pending.
14100 		 */
14101 		mutex_enter(&ill->ill_lock);
14102 		if (ill->ill_arp_closing) {
14103 			mutex_exit(&ill->ill_lock);
14104 			err = EINVAL;
14105 			goto failed;
14106 		} else {
14107 			if (ill->ill_ipif_up_count == 0 &&
14108 			    ill->ill_ipif_dup_count == 0 && !was_dup)
14109 				ill->ill_arp_bringup_pending = 1;
14110 			mutex_exit(&ill->ill_lock);
14111 		}
14112 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
14113 			addr = (caddr_t)&ipif->ipif_lcl_addr;
14114 			area_p = (uchar_t *)&ip_area_template;
14115 			ared_p = (uchar_t *)&ip_ared_template;
14116 		}
14117 	}
14118 
14119 	/*
14120 	 * Add an entry for the local address in ARP only if it
14121 	 * is not UNNUMBERED and the address is not INADDR_ANY.
14122 	 */
14123 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
14124 		area_t *area;
14125 
14126 		/* Now ask ARP to publish our address. */
14127 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
14128 		if (arp_add_mp == NULL)
14129 			goto failed;
14130 		area = (area_t *)arp_add_mp->b_rptr;
14131 		if (res_act != Res_act_initial) {
14132 			/*
14133 			 * Copy the new hardware address and length into
14134 			 * arp_add_mp to be sent to ARP.
14135 			 */
14136 			area->area_hw_addr_length = ill->ill_phys_addr_length;
14137 			bcopy(ill->ill_phys_addr,
14138 			    ((char *)area + area->area_hw_addr_offset),
14139 			    area->area_hw_addr_length);
14140 		}
14141 
14142 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
14143 		    ACE_F_MYADDR;
14144 
14145 		if (res_act == Res_act_defend) {
14146 			area->area_flags |= ACE_F_DEFEND;
14147 			/*
14148 			 * If we're just defending our address now, then
14149 			 * there's no need to set up ARP multicast mappings.
14150 			 * The publish command is enough.
14151 			 */
14152 			goto done;
14153 		}
14154 
14155 		if (res_act != Res_act_initial)
14156 			goto arp_setup_multicast;
14157 
14158 		/*
14159 		 * Allocate an ARP deletion message so we know we can tell ARP
14160 		 * when the interface goes down.
14161 		 */
14162 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
14163 		if (arp_del_mp == NULL)
14164 			goto failed;
14165 
14166 	} else {
14167 		if (res_act != Res_act_initial)
14168 			goto done;
14169 	}
14170 	/*
14171 	 * Need to bring up ARP or setup multicast mapping only
14172 	 * when the first interface is coming UP.
14173 	 */
14174 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
14175 	    was_dup) {
14176 		goto done;
14177 	}
14178 
14179 	/*
14180 	 * Allocate an ARP down message (to be saved) and an ARP up
14181 	 * message.
14182 	 */
14183 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
14184 	if (arp_down_mp == NULL)
14185 		goto failed;
14186 
14187 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
14188 	if (arp_up_mp == NULL)
14189 		goto failed;
14190 
14191 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
14192 		goto done;
14193 
14194 arp_setup_multicast:
14195 	/*
14196 	 * Setup the multicast mappings. This function initializes
14197 	 * ill_arp_del_mapping_mp also. This does not need to be done for
14198 	 * IPv6.
14199 	 */
14200 	if (!ill->ill_isv6) {
14201 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
14202 		if (err != 0)
14203 			goto failed;
14204 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
14205 		ASSERT(arp_add_mapping_mp != NULL);
14206 	}
14207 
14208 done:
14209 	if (arp_del_mp != NULL) {
14210 		ASSERT(ipif->ipif_arp_del_mp == NULL);
14211 		ipif->ipif_arp_del_mp = arp_del_mp;
14212 	}
14213 	if (arp_down_mp != NULL) {
14214 		ASSERT(ill->ill_arp_down_mp == NULL);
14215 		ill->ill_arp_down_mp = arp_down_mp;
14216 	}
14217 	if (arp_del_mapping_mp != NULL) {
14218 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
14219 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
14220 	}
14221 	if (arp_up_mp != NULL) {
14222 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
14223 		    ill->ill_name, ipif->ipif_id));
14224 		putnext(ill->ill_rq, arp_up_mp);
14225 	}
14226 	if (arp_add_mp != NULL) {
14227 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
14228 		    ill->ill_name, ipif->ipif_id));
14229 		/*
14230 		 * If it's an extended ARP implementation, then we'll wait to
14231 		 * hear that DAD has finished before using the interface.
14232 		 */
14233 		if (!ill->ill_arp_extend)
14234 			ipif->ipif_addr_ready = 1;
14235 		putnext(ill->ill_rq, arp_add_mp);
14236 	} else {
14237 		ipif->ipif_addr_ready = 1;
14238 	}
14239 	if (arp_add_mapping_mp != NULL) {
14240 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
14241 		    ill->ill_name, ipif->ipif_id));
14242 		putnext(ill->ill_rq, arp_add_mapping_mp);
14243 	}
14244 	if (res_act != Res_act_initial)
14245 		return (0);
14246 
14247 	if (ill->ill_flags & ILLF_NOARP)
14248 		err = ill_arp_off(ill);
14249 	else
14250 		err = ill_arp_on(ill);
14251 	if (err != 0) {
14252 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
14253 		freemsg(ipif->ipif_arp_del_mp);
14254 		freemsg(ill->ill_arp_down_mp);
14255 		freemsg(ill->ill_arp_del_mapping_mp);
14256 		ipif->ipif_arp_del_mp = NULL;
14257 		ill->ill_arp_down_mp = NULL;
14258 		ill->ill_arp_del_mapping_mp = NULL;
14259 		return (err);
14260 	}
14261 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
14262 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
14263 
14264 failed:
14265 	ip1dbg(("ipif_resolver_up: FAILED\n"));
14266 	freemsg(arp_add_mp);
14267 	freemsg(arp_del_mp);
14268 	freemsg(arp_add_mapping_mp);
14269 	freemsg(arp_up_mp);
14270 	freemsg(arp_down_mp);
14271 	ill->ill_arp_bringup_pending = 0;
14272 	return (err);
14273 }
14274 
14275 /*
14276  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
14277  * just gone back up.
14278  */
14279 static void
14280 ipif_arp_start_dad(ipif_t *ipif)
14281 {
14282 	ill_t *ill = ipif->ipif_ill;
14283 	mblk_t *arp_add_mp;
14284 	area_t *area;
14285 
14286 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
14287 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
14288 	    ipif->ipif_lcl_addr == INADDR_ANY ||
14289 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
14290 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
14291 		/*
14292 		 * If we can't contact ARP for some reason, that's not really a
14293 		 * problem.  Just send out the routing socket notification that
14294 		 * DAD completion would have done, and continue.
14295 		 */
14296 		ipif_mask_reply(ipif);
14297 		ip_rts_ifmsg(ipif);
14298 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14299 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14300 		ipif->ipif_addr_ready = 1;
14301 		return;
14302 	}
14303 
14304 	/* Setting the 'unverified' flag restarts DAD */
14305 	area = (area_t *)arp_add_mp->b_rptr;
14306 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
14307 	    ACE_F_UNVERIFIED;
14308 	putnext(ill->ill_rq, arp_add_mp);
14309 }
14310 
14311 static void
14312 ipif_ndp_start_dad(ipif_t *ipif)
14313 {
14314 	nce_t *nce;
14315 
14316 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
14317 	if (nce == NULL)
14318 		return;
14319 
14320 	if (!ndp_restart_dad(nce)) {
14321 		/*
14322 		 * If we can't restart DAD for some reason, that's not really a
14323 		 * problem.  Just send out the routing socket notification that
14324 		 * DAD completion would have done, and continue.
14325 		 */
14326 		ip_rts_ifmsg(ipif);
14327 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14328 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14329 		ipif->ipif_addr_ready = 1;
14330 	}
14331 	NCE_REFRELE(nce);
14332 }
14333 
14334 /*
14335  * Restart duplicate address detection on all interfaces on the given ill.
14336  *
14337  * This is called when an interface transitions from down to up
14338  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
14339  *
14340  * Note that since the underlying physical link has transitioned, we must cause
14341  * at least one routing socket message to be sent here, either via DAD
14342  * completion or just by default on the first ipif.  (If we don't do this, then
14343  * in.mpathd will see long delays when doing link-based failure recovery.)
14344  */
14345 void
14346 ill_restart_dad(ill_t *ill, boolean_t went_up)
14347 {
14348 	ipif_t *ipif;
14349 
14350 	if (ill == NULL)
14351 		return;
14352 
14353 	/*
14354 	 * If layer two doesn't support duplicate address detection, then just
14355 	 * send the routing socket message now and be done with it.
14356 	 */
14357 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
14358 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
14359 		ip_rts_ifmsg(ill->ill_ipif);
14360 		return;
14361 	}
14362 
14363 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14364 		if (went_up) {
14365 			if (ipif->ipif_flags & IPIF_UP) {
14366 				if (ill->ill_isv6)
14367 					ipif_ndp_start_dad(ipif);
14368 				else
14369 					ipif_arp_start_dad(ipif);
14370 			} else if (ill->ill_isv6 &&
14371 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14372 				/*
14373 				 * For IPv4, the ARP module itself will
14374 				 * automatically start the DAD process when it
14375 				 * sees DL_NOTE_LINK_UP.  We respond to the
14376 				 * AR_CN_READY at the completion of that task.
14377 				 * For IPv6, we must kick off the bring-up
14378 				 * process now.
14379 				 */
14380 				ndp_do_recovery(ipif);
14381 			} else {
14382 				/*
14383 				 * Unfortunately, the first ipif is "special"
14384 				 * and represents the underlying ill in the
14385 				 * routing socket messages.  Thus, when this
14386 				 * one ipif is down, we must still notify so
14387 				 * that the user knows the IFF_RUNNING status
14388 				 * change.  (If the first ipif is up, then
14389 				 * we'll handle eventual routing socket
14390 				 * notification via DAD completion.)
14391 				 */
14392 				if (ipif == ill->ill_ipif)
14393 					ip_rts_ifmsg(ill->ill_ipif);
14394 			}
14395 		} else {
14396 			/*
14397 			 * After link down, we'll need to send a new routing
14398 			 * message when the link comes back, so clear
14399 			 * ipif_addr_ready.
14400 			 */
14401 			ipif->ipif_addr_ready = 0;
14402 		}
14403 	}
14404 
14405 	/*
14406 	 * If we've torn down links, then notify the user right away.
14407 	 */
14408 	if (!went_up)
14409 		ip_rts_ifmsg(ill->ill_ipif);
14410 }
14411 
14412 /*
14413  * Wakeup all threads waiting to enter the ipsq, and sleeping
14414  * on any of the ills in this ipsq. The ill_lock of the ill
14415  * must be held so that waiters don't miss wakeups
14416  */
14417 static void
14418 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14419 {
14420 	phyint_t *phyint;
14421 
14422 	phyint = ipsq->ipsq_phyint_list;
14423 	while (phyint != NULL) {
14424 		if (phyint->phyint_illv4) {
14425 			if (!caller_holds_lock)
14426 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14427 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14428 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14429 			if (!caller_holds_lock)
14430 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14431 		}
14432 		if (phyint->phyint_illv6) {
14433 			if (!caller_holds_lock)
14434 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14435 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14436 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14437 			if (!caller_holds_lock)
14438 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14439 		}
14440 		phyint = phyint->phyint_ipsq_next;
14441 	}
14442 }
14443 
14444 static ipsq_t *
14445 ipsq_create(char *groupname, ip_stack_t *ipst)
14446 {
14447 	ipsq_t	*ipsq;
14448 
14449 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14450 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14451 	if (ipsq == NULL) {
14452 		return (NULL);
14453 	}
14454 
14455 	if (groupname != NULL)
14456 		(void) strcpy(ipsq->ipsq_name, groupname);
14457 	else
14458 		ipsq->ipsq_name[0] = '\0';
14459 
14460 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14461 	ipsq->ipsq_flags |= IPSQ_GROUP;
14462 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14463 	ipst->ips_ipsq_g_head = ipsq;
14464 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14465 	return (ipsq);
14466 }
14467 
14468 /*
14469  * Return an ipsq correspoding to the groupname. If 'create' is true
14470  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14471  * uniquely with an IPMP group. However during IPMP groupname operations,
14472  * multiple IPMP groups may be associated with a single ipsq. But no
14473  * IPMP group can be associated with more than 1 ipsq at any time.
14474  * For example
14475  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14476  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14477  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14478  *
14479  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14480  * status shown below during the execution of the above command.
14481  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14482  *
14483  * After the completion of the above groupname command we return to the stable
14484  * state shown below.
14485  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14486  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14487  *
14488  * Because of the above, we don't search based on the ipsq_name since that
14489  * would miss the correct ipsq during certain windows as shown above.
14490  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14491  * natural state.
14492  */
14493 static ipsq_t *
14494 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14495     ip_stack_t *ipst)
14496 {
14497 	ipsq_t	*ipsq;
14498 	int	group_len;
14499 	phyint_t *phyint;
14500 
14501 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14502 
14503 	group_len = strlen(groupname);
14504 	ASSERT(group_len != 0);
14505 	group_len++;
14506 
14507 	for (ipsq = ipst->ips_ipsq_g_head;
14508 	    ipsq != NULL;
14509 	    ipsq = ipsq->ipsq_next) {
14510 		/*
14511 		 * When an ipsq is being split, and ill_split_ipsq
14512 		 * calls this function, we exclude it from being considered.
14513 		 */
14514 		if (ipsq == exclude_ipsq)
14515 			continue;
14516 
14517 		/*
14518 		 * Compare against the ipsq_name. The groupname change happens
14519 		 * in 2 phases. The 1st phase merges the from group into
14520 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14521 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14522 		 * ipsq_name. At this point the phyint_groupname has not been
14523 		 * updated.
14524 		 */
14525 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14526 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14527 			/*
14528 			 * Verify that an ipmp groupname is exactly
14529 			 * part of 1 ipsq and is not found in any other
14530 			 * ipsq.
14531 			 */
14532 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14533 			    NULL);
14534 			return (ipsq);
14535 		}
14536 
14537 		/*
14538 		 * Comparison against ipsq_name alone is not sufficient.
14539 		 * In the case when groups are currently being
14540 		 * merged, the ipsq could hold other IPMP groups temporarily.
14541 		 * so we walk the phyint list and compare against the
14542 		 * phyint_groupname as well.
14543 		 */
14544 		phyint = ipsq->ipsq_phyint_list;
14545 		while (phyint != NULL) {
14546 			if ((group_len == phyint->phyint_groupname_len) &&
14547 			    (bcmp(phyint->phyint_groupname, groupname,
14548 			    group_len) == 0)) {
14549 				/*
14550 				 * Verify that an ipmp groupname is exactly
14551 				 * part of 1 ipsq and is not found in any other
14552 				 * ipsq.
14553 				 */
14554 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14555 				    ipst) == NULL);
14556 				return (ipsq);
14557 			}
14558 			phyint = phyint->phyint_ipsq_next;
14559 		}
14560 	}
14561 	if (create)
14562 		ipsq = ipsq_create(groupname, ipst);
14563 	return (ipsq);
14564 }
14565 
14566 static void
14567 ipsq_delete(ipsq_t *ipsq)
14568 {
14569 	ipsq_t *nipsq;
14570 	ipsq_t *pipsq = NULL;
14571 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14572 
14573 	/*
14574 	 * We don't hold the ipsq lock, but we are sure no new
14575 	 * messages can land up, since the ipsq_refs is zero.
14576 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14577 	 * is associated with this ipsq. (Lookups are based on ill_name
14578 	 * or phyint_groupname)
14579 	 */
14580 	ASSERT(ipsq->ipsq_refs == 0);
14581 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14582 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14583 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14584 		/*
14585 		 * This is not the ipsq of an IPMP group.
14586 		 */
14587 		ipsq->ipsq_ipst = NULL;
14588 		kmem_free(ipsq, sizeof (ipsq_t));
14589 		return;
14590 	}
14591 
14592 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14593 
14594 	/*
14595 	 * Locate the ipsq  before we can remove it from
14596 	 * the singly linked list of ipsq's.
14597 	 */
14598 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14599 	    nipsq = nipsq->ipsq_next) {
14600 		if (nipsq == ipsq) {
14601 			break;
14602 		}
14603 		pipsq = nipsq;
14604 	}
14605 
14606 	ASSERT(nipsq == ipsq);
14607 
14608 	/* unlink ipsq from the list */
14609 	if (pipsq != NULL)
14610 		pipsq->ipsq_next = ipsq->ipsq_next;
14611 	else
14612 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14613 	ipsq->ipsq_ipst = NULL;
14614 	kmem_free(ipsq, sizeof (ipsq_t));
14615 	rw_exit(&ipst->ips_ill_g_lock);
14616 }
14617 
14618 static void
14619 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14620     queue_t *q)
14621 {
14622 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14623 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14624 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14625 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14626 	ASSERT(current_mp != NULL);
14627 
14628 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14629 	    NEW_OP, NULL);
14630 
14631 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14632 	    new_ipsq->ipsq_xopq_mphead != NULL);
14633 
14634 	/*
14635 	 * move from old ipsq to the new ipsq.
14636 	 */
14637 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14638 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14639 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14640 
14641 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14642 }
14643 
14644 void
14645 ill_group_cleanup(ill_t *ill)
14646 {
14647 	ill_t *ill_v4;
14648 	ill_t *ill_v6;
14649 	ipif_t *ipif;
14650 
14651 	ill_v4 = ill->ill_phyint->phyint_illv4;
14652 	ill_v6 = ill->ill_phyint->phyint_illv6;
14653 
14654 	if (ill_v4 != NULL) {
14655 		mutex_enter(&ill_v4->ill_lock);
14656 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14657 		    ipif = ipif->ipif_next) {
14658 			IPIF_UNMARK_MOVING(ipif);
14659 		}
14660 		ill_v4->ill_up_ipifs = B_FALSE;
14661 		mutex_exit(&ill_v4->ill_lock);
14662 	}
14663 
14664 	if (ill_v6 != NULL) {
14665 		mutex_enter(&ill_v6->ill_lock);
14666 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14667 		    ipif = ipif->ipif_next) {
14668 			IPIF_UNMARK_MOVING(ipif);
14669 		}
14670 		ill_v6->ill_up_ipifs = B_FALSE;
14671 		mutex_exit(&ill_v6->ill_lock);
14672 	}
14673 }
14674 /*
14675  * This function is called when an ill has had a change in its group status
14676  * to bring up all the ipifs that were up before the change.
14677  */
14678 int
14679 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14680 {
14681 	ipif_t *ipif;
14682 	ill_t *ill_v4;
14683 	ill_t *ill_v6;
14684 	ill_t *from_ill;
14685 	int err = 0;
14686 
14687 
14688 	ASSERT(IAM_WRITER_ILL(ill));
14689 
14690 	/*
14691 	 * Except for ipif_state_flags and ill_state_flags the other
14692 	 * fields of the ipif/ill that are modified below are protected
14693 	 * implicitly since we are a writer. We would have tried to down
14694 	 * even an ipif that was already down, in ill_down_ipifs. So we
14695 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14696 	 */
14697 	ill_v4 = ill->ill_phyint->phyint_illv4;
14698 	ill_v6 = ill->ill_phyint->phyint_illv6;
14699 	if (ill_v4 != NULL) {
14700 		ill_v4->ill_up_ipifs = B_TRUE;
14701 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14702 		    ipif = ipif->ipif_next) {
14703 			mutex_enter(&ill_v4->ill_lock);
14704 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14705 			IPIF_UNMARK_MOVING(ipif);
14706 			mutex_exit(&ill_v4->ill_lock);
14707 			if (ipif->ipif_was_up) {
14708 				if (!(ipif->ipif_flags & IPIF_UP))
14709 					err = ipif_up(ipif, q, mp);
14710 				ipif->ipif_was_up = B_FALSE;
14711 				if (err != 0) {
14712 					/*
14713 					 * Can there be any other error ?
14714 					 */
14715 					ASSERT(err == EINPROGRESS);
14716 					return (err);
14717 				}
14718 			}
14719 		}
14720 		mutex_enter(&ill_v4->ill_lock);
14721 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14722 		mutex_exit(&ill_v4->ill_lock);
14723 		ill_v4->ill_up_ipifs = B_FALSE;
14724 		if (ill_v4->ill_move_in_progress) {
14725 			ASSERT(ill_v4->ill_move_peer != NULL);
14726 			ill_v4->ill_move_in_progress = B_FALSE;
14727 			from_ill = ill_v4->ill_move_peer;
14728 			from_ill->ill_move_in_progress = B_FALSE;
14729 			from_ill->ill_move_peer = NULL;
14730 			mutex_enter(&from_ill->ill_lock);
14731 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14732 			mutex_exit(&from_ill->ill_lock);
14733 			if (ill_v6 == NULL) {
14734 				if (from_ill->ill_phyint->phyint_flags &
14735 				    PHYI_STANDBY) {
14736 					phyint_inactive(from_ill->ill_phyint);
14737 				}
14738 				if (ill_v4->ill_phyint->phyint_flags &
14739 				    PHYI_STANDBY) {
14740 					phyint_inactive(ill_v4->ill_phyint);
14741 				}
14742 			}
14743 			ill_v4->ill_move_peer = NULL;
14744 		}
14745 	}
14746 
14747 	if (ill_v6 != NULL) {
14748 		ill_v6->ill_up_ipifs = B_TRUE;
14749 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14750 		    ipif = ipif->ipif_next) {
14751 			mutex_enter(&ill_v6->ill_lock);
14752 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14753 			IPIF_UNMARK_MOVING(ipif);
14754 			mutex_exit(&ill_v6->ill_lock);
14755 			if (ipif->ipif_was_up) {
14756 				if (!(ipif->ipif_flags & IPIF_UP))
14757 					err = ipif_up(ipif, q, mp);
14758 				ipif->ipif_was_up = B_FALSE;
14759 				if (err != 0) {
14760 					/*
14761 					 * Can there be any other error ?
14762 					 */
14763 					ASSERT(err == EINPROGRESS);
14764 					return (err);
14765 				}
14766 			}
14767 		}
14768 		mutex_enter(&ill_v6->ill_lock);
14769 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14770 		mutex_exit(&ill_v6->ill_lock);
14771 		ill_v6->ill_up_ipifs = B_FALSE;
14772 		if (ill_v6->ill_move_in_progress) {
14773 			ASSERT(ill_v6->ill_move_peer != NULL);
14774 			ill_v6->ill_move_in_progress = B_FALSE;
14775 			from_ill = ill_v6->ill_move_peer;
14776 			from_ill->ill_move_in_progress = B_FALSE;
14777 			from_ill->ill_move_peer = NULL;
14778 			mutex_enter(&from_ill->ill_lock);
14779 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14780 			mutex_exit(&from_ill->ill_lock);
14781 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14782 				phyint_inactive(from_ill->ill_phyint);
14783 			}
14784 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14785 				phyint_inactive(ill_v6->ill_phyint);
14786 			}
14787 			ill_v6->ill_move_peer = NULL;
14788 		}
14789 	}
14790 	return (0);
14791 }
14792 
14793 /*
14794  * bring down all the approriate ipifs.
14795  */
14796 /* ARGSUSED */
14797 static void
14798 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14799 {
14800 	ipif_t *ipif;
14801 
14802 	ASSERT(IAM_WRITER_ILL(ill));
14803 
14804 	/*
14805 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14806 	 * are modified below are protected implicitly since we are a writer
14807 	 */
14808 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14809 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14810 			continue;
14811 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14812 			/*
14813 			 * We go through the ipif_down logic even if the ipif
14814 			 * is already down, since routes can be added based
14815 			 * on down ipifs. Going through ipif_down once again
14816 			 * will delete any IREs created based on these routes.
14817 			 */
14818 			if (ipif->ipif_flags & IPIF_UP)
14819 				ipif->ipif_was_up = B_TRUE;
14820 			/*
14821 			 * If called with chk_nofailover true ipif is moving.
14822 			 */
14823 			mutex_enter(&ill->ill_lock);
14824 			if (chk_nofailover) {
14825 				ipif->ipif_state_flags |=
14826 				    IPIF_MOVING | IPIF_CHANGING;
14827 			} else {
14828 				ipif->ipif_state_flags |= IPIF_CHANGING;
14829 			}
14830 			mutex_exit(&ill->ill_lock);
14831 			/*
14832 			 * Need to re-create net/subnet bcast ires if
14833 			 * they are dependent on ipif.
14834 			 */
14835 			if (!ipif->ipif_isv6)
14836 				ipif_check_bcast_ires(ipif);
14837 			(void) ipif_logical_down(ipif, NULL, NULL);
14838 			ipif_non_duplicate(ipif);
14839 			ipif_down_tail(ipif);
14840 			/*
14841 			 * We don't do ipif_multicast_down for IPv4 in
14842 			 * ipif_down. We need to set this so that
14843 			 * ipif_multicast_up will join the
14844 			 * ALLHOSTS_GROUP on to_ill.
14845 			 */
14846 			ipif->ipif_multicast_up = B_FALSE;
14847 		}
14848 	}
14849 }
14850 
14851 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14852 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14853 	(ipsq)->ipsq_refs++;				\
14854 }
14855 
14856 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14857 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14858 	(ipsq)->ipsq_refs--;				\
14859 	if ((ipsq)->ipsq_refs == 0)				\
14860 		(ipsq)->ipsq_name[0] = '\0'; 		\
14861 }
14862 
14863 /*
14864  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14865  * new_ipsq.
14866  */
14867 static void
14868 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14869 {
14870 	phyint_t *phyint;
14871 	phyint_t *next_phyint;
14872 
14873 	/*
14874 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14875 	 * writer and the ill_lock of the ill in question. Also the dest
14876 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14877 	 */
14878 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14879 
14880 	phyint = cur_ipsq->ipsq_phyint_list;
14881 	cur_ipsq->ipsq_phyint_list = NULL;
14882 	while (phyint != NULL) {
14883 		next_phyint = phyint->phyint_ipsq_next;
14884 		IPSQ_DEC_REF(cur_ipsq, ipst);
14885 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14886 		new_ipsq->ipsq_phyint_list = phyint;
14887 		IPSQ_INC_REF(new_ipsq, ipst);
14888 		phyint->phyint_ipsq = new_ipsq;
14889 		phyint = next_phyint;
14890 	}
14891 }
14892 
14893 #define	SPLIT_SUCCESS		0
14894 #define	SPLIT_NOT_NEEDED	1
14895 #define	SPLIT_FAILED		2
14896 
14897 int
14898 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14899     ip_stack_t *ipst)
14900 {
14901 	ipsq_t *newipsq = NULL;
14902 
14903 	/*
14904 	 * Assertions denote pre-requisites for changing the ipsq of
14905 	 * a phyint
14906 	 */
14907 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14908 	/*
14909 	 * <ill-phyint> assocs can't change while ill_g_lock
14910 	 * is held as writer. See ill_phyint_reinit()
14911 	 */
14912 	ASSERT(phyint->phyint_illv4 == NULL ||
14913 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14914 	ASSERT(phyint->phyint_illv6 == NULL ||
14915 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14916 
14917 	if ((phyint->phyint_groupname_len !=
14918 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14919 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14920 	    phyint->phyint_groupname_len) != 0)) {
14921 		/*
14922 		 * Once we fail in creating a new ipsq due to memory shortage,
14923 		 * don't attempt to create new ipsq again, based on another
14924 		 * phyint, since we want all phyints belonging to an IPMP group
14925 		 * to be in the same ipsq even in the event of mem alloc fails.
14926 		 */
14927 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14928 		    cur_ipsq, ipst);
14929 		if (newipsq == NULL) {
14930 			/* Memory allocation failure */
14931 			return (SPLIT_FAILED);
14932 		} else {
14933 			/* ipsq_refs protected by ill_g_lock (writer) */
14934 			IPSQ_DEC_REF(cur_ipsq, ipst);
14935 			phyint->phyint_ipsq = newipsq;
14936 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14937 			newipsq->ipsq_phyint_list = phyint;
14938 			IPSQ_INC_REF(newipsq, ipst);
14939 			return (SPLIT_SUCCESS);
14940 		}
14941 	}
14942 	return (SPLIT_NOT_NEEDED);
14943 }
14944 
14945 /*
14946  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14947  * to do this split
14948  */
14949 static int
14950 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14951 {
14952 	ipsq_t *newipsq;
14953 
14954 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14955 	/*
14956 	 * <ill-phyint> assocs can't change while ill_g_lock
14957 	 * is held as writer. See ill_phyint_reinit()
14958 	 */
14959 
14960 	ASSERT(phyint->phyint_illv4 == NULL ||
14961 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14962 	ASSERT(phyint->phyint_illv6 == NULL ||
14963 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14964 
14965 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14966 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14967 		/*
14968 		 * ipsq_init failed due to no memory
14969 		 * caller will use the same ipsq
14970 		 */
14971 		return (SPLIT_FAILED);
14972 	}
14973 
14974 	/* ipsq_ref is protected by ill_g_lock (writer) */
14975 	IPSQ_DEC_REF(cur_ipsq, ipst);
14976 
14977 	/*
14978 	 * This is a new ipsq that is unknown to the world.
14979 	 * So we don't need to hold ipsq_lock,
14980 	 */
14981 	newipsq = phyint->phyint_ipsq;
14982 	newipsq->ipsq_writer = NULL;
14983 	newipsq->ipsq_reentry_cnt--;
14984 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14985 #ifdef ILL_DEBUG
14986 	newipsq->ipsq_depth = 0;
14987 #endif
14988 
14989 	return (SPLIT_SUCCESS);
14990 }
14991 
14992 /*
14993  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14994  * ipsq's representing their individual groups or themselves. Return
14995  * whether split needs to be retried again later.
14996  */
14997 static boolean_t
14998 ill_split_ipsq(ipsq_t *cur_ipsq)
14999 {
15000 	phyint_t *phyint;
15001 	phyint_t *next_phyint;
15002 	int	error;
15003 	boolean_t need_retry = B_FALSE;
15004 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
15005 
15006 	phyint = cur_ipsq->ipsq_phyint_list;
15007 	cur_ipsq->ipsq_phyint_list = NULL;
15008 	while (phyint != NULL) {
15009 		next_phyint = phyint->phyint_ipsq_next;
15010 		/*
15011 		 * 'created' will tell us whether the callee actually
15012 		 * created an ipsq. Lack of memory may force the callee
15013 		 * to return without creating an ipsq.
15014 		 */
15015 		if (phyint->phyint_groupname == NULL) {
15016 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
15017 		} else {
15018 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
15019 			    need_retry, ipst);
15020 		}
15021 
15022 		switch (error) {
15023 		case SPLIT_FAILED:
15024 			need_retry = B_TRUE;
15025 			/* FALLTHRU */
15026 		case SPLIT_NOT_NEEDED:
15027 			/*
15028 			 * Keep it on the list.
15029 			 */
15030 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
15031 			cur_ipsq->ipsq_phyint_list = phyint;
15032 			break;
15033 		case SPLIT_SUCCESS:
15034 			break;
15035 		default:
15036 			ASSERT(0);
15037 		}
15038 
15039 		phyint = next_phyint;
15040 	}
15041 	return (need_retry);
15042 }
15043 
15044 /*
15045  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
15046  * and return the ills in the list. This list will be
15047  * needed to unlock all the ills later on by the caller.
15048  * The <ill-ipsq> associations could change between the
15049  * lock and unlock. Hence the unlock can't traverse the
15050  * ipsq to get the list of ills.
15051  */
15052 static int
15053 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
15054 {
15055 	int	cnt = 0;
15056 	phyint_t	*phyint;
15057 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
15058 
15059 	/*
15060 	 * The caller holds ill_g_lock to ensure that the ill memberships
15061 	 * of the ipsq don't change
15062 	 */
15063 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
15064 
15065 	phyint = ipsq->ipsq_phyint_list;
15066 	while (phyint != NULL) {
15067 		if (phyint->phyint_illv4 != NULL) {
15068 			ASSERT(cnt < list_max);
15069 			list[cnt++] = phyint->phyint_illv4;
15070 		}
15071 		if (phyint->phyint_illv6 != NULL) {
15072 			ASSERT(cnt < list_max);
15073 			list[cnt++] = phyint->phyint_illv6;
15074 		}
15075 		phyint = phyint->phyint_ipsq_next;
15076 	}
15077 	ill_lock_ills(list, cnt);
15078 	return (cnt);
15079 }
15080 
15081 void
15082 ill_lock_ills(ill_t **list, int cnt)
15083 {
15084 	int	i;
15085 
15086 	if (cnt > 1) {
15087 		boolean_t try_again;
15088 		do {
15089 			try_again = B_FALSE;
15090 			for (i = 0; i < cnt - 1; i++) {
15091 				if (list[i] < list[i + 1]) {
15092 					ill_t	*tmp;
15093 
15094 					/* swap the elements */
15095 					tmp = list[i];
15096 					list[i] = list[i + 1];
15097 					list[i + 1] = tmp;
15098 					try_again = B_TRUE;
15099 				}
15100 			}
15101 		} while (try_again);
15102 	}
15103 
15104 	for (i = 0; i < cnt; i++) {
15105 		if (i == 0) {
15106 			if (list[i] != NULL)
15107 				mutex_enter(&list[i]->ill_lock);
15108 			else
15109 				return;
15110 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
15111 			mutex_enter(&list[i]->ill_lock);
15112 		}
15113 	}
15114 }
15115 
15116 void
15117 ill_unlock_ills(ill_t **list, int cnt)
15118 {
15119 	int	i;
15120 
15121 	for (i = 0; i < cnt; i++) {
15122 		if ((i == 0) && (list[i] != NULL)) {
15123 			mutex_exit(&list[i]->ill_lock);
15124 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
15125 			mutex_exit(&list[i]->ill_lock);
15126 		}
15127 	}
15128 }
15129 
15130 /*
15131  * Merge all the ills from 1 ipsq group into another ipsq group.
15132  * The source ipsq group is specified by the ipsq associated with
15133  * 'from_ill'. The destination ipsq group is specified by the ipsq
15134  * associated with 'to_ill' or 'groupname' respectively.
15135  * Note that ipsq itself does not have a reference count mechanism
15136  * and functions don't look up an ipsq and pass it around. Instead
15137  * functions pass around an ill or groupname, and the ipsq is looked
15138  * up from the ill or groupname and the required operation performed
15139  * atomically with the lookup on the ipsq.
15140  */
15141 static int
15142 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
15143     queue_t *q)
15144 {
15145 	ipsq_t *old_ipsq;
15146 	ipsq_t *new_ipsq;
15147 	ill_t	**ill_list;
15148 	int	cnt;
15149 	size_t	ill_list_size;
15150 	boolean_t became_writer_on_new_sq = B_FALSE;
15151 	ip_stack_t	*ipst = from_ill->ill_ipst;
15152 
15153 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
15154 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
15155 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
15156 
15157 	/*
15158 	 * Need to hold ill_g_lock as writer and also the ill_lock to
15159 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
15160 	 * ipsq_lock to prevent new messages from landing on an ipsq.
15161 	 */
15162 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15163 
15164 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
15165 	if (groupname != NULL)
15166 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
15167 	else {
15168 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
15169 	}
15170 
15171 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
15172 
15173 	/*
15174 	 * both groups are on the same ipsq.
15175 	 */
15176 	if (old_ipsq == new_ipsq) {
15177 		rw_exit(&ipst->ips_ill_g_lock);
15178 		return (0);
15179 	}
15180 
15181 	cnt = old_ipsq->ipsq_refs << 1;
15182 	ill_list_size = cnt * sizeof (ill_t *);
15183 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
15184 	if (ill_list == NULL) {
15185 		rw_exit(&ipst->ips_ill_g_lock);
15186 		return (ENOMEM);
15187 	}
15188 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
15189 
15190 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
15191 	mutex_enter(&new_ipsq->ipsq_lock);
15192 	if ((new_ipsq->ipsq_writer == NULL &&
15193 	    new_ipsq->ipsq_current_ipif == NULL) ||
15194 	    (new_ipsq->ipsq_writer == curthread)) {
15195 		new_ipsq->ipsq_writer = curthread;
15196 		new_ipsq->ipsq_reentry_cnt++;
15197 		became_writer_on_new_sq = B_TRUE;
15198 	}
15199 
15200 	/*
15201 	 * We are holding ill_g_lock as writer and all the ill locks of
15202 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
15203 	 * message can land up on the old ipsq even though we don't hold the
15204 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
15205 	 */
15206 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
15207 
15208 	/*
15209 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
15210 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
15211 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
15212 	 */
15213 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
15214 
15215 	/*
15216 	 * Mark the new ipsq as needing a split since it is currently
15217 	 * being shared by more than 1 IPMP group. The split will
15218 	 * occur at the end of ipsq_exit
15219 	 */
15220 	new_ipsq->ipsq_split = B_TRUE;
15221 
15222 	/* Now release all the locks */
15223 	mutex_exit(&new_ipsq->ipsq_lock);
15224 	ill_unlock_ills(ill_list, cnt);
15225 	rw_exit(&ipst->ips_ill_g_lock);
15226 
15227 	kmem_free(ill_list, ill_list_size);
15228 
15229 	/*
15230 	 * If we succeeded in becoming writer on the new ipsq, then
15231 	 * drain the new ipsq and start processing  all enqueued messages
15232 	 * including the current ioctl we are processing which is either
15233 	 * a set groupname or failover/failback.
15234 	 */
15235 	if (became_writer_on_new_sq)
15236 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
15237 
15238 	/*
15239 	 * syncq has been changed and all the messages have been moved.
15240 	 */
15241 	mutex_enter(&old_ipsq->ipsq_lock);
15242 	old_ipsq->ipsq_current_ipif = NULL;
15243 	old_ipsq->ipsq_current_ioctl = 0;
15244 	mutex_exit(&old_ipsq->ipsq_lock);
15245 	return (EINPROGRESS);
15246 }
15247 
15248 /*
15249  * Delete and add the loopback copy and non-loopback copy of
15250  * the BROADCAST ire corresponding to ill and addr. Used to
15251  * group broadcast ires together when ill becomes part of
15252  * a group.
15253  *
15254  * This function is also called when ill is leaving the group
15255  * so that the ires belonging to the group gets re-grouped.
15256  */
15257 static void
15258 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
15259 {
15260 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
15261 	ire_t **ire_ptpn = &ire_head;
15262 	ip_stack_t	*ipst = ill->ill_ipst;
15263 
15264 	/*
15265 	 * The loopback and non-loopback IREs are inserted in the order in which
15266 	 * they're found, on the basis that they are correctly ordered (loopback
15267 	 * first).
15268 	 */
15269 	for (;;) {
15270 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15271 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15272 		if (ire == NULL)
15273 			break;
15274 
15275 		/*
15276 		 * we are passing in KM_SLEEP because it is not easy to
15277 		 * go back to a sane state in case of memory failure.
15278 		 */
15279 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
15280 		ASSERT(nire != NULL);
15281 		bzero(nire, sizeof (ire_t));
15282 		/*
15283 		 * Don't use ire_max_frag directly since we don't
15284 		 * hold on to 'ire' until we add the new ire 'nire' and
15285 		 * we don't want the new ire to have a dangling reference
15286 		 * to 'ire'. The ire_max_frag of a broadcast ire must
15287 		 * be in sync with the ipif_mtu of the associate ipif.
15288 		 * For eg. this happens as a result of SIOCSLIFNAME,
15289 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
15290 		 * the driver. A change in ire_max_frag triggered as
15291 		 * as a result of path mtu discovery, or due to an
15292 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
15293 		 * route change -mtu command does not apply to broadcast ires.
15294 		 *
15295 		 * XXX We need a recovery strategy here if ire_init fails
15296 		 */
15297 		if (ire_init(nire,
15298 		    (uchar_t *)&ire->ire_addr,
15299 		    (uchar_t *)&ire->ire_mask,
15300 		    (uchar_t *)&ire->ire_src_addr,
15301 		    (uchar_t *)&ire->ire_gateway_addr,
15302 		    (uchar_t *)&ire->ire_in_src_addr,
15303 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
15304 		    &ire->ire_ipif->ipif_mtu,
15305 		    ire->ire_nce,
15306 		    ire->ire_rfq,
15307 		    ire->ire_stq,
15308 		    ire->ire_type,
15309 		    ire->ire_ipif,
15310 		    ire->ire_in_ill,
15311 		    ire->ire_cmask,
15312 		    ire->ire_phandle,
15313 		    ire->ire_ihandle,
15314 		    ire->ire_flags,
15315 		    &ire->ire_uinfo,
15316 		    NULL,
15317 		    NULL,
15318 		    ipst) == NULL) {
15319 			cmn_err(CE_PANIC, "ire_init() failed");
15320 		}
15321 		ire_delete(ire);
15322 		ire_refrele(ire);
15323 
15324 		/*
15325 		 * The newly created IREs are inserted at the tail of the list
15326 		 * starting with ire_head. As we've just allocated them no one
15327 		 * knows about them so it's safe.
15328 		 */
15329 		*ire_ptpn = nire;
15330 		ire_ptpn = &nire->ire_next;
15331 	}
15332 
15333 	for (nire = ire_head; nire != NULL; nire = nire_next) {
15334 		int error;
15335 		ire_t *oire;
15336 		/* unlink the IRE from our list before calling ire_add() */
15337 		nire_next = nire->ire_next;
15338 		nire->ire_next = NULL;
15339 
15340 		/* ire_add adds the ire at the right place in the list */
15341 		oire = nire;
15342 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
15343 		ASSERT(error == 0);
15344 		ASSERT(oire == nire);
15345 		ire_refrele(nire);	/* Held in ire_add */
15346 	}
15347 }
15348 
15349 /*
15350  * This function is usually called when an ill is inserted in
15351  * a group and all the ipifs are already UP. As all the ipifs
15352  * are already UP, the broadcast ires have already been created
15353  * and been inserted. But, ire_add_v4 would not have grouped properly.
15354  * We need to re-group for the benefit of ip_wput_ire which
15355  * expects BROADCAST ires to be grouped properly to avoid sending
15356  * more than one copy of the broadcast packet per group.
15357  *
15358  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
15359  *	  because when ipif_up_done ends up calling this, ires have
15360  *        already been added before illgrp_insert i.e before ill_group
15361  *	  has been initialized.
15362  */
15363 static void
15364 ill_group_bcast_for_xmit(ill_t *ill)
15365 {
15366 	ill_group_t *illgrp;
15367 	ipif_t *ipif;
15368 	ipaddr_t addr;
15369 	ipaddr_t net_mask;
15370 	ipaddr_t subnet_netmask;
15371 
15372 	illgrp = ill->ill_group;
15373 
15374 	/*
15375 	 * This function is called even when an ill is deleted from
15376 	 * the group. Hence, illgrp could be null.
15377 	 */
15378 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15379 		return;
15380 
15381 	/*
15382 	 * Delete all the BROADCAST ires matching this ill and add
15383 	 * them back. This time, ire_add_v4 should take care of
15384 	 * grouping them with others because ill is part of the
15385 	 * group.
15386 	 */
15387 	ill_bcast_delete_and_add(ill, 0);
15388 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15389 
15390 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15391 
15392 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15393 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15394 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15395 		} else {
15396 			net_mask = htonl(IN_CLASSA_NET);
15397 		}
15398 		addr = net_mask & ipif->ipif_subnet;
15399 		ill_bcast_delete_and_add(ill, addr);
15400 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15401 
15402 		subnet_netmask = ipif->ipif_net_mask;
15403 		addr = ipif->ipif_subnet;
15404 		ill_bcast_delete_and_add(ill, addr);
15405 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15406 	}
15407 }
15408 
15409 /*
15410  * This function is called from illgrp_delete when ill is being deleted
15411  * from the group.
15412  *
15413  * As ill is not there in the group anymore, any address belonging
15414  * to this ill should be cleared of IRE_MARK_NORECV.
15415  */
15416 static void
15417 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15418 {
15419 	ire_t *ire;
15420 	irb_t *irb;
15421 	ip_stack_t	*ipst = ill->ill_ipst;
15422 
15423 	ASSERT(ill->ill_group == NULL);
15424 
15425 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15426 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15427 
15428 	if (ire != NULL) {
15429 		/*
15430 		 * IPMP and plumbing operations are serialized on the ipsq, so
15431 		 * no one will insert or delete a broadcast ire under our feet.
15432 		 */
15433 		irb = ire->ire_bucket;
15434 		rw_enter(&irb->irb_lock, RW_READER);
15435 		ire_refrele(ire);
15436 
15437 		for (; ire != NULL; ire = ire->ire_next) {
15438 			if (ire->ire_addr != addr)
15439 				break;
15440 			if (ire_to_ill(ire) != ill)
15441 				continue;
15442 
15443 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15444 			ire->ire_marks &= ~IRE_MARK_NORECV;
15445 		}
15446 		rw_exit(&irb->irb_lock);
15447 	}
15448 }
15449 
15450 /*
15451  * This function must be called only after the broadcast ires
15452  * have been grouped together. For a given address addr, nominate
15453  * only one of the ires whose interface is not FAILED or OFFLINE.
15454  *
15455  * This is also called when an ipif goes down, so that we can nominate
15456  * a different ire with the same address for receiving.
15457  */
15458 static void
15459 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15460 {
15461 	irb_t *irb;
15462 	ire_t *ire;
15463 	ire_t *ire1;
15464 	ire_t *save_ire;
15465 	ire_t **irep = NULL;
15466 	boolean_t first = B_TRUE;
15467 	ire_t *clear_ire = NULL;
15468 	ire_t *start_ire = NULL;
15469 	ire_t	*new_lb_ire;
15470 	ire_t	*new_nlb_ire;
15471 	boolean_t new_lb_ire_used = B_FALSE;
15472 	boolean_t new_nlb_ire_used = B_FALSE;
15473 	uint64_t match_flags;
15474 	uint64_t phyi_flags;
15475 	boolean_t fallback = B_FALSE;
15476 	uint_t	max_frag;
15477 
15478 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15479 	    NULL, MATCH_IRE_TYPE, ipst);
15480 	/*
15481 	 * We may not be able to find some ires if a previous
15482 	 * ire_create failed. This happens when an ipif goes
15483 	 * down and we are unable to create BROADCAST ires due
15484 	 * to memory failure. Thus, we have to check for NULL
15485 	 * below. This should handle the case for LOOPBACK,
15486 	 * POINTOPOINT and interfaces with some POINTOPOINT
15487 	 * logicals for which there are no BROADCAST ires.
15488 	 */
15489 	if (ire == NULL)
15490 		return;
15491 	/*
15492 	 * Currently IRE_BROADCASTS are deleted when an ipif
15493 	 * goes down which runs exclusively. Thus, setting
15494 	 * IRE_MARK_RCVD should not race with ire_delete marking
15495 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15496 	 * be consistent with other parts of the code that walks
15497 	 * a given bucket.
15498 	 */
15499 	save_ire = ire;
15500 	irb = ire->ire_bucket;
15501 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15502 	if (new_lb_ire == NULL) {
15503 		ire_refrele(ire);
15504 		return;
15505 	}
15506 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15507 	if (new_nlb_ire == NULL) {
15508 		ire_refrele(ire);
15509 		kmem_cache_free(ire_cache, new_lb_ire);
15510 		return;
15511 	}
15512 	IRB_REFHOLD(irb);
15513 	rw_enter(&irb->irb_lock, RW_WRITER);
15514 	/*
15515 	 * Get to the first ire matching the address and the
15516 	 * group. If the address does not match we are done
15517 	 * as we could not find the IRE. If the address matches
15518 	 * we should get to the first one matching the group.
15519 	 */
15520 	while (ire != NULL) {
15521 		if (ire->ire_addr != addr ||
15522 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15523 			break;
15524 		}
15525 		ire = ire->ire_next;
15526 	}
15527 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15528 	start_ire = ire;
15529 redo:
15530 	while (ire != NULL && ire->ire_addr == addr &&
15531 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15532 		/*
15533 		 * The first ire for any address within a group
15534 		 * should always be the one with IRE_MARK_NORECV cleared
15535 		 * so that ip_wput_ire can avoid searching for one.
15536 		 * Note down the insertion point which will be used
15537 		 * later.
15538 		 */
15539 		if (first && (irep == NULL))
15540 			irep = ire->ire_ptpn;
15541 		/*
15542 		 * PHYI_FAILED is set when the interface fails.
15543 		 * This interface might have become good, but the
15544 		 * daemon has not yet detected. We should still
15545 		 * not receive on this. PHYI_OFFLINE should never
15546 		 * be picked as this has been offlined and soon
15547 		 * be removed.
15548 		 */
15549 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15550 		if (phyi_flags & PHYI_OFFLINE) {
15551 			ire->ire_marks |= IRE_MARK_NORECV;
15552 			ire = ire->ire_next;
15553 			continue;
15554 		}
15555 		if (phyi_flags & match_flags) {
15556 			ire->ire_marks |= IRE_MARK_NORECV;
15557 			ire = ire->ire_next;
15558 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15559 			    PHYI_INACTIVE) {
15560 				fallback = B_TRUE;
15561 			}
15562 			continue;
15563 		}
15564 		if (first) {
15565 			/*
15566 			 * We will move this to the front of the list later
15567 			 * on.
15568 			 */
15569 			clear_ire = ire;
15570 			ire->ire_marks &= ~IRE_MARK_NORECV;
15571 		} else {
15572 			ire->ire_marks |= IRE_MARK_NORECV;
15573 		}
15574 		first = B_FALSE;
15575 		ire = ire->ire_next;
15576 	}
15577 	/*
15578 	 * If we never nominated anybody, try nominating at least
15579 	 * an INACTIVE, if we found one. Do it only once though.
15580 	 */
15581 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15582 	    fallback) {
15583 		match_flags = PHYI_FAILED;
15584 		ire = start_ire;
15585 		irep = NULL;
15586 		goto redo;
15587 	}
15588 	ire_refrele(save_ire);
15589 
15590 	/*
15591 	 * irep non-NULL indicates that we entered the while loop
15592 	 * above. If clear_ire is at the insertion point, we don't
15593 	 * have to do anything. clear_ire will be NULL if all the
15594 	 * interfaces are failed.
15595 	 *
15596 	 * We cannot unlink and reinsert the ire at the right place
15597 	 * in the list since there can be other walkers of this bucket.
15598 	 * Instead we delete and recreate the ire
15599 	 */
15600 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15601 		ire_t *clear_ire_stq = NULL;
15602 
15603 		bzero(new_lb_ire, sizeof (ire_t));
15604 		/* XXX We need a recovery strategy here. */
15605 		if (ire_init(new_lb_ire,
15606 		    (uchar_t *)&clear_ire->ire_addr,
15607 		    (uchar_t *)&clear_ire->ire_mask,
15608 		    (uchar_t *)&clear_ire->ire_src_addr,
15609 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15610 		    (uchar_t *)&clear_ire->ire_in_src_addr,
15611 		    &clear_ire->ire_max_frag,
15612 		    NULL, /* let ire_nce_init derive the resolver info */
15613 		    clear_ire->ire_rfq,
15614 		    clear_ire->ire_stq,
15615 		    clear_ire->ire_type,
15616 		    clear_ire->ire_ipif,
15617 		    clear_ire->ire_in_ill,
15618 		    clear_ire->ire_cmask,
15619 		    clear_ire->ire_phandle,
15620 		    clear_ire->ire_ihandle,
15621 		    clear_ire->ire_flags,
15622 		    &clear_ire->ire_uinfo,
15623 		    NULL,
15624 		    NULL,
15625 		    ipst) == NULL)
15626 			cmn_err(CE_PANIC, "ire_init() failed");
15627 		if (clear_ire->ire_stq == NULL) {
15628 			ire_t *ire_next = clear_ire->ire_next;
15629 			if (ire_next != NULL &&
15630 			    ire_next->ire_stq != NULL &&
15631 			    ire_next->ire_addr == clear_ire->ire_addr &&
15632 			    ire_next->ire_ipif->ipif_ill ==
15633 			    clear_ire->ire_ipif->ipif_ill) {
15634 				clear_ire_stq = ire_next;
15635 
15636 				bzero(new_nlb_ire, sizeof (ire_t));
15637 				/* XXX We need a recovery strategy here. */
15638 				if (ire_init(new_nlb_ire,
15639 				    (uchar_t *)&clear_ire_stq->ire_addr,
15640 				    (uchar_t *)&clear_ire_stq->ire_mask,
15641 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15642 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15643 				    (uchar_t *)&clear_ire_stq->ire_in_src_addr,
15644 				    &clear_ire_stq->ire_max_frag,
15645 				    NULL,
15646 				    clear_ire_stq->ire_rfq,
15647 				    clear_ire_stq->ire_stq,
15648 				    clear_ire_stq->ire_type,
15649 				    clear_ire_stq->ire_ipif,
15650 				    clear_ire_stq->ire_in_ill,
15651 				    clear_ire_stq->ire_cmask,
15652 				    clear_ire_stq->ire_phandle,
15653 				    clear_ire_stq->ire_ihandle,
15654 				    clear_ire_stq->ire_flags,
15655 				    &clear_ire_stq->ire_uinfo,
15656 				    NULL,
15657 				    NULL,
15658 				    ipst) == NULL)
15659 					cmn_err(CE_PANIC, "ire_init() failed");
15660 			}
15661 		}
15662 
15663 		/*
15664 		 * Delete the ire. We can't call ire_delete() since
15665 		 * we are holding the bucket lock. We can't release the
15666 		 * bucket lock since we can't allow irep to change. So just
15667 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15668 		 * ire from the list and do the refrele.
15669 		 */
15670 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15671 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15672 
15673 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15674 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15675 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15676 		}
15677 
15678 		/*
15679 		 * Also take care of otherfields like ib/ob pkt count
15680 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15681 		 */
15682 
15683 		/* Set the max_frag before adding the ire */
15684 		max_frag = *new_lb_ire->ire_max_fragp;
15685 		new_lb_ire->ire_max_fragp = NULL;
15686 		new_lb_ire->ire_max_frag = max_frag;
15687 
15688 		/* Add the new ire's. Insert at *irep */
15689 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15690 		ire1 = *irep;
15691 		if (ire1 != NULL)
15692 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15693 		new_lb_ire->ire_next = ire1;
15694 		/* Link the new one in. */
15695 		new_lb_ire->ire_ptpn = irep;
15696 		membar_producer();
15697 		*irep = new_lb_ire;
15698 		new_lb_ire_used = B_TRUE;
15699 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15700 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15701 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15702 
15703 		if (clear_ire_stq != NULL) {
15704 			/* Set the max_frag before adding the ire */
15705 			max_frag = *new_nlb_ire->ire_max_fragp;
15706 			new_nlb_ire->ire_max_fragp = NULL;
15707 			new_nlb_ire->ire_max_frag = max_frag;
15708 
15709 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15710 			irep = &new_lb_ire->ire_next;
15711 			/* Add the new ire. Insert at *irep */
15712 			ire1 = *irep;
15713 			if (ire1 != NULL)
15714 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15715 			new_nlb_ire->ire_next = ire1;
15716 			/* Link the new one in. */
15717 			new_nlb_ire->ire_ptpn = irep;
15718 			membar_producer();
15719 			*irep = new_nlb_ire;
15720 			new_nlb_ire_used = B_TRUE;
15721 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15722 			    ire_stats_inserted);
15723 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15724 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15725 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
15726 		}
15727 	}
15728 	rw_exit(&irb->irb_lock);
15729 	if (!new_lb_ire_used)
15730 		kmem_cache_free(ire_cache, new_lb_ire);
15731 	if (!new_nlb_ire_used)
15732 		kmem_cache_free(ire_cache, new_nlb_ire);
15733 	IRB_REFRELE(irb);
15734 }
15735 
15736 /*
15737  * Whenever an ipif goes down we have to renominate a different
15738  * broadcast ire to receive. Whenever an ipif comes up, we need
15739  * to make sure that we have only one nominated to receive.
15740  */
15741 static void
15742 ipif_renominate_bcast(ipif_t *ipif)
15743 {
15744 	ill_t *ill = ipif->ipif_ill;
15745 	ipaddr_t subnet_addr;
15746 	ipaddr_t net_addr;
15747 	ipaddr_t net_mask = 0;
15748 	ipaddr_t subnet_netmask;
15749 	ipaddr_t addr;
15750 	ill_group_t *illgrp;
15751 	ip_stack_t	*ipst = ill->ill_ipst;
15752 
15753 	illgrp = ill->ill_group;
15754 	/*
15755 	 * If this is the last ipif going down, it might take
15756 	 * the ill out of the group. In that case ipif_down ->
15757 	 * illgrp_delete takes care of doing the nomination.
15758 	 * ipif_down does not call for this case.
15759 	 */
15760 	ASSERT(illgrp != NULL);
15761 
15762 	/* There could not have been any ires associated with this */
15763 	if (ipif->ipif_subnet == 0)
15764 		return;
15765 
15766 	ill_mark_bcast(illgrp, 0, ipst);
15767 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15768 
15769 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15770 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15771 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15772 	} else {
15773 		net_mask = htonl(IN_CLASSA_NET);
15774 	}
15775 	addr = net_mask & ipif->ipif_subnet;
15776 	ill_mark_bcast(illgrp, addr, ipst);
15777 
15778 	net_addr = ~net_mask | addr;
15779 	ill_mark_bcast(illgrp, net_addr, ipst);
15780 
15781 	subnet_netmask = ipif->ipif_net_mask;
15782 	addr = ipif->ipif_subnet;
15783 	ill_mark_bcast(illgrp, addr, ipst);
15784 
15785 	subnet_addr = ~subnet_netmask | addr;
15786 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15787 }
15788 
15789 /*
15790  * Whenever we form or delete ill groups, we need to nominate one set of
15791  * BROADCAST ires for receiving in the group.
15792  *
15793  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15794  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15795  *    for ill_ipif_up_count to be non-zero. This is the only case where
15796  *    ill_ipif_up_count is zero and we would still find the ires.
15797  *
15798  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15799  *    ipif is UP and we just have to do the nomination.
15800  *
15801  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15802  *    from the group. So, we have to do the nomination.
15803  *
15804  * Because of (3), there could be just one ill in the group. But we have
15805  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15806  * Thus, this function does not optimize when there is only one ill as
15807  * it is not correct for (3).
15808  */
15809 static void
15810 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15811 {
15812 	ill_t *ill;
15813 	ipif_t *ipif;
15814 	ipaddr_t subnet_addr;
15815 	ipaddr_t prev_subnet_addr = 0;
15816 	ipaddr_t net_addr;
15817 	ipaddr_t prev_net_addr = 0;
15818 	ipaddr_t net_mask = 0;
15819 	ipaddr_t subnet_netmask;
15820 	ipaddr_t addr;
15821 	ip_stack_t	*ipst;
15822 
15823 	/*
15824 	 * When the last memeber is leaving, there is nothing to
15825 	 * nominate.
15826 	 */
15827 	if (illgrp->illgrp_ill_count == 0) {
15828 		ASSERT(illgrp->illgrp_ill == NULL);
15829 		return;
15830 	}
15831 
15832 	ill = illgrp->illgrp_ill;
15833 	ASSERT(!ill->ill_isv6);
15834 	ipst = ill->ill_ipst;
15835 	/*
15836 	 * We assume that ires with same address and belonging to the
15837 	 * same group, has been grouped together. Nominating a *single*
15838 	 * ill in the group for sending and receiving broadcast is done
15839 	 * by making sure that the first BROADCAST ire (which will be
15840 	 * the one returned by ire_ctable_lookup for ip_rput and the
15841 	 * one that will be used in ip_wput_ire) will be the one that
15842 	 * will not have IRE_MARK_NORECV set.
15843 	 *
15844 	 * 1) ip_rput checks and discards packets received on ires marked
15845 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15846 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15847 	 *    first ire in the group for every broadcast address in the group.
15848 	 *    ip_rput will accept packets only on the first ire i.e only
15849 	 *    one copy of the ill.
15850 	 *
15851 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15852 	 *    packet for the whole group. It needs to send out on the ill
15853 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15854 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15855 	 *    the copy echoed back on other port where the ire is not marked
15856 	 *    with IRE_MARK_NORECV.
15857 	 *
15858 	 * Note that we just need to have the first IRE either loopback or
15859 	 * non-loopback (either of them may not exist if ire_create failed
15860 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15861 	 * always hit the first one and hence will always accept one copy.
15862 	 *
15863 	 * We have a broadcast ire per ill for all the unique prefixes
15864 	 * hosted on that ill. As we don't have a way of knowing the
15865 	 * unique prefixes on a given ill and hence in the whole group,
15866 	 * we just call ill_mark_bcast on all the prefixes that exist
15867 	 * in the group. For the common case of one prefix, the code
15868 	 * below optimizes by remebering the last address used for
15869 	 * markng. In the case of multiple prefixes, this will still
15870 	 * optimize depending the order of prefixes.
15871 	 *
15872 	 * The only unique address across the whole group is 0.0.0.0 and
15873 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15874 	 * the first ire in the bucket for receiving and disables the
15875 	 * others.
15876 	 */
15877 	ill_mark_bcast(illgrp, 0, ipst);
15878 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15879 	for (; ill != NULL; ill = ill->ill_group_next) {
15880 
15881 		for (ipif = ill->ill_ipif; ipif != NULL;
15882 		    ipif = ipif->ipif_next) {
15883 
15884 			if (!(ipif->ipif_flags & IPIF_UP) ||
15885 			    ipif->ipif_subnet == 0) {
15886 				continue;
15887 			}
15888 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15889 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15890 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15891 			} else {
15892 				net_mask = htonl(IN_CLASSA_NET);
15893 			}
15894 			addr = net_mask & ipif->ipif_subnet;
15895 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15896 				ill_mark_bcast(illgrp, addr, ipst);
15897 				net_addr = ~net_mask | addr;
15898 				ill_mark_bcast(illgrp, net_addr, ipst);
15899 			}
15900 			prev_net_addr = addr;
15901 
15902 			subnet_netmask = ipif->ipif_net_mask;
15903 			addr = ipif->ipif_subnet;
15904 			if (prev_subnet_addr == 0 ||
15905 			    prev_subnet_addr != addr) {
15906 				ill_mark_bcast(illgrp, addr, ipst);
15907 				subnet_addr = ~subnet_netmask | addr;
15908 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15909 			}
15910 			prev_subnet_addr = addr;
15911 		}
15912 	}
15913 }
15914 
15915 /*
15916  * This function is called while forming ill groups.
15917  *
15918  * Currently, we handle only allmulti groups. We want to join
15919  * allmulti on only one of the ills in the groups. In future,
15920  * when we have link aggregation, we may have to join normal
15921  * multicast groups on multiple ills as switch does inbound load
15922  * balancing. Following are the functions that calls this
15923  * function :
15924  *
15925  * 1) ill_recover_multicast : Interface is coming back UP.
15926  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15927  *    will call ill_recover_multicast to recover all the multicast
15928  *    groups. We need to make sure that only one member is joined
15929  *    in the ill group.
15930  *
15931  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15932  *    Somebody is joining allmulti. We need to make sure that only one
15933  *    member is joined in the group.
15934  *
15935  * 3) illgrp_insert : If allmulti has already joined, we need to make
15936  *    sure that only one member is joined in the group.
15937  *
15938  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15939  *    allmulti who we have nominated. We need to pick someother ill.
15940  *
15941  * 5) illgrp_delete : The ill we nominated is leaving the group,
15942  *    we need to pick a new ill to join the group.
15943  *
15944  * For (1), (2), (5) - we just have to check whether there is
15945  * a good ill joined in the group. If we could not find any ills
15946  * joined the group, we should join.
15947  *
15948  * For (4), the one that was nominated to receive, left the group.
15949  * There could be nobody joined in the group when this function is
15950  * called.
15951  *
15952  * For (3) - we need to explicitly check whether there are multiple
15953  * ills joined in the group.
15954  *
15955  * For simplicity, we don't differentiate any of the above cases. We
15956  * just leave the group if it is joined on any of them and join on
15957  * the first good ill.
15958  */
15959 int
15960 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15961 {
15962 	ilm_t *ilm;
15963 	ill_t *ill;
15964 	ill_t *fallback_inactive_ill = NULL;
15965 	ill_t *fallback_failed_ill = NULL;
15966 	int ret = 0;
15967 
15968 	/*
15969 	 * Leave the allmulti on all the ills and start fresh.
15970 	 */
15971 	for (ill = illgrp->illgrp_ill; ill != NULL;
15972 	    ill = ill->ill_group_next) {
15973 		if (ill->ill_join_allmulti)
15974 			(void) ip_leave_allmulti(ill->ill_ipif);
15975 	}
15976 
15977 	/*
15978 	 * Choose a good ill. Fallback to inactive or failed if
15979 	 * none available. We need to fallback to FAILED in the
15980 	 * case where we have 2 interfaces in a group - where
15981 	 * one of them is failed and another is a good one and
15982 	 * the good one (not marked inactive) is leaving the group.
15983 	 */
15984 	ret = 0;
15985 	for (ill = illgrp->illgrp_ill; ill != NULL;
15986 	    ill = ill->ill_group_next) {
15987 		/* Never pick an offline interface */
15988 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15989 			continue;
15990 
15991 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15992 			fallback_failed_ill = ill;
15993 			continue;
15994 		}
15995 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15996 			fallback_inactive_ill = ill;
15997 			continue;
15998 		}
15999 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16000 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16001 				ret = ip_join_allmulti(ill->ill_ipif);
16002 				/*
16003 				 * ip_join_allmulti can fail because of memory
16004 				 * failures. So, make sure we join at least
16005 				 * on one ill.
16006 				 */
16007 				if (ill->ill_join_allmulti)
16008 					return (0);
16009 			}
16010 		}
16011 	}
16012 	if (ret != 0) {
16013 		/*
16014 		 * If we tried nominating above and failed to do so,
16015 		 * return error. We might have tried multiple times.
16016 		 * But, return the latest error.
16017 		 */
16018 		return (ret);
16019 	}
16020 	if ((ill = fallback_inactive_ill) != NULL) {
16021 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16022 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16023 				ret = ip_join_allmulti(ill->ill_ipif);
16024 				return (ret);
16025 			}
16026 		}
16027 	} else if ((ill = fallback_failed_ill) != NULL) {
16028 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16029 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16030 				ret = ip_join_allmulti(ill->ill_ipif);
16031 				return (ret);
16032 			}
16033 		}
16034 	}
16035 	return (0);
16036 }
16037 
16038 /*
16039  * This function is called from illgrp_delete after it is
16040  * deleted from the group to reschedule responsibilities
16041  * to a different ill.
16042  */
16043 static void
16044 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
16045 {
16046 	ilm_t	*ilm;
16047 	ipif_t	*ipif;
16048 	ipaddr_t subnet_addr;
16049 	ipaddr_t net_addr;
16050 	ipaddr_t net_mask = 0;
16051 	ipaddr_t subnet_netmask;
16052 	ipaddr_t addr;
16053 	ip_stack_t *ipst = ill->ill_ipst;
16054 
16055 	ASSERT(ill->ill_group == NULL);
16056 	/*
16057 	 * Broadcast Responsibility:
16058 	 *
16059 	 * 1. If this ill has been nominated for receiving broadcast
16060 	 * packets, we need to find a new one. Before we find a new
16061 	 * one, we need to re-group the ires that are part of this new
16062 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
16063 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
16064 	 * thing for us.
16065 	 *
16066 	 * 2. If this ill was not nominated for receiving broadcast
16067 	 * packets, we need to clear the IRE_MARK_NORECV flag
16068 	 * so that we continue to send up broadcast packets.
16069 	 */
16070 	if (!ill->ill_isv6) {
16071 		/*
16072 		 * Case 1 above : No optimization here. Just redo the
16073 		 * nomination.
16074 		 */
16075 		ill_group_bcast_for_xmit(ill);
16076 		ill_nominate_bcast_rcv(illgrp);
16077 
16078 		/*
16079 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
16080 		 */
16081 		ill_clear_bcast_mark(ill, 0);
16082 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
16083 
16084 		for (ipif = ill->ill_ipif; ipif != NULL;
16085 		    ipif = ipif->ipif_next) {
16086 
16087 			if (!(ipif->ipif_flags & IPIF_UP) ||
16088 			    ipif->ipif_subnet == 0) {
16089 				continue;
16090 			}
16091 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
16092 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
16093 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
16094 			} else {
16095 				net_mask = htonl(IN_CLASSA_NET);
16096 			}
16097 			addr = net_mask & ipif->ipif_subnet;
16098 			ill_clear_bcast_mark(ill, addr);
16099 
16100 			net_addr = ~net_mask | addr;
16101 			ill_clear_bcast_mark(ill, net_addr);
16102 
16103 			subnet_netmask = ipif->ipif_net_mask;
16104 			addr = ipif->ipif_subnet;
16105 			ill_clear_bcast_mark(ill, addr);
16106 
16107 			subnet_addr = ~subnet_netmask | addr;
16108 			ill_clear_bcast_mark(ill, subnet_addr);
16109 		}
16110 	}
16111 
16112 	/*
16113 	 * Multicast Responsibility.
16114 	 *
16115 	 * If we have joined allmulti on this one, find a new member
16116 	 * in the group to join allmulti. As this ill is already part
16117 	 * of allmulti, we don't have to join on this one.
16118 	 *
16119 	 * If we have not joined allmulti on this one, there is no
16120 	 * responsibility to handoff. But we need to take new
16121 	 * responsibility i.e, join allmulti on this one if we need
16122 	 * to.
16123 	 */
16124 	if (ill->ill_join_allmulti) {
16125 		(void) ill_nominate_mcast_rcv(illgrp);
16126 	} else {
16127 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16128 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16129 				(void) ip_join_allmulti(ill->ill_ipif);
16130 				break;
16131 			}
16132 		}
16133 	}
16134 
16135 	/*
16136 	 * We intentionally do the flushing of IRE_CACHES only matching
16137 	 * on the ill and not on groups. Note that we are already deleted
16138 	 * from the group.
16139 	 *
16140 	 * This will make sure that all IRE_CACHES whose stq is pointing
16141 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
16142 	 * deleted and IRE_CACHES that are not pointing at this ill will
16143 	 * be left alone.
16144 	 */
16145 	if (ill->ill_isv6) {
16146 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
16147 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
16148 	} else {
16149 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
16150 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
16151 	}
16152 
16153 	/*
16154 	 * Some conn may have cached one of the IREs deleted above. By removing
16155 	 * the ire reference, we clean up the extra reference to the ill held in
16156 	 * ire->ire_stq.
16157 	 */
16158 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
16159 
16160 	/*
16161 	 * Re-do source address selection for all the members in the
16162 	 * group, if they borrowed source address from one of the ipifs
16163 	 * in this ill.
16164 	 */
16165 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
16166 		if (ill->ill_isv6) {
16167 			ipif_update_other_ipifs_v6(ipif, illgrp);
16168 		} else {
16169 			ipif_update_other_ipifs(ipif, illgrp);
16170 		}
16171 	}
16172 }
16173 
16174 /*
16175  * Delete the ill from the group. The caller makes sure that it is
16176  * in a group and it okay to delete from the group. So, we always
16177  * delete here.
16178  */
16179 static void
16180 illgrp_delete(ill_t *ill)
16181 {
16182 	ill_group_t *illgrp;
16183 	ill_group_t *tmpg;
16184 	ill_t *tmp_ill;
16185 	ip_stack_t	*ipst = ill->ill_ipst;
16186 
16187 	/*
16188 	 * Reset illgrp_ill_schednext if it was pointing at us.
16189 	 * We need to do this before we set ill_group to NULL.
16190 	 */
16191 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16192 	mutex_enter(&ill->ill_lock);
16193 
16194 	illgrp_reset_schednext(ill);
16195 
16196 	illgrp = ill->ill_group;
16197 
16198 	/* Delete the ill from illgrp. */
16199 	if (illgrp->illgrp_ill == ill) {
16200 		illgrp->illgrp_ill = ill->ill_group_next;
16201 	} else {
16202 		tmp_ill = illgrp->illgrp_ill;
16203 		while (tmp_ill->ill_group_next != ill) {
16204 			tmp_ill = tmp_ill->ill_group_next;
16205 			ASSERT(tmp_ill != NULL);
16206 		}
16207 		tmp_ill->ill_group_next = ill->ill_group_next;
16208 	}
16209 	ill->ill_group = NULL;
16210 	ill->ill_group_next = NULL;
16211 
16212 	illgrp->illgrp_ill_count--;
16213 	mutex_exit(&ill->ill_lock);
16214 	rw_exit(&ipst->ips_ill_g_lock);
16215 
16216 	/*
16217 	 * As this ill is leaving the group, we need to hand off
16218 	 * the responsibilities to the other ills in the group, if
16219 	 * this ill had some responsibilities.
16220 	 */
16221 
16222 	ill_handoff_responsibility(ill, illgrp);
16223 
16224 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16225 
16226 	if (illgrp->illgrp_ill_count == 0) {
16227 
16228 		ASSERT(illgrp->illgrp_ill == NULL);
16229 		if (ill->ill_isv6) {
16230 			if (illgrp == ipst->ips_illgrp_head_v6) {
16231 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
16232 			} else {
16233 				tmpg = ipst->ips_illgrp_head_v6;
16234 				while (tmpg->illgrp_next != illgrp) {
16235 					tmpg = tmpg->illgrp_next;
16236 					ASSERT(tmpg != NULL);
16237 				}
16238 				tmpg->illgrp_next = illgrp->illgrp_next;
16239 			}
16240 		} else {
16241 			if (illgrp == ipst->ips_illgrp_head_v4) {
16242 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
16243 			} else {
16244 				tmpg = ipst->ips_illgrp_head_v4;
16245 				while (tmpg->illgrp_next != illgrp) {
16246 					tmpg = tmpg->illgrp_next;
16247 					ASSERT(tmpg != NULL);
16248 				}
16249 				tmpg->illgrp_next = illgrp->illgrp_next;
16250 			}
16251 		}
16252 		mutex_destroy(&illgrp->illgrp_lock);
16253 		mi_free(illgrp);
16254 	}
16255 	rw_exit(&ipst->ips_ill_g_lock);
16256 
16257 	/*
16258 	 * Even though the ill is out of the group its not necessary
16259 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
16260 	 * We will split the ipsq when phyint_groupname is set to NULL.
16261 	 */
16262 
16263 	/*
16264 	 * Send a routing sockets message if we are deleting from
16265 	 * groups with names.
16266 	 */
16267 	if (ill->ill_phyint->phyint_groupname_len != 0)
16268 		ip_rts_ifmsg(ill->ill_ipif);
16269 }
16270 
16271 /*
16272  * Re-do source address selection. This is normally called when
16273  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
16274  * ipif comes up.
16275  */
16276 void
16277 ill_update_source_selection(ill_t *ill)
16278 {
16279 	ipif_t *ipif;
16280 
16281 	ASSERT(IAM_WRITER_ILL(ill));
16282 
16283 	if (ill->ill_group != NULL)
16284 		ill = ill->ill_group->illgrp_ill;
16285 
16286 	for (; ill != NULL; ill = ill->ill_group_next) {
16287 		for (ipif = ill->ill_ipif; ipif != NULL;
16288 		    ipif = ipif->ipif_next) {
16289 			if (ill->ill_isv6)
16290 				ipif_recreate_interface_routes_v6(NULL, ipif);
16291 			else
16292 				ipif_recreate_interface_routes(NULL, ipif);
16293 		}
16294 	}
16295 }
16296 
16297 /*
16298  * Insert ill in a group headed by illgrp_head. The caller can either
16299  * pass a groupname in which case we search for a group with the
16300  * same name to insert in or pass a group to insert in. This function
16301  * would only search groups with names.
16302  *
16303  * NOTE : The caller should make sure that there is at least one ipif
16304  *	  UP on this ill so that illgrp_scheduler can pick this ill
16305  *	  for outbound packets. If ill_ipif_up_count is zero, we have
16306  *	  already sent a DL_UNBIND to the driver and we don't want to
16307  *	  send anymore packets. We don't assert for ipif_up_count
16308  *	  to be greater than zero, because ipif_up_done wants to call
16309  *	  this function before bumping up the ipif_up_count. See
16310  *	  ipif_up_done() for details.
16311  */
16312 int
16313 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
16314     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
16315 {
16316 	ill_group_t *illgrp;
16317 	ill_t *prev_ill;
16318 	phyint_t *phyi;
16319 	ip_stack_t	*ipst = ill->ill_ipst;
16320 
16321 	ASSERT(ill->ill_group == NULL);
16322 
16323 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16324 	mutex_enter(&ill->ill_lock);
16325 
16326 	if (groupname != NULL) {
16327 		/*
16328 		 * Look for a group with a matching groupname to insert.
16329 		 */
16330 		for (illgrp = *illgrp_head; illgrp != NULL;
16331 		    illgrp = illgrp->illgrp_next) {
16332 
16333 			ill_t *tmp_ill;
16334 
16335 			/*
16336 			 * If we have an ill_group_t in the list which has
16337 			 * no ill_t assigned then we must be in the process of
16338 			 * removing this group. We skip this as illgrp_delete()
16339 			 * will remove it from the list.
16340 			 */
16341 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
16342 				ASSERT(illgrp->illgrp_ill_count == 0);
16343 				continue;
16344 			}
16345 
16346 			ASSERT(tmp_ill->ill_phyint != NULL);
16347 			phyi = tmp_ill->ill_phyint;
16348 			/*
16349 			 * Look at groups which has names only.
16350 			 */
16351 			if (phyi->phyint_groupname_len == 0)
16352 				continue;
16353 			/*
16354 			 * Names are stored in the phyint common to both
16355 			 * IPv4 and IPv6.
16356 			 */
16357 			if (mi_strcmp(phyi->phyint_groupname,
16358 			    groupname) == 0) {
16359 				break;
16360 			}
16361 		}
16362 	} else {
16363 		/*
16364 		 * If the caller passes in a NULL "grp_to_insert", we
16365 		 * allocate one below and insert this singleton.
16366 		 */
16367 		illgrp = grp_to_insert;
16368 	}
16369 
16370 	ill->ill_group_next = NULL;
16371 
16372 	if (illgrp == NULL) {
16373 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16374 		if (illgrp == NULL) {
16375 			return (ENOMEM);
16376 		}
16377 		illgrp->illgrp_next = *illgrp_head;
16378 		*illgrp_head = illgrp;
16379 		illgrp->illgrp_ill = ill;
16380 		illgrp->illgrp_ill_count = 1;
16381 		ill->ill_group = illgrp;
16382 		/*
16383 		 * Used in illgrp_scheduler to protect multiple threads
16384 		 * from traversing the list.
16385 		 */
16386 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16387 	} else {
16388 		ASSERT(ill->ill_net_type ==
16389 		    illgrp->illgrp_ill->ill_net_type);
16390 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16391 
16392 		/* Insert ill at tail of this group */
16393 		prev_ill = illgrp->illgrp_ill;
16394 		while (prev_ill->ill_group_next != NULL)
16395 			prev_ill = prev_ill->ill_group_next;
16396 		prev_ill->ill_group_next = ill;
16397 		ill->ill_group = illgrp;
16398 		illgrp->illgrp_ill_count++;
16399 		/*
16400 		 * Inherit group properties. Currently only forwarding
16401 		 * is the property we try to keep the same with all the
16402 		 * ills. When there are more, we will abstract this into
16403 		 * a function.
16404 		 */
16405 		ill->ill_flags &= ~ILLF_ROUTER;
16406 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16407 	}
16408 	mutex_exit(&ill->ill_lock);
16409 	rw_exit(&ipst->ips_ill_g_lock);
16410 
16411 	/*
16412 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16413 	 *    may be zero as it has not yet been bumped. But the ires
16414 	 *    have already been added. So, we do the nomination here
16415 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16416 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16417 	 *    ill_ipif_up_count here while nominating broadcast ires for
16418 	 *    receive.
16419 	 *
16420 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16421 	 *    to group them properly as ire_add() has already happened
16422 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16423 	 *    case, we need to do it here anyway.
16424 	 */
16425 	if (!ill->ill_isv6) {
16426 		ill_group_bcast_for_xmit(ill);
16427 		ill_nominate_bcast_rcv(illgrp);
16428 	}
16429 
16430 	if (!ipif_is_coming_up) {
16431 		/*
16432 		 * When ipif_up_done() calls this function, the multicast
16433 		 * groups have not been joined yet. So, there is no point in
16434 		 * nomination. ip_join_allmulti will handle groups when
16435 		 * ill_recover_multicast is called from ipif_up_done() later.
16436 		 */
16437 		(void) ill_nominate_mcast_rcv(illgrp);
16438 		/*
16439 		 * ipif_up_done calls ill_update_source_selection
16440 		 * anyway. Moreover, we don't want to re-create
16441 		 * interface routes while ipif_up_done() still has reference
16442 		 * to them. Refer to ipif_up_done() for more details.
16443 		 */
16444 		ill_update_source_selection(ill);
16445 	}
16446 
16447 	/*
16448 	 * Send a routing sockets message if we are inserting into
16449 	 * groups with names.
16450 	 */
16451 	if (groupname != NULL)
16452 		ip_rts_ifmsg(ill->ill_ipif);
16453 	return (0);
16454 }
16455 
16456 /*
16457  * Return the first phyint matching the groupname. There could
16458  * be more than one when there are ill groups.
16459  *
16460  * If 'usable' is set, then we exclude ones that are marked with any of
16461  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16462  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16463  * emulation of ipmp.
16464  */
16465 phyint_t *
16466 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16467 {
16468 	phyint_t *phyi;
16469 
16470 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16471 	/*
16472 	 * Group names are stored in the phyint - a common structure
16473 	 * to both IPv4 and IPv6.
16474 	 */
16475 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16476 	for (; phyi != NULL;
16477 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16478 	    phyi, AVL_AFTER)) {
16479 		if (phyi->phyint_groupname_len == 0)
16480 			continue;
16481 		/*
16482 		 * Skip the ones that should not be used since the callers
16483 		 * sometime use this for sending packets.
16484 		 */
16485 		if (usable && (phyi->phyint_flags &
16486 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16487 			continue;
16488 
16489 		ASSERT(phyi->phyint_groupname != NULL);
16490 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16491 			return (phyi);
16492 	}
16493 	return (NULL);
16494 }
16495 
16496 
16497 /*
16498  * Return the first usable phyint matching the group index. By 'usable'
16499  * we exclude ones that are marked ununsable with any of
16500  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16501  *
16502  * Used only for the ipmp/netinfo emulation of ipmp.
16503  */
16504 phyint_t *
16505 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16506 {
16507 	phyint_t *phyi;
16508 
16509 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16510 
16511 	if (!ipst->ips_ipmp_hook_emulation)
16512 		return (NULL);
16513 
16514 	/*
16515 	 * Group indicies are stored in the phyint - a common structure
16516 	 * to both IPv4 and IPv6.
16517 	 */
16518 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16519 	for (; phyi != NULL;
16520 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16521 	    phyi, AVL_AFTER)) {
16522 		/* Ignore the ones that do not have a group */
16523 		if (phyi->phyint_groupname_len == 0)
16524 			continue;
16525 
16526 		ASSERT(phyi->phyint_group_ifindex != 0);
16527 		/*
16528 		 * Skip the ones that should not be used since the callers
16529 		 * sometime use this for sending packets.
16530 		 */
16531 		if (phyi->phyint_flags &
16532 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16533 			continue;
16534 		if (phyi->phyint_group_ifindex == group_ifindex)
16535 			return (phyi);
16536 	}
16537 	return (NULL);
16538 }
16539 
16540 
16541 /*
16542  * MT notes on creation and deletion of IPMP groups
16543  *
16544  * Creation and deletion of IPMP groups introduce the need to merge or
16545  * split the associated serialization objects i.e the ipsq's. Normally all
16546  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16547  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16548  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16549  * is a need to change the <ill-ipsq> association and we have to operate on both
16550  * the source and destination IPMP groups. For eg. attempting to set the
16551  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16552  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16553  * source or destination IPMP group are mapped to a single ipsq for executing
16554  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16555  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16556  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16557  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16558  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16559  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16560  *
16561  * In the above example the ioctl handling code locates the current ipsq of hme0
16562  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16563  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16564  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16565  * the destination ipsq. If the destination ipsq is not busy, it also enters
16566  * the destination ipsq exclusively. Now the actual groupname setting operation
16567  * can proceed. If the destination ipsq is busy, the operation is enqueued
16568  * on the destination (merged) ipsq and will be handled in the unwind from
16569  * ipsq_exit.
16570  *
16571  * To prevent other threads accessing the ill while the group name change is
16572  * in progres, we bring down the ipifs which also removes the ill from the
16573  * group. The group is changed in phyint and when the first ipif on the ill
16574  * is brought up, the ill is inserted into the right IPMP group by
16575  * illgrp_insert.
16576  */
16577 /* ARGSUSED */
16578 int
16579 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16580     ip_ioctl_cmd_t *ipip, void *ifreq)
16581 {
16582 	int i;
16583 	char *tmp;
16584 	int namelen;
16585 	ill_t *ill = ipif->ipif_ill;
16586 	ill_t *ill_v4, *ill_v6;
16587 	int err = 0;
16588 	phyint_t *phyi;
16589 	phyint_t *phyi_tmp;
16590 	struct lifreq *lifr;
16591 	mblk_t	*mp1;
16592 	char *groupname;
16593 	ipsq_t *ipsq;
16594 	ip_stack_t	*ipst = ill->ill_ipst;
16595 
16596 	ASSERT(IAM_WRITER_IPIF(ipif));
16597 
16598 	/* Existance verified in ip_wput_nondata */
16599 	mp1 = mp->b_cont->b_cont;
16600 	lifr = (struct lifreq *)mp1->b_rptr;
16601 	groupname = lifr->lifr_groupname;
16602 
16603 	if (ipif->ipif_id != 0)
16604 		return (EINVAL);
16605 
16606 	phyi = ill->ill_phyint;
16607 	ASSERT(phyi != NULL);
16608 
16609 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16610 		return (EINVAL);
16611 
16612 	tmp = groupname;
16613 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16614 		;
16615 
16616 	if (i == LIFNAMSIZ) {
16617 		/* no null termination */
16618 		return (EINVAL);
16619 	}
16620 
16621 	/*
16622 	 * Calculate the namelen exclusive of the null
16623 	 * termination character.
16624 	 */
16625 	namelen = tmp - groupname;
16626 
16627 	ill_v4 = phyi->phyint_illv4;
16628 	ill_v6 = phyi->phyint_illv6;
16629 
16630 	/*
16631 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16632 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16633 	 * synchronization notes in ip.c
16634 	 */
16635 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16636 		return (EINVAL);
16637 	}
16638 
16639 	/*
16640 	 * mark the ill as changing.
16641 	 * this should queue all new requests on the syncq.
16642 	 */
16643 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16644 
16645 	if (ill_v4 != NULL)
16646 		ill_v4->ill_state_flags |= ILL_CHANGING;
16647 	if (ill_v6 != NULL)
16648 		ill_v6->ill_state_flags |= ILL_CHANGING;
16649 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16650 
16651 	if (namelen == 0) {
16652 		/*
16653 		 * Null string means remove this interface from the
16654 		 * existing group.
16655 		 */
16656 		if (phyi->phyint_groupname_len == 0) {
16657 			/*
16658 			 * Never was in a group.
16659 			 */
16660 			err = 0;
16661 			goto done;
16662 		}
16663 
16664 		/*
16665 		 * IPv4 or IPv6 may be temporarily out of the group when all
16666 		 * the ipifs are down. Thus, we need to check for ill_group to
16667 		 * be non-NULL.
16668 		 */
16669 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16670 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16671 			mutex_enter(&ill_v4->ill_lock);
16672 			if (!ill_is_quiescent(ill_v4)) {
16673 				/*
16674 				 * ipsq_pending_mp_add will not fail since
16675 				 * connp is NULL
16676 				 */
16677 				(void) ipsq_pending_mp_add(NULL,
16678 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16679 				mutex_exit(&ill_v4->ill_lock);
16680 				err = EINPROGRESS;
16681 				goto done;
16682 			}
16683 			mutex_exit(&ill_v4->ill_lock);
16684 		}
16685 
16686 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16687 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16688 			mutex_enter(&ill_v6->ill_lock);
16689 			if (!ill_is_quiescent(ill_v6)) {
16690 				(void) ipsq_pending_mp_add(NULL,
16691 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16692 				mutex_exit(&ill_v6->ill_lock);
16693 				err = EINPROGRESS;
16694 				goto done;
16695 			}
16696 			mutex_exit(&ill_v6->ill_lock);
16697 		}
16698 
16699 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16700 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16701 		mutex_enter(&phyi->phyint_lock);
16702 		ASSERT(phyi->phyint_groupname != NULL);
16703 		mi_free(phyi->phyint_groupname);
16704 		phyi->phyint_groupname = NULL;
16705 		phyi->phyint_groupname_len = 0;
16706 
16707 		/* Restore the ifindex used to be the per interface one */
16708 		phyi->phyint_group_ifindex = 0;
16709 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16710 		mutex_exit(&phyi->phyint_lock);
16711 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16712 		rw_exit(&ipst->ips_ill_g_lock);
16713 		err = ill_up_ipifs(ill, q, mp);
16714 
16715 		/*
16716 		 * set the split flag so that the ipsq can be split
16717 		 */
16718 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16719 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16720 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16721 
16722 	} else {
16723 		if (phyi->phyint_groupname_len != 0) {
16724 			ASSERT(phyi->phyint_groupname != NULL);
16725 			/* Are we inserting in the same group ? */
16726 			if (mi_strcmp(groupname,
16727 			    phyi->phyint_groupname) == 0) {
16728 				err = 0;
16729 				goto done;
16730 			}
16731 		}
16732 
16733 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16734 		/*
16735 		 * Merge ipsq for the group's.
16736 		 * This check is here as multiple groups/ills might be
16737 		 * sharing the same ipsq.
16738 		 * If we have to merege than the operation is restarted
16739 		 * on the new ipsq.
16740 		 */
16741 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16742 		if (phyi->phyint_ipsq != ipsq) {
16743 			rw_exit(&ipst->ips_ill_g_lock);
16744 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16745 			goto done;
16746 		}
16747 		/*
16748 		 * Running exclusive on new ipsq.
16749 		 */
16750 
16751 		ASSERT(ipsq != NULL);
16752 		ASSERT(ipsq->ipsq_writer == curthread);
16753 
16754 		/*
16755 		 * Check whether the ill_type and ill_net_type matches before
16756 		 * we allocate any memory so that the cleanup is easier.
16757 		 *
16758 		 * We can't group dissimilar ones as we can't load spread
16759 		 * packets across the group because of potential link-level
16760 		 * header differences.
16761 		 */
16762 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16763 		if (phyi_tmp != NULL) {
16764 			if ((ill_v4 != NULL &&
16765 			    phyi_tmp->phyint_illv4 != NULL) &&
16766 			    ((ill_v4->ill_net_type !=
16767 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16768 			    (ill_v4->ill_type !=
16769 			    phyi_tmp->phyint_illv4->ill_type))) {
16770 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16771 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16772 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16773 				rw_exit(&ipst->ips_ill_g_lock);
16774 				return (EINVAL);
16775 			}
16776 			if ((ill_v6 != NULL &&
16777 			    phyi_tmp->phyint_illv6 != NULL) &&
16778 			    ((ill_v6->ill_net_type !=
16779 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16780 			    (ill_v6->ill_type !=
16781 			    phyi_tmp->phyint_illv6->ill_type))) {
16782 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16783 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16784 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16785 				rw_exit(&ipst->ips_ill_g_lock);
16786 				return (EINVAL);
16787 			}
16788 		}
16789 
16790 		rw_exit(&ipst->ips_ill_g_lock);
16791 
16792 		/*
16793 		 * bring down all v4 ipifs.
16794 		 */
16795 		if (ill_v4 != NULL) {
16796 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16797 		}
16798 
16799 		/*
16800 		 * bring down all v6 ipifs.
16801 		 */
16802 		if (ill_v6 != NULL) {
16803 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16804 		}
16805 
16806 		/*
16807 		 * make sure all ipifs are down and there are no active
16808 		 * references. Call to ipsq_pending_mp_add will not fail
16809 		 * since connp is NULL.
16810 		 */
16811 		if (ill_v4 != NULL) {
16812 			mutex_enter(&ill_v4->ill_lock);
16813 			if (!ill_is_quiescent(ill_v4)) {
16814 				(void) ipsq_pending_mp_add(NULL,
16815 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16816 				mutex_exit(&ill_v4->ill_lock);
16817 				err = EINPROGRESS;
16818 				goto done;
16819 			}
16820 			mutex_exit(&ill_v4->ill_lock);
16821 		}
16822 
16823 		if (ill_v6 != NULL) {
16824 			mutex_enter(&ill_v6->ill_lock);
16825 			if (!ill_is_quiescent(ill_v6)) {
16826 				(void) ipsq_pending_mp_add(NULL,
16827 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16828 				mutex_exit(&ill_v6->ill_lock);
16829 				err = EINPROGRESS;
16830 				goto done;
16831 			}
16832 			mutex_exit(&ill_v6->ill_lock);
16833 		}
16834 
16835 		/*
16836 		 * allocate including space for null terminator
16837 		 * before we insert.
16838 		 */
16839 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16840 		if (tmp == NULL)
16841 			return (ENOMEM);
16842 
16843 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16844 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16845 		mutex_enter(&phyi->phyint_lock);
16846 		if (phyi->phyint_groupname_len != 0) {
16847 			ASSERT(phyi->phyint_groupname != NULL);
16848 			mi_free(phyi->phyint_groupname);
16849 		}
16850 
16851 		/*
16852 		 * setup the new group name.
16853 		 */
16854 		phyi->phyint_groupname = tmp;
16855 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16856 		phyi->phyint_groupname_len = namelen + 1;
16857 
16858 		if (ipst->ips_ipmp_hook_emulation) {
16859 			/*
16860 			 * If the group already exists we use the existing
16861 			 * group_ifindex, otherwise we pick a new index here.
16862 			 */
16863 			if (phyi_tmp != NULL) {
16864 				phyi->phyint_group_ifindex =
16865 				    phyi_tmp->phyint_group_ifindex;
16866 			} else {
16867 				/* XXX We need a recovery strategy here. */
16868 				if (!ip_assign_ifindex(
16869 				    &phyi->phyint_group_ifindex, ipst))
16870 					cmn_err(CE_PANIC,
16871 					    "ip_assign_ifindex() failed");
16872 			}
16873 		}
16874 		/*
16875 		 * Select whether the netinfo and hook use the per-interface
16876 		 * or per-group ifindex.
16877 		 */
16878 		if (ipst->ips_ipmp_hook_emulation)
16879 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16880 		else
16881 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16882 
16883 		if (ipst->ips_ipmp_hook_emulation &&
16884 		    phyi_tmp != NULL) {
16885 			/* First phyint in group - group PLUMB event */
16886 			ill_nic_info_plumb(ill, B_TRUE);
16887 		}
16888 		mutex_exit(&phyi->phyint_lock);
16889 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16890 		rw_exit(&ipst->ips_ill_g_lock);
16891 
16892 		err = ill_up_ipifs(ill, q, mp);
16893 	}
16894 
16895 done:
16896 	/*
16897 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16898 	 */
16899 	if (err != EINPROGRESS) {
16900 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16901 		if (ill_v4 != NULL)
16902 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16903 		if (ill_v6 != NULL)
16904 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16905 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16906 	}
16907 	return (err);
16908 }
16909 
16910 /* ARGSUSED */
16911 int
16912 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16913     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16914 {
16915 	ill_t *ill;
16916 	phyint_t *phyi;
16917 	struct lifreq *lifr;
16918 	mblk_t	*mp1;
16919 
16920 	/* Existence verified in ip_wput_nondata */
16921 	mp1 = mp->b_cont->b_cont;
16922 	lifr = (struct lifreq *)mp1->b_rptr;
16923 	ill = ipif->ipif_ill;
16924 	phyi = ill->ill_phyint;
16925 
16926 	lifr->lifr_groupname[0] = '\0';
16927 	/*
16928 	 * ill_group may be null if all the interfaces
16929 	 * are down. But still, the phyint should always
16930 	 * hold the name.
16931 	 */
16932 	if (phyi->phyint_groupname_len != 0) {
16933 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16934 		    phyi->phyint_groupname_len);
16935 	}
16936 
16937 	return (0);
16938 }
16939 
16940 
16941 typedef struct conn_move_s {
16942 	ill_t	*cm_from_ill;
16943 	ill_t	*cm_to_ill;
16944 	int	cm_ifindex;
16945 } conn_move_t;
16946 
16947 /*
16948  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16949  */
16950 static void
16951 conn_move(conn_t *connp, caddr_t arg)
16952 {
16953 	conn_move_t *connm;
16954 	int ifindex;
16955 	int i;
16956 	ill_t *from_ill;
16957 	ill_t *to_ill;
16958 	ilg_t *ilg;
16959 	ilm_t *ret_ilm;
16960 
16961 	connm = (conn_move_t *)arg;
16962 	ifindex = connm->cm_ifindex;
16963 	from_ill = connm->cm_from_ill;
16964 	to_ill = connm->cm_to_ill;
16965 
16966 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16967 
16968 	/* All multicast fields protected by conn_lock */
16969 	mutex_enter(&connp->conn_lock);
16970 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16971 	if ((connp->conn_outgoing_ill == from_ill) &&
16972 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16973 		connp->conn_outgoing_ill = to_ill;
16974 		connp->conn_incoming_ill = to_ill;
16975 	}
16976 
16977 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16978 
16979 	if ((connp->conn_multicast_ill == from_ill) &&
16980 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16981 		connp->conn_multicast_ill = connm->cm_to_ill;
16982 	}
16983 
16984 	/* Change IP_XMIT_IF associations */
16985 	if ((connp->conn_xmit_if_ill == from_ill) &&
16986 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
16987 		connp->conn_xmit_if_ill = to_ill;
16988 	}
16989 	/*
16990 	 * Change the ilg_ill to point to the new one. This assumes
16991 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16992 	 * has been told to receive packets on this interface.
16993 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16994 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16995 	 * some ilms may not have moved. We check to see whether
16996 	 * the ilms have moved to to_ill. We can't check on from_ill
16997 	 * as in the process of moving, we could have split an ilm
16998 	 * in to two - which has the same orig_ifindex and v6group.
16999 	 *
17000 	 * For IPv4, ilg_ipif moves implicitly. The code below really
17001 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
17002 	 */
17003 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
17004 		ilg = &connp->conn_ilg[i];
17005 		if ((ilg->ilg_ill == from_ill) &&
17006 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
17007 			/* ifindex != 0 indicates failback */
17008 			if (ifindex != 0) {
17009 				connp->conn_ilg[i].ilg_ill = to_ill;
17010 				continue;
17011 			}
17012 
17013 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
17014 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
17015 			    connp->conn_zoneid);
17016 
17017 			if (ret_ilm != NULL)
17018 				connp->conn_ilg[i].ilg_ill = to_ill;
17019 		}
17020 	}
17021 	mutex_exit(&connp->conn_lock);
17022 }
17023 
17024 static void
17025 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
17026 {
17027 	conn_move_t connm;
17028 	ip_stack_t	*ipst = from_ill->ill_ipst;
17029 
17030 	connm.cm_from_ill = from_ill;
17031 	connm.cm_to_ill = to_ill;
17032 	connm.cm_ifindex = ifindex;
17033 
17034 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
17035 }
17036 
17037 /*
17038  * ilm has been moved from from_ill to to_ill.
17039  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
17040  * appropriately.
17041  *
17042  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
17043  *	  the code there de-references ipif_ill to get the ill to
17044  *	  send multicast requests. It does not work as ipif is on its
17045  *	  move and already moved when this function is called.
17046  *	  Thus, we need to use from_ill and to_ill send down multicast
17047  *	  requests.
17048  */
17049 static void
17050 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
17051 {
17052 	ipif_t *ipif;
17053 	ilm_t *ilm;
17054 
17055 	/*
17056 	 * See whether we need to send down DL_ENABMULTI_REQ on
17057 	 * to_ill as ilm has just been added.
17058 	 */
17059 	ASSERT(IAM_WRITER_ILL(to_ill));
17060 	ASSERT(IAM_WRITER_ILL(from_ill));
17061 
17062 	ILM_WALKER_HOLD(to_ill);
17063 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
17064 
17065 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
17066 			continue;
17067 		/*
17068 		 * no locks held, ill/ipif cannot dissappear as long
17069 		 * as we are writer.
17070 		 */
17071 		ipif = to_ill->ill_ipif;
17072 		/*
17073 		 * No need to hold any lock as we are the writer and this
17074 		 * can only be changed by a writer.
17075 		 */
17076 		ilm->ilm_is_new = B_FALSE;
17077 
17078 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
17079 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
17080 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
17081 			    "resolver\n"));
17082 			continue;		/* Must be IRE_IF_NORESOLVER */
17083 		}
17084 
17085 
17086 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
17087 			ip1dbg(("ilm_send_multicast_reqs: "
17088 			    "to_ill MULTI_BCAST\n"));
17089 			goto from;
17090 		}
17091 
17092 		if (to_ill->ill_isv6)
17093 			mld_joingroup(ilm);
17094 		else
17095 			igmp_joingroup(ilm);
17096 
17097 		if (to_ill->ill_ipif_up_count == 0) {
17098 			/*
17099 			 * Nobody there. All multicast addresses will be
17100 			 * re-joined when we get the DL_BIND_ACK bringing the
17101 			 * interface up.
17102 			 */
17103 			ilm->ilm_notify_driver = B_FALSE;
17104 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
17105 			goto from;
17106 		}
17107 
17108 		/*
17109 		 * For allmulti address, we want to join on only one interface.
17110 		 * Checking for ilm_numentries_v6 is not correct as you may
17111 		 * find an ilm with zero address on to_ill, but we may not
17112 		 * have nominated to_ill for receiving. Thus, if we have
17113 		 * nominated from_ill (ill_join_allmulti is set), nominate
17114 		 * only if to_ill is not already nominated (to_ill normally
17115 		 * should not have been nominated if "from_ill" has already
17116 		 * been nominated. As we don't prevent failovers from happening
17117 		 * across groups, we don't assert).
17118 		 */
17119 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
17120 			/*
17121 			 * There is no need to hold ill locks as we are
17122 			 * writer on both ills and when ill_join_allmulti
17123 			 * is changed the thread is always a writer.
17124 			 */
17125 			if (from_ill->ill_join_allmulti &&
17126 			    !to_ill->ill_join_allmulti) {
17127 				(void) ip_join_allmulti(to_ill->ill_ipif);
17128 			}
17129 		} else if (ilm->ilm_notify_driver) {
17130 
17131 			/*
17132 			 * This is a newly moved ilm so we need to tell the
17133 			 * driver about the new group. There can be more than
17134 			 * one ilm's for the same group in the list each with a
17135 			 * different orig_ifindex. We have to inform the driver
17136 			 * once. In ilm_move_v[4,6] we only set the flag
17137 			 * ilm_notify_driver for the first ilm.
17138 			 */
17139 
17140 			(void) ip_ll_send_enabmulti_req(to_ill,
17141 			    &ilm->ilm_v6addr);
17142 		}
17143 
17144 		ilm->ilm_notify_driver = B_FALSE;
17145 
17146 		/*
17147 		 * See whether we need to send down DL_DISABMULTI_REQ on
17148 		 * from_ill as ilm has just been removed.
17149 		 */
17150 from:
17151 		ipif = from_ill->ill_ipif;
17152 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
17153 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
17154 			ip1dbg(("ilm_send_multicast_reqs: "
17155 			    "from_ill not resolver\n"));
17156 			continue;		/* Must be IRE_IF_NORESOLVER */
17157 		}
17158 
17159 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
17160 			ip1dbg(("ilm_send_multicast_reqs: "
17161 			    "from_ill MULTI_BCAST\n"));
17162 			continue;
17163 		}
17164 
17165 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
17166 			if (from_ill->ill_join_allmulti)
17167 				(void) ip_leave_allmulti(from_ill->ill_ipif);
17168 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
17169 			(void) ip_ll_send_disabmulti_req(from_ill,
17170 			    &ilm->ilm_v6addr);
17171 		}
17172 	}
17173 	ILM_WALKER_RELE(to_ill);
17174 }
17175 
17176 /*
17177  * This function is called when all multicast memberships needs
17178  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
17179  * called only once unlike the IPv4 counterpart where it is called after
17180  * every logical interface is moved. The reason is due to multicast
17181  * memberships are joined using an interface address in IPv4 while in
17182  * IPv6, interface index is used.
17183  */
17184 static void
17185 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
17186 {
17187 	ilm_t	*ilm;
17188 	ilm_t	*ilm_next;
17189 	ilm_t	*new_ilm;
17190 	ilm_t	**ilmp;
17191 	int	count;
17192 	char buf[INET6_ADDRSTRLEN];
17193 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
17194 	ip_stack_t	*ipst = from_ill->ill_ipst;
17195 
17196 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17197 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17198 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17199 
17200 	if (ifindex == 0) {
17201 		/*
17202 		 * Form the solicited node mcast address which is used later.
17203 		 */
17204 		ipif_t *ipif;
17205 
17206 		ipif = from_ill->ill_ipif;
17207 		ASSERT(ipif->ipif_id == 0);
17208 
17209 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
17210 	}
17211 
17212 	ilmp = &from_ill->ill_ilm;
17213 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17214 		ilm_next = ilm->ilm_next;
17215 
17216 		if (ilm->ilm_flags & ILM_DELETED) {
17217 			ilmp = &ilm->ilm_next;
17218 			continue;
17219 		}
17220 
17221 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
17222 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
17223 		ASSERT(ilm->ilm_orig_ifindex != 0);
17224 		if (ilm->ilm_orig_ifindex == ifindex) {
17225 			/*
17226 			 * We are failing back multicast memberships.
17227 			 * If the same ilm exists in to_ill, it means somebody
17228 			 * has joined the same group there e.g. ff02::1
17229 			 * is joined within the kernel when the interfaces
17230 			 * came UP.
17231 			 */
17232 			ASSERT(ilm->ilm_ipif == NULL);
17233 			if (new_ilm != NULL) {
17234 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17235 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17236 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17237 					new_ilm->ilm_is_new = B_TRUE;
17238 				}
17239 			} else {
17240 				/*
17241 				 * check if we can just move the ilm
17242 				 */
17243 				if (from_ill->ill_ilm_walker_cnt != 0) {
17244 					/*
17245 					 * We have walkers we cannot move
17246 					 * the ilm, so allocate a new ilm,
17247 					 * this (old) ilm will be marked
17248 					 * ILM_DELETED at the end of the loop
17249 					 * and will be freed when the
17250 					 * last walker exits.
17251 					 */
17252 					new_ilm = (ilm_t *)mi_zalloc
17253 					    (sizeof (ilm_t));
17254 					if (new_ilm == NULL) {
17255 						ip0dbg(("ilm_move_v6: "
17256 						    "FAILBACK of IPv6"
17257 						    " multicast address %s : "
17258 						    "from %s to"
17259 						    " %s failed : ENOMEM \n",
17260 						    inet_ntop(AF_INET6,
17261 						    &ilm->ilm_v6addr, buf,
17262 						    sizeof (buf)),
17263 						    from_ill->ill_name,
17264 						    to_ill->ill_name));
17265 
17266 							ilmp = &ilm->ilm_next;
17267 							continue;
17268 					}
17269 					*new_ilm = *ilm;
17270 					/*
17271 					 * we don't want new_ilm linked to
17272 					 * ilm's filter list.
17273 					 */
17274 					new_ilm->ilm_filter = NULL;
17275 				} else {
17276 					/*
17277 					 * No walkers we can move the ilm.
17278 					 * lets take it out of the list.
17279 					 */
17280 					*ilmp = ilm->ilm_next;
17281 					ilm->ilm_next = NULL;
17282 					new_ilm = ilm;
17283 				}
17284 
17285 				/*
17286 				 * if this is the first ilm for the group
17287 				 * set ilm_notify_driver so that we notify the
17288 				 * driver in ilm_send_multicast_reqs.
17289 				 */
17290 				if (ilm_lookup_ill_v6(to_ill,
17291 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17292 					new_ilm->ilm_notify_driver = B_TRUE;
17293 
17294 				new_ilm->ilm_ill = to_ill;
17295 				/* Add to the to_ill's list */
17296 				new_ilm->ilm_next = to_ill->ill_ilm;
17297 				to_ill->ill_ilm = new_ilm;
17298 				/*
17299 				 * set the flag so that mld_joingroup is
17300 				 * called in ilm_send_multicast_reqs().
17301 				 */
17302 				new_ilm->ilm_is_new = B_TRUE;
17303 			}
17304 			goto bottom;
17305 		} else if (ifindex != 0) {
17306 			/*
17307 			 * If this is FAILBACK (ifindex != 0) and the ifindex
17308 			 * has not matched above, look at the next ilm.
17309 			 */
17310 			ilmp = &ilm->ilm_next;
17311 			continue;
17312 		}
17313 		/*
17314 		 * If we are here, it means ifindex is 0. Failover
17315 		 * everything.
17316 		 *
17317 		 * We need to handle solicited node mcast address
17318 		 * and all_nodes mcast address differently as they
17319 		 * are joined witin the kenrel (ipif_multicast_up)
17320 		 * and potentially from the userland. We are called
17321 		 * after the ipifs of from_ill has been moved.
17322 		 * If we still find ilms on ill with solicited node
17323 		 * mcast address or all_nodes mcast address, it must
17324 		 * belong to the UP interface that has not moved e.g.
17325 		 * ipif_id 0 with the link local prefix does not move.
17326 		 * We join this on the new ill accounting for all the
17327 		 * userland memberships so that applications don't
17328 		 * see any failure.
17329 		 *
17330 		 * We need to make sure that we account only for the
17331 		 * solicited node and all node multicast addresses
17332 		 * that was brought UP on these. In the case of
17333 		 * a failover from A to B, we might have ilms belonging
17334 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
17335 		 * for the membership from the userland. If we are failing
17336 		 * over from B to C now, we will find the ones belonging
17337 		 * to A on B. These don't account for the ill_ipif_up_count.
17338 		 * They just move from B to C. The check below on
17339 		 * ilm_orig_ifindex ensures that.
17340 		 */
17341 		if ((ilm->ilm_orig_ifindex ==
17342 		    from_ill->ill_phyint->phyint_ifindex) &&
17343 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
17344 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
17345 		    &ilm->ilm_v6addr))) {
17346 			ASSERT(ilm->ilm_refcnt > 0);
17347 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
17348 			/*
17349 			 * For indentation reasons, we are not using a
17350 			 * "else" here.
17351 			 */
17352 			if (count == 0) {
17353 				ilmp = &ilm->ilm_next;
17354 				continue;
17355 			}
17356 			ilm->ilm_refcnt -= count;
17357 			if (new_ilm != NULL) {
17358 				/*
17359 				 * Can find one with the same
17360 				 * ilm_orig_ifindex, if we are failing
17361 				 * over to a STANDBY. This happens
17362 				 * when somebody wants to join a group
17363 				 * on a STANDBY interface and we
17364 				 * internally join on a different one.
17365 				 * If we had joined on from_ill then, a
17366 				 * failover now will find a new ilm
17367 				 * with this index.
17368 				 */
17369 				ip1dbg(("ilm_move_v6: FAILOVER, found"
17370 				    " new ilm on %s, group address %s\n",
17371 				    to_ill->ill_name,
17372 				    inet_ntop(AF_INET6,
17373 				    &ilm->ilm_v6addr, buf,
17374 				    sizeof (buf))));
17375 				new_ilm->ilm_refcnt += count;
17376 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17377 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17378 					new_ilm->ilm_is_new = B_TRUE;
17379 				}
17380 			} else {
17381 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17382 				if (new_ilm == NULL) {
17383 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17384 					    " multicast address %s : from %s to"
17385 					    " %s failed : ENOMEM \n",
17386 					    inet_ntop(AF_INET6,
17387 					    &ilm->ilm_v6addr, buf,
17388 					    sizeof (buf)), from_ill->ill_name,
17389 					    to_ill->ill_name));
17390 					ilmp = &ilm->ilm_next;
17391 					continue;
17392 				}
17393 				*new_ilm = *ilm;
17394 				new_ilm->ilm_filter = NULL;
17395 				new_ilm->ilm_refcnt = count;
17396 				new_ilm->ilm_timer = INFINITY;
17397 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17398 				new_ilm->ilm_is_new = B_TRUE;
17399 				/*
17400 				 * If the to_ill has not joined this
17401 				 * group we need to tell the driver in
17402 				 * ill_send_multicast_reqs.
17403 				 */
17404 				if (ilm_lookup_ill_v6(to_ill,
17405 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17406 					new_ilm->ilm_notify_driver = B_TRUE;
17407 
17408 				new_ilm->ilm_ill = to_ill;
17409 				/* Add to the to_ill's list */
17410 				new_ilm->ilm_next = to_ill->ill_ilm;
17411 				to_ill->ill_ilm = new_ilm;
17412 				ASSERT(new_ilm->ilm_ipif == NULL);
17413 			}
17414 			if (ilm->ilm_refcnt == 0) {
17415 				goto bottom;
17416 			} else {
17417 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17418 				CLEAR_SLIST(new_ilm->ilm_filter);
17419 				ilmp = &ilm->ilm_next;
17420 			}
17421 			continue;
17422 		} else {
17423 			/*
17424 			 * ifindex = 0 means, move everything pointing at
17425 			 * from_ill. We are doing this becuase ill has
17426 			 * either FAILED or became INACTIVE.
17427 			 *
17428 			 * As we would like to move things later back to
17429 			 * from_ill, we want to retain the identity of this
17430 			 * ilm. Thus, we don't blindly increment the reference
17431 			 * count on the ilms matching the address alone. We
17432 			 * need to match on the ilm_orig_index also. new_ilm
17433 			 * was obtained by matching ilm_orig_index also.
17434 			 */
17435 			if (new_ilm != NULL) {
17436 				/*
17437 				 * This is possible only if a previous restore
17438 				 * was incomplete i.e restore to
17439 				 * ilm_orig_ifindex left some ilms because
17440 				 * of some failures. Thus when we are failing
17441 				 * again, we might find our old friends there.
17442 				 */
17443 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17444 				    " on %s, group address %s\n",
17445 				    to_ill->ill_name,
17446 				    inet_ntop(AF_INET6,
17447 				    &ilm->ilm_v6addr, buf,
17448 				    sizeof (buf))));
17449 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17450 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17451 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17452 					new_ilm->ilm_is_new = B_TRUE;
17453 				}
17454 			} else {
17455 				if (from_ill->ill_ilm_walker_cnt != 0) {
17456 					new_ilm = (ilm_t *)
17457 					    mi_zalloc(sizeof (ilm_t));
17458 					if (new_ilm == NULL) {
17459 						ip0dbg(("ilm_move_v6: "
17460 						    "FAILOVER of IPv6"
17461 						    " multicast address %s : "
17462 						    "from %s to"
17463 						    " %s failed : ENOMEM \n",
17464 						    inet_ntop(AF_INET6,
17465 						    &ilm->ilm_v6addr, buf,
17466 						    sizeof (buf)),
17467 						    from_ill->ill_name,
17468 						    to_ill->ill_name));
17469 
17470 							ilmp = &ilm->ilm_next;
17471 							continue;
17472 					}
17473 					*new_ilm = *ilm;
17474 					new_ilm->ilm_filter = NULL;
17475 				} else {
17476 					*ilmp = ilm->ilm_next;
17477 					new_ilm = ilm;
17478 				}
17479 				/*
17480 				 * If the to_ill has not joined this
17481 				 * group we need to tell the driver in
17482 				 * ill_send_multicast_reqs.
17483 				 */
17484 				if (ilm_lookup_ill_v6(to_ill,
17485 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17486 					new_ilm->ilm_notify_driver = B_TRUE;
17487 
17488 				/* Add to the to_ill's list */
17489 				new_ilm->ilm_next = to_ill->ill_ilm;
17490 				to_ill->ill_ilm = new_ilm;
17491 				ASSERT(ilm->ilm_ipif == NULL);
17492 				new_ilm->ilm_ill = to_ill;
17493 				new_ilm->ilm_is_new = B_TRUE;
17494 			}
17495 
17496 		}
17497 
17498 bottom:
17499 		/*
17500 		 * Revert multicast filter state to (EXCLUDE, NULL).
17501 		 * new_ilm->ilm_is_new should already be set if needed.
17502 		 */
17503 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17504 		CLEAR_SLIST(new_ilm->ilm_filter);
17505 		/*
17506 		 * We allocated/got a new ilm, free the old one.
17507 		 */
17508 		if (new_ilm != ilm) {
17509 			if (from_ill->ill_ilm_walker_cnt == 0) {
17510 				*ilmp = ilm->ilm_next;
17511 				ilm->ilm_next = NULL;
17512 				FREE_SLIST(ilm->ilm_filter);
17513 				FREE_SLIST(ilm->ilm_pendsrcs);
17514 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17515 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17516 				mi_free((char *)ilm);
17517 			} else {
17518 				ilm->ilm_flags |= ILM_DELETED;
17519 				from_ill->ill_ilm_cleanup_reqd = 1;
17520 				ilmp = &ilm->ilm_next;
17521 			}
17522 		}
17523 	}
17524 }
17525 
17526 /*
17527  * Move all the multicast memberships to to_ill. Called when
17528  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17529  * different from IPv6 counterpart as multicast memberships are associated
17530  * with ills in IPv6. This function is called after every ipif is moved
17531  * unlike IPv6, where it is moved only once.
17532  */
17533 static void
17534 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17535 {
17536 	ilm_t	*ilm;
17537 	ilm_t	*ilm_next;
17538 	ilm_t	*new_ilm;
17539 	ilm_t	**ilmp;
17540 	ip_stack_t	*ipst = from_ill->ill_ipst;
17541 
17542 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17543 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17544 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17545 
17546 	ilmp = &from_ill->ill_ilm;
17547 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17548 		ilm_next = ilm->ilm_next;
17549 
17550 		if (ilm->ilm_flags & ILM_DELETED) {
17551 			ilmp = &ilm->ilm_next;
17552 			continue;
17553 		}
17554 
17555 		ASSERT(ilm->ilm_ipif != NULL);
17556 
17557 		if (ilm->ilm_ipif != ipif) {
17558 			ilmp = &ilm->ilm_next;
17559 			continue;
17560 		}
17561 
17562 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17563 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17564 			/*
17565 			 * We joined this in ipif_multicast_up
17566 			 * and we never did an ipif_multicast_down
17567 			 * for IPv4. If nobody else from the userland
17568 			 * has reference, we free the ilm, and later
17569 			 * when this ipif comes up on the new ill,
17570 			 * we will join this again.
17571 			 */
17572 			if (--ilm->ilm_refcnt == 0)
17573 				goto delete_ilm;
17574 
17575 			new_ilm = ilm_lookup_ipif(ipif,
17576 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17577 			if (new_ilm != NULL) {
17578 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17579 				/*
17580 				 * We still need to deal with the from_ill.
17581 				 */
17582 				new_ilm->ilm_is_new = B_TRUE;
17583 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17584 				CLEAR_SLIST(new_ilm->ilm_filter);
17585 				goto delete_ilm;
17586 			}
17587 			/*
17588 			 * If we could not find one e.g. ipif is
17589 			 * still down on to_ill, we add this ilm
17590 			 * on ill_new to preserve the reference
17591 			 * count.
17592 			 */
17593 		}
17594 		/*
17595 		 * When ipifs move, ilms always move with it
17596 		 * to the NEW ill. Thus we should never be
17597 		 * able to find ilm till we really move it here.
17598 		 */
17599 		ASSERT(ilm_lookup_ipif(ipif,
17600 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17601 
17602 		if (from_ill->ill_ilm_walker_cnt != 0) {
17603 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17604 			if (new_ilm == NULL) {
17605 				char buf[INET6_ADDRSTRLEN];
17606 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17607 				    " multicast address %s : "
17608 				    "from %s to"
17609 				    " %s failed : ENOMEM \n",
17610 				    inet_ntop(AF_INET,
17611 				    &ilm->ilm_v6addr, buf,
17612 				    sizeof (buf)),
17613 				    from_ill->ill_name,
17614 				    to_ill->ill_name));
17615 
17616 				ilmp = &ilm->ilm_next;
17617 				continue;
17618 			}
17619 			*new_ilm = *ilm;
17620 			/* We don't want new_ilm linked to ilm's filter list */
17621 			new_ilm->ilm_filter = NULL;
17622 		} else {
17623 			/* Remove from the list */
17624 			*ilmp = ilm->ilm_next;
17625 			new_ilm = ilm;
17626 		}
17627 
17628 		/*
17629 		 * If we have never joined this group on the to_ill
17630 		 * make sure we tell the driver.
17631 		 */
17632 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17633 		    ALL_ZONES) == NULL)
17634 			new_ilm->ilm_notify_driver = B_TRUE;
17635 
17636 		/* Add to the to_ill's list */
17637 		new_ilm->ilm_next = to_ill->ill_ilm;
17638 		to_ill->ill_ilm = new_ilm;
17639 		new_ilm->ilm_is_new = B_TRUE;
17640 
17641 		/*
17642 		 * Revert multicast filter state to (EXCLUDE, NULL)
17643 		 */
17644 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17645 		CLEAR_SLIST(new_ilm->ilm_filter);
17646 
17647 		/*
17648 		 * Delete only if we have allocated a new ilm.
17649 		 */
17650 		if (new_ilm != ilm) {
17651 delete_ilm:
17652 			if (from_ill->ill_ilm_walker_cnt == 0) {
17653 				/* Remove from the list */
17654 				*ilmp = ilm->ilm_next;
17655 				ilm->ilm_next = NULL;
17656 				FREE_SLIST(ilm->ilm_filter);
17657 				FREE_SLIST(ilm->ilm_pendsrcs);
17658 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17659 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17660 				mi_free((char *)ilm);
17661 			} else {
17662 				ilm->ilm_flags |= ILM_DELETED;
17663 				from_ill->ill_ilm_cleanup_reqd = 1;
17664 				ilmp = &ilm->ilm_next;
17665 			}
17666 		}
17667 	}
17668 }
17669 
17670 static uint_t
17671 ipif_get_id(ill_t *ill, uint_t id)
17672 {
17673 	uint_t	unit;
17674 	ipif_t	*tipif;
17675 	boolean_t found = B_FALSE;
17676 	ip_stack_t	*ipst = ill->ill_ipst;
17677 
17678 	/*
17679 	 * During failback, we want to go back to the same id
17680 	 * instead of the smallest id so that the original
17681 	 * configuration is maintained. id is non-zero in that
17682 	 * case.
17683 	 */
17684 	if (id != 0) {
17685 		/*
17686 		 * While failing back, if we still have an ipif with
17687 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17688 		 * as soon as we return from this function. It was
17689 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17690 		 * we can choose the smallest id. Thus we return zero
17691 		 * in that case ignoring the hint.
17692 		 */
17693 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17694 			return (0);
17695 		for (tipif = ill->ill_ipif; tipif != NULL;
17696 		    tipif = tipif->ipif_next) {
17697 			if (tipif->ipif_id == id) {
17698 				found = B_TRUE;
17699 				break;
17700 			}
17701 		}
17702 		/*
17703 		 * If somebody already plumbed another logical
17704 		 * with the same id, we won't be able to find it.
17705 		 */
17706 		if (!found)
17707 			return (id);
17708 	}
17709 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17710 		found = B_FALSE;
17711 		for (tipif = ill->ill_ipif; tipif != NULL;
17712 		    tipif = tipif->ipif_next) {
17713 			if (tipif->ipif_id == unit) {
17714 				found = B_TRUE;
17715 				break;
17716 			}
17717 		}
17718 		if (!found)
17719 			break;
17720 	}
17721 	return (unit);
17722 }
17723 
17724 /* ARGSUSED */
17725 static int
17726 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17727     ipif_t **rep_ipif_ptr)
17728 {
17729 	ill_t	*from_ill;
17730 	ipif_t	*rep_ipif;
17731 	uint_t	unit;
17732 	int err = 0;
17733 	ipif_t	*to_ipif;
17734 	struct iocblk	*iocp;
17735 	boolean_t failback_cmd;
17736 	boolean_t remove_ipif;
17737 	int	rc;
17738 	ip_stack_t	*ipst;
17739 
17740 	ASSERT(IAM_WRITER_ILL(to_ill));
17741 	ASSERT(IAM_WRITER_IPIF(ipif));
17742 
17743 	iocp = (struct iocblk *)mp->b_rptr;
17744 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17745 	remove_ipif = B_FALSE;
17746 
17747 	from_ill = ipif->ipif_ill;
17748 	ipst = from_ill->ill_ipst;
17749 
17750 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17751 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17752 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17753 
17754 	/*
17755 	 * Don't move LINK LOCAL addresses as they are tied to
17756 	 * physical interface.
17757 	 */
17758 	if (from_ill->ill_isv6 &&
17759 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17760 		ipif->ipif_was_up = B_FALSE;
17761 		IPIF_UNMARK_MOVING(ipif);
17762 		return (0);
17763 	}
17764 
17765 	/*
17766 	 * We set the ipif_id to maximum so that the search for
17767 	 * ipif_id will pick the lowest number i.e 0 in the
17768 	 * following 2 cases :
17769 	 *
17770 	 * 1) We have a replacement ipif at the head of to_ill.
17771 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17772 	 *    on to_ill and hence the MOVE might fail. We want to
17773 	 *    remove it only if we could move the ipif. Thus, by
17774 	 *    setting it to the MAX value, we make the search in
17775 	 *    ipif_get_id return the zeroth id.
17776 	 *
17777 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17778 	 *    we might just have a zero address plumbed on the ipif
17779 	 *    with zero id in the case of IPv4. We remove that while
17780 	 *    doing the failback. We want to remove it only if we
17781 	 *    could move the ipif. Thus, by setting it to the MAX
17782 	 *    value, we make the search in ipif_get_id return the
17783 	 *    zeroth id.
17784 	 *
17785 	 * Both (1) and (2) are done only when when we are moving
17786 	 * an ipif (either due to failover/failback) which originally
17787 	 * belonged to this interface i.e the ipif_orig_ifindex is
17788 	 * the same as to_ill's ifindex. This is needed so that
17789 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17790 	 * from B -> A (B is being removed from the group) and
17791 	 * FAILBACK from A -> B restores the original configuration.
17792 	 * Without the check for orig_ifindex, the second FAILOVER
17793 	 * could make the ipif belonging to B replace the A's zeroth
17794 	 * ipif and the subsequent failback re-creating the replacement
17795 	 * ipif again.
17796 	 *
17797 	 * NOTE : We created the replacement ipif when we did a
17798 	 * FAILOVER (See below). We could check for FAILBACK and
17799 	 * then look for replacement ipif to be removed. But we don't
17800 	 * want to do that because we wan't to allow the possibility
17801 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17802 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17803 	 * from B -> A.
17804 	 */
17805 	to_ipif = to_ill->ill_ipif;
17806 	if ((to_ill->ill_phyint->phyint_ifindex ==
17807 	    ipif->ipif_orig_ifindex) &&
17808 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17809 		ASSERT(to_ipif->ipif_id == 0);
17810 		remove_ipif = B_TRUE;
17811 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17812 	}
17813 	/*
17814 	 * Find the lowest logical unit number on the to_ill.
17815 	 * If we are failing back, try to get the original id
17816 	 * rather than the lowest one so that the original
17817 	 * configuration is maintained.
17818 	 *
17819 	 * XXX need a better scheme for this.
17820 	 */
17821 	if (failback_cmd) {
17822 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17823 	} else {
17824 		unit = ipif_get_id(to_ill, 0);
17825 	}
17826 
17827 	/* Reset back to zero in case we fail below */
17828 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17829 		to_ipif->ipif_id = 0;
17830 
17831 	if (unit == ipst->ips_ip_addrs_per_if) {
17832 		ipif->ipif_was_up = B_FALSE;
17833 		IPIF_UNMARK_MOVING(ipif);
17834 		return (EINVAL);
17835 	}
17836 
17837 	/*
17838 	 * ipif is ready to move from "from_ill" to "to_ill".
17839 	 *
17840 	 * 1) If we are moving ipif with id zero, create a
17841 	 *    replacement ipif for this ipif on from_ill. If this fails
17842 	 *    fail the MOVE operation.
17843 	 *
17844 	 * 2) Remove the replacement ipif on to_ill if any.
17845 	 *    We could remove the replacement ipif when we are moving
17846 	 *    the ipif with id zero. But what if somebody already
17847 	 *    unplumbed it ? Thus we always remove it if it is present.
17848 	 *    We want to do it only if we are sure we are going to
17849 	 *    move the ipif to to_ill which is why there are no
17850 	 *    returns due to error till ipif is linked to to_ill.
17851 	 *    Note that the first ipif that we failback will always
17852 	 *    be zero if it is present.
17853 	 */
17854 	if (ipif->ipif_id == 0) {
17855 		ipaddr_t inaddr_any = INADDR_ANY;
17856 
17857 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17858 		if (rep_ipif == NULL) {
17859 			ipif->ipif_was_up = B_FALSE;
17860 			IPIF_UNMARK_MOVING(ipif);
17861 			return (ENOMEM);
17862 		}
17863 		*rep_ipif = ipif_zero;
17864 		/*
17865 		 * Before we put the ipif on the list, store the addresses
17866 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17867 		 * assumes so. This logic is not any different from what
17868 		 * ipif_allocate does.
17869 		 */
17870 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17871 		    &rep_ipif->ipif_v6lcl_addr);
17872 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17873 		    &rep_ipif->ipif_v6src_addr);
17874 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17875 		    &rep_ipif->ipif_v6subnet);
17876 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17877 		    &rep_ipif->ipif_v6net_mask);
17878 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17879 		    &rep_ipif->ipif_v6brd_addr);
17880 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17881 		    &rep_ipif->ipif_v6pp_dst_addr);
17882 		/*
17883 		 * We mark IPIF_NOFAILOVER so that this can never
17884 		 * move.
17885 		 */
17886 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17887 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17888 		rep_ipif->ipif_replace_zero = B_TRUE;
17889 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17890 		    MUTEX_DEFAULT, NULL);
17891 		rep_ipif->ipif_id = 0;
17892 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17893 		rep_ipif->ipif_ill = from_ill;
17894 		rep_ipif->ipif_orig_ifindex =
17895 		    from_ill->ill_phyint->phyint_ifindex;
17896 		/* Insert at head */
17897 		rep_ipif->ipif_next = from_ill->ill_ipif;
17898 		from_ill->ill_ipif = rep_ipif;
17899 		/*
17900 		 * We don't really care to let apps know about
17901 		 * this interface.
17902 		 */
17903 	}
17904 
17905 	if (remove_ipif) {
17906 		/*
17907 		 * We set to a max value above for this case to get
17908 		 * id zero. ASSERT that we did get one.
17909 		 */
17910 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17911 		rep_ipif = to_ipif;
17912 		to_ill->ill_ipif = rep_ipif->ipif_next;
17913 		rep_ipif->ipif_next = NULL;
17914 		/*
17915 		 * If some apps scanned and find this interface,
17916 		 * it is time to let them know, so that they can
17917 		 * delete it.
17918 		 */
17919 
17920 		*rep_ipif_ptr = rep_ipif;
17921 	}
17922 
17923 	/* Get it out of the ILL interface list. */
17924 	ipif_remove(ipif, B_FALSE);
17925 
17926 	/* Assign the new ill */
17927 	ipif->ipif_ill = to_ill;
17928 	ipif->ipif_id = unit;
17929 	/* id has already been checked */
17930 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17931 	ASSERT(rc == 0);
17932 	/* Let SCTP update its list */
17933 	sctp_move_ipif(ipif, from_ill, to_ill);
17934 	/*
17935 	 * Handle the failover and failback of ipif_t between
17936 	 * ill_t that have differing maximum mtu values.
17937 	 */
17938 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17939 		if (ipif->ipif_saved_mtu == 0) {
17940 			/*
17941 			 * As this ipif_t is moving to an ill_t
17942 			 * that has a lower ill_max_mtu, its
17943 			 * ipif_mtu needs to be saved so it can
17944 			 * be restored during failback or during
17945 			 * failover to an ill_t which has a
17946 			 * higher ill_max_mtu.
17947 			 */
17948 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17949 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17950 		} else {
17951 			/*
17952 			 * The ipif_t is, once again, moving to
17953 			 * an ill_t that has a lower maximum mtu
17954 			 * value.
17955 			 */
17956 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17957 		}
17958 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17959 	    ipif->ipif_saved_mtu != 0) {
17960 		/*
17961 		 * The mtu of this ipif_t had to be reduced
17962 		 * during an earlier failover; this is an
17963 		 * opportunity for it to be increased (either as
17964 		 * part of another failover or a failback).
17965 		 */
17966 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17967 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17968 			ipif->ipif_saved_mtu = 0;
17969 		} else {
17970 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17971 		}
17972 	}
17973 
17974 	/*
17975 	 * We preserve all the other fields of the ipif including
17976 	 * ipif_saved_ire_mp. The routes that are saved here will
17977 	 * be recreated on the new interface and back on the old
17978 	 * interface when we move back.
17979 	 */
17980 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17981 
17982 	return (err);
17983 }
17984 
17985 static int
17986 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17987     int ifindex, ipif_t **rep_ipif_ptr)
17988 {
17989 	ipif_t *mipif;
17990 	ipif_t *ipif_next;
17991 	int err;
17992 
17993 	/*
17994 	 * We don't really try to MOVE back things if some of the
17995 	 * operations fail. The daemon will take care of moving again
17996 	 * later on.
17997 	 */
17998 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17999 		ipif_next = mipif->ipif_next;
18000 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
18001 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
18002 
18003 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
18004 
18005 			/*
18006 			 * When the MOVE fails, it is the job of the
18007 			 * application to take care of this properly
18008 			 * i.e try again if it is ENOMEM.
18009 			 */
18010 			if (mipif->ipif_ill != from_ill) {
18011 				/*
18012 				 * ipif has moved.
18013 				 *
18014 				 * Move the multicast memberships associated
18015 				 * with this ipif to the new ill. For IPv6, we
18016 				 * do it once after all the ipifs are moved
18017 				 * (in ill_move) as they are not associated
18018 				 * with ipifs.
18019 				 *
18020 				 * We need to move the ilms as the ipif has
18021 				 * already been moved to a new ill even
18022 				 * in the case of errors. Neither
18023 				 * ilm_free(ipif) will find the ilm
18024 				 * when somebody unplumbs this ipif nor
18025 				 * ilm_delete(ilm) will be able to find the
18026 				 * ilm, if we don't move now.
18027 				 */
18028 				if (!from_ill->ill_isv6)
18029 					ilm_move_v4(from_ill, to_ill, mipif);
18030 			}
18031 
18032 			if (err != 0)
18033 				return (err);
18034 		}
18035 	}
18036 	return (0);
18037 }
18038 
18039 static int
18040 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
18041 {
18042 	int ifindex;
18043 	int err;
18044 	struct iocblk	*iocp;
18045 	ipif_t	*ipif;
18046 	ipif_t *rep_ipif_ptr = NULL;
18047 	ipif_t	*from_ipif = NULL;
18048 	boolean_t check_rep_if = B_FALSE;
18049 	ip_stack_t	*ipst = from_ill->ill_ipst;
18050 
18051 	iocp = (struct iocblk *)mp->b_rptr;
18052 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
18053 		/*
18054 		 * Move everything pointing at from_ill to to_ill.
18055 		 * We acheive this by passing in 0 as ifindex.
18056 		 */
18057 		ifindex = 0;
18058 	} else {
18059 		/*
18060 		 * Move everything pointing at from_ill whose original
18061 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
18062 		 * We acheive this by passing in ifindex rather than 0.
18063 		 * Multicast vifs, ilgs move implicitly because ipifs move.
18064 		 */
18065 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
18066 		ifindex = to_ill->ill_phyint->phyint_ifindex;
18067 	}
18068 
18069 	/*
18070 	 * Determine if there is at least one ipif that would move from
18071 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
18072 	 * ipif (if it exists) on the to_ill would be consumed as a result of
18073 	 * the move, in which case we need to quiesce the replacement ipif also.
18074 	 */
18075 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
18076 	    from_ipif = from_ipif->ipif_next) {
18077 		if (((ifindex == 0) ||
18078 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
18079 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
18080 			check_rep_if = B_TRUE;
18081 			break;
18082 		}
18083 	}
18084 
18085 
18086 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
18087 
18088 	GRAB_ILL_LOCKS(from_ill, to_ill);
18089 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
18090 		(void) ipsq_pending_mp_add(NULL, ipif, q,
18091 		    mp, ILL_MOVE_OK);
18092 		RELEASE_ILL_LOCKS(from_ill, to_ill);
18093 		return (EINPROGRESS);
18094 	}
18095 
18096 	/* Check if the replacement ipif is quiescent to delete */
18097 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
18098 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
18099 		to_ill->ill_ipif->ipif_state_flags |=
18100 		    IPIF_MOVING | IPIF_CHANGING;
18101 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
18102 			(void) ipsq_pending_mp_add(NULL, ipif, q,
18103 			    mp, ILL_MOVE_OK);
18104 			RELEASE_ILL_LOCKS(from_ill, to_ill);
18105 			return (EINPROGRESS);
18106 		}
18107 	}
18108 	RELEASE_ILL_LOCKS(from_ill, to_ill);
18109 
18110 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
18111 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
18112 	GRAB_ILL_LOCKS(from_ill, to_ill);
18113 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
18114 
18115 	/* ilm_move is done inside ipif_move for IPv4 */
18116 	if (err == 0 && from_ill->ill_isv6)
18117 		ilm_move_v6(from_ill, to_ill, ifindex);
18118 
18119 	RELEASE_ILL_LOCKS(from_ill, to_ill);
18120 	rw_exit(&ipst->ips_ill_g_lock);
18121 
18122 	/*
18123 	 * send rts messages and multicast messages.
18124 	 */
18125 	if (rep_ipif_ptr != NULL) {
18126 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
18127 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
18128 			rep_ipif_ptr->ipif_recovery_id = 0;
18129 		}
18130 		ip_rts_ifmsg(rep_ipif_ptr);
18131 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
18132 		IPIF_TRACE_CLEANUP(rep_ipif_ptr);
18133 		mi_free(rep_ipif_ptr);
18134 	}
18135 
18136 	conn_move_ill(from_ill, to_ill, ifindex);
18137 
18138 	return (err);
18139 }
18140 
18141 /*
18142  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
18143  * Also checks for the validity of the arguments.
18144  * Note: We are already exclusive inside the from group.
18145  * It is upto the caller to release refcnt on the to_ill's.
18146  */
18147 static int
18148 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
18149     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
18150 {
18151 	int dst_index;
18152 	ipif_t *ipif_v4, *ipif_v6;
18153 	struct lifreq *lifr;
18154 	mblk_t *mp1;
18155 	boolean_t exists;
18156 	sin_t	*sin;
18157 	int	err = 0;
18158 	ip_stack_t	*ipst;
18159 
18160 	if (CONN_Q(q))
18161 		ipst = CONNQ_TO_IPST(q);
18162 	else
18163 		ipst = ILLQ_TO_IPST(q);
18164 
18165 
18166 	if ((mp1 = mp->b_cont) == NULL)
18167 		return (EPROTO);
18168 
18169 	if ((mp1 = mp1->b_cont) == NULL)
18170 		return (EPROTO);
18171 
18172 	lifr = (struct lifreq *)mp1->b_rptr;
18173 	sin = (sin_t *)&lifr->lifr_addr;
18174 
18175 	/*
18176 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
18177 	 * specific operations.
18178 	 */
18179 	if (sin->sin_family != AF_UNSPEC)
18180 		return (EINVAL);
18181 
18182 	/*
18183 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
18184 	 * NULLs for the last 4 args and we know the lookup won't fail
18185 	 * with EINPROGRESS.
18186 	 */
18187 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
18188 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
18189 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
18190 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
18191 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
18192 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
18193 
18194 	if (ipif_v4 == NULL && ipif_v6 == NULL)
18195 		return (ENXIO);
18196 
18197 	if (ipif_v4 != NULL) {
18198 		ASSERT(ipif_v4->ipif_refcnt != 0);
18199 		if (ipif_v4->ipif_id != 0) {
18200 			err = EINVAL;
18201 			goto done;
18202 		}
18203 
18204 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
18205 		*ill_from_v4 = ipif_v4->ipif_ill;
18206 	}
18207 
18208 	if (ipif_v6 != NULL) {
18209 		ASSERT(ipif_v6->ipif_refcnt != 0);
18210 		if (ipif_v6->ipif_id != 0) {
18211 			err = EINVAL;
18212 			goto done;
18213 		}
18214 
18215 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
18216 		*ill_from_v6 = ipif_v6->ipif_ill;
18217 	}
18218 
18219 	err = 0;
18220 	dst_index = lifr->lifr_movetoindex;
18221 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
18222 	    q, mp, ip_process_ioctl, &err, ipst);
18223 	if (err != 0) {
18224 		/*
18225 		 * There could be only v6.
18226 		 */
18227 		if (err != ENXIO)
18228 			goto done;
18229 		err = 0;
18230 	}
18231 
18232 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
18233 	    q, mp, ip_process_ioctl, &err, ipst);
18234 	if (err != 0) {
18235 		if (err != ENXIO)
18236 			goto done;
18237 		if (*ill_to_v4 == NULL) {
18238 			err = ENXIO;
18239 			goto done;
18240 		}
18241 		err = 0;
18242 	}
18243 
18244 	/*
18245 	 * If we have something to MOVE i.e "from" not NULL,
18246 	 * "to" should be non-NULL.
18247 	 */
18248 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
18249 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
18250 		err = EINVAL;
18251 	}
18252 
18253 done:
18254 	if (ipif_v4 != NULL)
18255 		ipif_refrele(ipif_v4);
18256 	if (ipif_v6 != NULL)
18257 		ipif_refrele(ipif_v6);
18258 	return (err);
18259 }
18260 
18261 /*
18262  * FAILOVER and FAILBACK are modelled as MOVE operations.
18263  *
18264  * We don't check whether the MOVE is within the same group or
18265  * not, because this ioctl can be used as a generic mechanism
18266  * to failover from interface A to B, though things will function
18267  * only if they are really part of the same group. Moreover,
18268  * all ipifs may be down and hence temporarily out of the group.
18269  *
18270  * ipif's that need to be moved are first brought down; V4 ipifs are brought
18271  * down first and then V6.  For each we wait for the ipif's to become quiescent.
18272  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
18273  * have been deleted and there are no active references. Once quiescent the
18274  * ipif's are moved and brought up on the new ill.
18275  *
18276  * Normally the source ill and destination ill belong to the same IPMP group
18277  * and hence the same ipsq_t. In the event they don't belong to the same
18278  * same group the two ipsq's are first merged into one ipsq - that of the
18279  * to_ill. The multicast memberships on the source and destination ill cannot
18280  * change during the move operation since multicast joins/leaves also have to
18281  * execute on the same ipsq and are hence serialized.
18282  */
18283 /* ARGSUSED */
18284 int
18285 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18286     ip_ioctl_cmd_t *ipip, void *ifreq)
18287 {
18288 	ill_t *ill_to_v4 = NULL;
18289 	ill_t *ill_to_v6 = NULL;
18290 	ill_t *ill_from_v4 = NULL;
18291 	ill_t *ill_from_v6 = NULL;
18292 	int err = 0;
18293 
18294 	/*
18295 	 * setup from and to ill's, we can get EINPROGRESS only for
18296 	 * to_ill's.
18297 	 */
18298 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
18299 	    &ill_to_v4, &ill_to_v6);
18300 
18301 	if (err != 0) {
18302 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
18303 		goto done;
18304 	}
18305 
18306 	/*
18307 	 * nothing to do.
18308 	 */
18309 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
18310 		goto done;
18311 	}
18312 
18313 	/*
18314 	 * nothing to do.
18315 	 */
18316 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
18317 		goto done;
18318 	}
18319 
18320 	/*
18321 	 * Mark the ill as changing.
18322 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
18323 	 * in ill_up_ipifs in case of error they are cleared below.
18324 	 */
18325 
18326 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18327 	if (ill_from_v4 != NULL)
18328 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
18329 	if (ill_from_v6 != NULL)
18330 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
18331 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18332 
18333 	/*
18334 	 * Make sure that both src and dst are
18335 	 * in the same syncq group. If not make it happen.
18336 	 * We are not holding any locks because we are the writer
18337 	 * on the from_ipsq and we will hold locks in ill_merge_groups
18338 	 * to protect to_ipsq against changing.
18339 	 */
18340 	if (ill_from_v4 != NULL) {
18341 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
18342 		    ill_to_v4->ill_phyint->phyint_ipsq) {
18343 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
18344 			    NULL, mp, q);
18345 			goto err_ret;
18346 
18347 		}
18348 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
18349 	} else {
18350 
18351 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
18352 		    ill_to_v6->ill_phyint->phyint_ipsq) {
18353 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
18354 			    NULL, mp, q);
18355 			goto err_ret;
18356 
18357 		}
18358 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
18359 	}
18360 
18361 	/*
18362 	 * Now that the ipsq's have been merged and we are the writer
18363 	 * lets mark to_ill as changing as well.
18364 	 */
18365 
18366 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18367 	if (ill_to_v4 != NULL)
18368 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
18369 	if (ill_to_v6 != NULL)
18370 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
18371 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18372 
18373 	/*
18374 	 * Its ok for us to proceed with the move even if
18375 	 * ill_pending_mp is non null on one of the from ill's as the reply
18376 	 * should not be looking at the ipif, it should only care about the
18377 	 * ill itself.
18378 	 */
18379 
18380 	/*
18381 	 * lets move ipv4 first.
18382 	 */
18383 	if (ill_from_v4 != NULL) {
18384 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18385 		ill_from_v4->ill_move_in_progress = B_TRUE;
18386 		ill_to_v4->ill_move_in_progress = B_TRUE;
18387 		ill_to_v4->ill_move_peer = ill_from_v4;
18388 		ill_from_v4->ill_move_peer = ill_to_v4;
18389 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18390 	}
18391 
18392 	/*
18393 	 * Now lets move ipv6.
18394 	 */
18395 	if (err == 0 && ill_from_v6 != NULL) {
18396 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18397 		ill_from_v6->ill_move_in_progress = B_TRUE;
18398 		ill_to_v6->ill_move_in_progress = B_TRUE;
18399 		ill_to_v6->ill_move_peer = ill_from_v6;
18400 		ill_from_v6->ill_move_peer = ill_to_v6;
18401 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18402 	}
18403 
18404 err_ret:
18405 	/*
18406 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18407 	 * moved to become quiescent.
18408 	 */
18409 	if (err == EINPROGRESS) {
18410 		goto done;
18411 	}
18412 
18413 	/*
18414 	 * if err is set ill_up_ipifs will not be called
18415 	 * lets clear the flags.
18416 	 */
18417 
18418 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18419 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18420 	/*
18421 	 * Some of the clearing may be redundant. But it is simple
18422 	 * not making any extra checks.
18423 	 */
18424 	if (ill_from_v6 != NULL) {
18425 		ill_from_v6->ill_move_in_progress = B_FALSE;
18426 		ill_from_v6->ill_move_peer = NULL;
18427 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18428 	}
18429 	if (ill_from_v4 != NULL) {
18430 		ill_from_v4->ill_move_in_progress = B_FALSE;
18431 		ill_from_v4->ill_move_peer = NULL;
18432 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18433 	}
18434 	if (ill_to_v6 != NULL) {
18435 		ill_to_v6->ill_move_in_progress = B_FALSE;
18436 		ill_to_v6->ill_move_peer = NULL;
18437 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18438 	}
18439 	if (ill_to_v4 != NULL) {
18440 		ill_to_v4->ill_move_in_progress = B_FALSE;
18441 		ill_to_v4->ill_move_peer = NULL;
18442 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18443 	}
18444 
18445 	/*
18446 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18447 	 * Do this always to maintain proper state i.e even in case of errors.
18448 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18449 	 * we need not call on both v4 and v6 interfaces.
18450 	 */
18451 	if (ill_from_v4 != NULL) {
18452 		if ((ill_from_v4->ill_phyint->phyint_flags &
18453 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18454 			phyint_inactive(ill_from_v4->ill_phyint);
18455 		}
18456 	} else if (ill_from_v6 != NULL) {
18457 		if ((ill_from_v6->ill_phyint->phyint_flags &
18458 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18459 			phyint_inactive(ill_from_v6->ill_phyint);
18460 		}
18461 	}
18462 
18463 	if (ill_to_v4 != NULL) {
18464 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18465 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18466 		}
18467 	} else if (ill_to_v6 != NULL) {
18468 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18469 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18470 		}
18471 	}
18472 
18473 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18474 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18475 
18476 no_err:
18477 	/*
18478 	 * lets bring the interfaces up on the to_ill.
18479 	 */
18480 	if (err == 0) {
18481 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18482 		    q, mp);
18483 	}
18484 
18485 	if (err == 0) {
18486 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18487 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18488 
18489 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18490 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18491 	}
18492 done:
18493 
18494 	if (ill_to_v4 != NULL) {
18495 		ill_refrele(ill_to_v4);
18496 	}
18497 	if (ill_to_v6 != NULL) {
18498 		ill_refrele(ill_to_v6);
18499 	}
18500 
18501 	return (err);
18502 }
18503 
18504 static void
18505 ill_dl_down(ill_t *ill)
18506 {
18507 	/*
18508 	 * The ill is down; unbind but stay attached since we're still
18509 	 * associated with a PPA. If we have negotiated DLPI capabilites
18510 	 * with the data link service provider (IDS_OK) then reset them.
18511 	 * The interval between unbinding and rebinding is potentially
18512 	 * unbounded hence we cannot assume things will be the same.
18513 	 * The DLPI capabilities will be probed again when the data link
18514 	 * is brought up.
18515 	 */
18516 	mblk_t	*mp = ill->ill_unbind_mp;
18517 	hook_nic_event_t *info;
18518 
18519 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18520 
18521 	ill->ill_unbind_mp = NULL;
18522 	if (mp != NULL) {
18523 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18524 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18525 		    ill->ill_name));
18526 		mutex_enter(&ill->ill_lock);
18527 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18528 		mutex_exit(&ill->ill_lock);
18529 		if (ill->ill_dlpi_capab_state == IDS_OK)
18530 			ill_capability_reset(ill);
18531 		ill_dlpi_send(ill, mp);
18532 	}
18533 
18534 	/*
18535 	 * Toss all of our multicast memberships.  We could keep them, but
18536 	 * then we'd have to do bookkeeping of any joins and leaves performed
18537 	 * by the application while the the interface is down (we can't just
18538 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18539 	 * on a downed interface).
18540 	 */
18541 	ill_leave_multicast(ill);
18542 
18543 	mutex_enter(&ill->ill_lock);
18544 
18545 	ill->ill_dl_up = 0;
18546 
18547 	if ((info = ill->ill_nic_event_info) != NULL) {
18548 		ip2dbg(("ill_dl_down:unexpected nic event %d attached for %s\n",
18549 		    info->hne_event, ill->ill_name));
18550 		if (info->hne_data != NULL)
18551 			kmem_free(info->hne_data, info->hne_datalen);
18552 		kmem_free(info, sizeof (hook_nic_event_t));
18553 	}
18554 
18555 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
18556 	if (info != NULL) {
18557 		ip_stack_t	*ipst = ill->ill_ipst;
18558 
18559 		info->hne_nic = ill->ill_phyint->phyint_hook_ifindex;
18560 		info->hne_lif = 0;
18561 		info->hne_event = NE_DOWN;
18562 		info->hne_data = NULL;
18563 		info->hne_datalen = 0;
18564 		info->hne_family = ill->ill_isv6 ?
18565 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
18566 	} else
18567 		ip2dbg(("ill_dl_down: could not attach DOWN nic event "
18568 		    "information for %s (ENOMEM)\n", ill->ill_name));
18569 
18570 	ill->ill_nic_event_info = info;
18571 
18572 	mutex_exit(&ill->ill_lock);
18573 }
18574 
18575 static void
18576 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18577 {
18578 	union DL_primitives *dlp;
18579 	t_uscalar_t prim;
18580 
18581 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18582 
18583 	dlp = (union DL_primitives *)mp->b_rptr;
18584 	prim = dlp->dl_primitive;
18585 
18586 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18587 	    dlpi_prim_str(prim), prim, ill->ill_name));
18588 
18589 	switch (prim) {
18590 	case DL_PHYS_ADDR_REQ:
18591 	{
18592 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18593 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18594 		break;
18595 	}
18596 	case DL_BIND_REQ:
18597 		mutex_enter(&ill->ill_lock);
18598 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18599 		mutex_exit(&ill->ill_lock);
18600 		break;
18601 	}
18602 
18603 	/*
18604 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18605 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18606 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18607 	 */
18608 	mutex_enter(&ill->ill_lock);
18609 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18610 	    (prim == DL_UNBIND_REQ)) {
18611 		ill->ill_dlpi_pending = prim;
18612 	}
18613 	mutex_exit(&ill->ill_lock);
18614 
18615 	putnext(ill->ill_wq, mp);
18616 }
18617 
18618 /*
18619  * Helper function for ill_dlpi_send().
18620  */
18621 /* ARGSUSED */
18622 static void
18623 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18624 {
18625 	ill_dlpi_send((ill_t *)q->q_ptr, mp);
18626 }
18627 
18628 /*
18629  * Send a DLPI control message to the driver but make sure there
18630  * is only one outstanding message. Uses ill_dlpi_pending to tell
18631  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18632  * when an ACK or a NAK is received to process the next queued message.
18633  */
18634 void
18635 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18636 {
18637 	mblk_t **mpp;
18638 
18639 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18640 
18641 	/*
18642 	 * To ensure that any DLPI requests for current exclusive operation
18643 	 * are always completely sent before any DLPI messages for other
18644 	 * operations, require writer access before enqueuing.
18645 	 */
18646 	if (!IAM_WRITER_ILL(ill)) {
18647 		ill_refhold(ill);
18648 		/* qwriter_ip() does the ill_refrele() */
18649 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18650 		    NEW_OP, B_TRUE);
18651 		return;
18652 	}
18653 
18654 	mutex_enter(&ill->ill_lock);
18655 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18656 		/* Must queue message. Tail insertion */
18657 		mpp = &ill->ill_dlpi_deferred;
18658 		while (*mpp != NULL)
18659 			mpp = &((*mpp)->b_next);
18660 
18661 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18662 		    ill->ill_name));
18663 
18664 		*mpp = mp;
18665 		mutex_exit(&ill->ill_lock);
18666 		return;
18667 	}
18668 	mutex_exit(&ill->ill_lock);
18669 	ill_dlpi_dispatch(ill, mp);
18670 }
18671 
18672 /*
18673  * Send all deferred DLPI messages without waiting for their ACKs.
18674  */
18675 void
18676 ill_dlpi_send_deferred(ill_t *ill)
18677 {
18678 	mblk_t *mp, *nextmp;
18679 
18680 	/*
18681 	 * Clear ill_dlpi_pending so that the message is not queued in
18682 	 * ill_dlpi_send().
18683 	 */
18684 	mutex_enter(&ill->ill_lock);
18685 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18686 	mp = ill->ill_dlpi_deferred;
18687 	ill->ill_dlpi_deferred = NULL;
18688 	mutex_exit(&ill->ill_lock);
18689 
18690 	for (; mp != NULL; mp = nextmp) {
18691 		nextmp = mp->b_next;
18692 		mp->b_next = NULL;
18693 		ill_dlpi_send(ill, mp);
18694 	}
18695 }
18696 
18697 /*
18698  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18699  */
18700 boolean_t
18701 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18702 {
18703 	t_uscalar_t prim_pending;
18704 
18705 	mutex_enter(&ill->ill_lock);
18706 	prim_pending = ill->ill_dlpi_pending;
18707 	mutex_exit(&ill->ill_lock);
18708 
18709 	/*
18710 	 * During teardown, ill_dlpi_send_deferred() will send requests
18711 	 * without waiting; don't bother printing any warnings in that case.
18712 	 */
18713 	if (!(ill->ill_flags & ILL_CONDEMNED) && prim_pending != prim) {
18714 		if (prim_pending == DL_PRIM_INVAL) {
18715 			(void) mi_strlog(ill->ill_rq, 1,
18716 			    SL_CONSOLE|SL_ERROR|SL_TRACE, "ip: received "
18717 			    "unsolicited ack for %s on %s\n",
18718 			    dlpi_prim_str(prim), ill->ill_name);
18719 		} else {
18720 			(void) mi_strlog(ill->ill_rq, 1,
18721 			    SL_CONSOLE|SL_ERROR|SL_TRACE, "ip: received "
18722 			    "unexpected ack for %s on %s (expecting %s)\n",
18723 			    dlpi_prim_str(prim), ill->ill_name,
18724 			    dlpi_prim_str(prim_pending));
18725 		}
18726 	}
18727 	return (prim_pending == prim);
18728 }
18729 
18730 /*
18731  * Called when an DLPI control message has been acked or nacked to
18732  * send down the next queued message (if any).
18733  */
18734 void
18735 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18736 {
18737 	mblk_t *mp;
18738 
18739 	ASSERT(IAM_WRITER_ILL(ill));
18740 	mutex_enter(&ill->ill_lock);
18741 
18742 	ASSERT(prim != DL_PRIM_INVAL);
18743 	ASSERT(ill->ill_dlpi_pending == prim);
18744 
18745 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18746 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18747 
18748 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18749 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18750 		cv_signal(&ill->ill_cv);
18751 		mutex_exit(&ill->ill_lock);
18752 		return;
18753 	}
18754 
18755 	ill->ill_dlpi_deferred = mp->b_next;
18756 	mp->b_next = NULL;
18757 	mutex_exit(&ill->ill_lock);
18758 
18759 	ill_dlpi_dispatch(ill, mp);
18760 }
18761 
18762 void
18763 conn_delete_ire(conn_t *connp, caddr_t arg)
18764 {
18765 	ipif_t	*ipif = (ipif_t *)arg;
18766 	ire_t	*ire;
18767 
18768 	/*
18769 	 * Look at the cached ires on conns which has pointers to ipifs.
18770 	 * We just call ire_refrele which clears up the reference
18771 	 * to ire. Called when a conn closes. Also called from ipif_free
18772 	 * to cleanup indirect references to the stale ipif via the cached ire.
18773 	 */
18774 	mutex_enter(&connp->conn_lock);
18775 	ire = connp->conn_ire_cache;
18776 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18777 		connp->conn_ire_cache = NULL;
18778 		mutex_exit(&connp->conn_lock);
18779 		IRE_REFRELE_NOTR(ire);
18780 		return;
18781 	}
18782 	mutex_exit(&connp->conn_lock);
18783 
18784 }
18785 
18786 /*
18787  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18788  * of IREs. Those IREs may have been previously cached in the conn structure.
18789  * This ipcl_walk() walker function releases all references to such IREs based
18790  * on the condemned flag.
18791  */
18792 /* ARGSUSED */
18793 void
18794 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18795 {
18796 	ire_t	*ire;
18797 
18798 	mutex_enter(&connp->conn_lock);
18799 	ire = connp->conn_ire_cache;
18800 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18801 		connp->conn_ire_cache = NULL;
18802 		mutex_exit(&connp->conn_lock);
18803 		IRE_REFRELE_NOTR(ire);
18804 		return;
18805 	}
18806 	mutex_exit(&connp->conn_lock);
18807 }
18808 
18809 /*
18810  * Take down a specific interface, but don't lose any information about it.
18811  * Also delete interface from its interface group (ifgrp).
18812  * (Always called as writer.)
18813  * This function goes through the down sequence even if the interface is
18814  * already down. There are 2 reasons.
18815  * a. Currently we permit interface routes that depend on down interfaces
18816  *    to be added. This behaviour itself is questionable. However it appears
18817  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18818  *    time. We go thru the cleanup in order to remove these routes.
18819  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18820  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18821  *    down, but we need to cleanup i.e. do ill_dl_down and
18822  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18823  *
18824  * IP-MT notes:
18825  *
18826  * Model of reference to interfaces.
18827  *
18828  * The following members in ipif_t track references to the ipif.
18829  *	int     ipif_refcnt;    Active reference count
18830  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18831  * The following members in ill_t track references to the ill.
18832  *	int             ill_refcnt;     active refcnt
18833  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18834  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18835  *
18836  * Reference to an ipif or ill can be obtained in any of the following ways.
18837  *
18838  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18839  * Pointers to ipif / ill from other data structures viz ire and conn.
18840  * Implicit reference to the ipif / ill by holding a reference to the ire.
18841  *
18842  * The ipif/ill lookup functions return a reference held ipif / ill.
18843  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18844  * This is a purely dynamic reference count associated with threads holding
18845  * references to the ipif / ill. Pointers from other structures do not
18846  * count towards this reference count.
18847  *
18848  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
18849  * ipif/ill. This is incremented whenever a new ire is created referencing the
18850  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
18851  * actually added to the ire hash table. The count is decremented in
18852  * ire_inactive where the ire is destroyed.
18853  *
18854  * nce's reference ill's thru nce_ill and the count of nce's associated with
18855  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18856  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18857  * table. Similarly it is decremented in ndp_inactive() where the nce
18858  * is destroyed.
18859  *
18860  * Flow of ioctls involving interface down/up
18861  *
18862  * The following is the sequence of an attempt to set some critical flags on an
18863  * up interface.
18864  * ip_sioctl_flags
18865  * ipif_down
18866  * wait for ipif to be quiescent
18867  * ipif_down_tail
18868  * ip_sioctl_flags_tail
18869  *
18870  * All set ioctls that involve down/up sequence would have a skeleton similar
18871  * to the above. All the *tail functions are called after the refcounts have
18872  * dropped to the appropriate values.
18873  *
18874  * The mechanism to quiesce an ipif is as follows.
18875  *
18876  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18877  * on the ipif. Callers either pass a flag requesting wait or the lookup
18878  *  functions will return NULL.
18879  *
18880  * Delete all ires referencing this ipif
18881  *
18882  * Any thread attempting to do an ipif_refhold on an ipif that has been
18883  * obtained thru a cached pointer will first make sure that
18884  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18885  * increment the refcount.
18886  *
18887  * The above guarantees that the ipif refcount will eventually come down to
18888  * zero and the ipif will quiesce, once all threads that currently hold a
18889  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18890  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18891  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
18892  * drop to zero.
18893  *
18894  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18895  *
18896  * Threads trying to lookup an ipif or ill can pass a flag requesting
18897  * wait and restart if the ipif / ill cannot be looked up currently.
18898  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18899  * failure if the ipif is currently undergoing an exclusive operation, and
18900  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18901  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18902  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18903  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18904  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18905  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18906  * until we release the ipsq_lock, even though the the ill/ipif state flags
18907  * can change after we drop the ill_lock.
18908  *
18909  * An attempt to send out a packet using an ipif that is currently
18910  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18911  * operation and restart it later when the exclusive condition on the ipif ends.
18912  * This is an example of not passing the wait flag to the lookup functions. For
18913  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18914  * out a multicast packet on that ipif will fail while the ipif is
18915  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18916  * currently IPIF_CHANGING will also fail.
18917  */
18918 int
18919 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18920 {
18921 	ill_t		*ill = ipif->ipif_ill;
18922 	phyint_t	*phyi;
18923 	conn_t		*connp;
18924 	boolean_t	success;
18925 	boolean_t	ipif_was_up = B_FALSE;
18926 	ip_stack_t	*ipst = ill->ill_ipst;
18927 
18928 	ASSERT(IAM_WRITER_IPIF(ipif));
18929 
18930 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18931 
18932 	if (ipif->ipif_flags & IPIF_UP) {
18933 		mutex_enter(&ill->ill_lock);
18934 		ipif->ipif_flags &= ~IPIF_UP;
18935 		ASSERT(ill->ill_ipif_up_count > 0);
18936 		--ill->ill_ipif_up_count;
18937 		mutex_exit(&ill->ill_lock);
18938 		ipif_was_up = B_TRUE;
18939 		/* Update status in SCTP's list */
18940 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18941 	}
18942 
18943 	/*
18944 	 * Blow away v6 memberships we established in ipif_multicast_up(); the
18945 	 * v4 ones are left alone (as is the ipif_multicast_up flag, so we
18946 	 * know not to rejoin when the interface is brought back up).
18947 	 */
18948 	if (ipif->ipif_isv6)
18949 		ipif_multicast_down(ipif);
18950 	/*
18951 	 * Remove from the mapping for __sin6_src_id. We insert only
18952 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18953 	 * stored as mapped addresses, we need to check for mapped
18954 	 * INADDR_ANY also.
18955 	 */
18956 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18957 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18958 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18959 		int err;
18960 
18961 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18962 		    ipif->ipif_zoneid, ipst);
18963 		if (err != 0) {
18964 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18965 		}
18966 	}
18967 
18968 	/*
18969 	 * Before we delete the ill from the group (if any), we need
18970 	 * to make sure that we delete all the routes dependent on
18971 	 * this and also any ipifs dependent on this ipif for
18972 	 * source address. We need to do before we delete from
18973 	 * the group because
18974 	 *
18975 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18976 	 *
18977 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18978 	 *    for re-doing source address selection. Note that
18979 	 *    ipif_select_source[_v6] called from
18980 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18981 	 *    because we have already marked down here i.e cleared
18982 	 *    IPIF_UP.
18983 	 */
18984 	if (ipif->ipif_isv6) {
18985 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18986 		    ipst);
18987 	} else {
18988 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18989 		    ipst);
18990 	}
18991 
18992 	/*
18993 	 * Need to add these also to be saved and restored when the
18994 	 * ipif is brought down and up
18995 	 */
18996 	mutex_enter(&ipst->ips_ire_mrtun_lock);
18997 	if (ipst->ips_ire_mrtun_count != 0) {
18998 		mutex_exit(&ipst->ips_ire_mrtun_lock);
18999 		ire_walk_ill_mrtun(0, 0, ipif_down_delete_ire,
19000 		    (char *)ipif, NULL, ipst);
19001 	} else {
19002 		mutex_exit(&ipst->ips_ire_mrtun_lock);
19003 	}
19004 
19005 	mutex_enter(&ipst->ips_ire_srcif_table_lock);
19006 	if (ipst->ips_ire_srcif_table_count > 0) {
19007 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
19008 		ire_walk_srcif_table_v4(ipif_down_delete_ire, (char *)ipif,
19009 		    ipst);
19010 	} else {
19011 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
19012 	}
19013 
19014 	/*
19015 	 * Cleaning up the conn_ire_cache or conns must be done only after the
19016 	 * ires have been deleted above. Otherwise a thread could end up
19017 	 * caching an ire in a conn after we have finished the cleanup of the
19018 	 * conn. The caching is done after making sure that the ire is not yet
19019 	 * condemned. Also documented in the block comment above ip_output
19020 	 */
19021 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
19022 	/* Also, delete the ires cached in SCTP */
19023 	sctp_ire_cache_flush(ipif);
19024 
19025 	/* Resolve any IPsec/IKE NAT-T instances that depend on this ipif. */
19026 	nattymod_clean_ipif(ipif);
19027 
19028 	/*
19029 	 * Update any other ipifs which have used "our" local address as
19030 	 * a source address. This entails removing and recreating IRE_INTERFACE
19031 	 * entries for such ipifs.
19032 	 */
19033 	if (ipif->ipif_isv6)
19034 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
19035 	else
19036 		ipif_update_other_ipifs(ipif, ill->ill_group);
19037 
19038 	if (ipif_was_up) {
19039 		/*
19040 		 * Check whether it is last ipif to leave this group.
19041 		 * If this is the last ipif to leave, we should remove
19042 		 * this ill from the group as ipif_select_source will not
19043 		 * be able to find any useful ipifs if this ill is selected
19044 		 * for load balancing.
19045 		 *
19046 		 * For nameless groups, we should call ifgrp_delete if this
19047 		 * belongs to some group. As this ipif is going down, we may
19048 		 * need to reconstruct groups.
19049 		 */
19050 		phyi = ill->ill_phyint;
19051 		/*
19052 		 * If the phyint_groupname_len is 0, it may or may not
19053 		 * be in the nameless group. If the phyint_groupname_len is
19054 		 * not 0, then this ill should be part of some group.
19055 		 * As we always insert this ill in the group if
19056 		 * phyint_groupname_len is not zero when the first ipif
19057 		 * comes up (in ipif_up_done), it should be in a group
19058 		 * when the namelen is not 0.
19059 		 *
19060 		 * NOTE : When we delete the ill from the group,it will
19061 		 * blow away all the IRE_CACHES pointing either at this ipif or
19062 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
19063 		 * should be pointing at this ill.
19064 		 */
19065 		ASSERT(phyi->phyint_groupname_len == 0 ||
19066 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
19067 
19068 		if (phyi->phyint_groupname_len != 0) {
19069 			if (ill->ill_ipif_up_count == 0)
19070 				illgrp_delete(ill);
19071 		}
19072 
19073 		/*
19074 		 * If we have deleted some of the broadcast ires associated
19075 		 * with this ipif, we need to re-nominate somebody else if
19076 		 * the ires that we deleted were the nominated ones.
19077 		 */
19078 		if (ill->ill_group != NULL && !ill->ill_isv6)
19079 			ipif_renominate_bcast(ipif);
19080 	}
19081 
19082 	/*
19083 	 * neighbor-discovery or arp entries for this interface.
19084 	 */
19085 	ipif_ndp_down(ipif);
19086 
19087 	/*
19088 	 * If mp is NULL the caller will wait for the appropriate refcnt.
19089 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
19090 	 * and ill_delete -> ipif_free -> ipif_down
19091 	 */
19092 	if (mp == NULL) {
19093 		ASSERT(q == NULL);
19094 		return (0);
19095 	}
19096 
19097 	if (CONN_Q(q)) {
19098 		connp = Q_TO_CONN(q);
19099 		mutex_enter(&connp->conn_lock);
19100 	} else {
19101 		connp = NULL;
19102 	}
19103 	mutex_enter(&ill->ill_lock);
19104 	/*
19105 	 * Are there any ire's pointing to this ipif that are still active ?
19106 	 * If this is the last ipif going down, are there any ire's pointing
19107 	 * to this ill that are still active ?
19108 	 */
19109 	if (ipif_is_quiescent(ipif)) {
19110 		mutex_exit(&ill->ill_lock);
19111 		if (connp != NULL)
19112 			mutex_exit(&connp->conn_lock);
19113 		return (0);
19114 	}
19115 
19116 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
19117 	    ill->ill_name, (void *)ill));
19118 	/*
19119 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
19120 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
19121 	 * which in turn is called by the last refrele on the ipif/ill/ire.
19122 	 */
19123 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
19124 	if (!success) {
19125 		/* The conn is closing. So just return */
19126 		ASSERT(connp != NULL);
19127 		mutex_exit(&ill->ill_lock);
19128 		mutex_exit(&connp->conn_lock);
19129 		return (EINTR);
19130 	}
19131 
19132 	mutex_exit(&ill->ill_lock);
19133 	if (connp != NULL)
19134 		mutex_exit(&connp->conn_lock);
19135 	return (EINPROGRESS);
19136 }
19137 
19138 void
19139 ipif_down_tail(ipif_t *ipif)
19140 {
19141 	ill_t	*ill = ipif->ipif_ill;
19142 
19143 	/*
19144 	 * Skip any loopback interface (null wq).
19145 	 * If this is the last logical interface on the ill
19146 	 * have ill_dl_down tell the driver we are gone (unbind)
19147 	 * Note that lun 0 can ipif_down even though
19148 	 * there are other logical units that are up.
19149 	 * This occurs e.g. when we change a "significant" IFF_ flag.
19150 	 */
19151 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
19152 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
19153 	    ill->ill_dl_up) {
19154 		ill_dl_down(ill);
19155 	}
19156 	ill->ill_logical_down = 0;
19157 
19158 	/*
19159 	 * Have to be after removing the routes in ipif_down_delete_ire.
19160 	 */
19161 	if (ipif->ipif_isv6) {
19162 		if (ill->ill_flags & ILLF_XRESOLV)
19163 			ipif_arp_down(ipif);
19164 	} else {
19165 		ipif_arp_down(ipif);
19166 	}
19167 
19168 	ip_rts_ifmsg(ipif);
19169 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
19170 }
19171 
19172 /*
19173  * Bring interface logically down without bringing the physical interface
19174  * down e.g. when the netmask is changed. This avoids long lasting link
19175  * negotiations between an ethernet interface and a certain switches.
19176  */
19177 static int
19178 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
19179 {
19180 	/*
19181 	 * The ill_logical_down flag is a transient flag. It is set here
19182 	 * and is cleared once the down has completed in ipif_down_tail.
19183 	 * This flag does not indicate whether the ill stream is in the
19184 	 * DL_BOUND state with the driver. Instead this flag is used by
19185 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
19186 	 * the driver. The state of the ill stream i.e. whether it is
19187 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
19188 	 */
19189 	ipif->ipif_ill->ill_logical_down = 1;
19190 	return (ipif_down(ipif, q, mp));
19191 }
19192 
19193 /*
19194  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
19195  * If the usesrc client ILL is already part of a usesrc group or not,
19196  * in either case a ire_stq with the matching usesrc client ILL will
19197  * locate the IRE's that need to be deleted. We want IREs to be created
19198  * with the new source address.
19199  */
19200 static void
19201 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
19202 {
19203 	ill_t	*ucill = (ill_t *)ill_arg;
19204 
19205 	ASSERT(IAM_WRITER_ILL(ucill));
19206 
19207 	if (ire->ire_stq == NULL)
19208 		return;
19209 
19210 	if ((ire->ire_type == IRE_CACHE) &&
19211 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
19212 		ire_delete(ire);
19213 }
19214 
19215 /*
19216  * ire_walk routine to delete every IRE dependent on the interface
19217  * address that is going down.	(Always called as writer.)
19218  * Works for both v4 and v6.
19219  * In addition for checking for ire_ipif matches it also checks for
19220  * IRE_CACHE entries which have the same source address as the
19221  * disappearing ipif since ipif_select_source might have picked
19222  * that source. Note that ipif_down/ipif_update_other_ipifs takes
19223  * care of any IRE_INTERFACE with the disappearing source address.
19224  */
19225 static void
19226 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
19227 {
19228 	ipif_t	*ipif = (ipif_t *)ipif_arg;
19229 	ill_t *ire_ill;
19230 	ill_t *ipif_ill;
19231 
19232 	ASSERT(IAM_WRITER_IPIF(ipif));
19233 	if (ire->ire_ipif == NULL)
19234 		return;
19235 
19236 	/*
19237 	 * For IPv4, we derive source addresses for an IRE from ipif's
19238 	 * belonging to the same IPMP group as the IRE's outgoing
19239 	 * interface.  If an IRE's outgoing interface isn't in the
19240 	 * same IPMP group as a particular ipif, then that ipif
19241 	 * couldn't have been used as a source address for this IRE.
19242 	 *
19243 	 * For IPv6, source addresses are only restricted to the IPMP group
19244 	 * if the IRE is for a link-local address or a multicast address.
19245 	 * Otherwise, source addresses for an IRE can be chosen from
19246 	 * interfaces other than the the outgoing interface for that IRE.
19247 	 *
19248 	 * For source address selection details, see ipif_select_source()
19249 	 * and ipif_select_source_v6().
19250 	 */
19251 	if (ire->ire_ipversion == IPV4_VERSION ||
19252 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
19253 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
19254 		ire_ill = ire->ire_ipif->ipif_ill;
19255 		ipif_ill = ipif->ipif_ill;
19256 
19257 		if (ire_ill->ill_group != ipif_ill->ill_group) {
19258 			return;
19259 		}
19260 	}
19261 
19262 
19263 	if (ire->ire_ipif != ipif) {
19264 		/*
19265 		 * Look for a matching source address.
19266 		 */
19267 		if (ire->ire_type != IRE_CACHE)
19268 			return;
19269 		if (ipif->ipif_flags & IPIF_NOLOCAL)
19270 			return;
19271 
19272 		if (ire->ire_ipversion == IPV4_VERSION) {
19273 			if (ire->ire_src_addr != ipif->ipif_src_addr)
19274 				return;
19275 		} else {
19276 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
19277 			    &ipif->ipif_v6lcl_addr))
19278 				return;
19279 		}
19280 		ire_delete(ire);
19281 		return;
19282 	}
19283 	/*
19284 	 * ire_delete() will do an ire_flush_cache which will delete
19285 	 * all ire_ipif matches
19286 	 */
19287 	ire_delete(ire);
19288 }
19289 
19290 /*
19291  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
19292  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
19293  * 2) when an interface is brought up or down (on that ill).
19294  * This ensures that the IRE_CACHE entries don't retain stale source
19295  * address selection results.
19296  */
19297 void
19298 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
19299 {
19300 	ill_t	*ill = (ill_t *)ill_arg;
19301 	ill_t	*ipif_ill;
19302 
19303 	ASSERT(IAM_WRITER_ILL(ill));
19304 	/*
19305 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19306 	 * Hence this should be IRE_CACHE.
19307 	 */
19308 	ASSERT(ire->ire_type == IRE_CACHE);
19309 
19310 	/*
19311 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
19312 	 * We are only interested in IRE_CACHES that has borrowed
19313 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
19314 	 * for which we need to look at ire_ipif->ipif_ill match
19315 	 * with ill.
19316 	 */
19317 	ASSERT(ire->ire_ipif != NULL);
19318 	ipif_ill = ire->ire_ipif->ipif_ill;
19319 	if (ipif_ill == ill || (ill->ill_group != NULL &&
19320 	    ipif_ill->ill_group == ill->ill_group)) {
19321 		ire_delete(ire);
19322 	}
19323 }
19324 
19325 /*
19326  * Delete all the ire whose stq references ill_arg.
19327  */
19328 static void
19329 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
19330 {
19331 	ill_t	*ill = (ill_t *)ill_arg;
19332 	ill_t	*ire_ill;
19333 
19334 	ASSERT(IAM_WRITER_ILL(ill));
19335 	/*
19336 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19337 	 * Hence this should be IRE_CACHE.
19338 	 */
19339 	ASSERT(ire->ire_type == IRE_CACHE);
19340 
19341 	/*
19342 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19343 	 * matches ill. We are only interested in IRE_CACHES that
19344 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
19345 	 * filtering here.
19346 	 */
19347 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
19348 
19349 	if (ire_ill == ill)
19350 		ire_delete(ire);
19351 }
19352 
19353 /*
19354  * This is called when an ill leaves the group. We want to delete
19355  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
19356  * pointing at ill.
19357  */
19358 static void
19359 illgrp_cache_delete(ire_t *ire, char *ill_arg)
19360 {
19361 	ill_t	*ill = (ill_t *)ill_arg;
19362 
19363 	ASSERT(IAM_WRITER_ILL(ill));
19364 	ASSERT(ill->ill_group == NULL);
19365 	/*
19366 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19367 	 * Hence this should be IRE_CACHE.
19368 	 */
19369 	ASSERT(ire->ire_type == IRE_CACHE);
19370 	/*
19371 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19372 	 * matches ill. We are interested in both.
19373 	 */
19374 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
19375 	    (ire->ire_ipif->ipif_ill == ill));
19376 
19377 	ire_delete(ire);
19378 }
19379 
19380 /*
19381  * Initiate deallocate of an IPIF. Always called as writer. Called by
19382  * ill_delete or ip_sioctl_removeif.
19383  */
19384 static void
19385 ipif_free(ipif_t *ipif)
19386 {
19387 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19388 
19389 	ASSERT(IAM_WRITER_IPIF(ipif));
19390 
19391 	if (ipif->ipif_recovery_id != 0)
19392 		(void) untimeout(ipif->ipif_recovery_id);
19393 	ipif->ipif_recovery_id = 0;
19394 
19395 	/* Remove conn references */
19396 	reset_conn_ipif(ipif);
19397 
19398 	/*
19399 	 * Make sure we have valid net and subnet broadcast ire's for the
19400 	 * other ipif's which share them with this ipif.
19401 	 */
19402 	if (!ipif->ipif_isv6)
19403 		ipif_check_bcast_ires(ipif);
19404 
19405 	/*
19406 	 * Take down the interface. We can be called either from ill_delete
19407 	 * or from ip_sioctl_removeif.
19408 	 */
19409 	(void) ipif_down(ipif, NULL, NULL);
19410 
19411 	/*
19412 	 * Now that the interface is down, there's no chance it can still
19413 	 * become a duplicate.  Cancel any timer that may have been set while
19414 	 * tearing down.
19415 	 */
19416 	if (ipif->ipif_recovery_id != 0)
19417 		(void) untimeout(ipif->ipif_recovery_id);
19418 	ipif->ipif_recovery_id = 0;
19419 
19420 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19421 	/* Remove pointers to this ill in the multicast routing tables */
19422 	reset_mrt_vif_ipif(ipif);
19423 	rw_exit(&ipst->ips_ill_g_lock);
19424 }
19425 
19426 /*
19427  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19428  * also ill_move().
19429  */
19430 static void
19431 ipif_free_tail(ipif_t *ipif)
19432 {
19433 	mblk_t	*mp;
19434 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19435 
19436 	/*
19437 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19438 	 */
19439 	mutex_enter(&ipif->ipif_saved_ire_lock);
19440 	mp = ipif->ipif_saved_ire_mp;
19441 	ipif->ipif_saved_ire_mp = NULL;
19442 	mutex_exit(&ipif->ipif_saved_ire_lock);
19443 	freemsg(mp);
19444 
19445 	/*
19446 	 * Need to hold both ill_g_lock and ill_lock while
19447 	 * inserting or removing an ipif from the linked list
19448 	 * of ipifs hanging off the ill.
19449 	 */
19450 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19451 	/*
19452 	 * Remove all multicast memberships on the interface now.
19453 	 * This removes IPv4 multicast memberships joined within
19454 	 * the kernel as ipif_down does not do ipif_multicast_down
19455 	 * for IPv4. IPv6 is not handled here as the multicast memberships
19456 	 * are based on ill and not on ipif.
19457 	 */
19458 	ilm_free(ipif);
19459 
19460 	/*
19461 	 * Since we held the ill_g_lock while doing the ilm_free above,
19462 	 * we can assert the ilms were really deleted and not just marked
19463 	 * ILM_DELETED.
19464 	 */
19465 	ASSERT(ilm_walk_ipif(ipif) == 0);
19466 
19467 	IPIF_TRACE_CLEANUP(ipif);
19468 
19469 	/* Ask SCTP to take it out of it list */
19470 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19471 
19472 	/* Get it out of the ILL interface list. */
19473 	ipif_remove(ipif, B_TRUE);
19474 	rw_exit(&ipst->ips_ill_g_lock);
19475 
19476 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19477 
19478 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19479 	ASSERT(ipif->ipif_recovery_id == 0);
19480 
19481 	/* Free the memory. */
19482 	mi_free(ipif);
19483 }
19484 
19485 /*
19486  * Returns an ipif name in the form "ill_name/unit" if ipif_id is not zero,
19487  * "ill_name" otherwise.
19488  */
19489 char *
19490 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19491 {
19492 	char	lbuf[32];
19493 	char	*name;
19494 	size_t	name_len;
19495 
19496 	buf[0] = '\0';
19497 	if (!ipif)
19498 		return (buf);
19499 	name = ipif->ipif_ill->ill_name;
19500 	name_len = ipif->ipif_ill->ill_name_length;
19501 	if (ipif->ipif_id != 0) {
19502 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19503 		    ipif->ipif_id);
19504 		name = lbuf;
19505 		name_len = mi_strlen(name) + 1;
19506 	}
19507 	len -= 1;
19508 	buf[len] = '\0';
19509 	len = MIN(len, name_len);
19510 	bcopy(name, buf, len);
19511 	return (buf);
19512 }
19513 
19514 /*
19515  * Find an IPIF based on the name passed in.  Names can be of the
19516  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19517  * The <phys> string can have forms like <dev><#> (e.g., le0),
19518  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19519  * When there is no colon, the implied unit id is zero. <phys> must
19520  * correspond to the name of an ILL.  (May be called as writer.)
19521  */
19522 static ipif_t *
19523 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19524     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19525     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19526 {
19527 	char	*cp;
19528 	char	*endp;
19529 	long	id;
19530 	ill_t	*ill;
19531 	ipif_t	*ipif;
19532 	uint_t	ire_type;
19533 	boolean_t did_alloc = B_FALSE;
19534 	ipsq_t	*ipsq;
19535 
19536 	if (error != NULL)
19537 		*error = 0;
19538 
19539 	/*
19540 	 * If the caller wants to us to create the ipif, make sure we have a
19541 	 * valid zoneid
19542 	 */
19543 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19544 
19545 	if (namelen == 0) {
19546 		if (error != NULL)
19547 			*error = ENXIO;
19548 		return (NULL);
19549 	}
19550 
19551 	*exists = B_FALSE;
19552 	/* Look for a colon in the name. */
19553 	endp = &name[namelen];
19554 	for (cp = endp; --cp > name; ) {
19555 		if (*cp == IPIF_SEPARATOR_CHAR)
19556 			break;
19557 	}
19558 
19559 	if (*cp == IPIF_SEPARATOR_CHAR) {
19560 		/*
19561 		 * Reject any non-decimal aliases for logical
19562 		 * interfaces. Aliases with leading zeroes
19563 		 * are also rejected as they introduce ambiguity
19564 		 * in the naming of the interfaces.
19565 		 * In order to confirm with existing semantics,
19566 		 * and to not break any programs/script relying
19567 		 * on that behaviour, if<0>:0 is considered to be
19568 		 * a valid interface.
19569 		 *
19570 		 * If alias has two or more digits and the first
19571 		 * is zero, fail.
19572 		 */
19573 		if (&cp[2] < endp && cp[1] == '0')
19574 			return (NULL);
19575 	}
19576 
19577 	if (cp <= name) {
19578 		cp = endp;
19579 	} else {
19580 		*cp = '\0';
19581 	}
19582 
19583 	/*
19584 	 * Look up the ILL, based on the portion of the name
19585 	 * before the slash. ill_lookup_on_name returns a held ill.
19586 	 * Temporary to check whether ill exists already. If so
19587 	 * ill_lookup_on_name will clear it.
19588 	 */
19589 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19590 	    q, mp, func, error, &did_alloc, ipst);
19591 	if (cp != endp)
19592 		*cp = IPIF_SEPARATOR_CHAR;
19593 	if (ill == NULL)
19594 		return (NULL);
19595 
19596 	/* Establish the unit number in the name. */
19597 	id = 0;
19598 	if (cp < endp && *endp == '\0') {
19599 		/* If there was a colon, the unit number follows. */
19600 		cp++;
19601 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19602 			ill_refrele(ill);
19603 			if (error != NULL)
19604 				*error = ENXIO;
19605 			return (NULL);
19606 		}
19607 	}
19608 
19609 	GRAB_CONN_LOCK(q);
19610 	mutex_enter(&ill->ill_lock);
19611 	/* Now see if there is an IPIF with this unit number. */
19612 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19613 		if (ipif->ipif_id == id) {
19614 			if (zoneid != ALL_ZONES &&
19615 			    zoneid != ipif->ipif_zoneid &&
19616 			    ipif->ipif_zoneid != ALL_ZONES) {
19617 				mutex_exit(&ill->ill_lock);
19618 				RELEASE_CONN_LOCK(q);
19619 				ill_refrele(ill);
19620 				if (error != NULL)
19621 					*error = ENXIO;
19622 				return (NULL);
19623 			}
19624 			/*
19625 			 * The block comment at the start of ipif_down
19626 			 * explains the use of the macros used below
19627 			 */
19628 			if (IPIF_CAN_LOOKUP(ipif)) {
19629 				ipif_refhold_locked(ipif);
19630 				mutex_exit(&ill->ill_lock);
19631 				if (!did_alloc)
19632 					*exists = B_TRUE;
19633 				/*
19634 				 * Drop locks before calling ill_refrele
19635 				 * since it can potentially call into
19636 				 * ipif_ill_refrele_tail which can end up
19637 				 * in trying to acquire any lock.
19638 				 */
19639 				RELEASE_CONN_LOCK(q);
19640 				ill_refrele(ill);
19641 				return (ipif);
19642 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19643 				ipsq = ill->ill_phyint->phyint_ipsq;
19644 				mutex_enter(&ipsq->ipsq_lock);
19645 				mutex_exit(&ill->ill_lock);
19646 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19647 				mutex_exit(&ipsq->ipsq_lock);
19648 				RELEASE_CONN_LOCK(q);
19649 				ill_refrele(ill);
19650 				*error = EINPROGRESS;
19651 				return (NULL);
19652 			}
19653 		}
19654 	}
19655 	RELEASE_CONN_LOCK(q);
19656 
19657 	if (!do_alloc) {
19658 		mutex_exit(&ill->ill_lock);
19659 		ill_refrele(ill);
19660 		if (error != NULL)
19661 			*error = ENXIO;
19662 		return (NULL);
19663 	}
19664 
19665 	/*
19666 	 * If none found, atomically allocate and return a new one.
19667 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19668 	 * to support "receive only" use of lo0:1 etc. as is still done
19669 	 * below as an initial guess.
19670 	 * However, this is now likely to be overriden later in ipif_up_done()
19671 	 * when we know for sure what address has been configured on the
19672 	 * interface, since we might have more than one loopback interface
19673 	 * with a loopback address, e.g. in the case of zones, and all the
19674 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19675 	 */
19676 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19677 		ire_type = IRE_LOOPBACK;
19678 	else
19679 		ire_type = IRE_LOCAL;
19680 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19681 	if (ipif != NULL)
19682 		ipif_refhold_locked(ipif);
19683 	else if (error != NULL)
19684 		*error = ENOMEM;
19685 	mutex_exit(&ill->ill_lock);
19686 	ill_refrele(ill);
19687 	return (ipif);
19688 }
19689 
19690 /*
19691  * This routine is called whenever a new address comes up on an ipif.  If
19692  * we are configured to respond to address mask requests, then we are supposed
19693  * to broadcast an address mask reply at this time.  This routine is also
19694  * called if we are already up, but a netmask change is made.  This is legal
19695  * but might not make the system manager very popular.	(May be called
19696  * as writer.)
19697  */
19698 void
19699 ipif_mask_reply(ipif_t *ipif)
19700 {
19701 	icmph_t	*icmph;
19702 	ipha_t	*ipha;
19703 	mblk_t	*mp;
19704 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19705 
19706 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19707 
19708 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19709 		return;
19710 
19711 	/* ICMP mask reply is IPv4 only */
19712 	ASSERT(!ipif->ipif_isv6);
19713 	/* ICMP mask reply is not for a loopback interface */
19714 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19715 
19716 	mp = allocb(REPLY_LEN, BPRI_HI);
19717 	if (mp == NULL)
19718 		return;
19719 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19720 
19721 	ipha = (ipha_t *)mp->b_rptr;
19722 	bzero(ipha, REPLY_LEN);
19723 	*ipha = icmp_ipha;
19724 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19725 	ipha->ipha_src = ipif->ipif_src_addr;
19726 	ipha->ipha_dst = ipif->ipif_brd_addr;
19727 	ipha->ipha_length = htons(REPLY_LEN);
19728 	ipha->ipha_ident = 0;
19729 
19730 	icmph = (icmph_t *)&ipha[1];
19731 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19732 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19733 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19734 
19735 	put(ipif->ipif_wq, mp);
19736 
19737 #undef	REPLY_LEN
19738 }
19739 
19740 /*
19741  * When the mtu in the ipif changes, we call this routine through ire_walk
19742  * to update all the relevant IREs.
19743  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19744  */
19745 static void
19746 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19747 {
19748 	ipif_t *ipif = (ipif_t *)ipif_arg;
19749 
19750 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19751 		return;
19752 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19753 }
19754 
19755 /*
19756  * When the mtu in the ill changes, we call this routine through ire_walk
19757  * to update all the relevant IREs.
19758  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19759  */
19760 void
19761 ill_mtu_change(ire_t *ire, char *ill_arg)
19762 {
19763 	ill_t	*ill = (ill_t *)ill_arg;
19764 
19765 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19766 		return;
19767 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19768 }
19769 
19770 /*
19771  * Join the ipif specific multicast groups.
19772  * Must be called after a mapping has been set up in the resolver.  (Always
19773  * called as writer.)
19774  */
19775 void
19776 ipif_multicast_up(ipif_t *ipif)
19777 {
19778 	int err, index;
19779 	ill_t *ill;
19780 
19781 	ASSERT(IAM_WRITER_IPIF(ipif));
19782 
19783 	ill = ipif->ipif_ill;
19784 	index = ill->ill_phyint->phyint_ifindex;
19785 
19786 	ip1dbg(("ipif_multicast_up\n"));
19787 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19788 		return;
19789 
19790 	if (ipif->ipif_isv6) {
19791 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19792 			return;
19793 
19794 		/* Join the all hosts multicast address */
19795 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19796 		/*
19797 		 * Passing B_TRUE means we have to join the multicast
19798 		 * membership on this interface even though this is
19799 		 * FAILED. If we join on a different one in the group,
19800 		 * we will not be able to delete the membership later
19801 		 * as we currently don't track where we join when we
19802 		 * join within the kernel unlike applications where
19803 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19804 		 * for more on this.
19805 		 */
19806 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19807 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19808 		if (err != 0) {
19809 			ip0dbg(("ipif_multicast_up: "
19810 			    "all_hosts_mcast failed %d\n",
19811 			    err));
19812 			return;
19813 		}
19814 		/*
19815 		 * Enable multicast for the solicited node multicast address
19816 		 */
19817 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19818 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19819 
19820 			ipv6_multi.s6_addr32[3] |=
19821 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19822 
19823 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19824 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19825 			    NULL);
19826 			if (err != 0) {
19827 				ip0dbg(("ipif_multicast_up: solicited MC"
19828 				    " failed %d\n", err));
19829 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19830 				    ill, ill->ill_phyint->phyint_ifindex,
19831 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19832 				return;
19833 			}
19834 		}
19835 	} else {
19836 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19837 			return;
19838 
19839 		/* Join the all hosts multicast address */
19840 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19841 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19842 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19843 		if (err) {
19844 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19845 			return;
19846 		}
19847 	}
19848 	ipif->ipif_multicast_up = 1;
19849 }
19850 
19851 /*
19852  * Blow away any IPv6 multicast groups that we joined in ipif_multicast_up();
19853  * any explicit memberships are blown away in ill_leave_multicast() when the
19854  * ill is brought down.
19855  */
19856 static void
19857 ipif_multicast_down(ipif_t *ipif)
19858 {
19859 	int err;
19860 
19861 	ASSERT(IAM_WRITER_IPIF(ipif));
19862 
19863 	ip1dbg(("ipif_multicast_down\n"));
19864 	if (!ipif->ipif_multicast_up)
19865 		return;
19866 
19867 	ASSERT(ipif->ipif_isv6);
19868 
19869 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19870 
19871 	/*
19872 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19873 	 * we should look for ilms on this ill rather than the ones that have
19874 	 * been failed over here.  They are here temporarily. As
19875 	 * ipif_multicast_up has joined on this ill, we should delete only
19876 	 * from this ill.
19877 	 */
19878 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19879 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19880 	    B_TRUE, B_TRUE);
19881 	if (err != 0) {
19882 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19883 		    err));
19884 	}
19885 	/*
19886 	 * Disable multicast for the solicited node multicast address
19887 	 */
19888 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19889 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19890 
19891 		ipv6_multi.s6_addr32[3] |=
19892 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19893 
19894 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19895 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19896 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19897 
19898 		if (err != 0) {
19899 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19900 			    err));
19901 		}
19902 	}
19903 
19904 	ipif->ipif_multicast_up = 0;
19905 }
19906 
19907 /*
19908  * Used when an interface comes up to recreate any extra routes on this
19909  * interface.
19910  */
19911 static ire_t **
19912 ipif_recover_ire(ipif_t *ipif)
19913 {
19914 	mblk_t	*mp;
19915 	ire_t	**ipif_saved_irep;
19916 	ire_t	**irep;
19917 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19918 
19919 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19920 	    ipif->ipif_id));
19921 
19922 	mutex_enter(&ipif->ipif_saved_ire_lock);
19923 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19924 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19925 	if (ipif_saved_irep == NULL) {
19926 		mutex_exit(&ipif->ipif_saved_ire_lock);
19927 		return (NULL);
19928 	}
19929 
19930 	irep = ipif_saved_irep;
19931 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19932 		ire_t		*ire;
19933 		queue_t		*rfq;
19934 		queue_t		*stq;
19935 		ifrt_t		*ifrt;
19936 		uchar_t		*src_addr;
19937 		uchar_t		*gateway_addr;
19938 		ushort_t	type;
19939 
19940 		/*
19941 		 * When the ire was initially created and then added in
19942 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19943 		 * in the case of a traditional interface route, or as one of
19944 		 * the IRE_OFFSUBNET types (with the exception of
19945 		 * IRE_HOST types ire which is created by icmp_redirect() and
19946 		 * which we don't need to save or recover).  In the case where
19947 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19948 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19949 		 * to satisfy software like GateD and Sun Cluster which creates
19950 		 * routes using the the loopback interface's address as a
19951 		 * gateway.
19952 		 *
19953 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19954 		 * ire_create() will be called in the same way here as
19955 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19956 		 * the route looks like a traditional interface route (where
19957 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19958 		 * the saved ifrt->ifrt_type.  This means that in the case where
19959 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19960 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19961 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19962 		 */
19963 		ifrt = (ifrt_t *)mp->b_rptr;
19964 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19965 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19966 			rfq = NULL;
19967 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19968 			    ? ipif->ipif_rq : ipif->ipif_wq;
19969 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19970 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19971 			    : (uint8_t *)&ipif->ipif_src_addr;
19972 			gateway_addr = NULL;
19973 			type = ipif->ipif_net_type;
19974 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19975 			/* Recover multiroute broadcast IRE. */
19976 			rfq = ipif->ipif_rq;
19977 			stq = ipif->ipif_wq;
19978 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19979 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19980 			    : (uint8_t *)&ipif->ipif_src_addr;
19981 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19982 			type = ifrt->ifrt_type;
19983 		} else {
19984 			rfq = NULL;
19985 			stq = NULL;
19986 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19987 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19988 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19989 			type = ifrt->ifrt_type;
19990 		}
19991 
19992 		/*
19993 		 * Create a copy of the IRE with the saved address and netmask.
19994 		 */
19995 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19996 		    "0x%x/0x%x\n",
19997 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19998 		    ntohl(ifrt->ifrt_addr),
19999 		    ntohl(ifrt->ifrt_mask)));
20000 		ire = ire_create(
20001 		    (uint8_t *)&ifrt->ifrt_addr,
20002 		    (uint8_t *)&ifrt->ifrt_mask,
20003 		    src_addr,
20004 		    gateway_addr,
20005 		    NULL,
20006 		    &ifrt->ifrt_max_frag,
20007 		    NULL,
20008 		    rfq,
20009 		    stq,
20010 		    type,
20011 		    ipif,
20012 		    NULL,
20013 		    0,
20014 		    0,
20015 		    0,
20016 		    ifrt->ifrt_flags,
20017 		    &ifrt->ifrt_iulp_info,
20018 		    NULL,
20019 		    NULL,
20020 		    ipst);
20021 
20022 		if (ire == NULL) {
20023 			mutex_exit(&ipif->ipif_saved_ire_lock);
20024 			kmem_free(ipif_saved_irep,
20025 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
20026 			return (NULL);
20027 		}
20028 
20029 		/*
20030 		 * Some software (for example, GateD and Sun Cluster) attempts
20031 		 * to create (what amount to) IRE_PREFIX routes with the
20032 		 * loopback address as the gateway.  This is primarily done to
20033 		 * set up prefixes with the RTF_REJECT flag set (for example,
20034 		 * when generating aggregate routes.)
20035 		 *
20036 		 * If the IRE type (as defined by ipif->ipif_net_type) is
20037 		 * IRE_LOOPBACK, then we map the request into a
20038 		 * IRE_IF_NORESOLVER.
20039 		 */
20040 		if (ipif->ipif_net_type == IRE_LOOPBACK)
20041 			ire->ire_type = IRE_IF_NORESOLVER;
20042 		/*
20043 		 * ire held by ire_add, will be refreled' towards the
20044 		 * the end of ipif_up_done
20045 		 */
20046 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
20047 		*irep = ire;
20048 		irep++;
20049 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
20050 	}
20051 	mutex_exit(&ipif->ipif_saved_ire_lock);
20052 	return (ipif_saved_irep);
20053 }
20054 
20055 /*
20056  * Used to set the netmask and broadcast address to default values when the
20057  * interface is brought up.  (Always called as writer.)
20058  */
20059 static void
20060 ipif_set_default(ipif_t *ipif)
20061 {
20062 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20063 
20064 	if (!ipif->ipif_isv6) {
20065 		/*
20066 		 * Interface holds an IPv4 address. Default
20067 		 * mask is the natural netmask.
20068 		 */
20069 		if (!ipif->ipif_net_mask) {
20070 			ipaddr_t	v4mask;
20071 
20072 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
20073 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
20074 		}
20075 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20076 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20077 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
20078 		} else {
20079 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
20080 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
20081 		}
20082 		/*
20083 		 * NOTE: SunOS 4.X does this even if the broadcast address
20084 		 * has been already set thus we do the same here.
20085 		 */
20086 		if (ipif->ipif_flags & IPIF_BROADCAST) {
20087 			ipaddr_t	v4addr;
20088 
20089 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
20090 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
20091 		}
20092 	} else {
20093 		/*
20094 		 * Interface holds an IPv6-only address.  Default
20095 		 * mask is all-ones.
20096 		 */
20097 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
20098 			ipif->ipif_v6net_mask = ipv6_all_ones;
20099 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20100 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20101 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
20102 		} else {
20103 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
20104 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
20105 		}
20106 	}
20107 }
20108 
20109 /*
20110  * Return 0 if this address can be used as local address without causing
20111  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
20112  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
20113  * Special checks are needed to allow the same IPv6 link-local address
20114  * on different ills.
20115  * TODO: allowing the same site-local address on different ill's.
20116  */
20117 int
20118 ip_addr_availability_check(ipif_t *new_ipif)
20119 {
20120 	in6_addr_t our_v6addr;
20121 	ill_t *ill;
20122 	ipif_t *ipif;
20123 	ill_walk_context_t ctx;
20124 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
20125 
20126 	ASSERT(IAM_WRITER_IPIF(new_ipif));
20127 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
20128 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
20129 
20130 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
20131 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
20132 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
20133 		return (0);
20134 
20135 	our_v6addr = new_ipif->ipif_v6lcl_addr;
20136 
20137 	if (new_ipif->ipif_isv6)
20138 		ill = ILL_START_WALK_V6(&ctx, ipst);
20139 	else
20140 		ill = ILL_START_WALK_V4(&ctx, ipst);
20141 
20142 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
20143 		for (ipif = ill->ill_ipif; ipif != NULL;
20144 		    ipif = ipif->ipif_next) {
20145 			if ((ipif == new_ipif) ||
20146 			    !(ipif->ipif_flags & IPIF_UP) ||
20147 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
20148 				continue;
20149 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
20150 			    &our_v6addr)) {
20151 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
20152 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
20153 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
20154 					ipif->ipif_flags |= IPIF_UNNUMBERED;
20155 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
20156 				    new_ipif->ipif_ill != ill)
20157 					continue;
20158 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
20159 				    new_ipif->ipif_ill != ill)
20160 					continue;
20161 				else if (new_ipif->ipif_zoneid !=
20162 				    ipif->ipif_zoneid &&
20163 				    ipif->ipif_zoneid != ALL_ZONES &&
20164 				    IS_LOOPBACK(ill))
20165 					continue;
20166 				else if (new_ipif->ipif_ill == ill)
20167 					return (EADDRINUSE);
20168 				else
20169 					return (EADDRNOTAVAIL);
20170 			}
20171 		}
20172 	}
20173 
20174 	return (0);
20175 }
20176 
20177 /*
20178  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
20179  * IREs for the ipif.
20180  * When the routine returns EINPROGRESS then mp has been consumed and
20181  * the ioctl will be acked from ip_rput_dlpi.
20182  */
20183 static int
20184 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
20185 {
20186 	ill_t	*ill = ipif->ipif_ill;
20187 	boolean_t isv6 = ipif->ipif_isv6;
20188 	int	err = 0;
20189 	boolean_t success;
20190 
20191 	ASSERT(IAM_WRITER_IPIF(ipif));
20192 
20193 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
20194 
20195 	/* Shouldn't get here if it is already up. */
20196 	if (ipif->ipif_flags & IPIF_UP)
20197 		return (EALREADY);
20198 
20199 	/* Skip arp/ndp for any loopback interface. */
20200 	if (ill->ill_wq != NULL) {
20201 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
20202 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
20203 
20204 		if (!ill->ill_dl_up) {
20205 			/*
20206 			 * ill_dl_up is not yet set. i.e. we are yet to
20207 			 * DL_BIND with the driver and this is the first
20208 			 * logical interface on the ill to become "up".
20209 			 * Tell the driver to get going (via DL_BIND_REQ).
20210 			 * Note that changing "significant" IFF_ flags
20211 			 * address/netmask etc cause a down/up dance, but
20212 			 * does not cause an unbind (DL_UNBIND) with the driver
20213 			 */
20214 			return (ill_dl_up(ill, ipif, mp, q));
20215 		}
20216 
20217 		/*
20218 		 * ipif_resolver_up may end up sending an
20219 		 * AR_INTERFACE_UP message to ARP, which would, in
20220 		 * turn send a DLPI message to the driver. ioctls are
20221 		 * serialized and so we cannot send more than one
20222 		 * interface up message at a time. If ipif_resolver_up
20223 		 * does send an interface up message to ARP, we get
20224 		 * EINPROGRESS and we will complete in ip_arp_done.
20225 		 */
20226 
20227 		ASSERT(connp != NULL || !CONN_Q(q));
20228 		ASSERT(ipsq->ipsq_pending_mp == NULL);
20229 		if (connp != NULL)
20230 			mutex_enter(&connp->conn_lock);
20231 		mutex_enter(&ill->ill_lock);
20232 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
20233 		mutex_exit(&ill->ill_lock);
20234 		if (connp != NULL)
20235 			mutex_exit(&connp->conn_lock);
20236 		if (!success)
20237 			return (EINTR);
20238 
20239 		/*
20240 		 * Crank up IPv6 neighbor discovery
20241 		 * Unlike ARP, this should complete when
20242 		 * ipif_ndp_up returns. However, for
20243 		 * ILLF_XRESOLV interfaces we also send a
20244 		 * AR_INTERFACE_UP to the external resolver.
20245 		 * That ioctl will complete in ip_rput.
20246 		 */
20247 		if (isv6) {
20248 			err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr);
20249 			if (err != 0) {
20250 				if (err != EINPROGRESS)
20251 					mp = ipsq_pending_mp_get(ipsq, &connp);
20252 				return (err);
20253 			}
20254 		}
20255 		/* Now, ARP */
20256 		err = ipif_resolver_up(ipif, Res_act_initial);
20257 		if (err == EINPROGRESS) {
20258 			/* We will complete it in ip_arp_done */
20259 			return (err);
20260 		}
20261 		mp = ipsq_pending_mp_get(ipsq, &connp);
20262 		ASSERT(mp != NULL);
20263 		if (err != 0)
20264 			return (err);
20265 	} else {
20266 		/*
20267 		 * Interfaces without underlying hardware don't do duplicate
20268 		 * address detection.
20269 		 */
20270 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
20271 		ipif->ipif_addr_ready = 1;
20272 	}
20273 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
20274 }
20275 
20276 /*
20277  * Perform a bind for the physical device.
20278  * When the routine returns EINPROGRESS then mp has been consumed and
20279  * the ioctl will be acked from ip_rput_dlpi.
20280  * Allocate an unbind message and save it until ipif_down.
20281  */
20282 static int
20283 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
20284 {
20285 	areq_t	*areq;
20286 	mblk_t	*areq_mp = NULL;
20287 	mblk_t	*bind_mp = NULL;
20288 	mblk_t	*unbind_mp = NULL;
20289 	conn_t	*connp;
20290 	boolean_t success;
20291 	uint16_t sap_addr;
20292 
20293 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
20294 	ASSERT(IAM_WRITER_ILL(ill));
20295 	ASSERT(mp != NULL);
20296 
20297 	/* Create a resolver cookie for ARP */
20298 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
20299 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
20300 		if (areq_mp == NULL)
20301 			return (ENOMEM);
20302 
20303 		freemsg(ill->ill_resolver_mp);
20304 		ill->ill_resolver_mp = areq_mp;
20305 		areq = (areq_t *)areq_mp->b_rptr;
20306 		sap_addr = ill->ill_sap;
20307 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
20308 	}
20309 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
20310 	    DL_BIND_REQ);
20311 	if (bind_mp == NULL)
20312 		goto bad;
20313 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
20314 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
20315 
20316 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
20317 	if (unbind_mp == NULL)
20318 		goto bad;
20319 
20320 	/*
20321 	 * Record state needed to complete this operation when the
20322 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
20323 	 */
20324 	ASSERT(WR(q)->q_next == NULL);
20325 	connp = Q_TO_CONN(q);
20326 
20327 	mutex_enter(&connp->conn_lock);
20328 	mutex_enter(&ipif->ipif_ill->ill_lock);
20329 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
20330 	mutex_exit(&ipif->ipif_ill->ill_lock);
20331 	mutex_exit(&connp->conn_lock);
20332 	if (!success)
20333 		goto bad;
20334 
20335 	/*
20336 	 * Save the unbind message for ill_dl_down(); it will be consumed when
20337 	 * the interface goes down.
20338 	 */
20339 	ASSERT(ill->ill_unbind_mp == NULL);
20340 	ill->ill_unbind_mp = unbind_mp;
20341 
20342 	ill_dlpi_send(ill, bind_mp);
20343 	/* Send down link-layer capabilities probe if not already done. */
20344 	ill_capability_probe(ill);
20345 
20346 	/*
20347 	 * Sysid used to rely on the fact that netboots set domainname
20348 	 * and the like. Now that miniroot boots aren't strictly netboots
20349 	 * and miniroot network configuration is driven from userland
20350 	 * these things still need to be set. This situation can be detected
20351 	 * by comparing the interface being configured here to the one
20352 	 * dhcack was set to reference by the boot loader. Once sysid is
20353 	 * converted to use dhcp_ipc_getinfo() this call can go away.
20354 	 */
20355 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
20356 	    (strcmp(ill->ill_name, dhcack) == 0) &&
20357 	    (strlen(srpc_domain) == 0)) {
20358 		if (dhcpinit() != 0)
20359 			cmn_err(CE_WARN, "no cached dhcp response");
20360 	}
20361 
20362 	/*
20363 	 * This operation will complete in ip_rput_dlpi with either
20364 	 * a DL_BIND_ACK or DL_ERROR_ACK.
20365 	 */
20366 	return (EINPROGRESS);
20367 bad:
20368 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
20369 	/*
20370 	 * We don't have to check for possible removal from illgrp
20371 	 * as we have not yet inserted in illgrp. For groups
20372 	 * without names, this ipif is still not UP and hence
20373 	 * this could not have possibly had any influence in forming
20374 	 * groups.
20375 	 */
20376 
20377 	freemsg(bind_mp);
20378 	freemsg(unbind_mp);
20379 	return (ENOMEM);
20380 }
20381 
20382 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
20383 
20384 /*
20385  * DLPI and ARP is up.
20386  * Create all the IREs associated with an interface bring up multicast.
20387  * Set the interface flag and finish other initialization
20388  * that potentially had to be differed to after DL_BIND_ACK.
20389  */
20390 int
20391 ipif_up_done(ipif_t *ipif)
20392 {
20393 	ire_t	*ire_array[20];
20394 	ire_t	**irep = ire_array;
20395 	ire_t	**irep1;
20396 	ipaddr_t net_mask = 0;
20397 	ipaddr_t subnet_mask, route_mask;
20398 	ill_t	*ill = ipif->ipif_ill;
20399 	queue_t	*stq;
20400 	ipif_t	 *src_ipif;
20401 	ipif_t   *tmp_ipif;
20402 	boolean_t	flush_ire_cache = B_TRUE;
20403 	int	err = 0;
20404 	phyint_t *phyi;
20405 	ire_t	**ipif_saved_irep = NULL;
20406 	int ipif_saved_ire_cnt;
20407 	int	cnt;
20408 	boolean_t	src_ipif_held = B_FALSE;
20409 	boolean_t	ire_added = B_FALSE;
20410 	boolean_t	loopback = B_FALSE;
20411 	ip_stack_t	*ipst = ill->ill_ipst;
20412 
20413 	ip1dbg(("ipif_up_done(%s:%u)\n",
20414 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
20415 	/* Check if this is a loopback interface */
20416 	if (ipif->ipif_ill->ill_wq == NULL)
20417 		loopback = B_TRUE;
20418 
20419 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20420 	/*
20421 	 * If all other interfaces for this ill are down or DEPRECATED,
20422 	 * or otherwise unsuitable for source address selection, remove
20423 	 * any IRE_CACHE entries for this ill to make sure source
20424 	 * address selection gets to take this new ipif into account.
20425 	 * No need to hold ill_lock while traversing the ipif list since
20426 	 * we are writer
20427 	 */
20428 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20429 	    tmp_ipif = tmp_ipif->ipif_next) {
20430 		if (((tmp_ipif->ipif_flags &
20431 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20432 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20433 		    (tmp_ipif == ipif))
20434 			continue;
20435 		/* first useable pre-existing interface */
20436 		flush_ire_cache = B_FALSE;
20437 		break;
20438 	}
20439 	if (flush_ire_cache)
20440 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20441 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20442 
20443 	/*
20444 	 * Figure out which way the send-to queue should go.  Only
20445 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20446 	 * should show up here.
20447 	 */
20448 	switch (ill->ill_net_type) {
20449 	case IRE_IF_RESOLVER:
20450 		stq = ill->ill_rq;
20451 		break;
20452 	case IRE_IF_NORESOLVER:
20453 	case IRE_LOOPBACK:
20454 		stq = ill->ill_wq;
20455 		break;
20456 	default:
20457 		return (EINVAL);
20458 	}
20459 
20460 	if (IS_LOOPBACK(ill)) {
20461 		/*
20462 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20463 		 * ipif_lookup_on_name(), but in the case of zones we can have
20464 		 * several loopback addresses on lo0. So all the interfaces with
20465 		 * loopback addresses need to be marked IRE_LOOPBACK.
20466 		 */
20467 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20468 		    htonl(INADDR_LOOPBACK))
20469 			ipif->ipif_ire_type = IRE_LOOPBACK;
20470 		else
20471 			ipif->ipif_ire_type = IRE_LOCAL;
20472 	}
20473 
20474 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20475 		/*
20476 		 * Can't use our source address. Select a different
20477 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20478 		 */
20479 		src_ipif = ipif_select_source(ipif->ipif_ill,
20480 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20481 		if (src_ipif == NULL)
20482 			src_ipif = ipif;	/* Last resort */
20483 		else
20484 			src_ipif_held = B_TRUE;
20485 	} else {
20486 		src_ipif = ipif;
20487 	}
20488 
20489 	/* Create all the IREs associated with this interface */
20490 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20491 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20492 
20493 		/*
20494 		 * If we're on a labeled system then make sure that zone-
20495 		 * private addresses have proper remote host database entries.
20496 		 */
20497 		if (is_system_labeled() &&
20498 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20499 		    !tsol_check_interface_address(ipif))
20500 			return (EINVAL);
20501 
20502 		/* Register the source address for __sin6_src_id */
20503 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20504 		    ipif->ipif_zoneid, ipst);
20505 		if (err != 0) {
20506 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20507 			return (err);
20508 		}
20509 
20510 		/* If the interface address is set, create the local IRE. */
20511 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20512 		    (void *)ipif,
20513 		    ipif->ipif_ire_type,
20514 		    ntohl(ipif->ipif_lcl_addr)));
20515 		*irep++ = ire_create(
20516 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20517 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20518 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20519 		    NULL,				/* no gateway */
20520 		    NULL,
20521 		    &ip_loopback_mtuplus,		/* max frag size */
20522 		    NULL,
20523 		    ipif->ipif_rq,			/* recv-from queue */
20524 		    NULL,				/* no send-to queue */
20525 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20526 		    ipif,
20527 		    NULL,
20528 		    0,
20529 		    0,
20530 		    0,
20531 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20532 		    RTF_PRIVATE : 0,
20533 		    &ire_uinfo_null,
20534 		    NULL,
20535 		    NULL,
20536 		    ipst);
20537 	} else {
20538 		ip1dbg((
20539 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20540 		    ipif->ipif_ire_type,
20541 		    ntohl(ipif->ipif_lcl_addr),
20542 		    (uint_t)ipif->ipif_flags));
20543 	}
20544 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20545 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20546 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20547 	} else {
20548 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20549 	}
20550 
20551 	subnet_mask = ipif->ipif_net_mask;
20552 
20553 	/*
20554 	 * If mask was not specified, use natural netmask of
20555 	 * interface address. Also, store this mask back into the
20556 	 * ipif struct.
20557 	 */
20558 	if (subnet_mask == 0) {
20559 		subnet_mask = net_mask;
20560 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20561 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20562 		    ipif->ipif_v6subnet);
20563 	}
20564 
20565 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20566 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20567 	    ipif->ipif_subnet != INADDR_ANY) {
20568 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20569 
20570 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20571 			route_mask = IP_HOST_MASK;
20572 		} else {
20573 			route_mask = subnet_mask;
20574 		}
20575 
20576 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20577 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20578 		    (void *)ipif, (void *)ill,
20579 		    ill->ill_net_type,
20580 		    ntohl(ipif->ipif_subnet)));
20581 		*irep++ = ire_create(
20582 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20583 		    (uchar_t *)&route_mask,		/* mask */
20584 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20585 		    NULL,				/* no gateway */
20586 		    NULL,
20587 		    &ipif->ipif_mtu,			/* max frag */
20588 		    NULL,
20589 		    NULL,				/* no recv queue */
20590 		    stq,				/* send-to queue */
20591 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20592 		    ipif,
20593 		    NULL,
20594 		    0,
20595 		    0,
20596 		    0,
20597 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20598 		    &ire_uinfo_null,
20599 		    NULL,
20600 		    NULL,
20601 		    ipst);
20602 	}
20603 
20604 	/*
20605 	 * If the interface address is set, create the broadcast IREs.
20606 	 *
20607 	 * ire_create_bcast checks if the proposed new IRE matches
20608 	 * any existing IRE's with the same physical interface (ILL).
20609 	 * This should get rid of duplicates.
20610 	 * ire_create_bcast also check IPIF_NOXMIT and does not create
20611 	 * any broadcast ires.
20612 	 */
20613 	if ((ipif->ipif_subnet != INADDR_ANY) &&
20614 	    (ipif->ipif_flags & IPIF_BROADCAST)) {
20615 		ipaddr_t addr;
20616 
20617 		ip1dbg(("ipif_up_done: creating broadcast IRE\n"));
20618 		irep = ire_check_and_create_bcast(ipif, 0, irep,
20619 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20620 		irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep,
20621 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20622 
20623 		/*
20624 		 * For backward compatibility, we need to create net
20625 		 * broadcast ire's based on the old "IP address class
20626 		 * system."  The reason is that some old machines only
20627 		 * respond to these class derived net broadcast.
20628 		 *
20629 		 * But we should not create these net broadcast ire's if
20630 		 * the subnet_mask is shorter than the IP address class based
20631 		 * derived netmask.  Otherwise, we may create a net
20632 		 * broadcast address which is the same as an IP address
20633 		 * on the subnet.  Then TCP will refuse to talk to that
20634 		 * address.
20635 		 *
20636 		 * Nor do we need IRE_BROADCAST ire's for the interface
20637 		 * with the netmask as 0xFFFFFFFF, as IRE_LOCAL for that
20638 		 * interface is already created.  Creating these broadcast
20639 		 * ire's will only create confusion as the "addr" is going
20640 		 * to be same as that of the IP address of the interface.
20641 		 */
20642 		if (net_mask < subnet_mask) {
20643 			addr = net_mask & ipif->ipif_subnet;
20644 			irep = ire_check_and_create_bcast(ipif, addr, irep,
20645 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20646 			irep = ire_check_and_create_bcast(ipif,
20647 			    ~net_mask | addr, irep,
20648 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20649 		}
20650 
20651 		if (subnet_mask != 0xFFFFFFFF) {
20652 			addr = ipif->ipif_subnet;
20653 			irep = ire_check_and_create_bcast(ipif, addr, irep,
20654 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20655 			irep = ire_check_and_create_bcast(ipif,
20656 			    ~subnet_mask|addr, irep,
20657 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20658 		}
20659 	}
20660 
20661 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20662 
20663 	/* If an earlier ire_create failed, get out now */
20664 	for (irep1 = irep; irep1 > ire_array; ) {
20665 		irep1--;
20666 		if (*irep1 == NULL) {
20667 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20668 			err = ENOMEM;
20669 			goto bad;
20670 		}
20671 	}
20672 
20673 	/*
20674 	 * Need to atomically check for ip_addr_availablity_check
20675 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20676 	 * from group also.The ill_g_lock is grabbed as reader
20677 	 * just to make sure no new ills or new ipifs are being added
20678 	 * to the system while we are checking the uniqueness of addresses.
20679 	 */
20680 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20681 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20682 	/* Mark it up, and increment counters. */
20683 	ipif->ipif_flags |= IPIF_UP;
20684 	ill->ill_ipif_up_count++;
20685 	err = ip_addr_availability_check(ipif);
20686 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20687 	rw_exit(&ipst->ips_ill_g_lock);
20688 
20689 	if (err != 0) {
20690 		/*
20691 		 * Our address may already be up on the same ill. In this case,
20692 		 * the ARP entry for our ipif replaced the one for the other
20693 		 * ipif. So we don't want to delete it (otherwise the other ipif
20694 		 * would be unable to send packets).
20695 		 * ip_addr_availability_check() identifies this case for us and
20696 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20697 		 * which is the expected error code.
20698 		 */
20699 		if (err == EADDRINUSE) {
20700 			freemsg(ipif->ipif_arp_del_mp);
20701 			ipif->ipif_arp_del_mp = NULL;
20702 			err = EADDRNOTAVAIL;
20703 		}
20704 		ill->ill_ipif_up_count--;
20705 		ipif->ipif_flags &= ~IPIF_UP;
20706 		goto bad;
20707 	}
20708 
20709 	/*
20710 	 * Add in all newly created IREs.  ire_create_bcast() has
20711 	 * already checked for duplicates of the IRE_BROADCAST type.
20712 	 * We want to add before we call ifgrp_insert which wants
20713 	 * to know whether IRE_IF_RESOLVER exists or not.
20714 	 *
20715 	 * NOTE : We refrele the ire though we may branch to "bad"
20716 	 *	  later on where we do ire_delete. This is okay
20717 	 *	  because nobody can delete it as we are running
20718 	 *	  exclusively.
20719 	 */
20720 	for (irep1 = irep; irep1 > ire_array; ) {
20721 		irep1--;
20722 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20723 		/*
20724 		 * refheld by ire_add. refele towards the end of the func
20725 		 */
20726 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20727 	}
20728 	ire_added = B_TRUE;
20729 	/*
20730 	 * Form groups if possible.
20731 	 *
20732 	 * If we are supposed to be in a ill_group with a name, insert it
20733 	 * now as we know that at least one ipif is UP. Otherwise form
20734 	 * nameless groups.
20735 	 *
20736 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20737 	 * this ipif into the appropriate interface group, or create a
20738 	 * new one. If this is already in a nameless group, we try to form
20739 	 * a bigger group looking at other ills potentially sharing this
20740 	 * ipif's prefix.
20741 	 */
20742 	phyi = ill->ill_phyint;
20743 	if (phyi->phyint_groupname_len != 0) {
20744 		ASSERT(phyi->phyint_groupname != NULL);
20745 		if (ill->ill_ipif_up_count == 1) {
20746 			ASSERT(ill->ill_group == NULL);
20747 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20748 			    phyi->phyint_groupname, NULL, B_TRUE);
20749 			if (err != 0) {
20750 				ip1dbg(("ipif_up_done: illgrp allocation "
20751 				    "failed, error %d\n", err));
20752 				goto bad;
20753 			}
20754 		}
20755 		ASSERT(ill->ill_group != NULL);
20756 	}
20757 
20758 	/*
20759 	 * When this is part of group, we need to make sure that
20760 	 * any broadcast ires created because of this ipif coming
20761 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20762 	 * so that we don't receive duplicate broadcast packets.
20763 	 */
20764 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20765 		ipif_renominate_bcast(ipif);
20766 
20767 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20768 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20769 	ipif_saved_irep = ipif_recover_ire(ipif);
20770 
20771 	if (!loopback) {
20772 		/*
20773 		 * If the broadcast address has been set, make sure it makes
20774 		 * sense based on the interface address.
20775 		 * Only match on ill since we are sharing broadcast addresses.
20776 		 */
20777 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20778 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20779 			ire_t	*ire;
20780 
20781 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20782 			    IRE_BROADCAST, ipif, ALL_ZONES,
20783 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20784 
20785 			if (ire == NULL) {
20786 				/*
20787 				 * If there isn't a matching broadcast IRE,
20788 				 * revert to the default for this netmask.
20789 				 */
20790 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20791 				mutex_enter(&ipif->ipif_ill->ill_lock);
20792 				ipif_set_default(ipif);
20793 				mutex_exit(&ipif->ipif_ill->ill_lock);
20794 			} else {
20795 				ire_refrele(ire);
20796 			}
20797 		}
20798 
20799 	}
20800 
20801 	/* This is the first interface on this ill */
20802 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20803 		/*
20804 		 * Need to recover all multicast memberships in the driver.
20805 		 * This had to be deferred until we had attached.
20806 		 */
20807 		ill_recover_multicast(ill);
20808 	}
20809 	/* Join the allhosts multicast address */
20810 	ipif_multicast_up(ipif);
20811 
20812 	if (!loopback) {
20813 		/*
20814 		 * See whether anybody else would benefit from the
20815 		 * new ipif that we added. We call this always rather
20816 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20817 		 * ipif is for the benefit of illgrp_insert (done above)
20818 		 * which does not do source address selection as it does
20819 		 * not want to re-create interface routes that we are
20820 		 * having reference to it here.
20821 		 */
20822 		ill_update_source_selection(ill);
20823 	}
20824 
20825 	for (irep1 = irep; irep1 > ire_array; ) {
20826 		irep1--;
20827 		if (*irep1 != NULL) {
20828 			/* was held in ire_add */
20829 			ire_refrele(*irep1);
20830 		}
20831 	}
20832 
20833 	cnt = ipif_saved_ire_cnt;
20834 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20835 		if (*irep1 != NULL) {
20836 			/* was held in ire_add */
20837 			ire_refrele(*irep1);
20838 		}
20839 	}
20840 
20841 	if (!loopback && ipif->ipif_addr_ready) {
20842 		/* Broadcast an address mask reply. */
20843 		ipif_mask_reply(ipif);
20844 	}
20845 	if (ipif_saved_irep != NULL) {
20846 		kmem_free(ipif_saved_irep,
20847 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20848 	}
20849 	if (src_ipif_held)
20850 		ipif_refrele(src_ipif);
20851 
20852 	/*
20853 	 * This had to be deferred until we had bound.  Tell routing sockets and
20854 	 * others that this interface is up if it looks like the address has
20855 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20856 	 * duplicate address detection to do its thing.
20857 	 */
20858 	if (ipif->ipif_addr_ready) {
20859 		ip_rts_ifmsg(ipif);
20860 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20861 		/* Let SCTP update the status for this ipif */
20862 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20863 	}
20864 	return (0);
20865 
20866 bad:
20867 	ip1dbg(("ipif_up_done: FAILED \n"));
20868 	/*
20869 	 * We don't have to bother removing from ill groups because
20870 	 *
20871 	 * 1) For groups with names, we insert only when the first ipif
20872 	 *    comes up. In that case if it fails, it will not be in any
20873 	 *    group. So, we need not try to remove for that case.
20874 	 *
20875 	 * 2) For groups without names, either we tried to insert ipif_ill
20876 	 *    in a group as singleton or found some other group to become
20877 	 *    a bigger group. For the former, if it fails we don't have
20878 	 *    anything to do as ipif_ill is not in the group and for the
20879 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20880 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20881 	 */
20882 	while (irep > ire_array) {
20883 		irep--;
20884 		if (*irep != NULL) {
20885 			ire_delete(*irep);
20886 			if (ire_added)
20887 				ire_refrele(*irep);
20888 		}
20889 	}
20890 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20891 
20892 	if (ipif_saved_irep != NULL) {
20893 		kmem_free(ipif_saved_irep,
20894 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20895 	}
20896 	if (src_ipif_held)
20897 		ipif_refrele(src_ipif);
20898 
20899 	ipif_arp_down(ipif);
20900 	return (err);
20901 }
20902 
20903 /*
20904  * Turn off the ARP with the ILLF_NOARP flag.
20905  */
20906 static int
20907 ill_arp_off(ill_t *ill)
20908 {
20909 	mblk_t	*arp_off_mp = NULL;
20910 	mblk_t	*arp_on_mp = NULL;
20911 
20912 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20913 
20914 	ASSERT(IAM_WRITER_ILL(ill));
20915 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20916 
20917 	/*
20918 	 * If the on message is still around we've already done
20919 	 * an arp_off without doing an arp_on thus there is no
20920 	 * work needed.
20921 	 */
20922 	if (ill->ill_arp_on_mp != NULL)
20923 		return (0);
20924 
20925 	/*
20926 	 * Allocate an ARP on message (to be saved) and an ARP off message
20927 	 */
20928 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20929 	if (!arp_off_mp)
20930 		return (ENOMEM);
20931 
20932 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20933 	if (!arp_on_mp)
20934 		goto failed;
20935 
20936 	ASSERT(ill->ill_arp_on_mp == NULL);
20937 	ill->ill_arp_on_mp = arp_on_mp;
20938 
20939 	/* Send an AR_INTERFACE_OFF request */
20940 	putnext(ill->ill_rq, arp_off_mp);
20941 	return (0);
20942 failed:
20943 
20944 	if (arp_off_mp)
20945 		freemsg(arp_off_mp);
20946 	return (ENOMEM);
20947 }
20948 
20949 /*
20950  * Turn on ARP by turning off the ILLF_NOARP flag.
20951  */
20952 static int
20953 ill_arp_on(ill_t *ill)
20954 {
20955 	mblk_t	*mp;
20956 
20957 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20958 
20959 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20960 
20961 	ASSERT(IAM_WRITER_ILL(ill));
20962 	/*
20963 	 * Send an AR_INTERFACE_ON request if we have already done
20964 	 * an arp_off (which allocated the message).
20965 	 */
20966 	if (ill->ill_arp_on_mp != NULL) {
20967 		mp = ill->ill_arp_on_mp;
20968 		ill->ill_arp_on_mp = NULL;
20969 		putnext(ill->ill_rq, mp);
20970 	}
20971 	return (0);
20972 }
20973 
20974 /*
20975  * Called after either deleting ill from the group or when setting
20976  * FAILED or STANDBY on the interface.
20977  */
20978 static void
20979 illgrp_reset_schednext(ill_t *ill)
20980 {
20981 	ill_group_t *illgrp;
20982 	ill_t *save_ill;
20983 
20984 	ASSERT(IAM_WRITER_ILL(ill));
20985 	/*
20986 	 * When called from illgrp_delete, ill_group will be non-NULL.
20987 	 * But when called from ip_sioctl_flags, it could be NULL if
20988 	 * somebody is setting FAILED/INACTIVE on some interface which
20989 	 * is not part of a group.
20990 	 */
20991 	illgrp = ill->ill_group;
20992 	if (illgrp == NULL)
20993 		return;
20994 	if (illgrp->illgrp_ill_schednext != ill)
20995 		return;
20996 
20997 	illgrp->illgrp_ill_schednext = NULL;
20998 	save_ill = ill;
20999 	/*
21000 	 * Choose a good ill to be the next one for
21001 	 * outbound traffic. As the flags FAILED/STANDBY is
21002 	 * not yet marked when called from ip_sioctl_flags,
21003 	 * we check for ill separately.
21004 	 */
21005 	for (ill = illgrp->illgrp_ill; ill != NULL;
21006 	    ill = ill->ill_group_next) {
21007 		if ((ill != save_ill) &&
21008 		    !(ill->ill_phyint->phyint_flags &
21009 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
21010 			illgrp->illgrp_ill_schednext = ill;
21011 			return;
21012 		}
21013 	}
21014 }
21015 
21016 /*
21017  * Given an ill, find the next ill in the group to be scheduled.
21018  * (This should be called by ip_newroute() before ire_create().)
21019  * The passed in ill may be pulled out of the group, after we have picked
21020  * up a different outgoing ill from the same group. However ire add will
21021  * atomically check this.
21022  */
21023 ill_t *
21024 illgrp_scheduler(ill_t *ill)
21025 {
21026 	ill_t *retill;
21027 	ill_group_t *illgrp;
21028 	int illcnt;
21029 	int i;
21030 	uint64_t flags;
21031 	ip_stack_t	*ipst = ill->ill_ipst;
21032 
21033 	/*
21034 	 * We don't use a lock to check for the ill_group. If this ill
21035 	 * is currently being inserted we may end up just returning this
21036 	 * ill itself. That is ok.
21037 	 */
21038 	if (ill->ill_group == NULL) {
21039 		ill_refhold(ill);
21040 		return (ill);
21041 	}
21042 
21043 	/*
21044 	 * Grab the ill_g_lock as reader to make sure we are dealing with
21045 	 * a set of stable ills. No ill can be added or deleted or change
21046 	 * group while we hold the reader lock.
21047 	 */
21048 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21049 	if ((illgrp = ill->ill_group) == NULL) {
21050 		rw_exit(&ipst->ips_ill_g_lock);
21051 		ill_refhold(ill);
21052 		return (ill);
21053 	}
21054 
21055 	illcnt = illgrp->illgrp_ill_count;
21056 	mutex_enter(&illgrp->illgrp_lock);
21057 	retill = illgrp->illgrp_ill_schednext;
21058 
21059 	if (retill == NULL)
21060 		retill = illgrp->illgrp_ill;
21061 
21062 	/*
21063 	 * We do a circular search beginning at illgrp_ill_schednext
21064 	 * or illgrp_ill. We don't check the flags against the ill lock
21065 	 * since it can change anytime. The ire creation will be atomic
21066 	 * and will fail if the ill is FAILED or OFFLINE.
21067 	 */
21068 	for (i = 0; i < illcnt; i++) {
21069 		flags = retill->ill_phyint->phyint_flags;
21070 
21071 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
21072 		    ILL_CAN_LOOKUP(retill)) {
21073 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
21074 			ill_refhold(retill);
21075 			break;
21076 		}
21077 		retill = retill->ill_group_next;
21078 		if (retill == NULL)
21079 			retill = illgrp->illgrp_ill;
21080 	}
21081 	mutex_exit(&illgrp->illgrp_lock);
21082 	rw_exit(&ipst->ips_ill_g_lock);
21083 
21084 	return (i == illcnt ? NULL : retill);
21085 }
21086 
21087 /*
21088  * Checks for availbility of a usable source address (if there is one) when the
21089  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
21090  * this selection is done regardless of the destination.
21091  */
21092 boolean_t
21093 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
21094 {
21095 	uint_t	ifindex;
21096 	ipif_t	*ipif = NULL;
21097 	ill_t	*uill;
21098 	boolean_t isv6;
21099 	ip_stack_t	*ipst = ill->ill_ipst;
21100 
21101 	ASSERT(ill != NULL);
21102 
21103 	isv6 = ill->ill_isv6;
21104 	ifindex = ill->ill_usesrc_ifindex;
21105 	if (ifindex != 0) {
21106 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
21107 		    NULL, ipst);
21108 		if (uill == NULL)
21109 			return (NULL);
21110 		mutex_enter(&uill->ill_lock);
21111 		for (ipif = uill->ill_ipif; ipif != NULL;
21112 		    ipif = ipif->ipif_next) {
21113 			if (!IPIF_CAN_LOOKUP(ipif))
21114 				continue;
21115 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
21116 				continue;
21117 			if (!(ipif->ipif_flags & IPIF_UP))
21118 				continue;
21119 			if (ipif->ipif_zoneid != zoneid)
21120 				continue;
21121 			if ((isv6 &&
21122 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
21123 			    (ipif->ipif_lcl_addr == INADDR_ANY))
21124 				continue;
21125 			mutex_exit(&uill->ill_lock);
21126 			ill_refrele(uill);
21127 			return (B_TRUE);
21128 		}
21129 		mutex_exit(&uill->ill_lock);
21130 		ill_refrele(uill);
21131 	}
21132 	return (B_FALSE);
21133 }
21134 
21135 /*
21136  * Determine the best source address given a destination address and an ill.
21137  * Prefers non-deprecated over deprecated but will return a deprecated
21138  * address if there is no other choice. If there is a usable source address
21139  * on the interface pointed to by ill_usesrc_ifindex then that is given
21140  * first preference.
21141  *
21142  * Returns NULL if there is no suitable source address for the ill.
21143  * This only occurs when there is no valid source address for the ill.
21144  */
21145 ipif_t *
21146 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
21147 {
21148 	ipif_t *ipif;
21149 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
21150 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
21151 	int index = 0;
21152 	boolean_t wrapped = B_FALSE;
21153 	boolean_t same_subnet_only = B_FALSE;
21154 	boolean_t ipif_same_found, ipif_other_found;
21155 	boolean_t specific_found;
21156 	ill_t	*till, *usill = NULL;
21157 	tsol_tpc_t *src_rhtp, *dst_rhtp;
21158 	ip_stack_t	*ipst = ill->ill_ipst;
21159 
21160 	if (ill->ill_usesrc_ifindex != 0) {
21161 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
21162 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
21163 		if (usill != NULL)
21164 			ill = usill;	/* Select source from usesrc ILL */
21165 		else
21166 			return (NULL);
21167 	}
21168 
21169 	/*
21170 	 * If we're dealing with an unlabeled destination on a labeled system,
21171 	 * make sure that we ignore source addresses that are incompatible with
21172 	 * the destination's default label.  That destination's default label
21173 	 * must dominate the minimum label on the source address.
21174 	 */
21175 	dst_rhtp = NULL;
21176 	if (is_system_labeled()) {
21177 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
21178 		if (dst_rhtp == NULL)
21179 			return (NULL);
21180 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
21181 			TPC_RELE(dst_rhtp);
21182 			dst_rhtp = NULL;
21183 		}
21184 	}
21185 
21186 	/*
21187 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
21188 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
21189 	 * After selecting the right ipif, under ill_lock make sure ipif is
21190 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
21191 	 * we retry. Inside the loop we still need to check for CONDEMNED,
21192 	 * but not under a lock.
21193 	 */
21194 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21195 
21196 retry:
21197 	till = ill;
21198 	ipif_arr[0] = NULL;
21199 
21200 	if (till->ill_group != NULL)
21201 		till = till->ill_group->illgrp_ill;
21202 
21203 	/*
21204 	 * Choose one good source address from each ill across the group.
21205 	 * If possible choose a source address in the same subnet as
21206 	 * the destination address.
21207 	 *
21208 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
21209 	 * This is okay because of the following.
21210 	 *
21211 	 *    If PHYI_FAILED is set and we still have non-deprecated
21212 	 *    addresses, it means the addresses have not yet been
21213 	 *    failed over to a different interface. We potentially
21214 	 *    select them to create IRE_CACHES, which will be later
21215 	 *    flushed when the addresses move over.
21216 	 *
21217 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
21218 	 *    addresses, it means either the user has configured them
21219 	 *    or PHYI_INACTIVE has not been cleared after the addresses
21220 	 *    been moved over. For the former, in.mpathd does a failover
21221 	 *    when the interface becomes INACTIVE and hence we should
21222 	 *    not find them. Once INACTIVE is set, we don't allow them
21223 	 *    to create logical interfaces anymore. For the latter, a
21224 	 *    flush will happen when INACTIVE is cleared which will
21225 	 *    flush the IRE_CACHES.
21226 	 *
21227 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
21228 	 *    over soon. We potentially select them to create IRE_CACHEs,
21229 	 *    which will be later flushed when the addresses move over.
21230 	 *
21231 	 * NOTE : As ipif_select_source is called to borrow source address
21232 	 * for an ipif that is part of a group, source address selection
21233 	 * will be re-done whenever the group changes i.e either an
21234 	 * insertion/deletion in the group.
21235 	 *
21236 	 * Fill ipif_arr[] with source addresses, using these rules:
21237 	 *
21238 	 *	1. At most one source address from a given ill ends up
21239 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
21240 	 *	   associated with a given ill ends up in ipif_arr[].
21241 	 *
21242 	 *	2. If there is at least one non-deprecated ipif in the
21243 	 *	   IPMP group with a source address on the same subnet as
21244 	 *	   our destination, then fill ipif_arr[] only with
21245 	 *	   source addresses on the same subnet as our destination.
21246 	 *	   Note that because of (1), only the first
21247 	 *	   non-deprecated ipif found with a source address
21248 	 *	   matching the destination ends up in ipif_arr[].
21249 	 *
21250 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
21251 	 *	   addresses not in the same subnet as our destination.
21252 	 *	   Again, because of (1), only the first off-subnet source
21253 	 *	   address will be chosen.
21254 	 *
21255 	 *	4. If there are no non-deprecated ipifs, then just use
21256 	 *	   the source address associated with the last deprecated
21257 	 *	   one we find that happens to be on the same subnet,
21258 	 *	   otherwise the first one not in the same subnet.
21259 	 */
21260 	specific_found = B_FALSE;
21261 	for (; till != NULL; till = till->ill_group_next) {
21262 		ipif_same_found = B_FALSE;
21263 		ipif_other_found = B_FALSE;
21264 		for (ipif = till->ill_ipif; ipif != NULL;
21265 		    ipif = ipif->ipif_next) {
21266 			if (!IPIF_CAN_LOOKUP(ipif))
21267 				continue;
21268 			/* Always skip NOLOCAL and ANYCAST interfaces */
21269 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
21270 				continue;
21271 			if (!(ipif->ipif_flags & IPIF_UP) ||
21272 			    !ipif->ipif_addr_ready)
21273 				continue;
21274 			if (ipif->ipif_zoneid != zoneid &&
21275 			    ipif->ipif_zoneid != ALL_ZONES)
21276 				continue;
21277 			/*
21278 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
21279 			 * but are not valid as source addresses.
21280 			 */
21281 			if (ipif->ipif_lcl_addr == INADDR_ANY)
21282 				continue;
21283 
21284 			/*
21285 			 * Check compatibility of local address for
21286 			 * destination's default label if we're on a labeled
21287 			 * system.  Incompatible addresses can't be used at
21288 			 * all.
21289 			 */
21290 			if (dst_rhtp != NULL) {
21291 				boolean_t incompat;
21292 
21293 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
21294 				    IPV4_VERSION, B_FALSE);
21295 				if (src_rhtp == NULL)
21296 					continue;
21297 				incompat =
21298 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
21299 				    src_rhtp->tpc_tp.tp_doi !=
21300 				    dst_rhtp->tpc_tp.tp_doi ||
21301 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
21302 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
21303 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
21304 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
21305 				TPC_RELE(src_rhtp);
21306 				if (incompat)
21307 					continue;
21308 			}
21309 
21310 			/*
21311 			 * We prefer not to use all all-zones addresses, if we
21312 			 * can avoid it, as they pose problems with unlabeled
21313 			 * destinations.
21314 			 */
21315 			if (ipif->ipif_zoneid != ALL_ZONES) {
21316 				if (!specific_found &&
21317 				    (!same_subnet_only ||
21318 				    (ipif->ipif_net_mask & dst) ==
21319 				    ipif->ipif_subnet)) {
21320 					index = 0;
21321 					specific_found = B_TRUE;
21322 					ipif_other_found = B_FALSE;
21323 				}
21324 			} else {
21325 				if (specific_found)
21326 					continue;
21327 			}
21328 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
21329 				if (ipif_dep == NULL ||
21330 				    (ipif->ipif_net_mask & dst) ==
21331 				    ipif->ipif_subnet)
21332 					ipif_dep = ipif;
21333 				continue;
21334 			}
21335 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
21336 				/* found a source address in the same subnet */
21337 				if (!same_subnet_only) {
21338 					same_subnet_only = B_TRUE;
21339 					index = 0;
21340 				}
21341 				ipif_same_found = B_TRUE;
21342 			} else {
21343 				if (same_subnet_only || ipif_other_found)
21344 					continue;
21345 				ipif_other_found = B_TRUE;
21346 			}
21347 			ipif_arr[index++] = ipif;
21348 			if (index == MAX_IPIF_SELECT_SOURCE) {
21349 				wrapped = B_TRUE;
21350 				index = 0;
21351 			}
21352 			if (ipif_same_found)
21353 				break;
21354 		}
21355 	}
21356 
21357 	if (ipif_arr[0] == NULL) {
21358 		ipif = ipif_dep;
21359 	} else {
21360 		if (wrapped)
21361 			index = MAX_IPIF_SELECT_SOURCE;
21362 		ipif = ipif_arr[ipif_rand(ipst) % index];
21363 		ASSERT(ipif != NULL);
21364 	}
21365 
21366 	if (ipif != NULL) {
21367 		mutex_enter(&ipif->ipif_ill->ill_lock);
21368 		if (!IPIF_CAN_LOOKUP(ipif)) {
21369 			mutex_exit(&ipif->ipif_ill->ill_lock);
21370 			goto retry;
21371 		}
21372 		ipif_refhold_locked(ipif);
21373 		mutex_exit(&ipif->ipif_ill->ill_lock);
21374 	}
21375 
21376 	rw_exit(&ipst->ips_ill_g_lock);
21377 	if (usill != NULL)
21378 		ill_refrele(usill);
21379 	if (dst_rhtp != NULL)
21380 		TPC_RELE(dst_rhtp);
21381 
21382 #ifdef DEBUG
21383 	if (ipif == NULL) {
21384 		char buf1[INET6_ADDRSTRLEN];
21385 
21386 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
21387 		    ill->ill_name,
21388 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
21389 	} else {
21390 		char buf1[INET6_ADDRSTRLEN];
21391 		char buf2[INET6_ADDRSTRLEN];
21392 
21393 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
21394 		    ipif->ipif_ill->ill_name,
21395 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
21396 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
21397 		    buf2, sizeof (buf2))));
21398 	}
21399 #endif /* DEBUG */
21400 	return (ipif);
21401 }
21402 
21403 
21404 /*
21405  * If old_ipif is not NULL, see if ipif was derived from old
21406  * ipif and if so, recreate the interface route by re-doing
21407  * source address selection. This happens when ipif_down ->
21408  * ipif_update_other_ipifs calls us.
21409  *
21410  * If old_ipif is NULL, just redo the source address selection
21411  * if needed. This happens when illgrp_insert or ipif_up_done
21412  * calls us.
21413  */
21414 static void
21415 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
21416 {
21417 	ire_t *ire;
21418 	ire_t *ipif_ire;
21419 	queue_t *stq;
21420 	ipif_t *nipif;
21421 	ill_t *ill;
21422 	boolean_t need_rele = B_FALSE;
21423 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21424 
21425 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
21426 	ASSERT(IAM_WRITER_IPIF(ipif));
21427 
21428 	ill = ipif->ipif_ill;
21429 	if (!(ipif->ipif_flags &
21430 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
21431 		/*
21432 		 * Can't possibly have borrowed the source
21433 		 * from old_ipif.
21434 		 */
21435 		return;
21436 	}
21437 
21438 	/*
21439 	 * Is there any work to be done? No work if the address
21440 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21441 	 * ipif_select_source() does not borrow addresses from
21442 	 * NOLOCAL and ANYCAST interfaces).
21443 	 */
21444 	if ((old_ipif != NULL) &&
21445 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21446 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21447 	    (old_ipif->ipif_flags &
21448 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21449 		return;
21450 	}
21451 
21452 	/*
21453 	 * Perform the same checks as when creating the
21454 	 * IRE_INTERFACE in ipif_up_done.
21455 	 */
21456 	if (!(ipif->ipif_flags & IPIF_UP))
21457 		return;
21458 
21459 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21460 	    (ipif->ipif_subnet == INADDR_ANY))
21461 		return;
21462 
21463 	ipif_ire = ipif_to_ire(ipif);
21464 	if (ipif_ire == NULL)
21465 		return;
21466 
21467 	/*
21468 	 * We know that ipif uses some other source for its
21469 	 * IRE_INTERFACE. Is it using the source of this
21470 	 * old_ipif?
21471 	 */
21472 	if (old_ipif != NULL &&
21473 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21474 		ire_refrele(ipif_ire);
21475 		return;
21476 	}
21477 	if (ip_debug > 2) {
21478 		/* ip1dbg */
21479 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21480 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21481 	}
21482 
21483 	stq = ipif_ire->ire_stq;
21484 
21485 	/*
21486 	 * Can't use our source address. Select a different
21487 	 * source address for the IRE_INTERFACE.
21488 	 */
21489 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21490 	if (nipif == NULL) {
21491 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21492 		nipif = ipif;
21493 	} else {
21494 		need_rele = B_TRUE;
21495 	}
21496 
21497 	ire = ire_create(
21498 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21499 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21500 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21501 	    NULL,				/* no gateway */
21502 	    NULL,
21503 	    &ipif->ipif_mtu,			/* max frag */
21504 	    NULL,				/* no src nce */
21505 	    NULL,				/* no recv from queue */
21506 	    stq,				/* send-to queue */
21507 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21508 	    ipif,
21509 	    NULL,
21510 	    0,
21511 	    0,
21512 	    0,
21513 	    0,
21514 	    &ire_uinfo_null,
21515 	    NULL,
21516 	    NULL,
21517 	    ipst);
21518 
21519 	if (ire != NULL) {
21520 		ire_t *ret_ire;
21521 		int error;
21522 
21523 		/*
21524 		 * We don't need ipif_ire anymore. We need to delete
21525 		 * before we add so that ire_add does not detect
21526 		 * duplicates.
21527 		 */
21528 		ire_delete(ipif_ire);
21529 		ret_ire = ire;
21530 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21531 		ASSERT(error == 0);
21532 		ASSERT(ire == ret_ire);
21533 		/* Held in ire_add */
21534 		ire_refrele(ret_ire);
21535 	}
21536 	/*
21537 	 * Either we are falling through from above or could not
21538 	 * allocate a replacement.
21539 	 */
21540 	ire_refrele(ipif_ire);
21541 	if (need_rele)
21542 		ipif_refrele(nipif);
21543 }
21544 
21545 /*
21546  * This old_ipif is going away.
21547  *
21548  * Determine if any other ipif's is using our address as
21549  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21550  * IPIF_DEPRECATED).
21551  * Find the IRE_INTERFACE for such ipifs and recreate them
21552  * to use an different source address following the rules in
21553  * ipif_up_done.
21554  *
21555  * This function takes an illgrp as an argument so that illgrp_delete
21556  * can call this to update source address even after deleting the
21557  * old_ipif->ipif_ill from the ill group.
21558  */
21559 static void
21560 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21561 {
21562 	ipif_t *ipif;
21563 	ill_t *ill;
21564 	char	buf[INET6_ADDRSTRLEN];
21565 
21566 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21567 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21568 
21569 	ill = old_ipif->ipif_ill;
21570 
21571 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21572 	    ill->ill_name,
21573 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21574 	    buf, sizeof (buf))));
21575 	/*
21576 	 * If this part of a group, look at all ills as ipif_select_source
21577 	 * borrows source address across all the ills in the group.
21578 	 */
21579 	if (illgrp != NULL)
21580 		ill = illgrp->illgrp_ill;
21581 
21582 	for (; ill != NULL; ill = ill->ill_group_next) {
21583 		for (ipif = ill->ill_ipif; ipif != NULL;
21584 		    ipif = ipif->ipif_next) {
21585 
21586 			if (ipif == old_ipif)
21587 				continue;
21588 
21589 			ipif_recreate_interface_routes(old_ipif, ipif);
21590 		}
21591 	}
21592 }
21593 
21594 /* ARGSUSED */
21595 int
21596 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21597 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21598 {
21599 	/*
21600 	 * ill_phyint_reinit merged the v4 and v6 into a single
21601 	 * ipsq. Could also have become part of a ipmp group in the
21602 	 * process, and we might not have been able to complete the
21603 	 * operation in ipif_set_values, if we could not become
21604 	 * exclusive.  If so restart it here.
21605 	 */
21606 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21607 }
21608 
21609 
21610 /*
21611  * Can operate on either a module or a driver queue.
21612  * Returns an error if not a module queue.
21613  */
21614 /* ARGSUSED */
21615 int
21616 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21617     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21618 {
21619 	queue_t		*q1 = q;
21620 	char 		*cp;
21621 	char		interf_name[LIFNAMSIZ];
21622 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21623 
21624 	if (q->q_next == NULL) {
21625 		ip1dbg((
21626 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21627 		return (EINVAL);
21628 	}
21629 
21630 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21631 		return (EALREADY);
21632 
21633 	do {
21634 		q1 = q1->q_next;
21635 	} while (q1->q_next);
21636 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21637 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21638 
21639 	/*
21640 	 * Here we are not going to delay the ioack until after
21641 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21642 	 * original ioctl message before sending the requests.
21643 	 */
21644 	return (ipif_set_values(q, mp, interf_name, &ppa));
21645 }
21646 
21647 /* ARGSUSED */
21648 int
21649 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21650     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21651 {
21652 	return (ENXIO);
21653 }
21654 
21655 /*
21656  * Net and subnet broadcast ire's are now specific to the particular
21657  * physical interface (ill) and not to any one locigal interface (ipif).
21658  * However, if a particular logical interface is being taken down, it's
21659  * associated ire's will be taken down as well.  Hence, when we go to
21660  * take down or change the local address, broadcast address or netmask
21661  * of a specific logical interface, we must check to make sure that we
21662  * have valid net and subnet broadcast ire's for the other logical
21663  * interfaces which may have been shared with the logical interface
21664  * being brought down or changed.
21665  *
21666  * There is one set of 0.0.0.0 and 255.255.255.255 per ill. Usually it
21667  * is tied to the first interface coming UP. If that ipif is going down,
21668  * we need to recreate them on the next valid ipif.
21669  *
21670  * Note: assume that the ipif passed in is still up so that it's IRE
21671  * entries are still valid.
21672  */
21673 static void
21674 ipif_check_bcast_ires(ipif_t *test_ipif)
21675 {
21676 	ipif_t	*ipif;
21677 	ire_t	*test_subnet_ire, *test_net_ire;
21678 	ire_t	*test_allzero_ire, *test_allone_ire;
21679 	ire_t	*ire_array[12];
21680 	ire_t	**irep = &ire_array[0];
21681 	ire_t	**irep1;
21682 	ipaddr_t net_addr, subnet_addr, net_mask, subnet_mask;
21683 	ipaddr_t test_net_addr, test_subnet_addr;
21684 	ipaddr_t test_net_mask, test_subnet_mask;
21685 	boolean_t need_net_bcast_ire = B_FALSE;
21686 	boolean_t need_subnet_bcast_ire = B_FALSE;
21687 	boolean_t allzero_bcast_ire_created = B_FALSE;
21688 	boolean_t allone_bcast_ire_created = B_FALSE;
21689 	boolean_t net_bcast_ire_created = B_FALSE;
21690 	boolean_t subnet_bcast_ire_created = B_FALSE;
21691 
21692 	ipif_t  *backup_ipif_net = (ipif_t *)NULL;
21693 	ipif_t  *backup_ipif_subnet = (ipif_t *)NULL;
21694 	ipif_t  *backup_ipif_allzeros = (ipif_t *)NULL;
21695 	ipif_t  *backup_ipif_allones = (ipif_t *)NULL;
21696 	uint64_t check_flags = IPIF_DEPRECATED | IPIF_NOLOCAL | IPIF_ANYCAST;
21697 	ip_stack_t	*ipst = test_ipif->ipif_ill->ill_ipst;
21698 
21699 	ASSERT(!test_ipif->ipif_isv6);
21700 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21701 
21702 	/*
21703 	 * No broadcast IREs for the LOOPBACK interface
21704 	 * or others such as point to point and IPIF_NOXMIT.
21705 	 */
21706 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21707 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21708 		return;
21709 
21710 	test_allzero_ire = ire_ctable_lookup(0, 0, IRE_BROADCAST,
21711 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF),
21712 	    ipst);
21713 
21714 	test_allone_ire = ire_ctable_lookup(INADDR_BROADCAST, 0, IRE_BROADCAST,
21715 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF),
21716 	    ipst);
21717 
21718 	test_net_mask = ip_net_mask(test_ipif->ipif_subnet);
21719 	test_subnet_mask = test_ipif->ipif_net_mask;
21720 
21721 	/*
21722 	 * If no net mask set, assume the default based on net class.
21723 	 */
21724 	if (test_subnet_mask == 0)
21725 		test_subnet_mask = test_net_mask;
21726 
21727 	/*
21728 	 * Check if there is a network broadcast ire associated with this ipif
21729 	 */
21730 	test_net_addr = test_net_mask  & test_ipif->ipif_subnet;
21731 	test_net_ire = ire_ctable_lookup(test_net_addr, 0, IRE_BROADCAST,
21732 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF),
21733 	    ipst);
21734 
21735 	/*
21736 	 * Check if there is a subnet broadcast IRE associated with this ipif
21737 	 */
21738 	test_subnet_addr = test_subnet_mask  & test_ipif->ipif_subnet;
21739 	test_subnet_ire = ire_ctable_lookup(test_subnet_addr, 0, IRE_BROADCAST,
21740 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF),
21741 	    ipst);
21742 
21743 	/*
21744 	 * No broadcast ire's associated with this ipif.
21745 	 */
21746 	if ((test_subnet_ire == NULL) && (test_net_ire == NULL) &&
21747 	    (test_allzero_ire == NULL) && (test_allone_ire == NULL)) {
21748 		return;
21749 	}
21750 
21751 	/*
21752 	 * We have established which bcast ires have to be replaced.
21753 	 * Next we try to locate ipifs that match there ires.
21754 	 * The rules are simple: If we find an ipif that matches on the subnet
21755 	 * address it will also match on the net address, the allzeros and
21756 	 * allones address. Any ipif that matches only on the net address will
21757 	 * also match the allzeros and allones addresses.
21758 	 * The other criterion is the ipif_flags. We look for non-deprecated
21759 	 * (and non-anycast and non-nolocal) ipifs as the best choice.
21760 	 * ipifs with check_flags matching (deprecated, etc) are used only
21761 	 * if good ipifs are not available. While looping, we save existing
21762 	 * deprecated ipifs as backup_ipif.
21763 	 * We loop through all the ipifs for this ill looking for ipifs
21764 	 * whose broadcast addr match the ipif passed in, but do not have
21765 	 * their own broadcast ires. For creating 0.0.0.0 and
21766 	 * 255.255.255.255 we just need an ipif on this ill to create.
21767 	 */
21768 	for (ipif = test_ipif->ipif_ill->ill_ipif; ipif != NULL;
21769 	    ipif = ipif->ipif_next) {
21770 
21771 		ASSERT(!ipif->ipif_isv6);
21772 		/*
21773 		 * Already checked the ipif passed in.
21774 		 */
21775 		if (ipif == test_ipif) {
21776 			continue;
21777 		}
21778 
21779 		/*
21780 		 * We only need to recreate broadcast ires if another ipif in
21781 		 * the same zone uses them. The new ires must be created in the
21782 		 * same zone.
21783 		 */
21784 		if (ipif->ipif_zoneid != test_ipif->ipif_zoneid) {
21785 			continue;
21786 		}
21787 
21788 		/*
21789 		 * Only interested in logical interfaces with valid local
21790 		 * addresses or with the ability to broadcast.
21791 		 */
21792 		if ((ipif->ipif_subnet == 0) ||
21793 		    !(ipif->ipif_flags & IPIF_BROADCAST) ||
21794 		    (ipif->ipif_flags & IPIF_NOXMIT) ||
21795 		    !(ipif->ipif_flags & IPIF_UP)) {
21796 			continue;
21797 		}
21798 		/*
21799 		 * Check if there is a net broadcast ire for this
21800 		 * net address.  If it turns out that the ipif we are
21801 		 * about to take down owns this ire, we must make a
21802 		 * new one because it is potentially going away.
21803 		 */
21804 		if (test_net_ire && (!net_bcast_ire_created)) {
21805 			net_mask = ip_net_mask(ipif->ipif_subnet);
21806 			net_addr = net_mask & ipif->ipif_subnet;
21807 			if (net_addr == test_net_addr) {
21808 				need_net_bcast_ire = B_TRUE;
21809 				/*
21810 				 * Use DEPRECATED ipif only if no good
21811 				 * ires are available. subnet_addr is
21812 				 * a better match than net_addr.
21813 				 */
21814 				if ((ipif->ipif_flags & check_flags) &&
21815 				    (backup_ipif_net == NULL)) {
21816 					backup_ipif_net = ipif;
21817 				}
21818 			}
21819 		}
21820 		/*
21821 		 * Check if there is a subnet broadcast ire for this
21822 		 * net address.  If it turns out that the ipif we are
21823 		 * about to take down owns this ire, we must make a
21824 		 * new one because it is potentially going away.
21825 		 */
21826 		if (test_subnet_ire && (!subnet_bcast_ire_created)) {
21827 			subnet_mask = ipif->ipif_net_mask;
21828 			subnet_addr = ipif->ipif_subnet;
21829 			if (subnet_addr == test_subnet_addr) {
21830 				need_subnet_bcast_ire = B_TRUE;
21831 				if ((ipif->ipif_flags & check_flags) &&
21832 				    (backup_ipif_subnet == NULL)) {
21833 					backup_ipif_subnet = ipif;
21834 				}
21835 			}
21836 		}
21837 
21838 
21839 		/* Short circuit here if this ipif is deprecated */
21840 		if (ipif->ipif_flags & check_flags) {
21841 			if ((test_allzero_ire != NULL) &&
21842 			    (!allzero_bcast_ire_created) &&
21843 			    (backup_ipif_allzeros == NULL)) {
21844 				backup_ipif_allzeros = ipif;
21845 			}
21846 			if ((test_allone_ire != NULL) &&
21847 			    (!allone_bcast_ire_created) &&
21848 			    (backup_ipif_allones == NULL)) {
21849 				backup_ipif_allones = ipif;
21850 			}
21851 			continue;
21852 		}
21853 
21854 		/*
21855 		 * Found an ipif which has the same broadcast ire as the
21856 		 * ipif passed in and the ipif passed in "owns" the ire.
21857 		 * Create new broadcast ire's for this broadcast addr.
21858 		 */
21859 		if (need_net_bcast_ire && !net_bcast_ire_created) {
21860 			irep = ire_create_bcast(ipif, net_addr, irep);
21861 			irep = ire_create_bcast(ipif,
21862 			    ~net_mask | net_addr, irep);
21863 			net_bcast_ire_created = B_TRUE;
21864 		}
21865 		if (need_subnet_bcast_ire && !subnet_bcast_ire_created) {
21866 			irep = ire_create_bcast(ipif, subnet_addr, irep);
21867 			irep = ire_create_bcast(ipif,
21868 			    ~subnet_mask | subnet_addr, irep);
21869 			subnet_bcast_ire_created = B_TRUE;
21870 		}
21871 		if (test_allzero_ire != NULL && !allzero_bcast_ire_created) {
21872 			irep = ire_create_bcast(ipif, 0, irep);
21873 			allzero_bcast_ire_created = B_TRUE;
21874 		}
21875 		if (test_allone_ire != NULL && !allone_bcast_ire_created) {
21876 			irep = ire_create_bcast(ipif, INADDR_BROADCAST, irep);
21877 			allone_bcast_ire_created = B_TRUE;
21878 		}
21879 		/*
21880 		 * Once we have created all the appropriate ires, we
21881 		 * just break out of this loop to add what we have created.
21882 		 * This has been indented similar to ire_match_args for
21883 		 * readability.
21884 		 */
21885 		if (((test_net_ire == NULL) ||
21886 		    (net_bcast_ire_created)) &&
21887 		    ((test_subnet_ire == NULL) ||
21888 		    (subnet_bcast_ire_created)) &&
21889 		    ((test_allzero_ire == NULL) ||
21890 		    (allzero_bcast_ire_created)) &&
21891 		    ((test_allone_ire == NULL) ||
21892 		    (allone_bcast_ire_created))) {
21893 			break;
21894 		}
21895 	}
21896 
21897 	/*
21898 	 * Create bcast ires on deprecated ipifs if no non-deprecated ipifs
21899 	 * exist. 6 pairs of bcast ires are needed.
21900 	 * Note - the old ires are deleted in ipif_down.
21901 	 */
21902 	if (need_net_bcast_ire && !net_bcast_ire_created && backup_ipif_net) {
21903 		ipif = backup_ipif_net;
21904 		irep = ire_create_bcast(ipif, net_addr, irep);
21905 		irep = ire_create_bcast(ipif, ~net_mask | net_addr, irep);
21906 		net_bcast_ire_created = B_TRUE;
21907 	}
21908 	if (need_subnet_bcast_ire && !subnet_bcast_ire_created &&
21909 	    backup_ipif_subnet) {
21910 		ipif = backup_ipif_subnet;
21911 		irep = ire_create_bcast(ipif, subnet_addr, irep);
21912 		irep = ire_create_bcast(ipif,
21913 		    ~subnet_mask | subnet_addr, irep);
21914 		subnet_bcast_ire_created = B_TRUE;
21915 	}
21916 	if (test_allzero_ire != NULL && !allzero_bcast_ire_created &&
21917 	    backup_ipif_allzeros) {
21918 		irep = ire_create_bcast(backup_ipif_allzeros, 0, irep);
21919 		allzero_bcast_ire_created = B_TRUE;
21920 	}
21921 	if (test_allone_ire != NULL && !allone_bcast_ire_created &&
21922 	    backup_ipif_allones) {
21923 		irep = ire_create_bcast(backup_ipif_allones,
21924 		    INADDR_BROADCAST, irep);
21925 		allone_bcast_ire_created = B_TRUE;
21926 	}
21927 
21928 	/*
21929 	 * If we can't create all of them, don't add any of them.
21930 	 * Code in ip_wput_ire and ire_to_ill assumes that we
21931 	 * always have a non-loopback copy and loopback copy
21932 	 * for a given address.
21933 	 */
21934 	for (irep1 = irep; irep1 > ire_array; ) {
21935 		irep1--;
21936 		if (*irep1 == NULL) {
21937 			ip0dbg(("ipif_check_bcast_ires: can't create "
21938 			    "IRE_BROADCAST, memory allocation failure\n"));
21939 			while (irep > ire_array) {
21940 				irep--;
21941 				if (*irep != NULL)
21942 					ire_delete(*irep);
21943 			}
21944 			goto bad;
21945 		}
21946 	}
21947 	for (irep1 = irep; irep1 > ire_array; ) {
21948 		int error;
21949 
21950 		irep1--;
21951 		error = ire_add(irep1, NULL, NULL, NULL, B_FALSE);
21952 		if (error == 0) {
21953 			ire_refrele(*irep1);		/* Held in ire_add */
21954 		}
21955 	}
21956 bad:
21957 	if (test_allzero_ire != NULL)
21958 		ire_refrele(test_allzero_ire);
21959 	if (test_allone_ire != NULL)
21960 		ire_refrele(test_allone_ire);
21961 	if (test_net_ire != NULL)
21962 		ire_refrele(test_net_ire);
21963 	if (test_subnet_ire != NULL)
21964 		ire_refrele(test_subnet_ire);
21965 }
21966 
21967 /*
21968  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21969  * from lifr_flags and the name from lifr_name.
21970  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21971  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21972  * Returns EINPROGRESS when mp has been consumed by queueing it on
21973  * ill_pending_mp and the ioctl will complete in ip_rput.
21974  *
21975  * Can operate on either a module or a driver queue.
21976  * Returns an error if not a module queue.
21977  */
21978 /* ARGSUSED */
21979 int
21980 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21981     ip_ioctl_cmd_t *ipip, void *if_req)
21982 {
21983 	int	err;
21984 	ill_t	*ill;
21985 	struct lifreq *lifr = (struct lifreq *)if_req;
21986 
21987 	ASSERT(ipif != NULL);
21988 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21989 
21990 	if (q->q_next == NULL) {
21991 		ip1dbg((
21992 		    "if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21993 		return (EINVAL);
21994 	}
21995 
21996 	ill = (ill_t *)q->q_ptr;
21997 	/*
21998 	 * If we are not writer on 'q' then this interface exists already
21999 	 * and previous lookups (ipif_extract_lifreq_cmn) found this ipif.
22000 	 * So return EALREADY
22001 	 */
22002 	if (ill != ipif->ipif_ill)
22003 		return (EALREADY);
22004 
22005 	if (ill->ill_name[0] != '\0')
22006 		return (EALREADY);
22007 
22008 	/*
22009 	 * Set all the flags. Allows all kinds of override. Provide some
22010 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
22011 	 * unless there is either multicast/broadcast support in the driver
22012 	 * or it is a pt-pt link.
22013 	 */
22014 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
22015 		/* Meaningless to IP thus don't allow them to be set. */
22016 		ip1dbg(("ip_setname: EINVAL 1\n"));
22017 		return (EINVAL);
22018 	}
22019 	/*
22020 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
22021 	 * ill_bcast_addr_length info.
22022 	 */
22023 	if (!ill->ill_needs_attach &&
22024 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
22025 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
22026 	    ill->ill_bcast_addr_length == 0)) {
22027 		/* Link not broadcast/pt-pt capable i.e. no multicast */
22028 		ip1dbg(("ip_setname: EINVAL 2\n"));
22029 		return (EINVAL);
22030 	}
22031 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
22032 	    ((lifr->lifr_flags & IFF_IPV6) ||
22033 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
22034 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
22035 		ip1dbg(("ip_setname: EINVAL 3\n"));
22036 		return (EINVAL);
22037 	}
22038 	if (lifr->lifr_flags & IFF_UP) {
22039 		/* Can only be set with SIOCSLIFFLAGS */
22040 		ip1dbg(("ip_setname: EINVAL 4\n"));
22041 		return (EINVAL);
22042 	}
22043 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
22044 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
22045 		ip1dbg(("ip_setname: EINVAL 5\n"));
22046 		return (EINVAL);
22047 	}
22048 	/*
22049 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
22050 	 */
22051 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
22052 	    !(lifr->lifr_flags & IFF_IPV6) &&
22053 	    !(ipif->ipif_isv6)) {
22054 		ip1dbg(("ip_setname: EINVAL 6\n"));
22055 		return (EINVAL);
22056 	}
22057 
22058 	/*
22059 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
22060 	 * we have all the flags here. So, we assign rather than we OR.
22061 	 * We can't OR the flags here because we don't want to set
22062 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
22063 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
22064 	 * on lifr_flags value here.
22065 	 */
22066 	/*
22067 	 * This ill has not been inserted into the global list.
22068 	 * So we are still single threaded and don't need any lock
22069 	 */
22070 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS &
22071 	    ~IFF_DUPLICATE;
22072 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
22073 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
22074 
22075 	/* We started off as V4. */
22076 	if (ill->ill_flags & ILLF_IPV6) {
22077 		ill->ill_phyint->phyint_illv6 = ill;
22078 		ill->ill_phyint->phyint_illv4 = NULL;
22079 	}
22080 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
22081 	return (err);
22082 }
22083 
22084 /* ARGSUSED */
22085 int
22086 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22087     ip_ioctl_cmd_t *ipip, void *if_req)
22088 {
22089 	/*
22090 	 * ill_phyint_reinit merged the v4 and v6 into a single
22091 	 * ipsq. Could also have become part of a ipmp group in the
22092 	 * process, and we might not have been able to complete the
22093 	 * slifname in ipif_set_values, if we could not become
22094 	 * exclusive.  If so restart it here
22095 	 */
22096 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
22097 }
22098 
22099 /*
22100  * Return a pointer to the ipif which matches the index, IP version type and
22101  * zoneid.
22102  */
22103 ipif_t *
22104 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
22105     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
22106 {
22107 	ill_t	*ill;
22108 	ipsq_t  *ipsq;
22109 	phyint_t *phyi;
22110 	ipif_t	*ipif;
22111 
22112 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
22113 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
22114 
22115 	if (err != NULL)
22116 		*err = 0;
22117 
22118 	/*
22119 	 * Indexes are stored in the phyint - a common structure
22120 	 * to both IPv4 and IPv6.
22121 	 */
22122 
22123 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
22124 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22125 	    (void *) &index, NULL);
22126 	if (phyi != NULL) {
22127 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
22128 		if (ill == NULL) {
22129 			rw_exit(&ipst->ips_ill_g_lock);
22130 			if (err != NULL)
22131 				*err = ENXIO;
22132 			return (NULL);
22133 		}
22134 		GRAB_CONN_LOCK(q);
22135 		mutex_enter(&ill->ill_lock);
22136 		if (ILL_CAN_LOOKUP(ill)) {
22137 			for (ipif = ill->ill_ipif; ipif != NULL;
22138 			    ipif = ipif->ipif_next) {
22139 				if (IPIF_CAN_LOOKUP(ipif) &&
22140 				    (zoneid == ALL_ZONES ||
22141 				    zoneid == ipif->ipif_zoneid ||
22142 				    ipif->ipif_zoneid == ALL_ZONES)) {
22143 					ipif_refhold_locked(ipif);
22144 					mutex_exit(&ill->ill_lock);
22145 					RELEASE_CONN_LOCK(q);
22146 					rw_exit(&ipst->ips_ill_g_lock);
22147 					return (ipif);
22148 				}
22149 			}
22150 		} else if (ILL_CAN_WAIT(ill, q)) {
22151 			ipsq = ill->ill_phyint->phyint_ipsq;
22152 			mutex_enter(&ipsq->ipsq_lock);
22153 			rw_exit(&ipst->ips_ill_g_lock);
22154 			mutex_exit(&ill->ill_lock);
22155 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
22156 			mutex_exit(&ipsq->ipsq_lock);
22157 			RELEASE_CONN_LOCK(q);
22158 			*err = EINPROGRESS;
22159 			return (NULL);
22160 		}
22161 		mutex_exit(&ill->ill_lock);
22162 		RELEASE_CONN_LOCK(q);
22163 	}
22164 	rw_exit(&ipst->ips_ill_g_lock);
22165 	if (err != NULL)
22166 		*err = ENXIO;
22167 	return (NULL);
22168 }
22169 
22170 typedef struct conn_change_s {
22171 	uint_t cc_old_ifindex;
22172 	uint_t cc_new_ifindex;
22173 } conn_change_t;
22174 
22175 /*
22176  * ipcl_walk function for changing interface index.
22177  */
22178 static void
22179 conn_change_ifindex(conn_t *connp, caddr_t arg)
22180 {
22181 	conn_change_t *connc;
22182 	uint_t old_ifindex;
22183 	uint_t new_ifindex;
22184 	int i;
22185 	ilg_t *ilg;
22186 
22187 	connc = (conn_change_t *)arg;
22188 	old_ifindex = connc->cc_old_ifindex;
22189 	new_ifindex = connc->cc_new_ifindex;
22190 
22191 	if (connp->conn_orig_bound_ifindex == old_ifindex)
22192 		connp->conn_orig_bound_ifindex = new_ifindex;
22193 
22194 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
22195 		connp->conn_orig_multicast_ifindex = new_ifindex;
22196 
22197 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
22198 		connp->conn_orig_xmit_ifindex = new_ifindex;
22199 
22200 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
22201 		ilg = &connp->conn_ilg[i];
22202 		if (ilg->ilg_orig_ifindex == old_ifindex)
22203 			ilg->ilg_orig_ifindex = new_ifindex;
22204 	}
22205 }
22206 
22207 /*
22208  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
22209  * to new_index if it matches the old_index.
22210  *
22211  * Failovers typically happen within a group of ills. But somebody
22212  * can remove an ill from the group after a failover happened. If
22213  * we are setting the ifindex after this, we potentially need to
22214  * look at all the ills rather than just the ones in the group.
22215  * We cut down the work by looking at matching ill_net_types
22216  * and ill_types as we could not possibly grouped them together.
22217  */
22218 static void
22219 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
22220 {
22221 	ill_t *ill;
22222 	ipif_t *ipif;
22223 	uint_t old_ifindex;
22224 	uint_t new_ifindex;
22225 	ilm_t *ilm;
22226 	ill_walk_context_t ctx;
22227 	ip_stack_t	*ipst = ill_orig->ill_ipst;
22228 
22229 	old_ifindex = connc->cc_old_ifindex;
22230 	new_ifindex = connc->cc_new_ifindex;
22231 
22232 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
22233 	ill = ILL_START_WALK_ALL(&ctx, ipst);
22234 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
22235 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
22236 		    (ill_orig->ill_type != ill->ill_type)) {
22237 			continue;
22238 		}
22239 		for (ipif = ill->ill_ipif; ipif != NULL;
22240 		    ipif = ipif->ipif_next) {
22241 			if (ipif->ipif_orig_ifindex == old_ifindex)
22242 				ipif->ipif_orig_ifindex = new_ifindex;
22243 		}
22244 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
22245 			if (ilm->ilm_orig_ifindex == old_ifindex)
22246 				ilm->ilm_orig_ifindex = new_ifindex;
22247 		}
22248 	}
22249 	rw_exit(&ipst->ips_ill_g_lock);
22250 }
22251 
22252 /*
22253  * We first need to ensure that the new index is unique, and
22254  * then carry the change across both v4 and v6 ill representation
22255  * of the physical interface.
22256  */
22257 /* ARGSUSED */
22258 int
22259 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22260     ip_ioctl_cmd_t *ipip, void *ifreq)
22261 {
22262 	ill_t		*ill;
22263 	ill_t		*ill_other;
22264 	phyint_t	*phyi;
22265 	int		old_index;
22266 	conn_change_t	connc;
22267 	struct ifreq	*ifr = (struct ifreq *)ifreq;
22268 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22269 	uint_t	index;
22270 	ill_t	*ill_v4;
22271 	ill_t	*ill_v6;
22272 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22273 
22274 	if (ipip->ipi_cmd_type == IF_CMD)
22275 		index = ifr->ifr_index;
22276 	else
22277 		index = lifr->lifr_index;
22278 
22279 	/*
22280 	 * Only allow on physical interface. Also, index zero is illegal.
22281 	 *
22282 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
22283 	 *
22284 	 * 1) If PHYI_FAILED is set, a failover could have happened which
22285 	 *    implies a possible failback might have to happen. As failback
22286 	 *    depends on the old index, we should fail setting the index.
22287 	 *
22288 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
22289 	 *    any addresses or multicast memberships are failed over to
22290 	 *    a non-STANDBY interface. As failback depends on the old
22291 	 *    index, we should fail setting the index for this case also.
22292 	 *
22293 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
22294 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
22295 	 */
22296 	ill = ipif->ipif_ill;
22297 	phyi = ill->ill_phyint;
22298 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
22299 	    ipif->ipif_id != 0 || index == 0) {
22300 		return (EINVAL);
22301 	}
22302 	old_index = phyi->phyint_ifindex;
22303 
22304 	/* If the index is not changing, no work to do */
22305 	if (old_index == index)
22306 		return (0);
22307 
22308 	/*
22309 	 * Use ill_lookup_on_ifindex to determine if the
22310 	 * new index is unused and if so allow the change.
22311 	 */
22312 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
22313 	    ipst);
22314 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
22315 	    ipst);
22316 	if (ill_v6 != NULL || ill_v4 != NULL) {
22317 		if (ill_v4 != NULL)
22318 			ill_refrele(ill_v4);
22319 		if (ill_v6 != NULL)
22320 			ill_refrele(ill_v6);
22321 		return (EBUSY);
22322 	}
22323 
22324 	/*
22325 	 * The new index is unused. Set it in the phyint.
22326 	 * Locate the other ill so that we can send a routing
22327 	 * sockets message.
22328 	 */
22329 	if (ill->ill_isv6) {
22330 		ill_other = phyi->phyint_illv4;
22331 	} else {
22332 		ill_other = phyi->phyint_illv6;
22333 	}
22334 
22335 	phyi->phyint_ifindex = index;
22336 
22337 	/* Update SCTP's ILL list */
22338 	sctp_ill_reindex(ill, old_index);
22339 
22340 	connc.cc_old_ifindex = old_index;
22341 	connc.cc_new_ifindex = index;
22342 	ip_change_ifindex(ill, &connc);
22343 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
22344 
22345 	/* Send the routing sockets message */
22346 	ip_rts_ifmsg(ipif);
22347 	if (ill_other != NULL)
22348 		ip_rts_ifmsg(ill_other->ill_ipif);
22349 
22350 	return (0);
22351 }
22352 
22353 /* ARGSUSED */
22354 int
22355 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22356     ip_ioctl_cmd_t *ipip, void *ifreq)
22357 {
22358 	struct ifreq	*ifr = (struct ifreq *)ifreq;
22359 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22360 
22361 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
22362 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22363 	/* Get the interface index */
22364 	if (ipip->ipi_cmd_type == IF_CMD) {
22365 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
22366 	} else {
22367 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
22368 	}
22369 	return (0);
22370 }
22371 
22372 /* ARGSUSED */
22373 int
22374 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22375     ip_ioctl_cmd_t *ipip, void *ifreq)
22376 {
22377 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22378 
22379 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
22380 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22381 	/* Get the interface zone */
22382 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22383 	lifr->lifr_zoneid = ipif->ipif_zoneid;
22384 	return (0);
22385 }
22386 
22387 /*
22388  * Set the zoneid of an interface.
22389  */
22390 /* ARGSUSED */
22391 int
22392 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22393     ip_ioctl_cmd_t *ipip, void *ifreq)
22394 {
22395 	struct lifreq	*lifr = (struct lifreq *)ifreq;
22396 	int err = 0;
22397 	boolean_t need_up = B_FALSE;
22398 	zone_t *zptr;
22399 	zone_status_t status;
22400 	zoneid_t zoneid;
22401 
22402 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22403 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
22404 		if (!is_system_labeled())
22405 			return (ENOTSUP);
22406 		zoneid = GLOBAL_ZONEID;
22407 	}
22408 
22409 	/* cannot assign instance zero to a non-global zone */
22410 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
22411 		return (ENOTSUP);
22412 
22413 	/*
22414 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
22415 	 * the event of a race with the zone shutdown processing, since IP
22416 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
22417 	 * interface will be cleaned up even if the zone is shut down
22418 	 * immediately after the status check. If the interface can't be brought
22419 	 * down right away, and the zone is shut down before the restart
22420 	 * function is called, we resolve the possible races by rechecking the
22421 	 * zone status in the restart function.
22422 	 */
22423 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
22424 		return (EINVAL);
22425 	status = zone_status_get(zptr);
22426 	zone_rele(zptr);
22427 
22428 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
22429 		return (EINVAL);
22430 
22431 	if (ipif->ipif_flags & IPIF_UP) {
22432 		/*
22433 		 * If the interface is already marked up,
22434 		 * we call ipif_down which will take care
22435 		 * of ditching any IREs that have been set
22436 		 * up based on the old interface address.
22437 		 */
22438 		err = ipif_logical_down(ipif, q, mp);
22439 		if (err == EINPROGRESS)
22440 			return (err);
22441 		ipif_down_tail(ipif);
22442 		need_up = B_TRUE;
22443 	}
22444 
22445 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
22446 	return (err);
22447 }
22448 
22449 static int
22450 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
22451     queue_t *q, mblk_t *mp, boolean_t need_up)
22452 {
22453 	int	err = 0;
22454 	ip_stack_t	*ipst;
22455 
22456 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
22457 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22458 
22459 	if (CONN_Q(q))
22460 		ipst = CONNQ_TO_IPST(q);
22461 	else
22462 		ipst = ILLQ_TO_IPST(q);
22463 
22464 	/*
22465 	 * For exclusive stacks we don't allow a different zoneid than
22466 	 * global.
22467 	 */
22468 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
22469 	    zoneid != GLOBAL_ZONEID)
22470 		return (EINVAL);
22471 
22472 	/* Set the new zone id. */
22473 	ipif->ipif_zoneid = zoneid;
22474 
22475 	/* Update sctp list */
22476 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22477 
22478 	if (need_up) {
22479 		/*
22480 		 * Now bring the interface back up.  If this
22481 		 * is the only IPIF for the ILL, ipif_up
22482 		 * will have to re-bind to the device, so
22483 		 * we may get back EINPROGRESS, in which
22484 		 * case, this IOCTL will get completed in
22485 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22486 		 */
22487 		err = ipif_up(ipif, q, mp);
22488 	}
22489 	return (err);
22490 }
22491 
22492 /* ARGSUSED */
22493 int
22494 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22495     ip_ioctl_cmd_t *ipip, void *if_req)
22496 {
22497 	struct lifreq *lifr = (struct lifreq *)if_req;
22498 	zoneid_t zoneid;
22499 	zone_t *zptr;
22500 	zone_status_t status;
22501 
22502 	ASSERT(ipif->ipif_id != 0);
22503 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22504 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22505 		zoneid = GLOBAL_ZONEID;
22506 
22507 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22508 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22509 
22510 	/*
22511 	 * We recheck the zone status to resolve the following race condition:
22512 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22513 	 * 2) hme0:1 is up and can't be brought down right away;
22514 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22515 	 * 3) zone "myzone" is halted; the zone status switches to
22516 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22517 	 * the interfaces to remove - hme0:1 is not returned because it's not
22518 	 * yet in "myzone", so it won't be removed;
22519 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22520 	 * status check here, we would have hme0:1 in "myzone" after it's been
22521 	 * destroyed.
22522 	 * Note that if the status check fails, we need to bring the interface
22523 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22524 	 * ipif_up_done[_v6]().
22525 	 */
22526 	status = ZONE_IS_UNINITIALIZED;
22527 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22528 		status = zone_status_get(zptr);
22529 		zone_rele(zptr);
22530 	}
22531 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22532 		if (ipif->ipif_isv6) {
22533 			(void) ipif_up_done_v6(ipif);
22534 		} else {
22535 			(void) ipif_up_done(ipif);
22536 		}
22537 		return (EINVAL);
22538 	}
22539 
22540 	ipif_down_tail(ipif);
22541 
22542 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22543 	    B_TRUE));
22544 }
22545 
22546 /* ARGSUSED */
22547 int
22548 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22549 	ip_ioctl_cmd_t *ipip, void *ifreq)
22550 {
22551 	struct lifreq	*lifr = ifreq;
22552 
22553 	ASSERT(q->q_next == NULL);
22554 	ASSERT(CONN_Q(q));
22555 
22556 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22557 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22558 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22559 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22560 
22561 	return (0);
22562 }
22563 
22564 
22565 /* Find the previous ILL in this usesrc group */
22566 static ill_t *
22567 ill_prev_usesrc(ill_t *uill)
22568 {
22569 	ill_t *ill;
22570 
22571 	for (ill = uill->ill_usesrc_grp_next;
22572 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22573 	    ill = ill->ill_usesrc_grp_next)
22574 		/* do nothing */;
22575 	return (ill);
22576 }
22577 
22578 /*
22579  * Release all members of the usesrc group. This routine is called
22580  * from ill_delete when the interface being unplumbed is the
22581  * group head.
22582  */
22583 static void
22584 ill_disband_usesrc_group(ill_t *uill)
22585 {
22586 	ill_t *next_ill, *tmp_ill;
22587 	ip_stack_t	*ipst = uill->ill_ipst;
22588 
22589 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22590 	next_ill = uill->ill_usesrc_grp_next;
22591 
22592 	do {
22593 		ASSERT(next_ill != NULL);
22594 		tmp_ill = next_ill->ill_usesrc_grp_next;
22595 		ASSERT(tmp_ill != NULL);
22596 		next_ill->ill_usesrc_grp_next = NULL;
22597 		next_ill->ill_usesrc_ifindex = 0;
22598 		next_ill = tmp_ill;
22599 	} while (next_ill->ill_usesrc_ifindex != 0);
22600 	uill->ill_usesrc_grp_next = NULL;
22601 }
22602 
22603 /*
22604  * Remove the client usesrc ILL from the list and relink to a new list
22605  */
22606 int
22607 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22608 {
22609 	ill_t *ill, *tmp_ill;
22610 	ip_stack_t	*ipst = ucill->ill_ipst;
22611 
22612 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22613 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22614 
22615 	/*
22616 	 * Check if the usesrc client ILL passed in is not already
22617 	 * in use as a usesrc ILL i.e one whose source address is
22618 	 * in use OR a usesrc ILL is not already in use as a usesrc
22619 	 * client ILL
22620 	 */
22621 	if ((ucill->ill_usesrc_ifindex == 0) ||
22622 	    (uill->ill_usesrc_ifindex != 0)) {
22623 		return (-1);
22624 	}
22625 
22626 	ill = ill_prev_usesrc(ucill);
22627 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22628 
22629 	/* Remove from the current list */
22630 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22631 		/* Only two elements in the list */
22632 		ASSERT(ill->ill_usesrc_ifindex == 0);
22633 		ill->ill_usesrc_grp_next = NULL;
22634 	} else {
22635 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22636 	}
22637 
22638 	if (ifindex == 0) {
22639 		ucill->ill_usesrc_ifindex = 0;
22640 		ucill->ill_usesrc_grp_next = NULL;
22641 		return (0);
22642 	}
22643 
22644 	ucill->ill_usesrc_ifindex = ifindex;
22645 	tmp_ill = uill->ill_usesrc_grp_next;
22646 	uill->ill_usesrc_grp_next = ucill;
22647 	ucill->ill_usesrc_grp_next =
22648 	    (tmp_ill != NULL) ? tmp_ill : uill;
22649 	return (0);
22650 }
22651 
22652 /*
22653  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22654  * ip.c for locking details.
22655  */
22656 /* ARGSUSED */
22657 int
22658 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22659     ip_ioctl_cmd_t *ipip, void *ifreq)
22660 {
22661 	struct lifreq *lifr = (struct lifreq *)ifreq;
22662 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22663 	    ill_flag_changed = B_FALSE;
22664 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22665 	int err = 0, ret;
22666 	uint_t ifindex;
22667 	phyint_t *us_phyint, *us_cli_phyint;
22668 	ipsq_t *ipsq = NULL;
22669 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22670 
22671 	ASSERT(IAM_WRITER_IPIF(ipif));
22672 	ASSERT(q->q_next == NULL);
22673 	ASSERT(CONN_Q(q));
22674 
22675 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22676 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22677 
22678 	ASSERT(us_cli_phyint != NULL);
22679 
22680 	/*
22681 	 * If the client ILL is being used for IPMP, abort.
22682 	 * Note, this can be done before ipsq_try_enter since we are already
22683 	 * exclusive on this ILL
22684 	 */
22685 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22686 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22687 		return (EINVAL);
22688 	}
22689 
22690 	ifindex = lifr->lifr_index;
22691 	if (ifindex == 0) {
22692 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22693 			/* non usesrc group interface, nothing to reset */
22694 			return (0);
22695 		}
22696 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22697 		/* valid reset request */
22698 		reset_flg = B_TRUE;
22699 	}
22700 
22701 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22702 	    ip_process_ioctl, &err, ipst);
22703 
22704 	if (usesrc_ill == NULL) {
22705 		return (err);
22706 	}
22707 
22708 	/*
22709 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22710 	 * group nor can either of the interfaces be used for standy. So
22711 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22712 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22713 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22714 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22715 	 * the usesrc_cli_ill
22716 	 */
22717 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22718 	    NEW_OP, B_TRUE);
22719 	if (ipsq == NULL) {
22720 		err = EINPROGRESS;
22721 		/* Operation enqueued on the ipsq of the usesrc ILL */
22722 		goto done;
22723 	}
22724 
22725 	/* Check if the usesrc_ill is used for IPMP */
22726 	us_phyint = usesrc_ill->ill_phyint;
22727 	if ((us_phyint->phyint_groupname != NULL) ||
22728 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22729 		err = EINVAL;
22730 		goto done;
22731 	}
22732 
22733 	/*
22734 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22735 	 * already a client then return EINVAL
22736 	 */
22737 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22738 		err = EINVAL;
22739 		goto done;
22740 	}
22741 
22742 	/*
22743 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22744 	 * be then this is a duplicate operation.
22745 	 */
22746 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22747 		err = 0;
22748 		goto done;
22749 	}
22750 
22751 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22752 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22753 	    usesrc_ill->ill_isv6));
22754 
22755 	/*
22756 	 * The next step ensures that no new ires will be created referencing
22757 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22758 	 * we go through an ire walk deleting all ire caches that reference
22759 	 * the client ill. New ires referencing the client ill that are added
22760 	 * to the ire table before the ILL_CHANGING flag is set, will be
22761 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22762 	 * the client ill while the ILL_CHANGING flag is set will be failed
22763 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22764 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22765 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22766 	 * belong to the same usesrc group.
22767 	 */
22768 	mutex_enter(&usesrc_cli_ill->ill_lock);
22769 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22770 	mutex_exit(&usesrc_cli_ill->ill_lock);
22771 	ill_flag_changed = B_TRUE;
22772 
22773 	if (ipif->ipif_isv6)
22774 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22775 		    ALL_ZONES, ipst);
22776 	else
22777 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22778 		    ALL_ZONES, ipst);
22779 
22780 	/*
22781 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22782 	 * and the ill_usesrc_ifindex fields
22783 	 */
22784 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22785 
22786 	if (reset_flg) {
22787 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22788 		if (ret != 0) {
22789 			err = EINVAL;
22790 		}
22791 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22792 		goto done;
22793 	}
22794 
22795 	/*
22796 	 * Four possibilities to consider:
22797 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22798 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22799 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22800 	 * 4. Both are part of their respective usesrc groups
22801 	 */
22802 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22803 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22804 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22805 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22806 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22807 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22808 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22809 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22810 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22811 		/* Insert at head of list */
22812 		usesrc_cli_ill->ill_usesrc_grp_next =
22813 		    usesrc_ill->ill_usesrc_grp_next;
22814 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22815 	} else {
22816 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22817 		    ifindex);
22818 		if (ret != 0)
22819 			err = EINVAL;
22820 	}
22821 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22822 
22823 done:
22824 	if (ill_flag_changed) {
22825 		mutex_enter(&usesrc_cli_ill->ill_lock);
22826 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22827 		mutex_exit(&usesrc_cli_ill->ill_lock);
22828 	}
22829 	if (ipsq != NULL)
22830 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
22831 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22832 	ill_refrele(usesrc_ill);
22833 	return (err);
22834 }
22835 
22836 /*
22837  * comparison function used by avl.
22838  */
22839 static int
22840 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22841 {
22842 
22843 	uint_t index;
22844 
22845 	ASSERT(phyip != NULL && index_ptr != NULL);
22846 
22847 	index = *((uint_t *)index_ptr);
22848 	/*
22849 	 * let the phyint with the lowest index be on top.
22850 	 */
22851 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22852 		return (1);
22853 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22854 		return (-1);
22855 	return (0);
22856 }
22857 
22858 /*
22859  * comparison function used by avl.
22860  */
22861 static int
22862 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22863 {
22864 	ill_t *ill;
22865 	int res = 0;
22866 
22867 	ASSERT(phyip != NULL && name_ptr != NULL);
22868 
22869 	if (((phyint_t *)phyip)->phyint_illv4)
22870 		ill = ((phyint_t *)phyip)->phyint_illv4;
22871 	else
22872 		ill = ((phyint_t *)phyip)->phyint_illv6;
22873 	ASSERT(ill != NULL);
22874 
22875 	res = strcmp(ill->ill_name, (char *)name_ptr);
22876 	if (res > 0)
22877 		return (1);
22878 	else if (res < 0)
22879 		return (-1);
22880 	return (0);
22881 }
22882 /*
22883  * This function is called from ill_delete when the ill is being
22884  * unplumbed. We remove the reference from the phyint and we also
22885  * free the phyint when there are no more references to it.
22886  */
22887 static void
22888 ill_phyint_free(ill_t *ill)
22889 {
22890 	phyint_t *phyi;
22891 	phyint_t *next_phyint;
22892 	ipsq_t *cur_ipsq;
22893 	ip_stack_t	*ipst = ill->ill_ipst;
22894 
22895 	ASSERT(ill->ill_phyint != NULL);
22896 
22897 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22898 	phyi = ill->ill_phyint;
22899 	ill->ill_phyint = NULL;
22900 	/*
22901 	 * ill_init allocates a phyint always to store the copy
22902 	 * of flags relevant to phyint. At that point in time, we could
22903 	 * not assign the name and hence phyint_illv4/v6 could not be
22904 	 * initialized. Later in ipif_set_values, we assign the name to
22905 	 * the ill, at which point in time we assign phyint_illv4/v6.
22906 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22907 	 */
22908 	if (ill->ill_flags & ILLF_IPV6) {
22909 		phyi->phyint_illv6 = NULL;
22910 	} else {
22911 		phyi->phyint_illv4 = NULL;
22912 	}
22913 	/*
22914 	 * ipif_down removes it from the group when the last ipif goes
22915 	 * down.
22916 	 */
22917 	ASSERT(ill->ill_group == NULL);
22918 
22919 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22920 		return;
22921 
22922 	/*
22923 	 * Make sure this phyint was put in the list.
22924 	 */
22925 	if (phyi->phyint_ifindex > 0) {
22926 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22927 		    phyi);
22928 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22929 		    phyi);
22930 	}
22931 	/*
22932 	 * remove phyint from the ipsq list.
22933 	 */
22934 	cur_ipsq = phyi->phyint_ipsq;
22935 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22936 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22937 	} else {
22938 		next_phyint = cur_ipsq->ipsq_phyint_list;
22939 		while (next_phyint != NULL) {
22940 			if (next_phyint->phyint_ipsq_next == phyi) {
22941 				next_phyint->phyint_ipsq_next =
22942 				    phyi->phyint_ipsq_next;
22943 				break;
22944 			}
22945 			next_phyint = next_phyint->phyint_ipsq_next;
22946 		}
22947 		ASSERT(next_phyint != NULL);
22948 	}
22949 	IPSQ_DEC_REF(cur_ipsq, ipst);
22950 
22951 	if (phyi->phyint_groupname_len != 0) {
22952 		ASSERT(phyi->phyint_groupname != NULL);
22953 		mi_free(phyi->phyint_groupname);
22954 	}
22955 	mi_free(phyi);
22956 }
22957 
22958 /*
22959  * Attach the ill to the phyint structure which can be shared by both
22960  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22961  * function is called from ipif_set_values and ill_lookup_on_name (for
22962  * loopback) where we know the name of the ill. We lookup the ill and if
22963  * there is one present already with the name use that phyint. Otherwise
22964  * reuse the one allocated by ill_init.
22965  */
22966 static void
22967 ill_phyint_reinit(ill_t *ill)
22968 {
22969 	boolean_t isv6 = ill->ill_isv6;
22970 	phyint_t *phyi_old;
22971 	phyint_t *phyi;
22972 	avl_index_t where = 0;
22973 	ill_t	*ill_other = NULL;
22974 	ipsq_t	*ipsq;
22975 	ip_stack_t	*ipst = ill->ill_ipst;
22976 
22977 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22978 
22979 	phyi_old = ill->ill_phyint;
22980 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22981 	    phyi_old->phyint_illv6 == NULL));
22982 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22983 	    phyi_old->phyint_illv4 == NULL));
22984 	ASSERT(phyi_old->phyint_ifindex == 0);
22985 
22986 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22987 	    ill->ill_name, &where);
22988 
22989 	/*
22990 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22991 	 *    the global list of ills. So no other thread could have located
22992 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22993 	 * 2. Now locate the other protocol instance of this ill.
22994 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22995 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22996 	 *    of neither ill can change.
22997 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22998 	 *    other ill.
22999 	 * 5. Release all locks.
23000 	 */
23001 
23002 	/*
23003 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
23004 	 * we are initializing IPv4.
23005 	 */
23006 	if (phyi != NULL) {
23007 		ill_other = (isv6) ? phyi->phyint_illv4 :
23008 		    phyi->phyint_illv6;
23009 		ASSERT(ill_other->ill_phyint != NULL);
23010 		ASSERT((isv6 && !ill_other->ill_isv6) ||
23011 		    (!isv6 && ill_other->ill_isv6));
23012 		GRAB_ILL_LOCKS(ill, ill_other);
23013 		/*
23014 		 * We are potentially throwing away phyint_flags which
23015 		 * could be different from the one that we obtain from
23016 		 * ill_other->ill_phyint. But it is okay as we are assuming
23017 		 * that the state maintained within IP is correct.
23018 		 */
23019 		mutex_enter(&phyi->phyint_lock);
23020 		if (isv6) {
23021 			ASSERT(phyi->phyint_illv6 == NULL);
23022 			phyi->phyint_illv6 = ill;
23023 		} else {
23024 			ASSERT(phyi->phyint_illv4 == NULL);
23025 			phyi->phyint_illv4 = ill;
23026 		}
23027 		/*
23028 		 * This is a new ill, currently undergoing SLIFNAME
23029 		 * So we could not have joined an IPMP group until now.
23030 		 */
23031 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
23032 		    phyi_old->phyint_groupname == NULL);
23033 
23034 		/*
23035 		 * This phyi_old is going away. Decref ipsq_refs and
23036 		 * assert it is zero. The ipsq itself will be freed in
23037 		 * ipsq_exit
23038 		 */
23039 		ipsq = phyi_old->phyint_ipsq;
23040 		IPSQ_DEC_REF(ipsq, ipst);
23041 		ASSERT(ipsq->ipsq_refs == 0);
23042 		/* Get the singleton phyint out of the ipsq list */
23043 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
23044 		ipsq->ipsq_phyint_list = NULL;
23045 		phyi_old->phyint_illv4 = NULL;
23046 		phyi_old->phyint_illv6 = NULL;
23047 		mi_free(phyi_old);
23048 	} else {
23049 		mutex_enter(&ill->ill_lock);
23050 		/*
23051 		 * We don't need to acquire any lock, since
23052 		 * the ill is not yet visible globally  and we
23053 		 * have not yet released the ill_g_lock.
23054 		 */
23055 		phyi = phyi_old;
23056 		mutex_enter(&phyi->phyint_lock);
23057 		/* XXX We need a recovery strategy here. */
23058 		if (!phyint_assign_ifindex(phyi, ipst))
23059 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
23060 
23061 		/* No IPMP group yet, thus the hook uses the ifindex */
23062 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
23063 
23064 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23065 		    (void *)phyi, where);
23066 
23067 		(void) avl_find(&ipst->ips_phyint_g_list->
23068 		    phyint_list_avl_by_index,
23069 		    &phyi->phyint_ifindex, &where);
23070 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23071 		    (void *)phyi, where);
23072 	}
23073 
23074 	/*
23075 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
23076 	 * pending mp is not affected because that is per ill basis.
23077 	 */
23078 	ill->ill_phyint = phyi;
23079 
23080 	/*
23081 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
23082 	 * We do this here as when the first ipif was allocated,
23083 	 * ipif_allocate does not know the right interface index.
23084 	 */
23085 
23086 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
23087 	/*
23088 	 * Now that the phyint's ifindex has been assigned, complete the
23089 	 * remaining
23090 	 */
23091 
23092 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
23093 	if (ill->ill_isv6) {
23094 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
23095 		    ill->ill_phyint->phyint_ifindex;
23096 	}
23097 
23098 	/*
23099 	 * Generate an event within the hooks framework to indicate that
23100 	 * a new interface has just been added to IP.  For this event to
23101 	 * be generated, the network interface must, at least, have an
23102 	 * ifindex assigned to it.
23103 	 *
23104 	 * This needs to be run inside the ill_g_lock perimeter to ensure
23105 	 * that the ordering of delivered events to listeners matches the
23106 	 * order of them in the kernel.
23107 	 *
23108 	 * This function could be called from ill_lookup_on_name. In that case
23109 	 * the interface is loopback "lo", which will not generate a NIC event.
23110 	 */
23111 	if (ill->ill_name_length <= 2 ||
23112 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
23113 		/*
23114 		 * Generate nic plumb event for ill_name even if
23115 		 * ipmp_hook_emulation is set. That avoids generating events
23116 		 * for the ill_names should ipmp_hook_emulation be turned on
23117 		 * later.
23118 		 */
23119 		ill_nic_info_plumb(ill, B_FALSE);
23120 	}
23121 	RELEASE_ILL_LOCKS(ill, ill_other);
23122 	mutex_exit(&phyi->phyint_lock);
23123 }
23124 
23125 /*
23126  * Allocate a NE_PLUMB nic info event and store in the ill.
23127  * If 'group' is set we do it for the group name, otherwise the ill name.
23128  * It will be sent when we leave the ipsq.
23129  */
23130 void
23131 ill_nic_info_plumb(ill_t *ill, boolean_t group)
23132 {
23133 	phyint_t	*phyi = ill->ill_phyint;
23134 	ip_stack_t	*ipst = ill->ill_ipst;
23135 	hook_nic_event_t *info;
23136 	char		*name;
23137 	int		namelen;
23138 
23139 	ASSERT(MUTEX_HELD(&ill->ill_lock));
23140 
23141 	if ((info = ill->ill_nic_event_info) != NULL) {
23142 		ip2dbg(("ill_nic_info_plumb: unexpected nic event %d "
23143 		    "attached for %s\n", info->hne_event,
23144 		    ill->ill_name));
23145 		if (info->hne_data != NULL)
23146 			kmem_free(info->hne_data, info->hne_datalen);
23147 		kmem_free(info, sizeof (hook_nic_event_t));
23148 		ill->ill_nic_event_info = NULL;
23149 	}
23150 
23151 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
23152 	if (info == NULL) {
23153 		ip2dbg(("ill_nic_info_plumb: could not attach PLUMB nic "
23154 		    "event information for %s (ENOMEM)\n",
23155 		    ill->ill_name));
23156 		return;
23157 	}
23158 
23159 	if (group) {
23160 		ASSERT(phyi->phyint_groupname_len != 0);
23161 		namelen = phyi->phyint_groupname_len;
23162 		name = phyi->phyint_groupname;
23163 	} else {
23164 		namelen = ill->ill_name_length;
23165 		name = ill->ill_name;
23166 	}
23167 
23168 	info->hne_nic = phyi->phyint_hook_ifindex;
23169 	info->hne_lif = 0;
23170 	info->hne_event = NE_PLUMB;
23171 	info->hne_family = ill->ill_isv6 ?
23172 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
23173 
23174 	info->hne_data = kmem_alloc(namelen, KM_NOSLEEP);
23175 	if (info->hne_data != NULL) {
23176 		info->hne_datalen = namelen;
23177 		bcopy(name, info->hne_data, info->hne_datalen);
23178 	} else {
23179 		ip2dbg(("ill_nic_info_plumb: could not attach "
23180 		    "name information for PLUMB nic event "
23181 		    "of %s (ENOMEM)\n", name));
23182 		kmem_free(info, sizeof (hook_nic_event_t));
23183 		info = NULL;
23184 	}
23185 	ill->ill_nic_event_info = info;
23186 }
23187 
23188 /*
23189  * Unhook the nic event message from the ill and enqueue it
23190  * into the nic event taskq.
23191  */
23192 void
23193 ill_nic_info_dispatch(ill_t *ill)
23194 {
23195 	hook_nic_event_t *info;
23196 
23197 	ASSERT(MUTEX_HELD(&ill->ill_lock));
23198 
23199 	if ((info = ill->ill_nic_event_info) != NULL) {
23200 		if (ddi_taskq_dispatch(eventq_queue_nic,
23201 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
23202 			ip2dbg(("ill_nic_info_dispatch: "
23203 			    "ddi_taskq_dispatch failed\n"));
23204 			if (info->hne_data != NULL)
23205 				kmem_free(info->hne_data, info->hne_datalen);
23206 			kmem_free(info, sizeof (hook_nic_event_t));
23207 		}
23208 		ill->ill_nic_event_info = NULL;
23209 	}
23210 }
23211 
23212 /*
23213  * Notify any downstream modules of the name of this interface.
23214  * An M_IOCTL is used even though we don't expect a successful reply.
23215  * Any reply message from the driver (presumably an M_IOCNAK) will
23216  * eventually get discarded somewhere upstream.  The message format is
23217  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
23218  * to IP.
23219  */
23220 static void
23221 ip_ifname_notify(ill_t *ill, queue_t *q)
23222 {
23223 	mblk_t *mp1, *mp2;
23224 	struct iocblk *iocp;
23225 	struct lifreq *lifr;
23226 
23227 	mp1 = mkiocb(SIOCSLIFNAME);
23228 	if (mp1 == NULL)
23229 		return;
23230 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
23231 	if (mp2 == NULL) {
23232 		freeb(mp1);
23233 		return;
23234 	}
23235 
23236 	mp1->b_cont = mp2;
23237 	iocp = (struct iocblk *)mp1->b_rptr;
23238 	iocp->ioc_count = sizeof (struct lifreq);
23239 
23240 	lifr = (struct lifreq *)mp2->b_rptr;
23241 	mp2->b_wptr += sizeof (struct lifreq);
23242 	bzero(lifr, sizeof (struct lifreq));
23243 
23244 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
23245 	lifr->lifr_ppa = ill->ill_ppa;
23246 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
23247 
23248 	putnext(q, mp1);
23249 }
23250 
23251 static int
23252 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
23253 {
23254 	int err;
23255 	ip_stack_t	*ipst = ill->ill_ipst;
23256 
23257 	/* Set the obsolete NDD per-interface forwarding name. */
23258 	err = ill_set_ndd_name(ill);
23259 	if (err != 0) {
23260 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
23261 		    err);
23262 	}
23263 
23264 	/* Tell downstream modules where they are. */
23265 	ip_ifname_notify(ill, q);
23266 
23267 	/*
23268 	 * ill_dl_phys returns EINPROGRESS in the usual case.
23269 	 * Error cases are ENOMEM ...
23270 	 */
23271 	err = ill_dl_phys(ill, ipif, mp, q);
23272 
23273 	/*
23274 	 * If there is no IRE expiration timer running, get one started.
23275 	 * igmp and mld timers will be triggered by the first multicast
23276 	 */
23277 	if (ipst->ips_ip_ire_expire_id == 0) {
23278 		/*
23279 		 * acquire the lock and check again.
23280 		 */
23281 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
23282 		if (ipst->ips_ip_ire_expire_id == 0) {
23283 			ipst->ips_ip_ire_expire_id = timeout(
23284 			    ip_trash_timer_expire, ipst,
23285 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
23286 		}
23287 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
23288 	}
23289 
23290 	if (ill->ill_isv6) {
23291 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
23292 		if (ipst->ips_mld_slowtimeout_id == 0) {
23293 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
23294 			    (void *)ipst,
23295 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
23296 		}
23297 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
23298 	} else {
23299 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
23300 		if (ipst->ips_igmp_slowtimeout_id == 0) {
23301 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
23302 			    (void *)ipst,
23303 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
23304 		}
23305 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
23306 	}
23307 
23308 	return (err);
23309 }
23310 
23311 /*
23312  * Common routine for ppa and ifname setting. Should be called exclusive.
23313  *
23314  * Returns EINPROGRESS when mp has been consumed by queueing it on
23315  * ill_pending_mp and the ioctl will complete in ip_rput.
23316  *
23317  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
23318  * the new name and new ppa in lifr_name and lifr_ppa respectively.
23319  * For SLIFNAME, we pass these values back to the userland.
23320  */
23321 static int
23322 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
23323 {
23324 	ill_t	*ill;
23325 	ipif_t	*ipif;
23326 	ipsq_t	*ipsq;
23327 	char	*ppa_ptr;
23328 	char	*old_ptr;
23329 	char	old_char;
23330 	int	error;
23331 	ip_stack_t	*ipst;
23332 
23333 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
23334 	ASSERT(q->q_next != NULL);
23335 	ASSERT(interf_name != NULL);
23336 
23337 	ill = (ill_t *)q->q_ptr;
23338 	ipst = ill->ill_ipst;
23339 
23340 	ASSERT(ill->ill_ipst != NULL);
23341 	ASSERT(ill->ill_name[0] == '\0');
23342 	ASSERT(IAM_WRITER_ILL(ill));
23343 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
23344 	ASSERT(ill->ill_ppa == UINT_MAX);
23345 
23346 	/* The ppa is sent down by ifconfig or is chosen */
23347 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
23348 		return (EINVAL);
23349 	}
23350 
23351 	/*
23352 	 * make sure ppa passed in is same as ppa in the name.
23353 	 * This check is not made when ppa == UINT_MAX in that case ppa
23354 	 * in the name could be anything. System will choose a ppa and
23355 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
23356 	 */
23357 	if (*new_ppa_ptr != UINT_MAX) {
23358 		/* stoi changes the pointer */
23359 		old_ptr = ppa_ptr;
23360 		/*
23361 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
23362 		 * (they don't have an externally visible ppa).  We assign one
23363 		 * here so that we can manage the interface.  Note that in
23364 		 * the past this value was always 0 for DLPI 1 drivers.
23365 		 */
23366 		if (*new_ppa_ptr == 0)
23367 			*new_ppa_ptr = stoi(&old_ptr);
23368 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
23369 			return (EINVAL);
23370 	}
23371 	/*
23372 	 * terminate string before ppa
23373 	 * save char at that location.
23374 	 */
23375 	old_char = ppa_ptr[0];
23376 	ppa_ptr[0] = '\0';
23377 
23378 	ill->ill_ppa = *new_ppa_ptr;
23379 	/*
23380 	 * Finish as much work now as possible before calling ill_glist_insert
23381 	 * which makes the ill globally visible and also merges it with the
23382 	 * other protocol instance of this phyint. The remaining work is
23383 	 * done after entering the ipsq which may happen sometime later.
23384 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
23385 	 */
23386 	ipif = ill->ill_ipif;
23387 
23388 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
23389 	ipif_assign_seqid(ipif);
23390 
23391 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
23392 		ill->ill_flags |= ILLF_IPV4;
23393 
23394 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
23395 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
23396 
23397 	if (ill->ill_flags & ILLF_IPV6) {
23398 
23399 		ill->ill_isv6 = B_TRUE;
23400 		if (ill->ill_rq != NULL) {
23401 			ill->ill_rq->q_qinfo = &rinit_ipv6;
23402 			ill->ill_wq->q_qinfo = &winit_ipv6;
23403 		}
23404 
23405 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
23406 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
23407 		ipif->ipif_v6src_addr = ipv6_all_zeros;
23408 		ipif->ipif_v6subnet = ipv6_all_zeros;
23409 		ipif->ipif_v6net_mask = ipv6_all_zeros;
23410 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
23411 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
23412 		/*
23413 		 * point-to-point or Non-mulicast capable
23414 		 * interfaces won't do NUD unless explicitly
23415 		 * configured to do so.
23416 		 */
23417 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
23418 		    !(ill->ill_flags & ILLF_MULTICAST)) {
23419 			ill->ill_flags |= ILLF_NONUD;
23420 		}
23421 		/* Make sure IPv4 specific flag is not set on IPv6 if */
23422 		if (ill->ill_flags & ILLF_NOARP) {
23423 			/*
23424 			 * Note: xresolv interfaces will eventually need
23425 			 * NOARP set here as well, but that will require
23426 			 * those external resolvers to have some
23427 			 * knowledge of that flag and act appropriately.
23428 			 * Not to be changed at present.
23429 			 */
23430 			ill->ill_flags &= ~ILLF_NOARP;
23431 		}
23432 		/*
23433 		 * Set the ILLF_ROUTER flag according to the global
23434 		 * IPv6 forwarding policy.
23435 		 */
23436 		if (ipst->ips_ipv6_forward != 0)
23437 			ill->ill_flags |= ILLF_ROUTER;
23438 	} else if (ill->ill_flags & ILLF_IPV4) {
23439 		ill->ill_isv6 = B_FALSE;
23440 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
23441 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
23442 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
23443 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
23444 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
23445 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
23446 		/*
23447 		 * Set the ILLF_ROUTER flag according to the global
23448 		 * IPv4 forwarding policy.
23449 		 */
23450 		if (ipst->ips_ip_g_forward != 0)
23451 			ill->ill_flags |= ILLF_ROUTER;
23452 	}
23453 
23454 	ASSERT(ill->ill_phyint != NULL);
23455 
23456 	/*
23457 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
23458 	 * be completed in ill_glist_insert -> ill_phyint_reinit
23459 	 */
23460 	if (!ill_allocate_mibs(ill))
23461 		return (ENOMEM);
23462 
23463 	/*
23464 	 * Pick a default sap until we get the DL_INFO_ACK back from
23465 	 * the driver.
23466 	 */
23467 	if (ill->ill_sap == 0) {
23468 		if (ill->ill_isv6)
23469 			ill->ill_sap  = IP6_DL_SAP;
23470 		else
23471 			ill->ill_sap  = IP_DL_SAP;
23472 	}
23473 
23474 	ill->ill_ifname_pending = 1;
23475 	ill->ill_ifname_pending_err = 0;
23476 
23477 	ill_refhold(ill);
23478 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
23479 	if ((error = ill_glist_insert(ill, interf_name,
23480 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
23481 		ill->ill_ppa = UINT_MAX;
23482 		ill->ill_name[0] = '\0';
23483 		/*
23484 		 * undo null termination done above.
23485 		 */
23486 		ppa_ptr[0] = old_char;
23487 		rw_exit(&ipst->ips_ill_g_lock);
23488 		ill_refrele(ill);
23489 		return (error);
23490 	}
23491 
23492 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
23493 
23494 	/*
23495 	 * When we return the buffer pointed to by interf_name should contain
23496 	 * the same name as in ill_name.
23497 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
23498 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
23499 	 * so copy full name and update the ppa ptr.
23500 	 * When ppa passed in != UINT_MAX all values are correct just undo
23501 	 * null termination, this saves a bcopy.
23502 	 */
23503 	if (*new_ppa_ptr == UINT_MAX) {
23504 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23505 		*new_ppa_ptr = ill->ill_ppa;
23506 	} else {
23507 		/*
23508 		 * undo null termination done above.
23509 		 */
23510 		ppa_ptr[0] = old_char;
23511 	}
23512 
23513 	/* Let SCTP know about this ILL */
23514 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23515 
23516 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23517 	    B_TRUE);
23518 
23519 	rw_exit(&ipst->ips_ill_g_lock);
23520 	ill_refrele(ill);
23521 	if (ipsq == NULL)
23522 		return (EINPROGRESS);
23523 
23524 	/*
23525 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23526 	 */
23527 	if (ipsq->ipsq_current_ipif == NULL)
23528 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23529 	else
23530 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23531 
23532 	error = ipif_set_values_tail(ill, ipif, mp, q);
23533 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
23534 	if (error != 0 && error != EINPROGRESS) {
23535 		/*
23536 		 * restore previous values
23537 		 */
23538 		ill->ill_isv6 = B_FALSE;
23539 	}
23540 	return (error);
23541 }
23542 
23543 
23544 void
23545 ipif_init(ip_stack_t *ipst)
23546 {
23547 	hrtime_t hrt;
23548 	int i;
23549 
23550 	/*
23551 	 * Can't call drv_getparm here as it is too early in the boot.
23552 	 * As we use ipif_src_random just for picking a different
23553 	 * source address everytime, this need not be really random.
23554 	 */
23555 	hrt = gethrtime();
23556 	ipst->ips_ipif_src_random =
23557 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23558 
23559 	for (i = 0; i < MAX_G_HEADS; i++) {
23560 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23561 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23562 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23563 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23564 	}
23565 
23566 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23567 	    ill_phyint_compare_index,
23568 	    sizeof (phyint_t),
23569 	    offsetof(struct phyint, phyint_avl_by_index));
23570 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23571 	    ill_phyint_compare_name,
23572 	    sizeof (phyint_t),
23573 	    offsetof(struct phyint, phyint_avl_by_name));
23574 }
23575 
23576 /*
23577  * This is called by ip_rt_add when src_addr value is other than zero.
23578  * src_addr signifies the source address of the incoming packet. For
23579  * reverse tunnel route we need to create a source addr based routing
23580  * table. This routine creates ip_mrtun_table if it's empty and then
23581  * it adds the route entry hashed by source address. It verifies that
23582  * the outgoing interface is always a non-resolver interface (tunnel).
23583  */
23584 int
23585 ip_mrtun_rt_add(ipaddr_t in_src_addr, int flags, ipif_t *ipif_arg,
23586     ipif_t *src_ipif, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func,
23587     ip_stack_t *ipst)
23588 {
23589 	ire_t   *ire;
23590 	ire_t	*save_ire;
23591 	ipif_t  *ipif;
23592 	ill_t   *in_ill = NULL;
23593 	ill_t	*out_ill;
23594 	queue_t	*stq;
23595 	mblk_t	*dlureq_mp;
23596 	int	error;
23597 
23598 	if (ire_arg != NULL)
23599 		*ire_arg = NULL;
23600 	ASSERT(in_src_addr != INADDR_ANY);
23601 
23602 	ipif = ipif_arg;
23603 	if (ipif != NULL) {
23604 		out_ill = ipif->ipif_ill;
23605 	} else {
23606 		ip1dbg(("ip_mrtun_rt_add: ipif is NULL\n"));
23607 		return (EINVAL);
23608 	}
23609 
23610 	if (src_ipif == NULL) {
23611 		ip1dbg(("ip_mrtun_rt_add: src_ipif is NULL\n"));
23612 		return (EINVAL);
23613 	}
23614 	in_ill = src_ipif->ipif_ill;
23615 
23616 	/*
23617 	 * Check for duplicates. We don't need to
23618 	 * match out_ill, because the uniqueness of
23619 	 * a route is only dependent on src_addr and
23620 	 * in_ill.
23621 	 */
23622 	ire = ire_mrtun_lookup(in_src_addr, in_ill);
23623 	if (ire != NULL) {
23624 		ire_refrele(ire);
23625 		return (EEXIST);
23626 	}
23627 	if (ipif->ipif_net_type != IRE_IF_NORESOLVER) {
23628 		ip2dbg(("ip_mrtun_rt_add: outgoing interface is type %d\n",
23629 		    ipif->ipif_net_type));
23630 		return (EINVAL);
23631 	}
23632 
23633 	stq = ipif->ipif_wq;
23634 	ASSERT(stq != NULL);
23635 
23636 	/*
23637 	 * The outgoing interface must be non-resolver
23638 	 * interface.
23639 	 */
23640 	dlureq_mp = ill_dlur_gen(NULL,
23641 	    out_ill->ill_phys_addr_length, out_ill->ill_sap,
23642 	    out_ill->ill_sap_length);
23643 
23644 	if (dlureq_mp == NULL) {
23645 		ip1dbg(("ip_newroute: dlureq_mp NULL\n"));
23646 		return (ENOMEM);
23647 	}
23648 
23649 	/* Create the IRE. */
23650 
23651 	ire = ire_create(
23652 	    NULL,				/* Zero dst addr */
23653 	    NULL,				/* Zero mask */
23654 	    NULL,				/* Zero gateway addr */
23655 	    NULL,				/* Zero ipif_src addr */
23656 	    (uint8_t *)&in_src_addr,		/* in_src-addr */
23657 	    &ipif->ipif_mtu,
23658 	    NULL,
23659 	    NULL,				/* rfq */
23660 	    stq,
23661 	    IRE_MIPRTUN,
23662 	    ipif,
23663 	    in_ill,
23664 	    0,
23665 	    0,
23666 	    0,
23667 	    flags,
23668 	    &ire_uinfo_null,
23669 	    NULL,
23670 	    NULL,
23671 	    ipst);
23672 
23673 	if (ire == NULL) {
23674 		freeb(dlureq_mp);
23675 		return (ENOMEM);
23676 	}
23677 	ip2dbg(("ip_mrtun_rt_add: mrtun route is created with type %d\n",
23678 	    ire->ire_type));
23679 	save_ire = ire;
23680 	ASSERT(save_ire != NULL);
23681 	error = ire_add_mrtun(&ire, q, mp, func);
23682 	/*
23683 	 * If ire_add_mrtun() failed, the ire passed in was freed
23684 	 * so there is no need to do so here.
23685 	 */
23686 	if (error != 0) {
23687 		return (error);
23688 	}
23689 
23690 	/* Duplicate check */
23691 	if (ire != save_ire) {
23692 		/* route already exists by now */
23693 		ire_refrele(ire);
23694 		return (EEXIST);
23695 	}
23696 
23697 	if (ire_arg != NULL) {
23698 		/*
23699 		 * Store the ire that was just added. the caller
23700 		 * ip_rts_request responsible for doing ire_refrele()
23701 		 * on it.
23702 		 */
23703 		*ire_arg = ire;
23704 	} else {
23705 		ire_refrele(ire);	/* held in ire_add_mrtun */
23706 	}
23707 
23708 	return (0);
23709 }
23710 
23711 /*
23712  * It is called by ip_rt_delete() only when mipagent requests to delete
23713  * a reverse tunnel route that was added by ip_mrtun_rt_add() before.
23714  */
23715 
23716 int
23717 ip_mrtun_rt_delete(ipaddr_t in_src_addr, ipif_t *src_ipif)
23718 {
23719 	ire_t   *ire = NULL;
23720 
23721 	if (in_src_addr == INADDR_ANY)
23722 		return (EINVAL);
23723 	if (src_ipif == NULL)
23724 		return (EINVAL);
23725 
23726 	/* search if this route exists in the ip_mrtun_table */
23727 	ire = ire_mrtun_lookup(in_src_addr, src_ipif->ipif_ill);
23728 	if (ire == NULL) {
23729 		ip2dbg(("ip_mrtun_rt_delete: ire not found\n"));
23730 		return (ESRCH);
23731 	}
23732 	ire_delete(ire);
23733 	ire_refrele(ire);
23734 	return (0);
23735 }
23736 
23737 /*
23738  * Lookup the ipif corresponding to the onlink destination address. For
23739  * point-to-point interfaces, it matches with remote endpoint destination
23740  * address. For point-to-multipoint interfaces it only tries to match the
23741  * destination with the interface's subnet address. The longest, most specific
23742  * match is found to take care of such rare network configurations like -
23743  * le0: 129.146.1.1/16
23744  * le1: 129.146.2.2/24
23745  * It is used only by SO_DONTROUTE at the moment.
23746  */
23747 ipif_t *
23748 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23749 {
23750 	ipif_t	*ipif, *best_ipif;
23751 	ill_t	*ill;
23752 	ill_walk_context_t ctx;
23753 
23754 	ASSERT(zoneid != ALL_ZONES);
23755 	best_ipif = NULL;
23756 
23757 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23758 	ill = ILL_START_WALK_V4(&ctx, ipst);
23759 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23760 		mutex_enter(&ill->ill_lock);
23761 		for (ipif = ill->ill_ipif; ipif != NULL;
23762 		    ipif = ipif->ipif_next) {
23763 			if (!IPIF_CAN_LOOKUP(ipif))
23764 				continue;
23765 			if (ipif->ipif_zoneid != zoneid &&
23766 			    ipif->ipif_zoneid != ALL_ZONES)
23767 				continue;
23768 			/*
23769 			 * Point-to-point case. Look for exact match with
23770 			 * destination address.
23771 			 */
23772 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23773 				if (ipif->ipif_pp_dst_addr == addr) {
23774 					ipif_refhold_locked(ipif);
23775 					mutex_exit(&ill->ill_lock);
23776 					rw_exit(&ipst->ips_ill_g_lock);
23777 					if (best_ipif != NULL)
23778 						ipif_refrele(best_ipif);
23779 					return (ipif);
23780 				}
23781 			} else if (ipif->ipif_subnet == (addr &
23782 			    ipif->ipif_net_mask)) {
23783 				/*
23784 				 * Point-to-multipoint case. Looping through to
23785 				 * find the most specific match. If there are
23786 				 * multiple best match ipif's then prefer ipif's
23787 				 * that are UP. If there is only one best match
23788 				 * ipif and it is DOWN we must still return it.
23789 				 */
23790 				if ((best_ipif == NULL) ||
23791 				    (ipif->ipif_net_mask >
23792 				    best_ipif->ipif_net_mask) ||
23793 				    ((ipif->ipif_net_mask ==
23794 				    best_ipif->ipif_net_mask) &&
23795 				    ((ipif->ipif_flags & IPIF_UP) &&
23796 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23797 					ipif_refhold_locked(ipif);
23798 					mutex_exit(&ill->ill_lock);
23799 					rw_exit(&ipst->ips_ill_g_lock);
23800 					if (best_ipif != NULL)
23801 						ipif_refrele(best_ipif);
23802 					best_ipif = ipif;
23803 					rw_enter(&ipst->ips_ill_g_lock,
23804 					    RW_READER);
23805 					mutex_enter(&ill->ill_lock);
23806 				}
23807 			}
23808 		}
23809 		mutex_exit(&ill->ill_lock);
23810 	}
23811 	rw_exit(&ipst->ips_ill_g_lock);
23812 	return (best_ipif);
23813 }
23814 
23815 
23816 /*
23817  * Save enough information so that we can recreate the IRE if
23818  * the interface goes down and then up.
23819  */
23820 static void
23821 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23822 {
23823 	mblk_t	*save_mp;
23824 
23825 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23826 	if (save_mp != NULL) {
23827 		ifrt_t	*ifrt;
23828 
23829 		save_mp->b_wptr += sizeof (ifrt_t);
23830 		ifrt = (ifrt_t *)save_mp->b_rptr;
23831 		bzero(ifrt, sizeof (ifrt_t));
23832 		ifrt->ifrt_type = ire->ire_type;
23833 		ifrt->ifrt_addr = ire->ire_addr;
23834 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23835 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23836 		ifrt->ifrt_mask = ire->ire_mask;
23837 		ifrt->ifrt_flags = ire->ire_flags;
23838 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23839 		mutex_enter(&ipif->ipif_saved_ire_lock);
23840 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23841 		ipif->ipif_saved_ire_mp = save_mp;
23842 		ipif->ipif_saved_ire_cnt++;
23843 		mutex_exit(&ipif->ipif_saved_ire_lock);
23844 	}
23845 }
23846 
23847 
23848 static void
23849 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23850 {
23851 	mblk_t	**mpp;
23852 	mblk_t	*mp;
23853 	ifrt_t	*ifrt;
23854 
23855 	/* Remove from ipif_saved_ire_mp list if it is there */
23856 	mutex_enter(&ipif->ipif_saved_ire_lock);
23857 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23858 	    mpp = &(*mpp)->b_cont) {
23859 		/*
23860 		 * On a given ipif, the triple of address, gateway and
23861 		 * mask is unique for each saved IRE (in the case of
23862 		 * ordinary interface routes, the gateway address is
23863 		 * all-zeroes).
23864 		 */
23865 		mp = *mpp;
23866 		ifrt = (ifrt_t *)mp->b_rptr;
23867 		if (ifrt->ifrt_addr == ire->ire_addr &&
23868 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23869 		    ifrt->ifrt_mask == ire->ire_mask) {
23870 			*mpp = mp->b_cont;
23871 			ipif->ipif_saved_ire_cnt--;
23872 			freeb(mp);
23873 			break;
23874 		}
23875 	}
23876 	mutex_exit(&ipif->ipif_saved_ire_lock);
23877 }
23878 
23879 
23880 /*
23881  * IP multirouting broadcast routes handling
23882  * Append CGTP broadcast IREs to regular ones created
23883  * at ifconfig time.
23884  */
23885 static void
23886 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23887 {
23888 	ire_t *ire_prim;
23889 
23890 	ASSERT(ire != NULL);
23891 	ASSERT(ire_dst != NULL);
23892 
23893 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23894 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23895 	if (ire_prim != NULL) {
23896 		/*
23897 		 * We are in the special case of broadcasts for
23898 		 * CGTP. We add an IRE_BROADCAST that holds
23899 		 * the RTF_MULTIRT flag, the destination
23900 		 * address of ire_dst and the low level
23901 		 * info of ire_prim. In other words, CGTP
23902 		 * broadcast is added to the redundant ipif.
23903 		 */
23904 		ipif_t *ipif_prim;
23905 		ire_t  *bcast_ire;
23906 
23907 		ipif_prim = ire_prim->ire_ipif;
23908 
23909 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23910 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23911 		    (void *)ire_dst, (void *)ire_prim,
23912 		    (void *)ipif_prim));
23913 
23914 		bcast_ire = ire_create(
23915 		    (uchar_t *)&ire->ire_addr,
23916 		    (uchar_t *)&ip_g_all_ones,
23917 		    (uchar_t *)&ire_dst->ire_src_addr,
23918 		    (uchar_t *)&ire->ire_gateway_addr,
23919 		    NULL,
23920 		    &ipif_prim->ipif_mtu,
23921 		    NULL,
23922 		    ipif_prim->ipif_rq,
23923 		    ipif_prim->ipif_wq,
23924 		    IRE_BROADCAST,
23925 		    ipif_prim,
23926 		    NULL,
23927 		    0,
23928 		    0,
23929 		    0,
23930 		    ire->ire_flags,
23931 		    &ire_uinfo_null,
23932 		    NULL,
23933 		    NULL,
23934 		    ipst);
23935 
23936 		if (bcast_ire != NULL) {
23937 
23938 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23939 			    B_FALSE) == 0) {
23940 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23941 				    "added bcast_ire %p\n",
23942 				    (void *)bcast_ire));
23943 
23944 				ipif_save_ire(bcast_ire->ire_ipif,
23945 				    bcast_ire);
23946 				ire_refrele(bcast_ire);
23947 			}
23948 		}
23949 		ire_refrele(ire_prim);
23950 	}
23951 }
23952 
23953 
23954 /*
23955  * IP multirouting broadcast routes handling
23956  * Remove the broadcast ire
23957  */
23958 static void
23959 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23960 {
23961 	ire_t *ire_dst;
23962 
23963 	ASSERT(ire != NULL);
23964 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23965 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23966 	if (ire_dst != NULL) {
23967 		ire_t *ire_prim;
23968 
23969 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23970 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23971 		if (ire_prim != NULL) {
23972 			ipif_t *ipif_prim;
23973 			ire_t  *bcast_ire;
23974 
23975 			ipif_prim = ire_prim->ire_ipif;
23976 
23977 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23978 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23979 			    (void *)ire_dst, (void *)ire_prim,
23980 			    (void *)ipif_prim));
23981 
23982 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23983 			    ire->ire_gateway_addr,
23984 			    IRE_BROADCAST,
23985 			    ipif_prim, ALL_ZONES,
23986 			    NULL,
23987 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23988 			    MATCH_IRE_MASK, ipst);
23989 
23990 			if (bcast_ire != NULL) {
23991 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23992 				    "looked up bcast_ire %p\n",
23993 				    (void *)bcast_ire));
23994 				ipif_remove_ire(bcast_ire->ire_ipif,
23995 				    bcast_ire);
23996 				ire_delete(bcast_ire);
23997 			}
23998 			ire_refrele(ire_prim);
23999 		}
24000 		ire_refrele(ire_dst);
24001 	}
24002 }
24003 
24004 /*
24005  * IPsec hardware acceleration capabilities related functions.
24006  */
24007 
24008 /*
24009  * Free a per-ill IPsec capabilities structure.
24010  */
24011 static void
24012 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
24013 {
24014 	if (capab->auth_hw_algs != NULL)
24015 		kmem_free(capab->auth_hw_algs, capab->algs_size);
24016 	if (capab->encr_hw_algs != NULL)
24017 		kmem_free(capab->encr_hw_algs, capab->algs_size);
24018 	if (capab->encr_algparm != NULL)
24019 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
24020 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
24021 }
24022 
24023 /*
24024  * Allocate a new per-ill IPsec capabilities structure. This structure
24025  * is specific to an IPsec protocol (AH or ESP). It is implemented as
24026  * an array which specifies, for each algorithm, whether this algorithm
24027  * is supported by the ill or not.
24028  */
24029 static ill_ipsec_capab_t *
24030 ill_ipsec_capab_alloc(void)
24031 {
24032 	ill_ipsec_capab_t *capab;
24033 	uint_t nelems;
24034 
24035 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
24036 	if (capab == NULL)
24037 		return (NULL);
24038 
24039 	/* we need one bit per algorithm */
24040 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
24041 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
24042 
24043 	/* allocate memory to store algorithm flags */
24044 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
24045 	if (capab->encr_hw_algs == NULL)
24046 		goto nomem;
24047 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
24048 	if (capab->auth_hw_algs == NULL)
24049 		goto nomem;
24050 	/*
24051 	 * Leave encr_algparm NULL for now since we won't need it half
24052 	 * the time
24053 	 */
24054 	return (capab);
24055 
24056 nomem:
24057 	ill_ipsec_capab_free(capab);
24058 	return (NULL);
24059 }
24060 
24061 /*
24062  * Resize capability array.  Since we're exclusive, this is OK.
24063  */
24064 static boolean_t
24065 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
24066 {
24067 	ipsec_capab_algparm_t *nalp, *oalp;
24068 	uint32_t olen, nlen;
24069 
24070 	oalp = capab->encr_algparm;
24071 	olen = capab->encr_algparm_size;
24072 
24073 	if (oalp != NULL) {
24074 		if (algid < capab->encr_algparm_end)
24075 			return (B_TRUE);
24076 	}
24077 
24078 	nlen = (algid + 1) * sizeof (*nalp);
24079 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
24080 	if (nalp == NULL)
24081 		return (B_FALSE);
24082 
24083 	if (oalp != NULL) {
24084 		bcopy(oalp, nalp, olen);
24085 		kmem_free(oalp, olen);
24086 	}
24087 	capab->encr_algparm = nalp;
24088 	capab->encr_algparm_size = nlen;
24089 	capab->encr_algparm_end = algid + 1;
24090 
24091 	return (B_TRUE);
24092 }
24093 
24094 /*
24095  * Compare the capabilities of the specified ill with the protocol
24096  * and algorithms specified by the SA passed as argument.
24097  * If they match, returns B_TRUE, B_FALSE if they do not match.
24098  *
24099  * The ill can be passed as a pointer to it, or by specifying its index
24100  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
24101  *
24102  * Called by ipsec_out_is_accelerated() do decide whether an outbound
24103  * packet is eligible for hardware acceleration, and by
24104  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
24105  * to a particular ill.
24106  */
24107 boolean_t
24108 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
24109     ipsa_t *sa, netstack_t *ns)
24110 {
24111 	boolean_t sa_isv6;
24112 	uint_t algid;
24113 	struct ill_ipsec_capab_s *cpp;
24114 	boolean_t need_refrele = B_FALSE;
24115 	ip_stack_t	*ipst = ns->netstack_ip;
24116 
24117 	if (ill == NULL) {
24118 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
24119 		    NULL, NULL, NULL, ipst);
24120 		if (ill == NULL) {
24121 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
24122 			return (B_FALSE);
24123 		}
24124 		need_refrele = B_TRUE;
24125 	}
24126 
24127 	/*
24128 	 * Use the address length specified by the SA to determine
24129 	 * if it corresponds to a IPv6 address, and fail the matching
24130 	 * if the isv6 flag passed as argument does not match.
24131 	 * Note: this check is used for SADB capability checking before
24132 	 * sending SA information to an ill.
24133 	 */
24134 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
24135 	if (sa_isv6 != ill_isv6)
24136 		/* protocol mismatch */
24137 		goto done;
24138 
24139 	/*
24140 	 * Check if the ill supports the protocol, algorithm(s) and
24141 	 * key size(s) specified by the SA, and get the pointers to
24142 	 * the algorithms supported by the ill.
24143 	 */
24144 	switch (sa->ipsa_type) {
24145 
24146 	case SADB_SATYPE_ESP:
24147 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
24148 			/* ill does not support ESP acceleration */
24149 			goto done;
24150 		cpp = ill->ill_ipsec_capab_esp;
24151 		algid = sa->ipsa_auth_alg;
24152 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
24153 			goto done;
24154 		algid = sa->ipsa_encr_alg;
24155 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
24156 			goto done;
24157 		if (algid < cpp->encr_algparm_end) {
24158 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
24159 			if (sa->ipsa_encrkeybits < alp->minkeylen)
24160 				goto done;
24161 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
24162 				goto done;
24163 		}
24164 		break;
24165 
24166 	case SADB_SATYPE_AH:
24167 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
24168 			/* ill does not support AH acceleration */
24169 			goto done;
24170 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
24171 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
24172 			goto done;
24173 		break;
24174 	}
24175 
24176 	if (need_refrele)
24177 		ill_refrele(ill);
24178 	return (B_TRUE);
24179 done:
24180 	if (need_refrele)
24181 		ill_refrele(ill);
24182 	return (B_FALSE);
24183 }
24184 
24185 
24186 /*
24187  * Add a new ill to the list of IPsec capable ills.
24188  * Called from ill_capability_ipsec_ack() when an ACK was received
24189  * indicating that IPsec hardware processing was enabled for an ill.
24190  *
24191  * ill must point to the ill for which acceleration was enabled.
24192  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
24193  */
24194 static void
24195 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
24196 {
24197 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
24198 	uint_t sa_type;
24199 	uint_t ipproto;
24200 	ip_stack_t	*ipst = ill->ill_ipst;
24201 
24202 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
24203 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
24204 
24205 	switch (dl_cap) {
24206 	case DL_CAPAB_IPSEC_AH:
24207 		sa_type = SADB_SATYPE_AH;
24208 		ills = &ipst->ips_ipsec_capab_ills_ah;
24209 		ipproto = IPPROTO_AH;
24210 		break;
24211 	case DL_CAPAB_IPSEC_ESP:
24212 		sa_type = SADB_SATYPE_ESP;
24213 		ills = &ipst->ips_ipsec_capab_ills_esp;
24214 		ipproto = IPPROTO_ESP;
24215 		break;
24216 	}
24217 
24218 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
24219 
24220 	/*
24221 	 * Add ill index to list of hardware accelerators. If
24222 	 * already in list, do nothing.
24223 	 */
24224 	for (cur_ill = *ills; cur_ill != NULL &&
24225 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
24226 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
24227 		;
24228 
24229 	if (cur_ill == NULL) {
24230 		/* if this is a new entry for this ill */
24231 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
24232 		if (new_ill == NULL) {
24233 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24234 			return;
24235 		}
24236 
24237 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
24238 		new_ill->ill_isv6 = ill->ill_isv6;
24239 		new_ill->next = *ills;
24240 		*ills = new_ill;
24241 	} else if (!sadb_resync) {
24242 		/* not resync'ing SADB and an entry exists for this ill */
24243 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24244 		return;
24245 	}
24246 
24247 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24248 
24249 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
24250 		/*
24251 		 * IPsec module for protocol loaded, initiate dump
24252 		 * of the SADB to this ill.
24253 		 */
24254 		sadb_ill_download(ill, sa_type);
24255 }
24256 
24257 /*
24258  * Remove an ill from the list of IPsec capable ills.
24259  */
24260 static void
24261 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
24262 {
24263 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
24264 	ip_stack_t	*ipst = ill->ill_ipst;
24265 
24266 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
24267 	    dl_cap == DL_CAPAB_IPSEC_ESP);
24268 
24269 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
24270 	    &ipst->ips_ipsec_capab_ills_esp;
24271 
24272 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
24273 
24274 	prev_ill = NULL;
24275 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
24276 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
24277 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
24278 		;
24279 	if (cur_ill == NULL) {
24280 		/* entry not found */
24281 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24282 		return;
24283 	}
24284 	if (prev_ill == NULL) {
24285 		/* entry at front of list */
24286 		*ills = NULL;
24287 	} else {
24288 		prev_ill->next = cur_ill->next;
24289 	}
24290 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
24291 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24292 }
24293 
24294 /*
24295  * Called by SADB to send a DL_CONTROL_REQ message to every ill
24296  * supporting the specified IPsec protocol acceleration.
24297  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
24298  * We free the mblk and, if sa is non-null, release the held referece.
24299  */
24300 void
24301 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
24302     netstack_t *ns)
24303 {
24304 	ipsec_capab_ill_t *ici, *cur_ici;
24305 	ill_t *ill;
24306 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
24307 	ip_stack_t	*ipst = ns->netstack_ip;
24308 
24309 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
24310 	    ipst->ips_ipsec_capab_ills_esp;
24311 
24312 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
24313 
24314 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
24315 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
24316 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
24317 
24318 		/*
24319 		 * Handle the case where the ill goes away while the SADB is
24320 		 * attempting to send messages.  If it's going away, it's
24321 		 * nuking its shadow SADB, so we don't care..
24322 		 */
24323 
24324 		if (ill == NULL)
24325 			continue;
24326 
24327 		if (sa != NULL) {
24328 			/*
24329 			 * Make sure capabilities match before
24330 			 * sending SA to ill.
24331 			 */
24332 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
24333 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
24334 				ill_refrele(ill);
24335 				continue;
24336 			}
24337 
24338 			mutex_enter(&sa->ipsa_lock);
24339 			sa->ipsa_flags |= IPSA_F_HW;
24340 			mutex_exit(&sa->ipsa_lock);
24341 		}
24342 
24343 		/*
24344 		 * Copy template message, and add it to the front
24345 		 * of the mblk ship list. We want to avoid holding
24346 		 * the ipsec_capab_ills_lock while sending the
24347 		 * message to the ills.
24348 		 *
24349 		 * The b_next and b_prev are temporarily used
24350 		 * to build a list of mblks to be sent down, and to
24351 		 * save the ill to which they must be sent.
24352 		 */
24353 		nmp = copymsg(mp);
24354 		if (nmp == NULL) {
24355 			ill_refrele(ill);
24356 			continue;
24357 		}
24358 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
24359 		nmp->b_next = mp_ship_list;
24360 		mp_ship_list = nmp;
24361 		nmp->b_prev = (mblk_t *)ill;
24362 	}
24363 
24364 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
24365 
24366 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
24367 		/* restore the mblk to a sane state */
24368 		next_mp = nmp->b_next;
24369 		nmp->b_next = NULL;
24370 		ill = (ill_t *)nmp->b_prev;
24371 		nmp->b_prev = NULL;
24372 
24373 		ill_dlpi_send(ill, nmp);
24374 		ill_refrele(ill);
24375 	}
24376 
24377 	if (sa != NULL)
24378 		IPSA_REFRELE(sa);
24379 	freemsg(mp);
24380 }
24381 
24382 /*
24383  * Derive an interface id from the link layer address.
24384  * Knows about IEEE 802 and IEEE EUI-64 mappings.
24385  */
24386 static boolean_t
24387 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
24388 {
24389 	char		*addr;
24390 
24391 	if (phys_length != ETHERADDRL)
24392 		return (B_FALSE);
24393 
24394 	/* Form EUI-64 like address */
24395 	addr = (char *)&v6addr->s6_addr32[2];
24396 	bcopy((char *)phys_addr, addr, 3);
24397 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
24398 	addr[3] = (char)0xff;
24399 	addr[4] = (char)0xfe;
24400 	bcopy((char *)phys_addr + 3, addr + 5, 3);
24401 	return (B_TRUE);
24402 }
24403 
24404 /* ARGSUSED */
24405 static boolean_t
24406 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
24407 {
24408 	return (B_FALSE);
24409 }
24410 
24411 /* ARGSUSED */
24412 static boolean_t
24413 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
24414     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
24415 {
24416 	/*
24417 	 * Multicast address mappings used over Ethernet/802.X.
24418 	 * This address is used as a base for mappings.
24419 	 */
24420 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
24421 	    0x00, 0x00, 0x00};
24422 
24423 	/*
24424 	 * Extract low order 32 bits from IPv6 multicast address.
24425 	 * Or that into the link layer address, starting from the
24426 	 * second byte.
24427 	 */
24428 	*hw_start = 2;
24429 	v6_extract_mask->s6_addr32[0] = 0;
24430 	v6_extract_mask->s6_addr32[1] = 0;
24431 	v6_extract_mask->s6_addr32[2] = 0;
24432 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
24433 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
24434 	return (B_TRUE);
24435 }
24436 
24437 /*
24438  * Indicate by return value whether multicast is supported. If not,
24439  * this code should not touch/change any parameters.
24440  */
24441 /* ARGSUSED */
24442 static boolean_t
24443 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
24444     uint32_t *hw_start, ipaddr_t *extract_mask)
24445 {
24446 	/*
24447 	 * Multicast address mappings used over Ethernet/802.X.
24448 	 * This address is used as a base for mappings.
24449 	 */
24450 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
24451 	    0x00, 0x00, 0x00 };
24452 
24453 	if (phys_length != ETHERADDRL)
24454 		return (B_FALSE);
24455 
24456 	*extract_mask = htonl(0x007fffff);
24457 	*hw_start = 2;
24458 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
24459 	return (B_TRUE);
24460 }
24461 
24462 /*
24463  * Derive IPoIB interface id from the link layer address.
24464  */
24465 static boolean_t
24466 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
24467 {
24468 	char		*addr;
24469 
24470 	if (phys_length != 20)
24471 		return (B_FALSE);
24472 	addr = (char *)&v6addr->s6_addr32[2];
24473 	bcopy(phys_addr + 12, addr, 8);
24474 	/*
24475 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
24476 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
24477 	 * rules. In these cases, the IBA considers these GUIDs to be in
24478 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
24479 	 * required; vendors are required not to assign global EUI-64's
24480 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
24481 	 * of the interface identifier. Whether the GUID is in modified
24482 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
24483 	 * bit set to 1.
24484 	 */
24485 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
24486 	return (B_TRUE);
24487 }
24488 
24489 /*
24490  * Note on mapping from multicast IP addresses to IPoIB multicast link
24491  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
24492  * The format of an IPoIB multicast address is:
24493  *
24494  *  4 byte QPN      Scope Sign.  Pkey
24495  * +--------------------------------------------+
24496  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
24497  * +--------------------------------------------+
24498  *
24499  * The Scope and Pkey components are properties of the IBA port and
24500  * network interface. They can be ascertained from the broadcast address.
24501  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
24502  */
24503 
24504 static boolean_t
24505 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
24506     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
24507 {
24508 	/*
24509 	 * Base IPoIB IPv6 multicast address used for mappings.
24510 	 * Does not contain the IBA scope/Pkey values.
24511 	 */
24512 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
24513 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
24514 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24515 
24516 	/*
24517 	 * Extract low order 80 bits from IPv6 multicast address.
24518 	 * Or that into the link layer address, starting from the
24519 	 * sixth byte.
24520 	 */
24521 	*hw_start = 6;
24522 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
24523 
24524 	/*
24525 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24526 	 */
24527 	*(maddr + 5) = *(bphys_addr + 5);
24528 	*(maddr + 8) = *(bphys_addr + 8);
24529 	*(maddr + 9) = *(bphys_addr + 9);
24530 
24531 	v6_extract_mask->s6_addr32[0] = 0;
24532 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
24533 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
24534 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
24535 	return (B_TRUE);
24536 }
24537 
24538 static boolean_t
24539 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
24540     uint32_t *hw_start, ipaddr_t *extract_mask)
24541 {
24542 	/*
24543 	 * Base IPoIB IPv4 multicast address used for mappings.
24544 	 * Does not contain the IBA scope/Pkey values.
24545 	 */
24546 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
24547 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
24548 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24549 
24550 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
24551 		return (B_FALSE);
24552 
24553 	/*
24554 	 * Extract low order 28 bits from IPv4 multicast address.
24555 	 * Or that into the link layer address, starting from the
24556 	 * sixteenth byte.
24557 	 */
24558 	*extract_mask = htonl(0x0fffffff);
24559 	*hw_start = 16;
24560 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
24561 
24562 	/*
24563 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24564 	 */
24565 	*(maddr + 5) = *(bphys_addr + 5);
24566 	*(maddr + 8) = *(bphys_addr + 8);
24567 	*(maddr + 9) = *(bphys_addr + 9);
24568 	return (B_TRUE);
24569 }
24570 
24571 /*
24572  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
24573  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
24574  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
24575  * the link-local address is preferred.
24576  */
24577 boolean_t
24578 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24579 {
24580 	ipif_t	*ipif;
24581 	ipif_t	*maybe_ipif = NULL;
24582 
24583 	mutex_enter(&ill->ill_lock);
24584 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24585 		mutex_exit(&ill->ill_lock);
24586 		if (ipifp != NULL)
24587 			*ipifp = NULL;
24588 		return (B_FALSE);
24589 	}
24590 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24591 		if (!IPIF_CAN_LOOKUP(ipif))
24592 			continue;
24593 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
24594 		    ipif->ipif_zoneid != ALL_ZONES)
24595 			continue;
24596 		if ((ipif->ipif_flags & flags) != flags)
24597 			continue;
24598 
24599 		if (ipifp == NULL) {
24600 			mutex_exit(&ill->ill_lock);
24601 			ASSERT(maybe_ipif == NULL);
24602 			return (B_TRUE);
24603 		}
24604 		if (!ill->ill_isv6 ||
24605 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
24606 			ipif_refhold_locked(ipif);
24607 			mutex_exit(&ill->ill_lock);
24608 			*ipifp = ipif;
24609 			return (B_TRUE);
24610 		}
24611 		if (maybe_ipif == NULL)
24612 			maybe_ipif = ipif;
24613 	}
24614 	if (ipifp != NULL) {
24615 		if (maybe_ipif != NULL)
24616 			ipif_refhold_locked(maybe_ipif);
24617 		*ipifp = maybe_ipif;
24618 	}
24619 	mutex_exit(&ill->ill_lock);
24620 	return (maybe_ipif != NULL);
24621 }
24622 
24623 /*
24624  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
24625  */
24626 boolean_t
24627 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24628 {
24629 	ill_t *illg;
24630 	ip_stack_t	*ipst = ill->ill_ipst;
24631 
24632 	/*
24633 	 * We look at the passed-in ill first without grabbing ill_g_lock.
24634 	 */
24635 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
24636 		return (B_TRUE);
24637 	}
24638 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
24639 	if (ill->ill_group == NULL) {
24640 		/* ill not in a group */
24641 		rw_exit(&ipst->ips_ill_g_lock);
24642 		return (B_FALSE);
24643 	}
24644 
24645 	/*
24646 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24647 	 * group. We need to look for an ipif in the zone on all the ills in the
24648 	 * group.
24649 	 */
24650 	illg = ill->ill_group->illgrp_ill;
24651 	do {
24652 		/*
24653 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24654 		 * that it's not there.
24655 		 */
24656 		if (illg != ill &&
24657 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24658 			break;
24659 		}
24660 	} while ((illg = illg->ill_group_next) != NULL);
24661 	rw_exit(&ipst->ips_ill_g_lock);
24662 	return (illg != NULL);
24663 }
24664 
24665 /*
24666  * Check if this ill is only being used to send ICMP probes for IPMP
24667  */
24668 boolean_t
24669 ill_is_probeonly(ill_t *ill)
24670 {
24671 	/*
24672 	 * Check if the interface is FAILED, or INACTIVE
24673 	 */
24674 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24675 		return (B_TRUE);
24676 
24677 	return (B_FALSE);
24678 }
24679 
24680 /*
24681  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24682  * If a pointer to an ipif_t is returned then the caller will need to do
24683  * an ill_refrele().
24684  *
24685  * If there is no real interface which matches the ifindex, then it looks
24686  * for a group that has a matching index. In the case of a group match the
24687  * lifidx must be zero. We don't need emulate the logical interfaces
24688  * since IP Filter's use of netinfo doesn't use that.
24689  */
24690 ipif_t *
24691 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24692     ip_stack_t *ipst)
24693 {
24694 	ipif_t *ipif;
24695 	ill_t *ill;
24696 
24697 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24698 	    ipst);
24699 
24700 	if (ill == NULL) {
24701 		/* Fallback to group names only if hook_emulation set */
24702 		if (!ipst->ips_ipmp_hook_emulation)
24703 			return (NULL);
24704 
24705 		if (lifidx != 0)
24706 			return (NULL);
24707 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24708 		if (ill == NULL)
24709 			return (NULL);
24710 	}
24711 
24712 	mutex_enter(&ill->ill_lock);
24713 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24714 		mutex_exit(&ill->ill_lock);
24715 		ill_refrele(ill);
24716 		return (NULL);
24717 	}
24718 
24719 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24720 		if (!IPIF_CAN_LOOKUP(ipif))
24721 			continue;
24722 		if (lifidx == ipif->ipif_id) {
24723 			ipif_refhold_locked(ipif);
24724 			break;
24725 		}
24726 	}
24727 
24728 	mutex_exit(&ill->ill_lock);
24729 	ill_refrele(ill);
24730 	return (ipif);
24731 }
24732 
24733 /*
24734  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24735  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24736  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24737  * for details.
24738  */
24739 void
24740 ill_fastpath_flush(ill_t *ill)
24741 {
24742 	ip_stack_t *ipst = ill->ill_ipst;
24743 
24744 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24745 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24746 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24747 }
24748 
24749 /*
24750  * Set the physical address information for `ill' to the contents of the
24751  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24752  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24753  * EINPROGRESS will be returned.
24754  */
24755 int
24756 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24757 {
24758 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24759 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24760 
24761 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24762 
24763 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24764 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24765 		/* Changing DL_IPV6_TOKEN is not yet supported */
24766 		return (0);
24767 	}
24768 
24769 	/*
24770 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24771 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24772 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24773 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24774 	 */
24775 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24776 		freemsg(mp);
24777 		return (ENOMEM);
24778 	}
24779 
24780 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24781 
24782 	/*
24783 	 * If we can quiesce the ill, then set the address.  If not, then
24784 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24785 	 */
24786 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24787 	mutex_enter(&ill->ill_lock);
24788 	if (!ill_is_quiescent(ill)) {
24789 		/* call cannot fail since `conn_t *' argument is NULL */
24790 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24791 		    mp, ILL_DOWN);
24792 		mutex_exit(&ill->ill_lock);
24793 		return (EINPROGRESS);
24794 	}
24795 	mutex_exit(&ill->ill_lock);
24796 
24797 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24798 	return (0);
24799 }
24800 
24801 /*
24802  * Once the ill associated with `q' has quiesced, set its physical address
24803  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24804  * are passed (linked by b_cont), since we sometimes need to save two distinct
24805  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24806  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24807  * is quiesced, we know any stale IREs with the old address information have
24808  * already been removed, so we don't need to call ill_fastpath_flush().
24809  */
24810 /* ARGSUSED */
24811 static void
24812 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24813 {
24814 	ill_t		*ill = q->q_ptr;
24815 	mblk_t		*addrmp2 = unlinkb(addrmp);
24816 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24817 	uint_t		addrlen, addroff;
24818 
24819 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24820 
24821 	addroff	= dlindp->dl_addr_offset;
24822 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24823 
24824 	switch (dlindp->dl_data) {
24825 	case DL_IPV6_LINK_LAYER_ADDR:
24826 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24827 		freemsg(addrmp2);
24828 		break;
24829 
24830 	case DL_CURR_PHYS_ADDR:
24831 		freemsg(ill->ill_phys_addr_mp);
24832 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24833 		ill->ill_phys_addr_mp = addrmp;
24834 		ill->ill_phys_addr_length = addrlen;
24835 
24836 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24837 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24838 		else
24839 			freemsg(addrmp2);
24840 		break;
24841 	default:
24842 		ASSERT(0);
24843 	}
24844 
24845 	/*
24846 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24847 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24848 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24849 	 * brought up.
24850 	 */
24851 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24852 		ipsq_current_finish(ipsq);
24853 }
24854 
24855 /*
24856  * Helper routine for setting the ill_nd_lla fields.
24857  */
24858 void
24859 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24860 {
24861 	freemsg(ill->ill_nd_lla_mp);
24862 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24863 	ill->ill_nd_lla_mp = ndmp;
24864 	ill->ill_nd_lla_len = addrlen;
24865 }
24866 
24867 major_t IP_MAJ;
24868 #define	IP	"ip"
24869 
24870 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24871 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24872 
24873 /*
24874  * Issue REMOVEIF ioctls to have the loopback interfaces
24875  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24876  * the former going away when the user-level processes in the zone
24877  * are killed  * and the latter are cleaned up by the stream head
24878  * str_stack_shutdown callback that undoes all I_PLINKs.
24879  */
24880 void
24881 ip_loopback_cleanup(ip_stack_t *ipst)
24882 {
24883 	int error;
24884 	ldi_handle_t	lh = NULL;
24885 	ldi_ident_t	li = NULL;
24886 	int		rval;
24887 	cred_t		*cr;
24888 	struct strioctl iocb;
24889 	struct lifreq	lifreq;
24890 
24891 	IP_MAJ = ddi_name_to_major(IP);
24892 
24893 #ifdef NS_DEBUG
24894 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24895 	    ipst->ips_netstack->netstack_stackid);
24896 #endif
24897 
24898 	bzero(&lifreq, sizeof (lifreq));
24899 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24900 
24901 	error = ldi_ident_from_major(IP_MAJ, &li);
24902 	if (error) {
24903 #ifdef DEBUG
24904 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24905 		    error);
24906 #endif
24907 		return;
24908 	}
24909 
24910 	cr = zone_get_kcred(netstackid_to_zoneid(
24911 	    ipst->ips_netstack->netstack_stackid));
24912 	ASSERT(cr != NULL);
24913 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24914 	if (error) {
24915 #ifdef DEBUG
24916 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24917 		    error);
24918 #endif
24919 		goto out;
24920 	}
24921 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24922 	iocb.ic_timout = 15;
24923 	iocb.ic_len = sizeof (lifreq);
24924 	iocb.ic_dp = (char *)&lifreq;
24925 
24926 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24927 	/* LINTED - statement has no consequent */
24928 	if (error) {
24929 #ifdef NS_DEBUG
24930 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24931 		    "UDP6 error %d\n", error);
24932 #endif
24933 	}
24934 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24935 	lh = NULL;
24936 
24937 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24938 	if (error) {
24939 #ifdef NS_DEBUG
24940 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24941 		    error);
24942 #endif
24943 		goto out;
24944 	}
24945 
24946 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24947 	iocb.ic_timout = 15;
24948 	iocb.ic_len = sizeof (lifreq);
24949 	iocb.ic_dp = (char *)&lifreq;
24950 
24951 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24952 	/* LINTED - statement has no consequent */
24953 	if (error) {
24954 #ifdef NS_DEBUG
24955 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24956 		    "UDP error %d\n", error);
24957 #endif
24958 	}
24959 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24960 	lh = NULL;
24961 
24962 out:
24963 	/* Close layered handles */
24964 	if (lh)
24965 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24966 	if (li)
24967 		ldi_ident_release(li);
24968 
24969 	crfree(cr);
24970 }
24971