xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_if.c (revision 5435d801)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /* Copyright (c) 1990 Mentat Inc. */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 /*
31  * This file contains the interface control functions for IP.
32  */
33 
34 #include <sys/types.h>
35 #include <sys/stream.h>
36 #include <sys/dlpi.h>
37 #include <sys/stropts.h>
38 #include <sys/strsun.h>
39 #include <sys/sysmacros.h>
40 #include <sys/strlog.h>
41 #include <sys/ddi.h>
42 #include <sys/sunddi.h>
43 #include <sys/cmn_err.h>
44 #include <sys/kstat.h>
45 #include <sys/debug.h>
46 #include <sys/zone.h>
47 
48 #include <sys/kmem.h>
49 #include <sys/systm.h>
50 #include <sys/param.h>
51 #include <sys/socket.h>
52 #define	_SUN_TPI_VERSION	2
53 #include <sys/tihdr.h>
54 #include <sys/isa_defs.h>
55 #include <net/if.h>
56 #include <net/if_arp.h>
57 #include <net/if_types.h>
58 #include <net/if_dl.h>
59 #include <net/route.h>
60 #include <sys/sockio.h>
61 #include <netinet/in.h>
62 #include <netinet/ip6.h>
63 #include <netinet/icmp6.h>
64 #include <netinet/igmp_var.h>
65 #include <sys/strsun.h>
66 #include <sys/policy.h>
67 #include <sys/ethernet.h>
68 
69 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
70 #include <inet/mi.h>
71 #include <inet/nd.h>
72 #include <inet/arp.h>
73 #include <inet/mib2.h>
74 #include <inet/ip.h>
75 #include <inet/ip6.h>
76 #include <inet/ip6_asp.h>
77 #include <inet/tcp.h>
78 #include <inet/ip_multi.h>
79 #include <inet/ip_ire.h>
80 #include <inet/ip_rts.h>
81 #include <inet/ip_ndp.h>
82 #include <inet/ip_if.h>
83 #include <inet/tun.h>
84 #include <inet/sctp_ip.h>
85 
86 #include <net/pfkeyv2.h>
87 #include <inet/ipsec_info.h>
88 #include <inet/sadb.h>
89 #include <inet/ipsec_impl.h>
90 /* EXPORT DELETE START */
91 #include <sys/iphada.h>
92 /* EXPORT DELETE END */
93 
94 
95 #include <netinet/igmp.h>
96 #include <inet/ip_listutils.h>
97 #include <netinet/ip_mroute.h>
98 #include <inet/ipclassifier.h>
99 #include <sys/mac.h>
100 
101 #include <sys/systeminfo.h>
102 #include <sys/bootconf.h>
103 
104 /* The character which tells where the ill_name ends */
105 #define	IPIF_SEPARATOR_CHAR	':'
106 
107 /* IP ioctl function table entry */
108 typedef struct ipft_s {
109 	int	ipft_cmd;
110 	pfi_t	ipft_pfi;
111 	int	ipft_min_size;
112 	int	ipft_flags;
113 } ipft_t;
114 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
115 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
116 
117 typedef struct ip_sock_ar_s {
118 	union {
119 		area_t	ip_sock_area;
120 		ared_t	ip_sock_ared;
121 		areq_t	ip_sock_areq;
122 	} ip_sock_ar_u;
123 	queue_t	*ip_sock_ar_q;
124 } ip_sock_ar_t;
125 
126 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
127 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
128 		    char *value, caddr_t cp, cred_t *ioc_cr);
129 
130 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
131 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
132 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
133     mblk_t *mp, boolean_t need_up);
134 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
135     mblk_t *mp, boolean_t need_up);
136 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
137     queue_t *q, mblk_t *mp, boolean_t need_up);
138 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
139     mblk_t *mp, boolean_t need_up);
140 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
141     mblk_t *mp);
142 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
143     queue_t *q, mblk_t *mp, boolean_t need_up);
144 static int	ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp,
145     sin_t *sin, boolean_t x_arp_ioctl, boolean_t if_arp_ioctl);
146 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **);
147 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
148 static void	ipsq_flush(ill_t *ill);
149 static void	ipsq_clean_all(ill_t *ill);
150 static void	ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring);
151 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
152     queue_t *q, mblk_t *mp, boolean_t need_up);
153 static void	ipsq_delete(ipsq_t *);
154 
155 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
156 		    boolean_t initialize);
157 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
158 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
159 static void	ipif_delete_cache_ire(ire_t *, char *);
160 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
161 static void	ipif_down_tail(ipif_t *ipif);
162 static void	ipif_free(ipif_t *ipif);
163 static void	ipif_free_tail(ipif_t *ipif);
164 static void	ipif_mask_reply(ipif_t *);
165 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
166 static void	ipif_multicast_down(ipif_t *ipif);
167 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
168 static void	ipif_set_default(ipif_t *ipif);
169 static int	ipif_set_values(queue_t *q, mblk_t *mp,
170     char *interf_name, uint_t *ppa);
171 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
172     queue_t *q);
173 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
174     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
175     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error);
176 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
177 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
178 
179 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
180 static int	ill_arp_off(ill_t *ill);
181 static int	ill_arp_on(ill_t *ill);
182 static void	ill_delete_interface_type(ill_if_t *);
183 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
184 static void	ill_down(ill_t *ill);
185 static void	ill_downi(ire_t *ire, char *ill_arg);
186 static void	ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg);
187 static void	ill_down_tail(ill_t *ill);
188 static void	ill_free_mib(ill_t *ill);
189 static void	ill_glist_delete(ill_t *);
190 static boolean_t ill_has_usable_ipif(ill_t *);
191 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
192 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
193 static void	ill_phyint_free(ill_t *ill);
194 static void	ill_phyint_reinit(ill_t *ill);
195 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
196 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
197 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
198 static void	ill_stq_cache_delete(ire_t *, char *);
199 
200 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
201 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
202 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
203     in6_addr_t *);
204 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
205     ipaddr_t *);
206 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
207 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
208     in6_addr_t *);
209 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
210     ipaddr_t *);
211 
212 static void	ipif_save_ire(ipif_t *, ire_t *);
213 static void	ipif_remove_ire(ipif_t *, ire_t *);
214 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *);
215 static void 	ip_cgtp_bcast_delete(ire_t *);
216 
217 /*
218  * Per-ill IPsec capabilities management.
219  */
220 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
221 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
222 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
223 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
224 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
225 static void ill_capability_proto(ill_t *, int, mblk_t *);
226 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
227     boolean_t);
228 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
229 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
230 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
231 /* EXPORT DELETE START */
232 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
233 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
234 /* EXPORT DELETE END */
235 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
236 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
237 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
238     dl_capability_sub_t *);
239 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
240 
241 static void ill_capability_poll_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
242 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
243 static void ill_capability_poll_reset(ill_t *, mblk_t **);
244 
245 static void	illgrp_cache_delete(ire_t *, char *);
246 static void	illgrp_delete(ill_t *ill);
247 static void	illgrp_reset_schednext(ill_t *ill);
248 
249 static ill_t	*ill_prev_usesrc(ill_t *);
250 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
251 static void	ill_disband_usesrc_group(ill_t *);
252 
253 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
254 
255 /*
256  * if we go over the memory footprint limit more than once in this msec
257  * interval, we'll start pruning aggressively.
258  */
259 int ip_min_frag_prune_time = 0;
260 
261 /*
262  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
263  * and the IPsec DOI
264  */
265 #define	MAX_IPSEC_ALGS	256
266 
267 #define	BITSPERBYTE	8
268 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
269 
270 #define	IPSEC_ALG_ENABLE(algs, algid) \
271 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
272 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
273 
274 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
275 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
276 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
277 
278 typedef uint8_t ipsec_capab_elem_t;
279 
280 /*
281  * Per-algorithm parameters.  Note that at present, only encryption
282  * algorithms have variable keysize (IKE does not provide a way to negotiate
283  * auth algorithm keysize).
284  *
285  * All sizes here are in bits.
286  */
287 typedef struct
288 {
289 	uint16_t	minkeylen;
290 	uint16_t	maxkeylen;
291 } ipsec_capab_algparm_t;
292 
293 /*
294  * Per-ill capabilities.
295  */
296 struct ill_ipsec_capab_s {
297 	ipsec_capab_elem_t *encr_hw_algs;
298 	ipsec_capab_elem_t *auth_hw_algs;
299 	uint32_t algs_size;	/* size of _hw_algs in bytes */
300 	/* algorithm key lengths */
301 	ipsec_capab_algparm_t *encr_algparm;
302 	uint32_t encr_algparm_size;
303 	uint32_t encr_algparm_end;
304 };
305 
306 /*
307  * List of AH and ESP IPsec acceleration capable ills
308  */
309 typedef struct ipsec_capab_ill_s {
310 	uint_t ill_index;
311 	boolean_t ill_isv6;
312 	struct ipsec_capab_ill_s *next;
313 } ipsec_capab_ill_t;
314 
315 static ipsec_capab_ill_t *ipsec_capab_ills_ah;
316 static ipsec_capab_ill_t *ipsec_capab_ills_esp;
317 krwlock_t ipsec_capab_ills_lock;
318 
319 /*
320  * The field values are larger than strictly necessary for simple
321  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
322  */
323 static area_t	ip_area_template = {
324 	AR_ENTRY_ADD,			/* area_cmd */
325 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
326 					/* area_name_offset */
327 	/* area_name_length temporarily holds this structure length */
328 	sizeof (area_t),			/* area_name_length */
329 	IP_ARP_PROTO_TYPE,		/* area_proto */
330 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
331 	IP_ADDR_LEN,			/* area_proto_addr_length */
332 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
333 					/* area_proto_mask_offset */
334 	0,				/* area_flags */
335 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
336 					/* area_hw_addr_offset */
337 	/* Zero length hw_addr_length means 'use your idea of the address' */
338 	0				/* area_hw_addr_length */
339 };
340 
341 /*
342  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
343  * support
344  */
345 static area_t	ip6_area_template = {
346 	AR_ENTRY_ADD,			/* area_cmd */
347 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
348 					/* area_name_offset */
349 	/* area_name_length temporarily holds this structure length */
350 	sizeof (area_t),			/* area_name_length */
351 	IP_ARP_PROTO_TYPE,		/* area_proto */
352 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
353 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
354 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
355 					/* area_proto_mask_offset */
356 	0,				/* area_flags */
357 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
358 					/* area_hw_addr_offset */
359 	/* Zero length hw_addr_length means 'use your idea of the address' */
360 	0				/* area_hw_addr_length */
361 };
362 
363 static ared_t	ip_ared_template = {
364 	AR_ENTRY_DELETE,
365 	sizeof (ared_t) + IP_ADDR_LEN,
366 	sizeof (ared_t),
367 	IP_ARP_PROTO_TYPE,
368 	sizeof (ared_t),
369 	IP_ADDR_LEN
370 };
371 
372 static ared_t	ip6_ared_template = {
373 	AR_ENTRY_DELETE,
374 	sizeof (ared_t) + IPV6_ADDR_LEN,
375 	sizeof (ared_t),
376 	IP_ARP_PROTO_TYPE,
377 	sizeof (ared_t),
378 	IPV6_ADDR_LEN
379 };
380 
381 /*
382  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
383  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
384  * areq is used).
385  */
386 static areq_t	ip_areq_template = {
387 	AR_ENTRY_QUERY,			/* cmd */
388 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
389 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
390 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
391 	sizeof (areq_t),			/* target addr offset */
392 	IP_ADDR_LEN,			/* target addr_length */
393 	0,				/* flags */
394 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
395 	IP_ADDR_LEN,			/* sender addr length */
396 	6,				/* xmit_count */
397 	1000,				/* (re)xmit_interval in milliseconds */
398 	4				/* max # of requests to buffer */
399 	/* anything else filled in by the code */
400 };
401 
402 static arc_t	ip_aru_template = {
403 	AR_INTERFACE_UP,
404 	sizeof (arc_t),		/* Name offset */
405 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
406 };
407 
408 static arc_t	ip_ard_template = {
409 	AR_INTERFACE_DOWN,
410 	sizeof (arc_t),		/* Name offset */
411 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
412 };
413 
414 static arc_t	ip_aron_template = {
415 	AR_INTERFACE_ON,
416 	sizeof (arc_t),		/* Name offset */
417 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
418 };
419 
420 static arc_t	ip_aroff_template = {
421 	AR_INTERFACE_OFF,
422 	sizeof (arc_t),		/* Name offset */
423 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
424 };
425 
426 
427 static arma_t	ip_arma_multi_template = {
428 	AR_MAPPING_ADD,
429 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
430 				/* Name offset */
431 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
432 	IP_ARP_PROTO_TYPE,
433 	sizeof (arma_t),			/* proto_addr_offset */
434 	IP_ADDR_LEN,				/* proto_addr_length */
435 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
436 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
437 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
438 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
439 	IP_MAX_HW_LEN,				/* hw_addr_length */
440 	0,					/* hw_mapping_start */
441 };
442 
443 static ipft_t	ip_ioctl_ftbl[] = {
444 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
445 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
446 		IPFT_F_NO_REPLY },
447 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
448 		IPFT_F_NO_REPLY },
449 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
450 	{ 0 }
451 };
452 
453 /* Simple ICMP IP Header Template */
454 static ipha_t icmp_ipha = {
455 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
456 };
457 
458 /* Flag descriptors for ip_ipif_report */
459 static nv_t	ipif_nv_tbl[] = {
460 	{ IPIF_UP,		"UP" },
461 	{ IPIF_BROADCAST,	"BROADCAST" },
462 	{ ILLF_DEBUG,		"DEBUG" },
463 	{ PHYI_LOOPBACK,	"LOOPBACK" },
464 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
465 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
466 	{ PHYI_RUNNING,		"RUNNING" },
467 	{ ILLF_NOARP,		"NOARP" },
468 	{ PHYI_PROMISC,		"PROMISC" },
469 	{ PHYI_ALLMULTI,	"ALLMULTI" },
470 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
471 	{ ILLF_MULTICAST,	"MULTICAST" },
472 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
473 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
474 	{ IPIF_DHCPRUNNING,	"DHCP" },
475 	{ IPIF_PRIVATE,		"PRIVATE" },
476 	{ IPIF_NOXMIT,		"NOXMIT" },
477 	{ IPIF_NOLOCAL,		"NOLOCAL" },
478 	{ IPIF_DEPRECATED,	"DEPRECATED" },
479 	{ IPIF_PREFERRED,	"PREFERRED" },
480 	{ IPIF_TEMPORARY,	"TEMPORARY" },
481 	{ IPIF_ADDRCONF,	"ADDRCONF" },
482 	{ PHYI_VIRTUAL,		"VIRTUAL" },
483 	{ ILLF_ROUTER,		"ROUTER" },
484 	{ ILLF_NONUD,		"NONUD" },
485 	{ IPIF_ANYCAST,		"ANYCAST" },
486 	{ ILLF_NORTEXCH,	"NORTEXCH" },
487 	{ ILLF_IPV4,		"IPV4" },
488 	{ ILLF_IPV6,		"IPV6" },
489 	{ IPIF_MIPRUNNING,	"MIP" },
490 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
491 	{ PHYI_FAILED,		"FAILED" },
492 	{ PHYI_STANDBY,		"STANDBY" },
493 	{ PHYI_INACTIVE,	"INACTIVE" },
494 	{ PHYI_OFFLINE,		"OFFLINE" },
495 };
496 
497 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
498 
499 static ip_m_t	ip_m_tbl[] = {
500 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
501 	    ip_ether_v6intfid },
502 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
503 	    ip_nodef_v6intfid },
504 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
505 	    ip_nodef_v6intfid },
506 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
507 	    ip_nodef_v6intfid },
508 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
509 	    ip_ether_v6intfid },
510 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
511 	    ip_ib_v6intfid },
512 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
513 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
514 	    ip_nodef_v6intfid }
515 };
516 
517 static ill_t	ill_null;		/* Empty ILL for init. */
518 char	ipif_loopback_name[] = "lo0";
519 static char *ipv4_forward_suffix = ":ip_forwarding";
520 static char *ipv6_forward_suffix = ":ip6_forwarding";
521 static kstat_t *loopback_ksp = NULL;
522 static	sin6_t	sin6_null;	/* Zero address for quick clears */
523 static	sin_t	sin_null;	/* Zero address for quick clears */
524 static	uint_t	ill_index = 1;	/* Used to assign interface indicies */
525 /* When set search for unused index */
526 static boolean_t ill_index_wrap = B_FALSE;
527 /* When set search for unused ipif_seqid */
528 static ipif_t	ipif_zero;
529 uint_t	ipif_src_random;
530 
531 /*
532  * For details on the protection offered by these locks please refer
533  * to the notes under the Synchronization section at the start of ip.c
534  */
535 krwlock_t	ill_g_lock;		/* The global ill_g_lock */
536 kmutex_t	ip_addr_avail_lock;	/* Address availability check lock */
537 ipsq_t		*ipsq_g_head;		/* List of all ipsq's on the system */
538 
539 krwlock_t	ill_g_usesrc_lock;	/* Protects usesrc related fields */
540 
541 /*
542  * illgrp_head/ifgrp_head is protected by IP's perimeter.
543  */
544 static  ill_group_t *illgrp_head_v4;	/* Head of IPv4 ill groups */
545 ill_group_t *illgrp_head_v6;		/* Head of IPv6 ill groups */
546 
547 ill_g_head_t	ill_g_heads[MAX_G_HEADS];   /* ILL List Head */
548 
549 /*
550  * ppa arena is created after these many
551  * interfaces have been plumbed.
552  */
553 uint_t	ill_no_arena = 12;
554 
555 #pragma align CACHE_ALIGN_SIZE(phyint_g_list)
556 static phyint_list_t phyint_g_list;	/* start of phyint list */
557 
558 static uint_t
559 ipif_rand(void)
560 {
561 	ipif_src_random = ipif_src_random * 1103515245 + 12345;
562 	return ((ipif_src_random >> 16) & 0x7fff);
563 }
564 
565 /*
566  * Allocate per-interface mibs. Only used for ipv6.
567  * Returns true if ok. False otherwise.
568  *  ipsq  may not yet be allocated (loopback case ).
569  */
570 static boolean_t
571 ill_allocate_mibs(ill_t *ill)
572 {
573 	ASSERT(ill->ill_isv6);
574 
575 	/* Already allocated? */
576 	if (ill->ill_ip6_mib != NULL) {
577 		ASSERT(ill->ill_icmp6_mib != NULL);
578 		return (B_TRUE);
579 	}
580 
581 	ill->ill_ip6_mib = kmem_zalloc(sizeof (*ill->ill_ip6_mib),
582 	    KM_NOSLEEP);
583 	if (ill->ill_ip6_mib == NULL) {
584 		return (B_FALSE);
585 	}
586 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
587 	    KM_NOSLEEP);
588 	if (ill->ill_icmp6_mib == NULL) {
589 		kmem_free(ill->ill_ip6_mib, sizeof (*ill->ill_ip6_mib));
590 		ill->ill_ip6_mib = NULL;
591 		return (B_FALSE);
592 	}
593 	/*
594 	 * The ipv6Ifindex and ipv6IfIcmpIndex will be assigned later
595 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
596 	 * -> ill_phyint_reinit
597 	 */
598 	return (B_TRUE);
599 }
600 
601 /*
602  * Common code for preparation of ARP commands.  Two points to remember:
603  * 	1) The ill_name is tacked on at the end of the allocated space so
604  *	   the templates name_offset field must contain the total space
605  *	   to allocate less the name length.
606  *
607  *	2) The templates name_length field should contain the *template*
608  *	   length.  We use it as a parameter to bcopy() and then write
609  *	   the real ill_name_length into the name_length field of the copy.
610  * (Always called as writer.)
611  */
612 mblk_t *
613 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
614 {
615 	arc_t	*arc = (arc_t *)template;
616 	char	*cp;
617 	int	len;
618 	mblk_t	*mp;
619 	uint_t	name_length = ill->ill_name_length;
620 	uint_t	template_len = arc->arc_name_length;
621 
622 	len = arc->arc_name_offset + name_length;
623 	mp = allocb(len, BPRI_HI);
624 	if (mp == NULL)
625 		return (NULL);
626 	cp = (char *)mp->b_rptr;
627 	mp->b_wptr = (uchar_t *)&cp[len];
628 	if (template_len)
629 		bcopy(template, cp, template_len);
630 	if (len > template_len)
631 		bzero(&cp[template_len], len - template_len);
632 	mp->b_datap->db_type = M_PROTO;
633 
634 	arc = (arc_t *)cp;
635 	arc->arc_name_length = name_length;
636 	cp = (char *)arc + arc->arc_name_offset;
637 	bcopy(ill->ill_name, cp, name_length);
638 
639 	if (addr) {
640 		area_t	*area = (area_t *)mp->b_rptr;
641 
642 		cp = (char *)area + area->area_proto_addr_offset;
643 		bcopy(addr, cp, area->area_proto_addr_length);
644 		if (area->area_cmd == AR_ENTRY_ADD) {
645 			cp = (char *)area;
646 			len = area->area_proto_addr_length;
647 			if (area->area_proto_mask_offset)
648 				cp += area->area_proto_mask_offset;
649 			else
650 				cp += area->area_proto_addr_offset + len;
651 			while (len-- > 0)
652 				*cp++ = (char)~0;
653 		}
654 	}
655 	return (mp);
656 }
657 
658 /*
659  * Completely vaporize a lower level tap and all associated interfaces.
660  * ill_delete is called only out of ip_close when the device control
661  * stream is being closed.
662  */
663 void
664 ill_delete(ill_t *ill)
665 {
666 	ipif_t	*ipif;
667 	ill_t	*prev_ill;
668 
669 	/*
670 	 * ill_delete may be forcibly entering the ipsq. The previous
671 	 * ioctl may not have completed and may need to be aborted.
672 	 * ipsq_flush takes care of it. If we don't need to enter the
673 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
674 	 * ill_delete_tail is sufficient.
675 	 */
676 	ipsq_flush(ill);
677 
678 	/*
679 	 * Nuke all interfaces.  ipif_free will take down the interface,
680 	 * remove it from the list, and free the data structure.
681 	 * Walk down the ipif list and remove the logical interfaces
682 	 * first before removing the main ipif. We can't unplumb
683 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
684 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
685 	 * POINTOPOINT.
686 	 *
687 	 * If ill_ipif was not properly initialized (i.e low on memory),
688 	 * then no interfaces to clean up. In this case just clean up the
689 	 * ill.
690 	 */
691 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
692 		ipif_free(ipif);
693 
694 	/*
695 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
696 	 * So nobody can be using this mp now. Free the mp allocated for
697 	 * honoring ILLF_NOARP
698 	 */
699 	freemsg(ill->ill_arp_on_mp);
700 	ill->ill_arp_on_mp = NULL;
701 
702 	/* Clean up msgs on pending upcalls for mrouted */
703 	reset_mrt_ill(ill);
704 
705 	/*
706 	 * ipif_free -> reset_conn_ipif will remove all multicast
707 	 * references for IPv4. For IPv6, we need to do it here as
708 	 * it points only at ills.
709 	 */
710 	reset_conn_ill(ill);
711 
712 	/*
713 	 * ill_down will arrange to blow off any IRE's dependent on this
714 	 * ILL, and shut down fragmentation reassembly.
715 	 */
716 	ill_down(ill);
717 
718 	/* Let SCTP know, so that it can remove this from its list. */
719 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
720 
721 	/*
722 	 * If an address on this ILL is being used as a source address then
723 	 * clear out the pointers in other ILLs that point to this ILL.
724 	 */
725 	rw_enter(&ill_g_usesrc_lock, RW_WRITER);
726 	if (ill->ill_usesrc_grp_next != NULL) {
727 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
728 			ill_disband_usesrc_group(ill);
729 		} else {	/* consumer of the usesrc ILL */
730 			prev_ill = ill_prev_usesrc(ill);
731 			prev_ill->ill_usesrc_grp_next =
732 			    ill->ill_usesrc_grp_next;
733 		}
734 	}
735 	rw_exit(&ill_g_usesrc_lock);
736 }
737 
738 /*
739  * ill_delete_tail is called from ip_modclose after all references
740  * to the closing ill are gone. The wait is done in ip_modclose
741  */
742 void
743 ill_delete_tail(ill_t *ill)
744 {
745 	mblk_t	**mpp;
746 	ipif_t	*ipif;
747 
748 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
749 		ipif_down_tail(ipif);
750 
751 	/*
752 	 * Send the detach if there's one to send (i.e., if we're above a
753 	 * style 2 DLPI driver).
754 	 */
755 	if (ill->ill_detach_mp != NULL) {
756 		ill_dlpi_send(ill, ill->ill_detach_mp);
757 		ill->ill_detach_mp = NULL;
758 	}
759 
760 	/*
761 	 * If polling capability is enabled (which signifies direct
762 	 * upcall into IP and driver has ill saved as a handle),
763 	 * we need to make sure that unbind has completed before we
764 	 * let the ill disappear and driver no longer has any reference
765 	 * to this ill.
766 	 */
767 	mutex_enter(&ill->ill_lock);
768 	if (ill->ill_capabilities & ILL_CAPAB_POLL) {
769 		while (!(ill->ill_state_flags & ILL_DL_UNBIND_DONE))
770 			cv_wait(&ill->ill_cv, &ill->ill_lock);
771 	}
772 	mutex_exit(&ill->ill_lock);
773 
774 	if (ill->ill_net_type != IRE_LOOPBACK)
775 		qprocsoff(ill->ill_rq);
776 
777 	/*
778 	 * We do an ipsq_flush once again now. New messages could have
779 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
780 	 * could also have landed up if an ioctl thread had looked up
781 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
782 	 * enqueued the ioctl when we did the ipsq_flush last time.
783 	 */
784 	ipsq_flush(ill);
785 
786 	/*
787 	 * Free capabilities.
788 	 */
789 	if (ill->ill_ipsec_capab_ah != NULL) {
790 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
791 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
792 		ill->ill_ipsec_capab_ah = NULL;
793 	}
794 
795 	if (ill->ill_ipsec_capab_esp != NULL) {
796 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
797 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
798 		ill->ill_ipsec_capab_esp = NULL;
799 	}
800 
801 	if (ill->ill_mdt_capab != NULL) {
802 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
803 		ill->ill_mdt_capab = NULL;
804 	}
805 
806 	if (ill->ill_hcksum_capab != NULL) {
807 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
808 		ill->ill_hcksum_capab = NULL;
809 	}
810 
811 	if (ill->ill_zerocopy_capab != NULL) {
812 		kmem_free(ill->ill_zerocopy_capab,
813 		    sizeof (ill_zerocopy_capab_t));
814 		ill->ill_zerocopy_capab = NULL;
815 	}
816 
817 	/*
818 	 * Clean up polling capabilities
819 	 */
820 	if (ill->ill_capabilities & ILL_CAPAB_POLL)
821 		ipsq_clean_all(ill);
822 
823 	if (ill->ill_poll_capab != NULL) {
824 		CONN_DEC_REF(ill->ill_poll_capab->ill_unbind_conn);
825 		ill->ill_poll_capab->ill_unbind_conn = NULL;
826 		kmem_free(ill->ill_poll_capab,
827 		    sizeof (ill_poll_capab_t) +
828 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
829 		ill->ill_poll_capab = NULL;
830 	}
831 
832 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
833 
834 	while (ill->ill_ipif != NULL)
835 		ipif_free_tail(ill->ill_ipif);
836 
837 	ill_down_tail(ill);
838 
839 	/*
840 	 * We have removed all references to ilm from conn and the ones joined
841 	 * within the kernel.
842 	 *
843 	 * We don't walk conns, mrts and ires because
844 	 *
845 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
846 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
847 	 *    ill references.
848 	 */
849 	ASSERT(ilm_walk_ill(ill) == 0);
850 	/*
851 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
852 	 * could free the phyint. No more reference to the phyint after this
853 	 * point.
854 	 */
855 	(void) ill_glist_delete(ill);
856 
857 	rw_enter(&ip_g_nd_lock, RW_WRITER);
858 	if (ill->ill_ndd_name != NULL)
859 		nd_unload(&ip_g_nd, ill->ill_ndd_name);
860 	rw_exit(&ip_g_nd_lock);
861 
862 
863 	if (ill->ill_frag_ptr != NULL) {
864 		uint_t count;
865 
866 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
867 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
868 		}
869 		mi_free(ill->ill_frag_ptr);
870 		ill->ill_frag_ptr = NULL;
871 		ill->ill_frag_hash_tbl = NULL;
872 	}
873 	if (ill->ill_nd_lla_mp != NULL)
874 		freemsg(ill->ill_nd_lla_mp);
875 	/* Free all retained control messages. */
876 	mpp = &ill->ill_first_mp_to_free;
877 	do {
878 		while (mpp[0]) {
879 			mblk_t  *mp;
880 			mblk_t  *mp1;
881 
882 			mp = mpp[0];
883 			mpp[0] = mp->b_next;
884 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
885 				mp1->b_next = NULL;
886 				mp1->b_prev = NULL;
887 			}
888 			freemsg(mp);
889 		}
890 	} while (mpp++ != &ill->ill_last_mp_to_free);
891 
892 	ill_free_mib(ill);
893 	ILL_TRACE_CLEANUP(ill);
894 }
895 
896 static void
897 ill_free_mib(ill_t *ill)
898 {
899 	if (ill->ill_ip6_mib != NULL) {
900 		kmem_free(ill->ill_ip6_mib, sizeof (*ill->ill_ip6_mib));
901 		ill->ill_ip6_mib = NULL;
902 	}
903 	if (ill->ill_icmp6_mib != NULL) {
904 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
905 		ill->ill_icmp6_mib = NULL;
906 	}
907 }
908 
909 /*
910  * Concatenate together a physical address and a sap.
911  *
912  * Sap_lengths are interpreted as follows:
913  *   sap_length == 0	==>	no sap
914  *   sap_length > 0	==>	sap is at the head of the dlpi address
915  *   sap_length < 0	==>	sap is at the tail of the dlpi address
916  */
917 static void
918 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
919     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
920 {
921 	uint16_t sap_addr = (uint16_t)sap_src;
922 
923 	if (sap_length == 0) {
924 		if (phys_src == NULL)
925 			bzero(dst, phys_length);
926 		else
927 			bcopy(phys_src, dst, phys_length);
928 	} else if (sap_length < 0) {
929 		if (phys_src == NULL)
930 			bzero(dst, phys_length);
931 		else
932 			bcopy(phys_src, dst, phys_length);
933 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
934 	} else {
935 		bcopy(&sap_addr, dst, sizeof (sap_addr));
936 		if (phys_src == NULL)
937 			bzero((char *)dst + sap_length, phys_length);
938 		else
939 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
940 	}
941 }
942 
943 /*
944  * Generate a dl_unitdata_req mblk for the device and address given.
945  * addr_length is the length of the physical portion of the address.
946  * If addr is NULL include an all zero address of the specified length.
947  * TRUE? In any case, addr_length is taken to be the entire length of the
948  * dlpi address, including the absolute value of sap_length.
949  */
950 mblk_t *
951 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
952 		t_scalar_t sap_length)
953 {
954 	dl_unitdata_req_t *dlur;
955 	mblk_t	*mp;
956 	t_scalar_t	abs_sap_length;		/* absolute value */
957 
958 	abs_sap_length = ABS(sap_length);
959 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
960 		DL_UNITDATA_REQ);
961 	if (mp == NULL)
962 		return (NULL);
963 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
964 	/* HACK: accomodate incompatible DLPI drivers */
965 	if (addr_length == 8)
966 		addr_length = 6;
967 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
968 	dlur->dl_dest_addr_offset = sizeof (*dlur);
969 	dlur->dl_priority.dl_min = 0;
970 	dlur->dl_priority.dl_max = 0;
971 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
972 	    (uchar_t *)&dlur[1]);
973 	return (mp);
974 }
975 
976 /*
977  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
978  * Return an error if we already have 1 or more ioctls in progress.
979  * This is used only for non-exclusive ioctls. Currently this is used
980  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
981  * and thus need to use ipsq_pending_mp_add.
982  */
983 boolean_t
984 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
985 {
986 	ASSERT(MUTEX_HELD(&ill->ill_lock));
987 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
988 	/*
989 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
990 	 */
991 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
992 	    (add_mp->b_datap->db_type == M_IOCTL));
993 
994 	ASSERT(MUTEX_HELD(&connp->conn_lock));
995 	/*
996 	 * Return error if the conn has started closing. The conn
997 	 * could have finished cleaning up the pending mp list,
998 	 * If so we should not add another mp to the list negating
999 	 * the cleanup.
1000 	 */
1001 	if (connp->conn_state_flags & CONN_CLOSING)
1002 		return (B_FALSE);
1003 	/*
1004 	 * Add the pending mp to the head of the list, chained by b_next.
1005 	 * Note down the conn on which the ioctl request came, in b_prev.
1006 	 * This will be used to later get the conn, when we get a response
1007 	 * on the ill queue, from some other module (typically arp)
1008 	 */
1009 	add_mp->b_next = (void *)ill->ill_pending_mp;
1010 	add_mp->b_queue = CONNP_TO_WQ(connp);
1011 	ill->ill_pending_mp = add_mp;
1012 	if (connp != NULL)
1013 		connp->conn_oper_pending_ill = ill;
1014 	return (B_TRUE);
1015 }
1016 
1017 /*
1018  * Retrieve the ill_pending_mp and return it. We have to walk the list
1019  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1020  */
1021 mblk_t *
1022 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1023 {
1024 	mblk_t	*prev = NULL;
1025 	mblk_t	*curr = NULL;
1026 	uint_t	id;
1027 	conn_t	*connp;
1028 
1029 	/*
1030 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1031 	 * up the pending mp, but it does not know the ioc_id and
1032 	 * passes in a zero for it.
1033 	 */
1034 	mutex_enter(&ill->ill_lock);
1035 	if (ioc_id != 0)
1036 		*connpp = NULL;
1037 
1038 	/* Search the list for the appropriate ioctl based on ioc_id */
1039 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1040 	    prev = curr, curr = curr->b_next) {
1041 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1042 		connp = Q_TO_CONN(curr->b_queue);
1043 		/* Match based on the ioc_id or based on the conn */
1044 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1045 			break;
1046 	}
1047 
1048 	if (curr != NULL) {
1049 		/* Unlink the mblk from the pending mp list */
1050 		if (prev != NULL) {
1051 			prev->b_next = curr->b_next;
1052 		} else {
1053 			ASSERT(ill->ill_pending_mp == curr);
1054 			ill->ill_pending_mp = curr->b_next;
1055 		}
1056 
1057 		/*
1058 		 * conn refcnt must have been bumped up at the start of
1059 		 * the ioctl. So we can safely access the conn.
1060 		 */
1061 		ASSERT(CONN_Q(curr->b_queue));
1062 		*connpp = Q_TO_CONN(curr->b_queue);
1063 		curr->b_next = NULL;
1064 		curr->b_queue = NULL;
1065 	}
1066 
1067 	mutex_exit(&ill->ill_lock);
1068 
1069 	return (curr);
1070 }
1071 
1072 /*
1073  * Add the pending mp to the list. There can be only 1 pending mp
1074  * in the list. Any exclusive ioctl that needs to wait for a response
1075  * from another module or driver needs to use this function to set
1076  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1077  * the other module/driver. This is also used while waiting for the
1078  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1079  */
1080 boolean_t
1081 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1082     int waitfor)
1083 {
1084 	ipsq_t	*ipsq;
1085 
1086 	ASSERT(IAM_WRITER_IPIF(ipif));
1087 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1088 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1089 	/*
1090 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1091 	 * M_ERROR/M_HANGUP from driver
1092 	 */
1093 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1094 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP));
1095 
1096 	ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1097 	if (connp != NULL) {
1098 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1099 		/*
1100 		 * Return error if the conn has started closing. The conn
1101 		 * could have finished cleaning up the pending mp list,
1102 		 * If so we should not add another mp to the list negating
1103 		 * the cleanup.
1104 		 */
1105 		if (connp->conn_state_flags & CONN_CLOSING)
1106 			return (B_FALSE);
1107 	}
1108 	mutex_enter(&ipsq->ipsq_lock);
1109 	ipsq->ipsq_pending_ipif = ipif;
1110 	/*
1111 	 * Note down the queue in b_queue. This will be returned by
1112 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1113 	 * the processing
1114 	 */
1115 	add_mp->b_next = NULL;
1116 	add_mp->b_queue = q;
1117 	ipsq->ipsq_pending_mp = add_mp;
1118 	ipsq->ipsq_waitfor = waitfor;
1119 	/*
1120 	 * ipsq_current_ipif is needed to restart the operation from
1121 	 * ipif_ill_refrele_tail when the last reference to the ipi/ill
1122 	 * is gone. Since this is not an ioctl ipsq_current_ipif has not
1123 	 * been set until now.
1124 	 */
1125 	if (DB_TYPE(add_mp) == M_ERROR || DB_TYPE(add_mp) == M_HANGUP) {
1126 		ASSERT(ipsq->ipsq_current_ipif == NULL);
1127 		ipsq->ipsq_current_ipif = ipif;
1128 		ipsq->ipsq_last_cmd = DB_TYPE(add_mp);
1129 	}
1130 	if (connp != NULL)
1131 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1132 	mutex_exit(&ipsq->ipsq_lock);
1133 	return (B_TRUE);
1134 }
1135 
1136 /*
1137  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1138  * queued in the list.
1139  */
1140 mblk_t *
1141 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1142 {
1143 	mblk_t	*curr = NULL;
1144 
1145 	mutex_enter(&ipsq->ipsq_lock);
1146 	*connpp = NULL;
1147 	if (ipsq->ipsq_pending_mp == NULL) {
1148 		mutex_exit(&ipsq->ipsq_lock);
1149 		return (NULL);
1150 	}
1151 
1152 	/* There can be only 1 such excl message */
1153 	curr = ipsq->ipsq_pending_mp;
1154 	ASSERT(curr != NULL && curr->b_next == NULL);
1155 	ipsq->ipsq_pending_ipif = NULL;
1156 	ipsq->ipsq_pending_mp = NULL;
1157 	ipsq->ipsq_waitfor = 0;
1158 	mutex_exit(&ipsq->ipsq_lock);
1159 
1160 	if (CONN_Q(curr->b_queue)) {
1161 		/*
1162 		 * This mp did a refhold on the conn, at the start of the ioctl.
1163 		 * So we can safely return a pointer to the conn to the caller.
1164 		 */
1165 		*connpp = Q_TO_CONN(curr->b_queue);
1166 	} else {
1167 		*connpp = NULL;
1168 	}
1169 	curr->b_next = NULL;
1170 	curr->b_prev = NULL;
1171 	return (curr);
1172 }
1173 
1174 /*
1175  * Cleanup the ioctl mp queued in ipsq_pending_mp
1176  * - Called in the ill_delete path
1177  * - Called in the M_ERROR or M_HANGUP path on the ill.
1178  * - Called in the conn close path.
1179  */
1180 boolean_t
1181 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1182 {
1183 	mblk_t	*mp;
1184 	ipsq_t	*ipsq;
1185 	queue_t	*q;
1186 	ipif_t	*ipif;
1187 
1188 	ASSERT(IAM_WRITER_ILL(ill));
1189 	ipsq = ill->ill_phyint->phyint_ipsq;
1190 	mutex_enter(&ipsq->ipsq_lock);
1191 	/*
1192 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1193 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1194 	 * even if it is meant for another ill, since we have to enqueue
1195 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1196 	 * If connp is non-null we are called from the conn close path.
1197 	 */
1198 	mp = ipsq->ipsq_pending_mp;
1199 	if (mp == NULL || (connp != NULL &&
1200 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1201 		mutex_exit(&ipsq->ipsq_lock);
1202 		return (B_FALSE);
1203 	}
1204 	/* Now remove from the ipsq_pending_mp */
1205 	ipsq->ipsq_pending_mp = NULL;
1206 	q = mp->b_queue;
1207 	mp->b_next = NULL;
1208 	mp->b_prev = NULL;
1209 	mp->b_queue = NULL;
1210 
1211 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1212 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1213 	if (ill->ill_move_in_progress) {
1214 		ILL_CLEAR_MOVE(ill);
1215 	} else if (ill->ill_up_ipifs) {
1216 		ill_group_cleanup(ill);
1217 	}
1218 
1219 	ipif = ipsq->ipsq_pending_ipif;
1220 	ipsq->ipsq_pending_ipif = NULL;
1221 	ipsq->ipsq_waitfor = 0;
1222 	ipsq->ipsq_current_ipif = NULL;
1223 	mutex_exit(&ipsq->ipsq_lock);
1224 
1225 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1226 		ip_ioctl_finish(q, mp, ENXIO, connp != NULL ? CONN_CLOSE :
1227 		    NO_COPYOUT, connp != NULL ? ipif : NULL, NULL);
1228 	} else {
1229 		/*
1230 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1231 		 * be just ip_ioctl_freemsg. we have to restart it
1232 		 * otherwise the thread will be stuck.
1233 		 */
1234 		ip_ioctl_freemsg(mp);
1235 	}
1236 	return (B_TRUE);
1237 }
1238 
1239 /*
1240  * The ill is closing. Cleanup all the pending mps. Called exclusively
1241  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1242  * knows this ill, and hence nobody can add an mp to this list
1243  */
1244 static void
1245 ill_pending_mp_cleanup(ill_t *ill)
1246 {
1247 	mblk_t	*mp;
1248 	queue_t	*q;
1249 
1250 	ASSERT(IAM_WRITER_ILL(ill));
1251 
1252 	mutex_enter(&ill->ill_lock);
1253 	/*
1254 	 * Every mp on the pending mp list originating from an ioctl
1255 	 * added 1 to the conn refcnt, at the start of the ioctl.
1256 	 * So bump it down now.  See comments in ip_wput_nondata()
1257 	 */
1258 	while (ill->ill_pending_mp != NULL) {
1259 		mp = ill->ill_pending_mp;
1260 		ill->ill_pending_mp = mp->b_next;
1261 		mutex_exit(&ill->ill_lock);
1262 
1263 		q = mp->b_queue;
1264 		ASSERT(CONN_Q(q));
1265 		mp->b_next = NULL;
1266 		mp->b_prev = NULL;
1267 		mp->b_queue = NULL;
1268 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL, NULL);
1269 		mutex_enter(&ill->ill_lock);
1270 	}
1271 	ill->ill_pending_ipif = NULL;
1272 
1273 	mutex_exit(&ill->ill_lock);
1274 }
1275 
1276 /*
1277  * Called in the conn close path and ill delete path
1278  */
1279 static void
1280 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1281 {
1282 	ipsq_t	*ipsq;
1283 	mblk_t	*prev;
1284 	mblk_t	*curr;
1285 	mblk_t	*next;
1286 	queue_t	*q;
1287 	mblk_t	*tmp_list = NULL;
1288 
1289 	ASSERT(IAM_WRITER_ILL(ill));
1290 	if (connp != NULL)
1291 		q = CONNP_TO_WQ(connp);
1292 	else
1293 		q = ill->ill_wq;
1294 
1295 	ipsq = ill->ill_phyint->phyint_ipsq;
1296 	/*
1297 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1298 	 * In the case of ioctl from a conn, there can be only 1 mp
1299 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1300 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1301 	 * ioctls meant for this ill form conn's are not flushed. They will
1302 	 * be processed during ipsq_exit and will not find the ill and will
1303 	 * return error.
1304 	 */
1305 	mutex_enter(&ipsq->ipsq_lock);
1306 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1307 	    curr = next) {
1308 		next = curr->b_next;
1309 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1310 			/* Unlink the mblk from the pending mp list */
1311 			if (prev != NULL) {
1312 				prev->b_next = curr->b_next;
1313 			} else {
1314 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1315 				ipsq->ipsq_xopq_mphead = curr->b_next;
1316 			}
1317 			if (ipsq->ipsq_xopq_mptail == curr)
1318 				ipsq->ipsq_xopq_mptail = prev;
1319 			/*
1320 			 * Create a temporary list and release the ipsq lock
1321 			 * New elements are added to the head of the tmp_list
1322 			 */
1323 			curr->b_next = tmp_list;
1324 			tmp_list = curr;
1325 		} else {
1326 			prev = curr;
1327 		}
1328 	}
1329 	mutex_exit(&ipsq->ipsq_lock);
1330 
1331 	while (tmp_list != NULL) {
1332 		curr = tmp_list;
1333 		tmp_list = curr->b_next;
1334 		curr->b_next = NULL;
1335 		curr->b_prev = NULL;
1336 		curr->b_queue = NULL;
1337 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1338 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1339 			    CONN_CLOSE : NO_COPYOUT, NULL, NULL);
1340 		} else {
1341 			/*
1342 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1343 			 * this can't be just ip_ioctl_freemsg. we have to
1344 			 * restart it otherwise the thread will be stuck.
1345 			 */
1346 			ip_ioctl_freemsg(curr);
1347 		}
1348 	}
1349 }
1350 
1351 /*
1352  * This conn has started closing. Cleanup any pending ioctl from this conn.
1353  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1354  */
1355 void
1356 conn_ioctl_cleanup(conn_t *connp)
1357 {
1358 	mblk_t *curr;
1359 	ipsq_t	*ipsq;
1360 	ill_t	*ill;
1361 	boolean_t refheld;
1362 
1363 	/*
1364 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1365 	 * ioctl has not yet started, the mp is pending in the list headed by
1366 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1367 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1368 	 * is currently executing now the mp is not queued anywhere but
1369 	 * conn_oper_pending_ill is null. The conn close will wait
1370 	 * till the conn_ref drops to zero.
1371 	 */
1372 	mutex_enter(&connp->conn_lock);
1373 	ill = connp->conn_oper_pending_ill;
1374 	if (ill == NULL) {
1375 		mutex_exit(&connp->conn_lock);
1376 		return;
1377 	}
1378 
1379 	curr = ill_pending_mp_get(ill, &connp, 0);
1380 	if (curr != NULL) {
1381 		mutex_exit(&connp->conn_lock);
1382 		CONN_DEC_REF(connp);
1383 		ip_ioctl_freemsg(curr);
1384 		return;
1385 	}
1386 	/*
1387 	 * We may not be able to refhold the ill if the ill/ipif
1388 	 * is changing. But we need to make sure that the ill will
1389 	 * not vanish. So we just bump up the ill_waiter count.
1390 	 */
1391 	refheld = ill_waiter_inc(ill);
1392 	mutex_exit(&connp->conn_lock);
1393 	if (refheld) {
1394 		if (ipsq_enter(ill, B_TRUE)) {
1395 			ill_waiter_dcr(ill);
1396 			/*
1397 			 * Check whether this ioctl has started and is
1398 			 * pending now in ipsq_pending_mp. If it is not
1399 			 * found there then check whether this ioctl has
1400 			 * not even started and is in the ipsq_xopq list.
1401 			 */
1402 			if (!ipsq_pending_mp_cleanup(ill, connp))
1403 				ipsq_xopq_mp_cleanup(ill, connp);
1404 			ipsq = ill->ill_phyint->phyint_ipsq;
1405 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1406 			return;
1407 		}
1408 	}
1409 
1410 	/*
1411 	 * The ill is also closing and we could not bump up the
1412 	 * ill_waiter_count or we could not enter the ipsq. Leave
1413 	 * the cleanup to ill_delete
1414 	 */
1415 	mutex_enter(&connp->conn_lock);
1416 	while (connp->conn_oper_pending_ill != NULL)
1417 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1418 	mutex_exit(&connp->conn_lock);
1419 	if (refheld)
1420 		ill_waiter_dcr(ill);
1421 }
1422 
1423 /*
1424  * ipcl_walk function for cleaning up conn_*_ill fields.
1425  */
1426 static void
1427 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1428 {
1429 	ill_t	*ill = (ill_t *)arg;
1430 	ire_t	*ire;
1431 
1432 	mutex_enter(&connp->conn_lock);
1433 	if (connp->conn_multicast_ill == ill) {
1434 		/* Revert to late binding */
1435 		connp->conn_multicast_ill = NULL;
1436 		connp->conn_orig_multicast_ifindex = 0;
1437 	}
1438 	if (connp->conn_incoming_ill == ill)
1439 		connp->conn_incoming_ill = NULL;
1440 	if (connp->conn_outgoing_ill == ill)
1441 		connp->conn_outgoing_ill = NULL;
1442 	if (connp->conn_outgoing_pill == ill)
1443 		connp->conn_outgoing_pill = NULL;
1444 	if (connp->conn_nofailover_ill == ill)
1445 		connp->conn_nofailover_ill = NULL;
1446 	if (connp->conn_xmit_if_ill == ill)
1447 		connp->conn_xmit_if_ill = NULL;
1448 	if (connp->conn_ire_cache != NULL) {
1449 		ire = connp->conn_ire_cache;
1450 		/*
1451 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1452 		 * interface X and ipif coming from interface Y, if interface
1453 		 * X and Y are part of the same IPMPgroup. Thus whenever
1454 		 * interface X goes down, remove all references to it by
1455 		 * checking both on ire_ipif and ire_stq.
1456 		 */
1457 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1458 		    (ire->ire_type == IRE_CACHE &&
1459 		    ire->ire_stq == ill->ill_wq)) {
1460 			connp->conn_ire_cache = NULL;
1461 			mutex_exit(&connp->conn_lock);
1462 			ire_refrele_notr(ire);
1463 			return;
1464 		}
1465 	}
1466 	mutex_exit(&connp->conn_lock);
1467 
1468 }
1469 
1470 /* ARGSUSED */
1471 void
1472 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1473 {
1474 	ill_t	*ill = q->q_ptr;
1475 	ipif_t	*ipif;
1476 
1477 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1478 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1479 		ipif_down_tail(ipif);
1480 	ill_down_tail(ill);
1481 	freemsg(mp);
1482 	ipsq->ipsq_current_ipif = NULL;
1483 }
1484 
1485 /*
1486  * ill_down_start is called when we want to down this ill and bring it up again
1487  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1488  * all interfaces, but don't tear down any plumbing.
1489  */
1490 boolean_t
1491 ill_down_start(queue_t *q, mblk_t *mp)
1492 {
1493 	ill_t	*ill;
1494 	ipif_t	*ipif;
1495 
1496 	ill = q->q_ptr;
1497 
1498 	ASSERT(IAM_WRITER_ILL(ill));
1499 
1500 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1501 		(void) ipif_down(ipif, NULL, NULL);
1502 
1503 	ill_down(ill);
1504 
1505 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1506 	mutex_enter(&ill->ill_lock);
1507 	/*
1508 	 * Atomically test and add the pending mp if references are
1509 	 * still active.
1510 	 */
1511 	if (!ill_is_quiescent(ill)) {
1512 		/*
1513 		 * Get rid of any pending mps and cleanup. Call will
1514 		 * not fail since we are passing a null connp.
1515 		 */
1516 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1517 		    mp, ILL_DOWN);
1518 		mutex_exit(&ill->ill_lock);
1519 		return (B_FALSE);
1520 	}
1521 	mutex_exit(&ill->ill_lock);
1522 	return (B_TRUE);
1523 }
1524 
1525 static void
1526 ill_down(ill_t *ill)
1527 {
1528 	/* Blow off any IREs dependent on this ILL. */
1529 	ire_walk(ill_downi, (char *)ill);
1530 
1531 	mutex_enter(&ire_mrtun_lock);
1532 	if (ire_mrtun_count != 0) {
1533 		mutex_exit(&ire_mrtun_lock);
1534 		ire_walk_ill_mrtun(0, 0, ill_downi_mrtun_srcif,
1535 		    (char *)ill, NULL);
1536 	} else {
1537 		mutex_exit(&ire_mrtun_lock);
1538 	}
1539 
1540 	/*
1541 	 * If any interface based forwarding table exists
1542 	 * Blow off the ires there dependent on this ill
1543 	 */
1544 	mutex_enter(&ire_srcif_table_lock);
1545 	if (ire_srcif_table_count > 0) {
1546 		mutex_exit(&ire_srcif_table_lock);
1547 		ire_walk_srcif_table_v4(ill_downi_mrtun_srcif, (char *)ill);
1548 	} else {
1549 		mutex_exit(&ire_srcif_table_lock);
1550 	}
1551 
1552 	/* Remove any conn_*_ill depending on this ill */
1553 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill);
1554 
1555 	if (ill->ill_group != NULL) {
1556 		illgrp_delete(ill);
1557 	}
1558 
1559 }
1560 
1561 static void
1562 ill_down_tail(ill_t *ill)
1563 {
1564 	int	i;
1565 
1566 	/* Destroy ill_srcif_table if it exists */
1567 	/* Lock not reqd really because nobody should be able to access */
1568 	mutex_enter(&ill->ill_lock);
1569 	if (ill->ill_srcif_table != NULL) {
1570 		ill->ill_srcif_refcnt = 0;
1571 		for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
1572 			rw_destroy(&ill->ill_srcif_table[i].irb_lock);
1573 		}
1574 		kmem_free(ill->ill_srcif_table,
1575 		    IP_SRCIF_TABLE_SIZE * sizeof (irb_t));
1576 		ill->ill_srcif_table = NULL;
1577 		ill->ill_srcif_refcnt = 0;
1578 		ill->ill_mrtun_refcnt = 0;
1579 	}
1580 	mutex_exit(&ill->ill_lock);
1581 }
1582 
1583 /*
1584  * ire_walk routine used to delete every IRE that depends on queues
1585  * associated with 'ill'.  (Always called as writer.)
1586  */
1587 static void
1588 ill_downi(ire_t *ire, char *ill_arg)
1589 {
1590 	ill_t	*ill = (ill_t *)ill_arg;
1591 
1592 	/*
1593 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1594 	 * interface X and ipif coming from interface Y, if interface
1595 	 * X and Y are part of the same IPMP group. Thus whenever interface
1596 	 * X goes down, remove all references to it by checking both
1597 	 * on ire_ipif and ire_stq.
1598 	 */
1599 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1600 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1601 		ire_delete(ire);
1602 	}
1603 }
1604 
1605 /*
1606  * A seperate routine for deleting revtun and srcif based routes
1607  * are needed because the ires only deleted when the interface
1608  * is unplumbed. Also these ires have ire_in_ill non-null as well.
1609  * we want to keep mobile IP specific code separate.
1610  */
1611 static void
1612 ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg)
1613 {
1614 	ill_t   *ill = (ill_t *)ill_arg;
1615 
1616 	ASSERT(ire->ire_in_ill != NULL);
1617 
1618 	if ((ire->ire_in_ill != NULL && ire->ire_in_ill == ill) ||
1619 	    (ire->ire_stq == ill->ill_wq) || (ire->ire_stq == ill->ill_rq)) {
1620 		ire_delete(ire);
1621 	}
1622 }
1623 
1624 /*
1625  * Remove ire/nce from the fastpath list.
1626  */
1627 void
1628 ill_fastpath_nack(ill_t *ill)
1629 {
1630 	if (ill->ill_isv6) {
1631 		nce_fastpath_list_dispatch(ill, NULL, NULL);
1632 	} else {
1633 		ire_fastpath_list_dispatch(ill, NULL, NULL);
1634 	}
1635 }
1636 
1637 /* Consume an M_IOCACK of the fastpath probe. */
1638 void
1639 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1640 {
1641 	mblk_t	*mp1 = mp;
1642 
1643 	/*
1644 	 * If this was the first attempt turn on the fastpath probing.
1645 	 */
1646 	mutex_enter(&ill->ill_lock);
1647 	if (ill->ill_dlpi_fastpath_state == IDMS_INPROGRESS)
1648 		ill->ill_dlpi_fastpath_state = IDMS_OK;
1649 	mutex_exit(&ill->ill_lock);
1650 
1651 	/* Free the M_IOCACK mblk, hold on to the data */
1652 	mp = mp->b_cont;
1653 	freeb(mp1);
1654 	if (mp == NULL)
1655 		return;
1656 	if (mp->b_cont != NULL) {
1657 		/*
1658 		 * Update all IRE's or NCE's that are waiting for
1659 		 * fastpath update.
1660 		 */
1661 		if (ill->ill_isv6) {
1662 			/*
1663 			 * update nce's in the fastpath list.
1664 			 */
1665 			nce_fastpath_list_dispatch(ill,
1666 			    ndp_fastpath_update, mp);
1667 		} else {
1668 
1669 			/*
1670 			 * update ire's in the fastpath list.
1671 			 */
1672 			ire_fastpath_list_dispatch(ill,
1673 			    ire_fastpath_update, mp);
1674 			/*
1675 			 * Check if we need to traverse reverse tunnel table.
1676 			 * Since there is only single ire_type (IRE_MIPRTUN)
1677 			 * in the table, we don't need to match on ire_type.
1678 			 * We have to check ire_mrtun_count and not the
1679 			 * ill_mrtun_refcnt since ill_mrtun_refcnt is set
1680 			 * on the incoming ill and here we are dealing with
1681 			 * outgoing ill.
1682 			 */
1683 			mutex_enter(&ire_mrtun_lock);
1684 			if (ire_mrtun_count != 0) {
1685 				mutex_exit(&ire_mrtun_lock);
1686 				ire_walk_ill_mrtun(MATCH_IRE_WQ, IRE_MIPRTUN,
1687 				    (void (*)(ire_t *, void *))
1688 					ire_fastpath_update, mp, ill);
1689 			} else {
1690 				mutex_exit(&ire_mrtun_lock);
1691 			}
1692 		}
1693 		mp1 = mp->b_cont;
1694 		freeb(mp);
1695 		mp = mp1;
1696 	} else {
1697 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1698 	}
1699 
1700 	freeb(mp);
1701 }
1702 
1703 /*
1704  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1705  * The data portion of the request is a dl_unitdata_req_t template for
1706  * what we would send downstream in the absence of a fastpath confirmation.
1707  */
1708 int
1709 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1710 {
1711 	struct iocblk	*ioc;
1712 	mblk_t	*mp;
1713 
1714 	if (dlur_mp == NULL)
1715 		return (EINVAL);
1716 
1717 	mutex_enter(&ill->ill_lock);
1718 	switch (ill->ill_dlpi_fastpath_state) {
1719 	case IDMS_FAILED:
1720 		/*
1721 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1722 		 * support it.
1723 		 */
1724 		mutex_exit(&ill->ill_lock);
1725 		return (ENOTSUP);
1726 	case IDMS_UNKNOWN:
1727 		/* This is the first probe */
1728 		ill->ill_dlpi_fastpath_state = IDMS_INPROGRESS;
1729 		break;
1730 	default:
1731 		break;
1732 	}
1733 	mutex_exit(&ill->ill_lock);
1734 
1735 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1736 		return (EAGAIN);
1737 
1738 	mp->b_cont = copyb(dlur_mp);
1739 	if (mp->b_cont == NULL) {
1740 		freeb(mp);
1741 		return (EAGAIN);
1742 	}
1743 
1744 	ioc = (struct iocblk *)mp->b_rptr;
1745 	ioc->ioc_count = msgdsize(mp->b_cont);
1746 
1747 	putnext(ill->ill_wq, mp);
1748 	return (0);
1749 }
1750 
1751 void
1752 ill_capability_probe(ill_t *ill)
1753 {
1754 	/*
1755 	 * Do so only if negotiation is enabled, capabilities are unknown,
1756 	 * and a capability negotiation is not already in progress.
1757 	 */
1758 	if (ill->ill_capab_state != IDMS_UNKNOWN &&
1759 	    ill->ill_capab_state != IDMS_RENEG)
1760 		return;
1761 
1762 	ill->ill_capab_state = IDMS_INPROGRESS;
1763 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1764 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1765 }
1766 
1767 void
1768 ill_capability_reset(ill_t *ill)
1769 {
1770 	mblk_t *sc_mp = NULL;
1771 	mblk_t *tmp;
1772 
1773 	/*
1774 	 * Note here that we reset the state to UNKNOWN, and later send
1775 	 * down the DL_CAPABILITY_REQ without first setting the state to
1776 	 * INPROGRESS.  We do this in order to distinguish the
1777 	 * DL_CAPABILITY_ACK response which may come back in response to
1778 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1779 	 * also handle the case where the driver doesn't send us back
1780 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1781 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1782 	 * features are turned off until the state reaches IDMS_OK.
1783 	 */
1784 	ill->ill_capab_state = IDMS_UNKNOWN;
1785 
1786 	/*
1787 	 * Disable sub-capabilities and request a list of sub-capability
1788 	 * messages which will be sent down to the driver.  Each handler
1789 	 * allocates the corresponding dl_capability_sub_t inside an
1790 	 * mblk, and links it to the existing sc_mp mblk, or return it
1791 	 * as sc_mp if it's the first sub-capability (the passed in
1792 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1793 	 * sc_mp will be pulled-up, before passing it downstream.
1794 	 */
1795 	ill_capability_mdt_reset(ill, &sc_mp);
1796 	ill_capability_hcksum_reset(ill, &sc_mp);
1797 	ill_capability_zerocopy_reset(ill, &sc_mp);
1798 /* EXPORT DELETE START */
1799 	ill_capability_ipsec_reset(ill, &sc_mp);
1800 /* EXPORT DELETE END */
1801 	ill_capability_poll_reset(ill, &sc_mp);
1802 
1803 	/* Nothing to send down in order to disable the capabilities? */
1804 	if (sc_mp == NULL)
1805 		return;
1806 
1807 	tmp = msgpullup(sc_mp, -1);
1808 	freemsg(sc_mp);
1809 	if ((sc_mp = tmp) == NULL) {
1810 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1811 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1812 		return;
1813 	}
1814 
1815 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1816 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1817 }
1818 
1819 /*
1820  * Request or set new-style hardware capabilities supported by DLS provider.
1821  */
1822 static void
1823 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1824 {
1825 	mblk_t *mp;
1826 	dl_capability_req_t *capb;
1827 	size_t size = 0;
1828 	uint8_t *ptr;
1829 
1830 	if (reqp != NULL)
1831 		size = MBLKL(reqp);
1832 
1833 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1834 	if (mp == NULL) {
1835 		freemsg(reqp);
1836 		return;
1837 	}
1838 	ptr = mp->b_rptr;
1839 
1840 	capb = (dl_capability_req_t *)ptr;
1841 	ptr += sizeof (dl_capability_req_t);
1842 
1843 	if (reqp != NULL) {
1844 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1845 		capb->dl_sub_length = size;
1846 		bcopy(reqp->b_rptr, ptr, size);
1847 		ptr += size;
1848 		mp->b_cont = reqp->b_cont;
1849 		freeb(reqp);
1850 	}
1851 	ASSERT(ptr == mp->b_wptr);
1852 
1853 	ill_dlpi_send(ill, mp);
1854 }
1855 
1856 static void
1857 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1858 {
1859 	dl_capab_id_t *id_ic;
1860 	uint_t sub_dl_cap = outers->dl_cap;
1861 	dl_capability_sub_t *inners;
1862 	uint8_t *capend;
1863 
1864 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1865 
1866 	/*
1867 	 * Note: range checks here are not absolutely sufficient to
1868 	 * make us robust against malformed messages sent by drivers;
1869 	 * this is in keeping with the rest of IP's dlpi handling.
1870 	 * (Remember, it's coming from something else in the kernel
1871 	 * address space)
1872 	 */
1873 
1874 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1875 	if (capend > mp->b_wptr) {
1876 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1877 		    "malformed sub-capability too long for mblk");
1878 		return;
1879 	}
1880 
1881 	id_ic = (dl_capab_id_t *)(outers + 1);
1882 
1883 	if (outers->dl_length < sizeof (*id_ic) ||
1884 	    (inners = &id_ic->id_subcap,
1885 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1886 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1887 		    "encapsulated capab type %d too long for mblk",
1888 		    inners->dl_cap);
1889 		return;
1890 	}
1891 
1892 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1893 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1894 		    "isn't as expected; pass-thru module(s) detected, "
1895 		    "discarding capability\n", inners->dl_cap));
1896 		return;
1897 	}
1898 
1899 	/* Process the encapsulated sub-capability */
1900 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1901 }
1902 
1903 /*
1904  * Process Multidata Transmit capability negotiation ack received from a
1905  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1906  * DL_CAPABILITY_ACK message.
1907  */
1908 static void
1909 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1910 {
1911 	mblk_t *nmp = NULL;
1912 	dl_capability_req_t *oc;
1913 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1914 	ill_mdt_capab_t **ill_mdt_capab;
1915 	uint_t sub_dl_cap = isub->dl_cap;
1916 	uint8_t *capend;
1917 
1918 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1919 
1920 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1921 
1922 	/*
1923 	 * Note: range checks here are not absolutely sufficient to
1924 	 * make us robust against malformed messages sent by drivers;
1925 	 * this is in keeping with the rest of IP's dlpi handling.
1926 	 * (Remember, it's coming from something else in the kernel
1927 	 * address space)
1928 	 */
1929 
1930 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1931 	if (capend > mp->b_wptr) {
1932 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1933 		    "malformed sub-capability too long for mblk");
1934 		return;
1935 	}
1936 
1937 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1938 
1939 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1940 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1941 		    "unsupported MDT sub-capability (version %d, expected %d)",
1942 		    mdt_ic->mdt_version, MDT_VERSION_2);
1943 		return;
1944 	}
1945 
1946 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1947 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1948 		    "capability isn't as expected; pass-thru module(s) "
1949 		    "detected, discarding capability\n"));
1950 		return;
1951 	}
1952 
1953 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1954 
1955 		if (*ill_mdt_capab == NULL) {
1956 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1957 			    KM_NOSLEEP);
1958 
1959 			if (*ill_mdt_capab == NULL) {
1960 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1961 				    "could not enable MDT version %d "
1962 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1963 				    ill->ill_name);
1964 				return;
1965 			}
1966 		}
1967 
1968 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1969 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1970 		    "header spaces, %d max pld bufs, %d span limit)\n",
1971 		    ill->ill_name, MDT_VERSION_2,
1972 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1973 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1974 
1975 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1976 		(*ill_mdt_capab)->ill_mdt_on = 1;
1977 		/*
1978 		 * Round the following values to the nearest 32-bit; ULP
1979 		 * may further adjust them to accomodate for additional
1980 		 * protocol headers.  We pass these values to ULP during
1981 		 * bind time.
1982 		 */
1983 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1984 		    roundup(mdt_ic->mdt_hdr_head, 4);
1985 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1986 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1987 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1988 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1989 
1990 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1991 	} else {
1992 		uint_t size;
1993 		uchar_t *rptr;
1994 
1995 		size = sizeof (dl_capability_req_t) +
1996 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1997 
1998 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1999 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2000 			    "could not enable MDT for %s (ENOMEM)\n",
2001 			    ill->ill_name);
2002 			return;
2003 		}
2004 
2005 		rptr = nmp->b_rptr;
2006 		/* initialize dl_capability_req_t */
2007 		oc = (dl_capability_req_t *)nmp->b_rptr;
2008 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2009 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2010 		    sizeof (dl_capab_mdt_t);
2011 		nmp->b_rptr += sizeof (dl_capability_req_t);
2012 
2013 		/* initialize dl_capability_sub_t */
2014 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2015 		nmp->b_rptr += sizeof (*isub);
2016 
2017 		/* initialize dl_capab_mdt_t */
2018 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2019 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2020 
2021 		nmp->b_rptr = rptr;
2022 
2023 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2024 		    "to enable MDT version %d\n", ill->ill_name,
2025 		    MDT_VERSION_2));
2026 
2027 		/* set ENABLE flag */
2028 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2029 
2030 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2031 		ill_dlpi_send(ill, nmp);
2032 	}
2033 }
2034 
2035 static void
2036 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2037 {
2038 	mblk_t *mp;
2039 	dl_capab_mdt_t *mdt_subcap;
2040 	dl_capability_sub_t *dl_subcap;
2041 	int size;
2042 
2043 	if (!(ill->ill_capabilities & ILL_CAPAB_MDT))
2044 		return;
2045 
2046 	ASSERT(ill->ill_mdt_capab != NULL);
2047 	/*
2048 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2049 	 * structure since it's possible that another thread is still
2050 	 * referring to it.  The structure only gets deallocated when
2051 	 * we destroy the ill.
2052 	 */
2053 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2054 
2055 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2056 
2057 	mp = allocb(size, BPRI_HI);
2058 	if (mp == NULL) {
2059 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2060 		    "request to disable MDT\n"));
2061 		return;
2062 	}
2063 
2064 	mp->b_wptr = mp->b_rptr + size;
2065 
2066 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2067 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2068 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2069 
2070 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2071 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2072 	mdt_subcap->mdt_flags = 0;
2073 	mdt_subcap->mdt_hdr_head = 0;
2074 	mdt_subcap->mdt_hdr_tail = 0;
2075 
2076 	if (*sc_mp != NULL)
2077 		linkb(*sc_mp, mp);
2078 	else
2079 		*sc_mp = mp;
2080 }
2081 
2082 /* EXPORT DELETE START */
2083 /*
2084  * Send a DL_NOTIFY_REQ to the specified ill to enable
2085  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2086  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2087  * acceleration.
2088  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2089  */
2090 static boolean_t
2091 ill_enable_promisc_notify(ill_t *ill)
2092 {
2093 	mblk_t *mp;
2094 	dl_notify_req_t *req;
2095 
2096 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2097 
2098 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2099 	if (mp == NULL)
2100 		return (B_FALSE);
2101 
2102 	req = (dl_notify_req_t *)mp->b_rptr;
2103 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2104 	    DL_NOTE_PROMISC_OFF_PHYS;
2105 
2106 	ill_dlpi_send(ill, mp);
2107 
2108 	return (B_TRUE);
2109 }
2110 
2111 
2112 /*
2113  * Allocate an IPsec capability request which will be filled by our
2114  * caller to turn on support for one or more algorithms.
2115  */
2116 static mblk_t *
2117 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2118 {
2119 	mblk_t *nmp;
2120 	dl_capability_req_t	*ocap;
2121 	dl_capab_ipsec_t	*ocip;
2122 	dl_capab_ipsec_t	*icip;
2123 	uint8_t			*ptr;
2124 	icip = (dl_capab_ipsec_t *)(isub + 1);
2125 
2126 	/*
2127 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2128 	 * PROMISC_ON/OFF notification from the provider. We need to
2129 	 * do this before enabling the algorithms to avoid leakage of
2130 	 * cleartext packets.
2131 	 */
2132 
2133 	if (!ill_enable_promisc_notify(ill))
2134 		return (NULL);
2135 
2136 	/*
2137 	 * Allocate new mblk which will contain a new capability
2138 	 * request to enable the capabilities.
2139 	 */
2140 
2141 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2142 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2143 	if (nmp == NULL)
2144 		return (NULL);
2145 
2146 	ptr = nmp->b_rptr;
2147 
2148 	/* initialize dl_capability_req_t */
2149 	ocap = (dl_capability_req_t *)ptr;
2150 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2151 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2152 	ptr += sizeof (dl_capability_req_t);
2153 
2154 	/* initialize dl_capability_sub_t */
2155 	bcopy(isub, ptr, sizeof (*isub));
2156 	ptr += sizeof (*isub);
2157 
2158 	/* initialize dl_capab_ipsec_t */
2159 	ocip = (dl_capab_ipsec_t *)ptr;
2160 	bcopy(icip, ocip, sizeof (*icip));
2161 
2162 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2163 	return (nmp);
2164 }
2165 
2166 /*
2167  * Process an IPsec capability negotiation ack received from a DLS Provider.
2168  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2169  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2170  */
2171 static void
2172 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2173 {
2174 	dl_capab_ipsec_t	*icip;
2175 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2176 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2177 	uint_t cipher, nciphers;
2178 	mblk_t *nmp;
2179 	uint_t alg_len;
2180 	boolean_t need_sadb_dump;
2181 	uint_t sub_dl_cap = isub->dl_cap;
2182 	ill_ipsec_capab_t **ill_capab;
2183 	uint64_t ill_capab_flag;
2184 	uint8_t *capend, *ciphend;
2185 	boolean_t sadb_resync;
2186 
2187 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2188 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2189 
2190 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2191 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2192 		ill_capab_flag = ILL_CAPAB_AH;
2193 	} else {
2194 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2195 		ill_capab_flag = ILL_CAPAB_ESP;
2196 	}
2197 
2198 	/*
2199 	 * If the ill capability structure exists, then this incoming
2200 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2201 	 * If this is so, then we'd need to resynchronize the SADB
2202 	 * after re-enabling the offloaded ciphers.
2203 	 */
2204 	sadb_resync = (*ill_capab != NULL);
2205 
2206 	/*
2207 	 * Note: range checks here are not absolutely sufficient to
2208 	 * make us robust against malformed messages sent by drivers;
2209 	 * this is in keeping with the rest of IP's dlpi handling.
2210 	 * (Remember, it's coming from something else in the kernel
2211 	 * address space)
2212 	 */
2213 
2214 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2215 	if (capend > mp->b_wptr) {
2216 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2217 		    "malformed sub-capability too long for mblk");
2218 		return;
2219 	}
2220 
2221 	/*
2222 	 * There are two types of acks we process here:
2223 	 * 1. acks in reply to a (first form) generic capability req
2224 	 *    (no ENABLE flag set)
2225 	 * 2. acks in reply to a ENABLE capability req.
2226 	 *    (ENABLE flag set)
2227 	 *
2228 	 * We process the subcapability passed as argument as follows:
2229 	 * 1 do initializations
2230 	 *   1.1 initialize nmp = NULL
2231 	 *   1.2 set need_sadb_dump to B_FALSE
2232 	 * 2 for each cipher in subcapability:
2233 	 *   2.1 if ENABLE flag is set:
2234 	 *	2.1.1 update per-ill ipsec capabilities info
2235 	 *	2.1.2 set need_sadb_dump to B_TRUE
2236 	 *   2.2 if ENABLE flag is not set:
2237 	 *	2.2.1 if nmp is NULL:
2238 	 *		2.2.1.1 allocate and initialize nmp
2239 	 *		2.2.1.2 init current pos in nmp
2240 	 *	2.2.2 copy current cipher to current pos in nmp
2241 	 *	2.2.3 set ENABLE flag in nmp
2242 	 *	2.2.4 update current pos
2243 	 * 3 if nmp is not equal to NULL, send enable request
2244 	 *   3.1 send capability request
2245 	 * 4 if need_sadb_dump is B_TRUE
2246 	 *   4.1 enable promiscuous on/off notifications
2247 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2248 	 *	AH or ESP SA's to interface.
2249 	 */
2250 
2251 	nmp = NULL;
2252 	oalg = NULL;
2253 	need_sadb_dump = B_FALSE;
2254 	icip = (dl_capab_ipsec_t *)(isub + 1);
2255 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2256 
2257 	nciphers = icip->cip_nciphers;
2258 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2259 
2260 	if (ciphend > capend) {
2261 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2262 		    "too many ciphers for sub-capability len");
2263 		return;
2264 	}
2265 
2266 	for (cipher = 0; cipher < nciphers; cipher++) {
2267 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2268 
2269 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2270 			/*
2271 			 * TBD: when we provide a way to disable capabilities
2272 			 * from above, need to manage the request-pending state
2273 			 * and fail if we were not expecting this ACK.
2274 			 */
2275 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2276 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2277 
2278 			/*
2279 			 * Update IPsec capabilities for this ill
2280 			 */
2281 
2282 			if (*ill_capab == NULL) {
2283 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2284 				    ("ill_capability_ipsec_ack: "
2285 					"allocating ipsec_capab for ill\n"));
2286 				*ill_capab = ill_ipsec_capab_alloc();
2287 
2288 				if (*ill_capab == NULL) {
2289 					cmn_err(CE_WARN,
2290 					    "ill_capability_ipsec_ack: "
2291 					    "could not enable IPsec Hardware "
2292 					    "acceleration for %s (ENOMEM)\n",
2293 					    ill->ill_name);
2294 					return;
2295 				}
2296 			}
2297 
2298 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2299 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2300 
2301 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2302 				cmn_err(CE_WARN,
2303 				    "ill_capability_ipsec_ack: "
2304 				    "malformed IPsec algorithm id %d",
2305 				    ialg->alg_prim);
2306 				continue;
2307 			}
2308 
2309 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2310 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2311 				    ialg->alg_prim);
2312 			} else {
2313 				ipsec_capab_algparm_t *alp;
2314 
2315 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2316 				    ialg->alg_prim);
2317 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2318 				    ialg->alg_prim)) {
2319 					cmn_err(CE_WARN,
2320 					    "ill_capability_ipsec_ack: "
2321 					    "no space for IPsec alg id %d",
2322 					    ialg->alg_prim);
2323 					continue;
2324 				}
2325 				alp = &((*ill_capab)->encr_algparm[
2326 						ialg->alg_prim]);
2327 				alp->minkeylen = ialg->alg_minbits;
2328 				alp->maxkeylen = ialg->alg_maxbits;
2329 			}
2330 			ill->ill_capabilities |= ill_capab_flag;
2331 			/*
2332 			 * indicate that a capability was enabled, which
2333 			 * will be used below to kick off a SADB dump
2334 			 * to the ill.
2335 			 */
2336 			need_sadb_dump = B_TRUE;
2337 		} else {
2338 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2339 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2340 				ialg->alg_prim));
2341 
2342 			if (nmp == NULL) {
2343 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2344 				if (nmp == NULL) {
2345 					/*
2346 					 * Sending the PROMISC_ON/OFF
2347 					 * notification request failed.
2348 					 * We cannot enable the algorithms
2349 					 * since the Provider will not
2350 					 * notify IP of promiscous mode
2351 					 * changes, which could lead
2352 					 * to leakage of packets.
2353 					 */
2354 					cmn_err(CE_WARN,
2355 					    "ill_capability_ipsec_ack: "
2356 					    "could not enable IPsec Hardware "
2357 					    "acceleration for %s (ENOMEM)\n",
2358 					    ill->ill_name);
2359 					return;
2360 				}
2361 				/* ptr to current output alg specifier */
2362 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2363 			}
2364 
2365 			/*
2366 			 * Copy current alg specifier, set ENABLE
2367 			 * flag, and advance to next output alg.
2368 			 * For now we enable all IPsec capabilities.
2369 			 */
2370 			ASSERT(oalg != NULL);
2371 			bcopy(ialg, oalg, alg_len);
2372 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2373 			nmp->b_wptr += alg_len;
2374 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2375 		}
2376 
2377 		/* move to next input algorithm specifier */
2378 		ialg = (dl_capab_ipsec_alg_t *)
2379 		    ((char *)ialg + alg_len);
2380 	}
2381 
2382 	if (nmp != NULL)
2383 		/*
2384 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2385 		 * IPsec hardware acceleration.
2386 		 */
2387 		ill_dlpi_send(ill, nmp);
2388 
2389 	if (need_sadb_dump)
2390 		/*
2391 		 * An acknowledgement corresponding to a request to
2392 		 * enable acceleration was received, notify SADB.
2393 		 */
2394 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2395 }
2396 
2397 /*
2398  * Given an mblk with enough space in it, create sub-capability entries for
2399  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2400  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2401  * in preparation for the reset the DL_CAPABILITY_REQ message.
2402  */
2403 static void
2404 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2405     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2406 {
2407 	dl_capab_ipsec_t *oipsec;
2408 	dl_capab_ipsec_alg_t *oalg;
2409 	dl_capability_sub_t *dl_subcap;
2410 	int i, k;
2411 
2412 	ASSERT(nciphers > 0);
2413 	ASSERT(ill_cap != NULL);
2414 	ASSERT(mp != NULL);
2415 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2416 
2417 	/* dl_capability_sub_t for "stype" */
2418 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2419 	dl_subcap->dl_cap = stype;
2420 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2421 	mp->b_wptr += sizeof (dl_capability_sub_t);
2422 
2423 	/* dl_capab_ipsec_t for "stype" */
2424 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2425 	oipsec->cip_version = 1;
2426 	oipsec->cip_nciphers = nciphers;
2427 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2428 
2429 	/* create entries for "stype" AUTH ciphers */
2430 	for (i = 0; i < ill_cap->algs_size; i++) {
2431 		for (k = 0; k < BITSPERBYTE; k++) {
2432 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2433 				continue;
2434 
2435 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2436 			bzero((void *)oalg, sizeof (*oalg));
2437 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2438 			oalg->alg_prim = k + (BITSPERBYTE * i);
2439 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2440 		}
2441 	}
2442 	/* create entries for "stype" ENCR ciphers */
2443 	for (i = 0; i < ill_cap->algs_size; i++) {
2444 		for (k = 0; k < BITSPERBYTE; k++) {
2445 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2446 				continue;
2447 
2448 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2449 			bzero((void *)oalg, sizeof (*oalg));
2450 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2451 			oalg->alg_prim = k + (BITSPERBYTE * i);
2452 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2453 		}
2454 	}
2455 }
2456 
2457 /*
2458  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2459  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2460  * POPC instruction, but our macro is more flexible for an arbitrary length
2461  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2462  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2463  * stays that way, we can reduce the number of iterations required.
2464  */
2465 #define	COUNT_1S(val, sum) {					\
2466 	uint8_t x = val & 0xff;					\
2467 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2468 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2469 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2470 }
2471 
2472 /* ARGSUSED */
2473 static void
2474 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2475 {
2476 	mblk_t *mp;
2477 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2478 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2479 	uint64_t ill_capabilities = ill->ill_capabilities;
2480 	int ah_cnt = 0, esp_cnt = 0;
2481 	int ah_len = 0, esp_len = 0;
2482 	int i, size = 0;
2483 
2484 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2485 		return;
2486 
2487 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2488 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2489 
2490 	/* Find out the number of ciphers for AH */
2491 	if (cap_ah != NULL) {
2492 		for (i = 0; i < cap_ah->algs_size; i++) {
2493 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2494 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2495 		}
2496 		if (ah_cnt > 0) {
2497 			size += sizeof (dl_capability_sub_t) +
2498 			    sizeof (dl_capab_ipsec_t);
2499 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2500 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2501 			size += ah_len;
2502 		}
2503 	}
2504 
2505 	/* Find out the number of ciphers for ESP */
2506 	if (cap_esp != NULL) {
2507 		for (i = 0; i < cap_esp->algs_size; i++) {
2508 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2509 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2510 		}
2511 		if (esp_cnt > 0) {
2512 			size += sizeof (dl_capability_sub_t) +
2513 			    sizeof (dl_capab_ipsec_t);
2514 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2515 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2516 			size += esp_len;
2517 		}
2518 	}
2519 
2520 	if (size == 0) {
2521 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2522 		    "there's nothing to reset\n"));
2523 		return;
2524 	}
2525 
2526 	mp = allocb(size, BPRI_HI);
2527 	if (mp == NULL) {
2528 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2529 		    "request to disable IPSEC Hardware Acceleration\n"));
2530 		return;
2531 	}
2532 
2533 	/*
2534 	 * Clear the capability flags for IPSec HA but retain the ill
2535 	 * capability structures since it's possible that another thread
2536 	 * is still referring to them.  The structures only get deallocated
2537 	 * when we destroy the ill.
2538 	 *
2539 	 * Various places check the flags to see if the ill is capable of
2540 	 * hardware acceleration, and by clearing them we ensure that new
2541 	 * outbound IPSec packets are sent down encrypted.
2542 	 */
2543 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2544 
2545 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2546 	if (ah_cnt > 0) {
2547 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2548 		    cap_ah, mp);
2549 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2550 	}
2551 
2552 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2553 	if (esp_cnt > 0) {
2554 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2555 		    cap_esp, mp);
2556 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2557 	}
2558 
2559 	/*
2560 	 * At this point we've composed a bunch of sub-capabilities to be
2561 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2562 	 * by the caller.  Upon receiving this reset message, the driver
2563 	 * must stop inbound decryption (by destroying all inbound SAs)
2564 	 * and let the corresponding packets come in encrypted.
2565 	 */
2566 
2567 	if (*sc_mp != NULL)
2568 		linkb(*sc_mp, mp);
2569 	else
2570 		*sc_mp = mp;
2571 }
2572 /* EXPORT DELETE END */
2573 
2574 static void
2575 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2576     boolean_t encapsulated)
2577 {
2578 	boolean_t legacy = B_FALSE;
2579 
2580 	/*
2581 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2582 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2583 	 * instructed the driver to disable its advertised capabilities,
2584 	 * so there's no point in accepting any response at this moment.
2585 	 */
2586 	if (ill->ill_capab_state == IDMS_UNKNOWN)
2587 		return;
2588 
2589 /* EXPORT DELETE START */
2590 	/*
2591 	 * Note that only the following two sub-capabilities may be
2592 	 * considered as "legacy", since their original definitions
2593 	 * do not incorporate the dl_mid_t module ID token, and hence
2594 	 * may require the use of the wrapper sub-capability.
2595 	 */
2596 	switch (subp->dl_cap) {
2597 	case DL_CAPAB_IPSEC_AH:
2598 	case DL_CAPAB_IPSEC_ESP:
2599 		legacy = B_TRUE;
2600 		break;
2601 	}
2602 /* EXPORT DELETE END */
2603 
2604 	/*
2605 	 * For legacy sub-capabilities which don't incorporate a queue_t
2606 	 * pointer in their structures, discard them if we detect that
2607 	 * there are intermediate modules in between IP and the driver.
2608 	 */
2609 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2610 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2611 		    "%d discarded; %d module(s) present below IP\n",
2612 		    subp->dl_cap, ill->ill_lmod_cnt));
2613 		return;
2614 	}
2615 
2616 	switch (subp->dl_cap) {
2617 /* EXPORT DELETE START */
2618 	case DL_CAPAB_IPSEC_AH:
2619 	case DL_CAPAB_IPSEC_ESP:
2620 		ill_capability_ipsec_ack(ill, mp, subp);
2621 		break;
2622 /* EXPORT DELETE END */
2623 	case DL_CAPAB_MDT:
2624 		ill_capability_mdt_ack(ill, mp, subp);
2625 		break;
2626 	case DL_CAPAB_HCKSUM:
2627 		ill_capability_hcksum_ack(ill, mp, subp);
2628 		break;
2629 	case DL_CAPAB_ZEROCOPY:
2630 		ill_capability_zerocopy_ack(ill, mp, subp);
2631 		break;
2632 	case DL_CAPAB_POLL:
2633 		ill_capability_poll_ack(ill, mp, subp);
2634 		break;
2635 	default:
2636 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2637 		    subp->dl_cap));
2638 	}
2639 }
2640 
2641 /*
2642  * As part of negotiating polling capability, the driver tells us
2643  * the default (or normal) blanking interval and packet threshold
2644  * (the receive timer fires if blanking interval is reached or
2645  * the packet threshold is reached).
2646  *
2647  * As part of manipulating the polling interval, we always use our
2648  * estimated interval (avg service time * number of packets queued
2649  * on the squeue) but we try to blank for a minimum of
2650  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2651  * packet threshold during this time. When we are not in polling mode
2652  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2653  * rr_min_blank_ratio but up the packet cnt by a ratio of
2654  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2655  * possible although for a shorter interval.
2656  */
2657 #define	RR_MAX_BLANK_RATIO	20
2658 #define	RR_MIN_BLANK_RATIO	10
2659 #define	RR_MAX_PKT_CNT_RATIO	3
2660 #define	RR_MIN_PKT_CNT_RATIO	3
2661 
2662 /*
2663  * These can be tuned via /etc/system.
2664  */
2665 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2666 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2667 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2668 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2669 
2670 static mac_resource_handle_t
2671 ill_ring_add(void *arg, mac_resource_t *mrp)
2672 {
2673 	ill_t			*ill = (ill_t *)arg;
2674 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2675 	ill_rx_ring_t		*rx_ring;
2676 	int			ip_rx_index;
2677 
2678 	if (mrp->mr_type != MAC_RX_FIFO) {
2679 		return (NULL);
2680 	}
2681 	ASSERT(ill != NULL);
2682 	ASSERT(ill->ill_poll_capab != NULL);
2683 	ASSERT(mrp != NULL);
2684 
2685 	mutex_enter(&ill->ill_lock);
2686 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2687 		rx_ring = &ill->ill_poll_capab->ill_ring_tbl[ip_rx_index];
2688 		ASSERT(rx_ring != NULL);
2689 
2690 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2691 			time_t normal_blank_time =
2692 			    mrfp->mrf_normal_blank_time;
2693 			uint_t normal_pkt_cnt =
2694 			    mrfp->mrf_normal_pkt_count;
2695 
2696 			bzero(rx_ring, sizeof (ill_rx_ring_t));
2697 
2698 			rx_ring->rr_blank = mrfp->mrf_blank;
2699 			rx_ring->rr_handle = mrfp->mrf_arg;
2700 			rx_ring->rr_ill = ill;
2701 			rx_ring->rr_normal_blank_time = normal_blank_time;
2702 			rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2703 
2704 			rx_ring->rr_max_blank_time =
2705 			    normal_blank_time * rr_max_blank_ratio;
2706 			rx_ring->rr_min_blank_time =
2707 			    normal_blank_time * rr_min_blank_ratio;
2708 			rx_ring->rr_max_pkt_cnt =
2709 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2710 			rx_ring->rr_min_pkt_cnt =
2711 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2712 
2713 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2714 			mutex_exit(&ill->ill_lock);
2715 
2716 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2717 			    (int), ip_rx_index);
2718 			return ((mac_resource_handle_t)rx_ring);
2719 		}
2720 	}
2721 
2722 	/*
2723 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2724 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2725 	 * should be made configurable. Meanwhile it cause no panic because
2726 	 * driver will pass ip_input a NULL handle which will make
2727 	 * IP allocate the default squeue and Polling mode will not
2728 	 * be used for this ring.
2729 	 */
2730 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2731 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2732 
2733 	mutex_exit(&ill->ill_lock);
2734 	return (NULL);
2735 }
2736 
2737 static boolean_t
2738 ill_capability_poll_init(ill_t *ill)
2739 {
2740 	ill_poll_capab_t	*ill_poll = ill->ill_poll_capab;
2741 	conn_t 			*connp;
2742 	size_t			sz;
2743 
2744 	if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2745 		if (ill_poll == NULL) {
2746 			cmn_err(CE_PANIC, "ill_capability_poll_init: "
2747 			    "polling enabled for ill=%s (%p) but data "
2748 			    "structs uninitialized\n", ill->ill_name,
2749 			    (void *)ill);
2750 		}
2751 		return (B_TRUE);
2752 	}
2753 
2754 	if (ill_poll != NULL) {
2755 		ill_rx_ring_t 	*rx_ring = ill_poll->ill_ring_tbl;
2756 		/* Polling is being re-enabled */
2757 
2758 		connp = ill_poll->ill_unbind_conn;
2759 		ASSERT(rx_ring != NULL);
2760 		bzero((void *)ill_poll, sizeof (ill_poll_capab_t));
2761 		bzero((void *)rx_ring,
2762 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2763 		ill_poll->ill_ring_tbl = rx_ring;
2764 		ill_poll->ill_unbind_conn = connp;
2765 		return (B_TRUE);
2766 	}
2767 
2768 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP)) == NULL)
2769 		return (B_FALSE);
2770 
2771 	sz = sizeof (ill_poll_capab_t);
2772 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2773 
2774 	ill_poll = kmem_zalloc(sz, KM_NOSLEEP);
2775 	if (ill_poll == NULL) {
2776 		cmn_err(CE_WARN, "ill_capability_poll_init: could not "
2777 		    "allocate poll_capab for %s (%p)\n", ill->ill_name,
2778 		    (void *)ill);
2779 		CONN_DEC_REF(connp);
2780 		return (B_FALSE);
2781 	}
2782 
2783 	/* Allocate space to hold ring table */
2784 	ill_poll->ill_ring_tbl = (ill_rx_ring_t *)&ill_poll[1];
2785 	ill->ill_poll_capab = ill_poll;
2786 	ill_poll->ill_unbind_conn = connp;
2787 	return (B_TRUE);
2788 }
2789 
2790 /*
2791  * ill_capability_poll_disable: disable polling capability. Since
2792  * any of the rings might already be in use, need to call ipsq_clean_all()
2793  * which gets behind the squeue to disable direct calls if necessary.
2794  * Clean up the direct tx function pointers as well.
2795  */
2796 static void
2797 ill_capability_poll_disable(ill_t *ill)
2798 {
2799 	ill_poll_capab_t	*ill_poll = ill->ill_poll_capab;
2800 
2801 	if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2802 		ipsq_clean_all(ill);
2803 		ill_poll->ill_tx = NULL;
2804 		ill_poll->ill_tx_handle = NULL;
2805 	}
2806 
2807 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
2808 }
2809 
2810 static void
2811 ill_capability_poll_capable(ill_t *ill, dl_capab_poll_t *ipoll,
2812     dl_capability_sub_t *isub)
2813 {
2814 	uint_t			size;
2815 	uchar_t			*rptr;
2816 	dl_capab_poll_t		poll, *opoll;
2817 	ill_poll_capab_t	*ill_poll;
2818 	mblk_t			*nmp = NULL;
2819 	dl_capability_req_t	*ocap;
2820 
2821 	if (!ill_capability_poll_init(ill))
2822 		return;
2823 	ill_poll = ill->ill_poll_capab;
2824 
2825 	/* Copy locally to get the members aligned */
2826 	bcopy((void *)ipoll, (void *)&poll, sizeof (dl_capab_poll_t));
2827 
2828 	/* Get the tx function and handle from the driver */
2829 	ill_poll->ill_tx = (ip_mac_tx_t)poll.poll_tx;
2830 	ill_poll->ill_tx_handle = (void *)poll.poll_tx_handle;
2831 
2832 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2833 	    isub->dl_length;
2834 
2835 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2836 		cmn_err(CE_WARN, "ill_capability_poll_ack: could not allocate "
2837 		    "memory for CAPAB_REQ for %s (%p)\n", ill->ill_name,
2838 		    (void *)ill);
2839 		return;
2840 	}
2841 
2842 	/* initialize dl_capability_req_t */
2843 	rptr = nmp->b_rptr;
2844 	ocap = (dl_capability_req_t *)rptr;
2845 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2846 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2847 	rptr += sizeof (dl_capability_req_t);
2848 
2849 	/* initialize dl_capability_sub_t */
2850 	bcopy(isub, rptr, sizeof (*isub));
2851 	rptr += sizeof (*isub);
2852 
2853 	opoll = (dl_capab_poll_t *)rptr;
2854 	rptr += sizeof (dl_capab_poll_t);
2855 
2856 	/* initialize dl_capab_poll_t to be sent down */
2857 	poll.poll_rx_handle = (uintptr_t)ill;
2858 	poll.poll_rx = (uintptr_t)ip_input;
2859 	poll.poll_ring_add = (uintptr_t)ill_ring_add;
2860 	poll.poll_flags = POLL_ENABLE;
2861 	bcopy((void *)&poll, (void *)opoll, sizeof (dl_capab_poll_t));
2862 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2863 
2864 	/* nmp points to a DL_CAPABILITY_REQ message to enable polling */
2865 	ill_dlpi_send(ill, nmp);
2866 }
2867 
2868 
2869 /*
2870  * Process a polling capability negotiation ack received
2871  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_POLL)
2872  * of a DL_CAPABILITY_ACK message.
2873  */
2874 static void
2875 ill_capability_poll_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2876 {
2877 	dl_capab_poll_t		*ipoll;
2878 	uint_t			sub_dl_cap = isub->dl_cap;
2879 	uint8_t			*capend;
2880 
2881 
2882 	ASSERT(sub_dl_cap == DL_CAPAB_POLL);
2883 
2884 	/*
2885 	 * Don't enable polling for ipv6 ill's
2886 	 */
2887 	if (ill->ill_isv6) {
2888 		return;
2889 	}
2890 
2891 	/*
2892 	 * Note: range checks here are not absolutely sufficient to
2893 	 * make us robust against malformed messages sent by drivers;
2894 	 * this is in keeping with the rest of IP's dlpi handling.
2895 	 * (Remember, it's coming from something else in the kernel
2896 	 * address space)
2897 	 */
2898 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2899 	if (capend > mp->b_wptr) {
2900 		cmn_err(CE_WARN, "ill_capability_poll_ack: "
2901 		    "malformed sub-capability too long for mblk");
2902 		return;
2903 	}
2904 
2905 	/*
2906 	 * There are two types of acks we process here:
2907 	 * 1. acks in reply to a (first form) generic capability req
2908 	 *    (poll_flag will be set to POLL_CAPABLE)
2909 	 * 2. acks in reply to a POLL_ENABLE capability req.
2910 	 *    (POLL_ENABLE flag set)
2911 	 */
2912 	ipoll = (dl_capab_poll_t *)(isub + 1);
2913 
2914 	if (!dlcapabcheckqid(&ipoll->poll_mid, ill->ill_lmod_rq)) {
2915 		ip1dbg(("ill_capability_poll_ack: mid token for polling "
2916 		    "capability isn't as expected; pass-thru "
2917 		    "module(s) detected, discarding capability\n"));
2918 		if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2919 			/*
2920 			 * This is a capability renegotitation case.
2921 			 * The interface better be unusable at this
2922 			 * point other wise bad things will happen
2923 			 * if we disable direct calls on a running
2924 			 * and up interface.
2925 			 */
2926 			ill_capability_poll_disable(ill);
2927 		}
2928 		return;
2929 	}
2930 
2931 	switch (ipoll->poll_flags) {
2932 	default:
2933 		/* Disable if unknown flag */
2934 	case POLL_DISABLE:
2935 		ill_capability_poll_disable(ill);
2936 		break;
2937 	case POLL_CAPABLE:
2938 		/*
2939 		 * If the capability was already enabled, its safe
2940 		 * to disable it first to get rid of stale information
2941 		 * and then start enabling it again.
2942 		 */
2943 		ill_capability_poll_disable(ill);
2944 		ill_capability_poll_capable(ill, ipoll, isub);
2945 		break;
2946 	case POLL_ENABLE:
2947 		if (!(ill->ill_capabilities & ILL_CAPAB_POLL)) {
2948 			ASSERT(ill->ill_poll_capab != NULL);
2949 			ill->ill_capabilities |= ILL_CAPAB_POLL;
2950 		}
2951 		break;
2952 	}
2953 }
2954 
2955 static void
2956 ill_capability_poll_reset(ill_t *ill, mblk_t **sc_mp)
2957 {
2958 	mblk_t *mp;
2959 	dl_capab_poll_t *ipoll;
2960 	dl_capability_sub_t *dl_subcap;
2961 	int size;
2962 
2963 	if (!(ill->ill_capabilities & ILL_CAPAB_POLL))
2964 		return;
2965 
2966 	ASSERT(ill->ill_poll_capab != NULL);
2967 
2968 	/*
2969 	 * Disable polling capability
2970 	 */
2971 	ill_capability_poll_disable(ill);
2972 
2973 	size = sizeof (*dl_subcap) + sizeof (*ipoll);
2974 
2975 	mp = allocb(size, BPRI_HI);
2976 	if (mp == NULL) {
2977 		ip1dbg(("ill_capability_poll_reset: unable to allocate "
2978 		    "request to disable polling\n"));
2979 		return;
2980 	}
2981 
2982 	mp->b_wptr = mp->b_rptr + size;
2983 
2984 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2985 	dl_subcap->dl_cap = DL_CAPAB_POLL;
2986 	dl_subcap->dl_length = sizeof (*ipoll);
2987 
2988 	ipoll = (dl_capab_poll_t *)(dl_subcap + 1);
2989 	ipoll->poll_flags = POLL_DISABLE;
2990 
2991 	if (*sc_mp != NULL)
2992 		linkb(*sc_mp, mp);
2993 	else
2994 		*sc_mp = mp;
2995 }
2996 
2997 
2998 /*
2999  * Process a hardware checksum offload capability negotiation ack received
3000  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3001  * of a DL_CAPABILITY_ACK message.
3002  */
3003 static void
3004 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3005 {
3006 	dl_capability_req_t	*ocap;
3007 	dl_capab_hcksum_t	*ihck, *ohck;
3008 	ill_hcksum_capab_t	**ill_hcksum;
3009 	mblk_t			*nmp = NULL;
3010 	uint_t			sub_dl_cap = isub->dl_cap;
3011 	uint8_t			*capend;
3012 
3013 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3014 
3015 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3016 
3017 	/*
3018 	 * Note: range checks here are not absolutely sufficient to
3019 	 * make us robust against malformed messages sent by drivers;
3020 	 * this is in keeping with the rest of IP's dlpi handling.
3021 	 * (Remember, it's coming from something else in the kernel
3022 	 * address space)
3023 	 */
3024 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3025 	if (capend > mp->b_wptr) {
3026 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3027 		    "malformed sub-capability too long for mblk");
3028 		return;
3029 	}
3030 
3031 	/*
3032 	 * There are two types of acks we process here:
3033 	 * 1. acks in reply to a (first form) generic capability req
3034 	 *    (no ENABLE flag set)
3035 	 * 2. acks in reply to a ENABLE capability req.
3036 	 *    (ENABLE flag set)
3037 	 */
3038 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3039 
3040 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3041 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3042 		    "unsupported hardware checksum "
3043 		    "sub-capability (version %d, expected %d)",
3044 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3045 		return;
3046 	}
3047 
3048 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3049 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3050 		    "checksum capability isn't as expected; pass-thru "
3051 		    "module(s) detected, discarding capability\n"));
3052 		return;
3053 	}
3054 
3055 #define	CURR_HCKSUM_CAPAB \
3056 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 | HCKSUM_IPHDRCKSUM)
3057 
3058 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3059 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3060 		/* do ENABLE processing */
3061 		if (*ill_hcksum == NULL) {
3062 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3063 			    KM_NOSLEEP);
3064 
3065 			if (*ill_hcksum == NULL) {
3066 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3067 				    "could not enable hcksum version %d "
3068 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3069 				    ill->ill_name);
3070 				return;
3071 			}
3072 		}
3073 
3074 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3075 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3076 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3077 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3078 		    "has enabled hardware checksumming\n ",
3079 		    ill->ill_name));
3080 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3081 		/*
3082 		 * Enabling hardware checksum offload
3083 		 * Currently IP supports {TCP,UDP}/IPv4
3084 		 * partial and full cksum offload and
3085 		 * IPv4 header checksum offload.
3086 		 * Allocate new mblk which will
3087 		 * contain a new capability request
3088 		 * to enable hardware checksum offload.
3089 		 */
3090 		uint_t	size;
3091 		uchar_t	*rptr;
3092 
3093 		size = sizeof (dl_capability_req_t) +
3094 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3095 
3096 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3097 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3098 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3099 			    ill->ill_name);
3100 			return;
3101 		}
3102 
3103 		rptr = nmp->b_rptr;
3104 		/* initialize dl_capability_req_t */
3105 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3106 		ocap->dl_sub_offset =
3107 		    sizeof (dl_capability_req_t);
3108 		ocap->dl_sub_length =
3109 		    sizeof (dl_capability_sub_t) +
3110 		    isub->dl_length;
3111 		nmp->b_rptr += sizeof (dl_capability_req_t);
3112 
3113 		/* initialize dl_capability_sub_t */
3114 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3115 		nmp->b_rptr += sizeof (*isub);
3116 
3117 		/* initialize dl_capab_hcksum_t */
3118 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3119 		bcopy(ihck, ohck, sizeof (*ihck));
3120 
3121 		nmp->b_rptr = rptr;
3122 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3123 
3124 		/* Set ENABLE flag */
3125 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3126 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3127 
3128 		/*
3129 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3130 		 * hardware checksum acceleration.
3131 		 */
3132 		ill_dlpi_send(ill, nmp);
3133 	} else
3134 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3135 		    "advertised %x hardware checksum capability flags\n",
3136 		    ill->ill_name, ihck->hcksum_txflags));
3137 }
3138 
3139 static void
3140 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3141 {
3142 	mblk_t *mp;
3143 	dl_capab_hcksum_t *hck_subcap;
3144 	dl_capability_sub_t *dl_subcap;
3145 	int size;
3146 
3147 	if (!(ill->ill_capabilities & ILL_CAPAB_HCKSUM))
3148 		return;
3149 
3150 	ASSERT(ill->ill_hcksum_capab != NULL);
3151 	/*
3152 	 * Clear the capability flag for hardware checksum offload but
3153 	 * retain the ill_hcksum_capab structure since it's possible that
3154 	 * another thread is still referring to it.  The structure only
3155 	 * gets deallocated when we destroy the ill.
3156 	 */
3157 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3158 
3159 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3160 
3161 	mp = allocb(size, BPRI_HI);
3162 	if (mp == NULL) {
3163 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3164 		    "request to disable hardware checksum offload\n"));
3165 		return;
3166 	}
3167 
3168 	mp->b_wptr = mp->b_rptr + size;
3169 
3170 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3171 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3172 	dl_subcap->dl_length = sizeof (*hck_subcap);
3173 
3174 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3175 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3176 	hck_subcap->hcksum_txflags = 0;
3177 
3178 	if (*sc_mp != NULL)
3179 		linkb(*sc_mp, mp);
3180 	else
3181 		*sc_mp = mp;
3182 }
3183 
3184 static void
3185 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3186 {
3187 	mblk_t *nmp = NULL;
3188 	dl_capability_req_t *oc;
3189 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3190 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3191 	uint_t sub_dl_cap = isub->dl_cap;
3192 	uint8_t *capend;
3193 
3194 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3195 
3196 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3197 
3198 	/*
3199 	 * Note: range checks here are not absolutely sufficient to
3200 	 * make us robust against malformed messages sent by drivers;
3201 	 * this is in keeping with the rest of IP's dlpi handling.
3202 	 * (Remember, it's coming from something else in the kernel
3203 	 * address space)
3204 	 */
3205 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3206 	if (capend > mp->b_wptr) {
3207 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3208 		    "malformed sub-capability too long for mblk");
3209 		return;
3210 	}
3211 
3212 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3213 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3214 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3215 		    "unsupported ZEROCOPY sub-capability (version %d, "
3216 		    "expected %d)", zc_ic->zerocopy_version,
3217 		    ZEROCOPY_VERSION_1);
3218 		return;
3219 	}
3220 
3221 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3222 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3223 		    "capability isn't as expected; pass-thru module(s) "
3224 		    "detected, discarding capability\n"));
3225 		return;
3226 	}
3227 
3228 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3229 		if (*ill_zerocopy_capab == NULL) {
3230 			*ill_zerocopy_capab =
3231 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3232 			    KM_NOSLEEP);
3233 
3234 			if (*ill_zerocopy_capab == NULL) {
3235 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3236 				    "could not enable Zero-copy version %d "
3237 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3238 				    ill->ill_name);
3239 				return;
3240 			}
3241 		}
3242 
3243 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3244 		    "supports Zero-copy version %d\n", ill->ill_name,
3245 		    ZEROCOPY_VERSION_1));
3246 
3247 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3248 		    zc_ic->zerocopy_version;
3249 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3250 		    zc_ic->zerocopy_flags;
3251 
3252 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3253 	} else {
3254 		uint_t size;
3255 		uchar_t *rptr;
3256 
3257 		size = sizeof (dl_capability_req_t) +
3258 		    sizeof (dl_capability_sub_t) +
3259 		    sizeof (dl_capab_zerocopy_t);
3260 
3261 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3262 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3263 			    "could not enable zerocopy for %s (ENOMEM)\n",
3264 			    ill->ill_name);
3265 			return;
3266 		}
3267 
3268 		rptr = nmp->b_rptr;
3269 		/* initialize dl_capability_req_t */
3270 		oc = (dl_capability_req_t *)rptr;
3271 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3272 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3273 		    sizeof (dl_capab_zerocopy_t);
3274 		rptr += sizeof (dl_capability_req_t);
3275 
3276 		/* initialize dl_capability_sub_t */
3277 		bcopy(isub, rptr, sizeof (*isub));
3278 		rptr += sizeof (*isub);
3279 
3280 		/* initialize dl_capab_zerocopy_t */
3281 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3282 		*zc_oc = *zc_ic;
3283 
3284 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3285 		    "to enable zero-copy version %d\n", ill->ill_name,
3286 		    ZEROCOPY_VERSION_1));
3287 
3288 		/* set VMSAFE_MEM flag */
3289 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3290 
3291 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3292 		ill_dlpi_send(ill, nmp);
3293 	}
3294 }
3295 
3296 static void
3297 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3298 {
3299 	mblk_t *mp;
3300 	dl_capab_zerocopy_t *zerocopy_subcap;
3301 	dl_capability_sub_t *dl_subcap;
3302 	int size;
3303 
3304 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3305 		return;
3306 
3307 	ASSERT(ill->ill_zerocopy_capab != NULL);
3308 	/*
3309 	 * Clear the capability flag for Zero-copy but retain the
3310 	 * ill_zerocopy_capab structure since it's possible that another
3311 	 * thread is still referring to it.  The structure only gets
3312 	 * deallocated when we destroy the ill.
3313 	 */
3314 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3315 
3316 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3317 
3318 	mp = allocb(size, BPRI_HI);
3319 	if (mp == NULL) {
3320 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3321 		    "request to disable Zero-copy\n"));
3322 		return;
3323 	}
3324 
3325 	mp->b_wptr = mp->b_rptr + size;
3326 
3327 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3328 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3329 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3330 
3331 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3332 	zerocopy_subcap->zerocopy_version =
3333 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3334 	zerocopy_subcap->zerocopy_flags = 0;
3335 
3336 	if (*sc_mp != NULL)
3337 		linkb(*sc_mp, mp);
3338 	else
3339 		*sc_mp = mp;
3340 }
3341 
3342 /*
3343  * Consume a new-style hardware capabilities negotiation ack.
3344  * Called from ip_rput_dlpi_writer().
3345  */
3346 void
3347 ill_capability_ack(ill_t *ill, mblk_t *mp)
3348 {
3349 	dl_capability_ack_t *capp;
3350 	dl_capability_sub_t *subp, *endp;
3351 
3352 	if (ill->ill_capab_state == IDMS_INPROGRESS)
3353 		ill->ill_capab_state = IDMS_OK;
3354 
3355 	capp = (dl_capability_ack_t *)mp->b_rptr;
3356 
3357 	if (capp->dl_sub_length == 0)
3358 		/* no new-style capabilities */
3359 		return;
3360 
3361 	/* make sure the driver supplied correct dl_sub_length */
3362 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3363 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3364 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3365 		return;
3366 	}
3367 
3368 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3369 	/*
3370 	 * There are sub-capabilities. Process the ones we know about.
3371 	 * Loop until we don't have room for another sub-cap header..
3372 	 */
3373 	for (subp = SC(capp, capp->dl_sub_offset),
3374 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3375 	    subp <= endp;
3376 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3377 
3378 		switch (subp->dl_cap) {
3379 		case DL_CAPAB_ID_WRAPPER:
3380 			ill_capability_id_ack(ill, mp, subp);
3381 			break;
3382 		default:
3383 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3384 			break;
3385 		}
3386 	}
3387 #undef SC
3388 }
3389 
3390 /*
3391  * This routine is called to scan the fragmentation reassembly table for
3392  * the specified ILL for any packets that are starting to smell.
3393  * dead_interval is the maximum time in seconds that will be tolerated.  It
3394  * will either be the value specified in ip_g_frag_timeout, or zero if the
3395  * ILL is shutting down and it is time to blow everything off.
3396  *
3397  * It returns the number of seconds (as a time_t) that the next frag timer
3398  * should be scheduled for, 0 meaning that the timer doesn't need to be
3399  * re-started.  Note that the method of calculating next_timeout isn't
3400  * entirely accurate since time will flow between the time we grab
3401  * current_time and the time we schedule the next timeout.  This isn't a
3402  * big problem since this is the timer for sending an ICMP reassembly time
3403  * exceeded messages, and it doesn't have to be exactly accurate.
3404  *
3405  * This function is
3406  * sometimes called as writer, although this is not required.
3407  */
3408 time_t
3409 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3410 {
3411 	ipfb_t	*ipfb;
3412 	ipfb_t	*endp;
3413 	ipf_t	*ipf;
3414 	ipf_t	*ipfnext;
3415 	mblk_t	*mp;
3416 	time_t	current_time = gethrestime_sec();
3417 	time_t	next_timeout = 0;
3418 	uint32_t	hdr_length;
3419 	mblk_t	*send_icmp_head;
3420 	mblk_t	*send_icmp_head_v6;
3421 
3422 	ipfb = ill->ill_frag_hash_tbl;
3423 	if (ipfb == NULL)
3424 		return (B_FALSE);
3425 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3426 	/* Walk the frag hash table. */
3427 	for (; ipfb < endp; ipfb++) {
3428 		send_icmp_head = NULL;
3429 		send_icmp_head_v6 = NULL;
3430 		mutex_enter(&ipfb->ipfb_lock);
3431 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3432 			time_t frag_time = current_time - ipf->ipf_timestamp;
3433 			time_t frag_timeout;
3434 
3435 			if (frag_time < dead_interval) {
3436 				/*
3437 				 * There are some outstanding fragments
3438 				 * that will timeout later.  Make note of
3439 				 * the time so that we can reschedule the
3440 				 * next timeout appropriately.
3441 				 */
3442 				frag_timeout = dead_interval - frag_time;
3443 				if (next_timeout == 0 ||
3444 				    frag_timeout < next_timeout) {
3445 					next_timeout = frag_timeout;
3446 				}
3447 				break;
3448 			}
3449 			/* Time's up.  Get it out of here. */
3450 			hdr_length = ipf->ipf_nf_hdr_len;
3451 			ipfnext = ipf->ipf_hash_next;
3452 			if (ipfnext)
3453 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3454 			*ipf->ipf_ptphn = ipfnext;
3455 			mp = ipf->ipf_mp->b_cont;
3456 			for (; mp; mp = mp->b_cont) {
3457 				/* Extra points for neatness. */
3458 				IP_REASS_SET_START(mp, 0);
3459 				IP_REASS_SET_END(mp, 0);
3460 			}
3461 			mp = ipf->ipf_mp->b_cont;
3462 			ill->ill_frag_count -= ipf->ipf_count;
3463 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3464 			ipfb->ipfb_count -= ipf->ipf_count;
3465 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3466 			ipfb->ipfb_frag_pkts--;
3467 			/*
3468 			 * We do not send any icmp message from here because
3469 			 * we currently are holding the ipfb_lock for this
3470 			 * hash chain. If we try and send any icmp messages
3471 			 * from here we may end up via a put back into ip
3472 			 * trying to get the same lock, causing a recursive
3473 			 * mutex panic. Instead we build a list and send all
3474 			 * the icmp messages after we have dropped the lock.
3475 			 */
3476 			if (ill->ill_isv6) {
3477 				BUMP_MIB(ill->ill_ip6_mib, ipv6ReasmFails);
3478 				if (hdr_length != 0) {
3479 					mp->b_next = send_icmp_head_v6;
3480 					send_icmp_head_v6 = mp;
3481 				} else {
3482 					freemsg(mp);
3483 				}
3484 			} else {
3485 				BUMP_MIB(&ip_mib, ipReasmFails);
3486 				if (hdr_length != 0) {
3487 					mp->b_next = send_icmp_head;
3488 					send_icmp_head = mp;
3489 				} else {
3490 					freemsg(mp);
3491 				}
3492 			}
3493 			freeb(ipf->ipf_mp);
3494 		}
3495 		mutex_exit(&ipfb->ipfb_lock);
3496 		/*
3497 		 * Now need to send any icmp messages that we delayed from
3498 		 * above.
3499 		 */
3500 		while (send_icmp_head_v6 != NULL) {
3501 			mp = send_icmp_head_v6;
3502 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3503 			mp->b_next = NULL;
3504 			icmp_time_exceeded_v6(ill->ill_wq, mp,
3505 			    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE, B_FALSE);
3506 		}
3507 		while (send_icmp_head != NULL) {
3508 			mp = send_icmp_head;
3509 			send_icmp_head = send_icmp_head->b_next;
3510 			mp->b_next = NULL;
3511 			icmp_time_exceeded(ill->ill_wq, mp,
3512 			    ICMP_REASSEMBLY_TIME_EXCEEDED);
3513 		}
3514 	}
3515 	/*
3516 	 * A non-dying ILL will use the return value to decide whether to
3517 	 * restart the frag timer, and for how long.
3518 	 */
3519 	return (next_timeout);
3520 }
3521 
3522 /*
3523  * This routine is called when the approximate count of mblk memory used
3524  * for the specified ILL has exceeded max_count.
3525  */
3526 void
3527 ill_frag_prune(ill_t *ill, uint_t max_count)
3528 {
3529 	ipfb_t	*ipfb;
3530 	ipf_t	*ipf;
3531 	size_t	count;
3532 
3533 	/*
3534 	 * If we are here within ip_min_frag_prune_time msecs remove
3535 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3536 	 * ill_frag_free_num_pkts.
3537 	 */
3538 	mutex_enter(&ill->ill_lock);
3539 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3540 	    (ip_min_frag_prune_time != 0 ?
3541 	    ip_min_frag_prune_time : msec_per_tick)) {
3542 
3543 		ill->ill_frag_free_num_pkts++;
3544 
3545 	} else {
3546 		ill->ill_frag_free_num_pkts = 0;
3547 	}
3548 	ill->ill_last_frag_clean_time = lbolt;
3549 	mutex_exit(&ill->ill_lock);
3550 
3551 	/*
3552 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3553 	 */
3554 	if (ill->ill_frag_free_num_pkts != 0) {
3555 		int ix;
3556 
3557 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3558 			ipfb = &ill->ill_frag_hash_tbl[ix];
3559 			mutex_enter(&ipfb->ipfb_lock);
3560 			if (ipfb->ipfb_ipf != NULL) {
3561 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3562 				    ill->ill_frag_free_num_pkts);
3563 			}
3564 			mutex_exit(&ipfb->ipfb_lock);
3565 		}
3566 	}
3567 	/*
3568 	 * While the reassembly list for this ILL is too big, prune a fragment
3569 	 * queue by age, oldest first.  Note that the per ILL count is
3570 	 * approximate, while the per frag hash bucket counts are accurate.
3571 	 */
3572 	while (ill->ill_frag_count > max_count) {
3573 		int	ix;
3574 		ipfb_t	*oipfb = NULL;
3575 		uint_t	oldest = UINT_MAX;
3576 
3577 		count = 0;
3578 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3579 			ipfb = &ill->ill_frag_hash_tbl[ix];
3580 			mutex_enter(&ipfb->ipfb_lock);
3581 			ipf = ipfb->ipfb_ipf;
3582 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3583 				oldest = ipf->ipf_gen;
3584 				oipfb = ipfb;
3585 			}
3586 			count += ipfb->ipfb_count;
3587 			mutex_exit(&ipfb->ipfb_lock);
3588 		}
3589 		/* Refresh the per ILL count */
3590 		ill->ill_frag_count = count;
3591 		if (oipfb == NULL) {
3592 			ill->ill_frag_count = 0;
3593 			break;
3594 		}
3595 		if (count <= max_count)
3596 			return;	/* Somebody beat us to it, nothing to do */
3597 		mutex_enter(&oipfb->ipfb_lock);
3598 		ipf = oipfb->ipfb_ipf;
3599 		if (ipf != NULL) {
3600 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3601 		}
3602 		mutex_exit(&oipfb->ipfb_lock);
3603 	}
3604 }
3605 
3606 /*
3607  * free 'free_cnt' fragmented packets starting at ipf.
3608  */
3609 void
3610 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3611 {
3612 	size_t	count;
3613 	mblk_t	*mp;
3614 	mblk_t	*tmp;
3615 	ipf_t **ipfp = ipf->ipf_ptphn;
3616 
3617 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3618 	ASSERT(ipfp != NULL);
3619 	ASSERT(ipf != NULL);
3620 
3621 	while (ipf != NULL && free_cnt-- > 0) {
3622 		count = ipf->ipf_count;
3623 		mp = ipf->ipf_mp;
3624 		ipf = ipf->ipf_hash_next;
3625 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3626 			IP_REASS_SET_START(tmp, 0);
3627 			IP_REASS_SET_END(tmp, 0);
3628 		}
3629 		ill->ill_frag_count -= count;
3630 		ASSERT(ipfb->ipfb_count >= count);
3631 		ipfb->ipfb_count -= count;
3632 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3633 		ipfb->ipfb_frag_pkts--;
3634 		freemsg(mp);
3635 		BUMP_MIB(&ip_mib, ipReasmFails);
3636 	}
3637 
3638 	if (ipf)
3639 		ipf->ipf_ptphn = ipfp;
3640 	ipfp[0] = ipf;
3641 }
3642 
3643 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3644 	"obsolete and may be removed in a future release of Solaris.  Use " \
3645 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3646 
3647 /*
3648  * For obsolete per-interface forwarding configuration;
3649  * called in response to ND_GET.
3650  */
3651 /* ARGSUSED */
3652 static int
3653 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3654 {
3655 	ill_t *ill = (ill_t *)cp;
3656 
3657 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3658 
3659 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3660 	return (0);
3661 }
3662 
3663 /*
3664  * For obsolete per-interface forwarding configuration;
3665  * called in response to ND_SET.
3666  */
3667 /* ARGSUSED */
3668 static int
3669 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3670     cred_t *ioc_cr)
3671 {
3672 	long value;
3673 	int retval;
3674 
3675 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3676 
3677 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3678 	    value < 0 || value > 1) {
3679 		return (EINVAL);
3680 	}
3681 
3682 	rw_enter(&ill_g_lock, RW_READER);
3683 	retval = ill_forward_set(q, mp, (value != 0), cp);
3684 	rw_exit(&ill_g_lock);
3685 	return (retval);
3686 }
3687 
3688 /*
3689  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3690  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3691  * up RTS_IFINFO routing socket messages for each interface whose flags we
3692  * change.
3693  */
3694 /* ARGSUSED */
3695 int
3696 ill_forward_set(queue_t *q, mblk_t *mp, boolean_t enable, caddr_t cp)
3697 {
3698 	ill_t *ill = (ill_t *)cp;
3699 	ill_group_t *illgrp;
3700 
3701 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ill_g_lock));
3702 
3703 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3704 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)) ||
3705 	    (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK))
3706 		return (EINVAL);
3707 
3708 	/*
3709 	 * If the ill is in an IPMP group, set the forwarding policy on all
3710 	 * members of the group to the same value.
3711 	 */
3712 	illgrp = ill->ill_group;
3713 	if (illgrp != NULL) {
3714 		ill_t *tmp_ill;
3715 
3716 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3717 		    tmp_ill = tmp_ill->ill_group_next) {
3718 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3719 			    (enable ? "Enabling" : "Disabling"),
3720 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3721 			    tmp_ill->ill_name));
3722 			mutex_enter(&tmp_ill->ill_lock);
3723 			if (enable)
3724 				tmp_ill->ill_flags |= ILLF_ROUTER;
3725 			else
3726 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3727 			mutex_exit(&tmp_ill->ill_lock);
3728 			if (tmp_ill->ill_isv6)
3729 				ill_set_nce_router_flags(tmp_ill, enable);
3730 			/* Notify routing socket listeners of this change. */
3731 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3732 		}
3733 	} else {
3734 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3735 		    (enable ? "Enabling" : "Disabling"),
3736 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3737 		mutex_enter(&ill->ill_lock);
3738 		if (enable)
3739 			ill->ill_flags |= ILLF_ROUTER;
3740 		else
3741 			ill->ill_flags &= ~ILLF_ROUTER;
3742 		mutex_exit(&ill->ill_lock);
3743 		if (ill->ill_isv6)
3744 			ill_set_nce_router_flags(ill, enable);
3745 		/* Notify routing socket listeners of this change. */
3746 		ip_rts_ifmsg(ill->ill_ipif);
3747 	}
3748 
3749 	return (0);
3750 }
3751 
3752 /*
3753  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3754  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3755  * set or clear.
3756  */
3757 static void
3758 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3759 {
3760 	ipif_t *ipif;
3761 	nce_t *nce;
3762 
3763 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3764 		nce = ndp_lookup(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3765 		if (nce != NULL) {
3766 			mutex_enter(&nce->nce_lock);
3767 			if (enable)
3768 				nce->nce_flags |= NCE_F_ISROUTER;
3769 			else
3770 				nce->nce_flags &= ~NCE_F_ISROUTER;
3771 			mutex_exit(&nce->nce_lock);
3772 			NCE_REFRELE(nce);
3773 		}
3774 	}
3775 }
3776 
3777 /*
3778  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3779  * for this ill.  Make sure the v6/v4 question has been answered about this
3780  * ill.  The creation of this ndd variable is only for backwards compatibility.
3781  * The preferred way to control per-interface IP forwarding is through the
3782  * ILLF_ROUTER interface flag.
3783  */
3784 static int
3785 ill_set_ndd_name(ill_t *ill)
3786 {
3787 	char *suffix;
3788 
3789 	ASSERT(IAM_WRITER_ILL(ill));
3790 
3791 	if (ill->ill_isv6)
3792 		suffix = ipv6_forward_suffix;
3793 	else
3794 		suffix = ipv4_forward_suffix;
3795 
3796 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
3797 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
3798 	/*
3799 	 * Copies over the '\0'.
3800 	 * Note that strlen(suffix) is always bounded.
3801 	 */
3802 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
3803 	    strlen(suffix) + 1);
3804 
3805 	/*
3806 	 * Use of the nd table requires holding the reader lock.
3807 	 * Modifying the nd table thru nd_load/nd_unload requires
3808 	 * the writer lock.
3809 	 */
3810 	rw_enter(&ip_g_nd_lock, RW_WRITER);
3811 	if (!nd_load(&ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
3812 	    nd_ill_forward_set, (caddr_t)ill)) {
3813 		/*
3814 		 * If the nd_load failed, it only meant that it could not
3815 		 * allocate a new bunch of room for further NDD expansion.
3816 		 * Because of that, the ill_ndd_name will be set to 0, and
3817 		 * this interface is at the mercy of the global ip_forwarding
3818 		 * variable.
3819 		 */
3820 		rw_exit(&ip_g_nd_lock);
3821 		ill->ill_ndd_name = NULL;
3822 		return (ENOMEM);
3823 	}
3824 	rw_exit(&ip_g_nd_lock);
3825 	return (0);
3826 }
3827 
3828 /*
3829  * Intializes the context structure and returns the first ill in the list
3830  * cuurently start_list and end_list can have values:
3831  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
3832  * IP_V4_G_HEAD		Traverse IPV4 list only.
3833  * IP_V6_G_HEAD		Traverse IPV6 list only.
3834  */
3835 
3836 /*
3837  * We don't check for CONDEMNED ills here. Caller must do that if
3838  * necessary under the ill lock.
3839  */
3840 ill_t *
3841 ill_first(int start_list, int end_list, ill_walk_context_t *ctx)
3842 {
3843 	ill_if_t *ifp;
3844 	ill_t *ill;
3845 	avl_tree_t *avl_tree;
3846 
3847 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
3848 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
3849 
3850 	/*
3851 	 * setup the lists to search
3852 	 */
3853 	if (end_list != MAX_G_HEADS) {
3854 		ctx->ctx_current_list = start_list;
3855 		ctx->ctx_last_list = end_list;
3856 	} else {
3857 		ctx->ctx_last_list = MAX_G_HEADS - 1;
3858 		ctx->ctx_current_list = 0;
3859 	}
3860 
3861 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
3862 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list);
3863 		if (ifp != (ill_if_t *)
3864 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list)) {
3865 			avl_tree = &ifp->illif_avl_by_ppa;
3866 			ill = avl_first(avl_tree);
3867 			/*
3868 			 * ill is guaranteed to be non NULL or ifp should have
3869 			 * not existed.
3870 			 */
3871 			ASSERT(ill != NULL);
3872 			return (ill);
3873 		}
3874 		ctx->ctx_current_list++;
3875 	}
3876 
3877 	return (NULL);
3878 }
3879 
3880 /*
3881  * returns the next ill in the list. ill_first() must have been called
3882  * before calling ill_next() or bad things will happen.
3883  */
3884 
3885 /*
3886  * We don't check for CONDEMNED ills here. Caller must do that if
3887  * necessary under the ill lock.
3888  */
3889 ill_t *
3890 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
3891 {
3892 	ill_if_t *ifp;
3893 	ill_t *ill;
3894 
3895 
3896 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
3897 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
3898 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list));
3899 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
3900 	    AVL_AFTER)) != NULL) {
3901 		return (ill);
3902 	}
3903 
3904 	/* goto next ill_ifp in the list. */
3905 	ifp = lastill->ill_ifptr->illif_next;
3906 
3907 	/* make sure not at end of circular list */
3908 	while (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list)) {
3909 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
3910 			return (NULL);
3911 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list);
3912 	}
3913 
3914 	return (avl_first(&ifp->illif_avl_by_ppa));
3915 }
3916 
3917 /*
3918  * Check interface name for correct format which is name+ppa.
3919  * name can contain characters and digits, the right most digits
3920  * make up the ppa number. use of octal is not allowed, name must contain
3921  * a ppa, return pointer to the start of ppa.
3922  * In case of error return NULL.
3923  */
3924 static char *
3925 ill_get_ppa_ptr(char *name)
3926 {
3927 	int namelen = mi_strlen(name);
3928 
3929 	int len = namelen;
3930 
3931 	name += len;
3932 	while (len > 0) {
3933 		name--;
3934 		if (*name < '0' || *name > '9')
3935 			break;
3936 		len--;
3937 	}
3938 
3939 	/* empty string, all digits, or no trailing digits */
3940 	if (len == 0 || len == (int)namelen)
3941 		return (NULL);
3942 
3943 	name++;
3944 	/* check for attempted use of octal */
3945 	if (*name == '0' && len != (int)namelen - 1)
3946 		return (NULL);
3947 	return (name);
3948 }
3949 
3950 /*
3951  * use avl tree to locate the ill.
3952  */
3953 static ill_t *
3954 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
3955     ipsq_func_t func, int *error)
3956 {
3957 	char *ppa_ptr = NULL;
3958 	int len;
3959 	uint_t ppa;
3960 	ill_t *ill = NULL;
3961 	ill_if_t *ifp;
3962 	int list;
3963 	ipsq_t *ipsq;
3964 
3965 	if (error != NULL)
3966 		*error = 0;
3967 
3968 	/*
3969 	 * get ppa ptr
3970 	 */
3971 	if (isv6)
3972 		list = IP_V6_G_HEAD;
3973 	else
3974 		list = IP_V4_G_HEAD;
3975 
3976 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
3977 		if (error != NULL)
3978 			*error = ENXIO;
3979 		return (NULL);
3980 	}
3981 
3982 	len = ppa_ptr - name + 1;
3983 
3984 	ppa = stoi(&ppa_ptr);
3985 
3986 	ifp = IP_VX_ILL_G_LIST(list);
3987 
3988 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list)) {
3989 		/*
3990 		 * match is done on len - 1 as the name is not null
3991 		 * terminated it contains ppa in addition to the interface
3992 		 * name.
3993 		 */
3994 		if ((ifp->illif_name_len == len) &&
3995 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
3996 			break;
3997 		} else {
3998 			ifp = ifp->illif_next;
3999 		}
4000 	}
4001 
4002 
4003 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list)) {
4004 		/*
4005 		 * Even the interface type does not exist.
4006 		 */
4007 		if (error != NULL)
4008 			*error = ENXIO;
4009 		return (NULL);
4010 	}
4011 
4012 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4013 	if (ill != NULL) {
4014 		/*
4015 		 * The block comment at the start of ipif_down
4016 		 * explains the use of the macros used below
4017 		 */
4018 		GRAB_CONN_LOCK(q);
4019 		mutex_enter(&ill->ill_lock);
4020 		if (ILL_CAN_LOOKUP(ill)) {
4021 			ill_refhold_locked(ill);
4022 			mutex_exit(&ill->ill_lock);
4023 			RELEASE_CONN_LOCK(q);
4024 			return (ill);
4025 		} else if (ILL_CAN_WAIT(ill, q)) {
4026 			ipsq = ill->ill_phyint->phyint_ipsq;
4027 			mutex_enter(&ipsq->ipsq_lock);
4028 			mutex_exit(&ill->ill_lock);
4029 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4030 			mutex_exit(&ipsq->ipsq_lock);
4031 			RELEASE_CONN_LOCK(q);
4032 			*error = EINPROGRESS;
4033 			return (NULL);
4034 		}
4035 		mutex_exit(&ill->ill_lock);
4036 		RELEASE_CONN_LOCK(q);
4037 	}
4038 	if (error != NULL)
4039 		*error = ENXIO;
4040 	return (NULL);
4041 }
4042 
4043 /*
4044  * comparison function for use with avl.
4045  */
4046 static int
4047 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4048 {
4049 	uint_t ppa;
4050 	uint_t ill_ppa;
4051 
4052 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4053 
4054 	ppa = *((uint_t *)ppa_ptr);
4055 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4056 	/*
4057 	 * We want the ill with the lowest ppa to be on the
4058 	 * top.
4059 	 */
4060 	if (ill_ppa < ppa)
4061 		return (1);
4062 	if (ill_ppa > ppa)
4063 		return (-1);
4064 	return (0);
4065 }
4066 
4067 /*
4068  * remove an interface type from the global list.
4069  */
4070 static void
4071 ill_delete_interface_type(ill_if_t *interface)
4072 {
4073 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
4074 
4075 	ASSERT(interface != NULL);
4076 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4077 
4078 	avl_destroy(&interface->illif_avl_by_ppa);
4079 	if (interface->illif_ppa_arena != NULL)
4080 		vmem_destroy(interface->illif_ppa_arena);
4081 
4082 	remque(interface);
4083 
4084 	mi_free(interface);
4085 }
4086 
4087 /*
4088  * remove ill from the global list.
4089  */
4090 static void
4091 ill_glist_delete(ill_t *ill)
4092 {
4093 	if (ill == NULL)
4094 		return;
4095 
4096 	rw_enter(&ill_g_lock, RW_WRITER);
4097 	/*
4098 	 * If the ill was never inserted into the AVL tree
4099 	 * we skip the if branch.
4100 	 */
4101 	if (ill->ill_ifptr != NULL) {
4102 		/*
4103 		 * remove from AVL tree and free ppa number
4104 		 */
4105 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4106 
4107 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4108 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4109 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4110 		}
4111 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4112 			ill_delete_interface_type(ill->ill_ifptr);
4113 		}
4114 
4115 		/*
4116 		 * Indicate ill is no longer in the list.
4117 		 */
4118 		ill->ill_ifptr = NULL;
4119 		ill->ill_name_length = 0;
4120 		ill->ill_name[0] = '\0';
4121 		ill->ill_ppa = UINT_MAX;
4122 	}
4123 	ill_phyint_free(ill);
4124 	rw_exit(&ill_g_lock);
4125 }
4126 
4127 /*
4128  * allocate a ppa, if the number of plumbed interfaces of this type are
4129  * less than ill_no_arena do a linear search to find a unused ppa.
4130  * When the number goes beyond ill_no_arena switch to using an arena.
4131  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4132  * is the return value for an error condition, so allocation starts at one
4133  * and is decremented by one.
4134  */
4135 static int
4136 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4137 {
4138 	ill_t *tmp_ill;
4139 	uint_t start, end;
4140 	int ppa;
4141 
4142 	if (ifp->illif_ppa_arena == NULL &&
4143 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4144 		/*
4145 		 * Create an arena.
4146 		 */
4147 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4148 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4149 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4150 			/* allocate what has already been assigned */
4151 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4152 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4153 		    tmp_ill, AVL_AFTER)) {
4154 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4155 			    1,		/* size */
4156 			    1,		/* align/quantum */
4157 			    0,		/* phase */
4158 			    0,		/* nocross */
4159 		(void *)((uintptr_t)tmp_ill->ill_ppa + 1), /* minaddr */
4160 		(void *)((uintptr_t)tmp_ill->ill_ppa + 2), /* maxaddr */
4161 			    VM_NOSLEEP|VM_FIRSTFIT);
4162 			if (ppa == 0) {
4163 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4164 				    " failed while switching"));
4165 				vmem_destroy(ifp->illif_ppa_arena);
4166 				ifp->illif_ppa_arena = NULL;
4167 				break;
4168 			}
4169 		}
4170 	}
4171 
4172 	if (ifp->illif_ppa_arena != NULL) {
4173 		if (ill->ill_ppa == UINT_MAX) {
4174 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4175 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4176 			if (ppa == 0)
4177 				return (EAGAIN);
4178 			ill->ill_ppa = --ppa;
4179 		} else {
4180 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4181 			    1, 		/* size */
4182 			    1, 		/* align/quantum */
4183 			    0, 		/* phase */
4184 			    0, 		/* nocross */
4185 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4186 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4187 			    VM_NOSLEEP|VM_FIRSTFIT);
4188 			/*
4189 			 * Most likely the allocation failed because
4190 			 * the requested ppa was in use.
4191 			 */
4192 			if (ppa == 0)
4193 				return (EEXIST);
4194 		}
4195 		return (0);
4196 	}
4197 
4198 	/*
4199 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4200 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4201 	 */
4202 	if (ill->ill_ppa == UINT_MAX) {
4203 		end = UINT_MAX - 1;
4204 		start = 0;
4205 	} else {
4206 		end = start = ill->ill_ppa;
4207 	}
4208 
4209 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4210 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4211 		if (start++ >= end) {
4212 			if (ill->ill_ppa == UINT_MAX)
4213 				return (EAGAIN);
4214 			else
4215 				return (EEXIST);
4216 		}
4217 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4218 	}
4219 	ill->ill_ppa = start;
4220 	return (0);
4221 }
4222 
4223 /*
4224  * Insert ill into the list of configured ill's. Once this function completes,
4225  * the ill is globally visible and is available through lookups. More precisely
4226  * this happens after the caller drops the ill_g_lock.
4227  */
4228 static int
4229 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4230 {
4231 	ill_if_t *ill_interface;
4232 	avl_index_t where = 0;
4233 	int error;
4234 	int name_length;
4235 	int index;
4236 	boolean_t check_length = B_FALSE;
4237 
4238 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
4239 
4240 	name_length = mi_strlen(name) + 1;
4241 
4242 	if (isv6)
4243 		index = IP_V6_G_HEAD;
4244 	else
4245 		index = IP_V4_G_HEAD;
4246 
4247 	ill_interface = IP_VX_ILL_G_LIST(index);
4248 	/*
4249 	 * Search for interface type based on name
4250 	 */
4251 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index)) {
4252 		if ((ill_interface->illif_name_len == name_length) &&
4253 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4254 			break;
4255 		}
4256 		ill_interface = ill_interface->illif_next;
4257 	}
4258 
4259 	/*
4260 	 * Interface type not found, create one.
4261 	 */
4262 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index)) {
4263 
4264 		ill_g_head_t ghead;
4265 
4266 		/*
4267 		 * allocate ill_if_t structure
4268 		 */
4269 
4270 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4271 		if (ill_interface == NULL) {
4272 			return (ENOMEM);
4273 		}
4274 
4275 
4276 
4277 		(void) strcpy(ill_interface->illif_name, name);
4278 		ill_interface->illif_name_len = name_length;
4279 
4280 		avl_create(&ill_interface->illif_avl_by_ppa,
4281 		    ill_compare_ppa, sizeof (ill_t),
4282 		    offsetof(struct ill_s, ill_avl_byppa));
4283 
4284 		/*
4285 		 * link the structure in the back to maintain order
4286 		 * of configuration for ifconfig output.
4287 		 */
4288 		ghead = ill_g_heads[index];
4289 		insque(ill_interface, ghead.ill_g_list_tail);
4290 
4291 	}
4292 
4293 	if (ill->ill_ppa == UINT_MAX)
4294 		check_length = B_TRUE;
4295 
4296 	error = ill_alloc_ppa(ill_interface, ill);
4297 	if (error != 0) {
4298 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4299 			ill_delete_interface_type(ill->ill_ifptr);
4300 		return (error);
4301 	}
4302 
4303 	/*
4304 	 * When the ppa is choosen by the system, check that there is
4305 	 * enough space to insert ppa. if a specific ppa was passed in this
4306 	 * check is not required as the interface name passed in will have
4307 	 * the right ppa in it.
4308 	 */
4309 	if (check_length) {
4310 		/*
4311 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4312 		 */
4313 		char buf[sizeof (uint_t) * 3];
4314 
4315 		/*
4316 		 * convert ppa to string to calculate the amount of space
4317 		 * required for it in the name.
4318 		 */
4319 		numtos(ill->ill_ppa, buf);
4320 
4321 		/* Do we have enough space to insert ppa ? */
4322 
4323 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4324 			/* Free ppa and interface type struct */
4325 			if (ill_interface->illif_ppa_arena != NULL) {
4326 				vmem_free(ill_interface->illif_ppa_arena,
4327 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4328 			}
4329 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4330 			    0) {
4331 				ill_delete_interface_type(ill->ill_ifptr);
4332 			}
4333 
4334 			return (EINVAL);
4335 		}
4336 	}
4337 
4338 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4339 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4340 
4341 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4342 	    &where);
4343 	ill->ill_ifptr = ill_interface;
4344 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4345 
4346 	ill_phyint_reinit(ill);
4347 	return (0);
4348 }
4349 
4350 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4351 static boolean_t
4352 ipsq_init(ill_t *ill)
4353 {
4354 	ipsq_t  *ipsq;
4355 
4356 	/* Init the ipsq and impicitly enter as writer */
4357 	ill->ill_phyint->phyint_ipsq =
4358 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4359 	if (ill->ill_phyint->phyint_ipsq == NULL)
4360 		return (B_FALSE);
4361 	ipsq = ill->ill_phyint->phyint_ipsq;
4362 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4363 	ill->ill_phyint->phyint_ipsq_next = NULL;
4364 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4365 	ipsq->ipsq_refs = 1;
4366 	ipsq->ipsq_writer = curthread;
4367 	ipsq->ipsq_reentry_cnt = 1;
4368 #ifdef ILL_DEBUG
4369 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack, IP_STACK_DEPTH);
4370 #endif
4371 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4372 	return (B_TRUE);
4373 }
4374 
4375 /*
4376  * ill_init is called by ip_open when a device control stream is opened.
4377  * It does a few initializations, and shoots a DL_INFO_REQ message down
4378  * to the driver.  The response is later picked up in ip_rput_dlpi and
4379  * used to set up default mechanisms for talking to the driver.  (Always
4380  * called as writer.)
4381  *
4382  * If this function returns error, ip_open will call ip_close which in
4383  * turn will call ill_delete to clean up any memory allocated here that
4384  * is not yet freed.
4385  */
4386 int
4387 ill_init(queue_t *q, ill_t *ill)
4388 {
4389 	int	count;
4390 	dl_info_req_t	*dlir;
4391 	mblk_t	*info_mp;
4392 	uchar_t *frag_ptr;
4393 
4394 	/*
4395 	 * The ill is initialized to zero by mi_alloc*(). In addition
4396 	 * some fields already contain valid values, initialized in
4397 	 * ip_open(), before we reach here.
4398 	 */
4399 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4400 
4401 	ill->ill_rq = q;
4402 	ill->ill_wq = WR(q);
4403 
4404 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4405 	    BPRI_HI);
4406 	if (info_mp == NULL)
4407 		return (ENOMEM);
4408 
4409 	/*
4410 	 * Allocate sufficient space to contain our fragment hash table and
4411 	 * the device name.
4412 	 */
4413 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4414 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4415 	if (frag_ptr == NULL) {
4416 		freemsg(info_mp);
4417 		return (ENOMEM);
4418 	}
4419 	ill->ill_frag_ptr = frag_ptr;
4420 	ill->ill_frag_free_num_pkts = 0;
4421 	ill->ill_last_frag_clean_time = 0;
4422 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4423 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4424 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4425 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4426 		    NULL, MUTEX_DEFAULT, NULL);
4427 	}
4428 
4429 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4430 	if (ill->ill_phyint == NULL) {
4431 		freemsg(info_mp);
4432 		mi_free(frag_ptr);
4433 		return (ENOMEM);
4434 	}
4435 
4436 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4437 	/*
4438 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4439 	 * at this point because of the following reason. If we can't
4440 	 * enter the ipsq at some point and cv_wait, the writer that
4441 	 * wakes us up tries to locate us using the list of all phyints
4442 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4443 	 * If we don't set it now, we risk a missed wakeup.
4444 	 */
4445 	ill->ill_phyint->phyint_illv4 = ill;
4446 	ill->ill_ppa = UINT_MAX;
4447 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4448 
4449 	if (!ipsq_init(ill)) {
4450 		freemsg(info_mp);
4451 		mi_free(frag_ptr);
4452 		mi_free(ill->ill_phyint);
4453 		return (ENOMEM);
4454 	}
4455 
4456 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4457 
4458 
4459 	/* Frag queue limit stuff */
4460 	ill->ill_frag_count = 0;
4461 	ill->ill_ipf_gen = 0;
4462 
4463 	ill->ill_global_timer = INFINITY;
4464 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
4465 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4466 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4467 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4468 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4469 
4470 	/*
4471 	 * Initialize IPv6 configuration variables.  The IP module is always
4472 	 * opened as an IPv4 module.  Instead tracking down the cases where
4473 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4474 	 * here for convenience, this has no effect until the ill is set to do
4475 	 * IPv6.
4476 	 */
4477 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4478 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4479 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4480 	ill->ill_max_buf = ND_MAX_Q;
4481 	ill->ill_refcnt = 0;
4482 
4483 	/* Send down the Info Request to the driver. */
4484 	info_mp->b_datap->db_type = M_PCPROTO;
4485 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4486 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4487 	dlir->dl_primitive = DL_INFO_REQ;
4488 
4489 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4490 
4491 	qprocson(q);
4492 	ill_dlpi_send(ill, info_mp);
4493 
4494 	return (0);
4495 }
4496 
4497 /*
4498  * ill_dls_info
4499  * creates datalink socket info from the device.
4500  */
4501 int
4502 ill_dls_info(struct sockaddr_dl *sdl, ipif_t *ipif)
4503 {
4504 	size_t	length;
4505 	ill_t	*ill = ipif->ipif_ill;
4506 
4507 	sdl->sdl_family = AF_LINK;
4508 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4509 	sdl->sdl_type = ipif->ipif_type;
4510 	(void) ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4511 	length = mi_strlen(sdl->sdl_data);
4512 	ASSERT(length < 256);
4513 	sdl->sdl_nlen = (uchar_t)length;
4514 	sdl->sdl_alen = ill->ill_phys_addr_length;
4515 	mutex_enter(&ill->ill_lock);
4516 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL) {
4517 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[length],
4518 		    ill->ill_phys_addr_length);
4519 	}
4520 	mutex_exit(&ill->ill_lock);
4521 	sdl->sdl_slen = 0;
4522 	return (sizeof (struct sockaddr_dl));
4523 }
4524 
4525 /*
4526  * ill_xarp_info
4527  * creates xarp info from the device.
4528  */
4529 static int
4530 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4531 {
4532 	sdl->sdl_family = AF_LINK;
4533 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4534 	sdl->sdl_type = ill->ill_type;
4535 	(void) ipif_get_name(ill->ill_ipif, sdl->sdl_data,
4536 	    sizeof (sdl->sdl_data));
4537 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4538 	sdl->sdl_alen = ill->ill_phys_addr_length;
4539 	sdl->sdl_slen = 0;
4540 	return (sdl->sdl_nlen);
4541 }
4542 
4543 static int
4544 loopback_kstat_update(kstat_t *ksp, int rw)
4545 {
4546 	kstat_named_t *kn = KSTAT_NAMED_PTR(ksp);
4547 
4548 	if (rw == KSTAT_WRITE)
4549 		return (EACCES);
4550 	kn[0].value.ui32 = loopback_packets;
4551 	kn[1].value.ui32 = loopback_packets;
4552 	return (0);
4553 }
4554 
4555 
4556 /*
4557  * Has ifindex been plumbed already.
4558  */
4559 static boolean_t
4560 phyint_exists(uint_t index)
4561 {
4562 	phyint_t *phyi;
4563 
4564 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
4565 	/*
4566 	 * Indexes are stored in the phyint - a common structure
4567 	 * to both IPv4 and IPv6.
4568 	 */
4569 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
4570 	    (void *) &index, NULL);
4571 	return (phyi != NULL);
4572 }
4573 
4574 /*
4575  * Assign a unique interface index for the phyint.
4576  */
4577 static boolean_t
4578 phyint_assign_ifindex(phyint_t *phyi)
4579 {
4580 	uint_t starting_index;
4581 
4582 	ASSERT(phyi->phyint_ifindex == 0);
4583 	if (!ill_index_wrap) {
4584 		phyi->phyint_ifindex = ill_index++;
4585 		if (ill_index == 0) {
4586 			/* Reached the uint_t limit Next time wrap  */
4587 			ill_index_wrap = B_TRUE;
4588 		}
4589 		return (B_TRUE);
4590 	}
4591 
4592 	/*
4593 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4594 	 * at this point and don't want to call any function that attempts
4595 	 * to get the lock again.
4596 	 */
4597 	starting_index = ill_index++;
4598 	for (; ill_index != starting_index; ill_index++) {
4599 		if (ill_index != 0 && !phyint_exists(ill_index)) {
4600 			/* found unused index - use it */
4601 			phyi->phyint_ifindex = ill_index;
4602 			return (B_TRUE);
4603 		}
4604 	}
4605 
4606 	/*
4607 	 * all interface indicies are inuse.
4608 	 */
4609 	return (B_FALSE);
4610 }
4611 
4612 /*
4613  * Return a pointer to the ill which matches the supplied name.  Note that
4614  * the ill name length includes the null termination character.  (May be
4615  * called as writer.)
4616  * If do_alloc and the interface is "lo0" it will be automatically created.
4617  * Cannot bump up reference on condemned ills. So dup detect can't be done
4618  * using this func.
4619  */
4620 ill_t *
4621 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4622     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc)
4623 {
4624 	ill_t	*ill;
4625 	ipif_t	*ipif;
4626 	kstat_named_t	*kn;
4627 	boolean_t isloopback;
4628 	ipsq_t *old_ipsq;
4629 
4630 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4631 
4632 	rw_enter(&ill_g_lock, RW_READER);
4633 	ill = ill_find_by_name(name, isv6, q, mp, func, error);
4634 	rw_exit(&ill_g_lock);
4635 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4636 		return (ill);
4637 
4638 	/*
4639 	 * Couldn't find it.  Does this happen to be a lookup for the
4640 	 * loopback device and are we allowed to allocate it?
4641 	 */
4642 	if (!isloopback || !do_alloc)
4643 		return (NULL);
4644 
4645 	rw_enter(&ill_g_lock, RW_WRITER);
4646 
4647 	ill = ill_find_by_name(name, isv6, q, mp, func, error);
4648 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4649 		rw_exit(&ill_g_lock);
4650 		return (ill);
4651 	}
4652 
4653 	/* Create the loopback device on demand */
4654 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4655 	    sizeof (ipif_loopback_name), BPRI_MED));
4656 	if (ill == NULL)
4657 		goto done;
4658 
4659 	*ill = ill_null;
4660 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4661 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4662 	if (ill->ill_phyint == NULL)
4663 		goto done;
4664 
4665 	if (isv6)
4666 		ill->ill_phyint->phyint_illv6 = ill;
4667 	else
4668 		ill->ill_phyint->phyint_illv4 = ill;
4669 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4670 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4671 	/* Add room for tcp+ip headers */
4672 	if (isv6) {
4673 		ill->ill_isv6 = B_TRUE;
4674 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4675 		if (!ill_allocate_mibs(ill))
4676 			goto done;
4677 	} else {
4678 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4679 	}
4680 	ill->ill_max_mtu = ill->ill_max_frag;
4681 	/*
4682 	 * ipif_loopback_name can't be pointed at directly because its used
4683 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4684 	 * from the glist, ill_glist_delete() sets the first character of
4685 	 * ill_name to '\0'.
4686 	 */
4687 	ill->ill_name = (char *)ill + sizeof (*ill);
4688 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4689 	ill->ill_name_length = sizeof (ipif_loopback_name);
4690 	/* Set ill_name_set for ill_phyint_reinit to work properly */
4691 
4692 	ill->ill_global_timer = INFINITY;
4693 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
4694 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4695 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4696 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4697 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4698 
4699 	/* No resolver here. */
4700 	ill->ill_net_type = IRE_LOOPBACK;
4701 
4702 	/* Initialize the ipsq */
4703 	if (!ipsq_init(ill))
4704 		goto done;
4705 
4706 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
4707 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
4708 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
4709 #ifdef ILL_DEBUG
4710 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
4711 #endif
4712 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
4713 	if (ipif == NULL)
4714 		goto done;
4715 
4716 	ill->ill_flags = ILLF_MULTICAST;
4717 
4718 	/* Set up default loopback address and mask. */
4719 	if (!isv6) {
4720 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
4721 
4722 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
4723 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4724 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
4725 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4726 		    ipif->ipif_v6subnet);
4727 		ill->ill_flags |= ILLF_IPV4;
4728 	} else {
4729 		ipif->ipif_v6lcl_addr = ipv6_loopback;
4730 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4731 		ipif->ipif_v6net_mask = ipv6_all_ones;
4732 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4733 		    ipif->ipif_v6subnet);
4734 		ill->ill_flags |= ILLF_IPV6;
4735 	}
4736 
4737 	/*
4738 	 * Chain us in at the end of the ill list. hold the ill
4739 	 * before we make it globally visible. 1 for the lookup.
4740 	 */
4741 	ill->ill_refcnt = 0;
4742 	ill_refhold(ill);
4743 
4744 	ill->ill_frag_count = 0;
4745 	ill->ill_frag_free_num_pkts = 0;
4746 	ill->ill_last_frag_clean_time = 0;
4747 
4748 	old_ipsq = ill->ill_phyint->phyint_ipsq;
4749 
4750 	if (ill_glist_insert(ill, "lo", isv6) != 0)
4751 		cmn_err(CE_PANIC, "cannot insert loopback interface");
4752 
4753 	/* Let SCTP know so that it can add this to its list */
4754 	sctp_update_ill(ill, SCTP_ILL_INSERT);
4755 
4756 	/* Let SCTP know about this IPIF, so that it can add it to its list */
4757 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
4758 
4759 	/*
4760 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
4761 	 */
4762 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
4763 		/* Loopback ills aren't in any IPMP group */
4764 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
4765 		ipsq_delete(old_ipsq);
4766 	}
4767 
4768 	/*
4769 	 * Delay this till the ipif is allocated as ipif_allocate
4770 	 * de-references ill_phyint for getting the ifindex. We
4771 	 * can't do this before ipif_allocate because ill_phyint_reinit
4772 	 * -> phyint_assign_ifindex expects ipif to be present.
4773 	 */
4774 	mutex_enter(&ill->ill_phyint->phyint_lock);
4775 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
4776 	mutex_exit(&ill->ill_phyint->phyint_lock);
4777 
4778 	if (loopback_ksp == NULL) {
4779 		/* Export loopback interface statistics */
4780 		loopback_ksp = kstat_create("lo", 0, ipif_loopback_name, "net",
4781 		    KSTAT_TYPE_NAMED, 2, 0);
4782 		if (loopback_ksp != NULL) {
4783 			loopback_ksp->ks_update = loopback_kstat_update;
4784 			kn = KSTAT_NAMED_PTR(loopback_ksp);
4785 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
4786 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
4787 			kstat_install(loopback_ksp);
4788 		}
4789 	}
4790 
4791 	if (error != NULL)
4792 		*error = 0;
4793 	*did_alloc = B_TRUE;
4794 	rw_exit(&ill_g_lock);
4795 	return (ill);
4796 done:
4797 	if (ill != NULL) {
4798 		if (ill->ill_phyint != NULL) {
4799 			ipsq_t	*ipsq;
4800 
4801 			ipsq = ill->ill_phyint->phyint_ipsq;
4802 			if (ipsq != NULL)
4803 				kmem_free(ipsq, sizeof (ipsq_t));
4804 			mi_free(ill->ill_phyint);
4805 		}
4806 		ill_free_mib(ill);
4807 		mi_free(ill);
4808 	}
4809 	rw_exit(&ill_g_lock);
4810 	if (error != NULL)
4811 		*error = ENOMEM;
4812 	return (NULL);
4813 }
4814 
4815 /*
4816  * Return a pointer to the ill which matches the index and IP version type.
4817  */
4818 ill_t *
4819 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
4820     ipsq_func_t func, int *err)
4821 {
4822 	ill_t	*ill;
4823 	ipsq_t  *ipsq;
4824 	phyint_t *phyi;
4825 
4826 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
4827 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
4828 
4829 	if (err != NULL)
4830 		*err = 0;
4831 
4832 	/*
4833 	 * Indexes are stored in the phyint - a common structure
4834 	 * to both IPv4 and IPv6.
4835 	 */
4836 	rw_enter(&ill_g_lock, RW_READER);
4837 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
4838 	    (void *) &index, NULL);
4839 	if (phyi != NULL) {
4840 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
4841 		if (ill != NULL) {
4842 			/*
4843 			 * The block comment at the start of ipif_down
4844 			 * explains the use of the macros used below
4845 			 */
4846 			GRAB_CONN_LOCK(q);
4847 			mutex_enter(&ill->ill_lock);
4848 			if (ILL_CAN_LOOKUP(ill)) {
4849 				ill_refhold_locked(ill);
4850 				mutex_exit(&ill->ill_lock);
4851 				RELEASE_CONN_LOCK(q);
4852 				rw_exit(&ill_g_lock);
4853 				return (ill);
4854 			} else if (ILL_CAN_WAIT(ill, q)) {
4855 				ipsq = ill->ill_phyint->phyint_ipsq;
4856 				mutex_enter(&ipsq->ipsq_lock);
4857 				rw_exit(&ill_g_lock);
4858 				mutex_exit(&ill->ill_lock);
4859 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4860 				mutex_exit(&ipsq->ipsq_lock);
4861 				RELEASE_CONN_LOCK(q);
4862 				*err = EINPROGRESS;
4863 				return (NULL);
4864 			}
4865 			RELEASE_CONN_LOCK(q);
4866 			mutex_exit(&ill->ill_lock);
4867 		}
4868 	}
4869 	rw_exit(&ill_g_lock);
4870 	if (err != NULL)
4871 		*err = ENXIO;
4872 	return (NULL);
4873 }
4874 
4875 /*
4876  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
4877  * that gives a running thread a reference to the ill. This reference must be
4878  * released by the thread when it is done accessing the ill and related
4879  * objects. ill_refcnt can not be used to account for static references
4880  * such as other structures pointing to an ill. Callers must generally
4881  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
4882  * or be sure that the ill is not being deleted or changing state before
4883  * calling the refhold functions. A non-zero ill_refcnt ensures that the
4884  * ill won't change any of its critical state such as address, netmask etc.
4885  */
4886 void
4887 ill_refhold(ill_t *ill)
4888 {
4889 	mutex_enter(&ill->ill_lock);
4890 	ill->ill_refcnt++;
4891 	ILL_TRACE_REF(ill);
4892 	mutex_exit(&ill->ill_lock);
4893 }
4894 
4895 void
4896 ill_refhold_locked(ill_t *ill)
4897 {
4898 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4899 	ill->ill_refcnt++;
4900 	ILL_TRACE_REF(ill);
4901 }
4902 
4903 int
4904 ill_check_and_refhold(ill_t *ill)
4905 {
4906 	mutex_enter(&ill->ill_lock);
4907 	if (ILL_CAN_LOOKUP(ill)) {
4908 		ill_refhold_locked(ill);
4909 		mutex_exit(&ill->ill_lock);
4910 		return (0);
4911 	}
4912 	mutex_exit(&ill->ill_lock);
4913 	return (ILL_LOOKUP_FAILED);
4914 }
4915 
4916 /*
4917  * Must not be called while holding any locks. Otherwise if this is
4918  * the last reference to be released, there is a chance of recursive mutex
4919  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4920  * to restart an ioctl.
4921  */
4922 void
4923 ill_refrele(ill_t *ill)
4924 {
4925 	mutex_enter(&ill->ill_lock);
4926 	ASSERT(ill->ill_refcnt != 0);
4927 	ill->ill_refcnt--;
4928 	ILL_UNTRACE_REF(ill);
4929 	if (ill->ill_refcnt != 0) {
4930 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
4931 		mutex_exit(&ill->ill_lock);
4932 		return;
4933 	}
4934 
4935 	/* Drops the ill_lock */
4936 	ipif_ill_refrele_tail(ill);
4937 }
4938 
4939 /*
4940  * Obtain a weak reference count on the ill. This reference ensures the
4941  * ill won't be freed, but the ill may change any of its critical state
4942  * such as netmask, address etc. Returns an error if the ill has started
4943  * closing.
4944  */
4945 boolean_t
4946 ill_waiter_inc(ill_t *ill)
4947 {
4948 	mutex_enter(&ill->ill_lock);
4949 	if (ill->ill_state_flags & ILL_CONDEMNED) {
4950 		mutex_exit(&ill->ill_lock);
4951 		return (B_FALSE);
4952 	}
4953 	ill->ill_waiters++;
4954 	mutex_exit(&ill->ill_lock);
4955 	return (B_TRUE);
4956 }
4957 
4958 void
4959 ill_waiter_dcr(ill_t *ill)
4960 {
4961 	mutex_enter(&ill->ill_lock);
4962 	ill->ill_waiters--;
4963 	if (ill->ill_waiters == 0)
4964 		cv_broadcast(&ill->ill_cv);
4965 	mutex_exit(&ill->ill_lock);
4966 }
4967 
4968 /*
4969  * Named Dispatch routine to produce a formatted report on all ILLs.
4970  * This report is accessed by using the ndd utility to "get" ND variable
4971  * "ip_ill_status".
4972  */
4973 /* ARGSUSED */
4974 int
4975 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
4976 {
4977 	ill_t		*ill;
4978 	ill_walk_context_t ctx;
4979 
4980 	(void) mi_mpprintf(mp,
4981 	    "ILL      " MI_COL_HDRPAD_STR
4982 	/*   01234567[89ABCDEF] */
4983 	    "rq       " MI_COL_HDRPAD_STR
4984 	/*   01234567[89ABCDEF] */
4985 	    "wq       " MI_COL_HDRPAD_STR
4986 	/*   01234567[89ABCDEF] */
4987 	    "upcnt mxfrg err name");
4988 	/*   12345 12345 123 xxxxxxxx  */
4989 
4990 	rw_enter(&ill_g_lock, RW_READER);
4991 	ill = ILL_START_WALK_ALL(&ctx);
4992 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4993 		(void) mi_mpprintf(mp,
4994 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
4995 		    "%05u %05u %03d %s",
4996 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
4997 		    ill->ill_ipif_up_count,
4998 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
4999 	}
5000 	rw_exit(&ill_g_lock);
5001 
5002 	return (0);
5003 }
5004 
5005 /*
5006  * Named Dispatch routine to produce a formatted report on all IPIFs.
5007  * This report is accessed by using the ndd utility to "get" ND variable
5008  * "ip_ipif_status".
5009  */
5010 /* ARGSUSED */
5011 int
5012 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5013 {
5014 	char	buf1[INET6_ADDRSTRLEN];
5015 	char	buf2[INET6_ADDRSTRLEN];
5016 	char	buf3[INET6_ADDRSTRLEN];
5017 	char	buf4[INET6_ADDRSTRLEN];
5018 	char	buf5[INET6_ADDRSTRLEN];
5019 	char	buf6[INET6_ADDRSTRLEN];
5020 	char	buf[LIFNAMSIZ];
5021 	ill_t	*ill;
5022 	ipif_t	*ipif;
5023 	nv_t	*nvp;
5024 	uint64_t flags;
5025 	zoneid_t zoneid;
5026 	ill_walk_context_t ctx;
5027 
5028 	(void) mi_mpprintf(mp,
5029 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5030 	    "\tlocal address\n"
5031 	    "\tsrc address\n"
5032 	    "\tsubnet\n"
5033 	    "\tmask\n"
5034 	    "\tbroadcast\n"
5035 	    "\tp-p-dst");
5036 
5037 	ASSERT(q->q_next == NULL);
5038 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5039 
5040 	rw_enter(&ill_g_lock, RW_READER);
5041 	ill = ILL_START_WALK_ALL(&ctx);
5042 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5043 		for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
5044 			if (zoneid != GLOBAL_ZONEID &&
5045 			    zoneid != ipif->ipif_zoneid)
5046 				continue;
5047 			(void) mi_mpprintf(mp,
5048 			    MI_COL_PTRFMT_STR
5049 			    "%04u %05u %u/%u/%u %s %d",
5050 			    (void *)ipif,
5051 			    ipif->ipif_metric, ipif->ipif_mtu,
5052 			    ipif->ipif_ib_pkt_count,
5053 			    ipif->ipif_ob_pkt_count,
5054 			    ipif->ipif_fo_pkt_count,
5055 			    ipif_get_name(ipif, buf, sizeof (buf)),
5056 			    ipif->ipif_zoneid);
5057 
5058 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5059 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5060 
5061 		/* Tack on text strings for any flags. */
5062 		nvp = ipif_nv_tbl;
5063 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5064 			if (nvp->nv_value & flags)
5065 				(void) mi_mpprintf_nr(mp, " %s",
5066 				    nvp->nv_name);
5067 		}
5068 		(void) mi_mpprintf(mp,
5069 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5070 		    inet_ntop(AF_INET6,
5071 			&ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5072 		    inet_ntop(AF_INET6,
5073 			&ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5074 		    inet_ntop(AF_INET6,
5075 			&ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5076 		    inet_ntop(AF_INET6,
5077 			&ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5078 		    inet_ntop(AF_INET6,
5079 			&ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5080 		    inet_ntop(AF_INET6,
5081 			&ipif->ipif_v6pp_dst_addr,
5082 			buf6, sizeof (buf6)));
5083 		}
5084 	}
5085 	rw_exit(&ill_g_lock);
5086 	return (0);
5087 }
5088 
5089 /*
5090  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5091  * driver.  We construct best guess defaults for lower level information that
5092  * we need.  If an interface is brought up without injection of any overriding
5093  * information from outside, we have to be ready to go with these defaults.
5094  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5095  * we primarely want the dl_provider_style.
5096  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5097  * at which point we assume the other part of the information is valid.
5098  */
5099 void
5100 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5101 {
5102 	uchar_t		*brdcst_addr;
5103 	uint_t		brdcst_addr_length, phys_addr_length;
5104 	t_scalar_t	sap_length;
5105 	dl_info_ack_t	*dlia;
5106 	ip_m_t		*ipm;
5107 	dl_qos_cl_sel1_t *sel1;
5108 
5109 	ASSERT(IAM_WRITER_ILL(ill));
5110 
5111 	/*
5112 	 * Till the ill is fully up ILL_CHANGING will be set and
5113 	 * the ill is not globally visible. So no need for a lock.
5114 	 */
5115 	dlia = (dl_info_ack_t *)mp->b_rptr;
5116 	ill->ill_mactype = dlia->dl_mac_type;
5117 
5118 	ipm = ip_m_lookup(dlia->dl_mac_type);
5119 	if (ipm == NULL) {
5120 		ipm = ip_m_lookup(DL_OTHER);
5121 		ASSERT(ipm != NULL);
5122 	}
5123 	ill->ill_media = ipm;
5124 
5125 	/*
5126 	 * When the new DLPI stuff is ready we'll pull lengths
5127 	 * from dlia.
5128 	 */
5129 	if (dlia->dl_version == DL_VERSION_2) {
5130 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5131 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5132 		    brdcst_addr_length);
5133 		if (brdcst_addr == NULL) {
5134 			brdcst_addr_length = 0;
5135 		}
5136 		sap_length = dlia->dl_sap_length;
5137 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5138 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5139 		    brdcst_addr_length, sap_length, phys_addr_length));
5140 	} else {
5141 		brdcst_addr_length = 6;
5142 		brdcst_addr = ip_six_byte_all_ones;
5143 		sap_length = -2;
5144 		phys_addr_length = brdcst_addr_length;
5145 	}
5146 
5147 	ill->ill_bcast_addr_length = brdcst_addr_length;
5148 	ill->ill_phys_addr_length = phys_addr_length;
5149 	ill->ill_sap_length = sap_length;
5150 	ill->ill_max_frag = dlia->dl_max_sdu;
5151 	ill->ill_max_mtu = ill->ill_max_frag;
5152 
5153 	ill->ill_type = ipm->ip_m_type;
5154 
5155 	if (!ill->ill_dlpi_style_set) {
5156 		if (dlia->dl_provider_style == DL_STYLE2)
5157 			ill->ill_needs_attach = 1;
5158 
5159 		/*
5160 		 * Allocate the first ipif on this ill. We don't delay it
5161 		 * further as ioctl handling assumes atleast one ipif to
5162 		 * be present.
5163 		 *
5164 		 * At this point we don't know whether the ill is v4 or v6.
5165 		 * We will know this whan the SIOCSLIFNAME happens and
5166 		 * the correct value for ill_isv6 will be assigned in
5167 		 * ipif_set_values(). We need to hold the ill lock and
5168 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5169 		 * the wakeup.
5170 		 */
5171 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5172 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5173 		mutex_enter(&ill->ill_lock);
5174 		ASSERT(ill->ill_dlpi_style_set == 0);
5175 		ill->ill_dlpi_style_set = 1;
5176 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5177 		cv_broadcast(&ill->ill_cv);
5178 		mutex_exit(&ill->ill_lock);
5179 		freemsg(mp);
5180 		return;
5181 	}
5182 	ASSERT(ill->ill_ipif != NULL);
5183 	/*
5184 	 * We know whether it is IPv4 or IPv6 now, as this is the
5185 	 * second DL_INFO_ACK we are recieving in response to the
5186 	 * DL_INFO_REQ sent in ipif_set_values.
5187 	 */
5188 	if (ill->ill_isv6)
5189 		ill->ill_sap = IP6_DL_SAP;
5190 	else
5191 		ill->ill_sap = IP_DL_SAP;
5192 	/*
5193 	 * Set ipif_mtu which is used to set the IRE's
5194 	 * ire_max_frag value. The driver could have sent
5195 	 * a different mtu from what it sent last time. No
5196 	 * need to call ipif_mtu_change because IREs have
5197 	 * not yet been created.
5198 	 */
5199 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5200 	/*
5201 	 * Clear all the flags that were set based on ill_bcast_addr_length
5202 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5203 	 * changed now and we need to re-evaluate.
5204 	 */
5205 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5206 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5207 
5208 	/*
5209 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5210 	 * changed now.
5211 	 */
5212 	if (ill->ill_bcast_addr_length == 0) {
5213 		if (ill->ill_resolver_mp != NULL)
5214 			freemsg(ill->ill_resolver_mp);
5215 		if (ill->ill_bcast_mp != NULL)
5216 			freemsg(ill->ill_bcast_mp);
5217 		if (ill->ill_flags & ILLF_XRESOLV)
5218 			ill->ill_net_type = IRE_IF_RESOLVER;
5219 		else
5220 			ill->ill_net_type = IRE_IF_NORESOLVER;
5221 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5222 		    ill->ill_phys_addr_length,
5223 		    ill->ill_sap,
5224 		    ill->ill_sap_length);
5225 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5226 
5227 		if (ill->ill_isv6)
5228 			/*
5229 			 * Note: xresolv interfaces will eventually need NOARP
5230 			 * set here as well, but that will require those
5231 			 * external resolvers to have some knowledge of
5232 			 * that flag and act appropriately. Not to be changed
5233 			 * at present.
5234 			 */
5235 			ill->ill_flags |= ILLF_NONUD;
5236 		else
5237 			ill->ill_flags |= ILLF_NOARP;
5238 
5239 		if (ill->ill_phys_addr_length == 0) {
5240 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5241 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5242 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5243 			} else {
5244 				/* pt-pt supports multicast. */
5245 				ill->ill_flags |= ILLF_MULTICAST;
5246 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5247 			}
5248 		}
5249 	} else {
5250 		ill->ill_net_type = IRE_IF_RESOLVER;
5251 		if (ill->ill_bcast_mp != NULL)
5252 			freemsg(ill->ill_bcast_mp);
5253 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5254 		    ill->ill_bcast_addr_length, ill->ill_sap,
5255 		    ill->ill_sap_length);
5256 		/*
5257 		 * Later detect lack of DLPI driver multicast
5258 		 * capability by catching DL_ENABMULTI errors in
5259 		 * ip_rput_dlpi.
5260 		 */
5261 		ill->ill_flags |= ILLF_MULTICAST;
5262 		if (!ill->ill_isv6)
5263 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5264 	}
5265 	/* By default an interface does not support any CoS marking */
5266 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5267 
5268 	/*
5269 	 * If we get QoS information in DL_INFO_ACK, the device supports
5270 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5271 	 */
5272 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5273 	    dlia->dl_qos_length);
5274 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5275 		ill->ill_flags |= ILLF_COS_ENABLED;
5276 	}
5277 
5278 	/* Clear any previous error indication. */
5279 	ill->ill_error = 0;
5280 	freemsg(mp);
5281 }
5282 
5283 /*
5284  * Perform various checks to verify that an address would make sense as a
5285  * local, remote, or subnet interface address.
5286  */
5287 static boolean_t
5288 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5289 {
5290 	ipaddr_t	net_mask;
5291 
5292 	/*
5293 	 * Don't allow all zeroes, all ones or experimental address, but allow
5294 	 * all ones netmask.
5295 	 */
5296 	if ((net_mask = ip_net_mask(addr)) == 0)
5297 		return (B_FALSE);
5298 	/* A given netmask overrides the "guess" netmask */
5299 	if (subnet_mask != 0)
5300 		net_mask = subnet_mask;
5301 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5302 	    (addr == (addr | ~net_mask)))) {
5303 		return (B_FALSE);
5304 	}
5305 	if (CLASSD(addr))
5306 		return (B_FALSE);
5307 
5308 	return (B_TRUE);
5309 }
5310 
5311 /*
5312  * ipif_lookup_group
5313  * Returns held ipif
5314  */
5315 ipif_t *
5316 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid)
5317 {
5318 	ire_t	*ire;
5319 	ipif_t	*ipif;
5320 
5321 	ire = ire_lookup_multi(group, zoneid);
5322 	if (ire == NULL)
5323 		return (NULL);
5324 	ipif = ire->ire_ipif;
5325 	ipif_refhold(ipif);
5326 	ire_refrele(ire);
5327 	return (ipif);
5328 }
5329 
5330 /*
5331  * Look for an ipif with the specified interface address and destination.
5332  * The destination address is used only for matching point-to-point interfaces.
5333  */
5334 ipif_t *
5335 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5336     ipsq_func_t func, int *error)
5337 {
5338 	ipif_t	*ipif;
5339 	ill_t	*ill;
5340 	ill_walk_context_t ctx;
5341 	ipsq_t	*ipsq;
5342 
5343 	if (error != NULL)
5344 		*error = 0;
5345 
5346 	/*
5347 	 * First match all the point-to-point interfaces
5348 	 * before looking at non-point-to-point interfaces.
5349 	 * This is done to avoid returning non-point-to-point
5350 	 * ipif instead of unnumbered point-to-point ipif.
5351 	 */
5352 	rw_enter(&ill_g_lock, RW_READER);
5353 	ill = ILL_START_WALK_V4(&ctx);
5354 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5355 		GRAB_CONN_LOCK(q);
5356 		mutex_enter(&ill->ill_lock);
5357 		for (ipif = ill->ill_ipif; ipif != NULL;
5358 		    ipif = ipif->ipif_next) {
5359 			/* Allow the ipif to be down */
5360 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5361 			    (ipif->ipif_lcl_addr == if_addr) &&
5362 			    (ipif->ipif_pp_dst_addr == dst)) {
5363 				/*
5364 				 * The block comment at the start of ipif_down
5365 				 * explains the use of the macros used below
5366 				 */
5367 				if (IPIF_CAN_LOOKUP(ipif)) {
5368 					ipif_refhold_locked(ipif);
5369 					mutex_exit(&ill->ill_lock);
5370 					RELEASE_CONN_LOCK(q);
5371 					rw_exit(&ill_g_lock);
5372 					return (ipif);
5373 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5374 					ipsq = ill->ill_phyint->phyint_ipsq;
5375 					mutex_enter(&ipsq->ipsq_lock);
5376 					mutex_exit(&ill->ill_lock);
5377 					rw_exit(&ill_g_lock);
5378 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5379 						ill);
5380 					mutex_exit(&ipsq->ipsq_lock);
5381 					RELEASE_CONN_LOCK(q);
5382 					*error = EINPROGRESS;
5383 					return (NULL);
5384 				}
5385 			}
5386 		}
5387 		mutex_exit(&ill->ill_lock);
5388 		RELEASE_CONN_LOCK(q);
5389 	}
5390 	rw_exit(&ill_g_lock);
5391 
5392 	/* lookup the ipif based on interface address */
5393 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error);
5394 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5395 	return (ipif);
5396 }
5397 
5398 /*
5399  * Look for an ipif with the specified address. For point-point links
5400  * we look for matches on either the destination address and the local
5401  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5402  * is set.
5403  * Matches on a specific ill if match_ill is set.
5404  */
5405 ipif_t *
5406 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5407     mblk_t *mp, ipsq_func_t func, int *error)
5408 {
5409 	ipif_t  *ipif;
5410 	ill_t   *ill;
5411 	boolean_t ptp = B_FALSE;
5412 	ipsq_t	*ipsq;
5413 	ill_walk_context_t	ctx;
5414 
5415 	if (error != NULL)
5416 		*error = 0;
5417 
5418 	rw_enter(&ill_g_lock, RW_READER);
5419 	/*
5420 	 * Repeat twice, first based on local addresses and
5421 	 * next time for pointopoint.
5422 	 */
5423 repeat:
5424 	ill = ILL_START_WALK_V4(&ctx);
5425 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5426 		if (match_ill != NULL && ill != match_ill) {
5427 			continue;
5428 		}
5429 		GRAB_CONN_LOCK(q);
5430 		mutex_enter(&ill->ill_lock);
5431 		for (ipif = ill->ill_ipif; ipif != NULL;
5432 		    ipif = ipif->ipif_next) {
5433 			if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid)
5434 				continue;
5435 			/* Allow the ipif to be down */
5436 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5437 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5438 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5439 			    (ipif->ipif_pp_dst_addr == addr))) {
5440 				/*
5441 				 * The block comment at the start of ipif_down
5442 				 * explains the use of the macros used below
5443 				 */
5444 				if (IPIF_CAN_LOOKUP(ipif)) {
5445 					ipif_refhold_locked(ipif);
5446 					mutex_exit(&ill->ill_lock);
5447 					RELEASE_CONN_LOCK(q);
5448 					rw_exit(&ill_g_lock);
5449 					return (ipif);
5450 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5451 					ipsq = ill->ill_phyint->phyint_ipsq;
5452 					mutex_enter(&ipsq->ipsq_lock);
5453 					mutex_exit(&ill->ill_lock);
5454 					rw_exit(&ill_g_lock);
5455 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5456 						ill);
5457 					mutex_exit(&ipsq->ipsq_lock);
5458 					RELEASE_CONN_LOCK(q);
5459 					*error = EINPROGRESS;
5460 					return (NULL);
5461 				}
5462 			}
5463 		}
5464 		mutex_exit(&ill->ill_lock);
5465 		RELEASE_CONN_LOCK(q);
5466 	}
5467 
5468 	/* Now try the ptp case */
5469 	if (ptp) {
5470 		rw_exit(&ill_g_lock);
5471 		if (error != NULL)
5472 			*error = ENXIO;
5473 		return (NULL);
5474 	}
5475 	ptp = B_TRUE;
5476 	goto repeat;
5477 }
5478 
5479 /*
5480  * Look for an ipif that matches the specified remote address i.e. the
5481  * ipif that would receive the specified packet.
5482  * First look for directly connected interfaces and then do a recursive
5483  * IRE lookup and pick the first ipif corresponding to the source address in the
5484  * ire.
5485  * Returns: held ipif
5486  */
5487 ipif_t *
5488 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
5489 {
5490 	ipif_t	*ipif;
5491 	ire_t	*ire;
5492 
5493 	ASSERT(!ill->ill_isv6);
5494 
5495 	/*
5496 	 * Someone could be changing this ipif currently or change it
5497 	 * after we return this. Thus  a few packets could use the old
5498 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
5499 	 * will atomically be updated or cleaned up with the new value
5500 	 * Thus we don't need a lock to check the flags or other attrs below.
5501 	 */
5502 	mutex_enter(&ill->ill_lock);
5503 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5504 		if (!IPIF_CAN_LOOKUP(ipif))
5505 			continue;
5506 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid)
5507 			continue;
5508 		/* Allow the ipif to be down */
5509 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
5510 			if ((ipif->ipif_pp_dst_addr == addr) ||
5511 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
5512 			    ipif->ipif_lcl_addr == addr)) {
5513 				ipif_refhold_locked(ipif);
5514 				mutex_exit(&ill->ill_lock);
5515 				return (ipif);
5516 			}
5517 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
5518 			ipif_refhold_locked(ipif);
5519 			mutex_exit(&ill->ill_lock);
5520 			return (ipif);
5521 		}
5522 	}
5523 	mutex_exit(&ill->ill_lock);
5524 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
5525 	    MATCH_IRE_RECURSIVE);
5526 	if (ire != NULL) {
5527 		/*
5528 		 * The callers of this function wants to know the
5529 		 * interface on which they have to send the replies
5530 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
5531 		 * derived from different ills, we really don't care
5532 		 * what we return here.
5533 		 */
5534 		ipif = ire->ire_ipif;
5535 		if (ipif != NULL) {
5536 			ipif_refhold(ipif);
5537 			ire_refrele(ire);
5538 			return (ipif);
5539 		}
5540 		ire_refrele(ire);
5541 	}
5542 	/* Pick the first interface */
5543 	ipif = ipif_get_next_ipif(NULL, ill);
5544 	return (ipif);
5545 }
5546 
5547 /*
5548  * This func does not prevent refcnt from increasing. But if
5549  * the caller has taken steps to that effect, then this func
5550  * can be used to determine whether the ill has become quiescent
5551  */
5552 boolean_t
5553 ill_is_quiescent(ill_t *ill)
5554 {
5555 	ipif_t	*ipif;
5556 
5557 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5558 
5559 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5560 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0)
5561 			return (B_FALSE);
5562 	}
5563 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
5564 	    ill->ill_nce_cnt != 0 || ill->ill_srcif_refcnt != 0 ||
5565 	    ill->ill_mrtun_refcnt != 0)
5566 		return (B_FALSE);
5567 	return (B_TRUE);
5568 }
5569 
5570 /*
5571  * This func does not prevent refcnt from increasing. But if
5572  * the caller has taken steps to that effect, then this func
5573  * can be used to determine whether the ipif has become quiescent
5574  */
5575 static boolean_t
5576 ipif_is_quiescent(ipif_t *ipif)
5577 {
5578 	ill_t *ill;
5579 
5580 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5581 
5582 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0)
5583 		return (B_FALSE);
5584 
5585 	ill = ipif->ipif_ill;
5586 	if (ill->ill_ipif_up_count != 0 || ill->ill_logical_down)
5587 		return (B_TRUE);
5588 
5589 	/* This is the last ipif going down or being deleted on this ill */
5590 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0)
5591 		return (B_FALSE);
5592 
5593 	return (B_TRUE);
5594 }
5595 
5596 /*
5597  * This func does not prevent refcnt from increasing. But if
5598  * the caller has taken steps to that effect, then this func
5599  * can be used to determine whether the ipifs marked with IPIF_MOVING
5600  * have become quiescent and can be moved in a failover/failback.
5601  */
5602 static ipif_t *
5603 ill_quiescent_to_move(ill_t *ill)
5604 {
5605 	ipif_t  *ipif;
5606 
5607 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5608 
5609 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5610 		if (ipif->ipif_state_flags & IPIF_MOVING) {
5611 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
5612 				return (ipif);
5613 			}
5614 		}
5615 	}
5616 	return (NULL);
5617 }
5618 
5619 /*
5620  * The ipif/ill/ire has been refreled. Do the tail processing.
5621  * Determine if the ipif or ill in question has become quiescent and if so
5622  * wakeup close and/or restart any queued pending ioctl that is waiting
5623  * for the ipif_down (or ill_down)
5624  */
5625 void
5626 ipif_ill_refrele_tail(ill_t *ill)
5627 {
5628 	mblk_t	*mp;
5629 	conn_t	*connp;
5630 	ipsq_t	*ipsq;
5631 	ipif_t	*ipif;
5632 
5633 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5634 
5635 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
5636 	    ill_is_quiescent(ill)) {
5637 		/* ill_close may be waiting */
5638 		cv_broadcast(&ill->ill_cv);
5639 	}
5640 
5641 	/* ipsq can't change because ill_lock  is held */
5642 	ipsq = ill->ill_phyint->phyint_ipsq;
5643 	if (ipsq->ipsq_waitfor == 0) {
5644 		/* Not waiting for anything, just return. */
5645 		mutex_exit(&ill->ill_lock);
5646 		return;
5647 	}
5648 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
5649 		ipsq->ipsq_pending_ipif != NULL);
5650 	/*
5651 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
5652 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
5653 	 * be zero for restarting an ioctl that ends up downing the ill.
5654 	 */
5655 	ipif = ipsq->ipsq_pending_ipif;
5656 	if (ipif->ipif_ill != ill) {
5657 		/* The ioctl is pending on some other ill. */
5658 		mutex_exit(&ill->ill_lock);
5659 		return;
5660 	}
5661 
5662 	switch (ipsq->ipsq_waitfor) {
5663 	case IPIF_DOWN:
5664 	case IPIF_FREE:
5665 		if (!ipif_is_quiescent(ipif)) {
5666 			mutex_exit(&ill->ill_lock);
5667 			return;
5668 		}
5669 		break;
5670 
5671 	case ILL_DOWN:
5672 	case ILL_FREE:
5673 		/*
5674 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
5675 		 * waits synchronously in ip_close, and no message is queued in
5676 		 * ipsq_pending_mp at all in this case
5677 		 */
5678 		if (!ill_is_quiescent(ill)) {
5679 			mutex_exit(&ill->ill_lock);
5680 			return;
5681 		}
5682 
5683 		break;
5684 
5685 	case ILL_MOVE_OK:
5686 		if (ill_quiescent_to_move(ill) != NULL) {
5687 			mutex_exit(&ill->ill_lock);
5688 			return;
5689 		}
5690 
5691 		break;
5692 	default:
5693 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
5694 		    (void *)ipsq, ipsq->ipsq_waitfor);
5695 	}
5696 
5697 	/*
5698 	 * Incr refcnt for the qwriter_ip call below which
5699 	 * does a refrele
5700 	 */
5701 	ill_refhold_locked(ill);
5702 	mutex_exit(&ill->ill_lock);
5703 
5704 	mp = ipsq_pending_mp_get(ipsq, &connp);
5705 	ASSERT(mp != NULL);
5706 
5707 	switch (mp->b_datap->db_type) {
5708 	case M_ERROR:
5709 	case M_HANGUP:
5710 		(void) qwriter_ip(NULL, ill, ill->ill_rq, mp,
5711 		    ipif_all_down_tail, CUR_OP, B_TRUE);
5712 		return;
5713 
5714 	case M_IOCTL:
5715 	case M_IOCDATA:
5716 		(void) qwriter_ip(NULL, ill,
5717 		    (connp != NULL ? CONNP_TO_WQ(connp) : ill->ill_wq), mp,
5718 		    ip_reprocess_ioctl, CUR_OP, B_TRUE);
5719 		return;
5720 
5721 	default:
5722 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
5723 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
5724 	}
5725 }
5726 
5727 #ifdef ILL_DEBUG
5728 /* Reuse trace buffer from beginning (if reached the end) and record trace */
5729 void
5730 th_trace_rrecord(th_trace_t *th_trace)
5731 {
5732 	tr_buf_t *tr_buf;
5733 	uint_t lastref;
5734 
5735 	lastref = th_trace->th_trace_lastref;
5736 	lastref++;
5737 	if (lastref == TR_BUF_MAX)
5738 		lastref = 0;
5739 	th_trace->th_trace_lastref = lastref;
5740 	tr_buf = &th_trace->th_trbuf[lastref];
5741 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, IP_STACK_DEPTH);
5742 }
5743 
5744 th_trace_t *
5745 th_trace_ipif_lookup(ipif_t *ipif)
5746 {
5747 	int bucket_id;
5748 	th_trace_t *th_trace;
5749 
5750 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5751 
5752 	bucket_id = IP_TR_HASH(curthread);
5753 	ASSERT(bucket_id < IP_TR_HASH_MAX);
5754 
5755 	for (th_trace = ipif->ipif_trace[bucket_id]; th_trace != NULL;
5756 	    th_trace = th_trace->th_next) {
5757 		if (th_trace->th_id == curthread)
5758 			return (th_trace);
5759 	}
5760 	return (NULL);
5761 }
5762 
5763 void
5764 ipif_trace_ref(ipif_t *ipif)
5765 {
5766 	int bucket_id;
5767 	th_trace_t *th_trace;
5768 
5769 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5770 
5771 	if (ipif->ipif_trace_disable)
5772 		return;
5773 
5774 	/*
5775 	 * Attempt to locate the trace buffer for the curthread.
5776 	 * If it does not exist, then allocate a new trace buffer
5777 	 * and link it in list of trace bufs for this ipif, at the head
5778 	 */
5779 	th_trace = th_trace_ipif_lookup(ipif);
5780 	if (th_trace == NULL) {
5781 		bucket_id = IP_TR_HASH(curthread);
5782 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
5783 		    KM_NOSLEEP);
5784 		if (th_trace == NULL) {
5785 			ipif->ipif_trace_disable = B_TRUE;
5786 			ipif_trace_cleanup(ipif);
5787 			return;
5788 		}
5789 		th_trace->th_id = curthread;
5790 		th_trace->th_next = ipif->ipif_trace[bucket_id];
5791 		th_trace->th_prev = &ipif->ipif_trace[bucket_id];
5792 		if (th_trace->th_next != NULL)
5793 			th_trace->th_next->th_prev = &th_trace->th_next;
5794 		ipif->ipif_trace[bucket_id] = th_trace;
5795 	}
5796 	ASSERT(th_trace->th_refcnt >= 0 &&
5797 		th_trace->th_refcnt < TR_BUF_MAX -1);
5798 	th_trace->th_refcnt++;
5799 	th_trace_rrecord(th_trace);
5800 }
5801 
5802 void
5803 ipif_untrace_ref(ipif_t *ipif)
5804 {
5805 	th_trace_t *th_trace;
5806 
5807 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5808 
5809 	if (ipif->ipif_trace_disable)
5810 		return;
5811 	th_trace = th_trace_ipif_lookup(ipif);
5812 	ASSERT(th_trace != NULL);
5813 	ASSERT(th_trace->th_refcnt > 0);
5814 
5815 	th_trace->th_refcnt--;
5816 	th_trace_rrecord(th_trace);
5817 }
5818 
5819 th_trace_t *
5820 th_trace_ill_lookup(ill_t *ill)
5821 {
5822 	th_trace_t *th_trace;
5823 	int bucket_id;
5824 
5825 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5826 
5827 	bucket_id = IP_TR_HASH(curthread);
5828 	ASSERT(bucket_id < IP_TR_HASH_MAX);
5829 
5830 	for (th_trace = ill->ill_trace[bucket_id]; th_trace != NULL;
5831 	    th_trace = th_trace->th_next) {
5832 		if (th_trace->th_id == curthread)
5833 			return (th_trace);
5834 	}
5835 	return (NULL);
5836 }
5837 
5838 void
5839 ill_trace_ref(ill_t *ill)
5840 {
5841 	int bucket_id;
5842 	th_trace_t *th_trace;
5843 
5844 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5845 	if (ill->ill_trace_disable)
5846 		return;
5847 	/*
5848 	 * Attempt to locate the trace buffer for the curthread.
5849 	 * If it does not exist, then allocate a new trace buffer
5850 	 * and link it in list of trace bufs for this ill, at the head
5851 	 */
5852 	th_trace = th_trace_ill_lookup(ill);
5853 	if (th_trace == NULL) {
5854 		bucket_id = IP_TR_HASH(curthread);
5855 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
5856 		    KM_NOSLEEP);
5857 		if (th_trace == NULL) {
5858 			ill->ill_trace_disable = B_TRUE;
5859 			ill_trace_cleanup(ill);
5860 			return;
5861 		}
5862 		th_trace->th_id = curthread;
5863 		th_trace->th_next = ill->ill_trace[bucket_id];
5864 		th_trace->th_prev = &ill->ill_trace[bucket_id];
5865 		if (th_trace->th_next != NULL)
5866 			th_trace->th_next->th_prev = &th_trace->th_next;
5867 		ill->ill_trace[bucket_id] = th_trace;
5868 	}
5869 	ASSERT(th_trace->th_refcnt >= 0 &&
5870 		th_trace->th_refcnt < TR_BUF_MAX - 1);
5871 
5872 	th_trace->th_refcnt++;
5873 	th_trace_rrecord(th_trace);
5874 }
5875 
5876 void
5877 ill_untrace_ref(ill_t *ill)
5878 {
5879 	th_trace_t *th_trace;
5880 
5881 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5882 
5883 	if (ill->ill_trace_disable)
5884 		return;
5885 	th_trace = th_trace_ill_lookup(ill);
5886 	ASSERT(th_trace != NULL);
5887 	ASSERT(th_trace->th_refcnt > 0);
5888 
5889 	th_trace->th_refcnt--;
5890 	th_trace_rrecord(th_trace);
5891 }
5892 
5893 /*
5894  * Verify that this thread has no refs to the ipif and free
5895  * the trace buffers
5896  */
5897 /* ARGSUSED */
5898 void
5899 ipif_thread_exit(ipif_t *ipif, void *dummy)
5900 {
5901 	th_trace_t *th_trace;
5902 
5903 	mutex_enter(&ipif->ipif_ill->ill_lock);
5904 
5905 	th_trace = th_trace_ipif_lookup(ipif);
5906 	if (th_trace == NULL) {
5907 		mutex_exit(&ipif->ipif_ill->ill_lock);
5908 		return;
5909 	}
5910 	ASSERT(th_trace->th_refcnt == 0);
5911 	/* unlink th_trace and free it */
5912 	*th_trace->th_prev = th_trace->th_next;
5913 	if (th_trace->th_next != NULL)
5914 		th_trace->th_next->th_prev = th_trace->th_prev;
5915 	th_trace->th_next = NULL;
5916 	th_trace->th_prev = NULL;
5917 	kmem_free(th_trace, sizeof (th_trace_t));
5918 
5919 	mutex_exit(&ipif->ipif_ill->ill_lock);
5920 }
5921 
5922 /*
5923  * Verify that this thread has no refs to the ill and free
5924  * the trace buffers
5925  */
5926 /* ARGSUSED */
5927 void
5928 ill_thread_exit(ill_t *ill, void *dummy)
5929 {
5930 	th_trace_t *th_trace;
5931 
5932 	mutex_enter(&ill->ill_lock);
5933 
5934 	th_trace = th_trace_ill_lookup(ill);
5935 	if (th_trace == NULL) {
5936 		mutex_exit(&ill->ill_lock);
5937 		return;
5938 	}
5939 	ASSERT(th_trace->th_refcnt == 0);
5940 	/* unlink th_trace and free it */
5941 	*th_trace->th_prev = th_trace->th_next;
5942 	if (th_trace->th_next != NULL)
5943 		th_trace->th_next->th_prev = th_trace->th_prev;
5944 	th_trace->th_next = NULL;
5945 	th_trace->th_prev = NULL;
5946 	kmem_free(th_trace, sizeof (th_trace_t));
5947 
5948 	mutex_exit(&ill->ill_lock);
5949 }
5950 #endif
5951 
5952 #ifdef ILL_DEBUG
5953 void
5954 ip_thread_exit(void)
5955 {
5956 	ill_t	*ill;
5957 	ipif_t	*ipif;
5958 	ill_walk_context_t	ctx;
5959 
5960 	rw_enter(&ill_g_lock, RW_READER);
5961 	ill = ILL_START_WALK_ALL(&ctx);
5962 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5963 		for (ipif = ill->ill_ipif; ipif != NULL;
5964 		    ipif = ipif->ipif_next) {
5965 			ipif_thread_exit(ipif, NULL);
5966 		}
5967 		ill_thread_exit(ill, NULL);
5968 	}
5969 	rw_exit(&ill_g_lock);
5970 
5971 	ire_walk(ire_thread_exit, NULL);
5972 	ndp_walk_impl(NULL, nce_thread_exit, NULL, B_FALSE);
5973 }
5974 
5975 /*
5976  * Called when ipif is unplumbed or when memory alloc fails
5977  */
5978 void
5979 ipif_trace_cleanup(ipif_t *ipif)
5980 {
5981 	int	i;
5982 	th_trace_t	*th_trace;
5983 	th_trace_t	*th_trace_next;
5984 
5985 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
5986 		for (th_trace = ipif->ipif_trace[i]; th_trace != NULL;
5987 		    th_trace = th_trace_next) {
5988 			th_trace_next = th_trace->th_next;
5989 			kmem_free(th_trace, sizeof (th_trace_t));
5990 		}
5991 		ipif->ipif_trace[i] = NULL;
5992 	}
5993 }
5994 
5995 /*
5996  * Called when ill is unplumbed or when memory alloc fails
5997  */
5998 void
5999 ill_trace_cleanup(ill_t *ill)
6000 {
6001 	int	i;
6002 	th_trace_t	*th_trace;
6003 	th_trace_t	*th_trace_next;
6004 
6005 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6006 		for (th_trace = ill->ill_trace[i]; th_trace != NULL;
6007 		    th_trace = th_trace_next) {
6008 			th_trace_next = th_trace->th_next;
6009 			kmem_free(th_trace, sizeof (th_trace_t));
6010 		}
6011 		ill->ill_trace[i] = NULL;
6012 	}
6013 }
6014 
6015 #else
6016 void ip_thread_exit(void) {}
6017 #endif
6018 
6019 void
6020 ipif_refhold_locked(ipif_t *ipif)
6021 {
6022 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6023 	ipif->ipif_refcnt++;
6024 	IPIF_TRACE_REF(ipif);
6025 }
6026 
6027 void
6028 ipif_refhold(ipif_t *ipif)
6029 {
6030 	ill_t	*ill;
6031 
6032 	ill = ipif->ipif_ill;
6033 	mutex_enter(&ill->ill_lock);
6034 	ipif->ipif_refcnt++;
6035 	IPIF_TRACE_REF(ipif);
6036 	mutex_exit(&ill->ill_lock);
6037 }
6038 
6039 /*
6040  * Must not be called while holding any locks. Otherwise if this is
6041  * the last reference to be released there is a chance of recursive mutex
6042  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6043  * to restart an ioctl.
6044  */
6045 void
6046 ipif_refrele(ipif_t *ipif)
6047 {
6048 	ill_t	*ill;
6049 
6050 	ill = ipif->ipif_ill;
6051 
6052 	mutex_enter(&ill->ill_lock);
6053 	ASSERT(ipif->ipif_refcnt != 0);
6054 	ipif->ipif_refcnt--;
6055 	IPIF_UNTRACE_REF(ipif);
6056 	if (ipif->ipif_refcnt != 0) {
6057 		mutex_exit(&ill->ill_lock);
6058 		return;
6059 	}
6060 
6061 	/* Drops the ill_lock */
6062 	ipif_ill_refrele_tail(ill);
6063 }
6064 
6065 ipif_t *
6066 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6067 {
6068 	ipif_t	*ipif;
6069 
6070 	mutex_enter(&ill->ill_lock);
6071 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6072 	    ipif != NULL; ipif = ipif->ipif_next) {
6073 		if (!IPIF_CAN_LOOKUP(ipif))
6074 			continue;
6075 		ipif_refhold_locked(ipif);
6076 		mutex_exit(&ill->ill_lock);
6077 		return (ipif);
6078 	}
6079 	mutex_exit(&ill->ill_lock);
6080 	return (NULL);
6081 }
6082 
6083 /*
6084  * TODO: make this table extendible at run time
6085  * Return a pointer to the mac type info for 'mac_type'
6086  */
6087 static ip_m_t *
6088 ip_m_lookup(t_uscalar_t mac_type)
6089 {
6090 	ip_m_t	*ipm;
6091 
6092 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6093 		if (ipm->ip_m_mac_type == mac_type)
6094 			return (ipm);
6095 	return (NULL);
6096 }
6097 
6098 /*
6099  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6100  * ipif_arg is passed in to associate it with the correct interface.
6101  * We may need to restart this operation if the ipif cannot be looked up
6102  * due to an exclusive operation that is currently in progress. The restart
6103  * entry point is specified by 'func'
6104  */
6105 int
6106 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6107     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6108     ire_t **ire_arg, boolean_t ioctl_msg, queue_t *q, mblk_t *mp,
6109     ipsq_func_t func)
6110 {
6111 	ire_t	*ire;
6112 	ire_t	*gw_ire = NULL;
6113 	ipif_t	*ipif = NULL;
6114 	boolean_t ipif_refheld = B_FALSE;
6115 	uint_t	type;
6116 	int	match_flags = MATCH_IRE_TYPE;
6117 	int	error;
6118 
6119 	ip1dbg(("ip_rt_add:"));
6120 
6121 	if (ire_arg != NULL)
6122 		*ire_arg = NULL;
6123 
6124 	/*
6125 	 * If this is the case of RTF_HOST being set, then we set the netmask
6126 	 * to all ones (regardless if one was supplied).
6127 	 */
6128 	if (flags & RTF_HOST)
6129 		mask = IP_HOST_MASK;
6130 
6131 	/*
6132 	 * Prevent routes with a zero gateway from being created (since
6133 	 * interfaces can currently be plumbed and brought up no assigned
6134 	 * address).
6135 	 * For routes with RTA_SRCIFP, the gateway address can be 0.0.0.0.
6136 	 */
6137 	if (gw_addr == 0 && src_ipif == NULL)
6138 		return (ENETUNREACH);
6139 	/*
6140 	 * Get the ipif, if any, corresponding to the gw_addr
6141 	 */
6142 	if (gw_addr != 0) {
6143 		ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func,
6144 		    &error);
6145 		if (ipif != NULL) {
6146 			if (IS_VNI(ipif->ipif_ill)) {
6147 				ipif_refrele(ipif);
6148 				return (EINVAL);
6149 			}
6150 			ipif_refheld = B_TRUE;
6151 		} else if (error == EINPROGRESS) {
6152 			ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6153 			return (EINPROGRESS);
6154 		} else {
6155 			error = 0;
6156 		}
6157 	}
6158 
6159 	if (ipif != NULL) {
6160 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6161 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6162 	} else {
6163 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6164 	}
6165 
6166 	/*
6167 	 * GateD will attempt to create routes with a loopback interface
6168 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6169 	 * these routes to be added, but create them as interface routes
6170 	 * since the gateway is an interface address.
6171 	 */
6172 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK))
6173 		flags &= ~RTF_GATEWAY;
6174 
6175 	/*
6176 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6177 	 * and the gateway address provided is one of the system's interface
6178 	 * addresses.  By using the routing socket interface and supplying an
6179 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6180 	 * specifying an interface route to be created is available which uses
6181 	 * the interface index that specifies the outgoing interface rather than
6182 	 * the address of an outgoing interface (which may not be able to
6183 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6184 	 * flag, routes can be specified which not only specify the next-hop to
6185 	 * be used when routing to a certain prefix, but also which outgoing
6186 	 * interface should be used.
6187 	 *
6188 	 * Previously, interfaces would have unique addresses assigned to them
6189 	 * and so the address assigned to a particular interface could be used
6190 	 * to identify a particular interface.  One exception to this was the
6191 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6192 	 *
6193 	 * With the advent of IPv6 and its link-local addresses, this
6194 	 * restriction was relaxed and interfaces could share addresses between
6195 	 * themselves.  In fact, typically all of the link-local interfaces on
6196 	 * an IPv6 node or router will have the same link-local address.  In
6197 	 * order to differentiate between these interfaces, the use of an
6198 	 * interface index is necessary and this index can be carried inside a
6199 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6200 	 * of using the interface index, however, is that all of the ipif's that
6201 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6202 	 * cannot be used to differentiate between ipif's (or logical
6203 	 * interfaces) that belong to the same ill (physical interface).
6204 	 *
6205 	 * For example, in the following case involving IPv4 interfaces and
6206 	 * logical interfaces
6207 	 *
6208 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6209 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6210 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6211 	 *
6212 	 * the ipif's corresponding to each of these interface routes can be
6213 	 * uniquely identified by the "gateway" (actually interface address).
6214 	 *
6215 	 * In this case involving multiple IPv6 default routes to a particular
6216 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6217 	 * default route is of interest:
6218 	 *
6219 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6220 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6221 	 */
6222 
6223 	/* RTF_GATEWAY not set */
6224 	if (!(flags & RTF_GATEWAY)) {
6225 		queue_t	*stq;
6226 		queue_t	*rfq = NULL;
6227 		ill_t	*in_ill = NULL;
6228 
6229 		/*
6230 		 * As the interface index specified with the RTA_IFP sockaddr is
6231 		 * the same for all ipif's off of an ill, the matching logic
6232 		 * below uses MATCH_IRE_ILL if such an index was specified.
6233 		 * This means that routes sharing the same prefix when added
6234 		 * using a RTA_IFP sockaddr must have distinct interface
6235 		 * indices (namely, they must be on distinct ill's).
6236 		 *
6237 		 * On the other hand, since the gateway address will usually be
6238 		 * different for each ipif on the system, the matching logic
6239 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6240 		 * route.  This means that interface routes for the same prefix
6241 		 * can be created if they belong to distinct ipif's and if a
6242 		 * RTA_IFP sockaddr is not present.
6243 		 */
6244 		if (ipif_arg != NULL) {
6245 			if (ipif_refheld)  {
6246 				ipif_refrele(ipif);
6247 				ipif_refheld = B_FALSE;
6248 			}
6249 			ipif = ipif_arg;
6250 			match_flags |= MATCH_IRE_ILL;
6251 		} else {
6252 			/*
6253 			 * Check the ipif corresponding to the gw_addr
6254 			 */
6255 			if (ipif == NULL)
6256 				return (ENETUNREACH);
6257 			match_flags |= MATCH_IRE_IPIF;
6258 		}
6259 		ASSERT(ipif != NULL);
6260 		/*
6261 		 * If src_ipif is not NULL, we have to create
6262 		 * an ire with non-null ire_in_ill value
6263 		 */
6264 		if (src_ipif != NULL) {
6265 			in_ill = src_ipif->ipif_ill;
6266 		}
6267 
6268 		/*
6269 		 * We check for an existing entry at this point.
6270 		 *
6271 		 * Since a netmask isn't passed in via the ioctl interface
6272 		 * (SIOCADDRT), we don't check for a matching netmask in that
6273 		 * case.
6274 		 */
6275 		if (!ioctl_msg)
6276 			match_flags |= MATCH_IRE_MASK;
6277 		if (src_ipif != NULL) {
6278 			/* Look up in the special table */
6279 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
6280 			    ipif, src_ipif->ipif_ill, match_flags);
6281 		} else {
6282 			ire = ire_ftable_lookup(dst_addr, mask, 0,
6283 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
6284 			    match_flags);
6285 		}
6286 		if (ire != NULL) {
6287 			ire_refrele(ire);
6288 			if (ipif_refheld)
6289 				ipif_refrele(ipif);
6290 			return (EEXIST);
6291 		}
6292 
6293 		if (src_ipif != NULL) {
6294 			/*
6295 			 * Create the special ire for the IRE table
6296 			 * which hangs out of ire_in_ill. This ire
6297 			 * is in-between IRE_CACHE and IRE_INTERFACE.
6298 			 * Thus rfq is non-NULL.
6299 			 */
6300 			rfq = ipif->ipif_rq;
6301 		}
6302 		/* Create the usual interface ires */
6303 
6304 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6305 		    ? ipif->ipif_rq : ipif->ipif_wq;
6306 
6307 		/*
6308 		 * Create a copy of the IRE_LOOPBACK,
6309 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6310 		 * the modified address and netmask.
6311 		 */
6312 		ire = ire_create(
6313 		    (uchar_t *)&dst_addr,
6314 		    (uint8_t *)&mask,
6315 		    (uint8_t *)&ipif->ipif_src_addr,
6316 		    NULL,
6317 		    NULL,
6318 		    &ipif->ipif_mtu,
6319 		    NULL,
6320 		    rfq,
6321 		    stq,
6322 		    ipif->ipif_net_type,
6323 		    ipif->ipif_resolver_mp,
6324 		    ipif,
6325 		    in_ill,
6326 		    0,
6327 		    0,
6328 		    0,
6329 		    flags,
6330 		    &ire_uinfo_null);
6331 		if (ire == NULL) {
6332 			if (ipif_refheld)
6333 				ipif_refrele(ipif);
6334 			return (ENOMEM);
6335 		}
6336 
6337 		/*
6338 		 * Some software (for example, GateD and Sun Cluster) attempts
6339 		 * to create (what amount to) IRE_PREFIX routes with the
6340 		 * loopback address as the gateway.  This is primarily done to
6341 		 * set up prefixes with the RTF_REJECT flag set (for example,
6342 		 * when generating aggregate routes.)
6343 		 *
6344 		 * If the IRE type (as defined by ipif->ipif_net_type) is
6345 		 * IRE_LOOPBACK, then we map the request into a
6346 		 * IRE_IF_NORESOLVER.
6347 		 *
6348 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
6349 		 * routine, but rather using ire_create() directly.
6350 		 */
6351 		if (ipif->ipif_net_type == IRE_LOOPBACK)
6352 			ire->ire_type = IRE_IF_NORESOLVER;
6353 		error = ire_add(&ire, q, mp, func);
6354 		if (error == 0)
6355 			goto save_ire;
6356 
6357 		/*
6358 		 * In the result of failure, ire_add() will have already
6359 		 * deleted the ire in question, so there is no need to
6360 		 * do that here.
6361 		 */
6362 		if (ipif_refheld)
6363 			ipif_refrele(ipif);
6364 		return (error);
6365 	}
6366 	if (ipif_refheld) {
6367 		ipif_refrele(ipif);
6368 		ipif_refheld = B_FALSE;
6369 	}
6370 
6371 	if (src_ipif != NULL) {
6372 		/* RTA_SRCIFP is not supported on RTF_GATEWAY */
6373 		ip2dbg(("ip_rt_add: SRCIF cannot be set with gateway route\n"));
6374 		return (EINVAL);
6375 	}
6376 	/*
6377 	 * Get an interface IRE for the specified gateway.
6378 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
6379 	 * gateway, it is currently unreachable and we fail the request
6380 	 * accordingly.
6381 	 */
6382 	ipif = ipif_arg;
6383 	if (ipif_arg != NULL)
6384 		match_flags |= MATCH_IRE_ILL;
6385 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
6386 	    ALL_ZONES, 0, match_flags);
6387 	if (gw_ire == NULL)
6388 		return (ENETUNREACH);
6389 
6390 	/*
6391 	 * We create one of three types of IREs as a result of this request
6392 	 * based on the netmask.  A netmask of all ones (which is automatically
6393 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
6394 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
6395 	 * created.  Otherwise, an IRE_PREFIX route is created for the
6396 	 * destination prefix.
6397 	 */
6398 	if (mask == IP_HOST_MASK)
6399 		type = IRE_HOST;
6400 	else if (mask == 0)
6401 		type = IRE_DEFAULT;
6402 	else
6403 		type = IRE_PREFIX;
6404 
6405 	/* check for a duplicate entry */
6406 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
6407 	    NULL, ALL_ZONES, 0, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW);
6408 	if (ire != NULL) {
6409 		ire_refrele(gw_ire);
6410 		ire_refrele(ire);
6411 		return (EEXIST);
6412 	}
6413 
6414 	/* Create the IRE. */
6415 	ire = ire_create(
6416 	    (uchar_t *)&dst_addr,		/* dest address */
6417 	    (uchar_t *)&mask,			/* mask */
6418 	    /* src address assigned by the caller? */
6419 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
6420 		(flags & RTF_SETSRC)) ?  &src_addr : NULL),
6421 	    (uchar_t *)&gw_addr,		/* gateway address */
6422 	    NULL,				/* no in-srcaddress */
6423 	    &gw_ire->ire_max_frag,
6424 	    NULL,				/* no Fast Path header */
6425 	    NULL,				/* no recv-from queue */
6426 	    NULL,				/* no send-to queue */
6427 	    (ushort_t)type,			/* IRE type */
6428 	    NULL,
6429 	    ipif_arg,
6430 	    NULL,
6431 	    0,
6432 	    0,
6433 	    0,
6434 	    flags,
6435 	    &gw_ire->ire_uinfo);		/* Inherit ULP info from gw */
6436 	if (ire == NULL) {
6437 		ire_refrele(gw_ire);
6438 		return (ENOMEM);
6439 	}
6440 
6441 	/*
6442 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
6443 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
6444 	 */
6445 
6446 	/* Add the new IRE. */
6447 	error = ire_add(&ire, q, mp, func);
6448 	if (error != 0) {
6449 		/*
6450 		 * In the result of failure, ire_add() will have already
6451 		 * deleted the ire in question, so there is no need to
6452 		 * do that here.
6453 		 */
6454 		ire_refrele(gw_ire);
6455 		return (error);
6456 	}
6457 
6458 	if (flags & RTF_MULTIRT) {
6459 		/*
6460 		 * Invoke the CGTP (multirouting) filtering module
6461 		 * to add the dst address in the filtering database.
6462 		 * Replicated inbound packets coming from that address
6463 		 * will be filtered to discard the duplicates.
6464 		 * It is not necessary to call the CGTP filter hook
6465 		 * when the dst address is a broadcast or multicast,
6466 		 * because an IP source address cannot be a broadcast
6467 		 * or a multicast.
6468 		 */
6469 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
6470 		    IRE_BROADCAST, NULL, NULL, MATCH_IRE_TYPE);
6471 		if (ire_dst != NULL) {
6472 			ip_cgtp_bcast_add(ire, ire_dst);
6473 			ire_refrele(ire_dst);
6474 			goto save_ire;
6475 		}
6476 		if ((ip_cgtp_filter_ops != NULL) && !CLASSD(ire->ire_addr)) {
6477 			int res = ip_cgtp_filter_ops->cfo_add_dest_v4(
6478 			    ire->ire_addr,
6479 			    ire->ire_gateway_addr,
6480 			    ire->ire_src_addr,
6481 			    gw_ire->ire_src_addr);
6482 			if (res != 0) {
6483 				ire_refrele(gw_ire);
6484 				ire_delete(ire);
6485 				return (res);
6486 			}
6487 		}
6488 	}
6489 
6490 save_ire:
6491 	if (gw_ire != NULL) {
6492 		ire_refrele(gw_ire);
6493 	}
6494 	/*
6495 	 * We do not do save_ire for the routes added with RTA_SRCIFP
6496 	 * flag. This route is only added and deleted by mipagent.
6497 	 * So, for simplicity of design, we refrain from saving
6498 	 * ires that are created with srcif value. This may change
6499 	 * in future if we find more usage of srcifp feature.
6500 	 */
6501 	if (ipif != NULL && src_ipif == NULL) {
6502 		/*
6503 		 * Save enough information so that we can recreate the IRE if
6504 		 * the interface goes down and then up.  The metrics associated
6505 		 * with the route will be saved as well when rts_setmetrics() is
6506 		 * called after the IRE has been created.  In the case where
6507 		 * memory cannot be allocated, none of this information will be
6508 		 * saved.
6509 		 */
6510 		ipif_save_ire(ipif, ire);
6511 	}
6512 	if (ioctl_msg)
6513 		ip_rts_rtmsg(RTM_OLDADD, ire, 0);
6514 	if (ire_arg != NULL) {
6515 		/*
6516 		 * Store the ire that was successfully added into where ire_arg
6517 		 * points to so that callers don't have to look it up
6518 		 * themselves (but they are responsible for ire_refrele()ing
6519 		 * the ire when they are finished with it).
6520 		 */
6521 		*ire_arg = ire;
6522 	} else {
6523 		ire_refrele(ire);		/* Held in ire_add */
6524 	}
6525 	if (ipif_refheld)
6526 		ipif_refrele(ipif);
6527 	return (0);
6528 }
6529 
6530 /*
6531  * ip_rt_delete is called to delete an IPv4 route.
6532  * ipif_arg is passed in to associate it with the correct interface.
6533  * src_ipif is passed to associate the incoming interface of the packet.
6534  * We may need to restart this operation if the ipif cannot be looked up
6535  * due to an exclusive operation that is currently in progress. The restart
6536  * entry point is specified by 'func'
6537  */
6538 /* ARGSUSED4 */
6539 int
6540 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6541     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6542     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func)
6543 {
6544 	ire_t	*ire = NULL;
6545 	ipif_t	*ipif;
6546 	boolean_t ipif_refheld = B_FALSE;
6547 	uint_t	type;
6548 	uint_t	match_flags = MATCH_IRE_TYPE;
6549 	int	err = 0;
6550 
6551 	ip1dbg(("ip_rt_delete:"));
6552 	/*
6553 	 * If this is the case of RTF_HOST being set, then we set the netmask
6554 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
6555 	 */
6556 	if (flags & RTF_HOST) {
6557 		mask = IP_HOST_MASK;
6558 		match_flags |= MATCH_IRE_MASK;
6559 	} else if (rtm_addrs & RTA_NETMASK) {
6560 		match_flags |= MATCH_IRE_MASK;
6561 	}
6562 
6563 	/*
6564 	 * Note that RTF_GATEWAY is never set on a delete, therefore
6565 	 * we check if the gateway address is one of our interfaces first,
6566 	 * and fall back on RTF_GATEWAY routes.
6567 	 *
6568 	 * This makes it possible to delete an original
6569 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
6570 	 *
6571 	 * As the interface index specified with the RTA_IFP sockaddr is the
6572 	 * same for all ipif's off of an ill, the matching logic below uses
6573 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
6574 	 * sharing the same prefix and interface index as the the route
6575 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
6576 	 * is specified in the request.
6577 	 *
6578 	 * On the other hand, since the gateway address will usually be
6579 	 * different for each ipif on the system, the matching logic
6580 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
6581 	 * route.  This means that interface routes for the same prefix can be
6582 	 * uniquely identified if they belong to distinct ipif's and if a
6583 	 * RTA_IFP sockaddr is not present.
6584 	 *
6585 	 * For more detail on specifying routes by gateway address and by
6586 	 * interface index, see the comments in ip_rt_add().
6587 	 * gw_addr could be zero in some cases when both RTA_SRCIFP and
6588 	 * RTA_IFP are specified. If RTA_SRCIFP is specified and  both
6589 	 * RTA_IFP and gateway_addr are NULL/zero, then delete will not
6590 	 * succeed.
6591 	 */
6592 	if (src_ipif != NULL) {
6593 		if (ipif_arg == NULL && gw_addr != 0) {
6594 			ipif_arg = ipif_lookup_interface(gw_addr, dst_addr,
6595 			    q, mp, func, &err);
6596 			if (ipif_arg != NULL)
6597 				ipif_refheld = B_TRUE;
6598 		}
6599 		if (ipif_arg == NULL) {
6600 			err = (err == EINPROGRESS) ? err : ESRCH;
6601 			return (err);
6602 		}
6603 		ipif = ipif_arg;
6604 	} else {
6605 		ipif = ipif_lookup_interface(gw_addr, dst_addr,
6606 			    q, mp, func, &err);
6607 		if (ipif != NULL)
6608 			ipif_refheld = B_TRUE;
6609 		else if (err == EINPROGRESS)
6610 			return (err);
6611 		else
6612 			err = 0;
6613 	}
6614 	if (ipif != NULL) {
6615 		if (ipif_arg != NULL) {
6616 			if (ipif_refheld) {
6617 				ipif_refrele(ipif);
6618 				ipif_refheld = B_FALSE;
6619 			}
6620 			ipif = ipif_arg;
6621 			match_flags |= MATCH_IRE_ILL;
6622 		} else {
6623 			match_flags |= MATCH_IRE_IPIF;
6624 		}
6625 		if (src_ipif != NULL) {
6626 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
6627 			    ipif, src_ipif->ipif_ill, match_flags);
6628 		} else {
6629 			if (ipif->ipif_ire_type == IRE_LOOPBACK) {
6630 				ire = ire_ctable_lookup(dst_addr, 0,
6631 				    IRE_LOOPBACK, ipif, ALL_ZONES, match_flags);
6632 			}
6633 			if (ire == NULL) {
6634 				ire = ire_ftable_lookup(dst_addr, mask, 0,
6635 				    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
6636 				    match_flags);
6637 			}
6638 		}
6639 	}
6640 
6641 	if (ire == NULL) {
6642 		/*
6643 		 * At this point, the gateway address is not one of our own
6644 		 * addresses or a matching interface route was not found.  We
6645 		 * set the IRE type to lookup based on whether
6646 		 * this is a host route, a default route or just a prefix.
6647 		 *
6648 		 * If an ipif_arg was passed in, then the lookup is based on an
6649 		 * interface index so MATCH_IRE_ILL is added to match_flags.
6650 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
6651 		 * set as the route being looked up is not a traditional
6652 		 * interface route.
6653 		 * Since we do not add gateway route with srcipif, we don't
6654 		 * expect to find it either.
6655 		 */
6656 		if (src_ipif != NULL) {
6657 			if (ipif_refheld)
6658 				ipif_refrele(ipif);
6659 			return (ESRCH);
6660 		} else {
6661 			match_flags &= ~MATCH_IRE_IPIF;
6662 			match_flags |= MATCH_IRE_GW;
6663 			if (ipif_arg != NULL)
6664 				match_flags |= MATCH_IRE_ILL;
6665 			if (mask == IP_HOST_MASK)
6666 				type = IRE_HOST;
6667 			else if (mask == 0)
6668 				type = IRE_DEFAULT;
6669 			else
6670 				type = IRE_PREFIX;
6671 			ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type,
6672 			    ipif_arg, NULL, ALL_ZONES, 0, match_flags);
6673 			if (ire == NULL && type == IRE_HOST) {
6674 				ire = ire_ftable_lookup(dst_addr, mask, gw_addr,
6675 				    IRE_HOST_REDIRECT, ipif_arg, NULL,
6676 				    ALL_ZONES, 0, match_flags);
6677 			}
6678 		}
6679 	}
6680 
6681 	if (ipif_refheld)
6682 		ipif_refrele(ipif);
6683 
6684 	/* ipif is not refheld anymore */
6685 	if (ire == NULL)
6686 		return (ESRCH);
6687 
6688 	if (ire->ire_flags & RTF_MULTIRT) {
6689 		/*
6690 		 * Invoke the CGTP (multirouting) filtering module
6691 		 * to remove the dst address from the filtering database.
6692 		 * Packets coming from that address will no longer be
6693 		 * filtered to remove duplicates.
6694 		 */
6695 		if (ip_cgtp_filter_ops != NULL) {
6696 			err = ip_cgtp_filter_ops->cfo_del_dest_v4(ire->ire_addr,
6697 			    ire->ire_gateway_addr);
6698 		}
6699 		ip_cgtp_bcast_delete(ire);
6700 	}
6701 
6702 	ipif = ire->ire_ipif;
6703 	/*
6704 	 * Removing from ipif_saved_ire_mp is not necessary
6705 	 * when src_ipif being non-NULL. ip_rt_add does not
6706 	 * save the ires which src_ipif being non-NULL.
6707 	 */
6708 	if (ipif != NULL && src_ipif == NULL) {
6709 		ipif_remove_ire(ipif, ire);
6710 	}
6711 	if (ioctl_msg)
6712 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0);
6713 	ire_delete(ire);
6714 	ire_refrele(ire);
6715 	return (err);
6716 }
6717 
6718 /*
6719  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6720  */
6721 /* ARGSUSED */
6722 int
6723 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6724     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6725 {
6726 	ipaddr_t dst_addr;
6727 	ipaddr_t gw_addr;
6728 	ipaddr_t mask;
6729 	int error = 0;
6730 	mblk_t *mp1;
6731 	struct rtentry *rt;
6732 	ipif_t *ipif = NULL;
6733 
6734 	ip1dbg(("ip_siocaddrt:"));
6735 	/* Existence of mp1 verified in ip_wput_nondata */
6736 	mp1 = mp->b_cont->b_cont;
6737 	rt = (struct rtentry *)mp1->b_rptr;
6738 
6739 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6740 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6741 
6742 	/*
6743 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6744 	 * to a particular host address.  In this case, we set the netmask to
6745 	 * all ones for the particular destination address.  Otherwise,
6746 	 * determine the netmask to be used based on dst_addr and the interfaces
6747 	 * in use.
6748 	 */
6749 	if (rt->rt_flags & RTF_HOST) {
6750 		mask = IP_HOST_MASK;
6751 	} else {
6752 		/*
6753 		 * Note that ip_subnet_mask returns a zero mask in the case of
6754 		 * default (an all-zeroes address).
6755 		 */
6756 		mask = ip_subnet_mask(dst_addr, &ipif);
6757 	}
6758 
6759 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags,
6760 	    NULL, NULL, NULL, B_TRUE, q, mp, ip_process_ioctl);
6761 	if (ipif != NULL)
6762 		ipif_refrele(ipif);
6763 	return (error);
6764 }
6765 
6766 /*
6767  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6768  */
6769 /* ARGSUSED */
6770 int
6771 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6772     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6773 {
6774 	ipaddr_t dst_addr;
6775 	ipaddr_t gw_addr;
6776 	ipaddr_t mask;
6777 	int error;
6778 	mblk_t *mp1;
6779 	struct rtentry *rt;
6780 	ipif_t *ipif = NULL;
6781 
6782 	ip1dbg(("ip_siocdelrt:"));
6783 	/* Existence of mp1 verified in ip_wput_nondata */
6784 	mp1 = mp->b_cont->b_cont;
6785 	rt = (struct rtentry *)mp1->b_rptr;
6786 
6787 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6788 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6789 
6790 	/*
6791 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6792 	 * to a particular host address.  In this case, we set the netmask to
6793 	 * all ones for the particular destination address.  Otherwise,
6794 	 * determine the netmask to be used based on dst_addr and the interfaces
6795 	 * in use.
6796 	 */
6797 	if (rt->rt_flags & RTF_HOST) {
6798 		mask = IP_HOST_MASK;
6799 	} else {
6800 		/*
6801 		 * Note that ip_subnet_mask returns a zero mask in the case of
6802 		 * default (an all-zeroes address).
6803 		 */
6804 		mask = ip_subnet_mask(dst_addr, &ipif);
6805 	}
6806 
6807 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6808 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, NULL,
6809 	    B_TRUE, q, mp, ip_process_ioctl);
6810 	if (ipif != NULL)
6811 		ipif_refrele(ipif);
6812 	return (error);
6813 }
6814 
6815 /*
6816  * Enqueue the mp onto the ipsq, chained by b_next.
6817  * b_prev stores the function to be executed later, and b_queue the queue
6818  * where this mp originated.
6819  */
6820 void
6821 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6822     ill_t *pending_ill)
6823 {
6824 	conn_t	*connp = NULL;
6825 
6826 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6827 	ASSERT(func != NULL);
6828 
6829 	mp->b_queue = q;
6830 	mp->b_prev = (void *)func;
6831 	mp->b_next = NULL;
6832 
6833 	switch (type) {
6834 	case CUR_OP:
6835 		if (ipsq->ipsq_mptail != NULL) {
6836 			ASSERT(ipsq->ipsq_mphead != NULL);
6837 			ipsq->ipsq_mptail->b_next = mp;
6838 		} else {
6839 			ASSERT(ipsq->ipsq_mphead == NULL);
6840 			ipsq->ipsq_mphead = mp;
6841 		}
6842 		ipsq->ipsq_mptail = mp;
6843 		break;
6844 
6845 	case NEW_OP:
6846 		if (ipsq->ipsq_xopq_mptail != NULL) {
6847 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6848 			ipsq->ipsq_xopq_mptail->b_next = mp;
6849 		} else {
6850 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6851 			ipsq->ipsq_xopq_mphead = mp;
6852 		}
6853 		ipsq->ipsq_xopq_mptail = mp;
6854 		break;
6855 	default:
6856 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6857 	}
6858 
6859 	if (CONN_Q(q) && pending_ill != NULL) {
6860 		connp = Q_TO_CONN(q);
6861 
6862 		ASSERT(MUTEX_HELD(&connp->conn_lock));
6863 		connp->conn_oper_pending_ill = pending_ill;
6864 	}
6865 }
6866 
6867 /*
6868  * Return the mp at the head of the ipsq. After emptying the ipsq
6869  * look at the next ioctl, if this ioctl is complete. Otherwise
6870  * return, we will resume when we complete the current ioctl.
6871  * The current ioctl will wait till it gets a response from the
6872  * driver below.
6873  */
6874 static mblk_t *
6875 ipsq_dq(ipsq_t *ipsq)
6876 {
6877 	mblk_t	*mp;
6878 
6879 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6880 
6881 	mp = ipsq->ipsq_mphead;
6882 	if (mp != NULL) {
6883 		ipsq->ipsq_mphead = mp->b_next;
6884 		if (ipsq->ipsq_mphead == NULL)
6885 			ipsq->ipsq_mptail = NULL;
6886 		mp->b_next = NULL;
6887 		return (mp);
6888 	}
6889 	if (ipsq->ipsq_current_ipif != NULL)
6890 		return (NULL);
6891 	mp = ipsq->ipsq_xopq_mphead;
6892 	if (mp != NULL) {
6893 		ipsq->ipsq_xopq_mphead = mp->b_next;
6894 		if (ipsq->ipsq_xopq_mphead == NULL)
6895 			ipsq->ipsq_xopq_mptail = NULL;
6896 		mp->b_next = NULL;
6897 		return (mp);
6898 	}
6899 	return (NULL);
6900 }
6901 
6902 /*
6903  * Enter the ipsq corresponding to ill, by waiting synchronously till
6904  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
6905  * will have to drain completely before ipsq_enter returns success.
6906  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
6907  * and the ipsq_exit logic will start the next enqueued ioctl after
6908  * completion of the current ioctl. If 'force' is used, we don't wait
6909  * for the enqueued ioctls. This is needed when a conn_close wants to
6910  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
6911  * of an ill can also use this option. But we dont' use it currently.
6912  */
6913 #define	ENTER_SQ_WAIT_TICKS 100
6914 boolean_t
6915 ipsq_enter(ill_t *ill, boolean_t force)
6916 {
6917 	ipsq_t	*ipsq;
6918 	boolean_t waited_enough = B_FALSE;
6919 
6920 	/*
6921 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
6922 	 * Since the <ill-ipsq> assocs could change while we wait for the
6923 	 * writer, it is easier to wait on a fixed global rather than try to
6924 	 * cv_wait on a changing ipsq.
6925 	 */
6926 	mutex_enter(&ill->ill_lock);
6927 	for (;;) {
6928 		if (ill->ill_state_flags & ILL_CONDEMNED) {
6929 			mutex_exit(&ill->ill_lock);
6930 			return (B_FALSE);
6931 		}
6932 
6933 		ipsq = ill->ill_phyint->phyint_ipsq;
6934 		mutex_enter(&ipsq->ipsq_lock);
6935 		if (ipsq->ipsq_writer == NULL &&
6936 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
6937 			break;
6938 		} else if (ipsq->ipsq_writer != NULL) {
6939 			mutex_exit(&ipsq->ipsq_lock);
6940 			cv_wait(&ill->ill_cv, &ill->ill_lock);
6941 		} else {
6942 			mutex_exit(&ipsq->ipsq_lock);
6943 			if (force) {
6944 				(void) cv_timedwait(&ill->ill_cv,
6945 				    &ill->ill_lock,
6946 				    lbolt + ENTER_SQ_WAIT_TICKS);
6947 				waited_enough = B_TRUE;
6948 				continue;
6949 			} else {
6950 				cv_wait(&ill->ill_cv, &ill->ill_lock);
6951 			}
6952 		}
6953 	}
6954 
6955 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
6956 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
6957 	ipsq->ipsq_writer = curthread;
6958 	ipsq->ipsq_reentry_cnt++;
6959 #ifdef ILL_DEBUG
6960 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
6961 #endif
6962 	mutex_exit(&ipsq->ipsq_lock);
6963 	mutex_exit(&ill->ill_lock);
6964 	return (B_TRUE);
6965 }
6966 
6967 /*
6968  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
6969  * certain critical operations like plumbing (i.e. most set ioctls),
6970  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
6971  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
6972  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
6973  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
6974  * threads executing in the ipsq. Responses from the driver pertain to the
6975  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
6976  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
6977  *
6978  * If a thread does not want to reenter the ipsq when it is already writer,
6979  * it must make sure that the specified reentry point to be called later
6980  * when the ipsq is empty, nor any code path starting from the specified reentry
6981  * point must never ever try to enter the ipsq again. Otherwise it can lead
6982  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
6983  * When the thread that is currently exclusive finishes, it (ipsq_exit)
6984  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
6985  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
6986  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
6987  * ioctl if the current ioctl has completed. If the current ioctl is still
6988  * in progress it simply returns. The current ioctl could be waiting for
6989  * a response from another module (arp_ or the driver or could be waiting for
6990  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
6991  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
6992  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
6993  * ipsq_current_ipif is clear which happens only on ioctl completion.
6994  */
6995 
6996 /*
6997  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
6998  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
6999  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7000  * completion.
7001  */
7002 ipsq_t *
7003 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7004     ipsq_func_t func, int type, boolean_t reentry_ok)
7005 {
7006 	ipsq_t	*ipsq;
7007 
7008 	/* Only 1 of ipif or ill can be specified */
7009 	ASSERT((ipif != NULL) ^ (ill != NULL));
7010 	if (ipif != NULL)
7011 		ill = ipif->ipif_ill;
7012 
7013 	/*
7014 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7015 	 * ipsq of an ill can't change when ill_lock is held.
7016 	 */
7017 	GRAB_CONN_LOCK(q);
7018 	mutex_enter(&ill->ill_lock);
7019 	ipsq = ill->ill_phyint->phyint_ipsq;
7020 	mutex_enter(&ipsq->ipsq_lock);
7021 
7022 	/*
7023 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7024 	 *    (Note: If the caller does not specify reentry_ok then neither
7025 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7026 	 *    again. Otherwise it can lead to an infinite loop
7027 	 * 2. Enter the ipsq if there is no current writer and this attempted
7028 	 *    entry is part of the current ioctl or operation
7029 	 * 3. Enter the ipsq if there is no current writer and this is a new
7030 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7031 	 *    empty and there is no ioctl (or operation) currently in progress
7032 	 */
7033 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7034 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7035 	    ipsq->ipsq_current_ipif == NULL))) ||
7036 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7037 		/* Success. */
7038 		ipsq->ipsq_reentry_cnt++;
7039 		ipsq->ipsq_writer = curthread;
7040 		mutex_exit(&ipsq->ipsq_lock);
7041 		mutex_exit(&ill->ill_lock);
7042 		RELEASE_CONN_LOCK(q);
7043 #ifdef ILL_DEBUG
7044 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7045 #endif
7046 		return (ipsq);
7047 	}
7048 
7049 	ipsq_enq(ipsq, q, mp, func, type, ill);
7050 
7051 	mutex_exit(&ipsq->ipsq_lock);
7052 	mutex_exit(&ill->ill_lock);
7053 	RELEASE_CONN_LOCK(q);
7054 	return (NULL);
7055 }
7056 
7057 /*
7058  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7059  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7060  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7061  * completion.
7062  *
7063  * This function does a refrele on the ipif/ill.
7064  */
7065 void
7066 qwriter_ip(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7067     ipsq_func_t func, int type, boolean_t reentry_ok)
7068 {
7069 	ipsq_t	*ipsq;
7070 
7071 	ipsq = ipsq_try_enter(ipif, ill, q, mp, func, type, reentry_ok);
7072 	/*
7073 	 * Caller must have done a refhold on the ipif. ipif_refrele
7074 	 * happens on the passed ipif. We can do this since we are
7075 	 * already exclusive, or we won't access ipif henceforth, Both
7076 	 * this func and caller will just return if we ipsq_try_enter
7077 	 * fails above. This is needed because func needs to
7078 	 * see the correct refcount. Eg. removeif can work only then.
7079 	 */
7080 	if (ipif != NULL)
7081 		ipif_refrele(ipif);
7082 	else
7083 		ill_refrele(ill);
7084 	if (ipsq != NULL) {
7085 		(*func)(ipsq, q, mp, NULL);
7086 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7087 	}
7088 }
7089 
7090 /*
7091  * If there are more than ILL_GRP_CNT ills in a group,
7092  * we use kmem alloc'd buffers, else use the stack
7093  */
7094 #define	ILL_GRP_CNT	14
7095 /*
7096  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7097  * Called by a thread that is currently exclusive on this ipsq.
7098  */
7099 void
7100 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7101 {
7102 	queue_t	*q;
7103 	mblk_t	*mp;
7104 	ipsq_func_t	func;
7105 	int	next;
7106 	ill_t	**ill_list = NULL;
7107 	size_t	ill_list_size = 0;
7108 	int	cnt = 0;
7109 	boolean_t need_ipsq_free = B_FALSE;
7110 
7111 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7112 	mutex_enter(&ipsq->ipsq_lock);
7113 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7114 	if (ipsq->ipsq_reentry_cnt != 1) {
7115 		ipsq->ipsq_reentry_cnt--;
7116 		mutex_exit(&ipsq->ipsq_lock);
7117 		return;
7118 	}
7119 
7120 	mp = ipsq_dq(ipsq);
7121 	while (mp != NULL) {
7122 again:
7123 		mutex_exit(&ipsq->ipsq_lock);
7124 		func = (ipsq_func_t)mp->b_prev;
7125 		q = (queue_t *)mp->b_queue;
7126 		mp->b_prev = NULL;
7127 		mp->b_queue = NULL;
7128 
7129 		/*
7130 		 * If 'q' is an conn queue, it is valid, since we did a
7131 		 * a refhold on the connp, at the start of the ioctl.
7132 		 * If 'q' is an ill queue, it is valid, since close of an
7133 		 * ill will clean up the 'ipsq'.
7134 		 */
7135 		(*func)(ipsq, q, mp, NULL);
7136 
7137 		mutex_enter(&ipsq->ipsq_lock);
7138 		mp = ipsq_dq(ipsq);
7139 	}
7140 
7141 	mutex_exit(&ipsq->ipsq_lock);
7142 
7143 	/*
7144 	 * Need to grab the locks in the right order. Need to
7145 	 * atomically check (under ipsq_lock) that there are no
7146 	 * messages before relinquishing the ipsq. Also need to
7147 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7148 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7149 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7150 	 * to grab ill_g_lock as writer.
7151 	 */
7152 	rw_enter(&ill_g_lock, ipsq->ipsq_split ? RW_WRITER : RW_READER);
7153 
7154 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7155 	if (ipsq->ipsq_refs != 0) {
7156 		/* At most 2 ills v4/v6 per phyint */
7157 		cnt = ipsq->ipsq_refs << 1;
7158 		ill_list_size = cnt * sizeof (ill_t *);
7159 		/*
7160 		 * If memory allocation fails, we will do the split
7161 		 * the next time ipsq_exit is called for whatever reason.
7162 		 * As long as the ipsq_split flag is set the need to
7163 		 * split is remembered.
7164 		 */
7165 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7166 		if (ill_list != NULL)
7167 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7168 	}
7169 	mutex_enter(&ipsq->ipsq_lock);
7170 	mp = ipsq_dq(ipsq);
7171 	if (mp != NULL) {
7172 		/* oops, some message has landed up, we can't get out */
7173 		if (ill_list != NULL)
7174 			ill_unlock_ills(ill_list, cnt);
7175 		rw_exit(&ill_g_lock);
7176 		if (ill_list != NULL)
7177 			kmem_free(ill_list, ill_list_size);
7178 		ill_list = NULL;
7179 		ill_list_size = 0;
7180 		cnt = 0;
7181 		goto again;
7182 	}
7183 
7184 	/*
7185 	 * Split only if no ioctl is pending and if memory alloc succeeded
7186 	 * above.
7187 	 */
7188 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7189 		ill_list != NULL) {
7190 		/*
7191 		 * No new ill can join this ipsq since we are holding the
7192 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7193 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7194 		 * If so we will retry on the next ipsq_exit.
7195 		 */
7196 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7197 	}
7198 
7199 	/*
7200 	 * We are holding the ipsq lock, hence no new messages can
7201 	 * land up on the ipsq, and there are no messages currently.
7202 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7203 	 * atomically while holding ill locks.
7204 	 */
7205 	ipsq->ipsq_writer = NULL;
7206 	ipsq->ipsq_reentry_cnt--;
7207 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7208 #ifdef ILL_DEBUG
7209 	ipsq->ipsq_depth = 0;
7210 #endif
7211 	mutex_exit(&ipsq->ipsq_lock);
7212 	/*
7213 	 * For IPMP this should wake up all ills in this ipsq.
7214 	 * We need to hold the ill_lock while waking up waiters to
7215 	 * avoid missed wakeups. But there is no need to acquire all
7216 	 * the ill locks and then wakeup. If we have not acquired all
7217 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7218 	 * wakes up ills one at a time after getting the right ill_lock
7219 	 */
7220 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7221 	if (ill_list != NULL)
7222 		ill_unlock_ills(ill_list, cnt);
7223 	if (ipsq->ipsq_refs == 0)
7224 		need_ipsq_free = B_TRUE;
7225 	rw_exit(&ill_g_lock);
7226 	if (ill_list != 0)
7227 		kmem_free(ill_list, ill_list_size);
7228 
7229 	if (need_ipsq_free) {
7230 		/*
7231 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7232 		 * looked up. ipsq can be looked up only thru ill or phyint
7233 		 * and there are no ills/phyint on this ipsq.
7234 		 */
7235 		ipsq_delete(ipsq);
7236 	}
7237 	/*
7238 	 * Now start any igmp or mld timers that could not be started
7239 	 * while inside the ipsq. The timers can't be started while inside
7240 	 * the ipsq, since igmp_start_timers may need to call untimeout()
7241 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
7242 	 * there could be a deadlock since the timeout handlers
7243 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
7244 	 * wait in ipsq_enter() trying to get the ipsq.
7245 	 *
7246 	 * However there is one exception to the above. If this thread is
7247 	 * itself the igmp/mld timeout handler thread, then we don't want
7248 	 * to start any new timer until the current handler is done. The
7249 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
7250 	 * all others pass B_TRUE.
7251 	 */
7252 	if (start_igmp_timer) {
7253 		mutex_enter(&igmp_timer_lock);
7254 		next = igmp_deferred_next;
7255 		igmp_deferred_next = INFINITY;
7256 		mutex_exit(&igmp_timer_lock);
7257 
7258 		if (next != INFINITY)
7259 			igmp_start_timers(next);
7260 	}
7261 
7262 	if (start_mld_timer) {
7263 		mutex_enter(&mld_timer_lock);
7264 		next = mld_deferred_next;
7265 		mld_deferred_next = INFINITY;
7266 		mutex_exit(&mld_timer_lock);
7267 
7268 		if (next != INFINITY)
7269 			mld_start_timers(next);
7270 	}
7271 }
7272 
7273 /*
7274  * The ill is closing. Flush all messages on the ipsq that originated
7275  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
7276  * for this ill since ipsq_enter could not have entered until then.
7277  * New messages can't be queued since the CONDEMNED flag is set.
7278  */
7279 static void
7280 ipsq_flush(ill_t *ill)
7281 {
7282 	queue_t	*q;
7283 	mblk_t	*prev;
7284 	mblk_t	*mp;
7285 	mblk_t	*mp_next;
7286 	ipsq_t	*ipsq;
7287 
7288 	ASSERT(IAM_WRITER_ILL(ill));
7289 	ipsq = ill->ill_phyint->phyint_ipsq;
7290 	/*
7291 	 * Flush any messages sent up by the driver.
7292 	 */
7293 	mutex_enter(&ipsq->ipsq_lock);
7294 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
7295 		mp_next = mp->b_next;
7296 		q = mp->b_queue;
7297 		if (q == ill->ill_rq || q == ill->ill_wq) {
7298 			/* Remove the mp from the ipsq */
7299 			if (prev == NULL)
7300 				ipsq->ipsq_mphead = mp->b_next;
7301 			else
7302 				prev->b_next = mp->b_next;
7303 			if (ipsq->ipsq_mptail == mp) {
7304 				ASSERT(mp_next == NULL);
7305 				ipsq->ipsq_mptail = prev;
7306 			}
7307 			ip_ioctl_freemsg(mp);
7308 		} else {
7309 			prev = mp;
7310 		}
7311 	}
7312 	mutex_exit(&ipsq->ipsq_lock);
7313 	(void) ipsq_pending_mp_cleanup(ill, NULL);
7314 	ipsq_xopq_mp_cleanup(ill, NULL);
7315 	ill_pending_mp_cleanup(ill);
7316 }
7317 
7318 /*
7319  * Clean up one squeue element. ill_inuse_ref is protected by ill_lock.
7320  * The real cleanup happens behind the squeue via ip_squeue_clean function but
7321  * we need to protect ourselfs from 2 threads trying to cleanup at the same
7322  * time (possible with one port going down for aggr and someone tearing down the
7323  * entire aggr simultaneously. So we use ill_inuse_ref protected by ill_lock
7324  * to indicate when the cleanup has started (1 ref) and when the cleanup
7325  * is done (0 ref). When a new ring gets assigned to squeue, we start by
7326  * putting 2 ref on ill_inuse_ref.
7327  */
7328 static void
7329 ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring)
7330 {
7331 	conn_t *connp;
7332 	squeue_t *sqp;
7333 	mblk_t *mp;
7334 
7335 	ASSERT(rx_ring != NULL);
7336 
7337 	/* Just clean one squeue */
7338 	mutex_enter(&ill->ill_lock);
7339 	while (rx_ring->rr_ring_state == ILL_RING_INPROC)
7340 		/* Some operations pending on the ring. Wait */
7341 		cv_wait(&ill->ill_cv, &ill->ill_lock);
7342 
7343 	if (rx_ring->rr_ring_state != ILL_RING_INUSE) {
7344 		/*
7345 		 * Someone already trying to clean
7346 		 * this squeue or its already been cleaned.
7347 		 */
7348 		mutex_exit(&ill->ill_lock);
7349 		return;
7350 	}
7351 	sqp = rx_ring->rr_sqp;
7352 
7353 	if (sqp == NULL) {
7354 		/*
7355 		 * The rx_ring never had a squeue assigned to it.
7356 		 * We are under ill_lock so we can clean it up
7357 		 * here itself since no one can get to it.
7358 		 */
7359 		rx_ring->rr_blank = NULL;
7360 		rx_ring->rr_handle = NULL;
7361 		rx_ring->rr_sqp = NULL;
7362 		rx_ring->rr_ring_state = ILL_RING_FREE;
7363 		mutex_exit(&ill->ill_lock);
7364 		return;
7365 	}
7366 
7367 	/* Set the state that its being cleaned */
7368 	rx_ring->rr_ring_state = ILL_RING_BEING_FREED;
7369 	ASSERT(sqp != NULL);
7370 	mutex_exit(&ill->ill_lock);
7371 
7372 	/*
7373 	 * Use the preallocated ill_unbind_conn for this purpose
7374 	 */
7375 	connp = ill->ill_poll_capab->ill_unbind_conn;
7376 	mp = &connp->conn_tcp->tcp_closemp;
7377 	CONN_INC_REF(connp);
7378 	squeue_enter(sqp, mp, ip_squeue_clean, connp, NULL);
7379 
7380 	mutex_enter(&ill->ill_lock);
7381 	while (rx_ring->rr_ring_state != ILL_RING_FREE)
7382 		cv_wait(&ill->ill_cv, &ill->ill_lock);
7383 
7384 	mutex_exit(&ill->ill_lock);
7385 }
7386 
7387 static void
7388 ipsq_clean_all(ill_t *ill)
7389 {
7390 	int idx;
7391 
7392 	/*
7393 	 * No need to clean if poll_capab isn't set for this ill
7394 	 */
7395 	if (!(ill->ill_capabilities & ILL_CAPAB_POLL))
7396 		return;
7397 
7398 	ill->ill_capabilities &= ~ILL_CAPAB_POLL;
7399 
7400 	for (idx = 0; idx < ILL_MAX_RINGS; idx++) {
7401 		ill_rx_ring_t *ipr = &ill->ill_poll_capab->ill_ring_tbl[idx];
7402 		ipsq_clean_ring(ill, ipr);
7403 	}
7404 }
7405 
7406 /* ARGSUSED */
7407 int
7408 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
7409     ip_ioctl_cmd_t *ipip, void *ifreq)
7410 {
7411 	ill_t	*ill;
7412 	struct lifreq	*lifr = (struct lifreq *)ifreq;
7413 	boolean_t isv6;
7414 	conn_t	*connp;
7415 
7416 	connp = Q_TO_CONN(q);
7417 	isv6 = connp->conn_af_isv6;
7418 	/*
7419 	 * Set original index.
7420 	 * Failover and failback move logical interfaces
7421 	 * from one physical interface to another.  The
7422 	 * original index indicates the parent of a logical
7423 	 * interface, in other words, the physical interface
7424 	 * the logical interface will be moved back to on
7425 	 * failback.
7426 	 */
7427 
7428 	/*
7429 	 * Don't allow the original index to be changed
7430 	 * for non-failover addresses, autoconfigured
7431 	 * addresses, or IPv6 link local addresses.
7432 	 */
7433 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
7434 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
7435 		return (EINVAL);
7436 	}
7437 	/*
7438 	 * The new original index must be in use by some
7439 	 * physical interface.
7440 	 */
7441 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
7442 	    NULL, NULL);
7443 	if (ill == NULL)
7444 		return (ENXIO);
7445 	ill_refrele(ill);
7446 
7447 	ipif->ipif_orig_ifindex = lifr->lifr_index;
7448 	/*
7449 	 * When this ipif gets failed back, don't
7450 	 * preserve the original id, as it is no
7451 	 * longer applicable.
7452 	 */
7453 	ipif->ipif_orig_ipifid = 0;
7454 	/*
7455 	 * For IPv4, change the original index of any
7456 	 * multicast addresses associated with the
7457 	 * ipif to the new value.
7458 	 */
7459 	if (!isv6) {
7460 		ilm_t *ilm;
7461 
7462 		mutex_enter(&ipif->ipif_ill->ill_lock);
7463 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
7464 		    ilm = ilm->ilm_next) {
7465 			if (ilm->ilm_ipif == ipif) {
7466 				ilm->ilm_orig_ifindex = lifr->lifr_index;
7467 			}
7468 		}
7469 		mutex_exit(&ipif->ipif_ill->ill_lock);
7470 	}
7471 	return (0);
7472 }
7473 
7474 /* ARGSUSED */
7475 int
7476 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
7477     ip_ioctl_cmd_t *ipip, void *ifreq)
7478 {
7479 	struct lifreq *lifr = (struct lifreq *)ifreq;
7480 
7481 	/*
7482 	 * Get the original interface index i.e the one
7483 	 * before FAILOVER if it ever happened.
7484 	 */
7485 	lifr->lifr_index = ipif->ipif_orig_ifindex;
7486 	return (0);
7487 }
7488 
7489 /*
7490  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
7491  * refhold and return the associated ipif
7492  */
7493 int
7494 ip_extract_tunreq(queue_t *q, mblk_t *mp, ipif_t **ipifp, ipsq_func_t func)
7495 {
7496 	boolean_t exists;
7497 	struct iftun_req *ta;
7498 	ipif_t	*ipif;
7499 	ill_t	*ill;
7500 	boolean_t isv6;
7501 	mblk_t	*mp1;
7502 	int	error;
7503 	conn_t	*connp;
7504 
7505 	/* Existence verified in ip_wput_nondata */
7506 	mp1 = mp->b_cont->b_cont;
7507 	ta = (struct iftun_req *)mp1->b_rptr;
7508 	/*
7509 	 * Null terminate the string to protect against buffer
7510 	 * overrun. String was generated by user code and may not
7511 	 * be trusted.
7512 	 */
7513 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
7514 
7515 	connp = Q_TO_CONN(q);
7516 	isv6 = connp->conn_af_isv6;
7517 
7518 	/* Disallows implicit create */
7519 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
7520 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
7521 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error);
7522 	if (ipif == NULL)
7523 		return (error);
7524 
7525 	if (ipif->ipif_id != 0) {
7526 		/*
7527 		 * We really don't want to set/get tunnel parameters
7528 		 * on virtual tunnel interfaces.  Only allow the
7529 		 * base tunnel to do these.
7530 		 */
7531 		ipif_refrele(ipif);
7532 		return (EINVAL);
7533 	}
7534 
7535 	/*
7536 	 * Send down to tunnel mod for ioctl processing.
7537 	 * Will finish ioctl in ip_rput_other().
7538 	 */
7539 	ill = ipif->ipif_ill;
7540 	if (ill->ill_net_type == IRE_LOOPBACK) {
7541 		ipif_refrele(ipif);
7542 		return (EOPNOTSUPP);
7543 	}
7544 
7545 	if (ill->ill_wq == NULL) {
7546 		ipif_refrele(ipif);
7547 		return (ENXIO);
7548 	}
7549 	/*
7550 	 * Mark the ioctl as coming from an IPv6 interface for
7551 	 * tun's convenience.
7552 	 */
7553 	if (ill->ill_isv6)
7554 		ta->ifta_flags |= 0x80000000;
7555 	*ipifp = ipif;
7556 	return (0);
7557 }
7558 
7559 /*
7560  * Parse an ifreq or lifreq struct coming down ioctls and refhold
7561  * and return the associated ipif.
7562  * Return value:
7563  *	Non zero: An error has occurred. ci may not be filled out.
7564  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
7565  *	a held ipif in ci.ci_ipif.
7566  */
7567 int
7568 ip_extract_lifreq_cmn(queue_t *q, mblk_t *mp, int cmd_type, int flags,
7569     cmd_info_t *ci, ipsq_func_t func)
7570 {
7571 	sin_t		*sin;
7572 	sin6_t		*sin6;
7573 	char		*name;
7574 	struct ifreq    *ifr;
7575 	struct lifreq    *lifr;
7576 	ipif_t		*ipif = NULL;
7577 	ill_t		*ill;
7578 	conn_t		*connp;
7579 	boolean_t	isv6;
7580 	struct iocblk   *iocp = (struct iocblk *)mp->b_rptr;
7581 	boolean_t	exists;
7582 	int		err;
7583 	mblk_t		*mp1;
7584 	zoneid_t	zoneid;
7585 
7586 	if (q->q_next != NULL) {
7587 		ill = (ill_t *)q->q_ptr;
7588 		isv6 = ill->ill_isv6;
7589 		connp = NULL;
7590 		zoneid = ALL_ZONES;
7591 	} else {
7592 		ill = NULL;
7593 		connp = Q_TO_CONN(q);
7594 		isv6 = connp->conn_af_isv6;
7595 		zoneid = connp->conn_zoneid;
7596 		if (zoneid == GLOBAL_ZONEID) {
7597 			/* global zone can access ipifs in all zones */
7598 			zoneid = ALL_ZONES;
7599 		}
7600 	}
7601 
7602 	/* Has been checked in ip_wput_nondata */
7603 	mp1 = mp->b_cont->b_cont;
7604 
7605 
7606 	if (cmd_type == IF_CMD) {
7607 		/* This a old style SIOC[GS]IF* command */
7608 		ifr = (struct ifreq *)mp1->b_rptr;
7609 		/*
7610 		 * Null terminate the string to protect against buffer
7611 		 * overrun. String was generated by user code and may not
7612 		 * be trusted.
7613 		 */
7614 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7615 		sin = (sin_t *)&ifr->ifr_addr;
7616 		name = ifr->ifr_name;
7617 		ci->ci_sin = sin;
7618 		ci->ci_sin6 = NULL;
7619 		ci->ci_lifr = (struct lifreq *)ifr;
7620 	} else {
7621 		/* This a new style SIOC[GS]LIF* command */
7622 		ASSERT(cmd_type == LIF_CMD);
7623 		lifr = (struct lifreq *)mp1->b_rptr;
7624 		/*
7625 		 * Null terminate the string to protect against buffer
7626 		 * overrun. String was generated by user code and may not
7627 		 * be trusted.
7628 		 */
7629 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7630 		name = lifr->lifr_name;
7631 		sin = (sin_t *)&lifr->lifr_addr;
7632 		sin6 = (sin6_t *)&lifr->lifr_addr;
7633 		if (iocp->ioc_cmd == SIOCSLIFGROUPNAME) {
7634 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
7635 			    LIFNAMSIZ);
7636 		}
7637 		ci->ci_sin = sin;
7638 		ci->ci_sin6 = sin6;
7639 		ci->ci_lifr = lifr;
7640 	}
7641 
7642 
7643 	if (iocp->ioc_cmd == SIOCSLIFNAME) {
7644 		/*
7645 		 * The ioctl will be failed if the ioctl comes down
7646 		 * an conn stream
7647 		 */
7648 		if (ill == NULL) {
7649 			/*
7650 			 * Not an ill queue, return EINVAL same as the
7651 			 * old error code.
7652 			 */
7653 			return (ENXIO);
7654 		}
7655 		ipif = ill->ill_ipif;
7656 		ipif_refhold(ipif);
7657 	} else {
7658 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
7659 		    &exists, isv6, zoneid,
7660 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err);
7661 		if (ipif == NULL) {
7662 			if (err == EINPROGRESS)
7663 				return (err);
7664 			if (iocp->ioc_cmd == SIOCLIFFAILOVER ||
7665 			    iocp->ioc_cmd == SIOCLIFFAILBACK) {
7666 				/*
7667 				 * Need to try both v4 and v6 since this
7668 				 * ioctl can come down either v4 or v6
7669 				 * socket. The lifreq.lifr_family passed
7670 				 * down by this ioctl is AF_UNSPEC.
7671 				 */
7672 				ipif = ipif_lookup_on_name(name,
7673 				    mi_strlen(name), B_FALSE, &exists, !isv6,
7674 				    zoneid, (connp == NULL) ? q :
7675 				    CONNP_TO_WQ(connp), mp, func, &err);
7676 				if (err == EINPROGRESS)
7677 					return (err);
7678 			}
7679 			err = 0;	/* Ensure we don't use it below */
7680 		}
7681 	}
7682 
7683 	/*
7684 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7685 	 */
7686 	if (ipif != NULL && ipif->ipif_isv6 && cmd_type == IF_CMD) {
7687 		ipif_refrele(ipif);
7688 		return (ENXIO);
7689 	}
7690 
7691 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7692 	    name[0] == '\0') {
7693 		/*
7694 		 * Handle a or a SIOC?IF* with a null name
7695 		 * during plumb (on the ill queue before the I_PLINK).
7696 		 */
7697 		ipif = ill->ill_ipif;
7698 		ipif_refhold(ipif);
7699 	}
7700 
7701 	if (ipif == NULL)
7702 		return (ENXIO);
7703 
7704 	/*
7705 	 * Allow only GET operations if this ipif has been created
7706 	 * temporarily due to a MOVE operation.
7707 	 */
7708 	if (ipif->ipif_replace_zero && !(flags & IPI_REPL)) {
7709 		ipif_refrele(ipif);
7710 		return (EINVAL);
7711 	}
7712 
7713 	ci->ci_ipif = ipif;
7714 	return (0);
7715 }
7716 
7717 /*
7718  * Return the total number of ipifs.
7719  */
7720 static uint_t
7721 ip_get_numifs(zoneid_t zoneid)
7722 {
7723 	uint_t numifs = 0;
7724 	ill_t	*ill;
7725 	ill_walk_context_t	ctx;
7726 	ipif_t	*ipif;
7727 
7728 	rw_enter(&ill_g_lock, RW_READER);
7729 	ill = ILL_START_WALK_V4(&ctx);
7730 
7731 	while (ill != NULL) {
7732 		for (ipif = ill->ill_ipif; ipif != NULL;
7733 		    ipif = ipif->ipif_next) {
7734 			if (ipif->ipif_zoneid == zoneid)
7735 				numifs++;
7736 		}
7737 		ill = ill_next(&ctx, ill);
7738 	}
7739 	rw_exit(&ill_g_lock);
7740 	return (numifs);
7741 }
7742 
7743 /*
7744  * Return the total number of ipifs.
7745  */
7746 static uint_t
7747 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid)
7748 {
7749 	uint_t numifs = 0;
7750 	ill_t	*ill;
7751 	ipif_t	*ipif;
7752 	ill_walk_context_t	ctx;
7753 
7754 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7755 
7756 	rw_enter(&ill_g_lock, RW_READER);
7757 	if (family == AF_INET)
7758 		ill = ILL_START_WALK_V4(&ctx);
7759 	else if (family == AF_INET6)
7760 		ill = ILL_START_WALK_V6(&ctx);
7761 	else
7762 		ill = ILL_START_WALK_ALL(&ctx);
7763 
7764 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7765 		for (ipif = ill->ill_ipif; ipif != NULL;
7766 		    ipif = ipif->ipif_next) {
7767 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7768 			    !(lifn_flags & LIFC_NOXMIT))
7769 				continue;
7770 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7771 			    !(lifn_flags & LIFC_TEMPORARY))
7772 				continue;
7773 			if (((ipif->ipif_flags &
7774 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7775 			    IPIF_DEPRECATED)) ||
7776 			    (ill->ill_phyint->phyint_flags &
7777 			    PHYI_LOOPBACK) ||
7778 			    !(ipif->ipif_flags & IPIF_UP)) &&
7779 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7780 				continue;
7781 
7782 			if (zoneid != ipif->ipif_zoneid &&
7783 			    (zoneid != GLOBAL_ZONEID ||
7784 			    !(lifn_flags & LIFC_ALLZONES)))
7785 				continue;
7786 
7787 			numifs++;
7788 		}
7789 	}
7790 	rw_exit(&ill_g_lock);
7791 	return (numifs);
7792 }
7793 
7794 uint_t
7795 ip_get_lifsrcofnum(ill_t *ill)
7796 {
7797 	uint_t numifs = 0;
7798 	ill_t	*ill_head = ill;
7799 
7800 	/*
7801 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7802 	 * other thread may be trying to relink the ILLs in this usesrc group
7803 	 * and adjusting the ill_usesrc_grp_next pointers
7804 	 */
7805 	rw_enter(&ill_g_usesrc_lock, RW_READER);
7806 	if ((ill->ill_usesrc_ifindex == 0) &&
7807 	    (ill->ill_usesrc_grp_next != NULL)) {
7808 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7809 		    ill = ill->ill_usesrc_grp_next)
7810 			numifs++;
7811 	}
7812 	rw_exit(&ill_g_usesrc_lock);
7813 
7814 	return (numifs);
7815 }
7816 
7817 /* Null values are passed in for ipif, sin, and ifreq */
7818 /* ARGSUSED */
7819 int
7820 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7821     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7822 {
7823 	int *nump;
7824 
7825 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7826 
7827 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
7828 	nump = (int *)mp->b_cont->b_cont->b_rptr;
7829 
7830 	*nump = ip_get_numifs(Q_TO_CONN(q)->conn_zoneid);
7831 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7832 	return (0);
7833 }
7834 
7835 /* Null values are passed in for ipif, sin, and ifreq */
7836 /* ARGSUSED */
7837 int
7838 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7839     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7840 {
7841 	struct lifnum *lifn;
7842 	mblk_t	*mp1;
7843 
7844 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7845 
7846 	/* Existence checked in ip_wput_nondata */
7847 	mp1 = mp->b_cont->b_cont;
7848 
7849 	lifn = (struct lifnum *)mp1->b_rptr;
7850 	switch (lifn->lifn_family) {
7851 	case AF_UNSPEC:
7852 	case AF_INET:
7853 	case AF_INET6:
7854 		break;
7855 	default:
7856 		return (EAFNOSUPPORT);
7857 	}
7858 
7859 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7860 	    Q_TO_CONN(q)->conn_zoneid);
7861 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7862 	return (0);
7863 }
7864 
7865 /* ARGSUSED */
7866 int
7867 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7868     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7869 {
7870 	STRUCT_HANDLE(ifconf, ifc);
7871 	mblk_t *mp1;
7872 	struct iocblk *iocp;
7873 	struct ifreq *ifr;
7874 	ill_walk_context_t	ctx;
7875 	ill_t	*ill;
7876 	ipif_t	*ipif;
7877 	struct sockaddr_in *sin;
7878 	int32_t	ifclen;
7879 	zoneid_t zoneid;
7880 
7881 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
7882 
7883 	ip1dbg(("ip_sioctl_get_ifconf"));
7884 	/* Existence verified in ip_wput_nondata */
7885 	mp1 = mp->b_cont->b_cont;
7886 	iocp = (struct iocblk *)mp->b_rptr;
7887 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7888 
7889 	/*
7890 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
7891 	 * the user buffer address and length into which the list of struct
7892 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
7893 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
7894 	 * the SIOCGIFCONF operation was redefined to simply provide
7895 	 * a large output buffer into which we are supposed to jam the ifreq
7896 	 * array.  The same ioctl command code was used, despite the fact that
7897 	 * both the applications and the kernel code had to change, thus making
7898 	 * it impossible to support both interfaces.
7899 	 *
7900 	 * For reasons not good enough to try to explain, the following
7901 	 * algorithm is used for deciding what to do with one of these:
7902 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
7903 	 * form with the output buffer coming down as the continuation message.
7904 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
7905 	 * and we have to copy in the ifconf structure to find out how big the
7906 	 * output buffer is and where to copy out to.  Sure no problem...
7907 	 *
7908 	 */
7909 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
7910 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
7911 		int numifs = 0;
7912 		size_t ifc_bufsize;
7913 
7914 		/*
7915 		 * Must be (better be!) continuation of a TRANSPARENT
7916 		 * IOCTL.  We just copied in the ifconf structure.
7917 		 */
7918 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
7919 		    (struct ifconf *)mp1->b_rptr);
7920 
7921 		/*
7922 		 * Allocate a buffer to hold requested information.
7923 		 *
7924 		 * If ifc_len is larger than what is needed, we only
7925 		 * allocate what we will use.
7926 		 *
7927 		 * If ifc_len is smaller than what is needed, return
7928 		 * EINVAL.
7929 		 *
7930 		 * XXX: the ill_t structure can hava 2 counters, for
7931 		 * v4 and v6 (not just ill_ipif_up_count) to store the
7932 		 * number of interfaces for a device, so we don't need
7933 		 * to count them here...
7934 		 */
7935 		numifs = ip_get_numifs(zoneid);
7936 
7937 		ifclen = STRUCT_FGET(ifc, ifc_len);
7938 		ifc_bufsize = numifs * sizeof (struct ifreq);
7939 		if (ifc_bufsize > ifclen) {
7940 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7941 				/* old behaviour */
7942 				return (EINVAL);
7943 			} else {
7944 				ifc_bufsize = ifclen;
7945 			}
7946 		}
7947 
7948 		mp1 = mi_copyout_alloc(q, mp,
7949 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
7950 		if (mp1 == NULL)
7951 			return (ENOMEM);
7952 
7953 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
7954 	}
7955 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7956 	/*
7957 	 * the SIOCGIFCONF ioctl only knows about
7958 	 * IPv4 addresses, so don't try to tell
7959 	 * it about interfaces with IPv6-only
7960 	 * addresses. (Last parm 'isv6' is B_FALSE)
7961 	 */
7962 
7963 	ifr = (struct ifreq *)mp1->b_rptr;
7964 
7965 	rw_enter(&ill_g_lock, RW_READER);
7966 	ill = ILL_START_WALK_V4(&ctx);
7967 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7968 		for (ipif = ill->ill_ipif; ipif;
7969 		    ipif = ipif->ipif_next) {
7970 			if (zoneid != ipif->ipif_zoneid)
7971 				continue;
7972 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
7973 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7974 					/* old behaviour */
7975 					rw_exit(&ill_g_lock);
7976 					return (EINVAL);
7977 				} else {
7978 					goto if_copydone;
7979 				}
7980 			}
7981 			(void) ipif_get_name(ipif,
7982 			    ifr->ifr_name,
7983 			    sizeof (ifr->ifr_name));
7984 			sin = (sin_t *)&ifr->ifr_addr;
7985 			*sin = sin_null;
7986 			sin->sin_family = AF_INET;
7987 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7988 			ifr++;
7989 		}
7990 	}
7991 if_copydone:
7992 	rw_exit(&ill_g_lock);
7993 	mp1->b_wptr = (uchar_t *)ifr;
7994 
7995 	if (STRUCT_BUF(ifc) != NULL) {
7996 		STRUCT_FSET(ifc, ifc_len,
7997 			(int)((uchar_t *)ifr - mp1->b_rptr));
7998 	}
7999 	return (0);
8000 }
8001 
8002 /*
8003  * Get the interfaces using the address hosted on the interface passed in,
8004  * as a source adddress
8005  */
8006 /* ARGSUSED */
8007 int
8008 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8009     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8010 {
8011 	mblk_t *mp1;
8012 	ill_t	*ill, *ill_head;
8013 	ipif_t	*ipif, *orig_ipif;
8014 	int	numlifs = 0;
8015 	size_t	lifs_bufsize, lifsmaxlen;
8016 	struct	lifreq *lifr;
8017 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8018 	uint_t	ifindex;
8019 	zoneid_t zoneid;
8020 	int err = 0;
8021 	boolean_t isv6 = B_FALSE;
8022 	struct	sockaddr_in	*sin;
8023 	struct	sockaddr_in6	*sin6;
8024 
8025 	STRUCT_HANDLE(lifsrcof, lifs);
8026 
8027 	ASSERT(q->q_next == NULL);
8028 
8029 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8030 
8031 	/* Existence verified in ip_wput_nondata */
8032 	mp1 = mp->b_cont->b_cont;
8033 
8034 	/*
8035 	 * Must be (better be!) continuation of a TRANSPARENT
8036 	 * IOCTL.  We just copied in the lifsrcof structure.
8037 	 */
8038 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8039 	    (struct lifsrcof *)mp1->b_rptr);
8040 
8041 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8042 		return (EINVAL);
8043 
8044 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8045 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8046 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8047 	    ip_process_ioctl, &err);
8048 	if (ipif == NULL) {
8049 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8050 		    ifindex));
8051 		return (err);
8052 	}
8053 
8054 
8055 	/* Allocate a buffer to hold requested information */
8056 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8057 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8058 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8059 	/* The actual size needed is always returned in lifs_len */
8060 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8061 
8062 	/* If the amount we need is more than what is passed in, abort */
8063 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8064 		ipif_refrele(ipif);
8065 		return (0);
8066 	}
8067 
8068 	mp1 = mi_copyout_alloc(q, mp,
8069 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8070 	if (mp1 == NULL) {
8071 		ipif_refrele(ipif);
8072 		return (ENOMEM);
8073 	}
8074 
8075 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8076 	bzero(mp1->b_rptr, lifs_bufsize);
8077 
8078 	lifr = (struct lifreq *)mp1->b_rptr;
8079 
8080 	ill = ill_head = ipif->ipif_ill;
8081 	orig_ipif = ipif;
8082 
8083 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8084 	rw_enter(&ill_g_usesrc_lock, RW_READER);
8085 	rw_enter(&ill_g_lock, RW_READER);
8086 
8087 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8088 	for (; (ill != NULL) && (ill != ill_head);
8089 	    ill = ill->ill_usesrc_grp_next) {
8090 
8091 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8092 			break;
8093 
8094 		ipif = ill->ill_ipif;
8095 		(void) ipif_get_name(ipif,
8096 		    lifr->lifr_name, sizeof (lifr->lifr_name));
8097 		if (ipif->ipif_isv6) {
8098 			sin6 = (sin6_t *)&lifr->lifr_addr;
8099 			*sin6 = sin6_null;
8100 			sin6->sin6_family = AF_INET6;
8101 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8102 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8103 			    &ipif->ipif_v6net_mask);
8104 		} else {
8105 			sin = (sin_t *)&lifr->lifr_addr;
8106 			*sin = sin_null;
8107 			sin->sin_family = AF_INET;
8108 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8109 			lifr->lifr_addrlen = ip_mask_to_plen(
8110 			    ipif->ipif_net_mask);
8111 		}
8112 		lifr++;
8113 	}
8114 	rw_exit(&ill_g_usesrc_lock);
8115 	rw_exit(&ill_g_lock);
8116 	ipif_refrele(orig_ipif);
8117 	mp1->b_wptr = (uchar_t *)lifr;
8118 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8119 
8120 	return (0);
8121 }
8122 
8123 /* ARGSUSED */
8124 int
8125 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8126     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8127 {
8128 	mblk_t *mp1;
8129 	int	list;
8130 	ill_t	*ill;
8131 	ipif_t	*ipif;
8132 	int	flags;
8133 	int	numlifs = 0;
8134 	size_t	lifc_bufsize;
8135 	struct	lifreq *lifr;
8136 	sa_family_t	family;
8137 	struct	sockaddr_in	*sin;
8138 	struct	sockaddr_in6	*sin6;
8139 	ill_walk_context_t	ctx;
8140 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8141 	int32_t	lifclen;
8142 	zoneid_t zoneid;
8143 	STRUCT_HANDLE(lifconf, lifc);
8144 
8145 	ip1dbg(("ip_sioctl_get_lifconf"));
8146 
8147 	ASSERT(q->q_next == NULL);
8148 
8149 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8150 
8151 	/* Existence verified in ip_wput_nondata */
8152 	mp1 = mp->b_cont->b_cont;
8153 
8154 	/*
8155 	 * An extended version of SIOCGIFCONF that takes an
8156 	 * additional address family and flags field.
8157 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8158 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8159 	 * interfaces are omitted.
8160 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8161 	 * unless LIFC_TEMPORARY is specified.
8162 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8163 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8164 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8165 	 * has priority over LIFC_NOXMIT.
8166 	 */
8167 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8168 
8169 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8170 		return (EINVAL);
8171 
8172 	/*
8173 	 * Must be (better be!) continuation of a TRANSPARENT
8174 	 * IOCTL.  We just copied in the lifconf structure.
8175 	 */
8176 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8177 
8178 	family = STRUCT_FGET(lifc, lifc_family);
8179 	flags = STRUCT_FGET(lifc, lifc_flags);
8180 
8181 	switch (family) {
8182 	case AF_UNSPEC:
8183 		/*
8184 		 * walk all ILL's.
8185 		 */
8186 		list = MAX_G_HEADS;
8187 		break;
8188 	case AF_INET:
8189 		/*
8190 		 * walk only IPV4 ILL's.
8191 		 */
8192 		list = IP_V4_G_HEAD;
8193 		break;
8194 	case AF_INET6:
8195 		/*
8196 		 * walk only IPV6 ILL's.
8197 		 */
8198 		list = IP_V6_G_HEAD;
8199 		break;
8200 	default:
8201 		return (EAFNOSUPPORT);
8202 	}
8203 
8204 	/*
8205 	 * Allocate a buffer to hold requested information.
8206 	 *
8207 	 * If lifc_len is larger than what is needed, we only
8208 	 * allocate what we will use.
8209 	 *
8210 	 * If lifc_len is smaller than what is needed, return
8211 	 * EINVAL.
8212 	 */
8213 	numlifs = ip_get_numlifs(family, flags, zoneid);
8214 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8215 	lifclen = STRUCT_FGET(lifc, lifc_len);
8216 	if (lifc_bufsize > lifclen) {
8217 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8218 			return (EINVAL);
8219 		else
8220 			lifc_bufsize = lifclen;
8221 	}
8222 
8223 	mp1 = mi_copyout_alloc(q, mp,
8224 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8225 	if (mp1 == NULL)
8226 		return (ENOMEM);
8227 
8228 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8229 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8230 
8231 	lifr = (struct lifreq *)mp1->b_rptr;
8232 
8233 	rw_enter(&ill_g_lock, RW_READER);
8234 	ill = ill_first(list, list, &ctx);
8235 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8236 		for (ipif = ill->ill_ipif; ipif != NULL;
8237 		    ipif = ipif->ipif_next) {
8238 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8239 			    !(flags & LIFC_NOXMIT))
8240 				continue;
8241 
8242 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8243 			    !(flags & LIFC_TEMPORARY))
8244 				continue;
8245 
8246 			if (((ipif->ipif_flags &
8247 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8248 			    IPIF_DEPRECATED)) ||
8249 			    (ill->ill_phyint->phyint_flags &
8250 			    PHYI_LOOPBACK) ||
8251 			    !(ipif->ipif_flags & IPIF_UP)) &&
8252 			    (flags & LIFC_EXTERNAL_SOURCE))
8253 				continue;
8254 
8255 			if (zoneid != ipif->ipif_zoneid &&
8256 			    (zoneid != GLOBAL_ZONEID ||
8257 			    !(flags & LIFC_ALLZONES)))
8258 				continue;
8259 
8260 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8261 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8262 					rw_exit(&ill_g_lock);
8263 					return (EINVAL);
8264 				} else {
8265 					goto lif_copydone;
8266 				}
8267 			}
8268 
8269 			(void) ipif_get_name(ipif,
8270 				lifr->lifr_name,
8271 				sizeof (lifr->lifr_name));
8272 			if (ipif->ipif_isv6) {
8273 				sin6 = (sin6_t *)&lifr->lifr_addr;
8274 				*sin6 = sin6_null;
8275 				sin6->sin6_family = AF_INET6;
8276 				sin6->sin6_addr =
8277 				ipif->ipif_v6lcl_addr;
8278 				lifr->lifr_addrlen =
8279 				ip_mask_to_plen_v6(
8280 				    &ipif->ipif_v6net_mask);
8281 			} else {
8282 				sin = (sin_t *)&lifr->lifr_addr;
8283 				*sin = sin_null;
8284 				sin->sin_family = AF_INET;
8285 				sin->sin_addr.s_addr =
8286 				    ipif->ipif_lcl_addr;
8287 				lifr->lifr_addrlen =
8288 				    ip_mask_to_plen(
8289 				    ipif->ipif_net_mask);
8290 			}
8291 			lifr++;
8292 		}
8293 	}
8294 lif_copydone:
8295 	rw_exit(&ill_g_lock);
8296 
8297 	mp1->b_wptr = (uchar_t *)lifr;
8298 	if (STRUCT_BUF(lifc) != NULL) {
8299 		STRUCT_FSET(lifc, lifc_len,
8300 			(int)((uchar_t *)lifr - mp1->b_rptr));
8301 	}
8302 	return (0);
8303 }
8304 
8305 static void
8306 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
8307 {
8308 	ip6_asp_t *table;
8309 	size_t table_size;
8310 	mblk_t *data_mp;
8311 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8312 
8313 	/* These two ioctls are I_STR only */
8314 	if (iocp->ioc_count == TRANSPARENT) {
8315 		miocnak(q, mp, 0, EINVAL);
8316 		return;
8317 	}
8318 
8319 	data_mp = mp->b_cont;
8320 	if (data_mp == NULL) {
8321 		/* The user passed us a NULL argument */
8322 		table = NULL;
8323 		table_size = iocp->ioc_count;
8324 	} else {
8325 		/*
8326 		 * The user provided a table.  The stream head
8327 		 * may have copied in the user data in chunks,
8328 		 * so make sure everything is pulled up
8329 		 * properly.
8330 		 */
8331 		if (MBLKL(data_mp) < iocp->ioc_count) {
8332 			mblk_t *new_data_mp;
8333 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
8334 			    NULL) {
8335 				miocnak(q, mp, 0, ENOMEM);
8336 				return;
8337 			}
8338 			freemsg(data_mp);
8339 			data_mp = new_data_mp;
8340 			mp->b_cont = data_mp;
8341 		}
8342 		table = (ip6_asp_t *)data_mp->b_rptr;
8343 		table_size = iocp->ioc_count;
8344 	}
8345 
8346 	switch (iocp->ioc_cmd) {
8347 	case SIOCGIP6ADDRPOLICY:
8348 		iocp->ioc_rval = ip6_asp_get(table, table_size);
8349 		if (iocp->ioc_rval == -1)
8350 			iocp->ioc_error = EINVAL;
8351 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8352 		else if (table != NULL &&
8353 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
8354 			ip6_asp_t *src = table;
8355 			ip6_asp32_t *dst = (void *)table;
8356 			int count = table_size / sizeof (ip6_asp_t);
8357 			int i;
8358 
8359 			/*
8360 			 * We need to do an in-place shrink of the array
8361 			 * to match the alignment attributes of the
8362 			 * 32-bit ABI looking at it.
8363 			 */
8364 			/* LINTED: logical expression always true: op "||" */
8365 			ASSERT(sizeof (*src) > sizeof (*dst));
8366 			for (i = 1; i < count; i++)
8367 				bcopy(src + i, dst + i, sizeof (*dst));
8368 		}
8369 #endif
8370 		break;
8371 
8372 	case SIOCSIP6ADDRPOLICY:
8373 		ASSERT(mp->b_prev == NULL);
8374 		mp->b_prev = (void *)q;
8375 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8376 		/*
8377 		 * We pass in the datamodel here so that the ip6_asp_replace()
8378 		 * routine can handle converting from 32-bit to native formats
8379 		 * where necessary.
8380 		 *
8381 		 * A better way to handle this might be to convert the inbound
8382 		 * data structure here, and hang it off a new 'mp'; thus the
8383 		 * ip6_asp_replace() logic would always be dealing with native
8384 		 * format data structures..
8385 		 *
8386 		 * (An even simpler way to handle these ioctls is to just
8387 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
8388 		 * and just recompile everything that depends on it.)
8389 		 */
8390 #endif
8391 		ip6_asp_replace(mp, table, table_size, B_FALSE,
8392 		    iocp->ioc_flag & IOC_MODELS);
8393 		return;
8394 	}
8395 
8396 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
8397 	qreply(q, mp);
8398 }
8399 
8400 static void
8401 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
8402 {
8403 	mblk_t 		*data_mp;
8404 	struct dstinforeq	*dir;
8405 	uint8_t		*end, *cur;
8406 	in6_addr_t	*daddr, *saddr;
8407 	ipaddr_t	v4daddr;
8408 	ire_t		*ire;
8409 	char		*slabel, *dlabel;
8410 	boolean_t	isipv4;
8411 	int		match_ire;
8412 	ill_t		*dst_ill;
8413 	ipif_t		*src_ipif, *ire_ipif;
8414 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8415 	zoneid_t	zoneid;
8416 
8417 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8418 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8419 
8420 	/*
8421 	 * This ioctl is I_STR only, and must have a
8422 	 * data mblk following the M_IOCTL mblk.
8423 	 */
8424 	data_mp = mp->b_cont;
8425 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
8426 		miocnak(q, mp, 0, EINVAL);
8427 		return;
8428 	}
8429 
8430 	if (MBLKL(data_mp) < iocp->ioc_count) {
8431 		mblk_t *new_data_mp;
8432 
8433 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
8434 			miocnak(q, mp, 0, ENOMEM);
8435 			return;
8436 		}
8437 		freemsg(data_mp);
8438 		data_mp = new_data_mp;
8439 		mp->b_cont = data_mp;
8440 	}
8441 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
8442 
8443 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
8444 	    end - cur >= sizeof (struct dstinforeq);
8445 	    cur += sizeof (struct dstinforeq)) {
8446 		dir = (struct dstinforeq *)cur;
8447 		daddr = &dir->dir_daddr;
8448 		saddr = &dir->dir_saddr;
8449 
8450 		/*
8451 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
8452 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
8453 		 * and ipif_select_source[_v6]() do not.
8454 		 */
8455 		dir->dir_dscope = ip_addr_scope_v6(daddr);
8456 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence);
8457 
8458 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
8459 		if (isipv4) {
8460 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
8461 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
8462 			    0, NULL, NULL, zoneid, 0, match_ire);
8463 		} else {
8464 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
8465 			    0, NULL, NULL, zoneid, 0, match_ire);
8466 		}
8467 		if (ire == NULL) {
8468 			dir->dir_dreachable = 0;
8469 
8470 			/* move on to next dst addr */
8471 			continue;
8472 		}
8473 		dir->dir_dreachable = 1;
8474 
8475 		ire_ipif = ire->ire_ipif;
8476 		if (ire_ipif == NULL)
8477 			goto next_dst;
8478 
8479 		/*
8480 		 * We expect to get back an interface ire or a
8481 		 * gateway ire cache entry.  For both types, the
8482 		 * output interface is ire_ipif->ipif_ill.
8483 		 */
8484 		dst_ill = ire_ipif->ipif_ill;
8485 		dir->dir_dmactype = dst_ill->ill_mactype;
8486 
8487 		if (isipv4) {
8488 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
8489 		} else {
8490 			src_ipif = ipif_select_source_v6(dst_ill,
8491 			    daddr, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
8492 			    zoneid);
8493 		}
8494 		if (src_ipif == NULL)
8495 			goto next_dst;
8496 
8497 		*saddr = src_ipif->ipif_v6lcl_addr;
8498 		dir->dir_sscope = ip_addr_scope_v6(saddr);
8499 		slabel = ip6_asp_lookup(saddr, NULL);
8500 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
8501 		dir->dir_sdeprecated =
8502 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
8503 		ipif_refrele(src_ipif);
8504 next_dst:
8505 		ire_refrele(ire);
8506 	}
8507 	miocack(q, mp, iocp->ioc_count, 0);
8508 }
8509 
8510 
8511 /*
8512  * Check if this is an address assigned to this machine.
8513  * Skips interfaces that are down by using ire checks.
8514  * Translates mapped addresses to v4 addresses and then
8515  * treats them as such, returning true if the v4 address
8516  * associated with this mapped address is configured.
8517  * Note: Applications will have to be careful what they do
8518  * with the response; use of mapped addresses limits
8519  * what can be done with the socket, especially with
8520  * respect to socket options and ioctls - neither IPv4
8521  * options nor IPv6 sticky options/ancillary data options
8522  * may be used.
8523  */
8524 /* ARGSUSED */
8525 int
8526 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8527     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8528 {
8529 	struct sioc_addrreq *sia;
8530 	sin_t *sin;
8531 	ire_t *ire;
8532 	mblk_t *mp1;
8533 	zoneid_t zoneid;
8534 
8535 	ip1dbg(("ip_sioctl_tmyaddr"));
8536 
8537 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8538 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8539 
8540 	/* Existence verified in ip_wput_nondata */
8541 	mp1 = mp->b_cont->b_cont;
8542 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8543 	sin = (sin_t *)&sia->sa_addr;
8544 	switch (sin->sin_family) {
8545 	case AF_INET6: {
8546 		sin6_t *sin6 = (sin6_t *)sin;
8547 
8548 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8549 			ipaddr_t v4_addr;
8550 
8551 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8552 			    v4_addr);
8553 			ire = ire_ctable_lookup(v4_addr, 0,
8554 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8555 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8556 		} else {
8557 			in6_addr_t v6addr;
8558 
8559 			v6addr = sin6->sin6_addr;
8560 			ire = ire_ctable_lookup_v6(&v6addr, 0,
8561 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8562 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8563 		}
8564 		break;
8565 	}
8566 	case AF_INET: {
8567 		ipaddr_t v4addr;
8568 
8569 		v4addr = sin->sin_addr.s_addr;
8570 		ire = ire_ctable_lookup(v4addr, 0,
8571 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8572 		    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8573 		break;
8574 	}
8575 	default:
8576 		return (EAFNOSUPPORT);
8577 	}
8578 	if (ire != NULL) {
8579 		sia->sa_res = 1;
8580 		ire_refrele(ire);
8581 	} else {
8582 		sia->sa_res = 0;
8583 	}
8584 	return (0);
8585 }
8586 
8587 /*
8588  * Check if this is an address assigned on-link i.e. neighbor,
8589  * and makes sure it's reachable from the current zone.
8590  * Returns true for my addresses as well.
8591  * Translates mapped addresses to v4 addresses and then
8592  * treats them as such, returning true if the v4 address
8593  * associated with this mapped address is configured.
8594  * Note: Applications will have to be careful what they do
8595  * with the response; use of mapped addresses limits
8596  * what can be done with the socket, especially with
8597  * respect to socket options and ioctls - neither IPv4
8598  * options nor IPv6 sticky options/ancillary data options
8599  * may be used.
8600  */
8601 /* ARGSUSED */
8602 int
8603 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8604     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8605 {
8606 	struct sioc_addrreq *sia;
8607 	sin_t *sin;
8608 	mblk_t	*mp1;
8609 	ire_t *ire = NULL;
8610 	zoneid_t zoneid;
8611 
8612 	ip1dbg(("ip_sioctl_tonlink"));
8613 
8614 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8615 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8616 
8617 	/* Existence verified in ip_wput_nondata */
8618 	mp1 = mp->b_cont->b_cont;
8619 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8620 	sin = (sin_t *)&sia->sa_addr;
8621 
8622 	/*
8623 	 * Match addresses with a zero gateway field to avoid
8624 	 * routes going through a router.
8625 	 * Exclude broadcast and multicast addresses.
8626 	 */
8627 	switch (sin->sin_family) {
8628 	case AF_INET6: {
8629 		sin6_t *sin6 = (sin6_t *)sin;
8630 
8631 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8632 			ipaddr_t v4_addr;
8633 
8634 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8635 			    v4_addr);
8636 			if (!CLASSD(v4_addr)) {
8637 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
8638 				    NULL, NULL, zoneid, MATCH_IRE_GW);
8639 			}
8640 		} else {
8641 			in6_addr_t v6addr;
8642 			in6_addr_t v6gw;
8643 
8644 			v6addr = sin6->sin6_addr;
8645 			v6gw = ipv6_all_zeros;
8646 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8647 				ire = ire_route_lookup_v6(&v6addr, 0,
8648 				    &v6gw, 0, NULL, NULL, zoneid,
8649 				    MATCH_IRE_GW);
8650 			}
8651 		}
8652 		break;
8653 	}
8654 	case AF_INET: {
8655 		ipaddr_t v4addr;
8656 
8657 		v4addr = sin->sin_addr.s_addr;
8658 		if (!CLASSD(v4addr)) {
8659 			ire = ire_route_lookup(v4addr, 0, 0, 0,
8660 			    NULL, NULL, zoneid, MATCH_IRE_GW);
8661 		}
8662 		break;
8663 	}
8664 	default:
8665 		return (EAFNOSUPPORT);
8666 	}
8667 	sia->sa_res = 0;
8668 	if (ire != NULL) {
8669 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
8670 		    IRE_LOCAL|IRE_LOOPBACK)) {
8671 			sia->sa_res = 1;
8672 		}
8673 		ire_refrele(ire);
8674 	}
8675 	return (0);
8676 }
8677 
8678 /*
8679  * TBD: implement when kernel maintaines a list of site prefixes.
8680  */
8681 /* ARGSUSED */
8682 int
8683 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8684     ip_ioctl_cmd_t *ipip, void *ifreq)
8685 {
8686 	return (ENXIO);
8687 }
8688 
8689 /* ARGSUSED */
8690 int
8691 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8692     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8693 {
8694 	ill_t  		*ill;
8695 	mblk_t		*mp1;
8696 	conn_t		*connp;
8697 	boolean_t	success;
8698 
8699 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
8700 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
8701 	/* ioctl comes down on an conn */
8702 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8703 	connp = Q_TO_CONN(q);
8704 
8705 	mp->b_datap->db_type = M_IOCTL;
8706 
8707 	/*
8708 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
8709 	 * The original mp contains contaminated b_next values due to 'mi',
8710 	 * which is needed to do the mi_copy_done. Unfortunately if we
8711 	 * send down the original mblk itself and if we are popped due to an
8712 	 * an unplumb before the response comes back from tunnel,
8713 	 * the streamhead (which does a freemsg) will see this contaminated
8714 	 * message and the assertion in freemsg about non-null b_next/b_prev
8715 	 * will panic a DEBUG kernel.
8716 	 */
8717 	mp1 = copymsg(mp);
8718 	if (mp1 == NULL)
8719 		return (ENOMEM);
8720 
8721 	ill = ipif->ipif_ill;
8722 	mutex_enter(&connp->conn_lock);
8723 	mutex_enter(&ill->ill_lock);
8724 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
8725 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
8726 		    mp, 0);
8727 	} else {
8728 		success = ill_pending_mp_add(ill, connp, mp);
8729 	}
8730 	mutex_exit(&ill->ill_lock);
8731 	mutex_exit(&connp->conn_lock);
8732 
8733 	if (success) {
8734 		ip1dbg(("sending down tunparam request "));
8735 		putnext(ill->ill_wq, mp1);
8736 		return (EINPROGRESS);
8737 	} else {
8738 		/* The conn has started closing */
8739 		freemsg(mp1);
8740 		return (EINTR);
8741 	}
8742 }
8743 
8744 static int
8745 ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp, sin_t *sin,
8746     boolean_t x_arp_ioctl, boolean_t if_arp_ioctl)
8747 {
8748 	mblk_t *mp1;
8749 	mblk_t *mp2;
8750 	mblk_t *pending_mp;
8751 	ipaddr_t ipaddr;
8752 	area_t *area;
8753 	struct iocblk *iocp;
8754 	conn_t *connp;
8755 	struct arpreq *ar;
8756 	struct xarpreq *xar;
8757 	boolean_t success;
8758 	int flags, alength;
8759 	char *lladdr;
8760 
8761 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8762 	connp = Q_TO_CONN(q);
8763 
8764 	iocp = (struct iocblk *)mp->b_rptr;
8765 	/*
8766 	 * ill has already been set depending on whether
8767 	 * bsd style or interface style ioctl.
8768 	 */
8769 	ASSERT(ill != NULL);
8770 
8771 	/*
8772 	 * Is this one of the new SIOC*XARP ioctls?
8773 	 */
8774 	if (x_arp_ioctl) {
8775 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8776 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8777 		ar = NULL;
8778 
8779 		flags = xar->xarp_flags;
8780 		lladdr = LLADDR(&xar->xarp_ha);
8781 		/*
8782 		 * Validate against user's link layer address length
8783 		 * input and name and addr length limits.
8784 		 */
8785 		alength = ill->ill_phys_addr_length;
8786 		if (iocp->ioc_cmd == SIOCSXARP) {
8787 			if (alength != xar->xarp_ha.sdl_alen ||
8788 			    (alength + xar->xarp_ha.sdl_nlen >
8789 			    sizeof (xar->xarp_ha.sdl_data)))
8790 				return (EINVAL);
8791 		}
8792 	} else {
8793 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8794 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8795 		xar = NULL;
8796 
8797 		flags = ar->arp_flags;
8798 		lladdr = ar->arp_ha.sa_data;
8799 		/*
8800 		 * Theoretically, the sa_family could tell us what link
8801 		 * layer type this operation is trying to deal with. By
8802 		 * common usage AF_UNSPEC means ethernet. We'll assume
8803 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8804 		 * for now. Our new SIOC*XARP ioctls can be used more
8805 		 * generally.
8806 		 *
8807 		 * If the underlying media happens to have a non 6 byte
8808 		 * address, arp module will fail set/get, but the del
8809 		 * operation will succeed.
8810 		 */
8811 		alength = 6;
8812 		if ((iocp->ioc_cmd != SIOCDARP) &&
8813 		    (alength != ill->ill_phys_addr_length)) {
8814 			return (EINVAL);
8815 		}
8816 	}
8817 
8818 	/*
8819 	 * We are going to pass up to ARP a packet chain that looks
8820 	 * like:
8821 	 *
8822 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
8823 	 *
8824 	 * Get a copy of the original IOCTL mblk to head the chain,
8825 	 * to be sent up (in mp1). Also get another copy to store
8826 	 * in the ill_pending_mp list, for matching the response
8827 	 * when it comes back from ARP.
8828 	 */
8829 	mp1 = copyb(mp);
8830 	pending_mp = copymsg(mp);
8831 	if (mp1 == NULL || pending_mp == NULL) {
8832 		if (mp1 != NULL)
8833 			freeb(mp1);
8834 		if (pending_mp != NULL)
8835 			ip_ioctl_freemsg(pending_mp);
8836 		return (ENOMEM);
8837 	}
8838 
8839 	ipaddr = sin->sin_addr.s_addr;
8840 
8841 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
8842 	    (caddr_t)&ipaddr);
8843 	if (mp2 == NULL) {
8844 		freeb(mp1);
8845 		ip_ioctl_freemsg(pending_mp);
8846 		return (ENOMEM);
8847 	}
8848 	/* Put together the chain. */
8849 	mp1->b_cont = mp2;
8850 	mp1->b_datap->db_type = M_IOCTL;
8851 	mp2->b_cont = mp;
8852 	mp2->b_datap->db_type = M_DATA;
8853 
8854 	iocp = (struct iocblk *)mp1->b_rptr;
8855 
8856 	/*
8857 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
8858 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
8859 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
8860 	 * ioc_count field; set ioc_count to be correct.
8861 	 */
8862 	iocp->ioc_count = MBLKL(mp1->b_cont);
8863 
8864 	/*
8865 	 * Set the proper command in the ARP message.
8866 	 * Convert the SIOC{G|S|D}ARP calls into our
8867 	 * AR_ENTRY_xxx calls.
8868 	 */
8869 	area = (area_t *)mp2->b_rptr;
8870 	switch (iocp->ioc_cmd) {
8871 	case SIOCDARP:
8872 	case SIOCDXARP:
8873 		/*
8874 		 * We defer deleting the corresponding IRE until
8875 		 * we return from arp.
8876 		 */
8877 		area->area_cmd = AR_ENTRY_DELETE;
8878 		area->area_proto_mask_offset = 0;
8879 		break;
8880 	case SIOCGARP:
8881 	case SIOCGXARP:
8882 		area->area_cmd = AR_ENTRY_SQUERY;
8883 		area->area_proto_mask_offset = 0;
8884 		break;
8885 	case SIOCSARP:
8886 	case SIOCSXARP: {
8887 		/*
8888 		 * Delete the corresponding ire to make sure IP will
8889 		 * pick up any change from arp.
8890 		 */
8891 		if (!if_arp_ioctl) {
8892 			(void) ip_ire_clookup_and_delete(ipaddr, NULL);
8893 			break;
8894 		} else {
8895 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
8896 			if (ipif != NULL) {
8897 				(void) ip_ire_clookup_and_delete(ipaddr, ipif);
8898 				ipif_refrele(ipif);
8899 			}
8900 			break;
8901 		}
8902 	}
8903 	}
8904 	iocp->ioc_cmd = area->area_cmd;
8905 
8906 	/*
8907 	 * Before sending 'mp' to ARP, we have to clear the b_next
8908 	 * and b_prev. Otherwise if STREAMS encounters such a message
8909 	 * in freemsg(), (because ARP can close any time) it can cause
8910 	 * a panic. But mi code needs the b_next and b_prev values of
8911 	 * mp->b_cont, to complete the ioctl. So we store it here
8912 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
8913 	 * when the response comes down from ARP.
8914 	 */
8915 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
8916 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
8917 	mp->b_cont->b_next = NULL;
8918 	mp->b_cont->b_prev = NULL;
8919 
8920 	mutex_enter(&connp->conn_lock);
8921 	mutex_enter(&ill->ill_lock);
8922 	/* conn has not yet started closing, hence this can't fail */
8923 	success = ill_pending_mp_add(ill, connp, pending_mp);
8924 	ASSERT(success);
8925 	mutex_exit(&ill->ill_lock);
8926 	mutex_exit(&connp->conn_lock);
8927 
8928 	/*
8929 	 * Fill in the rest of the ARP operation fields.
8930 	 */
8931 	area->area_hw_addr_length = alength;
8932 	bcopy(lladdr,
8933 	    (char *)area + area->area_hw_addr_offset,
8934 	    area->area_hw_addr_length);
8935 	/* Translate the flags. */
8936 	if (flags & ATF_PERM)
8937 		area->area_flags |= ACE_F_PERMANENT;
8938 	if (flags & ATF_PUBL)
8939 		area->area_flags |= ACE_F_PUBLISH;
8940 
8941 	/*
8942 	 * Up to ARP it goes.  The response will come
8943 	 * back in ip_wput as an M_IOCACK message, and
8944 	 * will be handed to ip_sioctl_iocack for
8945 	 * completion.
8946 	 */
8947 	putnext(ill->ill_rq, mp1);
8948 	return (EINPROGRESS);
8949 }
8950 
8951 /* ARGSUSED */
8952 int
8953 ip_sioctl_xarp(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8954     ip_ioctl_cmd_t *ipip, void *ifreq)
8955 {
8956 	struct xarpreq *xar;
8957 	boolean_t isv6;
8958 	mblk_t	*mp1;
8959 	int	err;
8960 	conn_t	*connp;
8961 	int ifnamelen;
8962 	ire_t	*ire = NULL;
8963 	ill_t	*ill = NULL;
8964 	struct sockaddr_in *sin;
8965 	boolean_t if_arp_ioctl = B_FALSE;
8966 
8967 	/* ioctl comes down on an conn */
8968 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8969 	connp = Q_TO_CONN(q);
8970 	isv6 = connp->conn_af_isv6;
8971 
8972 	/* Existance verified in ip_wput_nondata */
8973 	mp1 = mp->b_cont->b_cont;
8974 
8975 	ASSERT(MBLKL(mp1) >= sizeof (*xar));
8976 	xar = (struct xarpreq *)mp1->b_rptr;
8977 	sin = (sin_t *)&xar->xarp_pa;
8978 
8979 	if (isv6 || (xar->xarp_ha.sdl_family != AF_LINK) ||
8980 	    (xar->xarp_pa.ss_family != AF_INET))
8981 		return (ENXIO);
8982 
8983 	ifnamelen = xar->xarp_ha.sdl_nlen;
8984 	if (ifnamelen != 0) {
8985 		char	*cptr, cval;
8986 
8987 		if (ifnamelen >= LIFNAMSIZ)
8988 			return (EINVAL);
8989 
8990 		/*
8991 		 * Instead of bcopying a bunch of bytes,
8992 		 * null-terminate the string in-situ.
8993 		 */
8994 		cptr = xar->xarp_ha.sdl_data + ifnamelen;
8995 		cval = *cptr;
8996 		*cptr = '\0';
8997 		ill = ill_lookup_on_name(xar->xarp_ha.sdl_data,
8998 		    B_FALSE, isv6, CONNP_TO_WQ(connp), mp, ip_process_ioctl,
8999 		    &err, NULL);
9000 		*cptr = cval;
9001 		if (ill == NULL)
9002 			return (err);
9003 		if (ill->ill_net_type != IRE_IF_RESOLVER) {
9004 			ill_refrele(ill);
9005 			return (ENXIO);
9006 		}
9007 
9008 		if_arp_ioctl = B_TRUE;
9009 	} else {
9010 		/*
9011 		 * PSARC 2003/088 states that if sdl_nlen == 0, it behaves
9012 		 * as an extended BSD ioctl. The kernel uses the IP address
9013 		 * to figure out the network interface.
9014 		 */
9015 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES);
9016 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9017 		    ((ill = ire_to_ill(ire)) == NULL)) {
9018 			if (ire != NULL)
9019 				ire_refrele(ire);
9020 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9021 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9022 			    MATCH_IRE_TYPE);
9023 			if ((ire == NULL) ||
9024 			    ((ill = ire_to_ill(ire)) == NULL)) {
9025 				if (ire != NULL)
9026 					ire_refrele(ire);
9027 				return (ENXIO);
9028 			}
9029 		}
9030 		ASSERT(ire != NULL && ill != NULL);
9031 	}
9032 
9033 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_TRUE, if_arp_ioctl);
9034 	if (if_arp_ioctl)
9035 		ill_refrele(ill);
9036 	if (ire != NULL)
9037 		ire_refrele(ire);
9038 
9039 	return (err);
9040 }
9041 
9042 /*
9043  * ARP IOCTLs.
9044  * How does IP get in the business of fronting ARP configuration/queries?
9045  * Well its like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9046  * are by tradition passed in through a datagram socket.  That lands in IP.
9047  * As it happens, this is just as well since the interface is quite crude in
9048  * that it passes in no information about protocol or hardware types, or
9049  * interface association.  After making the protocol assumption, IP is in
9050  * the position to look up the name of the ILL, which ARP will need, and
9051  * format a request that can be handled by ARP.	 The request is passed up
9052  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9053  * back a response.  ARP supports its own set of more general IOCTLs, in
9054  * case anyone is interested.
9055  */
9056 /* ARGSUSED */
9057 int
9058 ip_sioctl_arp(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9059     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9060 {
9061 	struct arpreq *ar;
9062 	struct sockaddr_in *sin;
9063 	ire_t	*ire;
9064 	boolean_t isv6;
9065 	mblk_t	*mp1;
9066 	int	err;
9067 	conn_t	*connp;
9068 	ill_t	*ill;
9069 
9070 	/* ioctl comes down on an conn */
9071 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9072 	connp = Q_TO_CONN(q);
9073 	isv6 = connp->conn_af_isv6;
9074 	if (isv6)
9075 		return (ENXIO);
9076 
9077 	/* Existance verified in ip_wput_nondata */
9078 	mp1 = mp->b_cont->b_cont;
9079 
9080 	ar = (struct arpreq *)mp1->b_rptr;
9081 	sin = (sin_t *)&ar->arp_pa;
9082 
9083 	/*
9084 	 * We need to let ARP know on which interface the IP
9085 	 * address has an ARP mapping. In the IPMP case, a
9086 	 * simple forwarding table lookup will return the
9087 	 * IRE_IF_RESOLVER for the first interface in the group,
9088 	 * which might not be the interface on which the
9089 	 * requested IP address was resolved due to the ill
9090 	 * selection algorithm (see ip_newroute_get_dst_ill()).
9091 	 * So we do a cache table lookup first: if the IRE cache
9092 	 * entry for the IP address is still there, it will
9093 	 * contain the ill pointer for the right interface, so
9094 	 * we use that. If the cache entry has been flushed, we
9095 	 * fall back to the forwarding table lookup. This should
9096 	 * be rare enough since IRE cache entries have a longer
9097 	 * life expectancy than ARP cache entries.
9098 	 */
9099 	ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES);
9100 	if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9101 	    ((ill = ire_to_ill(ire)) == NULL)) {
9102 		if (ire != NULL)
9103 			ire_refrele(ire);
9104 		ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9105 		    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9106 		    MATCH_IRE_TYPE);
9107 		if ((ire == NULL) || ((ill = ire_to_ill(ire)) == NULL)) {
9108 			if (ire != NULL)
9109 				ire_refrele(ire);
9110 			return (ENXIO);
9111 		}
9112 	}
9113 	ASSERT(ire != NULL && ill != NULL);
9114 
9115 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_FALSE, B_FALSE);
9116 	ire_refrele(ire);
9117 	return (err);
9118 }
9119 
9120 /*
9121  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9122  * atomically set/clear the muxids. Also complete the ioctl by acking or
9123  * naking it.  Note that the code is structured such that the link type,
9124  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9125  * its clones use the persistent link, while pppd(1M) and perhaps many
9126  * other daemons may use non-persistent link.  When combined with some
9127  * ill_t states, linking and unlinking lower streams may be used as
9128  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9129  */
9130 /* ARGSUSED */
9131 void
9132 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9133 {
9134 	mblk_t *mp1;
9135 	mblk_t *mp2;
9136 	struct linkblk *li;
9137 	queue_t	*ipwq;
9138 	char	*name;
9139 	struct qinit *qinfo;
9140 	struct ipmx_s *ipmxp;
9141 	ill_t	*ill = NULL;
9142 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9143 	int	err = 0;
9144 	boolean_t	entered_ipsq = B_FALSE;
9145 	boolean_t islink;
9146 	queue_t *dwq = NULL;
9147 
9148 	ASSERT(iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_PUNLINK ||
9149 	    iocp->ioc_cmd == I_LINK || iocp->ioc_cmd == I_UNLINK);
9150 
9151 	islink = (iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_LINK) ?
9152 	    B_TRUE : B_FALSE;
9153 
9154 	mp1 = mp->b_cont;	/* This is the linkblk info */
9155 	li = (struct linkblk *)mp1->b_rptr;
9156 
9157 	/*
9158 	 * ARP has added this special mblk, and the utility is asking us
9159 	 * to perform consistency checks, and also atomically set the
9160 	 * muxid. Ifconfig is an example.  It achieves this by using
9161 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9162 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9163 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9164 	 * and other comments in this routine for more details.
9165 	 */
9166 	mp2 = mp1->b_cont;	/* This is added by ARP */
9167 
9168 	/*
9169 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9170 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9171 	 * get the special mblk above.  For backward compatibility, we just
9172 	 * return success.  The utility will use SIOCSLIFMUXID to store
9173 	 * the muxids.  This is not atomic, and can leave the streams
9174 	 * unplumbable if the utility is interrrupted, before it does the
9175 	 * SIOCSLIFMUXID.
9176 	 */
9177 	if (mp2 == NULL) {
9178 		/*
9179 		 * At this point we don't know whether or not this is the
9180 		 * IP module stream or the ARP device stream.  We need to
9181 		 * walk the lower stream in order to find this out, since
9182 		 * the capability negotiation is done only on the IP module
9183 		 * stream.  IP module instance is identified by the module
9184 		 * name IP, non-null q_next, and it's wput not being ip_lwput.
9185 		 * STREAMS ensures that the lower stream (l_qbot) will not
9186 		 * vanish until this ioctl completes. So we can safely walk
9187 		 * the stream or refer to the q_ptr.
9188 		 */
9189 		ipwq = li->l_qbot;
9190 		while (ipwq != NULL) {
9191 			qinfo = ipwq->q_qinfo;
9192 			name = qinfo->qi_minfo->mi_idname;
9193 			if (name != NULL && name[0] != NULL &&
9194 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
9195 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
9196 			    (ipwq->q_next != NULL)) {
9197 				break;
9198 			}
9199 			ipwq = ipwq->q_next;
9200 		}
9201 		/*
9202 		 * This looks like an IP module stream, so trigger
9203 		 * the capability reset or re-negotiation if necessary.
9204 		 */
9205 		if (ipwq != NULL) {
9206 			ill = ipwq->q_ptr;
9207 			ASSERT(ill != NULL);
9208 
9209 			if (ipsq == NULL) {
9210 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
9211 				    ip_sioctl_plink, NEW_OP, B_TRUE);
9212 				if (ipsq == NULL)
9213 					return;
9214 				entered_ipsq = B_TRUE;
9215 			}
9216 			ASSERT(IAM_WRITER_ILL(ill));
9217 			/*
9218 			 * Store the upper read queue of the module
9219 			 * immediately below IP, and count the total
9220 			 * number of lower modules.  Do this only
9221 			 * for I_PLINK or I_LINK event.
9222 			 */
9223 			ill->ill_lmod_rq = NULL;
9224 			ill->ill_lmod_cnt = 0;
9225 			if (islink && (dwq = ipwq->q_next) != NULL) {
9226 				ill->ill_lmod_rq = RD(dwq);
9227 
9228 				while (dwq != NULL) {
9229 					ill->ill_lmod_cnt++;
9230 					dwq = dwq->q_next;
9231 				}
9232 			}
9233 			/*
9234 			 * There's no point in resetting or re-negotiating if
9235 			 * we are not bound to the driver, so only do this if
9236 			 * the DLPI state is idle (up); we assume such state
9237 			 * since ill_ipif_up_count gets incremented in
9238 			 * ipif_up_done(), which is after we are bound to the
9239 			 * driver.  Note that in the case of logical
9240 			 * interfaces, IP won't rebind to the driver unless
9241 			 * the ill_ipif_up_count is 0, meaning that all other
9242 			 * IP interfaces (including the main ipif) are in the
9243 			 * down state.  Because of this, we use such counter
9244 			 * as an indicator, instead of relying on the IPIF_UP
9245 			 * flag, which is per ipif instance.
9246 			 */
9247 			if (ill->ill_ipif_up_count > 0) {
9248 				if (islink)
9249 					ill_capability_probe(ill);
9250 				else
9251 					ill_capability_reset(ill);
9252 			}
9253 		}
9254 		goto done;
9255 	}
9256 
9257 	/*
9258 	 * This is an I_{P}LINK sent down by ifconfig on
9259 	 * /dev/arp. ARP has appended this last (3rd) mblk,
9260 	 * giving more info. STREAMS ensures that the lower
9261 	 * stream (l_qbot) will not vanish until this ioctl
9262 	 * completes. So we can safely walk the stream or refer
9263 	 * to the q_ptr.
9264 	 */
9265 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9266 	if (ipmxp->ipmx_arpdev_stream) {
9267 		/*
9268 		 * The operation is occuring on the arp-device
9269 		 * stream.
9270 		 */
9271 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9272 		    q, mp, ip_sioctl_plink, &err, NULL);
9273 		if (ill == NULL) {
9274 			if (err == EINPROGRESS) {
9275 				return;
9276 			} else {
9277 				err = EINVAL;
9278 				goto done;
9279 			}
9280 		}
9281 
9282 		if (ipsq == NULL) {
9283 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9284 			    NEW_OP, B_TRUE);
9285 			if (ipsq == NULL) {
9286 				ill_refrele(ill);
9287 				return;
9288 			}
9289 			entered_ipsq = B_TRUE;
9290 		}
9291 		ASSERT(IAM_WRITER_ILL(ill));
9292 		ill_refrele(ill);
9293 		/*
9294 		 * To ensure consistency between IP and ARP,
9295 		 * the following LIFO scheme is used in
9296 		 * plink/punlink. (IP first, ARP last).
9297 		 * This is because the muxid's are stored
9298 		 * in the IP stream on the ill.
9299 		 *
9300 		 * I_{P}LINK: ifconfig plinks the IP stream before
9301 		 * plinking the ARP stream. On an arp-dev
9302 		 * stream, IP checks that it is not yet
9303 		 * plinked, and it also checks that the
9304 		 * corresponding IP stream is already plinked.
9305 		 *
9306 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream
9307 		 * before punlinking the IP stream. IP does
9308 		 * not allow punlink of the IP stream unless
9309 		 * the arp stream has been punlinked.
9310 		 *
9311 		 */
9312 		if ((islink &&
9313 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9314 		    (!islink &&
9315 		    ill->ill_arp_muxid != li->l_index)) {
9316 			err = EINVAL;
9317 			goto done;
9318 		}
9319 		if (islink) {
9320 			ill->ill_arp_muxid = li->l_index;
9321 		} else {
9322 			ill->ill_arp_muxid = 0;
9323 		}
9324 	} else {
9325 		/*
9326 		 * This must be the IP module stream with or
9327 		 * without arp. Walk the stream and locate the
9328 		 * IP module. An IP module instance is
9329 		 * identified by the module name IP, non-null
9330 		 * q_next, and it's wput not being ip_lwput.
9331 		 */
9332 		ipwq = li->l_qbot;
9333 		while (ipwq != NULL) {
9334 			qinfo = ipwq->q_qinfo;
9335 			name = qinfo->qi_minfo->mi_idname;
9336 			if (name != NULL && name[0] != NULL &&
9337 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
9338 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
9339 			    (ipwq->q_next != NULL)) {
9340 				break;
9341 			}
9342 			ipwq = ipwq->q_next;
9343 		}
9344 		if (ipwq != NULL) {
9345 			ill = ipwq->q_ptr;
9346 			ASSERT(ill != NULL);
9347 
9348 			if (ipsq == NULL) {
9349 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
9350 				    ip_sioctl_plink, NEW_OP, B_TRUE);
9351 				if (ipsq == NULL)
9352 					return;
9353 				entered_ipsq = B_TRUE;
9354 			}
9355 			ASSERT(IAM_WRITER_ILL(ill));
9356 			/*
9357 			 * Return error if the ip_mux_id is
9358 			 * non-zero and command is I_{P}LINK.
9359 			 * If command is I_{P}UNLINK, return
9360 			 * error if the arp-devstr is not
9361 			 * yet punlinked.
9362 			 */
9363 			if ((islink && ill->ill_ip_muxid != 0) ||
9364 			    (!islink && ill->ill_arp_muxid != 0)) {
9365 				err = EINVAL;
9366 				goto done;
9367 			}
9368 			ill->ill_lmod_rq = NULL;
9369 			ill->ill_lmod_cnt = 0;
9370 			if (islink) {
9371 				/*
9372 				 * Store the upper read queue of the module
9373 				 * immediately below IP, and count the total
9374 				 * number of lower modules.
9375 				 */
9376 				if ((dwq = ipwq->q_next) != NULL) {
9377 					ill->ill_lmod_rq = RD(dwq);
9378 
9379 					while (dwq != NULL) {
9380 						ill->ill_lmod_cnt++;
9381 						dwq = dwq->q_next;
9382 					}
9383 				}
9384 				ill->ill_ip_muxid = li->l_index;
9385 			} else {
9386 				ill->ill_ip_muxid = 0;
9387 			}
9388 
9389 			/*
9390 			 * See comments above about resetting/re-
9391 			 * negotiating driver sub-capabilities.
9392 			 */
9393 			if (ill->ill_ipif_up_count > 0) {
9394 				if (islink)
9395 					ill_capability_probe(ill);
9396 				else
9397 					ill_capability_reset(ill);
9398 			}
9399 		}
9400 	}
9401 done:
9402 	iocp->ioc_count = 0;
9403 	iocp->ioc_error = err;
9404 	if (err == 0)
9405 		mp->b_datap->db_type = M_IOCACK;
9406 	else
9407 		mp->b_datap->db_type = M_IOCNAK;
9408 	qreply(q, mp);
9409 
9410 	/* Conn was refheld in ip_sioctl_copyin_setup */
9411 	if (CONN_Q(q))
9412 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9413 	if (entered_ipsq)
9414 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
9415 }
9416 
9417 /*
9418  * Search the ioctl command in the ioctl tables and return a pointer
9419  * to the ioctl command information. The ioctl command tables are
9420  * static and fully populated at compile time.
9421  */
9422 ip_ioctl_cmd_t *
9423 ip_sioctl_lookup(int ioc_cmd)
9424 {
9425 	int index;
9426 	ip_ioctl_cmd_t *ipip;
9427 	ip_ioctl_cmd_t *ipip_end;
9428 
9429 	if (ioc_cmd == IPI_DONTCARE)
9430 		return (NULL);
9431 
9432 	/*
9433 	 * Do a 2 step search. First search the indexed table
9434 	 * based on the least significant byte of the ioctl cmd.
9435 	 * If we don't find a match, then search the misc table
9436 	 * serially.
9437 	 */
9438 	index = ioc_cmd & 0xFF;
9439 	if (index < ip_ndx_ioctl_count) {
9440 		ipip = &ip_ndx_ioctl_table[index];
9441 		if (ipip->ipi_cmd == ioc_cmd) {
9442 			/* Found a match in the ndx table */
9443 			return (ipip);
9444 		}
9445 	}
9446 
9447 	/* Search the misc table */
9448 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
9449 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
9450 		if (ipip->ipi_cmd == ioc_cmd)
9451 			/* Found a match in the misc table */
9452 			return (ipip);
9453 	}
9454 
9455 	return (NULL);
9456 }
9457 
9458 /*
9459  * Wrapper function for resuming deferred ioctl processing
9460  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
9461  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
9462  */
9463 /* ARGSUSED */
9464 void
9465 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
9466     void *dummy_arg)
9467 {
9468 	ip_sioctl_copyin_setup(q, mp);
9469 }
9470 
9471 /*
9472  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
9473  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
9474  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
9475  * We establish here the size of the block to be copied in.  mi_copyin
9476  * arranges for this to happen, an processing continues in ip_wput with
9477  * an M_IOCDATA message.
9478  */
9479 void
9480 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
9481 {
9482 	int	copyin_size;
9483 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9484 	ip_ioctl_cmd_t *ipip;
9485 	cred_t *cr;
9486 
9487 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
9488 	if (ipip == NULL) {
9489 		/*
9490 		 * The ioctl is not one we understand or own.
9491 		 * Pass it along to be processed down stream,
9492 		 * if this is a module instance of IP, else nak
9493 		 * the ioctl.
9494 		 */
9495 		if (q->q_next == NULL) {
9496 			goto nak;
9497 		} else {
9498 			putnext(q, mp);
9499 			return;
9500 		}
9501 	}
9502 
9503 	/*
9504 	 * If this is deferred, then we will do all the checks when we
9505 	 * come back.
9506 	 */
9507 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
9508 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup()) {
9509 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
9510 		return;
9511 	}
9512 
9513 	/*
9514 	 * Only allow a very small subset of IP ioctls on this stream if
9515 	 * IP is a module and not a driver. Allowing ioctls to be processed
9516 	 * in this case may cause assert failures or data corruption.
9517 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
9518 	 * ioctls allowed on an IP module stream, after which this stream
9519 	 * normally becomes a multiplexor (at which time the stream head
9520 	 * will fail all ioctls).
9521 	 */
9522 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
9523 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
9524 			/*
9525 			 * Pass common Streams ioctls which the IP
9526 			 * module does not own or consume along to
9527 			 * be processed down stream.
9528 			 */
9529 			putnext(q, mp);
9530 			return;
9531 		} else {
9532 			goto nak;
9533 		}
9534 	}
9535 
9536 	/* Make sure we have ioctl data to process. */
9537 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
9538 		goto nak;
9539 
9540 	/*
9541 	 * Prefer dblk credential over ioctl credential; some synthesized
9542 	 * ioctls have kcred set because there's no way to crhold()
9543 	 * a credential in some contexts.  (ioc_cr is not crfree() by
9544 	 * the framework; the caller of ioctl needs to hold the reference
9545 	 * for the duration of the call).
9546 	 */
9547 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
9548 
9549 	/* Make sure normal users don't send down privileged ioctls */
9550 	if ((ipip->ipi_flags & IPI_PRIV) &&
9551 	    (cr != NULL) && secpolicy_net_config(cr, B_TRUE) != 0) {
9552 		/* We checked the privilege earlier but log it here */
9553 		miocnak(q, mp, 0, secpolicy_net_config(cr, B_FALSE));
9554 		return;
9555 	}
9556 
9557 	/*
9558 	 * The ioctl command tables can only encode fixed length
9559 	 * ioctl data. If the length is variable, the table will
9560 	 * encode the length as zero. Such special cases are handled
9561 	 * below in the switch.
9562 	 */
9563 	if (ipip->ipi_copyin_size != 0) {
9564 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
9565 		return;
9566 	}
9567 
9568 	switch (iocp->ioc_cmd) {
9569 	case O_SIOCGIFCONF:
9570 	case SIOCGIFCONF:
9571 		/*
9572 		 * This IOCTL is hilarious.  See comments in
9573 		 * ip_sioctl_get_ifconf for the story.
9574 		 */
9575 		if (iocp->ioc_count == TRANSPARENT)
9576 			copyin_size = SIZEOF_STRUCT(ifconf,
9577 			    iocp->ioc_flag);
9578 		else
9579 			copyin_size = iocp->ioc_count;
9580 		mi_copyin(q, mp, NULL, copyin_size);
9581 		return;
9582 
9583 	case O_SIOCGLIFCONF:
9584 	case SIOCGLIFCONF:
9585 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
9586 		mi_copyin(q, mp, NULL, copyin_size);
9587 		return;
9588 
9589 	case SIOCGLIFSRCOF:
9590 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
9591 		mi_copyin(q, mp, NULL, copyin_size);
9592 		return;
9593 	case SIOCGIP6ADDRPOLICY:
9594 		ip_sioctl_ip6addrpolicy(q, mp);
9595 		ip6_asp_table_refrele();
9596 		return;
9597 
9598 	case SIOCSIP6ADDRPOLICY:
9599 		ip_sioctl_ip6addrpolicy(q, mp);
9600 		return;
9601 
9602 	case SIOCGDSTINFO:
9603 		ip_sioctl_dstinfo(q, mp);
9604 		ip6_asp_table_refrele();
9605 		return;
9606 
9607 	case I_PLINK:
9608 	case I_PUNLINK:
9609 	case I_LINK:
9610 	case I_UNLINK:
9611 		/*
9612 		 * We treat non-persistent link similarly as the persistent
9613 		 * link case, in terms of plumbing/unplumbing, as well as
9614 		 * dynamic re-plumbing events indicator.  See comments
9615 		 * in ip_sioctl_plink() for more.
9616 		 *
9617 		 * Request can be enqueued in the 'ipsq' while waiting
9618 		 * to become exclusive. So bump up the conn ref.
9619 		 */
9620 		if (CONN_Q(q))
9621 			CONN_INC_REF(Q_TO_CONN(q));
9622 		ip_sioctl_plink(NULL, q, mp, NULL);
9623 		return;
9624 
9625 	case ND_GET:
9626 	case ND_SET:
9627 		/*
9628 		 * Use of the nd table requires holding the reader lock.
9629 		 * Modifying the nd table thru nd_load/nd_unload requires
9630 		 * the writer lock.
9631 		 */
9632 		rw_enter(&ip_g_nd_lock, RW_READER);
9633 		if (nd_getset(q, ip_g_nd, mp)) {
9634 			rw_exit(&ip_g_nd_lock);
9635 
9636 			if (iocp->ioc_error)
9637 				iocp->ioc_count = 0;
9638 			mp->b_datap->db_type = M_IOCACK;
9639 			qreply(q, mp);
9640 			return;
9641 		}
9642 		rw_exit(&ip_g_nd_lock);
9643 		/*
9644 		 * We don't understand this subioctl of ND_GET / ND_SET.
9645 		 * Maybe intended for some driver / module below us
9646 		 */
9647 		if (q->q_next) {
9648 			putnext(q, mp);
9649 		} else {
9650 			iocp->ioc_error = ENOENT;
9651 			mp->b_datap->db_type = M_IOCNAK;
9652 			iocp->ioc_count = 0;
9653 			qreply(q, mp);
9654 		}
9655 		return;
9656 
9657 	case IP_IOCTL:
9658 		ip_wput_ioctl(q, mp);
9659 		return;
9660 	default:
9661 		cmn_err(CE_PANIC, "should not happen ");
9662 	}
9663 nak:
9664 	if (mp->b_cont != NULL) {
9665 		freemsg(mp->b_cont);
9666 		mp->b_cont = NULL;
9667 	}
9668 	iocp->ioc_error = EINVAL;
9669 	mp->b_datap->db_type = M_IOCNAK;
9670 	iocp->ioc_count = 0;
9671 	qreply(q, mp);
9672 }
9673 
9674 /* ip_wput hands off ARP IOCTL responses to us */
9675 void
9676 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
9677 {
9678 	struct arpreq *ar;
9679 	struct xarpreq *xar;
9680 	area_t	*area;
9681 	mblk_t	*area_mp;
9682 	struct iocblk *iocp;
9683 	mblk_t	*orig_ioc_mp, *tmp;
9684 	struct iocblk	*orig_iocp;
9685 	ill_t *ill;
9686 	conn_t *connp = NULL;
9687 	uint_t ioc_id;
9688 	mblk_t *pending_mp;
9689 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
9690 	int *flagsp;
9691 	char *storage = NULL;
9692 	sin_t *sin;
9693 	ipaddr_t addr;
9694 	int err;
9695 
9696 	ill = q->q_ptr;
9697 	ASSERT(ill != NULL);
9698 
9699 	/*
9700 	 * We should get back from ARP a packet chain that looks like:
9701 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9702 	 */
9703 	if (!(area_mp = mp->b_cont) ||
9704 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
9705 	    !(orig_ioc_mp = area_mp->b_cont) ||
9706 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
9707 		freemsg(mp);
9708 		return;
9709 	}
9710 
9711 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
9712 
9713 	tmp = (orig_ioc_mp->b_cont)->b_cont;
9714 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
9715 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
9716 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
9717 		x_arp_ioctl = B_TRUE;
9718 		xar = (struct xarpreq *)tmp->b_rptr;
9719 		sin = (sin_t *)&xar->xarp_pa;
9720 		flagsp = &xar->xarp_flags;
9721 		storage = xar->xarp_ha.sdl_data;
9722 		if (xar->xarp_ha.sdl_nlen != 0)
9723 			ifx_arp_ioctl = B_TRUE;
9724 	} else {
9725 		ar = (struct arpreq *)tmp->b_rptr;
9726 		sin = (sin_t *)&ar->arp_pa;
9727 		flagsp = &ar->arp_flags;
9728 		storage = ar->arp_ha.sa_data;
9729 	}
9730 
9731 	iocp = (struct iocblk *)mp->b_rptr;
9732 
9733 	/*
9734 	 * Pick out the originating queue based on the ioc_id.
9735 	 */
9736 	ioc_id = iocp->ioc_id;
9737 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
9738 	if (pending_mp == NULL) {
9739 		ASSERT(connp == NULL);
9740 		ip_ioctl_freemsg(mp);
9741 		return;
9742 	}
9743 	ASSERT(connp != NULL);
9744 	q = CONNP_TO_WQ(connp);
9745 
9746 	/* Uncouple the internally generated IOCTL from the original one */
9747 	area = (area_t *)area_mp->b_rptr;
9748 	area_mp->b_cont = NULL;
9749 
9750 	/*
9751 	 * Restore the b_next and b_prev used by mi code. This is needed
9752 	 * to complete the ioctl using mi* functions. We stored them in
9753 	 * the pending mp prior to sending the request to ARP.
9754 	 */
9755 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
9756 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
9757 	ip_ioctl_freemsg(pending_mp);
9758 
9759 	/*
9760 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
9761 	 * Catch the case where there is an IRE_CACHE by no entry in the
9762 	 * arp table.
9763 	 */
9764 	addr = sin->sin_addr.s_addr;
9765 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
9766 		ire_t			*ire;
9767 		dl_unitdata_req_t	*dlup;
9768 		mblk_t			*llmp;
9769 		int			addr_len;
9770 		ill_t			*ipsqill = NULL;
9771 
9772 		if (ifx_arp_ioctl) {
9773 			/*
9774 			 * There's no need to lookup the ill, since
9775 			 * we've already done that when we started
9776 			 * processing the ioctl and sent the message
9777 			 * to ARP on that ill.  So use the ill that
9778 			 * is stored in q->q_ptr.
9779 			 */
9780 			ipsqill = ill;
9781 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
9782 			    ipsqill->ill_ipif, ALL_ZONES,
9783 			    MATCH_IRE_TYPE | MATCH_IRE_ILL);
9784 		} else {
9785 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
9786 			    NULL, ALL_ZONES, MATCH_IRE_TYPE);
9787 			if (ire != NULL)
9788 				ipsqill = ire_to_ill(ire);
9789 		}
9790 
9791 		if ((x_arp_ioctl) && (ipsqill != NULL))
9792 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
9793 
9794 		if (ire != NULL) {
9795 			*flagsp = ATF_INUSE;
9796 			llmp = ire->ire_dlureq_mp;
9797 			if (llmp != NULL && ipsqill != NULL) {
9798 				uchar_t *macaddr;
9799 
9800 				addr_len = ipsqill->ill_phys_addr_length;
9801 				if (x_arp_ioctl && ((addr_len +
9802 				    ipsqill->ill_name_length) >
9803 				    sizeof (xar->xarp_ha.sdl_data))) {
9804 					ire_refrele(ire);
9805 					freemsg(mp);
9806 					ip_ioctl_finish(q, orig_ioc_mp,
9807 					    EINVAL, NO_COPYOUT, NULL, NULL);
9808 					return;
9809 				}
9810 				*flagsp |= ATF_COM;
9811 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
9812 				if (ipsqill->ill_sap_length < 0)
9813 					macaddr = llmp->b_rptr +
9814 					    dlup->dl_dest_addr_offset;
9815 				else
9816 					macaddr = llmp->b_rptr +
9817 					    dlup->dl_dest_addr_offset +
9818 					    ipsqill->ill_sap_length;
9819 				/*
9820 				 * For SIOCGARP, MAC address length
9821 				 * validation has already been done
9822 				 * before the ioctl was issued to ARP to
9823 				 * allow it to progress only on 6 byte
9824 				 * addressable (ethernet like) media. Thus
9825 				 * the mac address copying can not overwrite
9826 				 * the sa_data area below.
9827 				 */
9828 				bcopy(macaddr, storage, addr_len);
9829 			}
9830 			/* Ditch the internal IOCTL. */
9831 			freemsg(mp);
9832 			ire_refrele(ire);
9833 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL, NULL);
9834 			return;
9835 		}
9836 	}
9837 
9838 	/*
9839 	 * Delete the coresponding IRE_CACHE if any.
9840 	 * Reset the error if there was one (in case there was no entry
9841 	 * in arp.)
9842 	 */
9843 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
9844 		ipif_t *ipintf = NULL;
9845 
9846 		if (ifx_arp_ioctl) {
9847 			/*
9848 			 * There's no need to lookup the ill, since
9849 			 * we've already done that when we started
9850 			 * processing the ioctl and sent the message
9851 			 * to ARP on that ill.  So use the ill that
9852 			 * is stored in q->q_ptr.
9853 			 */
9854 			ipintf = ill->ill_ipif;
9855 		}
9856 		if (ip_ire_clookup_and_delete(addr, ipintf)) {
9857 			/*
9858 			 * The address in "addr" may be an entry for a
9859 			 * router. If that's true, then any off-net
9860 			 * IRE_CACHE entries that go through the router
9861 			 * with address "addr" must be clobbered. Use
9862 			 * ire_walk to achieve this goal.
9863 			 */
9864 			if (ifx_arp_ioctl)
9865 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
9866 				    ire_delete_cache_gw, (char *)&addr, ill);
9867 			else
9868 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
9869 				    ALL_ZONES);
9870 			iocp->ioc_error = 0;
9871 		}
9872 	}
9873 
9874 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
9875 		err = iocp->ioc_error;
9876 		freemsg(mp);
9877 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL, NULL);
9878 		return;
9879 	}
9880 
9881 	/*
9882 	 * Completion of an SIOCG{X}ARP.  Translate the information from
9883 	 * the area_t into the struct {x}arpreq.
9884 	 */
9885 	if (x_arp_ioctl) {
9886 		storage += ill_xarp_info(&xar->xarp_ha, ill);
9887 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
9888 		    sizeof (xar->xarp_ha.sdl_data)) {
9889 			freemsg(mp);
9890 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL,
9891 			    NO_COPYOUT, NULL, NULL);
9892 			return;
9893 		}
9894 	}
9895 	*flagsp = ATF_INUSE;
9896 	if (area->area_flags & ACE_F_PERMANENT)
9897 		*flagsp |= ATF_PERM;
9898 	if (area->area_flags & ACE_F_PUBLISH)
9899 		*flagsp |= ATF_PUBL;
9900 	if (area->area_hw_addr_length != 0) {
9901 		*flagsp |= ATF_COM;
9902 		/*
9903 		 * For SIOCGARP, MAC address length validation has
9904 		 * already been done before the ioctl was issued to ARP
9905 		 * to allow it to progress only on 6 byte addressable
9906 		 * (ethernet like) media. Thus the mac address copying
9907 		 * can not overwrite the sa_data area below.
9908 		 */
9909 		bcopy((char *)area + area->area_hw_addr_offset,
9910 		    storage, area->area_hw_addr_length);
9911 	}
9912 
9913 	/* Ditch the internal IOCTL. */
9914 	freemsg(mp);
9915 	/* Complete the original. */
9916 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL, NULL);
9917 }
9918 
9919 /*
9920  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
9921  * interface) create the next available logical interface for this
9922  * physical interface.
9923  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
9924  * ipif with the specified name.
9925  *
9926  * If the address family is not AF_UNSPEC then set the address as well.
9927  *
9928  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
9929  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
9930  *
9931  * Executed as a writer on the ill or ill group.
9932  * So no lock is needed to traverse the ipif chain, or examine the
9933  * phyint flags.
9934  */
9935 /* ARGSUSED */
9936 int
9937 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9938     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9939 {
9940 	mblk_t	*mp1;
9941 	struct lifreq *lifr;
9942 	boolean_t	isv6;
9943 	boolean_t	exists;
9944 	char 	*name;
9945 	char	*endp;
9946 	char	*cp;
9947 	int	namelen;
9948 	ipif_t	*ipif;
9949 	long	id;
9950 	ipsq_t	*ipsq;
9951 	ill_t	*ill;
9952 	sin_t	*sin;
9953 	int	err = 0;
9954 	boolean_t found_sep = B_FALSE;
9955 	conn_t	*connp;
9956 	zoneid_t zoneid;
9957 	int	orig_ifindex = 0;
9958 
9959 	ip1dbg(("ip_sioctl_addif\n"));
9960 	/* Existence of mp1 has been checked in ip_wput_nondata */
9961 	mp1 = mp->b_cont->b_cont;
9962 	/*
9963 	 * Null terminate the string to protect against buffer
9964 	 * overrun. String was generated by user code and may not
9965 	 * be trusted.
9966 	 */
9967 	lifr = (struct lifreq *)mp1->b_rptr;
9968 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
9969 	name = lifr->lifr_name;
9970 	ASSERT(CONN_Q(q));
9971 	connp = Q_TO_CONN(q);
9972 	isv6 = connp->conn_af_isv6;
9973 	zoneid = connp->conn_zoneid;
9974 	namelen = mi_strlen(name);
9975 	if (namelen == 0)
9976 		return (EINVAL);
9977 
9978 	exists = B_FALSE;
9979 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
9980 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
9981 		/*
9982 		 * Allow creating lo0 using SIOCLIFADDIF.
9983 		 * can't be any other writer thread. So can pass null below
9984 		 * for the last 4 args to ipif_lookup_name.
9985 		 */
9986 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen,
9987 		    B_TRUE, &exists, isv6, zoneid, NULL, NULL, NULL, NULL);
9988 		/* Prevent any further action */
9989 		if (ipif == NULL) {
9990 			return (ENOBUFS);
9991 		} else if (!exists) {
9992 			/* We created the ipif now and as writer */
9993 			ipif_refrele(ipif);
9994 			return (0);
9995 		} else {
9996 			ill = ipif->ipif_ill;
9997 			ill_refhold(ill);
9998 			ipif_refrele(ipif);
9999 		}
10000 	} else {
10001 		/* Look for a colon in the name. */
10002 		endp = &name[namelen];
10003 		for (cp = endp; --cp > name; ) {
10004 			if (*cp == IPIF_SEPARATOR_CHAR) {
10005 				found_sep = B_TRUE;
10006 				/*
10007 				 * Reject any non-decimal aliases for plumbing
10008 				 * of logical interfaces. Aliases with leading
10009 				 * zeroes are also rejected as they introduce
10010 				 * ambiguity in the naming of the interfaces.
10011 				 * Comparing with "0" takes care of all such
10012 				 * cases.
10013 				 */
10014 				if ((strncmp("0", cp+1, 1)) == 0)
10015 					return (EINVAL);
10016 
10017 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10018 				    id <= 0 || *endp != '\0') {
10019 					return (EINVAL);
10020 				}
10021 				*cp = '\0';
10022 				break;
10023 			}
10024 		}
10025 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10026 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL);
10027 		if (found_sep)
10028 			*cp = IPIF_SEPARATOR_CHAR;
10029 		if (ill == NULL)
10030 			return (err);
10031 	}
10032 
10033 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10034 	    B_TRUE);
10035 
10036 	/*
10037 	 * Release the refhold due to the lookup, now that we are excl
10038 	 * or we are just returning
10039 	 */
10040 	ill_refrele(ill);
10041 
10042 	if (ipsq == NULL)
10043 		return (EINPROGRESS);
10044 
10045 	/*
10046 	 * If the interface is failed, inactive or offlined, look for a working
10047 	 * interface in the ill group and create the ipif there. If we can't
10048 	 * find a good interface, create the ipif anyway so that in.mpathd can
10049 	 * move it to the first repaired interface.
10050 	 */
10051 	if ((ill->ill_phyint->phyint_flags &
10052 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10053 	    ill->ill_phyint->phyint_groupname_len != 0) {
10054 		phyint_t *phyi;
10055 		char *groupname = ill->ill_phyint->phyint_groupname;
10056 
10057 		/*
10058 		 * We're looking for a working interface, but it doesn't matter
10059 		 * if it's up or down; so instead of following the group lists,
10060 		 * we look at each physical interface and compare the groupname.
10061 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10062 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10063 		 * Otherwise we create the ipif on the failed interface.
10064 		 */
10065 		rw_enter(&ill_g_lock, RW_READER);
10066 		phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
10067 		for (; phyi != NULL;
10068 		    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
10069 		    phyi, AVL_AFTER)) {
10070 			if (phyi->phyint_groupname_len == 0)
10071 				continue;
10072 			ASSERT(phyi->phyint_groupname != NULL);
10073 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10074 			    !(phyi->phyint_flags &
10075 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10076 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10077 			    (phyi->phyint_illv4 != NULL))) {
10078 				break;
10079 			}
10080 		}
10081 		rw_exit(&ill_g_lock);
10082 
10083 		if (phyi != NULL) {
10084 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10085 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10086 			    phyi->phyint_illv4);
10087 		}
10088 	}
10089 
10090 	/*
10091 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10092 	 * before or after us.
10093 	 */
10094 	ASSERT(IAM_WRITER_ILL(ill));
10095 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10096 
10097 	if (found_sep && orig_ifindex == 0) {
10098 		/* Now see if there is an IPIF with this unit number. */
10099 		for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
10100 			if (ipif->ipif_id == id) {
10101 				err = EEXIST;
10102 				goto done;
10103 			}
10104 		}
10105 	}
10106 
10107 	/*
10108 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10109 	 * of lo0. We never come here when we plumb lo0:0. It
10110 	 * happens in ipif_lookup_on_name.
10111 	 * The specified unit number is ignored when we create the ipif on a
10112 	 * different interface. However, we save it in ipif_orig_ipifid below so
10113 	 * that the ipif fails back to the right position.
10114 	 */
10115 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10116 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10117 		err = ENOBUFS;
10118 		goto done;
10119 	}
10120 
10121 	/* Return created name with ioctl */
10122 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10123 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10124 	ip1dbg(("created %s\n", lifr->lifr_name));
10125 
10126 	/* Set address */
10127 	sin = (sin_t *)&lifr->lifr_addr;
10128 	if (sin->sin_family != AF_UNSPEC) {
10129 		err = ip_sioctl_addr(ipif, sin, q, mp,
10130 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10131 	}
10132 
10133 	/* Set ifindex and unit number for failback */
10134 	if (err == 0 && orig_ifindex != 0) {
10135 		ipif->ipif_orig_ifindex = orig_ifindex;
10136 		if (found_sep) {
10137 			ipif->ipif_orig_ipifid = id;
10138 		}
10139 	}
10140 
10141 done:
10142 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10143 	return (err);
10144 }
10145 
10146 /*
10147  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10148  * interface) delete it based on the IP address (on this physical interface).
10149  * Otherwise delete it based on the ipif_id.
10150  * Also, special handling to allow a removeif of lo0.
10151  */
10152 /* ARGSUSED */
10153 int
10154 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10155     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10156 {
10157 	conn_t		*connp;
10158 	ill_t		*ill = ipif->ipif_ill;
10159 	boolean_t	 success;
10160 
10161 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10162 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10163 	ASSERT(IAM_WRITER_IPIF(ipif));
10164 
10165 	connp = Q_TO_CONN(q);
10166 	/*
10167 	 * Special case for unplumbing lo0 (the loopback physical interface).
10168 	 * If unplumbing lo0, the incoming address structure has been
10169 	 * initialized to all zeros. When unplumbing lo0, all its logical
10170 	 * interfaces must be removed too.
10171 	 *
10172 	 * Note that this interface may be called to remove a specific
10173 	 * loopback logical interface (eg, lo0:1). But in that case
10174 	 * ipif->ipif_id != 0 so that the code path for that case is the
10175 	 * same as any other interface (meaning it skips the code directly
10176 	 * below).
10177 	 */
10178 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10179 		if (sin->sin_family == AF_UNSPEC &&
10180 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10181 			/*
10182 			 * Mark it condemned. No new ref. will be made to ill.
10183 			 */
10184 			mutex_enter(&ill->ill_lock);
10185 			ill->ill_state_flags |= ILL_CONDEMNED;
10186 			for (ipif = ill->ill_ipif; ipif != NULL;
10187 			    ipif = ipif->ipif_next) {
10188 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10189 			}
10190 			mutex_exit(&ill->ill_lock);
10191 
10192 			ipif = ill->ill_ipif;
10193 			/* unplumb the loopback interface */
10194 			ill_delete(ill);
10195 			mutex_enter(&connp->conn_lock);
10196 			mutex_enter(&ill->ill_lock);
10197 			ASSERT(ill->ill_group == NULL);
10198 
10199 			/* Are any references to this ill active */
10200 			if (ill_is_quiescent(ill)) {
10201 				mutex_exit(&ill->ill_lock);
10202 				mutex_exit(&connp->conn_lock);
10203 				ill_delete_tail(ill);
10204 				return (0);
10205 			}
10206 			success = ipsq_pending_mp_add(connp, ipif,
10207 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10208 			mutex_exit(&connp->conn_lock);
10209 			mutex_exit(&ill->ill_lock);
10210 			if (success)
10211 				return (EINPROGRESS);
10212 			else
10213 				return (EINTR);
10214 		}
10215 	}
10216 
10217 	/*
10218 	 * We are exclusive on the ipsq, so an ill move will be serialized
10219 	 * before or after us.
10220 	 */
10221 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10222 
10223 	if (ipif->ipif_id == 0) {
10224 		/* Find based on address */
10225 		if (ipif->ipif_isv6) {
10226 			sin6_t *sin6;
10227 
10228 			if (sin->sin_family != AF_INET6)
10229 				return (EAFNOSUPPORT);
10230 
10231 			sin6 = (sin6_t *)sin;
10232 			/* We are a writer, so we should be able to lookup */
10233 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10234 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL);
10235 			if (ipif == NULL) {
10236 				/*
10237 				 * Maybe the address in on another interface in
10238 				 * the same IPMP group? We check this below.
10239 				 */
10240 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10241 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL);
10242 			}
10243 		} else {
10244 			ipaddr_t addr;
10245 
10246 			if (sin->sin_family != AF_INET)
10247 				return (EAFNOSUPPORT);
10248 
10249 			addr = sin->sin_addr.s_addr;
10250 			/* We are a writer, so we should be able to lookup */
10251 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10252 			    NULL, NULL, NULL);
10253 			if (ipif == NULL) {
10254 				/*
10255 				 * Maybe the address in on another interface in
10256 				 * the same IPMP group? We check this below.
10257 				 */
10258 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10259 				    NULL, NULL, NULL, NULL);
10260 			}
10261 		}
10262 		if (ipif == NULL) {
10263 			return (EADDRNOTAVAIL);
10264 		}
10265 		/*
10266 		 * When the address to be removed is hosted on a different
10267 		 * interface, we check if the interface is in the same IPMP
10268 		 * group as the specified one; if so we proceed with the
10269 		 * removal.
10270 		 * ill->ill_group is NULL when the ill is down, so we have to
10271 		 * compare the group names instead.
10272 		 */
10273 		if (ipif->ipif_ill != ill &&
10274 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10275 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10276 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10277 		    ill->ill_phyint->phyint_groupname) != 0)) {
10278 			ipif_refrele(ipif);
10279 			return (EADDRNOTAVAIL);
10280 		}
10281 
10282 		/* This is a writer */
10283 		ipif_refrele(ipif);
10284 	}
10285 
10286 	/*
10287 	 * Can not delete instance zero since it is tied to the ill.
10288 	 */
10289 	if (ipif->ipif_id == 0)
10290 		return (EBUSY);
10291 
10292 	mutex_enter(&ill->ill_lock);
10293 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10294 	mutex_exit(&ill->ill_lock);
10295 
10296 	ipif_free(ipif);
10297 
10298 	mutex_enter(&connp->conn_lock);
10299 	mutex_enter(&ill->ill_lock);
10300 
10301 	/* Are any references to this ipif active */
10302 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
10303 		mutex_exit(&ill->ill_lock);
10304 		mutex_exit(&connp->conn_lock);
10305 		ipif_down_tail(ipif);
10306 		ipif_free_tail(ipif);
10307 		return (0);
10308 	    }
10309 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10310 	    IPIF_FREE);
10311 	mutex_exit(&ill->ill_lock);
10312 	mutex_exit(&connp->conn_lock);
10313 	if (success)
10314 		return (EINPROGRESS);
10315 	else
10316 		return (EINTR);
10317 }
10318 
10319 /*
10320  * Restart the removeif ioctl. The refcnt has gone down to 0.
10321  * The ipif is already condemned. So can't find it thru lookups.
10322  */
10323 /* ARGSUSED */
10324 int
10325 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10326     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10327 {
10328 	ill_t *ill;
10329 
10330 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10331 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10332 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10333 		ill = ipif->ipif_ill;
10334 		ASSERT(IAM_WRITER_ILL(ill));
10335 		ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) &&
10336 		    (ill->ill_state_flags & IPIF_CONDEMNED));
10337 		ill_delete_tail(ill);
10338 		return (0);
10339 	}
10340 
10341 	ill = ipif->ipif_ill;
10342 	ASSERT(IAM_WRITER_IPIF(ipif));
10343 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10344 
10345 	ipif_down_tail(ipif);
10346 	ipif_free_tail(ipif);
10347 
10348 	ILL_UNMARK_CHANGING(ill);
10349 	return (0);
10350 }
10351 
10352 /*
10353  * Set the local interface address.
10354  * Allow an address of all zero when the interface is down.
10355  */
10356 /* ARGSUSED */
10357 int
10358 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10359     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10360 {
10361 	int err = 0;
10362 	in6_addr_t v6addr;
10363 	boolean_t need_up = B_FALSE;
10364 
10365 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
10366 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10367 
10368 	ASSERT(IAM_WRITER_IPIF(ipif));
10369 
10370 	if (ipif->ipif_isv6) {
10371 		sin6_t *sin6;
10372 		ill_t *ill;
10373 		phyint_t *phyi;
10374 
10375 		if (sin->sin_family != AF_INET6)
10376 			return (EAFNOSUPPORT);
10377 
10378 		sin6 = (sin6_t *)sin;
10379 		v6addr = sin6->sin6_addr;
10380 		ill = ipif->ipif_ill;
10381 		phyi = ill->ill_phyint;
10382 
10383 		/*
10384 		 * Enforce that true multicast interfaces have a link-local
10385 		 * address for logical unit 0.
10386 		 */
10387 		if (ipif->ipif_id == 0 &&
10388 		    (ill->ill_flags & ILLF_MULTICAST) &&
10389 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
10390 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
10391 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
10392 			return (EADDRNOTAVAIL);
10393 		}
10394 
10395 		/*
10396 		 * up interfaces shouldn't have the unspecified address
10397 		 * unless they also have the IPIF_NOLOCAL flags set and
10398 		 * have a subnet assigned.
10399 		 */
10400 		if ((ipif->ipif_flags & IPIF_UP) &&
10401 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
10402 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
10403 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
10404 			return (EADDRNOTAVAIL);
10405 		}
10406 
10407 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
10408 			return (EADDRNOTAVAIL);
10409 	} else {
10410 		ipaddr_t addr;
10411 
10412 		if (sin->sin_family != AF_INET)
10413 			return (EAFNOSUPPORT);
10414 
10415 		addr = sin->sin_addr.s_addr;
10416 
10417 		/* Allow 0 as the local address. */
10418 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
10419 			return (EADDRNOTAVAIL);
10420 
10421 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10422 	}
10423 
10424 
10425 	/*
10426 	 * Even if there is no change we redo things just to rerun
10427 	 * ipif_set_default.
10428 	 */
10429 	if (ipif->ipif_flags & IPIF_UP) {
10430 		/*
10431 		 * Setting a new local address, make sure
10432 		 * we have net and subnet bcast ire's for
10433 		 * the old address if we need them.
10434 		 */
10435 		if (!ipif->ipif_isv6)
10436 			ipif_check_bcast_ires(ipif);
10437 		/*
10438 		 * If the interface is already marked up,
10439 		 * we call ipif_down which will take care
10440 		 * of ditching any IREs that have been set
10441 		 * up based on the old interface address.
10442 		 */
10443 		err = ipif_logical_down(ipif, q, mp);
10444 		if (err == EINPROGRESS)
10445 			return (err);
10446 		ipif_down_tail(ipif);
10447 		need_up = 1;
10448 	}
10449 
10450 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
10451 	return (err);
10452 }
10453 
10454 int
10455 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10456     boolean_t need_up)
10457 {
10458 	in6_addr_t v6addr;
10459 	ipaddr_t addr;
10460 	sin6_t	*sin6;
10461 	int	err = 0;
10462 
10463 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
10464 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10465 	ASSERT(IAM_WRITER_IPIF(ipif));
10466 	if (ipif->ipif_isv6) {
10467 		sin6 = (sin6_t *)sin;
10468 		v6addr = sin6->sin6_addr;
10469 	} else {
10470 		addr = sin->sin_addr.s_addr;
10471 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10472 	}
10473 	mutex_enter(&ipif->ipif_ill->ill_lock);
10474 	ipif->ipif_v6lcl_addr = v6addr;
10475 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
10476 		ipif->ipif_v6src_addr = ipv6_all_zeros;
10477 	} else {
10478 		ipif->ipif_v6src_addr = v6addr;
10479 	}
10480 
10481 	if ((ipif->ipif_isv6) && IN6_IS_ADDR_6TO4(&v6addr) &&
10482 		(!ipif->ipif_ill->ill_is_6to4tun)) {
10483 		queue_t *wqp = ipif->ipif_ill->ill_wq;
10484 
10485 		/*
10486 		 * The local address of this interface is a 6to4 address,
10487 		 * check if this interface is in fact a 6to4 tunnel or just
10488 		 * an interface configured with a 6to4 address.  We are only
10489 		 * interested in the former.
10490 		 */
10491 		if (wqp != NULL) {
10492 			while ((wqp->q_next != NULL) &&
10493 			    (wqp->q_next->q_qinfo != NULL) &&
10494 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
10495 
10496 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
10497 				    == TUN6TO4_MODID) {
10498 					/* set for use in IP */
10499 					ipif->ipif_ill->ill_is_6to4tun = 1;
10500 					break;
10501 				}
10502 				wqp = wqp->q_next;
10503 			}
10504 		}
10505 	}
10506 
10507 	ipif_set_default(ipif);
10508 	mutex_exit(&ipif->ipif_ill->ill_lock);
10509 
10510 	if (need_up) {
10511 		/*
10512 		 * Now bring the interface back up.  If this
10513 		 * is the only IPIF for the ILL, ipif_up
10514 		 * will have to re-bind to the device, so
10515 		 * we may get back EINPROGRESS, in which
10516 		 * case, this IOCTL will get completed in
10517 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10518 		 */
10519 		err = ipif_up(ipif, q, mp);
10520 	} else {
10521 		/*
10522 		 * Update the IPIF list in SCTP, ipif_up_done() will do it
10523 		 * if need_up is true.
10524 		 */
10525 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10526 	}
10527 
10528 	return (err);
10529 }
10530 
10531 
10532 /*
10533  * Restart entry point to restart the address set operation after the
10534  * refcounts have dropped to zero.
10535  */
10536 /* ARGSUSED */
10537 int
10538 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10539     ip_ioctl_cmd_t *ipip, void *ifreq)
10540 {
10541 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
10542 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10543 	ASSERT(IAM_WRITER_IPIF(ipif));
10544 	ipif_down_tail(ipif);
10545 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
10546 }
10547 
10548 /* ARGSUSED */
10549 int
10550 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10551     ip_ioctl_cmd_t *ipip, void *if_req)
10552 {
10553 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
10554 	struct lifreq *lifr = (struct lifreq *)if_req;
10555 
10556 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
10557 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10558 	/*
10559 	 * The net mask and address can't change since we have a
10560 	 * reference to the ipif. So no lock is necessary.
10561 	 */
10562 	if (ipif->ipif_isv6) {
10563 		*sin6 = sin6_null;
10564 		sin6->sin6_family = AF_INET6;
10565 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
10566 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10567 		lifr->lifr_addrlen =
10568 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10569 	} else {
10570 		*sin = sin_null;
10571 		sin->sin_family = AF_INET;
10572 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
10573 		if (ipip->ipi_cmd_type == LIF_CMD) {
10574 			lifr->lifr_addrlen =
10575 			    ip_mask_to_plen(ipif->ipif_net_mask);
10576 		}
10577 	}
10578 	return (0);
10579 }
10580 
10581 /*
10582  * Set the destination address for a pt-pt interface.
10583  */
10584 /* ARGSUSED */
10585 int
10586 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10587     ip_ioctl_cmd_t *ipip, void *if_req)
10588 {
10589 	int err = 0;
10590 	in6_addr_t v6addr;
10591 	boolean_t need_up = B_FALSE;
10592 
10593 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
10594 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10595 	ASSERT(IAM_WRITER_IPIF(ipif));
10596 
10597 	if (ipif->ipif_isv6) {
10598 		sin6_t *sin6;
10599 
10600 		if (sin->sin_family != AF_INET6)
10601 			return (EAFNOSUPPORT);
10602 
10603 		sin6 = (sin6_t *)sin;
10604 		v6addr = sin6->sin6_addr;
10605 
10606 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
10607 			return (EADDRNOTAVAIL);
10608 	} else {
10609 		ipaddr_t addr;
10610 
10611 		if (sin->sin_family != AF_INET)
10612 			return (EAFNOSUPPORT);
10613 
10614 		addr = sin->sin_addr.s_addr;
10615 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
10616 			return (EADDRNOTAVAIL);
10617 
10618 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10619 	}
10620 
10621 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
10622 		return (0);	/* No change */
10623 
10624 	if (ipif->ipif_flags & IPIF_UP) {
10625 		/*
10626 		 * If the interface is already marked up,
10627 		 * we call ipif_down which will take care
10628 		 * of ditching any IREs that have been set
10629 		 * up based on the old pp dst address.
10630 		 */
10631 		err = ipif_logical_down(ipif, q, mp);
10632 		if (err == EINPROGRESS)
10633 			return (err);
10634 		ipif_down_tail(ipif);
10635 		need_up = B_TRUE;
10636 	}
10637 	/*
10638 	 * could return EINPROGRESS. If so ioctl will complete in
10639 	 * ip_rput_dlpi_writer
10640 	 */
10641 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
10642 	return (err);
10643 }
10644 
10645 static int
10646 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10647     boolean_t need_up)
10648 {
10649 	in6_addr_t v6addr;
10650 	ill_t	*ill = ipif->ipif_ill;
10651 	int	err = 0;
10652 
10653 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n",
10654 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10655 	if (ipif->ipif_isv6) {
10656 		sin6_t *sin6;
10657 
10658 		sin6 = (sin6_t *)sin;
10659 		v6addr = sin6->sin6_addr;
10660 	} else {
10661 		ipaddr_t addr;
10662 
10663 		addr = sin->sin_addr.s_addr;
10664 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10665 	}
10666 	mutex_enter(&ill->ill_lock);
10667 	/* Set point to point destination address. */
10668 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10669 		/*
10670 		 * Allow this as a means of creating logical
10671 		 * pt-pt interfaces on top of e.g. an Ethernet.
10672 		 * XXX Undocumented HACK for testing.
10673 		 * pt-pt interfaces are created with NUD disabled.
10674 		 */
10675 		ipif->ipif_flags |= IPIF_POINTOPOINT;
10676 		ipif->ipif_flags &= ~IPIF_BROADCAST;
10677 		if (ipif->ipif_isv6)
10678 			ipif->ipif_ill->ill_flags |= ILLF_NONUD;
10679 	}
10680 
10681 	/* Set the new address. */
10682 	ipif->ipif_v6pp_dst_addr = v6addr;
10683 	/* Make sure subnet tracks pp_dst */
10684 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
10685 	mutex_exit(&ill->ill_lock);
10686 
10687 	if (need_up) {
10688 		/*
10689 		 * Now bring the interface back up.  If this
10690 		 * is the only IPIF for the ILL, ipif_up
10691 		 * will have to re-bind to the device, so
10692 		 * we may get back EINPROGRESS, in which
10693 		 * case, this IOCTL will get completed in
10694 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10695 		 */
10696 		err = ipif_up(ipif, q, mp);
10697 	}
10698 	return (err);
10699 }
10700 
10701 /*
10702  * Restart entry point to restart the dstaddress set operation after the
10703  * refcounts have dropped to zero.
10704  */
10705 /* ARGSUSED */
10706 int
10707 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10708     ip_ioctl_cmd_t *ipip, void *ifreq)
10709 {
10710 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
10711 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10712 	ipif_down_tail(ipif);
10713 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
10714 }
10715 
10716 /* ARGSUSED */
10717 int
10718 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10719     ip_ioctl_cmd_t *ipip, void *if_req)
10720 {
10721 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
10722 
10723 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
10724 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10725 	/*
10726 	 * Get point to point destination address. The addresses can't
10727 	 * change since we hold a reference to the ipif.
10728 	 */
10729 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
10730 		return (EADDRNOTAVAIL);
10731 
10732 	if (ipif->ipif_isv6) {
10733 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10734 		*sin6 = sin6_null;
10735 		sin6->sin6_family = AF_INET6;
10736 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
10737 	} else {
10738 		*sin = sin_null;
10739 		sin->sin_family = AF_INET;
10740 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
10741 	}
10742 	return (0);
10743 }
10744 
10745 /*
10746  * part of ipmp, make this func return the active/inactive state and
10747  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
10748  */
10749 /*
10750  * This function either sets or clears the IFF_INACTIVE flag.
10751  *
10752  * As long as there are some addresses or multicast memberships on the
10753  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
10754  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
10755  * will be used for outbound packets.
10756  */
10757 static void
10758 phyint_standby_inactive(phyint_t *phyi)
10759 {
10760 	ill_t *ill_v4;
10761 	ill_t *ill_v6;
10762 	ipif_t *ipif;
10763 	ilm_t *ilm;
10764 
10765 	ill_v4 = phyi->phyint_illv4;
10766 	ill_v6 = phyi->phyint_illv6;
10767 
10768 	/*
10769 	 * No need for a lock while traversing the list since iam
10770 	 * a writer
10771 	 */
10772 	if (ill_v4 != NULL) {
10773 		ASSERT(IAM_WRITER_ILL(ill_v4));
10774 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
10775 		    ipif = ipif->ipif_next) {
10776 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
10777 				mutex_enter(&phyi->phyint_lock);
10778 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10779 				mutex_exit(&phyi->phyint_lock);
10780 				return;
10781 			}
10782 		}
10783 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
10784 		    ilm = ilm->ilm_next) {
10785 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
10786 				mutex_enter(&phyi->phyint_lock);
10787 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10788 				mutex_exit(&phyi->phyint_lock);
10789 				return;
10790 			}
10791 		}
10792 	}
10793 	if (ill_v6 != NULL) {
10794 		ill_v6 = phyi->phyint_illv6;
10795 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
10796 		    ipif = ipif->ipif_next) {
10797 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
10798 				mutex_enter(&phyi->phyint_lock);
10799 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10800 				mutex_exit(&phyi->phyint_lock);
10801 				return;
10802 			}
10803 		}
10804 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
10805 		    ilm = ilm->ilm_next) {
10806 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
10807 				mutex_enter(&phyi->phyint_lock);
10808 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10809 				mutex_exit(&phyi->phyint_lock);
10810 				return;
10811 			}
10812 		}
10813 	}
10814 	mutex_enter(&phyi->phyint_lock);
10815 	phyi->phyint_flags |= PHYI_INACTIVE;
10816 	mutex_exit(&phyi->phyint_lock);
10817 }
10818 
10819 /*
10820  * This function is called only when the phyint flags change. Currently
10821  * called from ip_sioctl_flags. We re-do the broadcast nomination so
10822  * that we can select a good ill.
10823  */
10824 static void
10825 ip_redo_nomination(phyint_t *phyi)
10826 {
10827 	ill_t *ill_v4;
10828 
10829 	ill_v4 = phyi->phyint_illv4;
10830 
10831 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
10832 		ASSERT(IAM_WRITER_ILL(ill_v4));
10833 		if (ill_v4->ill_group->illgrp_ill_count > 1)
10834 			ill_nominate_bcast_rcv(ill_v4->ill_group);
10835 	}
10836 }
10837 
10838 /*
10839  * Set interface flags.
10840  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
10841  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
10842  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
10843  *
10844  * NOTE : We really don't enforce that ipif_id zero should be used
10845  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
10846  *	  is because applications generally does SICGLIFFLAGS and
10847  *	  ORs in the new flags (that affects the logical) and does a
10848  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
10849  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
10850  *	  flags that will be turned on is correct with respect to
10851  *	  ipif_id 0. For backward compatibility reasons, it is not done.
10852  */
10853 /* ARGSUSED */
10854 int
10855 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10856     ip_ioctl_cmd_t *ipip, void *if_req)
10857 {
10858 	uint64_t turn_on;
10859 	uint64_t turn_off;
10860 	int	err;
10861 	boolean_t need_up = B_FALSE;
10862 	phyint_t *phyi;
10863 	ill_t *ill;
10864 	uint64_t intf_flags;
10865 	boolean_t phyint_flags_modified = B_FALSE;
10866 	uint64_t flags;
10867 	struct ifreq *ifr;
10868 	struct lifreq *lifr;
10869 	boolean_t set_linklocal = B_FALSE;
10870 	boolean_t zero_source = B_FALSE;
10871 
10872 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
10873 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10874 
10875 	ASSERT(IAM_WRITER_IPIF(ipif));
10876 
10877 	ill = ipif->ipif_ill;
10878 	phyi = ill->ill_phyint;
10879 
10880 	if (ipip->ipi_cmd_type == IF_CMD) {
10881 		ifr = (struct ifreq *)if_req;
10882 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
10883 	} else {
10884 		lifr = (struct lifreq *)if_req;
10885 		flags = lifr->lifr_flags;
10886 	}
10887 
10888 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10889 
10890 	/*
10891 	 * Has the flags been set correctly till now ?
10892 	 */
10893 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10894 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10895 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10896 	/*
10897 	 * Compare the new flags to the old, and partition
10898 	 * into those coming on and those going off.
10899 	 * For the 16 bit command keep the bits above bit 16 unchanged.
10900 	 */
10901 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
10902 		flags |= intf_flags & ~0xFFFF;
10903 
10904 	/*
10905 	 * First check which bits will change and then which will
10906 	 * go on and off
10907 	 */
10908 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
10909 	if (!turn_on)
10910 		return (0);	/* No change */
10911 
10912 	turn_off = intf_flags & turn_on;
10913 	turn_on ^= turn_off;
10914 	err = 0;
10915 
10916 	/*
10917 	 * Don't allow any bits belonging to the logical interface
10918 	 * to be set or cleared on the replacement ipif that was
10919 	 * created temporarily during a MOVE.
10920 	 */
10921 	if (ipif->ipif_replace_zero &&
10922 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
10923 		return (EINVAL);
10924 	}
10925 
10926 	/*
10927 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
10928 	 * IPv6 interfaces.
10929 	 */
10930 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
10931 		return (EINVAL);
10932 
10933 	/*
10934 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
10935 	 * interfaces.  It makes no sense in that context.
10936 	 */
10937 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
10938 		return (EINVAL);
10939 
10940 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
10941 		zero_source = B_TRUE;
10942 
10943 	/*
10944 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
10945 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
10946 	 * If the link local address isn't set, and can be set, it will get
10947 	 * set later on in this function.
10948 	 */
10949 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
10950 	    (flags & IFF_UP) && !zero_source &&
10951 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
10952 		if (ipif_cant_setlinklocal(ipif))
10953 			return (EINVAL);
10954 		set_linklocal = B_TRUE;
10955 	}
10956 
10957 	/*
10958 	 * ILL cannot be part of a usesrc group and and IPMP group at the
10959 	 * same time. No need to grab ill_g_usesrc_lock here, see
10960 	 * synchronization notes in ip.c
10961 	 */
10962 	if (turn_on & PHYI_STANDBY &&
10963 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
10964 		return (EINVAL);
10965 	}
10966 
10967 	/*
10968 	 * If we modify physical interface flags, we'll potentially need to
10969 	 * send up two routing socket messages for the changes (one for the
10970 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
10971 	 */
10972 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10973 		phyint_flags_modified = B_TRUE;
10974 
10975 	/*
10976 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
10977 	 * we need to flush the IRE_CACHES belonging to this ill.
10978 	 * We handle this case here without doing the DOWN/UP dance
10979 	 * like it is done for other flags. If some other flags are
10980 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
10981 	 * below will handle it by bringing it down and then
10982 	 * bringing it UP.
10983 	 */
10984 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
10985 		ill_t *ill_v4, *ill_v6;
10986 
10987 		ill_v4 = phyi->phyint_illv4;
10988 		ill_v6 = phyi->phyint_illv6;
10989 
10990 		/*
10991 		 * First set the INACTIVE flag if needed. Then delete the ires.
10992 		 * ire_add will atomically prevent creating new IRE_CACHEs
10993 		 * unless hidden flag is set
10994 		 */
10995 		if (turn_on & PHYI_STANDBY) {
10996 			/*
10997 			 * We set INACTIVE only when STANDBY is set.
10998 			 */
10999 			ASSERT(!(phyi->phyint_flags & PHYI_INACTIVE));
11000 			phyint_standby_inactive(phyi);
11001 		}
11002 		if (turn_off & PHYI_STANDBY) {
11003 			/*
11004 			 * PHYI_INACTIVE makes sense only when PHYI_STANDBY is
11005 			 * set.
11006 			 */
11007 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11008 		}
11009 		/*
11010 		 * We should always send up a message so that the
11011 		 * daemons come to know of it. Note that the zeroth
11012 		 * interface can be down and the check below for IPIF_UP
11013 		 * will not make sense as we are actually setting
11014 		 * a phyint flag here. We assume that the ipif used
11015 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11016 		 * send up any message for non-zero ipifs).
11017 		 */
11018 		phyint_flags_modified = B_TRUE;
11019 
11020 		if (ill_v4 != NULL) {
11021 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11022 			    IRE_CACHE, ill_stq_cache_delete,
11023 			    (char *)ill_v4, ill_v4);
11024 			illgrp_reset_schednext(ill_v4);
11025 		}
11026 		if (ill_v6 != NULL) {
11027 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11028 			    IRE_CACHE, ill_stq_cache_delete,
11029 			    (char *)ill_v6, ill_v6);
11030 			illgrp_reset_schednext(ill_v6);
11031 		}
11032 	}
11033 
11034 	/*
11035 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11036 	 * status of the interface and, if the interface is part of an IPMP
11037 	 * group, all other interfaces that are part of the same IPMP
11038 	 * group.
11039 	 */
11040 	if ((turn_on | turn_off) & ILLF_ROUTER) {
11041 		(void) ill_forward_set(q, mp, ((turn_on & ILLF_ROUTER) != 0),
11042 		    (caddr_t)ill);
11043 	}
11044 
11045 	/*
11046 	 * If the interface is not UP and we are not going to
11047 	 * bring it UP, record the flags and return. When the
11048 	 * interface comes UP later, the right actions will be
11049 	 * taken.
11050 	 */
11051 	if (!(ipif->ipif_flags & IPIF_UP) &&
11052 	    !(turn_on & IPIF_UP)) {
11053 		/* Record new flags in their respective places. */
11054 		mutex_enter(&ill->ill_lock);
11055 		mutex_enter(&ill->ill_phyint->phyint_lock);
11056 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11057 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11058 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11059 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11060 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11061 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11062 		mutex_exit(&ill->ill_lock);
11063 		mutex_exit(&ill->ill_phyint->phyint_lock);
11064 
11065 		/*
11066 		 * We do the broadcast and nomination here rather
11067 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11068 		 * the case of FAILBACK from INACTIVE standby to the
11069 		 * interface that has been repaired, PHYI_FAILED has not
11070 		 * been cleared yet. If there are only two interfaces in
11071 		 * that group, all we have is a FAILED and INACTIVE
11072 		 * interface. If we do the nomination soon after a failback,
11073 		 * the broadcast nomination code would select the
11074 		 * INACTIVE interface for receiving broadcasts as FAILED is
11075 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11076 		 * receive broadcast packets, we need to redo nomination
11077 		 * when the FAILED is cleared here. Thus, in general we
11078 		 * always do the nomination here for FAILED, STANDBY
11079 		 * and OFFLINE.
11080 		 */
11081 		if (((turn_on | turn_off) &
11082 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11083 			ip_redo_nomination(phyi);
11084 		}
11085 		if (phyint_flags_modified) {
11086 			if (phyi->phyint_illv4 != NULL) {
11087 				ip_rts_ifmsg(phyi->phyint_illv4->
11088 				    ill_ipif);
11089 			}
11090 			if (phyi->phyint_illv6 != NULL) {
11091 				ip_rts_ifmsg(phyi->phyint_illv6->
11092 				    ill_ipif);
11093 			}
11094 		}
11095 		return (0);
11096 	} else if (set_linklocal || zero_source) {
11097 		mutex_enter(&ill->ill_lock);
11098 		if (set_linklocal)
11099 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11100 		if (zero_source)
11101 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11102 		mutex_exit(&ill->ill_lock);
11103 	}
11104 
11105 	/*
11106 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11107 	 * or point-to-point interfaces with an unspecified destination. We do
11108 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11109 	 * have a subnet assigned, which is how in.ndpd currently manages its
11110 	 * onlink prefix list when no addresses are configured with those
11111 	 * prefixes.
11112 	 */
11113 	if (ipif->ipif_isv6 &&
11114 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11115 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11116 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11117 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11118 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11119 		return (EINVAL);
11120 	}
11121 
11122 	/*
11123 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11124 	 * from being brought up.
11125 	 */
11126 	if (!ipif->ipif_isv6 &&
11127 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11128 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11129 		return (EINVAL);
11130 	}
11131 
11132 	/*
11133 	 * The only flag changes that we currently take specific action on
11134 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11135 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11136 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11137 	 * the flags and bringing it back up again.
11138 	 */
11139 	if ((turn_on|turn_off) &
11140 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11141 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11142 		/*
11143 		 * Taking this ipif down, make sure we have
11144 		 * valid net and subnet bcast ire's for other
11145 		 * logical interfaces, if we need them.
11146 		 */
11147 		if (!ipif->ipif_isv6)
11148 			ipif_check_bcast_ires(ipif);
11149 
11150 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11151 		    !(turn_off & IPIF_UP)) {
11152 			need_up = B_TRUE;
11153 			if (ipif->ipif_flags & IPIF_UP)
11154 				ill->ill_logical_down = 1;
11155 			turn_on &= ~IPIF_UP;
11156 		}
11157 		err = ipif_down(ipif, q, mp);
11158 		ip1dbg(("ipif_down returns %d err ", err));
11159 		if (err == EINPROGRESS)
11160 			return (err);
11161 		ipif_down_tail(ipif);
11162 	}
11163 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
11164 }
11165 
11166 static int
11167 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
11168     boolean_t need_up)
11169 {
11170 	ill_t	*ill;
11171 	phyint_t *phyi;
11172 	uint64_t turn_on;
11173 	uint64_t turn_off;
11174 	uint64_t intf_flags;
11175 	boolean_t phyint_flags_modified = B_FALSE;
11176 	int	err = 0;
11177 	boolean_t set_linklocal = B_FALSE;
11178 	boolean_t zero_source = B_FALSE;
11179 
11180 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
11181 		ipif->ipif_ill->ill_name, ipif->ipif_id));
11182 
11183 	ASSERT(IAM_WRITER_IPIF(ipif));
11184 
11185 	ill = ipif->ipif_ill;
11186 	phyi = ill->ill_phyint;
11187 
11188 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11189 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
11190 
11191 	turn_off = intf_flags & turn_on;
11192 	turn_on ^= turn_off;
11193 
11194 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
11195 		phyint_flags_modified = B_TRUE;
11196 
11197 	/*
11198 	 * Now we change the flags. Track current value of
11199 	 * other flags in their respective places.
11200 	 */
11201 	mutex_enter(&ill->ill_lock);
11202 	mutex_enter(&phyi->phyint_lock);
11203 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11204 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11205 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11206 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11207 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11208 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11209 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
11210 		set_linklocal = B_TRUE;
11211 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
11212 	}
11213 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
11214 		zero_source = B_TRUE;
11215 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
11216 	}
11217 	mutex_exit(&ill->ill_lock);
11218 	mutex_exit(&phyi->phyint_lock);
11219 
11220 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
11221 		ip_redo_nomination(phyi);
11222 
11223 	if (set_linklocal)
11224 		(void) ipif_setlinklocal(ipif);
11225 
11226 	if (zero_source)
11227 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11228 	else
11229 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
11230 
11231 	if (need_up) {
11232 		/*
11233 		 * XXX ipif_up really does not know whether a phyint flags
11234 		 * was modified or not. So, it sends up information on
11235 		 * only one routing sockets message. As we don't bring up
11236 		 * the interface and also set STANDBY/FAILED simultaneously
11237 		 * it should be okay.
11238 		 */
11239 		err = ipif_up(ipif, q, mp);
11240 	} else {
11241 		/*
11242 		 * Make sure routing socket sees all changes to the flags.
11243 		 * ipif_up_done* handles this when we use ipif_up.
11244 		 */
11245 		if (phyint_flags_modified) {
11246 			if (phyi->phyint_illv4 != NULL) {
11247 				ip_rts_ifmsg(phyi->phyint_illv4->
11248 				    ill_ipif);
11249 			}
11250 			if (phyi->phyint_illv6 != NULL) {
11251 				ip_rts_ifmsg(phyi->phyint_illv6->
11252 				    ill_ipif);
11253 			}
11254 		} else {
11255 			ip_rts_ifmsg(ipif);
11256 		}
11257 	}
11258 	return (err);
11259 }
11260 
11261 /*
11262  * Restart entry point to restart the flags restart operation after the
11263  * refcounts have dropped to zero.
11264  */
11265 /* ARGSUSED */
11266 int
11267 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11268     ip_ioctl_cmd_t *ipip, void *if_req)
11269 {
11270 	int	err;
11271 	struct ifreq *ifr = (struct ifreq *)if_req;
11272 	struct lifreq *lifr = (struct lifreq *)if_req;
11273 
11274 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
11275 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11276 
11277 	ipif_down_tail(ipif);
11278 	if (ipip->ipi_cmd_type == IF_CMD) {
11279 		/*
11280 		 * Since ip_sioctl_flags expects an int and ifr_flags
11281 		 * is a short we need to cast ifr_flags into an int
11282 		 * to avoid having sign extension cause bits to get
11283 		 * set that should not be.
11284 		 */
11285 		err = ip_sioctl_flags_tail(ipif,
11286 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
11287 		    q, mp, B_TRUE);
11288 	} else {
11289 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
11290 		    q, mp, B_TRUE);
11291 	}
11292 	return (err);
11293 }
11294 
11295 /* ARGSUSED */
11296 int
11297 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11298     ip_ioctl_cmd_t *ipip, void *if_req)
11299 {
11300 	/*
11301 	 * Has the flags been set correctly till now ?
11302 	 */
11303 	ill_t *ill = ipif->ipif_ill;
11304 	phyint_t *phyi = ill->ill_phyint;
11305 
11306 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
11307 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11308 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11309 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11310 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11311 
11312 	/*
11313 	 * Need a lock since some flags can be set even when there are
11314 	 * references to the ipif.
11315 	 */
11316 	mutex_enter(&ill->ill_lock);
11317 	if (ipip->ipi_cmd_type == IF_CMD) {
11318 		struct ifreq *ifr = (struct ifreq *)if_req;
11319 
11320 		/* Get interface flags (low 16 only). */
11321 		ifr->ifr_flags = ((ipif->ipif_flags |
11322 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
11323 	} else {
11324 		struct lifreq *lifr = (struct lifreq *)if_req;
11325 
11326 		/* Get interface flags. */
11327 		lifr->lifr_flags = ipif->ipif_flags |
11328 		    ill->ill_flags | phyi->phyint_flags;
11329 	}
11330 	mutex_exit(&ill->ill_lock);
11331 	return (0);
11332 }
11333 
11334 /* ARGSUSED */
11335 int
11336 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11337     ip_ioctl_cmd_t *ipip, void *if_req)
11338 {
11339 	int mtu;
11340 	int ip_min_mtu;
11341 	struct ifreq	*ifr;
11342 	struct lifreq *lifr;
11343 	ire_t	*ire;
11344 
11345 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
11346 	    ipif->ipif_id, (void *)ipif));
11347 	if (ipip->ipi_cmd_type == IF_CMD) {
11348 		ifr = (struct ifreq *)if_req;
11349 		mtu = ifr->ifr_metric;
11350 	} else {
11351 		lifr = (struct lifreq *)if_req;
11352 		mtu = lifr->lifr_mtu;
11353 	}
11354 
11355 	if (ipif->ipif_isv6)
11356 		ip_min_mtu = IPV6_MIN_MTU;
11357 	else
11358 		ip_min_mtu = IP_MIN_MTU;
11359 
11360 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
11361 		return (EINVAL);
11362 
11363 	/*
11364 	 * Change the MTU size in all relevant ire's.
11365 	 * Mtu change Vs. new ire creation - protocol below.
11366 	 * First change ipif_mtu and the ire_max_frag of the
11367 	 * interface ire. Then do an ire walk and change the
11368 	 * ire_max_frag of all affected ires. During ire_add
11369 	 * under the bucket lock, set the ire_max_frag of the
11370 	 * new ire being created from the ipif/ire from which
11371 	 * it is being derived. If an mtu change happens after
11372 	 * the ire is added, the new ire will be cleaned up.
11373 	 * Conversely if the mtu change happens before the ire
11374 	 * is added, ire_add will see the new value of the mtu.
11375 	 */
11376 	ipif->ipif_mtu = mtu;
11377 	ipif->ipif_flags |= IPIF_FIXEDMTU;
11378 
11379 	if (ipif->ipif_isv6)
11380 		ire = ipif_to_ire_v6(ipif);
11381 	else
11382 		ire = ipif_to_ire(ipif);
11383 	if (ire != NULL) {
11384 		ire->ire_max_frag = ipif->ipif_mtu;
11385 		ire_refrele(ire);
11386 	}
11387 	if (ipif->ipif_flags & IPIF_UP) {
11388 		if (ipif->ipif_isv6)
11389 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES);
11390 		else
11391 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES);
11392 	}
11393 	/* Update the MTU in SCTP's list */
11394 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11395 	return (0);
11396 }
11397 
11398 /* Get interface MTU. */
11399 /* ARGSUSED */
11400 int
11401 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11402 	ip_ioctl_cmd_t *ipip, void *if_req)
11403 {
11404 	struct ifreq	*ifr;
11405 	struct lifreq	*lifr;
11406 
11407 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
11408 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11409 	if (ipip->ipi_cmd_type == IF_CMD) {
11410 		ifr = (struct ifreq *)if_req;
11411 		ifr->ifr_metric = ipif->ipif_mtu;
11412 	} else {
11413 		lifr = (struct lifreq *)if_req;
11414 		lifr->lifr_mtu = ipif->ipif_mtu;
11415 	}
11416 	return (0);
11417 }
11418 
11419 /* Set interface broadcast address. */
11420 /* ARGSUSED2 */
11421 int
11422 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11423 	ip_ioctl_cmd_t *ipip, void *if_req)
11424 {
11425 	ipaddr_t addr;
11426 	ire_t	*ire;
11427 
11428 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
11429 	    ipif->ipif_id));
11430 
11431 	ASSERT(IAM_WRITER_IPIF(ipif));
11432 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
11433 		return (EADDRNOTAVAIL);
11434 
11435 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
11436 
11437 	if (sin->sin_family != AF_INET)
11438 		return (EAFNOSUPPORT);
11439 
11440 	addr = sin->sin_addr.s_addr;
11441 	if (ipif->ipif_flags & IPIF_UP) {
11442 		/*
11443 		 * If we are already up, make sure the new
11444 		 * broadcast address makes sense.  If it does,
11445 		 * there should be an IRE for it already.
11446 		 * Don't match on ipif, only on the ill
11447 		 * since we are sharing these now. Don't use
11448 		 * MATCH_IRE_ILL_GROUP as we are looking for
11449 		 * the broadcast ire on this ill and each ill
11450 		 * in the group has its own broadcast ire.
11451 		 */
11452 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
11453 		    ipif, ALL_ZONES, (MATCH_IRE_ILL | MATCH_IRE_TYPE));
11454 		if (ire == NULL) {
11455 			return (EINVAL);
11456 		} else {
11457 			ire_refrele(ire);
11458 		}
11459 	}
11460 	/*
11461 	 * Changing the broadcast addr for this ipif.
11462 	 * Make sure we have valid net and subnet bcast
11463 	 * ire's for other logical interfaces, if needed.
11464 	 */
11465 	if (addr != ipif->ipif_brd_addr)
11466 		ipif_check_bcast_ires(ipif);
11467 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
11468 	return (0);
11469 }
11470 
11471 /* Get interface broadcast address. */
11472 /* ARGSUSED */
11473 int
11474 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11475     ip_ioctl_cmd_t *ipip, void *if_req)
11476 {
11477 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
11478 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11479 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
11480 		return (EADDRNOTAVAIL);
11481 
11482 	/* IPIF_BROADCAST not possible with IPv6 */
11483 	ASSERT(!ipif->ipif_isv6);
11484 	*sin = sin_null;
11485 	sin->sin_family = AF_INET;
11486 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
11487 	return (0);
11488 }
11489 
11490 /*
11491  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
11492  */
11493 /* ARGSUSED */
11494 int
11495 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11496     ip_ioctl_cmd_t *ipip, void *if_req)
11497 {
11498 	int err = 0;
11499 	in6_addr_t v6mask;
11500 
11501 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
11502 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11503 
11504 	ASSERT(IAM_WRITER_IPIF(ipif));
11505 
11506 	if (ipif->ipif_isv6) {
11507 		sin6_t *sin6;
11508 
11509 		if (sin->sin_family != AF_INET6)
11510 			return (EAFNOSUPPORT);
11511 
11512 		sin6 = (sin6_t *)sin;
11513 		v6mask = sin6->sin6_addr;
11514 	} else {
11515 		ipaddr_t mask;
11516 
11517 		if (sin->sin_family != AF_INET)
11518 			return (EAFNOSUPPORT);
11519 
11520 		mask = sin->sin_addr.s_addr;
11521 		V4MASK_TO_V6(mask, v6mask);
11522 	}
11523 
11524 	/*
11525 	 * No big deal if the interface isn't already up, or the mask
11526 	 * isn't really changing, or this is pt-pt.
11527 	 */
11528 	if (!(ipif->ipif_flags & IPIF_UP) ||
11529 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
11530 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
11531 		ipif->ipif_v6net_mask = v6mask;
11532 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11533 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
11534 			    ipif->ipif_v6net_mask,
11535 			    ipif->ipif_v6subnet);
11536 		}
11537 		return (0);
11538 	}
11539 	/*
11540 	 * Make sure we have valid net and subnet broadcast ire's
11541 	 * for the old netmask, if needed by other logical interfaces.
11542 	 */
11543 	if (!ipif->ipif_isv6)
11544 		ipif_check_bcast_ires(ipif);
11545 
11546 	err = ipif_logical_down(ipif, q, mp);
11547 	if (err == EINPROGRESS)
11548 		return (err);
11549 	ipif_down_tail(ipif);
11550 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
11551 	return (err);
11552 }
11553 
11554 static int
11555 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
11556 {
11557 	in6_addr_t v6mask;
11558 	int err = 0;
11559 
11560 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
11561 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11562 
11563 	if (ipif->ipif_isv6) {
11564 		sin6_t *sin6;
11565 
11566 		sin6 = (sin6_t *)sin;
11567 		v6mask = sin6->sin6_addr;
11568 	} else {
11569 		ipaddr_t mask;
11570 
11571 		mask = sin->sin_addr.s_addr;
11572 		V4MASK_TO_V6(mask, v6mask);
11573 	}
11574 
11575 	ipif->ipif_v6net_mask = v6mask;
11576 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11577 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
11578 		    ipif->ipif_v6subnet);
11579 	}
11580 	err = ipif_up(ipif, q, mp);
11581 
11582 	if (err == 0 || err == EINPROGRESS) {
11583 		/*
11584 		 * The interface must be DL_BOUND if this packet has to
11585 		 * go out on the wire. Since we only go through a logical
11586 		 * down and are bound with the driver during an internal
11587 		 * down/up that is satisfied.
11588 		 */
11589 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
11590 			/* Potentially broadcast an address mask reply. */
11591 			ipif_mask_reply(ipif);
11592 		}
11593 	}
11594 	return (err);
11595 }
11596 
11597 /* ARGSUSED */
11598 int
11599 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11600     ip_ioctl_cmd_t *ipip, void *if_req)
11601 {
11602 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
11603 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11604 	ipif_down_tail(ipif);
11605 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
11606 }
11607 
11608 /* Get interface net mask. */
11609 /* ARGSUSED */
11610 int
11611 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11612     ip_ioctl_cmd_t *ipip, void *if_req)
11613 {
11614 	struct lifreq *lifr = (struct lifreq *)if_req;
11615 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
11616 
11617 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
11618 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11619 
11620 	/*
11621 	 * net mask can't change since we have a reference to the ipif.
11622 	 */
11623 	if (ipif->ipif_isv6) {
11624 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11625 		*sin6 = sin6_null;
11626 		sin6->sin6_family = AF_INET6;
11627 		sin6->sin6_addr = ipif->ipif_v6net_mask;
11628 		lifr->lifr_addrlen =
11629 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11630 	} else {
11631 		*sin = sin_null;
11632 		sin->sin_family = AF_INET;
11633 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
11634 		if (ipip->ipi_cmd_type == LIF_CMD) {
11635 			lifr->lifr_addrlen =
11636 			    ip_mask_to_plen(ipif->ipif_net_mask);
11637 		}
11638 	}
11639 	return (0);
11640 }
11641 
11642 /* ARGSUSED */
11643 int
11644 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11645     ip_ioctl_cmd_t *ipip, void *if_req)
11646 {
11647 
11648 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
11649 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11650 	/*
11651 	 * Set interface metric.  We don't use this for
11652 	 * anything but we keep track of it in case it is
11653 	 * important to routing applications or such.
11654 	 */
11655 	if (ipip->ipi_cmd_type == IF_CMD) {
11656 		struct ifreq    *ifr;
11657 
11658 		ifr = (struct ifreq *)if_req;
11659 		ipif->ipif_metric = ifr->ifr_metric;
11660 	} else {
11661 		struct lifreq   *lifr;
11662 
11663 		lifr = (struct lifreq *)if_req;
11664 		ipif->ipif_metric = lifr->lifr_metric;
11665 	}
11666 	return (0);
11667 }
11668 
11669 
11670 /* ARGSUSED */
11671 int
11672 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11673     ip_ioctl_cmd_t *ipip, void *if_req)
11674 {
11675 
11676 	/* Get interface metric. */
11677 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
11678 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11679 	if (ipip->ipi_cmd_type == IF_CMD) {
11680 		struct ifreq    *ifr;
11681 
11682 		ifr = (struct ifreq *)if_req;
11683 		ifr->ifr_metric = ipif->ipif_metric;
11684 	} else {
11685 		struct lifreq   *lifr;
11686 
11687 		lifr = (struct lifreq *)if_req;
11688 		lifr->lifr_metric = ipif->ipif_metric;
11689 	}
11690 
11691 	return (0);
11692 }
11693 
11694 /* ARGSUSED */
11695 int
11696 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11697     ip_ioctl_cmd_t *ipip, void *if_req)
11698 {
11699 
11700 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
11701 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11702 	/*
11703 	 * Set the muxid returned from I_PLINK.
11704 	 */
11705 	if (ipip->ipi_cmd_type == IF_CMD) {
11706 		struct ifreq *ifr = (struct ifreq *)if_req;
11707 
11708 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
11709 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
11710 	} else {
11711 		struct lifreq *lifr = (struct lifreq *)if_req;
11712 
11713 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
11714 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
11715 	}
11716 	return (0);
11717 }
11718 
11719 /* ARGSUSED */
11720 int
11721 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11722     ip_ioctl_cmd_t *ipip, void *if_req)
11723 {
11724 
11725 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
11726 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11727 	/*
11728 	 * Get the muxid saved in ill for I_PUNLINK.
11729 	 */
11730 	if (ipip->ipi_cmd_type == IF_CMD) {
11731 		struct ifreq *ifr = (struct ifreq *)if_req;
11732 
11733 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
11734 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
11735 	} else {
11736 		struct lifreq *lifr = (struct lifreq *)if_req;
11737 
11738 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
11739 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
11740 	}
11741 	return (0);
11742 }
11743 
11744 /*
11745  * Set the subnet prefix. Does not modify the broadcast address.
11746  */
11747 /* ARGSUSED */
11748 int
11749 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11750     ip_ioctl_cmd_t *ipip, void *if_req)
11751 {
11752 	int err = 0;
11753 	in6_addr_t v6addr;
11754 	in6_addr_t v6mask;
11755 	boolean_t need_up = B_FALSE;
11756 	int addrlen;
11757 
11758 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
11759 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11760 
11761 	ASSERT(IAM_WRITER_IPIF(ipif));
11762 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
11763 
11764 	if (ipif->ipif_isv6) {
11765 		sin6_t *sin6;
11766 
11767 		if (sin->sin_family != AF_INET6)
11768 			return (EAFNOSUPPORT);
11769 
11770 		sin6 = (sin6_t *)sin;
11771 		v6addr = sin6->sin6_addr;
11772 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
11773 			return (EADDRNOTAVAIL);
11774 	} else {
11775 		ipaddr_t addr;
11776 
11777 		if (sin->sin_family != AF_INET)
11778 			return (EAFNOSUPPORT);
11779 
11780 		addr = sin->sin_addr.s_addr;
11781 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
11782 			return (EADDRNOTAVAIL);
11783 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11784 		/* Add 96 bits */
11785 		addrlen += IPV6_ABITS - IP_ABITS;
11786 	}
11787 
11788 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
11789 		return (EINVAL);
11790 
11791 	/* Check if bits in the address is set past the mask */
11792 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
11793 		return (EINVAL);
11794 
11795 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
11796 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
11797 		return (0);	/* No change */
11798 
11799 	if (ipif->ipif_flags & IPIF_UP) {
11800 		/*
11801 		 * If the interface is already marked up,
11802 		 * we call ipif_down which will take care
11803 		 * of ditching any IREs that have been set
11804 		 * up based on the old interface address.
11805 		 */
11806 		err = ipif_logical_down(ipif, q, mp);
11807 		if (err == EINPROGRESS)
11808 			return (err);
11809 		ipif_down_tail(ipif);
11810 		need_up = B_TRUE;
11811 	}
11812 
11813 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
11814 	return (err);
11815 }
11816 
11817 static int
11818 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
11819     queue_t *q, mblk_t *mp, boolean_t need_up)
11820 {
11821 	ill_t	*ill = ipif->ipif_ill;
11822 	int	err = 0;
11823 
11824 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
11825 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11826 
11827 	/* Set the new address. */
11828 	mutex_enter(&ill->ill_lock);
11829 	ipif->ipif_v6net_mask = v6mask;
11830 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11831 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
11832 		    ipif->ipif_v6subnet);
11833 	}
11834 	mutex_exit(&ill->ill_lock);
11835 
11836 	if (need_up) {
11837 		/*
11838 		 * Now bring the interface back up.  If this
11839 		 * is the only IPIF for the ILL, ipif_up
11840 		 * will have to re-bind to the device, so
11841 		 * we may get back EINPROGRESS, in which
11842 		 * case, this IOCTL will get completed in
11843 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11844 		 */
11845 		err = ipif_up(ipif, q, mp);
11846 		if (err == EINPROGRESS)
11847 			return (err);
11848 	}
11849 	return (err);
11850 }
11851 
11852 /* ARGSUSED */
11853 int
11854 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11855     ip_ioctl_cmd_t *ipip, void *if_req)
11856 {
11857 	int	addrlen;
11858 	in6_addr_t v6addr;
11859 	in6_addr_t v6mask;
11860 	struct lifreq *lifr = (struct lifreq *)if_req;
11861 
11862 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
11863 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11864 	ipif_down_tail(ipif);
11865 
11866 	addrlen = lifr->lifr_addrlen;
11867 	if (ipif->ipif_isv6) {
11868 		sin6_t *sin6;
11869 
11870 		sin6 = (sin6_t *)sin;
11871 		v6addr = sin6->sin6_addr;
11872 	} else {
11873 		ipaddr_t addr;
11874 
11875 		addr = sin->sin_addr.s_addr;
11876 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11877 		addrlen += IPV6_ABITS - IP_ABITS;
11878 	}
11879 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
11880 
11881 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
11882 }
11883 
11884 /* ARGSUSED */
11885 int
11886 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11887     ip_ioctl_cmd_t *ipip, void *if_req)
11888 {
11889 	struct lifreq *lifr = (struct lifreq *)if_req;
11890 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
11891 
11892 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
11893 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11894 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11895 
11896 	if (ipif->ipif_isv6) {
11897 		*sin6 = sin6_null;
11898 		sin6->sin6_family = AF_INET6;
11899 		sin6->sin6_addr = ipif->ipif_v6subnet;
11900 		lifr->lifr_addrlen =
11901 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11902 	} else {
11903 		*sin = sin_null;
11904 		sin->sin_family = AF_INET;
11905 		sin->sin_addr.s_addr = ipif->ipif_subnet;
11906 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
11907 	}
11908 	return (0);
11909 }
11910 
11911 /*
11912  * Set the IPv6 address token.
11913  */
11914 /* ARGSUSED */
11915 int
11916 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11917     ip_ioctl_cmd_t *ipi, void *if_req)
11918 {
11919 	ill_t *ill = ipif->ipif_ill;
11920 	int err;
11921 	in6_addr_t v6addr;
11922 	in6_addr_t v6mask;
11923 	boolean_t need_up = B_FALSE;
11924 	int i;
11925 	sin6_t *sin6 = (sin6_t *)sin;
11926 	struct lifreq *lifr = (struct lifreq *)if_req;
11927 	int addrlen;
11928 
11929 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
11930 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11931 	ASSERT(IAM_WRITER_IPIF(ipif));
11932 
11933 	addrlen = lifr->lifr_addrlen;
11934 	/* Only allow for logical unit zero i.e. not on "le0:17" */
11935 	if (ipif->ipif_id != 0)
11936 		return (EINVAL);
11937 
11938 	if (!ipif->ipif_isv6)
11939 		return (EINVAL);
11940 
11941 	if (addrlen > IPV6_ABITS)
11942 		return (EINVAL);
11943 
11944 	v6addr = sin6->sin6_addr;
11945 
11946 	/*
11947 	 * The length of the token is the length from the end.  To get
11948 	 * the proper mask for this, compute the mask of the bits not
11949 	 * in the token; ie. the prefix, and then xor to get the mask.
11950 	 */
11951 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
11952 		return (EINVAL);
11953 	for (i = 0; i < 4; i++) {
11954 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11955 	}
11956 
11957 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
11958 	    ill->ill_token_length == addrlen)
11959 		return (0);	/* No change */
11960 
11961 	if (ipif->ipif_flags & IPIF_UP) {
11962 		err = ipif_logical_down(ipif, q, mp);
11963 		if (err == EINPROGRESS)
11964 			return (err);
11965 		ipif_down_tail(ipif);
11966 		need_up = B_TRUE;
11967 	}
11968 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
11969 	return (err);
11970 }
11971 
11972 static int
11973 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
11974     mblk_t *mp, boolean_t need_up)
11975 {
11976 	in6_addr_t v6addr;
11977 	in6_addr_t v6mask;
11978 	ill_t	*ill = ipif->ipif_ill;
11979 	int	i;
11980 	int	err = 0;
11981 
11982 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
11983 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11984 	v6addr = sin6->sin6_addr;
11985 	/*
11986 	 * The length of the token is the length from the end.  To get
11987 	 * the proper mask for this, compute the mask of the bits not
11988 	 * in the token; ie. the prefix, and then xor to get the mask.
11989 	 */
11990 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
11991 	for (i = 0; i < 4; i++)
11992 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11993 
11994 	mutex_enter(&ill->ill_lock);
11995 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
11996 	ill->ill_token_length = addrlen;
11997 	mutex_exit(&ill->ill_lock);
11998 
11999 	if (need_up) {
12000 		/*
12001 		 * Now bring the interface back up.  If this
12002 		 * is the only IPIF for the ILL, ipif_up
12003 		 * will have to re-bind to the device, so
12004 		 * we may get back EINPROGRESS, in which
12005 		 * case, this IOCTL will get completed in
12006 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12007 		 */
12008 		err = ipif_up(ipif, q, mp);
12009 		if (err == EINPROGRESS)
12010 			return (err);
12011 	}
12012 	return (err);
12013 }
12014 
12015 /* ARGSUSED */
12016 int
12017 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12018     ip_ioctl_cmd_t *ipi, void *if_req)
12019 {
12020 	ill_t *ill;
12021 	sin6_t *sin6 = (sin6_t *)sin;
12022 	struct lifreq *lifr = (struct lifreq *)if_req;
12023 
12024 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12025 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12026 	if (ipif->ipif_id != 0)
12027 		return (EINVAL);
12028 
12029 	ill = ipif->ipif_ill;
12030 	if (!ill->ill_isv6)
12031 		return (ENXIO);
12032 
12033 	*sin6 = sin6_null;
12034 	sin6->sin6_family = AF_INET6;
12035 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12036 	sin6->sin6_addr = ill->ill_token;
12037 	lifr->lifr_addrlen = ill->ill_token_length;
12038 	return (0);
12039 }
12040 
12041 /*
12042  * Set (hardware) link specific information that might override
12043  * what was acquired through the DL_INFO_ACK.
12044  * The logic is as follows.
12045  *
12046  * become exclusive
12047  * set CHANGING flag
12048  * change mtu on affected IREs
12049  * clear CHANGING flag
12050  *
12051  * An ire add that occurs before the CHANGING flag is set will have its mtu
12052  * changed by the ip_sioctl_lnkinfo.
12053  *
12054  * During the time the CHANGING flag is set, no new ires will be added to the
12055  * bucket, and ire add will fail (due the CHANGING flag).
12056  *
12057  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12058  * before it is added to the bucket.
12059  *
12060  * Obviously only 1 thread can set the CHANGING flag and we need to become
12061  * exclusive to set the flag.
12062  */
12063 /* ARGSUSED */
12064 int
12065 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12066     ip_ioctl_cmd_t *ipi, void *if_req)
12067 {
12068 	ill_t		*ill = ipif->ipif_ill;
12069 	ipif_t		*nipif;
12070 	int		ip_min_mtu;
12071 	boolean_t	mtu_walk = B_FALSE;
12072 	struct lifreq	*lifr = (struct lifreq *)if_req;
12073 	lif_ifinfo_req_t *lir;
12074 	ire_t		*ire;
12075 
12076 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12077 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12078 	lir = &lifr->lifr_ifinfo;
12079 	ASSERT(IAM_WRITER_IPIF(ipif));
12080 
12081 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12082 	if (ipif->ipif_id != 0)
12083 		return (EINVAL);
12084 
12085 	/* Set interface MTU. */
12086 	if (ipif->ipif_isv6)
12087 		ip_min_mtu = IPV6_MIN_MTU;
12088 	else
12089 		ip_min_mtu = IP_MIN_MTU;
12090 
12091 	/*
12092 	 * Verify values before we set anything. Allow zero to
12093 	 * mean unspecified.
12094 	 */
12095 	if (lir->lir_maxmtu != 0 &&
12096 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12097 	    lir->lir_maxmtu < ip_min_mtu))
12098 		return (EINVAL);
12099 	if (lir->lir_reachtime != 0 &&
12100 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12101 		return (EINVAL);
12102 	if (lir->lir_reachretrans != 0 &&
12103 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12104 		return (EINVAL);
12105 
12106 	mutex_enter(&ill->ill_lock);
12107 	ill->ill_state_flags |= ILL_CHANGING;
12108 	for (nipif = ill->ill_ipif; nipif != NULL;
12109 	    nipif = nipif->ipif_next) {
12110 		nipif->ipif_state_flags |= IPIF_CHANGING;
12111 	}
12112 
12113 	mutex_exit(&ill->ill_lock);
12114 
12115 	if (lir->lir_maxmtu != 0) {
12116 		ill->ill_max_mtu = lir->lir_maxmtu;
12117 		ill->ill_mtu_userspecified = 1;
12118 		mtu_walk = B_TRUE;
12119 	}
12120 
12121 	if (lir->lir_reachtime != 0)
12122 		ill->ill_reachable_time = lir->lir_reachtime;
12123 
12124 	if (lir->lir_reachretrans != 0)
12125 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12126 
12127 	ill->ill_max_hops = lir->lir_maxhops;
12128 
12129 	ill->ill_max_buf = ND_MAX_Q;
12130 
12131 	if (mtu_walk) {
12132 		/*
12133 		 * Set the MTU on all ipifs associated with this ill except
12134 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12135 		 */
12136 		for (nipif = ill->ill_ipif; nipif != NULL;
12137 		    nipif = nipif->ipif_next) {
12138 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12139 				continue;
12140 
12141 			nipif->ipif_mtu = ill->ill_max_mtu;
12142 
12143 			if (!(nipif->ipif_flags & IPIF_UP))
12144 				continue;
12145 
12146 			if (nipif->ipif_isv6)
12147 				ire = ipif_to_ire_v6(nipif);
12148 			else
12149 				ire = ipif_to_ire(nipif);
12150 			if (ire != NULL) {
12151 				ire->ire_max_frag = ipif->ipif_mtu;
12152 				ire_refrele(ire);
12153 			}
12154 			if (ill->ill_isv6) {
12155 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
12156 				    ipif_mtu_change, (char *)nipif,
12157 				    ill);
12158 			} else {
12159 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
12160 				    ipif_mtu_change, (char *)nipif,
12161 				    ill);
12162 			}
12163 		}
12164 	}
12165 
12166 	mutex_enter(&ill->ill_lock);
12167 	for (nipif = ill->ill_ipif; nipif != NULL;
12168 	    nipif = nipif->ipif_next) {
12169 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12170 	}
12171 	ILL_UNMARK_CHANGING(ill);
12172 	mutex_exit(&ill->ill_lock);
12173 
12174 	return (0);
12175 }
12176 
12177 /* ARGSUSED */
12178 int
12179 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12180     ip_ioctl_cmd_t *ipi, void *if_req)
12181 {
12182 	struct lif_ifinfo_req *lir;
12183 	ill_t *ill = ipif->ipif_ill;
12184 
12185 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
12186 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12187 	if (ipif->ipif_id != 0)
12188 		return (EINVAL);
12189 
12190 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
12191 	lir->lir_maxhops = ill->ill_max_hops;
12192 	lir->lir_reachtime = ill->ill_reachable_time;
12193 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
12194 	lir->lir_maxmtu = ill->ill_max_mtu;
12195 
12196 	return (0);
12197 }
12198 
12199 /*
12200  * Return best guess as to the subnet mask for the specified address.
12201  * Based on the subnet masks for all the configured interfaces.
12202  *
12203  * We end up returning a zero mask in the case of default, multicast or
12204  * experimental.
12205  */
12206 static ipaddr_t
12207 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp)
12208 {
12209 	ipaddr_t net_mask;
12210 	ill_t	*ill;
12211 	ipif_t	*ipif;
12212 	ill_walk_context_t ctx;
12213 	ipif_t	*fallback_ipif = NULL;
12214 
12215 	net_mask = ip_net_mask(addr);
12216 	if (net_mask == 0) {
12217 		*ipifp = NULL;
12218 		return (0);
12219 	}
12220 
12221 	/* Let's check to see if this is maybe a local subnet route. */
12222 	/* this function only applies to IPv4 interfaces */
12223 	rw_enter(&ill_g_lock, RW_READER);
12224 	ill = ILL_START_WALK_V4(&ctx);
12225 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
12226 		mutex_enter(&ill->ill_lock);
12227 		for (ipif = ill->ill_ipif; ipif != NULL;
12228 		    ipif = ipif->ipif_next) {
12229 			if (!IPIF_CAN_LOOKUP(ipif))
12230 				continue;
12231 			if (!(ipif->ipif_flags & IPIF_UP))
12232 				continue;
12233 			if ((ipif->ipif_subnet & net_mask) ==
12234 			    (addr & net_mask)) {
12235 				/*
12236 				 * Don't trust pt-pt interfaces if there are
12237 				 * other interfaces.
12238 				 */
12239 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
12240 					if (fallback_ipif == NULL) {
12241 						ipif_refhold_locked(ipif);
12242 						fallback_ipif = ipif;
12243 					}
12244 					continue;
12245 				}
12246 
12247 				/*
12248 				 * Fine. Just assume the same net mask as the
12249 				 * directly attached subnet interface is using.
12250 				 */
12251 				ipif_refhold_locked(ipif);
12252 				mutex_exit(&ill->ill_lock);
12253 				rw_exit(&ill_g_lock);
12254 				if (fallback_ipif != NULL)
12255 					ipif_refrele(fallback_ipif);
12256 				*ipifp = ipif;
12257 				return (ipif->ipif_net_mask);
12258 			}
12259 		}
12260 		mutex_exit(&ill->ill_lock);
12261 	}
12262 	rw_exit(&ill_g_lock);
12263 
12264 	*ipifp = fallback_ipif;
12265 	return ((fallback_ipif != NULL) ?
12266 	    fallback_ipif->ipif_net_mask : net_mask);
12267 }
12268 
12269 /*
12270  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
12271  */
12272 static void
12273 ip_wput_ioctl(queue_t *q, mblk_t *mp)
12274 {
12275 	IOCP	iocp;
12276 	ipft_t	*ipft;
12277 	ipllc_t	*ipllc;
12278 	mblk_t	*mp1;
12279 	cred_t	*cr;
12280 	int	error = 0;
12281 	conn_t	*connp;
12282 
12283 	ip1dbg(("ip_wput_ioctl"));
12284 	iocp = (IOCP)mp->b_rptr;
12285 	mp1 = mp->b_cont;
12286 	if (mp1 == NULL) {
12287 		iocp->ioc_error = EINVAL;
12288 		mp->b_datap->db_type = M_IOCNAK;
12289 		iocp->ioc_count = 0;
12290 		qreply(q, mp);
12291 		return;
12292 	}
12293 
12294 	/*
12295 	 * These IOCTLs provide various control capabilities to
12296 	 * upstream agents such as ULPs and processes.	There
12297 	 * are currently two such IOCTLs implemented.  They
12298 	 * are used by TCP to provide update information for
12299 	 * existing IREs and to forcibly delete an IRE for a
12300 	 * host that is not responding, thereby forcing an
12301 	 * attempt at a new route.
12302 	 */
12303 	iocp->ioc_error = EINVAL;
12304 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
12305 		goto done;
12306 
12307 	ipllc = (ipllc_t *)mp1->b_rptr;
12308 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
12309 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
12310 			break;
12311 	}
12312 	/*
12313 	 * prefer credential from mblk over ioctl;
12314 	 * see ip_sioctl_copyin_setup
12315 	 */
12316 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
12317 
12318 	/*
12319 	 * Refhold the conn in case the request gets queued up in some lookup
12320 	 */
12321 	ASSERT(CONN_Q(q));
12322 	connp = Q_TO_CONN(q);
12323 	CONN_INC_REF(connp);
12324 	if (ipft->ipft_pfi &&
12325 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
12326 		pullupmsg(mp1, ipft->ipft_min_size))) {
12327 		error = (*ipft->ipft_pfi)(q,
12328 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
12329 	}
12330 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
12331 		/*
12332 		 * CONN_OPER_PENDING_DONE happens in the function called
12333 		 * through ipft_pfi above.
12334 		 */
12335 		return;
12336 	}
12337 
12338 	CONN_OPER_PENDING_DONE(connp);
12339 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
12340 		freemsg(mp);
12341 		return;
12342 	}
12343 	iocp->ioc_error = error;
12344 
12345 done:
12346 	mp->b_datap->db_type = M_IOCACK;
12347 	if (iocp->ioc_error)
12348 		iocp->ioc_count = 0;
12349 	qreply(q, mp);
12350 }
12351 
12352 /*
12353  * Lookup an ipif using the sequence id (ipif_seqid)
12354  */
12355 ipif_t *
12356 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
12357 {
12358 	ipif_t *ipif;
12359 
12360 	ASSERT(MUTEX_HELD(&ill->ill_lock));
12361 
12362 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12363 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
12364 			return (ipif);
12365 	}
12366 	return (NULL);
12367 }
12368 
12369 uint64_t ipif_g_seqid;
12370 
12371 /*
12372  * Assign a unique id for the ipif. This is used later when we send
12373  * IRES to ARP for resolution where we initialize ire_ipif_seqid
12374  * to the value pointed by ire_ipif->ipif_seqid. Later when the
12375  * IRE is added, we verify that ipif has not disappeared.
12376  */
12377 
12378 static void
12379 ipif_assign_seqid(ipif_t *ipif)
12380 {
12381 	ipif->ipif_seqid = atomic_add_64_nv(&ipif_g_seqid, 1);
12382 }
12383 
12384 /*
12385  * Insert the ipif, so that the list of ipifs on the ill will be sorted
12386  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
12387  * be inserted into the first space available in the list. The value of
12388  * ipif_id will then be set to the appropriate value for its position.
12389  */
12390 static int
12391 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
12392 {
12393 	ill_t *ill;
12394 	ipif_t *tipif;
12395 	ipif_t **tipifp;
12396 	int id;
12397 
12398 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
12399 	    IAM_WRITER_IPIF(ipif));
12400 
12401 	ill = ipif->ipif_ill;
12402 	ASSERT(ill != NULL);
12403 
12404 	/*
12405 	 * In the case of lo0:0 we already hold the ill_g_lock.
12406 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
12407 	 * ipif_insert. Another such caller is ipif_move.
12408 	 */
12409 	if (acquire_g_lock)
12410 		rw_enter(&ill_g_lock, RW_WRITER);
12411 	if (acquire_ill_lock)
12412 		mutex_enter(&ill->ill_lock);
12413 	id = ipif->ipif_id;
12414 	tipifp = &(ill->ill_ipif);
12415 	if (id == -1) {	/* need to find a real id */
12416 		id = 0;
12417 		while ((tipif = *tipifp) != NULL) {
12418 			ASSERT(tipif->ipif_id >= id);
12419 			if (tipif->ipif_id != id)
12420 				break; /* non-consecutive id */
12421 			id++;
12422 			tipifp = &(tipif->ipif_next);
12423 		}
12424 		/* limit number of logical interfaces */
12425 		if (id >= ip_addrs_per_if) {
12426 			if (acquire_ill_lock)
12427 				mutex_exit(&ill->ill_lock);
12428 			if (acquire_g_lock)
12429 				rw_exit(&ill_g_lock);
12430 			return (-1);
12431 		}
12432 		ipif->ipif_id = id; /* assign new id */
12433 	} else if (id < ip_addrs_per_if) {
12434 		/* we have a real id; insert ipif in the right place */
12435 		while ((tipif = *tipifp) != NULL) {
12436 			ASSERT(tipif->ipif_id != id);
12437 			if (tipif->ipif_id > id)
12438 				break; /* found correct location */
12439 			tipifp = &(tipif->ipif_next);
12440 		}
12441 	} else {
12442 		if (acquire_ill_lock)
12443 			mutex_exit(&ill->ill_lock);
12444 		if (acquire_g_lock)
12445 			rw_exit(&ill_g_lock);
12446 		return (-1);
12447 	}
12448 
12449 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
12450 
12451 	ipif->ipif_next = tipif;
12452 	*tipifp = ipif;
12453 	if (acquire_ill_lock)
12454 		mutex_exit(&ill->ill_lock);
12455 	if (acquire_g_lock)
12456 		rw_exit(&ill_g_lock);
12457 	return (0);
12458 }
12459 
12460 /*
12461  * Allocate and initialize a new interface control structure.  (Always
12462  * called as writer.)
12463  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
12464  * is not part of the global linked list of ills. ipif_seqid is unique
12465  * in the system and to preserve the uniqueness, it is assigned only
12466  * when ill becomes part of the global list. At that point ill will
12467  * have a name. If it doesn't get assigned here, it will get assigned
12468  * in ipif_set_values() as part of SIOCSLIFNAME processing.
12469  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
12470  * the interface flags or any other information from the DL_INFO_ACK for
12471  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
12472  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
12473  * second DL_INFO_ACK comes in from the driver.
12474  */
12475 static ipif_t *
12476 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
12477 {
12478 	ipif_t	*ipif;
12479 	phyint_t *phyi;
12480 
12481 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
12482 	    ill->ill_name, id, (void *)ill));
12483 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
12484 
12485 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
12486 		return (NULL);
12487 	*ipif = ipif_zero;	/* start clean */
12488 
12489 	ipif->ipif_ill = ill;
12490 	ipif->ipif_id = id;	/* could be -1 */
12491 	ipif->ipif_zoneid = GLOBAL_ZONEID;
12492 
12493 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
12494 
12495 	ipif->ipif_refcnt = 0;
12496 	ipif->ipif_saved_ire_cnt = 0;
12497 
12498 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
12499 		mi_free(ipif);
12500 		return (NULL);
12501 	}
12502 	/* -1 id should have been replaced by real id */
12503 	id = ipif->ipif_id;
12504 	ASSERT(id >= 0);
12505 
12506 	if (ill->ill_name[0] != '\0') {
12507 		ipif_assign_seqid(ipif);
12508 		if (ill->ill_phyint->phyint_ifindex != 0)
12509 			sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
12510 	}
12511 	/*
12512 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
12513 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
12514 	 * ioctl sets ipif_orig_ipifid to zero.
12515 	 */
12516 	ipif->ipif_orig_ipifid = id;
12517 
12518 	/*
12519 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
12520 	 * The ipif is still not up and can't be looked up until the
12521 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
12522 	 */
12523 	mutex_enter(&ill->ill_lock);
12524 	mutex_enter(&ill->ill_phyint->phyint_lock);
12525 	/*
12526 	 * Set the running flag when logical interface zero is created.
12527 	 * For subsequent logical interfaces, a DLPI link down
12528 	 * notification message may have cleared the running flag to
12529 	 * indicate the link is down, so we shouldn't just blindly set it.
12530 	 */
12531 	if (id == 0)
12532 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
12533 	ipif->ipif_ire_type = ire_type;
12534 	phyi = ill->ill_phyint;
12535 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
12536 
12537 	if (ipif->ipif_isv6) {
12538 		ill->ill_flags |= ILLF_IPV6;
12539 	} else {
12540 		ipaddr_t inaddr_any = INADDR_ANY;
12541 
12542 		ill->ill_flags |= ILLF_IPV4;
12543 
12544 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
12545 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12546 		    &ipif->ipif_v6lcl_addr);
12547 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12548 		    &ipif->ipif_v6src_addr);
12549 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12550 		    &ipif->ipif_v6subnet);
12551 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12552 		    &ipif->ipif_v6net_mask);
12553 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12554 		    &ipif->ipif_v6brd_addr);
12555 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12556 		    &ipif->ipif_v6pp_dst_addr);
12557 	}
12558 
12559 	/*
12560 	 * Don't set the interface flags etc. now, will do it in
12561 	 * ip_ll_subnet_defaults.
12562 	 */
12563 	if (!initialize) {
12564 		mutex_exit(&ill->ill_lock);
12565 		mutex_exit(&ill->ill_phyint->phyint_lock);
12566 		return (ipif);
12567 	}
12568 	ipif->ipif_mtu = ill->ill_max_mtu;
12569 
12570 	if (ill->ill_bcast_addr_length != 0) {
12571 		/*
12572 		 * Later detect lack of DLPI driver multicast
12573 		 * capability by catching DL_ENABMULTI errors in
12574 		 * ip_rput_dlpi.
12575 		 */
12576 		ill->ill_flags |= ILLF_MULTICAST;
12577 		if (!ipif->ipif_isv6)
12578 			ipif->ipif_flags |= IPIF_BROADCAST;
12579 	} else {
12580 		if (ill->ill_net_type != IRE_LOOPBACK) {
12581 			if (ipif->ipif_isv6)
12582 				/*
12583 				 * Note: xresolv interfaces will eventually need
12584 				 * NOARP set here as well, but that will require
12585 				 * those external resolvers to have some
12586 				 * knowledge of that flag and act appropriately.
12587 				 * Not to be changed at present.
12588 				 */
12589 				ill->ill_flags |= ILLF_NONUD;
12590 			else
12591 				ill->ill_flags |= ILLF_NOARP;
12592 		}
12593 		if (ill->ill_phys_addr_length == 0) {
12594 			if (ill->ill_media &&
12595 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
12596 				ipif->ipif_flags |= IPIF_NOXMIT;
12597 				phyi->phyint_flags |= PHYI_VIRTUAL;
12598 			} else {
12599 				/* pt-pt supports multicast. */
12600 				ill->ill_flags |= ILLF_MULTICAST;
12601 				if (ill->ill_net_type == IRE_LOOPBACK) {
12602 					phyi->phyint_flags |=
12603 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
12604 				} else {
12605 					ipif->ipif_flags |= IPIF_POINTOPOINT;
12606 				}
12607 			}
12608 		}
12609 	}
12610 	mutex_exit(&ill->ill_lock);
12611 	mutex_exit(&ill->ill_phyint->phyint_lock);
12612 	return (ipif);
12613 }
12614 
12615 /*
12616  * If appropriate, send a message up to the resolver delete the entry
12617  * for the address of this interface which is going out of business.
12618  * (Always called as writer).
12619  *
12620  * NOTE : We need to check for NULL mps as some of the fields are
12621  *	  initialized only for some interface types. See ipif_resolver_up()
12622  *	  for details.
12623  */
12624 void
12625 ipif_arp_down(ipif_t *ipif)
12626 {
12627 	mblk_t	*mp;
12628 
12629 	ip1dbg(("ipif_arp_down(%s:%u)\n",
12630 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12631 	ASSERT(IAM_WRITER_IPIF(ipif));
12632 
12633 	/* Delete the mapping for the local address */
12634 	mp = ipif->ipif_arp_del_mp;
12635 	if (mp != NULL) {
12636 		ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12637 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12638 		    ipif->ipif_ill->ill_name, ipif->ipif_id));
12639 		putnext(ipif->ipif_ill->ill_rq, mp);
12640 		ipif->ipif_arp_del_mp = NULL;
12641 	}
12642 
12643 	/*
12644 	 * If this is the last ipif that is going down, we need
12645 	 * to clean up ARP completely.
12646 	 */
12647 	if (ipif->ipif_ill->ill_ipif_up_count == 0) {
12648 
12649 		/* Send up AR_INTERFACE_DOWN message */
12650 		mp = ipif->ipif_ill->ill_arp_down_mp;
12651 		if (mp != NULL) {
12652 			ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12653 			    dlpi_prim_str(*(int *)mp->b_rptr),
12654 			    *(int *)mp->b_rptr, ipif->ipif_ill->ill_name,
12655 			    ipif->ipif_id));
12656 			putnext(ipif->ipif_ill->ill_rq, mp);
12657 			ipif->ipif_ill->ill_arp_down_mp = NULL;
12658 		}
12659 
12660 		/* Tell ARP to delete the multicast mappings */
12661 		mp = ipif->ipif_ill->ill_arp_del_mapping_mp;
12662 		if (mp != NULL) {
12663 			ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12664 			    dlpi_prim_str(*(int *)mp->b_rptr),
12665 			    *(int *)mp->b_rptr, ipif->ipif_ill->ill_name,
12666 			    ipif->ipif_id));
12667 			putnext(ipif->ipif_ill->ill_rq, mp);
12668 			ipif->ipif_ill->ill_arp_del_mapping_mp = NULL;
12669 		}
12670 	}
12671 }
12672 
12673 /*
12674  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
12675  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
12676  * that it wants the add_mp allocated in this function to be returned
12677  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
12678  * just re-do the multicast, it wants us to send the add_mp to ARP also.
12679  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
12680  * as it does a ipif_arp_down after calling this function - which will
12681  * remove what we add here.
12682  *
12683  * Returns -1 on failures and 0 on success.
12684  */
12685 int
12686 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
12687 {
12688 	mblk_t	*del_mp = NULL;
12689 	mblk_t *add_mp = NULL;
12690 	mblk_t *mp;
12691 	ill_t	*ill = ipif->ipif_ill;
12692 	phyint_t *phyi = ill->ill_phyint;
12693 	ipaddr_t addr, mask, extract_mask = 0;
12694 	arma_t	*arma;
12695 	uint8_t *maddr, *bphys_addr;
12696 	uint32_t hw_start;
12697 	dl_unitdata_req_t *dlur;
12698 
12699 	ASSERT(IAM_WRITER_IPIF(ipif));
12700 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
12701 		return (0);
12702 
12703 	/*
12704 	 * Delete the existing mapping from ARP. Normally ipif_down
12705 	 * -> ipif_arp_down should send this up to ARP. The only
12706 	 * reason we would find this when we are switching from
12707 	 * Multicast to Broadcast where we did not do a down.
12708 	 */
12709 	mp = ill->ill_arp_del_mapping_mp;
12710 	if (mp != NULL) {
12711 		ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12712 		    dlpi_prim_str(*(int *)mp->b_rptr),
12713 		    *(int *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
12714 		putnext(ill->ill_rq, mp);
12715 		ill->ill_arp_del_mapping_mp = NULL;
12716 	}
12717 
12718 	if (arp_add_mapping_mp != NULL)
12719 		*arp_add_mapping_mp = NULL;
12720 
12721 	/*
12722 	 * Check that the address is not to long for the constant
12723 	 * length reserved in the template arma_t.
12724 	 */
12725 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
12726 		return (-1);
12727 
12728 	/* Add mapping mblk */
12729 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
12730 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
12731 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
12732 	    (caddr_t)&addr);
12733 	if (add_mp == NULL)
12734 		return (-1);
12735 	arma = (arma_t *)add_mp->b_rptr;
12736 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
12737 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
12738 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
12739 
12740 	/*
12741 	 * Determine the broadcast address.
12742 	 */
12743 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
12744 	if (ill->ill_sap_length < 0)
12745 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
12746 	else
12747 		bphys_addr = (uchar_t *)dlur +
12748 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
12749 	/*
12750 	 * Check PHYI_MULTI_BCAST and length of physical
12751 	 * address to determine if we use the mapping or the
12752 	 * broadcast address.
12753 	 */
12754 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
12755 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
12756 		    bphys_addr, maddr, &hw_start, &extract_mask))
12757 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
12758 
12759 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
12760 	    (ill->ill_flags & ILLF_MULTICAST)) {
12761 		/* Make sure this will not match the "exact" entry. */
12762 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
12763 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
12764 		    (caddr_t)&addr);
12765 		if (del_mp == NULL) {
12766 			freemsg(add_mp);
12767 			return (-1);
12768 		}
12769 		bcopy(&extract_mask, (char *)arma +
12770 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
12771 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
12772 			/* Use link-layer broadcast address for MULTI_BCAST */
12773 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
12774 			ip2dbg(("ipif_arp_setup_multicast: adding"
12775 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
12776 		} else {
12777 			arma->arma_hw_mapping_start = hw_start;
12778 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
12779 			    " ARP setup for %s\n", ill->ill_name));
12780 		}
12781 	} else {
12782 		freemsg(add_mp);
12783 		ASSERT(del_mp == NULL);
12784 		/* It is neither MULTICAST nor MULTI_BCAST */
12785 		return (0);
12786 	}
12787 	ASSERT(add_mp != NULL && del_mp != NULL);
12788 	ill->ill_arp_del_mapping_mp = del_mp;
12789 	if (arp_add_mapping_mp != NULL) {
12790 		/* The caller just wants the mblks allocated */
12791 		*arp_add_mapping_mp = add_mp;
12792 	} else {
12793 		/* The caller wants us to send it to arp */
12794 		putnext(ill->ill_rq, add_mp);
12795 	}
12796 	return (0);
12797 }
12798 
12799 /*
12800  * Get the resolver set up for a new interface address.
12801  * (Always called as writer.)
12802  * Called both for IPv4 and IPv6 interfaces,
12803  * though it only sets up the resolver for v6
12804  * if it's an xresolv interface (one using an external resolver).
12805  * Honors ILLF_NOARP.
12806  * The boolean value arp_just_publish, if B_TRUE, indicates that
12807  * it only needs to send an AR_ENTRY_ADD message up to ARP for
12808  * IPv4 interfaces. Currently, B_TRUE is only set when this
12809  * function is called by ip_rput_dlpi_writer() to handle
12810  * asynchronous hardware address change notification.
12811  * Returns error on failure.
12812  */
12813 int
12814 ipif_resolver_up(ipif_t *ipif, boolean_t arp_just_publish)
12815 {
12816 	caddr_t	addr;
12817 	mblk_t	*arp_up_mp = NULL;
12818 	mblk_t	*arp_down_mp = NULL;
12819 	mblk_t	*arp_add_mp = NULL;
12820 	mblk_t	*arp_del_mp = NULL;
12821 	mblk_t	*arp_add_mapping_mp = NULL;
12822 	mblk_t	*arp_del_mapping_mp = NULL;
12823 	ill_t	*ill = ipif->ipif_ill;
12824 	uchar_t	*area_p = NULL;
12825 	uchar_t	*ared_p = NULL;
12826 	int	err = ENOMEM;
12827 
12828 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
12829 	    ipif->ipif_ill->ill_name, ipif->ipif_id,
12830 	    (uint_t)ipif->ipif_flags));
12831 	ASSERT(IAM_WRITER_IPIF(ipif));
12832 
12833 	if ((ill->ill_net_type != IRE_IF_RESOLVER) ||
12834 	    (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))) {
12835 		return (0);
12836 	}
12837 
12838 	if (ill->ill_isv6) {
12839 		/*
12840 		 * External resolver for IPv6
12841 		 */
12842 		ASSERT(!arp_just_publish);
12843 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
12844 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
12845 			area_p = (uchar_t *)&ip6_area_template;
12846 			ared_p = (uchar_t *)&ip6_ared_template;
12847 		}
12848 	} else {
12849 		/*
12850 		 * IPv4 arp case. If the ARP stream has already started
12851 		 * closing, fail this request for ARP bringup. Else
12852 		 * record the fact that an ARP bringup is pending.
12853 		 */
12854 		mutex_enter(&ill->ill_lock);
12855 		if (ill->ill_arp_closing) {
12856 			mutex_exit(&ill->ill_lock);
12857 			err = EINVAL;
12858 			goto failed;
12859 		} else {
12860 			if (ill->ill_ipif_up_count == 0)
12861 				ill->ill_arp_bringup_pending = 1;
12862 			mutex_exit(&ill->ill_lock);
12863 		}
12864 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
12865 			addr = (caddr_t)&ipif->ipif_lcl_addr;
12866 			area_p = (uchar_t *)&ip_area_template;
12867 			ared_p = (uchar_t *)&ip_ared_template;
12868 		}
12869 	}
12870 
12871 	/*
12872 	 * Add an entry for the local address in ARP only if it
12873 	 * is not UNNUMBERED and the address is not INADDR_ANY.
12874 	 */
12875 	if (((ipif->ipif_flags & IPIF_UNNUMBERED) == 0) && area_p != NULL) {
12876 		/* Now ask ARP to publish our address. */
12877 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
12878 		if (arp_add_mp == NULL)
12879 			goto failed;
12880 		if (arp_just_publish) {
12881 			/*
12882 			 * Copy the new hardware address and length into
12883 			 * arp_add_mp to be sent to ARP.
12884 			 */
12885 			area_t *area = (area_t *)arp_add_mp->b_rptr;
12886 			area->area_hw_addr_length =
12887 			    ill->ill_phys_addr_length;
12888 			bcopy((char *)ill->ill_phys_addr,
12889 			    ((char *)area + area->area_hw_addr_offset),
12890 			    area->area_hw_addr_length);
12891 		}
12892 
12893 		((area_t *)arp_add_mp->b_rptr)->area_flags =
12894 		    ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR;
12895 
12896 		if (arp_just_publish)
12897 			goto arp_setup_multicast;
12898 
12899 		/*
12900 		 * Allocate an ARP deletion message so we know we can tell ARP
12901 		 * when the interface goes down.
12902 		 */
12903 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
12904 		if (arp_del_mp == NULL)
12905 			goto failed;
12906 
12907 	} else {
12908 		if (arp_just_publish)
12909 			goto done;
12910 	}
12911 	/*
12912 	 * Need to bring up ARP or setup multicast mapping only
12913 	 * when the first interface is coming UP.
12914 	 */
12915 	if (ill->ill_ipif_up_count != 0)
12916 		goto done;
12917 
12918 	/*
12919 	 * Allocate an ARP down message (to be saved) and an ARP up
12920 	 * message.
12921 	 */
12922 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
12923 	if (arp_down_mp == NULL)
12924 		goto failed;
12925 
12926 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
12927 	if (arp_up_mp == NULL)
12928 		goto failed;
12929 
12930 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
12931 		goto done;
12932 
12933 arp_setup_multicast:
12934 	/*
12935 	 * Setup the multicast mappings. This function initializes
12936 	 * ill_arp_del_mapping_mp also. This does not need to be done for
12937 	 * IPv6.
12938 	 */
12939 	if (!ill->ill_isv6) {
12940 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
12941 		if (err != 0)
12942 			goto failed;
12943 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
12944 		ASSERT(arp_add_mapping_mp != NULL);
12945 	}
12946 
12947 done:;
12948 	if (arp_del_mp != NULL) {
12949 		ASSERT(ipif->ipif_arp_del_mp == NULL);
12950 		ipif->ipif_arp_del_mp = arp_del_mp;
12951 	}
12952 	if (arp_down_mp != NULL) {
12953 		ASSERT(ill->ill_arp_down_mp == NULL);
12954 		ill->ill_arp_down_mp = arp_down_mp;
12955 	}
12956 	if (arp_del_mapping_mp != NULL) {
12957 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
12958 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
12959 	}
12960 	if (arp_up_mp != NULL) {
12961 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
12962 			ipif->ipif_ill->ill_name, ipif->ipif_id));
12963 		putnext(ill->ill_rq, arp_up_mp);
12964 	}
12965 	if (arp_add_mp != NULL) {
12966 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
12967 			ipif->ipif_ill->ill_name, ipif->ipif_id));
12968 		putnext(ill->ill_rq, arp_add_mp);
12969 	}
12970 	if (arp_add_mapping_mp != NULL) {
12971 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
12972 			ipif->ipif_ill->ill_name, ipif->ipif_id));
12973 		putnext(ill->ill_rq, arp_add_mapping_mp);
12974 	}
12975 	if (arp_just_publish)
12976 		return (0);
12977 
12978 	if (ill->ill_flags & ILLF_NOARP)
12979 		err = ill_arp_off(ill);
12980 	else
12981 		err = ill_arp_on(ill);
12982 	if (err) {
12983 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
12984 		freemsg(ipif->ipif_arp_del_mp);
12985 		if (arp_down_mp != NULL)
12986 			freemsg(ill->ill_arp_down_mp);
12987 		if (ill->ill_arp_del_mapping_mp != NULL)
12988 			freemsg(ill->ill_arp_del_mapping_mp);
12989 		ipif->ipif_arp_del_mp = NULL;
12990 		ill->ill_arp_down_mp = NULL;
12991 		ill->ill_arp_del_mapping_mp = NULL;
12992 		return (err);
12993 	}
12994 	return (ill->ill_ipif_up_count != 0 ? 0 : EINPROGRESS);
12995 
12996 failed:;
12997 	ip1dbg(("ipif_resolver_up: FAILED\n"));
12998 	freemsg(arp_add_mp);
12999 	freemsg(arp_del_mp);
13000 	freemsg(arp_add_mapping_mp);
13001 	freemsg(arp_up_mp);
13002 	freemsg(arp_down_mp);
13003 	ill->ill_arp_bringup_pending = 0;
13004 	return (err);
13005 }
13006 
13007 /*
13008  * Wakeup all threads waiting to enter the ipsq, and sleeping
13009  * on any of the ills in this ipsq. The ill_lock of the ill
13010  * must be held so that waiters don't miss wakeups
13011  */
13012 static void
13013 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
13014 {
13015 	phyint_t *phyint;
13016 
13017 	phyint = ipsq->ipsq_phyint_list;
13018 	while (phyint != NULL) {
13019 		if (phyint->phyint_illv4) {
13020 			if (!caller_holds_lock)
13021 				mutex_enter(&phyint->phyint_illv4->ill_lock);
13022 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13023 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
13024 			if (!caller_holds_lock)
13025 				mutex_exit(&phyint->phyint_illv4->ill_lock);
13026 		}
13027 		if (phyint->phyint_illv6) {
13028 			if (!caller_holds_lock)
13029 				mutex_enter(&phyint->phyint_illv6->ill_lock);
13030 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13031 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
13032 			if (!caller_holds_lock)
13033 				mutex_exit(&phyint->phyint_illv6->ill_lock);
13034 		}
13035 		phyint = phyint->phyint_ipsq_next;
13036 	}
13037 }
13038 
13039 static ipsq_t *
13040 ipsq_create(char *groupname)
13041 {
13042 	ipsq_t	*ipsq;
13043 
13044 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13045 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
13046 	if (ipsq == NULL) {
13047 		return (NULL);
13048 	}
13049 
13050 	if (groupname != NULL)
13051 		(void) strcpy(ipsq->ipsq_name, groupname);
13052 	else
13053 		ipsq->ipsq_name[0] = '\0';
13054 
13055 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
13056 	ipsq->ipsq_flags |= IPSQ_GROUP;
13057 	ipsq->ipsq_next = ipsq_g_head;
13058 	ipsq_g_head = ipsq;
13059 	return (ipsq);
13060 }
13061 
13062 /*
13063  * Return an ipsq correspoding to the groupname. If 'create' is true
13064  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
13065  * uniquely with an IPMP group. However during IPMP groupname operations,
13066  * multiple IPMP groups may be associated with a single ipsq. But no
13067  * IPMP group can be associated with more than 1 ipsq at any time.
13068  * For example
13069  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
13070  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
13071  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
13072  *
13073  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
13074  * status shown below during the execution of the above command.
13075  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
13076  *
13077  * After the completion of the above groupname command we return to the stable
13078  * state shown below.
13079  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
13080  *	hme4			mpk17-85	ipsq2	mpk17-85	1
13081  *
13082  * Because of the above, we don't search based on the ipsq_name since that
13083  * would miss the correct ipsq during certain windows as shown above.
13084  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
13085  * natural state.
13086  */
13087 static ipsq_t *
13088 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq)
13089 {
13090 	ipsq_t	*ipsq;
13091 	int	group_len;
13092 	phyint_t *phyint;
13093 
13094 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
13095 
13096 	group_len = strlen(groupname);
13097 	ASSERT(group_len != 0);
13098 	group_len++;
13099 
13100 	for (ipsq = ipsq_g_head; ipsq != NULL; ipsq = ipsq->ipsq_next) {
13101 		/*
13102 		 * When an ipsq is being split, and ill_split_ipsq
13103 		 * calls this function, we exclude it from being considered.
13104 		 */
13105 		if (ipsq == exclude_ipsq)
13106 			continue;
13107 
13108 		/*
13109 		 * Compare against the ipsq_name. The groupname change happens
13110 		 * in 2 phases. The 1st phase merges the from group into
13111 		 * the to group's ipsq, by calling ill_merge_groups and restarts
13112 		 * the ioctl. The 2nd phase then locates the ipsq again thru
13113 		 * ipsq_name. At this point the phyint_groupname has not been
13114 		 * updated.
13115 		 */
13116 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
13117 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
13118 			/*
13119 			 * Verify that an ipmp groupname is exactly
13120 			 * part of 1 ipsq and is not found in any other
13121 			 * ipsq.
13122 			 */
13123 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq) ==
13124 			    NULL);
13125 			return (ipsq);
13126 		}
13127 
13128 		/*
13129 		 * Comparison against ipsq_name alone is not sufficient.
13130 		 * In the case when groups are currently being
13131 		 * merged, the ipsq could hold other IPMP groups temporarily.
13132 		 * so we walk the phyint list and compare against the
13133 		 * phyint_groupname as well.
13134 		 */
13135 		phyint = ipsq->ipsq_phyint_list;
13136 		while (phyint != NULL) {
13137 			if ((group_len == phyint->phyint_groupname_len) &&
13138 			    (bcmp(phyint->phyint_groupname, groupname,
13139 			    group_len) == 0)) {
13140 				/*
13141 				 * Verify that an ipmp groupname is exactly
13142 				 * part of 1 ipsq and is not found in any other
13143 				 * ipsq.
13144 				 */
13145 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq)
13146 					== NULL);
13147 				return (ipsq);
13148 			}
13149 			phyint = phyint->phyint_ipsq_next;
13150 		}
13151 	}
13152 	if (create)
13153 		ipsq = ipsq_create(groupname);
13154 	return (ipsq);
13155 }
13156 
13157 static void
13158 ipsq_delete(ipsq_t *ipsq)
13159 {
13160 	ipsq_t *nipsq;
13161 	ipsq_t *pipsq = NULL;
13162 
13163 	/*
13164 	 * We don't hold the ipsq lock, but we are sure no new
13165 	 * messages can land up, since the ipsq_refs is zero.
13166 	 * i.e. this ipsq is unnamed and no phyint or phyint group
13167 	 * is associated with this ipsq. (Lookups are based on ill_name
13168 	 * or phyint_group_name)
13169 	 */
13170 	ASSERT(ipsq->ipsq_refs == 0);
13171 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
13172 	ASSERT(ipsq->ipsq_pending_mp == NULL);
13173 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
13174 		/*
13175 		 * This is not the ipsq of an IPMP group.
13176 		 */
13177 		kmem_free(ipsq, sizeof (ipsq_t));
13178 		return;
13179 	}
13180 
13181 	rw_enter(&ill_g_lock, RW_WRITER);
13182 
13183 	/*
13184 	 * Locate the ipsq  before we can remove it from
13185 	 * the singly linked list of ipsq's.
13186 	 */
13187 	for (nipsq = ipsq_g_head; nipsq != NULL; nipsq = nipsq->ipsq_next) {
13188 		if (nipsq == ipsq) {
13189 			break;
13190 		}
13191 		pipsq = nipsq;
13192 	}
13193 
13194 	ASSERT(nipsq == ipsq);
13195 
13196 	/* unlink ipsq from the list */
13197 	if (pipsq != NULL)
13198 		pipsq->ipsq_next = ipsq->ipsq_next;
13199 	else
13200 		ipsq_g_head = ipsq->ipsq_next;
13201 	kmem_free(ipsq, sizeof (ipsq_t));
13202 	rw_exit(&ill_g_lock);
13203 }
13204 
13205 static void
13206 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
13207     queue_t *q)
13208 
13209 {
13210 
13211 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
13212 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
13213 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
13214 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
13215 	ASSERT(current_mp != NULL);
13216 
13217 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
13218 		NEW_OP, NULL);
13219 
13220 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
13221 	    new_ipsq->ipsq_xopq_mphead != NULL);
13222 
13223 	/*
13224 	 * move from old ipsq to the new ipsq.
13225 	 */
13226 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
13227 	if (old_ipsq->ipsq_xopq_mphead != NULL)
13228 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
13229 
13230 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
13231 }
13232 
13233 void
13234 ill_group_cleanup(ill_t *ill)
13235 {
13236 	ill_t *ill_v4;
13237 	ill_t *ill_v6;
13238 	ipif_t *ipif;
13239 
13240 	ill_v4 = ill->ill_phyint->phyint_illv4;
13241 	ill_v6 = ill->ill_phyint->phyint_illv6;
13242 
13243 	if (ill_v4 != NULL) {
13244 		mutex_enter(&ill_v4->ill_lock);
13245 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
13246 		    ipif = ipif->ipif_next) {
13247 			IPIF_UNMARK_MOVING(ipif);
13248 		}
13249 		ill_v4->ill_up_ipifs = B_FALSE;
13250 		mutex_exit(&ill_v4->ill_lock);
13251 	}
13252 
13253 	if (ill_v6 != NULL) {
13254 		mutex_enter(&ill_v6->ill_lock);
13255 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
13256 		    ipif = ipif->ipif_next) {
13257 			IPIF_UNMARK_MOVING(ipif);
13258 		}
13259 		ill_v6->ill_up_ipifs = B_FALSE;
13260 		mutex_exit(&ill_v6->ill_lock);
13261 	}
13262 }
13263 /*
13264  * This function is called when an ill has had a change in its group status
13265  * to bring up all the ipifs that were up before the change.
13266  */
13267 int
13268 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
13269 {
13270 	ipif_t *ipif;
13271 	ill_t *ill_v4;
13272 	ill_t *ill_v6;
13273 	ill_t *from_ill;
13274 	int err = 0;
13275 
13276 
13277 	ASSERT(IAM_WRITER_ILL(ill));
13278 
13279 	/*
13280 	 * Except for ipif_state_flags and ill_state_flags the other
13281 	 * fields of the ipif/ill that are modified below are protected
13282 	 * implicitly since we are a writer. We would have tried to down
13283 	 * even an ipif that was already down, in ill_down_ipifs. So we
13284 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
13285 	 */
13286 	ill_v4 = ill->ill_phyint->phyint_illv4;
13287 	ill_v6 = ill->ill_phyint->phyint_illv6;
13288 	if (ill_v4 != NULL) {
13289 		ill_v4->ill_up_ipifs = B_TRUE;
13290 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
13291 		    ipif = ipif->ipif_next) {
13292 			mutex_enter(&ill_v4->ill_lock);
13293 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
13294 			IPIF_UNMARK_MOVING(ipif);
13295 			mutex_exit(&ill_v4->ill_lock);
13296 			if (ipif->ipif_was_up) {
13297 				if (!(ipif->ipif_flags & IPIF_UP))
13298 					err = ipif_up(ipif, q, mp);
13299 				ipif->ipif_was_up = B_FALSE;
13300 				if (err != 0) {
13301 					/*
13302 					 * Can there be any other error ?
13303 					 */
13304 					ASSERT(err == EINPROGRESS);
13305 					return (err);
13306 				}
13307 			}
13308 		}
13309 		mutex_enter(&ill_v4->ill_lock);
13310 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
13311 		mutex_exit(&ill_v4->ill_lock);
13312 		ill_v4->ill_up_ipifs = B_FALSE;
13313 		if (ill_v4->ill_move_in_progress) {
13314 			ASSERT(ill_v4->ill_move_peer != NULL);
13315 			ill_v4->ill_move_in_progress = B_FALSE;
13316 			from_ill = ill_v4->ill_move_peer;
13317 			from_ill->ill_move_in_progress = B_FALSE;
13318 			from_ill->ill_move_peer = NULL;
13319 			mutex_enter(&from_ill->ill_lock);
13320 			from_ill->ill_state_flags &= ~ILL_CHANGING;
13321 			mutex_exit(&from_ill->ill_lock);
13322 			if (ill_v6 == NULL) {
13323 				if (from_ill->ill_phyint->phyint_flags &
13324 				    PHYI_STANDBY) {
13325 					phyint_standby_inactive
13326 					    (from_ill->ill_phyint);
13327 				}
13328 				if (ill_v4->ill_phyint->phyint_flags &
13329 				    PHYI_STANDBY) {
13330 					phyint_standby_inactive
13331 						(ill_v4->ill_phyint);
13332 				}
13333 			}
13334 			ill_v4->ill_move_peer = NULL;
13335 		}
13336 	}
13337 
13338 	if (ill_v6 != NULL) {
13339 		ill_v6->ill_up_ipifs = B_TRUE;
13340 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
13341 		    ipif = ipif->ipif_next) {
13342 			mutex_enter(&ill_v6->ill_lock);
13343 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
13344 			IPIF_UNMARK_MOVING(ipif);
13345 			mutex_exit(&ill_v6->ill_lock);
13346 			if (ipif->ipif_was_up) {
13347 				if (!(ipif->ipif_flags & IPIF_UP))
13348 					err = ipif_up(ipif, q, mp);
13349 				ipif->ipif_was_up = B_FALSE;
13350 				if (err != 0) {
13351 					/*
13352 					 * Can there be any other error ?
13353 					 */
13354 					ASSERT(err == EINPROGRESS);
13355 					return (err);
13356 				}
13357 			}
13358 		}
13359 		mutex_enter(&ill_v6->ill_lock);
13360 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
13361 		mutex_exit(&ill_v6->ill_lock);
13362 		ill_v6->ill_up_ipifs = B_FALSE;
13363 		if (ill_v6->ill_move_in_progress) {
13364 			ASSERT(ill_v6->ill_move_peer != NULL);
13365 			ill_v6->ill_move_in_progress = B_FALSE;
13366 			from_ill = ill_v6->ill_move_peer;
13367 			from_ill->ill_move_in_progress = B_FALSE;
13368 			from_ill->ill_move_peer = NULL;
13369 			mutex_enter(&from_ill->ill_lock);
13370 			from_ill->ill_state_flags &= ~ILL_CHANGING;
13371 			mutex_exit(&from_ill->ill_lock);
13372 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
13373 				phyint_standby_inactive(from_ill->ill_phyint);
13374 			}
13375 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
13376 				phyint_standby_inactive(ill_v6->ill_phyint);
13377 			}
13378 			ill_v6->ill_move_peer = NULL;
13379 		}
13380 	}
13381 	return (0);
13382 }
13383 
13384 /*
13385  * bring down all the approriate ipifs.
13386  */
13387 /* ARGSUSED */
13388 static void
13389 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
13390 {
13391 	ipif_t *ipif;
13392 
13393 	ASSERT(IAM_WRITER_ILL(ill));
13394 
13395 	/*
13396 	 * Except for ipif_state_flags the other fields of the ipif/ill that
13397 	 * are modified below are protected implicitly since we are a writer
13398 	 */
13399 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13400 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
13401 			continue;
13402 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
13403 			/*
13404 			 * We go through the ipif_down logic even if the ipif
13405 			 * is already down, since routes can be added based
13406 			 * on down ipifs. Going through ipif_down once again
13407 			 * will delete any IREs created based on these routes.
13408 			 */
13409 			if (ipif->ipif_flags & IPIF_UP)
13410 				ipif->ipif_was_up = B_TRUE;
13411 			/*
13412 			 * If called with chk_nofailover true ipif is moving.
13413 			 */
13414 			mutex_enter(&ill->ill_lock);
13415 			if (chk_nofailover) {
13416 				ipif->ipif_state_flags |=
13417 					IPIF_MOVING | IPIF_CHANGING;
13418 			} else {
13419 				ipif->ipif_state_flags |= IPIF_CHANGING;
13420 			}
13421 			mutex_exit(&ill->ill_lock);
13422 			/*
13423 			 * Need to re-create net/subnet bcast ires if
13424 			 * they are dependent on ipif.
13425 			 */
13426 			if (!ipif->ipif_isv6)
13427 				ipif_check_bcast_ires(ipif);
13428 			(void) ipif_logical_down(ipif, NULL, NULL);
13429 			ipif_down_tail(ipif);
13430 			/*
13431 			 * We don't do ipif_multicast_down for IPv4 in
13432 			 * ipif_down. We need to set this so that
13433 			 * ipif_multicast_up will join the
13434 			 * ALLHOSTS_GROUP on to_ill.
13435 			 */
13436 			ipif->ipif_multicast_up = B_FALSE;
13437 		}
13438 	}
13439 }
13440 
13441 #define	IPSQ_INC_REF(ipsq)	{			\
13442 	ASSERT(RW_WRITE_HELD(&ill_g_lock));		\
13443 	(ipsq)->ipsq_refs++;				\
13444 }
13445 
13446 #define	IPSQ_DEC_REF(ipsq)	{			\
13447 	ASSERT(RW_WRITE_HELD(&ill_g_lock));		\
13448 	(ipsq)->ipsq_refs--;				\
13449 	if ((ipsq)->ipsq_refs == 0)				\
13450 		(ipsq)->ipsq_name[0] = '\0'; 		\
13451 }
13452 
13453 /*
13454  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
13455  * new_ipsq.
13456  */
13457 static void
13458 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq)
13459 {
13460 	phyint_t *phyint;
13461 	phyint_t *next_phyint;
13462 
13463 	/*
13464 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
13465 	 * writer and the ill_lock of the ill in question. Also the dest
13466 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
13467 	 */
13468 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13469 
13470 	phyint = cur_ipsq->ipsq_phyint_list;
13471 	cur_ipsq->ipsq_phyint_list = NULL;
13472 	while (phyint != NULL) {
13473 		next_phyint = phyint->phyint_ipsq_next;
13474 		IPSQ_DEC_REF(cur_ipsq);
13475 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
13476 		new_ipsq->ipsq_phyint_list = phyint;
13477 		IPSQ_INC_REF(new_ipsq);
13478 		phyint->phyint_ipsq = new_ipsq;
13479 		phyint = next_phyint;
13480 	}
13481 }
13482 
13483 #define	SPLIT_SUCCESS		0
13484 #define	SPLIT_NOT_NEEDED	1
13485 #define	SPLIT_FAILED		2
13486 
13487 int
13488 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry)
13489 {
13490 	ipsq_t *newipsq = NULL;
13491 
13492 	/*
13493 	 * Assertions denote pre-requisites for changing the ipsq of
13494 	 * a phyint
13495 	 */
13496 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13497 	/*
13498 	 * <ill-phyint> assocs can't change while ill_g_lock
13499 	 * is held as writer. See ill_phyint_reinit()
13500 	 */
13501 	ASSERT(phyint->phyint_illv4 == NULL ||
13502 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13503 	ASSERT(phyint->phyint_illv6 == NULL ||
13504 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13505 
13506 	if ((phyint->phyint_groupname_len !=
13507 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
13508 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
13509 	    phyint->phyint_groupname_len) != 0)) {
13510 		/*
13511 		 * Once we fail in creating a new ipsq due to memory shortage,
13512 		 * don't attempt to create new ipsq again, based on another
13513 		 * phyint, since we want all phyints belonging to an IPMP group
13514 		 * to be in the same ipsq even in the event of mem alloc fails.
13515 		 */
13516 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
13517 		    cur_ipsq);
13518 		if (newipsq == NULL) {
13519 			/* Memory allocation failure */
13520 			return (SPLIT_FAILED);
13521 		} else {
13522 			/* ipsq_refs protected by ill_g_lock (writer) */
13523 			IPSQ_DEC_REF(cur_ipsq);
13524 			phyint->phyint_ipsq = newipsq;
13525 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
13526 			newipsq->ipsq_phyint_list = phyint;
13527 			IPSQ_INC_REF(newipsq);
13528 			return (SPLIT_SUCCESS);
13529 		}
13530 	}
13531 	return (SPLIT_NOT_NEEDED);
13532 }
13533 
13534 /*
13535  * The ill locks of the phyint and the ill_g_lock (writer) must be held
13536  * to do this split
13537  */
13538 static int
13539 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq)
13540 {
13541 	ipsq_t *newipsq;
13542 
13543 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13544 	/*
13545 	 * <ill-phyint> assocs can't change while ill_g_lock
13546 	 * is held as writer. See ill_phyint_reinit()
13547 	 */
13548 
13549 	ASSERT(phyint->phyint_illv4 == NULL ||
13550 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13551 	ASSERT(phyint->phyint_illv6 == NULL ||
13552 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13553 
13554 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
13555 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
13556 		/*
13557 		 * ipsq_init failed due to no memory
13558 		 * caller will use the same ipsq
13559 		 */
13560 		return (SPLIT_FAILED);
13561 	}
13562 
13563 	/* ipsq_ref is protected by ill_g_lock (writer) */
13564 	IPSQ_DEC_REF(cur_ipsq);
13565 
13566 	/*
13567 	 * This is a new ipsq that is unknown to the world.
13568 	 * So we don't need to hold ipsq_lock,
13569 	 */
13570 	newipsq = phyint->phyint_ipsq;
13571 	newipsq->ipsq_writer = NULL;
13572 	newipsq->ipsq_reentry_cnt--;
13573 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
13574 #ifdef ILL_DEBUG
13575 	newipsq->ipsq_depth = 0;
13576 #endif
13577 
13578 	return (SPLIT_SUCCESS);
13579 }
13580 
13581 /*
13582  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
13583  * ipsq's representing their individual groups or themselves. Return
13584  * whether split needs to be retried again later.
13585  */
13586 static boolean_t
13587 ill_split_ipsq(ipsq_t *cur_ipsq)
13588 {
13589 	phyint_t *phyint;
13590 	phyint_t *next_phyint;
13591 	int	error;
13592 	boolean_t need_retry = B_FALSE;
13593 
13594 	phyint = cur_ipsq->ipsq_phyint_list;
13595 	cur_ipsq->ipsq_phyint_list = NULL;
13596 	while (phyint != NULL) {
13597 		next_phyint = phyint->phyint_ipsq_next;
13598 		/*
13599 		 * 'created' will tell us whether the callee actually
13600 		 * created an ipsq. Lack of memory may force the callee
13601 		 * to return without creating an ipsq.
13602 		 */
13603 		if (phyint->phyint_groupname == NULL) {
13604 			error = ill_split_to_own_ipsq(phyint, cur_ipsq);
13605 		} else {
13606 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
13607 					need_retry);
13608 		}
13609 
13610 		switch (error) {
13611 		case SPLIT_FAILED:
13612 			need_retry = B_TRUE;
13613 			/* FALLTHRU */
13614 		case SPLIT_NOT_NEEDED:
13615 			/*
13616 			 * Keep it on the list.
13617 			 */
13618 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
13619 			cur_ipsq->ipsq_phyint_list = phyint;
13620 			break;
13621 		case SPLIT_SUCCESS:
13622 			break;
13623 		default:
13624 			ASSERT(0);
13625 		}
13626 
13627 		phyint = next_phyint;
13628 	}
13629 	return (need_retry);
13630 }
13631 
13632 /*
13633  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
13634  * and return the ills in the list. This list will be
13635  * needed to unlock all the ills later on by the caller.
13636  * The <ill-ipsq> associations could change between the
13637  * lock and unlock. Hence the unlock can't traverse the
13638  * ipsq to get the list of ills.
13639  */
13640 static int
13641 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
13642 {
13643 	int	cnt = 0;
13644 	phyint_t	*phyint;
13645 
13646 	/*
13647 	 * The caller holds ill_g_lock to ensure that the ill memberships
13648 	 * of the ipsq don't change
13649 	 */
13650 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
13651 
13652 	phyint = ipsq->ipsq_phyint_list;
13653 	while (phyint != NULL) {
13654 		if (phyint->phyint_illv4 != NULL) {
13655 			ASSERT(cnt < list_max);
13656 			list[cnt++] = phyint->phyint_illv4;
13657 		}
13658 		if (phyint->phyint_illv6 != NULL) {
13659 			ASSERT(cnt < list_max);
13660 			list[cnt++] = phyint->phyint_illv6;
13661 		}
13662 		phyint = phyint->phyint_ipsq_next;
13663 	}
13664 	ill_lock_ills(list, cnt);
13665 	return (cnt);
13666 }
13667 
13668 void
13669 ill_lock_ills(ill_t **list, int cnt)
13670 {
13671 	int	i;
13672 
13673 	if (cnt > 1) {
13674 		boolean_t try_again;
13675 		do {
13676 			try_again = B_FALSE;
13677 			for (i = 0; i < cnt - 1; i++) {
13678 				if (list[i] < list[i + 1]) {
13679 					ill_t	*tmp;
13680 
13681 					/* swap the elements */
13682 					tmp = list[i];
13683 					list[i] = list[i + 1];
13684 					list[i + 1] = tmp;
13685 					try_again = B_TRUE;
13686 				}
13687 			}
13688 		} while (try_again);
13689 	}
13690 
13691 	for (i = 0; i < cnt; i++) {
13692 		if (i == 0) {
13693 			if (list[i] != NULL)
13694 				mutex_enter(&list[i]->ill_lock);
13695 			else
13696 				return;
13697 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
13698 			mutex_enter(&list[i]->ill_lock);
13699 		}
13700 	}
13701 }
13702 
13703 void
13704 ill_unlock_ills(ill_t **list, int cnt)
13705 {
13706 	int	i;
13707 
13708 	for (i = 0; i < cnt; i++) {
13709 		if ((i == 0) && (list[i] != NULL)) {
13710 			mutex_exit(&list[i]->ill_lock);
13711 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
13712 			mutex_exit(&list[i]->ill_lock);
13713 		}
13714 	}
13715 }
13716 
13717 /*
13718  * Merge all the ills from 1 ipsq group into another ipsq group.
13719  * The source ipsq group is specified by the ipsq associated with
13720  * 'from_ill'. The destination ipsq group is specified by the ipsq
13721  * associated with 'to_ill' or 'groupname' respectively.
13722  * Note that ipsq itself does not have a reference count mechanism
13723  * and functions don't look up an ipsq and pass it around. Instead
13724  * functions pass around an ill or groupname, and the ipsq is looked
13725  * up from the ill or groupname and the required operation performed
13726  * atomically with the lookup on the ipsq.
13727  */
13728 static int
13729 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
13730     queue_t *q)
13731 {
13732 	ipsq_t *old_ipsq;
13733 	ipsq_t *new_ipsq;
13734 	ill_t	**ill_list;
13735 	int	cnt;
13736 	size_t	ill_list_size;
13737 	boolean_t became_writer_on_new_sq = B_FALSE;
13738 
13739 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
13740 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
13741 
13742 	/*
13743 	 * Need to hold ill_g_lock as writer and also the ill_lock to
13744 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
13745 	 * ipsq_lock to prevent new messages from landing on an ipsq.
13746 	 */
13747 	rw_enter(&ill_g_lock, RW_WRITER);
13748 
13749 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
13750 	if (groupname != NULL)
13751 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL);
13752 	else {
13753 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
13754 	}
13755 
13756 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
13757 
13758 	/*
13759 	 * both groups are on the same ipsq.
13760 	 */
13761 	if (old_ipsq == new_ipsq) {
13762 		rw_exit(&ill_g_lock);
13763 		return (0);
13764 	}
13765 
13766 	cnt = old_ipsq->ipsq_refs << 1;
13767 	ill_list_size = cnt * sizeof (ill_t *);
13768 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
13769 	if (ill_list == NULL) {
13770 		rw_exit(&ill_g_lock);
13771 		return (ENOMEM);
13772 	}
13773 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
13774 
13775 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
13776 	mutex_enter(&new_ipsq->ipsq_lock);
13777 	if ((new_ipsq->ipsq_writer == NULL &&
13778 		new_ipsq->ipsq_current_ipif == NULL) ||
13779 	    (new_ipsq->ipsq_writer == curthread)) {
13780 		new_ipsq->ipsq_writer = curthread;
13781 		new_ipsq->ipsq_reentry_cnt++;
13782 		became_writer_on_new_sq = B_TRUE;
13783 	}
13784 
13785 	/*
13786 	 * We are holding ill_g_lock as writer and all the ill locks of
13787 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
13788 	 * message can land up on the old ipsq even though we don't hold the
13789 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
13790 	 */
13791 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
13792 
13793 	/*
13794 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
13795 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
13796 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
13797 	 */
13798 	ill_merge_ipsq(old_ipsq, new_ipsq);
13799 
13800 	/*
13801 	 * Mark the new ipsq as needing a split since it is currently
13802 	 * being shared by more than 1 IPMP group. The split will
13803 	 * occur at the end of ipsq_exit
13804 	 */
13805 	new_ipsq->ipsq_split = B_TRUE;
13806 
13807 	/* Now release all the locks */
13808 	mutex_exit(&new_ipsq->ipsq_lock);
13809 	ill_unlock_ills(ill_list, cnt);
13810 	rw_exit(&ill_g_lock);
13811 
13812 	kmem_free(ill_list, ill_list_size);
13813 
13814 	/*
13815 	 * If we succeeded in becoming writer on the new ipsq, then
13816 	 * drain the new ipsq and start processing  all enqueued messages
13817 	 * including the current ioctl we are processing which is either
13818 	 * a set groupname or failover/failback.
13819 	 */
13820 	if (became_writer_on_new_sq)
13821 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
13822 
13823 	/*
13824 	 * syncq has been changed and all the messages have been moved.
13825 	 */
13826 	mutex_enter(&old_ipsq->ipsq_lock);
13827 	old_ipsq->ipsq_current_ipif = NULL;
13828 	mutex_exit(&old_ipsq->ipsq_lock);
13829 	return (EINPROGRESS);
13830 }
13831 
13832 /*
13833  * Delete and add the loopback copy and non-loopback copy of
13834  * the BROADCAST ire corresponding to ill and addr. Used to
13835  * group broadcast ires together when ill becomes part of
13836  * a group.
13837  *
13838  * This function is also called when ill is leaving the group
13839  * so that the ires belonging to the group gets re-grouped.
13840  */
13841 static void
13842 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
13843 {
13844 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
13845 	ire_t **ire_ptpn = &ire_head;
13846 
13847 	/*
13848 	 * The loopback and non-loopback IREs are inserted in the order in which
13849 	 * they're found, on the basis that they are correctly ordered (loopback
13850 	 * first).
13851 	 */
13852 	for (;;) {
13853 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
13854 		    ALL_ZONES, MATCH_IRE_TYPE | MATCH_IRE_ILL);
13855 		if (ire == NULL)
13856 			break;
13857 
13858 		/*
13859 		 * we are passing in KM_SLEEP because it is not easy to
13860 		 * go back to a sane state in case of memory failure.
13861 		 */
13862 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
13863 		ASSERT(nire != NULL);
13864 		bzero(nire, sizeof (ire_t));
13865 		/*
13866 		 * Don't use ire_max_frag directly since we don't
13867 		 * hold on to 'ire' until we add the new ire 'nire' and
13868 		 * we don't want the new ire to have a dangling reference
13869 		 * to 'ire'. The ire_max_frag of a broadcast ire must
13870 		 * be in sync with the ipif_mtu of the associate ipif.
13871 		 * For eg. this happens as a result of SIOCSLIFNAME,
13872 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
13873 		 * the driver. A change in ire_max_frag triggered as
13874 		 * as a result of path mtu discovery, or due to an
13875 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
13876 		 * route change -mtu command does not apply to broadcast ires.
13877 		 *
13878 		 * XXX We need a recovery strategy here if ire_init fails
13879 		 */
13880 		if (ire_init(nire,
13881 		    (uchar_t *)&ire->ire_addr,
13882 		    (uchar_t *)&ire->ire_mask,
13883 		    (uchar_t *)&ire->ire_src_addr,
13884 		    (uchar_t *)&ire->ire_gateway_addr,
13885 		    (uchar_t *)&ire->ire_in_src_addr,
13886 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
13887 			&ire->ire_ipif->ipif_mtu,
13888 		    ire->ire_fp_mp,
13889 		    ire->ire_rfq,
13890 		    ire->ire_stq,
13891 		    ire->ire_type,
13892 		    ire->ire_dlureq_mp,
13893 		    ire->ire_ipif,
13894 		    ire->ire_in_ill,
13895 		    ire->ire_cmask,
13896 		    ire->ire_phandle,
13897 		    ire->ire_ihandle,
13898 		    ire->ire_flags,
13899 		    &ire->ire_uinfo) == NULL) {
13900 			cmn_err(CE_PANIC, "ire_init() failed");
13901 		}
13902 		ire_delete(ire);
13903 		ire_refrele(ire);
13904 
13905 		/*
13906 		 * The newly created IREs are inserted at the tail of the list
13907 		 * starting with ire_head. As we've just allocated them no one
13908 		 * knows about them so it's safe.
13909 		 */
13910 		*ire_ptpn = nire;
13911 		ire_ptpn = &nire->ire_next;
13912 	}
13913 
13914 	for (nire = ire_head; nire != NULL; nire = nire_next) {
13915 		int error;
13916 		ire_t *oire;
13917 		/* unlink the IRE from our list before calling ire_add() */
13918 		nire_next = nire->ire_next;
13919 		nire->ire_next = NULL;
13920 
13921 		/* ire_add adds the ire at the right place in the list */
13922 		oire = nire;
13923 		error = ire_add(&nire, NULL, NULL, NULL);
13924 		ASSERT(error == 0);
13925 		ASSERT(oire == nire);
13926 		ire_refrele(nire);	/* Held in ire_add */
13927 	}
13928 }
13929 
13930 /*
13931  * This function is usually called when an ill is inserted in
13932  * a group and all the ipifs are already UP. As all the ipifs
13933  * are already UP, the broadcast ires have already been created
13934  * and been inserted. But, ire_add_v4 would not have grouped properly.
13935  * We need to re-group for the benefit of ip_wput_ire which
13936  * expects BROADCAST ires to be grouped properly to avoid sending
13937  * more than one copy of the broadcast packet per group.
13938  *
13939  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
13940  *	  because when ipif_up_done ends up calling this, ires have
13941  *        already been added before illgrp_insert i.e before ill_group
13942  *	  has been initialized.
13943  */
13944 static void
13945 ill_group_bcast_for_xmit(ill_t *ill)
13946 {
13947 	ill_group_t *illgrp;
13948 	ipif_t *ipif;
13949 	ipaddr_t addr;
13950 	ipaddr_t net_mask;
13951 	ipaddr_t subnet_netmask;
13952 
13953 	illgrp = ill->ill_group;
13954 
13955 	/*
13956 	 * This function is called even when an ill is deleted from
13957 	 * the group. Hence, illgrp could be null.
13958 	 */
13959 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
13960 		return;
13961 
13962 	/*
13963 	 * Delete all the BROADCAST ires matching this ill and add
13964 	 * them back. This time, ire_add_v4 should take care of
13965 	 * grouping them with others because ill is part of the
13966 	 * group.
13967 	 */
13968 	ill_bcast_delete_and_add(ill, 0);
13969 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
13970 
13971 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13972 
13973 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
13974 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
13975 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
13976 		} else {
13977 			net_mask = htonl(IN_CLASSA_NET);
13978 		}
13979 		addr = net_mask & ipif->ipif_subnet;
13980 		ill_bcast_delete_and_add(ill, addr);
13981 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
13982 
13983 		subnet_netmask = ipif->ipif_net_mask;
13984 		addr = ipif->ipif_subnet;
13985 		ill_bcast_delete_and_add(ill, addr);
13986 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
13987 	}
13988 }
13989 
13990 /*
13991  * This function is called from illgrp_delete when ill is being deleted
13992  * from the group.
13993  *
13994  * As ill is not there in the group anymore, any address belonging
13995  * to this ill should be cleared of IRE_MARK_NORECV.
13996  */
13997 static void
13998 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
13999 {
14000 	ire_t *ire;
14001 	irb_t *irb;
14002 
14003 	ASSERT(ill->ill_group == NULL);
14004 
14005 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14006 	    ALL_ZONES, MATCH_IRE_TYPE | MATCH_IRE_ILL);
14007 
14008 	if (ire != NULL) {
14009 		/*
14010 		 * IPMP and plumbing operations are serialized on the ipsq, so
14011 		 * no one will insert or delete a broadcast ire under our feet.
14012 		 */
14013 		irb = ire->ire_bucket;
14014 		rw_enter(&irb->irb_lock, RW_READER);
14015 		ire_refrele(ire);
14016 
14017 		for (; ire != NULL; ire = ire->ire_next) {
14018 			if (ire->ire_addr != addr)
14019 				break;
14020 			if (ire_to_ill(ire) != ill)
14021 				continue;
14022 
14023 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
14024 			ire->ire_marks &= ~IRE_MARK_NORECV;
14025 		}
14026 		rw_exit(&irb->irb_lock);
14027 	}
14028 }
14029 
14030 /*
14031  * This function must be called only after the broadcast ires
14032  * have been grouped together. For a given address addr, nominate
14033  * only one of the ires whose interface is not FAILED or OFFLINE.
14034  *
14035  * This is also called when an ipif goes down, so that we can nominate
14036  * a different ire with the same address for receiving.
14037  */
14038 static void
14039 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr)
14040 {
14041 	irb_t *irb;
14042 	ire_t *ire;
14043 	ire_t *ire1;
14044 	ire_t *save_ire;
14045 	ire_t **irep = NULL;
14046 	boolean_t first = B_TRUE;
14047 	ire_t *clear_ire = NULL;
14048 	ire_t *start_ire = NULL;
14049 	ire_t	*new_lb_ire;
14050 	ire_t	*new_nlb_ire;
14051 	boolean_t new_lb_ire_used = B_FALSE;
14052 	boolean_t new_nlb_ire_used = B_FALSE;
14053 	uint64_t match_flags;
14054 	uint64_t phyi_flags;
14055 	boolean_t fallback = B_FALSE;
14056 
14057 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
14058 	    MATCH_IRE_TYPE);
14059 	/*
14060 	 * We may not be able to find some ires if a previous
14061 	 * ire_create failed. This happens when an ipif goes
14062 	 * down and we are unable to create BROADCAST ires due
14063 	 * to memory failure. Thus, we have to check for NULL
14064 	 * below. This should handle the case for LOOPBACK,
14065 	 * POINTOPOINT and interfaces with some POINTOPOINT
14066 	 * logicals for which there are no BROADCAST ires.
14067 	 */
14068 	if (ire == NULL)
14069 		return;
14070 	/*
14071 	 * Currently IRE_BROADCASTS are deleted when an ipif
14072 	 * goes down which runs exclusively. Thus, setting
14073 	 * IRE_MARK_RCVD should not race with ire_delete marking
14074 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
14075 	 * be consistent with other parts of the code that walks
14076 	 * a given bucket.
14077 	 */
14078 	save_ire = ire;
14079 	irb = ire->ire_bucket;
14080 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
14081 	if (new_lb_ire == NULL) {
14082 		ire_refrele(ire);
14083 		return;
14084 	}
14085 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
14086 	if (new_nlb_ire == NULL) {
14087 		ire_refrele(ire);
14088 		kmem_cache_free(ire_cache, new_lb_ire);
14089 		return;
14090 	}
14091 	IRB_REFHOLD(irb);
14092 	rw_enter(&irb->irb_lock, RW_WRITER);
14093 	/*
14094 	 * Get to the first ire matching the address and the
14095 	 * group. If the address does not match we are done
14096 	 * as we could not find the IRE. If the address matches
14097 	 * we should get to the first one matching the group.
14098 	 */
14099 	while (ire != NULL) {
14100 		if (ire->ire_addr != addr ||
14101 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
14102 			break;
14103 		}
14104 		ire = ire->ire_next;
14105 	}
14106 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
14107 	start_ire = ire;
14108 redo:
14109 	while (ire != NULL && ire->ire_addr == addr &&
14110 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
14111 		/*
14112 		 * The first ire for any address within a group
14113 		 * should always be the one with IRE_MARK_NORECV cleared
14114 		 * so that ip_wput_ire can avoid searching for one.
14115 		 * Note down the insertion point which will be used
14116 		 * later.
14117 		 */
14118 		if (first && (irep == NULL))
14119 			irep = ire->ire_ptpn;
14120 		/*
14121 		 * PHYI_FAILED is set when the interface fails.
14122 		 * This interface might have become good, but the
14123 		 * daemon has not yet detected. We should still
14124 		 * not receive on this. PHYI_OFFLINE should never
14125 		 * be picked as this has been offlined and soon
14126 		 * be removed.
14127 		 */
14128 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
14129 		if (phyi_flags & PHYI_OFFLINE) {
14130 			ire->ire_marks |= IRE_MARK_NORECV;
14131 			ire = ire->ire_next;
14132 			continue;
14133 		}
14134 		if (phyi_flags & match_flags) {
14135 			ire->ire_marks |= IRE_MARK_NORECV;
14136 			ire = ire->ire_next;
14137 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
14138 			    PHYI_INACTIVE) {
14139 				fallback = B_TRUE;
14140 			}
14141 			continue;
14142 		}
14143 		if (first) {
14144 			/*
14145 			 * We will move this to the front of the list later
14146 			 * on.
14147 			 */
14148 			clear_ire = ire;
14149 			ire->ire_marks &= ~IRE_MARK_NORECV;
14150 		} else {
14151 			ire->ire_marks |= IRE_MARK_NORECV;
14152 		}
14153 		first = B_FALSE;
14154 		ire = ire->ire_next;
14155 	}
14156 	/*
14157 	 * If we never nominated anybody, try nominating at least
14158 	 * an INACTIVE, if we found one. Do it only once though.
14159 	 */
14160 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
14161 	    fallback) {
14162 		match_flags = PHYI_FAILED;
14163 		ire = start_ire;
14164 		irep = NULL;
14165 		goto redo;
14166 	}
14167 	ire_refrele(save_ire);
14168 
14169 	/*
14170 	 * irep non-NULL indicates that we entered the while loop
14171 	 * above. If clear_ire is at the insertion point, we don't
14172 	 * have to do anything. clear_ire will be NULL if all the
14173 	 * interfaces are failed.
14174 	 *
14175 	 * We cannot unlink and reinsert the ire at the right place
14176 	 * in the list since there can be other walkers of this bucket.
14177 	 * Instead we delete and recreate the ire
14178 	 */
14179 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
14180 		ire_t *clear_ire_stq = NULL;
14181 		bzero(new_lb_ire, sizeof (ire_t));
14182 		/* XXX We need a recovery strategy here. */
14183 		if (ire_init(new_lb_ire,
14184 		    (uchar_t *)&clear_ire->ire_addr,
14185 		    (uchar_t *)&clear_ire->ire_mask,
14186 		    (uchar_t *)&clear_ire->ire_src_addr,
14187 		    (uchar_t *)&clear_ire->ire_gateway_addr,
14188 		    (uchar_t *)&clear_ire->ire_in_src_addr,
14189 		    &clear_ire->ire_max_frag,
14190 		    clear_ire->ire_fp_mp,
14191 		    clear_ire->ire_rfq,
14192 		    clear_ire->ire_stq,
14193 		    clear_ire->ire_type,
14194 		    clear_ire->ire_dlureq_mp,
14195 		    clear_ire->ire_ipif,
14196 		    clear_ire->ire_in_ill,
14197 		    clear_ire->ire_cmask,
14198 		    clear_ire->ire_phandle,
14199 		    clear_ire->ire_ihandle,
14200 		    clear_ire->ire_flags,
14201 		    &clear_ire->ire_uinfo) == NULL)
14202 			cmn_err(CE_PANIC, "ire_init() failed");
14203 		if (clear_ire->ire_stq == NULL) {
14204 			ire_t *ire_next = clear_ire->ire_next;
14205 			if (ire_next != NULL &&
14206 			    ire_next->ire_stq != NULL &&
14207 			    ire_next->ire_addr == clear_ire->ire_addr &&
14208 			    ire_next->ire_ipif->ipif_ill ==
14209 			    clear_ire->ire_ipif->ipif_ill) {
14210 				clear_ire_stq = ire_next;
14211 
14212 				bzero(new_nlb_ire, sizeof (ire_t));
14213 				/* XXX We need a recovery strategy here. */
14214 				if (ire_init(new_nlb_ire,
14215 				    (uchar_t *)&clear_ire_stq->ire_addr,
14216 				    (uchar_t *)&clear_ire_stq->ire_mask,
14217 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
14218 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
14219 				    (uchar_t *)&clear_ire_stq->ire_in_src_addr,
14220 				    &clear_ire_stq->ire_max_frag,
14221 				    clear_ire_stq->ire_fp_mp,
14222 				    clear_ire_stq->ire_rfq,
14223 				    clear_ire_stq->ire_stq,
14224 				    clear_ire_stq->ire_type,
14225 				    clear_ire_stq->ire_dlureq_mp,
14226 				    clear_ire_stq->ire_ipif,
14227 				    clear_ire_stq->ire_in_ill,
14228 				    clear_ire_stq->ire_cmask,
14229 				    clear_ire_stq->ire_phandle,
14230 				    clear_ire_stq->ire_ihandle,
14231 				    clear_ire_stq->ire_flags,
14232 				    &clear_ire_stq->ire_uinfo) == NULL)
14233 					cmn_err(CE_PANIC, "ire_init() failed");
14234 			}
14235 		}
14236 
14237 		/*
14238 		 * Delete the ire. We can't call ire_delete() since
14239 		 * we are holding the bucket lock. We can't release the
14240 		 * bucket lock since we can't allow irep to change. So just
14241 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
14242 		 * ire from the list and do the refrele.
14243 		 */
14244 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
14245 		irb->irb_marks |= IRE_MARK_CONDEMNED;
14246 
14247 		if (clear_ire_stq != NULL) {
14248 			ire_fastpath_list_delete(
14249 			    (ill_t *)clear_ire_stq->ire_stq->q_ptr,
14250 			    clear_ire_stq);
14251 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
14252 		}
14253 
14254 		/*
14255 		 * Also take care of otherfields like ib/ob pkt count
14256 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
14257 		 */
14258 
14259 		/* Add the new ire's. Insert at *irep */
14260 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
14261 		ire1 = *irep;
14262 		if (ire1 != NULL)
14263 			ire1->ire_ptpn = &new_lb_ire->ire_next;
14264 		new_lb_ire->ire_next = ire1;
14265 		/* Link the new one in. */
14266 		new_lb_ire->ire_ptpn = irep;
14267 		membar_producer();
14268 		*irep = new_lb_ire;
14269 		new_lb_ire_used = B_TRUE;
14270 		BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
14271 		new_lb_ire->ire_bucket->irb_ire_cnt++;
14272 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
14273 
14274 		if (clear_ire_stq != NULL) {
14275 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
14276 			irep = &new_lb_ire->ire_next;
14277 			/* Add the new ire. Insert at *irep */
14278 			ire1 = *irep;
14279 			if (ire1 != NULL)
14280 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
14281 			new_nlb_ire->ire_next = ire1;
14282 			/* Link the new one in. */
14283 			new_nlb_ire->ire_ptpn = irep;
14284 			membar_producer();
14285 			*irep = new_nlb_ire;
14286 			new_nlb_ire_used = B_TRUE;
14287 			BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
14288 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
14289 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
14290 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
14291 		}
14292 	}
14293 	rw_exit(&irb->irb_lock);
14294 	if (!new_lb_ire_used)
14295 		kmem_cache_free(ire_cache, new_lb_ire);
14296 	if (!new_nlb_ire_used)
14297 		kmem_cache_free(ire_cache, new_nlb_ire);
14298 	IRB_REFRELE(irb);
14299 }
14300 
14301 /*
14302  * Whenever an ipif goes down we have to renominate a different
14303  * broadcast ire to receive. Whenever an ipif comes up, we need
14304  * to make sure that we have only one nominated to receive.
14305  */
14306 static void
14307 ipif_renominate_bcast(ipif_t *ipif)
14308 {
14309 	ill_t *ill = ipif->ipif_ill;
14310 	ipaddr_t subnet_addr;
14311 	ipaddr_t net_addr;
14312 	ipaddr_t net_mask = 0;
14313 	ipaddr_t subnet_netmask;
14314 	ipaddr_t addr;
14315 	ill_group_t *illgrp;
14316 
14317 	illgrp = ill->ill_group;
14318 	/*
14319 	 * If this is the last ipif going down, it might take
14320 	 * the ill out of the group. In that case ipif_down ->
14321 	 * illgrp_delete takes care of doing the nomination.
14322 	 * ipif_down does not call for this case.
14323 	 */
14324 	ASSERT(illgrp != NULL);
14325 
14326 	/* There could not have been any ires associated with this */
14327 	if (ipif->ipif_subnet == 0)
14328 		return;
14329 
14330 	ill_mark_bcast(illgrp, 0);
14331 	ill_mark_bcast(illgrp, INADDR_BROADCAST);
14332 
14333 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14334 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14335 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14336 	} else {
14337 		net_mask = htonl(IN_CLASSA_NET);
14338 	}
14339 	addr = net_mask & ipif->ipif_subnet;
14340 	ill_mark_bcast(illgrp, addr);
14341 
14342 	net_addr = ~net_mask | addr;
14343 	ill_mark_bcast(illgrp, net_addr);
14344 
14345 	subnet_netmask = ipif->ipif_net_mask;
14346 	addr = ipif->ipif_subnet;
14347 	ill_mark_bcast(illgrp, addr);
14348 
14349 	subnet_addr = ~subnet_netmask | addr;
14350 	ill_mark_bcast(illgrp, subnet_addr);
14351 }
14352 
14353 /*
14354  * Whenever we form or delete ill groups, we need to nominate one set of
14355  * BROADCAST ires for receiving in the group.
14356  *
14357  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
14358  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
14359  *    for ill_ipif_up_count to be non-zero. This is the only case where
14360  *    ill_ipif_up_count is zero and we would still find the ires.
14361  *
14362  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
14363  *    ipif is UP and we just have to do the nomination.
14364  *
14365  * 3) When ill_handoff_responsibility calls us, some ill has been removed
14366  *    from the group. So, we have to do the nomination.
14367  *
14368  * Because of (3), there could be just one ill in the group. But we have
14369  * to nominate still as IRE_MARK_NORCV may have been marked on this.
14370  * Thus, this function does not optimize when there is only one ill as
14371  * it is not correct for (3).
14372  */
14373 static void
14374 ill_nominate_bcast_rcv(ill_group_t *illgrp)
14375 {
14376 	ill_t *ill;
14377 	ipif_t *ipif;
14378 	ipaddr_t subnet_addr;
14379 	ipaddr_t prev_subnet_addr = 0;
14380 	ipaddr_t net_addr;
14381 	ipaddr_t prev_net_addr = 0;
14382 	ipaddr_t net_mask = 0;
14383 	ipaddr_t subnet_netmask;
14384 	ipaddr_t addr;
14385 
14386 	/*
14387 	 * When the last memeber is leaving, there is nothing to
14388 	 * nominate.
14389 	 */
14390 	if (illgrp->illgrp_ill_count == 0) {
14391 		ASSERT(illgrp->illgrp_ill == NULL);
14392 		return;
14393 	}
14394 
14395 	ill = illgrp->illgrp_ill;
14396 	ASSERT(!ill->ill_isv6);
14397 	/*
14398 	 * We assume that ires with same address and belonging to the
14399 	 * same group, has been grouped together. Nominating a *single*
14400 	 * ill in the group for sending and receiving broadcast is done
14401 	 * by making sure that the first BROADCAST ire (which will be
14402 	 * the one returned by ire_ctable_lookup for ip_rput and the
14403 	 * one that will be used in ip_wput_ire) will be the one that
14404 	 * will not have IRE_MARK_NORECV set.
14405 	 *
14406 	 * 1) ip_rput checks and discards packets received on ires marked
14407 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
14408 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
14409 	 *    first ire in the group for every broadcast address in the group.
14410 	 *    ip_rput will accept packets only on the first ire i.e only
14411 	 *    one copy of the ill.
14412 	 *
14413 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
14414 	 *    packet for the whole group. It needs to send out on the ill
14415 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
14416 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
14417 	 *    the copy echoed back on other port where the ire is not marked
14418 	 *    with IRE_MARK_NORECV.
14419 	 *
14420 	 * Note that we just need to have the first IRE either loopback or
14421 	 * non-loopback (either of them may not exist if ire_create failed
14422 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
14423 	 * always hit the first one and hence will always accept one copy.
14424 	 *
14425 	 * We have a broadcast ire per ill for all the unique prefixes
14426 	 * hosted on that ill. As we don't have a way of knowing the
14427 	 * unique prefixes on a given ill and hence in the whole group,
14428 	 * we just call ill_mark_bcast on all the prefixes that exist
14429 	 * in the group. For the common case of one prefix, the code
14430 	 * below optimizes by remebering the last address used for
14431 	 * markng. In the case of multiple prefixes, this will still
14432 	 * optimize depending the order of prefixes.
14433 	 *
14434 	 * The only unique address across the whole group is 0.0.0.0 and
14435 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
14436 	 * the first ire in the bucket for receiving and disables the
14437 	 * others.
14438 	 */
14439 	ill_mark_bcast(illgrp, 0);
14440 	ill_mark_bcast(illgrp, INADDR_BROADCAST);
14441 	for (; ill != NULL; ill = ill->ill_group_next) {
14442 
14443 		for (ipif = ill->ill_ipif; ipif != NULL;
14444 		    ipif = ipif->ipif_next) {
14445 
14446 			if (!(ipif->ipif_flags & IPIF_UP) ||
14447 			    ipif->ipif_subnet == 0) {
14448 				continue;
14449 			}
14450 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14451 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14452 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14453 			} else {
14454 				net_mask = htonl(IN_CLASSA_NET);
14455 			}
14456 			addr = net_mask & ipif->ipif_subnet;
14457 			if (prev_net_addr == 0 || prev_net_addr != addr) {
14458 				ill_mark_bcast(illgrp, addr);
14459 				net_addr = ~net_mask | addr;
14460 				ill_mark_bcast(illgrp, net_addr);
14461 			}
14462 			prev_net_addr = addr;
14463 
14464 			subnet_netmask = ipif->ipif_net_mask;
14465 			addr = ipif->ipif_subnet;
14466 			if (prev_subnet_addr == 0 ||
14467 			    prev_subnet_addr != addr) {
14468 				ill_mark_bcast(illgrp, addr);
14469 				subnet_addr = ~subnet_netmask | addr;
14470 				ill_mark_bcast(illgrp, subnet_addr);
14471 			}
14472 			prev_subnet_addr = addr;
14473 		}
14474 	}
14475 }
14476 
14477 /*
14478  * This function is called while forming ill groups.
14479  *
14480  * Currently, we handle only allmulti groups. We want to join
14481  * allmulti on only one of the ills in the groups. In future,
14482  * when we have link aggregation, we may have to join normal
14483  * multicast groups on multiple ills as switch does inbound load
14484  * balancing. Following are the functions that calls this
14485  * function :
14486  *
14487  * 1) ill_recover_multicast : Interface is coming back UP.
14488  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
14489  *    will call ill_recover_multicast to recover all the multicast
14490  *    groups. We need to make sure that only one member is joined
14491  *    in the ill group.
14492  *
14493  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
14494  *    Somebody is joining allmulti. We need to make sure that only one
14495  *    member is joined in the group.
14496  *
14497  * 3) illgrp_insert : If allmulti has already joined, we need to make
14498  *    sure that only one member is joined in the group.
14499  *
14500  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
14501  *    allmulti who we have nominated. We need to pick someother ill.
14502  *
14503  * 5) illgrp_delete : The ill we nominated is leaving the group,
14504  *    we need to pick a new ill to join the group.
14505  *
14506  * For (1), (2), (5) - we just have to check whether there is
14507  * a good ill joined in the group. If we could not find any ills
14508  * joined the group, we should join.
14509  *
14510  * For (4), the one that was nominated to receive, left the group.
14511  * There could be nobody joined in the group when this function is
14512  * called.
14513  *
14514  * For (3) - we need to explicitly check whether there are multiple
14515  * ills joined in the group.
14516  *
14517  * For simplicity, we don't differentiate any of the above cases. We
14518  * just leave the group if it is joined on any of them and join on
14519  * the first good ill.
14520  */
14521 int
14522 ill_nominate_mcast_rcv(ill_group_t *illgrp)
14523 {
14524 	ilm_t *ilm;
14525 	ill_t *ill;
14526 	ill_t *fallback_stand_ill = NULL;
14527 	ill_t *fallback_failed_ill = NULL;
14528 	int ret = 0;
14529 
14530 	/*
14531 	 * Leave the allmulti on all the ills and start fresh.
14532 	 */
14533 	for (ill = illgrp->illgrp_ill; ill != NULL;
14534 	    ill = ill->ill_group_next) {
14535 		if (ill->ill_join_allmulti)
14536 			(void) ip_leave_allmulti(ill->ill_ipif);
14537 	}
14538 
14539 	/*
14540 	 * Choose a good ill. Fallback to standby or failed if
14541 	 * none available. We need to fallback to FAILED in the
14542 	 * case where we have 2 interfaces in a group - where
14543 	 * one of them is failed and another is a good one and
14544 	 * the good one (not marked standby) is leaving the group.
14545 	 */
14546 	ret = 0;
14547 	for (ill = illgrp->illgrp_ill; ill != NULL;
14548 	    ill = ill->ill_group_next) {
14549 		/* Never pick an offline interface */
14550 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
14551 			continue;
14552 
14553 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
14554 			fallback_failed_ill = ill;
14555 			continue;
14556 		}
14557 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
14558 			fallback_stand_ill = ill;
14559 			continue;
14560 		}
14561 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14562 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14563 				ret = ip_join_allmulti(ill->ill_ipif);
14564 				/*
14565 				 * ip_join_allmulti can fail because of memory
14566 				 * failures. So, make sure we join at least
14567 				 * on one ill.
14568 				 */
14569 				if (ill->ill_join_allmulti)
14570 					return (0);
14571 			}
14572 		}
14573 	}
14574 	if (ret != 0) {
14575 		/*
14576 		 * If we tried nominating above and failed to do so,
14577 		 * return error. We might have tried multiple times.
14578 		 * But, return the latest error.
14579 		 */
14580 		return (ret);
14581 	}
14582 	if ((ill = fallback_stand_ill) != NULL) {
14583 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14584 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14585 				ret = ip_join_allmulti(ill->ill_ipif);
14586 				return (ret);
14587 			}
14588 		}
14589 	} else if ((ill = fallback_failed_ill) != NULL) {
14590 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14591 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14592 				ret = ip_join_allmulti(ill->ill_ipif);
14593 				return (ret);
14594 			}
14595 		}
14596 	}
14597 	return (0);
14598 }
14599 
14600 /*
14601  * This function is called from illgrp_delete after it is
14602  * deleted from the group to reschedule responsibilities
14603  * to a different ill.
14604  */
14605 static void
14606 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
14607 {
14608 	ilm_t	*ilm;
14609 	ipif_t	*ipif;
14610 	ipaddr_t subnet_addr;
14611 	ipaddr_t net_addr;
14612 	ipaddr_t net_mask = 0;
14613 	ipaddr_t subnet_netmask;
14614 	ipaddr_t addr;
14615 
14616 	ASSERT(ill->ill_group == NULL);
14617 	/*
14618 	 * Broadcast Responsibility:
14619 	 *
14620 	 * 1. If this ill has been nominated for receiving broadcast
14621 	 * packets, we need to find a new one. Before we find a new
14622 	 * one, we need to re-group the ires that are part of this new
14623 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
14624 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
14625 	 * thing for us.
14626 	 *
14627 	 * 2. If this ill was not nominated for receiving broadcast
14628 	 * packets, we need to clear the IRE_MARK_NORECV flag
14629 	 * so that we continue to send up broadcast packets.
14630 	 */
14631 	if (!ill->ill_isv6) {
14632 		/*
14633 		 * Case 1 above : No optimization here. Just redo the
14634 		 * nomination.
14635 		 */
14636 		ill_group_bcast_for_xmit(ill);
14637 		ill_nominate_bcast_rcv(illgrp);
14638 
14639 		/*
14640 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
14641 		 */
14642 		ill_clear_bcast_mark(ill, 0);
14643 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
14644 
14645 		for (ipif = ill->ill_ipif; ipif != NULL;
14646 		    ipif = ipif->ipif_next) {
14647 
14648 			if (!(ipif->ipif_flags & IPIF_UP) ||
14649 			    ipif->ipif_subnet == 0) {
14650 				continue;
14651 			}
14652 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14653 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14654 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14655 			} else {
14656 				net_mask = htonl(IN_CLASSA_NET);
14657 			}
14658 			addr = net_mask & ipif->ipif_subnet;
14659 			ill_clear_bcast_mark(ill, addr);
14660 
14661 			net_addr = ~net_mask | addr;
14662 			ill_clear_bcast_mark(ill, net_addr);
14663 
14664 			subnet_netmask = ipif->ipif_net_mask;
14665 			addr = ipif->ipif_subnet;
14666 			ill_clear_bcast_mark(ill, addr);
14667 
14668 			subnet_addr = ~subnet_netmask | addr;
14669 			ill_clear_bcast_mark(ill, subnet_addr);
14670 		}
14671 	}
14672 
14673 	/*
14674 	 * Multicast Responsibility.
14675 	 *
14676 	 * If we have joined allmulti on this one, find a new member
14677 	 * in the group to join allmulti. As this ill is already part
14678 	 * of allmulti, we don't have to join on this one.
14679 	 *
14680 	 * If we have not joined allmulti on this one, there is no
14681 	 * responsibility to handoff. But we need to take new
14682 	 * responsibility i.e, join allmulti on this one if we need
14683 	 * to.
14684 	 */
14685 	if (ill->ill_join_allmulti) {
14686 		(void) ill_nominate_mcast_rcv(illgrp);
14687 	} else {
14688 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14689 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14690 				(void) ip_join_allmulti(ill->ill_ipif);
14691 				break;
14692 			}
14693 		}
14694 	}
14695 
14696 	/*
14697 	 * We intentionally do the flushing of IRE_CACHES only matching
14698 	 * on the ill and not on groups. Note that we are already deleted
14699 	 * from the group.
14700 	 *
14701 	 * This will make sure that all IRE_CACHES whose stq is pointing
14702 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
14703 	 * deleted and IRE_CACHES that are not pointing at this ill will
14704 	 * be left alone.
14705 	 */
14706 	if (ill->ill_isv6) {
14707 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
14708 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
14709 	} else {
14710 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
14711 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
14712 	}
14713 
14714 	/*
14715 	 * Some conn may have cached one of the IREs deleted above. By removing
14716 	 * the ire reference, we clean up the extra reference to the ill held in
14717 	 * ire->ire_stq.
14718 	 */
14719 	ipcl_walk(conn_cleanup_stale_ire, NULL);
14720 
14721 	/*
14722 	 * Re-do source address selection for all the members in the
14723 	 * group, if they borrowed source address from one of the ipifs
14724 	 * in this ill.
14725 	 */
14726 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14727 		if (ill->ill_isv6) {
14728 			ipif_update_other_ipifs_v6(ipif, illgrp);
14729 		} else {
14730 			ipif_update_other_ipifs(ipif, illgrp);
14731 		}
14732 	}
14733 }
14734 
14735 /*
14736  * Delete the ill from the group. The caller makes sure that it is
14737  * in a group and it okay to delete from the group. So, we always
14738  * delete here.
14739  */
14740 static void
14741 illgrp_delete(ill_t *ill)
14742 {
14743 	ill_group_t *illgrp;
14744 	ill_group_t *tmpg;
14745 	ill_t *tmp_ill;
14746 
14747 	/*
14748 	 * Reset illgrp_ill_schednext if it was pointing at us.
14749 	 * We need to do this before we set ill_group to NULL.
14750 	 */
14751 	rw_enter(&ill_g_lock, RW_WRITER);
14752 	mutex_enter(&ill->ill_lock);
14753 
14754 	illgrp_reset_schednext(ill);
14755 
14756 	illgrp = ill->ill_group;
14757 
14758 	/* Delete the ill from illgrp. */
14759 	if (illgrp->illgrp_ill == ill) {
14760 		illgrp->illgrp_ill = ill->ill_group_next;
14761 	} else {
14762 		tmp_ill = illgrp->illgrp_ill;
14763 		while (tmp_ill->ill_group_next != ill) {
14764 			tmp_ill = tmp_ill->ill_group_next;
14765 			ASSERT(tmp_ill != NULL);
14766 		}
14767 		tmp_ill->ill_group_next = ill->ill_group_next;
14768 	}
14769 	ill->ill_group = NULL;
14770 	ill->ill_group_next = NULL;
14771 
14772 	illgrp->illgrp_ill_count--;
14773 	mutex_exit(&ill->ill_lock);
14774 	rw_exit(&ill_g_lock);
14775 
14776 	/*
14777 	 * As this ill is leaving the group, we need to hand off
14778 	 * the responsibilities to the other ills in the group, if
14779 	 * this ill had some responsibilities.
14780 	 */
14781 
14782 	ill_handoff_responsibility(ill, illgrp);
14783 
14784 	rw_enter(&ill_g_lock, RW_WRITER);
14785 
14786 	if (illgrp->illgrp_ill_count == 0) {
14787 
14788 		ASSERT(illgrp->illgrp_ill == NULL);
14789 		if (ill->ill_isv6) {
14790 			if (illgrp == illgrp_head_v6) {
14791 				illgrp_head_v6 = illgrp->illgrp_next;
14792 			} else {
14793 				tmpg = illgrp_head_v6;
14794 				while (tmpg->illgrp_next != illgrp) {
14795 					tmpg = tmpg->illgrp_next;
14796 					ASSERT(tmpg != NULL);
14797 				}
14798 				tmpg->illgrp_next = illgrp->illgrp_next;
14799 			}
14800 		} else {
14801 			if (illgrp == illgrp_head_v4) {
14802 				illgrp_head_v4 = illgrp->illgrp_next;
14803 			} else {
14804 				tmpg = illgrp_head_v4;
14805 				while (tmpg->illgrp_next != illgrp) {
14806 					tmpg = tmpg->illgrp_next;
14807 					ASSERT(tmpg != NULL);
14808 				}
14809 				tmpg->illgrp_next = illgrp->illgrp_next;
14810 			}
14811 		}
14812 		mutex_destroy(&illgrp->illgrp_lock);
14813 		mi_free(illgrp);
14814 	}
14815 	rw_exit(&ill_g_lock);
14816 
14817 	/*
14818 	 * Even though the ill is out of the group its not necessary
14819 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
14820 	 * We will split the ipsq when phyint_groupname is set to NULL.
14821 	 */
14822 
14823 	/*
14824 	 * Send a routing sockets message if we are deleting from
14825 	 * groups with names.
14826 	 */
14827 	if (ill->ill_phyint->phyint_groupname_len != 0)
14828 		ip_rts_ifmsg(ill->ill_ipif);
14829 }
14830 
14831 /*
14832  * Re-do source address selection. This is normally called when
14833  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
14834  * ipif comes up.
14835  */
14836 void
14837 ill_update_source_selection(ill_t *ill)
14838 {
14839 	ipif_t *ipif;
14840 
14841 	ASSERT(IAM_WRITER_ILL(ill));
14842 
14843 	if (ill->ill_group != NULL)
14844 		ill = ill->ill_group->illgrp_ill;
14845 
14846 	for (; ill != NULL; ill = ill->ill_group_next) {
14847 		for (ipif = ill->ill_ipif; ipif != NULL;
14848 		    ipif = ipif->ipif_next) {
14849 			if (ill->ill_isv6)
14850 				ipif_recreate_interface_routes_v6(NULL, ipif);
14851 			else
14852 				ipif_recreate_interface_routes(NULL, ipif);
14853 		}
14854 	}
14855 }
14856 
14857 /*
14858  * Insert ill in a group headed by illgrp_head. The caller can either
14859  * pass a groupname in which case we search for a group with the
14860  * same name to insert in or pass a group to insert in. This function
14861  * would only search groups with names.
14862  *
14863  * NOTE : The caller should make sure that there is at least one ipif
14864  *	  UP on this ill so that illgrp_scheduler can pick this ill
14865  *	  for outbound packets. If ill_ipif_up_count is zero, we have
14866  *	  already sent a DL_UNBIND to the driver and we don't want to
14867  *	  send anymore packets. We don't assert for ipif_up_count
14868  *	  to be greater than zero, because ipif_up_done wants to call
14869  *	  this function before bumping up the ipif_up_count. See
14870  *	  ipif_up_done() for details.
14871  */
14872 int
14873 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
14874     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
14875 {
14876 	ill_group_t *illgrp;
14877 	ill_t *prev_ill;
14878 	phyint_t *phyi;
14879 
14880 	ASSERT(ill->ill_group == NULL);
14881 
14882 	rw_enter(&ill_g_lock, RW_WRITER);
14883 	mutex_enter(&ill->ill_lock);
14884 
14885 	if (groupname != NULL) {
14886 		/*
14887 		 * Look for a group with a matching groupname to insert.
14888 		 */
14889 		for (illgrp = *illgrp_head; illgrp != NULL;
14890 		    illgrp = illgrp->illgrp_next) {
14891 
14892 			ill_t *tmp_ill;
14893 
14894 			tmp_ill = illgrp->illgrp_ill;
14895 			ASSERT(tmp_ill != NULL && tmp_ill->ill_phyint != NULL);
14896 			phyi = tmp_ill->ill_phyint;
14897 			/*
14898 			 * Look at groups which has names only.
14899 			 */
14900 			if (phyi->phyint_groupname_len == 0)
14901 				continue;
14902 			/*
14903 			 * Names are stored in the phyint common to both
14904 			 * IPv4 and IPv6.
14905 			 */
14906 			if (mi_strcmp(phyi->phyint_groupname,
14907 			    groupname) == 0) {
14908 				break;
14909 			}
14910 		}
14911 	} else {
14912 		/*
14913 		 * If the caller passes in a NULL "grp_to_insert", we
14914 		 * allocate one below and insert this singleton.
14915 		 */
14916 		illgrp = grp_to_insert;
14917 	}
14918 
14919 	ill->ill_group_next = NULL;
14920 
14921 	if (illgrp == NULL) {
14922 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
14923 		if (illgrp == NULL) {
14924 			return (ENOMEM);
14925 		}
14926 		illgrp->illgrp_next = *illgrp_head;
14927 		*illgrp_head = illgrp;
14928 		illgrp->illgrp_ill = ill;
14929 		illgrp->illgrp_ill_count = 1;
14930 		ill->ill_group = illgrp;
14931 		/*
14932 		 * Used in illgrp_scheduler to protect multiple threads
14933 		 * from traversing the list.
14934 		 */
14935 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
14936 	} else {
14937 		ASSERT(ill->ill_net_type ==
14938 		    illgrp->illgrp_ill->ill_net_type);
14939 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
14940 
14941 		/* Insert ill at tail of this group */
14942 		prev_ill = illgrp->illgrp_ill;
14943 		while (prev_ill->ill_group_next != NULL)
14944 			prev_ill = prev_ill->ill_group_next;
14945 		prev_ill->ill_group_next = ill;
14946 		ill->ill_group = illgrp;
14947 		illgrp->illgrp_ill_count++;
14948 		/*
14949 		 * Inherit group properties. Currently only forwarding
14950 		 * is the property we try to keep the same with all the
14951 		 * ills. When there are more, we will abstract this into
14952 		 * a function.
14953 		 */
14954 		ill->ill_flags &= ~ILLF_ROUTER;
14955 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
14956 	}
14957 	mutex_exit(&ill->ill_lock);
14958 	rw_exit(&ill_g_lock);
14959 
14960 	/*
14961 	 * 1) When ipif_up_done() calls this function, ipif_up_count
14962 	 *    may be zero as it has not yet been bumped. But the ires
14963 	 *    have already been added. So, we do the nomination here
14964 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
14965 	 *    for ill_ipif_up_count != 0. Thus we don't check for
14966 	 *    ill_ipif_up_count here while nominating broadcast ires for
14967 	 *    receive.
14968 	 *
14969 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
14970 	 *    to group them properly as ire_add() has already happened
14971 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
14972 	 *    case, we need to do it here anyway.
14973 	 */
14974 	if (!ill->ill_isv6) {
14975 		ill_group_bcast_for_xmit(ill);
14976 		ill_nominate_bcast_rcv(illgrp);
14977 	}
14978 
14979 	if (!ipif_is_coming_up) {
14980 		/*
14981 		 * When ipif_up_done() calls this function, the multicast
14982 		 * groups have not been joined yet. So, there is no point in
14983 		 * nomination. ip_join_allmulti will handle groups when
14984 		 * ill_recover_multicast is called from ipif_up_done() later.
14985 		 */
14986 		(void) ill_nominate_mcast_rcv(illgrp);
14987 		/*
14988 		 * ipif_up_done calls ill_update_source_selection
14989 		 * anyway. Moreover, we don't want to re-create
14990 		 * interface routes while ipif_up_done() still has reference
14991 		 * to them. Refer to ipif_up_done() for more details.
14992 		 */
14993 		ill_update_source_selection(ill);
14994 	}
14995 
14996 	/*
14997 	 * Send a routing sockets message if we are inserting into
14998 	 * groups with names.
14999 	 */
15000 	if (groupname != NULL)
15001 		ip_rts_ifmsg(ill->ill_ipif);
15002 	return (0);
15003 }
15004 
15005 /*
15006  * Return the first phyint matching the groupname. There could
15007  * be more than one when there are ill groups.
15008  *
15009  * Needs work: called only from ip_sioctl_groupname
15010  */
15011 static phyint_t *
15012 phyint_lookup_group(char *groupname)
15013 {
15014 	phyint_t *phyi;
15015 
15016 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
15017 	/*
15018 	 * Group names are stored in the phyint - a common structure
15019 	 * to both IPv4 and IPv6.
15020 	 */
15021 	phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
15022 	for (; phyi != NULL;
15023 	    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
15024 	    phyi, AVL_AFTER)) {
15025 		if (phyi->phyint_groupname_len == 0)
15026 			continue;
15027 		ASSERT(phyi->phyint_groupname != NULL);
15028 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
15029 			return (phyi);
15030 	}
15031 	return (NULL);
15032 }
15033 
15034 
15035 
15036 /*
15037  * MT notes on creation and deletion of IPMP groups
15038  *
15039  * Creation and deletion of IPMP groups introduce the need to merge or
15040  * split the associated serialization objects i.e the ipsq's. Normally all
15041  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
15042  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
15043  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
15044  * is a need to change the <ill-ipsq> association and we have to operate on both
15045  * the source and destination IPMP groups. For eg. attempting to set the
15046  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
15047  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
15048  * source or destination IPMP group are mapped to a single ipsq for executing
15049  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
15050  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
15051  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
15052  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
15053  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
15054  * ipsq has to be examined for redoing the <ill-ipsq> associations.
15055  *
15056  * In the above example the ioctl handling code locates the current ipsq of hme0
15057  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
15058  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
15059  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
15060  * the destination ipsq. If the destination ipsq is not busy, it also enters
15061  * the destination ipsq exclusively. Now the actual groupname setting operation
15062  * can proceed. If the destination ipsq is busy, the operation is enqueued
15063  * on the destination (merged) ipsq and will be handled in the unwind from
15064  * ipsq_exit.
15065  *
15066  * To prevent other threads accessing the ill while the group name change is
15067  * in progres, we bring down the ipifs which also removes the ill from the
15068  * group. The group is changed in phyint and when the first ipif on the ill
15069  * is brought up, the ill is inserted into the right IPMP group by
15070  * illgrp_insert.
15071  */
15072 /* ARGSUSED */
15073 int
15074 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15075     ip_ioctl_cmd_t *ipip, void *ifreq)
15076 {
15077 	int i;
15078 	char *tmp;
15079 	int namelen;
15080 	ill_t *ill = ipif->ipif_ill;
15081 	ill_t *ill_v4, *ill_v6;
15082 	int err = 0;
15083 	phyint_t *phyi;
15084 	phyint_t *phyi_tmp;
15085 	struct lifreq *lifr;
15086 	mblk_t	*mp1;
15087 	char *groupname;
15088 	ipsq_t *ipsq;
15089 
15090 	ASSERT(IAM_WRITER_IPIF(ipif));
15091 
15092 	/* Existance verified in ip_wput_nondata */
15093 	mp1 = mp->b_cont->b_cont;
15094 	lifr = (struct lifreq *)mp1->b_rptr;
15095 	groupname = lifr->lifr_groupname;
15096 
15097 	if (ipif->ipif_id != 0)
15098 		return (EINVAL);
15099 
15100 	phyi = ill->ill_phyint;
15101 	ASSERT(phyi != NULL);
15102 
15103 	if (phyi->phyint_flags & PHYI_VIRTUAL)
15104 		return (EINVAL);
15105 
15106 	tmp = groupname;
15107 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
15108 		;
15109 
15110 	if (i == LIFNAMSIZ) {
15111 		/* no null termination */
15112 		return (EINVAL);
15113 	}
15114 
15115 	/*
15116 	 * Calculate the namelen exclusive of the null
15117 	 * termination character.
15118 	 */
15119 	namelen = tmp - groupname;
15120 
15121 	ill_v4 = phyi->phyint_illv4;
15122 	ill_v6 = phyi->phyint_illv6;
15123 
15124 	/*
15125 	 * ILL cannot be part of a usesrc group and and IPMP group at the
15126 	 * same time. No need to grab the ill_g_usesrc_lock here, see
15127 	 * synchronization notes in ip.c
15128 	 */
15129 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
15130 		return (EINVAL);
15131 	}
15132 
15133 	/*
15134 	 * mark the ill as changing.
15135 	 * this should queue all new requests on the syncq.
15136 	 */
15137 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
15138 
15139 	if (ill_v4 != NULL)
15140 		ill_v4->ill_state_flags |= ILL_CHANGING;
15141 	if (ill_v6 != NULL)
15142 		ill_v6->ill_state_flags |= ILL_CHANGING;
15143 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15144 
15145 	if (namelen == 0) {
15146 		/*
15147 		 * Null string means remove this interface from the
15148 		 * existing group.
15149 		 */
15150 		if (phyi->phyint_groupname_len == 0) {
15151 			/*
15152 			 * Never was in a group.
15153 			 */
15154 			err = 0;
15155 			goto done;
15156 		}
15157 
15158 		/*
15159 		 * IPv4 or IPv6 may be temporarily out of the group when all
15160 		 * the ipifs are down. Thus, we need to check for ill_group to
15161 		 * be non-NULL.
15162 		 */
15163 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
15164 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
15165 			mutex_enter(&ill_v4->ill_lock);
15166 			if (!ill_is_quiescent(ill_v4)) {
15167 				/*
15168 				 * ipsq_pending_mp_add will not fail since
15169 				 * connp is NULL
15170 				 */
15171 				(void) ipsq_pending_mp_add(NULL,
15172 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
15173 				mutex_exit(&ill_v4->ill_lock);
15174 				err = EINPROGRESS;
15175 				goto done;
15176 			}
15177 			mutex_exit(&ill_v4->ill_lock);
15178 		}
15179 
15180 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
15181 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
15182 			mutex_enter(&ill_v6->ill_lock);
15183 			if (!ill_is_quiescent(ill_v6)) {
15184 				(void) ipsq_pending_mp_add(NULL,
15185 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
15186 				mutex_exit(&ill_v6->ill_lock);
15187 				err = EINPROGRESS;
15188 				goto done;
15189 			}
15190 			mutex_exit(&ill_v6->ill_lock);
15191 		}
15192 
15193 		rw_enter(&ill_g_lock, RW_WRITER);
15194 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15195 		mutex_enter(&phyi->phyint_lock);
15196 		ASSERT(phyi->phyint_groupname != NULL);
15197 		mi_free(phyi->phyint_groupname);
15198 		phyi->phyint_groupname = NULL;
15199 		phyi->phyint_groupname_len = 0;
15200 		mutex_exit(&phyi->phyint_lock);
15201 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15202 		rw_exit(&ill_g_lock);
15203 		err = ill_up_ipifs(ill, q, mp);
15204 
15205 		/*
15206 		 * set the split flag so that the ipsq can be split
15207 		 */
15208 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15209 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
15210 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15211 
15212 	} else {
15213 		if (phyi->phyint_groupname_len != 0) {
15214 			ASSERT(phyi->phyint_groupname != NULL);
15215 			/* Are we inserting in the same group ? */
15216 			if (mi_strcmp(groupname,
15217 			    phyi->phyint_groupname) == 0) {
15218 				err = 0;
15219 				goto done;
15220 			}
15221 		}
15222 
15223 		rw_enter(&ill_g_lock, RW_READER);
15224 		/*
15225 		 * Merge ipsq for the group's.
15226 		 * This check is here as multiple groups/ills might be
15227 		 * sharing the same ipsq.
15228 		 * If we have to merege than the operation is restarted
15229 		 * on the new ipsq.
15230 		 */
15231 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL);
15232 		if (phyi->phyint_ipsq != ipsq) {
15233 			rw_exit(&ill_g_lock);
15234 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
15235 			goto done;
15236 		}
15237 		/*
15238 		 * Running exclusive on new ipsq.
15239 		 */
15240 
15241 		ASSERT(ipsq != NULL);
15242 		ASSERT(ipsq->ipsq_writer == curthread);
15243 
15244 		/*
15245 		 * Check whether the ill_type and ill_net_type matches before
15246 		 * we allocate any memory so that the cleanup is easier.
15247 		 *
15248 		 * We can't group dissimilar ones as we can't load spread
15249 		 * packets across the group because of potential link-level
15250 		 * header differences.
15251 		 */
15252 		phyi_tmp = phyint_lookup_group(groupname);
15253 		if (phyi_tmp != NULL) {
15254 			if ((ill_v4 != NULL &&
15255 			    phyi_tmp->phyint_illv4 != NULL) &&
15256 			    ((ill_v4->ill_net_type !=
15257 			    phyi_tmp->phyint_illv4->ill_net_type) ||
15258 			    (ill_v4->ill_type !=
15259 			    phyi_tmp->phyint_illv4->ill_type))) {
15260 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15261 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
15262 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15263 				rw_exit(&ill_g_lock);
15264 				return (EINVAL);
15265 			}
15266 			if ((ill_v6 != NULL &&
15267 			    phyi_tmp->phyint_illv6 != NULL) &&
15268 			    ((ill_v6->ill_net_type !=
15269 			    phyi_tmp->phyint_illv6->ill_net_type) ||
15270 			    (ill_v6->ill_type !=
15271 			    phyi_tmp->phyint_illv6->ill_type))) {
15272 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15273 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
15274 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15275 				rw_exit(&ill_g_lock);
15276 				return (EINVAL);
15277 			}
15278 		}
15279 
15280 		rw_exit(&ill_g_lock);
15281 
15282 		/*
15283 		 * bring down all v4 ipifs.
15284 		 */
15285 		if (ill_v4 != NULL) {
15286 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
15287 		}
15288 
15289 		/*
15290 		 * bring down all v6 ipifs.
15291 		 */
15292 		if (ill_v6 != NULL) {
15293 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
15294 		}
15295 
15296 		/*
15297 		 * make sure all ipifs are down and there are no active
15298 		 * references. Call to ipsq_pending_mp_add will not fail
15299 		 * since connp is NULL.
15300 		 */
15301 		if (ill_v4 != NULL) {
15302 			mutex_enter(&ill_v4->ill_lock);
15303 			if (!ill_is_quiescent(ill_v4)) {
15304 				(void) ipsq_pending_mp_add(NULL,
15305 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
15306 				mutex_exit(&ill_v4->ill_lock);
15307 				err = EINPROGRESS;
15308 				goto done;
15309 			}
15310 			mutex_exit(&ill_v4->ill_lock);
15311 		}
15312 
15313 		if (ill_v6 != NULL) {
15314 			mutex_enter(&ill_v6->ill_lock);
15315 			if (!ill_is_quiescent(ill_v6)) {
15316 				(void) ipsq_pending_mp_add(NULL,
15317 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
15318 				mutex_exit(&ill_v6->ill_lock);
15319 				err = EINPROGRESS;
15320 				goto done;
15321 			}
15322 			mutex_exit(&ill_v6->ill_lock);
15323 		}
15324 
15325 		/*
15326 		 * allocate including space for null terminator
15327 		 * before we insert.
15328 		 */
15329 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
15330 		if (tmp == NULL)
15331 			return (ENOMEM);
15332 
15333 		rw_enter(&ill_g_lock, RW_WRITER);
15334 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15335 		mutex_enter(&phyi->phyint_lock);
15336 		if (phyi->phyint_groupname_len != 0) {
15337 			ASSERT(phyi->phyint_groupname != NULL);
15338 			mi_free(phyi->phyint_groupname);
15339 		}
15340 
15341 		/*
15342 		 * setup the new group name.
15343 		 */
15344 		phyi->phyint_groupname = tmp;
15345 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
15346 		phyi->phyint_groupname_len = namelen + 1;
15347 		mutex_exit(&phyi->phyint_lock);
15348 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15349 		rw_exit(&ill_g_lock);
15350 
15351 		err = ill_up_ipifs(ill, q, mp);
15352 	}
15353 
15354 done:
15355 	/*
15356 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
15357 	 */
15358 	if (err != EINPROGRESS) {
15359 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15360 		if (ill_v4 != NULL)
15361 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
15362 		if (ill_v6 != NULL)
15363 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
15364 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15365 	}
15366 	return (err);
15367 }
15368 
15369 /* ARGSUSED */
15370 int
15371 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
15372     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15373 {
15374 	ill_t *ill;
15375 	phyint_t *phyi;
15376 	struct lifreq *lifr;
15377 	mblk_t	*mp1;
15378 
15379 	/* Existence verified in ip_wput_nondata */
15380 	mp1 = mp->b_cont->b_cont;
15381 	lifr = (struct lifreq *)mp1->b_rptr;
15382 	ill = ipif->ipif_ill;
15383 	phyi = ill->ill_phyint;
15384 
15385 	lifr->lifr_groupname[0] = '\0';
15386 	/*
15387 	 * ill_group may be null if all the interfaces
15388 	 * are down. But still, the phyint should always
15389 	 * hold the name.
15390 	 */
15391 	if (phyi->phyint_groupname_len != 0) {
15392 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
15393 		    phyi->phyint_groupname_len);
15394 	}
15395 
15396 	return (0);
15397 }
15398 
15399 
15400 typedef struct conn_move_s {
15401 	ill_t	*cm_from_ill;
15402 	ill_t	*cm_to_ill;
15403 	int	cm_ifindex;
15404 } conn_move_t;
15405 
15406 /*
15407  * ipcl_walk function for moving conn_multicast_ill for a given ill.
15408  */
15409 static void
15410 conn_move(conn_t *connp, caddr_t arg)
15411 {
15412 	conn_move_t *connm;
15413 	int ifindex;
15414 	int i;
15415 	ill_t *from_ill;
15416 	ill_t *to_ill;
15417 	ilg_t *ilg;
15418 	ilm_t *ret_ilm;
15419 
15420 	connm = (conn_move_t *)arg;
15421 	ifindex = connm->cm_ifindex;
15422 	from_ill = connm->cm_from_ill;
15423 	to_ill = connm->cm_to_ill;
15424 
15425 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
15426 
15427 	/* All multicast fields protected by conn_lock */
15428 	mutex_enter(&connp->conn_lock);
15429 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
15430 	if ((connp->conn_outgoing_ill == from_ill) &&
15431 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
15432 		connp->conn_outgoing_ill = to_ill;
15433 		connp->conn_incoming_ill = to_ill;
15434 	}
15435 
15436 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
15437 
15438 	if ((connp->conn_multicast_ill == from_ill) &&
15439 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
15440 		connp->conn_multicast_ill = connm->cm_to_ill;
15441 	}
15442 
15443 	/* Change IP_XMIT_IF associations */
15444 	if ((connp->conn_xmit_if_ill == from_ill) &&
15445 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
15446 		connp->conn_xmit_if_ill = to_ill;
15447 	}
15448 	/*
15449 	 * Change the ilg_ill to point to the new one. This assumes
15450 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
15451 	 * has been told to receive packets on this interface.
15452 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
15453 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
15454 	 * some ilms may not have moved. We check to see whether
15455 	 * the ilms have moved to to_ill. We can't check on from_ill
15456 	 * as in the process of moving, we could have split an ilm
15457 	 * in to two - which has the same orig_ifindex and v6group.
15458 	 *
15459 	 * For IPv4, ilg_ipif moves implicitly. The code below really
15460 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
15461 	 */
15462 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
15463 		ilg = &connp->conn_ilg[i];
15464 		if ((ilg->ilg_ill == from_ill) &&
15465 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
15466 			/* ifindex != 0 indicates failback */
15467 			if (ifindex != 0) {
15468 				connp->conn_ilg[i].ilg_ill = to_ill;
15469 				continue;
15470 			}
15471 
15472 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
15473 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
15474 			    connp->conn_zoneid);
15475 
15476 			if (ret_ilm != NULL)
15477 				connp->conn_ilg[i].ilg_ill = to_ill;
15478 		}
15479 	}
15480 	mutex_exit(&connp->conn_lock);
15481 }
15482 
15483 static void
15484 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
15485 {
15486 	conn_move_t connm;
15487 
15488 	connm.cm_from_ill = from_ill;
15489 	connm.cm_to_ill = to_ill;
15490 	connm.cm_ifindex = ifindex;
15491 
15492 	ipcl_walk(conn_move, (caddr_t)&connm);
15493 }
15494 
15495 /*
15496  * ilm has been moved from from_ill to to_ill.
15497  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
15498  * appropriately.
15499  *
15500  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
15501  *	  the code there de-references ipif_ill to get the ill to
15502  *	  send multicast requests. It does not work as ipif is on its
15503  *	  move and already moved when this function is called.
15504  *	  Thus, we need to use from_ill and to_ill send down multicast
15505  *	  requests.
15506  */
15507 static void
15508 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
15509 {
15510 	ipif_t *ipif;
15511 	ilm_t *ilm;
15512 
15513 	/*
15514 	 * See whether we need to send down DL_ENABMULTI_REQ on
15515 	 * to_ill as ilm has just been added.
15516 	 */
15517 	ASSERT(IAM_WRITER_ILL(to_ill));
15518 	ASSERT(IAM_WRITER_ILL(from_ill));
15519 
15520 	ILM_WALKER_HOLD(to_ill);
15521 	for (ilm = to_ill->ill_ilm; ilm != NULL && ilm->ilm_is_new &&
15522 	    !(ilm->ilm_flags & ILM_DELETED); ilm = ilm->ilm_next) {
15523 
15524 		/*
15525 		 * no locks held, ill/ipif cannot dissappear as long
15526 		 * as we are writer.
15527 		 */
15528 		ipif = to_ill->ill_ipif;
15529 		/*
15530 		 * No need to hold any lock as we are the writer and this
15531 		 * can only be changed by a writer.
15532 		 */
15533 		ilm->ilm_is_new = B_FALSE;
15534 
15535 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
15536 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
15537 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
15538 			    "resolver\n"));
15539 			continue;		/* Must be IRE_IF_NORESOLVER */
15540 		}
15541 
15542 
15543 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
15544 			ip1dbg(("ilm_send_multicast_reqs: "
15545 			    "to_ill MULTI_BCAST\n"));
15546 			ilm->ilm_join_mld = B_FALSE;
15547 			goto from;
15548 		}
15549 
15550 		if (ilm->ilm_join_mld) {
15551 			ASSERT(to_ill->ill_isv6);
15552 			mld_joingroup(ilm);
15553 		}
15554 
15555 		ilm->ilm_join_mld = B_FALSE;
15556 
15557 		if (to_ill->ill_ipif_up_count == 0) {
15558 			/*
15559 			 * Nobody there. All multicast addresses will be
15560 			 * re-joined when we get the DL_BIND_ACK bringing the
15561 			 * interface up.
15562 			 */
15563 			ilm->ilm_notify_driver = B_FALSE;
15564 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
15565 			goto from;
15566 		}
15567 
15568 		/*
15569 		 * For allmulti address, we want to join on only one interface.
15570 		 * Checking for ilm_numentries_v6 is not correct as you may
15571 		 * find an ilm with zero address on to_ill, but we may not
15572 		 * have nominated to_ill for receiving. Thus, if we have
15573 		 * nominated from_ill (ill_join_allmulti is set), nominate
15574 		 * only if to_ill is not already nominated (to_ill normally
15575 		 * should not have been nominated if "from_ill" has already
15576 		 * been nominated. As we don't prevent failovers from happening
15577 		 * across groups, we don't assert).
15578 		 */
15579 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15580 			/*
15581 			 * There is no need to hold ill locks as we are
15582 			 * writer on both ills and when ill_join_allmulti
15583 			 * is changed the thread is always a writer.
15584 			 */
15585 			if (from_ill->ill_join_allmulti &&
15586 			    !to_ill->ill_join_allmulti) {
15587 				(void) ip_join_allmulti(to_ill->ill_ipif);
15588 			}
15589 		} else if (ilm->ilm_notify_driver) {
15590 
15591 			/*
15592 			 * This is a newly moved ilm so we need to tell the
15593 			 * driver about the new group. There can be more than
15594 			 * one ilm's for the same group in the list each with a
15595 			 * different orig_ifindex. We have to inform the driver
15596 			 * once. In ilm_move_v[4,6] we only set the flag
15597 			 * ilm_notify_driver for the first ilm.
15598 			 */
15599 
15600 			(void) ip_ll_send_enabmulti_req(to_ill,
15601 			    &ilm->ilm_v6addr);
15602 		}
15603 
15604 		ilm->ilm_notify_driver = B_FALSE;
15605 
15606 		/*
15607 		 * See whether we need to send down DL_DISABMULTI_REQ on
15608 		 * from_ill as ilm has just been removed.
15609 		 */
15610 from:
15611 		ipif = from_ill->ill_ipif;
15612 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
15613 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
15614 			ip1dbg(("ilm_send_multicast_reqs: "
15615 			    "from_ill not resolver\n"));
15616 			continue;		/* Must be IRE_IF_NORESOLVER */
15617 		}
15618 
15619 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
15620 			ip1dbg(("ilm_send_multicast_reqs: "
15621 			    "from_ill MULTI_BCAST\n"));
15622 			continue;
15623 		}
15624 
15625 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15626 			if (from_ill->ill_join_allmulti)
15627 			    (void) ip_leave_allmulti(from_ill->ill_ipif);
15628 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
15629 			(void) ip_ll_send_disabmulti_req(from_ill,
15630 		    &ilm->ilm_v6addr);
15631 		}
15632 	}
15633 	ILM_WALKER_RELE(to_ill);
15634 }
15635 
15636 /*
15637  * This function is called when all multicast memberships needs
15638  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
15639  * called only once unlike the IPv4 counterpart where it is called after
15640  * every logical interface is moved. The reason is due to multicast
15641  * memberships are joined using an interface address in IPv4 while in
15642  * IPv6, interface index is used.
15643  */
15644 static void
15645 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
15646 {
15647 	ilm_t	*ilm;
15648 	ilm_t	*ilm_next;
15649 	ilm_t	*new_ilm;
15650 	ilm_t	**ilmp;
15651 	int	count;
15652 	char buf[INET6_ADDRSTRLEN];
15653 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
15654 
15655 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
15656 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
15657 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
15658 
15659 	if (ifindex == 0) {
15660 		/*
15661 		 * Form the solicited node mcast address which is used later.
15662 		 */
15663 		ipif_t *ipif;
15664 
15665 		ipif = from_ill->ill_ipif;
15666 		ASSERT(ipif->ipif_id == 0);
15667 
15668 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
15669 	}
15670 
15671 	ilmp = &from_ill->ill_ilm;
15672 	for (ilm = from_ill->ill_ilm; ilm != NULL &&
15673 	    !(ilm->ilm_flags & ILM_DELETED); ilm = ilm_next) {
15674 		ilm_next = ilm->ilm_next;
15675 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
15676 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
15677 		ASSERT(ilm->ilm_orig_ifindex != 0);
15678 		if (ilm->ilm_orig_ifindex == ifindex) {
15679 			/*
15680 			 * We are failing back multicast memberships.
15681 			 * If the same ilm exists in to_ill, it means somebody
15682 			 * has joined the same group there e.g. ff02::1
15683 			 * is joined within the kernel when the interfaces
15684 			 * came UP.
15685 			 */
15686 			ASSERT(ilm->ilm_ipif == NULL);
15687 			if (new_ilm != NULL) {
15688 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
15689 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
15690 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
15691 					new_ilm->ilm_join_mld = B_TRUE;
15692 				}
15693 			} else {
15694 				/*
15695 				 * check if we can just move the ilm
15696 				 */
15697 				if (from_ill->ill_ilm_walker_cnt != 0) {
15698 					/*
15699 					 * We have walkers we cannot move
15700 					 * the ilm, so allocate a new ilm,
15701 					 * this (old) ilm will be marked
15702 					 * ILM_DELETED at the end of the loop
15703 					 * and will be freed when the
15704 					 * last walker exits.
15705 					 */
15706 					new_ilm = (ilm_t *)mi_zalloc
15707 					    (sizeof (ilm_t));
15708 					if (new_ilm == NULL) {
15709 						ip0dbg(("ilm_move_v6: "
15710 						    "FAILBACK of IPv6"
15711 						    " multicast address %s : "
15712 						    "from %s to"
15713 						    " %s failed : ENOMEM \n",
15714 						    inet_ntop(AF_INET6,
15715 						    &ilm->ilm_v6addr, buf,
15716 						    sizeof (buf)),
15717 						    from_ill->ill_name,
15718 						    to_ill->ill_name));
15719 
15720 							ilmp = &ilm->ilm_next;
15721 							continue;
15722 					}
15723 					*new_ilm = *ilm;
15724 					/*
15725 					 * we don't want new_ilm linked to
15726 					 * ilm's filter list.
15727 					 */
15728 					new_ilm->ilm_filter = NULL;
15729 				} else {
15730 					/*
15731 					 * No walkers we can move the ilm.
15732 					 * lets take it out of the list.
15733 					 */
15734 					*ilmp = ilm->ilm_next;
15735 					ilm->ilm_next = NULL;
15736 					new_ilm = ilm;
15737 				}
15738 
15739 				new_ilm->ilm_ill = to_ill;
15740 				/* Add to the to_ill's list */
15741 				new_ilm->ilm_next = to_ill->ill_ilm;
15742 				to_ill->ill_ilm = new_ilm;
15743 				/*
15744 				 * set the flag so that mld_joingroup is
15745 				 * called in ilm_send_multicast_reqs().
15746 				 */
15747 				new_ilm->ilm_join_mld = B_TRUE;
15748 				/*
15749 				 * if this is the first ilm for the group
15750 				 * set ilm_notify_driver so that we notify the
15751 				 * driver in ilm_send_multicast_reqs.
15752 				 */
15753 				if (ilm_lookup_ill_v6(to_ill,
15754 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
15755 					new_ilm->ilm_notify_driver = B_TRUE;
15756 			}
15757 			goto bottom;
15758 		} else if (ifindex != 0) {
15759 			/*
15760 			 * If this is FAILBACK (ifindex != 0) and the ifindex
15761 			 * has not matched above, look at the next ilm.
15762 			 */
15763 			ilmp = &ilm->ilm_next;
15764 			continue;
15765 		}
15766 		/*
15767 		 * If we are here, it means ifindex is 0. Failover
15768 		 * everything.
15769 		 *
15770 		 * We need to handle solicited node mcast address
15771 		 * and all_nodes mcast address differently as they
15772 		 * are joined witin the kenrel (ipif_multicast_up)
15773 		 * and potentially from the userland. We are called
15774 		 * after the ipifs of from_ill has been moved.
15775 		 * If we still find ilms on ill with solicited node
15776 		 * mcast address or all_nodes mcast address, it must
15777 		 * belong to the UP interface that has not moved e.g.
15778 		 * ipif_id 0 with the link local prefix does not move.
15779 		 * We join this on the new ill accounting for all the
15780 		 * userland memberships so that applications don't
15781 		 * see any failure.
15782 		 *
15783 		 * We need to make sure that we account only for the
15784 		 * solicited node and all node multicast addresses
15785 		 * that was brought UP on these. In the case of
15786 		 * a failover from A to B, we might have ilms belonging
15787 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
15788 		 * for the membership from the userland. If we are failing
15789 		 * over from B to C now, we will find the ones belonging
15790 		 * to A on B. These don't account for the ill_ipif_up_count.
15791 		 * They just move from B to C. The check below on
15792 		 * ilm_orig_ifindex ensures that.
15793 		 */
15794 		if ((ilm->ilm_orig_ifindex ==
15795 		    from_ill->ill_phyint->phyint_ifindex) &&
15796 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
15797 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
15798 		    &ilm->ilm_v6addr))) {
15799 			ASSERT(ilm->ilm_refcnt > 0);
15800 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
15801 			/*
15802 			 * For indentation reasons, we are not using a
15803 			 * "else" here.
15804 			 */
15805 			if (count == 0) {
15806 				ilmp = &ilm->ilm_next;
15807 				continue;
15808 			}
15809 			ilm->ilm_refcnt -= count;
15810 			if (new_ilm != NULL) {
15811 				/*
15812 				 * Can find one with the same
15813 				 * ilm_orig_ifindex, if we are failing
15814 				 * over to a STANDBY. This happens
15815 				 * when somebody wants to join a group
15816 				 * on a STANDBY interface and we
15817 				 * internally join on a different one.
15818 				 * If we had joined on from_ill then, a
15819 				 * failover now will find a new ilm
15820 				 * with this index.
15821 				 */
15822 				ip1dbg(("ilm_move_v6: FAILOVER, found"
15823 				    " new ilm on %s, group address %s\n",
15824 				    to_ill->ill_name,
15825 				    inet_ntop(AF_INET6,
15826 				    &ilm->ilm_v6addr, buf,
15827 				    sizeof (buf))));
15828 				new_ilm->ilm_refcnt += count;
15829 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
15830 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
15831 					new_ilm->ilm_join_mld = B_TRUE;
15832 				}
15833 			} else {
15834 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
15835 				if (new_ilm == NULL) {
15836 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
15837 					    " multicast address %s : from %s to"
15838 					    " %s failed : ENOMEM \n",
15839 					    inet_ntop(AF_INET6,
15840 					    &ilm->ilm_v6addr, buf,
15841 					    sizeof (buf)), from_ill->ill_name,
15842 					    to_ill->ill_name));
15843 					ilmp = &ilm->ilm_next;
15844 					continue;
15845 				}
15846 				*new_ilm = *ilm;
15847 				new_ilm->ilm_filter = NULL;
15848 				new_ilm->ilm_refcnt = count;
15849 				new_ilm->ilm_ill = to_ill;
15850 				new_ilm->ilm_timer = INFINITY;
15851 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
15852 				new_ilm->ilm_join_mld = B_TRUE;
15853 				/* Add to the to_ill's list */
15854 				new_ilm->ilm_next = to_ill->ill_ilm;
15855 				to_ill->ill_ilm = new_ilm;
15856 				/*
15857 				 * If the to_ill has not joined this
15858 				 * group we need to tell the driver in
15859 				 * ill_send_multicast_reqs.
15860 				 */
15861 				if (ilm_lookup_ill_v6(to_ill,
15862 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
15863 					new_ilm->ilm_notify_driver = B_TRUE;
15864 				ASSERT(new_ilm->ilm_ipif == NULL);
15865 			}
15866 			if (ilm->ilm_refcnt == 0) {
15867 				goto bottom;
15868 			} else {
15869 				new_ilm->ilm_is_new = B_TRUE;
15870 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
15871 				CLEAR_SLIST(new_ilm->ilm_filter);
15872 				ilmp = &ilm->ilm_next;
15873 			}
15874 			continue;
15875 		} else {
15876 			/*
15877 			 * ifindex = 0 means, move everything pointing at
15878 			 * from_ill. We are doing this becuase ill has
15879 			 * either FAILED or became INACTIVE.
15880 			 *
15881 			 * As we would like to move things later back to
15882 			 * from_ill, we want to retain the identity of this
15883 			 * ilm. Thus, we don't blindly increment the reference
15884 			 * count on the ilms matching the address alone. We
15885 			 * need to match on the ilm_orig_index also. new_ilm
15886 			 * was obtained by matching ilm_orig_index also.
15887 			 */
15888 			if (new_ilm != NULL) {
15889 				/*
15890 				 * This is possible only if a previous restore
15891 				 * was incomplete i.e restore to
15892 				 * ilm_orig_ifindex left some ilms because
15893 				 * of some failures. Thus when we are failing
15894 				 * again, we might find our old friends there.
15895 				 */
15896 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
15897 				    " on %s, group address %s\n",
15898 				    to_ill->ill_name,
15899 				    inet_ntop(AF_INET6,
15900 				    &ilm->ilm_v6addr, buf,
15901 				    sizeof (buf))));
15902 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
15903 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
15904 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
15905 					new_ilm->ilm_join_mld = B_TRUE;
15906 				}
15907 			} else {
15908 				if (from_ill->ill_ilm_walker_cnt != 0) {
15909 					new_ilm = (ilm_t *)
15910 					    mi_zalloc(sizeof (ilm_t));
15911 					if (new_ilm == NULL) {
15912 						ip0dbg(("ilm_move_v6: "
15913 						    "FAILOVER of IPv6"
15914 						    " multicast address %s : "
15915 						    "from %s to"
15916 						    " %s failed : ENOMEM \n",
15917 						    inet_ntop(AF_INET6,
15918 						    &ilm->ilm_v6addr, buf,
15919 						    sizeof (buf)),
15920 						    from_ill->ill_name,
15921 						    to_ill->ill_name));
15922 
15923 							ilmp = &ilm->ilm_next;
15924 							continue;
15925 					}
15926 					*new_ilm = *ilm;
15927 					new_ilm->ilm_filter = NULL;
15928 				} else {
15929 					*ilmp = ilm->ilm_next;
15930 					new_ilm = ilm;
15931 				}
15932 				/* Add to the to_ill's list */
15933 				new_ilm->ilm_next = to_ill->ill_ilm;
15934 				to_ill->ill_ilm = new_ilm;
15935 				ASSERT(ilm->ilm_ipif == NULL);
15936 				new_ilm->ilm_ill = to_ill;
15937 				new_ilm->ilm_join_mld = B_TRUE;
15938 				/*
15939 				 * If the to_ill has not joined this
15940 				 * group we need to tell the driver in
15941 				 * ill_send_multicast_reqs.
15942 				 */
15943 				if (ilm_lookup_ill_v6(to_ill,
15944 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
15945 					new_ilm->ilm_notify_driver = B_TRUE;
15946 			}
15947 
15948 		}
15949 
15950 bottom:
15951 		/*
15952 		 * set ilm_send_multicast_reqs so that we inform the
15953 		 * driver about the multicast group.
15954 		 */
15955 		new_ilm->ilm_is_new = B_TRUE;
15956 		/*
15957 		 * Revert multicast filter state to (EXCLUDE, NULL).
15958 		 * new_ilm->ilm_join_mld should already be set if needed.
15959 		 */
15960 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
15961 		CLEAR_SLIST(new_ilm->ilm_filter);
15962 		/*
15963 		 * We allocated/got a new ilm, free the old one.
15964 		 */
15965 		if (new_ilm != ilm) {
15966 			if (from_ill->ill_ilm_walker_cnt == 0) {
15967 				*ilmp = ilm->ilm_next;
15968 				ilm->ilm_next = NULL;
15969 				FREE_SLIST(ilm->ilm_filter);
15970 				FREE_SLIST(ilm->ilm_pendsrcs);
15971 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
15972 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
15973 				mi_free((char *)ilm);
15974 			} else {
15975 				ilm->ilm_flags |= ILM_DELETED;
15976 				from_ill->ill_ilm_cleanup_reqd = 1;
15977 				ilmp = &ilm->ilm_next;
15978 			}
15979 		}
15980 	}
15981 }
15982 
15983 /*
15984  * Move all the multicast memberships to to_ill. Called when
15985  * an ipif moves from "from_ill" to "to_ill". This function is slightly
15986  * different from IPv6 counterpart as multicast memberships are associated
15987  * with ills in IPv6. This function is called after every ipif is moved
15988  * unlike IPv6, where it is moved only once.
15989  */
15990 static void
15991 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
15992 {
15993 	ilm_t	*ilm;
15994 	ilm_t	*ilm_next;
15995 	ilm_t	*new_ilm;
15996 	ilm_t	**ilmp;
15997 
15998 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
15999 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16000 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
16001 
16002 	ilmp = &from_ill->ill_ilm;
16003 	for (ilm = from_ill->ill_ilm; ilm != NULL &&
16004 	    !(ilm->ilm_flags & ILM_DELETED); ilm = ilm_next) {
16005 		ilm_next = ilm->ilm_next;
16006 		ASSERT(ilm->ilm_ipif != NULL);
16007 
16008 		if (ilm->ilm_ipif != ipif) {
16009 			ilmp = &ilm->ilm_next;
16010 			continue;
16011 		}
16012 
16013 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
16014 		    htonl(INADDR_ALLHOSTS_GROUP)) {
16015 			/*
16016 			 * We joined this in ipif_multicast_up
16017 			 * and we never did an ipif_multicast_down
16018 			 * for IPv4. If nobody else from the userland
16019 			 * has reference, we free the ilm, and later
16020 			 * when this ipif comes up on the new ill,
16021 			 * we will join this again.
16022 			 */
16023 			if (--ilm->ilm_refcnt == 0)
16024 				goto delete_ilm;
16025 
16026 			new_ilm = ilm_lookup_ipif(ipif,
16027 			    V4_PART_OF_V6(ilm->ilm_v6addr));
16028 			if (new_ilm != NULL) {
16029 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16030 				/*
16031 				 * We still need to deal with the from_ill.
16032 				 */
16033 				new_ilm->ilm_is_new = B_TRUE;
16034 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16035 				CLEAR_SLIST(new_ilm->ilm_filter);
16036 				goto delete_ilm;
16037 			}
16038 			/*
16039 			 * If we could not find one e.g. ipif is
16040 			 * still down on to_ill, we add this ilm
16041 			 * on ill_new to preserve the reference
16042 			 * count.
16043 			 */
16044 		}
16045 		/*
16046 		 * When ipifs move, ilms always move with it
16047 		 * to the NEW ill. Thus we should never be
16048 		 * able to find ilm till we really move it here.
16049 		 */
16050 		ASSERT(ilm_lookup_ipif(ipif,
16051 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
16052 
16053 		if (from_ill->ill_ilm_walker_cnt != 0) {
16054 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
16055 			if (new_ilm == NULL) {
16056 				char buf[INET6_ADDRSTRLEN];
16057 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
16058 				    " multicast address %s : "
16059 				    "from %s to"
16060 				    " %s failed : ENOMEM \n",
16061 				    inet_ntop(AF_INET,
16062 				    &ilm->ilm_v6addr, buf,
16063 				    sizeof (buf)),
16064 				    from_ill->ill_name,
16065 				    to_ill->ill_name));
16066 
16067 				ilmp = &ilm->ilm_next;
16068 				continue;
16069 			}
16070 			*new_ilm = *ilm;
16071 			/* We don't want new_ilm linked to ilm's filter list */
16072 			new_ilm->ilm_filter = NULL;
16073 		} else {
16074 			/* Remove from the list */
16075 			*ilmp = ilm->ilm_next;
16076 			new_ilm = ilm;
16077 		}
16078 
16079 		/*
16080 		 * If we have never joined this group on the to_ill
16081 		 * make sure we tell the driver.
16082 		 */
16083 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
16084 		    ALL_ZONES) == NULL)
16085 			new_ilm->ilm_notify_driver = B_TRUE;
16086 
16087 		/* Add to the to_ill's list */
16088 		new_ilm->ilm_next = to_ill->ill_ilm;
16089 		to_ill->ill_ilm = new_ilm;
16090 		new_ilm->ilm_is_new = B_TRUE;
16091 
16092 		/*
16093 		 * Revert multicast filter state to (EXCLUDE, NULL)
16094 		 */
16095 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16096 		CLEAR_SLIST(new_ilm->ilm_filter);
16097 
16098 		/*
16099 		 * Delete only if we have allocated a new ilm.
16100 		 */
16101 		if (new_ilm != ilm) {
16102 delete_ilm:
16103 			if (from_ill->ill_ilm_walker_cnt == 0) {
16104 				/* Remove from the list */
16105 				*ilmp = ilm->ilm_next;
16106 				ilm->ilm_next = NULL;
16107 				FREE_SLIST(ilm->ilm_filter);
16108 				FREE_SLIST(ilm->ilm_pendsrcs);
16109 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
16110 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
16111 				mi_free((char *)ilm);
16112 			} else {
16113 				ilm->ilm_flags |= ILM_DELETED;
16114 				from_ill->ill_ilm_cleanup_reqd = 1;
16115 				ilmp = &ilm->ilm_next;
16116 			}
16117 		}
16118 	}
16119 }
16120 
16121 static uint_t
16122 ipif_get_id(ill_t *ill, uint_t id)
16123 {
16124 	uint_t	unit;
16125 	ipif_t	*tipif;
16126 	boolean_t found = B_FALSE;
16127 
16128 	/*
16129 	 * During failback, we want to go back to the same id
16130 	 * instead of the smallest id so that the original
16131 	 * configuration is maintained. id is non-zero in that
16132 	 * case.
16133 	 */
16134 	if (id != 0) {
16135 		/*
16136 		 * While failing back, if we still have an ipif with
16137 		 * MAX_ADDRS_PER_IF, it means this will be replaced
16138 		 * as soon as we return from this function. It was
16139 		 * to set to MAX_ADDRS_PER_IF by the caller so that
16140 		 * we can choose the smallest id. Thus we return zero
16141 		 * in that case ignoring the hint.
16142 		 */
16143 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
16144 			return (0);
16145 		for (tipif = ill->ill_ipif; tipif != NULL;
16146 		    tipif = tipif->ipif_next) {
16147 			if (tipif->ipif_id == id) {
16148 				found = B_TRUE;
16149 				break;
16150 			}
16151 		}
16152 		/*
16153 		 * If somebody already plumbed another logical
16154 		 * with the same id, we won't be able to find it.
16155 		 */
16156 		if (!found)
16157 			return (id);
16158 	}
16159 	for (unit = 0; unit <= ip_addrs_per_if; unit++) {
16160 		found = B_FALSE;
16161 		for (tipif = ill->ill_ipif; tipif != NULL;
16162 		    tipif = tipif->ipif_next) {
16163 			if (tipif->ipif_id == unit) {
16164 				found = B_TRUE;
16165 				break;
16166 			}
16167 		}
16168 		if (!found)
16169 			break;
16170 	}
16171 	return (unit);
16172 }
16173 
16174 /* ARGSUSED */
16175 static int
16176 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
16177     ipif_t **rep_ipif_ptr)
16178 {
16179 	ill_t	*from_ill;
16180 	ipif_t	*rep_ipif;
16181 	ipif_t	**ipifp;
16182 	uint_t	unit;
16183 	int err = 0;
16184 	ipif_t	*to_ipif;
16185 	struct iocblk	*iocp;
16186 	boolean_t failback_cmd;
16187 	boolean_t remove_ipif;
16188 	int	rc;
16189 
16190 	ASSERT(IAM_WRITER_ILL(to_ill));
16191 	ASSERT(IAM_WRITER_IPIF(ipif));
16192 
16193 	iocp = (struct iocblk *)mp->b_rptr;
16194 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
16195 	remove_ipif = B_FALSE;
16196 
16197 	from_ill = ipif->ipif_ill;
16198 
16199 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16200 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16201 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
16202 
16203 	/*
16204 	 * Don't move LINK LOCAL addresses as they are tied to
16205 	 * physical interface.
16206 	 */
16207 	if (from_ill->ill_isv6 &&
16208 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
16209 		ipif->ipif_was_up = B_FALSE;
16210 		IPIF_UNMARK_MOVING(ipif);
16211 		return (0);
16212 	}
16213 
16214 	/*
16215 	 * We set the ipif_id to maximum so that the search for
16216 	 * ipif_id will pick the lowest number i.e 0 in the
16217 	 * following 2 cases :
16218 	 *
16219 	 * 1) We have a replacement ipif at the head of to_ill.
16220 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
16221 	 *    on to_ill and hence the MOVE might fail. We want to
16222 	 *    remove it only if we could move the ipif. Thus, by
16223 	 *    setting it to the MAX value, we make the search in
16224 	 *    ipif_get_id return the zeroth id.
16225 	 *
16226 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
16227 	 *    we might just have a zero address plumbed on the ipif
16228 	 *    with zero id in the case of IPv4. We remove that while
16229 	 *    doing the failback. We want to remove it only if we
16230 	 *    could move the ipif. Thus, by setting it to the MAX
16231 	 *    value, we make the search in ipif_get_id return the
16232 	 *    zeroth id.
16233 	 *
16234 	 * Both (1) and (2) are done only when when we are moving
16235 	 * an ipif (either due to failover/failback) which originally
16236 	 * belonged to this interface i.e the ipif_orig_ifindex is
16237 	 * the same as to_ill's ifindex. This is needed so that
16238 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
16239 	 * from B -> A (B is being removed from the group) and
16240 	 * FAILBACK from A -> B restores the original configuration.
16241 	 * Without the check for orig_ifindex, the second FAILOVER
16242 	 * could make the ipif belonging to B replace the A's zeroth
16243 	 * ipif and the subsequent failback re-creating the replacement
16244 	 * ipif again.
16245 	 *
16246 	 * NOTE : We created the replacement ipif when we did a
16247 	 * FAILOVER (See below). We could check for FAILBACK and
16248 	 * then look for replacement ipif to be removed. But we don't
16249 	 * want to do that because we wan't to allow the possibility
16250 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
16251 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
16252 	 * from B -> A.
16253 	 */
16254 	to_ipif = to_ill->ill_ipif;
16255 	if ((to_ill->ill_phyint->phyint_ifindex ==
16256 	    ipif->ipif_orig_ifindex) &&
16257 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
16258 		ASSERT(to_ipif->ipif_id == 0);
16259 		remove_ipif = B_TRUE;
16260 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
16261 	}
16262 	/*
16263 	 * Find the lowest logical unit number on the to_ill.
16264 	 * If we are failing back, try to get the original id
16265 	 * rather than the lowest one so that the original
16266 	 * configuration is maintained.
16267 	 *
16268 	 * XXX need a better scheme for this.
16269 	 */
16270 	if (failback_cmd) {
16271 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
16272 	} else {
16273 		unit = ipif_get_id(to_ill, 0);
16274 	}
16275 
16276 	/* Reset back to zero in case we fail below */
16277 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
16278 		to_ipif->ipif_id = 0;
16279 
16280 	if (unit == ip_addrs_per_if) {
16281 		ipif->ipif_was_up = B_FALSE;
16282 		IPIF_UNMARK_MOVING(ipif);
16283 		return (EINVAL);
16284 	}
16285 
16286 	/*
16287 	 * ipif is ready to move from "from_ill" to "to_ill".
16288 	 *
16289 	 * 1) If we are moving ipif with id zero, create a
16290 	 *    replacement ipif for this ipif on from_ill. If this fails
16291 	 *    fail the MOVE operation.
16292 	 *
16293 	 * 2) Remove the replacement ipif on to_ill if any.
16294 	 *    We could remove the replacement ipif when we are moving
16295 	 *    the ipif with id zero. But what if somebody already
16296 	 *    unplumbed it ? Thus we always remove it if it is present.
16297 	 *    We want to do it only if we are sure we are going to
16298 	 *    move the ipif to to_ill which is why there are no
16299 	 *    returns due to error till ipif is linked to to_ill.
16300 	 *    Note that the first ipif that we failback will always
16301 	 *    be zero if it is present.
16302 	 */
16303 	if (ipif->ipif_id == 0) {
16304 		ipaddr_t inaddr_any = INADDR_ANY;
16305 
16306 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
16307 		if (rep_ipif == NULL) {
16308 			ipif->ipif_was_up = B_FALSE;
16309 			IPIF_UNMARK_MOVING(ipif);
16310 			return (ENOMEM);
16311 		}
16312 		*rep_ipif = ipif_zero;
16313 		/*
16314 		 * Before we put the ipif on the list, store the addresses
16315 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
16316 		 * assumes so. This logic is not any different from what
16317 		 * ipif_allocate does.
16318 		 */
16319 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16320 		    &rep_ipif->ipif_v6lcl_addr);
16321 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16322 		    &rep_ipif->ipif_v6src_addr);
16323 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16324 		    &rep_ipif->ipif_v6subnet);
16325 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16326 		    &rep_ipif->ipif_v6net_mask);
16327 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16328 		    &rep_ipif->ipif_v6brd_addr);
16329 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16330 		    &rep_ipif->ipif_v6pp_dst_addr);
16331 		/*
16332 		 * We mark IPIF_NOFAILOVER so that this can never
16333 		 * move.
16334 		 */
16335 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
16336 		rep_ipif->ipif_flags &= ~IPIF_UP;
16337 		rep_ipif->ipif_replace_zero = B_TRUE;
16338 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
16339 		    MUTEX_DEFAULT, NULL);
16340 		rep_ipif->ipif_id = 0;
16341 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
16342 		rep_ipif->ipif_ill = from_ill;
16343 		rep_ipif->ipif_orig_ifindex =
16344 		    from_ill->ill_phyint->phyint_ifindex;
16345 		/* Insert at head */
16346 		rep_ipif->ipif_next = from_ill->ill_ipif;
16347 		from_ill->ill_ipif = rep_ipif;
16348 		/*
16349 		 * We don't really care to let apps know about
16350 		 * this interface.
16351 		 */
16352 	}
16353 
16354 	if (remove_ipif) {
16355 		/*
16356 		 * We set to a max value above for this case to get
16357 		 * id zero. ASSERT that we did get one.
16358 		 */
16359 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
16360 		rep_ipif = to_ipif;
16361 		to_ill->ill_ipif = rep_ipif->ipif_next;
16362 		rep_ipif->ipif_next = NULL;
16363 		/*
16364 		 * If some apps scanned and find this interface,
16365 		 * it is time to let them know, so that they can
16366 		 * delete it.
16367 		 */
16368 
16369 		*rep_ipif_ptr = rep_ipif;
16370 	}
16371 
16372 	/* Get it out of the ILL interface list. */
16373 	ipifp = &ipif->ipif_ill->ill_ipif;
16374 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
16375 		if (*ipifp == ipif) {
16376 			*ipifp = ipif->ipif_next;
16377 			break;
16378 		}
16379 	}
16380 
16381 	/* Assign the new ill */
16382 	ipif->ipif_ill = to_ill;
16383 	ipif->ipif_id = unit;
16384 	/* id has already been checked */
16385 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
16386 	ASSERT(rc == 0);
16387 	/* Let SCTP update its list */
16388 	sctp_move_ipif(ipif, from_ill, to_ill);
16389 	/*
16390 	 * Handle the failover and failback of ipif_t between
16391 	 * ill_t that have differing maximum mtu values.
16392 	 */
16393 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
16394 		if (ipif->ipif_saved_mtu == 0) {
16395 			/*
16396 			 * As this ipif_t is moving to an ill_t
16397 			 * that has a lower ill_max_mtu, its
16398 			 * ipif_mtu needs to be saved so it can
16399 			 * be restored during failback or during
16400 			 * failover to an ill_t which has a
16401 			 * higher ill_max_mtu.
16402 			 */
16403 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
16404 			ipif->ipif_mtu = to_ill->ill_max_mtu;
16405 		} else {
16406 			/*
16407 			 * The ipif_t is, once again, moving to
16408 			 * an ill_t that has a lower maximum mtu
16409 			 * value.
16410 			 */
16411 			ipif->ipif_mtu = to_ill->ill_max_mtu;
16412 		}
16413 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
16414 	    ipif->ipif_saved_mtu != 0) {
16415 		/*
16416 		 * The mtu of this ipif_t had to be reduced
16417 		 * during an earlier failover; this is an
16418 		 * opportunity for it to be increased (either as
16419 		 * part of another failover or a failback).
16420 		 */
16421 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
16422 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
16423 			ipif->ipif_saved_mtu = 0;
16424 		} else {
16425 			ipif->ipif_mtu = to_ill->ill_max_mtu;
16426 		}
16427 	}
16428 
16429 	/*
16430 	 * We preserve all the other fields of the ipif including
16431 	 * ipif_saved_ire_mp. The routes that are saved here will
16432 	 * be recreated on the new interface and back on the old
16433 	 * interface when we move back.
16434 	 */
16435 	ASSERT(ipif->ipif_arp_del_mp == NULL);
16436 
16437 	return (err);
16438 }
16439 
16440 static int
16441 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
16442     int ifindex, ipif_t **rep_ipif_ptr)
16443 {
16444 	ipif_t *mipif;
16445 	ipif_t *ipif_next;
16446 	int err;
16447 
16448 	/*
16449 	 * We don't really try to MOVE back things if some of the
16450 	 * operations fail. The daemon will take care of moving again
16451 	 * later on.
16452 	 */
16453 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
16454 		ipif_next = mipif->ipif_next;
16455 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
16456 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
16457 
16458 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
16459 
16460 			/*
16461 			 * When the MOVE fails, it is the job of the
16462 			 * application to take care of this properly
16463 			 * i.e try again if it is ENOMEM.
16464 			 */
16465 			if (mipif->ipif_ill != from_ill) {
16466 				/*
16467 				 * ipif has moved.
16468 				 *
16469 				 * Move the multicast memberships associated
16470 				 * with this ipif to the new ill. For IPv6, we
16471 				 * do it once after all the ipifs are moved
16472 				 * (in ill_move) as they are not associated
16473 				 * with ipifs.
16474 				 *
16475 				 * We need to move the ilms as the ipif has
16476 				 * already been moved to a new ill even
16477 				 * in the case of errors. Neither
16478 				 * ilm_free(ipif) will find the ilm
16479 				 * when somebody unplumbs this ipif nor
16480 				 * ilm_delete(ilm) will be able to find the
16481 				 * ilm, if we don't move now.
16482 				 */
16483 				if (!from_ill->ill_isv6)
16484 					ilm_move_v4(from_ill, to_ill, mipif);
16485 			}
16486 
16487 			if (err != 0)
16488 				return (err);
16489 		}
16490 	}
16491 	return (0);
16492 }
16493 
16494 static int
16495 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
16496 {
16497 	int ifindex;
16498 	int err;
16499 	struct iocblk	*iocp;
16500 	ipif_t	*ipif;
16501 	ipif_t *rep_ipif_ptr = NULL;
16502 	ipif_t	*from_ipif = NULL;
16503 	boolean_t check_rep_if = B_FALSE;
16504 
16505 	iocp = (struct iocblk *)mp->b_rptr;
16506 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
16507 		/*
16508 		 * Move everything pointing at from_ill to to_ill.
16509 		 * We acheive this by passing in 0 as ifindex.
16510 		 */
16511 		ifindex = 0;
16512 	} else {
16513 		/*
16514 		 * Move everything pointing at from_ill whose original
16515 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
16516 		 * We acheive this by passing in ifindex rather than 0.
16517 		 * Multicast vifs, ilgs move implicitly because ipifs move.
16518 		 */
16519 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
16520 		ifindex = to_ill->ill_phyint->phyint_ifindex;
16521 	}
16522 
16523 	/*
16524 	 * Determine if there is at least one ipif that would move from
16525 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
16526 	 * ipif (if it exists) on the to_ill would be consumed as a result of
16527 	 * the move, in which case we need to quiesce the replacement ipif also.
16528 	 */
16529 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
16530 	    from_ipif = from_ipif->ipif_next) {
16531 		if (((ifindex == 0) ||
16532 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
16533 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
16534 			check_rep_if = B_TRUE;
16535 			break;
16536 		}
16537 	}
16538 
16539 
16540 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
16541 
16542 	GRAB_ILL_LOCKS(from_ill, to_ill);
16543 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
16544 		(void) ipsq_pending_mp_add(NULL, ipif, q,
16545 		    mp, ILL_MOVE_OK);
16546 		RELEASE_ILL_LOCKS(from_ill, to_ill);
16547 		return (EINPROGRESS);
16548 	}
16549 
16550 	/* Check if the replacement ipif is quiescent to delete */
16551 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
16552 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
16553 		to_ill->ill_ipif->ipif_state_flags |=
16554 		    IPIF_MOVING | IPIF_CHANGING;
16555 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
16556 			(void) ipsq_pending_mp_add(NULL, ipif, q,
16557 			    mp, ILL_MOVE_OK);
16558 			RELEASE_ILL_LOCKS(from_ill, to_ill);
16559 			return (EINPROGRESS);
16560 		}
16561 	}
16562 	RELEASE_ILL_LOCKS(from_ill, to_ill);
16563 
16564 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
16565 	rw_enter(&ill_g_lock, RW_WRITER);
16566 	GRAB_ILL_LOCKS(from_ill, to_ill);
16567 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
16568 
16569 	/* ilm_move is done inside ipif_move for IPv4 */
16570 	if (err == 0 && from_ill->ill_isv6)
16571 		ilm_move_v6(from_ill, to_ill, ifindex);
16572 
16573 	RELEASE_ILL_LOCKS(from_ill, to_ill);
16574 	rw_exit(&ill_g_lock);
16575 
16576 	/*
16577 	 * send rts messages and multicast messages.
16578 	 */
16579 	if (rep_ipif_ptr != NULL) {
16580 		ip_rts_ifmsg(rep_ipif_ptr);
16581 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
16582 		IPIF_TRACE_CLEANUP(rep_ipif_ptr);
16583 		mi_free(rep_ipif_ptr);
16584 	}
16585 
16586 	ilm_send_multicast_reqs(from_ill, to_ill);
16587 
16588 	conn_move_ill(from_ill, to_ill, ifindex);
16589 
16590 	return (err);
16591 }
16592 
16593 /*
16594  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
16595  * Also checks for the validity of the arguments.
16596  * Note: We are already exclusive inside the from group.
16597  * It is upto the caller to release refcnt on the to_ill's.
16598  */
16599 static int
16600 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
16601     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
16602 {
16603 	int dst_index;
16604 	ipif_t *ipif_v4, *ipif_v6;
16605 	struct lifreq *lifr;
16606 	mblk_t *mp1;
16607 	boolean_t exists;
16608 	sin_t	*sin;
16609 	int	err = 0;
16610 
16611 	if ((mp1 = mp->b_cont) == NULL)
16612 		return (EPROTO);
16613 
16614 	if ((mp1 = mp1->b_cont) == NULL)
16615 		return (EPROTO);
16616 
16617 	lifr = (struct lifreq *)mp1->b_rptr;
16618 	sin = (sin_t *)&lifr->lifr_addr;
16619 
16620 	/*
16621 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
16622 	 * specific operations.
16623 	 */
16624 	if (sin->sin_family != AF_UNSPEC)
16625 		return (EINVAL);
16626 
16627 	/*
16628 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
16629 	 * NULLs for the last 4 args and we know the lookup won't fail
16630 	 * with EINPROGRESS.
16631 	 */
16632 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
16633 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
16634 	    ALL_ZONES, NULL, NULL, NULL, NULL);
16635 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
16636 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
16637 	    ALL_ZONES, NULL, NULL, NULL, NULL);
16638 
16639 	if (ipif_v4 == NULL && ipif_v6 == NULL)
16640 		return (ENXIO);
16641 
16642 	if (ipif_v4 != NULL) {
16643 		ASSERT(ipif_v4->ipif_refcnt != 0);
16644 		if (ipif_v4->ipif_id != 0) {
16645 			err = EINVAL;
16646 			goto done;
16647 		}
16648 
16649 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
16650 		*ill_from_v4 = ipif_v4->ipif_ill;
16651 	}
16652 
16653 	if (ipif_v6 != NULL) {
16654 		ASSERT(ipif_v6->ipif_refcnt != 0);
16655 		if (ipif_v6->ipif_id != 0) {
16656 			err = EINVAL;
16657 			goto done;
16658 		}
16659 
16660 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
16661 		*ill_from_v6 = ipif_v6->ipif_ill;
16662 	}
16663 
16664 	err = 0;
16665 	dst_index = lifr->lifr_movetoindex;
16666 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
16667 	    q, mp, ip_process_ioctl, &err);
16668 	if (err != 0) {
16669 		/*
16670 		 * There could be only v6.
16671 		 */
16672 		if (err != ENXIO)
16673 			goto done;
16674 		err = 0;
16675 	}
16676 
16677 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
16678 	    q, mp, ip_process_ioctl, &err);
16679 	if (err != 0) {
16680 		if (err != ENXIO)
16681 			goto done;
16682 		if (*ill_to_v4 == NULL) {
16683 			err = ENXIO;
16684 			goto done;
16685 		}
16686 		err = 0;
16687 	}
16688 
16689 	/*
16690 	 * If we have something to MOVE i.e "from" not NULL,
16691 	 * "to" should be non-NULL.
16692 	 */
16693 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
16694 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
16695 		err = EINVAL;
16696 	}
16697 
16698 done:
16699 	if (ipif_v4 != NULL)
16700 		ipif_refrele(ipif_v4);
16701 	if (ipif_v6 != NULL)
16702 		ipif_refrele(ipif_v6);
16703 	return (err);
16704 }
16705 
16706 /*
16707  * FAILOVER and FAILBACK are modelled as MOVE operations.
16708  *
16709  * We don't check whether the MOVE is within the same group or
16710  * not, because this ioctl can be used as a generic mechanism
16711  * to failover from interface A to B, though things will function
16712  * only if they are really part of the same group. Moreover,
16713  * all ipifs may be down and hence temporarily out of the group.
16714  *
16715  * ipif's that need to be moved are first brought down; V4 ipifs are brought
16716  * down first and then V6.  For each we wait for the ipif's to become quiescent.
16717  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
16718  * have been deleted and there are no active references. Once quiescent the
16719  * ipif's are moved and brought up on the new ill.
16720  *
16721  * Normally the source ill and destination ill belong to the same IPMP group
16722  * and hence the same ipsq_t. In the event they don't belong to the same
16723  * same group the two ipsq's are first merged into one ipsq - that of the
16724  * to_ill. The multicast memberships on the source and destination ill cannot
16725  * change during the move operation since multicast joins/leaves also have to
16726  * execute on the same ipsq and are hence serialized.
16727  */
16728 /* ARGSUSED */
16729 int
16730 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16731     ip_ioctl_cmd_t *ipip, void *ifreq)
16732 {
16733 	ill_t *ill_to_v4 = NULL;
16734 	ill_t *ill_to_v6 = NULL;
16735 	ill_t *ill_from_v4 = NULL;
16736 	ill_t *ill_from_v6 = NULL;
16737 	int err = 0;
16738 
16739 	/*
16740 	 * setup from and to ill's, we can get EINPROGRESS only for
16741 	 * to_ill's.
16742 	 */
16743 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
16744 	    &ill_to_v4, &ill_to_v6);
16745 
16746 	if (err != 0) {
16747 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
16748 		goto done;
16749 	}
16750 
16751 	/*
16752 	 * nothing to do.
16753 	 */
16754 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
16755 		goto done;
16756 	}
16757 
16758 	/*
16759 	 * nothing to do.
16760 	 */
16761 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
16762 		goto done;
16763 	}
16764 
16765 	/*
16766 	 * Mark the ill as changing.
16767 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
16768 	 * in ill_up_ipifs in case of error they are cleared below.
16769 	 */
16770 
16771 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
16772 	if (ill_from_v4 != NULL)
16773 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
16774 	if (ill_from_v6 != NULL)
16775 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
16776 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
16777 
16778 	/*
16779 	 * Make sure that both src and dst are
16780 	 * in the same syncq group. If not make it happen.
16781 	 * We are not holding any locks because we are the writer
16782 	 * on the from_ipsq and we will hold locks in ill_merge_groups
16783 	 * to protect to_ipsq against changing.
16784 	 */
16785 	if (ill_from_v4 != NULL) {
16786 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
16787 		    ill_to_v4->ill_phyint->phyint_ipsq) {
16788 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
16789 			    NULL, mp, q);
16790 			goto err_ret;
16791 
16792 		}
16793 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
16794 	} else {
16795 
16796 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
16797 		    ill_to_v6->ill_phyint->phyint_ipsq) {
16798 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
16799 			    NULL, mp, q);
16800 			goto err_ret;
16801 
16802 		}
16803 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
16804 	}
16805 
16806 	/*
16807 	 * Now that the ipsq's have been merged and we are the writer
16808 	 * lets mark to_ill as changing as well.
16809 	 */
16810 
16811 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
16812 	if (ill_to_v4 != NULL)
16813 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
16814 	if (ill_to_v6 != NULL)
16815 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
16816 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
16817 
16818 	/*
16819 	 * Its ok for us to proceed with the move even if
16820 	 * ill_pending_mp is non null on one of the from ill's as the reply
16821 	 * should not be looking at the ipif, it should only care about the
16822 	 * ill itself.
16823 	 */
16824 
16825 	/*
16826 	 * lets move ipv4 first.
16827 	 */
16828 	if (ill_from_v4 != NULL) {
16829 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
16830 		ill_from_v4->ill_move_in_progress = B_TRUE;
16831 		ill_to_v4->ill_move_in_progress = B_TRUE;
16832 		ill_to_v4->ill_move_peer = ill_from_v4;
16833 		ill_from_v4->ill_move_peer = ill_to_v4;
16834 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
16835 	}
16836 
16837 	/*
16838 	 * Now lets move ipv6.
16839 	 */
16840 	if (err == 0 && ill_from_v6 != NULL) {
16841 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
16842 		ill_from_v6->ill_move_in_progress = B_TRUE;
16843 		ill_to_v6->ill_move_in_progress = B_TRUE;
16844 		ill_to_v6->ill_move_peer = ill_from_v6;
16845 		ill_from_v6->ill_move_peer = ill_to_v6;
16846 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
16847 	}
16848 
16849 err_ret:
16850 	if (err == 0)
16851 		goto no_err;
16852 	/*
16853 	 * EINPROGRESS means we are waiting for the ipif's that need to be
16854 	 * moved to become quiescent.
16855 	 */
16856 	if (err == EINPROGRESS) {
16857 		goto done;
16858 	}
16859 
16860 	/*
16861 	 * if err is set ill_up_ipifs will not be called
16862 	 * lets clear the flags.
16863 	 */
16864 
16865 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
16866 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
16867 	/*
16868 	 * Some of the clearing may be redundant. But it is simple
16869 	 * not making any extra checks.
16870 	 */
16871 	if (ill_from_v6 != NULL) {
16872 		ill_from_v6->ill_move_in_progress = B_FALSE;
16873 		ill_from_v6->ill_move_peer = NULL;
16874 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
16875 	}
16876 	if (ill_from_v4 != NULL) {
16877 		ill_from_v4->ill_move_in_progress = B_FALSE;
16878 		ill_from_v4->ill_move_peer = NULL;
16879 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
16880 	}
16881 	if (ill_to_v6 != NULL) {
16882 		ill_to_v6->ill_move_in_progress = B_FALSE;
16883 		ill_to_v6->ill_move_peer = NULL;
16884 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
16885 	}
16886 	if (ill_to_v4 != NULL) {
16887 		ill_to_v4->ill_move_in_progress = B_FALSE;
16888 		ill_to_v4->ill_move_peer = NULL;
16889 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
16890 	}
16891 	/*
16892 	 * Check for setting INACTIVE, if STANDBY is set. Do this always
16893 	 * to maintain proper state i.e even in the case of errors.
16894 	 * As phyint_standby_inactive looks at both v4 and v6 interfaces,
16895 	 * we need not call on both v4 and v6 interfaces.
16896 	 */
16897 	if (ill_from_v4 != NULL) {
16898 		if (ill_from_v4->ill_phyint->phyint_flags & PHYI_STANDBY) {
16899 			phyint_standby_inactive(ill_from_v4->ill_phyint);
16900 		}
16901 	} else if (ill_from_v6 != NULL) {
16902 		if (ill_from_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
16903 			phyint_standby_inactive(ill_from_v6->ill_phyint);
16904 		}
16905 	}
16906 
16907 	if (ill_to_v4 != NULL) {
16908 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_STANDBY) {
16909 			phyint_standby_inactive(ill_to_v4->ill_phyint);
16910 		}
16911 
16912 	} else if (ill_to_v6 != NULL) {
16913 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
16914 			phyint_standby_inactive(ill_to_v6->ill_phyint);
16915 		}
16916 	}
16917 
16918 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
16919 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
16920 
16921 no_err:
16922 	/*
16923 	 * lets bring the interfaces up on the to_ill.
16924 	 */
16925 	if (err == 0) {
16926 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
16927 		    q, mp);
16928 	}
16929 done:
16930 
16931 	if (ill_to_v4 != NULL) {
16932 		ill_refrele(ill_to_v4);
16933 	}
16934 	if (ill_to_v6 != NULL) {
16935 		ill_refrele(ill_to_v6);
16936 	}
16937 
16938 	return (err);
16939 }
16940 
16941 static void
16942 ill_dl_down(ill_t *ill)
16943 {
16944 	/*
16945 	 * The ill is down; unbind but stay attached since we're still
16946 	 * associated with a PPA.
16947 	 */
16948 	mblk_t	*mp = ill->ill_unbind_mp;
16949 
16950 	ill->ill_unbind_mp = NULL;
16951 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
16952 	if (mp != NULL) {
16953 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
16954 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
16955 		    ill->ill_name));
16956 		ill_dlpi_send(ill, mp);
16957 	}
16958 
16959 	/*
16960 	 * Toss all of our multicast memberships.  We could keep them, but
16961 	 * then we'd have to do bookkeeping of any joins and leaves performed
16962 	 * by the application while the the interface is down (we can't just
16963 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
16964 	 * on a downed interface).
16965 	 */
16966 	ill_leave_multicast(ill);
16967 
16968 	mutex_enter(&ill->ill_lock);
16969 	ill->ill_dl_up = 0;
16970 	mutex_exit(&ill->ill_lock);
16971 }
16972 
16973 void
16974 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
16975 {
16976 	union DL_primitives *dlp;
16977 	t_uscalar_t prim;
16978 
16979 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
16980 
16981 	dlp = (union DL_primitives *)mp->b_rptr;
16982 	prim = dlp->dl_primitive;
16983 
16984 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
16985 		dlpi_prim_str(prim), prim, ill->ill_name));
16986 
16987 	switch (prim) {
16988 	case DL_PHYS_ADDR_REQ:
16989 	{
16990 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
16991 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
16992 		break;
16993 	}
16994 	case DL_BIND_REQ:
16995 		mutex_enter(&ill->ill_lock);
16996 		ill->ill_state_flags &= ~ILL_DL_UNBIND_DONE;
16997 		mutex_exit(&ill->ill_lock);
16998 		break;
16999 	}
17000 
17001 	ill->ill_dlpi_pending = prim;
17002 
17003 	/*
17004 	 * Some drivers send M_FLUSH up to IP as part of unbind
17005 	 * request.  When this M_FLUSH is sent back to the driver,
17006 	 * this can go after we send the detach request if the
17007 	 * M_FLUSH ends up in IP's syncq. To avoid that, we reply
17008 	 * to the M_FLUSH in ip_rput and locally generate another
17009 	 * M_FLUSH for the correctness.  This will get freed in
17010 	 * ip_wput_nondata.
17011 	 */
17012 	if (prim == DL_UNBIND_REQ)
17013 		(void) putnextctl1(ill->ill_rq, M_FLUSH, FLUSHRW);
17014 
17015 	putnext(ill->ill_wq, mp);
17016 }
17017 
17018 /*
17019  * Send a DLPI control message to the driver but make sure there
17020  * is only one outstanding message. Uses ill_dlpi_pending to tell
17021  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
17022  * when an ACK or a NAK is received to process the next queued message.
17023  *
17024  * We don't protect ill_dlpi_pending with any lock. This is okay as
17025  * every place where its accessed, ip is exclusive while accessing
17026  * ill_dlpi_pending except when this function is called from ill_init()
17027  */
17028 void
17029 ill_dlpi_send(ill_t *ill, mblk_t *mp)
17030 {
17031 	mblk_t **mpp;
17032 
17033 	ASSERT(IAM_WRITER_ILL(ill));
17034 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
17035 
17036 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
17037 		/* Must queue message. Tail insertion */
17038 		mpp = &ill->ill_dlpi_deferred;
17039 		while (*mpp != NULL)
17040 			mpp = &((*mpp)->b_next);
17041 
17042 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
17043 		    ill->ill_name));
17044 
17045 		*mpp = mp;
17046 		return;
17047 	}
17048 
17049 	ill_dlpi_dispatch(ill, mp);
17050 }
17051 
17052 /*
17053  * Called when an DLPI control message has been acked or nacked to
17054  * send down the next queued message (if any).
17055  */
17056 void
17057 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
17058 {
17059 	mblk_t *mp;
17060 
17061 	ASSERT(IAM_WRITER_ILL(ill));
17062 
17063 	ASSERT(prim != DL_PRIM_INVAL);
17064 	if (ill->ill_dlpi_pending != prim) {
17065 		if (ill->ill_dlpi_pending == DL_PRIM_INVAL) {
17066 			(void) mi_strlog(ill->ill_rq, 1,
17067 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
17068 			    "ill_dlpi_done: unsolicited ack for %s from %s\n",
17069 			    dlpi_prim_str(prim), ill->ill_name);
17070 		} else {
17071 			(void) mi_strlog(ill->ill_rq, 1,
17072 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
17073 			    "ill_dlpi_done: unexpected ack for %s from %s "
17074 			    "(expecting ack for %s)\n",
17075 			    dlpi_prim_str(prim), ill->ill_name,
17076 			    dlpi_prim_str(ill->ill_dlpi_pending));
17077 		}
17078 		return;
17079 	}
17080 
17081 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
17082 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
17083 
17084 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
17085 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
17086 		return;
17087 	}
17088 
17089 	ill->ill_dlpi_deferred = mp->b_next;
17090 	mp->b_next = NULL;
17091 
17092 	ill_dlpi_dispatch(ill, mp);
17093 }
17094 
17095 void
17096 conn_delete_ire(conn_t *connp, caddr_t arg)
17097 {
17098 	ipif_t	*ipif = (ipif_t *)arg;
17099 	ire_t	*ire;
17100 
17101 	/*
17102 	 * Look at the cached ires on conns which has pointers to ipifs.
17103 	 * We just call ire_refrele which clears up the reference
17104 	 * to ire. Called when a conn closes. Also called from ipif_free
17105 	 * to cleanup indirect references to the stale ipif via the cached ire.
17106 	 */
17107 	mutex_enter(&connp->conn_lock);
17108 	ire = connp->conn_ire_cache;
17109 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
17110 		connp->conn_ire_cache = NULL;
17111 		mutex_exit(&connp->conn_lock);
17112 		IRE_REFRELE_NOTR(ire);
17113 		return;
17114 	}
17115 	mutex_exit(&connp->conn_lock);
17116 
17117 }
17118 
17119 /*
17120  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
17121  * of IREs. Those IREs may have been previously cached in the conn structure.
17122  * This ipcl_walk() walker function releases all references to such IREs based
17123  * on the condemned flag.
17124  */
17125 /* ARGSUSED */
17126 void
17127 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
17128 {
17129 	ire_t	*ire;
17130 
17131 	mutex_enter(&connp->conn_lock);
17132 	ire = connp->conn_ire_cache;
17133 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
17134 		connp->conn_ire_cache = NULL;
17135 		mutex_exit(&connp->conn_lock);
17136 		IRE_REFRELE_NOTR(ire);
17137 		return;
17138 	}
17139 	mutex_exit(&connp->conn_lock);
17140 }
17141 
17142 /*
17143  * Take down a specific interface, but don't lose any information about it.
17144  * Also delete interface from its interface group (ifgrp).
17145  * (Always called as writer.)
17146  * This function goes through the down sequence even if the interface is
17147  * already down. There are 2 reasons.
17148  * a. Currently we permit interface routes that depend on down interfaces
17149  *    to be added. This behaviour itself is questionable. However it appears
17150  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
17151  *    time. We go thru the cleanup in order to remove these routes.
17152  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
17153  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
17154  *    down, but we need to cleanup i.e. do ill_dl_down and
17155  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
17156  *
17157  * IP-MT notes:
17158  *
17159  * Model of reference to interfaces.
17160  *
17161  * The following members in ipif_t track references to the ipif.
17162  *	int     ipif_refcnt;    Active reference count
17163  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
17164  * The following members in ill_t track references to the ill.
17165  *	int             ill_refcnt;     active refcnt
17166  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
17167  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
17168  *
17169  * Reference to an ipif or ill can be obtained in any of the following ways.
17170  *
17171  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
17172  * Pointers to ipif / ill from other data structures viz ire and conn.
17173  * Implicit reference to the ipif / ill by holding a reference to the ire.
17174  *
17175  * The ipif/ill lookup functions return a reference held ipif / ill.
17176  * ipif_refcnt and ill_refcnt track the reference counts respectively.
17177  * This is a purely dynamic reference count associated with threads holding
17178  * references to the ipif / ill. Pointers from other structures do not
17179  * count towards this reference count.
17180  *
17181  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
17182  * ipif/ill. This is incremented whenever a new ire is created referencing the
17183  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
17184  * actually added to the ire hash table. The count is decremented in
17185  * ire_inactive where the ire is destroyed.
17186  *
17187  * nce's reference ill's thru nce_ill and the count of nce's associated with
17188  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
17189  * ndp_add() where the nce is actually added to the table. Similarly it is
17190  * decremented in ndp_inactive where the nce is destroyed.
17191  *
17192  * Flow of ioctls involving interface down/up
17193  *
17194  * The following is the sequence of an attempt to set some critical flags on an
17195  * up interface.
17196  * ip_sioctl_flags
17197  * ipif_down
17198  * wait for ipif to be quiescent
17199  * ipif_down_tail
17200  * ip_sioctl_flags_tail
17201  *
17202  * All set ioctls that involve down/up sequence would have a skeleton similar
17203  * to the above. All the *tail functions are called after the refcounts have
17204  * dropped to the appropriate values.
17205  *
17206  * The mechanism to quiesce an ipif is as follows.
17207  *
17208  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
17209  * on the ipif. Callers either pass a flag requesting wait or the lookup
17210  *  functions will return NULL.
17211  *
17212  * Delete all ires referencing this ipif
17213  *
17214  * Any thread attempting to do an ipif_refhold on an ipif that has been
17215  * obtained thru a cached pointer will first make sure that
17216  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
17217  * increment the refcount.
17218  *
17219  * The above guarantees that the ipif refcount will eventually come down to
17220  * zero and the ipif will quiesce, once all threads that currently hold a
17221  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
17222  * ipif_refcount has dropped to zero and all ire's associated with this ipif
17223  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
17224  * drop to zero.
17225  *
17226  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
17227  *
17228  * Threads trying to lookup an ipif or ill can pass a flag requesting
17229  * wait and restart if the ipif / ill cannot be looked up currently.
17230  * For eg. bind, and route operations (Eg. route add / delete) cannot return
17231  * failure if the ipif is currently undergoing an exclusive operation, and
17232  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
17233  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
17234  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
17235  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
17236  * change while the ill_lock is held. Before dropping the ill_lock we acquire
17237  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
17238  * until we release the ipsq_lock, even though the the ill/ipif state flags
17239  * can change after we drop the ill_lock.
17240  *
17241  * An attempt to send out a packet using an ipif that is currently
17242  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
17243  * operation and restart it later when the exclusive condition on the ipif ends.
17244  * This is an example of not passing the wait flag to the lookup functions. For
17245  * example an attempt to refhold and use conn->conn_multicast_ipif and send
17246  * out a multicast packet on that ipif will fail while the ipif is
17247  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
17248  * currently IPIF_CHANGING will also fail.
17249  */
17250 int
17251 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
17252 {
17253 	ill_t		*ill = ipif->ipif_ill;
17254 	phyint_t	*phyi;
17255 	conn_t		*connp;
17256 	boolean_t	success;
17257 	boolean_t	ipif_was_up = B_FALSE;
17258 
17259 	ASSERT(IAM_WRITER_IPIF(ipif));
17260 
17261 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
17262 
17263 	if (ipif->ipif_flags & IPIF_UP) {
17264 		mutex_enter(&ill->ill_lock);
17265 		ipif->ipif_flags &= ~IPIF_UP;
17266 		ASSERT(ill->ill_ipif_up_count > 0);
17267 		--ill->ill_ipif_up_count;
17268 		mutex_exit(&ill->ill_lock);
17269 		ipif_was_up = B_TRUE;
17270 		/* Update status in SCTP's list */
17271 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
17272 	}
17273 
17274 	/*
17275 	 * Blow away v6 memberships we established in ipif_multicast_up(); the
17276 	 * v4 ones are left alone (as is the ipif_multicast_up flag, so we
17277 	 * know not to rejoin when the interface is brought back up).
17278 	 */
17279 	if (ipif->ipif_isv6)
17280 		ipif_multicast_down(ipif);
17281 	/*
17282 	 * Remove from the mapping for __sin6_src_id. We insert only
17283 	 * when the address is not INADDR_ANY. As IPv4 addresses are
17284 	 * stored as mapped addresses, we need to check for mapped
17285 	 * INADDR_ANY also.
17286 	 */
17287 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
17288 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
17289 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
17290 		int err;
17291 
17292 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
17293 		    ipif->ipif_zoneid);
17294 		if (err != 0) {
17295 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
17296 		}
17297 	}
17298 
17299 	/*
17300 	 * Before we delete the ill from the group (if any), we need
17301 	 * to make sure that we delete all the routes dependent on
17302 	 * this and also any ipifs dependent on this ipif for
17303 	 * source address. We need to do before we delete from
17304 	 * the group because
17305 	 *
17306 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
17307 	 *
17308 	 * 2) ipif_update_other_ipifs needs to walk the whole group
17309 	 *    for re-doing source address selection. Note that
17310 	 *    ipif_select_source[_v6] called from
17311 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
17312 	 *    because we have already marked down here i.e cleared
17313 	 *    IPIF_UP.
17314 	 */
17315 	if (ipif->ipif_isv6)
17316 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES);
17317 	else
17318 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES);
17319 
17320 	/*
17321 	 * Need to add these also to be saved and restored when the
17322 	 * ipif is brought down and up
17323 	 */
17324 	mutex_enter(&ire_mrtun_lock);
17325 	if (ire_mrtun_count != 0) {
17326 		mutex_exit(&ire_mrtun_lock);
17327 		ire_walk_ill_mrtun(0, 0, ipif_down_delete_ire,
17328 		    (char *)ipif, NULL);
17329 	} else {
17330 		mutex_exit(&ire_mrtun_lock);
17331 	}
17332 
17333 	mutex_enter(&ire_srcif_table_lock);
17334 	if (ire_srcif_table_count > 0) {
17335 		mutex_exit(&ire_srcif_table_lock);
17336 		ire_walk_srcif_table_v4(ipif_down_delete_ire, (char *)ipif);
17337 	} else {
17338 		mutex_exit(&ire_srcif_table_lock);
17339 	}
17340 
17341 	/*
17342 	 * Cleaning up the conn_ire_cache or conns must be done only after the
17343 	 * ires have been deleted above. Otherwise a thread could end up
17344 	 * caching an ire in a conn after we have finished the cleanup of the
17345 	 * conn. The caching is done after making sure that the ire is not yet
17346 	 * condemned. Also documented in the block comment above ip_output
17347 	 */
17348 	ipcl_walk(conn_cleanup_stale_ire, NULL);
17349 	/* Also, delete the ires cached in SCTP */
17350 	sctp_ire_cache_flush(ipif);
17351 
17352 	/* Resolve any IPsec/IKE NAT-T instances that depend on this ipif. */
17353 	nattymod_clean_ipif(ipif);
17354 
17355 	/*
17356 	 * Update any other ipifs which have used "our" local address as
17357 	 * a source address. This entails removing and recreating IRE_INTERFACE
17358 	 * entries for such ipifs.
17359 	 */
17360 	if (ipif->ipif_isv6)
17361 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
17362 	else
17363 		ipif_update_other_ipifs(ipif, ill->ill_group);
17364 
17365 	if (ipif_was_up) {
17366 		/*
17367 		 * Check whether it is last ipif to leave this group.
17368 		 * If this is the last ipif to leave, we should remove
17369 		 * this ill from the group as ipif_select_source will not
17370 		 * be able to find any useful ipifs if this ill is selected
17371 		 * for load balancing.
17372 		 *
17373 		 * For nameless groups, we should call ifgrp_delete if this
17374 		 * belongs to some group. As this ipif is going down, we may
17375 		 * need to reconstruct groups.
17376 		 */
17377 		phyi = ill->ill_phyint;
17378 		/*
17379 		 * If the phyint_groupname_len is 0, it may or may not
17380 		 * be in the nameless group. If the phyint_groupname_len is
17381 		 * not 0, then this ill should be part of some group.
17382 		 * As we always insert this ill in the group if
17383 		 * phyint_groupname_len is not zero when the first ipif
17384 		 * comes up (in ipif_up_done), it should be in a group
17385 		 * when the namelen is not 0.
17386 		 *
17387 		 * NOTE : When we delete the ill from the group,it will
17388 		 * blow away all the IRE_CACHES pointing either at this ipif or
17389 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
17390 		 * should be pointing at this ill.
17391 		 */
17392 		ASSERT(phyi->phyint_groupname_len == 0 ||
17393 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
17394 
17395 		if (phyi->phyint_groupname_len != 0) {
17396 			if (ill->ill_ipif_up_count == 0)
17397 				illgrp_delete(ill);
17398 		}
17399 
17400 		/*
17401 		 * If we have deleted some of the broadcast ires associated
17402 		 * with this ipif, we need to re-nominate somebody else if
17403 		 * the ires that we deleted were the nominated ones.
17404 		 */
17405 		if (ill->ill_group != NULL && !ill->ill_isv6)
17406 			ipif_renominate_bcast(ipif);
17407 	}
17408 
17409 	if (ipif->ipif_isv6)
17410 		ipif_ndp_down(ipif);
17411 
17412 	/*
17413 	 * If mp is NULL the caller will wait for the appropriate refcnt.
17414 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
17415 	 * and ill_delete -> ipif_free -> ipif_down
17416 	 */
17417 	if (mp == NULL) {
17418 		ASSERT(q == NULL);
17419 		return (0);
17420 	}
17421 
17422 	if (CONN_Q(q)) {
17423 		connp = Q_TO_CONN(q);
17424 		mutex_enter(&connp->conn_lock);
17425 	} else {
17426 		connp = NULL;
17427 	}
17428 	mutex_enter(&ill->ill_lock);
17429 	/*
17430 	 * Are there any ire's pointing to this ipif that are still active ?
17431 	 * If this is the last ipif going down, are there any ire's pointing
17432 	 * to this ill that are still active ?
17433 	 */
17434 	if (ipif_is_quiescent(ipif)) {
17435 		mutex_exit(&ill->ill_lock);
17436 		if (connp != NULL)
17437 			mutex_exit(&connp->conn_lock);
17438 		return (0);
17439 	}
17440 
17441 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
17442 	    ill->ill_name, (void *)ill));
17443 	/*
17444 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
17445 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
17446 	 * which in turn is called by the last refrele on the ipif/ill/ire.
17447 	 */
17448 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
17449 	if (!success) {
17450 		/* The conn is closing. So just return */
17451 		ASSERT(connp != NULL);
17452 		mutex_exit(&ill->ill_lock);
17453 		mutex_exit(&connp->conn_lock);
17454 		return (EINTR);
17455 	}
17456 
17457 	mutex_exit(&ill->ill_lock);
17458 	if (connp != NULL)
17459 		mutex_exit(&connp->conn_lock);
17460 	return (EINPROGRESS);
17461 }
17462 
17463 static void
17464 ipif_down_tail(ipif_t *ipif)
17465 {
17466 	ill_t	*ill = ipif->ipif_ill;
17467 
17468 	/*
17469 	 * Skip any loopback interface (null wq).
17470 	 * If this is the last logical interface on the ill
17471 	 * have ill_dl_down tell the driver we are gone (unbind)
17472 	 * Note that lun 0 can ipif_down even though
17473 	 * there are other logical units that are up.
17474 	 * This occurs e.g. when we change a "significant" IFF_ flag.
17475 	 */
17476 	if (ipif->ipif_ill->ill_wq != NULL) {
17477 		if (!ill->ill_logical_down && (ill->ill_ipif_up_count == 0) &&
17478 		    ill->ill_dl_up) {
17479 			ill_dl_down(ill);
17480 		}
17481 	}
17482 	ill->ill_logical_down = 0;
17483 
17484 	/*
17485 	 * Have to be after removing the routes in ipif_down_delete_ire.
17486 	 */
17487 	if (ipif->ipif_isv6) {
17488 		if (ipif->ipif_ill->ill_flags & ILLF_XRESOLV)
17489 			ipif_arp_down(ipif);
17490 	} else {
17491 		ipif_arp_down(ipif);
17492 	}
17493 
17494 	ip_rts_ifmsg(ipif);
17495 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
17496 }
17497 
17498 /*
17499  * Bring interface logically down without bringing the physical interface
17500  * down e.g. when the netmask is changed. This avoids long lasting link
17501  * negotiations between an ethernet interface and a certain switches.
17502  */
17503 static int
17504 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
17505 {
17506 	/*
17507 	 * The ill_logical_down flag is a transient flag. It is set here
17508 	 * and is cleared once the down has completed in ipif_down_tail.
17509 	 * This flag does not indicate whether the ill stream is in the
17510 	 * DL_BOUND state with the driver. Instead this flag is used by
17511 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
17512 	 * the driver. The state of the ill stream i.e. whether it is
17513 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
17514 	 */
17515 	ipif->ipif_ill->ill_logical_down = 1;
17516 	return (ipif_down(ipif, q, mp));
17517 }
17518 
17519 /*
17520  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
17521  * If the usesrc client ILL is already part of a usesrc group or not,
17522  * in either case a ire_stq with the matching usesrc client ILL will
17523  * locate the IRE's that need to be deleted. We want IREs to be created
17524  * with the new source address.
17525  */
17526 static void
17527 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
17528 {
17529 	ill_t	*ucill = (ill_t *)ill_arg;
17530 
17531 	ASSERT(IAM_WRITER_ILL(ucill));
17532 
17533 	if (ire->ire_stq == NULL)
17534 		return;
17535 
17536 	if ((ire->ire_type == IRE_CACHE) &&
17537 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
17538 		ire_delete(ire);
17539 }
17540 
17541 /*
17542  * ire_walk routine to delete every IRE dependent on the interface
17543  * address that is going down.	(Always called as writer.)
17544  * Works for both v4 and v6.
17545  * In addition for checking for ire_ipif matches it also checks for
17546  * IRE_CACHE entries which have the same source address as the
17547  * disappearing ipif since ipif_select_source might have picked
17548  * that source. Note that ipif_down/ipif_update_other_ipifs takes
17549  * care of any IRE_INTERFACE with the disappearing source address.
17550  */
17551 static void
17552 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
17553 {
17554 	ipif_t	*ipif = (ipif_t *)ipif_arg;
17555 	ill_t *ire_ill;
17556 	ill_t *ipif_ill;
17557 
17558 	ASSERT(IAM_WRITER_IPIF(ipif));
17559 	if (ire->ire_ipif == NULL)
17560 		return;
17561 
17562 	/*
17563 	 * For IPv4, we derive source addresses for an IRE from ipif's
17564 	 * belonging to the same IPMP group as the IRE's outgoing
17565 	 * interface.  If an IRE's outgoing interface isn't in the
17566 	 * same IPMP group as a particular ipif, then that ipif
17567 	 * couldn't have been used as a source address for this IRE.
17568 	 *
17569 	 * For IPv6, source addresses are only restricted to the IPMP group
17570 	 * if the IRE is for a link-local address or a multicast address.
17571 	 * Otherwise, source addresses for an IRE can be chosen from
17572 	 * interfaces other than the the outgoing interface for that IRE.
17573 	 *
17574 	 * For source address selection details, see ipif_select_source()
17575 	 * and ipif_select_source_v6().
17576 	 */
17577 	if (ire->ire_ipversion == IPV4_VERSION ||
17578 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
17579 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
17580 		ire_ill = ire->ire_ipif->ipif_ill;
17581 		ipif_ill = ipif->ipif_ill;
17582 
17583 		if (ire_ill->ill_group != ipif_ill->ill_group) {
17584 			return;
17585 		}
17586 	}
17587 
17588 
17589 	if (ire->ire_ipif != ipif) {
17590 		/*
17591 		 * Look for a matching source address.
17592 		 */
17593 		if (ire->ire_type != IRE_CACHE)
17594 			return;
17595 		if (ipif->ipif_flags & IPIF_NOLOCAL)
17596 			return;
17597 
17598 		if (ire->ire_ipversion == IPV4_VERSION) {
17599 			if (ire->ire_src_addr != ipif->ipif_src_addr)
17600 				return;
17601 		} else {
17602 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
17603 			    &ipif->ipif_v6lcl_addr))
17604 				return;
17605 		}
17606 		ire_delete(ire);
17607 		return;
17608 	}
17609 	/*
17610 	 * ire_delete() will do an ire_flush_cache which will delete
17611 	 * all ire_ipif matches
17612 	 */
17613 	ire_delete(ire);
17614 }
17615 
17616 /*
17617  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
17618  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
17619  * 2) when an interface is brought up or down (on that ill).
17620  * This ensures that the IRE_CACHE entries don't retain stale source
17621  * address selection results.
17622  */
17623 void
17624 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
17625 {
17626 	ill_t	*ill = (ill_t *)ill_arg;
17627 	ill_t	*ipif_ill;
17628 
17629 	ASSERT(IAM_WRITER_ILL(ill));
17630 	/*
17631 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
17632 	 * Hence this should be IRE_CACHE.
17633 	 */
17634 	ASSERT(ire->ire_type == IRE_CACHE);
17635 
17636 	/*
17637 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
17638 	 * We are only interested in IRE_CACHES that has borrowed
17639 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
17640 	 * for which we need to look at ire_ipif->ipif_ill match
17641 	 * with ill.
17642 	 */
17643 	ASSERT(ire->ire_ipif != NULL);
17644 	ipif_ill = ire->ire_ipif->ipif_ill;
17645 	if (ipif_ill == ill || (ill->ill_group != NULL &&
17646 	    ipif_ill->ill_group == ill->ill_group)) {
17647 		ire_delete(ire);
17648 	}
17649 }
17650 
17651 /*
17652  * Delete all the ire whose stq references ill_arg.
17653  */
17654 static void
17655 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
17656 {
17657 	ill_t	*ill = (ill_t *)ill_arg;
17658 	ill_t	*ire_ill;
17659 
17660 	ASSERT(IAM_WRITER_ILL(ill));
17661 	/*
17662 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
17663 	 * Hence this should be IRE_CACHE.
17664 	 */
17665 	ASSERT(ire->ire_type == IRE_CACHE);
17666 
17667 	/*
17668 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
17669 	 * matches ill. We are only interested in IRE_CACHES that
17670 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
17671 	 * filtering here.
17672 	 */
17673 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
17674 
17675 	if (ire_ill == ill)
17676 		ire_delete(ire);
17677 }
17678 
17679 /*
17680  * This is called when an ill leaves the group. We want to delete
17681  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
17682  * pointing at ill.
17683  */
17684 static void
17685 illgrp_cache_delete(ire_t *ire, char *ill_arg)
17686 {
17687 	ill_t	*ill = (ill_t *)ill_arg;
17688 
17689 	ASSERT(IAM_WRITER_ILL(ill));
17690 	ASSERT(ill->ill_group == NULL);
17691 	/*
17692 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
17693 	 * Hence this should be IRE_CACHE.
17694 	 */
17695 	ASSERT(ire->ire_type == IRE_CACHE);
17696 	/*
17697 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
17698 	 * matches ill. We are interested in both.
17699 	 */
17700 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
17701 	    (ire->ire_ipif->ipif_ill == ill));
17702 
17703 	ire_delete(ire);
17704 }
17705 
17706 /*
17707  * Initiate deallocate of an IPIF. Always called as writer. Called by
17708  * ill_delete or ip_sioctl_removeif.
17709  */
17710 static void
17711 ipif_free(ipif_t *ipif)
17712 {
17713 	ASSERT(IAM_WRITER_IPIF(ipif));
17714 
17715 	/* Remove conn references */
17716 	reset_conn_ipif(ipif);
17717 
17718 	/*
17719 	 * Make sure we have valid net and subnet broadcast ire's for the
17720 	 * other ipif's which share them with this ipif.
17721 	 */
17722 	if (!ipif->ipif_isv6)
17723 		ipif_check_bcast_ires(ipif);
17724 
17725 	/*
17726 	 * Take down the interface. We can be called either from ill_delete
17727 	 * or from ip_sioctl_removeif.
17728 	 */
17729 	(void) ipif_down(ipif, NULL, NULL);
17730 
17731 	rw_enter(&ill_g_lock, RW_WRITER);
17732 	/* Remove pointers to this ill in the multicast routing tables */
17733 	reset_mrt_vif_ipif(ipif);
17734 	rw_exit(&ill_g_lock);
17735 }
17736 
17737 static void
17738 ipif_free_tail(ipif_t *ipif)
17739 {
17740 	mblk_t	*mp;
17741 	ipif_t	**ipifp;
17742 
17743 	/*
17744 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
17745 	 */
17746 	mutex_enter(&ipif->ipif_saved_ire_lock);
17747 	mp = ipif->ipif_saved_ire_mp;
17748 	ipif->ipif_saved_ire_mp = NULL;
17749 	mutex_exit(&ipif->ipif_saved_ire_lock);
17750 	freemsg(mp);
17751 
17752 	/*
17753 	 * Need to hold both ill_g_lock and ill_lock while
17754 	 * inserting or removing an ipif from the linked list
17755 	 * of ipifs hanging off the ill.
17756 	 */
17757 	rw_enter(&ill_g_lock, RW_WRITER);
17758 	/*
17759 	 * Remove all multicast memberships on the interface now.
17760 	 * This removes IPv4 multicast memberships joined within
17761 	 * the kernel as ipif_down does not do ipif_multicast_down
17762 	 * for IPv4. IPv6 is not handled here as the multicast memberships
17763 	 * are based on ill and not on ipif.
17764 	 */
17765 	ilm_free(ipif);
17766 
17767 	/*
17768 	 * Since we held the ill_g_lock while doing the ilm_free above,
17769 	 * we can assert the ilms were really deleted and not just marked
17770 	 * ILM_DELETED.
17771 	 */
17772 	ASSERT(ilm_walk_ipif(ipif) == 0);
17773 
17774 
17775 	IPIF_TRACE_CLEANUP(ipif);
17776 
17777 	/* Ask SCTP to take it out of it list */
17778 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
17779 
17780 	mutex_enter(&ipif->ipif_ill->ill_lock);
17781 	/* Get it out of the ILL interface list. */
17782 	ipifp = &ipif->ipif_ill->ill_ipif;
17783 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
17784 		if (*ipifp == ipif) {
17785 			*ipifp = ipif->ipif_next;
17786 			break;
17787 		}
17788 	}
17789 
17790 	mutex_exit(&ipif->ipif_ill->ill_lock);
17791 	rw_exit(&ill_g_lock);
17792 
17793 	mutex_destroy(&ipif->ipif_saved_ire_lock);
17794 	/* Free the memory. */
17795 	mi_free((char *)ipif);
17796 }
17797 
17798 /*
17799  * Returns an ipif name in the form "ill_name/unit" if ipif_id is not zero,
17800  * "ill_name" otherwise.
17801  */
17802 char *
17803 ipif_get_name(ipif_t *ipif, char *buf, int len)
17804 {
17805 	char	lbuf[32];
17806 	char	*name;
17807 	size_t	name_len;
17808 
17809 	buf[0] = '\0';
17810 	if (!ipif)
17811 		return (buf);
17812 	name = ipif->ipif_ill->ill_name;
17813 	name_len = ipif->ipif_ill->ill_name_length;
17814 	if (ipif->ipif_id != 0) {
17815 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
17816 		    ipif->ipif_id);
17817 		name = lbuf;
17818 		name_len = mi_strlen(name) + 1;
17819 	}
17820 	len -= 1;
17821 	buf[len] = '\0';
17822 	len = MIN(len, name_len);
17823 	bcopy(name, buf, len);
17824 	return (buf);
17825 }
17826 
17827 /*
17828  * Find an IPIF based on the name passed in.  Names can be of the
17829  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
17830  * The <phys> string can have forms like <dev><#> (e.g., le0),
17831  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
17832  * When there is no colon, the implied unit id is zero. <phys> must
17833  * correspond to the name of an ILL.  (May be called as writer.)
17834  */
17835 static ipif_t *
17836 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
17837     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
17838     mblk_t *mp, ipsq_func_t func, int *error)
17839 {
17840 	char	*cp;
17841 	char	*endp;
17842 	long	id;
17843 	ill_t	*ill;
17844 	ipif_t	*ipif;
17845 	uint_t	ire_type;
17846 	boolean_t did_alloc = B_FALSE;
17847 	ipsq_t	*ipsq;
17848 
17849 	if (error != NULL)
17850 		*error = 0;
17851 
17852 	/*
17853 	 * If the caller wants to us to create the ipif, make sure we have a
17854 	 * valid zoneid
17855 	 */
17856 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
17857 
17858 	if (namelen == 0) {
17859 		if (error != NULL)
17860 			*error = ENXIO;
17861 		return (NULL);
17862 	}
17863 
17864 	*exists = B_FALSE;
17865 	/* Look for a colon in the name. */
17866 	endp = &name[namelen];
17867 	for (cp = endp; --cp > name; ) {
17868 		if (*cp == IPIF_SEPARATOR_CHAR)
17869 			break;
17870 	}
17871 
17872 	if (*cp == IPIF_SEPARATOR_CHAR) {
17873 		/*
17874 		 * Reject any non-decimal aliases for logical
17875 		 * interfaces. Aliases with leading zeroes
17876 		 * are also rejected as they introduce ambiguity
17877 		 * in the naming of the interfaces.
17878 		 * In order to confirm with existing semantics,
17879 		 * and to not break any programs/script relying
17880 		 * on that behaviour, if<0>:0 is considered to be
17881 		 * a valid interface.
17882 		 *
17883 		 * If alias has two or more digits and the first
17884 		 * is zero, fail.
17885 		 */
17886 		if (&cp[2] < endp && cp[1] == '0')
17887 			return (NULL);
17888 	}
17889 
17890 	if (cp <= name) {
17891 		cp = endp;
17892 	} else {
17893 		*cp = '\0';
17894 	}
17895 
17896 	/*
17897 	 * Look up the ILL, based on the portion of the name
17898 	 * before the slash. ill_lookup_on_name returns a held ill.
17899 	 * Temporary to check whether ill exists already. If so
17900 	 * ill_lookup_on_name will clear it.
17901 	 */
17902 	ill = ill_lookup_on_name(name, do_alloc, isv6,
17903 	    q, mp, func, error, &did_alloc);
17904 	if (cp != endp)
17905 		*cp = IPIF_SEPARATOR_CHAR;
17906 	if (ill == NULL)
17907 		return (NULL);
17908 
17909 	/* Establish the unit number in the name. */
17910 	id = 0;
17911 	if (cp < endp && *endp == '\0') {
17912 		/* If there was a colon, the unit number follows. */
17913 		cp++;
17914 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
17915 			ill_refrele(ill);
17916 			if (error != NULL)
17917 				*error = ENXIO;
17918 			return (NULL);
17919 		}
17920 	}
17921 
17922 	GRAB_CONN_LOCK(q);
17923 	mutex_enter(&ill->ill_lock);
17924 	/* Now see if there is an IPIF with this unit number. */
17925 	for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
17926 		if (ipif->ipif_id == id) {
17927 			if (zoneid != ALL_ZONES &&
17928 			    zoneid != ipif->ipif_zoneid) {
17929 				mutex_exit(&ill->ill_lock);
17930 				RELEASE_CONN_LOCK(q);
17931 				ill_refrele(ill);
17932 				if (error != NULL)
17933 					*error = ENXIO;
17934 				return (NULL);
17935 			}
17936 			/*
17937 			 * The block comment at the start of ipif_down
17938 			 * explains the use of the macros used below
17939 			 */
17940 			if (IPIF_CAN_LOOKUP(ipif)) {
17941 				ipif_refhold_locked(ipif);
17942 				mutex_exit(&ill->ill_lock);
17943 				if (!did_alloc)
17944 					*exists = B_TRUE;
17945 				/*
17946 				 * Drop locks before calling ill_refrele
17947 				 * since it can potentially call into
17948 				 * ipif_ill_refrele_tail which can end up
17949 				 * in trying to acquire any lock.
17950 				 */
17951 				RELEASE_CONN_LOCK(q);
17952 				ill_refrele(ill);
17953 				return (ipif);
17954 			} else if (IPIF_CAN_WAIT(ipif, q)) {
17955 				ipsq = ill->ill_phyint->phyint_ipsq;
17956 				mutex_enter(&ipsq->ipsq_lock);
17957 				mutex_exit(&ill->ill_lock);
17958 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
17959 				mutex_exit(&ipsq->ipsq_lock);
17960 				RELEASE_CONN_LOCK(q);
17961 				ill_refrele(ill);
17962 				*error = EINPROGRESS;
17963 				return (NULL);
17964 			}
17965 		}
17966 	}
17967 	RELEASE_CONN_LOCK(q);
17968 
17969 	if (!do_alloc) {
17970 		mutex_exit(&ill->ill_lock);
17971 		ill_refrele(ill);
17972 		if (error != NULL)
17973 			*error = ENXIO;
17974 		return (NULL);
17975 	}
17976 
17977 	/*
17978 	 * If none found, atomically allocate and return a new one.
17979 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
17980 	 * to support "receive only" use of lo0:1 etc. as is still done
17981 	 * below as an initial guess.
17982 	 * However, this is now likely to be overriden later in ipif_up_done()
17983 	 * when we know for sure what address has been configured on the
17984 	 * interface, since we might have more than one loopback interface
17985 	 * with a loopback address, e.g. in the case of zones, and all the
17986 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
17987 	 */
17988 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
17989 		ire_type = IRE_LOOPBACK;
17990 	else
17991 		ire_type = IRE_LOCAL;
17992 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
17993 	if (ipif != NULL)
17994 		ipif_refhold_locked(ipif);
17995 	else if (error != NULL)
17996 		*error = ENOMEM;
17997 	mutex_exit(&ill->ill_lock);
17998 	ill_refrele(ill);
17999 	return (ipif);
18000 }
18001 
18002 /*
18003  * This routine is called whenever a new address comes up on an ipif.  If
18004  * we are configured to respond to address mask requests, then we are supposed
18005  * to broadcast an address mask reply at this time.  This routine is also
18006  * called if we are already up, but a netmask change is made.  This is legal
18007  * but might not make the system manager very popular.	(May be called
18008  * as writer.)
18009  */
18010 static void
18011 ipif_mask_reply(ipif_t *ipif)
18012 {
18013 	icmph_t	*icmph;
18014 	ipha_t	*ipha;
18015 	mblk_t	*mp;
18016 
18017 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
18018 
18019 	if (!ip_respond_to_address_mask_broadcast)
18020 		return;
18021 
18022 	/* ICMP mask reply is IPv4 only */
18023 	ASSERT(!ipif->ipif_isv6);
18024 	/* ICMP mask reply is not for a loopback interface */
18025 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
18026 
18027 	mp = allocb(REPLY_LEN, BPRI_HI);
18028 	if (mp == NULL)
18029 		return;
18030 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
18031 
18032 	ipha = (ipha_t *)mp->b_rptr;
18033 	bzero(ipha, REPLY_LEN);
18034 	*ipha = icmp_ipha;
18035 	ipha->ipha_ttl = ip_broadcast_ttl;
18036 	ipha->ipha_src = ipif->ipif_src_addr;
18037 	ipha->ipha_dst = ipif->ipif_brd_addr;
18038 	ipha->ipha_length = htons(REPLY_LEN);
18039 	ipha->ipha_ident = 0;
18040 
18041 	icmph = (icmph_t *)&ipha[1];
18042 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
18043 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
18044 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
18045 
18046 	put(ipif->ipif_wq, mp);
18047 
18048 #undef	REPLY_LEN
18049 }
18050 
18051 /*
18052  * When the mtu in the ipif changes, we call this routine through ire_walk
18053  * to update all the relevant IREs.
18054  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
18055  */
18056 static void
18057 ipif_mtu_change(ire_t *ire, char *ipif_arg)
18058 {
18059 	ipif_t *ipif = (ipif_t *)ipif_arg;
18060 
18061 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
18062 		return;
18063 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
18064 }
18065 
18066 /*
18067  * When the mtu in the ill changes, we call this routine through ire_walk
18068  * to update all the relevant IREs.
18069  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
18070  */
18071 void
18072 ill_mtu_change(ire_t *ire, char *ill_arg)
18073 {
18074 	ill_t	*ill = (ill_t *)ill_arg;
18075 
18076 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
18077 		return;
18078 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
18079 }
18080 
18081 /*
18082  * Join the ipif specific multicast groups.
18083  * Must be called after a mapping has been set up in the resolver.  (Always
18084  * called as writer.)
18085  */
18086 void
18087 ipif_multicast_up(ipif_t *ipif)
18088 {
18089 	int err, index;
18090 	ill_t *ill;
18091 
18092 	ASSERT(IAM_WRITER_IPIF(ipif));
18093 
18094 	ill = ipif->ipif_ill;
18095 	index = ill->ill_phyint->phyint_ifindex;
18096 
18097 	ip1dbg(("ipif_multicast_up\n"));
18098 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
18099 		return;
18100 
18101 	if (ipif->ipif_isv6) {
18102 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
18103 			return;
18104 
18105 		/* Join the all hosts multicast address */
18106 		ip1dbg(("ipif_multicast_up - addmulti\n"));
18107 		/*
18108 		 * Passing B_TRUE means we have to join the multicast
18109 		 * membership on this interface even though this is
18110 		 * FAILED. If we join on a different one in the group,
18111 		 * we will not be able to delete the membership later
18112 		 * as we currently don't track where we join when we
18113 		 * join within the kernel unlike applications where
18114 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
18115 		 * for more on this.
18116 		 */
18117 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
18118 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
18119 		if (err != 0) {
18120 			ip0dbg(("ipif_multicast_up: "
18121 			    "all_hosts_mcast failed %d\n",
18122 			    err));
18123 			return;
18124 		}
18125 		/*
18126 		 * Enable multicast for the solicited node multicast address
18127 		 */
18128 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
18129 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
18130 
18131 			ipv6_multi.s6_addr32[3] |=
18132 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
18133 
18134 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
18135 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
18136 			    NULL);
18137 			if (err != 0) {
18138 				ip0dbg(("ipif_multicast_up: solicited MC"
18139 				    " failed %d\n", err));
18140 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
18141 				    ill, ill->ill_phyint->phyint_ifindex,
18142 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
18143 				return;
18144 			}
18145 		}
18146 	} else {
18147 		if (ipif->ipif_lcl_addr == INADDR_ANY)
18148 			return;
18149 
18150 		/* Join the all hosts multicast address */
18151 		ip1dbg(("ipif_multicast_up - addmulti\n"));
18152 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
18153 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
18154 		if (err) {
18155 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
18156 			return;
18157 		}
18158 	}
18159 	ipif->ipif_multicast_up = 1;
18160 }
18161 
18162 /*
18163  * Blow away any IPv6 multicast groups that we joined in ipif_multicast_up();
18164  * any explicit memberships are blown away in ill_leave_multicast() when the
18165  * ill is brought down.
18166  */
18167 static void
18168 ipif_multicast_down(ipif_t *ipif)
18169 {
18170 	int err;
18171 
18172 	ASSERT(IAM_WRITER_IPIF(ipif));
18173 
18174 	ip1dbg(("ipif_multicast_down\n"));
18175 	if (!ipif->ipif_multicast_up)
18176 		return;
18177 
18178 	ASSERT(ipif->ipif_isv6);
18179 
18180 	ip1dbg(("ipif_multicast_down - delmulti\n"));
18181 
18182 	/*
18183 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
18184 	 * we should look for ilms on this ill rather than the ones that have
18185 	 * been failed over here.  They are here temporarily. As
18186 	 * ipif_multicast_up has joined on this ill, we should delete only
18187 	 * from this ill.
18188 	 */
18189 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
18190 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
18191 	    B_TRUE, B_TRUE);
18192 	if (err != 0) {
18193 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
18194 		    err));
18195 	}
18196 	/*
18197 	 * Disable multicast for the solicited node multicast address
18198 	 */
18199 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
18200 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
18201 
18202 		ipv6_multi.s6_addr32[3] |=
18203 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
18204 
18205 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
18206 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
18207 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
18208 
18209 		if (err != 0) {
18210 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
18211 			    err));
18212 		}
18213 	}
18214 
18215 	ipif->ipif_multicast_up = 0;
18216 }
18217 
18218 /*
18219  * Used when an interface comes up to recreate any extra routes on this
18220  * interface.
18221  */
18222 static ire_t **
18223 ipif_recover_ire(ipif_t *ipif)
18224 {
18225 	mblk_t	*mp;
18226 	ire_t	**ipif_saved_irep;
18227 	ire_t	**irep;
18228 
18229 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
18230 	    ipif->ipif_id));
18231 
18232 	mutex_enter(&ipif->ipif_saved_ire_lock);
18233 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
18234 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
18235 	if (ipif_saved_irep == NULL) {
18236 		mutex_exit(&ipif->ipif_saved_ire_lock);
18237 		return (NULL);
18238 	}
18239 
18240 	irep = ipif_saved_irep;
18241 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
18242 		ire_t		*ire;
18243 		queue_t		*rfq;
18244 		queue_t		*stq;
18245 		ifrt_t		*ifrt;
18246 		uchar_t		*src_addr;
18247 		uchar_t		*gateway_addr;
18248 		mblk_t		*resolver_mp;
18249 		ushort_t	type;
18250 
18251 		/*
18252 		 * When the ire was initially created and then added in
18253 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
18254 		 * in the case of a traditional interface route, or as one of
18255 		 * the IRE_OFFSUBNET types (with the exception of
18256 		 * IRE_HOST_REDIRECT which is created by icmp_redirect() and
18257 		 * which we don't need to save or recover).  In the case where
18258 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
18259 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
18260 		 * to satisfy software like GateD and Sun Cluster which creates
18261 		 * routes using the the loopback interface's address as a
18262 		 * gateway.
18263 		 *
18264 		 * As ifrt->ifrt_type reflects the already updated ire_type and
18265 		 * since ire_create() expects that IRE_IF_NORESOLVER will have
18266 		 * a valid ire_dlureq_mp field (which doesn't make sense for a
18267 		 * IRE_LOOPBACK), ire_create() will be called in the same way
18268 		 * here as in ip_rt_add(), namely using ipif->ipif_net_type when
18269 		 * the route looks like a traditional interface route (where
18270 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
18271 		 * the saved ifrt->ifrt_type.  This means that in the case where
18272 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
18273 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
18274 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
18275 		 */
18276 		ifrt = (ifrt_t *)mp->b_rptr;
18277 		if (ifrt->ifrt_type & IRE_INTERFACE) {
18278 			rfq = NULL;
18279 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
18280 			    ? ipif->ipif_rq : ipif->ipif_wq;
18281 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
18282 			    ? (uint8_t *)&ifrt->ifrt_src_addr
18283 			    : (uint8_t *)&ipif->ipif_src_addr;
18284 			gateway_addr = NULL;
18285 			resolver_mp = ipif->ipif_resolver_mp;
18286 			type = ipif->ipif_net_type;
18287 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
18288 			/* Recover multiroute broadcast IRE. */
18289 			rfq = ipif->ipif_rq;
18290 			stq = ipif->ipif_wq;
18291 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
18292 			    ? (uint8_t *)&ifrt->ifrt_src_addr
18293 			    : (uint8_t *)&ipif->ipif_src_addr;
18294 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
18295 			resolver_mp = ipif->ipif_bcast_mp;
18296 			type = ifrt->ifrt_type;
18297 		} else {
18298 			rfq = NULL;
18299 			stq = NULL;
18300 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
18301 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
18302 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
18303 			resolver_mp = NULL;
18304 			type = ifrt->ifrt_type;
18305 		}
18306 
18307 		/*
18308 		 * Create a copy of the IRE with the saved address and netmask.
18309 		 */
18310 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
18311 		    "0x%x/0x%x\n",
18312 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
18313 		    ntohl(ifrt->ifrt_addr),
18314 		    ntohl(ifrt->ifrt_mask)));
18315 		ire = ire_create(
18316 		    (uint8_t *)&ifrt->ifrt_addr,
18317 		    (uint8_t *)&ifrt->ifrt_mask,
18318 		    src_addr,
18319 		    gateway_addr,
18320 		    NULL,
18321 		    &ifrt->ifrt_max_frag,
18322 		    NULL,
18323 		    rfq,
18324 		    stq,
18325 		    type,
18326 		    resolver_mp,
18327 		    ipif,
18328 		    NULL,
18329 		    0,
18330 		    0,
18331 		    0,
18332 		    ifrt->ifrt_flags,
18333 		    &ifrt->ifrt_iulp_info);
18334 
18335 		if (ire == NULL) {
18336 			mutex_exit(&ipif->ipif_saved_ire_lock);
18337 			kmem_free(ipif_saved_irep,
18338 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
18339 			return (NULL);
18340 		}
18341 
18342 		/*
18343 		 * Some software (for example, GateD and Sun Cluster) attempts
18344 		 * to create (what amount to) IRE_PREFIX routes with the
18345 		 * loopback address as the gateway.  This is primarily done to
18346 		 * set up prefixes with the RTF_REJECT flag set (for example,
18347 		 * when generating aggregate routes.)
18348 		 *
18349 		 * If the IRE type (as defined by ipif->ipif_net_type) is
18350 		 * IRE_LOOPBACK, then we map the request into a
18351 		 * IRE_IF_NORESOLVER.
18352 		 */
18353 		if (ipif->ipif_net_type == IRE_LOOPBACK)
18354 			ire->ire_type = IRE_IF_NORESOLVER;
18355 		/*
18356 		 * ire held by ire_add, will be refreled' towards the
18357 		 * the end of ipif_up_done
18358 		 */
18359 		(void) ire_add(&ire, NULL, NULL, NULL);
18360 		*irep = ire;
18361 		irep++;
18362 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
18363 	}
18364 	mutex_exit(&ipif->ipif_saved_ire_lock);
18365 	return (ipif_saved_irep);
18366 }
18367 
18368 /*
18369  * Used to set the netmask and broadcast address to default values when the
18370  * interface is brought up.  (Always called as writer.)
18371  */
18372 static void
18373 ipif_set_default(ipif_t *ipif)
18374 {
18375 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
18376 
18377 	if (!ipif->ipif_isv6) {
18378 		/*
18379 		 * Interface holds an IPv4 address. Default
18380 		 * mask is the natural netmask.
18381 		 */
18382 		if (!ipif->ipif_net_mask) {
18383 			ipaddr_t	v4mask;
18384 
18385 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
18386 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
18387 		}
18388 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18389 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
18390 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
18391 		} else {
18392 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
18393 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
18394 		}
18395 		/*
18396 		 * NOTE: SunOS 4.X does this even if the broadcast address
18397 		 * has been already set thus we do the same here.
18398 		 */
18399 		if (ipif->ipif_flags & IPIF_BROADCAST) {
18400 			ipaddr_t	v4addr;
18401 
18402 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
18403 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
18404 		}
18405 	} else {
18406 		/*
18407 		 * Interface holds an IPv6-only address.  Default
18408 		 * mask is all-ones.
18409 		 */
18410 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
18411 			ipif->ipif_v6net_mask = ipv6_all_ones;
18412 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18413 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
18414 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
18415 		} else {
18416 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
18417 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
18418 		}
18419 	}
18420 }
18421 
18422 /*
18423  * Return 0 if this address can be used as local address without causing
18424  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
18425  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
18426  * Special checks are needed to allow the same IPv6 link-local address
18427  * on different ills.
18428  * TODO: allowing the same site-local address on different ill's.
18429  */
18430 int
18431 ip_addr_availability_check(ipif_t *new_ipif)
18432 {
18433 	in6_addr_t our_v6addr;
18434 	ill_t *ill;
18435 	ipif_t *ipif;
18436 	ill_walk_context_t ctx;
18437 
18438 	ASSERT(IAM_WRITER_IPIF(new_ipif));
18439 	ASSERT(MUTEX_HELD(&ip_addr_avail_lock));
18440 	ASSERT(RW_READ_HELD(&ill_g_lock));
18441 
18442 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
18443 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
18444 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
18445 		return (0);
18446 
18447 	our_v6addr = new_ipif->ipif_v6lcl_addr;
18448 
18449 	if (new_ipif->ipif_isv6)
18450 		ill = ILL_START_WALK_V6(&ctx);
18451 	else
18452 		ill = ILL_START_WALK_V4(&ctx);
18453 
18454 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
18455 		for (ipif = ill->ill_ipif; ipif != NULL;
18456 		    ipif = ipif->ipif_next) {
18457 			if ((ipif == new_ipif) ||
18458 			    !(ipif->ipif_flags & IPIF_UP) ||
18459 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
18460 				continue;
18461 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
18462 			    &our_v6addr)) {
18463 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
18464 				    new_ipif->ipif_flags |= IPIF_UNNUMBERED;
18465 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
18466 				    ipif->ipif_flags |= IPIF_UNNUMBERED;
18467 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
18468 				    new_ipif->ipif_ill != ill)
18469 					continue;
18470 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
18471 				    new_ipif->ipif_ill != ill)
18472 					continue;
18473 				else if (new_ipif->ipif_zoneid !=
18474 				    ipif->ipif_zoneid &&
18475 				    (ill->ill_phyint->phyint_flags &
18476 				    PHYI_LOOPBACK))
18477 					continue;
18478 				else if (new_ipif->ipif_ill == ill)
18479 					return (EADDRINUSE);
18480 				else
18481 					return (EADDRNOTAVAIL);
18482 			}
18483 		}
18484 	}
18485 
18486 	return (0);
18487 }
18488 
18489 /*
18490  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
18491  * IREs for the ipif.
18492  * When the routine returns EINPROGRESS then mp has been consumed and
18493  * the ioctl will be acked from ip_rput_dlpi.
18494  */
18495 static int
18496 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
18497 {
18498 	ill_t	*ill = ipif->ipif_ill;
18499 	boolean_t isv6 = ipif->ipif_isv6;
18500 	int	err = 0;
18501 	boolean_t success;
18502 
18503 	ASSERT(IAM_WRITER_IPIF(ipif));
18504 
18505 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18506 
18507 	/* Shouldn't get here if it is already up. */
18508 	if (ipif->ipif_flags & IPIF_UP)
18509 		return (EALREADY);
18510 
18511 	/* Skip arp/ndp for any loopback interface. */
18512 	if (ill->ill_wq != NULL) {
18513 		conn_t *connp = Q_TO_CONN(q);
18514 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
18515 
18516 		if (!ill->ill_dl_up) {
18517 			/*
18518 			 * ill_dl_up is not yet set. i.e. we are yet to
18519 			 * DL_BIND with the driver and this is the first
18520 			 * logical interface on the ill to become "up".
18521 			 * Tell the driver to get going (via DL_BIND_REQ).
18522 			 * Note that changing "significant" IFF_ flags
18523 			 * address/netmask etc cause a down/up dance, but
18524 			 * does not cause an unbind (DL_UNBIND) with the driver
18525 			 */
18526 			return (ill_dl_up(ill, ipif, mp, q));
18527 		}
18528 
18529 		/*
18530 		 * ipif_resolver_up may end up sending an
18531 		 * AR_INTERFACE_UP message to ARP, which would, in
18532 		 * turn send a DLPI message to the driver. ioctls are
18533 		 * serialized and so we cannot send more than one
18534 		 * interface up message at a time. If ipif_resolver_up
18535 		 * does send an interface up message to ARP, we get
18536 		 * EINPROGRESS and we will complete in ip_arp_done.
18537 		 */
18538 
18539 		ASSERT(connp != NULL);
18540 		ASSERT(ipsq->ipsq_pending_mp == NULL);
18541 		mutex_enter(&connp->conn_lock);
18542 		mutex_enter(&ill->ill_lock);
18543 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
18544 		mutex_exit(&ill->ill_lock);
18545 		mutex_exit(&connp->conn_lock);
18546 		if (!success)
18547 			return (EINTR);
18548 
18549 		/*
18550 		 * Crank up IPv6 neighbor discovery
18551 		 * Unlike ARP, this should complete when
18552 		 * ipif_ndp_up returns. However, for
18553 		 * ILLF_XRESOLV interfaces we also send a
18554 		 * AR_INTERFACE_UP to the external resolver.
18555 		 * That ioctl will complete in ip_rput.
18556 		 */
18557 		if (isv6) {
18558 			err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr,
18559 			    B_FALSE);
18560 			if (err != 0) {
18561 				mp = ipsq_pending_mp_get(ipsq, &connp);
18562 				return (err);
18563 			}
18564 		}
18565 		/* Now, ARP */
18566 		if ((err = ipif_resolver_up(ipif, B_FALSE)) ==
18567 		    EINPROGRESS) {
18568 			/* We will complete it in ip_arp_done */
18569 			return (err);
18570 		}
18571 		mp = ipsq_pending_mp_get(ipsq, &connp);
18572 		ASSERT(mp != NULL);
18573 		if (err != 0)
18574 			return (err);
18575 	}
18576 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
18577 }
18578 
18579 /*
18580  * Perform a bind for the physical device.
18581  * When the routine returns EINPROGRESS then mp has been consumed and
18582  * the ioctl will be acked from ip_rput_dlpi.
18583  * Allocate an unbind message and save it until ipif_down.
18584  */
18585 static int
18586 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
18587 {
18588 	mblk_t	*areq_mp = NULL;
18589 	mblk_t	*bind_mp = NULL;
18590 	mblk_t	*unbind_mp = NULL;
18591 	conn_t	*connp;
18592 	boolean_t success;
18593 
18594 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
18595 	ASSERT(IAM_WRITER_ILL(ill));
18596 
18597 	ASSERT(mp != NULL);
18598 
18599 	/* Create a resolver cookie for ARP */
18600 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
18601 		areq_t		*areq;
18602 		uint16_t	sap_addr;
18603 
18604 		areq_mp = ill_arp_alloc(ill,
18605 			(uchar_t *)&ip_areq_template, 0);
18606 		if (areq_mp == NULL) {
18607 			return (ENOMEM);
18608 		}
18609 		freemsg(ill->ill_resolver_mp);
18610 		ill->ill_resolver_mp = areq_mp;
18611 		areq = (areq_t *)areq_mp->b_rptr;
18612 		sap_addr = ill->ill_sap;
18613 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
18614 		/*
18615 		 * Wait till we call ill_pending_mp_add to determine
18616 		 * the success before we free the ill_resolver_mp and
18617 		 * attach areq_mp in it's place.
18618 		 */
18619 	}
18620 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
18621 	    DL_BIND_REQ);
18622 	if (bind_mp == NULL)
18623 		goto bad;
18624 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
18625 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
18626 
18627 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
18628 	if (unbind_mp == NULL)
18629 		goto bad;
18630 
18631 	/*
18632 	 * Record state needed to complete this operation when the
18633 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
18634 	 */
18635 	if (WR(q)->q_next == NULL) {
18636 		connp = Q_TO_CONN(q);
18637 		mutex_enter(&connp->conn_lock);
18638 	} else {
18639 		connp = NULL;
18640 	}
18641 	mutex_enter(&ipif->ipif_ill->ill_lock);
18642 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
18643 	mutex_exit(&ipif->ipif_ill->ill_lock);
18644 	if (connp != NULL)
18645 		mutex_exit(&connp->conn_lock);
18646 	if (!success)
18647 		goto bad;
18648 
18649 	/*
18650 	 * Save the unbind message for ill_dl_down(); it will be consumed when
18651 	 * the interface goes down.
18652 	 */
18653 	ASSERT(ill->ill_unbind_mp == NULL);
18654 	ill->ill_unbind_mp = unbind_mp;
18655 
18656 	ill_dlpi_send(ill, bind_mp);
18657 	/* Send down link-layer capabilities probe if not already done. */
18658 	ill_capability_probe(ill);
18659 
18660 	/*
18661 	 * Sysid used to rely on the fact that netboots set domainname
18662 	 * and the like. Now that miniroot boots aren't strictly netboots
18663 	 * and miniroot network configuration is driven from userland
18664 	 * these things still need to be set. This situation can be detected
18665 	 * by comparing the interface being configured here to the one
18666 	 * dhcack was set to reference by the boot loader. Once sysid is
18667 	 * converted to use dhcp_ipc_getinfo() this call can go away.
18668 	 */
18669 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
18670 	    (strcmp(ill->ill_name, dhcack) == 0) &&
18671 	    (strlen(srpc_domain) == 0)) {
18672 		if (dhcpinit() != 0)
18673 			cmn_err(CE_WARN, "no cached dhcp response");
18674 	}
18675 
18676 	/*
18677 	 * This operation will complete in ip_rput_dlpi with either
18678 	 * a DL_BIND_ACK or DL_ERROR_ACK.
18679 	 */
18680 	return (EINPROGRESS);
18681 bad:
18682 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
18683 	/*
18684 	 * We don't have to check for possible removal from illgrp
18685 	 * as we have not yet inserted in illgrp. For groups
18686 	 * without names, this ipif is still not UP and hence
18687 	 * this could not have possibly had any influence in forming
18688 	 * groups.
18689 	 */
18690 
18691 	if (bind_mp != NULL)
18692 		freemsg(bind_mp);
18693 	if (unbind_mp != NULL)
18694 		freemsg(unbind_mp);
18695 	return (ENOMEM);
18696 }
18697 
18698 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
18699 
18700 /*
18701  * DLPI and ARP is up.
18702  * Create all the IREs associated with an interface bring up multicast.
18703  * Set the interface flag and finish other initialization
18704  * that potentially had to be differed to after DL_BIND_ACK.
18705  */
18706 int
18707 ipif_up_done(ipif_t *ipif)
18708 {
18709 	ire_t	*ire_array[20];
18710 	ire_t	**irep = ire_array;
18711 	ire_t	**irep1;
18712 	ipaddr_t net_mask = 0;
18713 	ipaddr_t subnet_mask, route_mask;
18714 	ill_t	*ill = ipif->ipif_ill;
18715 	queue_t	*stq;
18716 	ipif_t	 *src_ipif;
18717 	ipif_t   *tmp_ipif;
18718 	boolean_t	flush_ire_cache = B_TRUE;
18719 	int	err = 0;
18720 	phyint_t *phyi;
18721 	ire_t	**ipif_saved_irep = NULL;
18722 	int ipif_saved_ire_cnt;
18723 	int	cnt;
18724 	boolean_t	src_ipif_held = B_FALSE;
18725 	boolean_t	ire_added = B_FALSE;
18726 	boolean_t	loopback = B_FALSE;
18727 
18728 	ip1dbg(("ipif_up_done(%s:%u)\n",
18729 		ipif->ipif_ill->ill_name, ipif->ipif_id));
18730 	/* Check if this is a loopback interface */
18731 	if (ipif->ipif_ill->ill_wq == NULL)
18732 		loopback = B_TRUE;
18733 
18734 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
18735 	/*
18736 	 * If all other interfaces for this ill are down or DEPRECATED,
18737 	 * or otherwise unsuitable for source address selection, remove
18738 	 * any IRE_CACHE entries for this ill to make sure source
18739 	 * address selection gets to take this new ipif into account.
18740 	 * No need to hold ill_lock while traversing the ipif list since
18741 	 * we are writer
18742 	 */
18743 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
18744 		tmp_ipif = tmp_ipif->ipif_next) {
18745 		if (((tmp_ipif->ipif_flags &
18746 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
18747 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
18748 		    (tmp_ipif == ipif))
18749 			continue;
18750 		/* first useable pre-existing interface */
18751 		flush_ire_cache = B_FALSE;
18752 		break;
18753 	}
18754 	if (flush_ire_cache)
18755 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
18756 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
18757 
18758 	/*
18759 	 * Figure out which way the send-to queue should go.  Only
18760 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
18761 	 * should show up here.
18762 	 */
18763 	switch (ill->ill_net_type) {
18764 	case IRE_IF_RESOLVER:
18765 		stq = ill->ill_rq;
18766 		break;
18767 	case IRE_IF_NORESOLVER:
18768 	case IRE_LOOPBACK:
18769 		stq = ill->ill_wq;
18770 		break;
18771 	default:
18772 		return (EINVAL);
18773 	}
18774 
18775 	if (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) {
18776 		/*
18777 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
18778 		 * ipif_lookup_on_name(), but in the case of zones we can have
18779 		 * several loopback addresses on lo0. So all the interfaces with
18780 		 * loopback addresses need to be marked IRE_LOOPBACK.
18781 		 */
18782 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
18783 		    htonl(INADDR_LOOPBACK))
18784 			ipif->ipif_ire_type = IRE_LOOPBACK;
18785 		else
18786 			ipif->ipif_ire_type = IRE_LOCAL;
18787 	}
18788 
18789 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
18790 		/*
18791 		 * Can't use our source address. Select a different
18792 		 * source address for the IRE_INTERFACE and IRE_LOCAL
18793 		 */
18794 		src_ipif = ipif_select_source(ipif->ipif_ill,
18795 		    ipif->ipif_subnet, ipif->ipif_zoneid);
18796 		if (src_ipif == NULL)
18797 			src_ipif = ipif;	/* Last resort */
18798 		else
18799 			src_ipif_held = B_TRUE;
18800 	} else {
18801 		src_ipif = ipif;
18802 	}
18803 
18804 	/* Create all the IREs associated with this interface */
18805 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
18806 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18807 		/* Register the source address for __sin6_src_id */
18808 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
18809 		    ipif->ipif_zoneid);
18810 		if (err != 0) {
18811 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
18812 			return (err);
18813 		}
18814 		/* If the interface address is set, create the local IRE. */
18815 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
18816 			(void *)ipif,
18817 			ipif->ipif_ire_type,
18818 			ntohl(ipif->ipif_lcl_addr)));
18819 		*irep++ = ire_create(
18820 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
18821 		    (uchar_t *)&ip_g_all_ones,		/* mask */
18822 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
18823 		    NULL,				/* no gateway */
18824 		    NULL,
18825 		    &ip_loopback_mtuplus,		/* max frag size */
18826 		    NULL,
18827 		    ipif->ipif_rq,			/* recv-from queue */
18828 		    NULL,				/* no send-to queue */
18829 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
18830 		    NULL,
18831 		    ipif,
18832 		    NULL,
18833 		    0,
18834 		    0,
18835 		    0,
18836 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
18837 		    RTF_PRIVATE : 0,
18838 		    &ire_uinfo_null);
18839 	} else {
18840 		ip1dbg((
18841 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
18842 		    ipif->ipif_ire_type,
18843 		    ntohl(ipif->ipif_lcl_addr),
18844 		    (uint_t)ipif->ipif_flags));
18845 	}
18846 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
18847 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18848 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
18849 	} else {
18850 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
18851 	}
18852 
18853 	subnet_mask = ipif->ipif_net_mask;
18854 
18855 	/*
18856 	 * If mask was not specified, use natural netmask of
18857 	 * interface address. Also, store this mask back into the
18858 	 * ipif struct.
18859 	 */
18860 	if (subnet_mask == 0) {
18861 		subnet_mask = net_mask;
18862 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
18863 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
18864 		    ipif->ipif_v6subnet);
18865 	}
18866 
18867 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
18868 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
18869 	    ipif->ipif_subnet != INADDR_ANY) {
18870 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
18871 
18872 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18873 			route_mask = IP_HOST_MASK;
18874 		} else {
18875 			route_mask = subnet_mask;
18876 		}
18877 
18878 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
18879 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
18880 			(void *)ipif, (void *)ill,
18881 			ill->ill_net_type,
18882 			ntohl(ipif->ipif_subnet)));
18883 		*irep++ = ire_create(
18884 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
18885 		    (uchar_t *)&route_mask,		/* mask */
18886 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
18887 		    NULL,				/* no gateway */
18888 		    NULL,
18889 		    &ipif->ipif_mtu,			/* max frag */
18890 		    NULL,
18891 		    NULL,				/* no recv queue */
18892 		    stq,				/* send-to queue */
18893 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
18894 		    ill->ill_resolver_mp,		/* xmit header */
18895 		    ipif,
18896 		    NULL,
18897 		    0,
18898 		    0,
18899 		    0,
18900 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
18901 		    &ire_uinfo_null);
18902 	}
18903 
18904 	/*
18905 	 * If the interface address is set, create the broadcast IREs.
18906 	 *
18907 	 * ire_create_bcast checks if the proposed new IRE matches
18908 	 * any existing IRE's with the same physical interface (ILL).
18909 	 * This should get rid of duplicates.
18910 	 * ire_create_bcast also check IPIF_NOXMIT and does not create
18911 	 * any broadcast ires.
18912 	 */
18913 	if ((ipif->ipif_subnet != INADDR_ANY) &&
18914 	    (ipif->ipif_flags & IPIF_BROADCAST)) {
18915 		ipaddr_t addr;
18916 
18917 		ip1dbg(("ipif_up_done: creating broadcast IRE\n"));
18918 		irep = ire_check_and_create_bcast(ipif, 0, irep,
18919 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
18920 		irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep,
18921 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
18922 
18923 		/*
18924 		 * For backward compatibility, we need to create net
18925 		 * broadcast ire's based on the old "IP address class
18926 		 * system."  The reason is that some old machines only
18927 		 * respond to these class derived net broadcast.
18928 		 *
18929 		 * But we should not create these net broadcast ire's if
18930 		 * the subnet_mask is shorter than the IP address class based
18931 		 * derived netmask.  Otherwise, we may create a net
18932 		 * broadcast address which is the same as an IP address
18933 		 * on the subnet.  Then TCP will refuse to talk to that
18934 		 * address.
18935 		 *
18936 		 * Nor do we need IRE_BROADCAST ire's for the interface
18937 		 * with the netmask as 0xFFFFFFFF, as IRE_LOCAL for that
18938 		 * interface is already created.  Creating these broadcast
18939 		 * ire's will only create confusion as the "addr" is going
18940 		 * to be same as that of the IP address of the interface.
18941 		 */
18942 		if (net_mask < subnet_mask) {
18943 			addr = net_mask & ipif->ipif_subnet;
18944 			irep = ire_check_and_create_bcast(ipif, addr, irep,
18945 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
18946 			irep = ire_check_and_create_bcast(ipif,
18947 			    ~net_mask | addr, irep,
18948 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
18949 		}
18950 
18951 		if (subnet_mask != 0xFFFFFFFF) {
18952 			addr = ipif->ipif_subnet;
18953 			irep = ire_check_and_create_bcast(ipif, addr, irep,
18954 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
18955 			irep = ire_check_and_create_bcast(ipif,
18956 			    ~subnet_mask|addr, irep,
18957 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
18958 		}
18959 	}
18960 
18961 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
18962 
18963 	/* If an earlier ire_create failed, get out now */
18964 	for (irep1 = irep; irep1 > ire_array; ) {
18965 		irep1--;
18966 		if (*irep1 == NULL) {
18967 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
18968 			err = ENOMEM;
18969 			goto bad;
18970 		}
18971 	}
18972 
18973 	/*
18974 	 * Need to atomically check for ip_addr_availablity_check
18975 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
18976 	 * from group also.The ill_g_lock is grabbed as reader
18977 	 * just to make sure no new ills or new ipifs are being added
18978 	 * to the system while we are checking the uniqueness of addresses.
18979 	 */
18980 	rw_enter(&ill_g_lock, RW_READER);
18981 	mutex_enter(&ip_addr_avail_lock);
18982 	/* Mark it up, and increment counters. */
18983 	ill->ill_ipif_up_count++;
18984 	ipif->ipif_flags |= IPIF_UP;
18985 	err = ip_addr_availability_check(ipif);
18986 	mutex_exit(&ip_addr_avail_lock);
18987 	rw_exit(&ill_g_lock);
18988 
18989 	if (err != 0) {
18990 		/*
18991 		 * Our address may already be up on the same ill. In this case,
18992 		 * the ARP entry for our ipif replaced the one for the other
18993 		 * ipif. So we don't want to delete it (otherwise the other ipif
18994 		 * would be unable to send packets).
18995 		 * ip_addr_availability_check() identifies this case for us and
18996 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
18997 		 * which is the expected error code.
18998 		 */
18999 		if (err == EADDRINUSE) {
19000 			freemsg(ipif->ipif_arp_del_mp);
19001 			ipif->ipif_arp_del_mp = NULL;
19002 			err = EADDRNOTAVAIL;
19003 		}
19004 		ill->ill_ipif_up_count--;
19005 		ipif->ipif_flags &= ~IPIF_UP;
19006 		goto bad;
19007 	}
19008 
19009 	/*
19010 	 * Add in all newly created IREs.  ire_create_bcast() has
19011 	 * already checked for duplicates of the IRE_BROADCAST type.
19012 	 * We want to add before we call ifgrp_insert which wants
19013 	 * to know whether IRE_IF_RESOLVER exists or not.
19014 	 *
19015 	 * NOTE : We refrele the ire though we may branch to "bad"
19016 	 *	  later on where we do ire_delete. This is okay
19017 	 *	  because nobody can delete it as we are running
19018 	 *	  exclusively.
19019 	 */
19020 	for (irep1 = irep; irep1 > ire_array; ) {
19021 		irep1--;
19022 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
19023 		/*
19024 		 * refheld by ire_add. refele towards the end of the func
19025 		 */
19026 		(void) ire_add(irep1, NULL, NULL, NULL);
19027 	}
19028 	ire_added = B_TRUE;
19029 	/*
19030 	 * Form groups if possible.
19031 	 *
19032 	 * If we are supposed to be in a ill_group with a name, insert it
19033 	 * now as we know that at least one ipif is UP. Otherwise form
19034 	 * nameless groups.
19035 	 *
19036 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
19037 	 * this ipif into the appropriate interface group, or create a
19038 	 * new one. If this is already in a nameless group, we try to form
19039 	 * a bigger group looking at other ills potentially sharing this
19040 	 * ipif's prefix.
19041 	 */
19042 	phyi = ill->ill_phyint;
19043 	if (phyi->phyint_groupname_len != 0) {
19044 		ASSERT(phyi->phyint_groupname != NULL);
19045 		if (ill->ill_ipif_up_count == 1) {
19046 			ASSERT(ill->ill_group == NULL);
19047 			err = illgrp_insert(&illgrp_head_v4, ill,
19048 			    phyi->phyint_groupname, NULL, B_TRUE);
19049 			if (err != 0) {
19050 				ip1dbg(("ipif_up_done: illgrp allocation "
19051 				    "failed, error %d\n", err));
19052 				goto bad;
19053 			}
19054 		}
19055 		ASSERT(ill->ill_group != NULL);
19056 	}
19057 
19058 	/*
19059 	 * When this is part of group, we need to make sure that
19060 	 * any broadcast ires created because of this ipif coming
19061 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
19062 	 * so that we don't receive duplicate broadcast packets.
19063 	 */
19064 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
19065 		ipif_renominate_bcast(ipif);
19066 
19067 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
19068 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
19069 	ipif_saved_irep = ipif_recover_ire(ipif);
19070 
19071 	if (!loopback) {
19072 		/*
19073 		 * If the broadcast address has been set, make sure it makes
19074 		 * sense based on the interface address.
19075 		 * Only match on ill since we are sharing broadcast addresses.
19076 		 */
19077 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
19078 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
19079 			ire_t	*ire;
19080 
19081 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
19082 			    IRE_BROADCAST, ipif, ALL_ZONES,
19083 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19084 
19085 			if (ire == NULL) {
19086 				/*
19087 				 * If there isn't a matching broadcast IRE,
19088 				 * revert to the default for this netmask.
19089 				 */
19090 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
19091 				mutex_enter(&ipif->ipif_ill->ill_lock);
19092 				ipif_set_default(ipif);
19093 				mutex_exit(&ipif->ipif_ill->ill_lock);
19094 			} else {
19095 				ire_refrele(ire);
19096 			}
19097 		}
19098 
19099 	}
19100 
19101 
19102 	/* This is the first interface on this ill */
19103 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
19104 		/*
19105 		 * Need to recover all multicast memberships in the driver.
19106 		 * This had to be deferred until we had attached.
19107 		 */
19108 		ill_recover_multicast(ill);
19109 	}
19110 	/* Join the allhosts multicast address */
19111 	ipif_multicast_up(ipif);
19112 
19113 	if (!loopback) {
19114 		/*
19115 		 * See whether anybody else would benefit from the
19116 		 * new ipif that we added. We call this always rather
19117 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
19118 		 * ipif is for the benefit of illgrp_insert (done above)
19119 		 * which does not do source address selection as it does
19120 		 * not want to re-create interface routes that we are
19121 		 * having reference to it here.
19122 		 */
19123 		ill_update_source_selection(ill);
19124 	}
19125 
19126 	for (irep1 = irep; irep1 > ire_array; ) {
19127 		irep1--;
19128 		if (*irep1 != NULL) {
19129 			/* was held in ire_add */
19130 			ire_refrele(*irep1);
19131 		}
19132 	}
19133 
19134 	cnt = ipif_saved_ire_cnt;
19135 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
19136 		if (*irep1 != NULL) {
19137 			/* was held in ire_add */
19138 			ire_refrele(*irep1);
19139 		}
19140 	}
19141 
19142 	/*
19143 	 * This had to be deferred until we had bound.
19144 	 * tell routing sockets that this interface is up
19145 	 */
19146 	ip_rts_ifmsg(ipif);
19147 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
19148 
19149 	if (!loopback) {
19150 		/* Broadcast an address mask reply. */
19151 		ipif_mask_reply(ipif);
19152 	}
19153 	if (ipif_saved_irep != NULL) {
19154 		kmem_free(ipif_saved_irep,
19155 		    ipif_saved_ire_cnt * sizeof (ire_t *));
19156 	}
19157 	if (src_ipif_held)
19158 		ipif_refrele(src_ipif);
19159 	/* Let SCTP update the status for this ipif */
19160 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
19161 	return (0);
19162 
19163 bad:
19164 	ip1dbg(("ipif_up_done: FAILED \n"));
19165 	/*
19166 	 * We don't have to bother removing from ill groups because
19167 	 *
19168 	 * 1) For groups with names, we insert only when the first ipif
19169 	 *    comes up. In that case if it fails, it will not be in any
19170 	 *    group. So, we need not try to remove for that case.
19171 	 *
19172 	 * 2) For groups without names, either we tried to insert ipif_ill
19173 	 *    in a group as singleton or found some other group to become
19174 	 *    a bigger group. For the former, if it fails we don't have
19175 	 *    anything to do as ipif_ill is not in the group and for the
19176 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
19177 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
19178 	 */
19179 	while (irep > ire_array) {
19180 		irep--;
19181 		if (*irep != NULL) {
19182 			ire_delete(*irep);
19183 			if (ire_added)
19184 				ire_refrele(*irep);
19185 		}
19186 	}
19187 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid);
19188 
19189 	if (ipif_saved_irep != NULL) {
19190 		kmem_free(ipif_saved_irep,
19191 		    ipif_saved_ire_cnt * sizeof (ire_t *));
19192 	}
19193 	if (src_ipif_held)
19194 		ipif_refrele(src_ipif);
19195 
19196 	ipif_arp_down(ipif);
19197 	return (err);
19198 }
19199 
19200 /*
19201  * Turn off the ARP with the ILLF_NOARP flag.
19202  */
19203 static int
19204 ill_arp_off(ill_t *ill)
19205 {
19206 	mblk_t	*arp_off_mp = NULL;
19207 	mblk_t	*arp_on_mp = NULL;
19208 
19209 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
19210 
19211 	ASSERT(IAM_WRITER_ILL(ill));
19212 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
19213 
19214 	/*
19215 	 * If the on message is still around we've already done
19216 	 * an arp_off without doing an arp_on thus there is no
19217 	 * work needed.
19218 	 */
19219 	if (ill->ill_arp_on_mp != NULL)
19220 		return (0);
19221 
19222 	/*
19223 	 * Allocate an ARP on message (to be saved) and an ARP off message
19224 	 */
19225 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
19226 	if (!arp_off_mp)
19227 		return (ENOMEM);
19228 
19229 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
19230 	if (!arp_on_mp)
19231 		goto failed;
19232 
19233 	ASSERT(ill->ill_arp_on_mp == NULL);
19234 	ill->ill_arp_on_mp = arp_on_mp;
19235 
19236 	/* Send an AR_INTERFACE_OFF request */
19237 	putnext(ill->ill_rq, arp_off_mp);
19238 	return (0);
19239 failed:
19240 
19241 	if (arp_off_mp)
19242 		freemsg(arp_off_mp);
19243 	return (ENOMEM);
19244 }
19245 
19246 /*
19247  * Turn on ARP by turning off the ILLF_NOARP flag.
19248  */
19249 static int
19250 ill_arp_on(ill_t *ill)
19251 {
19252 	mblk_t	*mp;
19253 
19254 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
19255 
19256 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
19257 
19258 	ASSERT(IAM_WRITER_ILL(ill));
19259 	/*
19260 	 * Send an AR_INTERFACE_ON request if we have already done
19261 	 * an arp_off (which allocated the message).
19262 	 */
19263 	if (ill->ill_arp_on_mp != NULL) {
19264 		mp = ill->ill_arp_on_mp;
19265 		ill->ill_arp_on_mp = NULL;
19266 		putnext(ill->ill_rq, mp);
19267 	}
19268 	return (0);
19269 }
19270 
19271 /*
19272  * Called after either deleting ill from the group or when setting
19273  * FAILED or STANDBY on the interface.
19274  */
19275 static void
19276 illgrp_reset_schednext(ill_t *ill)
19277 {
19278 	ill_group_t *illgrp;
19279 	ill_t *save_ill;
19280 
19281 	ASSERT(IAM_WRITER_ILL(ill));
19282 	/*
19283 	 * When called from illgrp_delete, ill_group will be non-NULL.
19284 	 * But when called from ip_sioctl_flags, it could be NULL if
19285 	 * somebody is setting FAILED/INACTIVE on some interface which
19286 	 * is not part of a group.
19287 	 */
19288 	illgrp = ill->ill_group;
19289 	if (illgrp == NULL)
19290 		return;
19291 	if (illgrp->illgrp_ill_schednext != ill)
19292 		return;
19293 
19294 	illgrp->illgrp_ill_schednext = NULL;
19295 	save_ill = ill;
19296 	/*
19297 	 * Choose a good ill to be the next one for
19298 	 * outbound traffic. As the flags FAILED/STANDBY is
19299 	 * not yet marked when called from ip_sioctl_flags,
19300 	 * we check for ill separately.
19301 	 */
19302 	for (ill = illgrp->illgrp_ill; ill != NULL;
19303 	    ill = ill->ill_group_next) {
19304 		if ((ill != save_ill) &&
19305 		    !(ill->ill_phyint->phyint_flags &
19306 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
19307 			illgrp->illgrp_ill_schednext = ill;
19308 			return;
19309 		}
19310 	}
19311 }
19312 
19313 /*
19314  * Given an ill, find the next ill in the group to be scheduled.
19315  * (This should be called by ip_newroute() before ire_create().)
19316  * The passed in ill may be pulled out of the group, after we have picked
19317  * up a different outgoing ill from the same group. However ire add will
19318  * atomically check this.
19319  */
19320 ill_t *
19321 illgrp_scheduler(ill_t *ill)
19322 {
19323 	ill_t *retill;
19324 	ill_group_t *illgrp;
19325 	int illcnt;
19326 	int i;
19327 	uint64_t flags;
19328 
19329 	/*
19330 	 * We don't use a lock to check for the ill_group. If this ill
19331 	 * is currently being inserted we may end up just returning this
19332 	 * ill itself. That is ok.
19333 	 */
19334 	if (ill->ill_group == NULL) {
19335 		ill_refhold(ill);
19336 		return (ill);
19337 	}
19338 
19339 	/*
19340 	 * Grab the ill_g_lock as reader to make sure we are dealing with
19341 	 * a set of stable ills. No ill can be added or deleted or change
19342 	 * group while we hold the reader lock.
19343 	 */
19344 	rw_enter(&ill_g_lock, RW_READER);
19345 	if ((illgrp = ill->ill_group) == NULL) {
19346 		rw_exit(&ill_g_lock);
19347 		ill_refhold(ill);
19348 		return (ill);
19349 	}
19350 
19351 	illcnt = illgrp->illgrp_ill_count;
19352 	mutex_enter(&illgrp->illgrp_lock);
19353 	retill = illgrp->illgrp_ill_schednext;
19354 
19355 	if (retill == NULL)
19356 		retill = illgrp->illgrp_ill;
19357 
19358 	/*
19359 	 * We do a circular search beginning at illgrp_ill_schednext
19360 	 * or illgrp_ill. We don't check the flags against the ill lock
19361 	 * since it can change anytime. The ire creation will be atomic
19362 	 * and will fail if the ill is FAILED or OFFLINE.
19363 	 */
19364 	for (i = 0; i < illcnt; i++) {
19365 		flags = retill->ill_phyint->phyint_flags;
19366 
19367 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
19368 		    ILL_CAN_LOOKUP(retill)) {
19369 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
19370 			ill_refhold(retill);
19371 			break;
19372 		}
19373 		retill = retill->ill_group_next;
19374 		if (retill == NULL)
19375 			retill = illgrp->illgrp_ill;
19376 	}
19377 	mutex_exit(&illgrp->illgrp_lock);
19378 	rw_exit(&ill_g_lock);
19379 
19380 	return (i == illcnt ? NULL : retill);
19381 }
19382 
19383 /*
19384  * Checks for availbility of a usable source address (if there is one) when the
19385  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
19386  * this selection is done regardless of the destination.
19387  */
19388 boolean_t
19389 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
19390 {
19391 	uint_t	ifindex;
19392 	ipif_t	*ipif = NULL;
19393 	ill_t	*uill;
19394 	boolean_t isv6;
19395 
19396 	ASSERT(ill != NULL);
19397 
19398 	isv6 = ill->ill_isv6;
19399 	ifindex = ill->ill_usesrc_ifindex;
19400 	if (ifindex != 0) {
19401 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
19402 		    NULL);
19403 		if (uill == NULL)
19404 			return (NULL);
19405 		mutex_enter(&uill->ill_lock);
19406 		for (ipif = uill->ill_ipif; ipif != NULL;
19407 		    ipif = ipif->ipif_next) {
19408 			if (!IPIF_CAN_LOOKUP(ipif))
19409 				continue;
19410 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
19411 				continue;
19412 			if (!(ipif->ipif_flags & IPIF_UP))
19413 				continue;
19414 			if (ipif->ipif_zoneid != zoneid)
19415 				continue;
19416 			if ((isv6 &&
19417 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
19418 			    (ipif->ipif_lcl_addr == INADDR_ANY))
19419 				continue;
19420 			mutex_exit(&uill->ill_lock);
19421 			ill_refrele(uill);
19422 			return (B_TRUE);
19423 		}
19424 		mutex_exit(&uill->ill_lock);
19425 		ill_refrele(uill);
19426 	}
19427 	return (B_FALSE);
19428 }
19429 
19430 /*
19431  * Determine the best source address given a destination address and an ill.
19432  * Prefers non-deprecated over deprecated but will return a deprecated
19433  * address if there is no other choice. If there is a usable source address
19434  * on the interface pointed to by ill_usesrc_ifindex then that is given
19435  * first preference.
19436  *
19437  * Returns NULL if there is no suitable source address for the ill.
19438  * This only occurs when there is no valid source address for the ill.
19439  */
19440 ipif_t *
19441 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
19442 {
19443 	ipif_t *ipif;
19444 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
19445 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
19446 	int index = 0;
19447 	boolean_t wrapped = B_FALSE;
19448 	boolean_t same_subnet_only = B_FALSE;
19449 	boolean_t ipif_same_found, ipif_other_found;
19450 	ill_t	*till, *usill = NULL;
19451 
19452 	if (ill->ill_usesrc_ifindex != 0) {
19453 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex, B_FALSE,
19454 		    NULL, NULL, NULL, NULL);
19455 		if (usill != NULL)
19456 			ill = usill;	/* Select source from usesrc ILL */
19457 		else
19458 			return (NULL);
19459 	}
19460 
19461 	/*
19462 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
19463 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
19464 	 * After selecting the right ipif, under ill_lock make sure ipif is
19465 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
19466 	 * we retry. Inside the loop we still need to check for CONDEMNED,
19467 	 * but not under a lock.
19468 	 */
19469 	rw_enter(&ill_g_lock, RW_READER);
19470 
19471 retry:
19472 	till = ill;
19473 	ipif_arr[0] = NULL;
19474 
19475 	if (till->ill_group != NULL)
19476 		till = till->ill_group->illgrp_ill;
19477 
19478 	/*
19479 	 * Choose one good source address from each ill across the group.
19480 	 * If possible choose a source address in the same subnet as
19481 	 * the destination address.
19482 	 *
19483 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
19484 	 * This is okay because of the following.
19485 	 *
19486 	 *    If PHYI_FAILED is set and we still have non-deprecated
19487 	 *    addresses, it means the addresses have not yet been
19488 	 *    failed over to a different interface. We potentially
19489 	 *    select them to create IRE_CACHES, which will be later
19490 	 *    flushed when the addresses move over.
19491 	 *
19492 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
19493 	 *    addresses, it means either the user has configured them
19494 	 *    or PHYI_INACTIVE has not been cleared after the addresses
19495 	 *    been moved over. For the former, in.mpathd does a failover
19496 	 *    when the interface becomes INACTIVE and hence we should
19497 	 *    not find them. Once INACTIVE is set, we don't allow them
19498 	 *    to create logical interfaces anymore. For the latter, a
19499 	 *    flush will happen when INACTIVE is cleared which will
19500 	 *    flush the IRE_CACHES.
19501 	 *
19502 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
19503 	 *    over soon. We potentially select them to create IRE_CACHEs,
19504 	 *    which will be later flushed when the addresses move over.
19505 	 *
19506 	 * NOTE : As ipif_select_source is called to borrow source address
19507 	 * for an ipif that is part of a group, source address selection
19508 	 * will be re-done whenever the group changes i.e either an
19509 	 * insertion/deletion in the group.
19510 	 *
19511 	 * Fill ipif_arr[] with source addresses, using these rules:
19512 	 *
19513 	 *	1. At most one source address from a given ill ends up
19514 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
19515 	 *	   associated with a given ill ends up in ipif_arr[].
19516 	 *
19517 	 *	2. If there is at least one non-deprecated ipif in the
19518 	 *	   IPMP group with a source address on the same subnet as
19519 	 *	   our destination, then fill ipif_arr[] only with
19520 	 *	   source addresses on the same subnet as our destination.
19521 	 *	   Note that because of (1), only the first
19522 	 *	   non-deprecated ipif found with a source address
19523 	 *	   matching the destination ends up in ipif_arr[].
19524 	 *
19525 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
19526 	 *	   addresses not in the same subnet as our destination.
19527 	 *	   Again, because of (1), only the first off-subnet source
19528 	 *	   address will be chosen.
19529 	 *
19530 	 *	4. If there are no non-deprecated ipifs, then just use
19531 	 *	   the source address associated with the last deprecated
19532 	 *	   one we find that happens to be on the same subnet,
19533 	 *	   otherwise the first one not in the same subnet.
19534 	 */
19535 	for (; till != NULL; till = till->ill_group_next) {
19536 		ipif_same_found = B_FALSE;
19537 		ipif_other_found = B_FALSE;
19538 		for (ipif = till->ill_ipif; ipif != NULL;
19539 		    ipif = ipif->ipif_next) {
19540 			if (!IPIF_CAN_LOOKUP(ipif))
19541 				continue;
19542 			/* Always skip NOLOCAL and ANYCAST interfaces */
19543 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
19544 				continue;
19545 			if (!(ipif->ipif_flags & IPIF_UP))
19546 				continue;
19547 			if (ipif->ipif_zoneid != zoneid)
19548 				continue;
19549 			/*
19550 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
19551 			 * but are not valid as source addresses.
19552 			 */
19553 			if (ipif->ipif_lcl_addr == INADDR_ANY)
19554 				continue;
19555 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
19556 				if (ipif_dep == NULL ||
19557 				    (ipif->ipif_net_mask & dst) ==
19558 				    ipif->ipif_subnet)
19559 					ipif_dep = ipif;
19560 				continue;
19561 			}
19562 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
19563 				/* found a source address in the same subnet */
19564 				if (same_subnet_only == B_FALSE) {
19565 					same_subnet_only = B_TRUE;
19566 					index = 0;
19567 				}
19568 				ipif_same_found = B_TRUE;
19569 			} else {
19570 				if (same_subnet_only == B_TRUE ||
19571 				    ipif_other_found == B_TRUE)
19572 					continue;
19573 				ipif_other_found = B_TRUE;
19574 			}
19575 			ipif_arr[index++] = ipif;
19576 			if (index == MAX_IPIF_SELECT_SOURCE) {
19577 				wrapped = B_TRUE;
19578 				index = 0;
19579 			}
19580 			if (ipif_same_found == B_TRUE)
19581 				break;
19582 		}
19583 	}
19584 
19585 	if (ipif_arr[0] == NULL) {
19586 		ipif = ipif_dep;
19587 	} else {
19588 		if (wrapped)
19589 			index = MAX_IPIF_SELECT_SOURCE;
19590 		ipif = ipif_arr[ipif_rand() % index];
19591 		ASSERT(ipif != NULL);
19592 	}
19593 
19594 	if (ipif != NULL) {
19595 		mutex_enter(&ipif->ipif_ill->ill_lock);
19596 		if (!IPIF_CAN_LOOKUP(ipif)) {
19597 			mutex_exit(&ipif->ipif_ill->ill_lock);
19598 			goto retry;
19599 		}
19600 		ipif_refhold_locked(ipif);
19601 		mutex_exit(&ipif->ipif_ill->ill_lock);
19602 	}
19603 
19604 	rw_exit(&ill_g_lock);
19605 	if (usill != NULL)
19606 		ill_refrele(usill);
19607 
19608 #ifdef DEBUG
19609 	if (ipif == NULL) {
19610 		char buf1[INET6_ADDRSTRLEN];
19611 
19612 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
19613 		    ill->ill_name,
19614 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
19615 	} else {
19616 		char buf1[INET6_ADDRSTRLEN];
19617 		char buf2[INET6_ADDRSTRLEN];
19618 
19619 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
19620 		    ipif->ipif_ill->ill_name,
19621 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
19622 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
19623 		    buf2, sizeof (buf2))));
19624 	}
19625 #endif /* DEBUG */
19626 	return (ipif);
19627 }
19628 
19629 
19630 /*
19631  * If old_ipif is not NULL, see if ipif was derived from old
19632  * ipif and if so, recreate the interface route by re-doing
19633  * source address selection. This happens when ipif_down ->
19634  * ipif_update_other_ipifs calls us.
19635  *
19636  * If old_ipif is NULL, just redo the source address selection
19637  * if needed. This happens when illgrp_insert or ipif_up_done
19638  * calls us.
19639  */
19640 static void
19641 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
19642 {
19643 	ire_t *ire;
19644 	ire_t *ipif_ire;
19645 	queue_t *stq;
19646 	ipif_t *nipif;
19647 	ill_t *ill;
19648 	boolean_t need_rele = B_FALSE;
19649 
19650 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
19651 	ASSERT(IAM_WRITER_IPIF(ipif));
19652 
19653 	ill = ipif->ipif_ill;
19654 	if (!(ipif->ipif_flags &
19655 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
19656 		/*
19657 		 * Can't possibly have borrowed the source
19658 		 * from old_ipif.
19659 		 */
19660 		return;
19661 	}
19662 
19663 	/*
19664 	 * Is there any work to be done? No work if the address
19665 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
19666 	 * ipif_select_source() does not borrow addresses from
19667 	 * NOLOCAL and ANYCAST interfaces).
19668 	 */
19669 	if ((old_ipif != NULL) &&
19670 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
19671 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
19672 	    (old_ipif->ipif_flags &
19673 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
19674 		return;
19675 	}
19676 
19677 	/*
19678 	 * Perform the same checks as when creating the
19679 	 * IRE_INTERFACE in ipif_up_done.
19680 	 */
19681 	if (!(ipif->ipif_flags & IPIF_UP))
19682 		return;
19683 
19684 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
19685 	    (ipif->ipif_subnet == INADDR_ANY))
19686 		return;
19687 
19688 	ipif_ire = ipif_to_ire(ipif);
19689 	if (ipif_ire == NULL)
19690 		return;
19691 
19692 	/*
19693 	 * We know that ipif uses some other source for its
19694 	 * IRE_INTERFACE. Is it using the source of this
19695 	 * old_ipif?
19696 	 */
19697 	if (old_ipif != NULL &&
19698 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
19699 		ire_refrele(ipif_ire);
19700 		return;
19701 	}
19702 	if (ip_debug > 2) {
19703 		/* ip1dbg */
19704 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
19705 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
19706 	}
19707 
19708 	stq = ipif_ire->ire_stq;
19709 
19710 	/*
19711 	 * Can't use our source address. Select a different
19712 	 * source address for the IRE_INTERFACE.
19713 	 */
19714 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
19715 	if (nipif == NULL) {
19716 		/* Last resort - all ipif's have IPIF_NOLOCAL */
19717 		nipif = ipif;
19718 	} else {
19719 		need_rele = B_TRUE;
19720 	}
19721 
19722 	ire = ire_create(
19723 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
19724 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
19725 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
19726 	    NULL,				/* no gateway */
19727 	    NULL,
19728 	    &ipif->ipif_mtu,			/* max frag */
19729 	    NULL,				/* fast path header */
19730 	    NULL,				/* no recv from queue */
19731 	    stq,				/* send-to queue */
19732 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
19733 	    ill->ill_resolver_mp,		/* xmit header */
19734 	    ipif,
19735 	    NULL,
19736 	    0,
19737 	    0,
19738 	    0,
19739 	    0,
19740 	    &ire_uinfo_null);
19741 
19742 	if (ire != NULL) {
19743 		ire_t *ret_ire;
19744 		int error;
19745 
19746 		/*
19747 		 * We don't need ipif_ire anymore. We need to delete
19748 		 * before we add so that ire_add does not detect
19749 		 * duplicates.
19750 		 */
19751 		ire_delete(ipif_ire);
19752 		ret_ire = ire;
19753 		error = ire_add(&ret_ire, NULL, NULL, NULL);
19754 		ASSERT(error == 0);
19755 		ASSERT(ire == ret_ire);
19756 		/* Held in ire_add */
19757 		ire_refrele(ret_ire);
19758 	}
19759 	/*
19760 	 * Either we are falling through from above or could not
19761 	 * allocate a replacement.
19762 	 */
19763 	ire_refrele(ipif_ire);
19764 	if (need_rele)
19765 		ipif_refrele(nipif);
19766 }
19767 
19768 /*
19769  * This old_ipif is going away.
19770  *
19771  * Determine if any other ipif's is using our address as
19772  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
19773  * IPIF_DEPRECATED).
19774  * Find the IRE_INTERFACE for such ipifs and recreate them
19775  * to use an different source address following the rules in
19776  * ipif_up_done.
19777  *
19778  * This function takes an illgrp as an argument so that illgrp_delete
19779  * can call this to update source address even after deleting the
19780  * old_ipif->ipif_ill from the ill group.
19781  */
19782 static void
19783 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
19784 {
19785 	ipif_t *ipif;
19786 	ill_t *ill;
19787 	char	buf[INET6_ADDRSTRLEN];
19788 
19789 	ASSERT(IAM_WRITER_IPIF(old_ipif));
19790 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
19791 
19792 	ill = old_ipif->ipif_ill;
19793 
19794 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
19795 	    ill->ill_name,
19796 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
19797 	    buf, sizeof (buf))));
19798 	/*
19799 	 * If this part of a group, look at all ills as ipif_select_source
19800 	 * borrows source address across all the ills in the group.
19801 	 */
19802 	if (illgrp != NULL)
19803 		ill = illgrp->illgrp_ill;
19804 
19805 	for (; ill != NULL; ill = ill->ill_group_next) {
19806 		for (ipif = ill->ill_ipif; ipif != NULL;
19807 		    ipif = ipif->ipif_next) {
19808 
19809 			if (ipif == old_ipif)
19810 				continue;
19811 
19812 			ipif_recreate_interface_routes(old_ipif, ipif);
19813 		}
19814 	}
19815 }
19816 
19817 /* ARGSUSED */
19818 int
19819 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
19820 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
19821 {
19822 	/*
19823 	 * ill_phyint_reinit merged the v4 and v6 into a single
19824 	 * ipsq. Could also have become part of a ipmp group in the
19825 	 * process, and we might not have been able to complete the
19826 	 * operation in ipif_set_values, if we could not become
19827 	 * exclusive.  If so restart it here.
19828 	 */
19829 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
19830 }
19831 
19832 
19833 /* ARGSUSED */
19834 int
19835 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
19836     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
19837 {
19838 	queue_t		*q1 = q;
19839 	char 		*cp;
19840 	char		interf_name[LIFNAMSIZ];
19841 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
19842 
19843 	if (!q->q_next) {
19844 		ip1dbg((
19845 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
19846 		return (EINVAL);
19847 	}
19848 
19849 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
19850 		return (EALREADY);
19851 
19852 	do {
19853 		q1 = q1->q_next;
19854 	} while (q1->q_next);
19855 	cp = q1->q_qinfo->qi_minfo->mi_idname;
19856 	(void) sprintf(interf_name, "%s%d", cp, ppa);
19857 
19858 	/*
19859 	 * Here we are not going to delay the ioack until after
19860 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
19861 	 * original ioctl message before sending the requests.
19862 	 */
19863 	return (ipif_set_values(q, mp, interf_name, &ppa));
19864 }
19865 
19866 /* ARGSUSED */
19867 int
19868 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
19869     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
19870 {
19871 	return (ENXIO);
19872 }
19873 
19874 /*
19875  * Net and subnet broadcast ire's are now specific to the particular
19876  * physical interface (ill) and not to any one locigal interface (ipif).
19877  * However, if a particular logical interface is being taken down, it's
19878  * associated ire's will be taken down as well.  Hence, when we go to
19879  * take down or change the local address, broadcast address or netmask
19880  * of a specific logical interface, we must check to make sure that we
19881  * have valid net and subnet broadcast ire's for the other logical
19882  * interfaces which may have been shared with the logical interface
19883  * being brought down or changed.
19884  *
19885  * There is one set of 0.0.0.0 and 255.255.255.255 per ill. Usually it
19886  * is tied to the first interface coming UP. If that ipif is going down,
19887  * we need to recreate them on the next valid ipif.
19888  *
19889  * Note: assume that the ipif passed in is still up so that it's IRE
19890  * entries are still valid.
19891  */
19892 static void
19893 ipif_check_bcast_ires(ipif_t *test_ipif)
19894 {
19895 	ipif_t	*ipif;
19896 	ire_t	*test_subnet_ire, *test_net_ire;
19897 	ire_t	*test_allzero_ire, *test_allone_ire;
19898 	ire_t	*ire_array[12];
19899 	ire_t	**irep = &ire_array[0];
19900 	ire_t	**irep1;
19901 
19902 	ipaddr_t net_addr, subnet_addr, net_mask, subnet_mask;
19903 	ipaddr_t test_net_addr, test_subnet_addr;
19904 	ipaddr_t test_net_mask, test_subnet_mask;
19905 	boolean_t need_net_bcast_ire = B_FALSE;
19906 	boolean_t need_subnet_bcast_ire = B_FALSE;
19907 	boolean_t allzero_bcast_ire_created = B_FALSE;
19908 	boolean_t allone_bcast_ire_created = B_FALSE;
19909 	boolean_t net_bcast_ire_created = B_FALSE;
19910 	boolean_t subnet_bcast_ire_created = B_FALSE;
19911 
19912 	ipif_t  *backup_ipif_net = (ipif_t *)NULL;
19913 	ipif_t  *backup_ipif_subnet = (ipif_t *)NULL;
19914 	ipif_t  *backup_ipif_allzeros = (ipif_t *)NULL;
19915 	ipif_t  *backup_ipif_allones = (ipif_t *)NULL;
19916 	uint64_t check_flags = IPIF_DEPRECATED | IPIF_NOLOCAL | IPIF_ANYCAST;
19917 
19918 	ASSERT(!test_ipif->ipif_isv6);
19919 	ASSERT(IAM_WRITER_IPIF(test_ipif));
19920 
19921 	/*
19922 	 * No broadcast IREs for the LOOPBACK interface
19923 	 * or others such as point to point and IPIF_NOXMIT.
19924 	 */
19925 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
19926 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
19927 		return;
19928 
19929 	test_allzero_ire = ire_ctable_lookup(0, 0, IRE_BROADCAST,
19930 	    test_ipif, ALL_ZONES, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
19931 
19932 	test_allone_ire = ire_ctable_lookup(INADDR_BROADCAST, 0, IRE_BROADCAST,
19933 	    test_ipif, ALL_ZONES, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
19934 
19935 	test_net_mask = ip_net_mask(test_ipif->ipif_subnet);
19936 	test_subnet_mask = test_ipif->ipif_net_mask;
19937 
19938 	/*
19939 	 * If no net mask set, assume the default based on net class.
19940 	 */
19941 	if (test_subnet_mask == 0)
19942 		test_subnet_mask = test_net_mask;
19943 
19944 	/*
19945 	 * Check if there is a network broadcast ire associated with this ipif
19946 	 */
19947 	test_net_addr = test_net_mask  & test_ipif->ipif_subnet;
19948 	test_net_ire = ire_ctable_lookup(test_net_addr, 0, IRE_BROADCAST,
19949 	    test_ipif, ALL_ZONES, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
19950 
19951 	/*
19952 	 * Check if there is a subnet broadcast IRE associated with this ipif
19953 	 */
19954 	test_subnet_addr = test_subnet_mask  & test_ipif->ipif_subnet;
19955 	test_subnet_ire = ire_ctable_lookup(test_subnet_addr, 0, IRE_BROADCAST,
19956 	    test_ipif, ALL_ZONES, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
19957 
19958 	/*
19959 	 * No broadcast ire's associated with this ipif.
19960 	 */
19961 	if ((test_subnet_ire == NULL) && (test_net_ire == NULL) &&
19962 	    (test_allzero_ire == NULL) && (test_allone_ire == NULL)) {
19963 		return;
19964 	}
19965 
19966 	/*
19967 	 * We have established which bcast ires have to be replaced.
19968 	 * Next we try to locate ipifs that match there ires.
19969 	 * The rules are simple: If we find an ipif that matches on the subnet
19970 	 * address it will also match on the net address, the allzeros and
19971 	 * allones address. Any ipif that matches only on the net address will
19972 	 * also match the allzeros and allones addresses.
19973 	 * The other criterion is the ipif_flags. We look for non-deprecated
19974 	 * (and non-anycast and non-nolocal) ipifs as the best choice.
19975 	 * ipifs with check_flags matching (deprecated, etc) are used only
19976 	 * if good ipifs are not available. While looping, we save existing
19977 	 * deprecated ipifs as backup_ipif.
19978 	 * We loop through all the ipifs for this ill looking for ipifs
19979 	 * whose broadcast addr match the ipif passed in, but do not have
19980 	 * their own broadcast ires. For creating 0.0.0.0 and
19981 	 * 255.255.255.255 we just need an ipif on this ill to create.
19982 	 */
19983 	for (ipif = test_ipif->ipif_ill->ill_ipif; ipif != NULL;
19984 	    ipif = ipif->ipif_next) {
19985 
19986 		ASSERT(!ipif->ipif_isv6);
19987 		/*
19988 		 * Already checked the ipif passed in.
19989 		 */
19990 		if (ipif == test_ipif) {
19991 			continue;
19992 		}
19993 
19994 		/*
19995 		 * We only need to recreate broadcast ires if another ipif in
19996 		 * the same zone uses them. The new ires must be created in the
19997 		 * same zone.
19998 		 */
19999 		if (ipif->ipif_zoneid != test_ipif->ipif_zoneid) {
20000 			continue;
20001 		}
20002 
20003 		/*
20004 		 * Only interested in logical interfaces with valid local
20005 		 * addresses or with the ability to broadcast.
20006 		 */
20007 		if ((ipif->ipif_subnet == 0) ||
20008 		    !(ipif->ipif_flags & IPIF_BROADCAST) ||
20009 		    (ipif->ipif_flags & IPIF_NOXMIT) ||
20010 		    !(ipif->ipif_flags & IPIF_UP)) {
20011 			continue;
20012 		}
20013 		/*
20014 		 * Check if there is a net broadcast ire for this
20015 		 * net address.  If it turns out that the ipif we are
20016 		 * about to take down owns this ire, we must make a
20017 		 * new one because it is potentially going away.
20018 		 */
20019 		if (test_net_ire && (!net_bcast_ire_created)) {
20020 			net_mask = ip_net_mask(ipif->ipif_subnet);
20021 			net_addr = net_mask & ipif->ipif_subnet;
20022 			if (net_addr == test_net_addr) {
20023 				need_net_bcast_ire = B_TRUE;
20024 				/*
20025 				 * Use DEPRECATED ipif only if no good
20026 				 * ires are available. subnet_addr is
20027 				 * a better match than net_addr.
20028 				 */
20029 				if ((ipif->ipif_flags & check_flags) &&
20030 				    (backup_ipif_net == NULL)) {
20031 					backup_ipif_net = ipif;
20032 				}
20033 			}
20034 		}
20035 		/*
20036 		 * Check if there is a subnet broadcast ire for this
20037 		 * net address.  If it turns out that the ipif we are
20038 		 * about to take down owns this ire, we must make a
20039 		 * new one because it is potentially going away.
20040 		 */
20041 		if (test_subnet_ire && (!subnet_bcast_ire_created)) {
20042 			subnet_mask = ipif->ipif_net_mask;
20043 			subnet_addr = ipif->ipif_subnet;
20044 			if (subnet_addr == test_subnet_addr) {
20045 				need_subnet_bcast_ire = B_TRUE;
20046 				if ((ipif->ipif_flags & check_flags) &&
20047 				    (backup_ipif_subnet == NULL)) {
20048 					backup_ipif_subnet = ipif;
20049 				}
20050 			}
20051 		}
20052 
20053 
20054 		/* Short circuit here if this ipif is deprecated */
20055 		if (ipif->ipif_flags & check_flags) {
20056 			if ((test_allzero_ire != NULL) &&
20057 			    (!allzero_bcast_ire_created) &&
20058 			    (backup_ipif_allzeros == NULL)) {
20059 				backup_ipif_allzeros = ipif;
20060 			}
20061 			if ((test_allone_ire != NULL) &&
20062 			    (!allone_bcast_ire_created) &&
20063 			    (backup_ipif_allones == NULL)) {
20064 				backup_ipif_allones = ipif;
20065 			}
20066 			continue;
20067 		}
20068 
20069 		/*
20070 		 * Found an ipif which has the same broadcast ire as the
20071 		 * ipif passed in and the ipif passed in "owns" the ire.
20072 		 * Create new broadcast ire's for this broadcast addr.
20073 		 */
20074 		if (need_net_bcast_ire && !net_bcast_ire_created) {
20075 			irep = ire_create_bcast(ipif, net_addr, irep);
20076 			irep = ire_create_bcast(ipif,
20077 			    ~net_mask | net_addr, irep);
20078 			net_bcast_ire_created = B_TRUE;
20079 		}
20080 		if (need_subnet_bcast_ire && !subnet_bcast_ire_created) {
20081 			irep = ire_create_bcast(ipif, subnet_addr, irep);
20082 			irep = ire_create_bcast(ipif,
20083 			    ~subnet_mask | subnet_addr, irep);
20084 			subnet_bcast_ire_created = B_TRUE;
20085 		}
20086 		if (test_allzero_ire != NULL && !allzero_bcast_ire_created) {
20087 			irep = ire_create_bcast(ipif, 0, irep);
20088 			allzero_bcast_ire_created = B_TRUE;
20089 		}
20090 		if (test_allone_ire != NULL && !allone_bcast_ire_created) {
20091 			irep = ire_create_bcast(ipif, INADDR_BROADCAST, irep);
20092 			allone_bcast_ire_created = B_TRUE;
20093 		}
20094 		/*
20095 		 * Once we have created all the appropriate ires, we
20096 		 * just break out of this loop to add what we have created.
20097 		 * This has been indented similar to ire_match_args for
20098 		 * readability.
20099 		 */
20100 		if (((test_net_ire == NULL) ||
20101 			(net_bcast_ire_created)) &&
20102 		    ((test_subnet_ire == NULL) ||
20103 			(subnet_bcast_ire_created)) &&
20104 		    ((test_allzero_ire == NULL) ||
20105 			(allzero_bcast_ire_created)) &&
20106 		    ((test_allone_ire == NULL) ||
20107 			(allone_bcast_ire_created))) {
20108 			break;
20109 		}
20110 	}
20111 
20112 	/*
20113 	 * Create bcast ires on deprecated ipifs if no non-deprecated ipifs
20114 	 * exist. 6 pairs of bcast ires are needed.
20115 	 * Note - the old ires are deleted in ipif_down.
20116 	 */
20117 	if (need_net_bcast_ire && !net_bcast_ire_created && backup_ipif_net) {
20118 		ipif = backup_ipif_net;
20119 		irep = ire_create_bcast(ipif, net_addr, irep);
20120 		irep = ire_create_bcast(ipif, ~net_mask | net_addr, irep);
20121 		net_bcast_ire_created = B_TRUE;
20122 	}
20123 	if (need_subnet_bcast_ire && !subnet_bcast_ire_created &&
20124 	    backup_ipif_subnet) {
20125 		ipif = backup_ipif_subnet;
20126 		irep = ire_create_bcast(ipif, subnet_addr, irep);
20127 		irep = ire_create_bcast(ipif,
20128 		    ~subnet_mask | subnet_addr, irep);
20129 		subnet_bcast_ire_created = B_TRUE;
20130 	}
20131 	if (test_allzero_ire != NULL && !allzero_bcast_ire_created &&
20132 	    backup_ipif_allzeros) {
20133 		irep = ire_create_bcast(backup_ipif_allzeros, 0, irep);
20134 		allzero_bcast_ire_created = B_TRUE;
20135 	}
20136 	if (test_allone_ire != NULL && !allone_bcast_ire_created &&
20137 	    backup_ipif_allones) {
20138 		irep = ire_create_bcast(backup_ipif_allones,
20139 		    INADDR_BROADCAST, irep);
20140 		allone_bcast_ire_created = B_TRUE;
20141 	}
20142 
20143 	/*
20144 	 * If we can't create all of them, don't add any of them.
20145 	 * Code in ip_wput_ire and ire_to_ill assumes that we
20146 	 * always have a non-loopback copy and loopback copy
20147 	 * for a given address.
20148 	 */
20149 	for (irep1 = irep; irep1 > ire_array; ) {
20150 		irep1--;
20151 		if (*irep1 == NULL) {
20152 			ip0dbg(("ipif_check_bcast_ires: can't create "
20153 			    "IRE_BROADCAST, memory allocation failure\n"));
20154 			while (irep > ire_array) {
20155 				irep--;
20156 				if (*irep != NULL)
20157 					ire_delete(*irep);
20158 			}
20159 			goto bad;
20160 		}
20161 	}
20162 	for (irep1 = irep; irep1 > ire_array; ) {
20163 		int error;
20164 
20165 		irep1--;
20166 		error = ire_add(irep1, NULL, NULL, NULL);
20167 		if (error == 0) {
20168 			ire_refrele(*irep1);		/* Held in ire_add */
20169 		}
20170 	}
20171 bad:
20172 	if (test_allzero_ire != NULL)
20173 		ire_refrele(test_allzero_ire);
20174 	if (test_allone_ire != NULL)
20175 		ire_refrele(test_allone_ire);
20176 	if (test_net_ire != NULL)
20177 		ire_refrele(test_net_ire);
20178 	if (test_subnet_ire != NULL)
20179 		ire_refrele(test_subnet_ire);
20180 }
20181 
20182 /*
20183  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
20184  * from lifr_flags and the name from lifr_name.
20185  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
20186  * since ipif_lookup_on_name uses the _isv6 flags when matching.
20187  * Returns EINPROGRESS when mp has been consumed by queueing it on
20188  * ill_pending_mp and the ioctl will complete in ip_rput.
20189  */
20190 /* ARGSUSED */
20191 int
20192 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20193     ip_ioctl_cmd_t *ipip, void *if_req)
20194 {
20195 	int	err;
20196 	ill_t	*ill;
20197 	struct lifreq *lifr = (struct lifreq *)if_req;
20198 
20199 	ASSERT(ipif != NULL);
20200 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
20201 	ASSERT(q->q_next != NULL);
20202 
20203 	ill = (ill_t *)q->q_ptr;
20204 	/*
20205 	 * If we are not writer on 'q' then this interface exists already
20206 	 * and previous lookups (ipif_extract_lifreq_cmn) found this ipif.
20207 	 * So return EALREADY
20208 	 */
20209 	if (ill != ipif->ipif_ill)
20210 		return (EALREADY);
20211 
20212 	if (ill->ill_name[0] != '\0')
20213 		return (EALREADY);
20214 
20215 	/*
20216 	 * Set all the flags. Allows all kinds of override. Provide some
20217 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
20218 	 * unless there is either multicast/broadcast support in the driver
20219 	 * or it is a pt-pt link.
20220 	 */
20221 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
20222 		/* Meaningless to IP thus don't allow them to be set. */
20223 		ip1dbg(("ip_setname: EINVAL 1\n"));
20224 		return (EINVAL);
20225 	}
20226 	/*
20227 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
20228 	 * ill_bcast_addr_length info.
20229 	 */
20230 	if (!ill->ill_needs_attach &&
20231 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
20232 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
20233 	    ill->ill_bcast_addr_length == 0)) {
20234 		/* Link not broadcast/pt-pt capable i.e. no multicast */
20235 		ip1dbg(("ip_setname: EINVAL 2\n"));
20236 		return (EINVAL);
20237 	}
20238 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
20239 	    ((lifr->lifr_flags & IFF_IPV6) ||
20240 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
20241 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
20242 		ip1dbg(("ip_setname: EINVAL 3\n"));
20243 		return (EINVAL);
20244 	}
20245 	if (lifr->lifr_flags & IFF_UP) {
20246 		/* Can only be set with SIOCSLIFFLAGS */
20247 		ip1dbg(("ip_setname: EINVAL 4\n"));
20248 		return (EINVAL);
20249 	}
20250 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
20251 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
20252 		ip1dbg(("ip_setname: EINVAL 5\n"));
20253 		return (EINVAL);
20254 	}
20255 	/*
20256 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
20257 	 */
20258 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
20259 	    !(lifr->lifr_flags & IFF_IPV6) &&
20260 	    !(ipif->ipif_isv6)) {
20261 		ip1dbg(("ip_setname: EINVAL 6\n"));
20262 		return (EINVAL);
20263 	}
20264 
20265 	/*
20266 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
20267 	 * we have all the flags here. So, we assign rather than we OR.
20268 	 * We can't OR the flags here because we don't want to set
20269 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
20270 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
20271 	 * on lifr_flags value here.
20272 	 */
20273 	/*
20274 	 * This ill has not been inserted into the global list.
20275 	 * So we are still single threaded and don't need any lock
20276 	 */
20277 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS;
20278 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
20279 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
20280 
20281 	/* We started off as V4. */
20282 	if (ill->ill_flags & ILLF_IPV6) {
20283 		ill->ill_phyint->phyint_illv6 = ill;
20284 		ill->ill_phyint->phyint_illv4 = NULL;
20285 	}
20286 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
20287 	return (err);
20288 }
20289 
20290 /* ARGSUSED */
20291 int
20292 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20293     ip_ioctl_cmd_t *ipip, void *if_req)
20294 {
20295 	/*
20296 	 * ill_phyint_reinit merged the v4 and v6 into a single
20297 	 * ipsq. Could also have become part of a ipmp group in the
20298 	 * process, and we might not have been able to complete the
20299 	 * slifname in ipif_set_values, if we could not become
20300 	 * exclusive.  If so restart it here
20301 	 */
20302 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
20303 }
20304 
20305 /*
20306  * Return a pointer to the ipif which matches the index, IP version type and
20307  * zoneid.
20308  */
20309 ipif_t *
20310 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
20311     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
20312 {
20313 	ill_t	*ill;
20314 	ipsq_t  *ipsq;
20315 	phyint_t *phyi;
20316 	ipif_t	*ipif;
20317 
20318 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
20319 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
20320 
20321 	if (err != NULL)
20322 		*err = 0;
20323 
20324 	/*
20325 	 * Indexes are stored in the phyint - a common structure
20326 	 * to both IPv4 and IPv6.
20327 	 */
20328 
20329 	rw_enter(&ill_g_lock, RW_READER);
20330 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
20331 	    (void *) &index, NULL);
20332 	if (phyi != NULL) {
20333 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
20334 		if (ill == NULL) {
20335 			rw_exit(&ill_g_lock);
20336 			if (err != NULL)
20337 				*err = ENXIO;
20338 			return (NULL);
20339 		}
20340 		GRAB_CONN_LOCK(q);
20341 		mutex_enter(&ill->ill_lock);
20342 		if (ILL_CAN_LOOKUP(ill)) {
20343 			for (ipif = ill->ill_ipif; ipif != NULL;
20344 			    ipif = ipif->ipif_next) {
20345 				if (IPIF_CAN_LOOKUP(ipif) &&
20346 				    (zoneid == ALL_ZONES ||
20347 				    zoneid == ipif->ipif_zoneid)) {
20348 					ipif_refhold_locked(ipif);
20349 					mutex_exit(&ill->ill_lock);
20350 					RELEASE_CONN_LOCK(q);
20351 					rw_exit(&ill_g_lock);
20352 					return (ipif);
20353 				}
20354 			}
20355 		} else if (ILL_CAN_WAIT(ill, q)) {
20356 			ipsq = ill->ill_phyint->phyint_ipsq;
20357 			mutex_enter(&ipsq->ipsq_lock);
20358 			rw_exit(&ill_g_lock);
20359 			mutex_exit(&ill->ill_lock);
20360 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
20361 			mutex_exit(&ipsq->ipsq_lock);
20362 			RELEASE_CONN_LOCK(q);
20363 			*err = EINPROGRESS;
20364 			return (NULL);
20365 		}
20366 		mutex_exit(&ill->ill_lock);
20367 		RELEASE_CONN_LOCK(q);
20368 	}
20369 	rw_exit(&ill_g_lock);
20370 	if (err != NULL)
20371 		*err = ENXIO;
20372 	return (NULL);
20373 }
20374 
20375 typedef struct conn_change_s {
20376 	uint_t cc_old_ifindex;
20377 	uint_t cc_new_ifindex;
20378 } conn_change_t;
20379 
20380 /*
20381  * ipcl_walk function for changing interface index.
20382  */
20383 static void
20384 conn_change_ifindex(conn_t *connp, caddr_t arg)
20385 {
20386 	conn_change_t *connc;
20387 	uint_t old_ifindex;
20388 	uint_t new_ifindex;
20389 	int i;
20390 	ilg_t *ilg;
20391 
20392 	connc = (conn_change_t *)arg;
20393 	old_ifindex = connc->cc_old_ifindex;
20394 	new_ifindex = connc->cc_new_ifindex;
20395 
20396 	if (connp->conn_orig_bound_ifindex == old_ifindex)
20397 		connp->conn_orig_bound_ifindex = new_ifindex;
20398 
20399 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
20400 		connp->conn_orig_multicast_ifindex = new_ifindex;
20401 
20402 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
20403 		connp->conn_orig_xmit_ifindex = new_ifindex;
20404 
20405 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
20406 		ilg = &connp->conn_ilg[i];
20407 		if (ilg->ilg_orig_ifindex == old_ifindex)
20408 			ilg->ilg_orig_ifindex = new_ifindex;
20409 	}
20410 }
20411 
20412 /*
20413  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
20414  * to new_index if it matches the old_index.
20415  *
20416  * Failovers typically happen within a group of ills. But somebody
20417  * can remove an ill from the group after a failover happened. If
20418  * we are setting the ifindex after this, we potentially need to
20419  * look at all the ills rather than just the ones in the group.
20420  * We cut down the work by looking at matching ill_net_types
20421  * and ill_types as we could not possibly grouped them together.
20422  */
20423 static void
20424 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
20425 {
20426 	ill_t *ill;
20427 	ipif_t *ipif;
20428 	uint_t old_ifindex;
20429 	uint_t new_ifindex;
20430 	ilm_t *ilm;
20431 	ill_walk_context_t ctx;
20432 
20433 	old_ifindex = connc->cc_old_ifindex;
20434 	new_ifindex = connc->cc_new_ifindex;
20435 
20436 	rw_enter(&ill_g_lock, RW_READER);
20437 	ill = ILL_START_WALK_ALL(&ctx);
20438 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
20439 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
20440 			(ill_orig->ill_type != ill->ill_type)) {
20441 			continue;
20442 		}
20443 		for (ipif = ill->ill_ipif; ipif != NULL;
20444 				ipif = ipif->ipif_next) {
20445 			if (ipif->ipif_orig_ifindex == old_ifindex)
20446 				ipif->ipif_orig_ifindex = new_ifindex;
20447 		}
20448 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
20449 			if (ilm->ilm_orig_ifindex == old_ifindex)
20450 				ilm->ilm_orig_ifindex = new_ifindex;
20451 		}
20452 	}
20453 	rw_exit(&ill_g_lock);
20454 }
20455 
20456 /*
20457  * We first need to ensure that the new index is unique, and
20458  * then carry the change across both v4 and v6 ill representation
20459  * of the physical interface.
20460  */
20461 /* ARGSUSED */
20462 int
20463 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20464     ip_ioctl_cmd_t *ipip, void *ifreq)
20465 {
20466 	ill_t		*ill;
20467 	ill_t		*ill_other;
20468 	phyint_t	*phyi;
20469 	int		old_index;
20470 	conn_change_t	connc;
20471 	struct ifreq	*ifr = (struct ifreq *)ifreq;
20472 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20473 	uint_t	index;
20474 	ill_t	*ill_v4;
20475 	ill_t	*ill_v6;
20476 
20477 	if (ipip->ipi_cmd_type == IF_CMD)
20478 		index = ifr->ifr_index;
20479 	else
20480 		index = lifr->lifr_index;
20481 
20482 	/*
20483 	 * Only allow on physical interface. Also, index zero is illegal.
20484 	 *
20485 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
20486 	 *
20487 	 * 1) If PHYI_FAILED is set, a failover could have happened which
20488 	 *    implies a possible failback might have to happen. As failback
20489 	 *    depends on the old index, we should fail setting the index.
20490 	 *
20491 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
20492 	 *    any addresses or multicast memberships are failed over to
20493 	 *    a non-STANDBY interface. As failback depends on the old
20494 	 *    index, we should fail setting the index for this case also.
20495 	 *
20496 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
20497 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
20498 	 */
20499 	ill = ipif->ipif_ill;
20500 	phyi = ill->ill_phyint;
20501 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
20502 	    ipif->ipif_id != 0 || index == 0) {
20503 		return (EINVAL);
20504 	}
20505 	old_index = phyi->phyint_ifindex;
20506 
20507 	/* If the index is not changing, no work to do */
20508 	if (old_index == index)
20509 		return (0);
20510 
20511 	/*
20512 	 * Use ill_lookup_on_ifindex to determine if the
20513 	 * new index is unused and if so allow the change.
20514 	 */
20515 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL);
20516 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL);
20517 	if (ill_v6 != NULL || ill_v4 != NULL) {
20518 		if (ill_v4 != NULL)
20519 			ill_refrele(ill_v4);
20520 		if (ill_v6 != NULL)
20521 			ill_refrele(ill_v6);
20522 		return (EBUSY);
20523 	}
20524 
20525 	/*
20526 	 * The new index is unused. Set it in the phyint.
20527 	 * Locate the other ill so that we can send a routing
20528 	 * sockets message.
20529 	 */
20530 	if (ill->ill_isv6) {
20531 		ill_other = phyi->phyint_illv4;
20532 	} else {
20533 		ill_other = phyi->phyint_illv6;
20534 	}
20535 
20536 	phyi->phyint_ifindex = index;
20537 
20538 	connc.cc_old_ifindex = old_index;
20539 	connc.cc_new_ifindex = index;
20540 	ip_change_ifindex(ill, &connc);
20541 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc);
20542 
20543 	/* Send the routing sockets message */
20544 	ip_rts_ifmsg(ipif);
20545 	if (ill_other != NULL)
20546 		ip_rts_ifmsg(ill_other->ill_ipif);
20547 
20548 	return (0);
20549 }
20550 
20551 /* ARGSUSED */
20552 int
20553 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20554     ip_ioctl_cmd_t *ipip, void *ifreq)
20555 {
20556 	struct ifreq	*ifr = (struct ifreq *)ifreq;
20557 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20558 
20559 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
20560 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20561 	/* Get the interface index */
20562 	if (ipip->ipi_cmd_type == IF_CMD) {
20563 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
20564 	} else {
20565 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
20566 	}
20567 	return (0);
20568 }
20569 
20570 /* ARGSUSED */
20571 int
20572 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20573     ip_ioctl_cmd_t *ipip, void *ifreq)
20574 {
20575 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20576 
20577 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
20578 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20579 	/* Get the interface zone */
20580 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
20581 	lifr->lifr_zoneid = ipif->ipif_zoneid;
20582 	return (0);
20583 }
20584 
20585 /*
20586  * Set the zoneid of an interface.
20587  */
20588 /* ARGSUSED */
20589 int
20590 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20591     ip_ioctl_cmd_t *ipip, void *ifreq)
20592 {
20593 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20594 	int err = 0;
20595 	boolean_t need_up = B_FALSE;
20596 	zone_t *zptr;
20597 	zone_status_t status;
20598 	zoneid_t zoneid;
20599 
20600 	/* cannot assign instance zero to a non-global zone */
20601 	if (ipif->ipif_id == 0)
20602 		return (ENOTSUP);
20603 
20604 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
20605 	zoneid = lifr->lifr_zoneid;
20606 
20607 	/*
20608 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
20609 	 * the event of a race with the zone shutdown processing, since IP
20610 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
20611 	 * interface will be cleaned up even if the zone is shut down
20612 	 * immediately after the status check. If the interface can't be brought
20613 	 * down right away, and the zone is shut down before the restart
20614 	 * function is called, we resolve the possible races by rechecking the
20615 	 * zone status in the restart function.
20616 	 */
20617 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
20618 		return (EINVAL);
20619 	status = zone_status_get(zptr);
20620 	zone_rele(zptr);
20621 
20622 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
20623 		return (EINVAL);
20624 
20625 	if (ipif->ipif_flags & IPIF_UP) {
20626 		/*
20627 		 * If the interface is already marked up,
20628 		 * we call ipif_down which will take care
20629 		 * of ditching any IREs that have been set
20630 		 * up based on the old interface address.
20631 		 */
20632 		err = ipif_logical_down(ipif, q, mp);
20633 		if (err == EINPROGRESS)
20634 			return (err);
20635 		ipif_down_tail(ipif);
20636 		need_up = B_TRUE;
20637 	}
20638 
20639 	err = ip_sioctl_slifzone_tail(ipif, zoneid, q, mp, need_up);
20640 	return (err);
20641 }
20642 
20643 static int
20644 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
20645     queue_t *q, mblk_t *mp, boolean_t need_up)
20646 {
20647 	int	err = 0;
20648 
20649 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
20650 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20651 
20652 	/* Set the new zone id. */
20653 	ipif->ipif_zoneid = zoneid;
20654 
20655 	/* Update sctp list */
20656 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
20657 
20658 	if (need_up) {
20659 		/*
20660 		 * Now bring the interface back up.  If this
20661 		 * is the only IPIF for the ILL, ipif_up
20662 		 * will have to re-bind to the device, so
20663 		 * we may get back EINPROGRESS, in which
20664 		 * case, this IOCTL will get completed in
20665 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
20666 		 */
20667 		err = ipif_up(ipif, q, mp);
20668 	}
20669 	return (err);
20670 }
20671 
20672 /* ARGSUSED */
20673 int
20674 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20675     ip_ioctl_cmd_t *ipip, void *if_req)
20676 {
20677 	struct lifreq *lifr = (struct lifreq *)if_req;
20678 	zoneid_t zoneid;
20679 	zone_t *zptr;
20680 	zone_status_t status;
20681 
20682 	ASSERT(ipif->ipif_id != 0);
20683 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
20684 	zoneid = lifr->lifr_zoneid;
20685 
20686 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
20687 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20688 
20689 	/*
20690 	 * We recheck the zone status to resolve the following race condition:
20691 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
20692 	 * 2) hme0:1 is up and can't be brought down right away;
20693 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
20694 	 * 3) zone "myzone" is halted; the zone status switches to
20695 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
20696 	 * the interfaces to remove - hme0:1 is not returned because it's not
20697 	 * yet in "myzone", so it won't be removed;
20698 	 * 4) the restart function for SIOCSLIFZONE is called; without the
20699 	 * status check here, we would have hme0:1 in "myzone" after it's been
20700 	 * destroyed.
20701 	 * Note that if the status check fails, we need to bring the interface
20702 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
20703 	 * ipif_up_done[_v6]().
20704 	 */
20705 	status = ZONE_IS_UNINITIALIZED;
20706 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
20707 		status = zone_status_get(zptr);
20708 		zone_rele(zptr);
20709 	}
20710 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
20711 		if (ipif->ipif_isv6) {
20712 			(void) ipif_up_done_v6(ipif);
20713 		} else {
20714 			(void) ipif_up_done(ipif);
20715 		}
20716 		return (EINVAL);
20717 	}
20718 
20719 	ipif_down_tail(ipif);
20720 
20721 	return (ip_sioctl_slifzone_tail(ipif, zoneid, q, mp, B_TRUE));
20722 }
20723 
20724 /* ARGSUSED */
20725 int
20726 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20727 	ip_ioctl_cmd_t *ipip, void *ifreq)
20728 {
20729 	struct lifreq	*lifr = ifreq;
20730 
20731 	ASSERT(q->q_next == NULL);
20732 	ASSERT(CONN_Q(q));
20733 
20734 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
20735 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20736 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
20737 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
20738 
20739 	return (0);
20740 }
20741 
20742 
20743 /* Find the previous ILL in this usesrc group */
20744 static ill_t *
20745 ill_prev_usesrc(ill_t *uill)
20746 {
20747 	ill_t *ill;
20748 
20749 	for (ill = uill->ill_usesrc_grp_next;
20750 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
20751 	    ill = ill->ill_usesrc_grp_next)
20752 		/* do nothing */;
20753 	return (ill);
20754 }
20755 
20756 /*
20757  * Release all members of the usesrc group. This routine is called
20758  * from ill_delete when the interface being unplumbed is the
20759  * group head.
20760  */
20761 static void
20762 ill_disband_usesrc_group(ill_t *uill)
20763 {
20764 	ill_t *next_ill, *tmp_ill;
20765 	ASSERT(RW_WRITE_HELD(&ill_g_usesrc_lock));
20766 	next_ill = uill->ill_usesrc_grp_next;
20767 
20768 	do {
20769 		ASSERT(next_ill != NULL);
20770 		tmp_ill = next_ill->ill_usesrc_grp_next;
20771 		ASSERT(tmp_ill != NULL);
20772 		next_ill->ill_usesrc_grp_next = NULL;
20773 		next_ill->ill_usesrc_ifindex = 0;
20774 		next_ill = tmp_ill;
20775 	} while (next_ill->ill_usesrc_ifindex != 0);
20776 	uill->ill_usesrc_grp_next = NULL;
20777 }
20778 
20779 /*
20780  * Remove the client usesrc ILL from the list and relink to a new list
20781  */
20782 int
20783 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
20784 {
20785 	ill_t *ill, *tmp_ill;
20786 
20787 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
20788 	    (uill != NULL) && RW_WRITE_HELD(&ill_g_usesrc_lock));
20789 
20790 	/*
20791 	 * Check if the usesrc client ILL passed in is not already
20792 	 * in use as a usesrc ILL i.e one whose source address is
20793 	 * in use OR a usesrc ILL is not already in use as a usesrc
20794 	 * client ILL
20795 	 */
20796 	if ((ucill->ill_usesrc_ifindex == 0) ||
20797 	    (uill->ill_usesrc_ifindex != 0)) {
20798 		return (-1);
20799 	}
20800 
20801 	ill = ill_prev_usesrc(ucill);
20802 	ASSERT(ill->ill_usesrc_grp_next != NULL);
20803 
20804 	/* Remove from the current list */
20805 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
20806 		/* Only two elements in the list */
20807 		ASSERT(ill->ill_usesrc_ifindex == 0);
20808 		ill->ill_usesrc_grp_next = NULL;
20809 	} else {
20810 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
20811 	}
20812 
20813 	if (ifindex == 0) {
20814 		ucill->ill_usesrc_ifindex = 0;
20815 		ucill->ill_usesrc_grp_next = NULL;
20816 		return (0);
20817 	}
20818 
20819 	ucill->ill_usesrc_ifindex = ifindex;
20820 	tmp_ill = uill->ill_usesrc_grp_next;
20821 	uill->ill_usesrc_grp_next = ucill;
20822 	ucill->ill_usesrc_grp_next =
20823 	    (tmp_ill != NULL) ? tmp_ill : uill;
20824 	return (0);
20825 }
20826 
20827 /*
20828  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
20829  * ip.c for locking details.
20830  */
20831 /* ARGSUSED */
20832 int
20833 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20834     ip_ioctl_cmd_t *ipip, void *ifreq)
20835 {
20836 	struct lifreq *lifr = (struct lifreq *)ifreq;
20837 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
20838 	    ill_flag_changed = B_FALSE;
20839 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
20840 	int err = 0, ret;
20841 	uint_t ifindex;
20842 	phyint_t *us_phyint, *us_cli_phyint;
20843 	ipsq_t *ipsq = NULL;
20844 
20845 	ASSERT(IAM_WRITER_IPIF(ipif));
20846 	ASSERT(q->q_next == NULL);
20847 	ASSERT(CONN_Q(q));
20848 
20849 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
20850 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
20851 
20852 	ASSERT(us_cli_phyint != NULL);
20853 
20854 	/*
20855 	 * If the client ILL is being used for IPMP, abort.
20856 	 * Note, this can be done before ipsq_try_enter since we are already
20857 	 * exclusive on this ILL
20858 	 */
20859 	if ((us_cli_phyint->phyint_groupname != NULL) ||
20860 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
20861 		return (EINVAL);
20862 	}
20863 
20864 	ifindex = lifr->lifr_index;
20865 	if (ifindex == 0) {
20866 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
20867 			/* non usesrc group interface, nothing to reset */
20868 			return (0);
20869 		}
20870 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
20871 		/* valid reset request */
20872 		reset_flg = B_TRUE;
20873 	}
20874 
20875 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
20876 	    ip_process_ioctl, &err);
20877 
20878 	if (usesrc_ill == NULL) {
20879 		return (err);
20880 	}
20881 
20882 	/*
20883 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
20884 	 * group nor can either of the interfaces be used for standy. So
20885 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
20886 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
20887 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
20888 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
20889 	 * the usesrc_cli_ill
20890 	 */
20891 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
20892 	    NEW_OP, B_TRUE);
20893 	if (ipsq == NULL) {
20894 		err = EINPROGRESS;
20895 		/* Operation enqueued on the ipsq of the usesrc ILL */
20896 		goto done;
20897 	}
20898 
20899 	/* Check if the usesrc_ill is used for IPMP */
20900 	us_phyint = usesrc_ill->ill_phyint;
20901 	if ((us_phyint->phyint_groupname != NULL) ||
20902 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
20903 		err = EINVAL;
20904 		goto done;
20905 	}
20906 
20907 	/*
20908 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
20909 	 * already a client then return EINVAL
20910 	 */
20911 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
20912 		err = EINVAL;
20913 		goto done;
20914 	}
20915 
20916 	/*
20917 	 * If the ill_usesrc_ifindex field is already set to what it needs to
20918 	 * be then this is a duplicate operation.
20919 	 */
20920 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
20921 		err = 0;
20922 		goto done;
20923 	}
20924 
20925 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
20926 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
20927 	    usesrc_ill->ill_isv6));
20928 
20929 	/*
20930 	 * The next step ensures that no new ires will be created referencing
20931 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
20932 	 * we go through an ire walk deleting all ire caches that reference
20933 	 * the client ill. New ires referencing the client ill that are added
20934 	 * to the ire table before the ILL_CHANGING flag is set, will be
20935 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
20936 	 * the client ill while the ILL_CHANGING flag is set will be failed
20937 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
20938 	 * checks (under the ill_g_usesrc_lock) that the ire being added
20939 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
20940 	 * belong to the same usesrc group.
20941 	 */
20942 	mutex_enter(&usesrc_cli_ill->ill_lock);
20943 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
20944 	mutex_exit(&usesrc_cli_ill->ill_lock);
20945 	ill_flag_changed = B_TRUE;
20946 
20947 	if (ipif->ipif_isv6)
20948 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
20949 		    ALL_ZONES);
20950 	else
20951 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
20952 		    ALL_ZONES);
20953 
20954 	/*
20955 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
20956 	 * and the ill_usesrc_ifindex fields
20957 	 */
20958 	rw_enter(&ill_g_usesrc_lock, RW_WRITER);
20959 
20960 	if (reset_flg) {
20961 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
20962 		if (ret != 0) {
20963 			err = EINVAL;
20964 		}
20965 		rw_exit(&ill_g_usesrc_lock);
20966 		goto done;
20967 	}
20968 
20969 	/*
20970 	 * Four possibilities to consider:
20971 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
20972 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
20973 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
20974 	 * 4. Both are part of their respective usesrc groups
20975 	 */
20976 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
20977 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
20978 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
20979 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
20980 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
20981 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
20982 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
20983 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
20984 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
20985 		/* Insert at head of list */
20986 		usesrc_cli_ill->ill_usesrc_grp_next =
20987 		    usesrc_ill->ill_usesrc_grp_next;
20988 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
20989 	} else {
20990 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
20991 		    ifindex);
20992 		if (ret != 0)
20993 			err = EINVAL;
20994 	}
20995 	rw_exit(&ill_g_usesrc_lock);
20996 
20997 done:
20998 	if (ill_flag_changed) {
20999 		mutex_enter(&usesrc_cli_ill->ill_lock);
21000 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
21001 		mutex_exit(&usesrc_cli_ill->ill_lock);
21002 	}
21003 	if (ipsq != NULL)
21004 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
21005 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
21006 	ill_refrele(usesrc_ill);
21007 	return (err);
21008 }
21009 
21010 /*
21011  * comparison function used by avl.
21012  */
21013 static int
21014 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
21015 {
21016 
21017 	uint_t index;
21018 
21019 	ASSERT(phyip != NULL && index_ptr != NULL);
21020 
21021 	index = *((uint_t *)index_ptr);
21022 	/*
21023 	 * let the phyint with the lowest index be on top.
21024 	 */
21025 	if (((phyint_t *)phyip)->phyint_ifindex < index)
21026 		return (1);
21027 	if (((phyint_t *)phyip)->phyint_ifindex > index)
21028 		return (-1);
21029 	return (0);
21030 }
21031 
21032 /*
21033  * comparison function used by avl.
21034  */
21035 static int
21036 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
21037 {
21038 	ill_t *ill;
21039 	int res = 0;
21040 
21041 	ASSERT(phyip != NULL && name_ptr != NULL);
21042 
21043 	if (((phyint_t *)phyip)->phyint_illv4)
21044 		ill = ((phyint_t *)phyip)->phyint_illv4;
21045 	else
21046 		ill = ((phyint_t *)phyip)->phyint_illv6;
21047 	ASSERT(ill != NULL);
21048 
21049 	res = strcmp(ill->ill_name, (char *)name_ptr);
21050 	if (res > 0)
21051 		return (1);
21052 	else if (res < 0)
21053 		return (-1);
21054 	return (0);
21055 }
21056 /*
21057  * This function is called from ill_delete when the ill is being
21058  * unplumbed. We remove the reference from the phyint and we also
21059  * free the phyint when there are no more references to it.
21060  */
21061 static void
21062 ill_phyint_free(ill_t *ill)
21063 {
21064 	phyint_t *phyi;
21065 	phyint_t *next_phyint;
21066 	ipsq_t *cur_ipsq;
21067 
21068 	ASSERT(ill->ill_phyint != NULL);
21069 
21070 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
21071 	phyi = ill->ill_phyint;
21072 	ill->ill_phyint = NULL;
21073 	/*
21074 	 * ill_init allocates a phyint always to store the copy
21075 	 * of flags relevant to phyint. At that point in time, we could
21076 	 * not assign the name and hence phyint_illv4/v6 could not be
21077 	 * initialized. Later in ipif_set_values, we assign the name to
21078 	 * the ill, at which point in time we assign phyint_illv4/v6.
21079 	 * Thus we don't rely on phyint_illv6 to be initialized always.
21080 	 */
21081 	if (ill->ill_flags & ILLF_IPV6) {
21082 		phyi->phyint_illv6 = NULL;
21083 	} else {
21084 		phyi->phyint_illv4 = NULL;
21085 	}
21086 	/*
21087 	 * ipif_down removes it from the group when the last ipif goes
21088 	 * down.
21089 	 */
21090 	ASSERT(ill->ill_group == NULL);
21091 
21092 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
21093 		return;
21094 
21095 	/*
21096 	 * Make sure this phyint was put in the list.
21097 	 */
21098 	if (phyi->phyint_ifindex > 0) {
21099 		avl_remove(&phyint_g_list.phyint_list_avl_by_index,
21100 		    phyi);
21101 		avl_remove(&phyint_g_list.phyint_list_avl_by_name,
21102 		    phyi);
21103 	}
21104 	/*
21105 	 * remove phyint from the ipsq list.
21106 	 */
21107 	cur_ipsq = phyi->phyint_ipsq;
21108 	if (phyi == cur_ipsq->ipsq_phyint_list) {
21109 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
21110 	} else {
21111 		next_phyint = cur_ipsq->ipsq_phyint_list;
21112 		while (next_phyint != NULL) {
21113 			if (next_phyint->phyint_ipsq_next == phyi) {
21114 				next_phyint->phyint_ipsq_next =
21115 					phyi->phyint_ipsq_next;
21116 				break;
21117 			}
21118 			next_phyint = next_phyint->phyint_ipsq_next;
21119 		}
21120 		ASSERT(next_phyint != NULL);
21121 	}
21122 	IPSQ_DEC_REF(cur_ipsq);
21123 
21124 	if (phyi->phyint_groupname_len != 0) {
21125 		ASSERT(phyi->phyint_groupname != NULL);
21126 		mi_free(phyi->phyint_groupname);
21127 	}
21128 	mi_free(phyi);
21129 }
21130 
21131 /*
21132  * Attach the ill to the phyint structure which can be shared by both
21133  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
21134  * function is called from ipif_set_values and ill_lookup_on_name (for
21135  * loopback) where we know the name of the ill. We lookup the ill and if
21136  * there is one present already with the name use that phyint. Otherwise
21137  * reuse the one allocated by ill_init.
21138  */
21139 static void
21140 ill_phyint_reinit(ill_t *ill)
21141 {
21142 	boolean_t isv6 = ill->ill_isv6;
21143 	phyint_t *phyi_old;
21144 	phyint_t *phyi;
21145 	avl_index_t where = 0;
21146 	ill_t	*ill_other = NULL;
21147 	ipsq_t	*ipsq;
21148 
21149 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
21150 
21151 	phyi_old = ill->ill_phyint;
21152 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
21153 	    phyi_old->phyint_illv6 == NULL));
21154 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
21155 	    phyi_old->phyint_illv4 == NULL));
21156 	ASSERT(phyi_old->phyint_ifindex == 0);
21157 
21158 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_name,
21159 	    ill->ill_name, &where);
21160 
21161 	/*
21162 	 * 1. We grabbed the ill_g_lock before inserting this ill into
21163 	 *    the global list of ills. So no other thread could have located
21164 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
21165 	 * 2. Now locate the other protocol instance of this ill.
21166 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
21167 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
21168 	 *    of neither ill can change.
21169 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
21170 	 *    other ill.
21171 	 * 5. Release all locks.
21172 	 */
21173 
21174 	/*
21175 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
21176 	 * we are initializing IPv4.
21177 	 */
21178 	if (phyi != NULL) {
21179 		ill_other = (isv6) ? phyi->phyint_illv4 :
21180 		    phyi->phyint_illv6;
21181 		ASSERT(ill_other->ill_phyint != NULL);
21182 		ASSERT((isv6 && !ill_other->ill_isv6) ||
21183 		    (!isv6 && ill_other->ill_isv6));
21184 		GRAB_ILL_LOCKS(ill, ill_other);
21185 		/*
21186 		 * We are potentially throwing away phyint_flags which
21187 		 * could be different from the one that we obtain from
21188 		 * ill_other->ill_phyint. But it is okay as we are assuming
21189 		 * that the state maintained within IP is correct.
21190 		 */
21191 		mutex_enter(&phyi->phyint_lock);
21192 		if (isv6) {
21193 			ASSERT(phyi->phyint_illv6 == NULL);
21194 			phyi->phyint_illv6 = ill;
21195 		} else {
21196 			ASSERT(phyi->phyint_illv4 == NULL);
21197 			phyi->phyint_illv4 = ill;
21198 		}
21199 		/*
21200 		 * This is a new ill, currently undergoing SLIFNAME
21201 		 * So we could not have joined an IPMP group until now.
21202 		 */
21203 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
21204 		    phyi_old->phyint_groupname == NULL);
21205 
21206 		/*
21207 		 * This phyi_old is going away. Decref ipsq_refs and
21208 		 * assert it is zero. The ipsq itself will be freed in
21209 		 * ipsq_exit
21210 		 */
21211 		ipsq = phyi_old->phyint_ipsq;
21212 		IPSQ_DEC_REF(ipsq);
21213 		ASSERT(ipsq->ipsq_refs == 0);
21214 		/* Get the singleton phyint out of the ipsq list */
21215 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
21216 		ipsq->ipsq_phyint_list = NULL;
21217 		phyi_old->phyint_illv4 = NULL;
21218 		phyi_old->phyint_illv6 = NULL;
21219 		mi_free(phyi_old);
21220 	} else {
21221 		mutex_enter(&ill->ill_lock);
21222 		/*
21223 		 * We don't need to acquire any lock, since
21224 		 * the ill is not yet visible globally  and we
21225 		 * have not yet released the ill_g_lock.
21226 		 */
21227 		phyi = phyi_old;
21228 		mutex_enter(&phyi->phyint_lock);
21229 		/* XXX We need a recovery strategy here. */
21230 		if (!phyint_assign_ifindex(phyi))
21231 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
21232 
21233 		avl_insert(&phyint_g_list.phyint_list_avl_by_name,
21234 		    (void *)phyi, where);
21235 
21236 		(void) avl_find(&phyint_g_list.phyint_list_avl_by_index,
21237 		    &phyi->phyint_ifindex, &where);
21238 		avl_insert(&phyint_g_list.phyint_list_avl_by_index,
21239 		    (void *)phyi, where);
21240 	}
21241 
21242 	/*
21243 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
21244 	 * pending mp is not affected because that is per ill basis.
21245 	 */
21246 	ill->ill_phyint = phyi;
21247 
21248 	/*
21249 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
21250 	 * We do this here as when the first ipif was allocated,
21251 	 * ipif_allocate does not know the right interface index.
21252 	 */
21253 
21254 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
21255 	/*
21256 	 * Now that the phyint's ifindex has been assigned, complete the
21257 	 * remaining
21258 	 */
21259 	if (ill->ill_isv6) {
21260 		ill->ill_ip6_mib->ipv6IfIndex =
21261 		    ill->ill_phyint->phyint_ifindex;
21262 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
21263 		    ill->ill_phyint->phyint_ifindex;
21264 	}
21265 
21266 	RELEASE_ILL_LOCKS(ill, ill_other);
21267 	mutex_exit(&phyi->phyint_lock);
21268 }
21269 
21270 /*
21271  * Notify any downstream modules of the name of this interface.
21272  * An M_IOCTL is used even though we don't expect a successful reply.
21273  * Any reply message from the driver (presumably an M_IOCNAK) will
21274  * eventually get discarded somewhere upstream.  The message format is
21275  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
21276  * to IP.
21277  */
21278 static void
21279 ip_ifname_notify(ill_t *ill, queue_t *q)
21280 {
21281 	mblk_t *mp1, *mp2;
21282 	struct iocblk *iocp;
21283 	struct lifreq *lifr;
21284 
21285 	mp1 = mkiocb(SIOCSLIFNAME);
21286 	if (mp1 == NULL)
21287 		return;
21288 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
21289 	if (mp2 == NULL) {
21290 		freeb(mp1);
21291 		return;
21292 	}
21293 
21294 	mp1->b_cont = mp2;
21295 	iocp = (struct iocblk *)mp1->b_rptr;
21296 	iocp->ioc_count = sizeof (struct lifreq);
21297 
21298 	lifr = (struct lifreq *)mp2->b_rptr;
21299 	mp2->b_wptr += sizeof (struct lifreq);
21300 	bzero(lifr, sizeof (struct lifreq));
21301 
21302 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
21303 	lifr->lifr_ppa = ill->ill_ppa;
21304 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
21305 
21306 	putnext(q, mp1);
21307 }
21308 
21309 static boolean_t ip_trash_timer_started = B_FALSE;
21310 
21311 static int
21312 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
21313 {
21314 	int err;
21315 
21316 	/* Set the obsolete NDD per-interface forwarding name. */
21317 	err = ill_set_ndd_name(ill);
21318 	if (err != 0) {
21319 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
21320 		    err);
21321 	}
21322 
21323 	/* Tell downstream modules where they are. */
21324 	ip_ifname_notify(ill, q);
21325 
21326 	/*
21327 	 * ill_dl_phys returns EINPROGRESS in the usual case.
21328 	 * Error cases are ENOMEM ...
21329 	 */
21330 	err = ill_dl_phys(ill, ipif, mp, q);
21331 
21332 	/*
21333 	 * If there is no IRE expiration timer running, get one started.
21334 	 * igmp and mld timers will be triggered by the first multicast
21335 	 */
21336 	if (!ip_trash_timer_started) {
21337 		/*
21338 		 * acquire the lock and check again.
21339 		 */
21340 		mutex_enter(&ip_trash_timer_lock);
21341 		if (!ip_trash_timer_started) {
21342 			ip_ire_expire_id = timeout(ip_trash_timer_expire, NULL,
21343 			    MSEC_TO_TICK(ip_timer_interval));
21344 			ip_trash_timer_started = B_TRUE;
21345 		}
21346 		mutex_exit(&ip_trash_timer_lock);
21347 	}
21348 
21349 	if (ill->ill_isv6) {
21350 		mutex_enter(&mld_slowtimeout_lock);
21351 		if (mld_slowtimeout_id == 0) {
21352 			mld_slowtimeout_id = timeout(mld_slowtimo, NULL,
21353 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
21354 		}
21355 		mutex_exit(&mld_slowtimeout_lock);
21356 	} else {
21357 		mutex_enter(&igmp_slowtimeout_lock);
21358 		if (igmp_slowtimeout_id == 0) {
21359 			igmp_slowtimeout_id = timeout(igmp_slowtimo, NULL,
21360 				MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
21361 		}
21362 		mutex_exit(&igmp_slowtimeout_lock);
21363 	}
21364 
21365 	return (err);
21366 }
21367 
21368 /*
21369  * Common routine for ppa and ifname setting. Should be called exclusive.
21370  *
21371  * Returns EINPROGRESS when mp has been consumed by queueing it on
21372  * ill_pending_mp and the ioctl will complete in ip_rput.
21373  *
21374  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
21375  * the new name and new ppa in lifr_name and lifr_ppa respectively.
21376  * For SLIFNAME, we pass these values back to the userland.
21377  */
21378 static int
21379 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
21380 {
21381 	ill_t	*ill;
21382 	ipif_t	*ipif;
21383 	ipsq_t	*ipsq;
21384 	char	*ppa_ptr;
21385 	char	*old_ptr;
21386 	char	old_char;
21387 	int	error;
21388 
21389 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
21390 	ASSERT(q->q_next != NULL);
21391 	ASSERT(interf_name != NULL);
21392 
21393 	ill = (ill_t *)q->q_ptr;
21394 
21395 	ASSERT(ill->ill_name[0] == '\0');
21396 	ASSERT(IAM_WRITER_ILL(ill));
21397 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
21398 	ASSERT(ill->ill_ppa == UINT_MAX);
21399 
21400 	/* The ppa is sent down by ifconfig or is chosen */
21401 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
21402 		return (EINVAL);
21403 	}
21404 
21405 	/*
21406 	 * make sure ppa passed in is same as ppa in the name.
21407 	 * This check is not made when ppa == UINT_MAX in that case ppa
21408 	 * in the name could be anything. System will choose a ppa and
21409 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
21410 	 */
21411 	if (*new_ppa_ptr != UINT_MAX) {
21412 		/* stoi changes the pointer */
21413 		old_ptr = ppa_ptr;
21414 		/*
21415 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
21416 		 * (they don't have an externally visible ppa).  We assign one
21417 		 * here so that we can manage the interface.  Note that in
21418 		 * the past this value was always 0 for DLPI 1 drivers.
21419 		 */
21420 		if (*new_ppa_ptr == 0)
21421 			*new_ppa_ptr = stoi(&old_ptr);
21422 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
21423 			return (EINVAL);
21424 	}
21425 	/*
21426 	 * terminate string before ppa
21427 	 * save char at that location.
21428 	 */
21429 	old_char = ppa_ptr[0];
21430 	ppa_ptr[0] = '\0';
21431 
21432 	ill->ill_ppa = *new_ppa_ptr;
21433 	/*
21434 	 * Finish as much work now as possible before calling ill_glist_insert
21435 	 * which makes the ill globally visible and also merges it with the
21436 	 * other protocol instance of this phyint. The remaining work is
21437 	 * done after entering the ipsq which may happen sometime later.
21438 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
21439 	 */
21440 	ipif = ill->ill_ipif;
21441 
21442 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
21443 	ipif_assign_seqid(ipif);
21444 
21445 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
21446 		ill->ill_flags |= ILLF_IPV4;
21447 
21448 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
21449 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
21450 
21451 	if (ill->ill_flags & ILLF_IPV6) {
21452 
21453 		ill->ill_isv6 = B_TRUE;
21454 		if (ill->ill_rq != NULL) {
21455 			ill->ill_rq->q_qinfo = &rinit_ipv6;
21456 			ill->ill_wq->q_qinfo = &winit_ipv6;
21457 		}
21458 
21459 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
21460 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
21461 		ipif->ipif_v6src_addr = ipv6_all_zeros;
21462 		ipif->ipif_v6subnet = ipv6_all_zeros;
21463 		ipif->ipif_v6net_mask = ipv6_all_zeros;
21464 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
21465 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
21466 		/*
21467 		 * point-to-point or Non-mulicast capable
21468 		 * interfaces won't do NUD unless explicitly
21469 		 * configured to do so.
21470 		 */
21471 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
21472 		    !(ill->ill_flags & ILLF_MULTICAST)) {
21473 			ill->ill_flags |= ILLF_NONUD;
21474 		}
21475 		/* Make sure IPv4 specific flag is not set on IPv6 if */
21476 		if (ill->ill_flags & ILLF_NOARP) {
21477 			/*
21478 			 * Note: xresolv interfaces will eventually need
21479 			 * NOARP set here as well, but that will require
21480 			 * those external resolvers to have some
21481 			 * knowledge of that flag and act appropriately.
21482 			 * Not to be changed at present.
21483 			 */
21484 			ill->ill_flags &= ~ILLF_NOARP;
21485 		}
21486 		/*
21487 		 * Set the ILLF_ROUTER flag according to the global
21488 		 * IPv6 forwarding policy.
21489 		 */
21490 		if (ipv6_forward != 0)
21491 			ill->ill_flags |= ILLF_ROUTER;
21492 	} else if (ill->ill_flags & ILLF_IPV4) {
21493 		ill->ill_isv6 = B_FALSE;
21494 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
21495 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
21496 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
21497 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
21498 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
21499 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
21500 		/*
21501 		 * Set the ILLF_ROUTER flag according to the global
21502 		 * IPv4 forwarding policy.
21503 		 */
21504 		if (ip_g_forward != 0)
21505 			ill->ill_flags |= ILLF_ROUTER;
21506 	}
21507 
21508 	ASSERT(ill->ill_phyint != NULL);
21509 
21510 	/*
21511 	 * The ipv6Ifindex and ipv6IfIcmpIfIndex assignments will
21512 	 * be completed in ill_glist_insert -> ill_phyint_reinit
21513 	 */
21514 	if (ill->ill_isv6) {
21515 		/* allocate v6 mib */
21516 		if (!ill_allocate_mibs(ill))
21517 			return (ENOMEM);
21518 	}
21519 
21520 	/*
21521 	 * Pick a default sap until we get the DL_INFO_ACK back from
21522 	 * the driver.
21523 	 */
21524 	if (ill->ill_sap == 0) {
21525 		if (ill->ill_isv6)
21526 			ill->ill_sap  = IP6_DL_SAP;
21527 		else
21528 			ill->ill_sap  = IP_DL_SAP;
21529 	}
21530 
21531 	ill->ill_ifname_pending = 1;
21532 	ill->ill_ifname_pending_err = 0;
21533 
21534 	ill_refhold(ill);
21535 	rw_enter(&ill_g_lock, RW_WRITER);
21536 	if ((error = ill_glist_insert(ill, interf_name,
21537 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
21538 		ill->ill_ppa = UINT_MAX;
21539 		ill->ill_name[0] = '\0';
21540 		/*
21541 		 * undo null termination done above.
21542 		 */
21543 		ppa_ptr[0] = old_char;
21544 		rw_exit(&ill_g_lock);
21545 		ill_refrele(ill);
21546 		return (error);
21547 	}
21548 
21549 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
21550 
21551 	/*
21552 	 * When we return the buffer pointed to by interf_name should contain
21553 	 * the same name as in ill_name.
21554 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
21555 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
21556 	 * so copy full name and update the ppa ptr.
21557 	 * When ppa passed in != UINT_MAX all values are correct just undo
21558 	 * null termination, this saves a bcopy.
21559 	 */
21560 	if (*new_ppa_ptr == UINT_MAX) {
21561 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
21562 		*new_ppa_ptr = ill->ill_ppa;
21563 	} else {
21564 		/*
21565 		 * undo null termination done above.
21566 		 */
21567 		ppa_ptr[0] = old_char;
21568 	}
21569 
21570 	/* Let SCTP know about this ILL */
21571 	sctp_update_ill(ill, SCTP_ILL_INSERT);
21572 
21573 	/* and also about the first ipif */
21574 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
21575 
21576 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
21577 	    B_TRUE);
21578 
21579 	rw_exit(&ill_g_lock);
21580 	ill_refrele(ill);
21581 	if (ipsq == NULL)
21582 		return (EINPROGRESS);
21583 
21584 	/*
21585 	 * Need to set the ipsq_current_ipif now, if we have changed ipsq
21586 	 * due to the phyint merge in ill_phyint_reinit.
21587 	 */
21588 	ASSERT(ipsq->ipsq_current_ipif == NULL ||
21589 		ipsq->ipsq_current_ipif == ipif);
21590 	ipsq->ipsq_current_ipif = ipif;
21591 	ipsq->ipsq_last_cmd = SIOCSLIFNAME;
21592 	error = ipif_set_values_tail(ill, ipif, mp, q);
21593 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
21594 	if (error != 0 && error != EINPROGRESS) {
21595 		/*
21596 		 * restore previous values
21597 		 */
21598 		ill->ill_isv6 = B_FALSE;
21599 	}
21600 	return (error);
21601 }
21602 
21603 
21604 extern void (*ip_cleanup_func)(void);
21605 
21606 void
21607 ipif_init(void)
21608 {
21609 	hrtime_t hrt;
21610 	int i;
21611 
21612 	/*
21613 	 * Can't call drv_getparm here as it is too early in the boot.
21614 	 * As we use ipif_src_random just for picking a different
21615 	 * source address everytime, this need not be really random.
21616 	 */
21617 	hrt = gethrtime();
21618 	ipif_src_random = ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
21619 
21620 	for (i = 0; i < MAX_G_HEADS; i++) {
21621 		ill_g_heads[i].ill_g_list_head = (ill_if_t *)&ill_g_heads[i];
21622 		ill_g_heads[i].ill_g_list_tail = (ill_if_t *)&ill_g_heads[i];
21623 	}
21624 
21625 	avl_create(&phyint_g_list.phyint_list_avl_by_index,
21626 	    ill_phyint_compare_index,
21627 	    sizeof (phyint_t),
21628 	    offsetof(struct phyint, phyint_avl_by_index));
21629 	avl_create(&phyint_g_list.phyint_list_avl_by_name,
21630 	    ill_phyint_compare_name,
21631 	    sizeof (phyint_t),
21632 	    offsetof(struct phyint, phyint_avl_by_name));
21633 
21634 	ip_cleanup_func = ip_thread_exit;
21635 }
21636 
21637 /*
21638  * This is called by ip_rt_add when src_addr value is other than zero.
21639  * src_addr signifies the source address of the incoming packet. For
21640  * reverse tunnel route we need to create a source addr based routing
21641  * table. This routine creates ip_mrtun_table if it's empty and then
21642  * it adds the route entry hashed by source address. It verifies that
21643  * the outgoing interface is always a non-resolver interface (tunnel).
21644  */
21645 int
21646 ip_mrtun_rt_add(ipaddr_t in_src_addr, int flags, ipif_t *ipif_arg,
21647     ipif_t *src_ipif, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func)
21648 {
21649 	ire_t   *ire;
21650 	ire_t	*save_ire;
21651 	ipif_t  *ipif;
21652 	ill_t   *in_ill = NULL;
21653 	ill_t	*out_ill;
21654 	queue_t	*stq;
21655 	mblk_t	*dlureq_mp;
21656 	int	error;
21657 
21658 	if (ire_arg != NULL)
21659 		*ire_arg = NULL;
21660 	ASSERT(in_src_addr != INADDR_ANY);
21661 
21662 	ipif = ipif_arg;
21663 	if (ipif != NULL) {
21664 		out_ill = ipif->ipif_ill;
21665 	} else {
21666 		ip1dbg(("ip_mrtun_rt_add: ipif is NULL\n"));
21667 		return (EINVAL);
21668 	}
21669 
21670 	if (src_ipif == NULL) {
21671 		ip1dbg(("ip_mrtun_rt_add: src_ipif is NULL\n"));
21672 		return (EINVAL);
21673 	}
21674 	in_ill = src_ipif->ipif_ill;
21675 
21676 	/*
21677 	 * Check for duplicates. We don't need to
21678 	 * match out_ill, because the uniqueness of
21679 	 * a route is only dependent on src_addr and
21680 	 * in_ill.
21681 	 */
21682 	ire = ire_mrtun_lookup(in_src_addr, in_ill);
21683 	if (ire != NULL) {
21684 		ire_refrele(ire);
21685 		return (EEXIST);
21686 	}
21687 	if (ipif->ipif_net_type != IRE_IF_NORESOLVER) {
21688 		ip2dbg(("ip_mrtun_rt_add: outgoing interface is type %d\n",
21689 		    ipif->ipif_net_type));
21690 		return (EINVAL);
21691 	}
21692 
21693 	stq = ipif->ipif_wq;
21694 	ASSERT(stq != NULL);
21695 
21696 	/*
21697 	 * The outgoing interface must be non-resolver
21698 	 * interface.
21699 	 */
21700 	dlureq_mp = ill_dlur_gen(NULL,
21701 	    out_ill->ill_phys_addr_length, out_ill->ill_sap,
21702 	    out_ill->ill_sap_length);
21703 
21704 	if (dlureq_mp == NULL) {
21705 		ip1dbg(("ip_newroute: dlureq_mp NULL\n"));
21706 		return (ENOMEM);
21707 	}
21708 
21709 	/* Create the IRE. */
21710 
21711 	ire = ire_create(
21712 	    NULL,				/* Zero dst addr */
21713 	    NULL,				/* Zero mask */
21714 	    NULL,				/* Zero gateway addr */
21715 	    NULL,				/* Zero ipif_src addr */
21716 	    (uint8_t *)&in_src_addr,		/* in_src-addr */
21717 	    &ipif->ipif_mtu,
21718 	    NULL,
21719 	    NULL,				/* rfq */
21720 	    stq,
21721 	    IRE_MIPRTUN,
21722 	    dlureq_mp,
21723 	    ipif,
21724 	    in_ill,
21725 	    0,
21726 	    0,
21727 	    0,
21728 	    flags,
21729 	    &ire_uinfo_null);
21730 
21731 	if (ire == NULL)
21732 		return (ENOMEM);
21733 	ip2dbg(("ip_mrtun_rt_add: mrtun route is created with type %d\n",
21734 	    ire->ire_type));
21735 	save_ire = ire;
21736 	ASSERT(save_ire != NULL);
21737 	error = ire_add_mrtun(&ire, q, mp, func);
21738 	/*
21739 	 * If ire_add_mrtun() failed, the ire passed in was freed
21740 	 * so there is no need to do so here.
21741 	 */
21742 	if (error != 0) {
21743 		return (error);
21744 	}
21745 
21746 	/* Duplicate check */
21747 	if (ire != save_ire) {
21748 		/* route already exists by now */
21749 		ire_refrele(ire);
21750 		return (EEXIST);
21751 	}
21752 
21753 	if (ire_arg != NULL) {
21754 		/*
21755 		 * Store the ire that was just added. the caller
21756 		 * ip_rts_request responsible for doing ire_refrele()
21757 		 * on it.
21758 		 */
21759 		*ire_arg = ire;
21760 	} else {
21761 		ire_refrele(ire);	/* held in ire_add_mrtun */
21762 	}
21763 
21764 	return (0);
21765 }
21766 
21767 /*
21768  * It is called by ip_rt_delete() only when mipagent requests to delete
21769  * a reverse tunnel route that was added by ip_mrtun_rt_add() before.
21770  */
21771 
21772 int
21773 ip_mrtun_rt_delete(ipaddr_t in_src_addr, ipif_t *src_ipif)
21774 {
21775 	ire_t   *ire = NULL;
21776 
21777 	if (in_src_addr == INADDR_ANY)
21778 		return (EINVAL);
21779 	if (src_ipif == NULL)
21780 		return (EINVAL);
21781 
21782 	/* search if this route exists in the ip_mrtun_table */
21783 	ire = ire_mrtun_lookup(in_src_addr, src_ipif->ipif_ill);
21784 	if (ire == NULL) {
21785 		ip2dbg(("ip_mrtun_rt_delete: ire not found\n"));
21786 		return (ESRCH);
21787 	}
21788 	ire_delete(ire);
21789 	ire_refrele(ire);
21790 	return (0);
21791 }
21792 
21793 /*
21794  * Lookup the ipif corresponding to the onlink destination address. For
21795  * point-to-point interfaces, it matches with remote endpoint destination
21796  * address. For point-to-multipoint interfaces it only tries to match the
21797  * destination with the interface's subnet address. The longest, most specific
21798  * match is found to take care of such rare network configurations like -
21799  * le0: 129.146.1.1/16
21800  * le1: 129.146.2.2/24
21801  * It is used only by SO_DONTROUTE at the moment.
21802  */
21803 ipif_t *
21804 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid)
21805 {
21806 	ipif_t	*ipif, *best_ipif;
21807 	ill_t	*ill;
21808 	ill_walk_context_t ctx;
21809 
21810 	ASSERT(zoneid != ALL_ZONES);
21811 	best_ipif = NULL;
21812 
21813 	rw_enter(&ill_g_lock, RW_READER);
21814 	ill = ILL_START_WALK_V4(&ctx);
21815 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21816 		mutex_enter(&ill->ill_lock);
21817 		for (ipif = ill->ill_ipif; ipif != NULL;
21818 		    ipif = ipif->ipif_next) {
21819 			if (!IPIF_CAN_LOOKUP(ipif))
21820 				continue;
21821 			if (ipif->ipif_zoneid != zoneid)
21822 				continue;
21823 			/*
21824 			 * Point-to-point case. Look for exact match with
21825 			 * destination address.
21826 			 */
21827 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
21828 				if (ipif->ipif_pp_dst_addr == addr) {
21829 					ipif_refhold_locked(ipif);
21830 					mutex_exit(&ill->ill_lock);
21831 					rw_exit(&ill_g_lock);
21832 					if (best_ipif != NULL)
21833 						ipif_refrele(best_ipif);
21834 					return (ipif);
21835 				}
21836 			} else if (ipif->ipif_subnet == (addr &
21837 			    ipif->ipif_net_mask)) {
21838 				/*
21839 				 * Point-to-multipoint case. Looping through to
21840 				 * find the most specific match. If there are
21841 				 * multiple best match ipif's then prefer ipif's
21842 				 * that are UP. If there is only one best match
21843 				 * ipif and it is DOWN we must still return it.
21844 				 */
21845 				if ((best_ipif == NULL) ||
21846 				    (ipif->ipif_net_mask >
21847 				    best_ipif->ipif_net_mask) ||
21848 				    ((ipif->ipif_net_mask ==
21849 				    best_ipif->ipif_net_mask) &&
21850 				    ((ipif->ipif_flags & IPIF_UP) &&
21851 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
21852 					ipif_refhold_locked(ipif);
21853 					mutex_exit(&ill->ill_lock);
21854 					rw_exit(&ill_g_lock);
21855 					if (best_ipif != NULL)
21856 						ipif_refrele(best_ipif);
21857 					best_ipif = ipif;
21858 					rw_enter(&ill_g_lock, RW_READER);
21859 					mutex_enter(&ill->ill_lock);
21860 				}
21861 			}
21862 		}
21863 		mutex_exit(&ill->ill_lock);
21864 	}
21865 	rw_exit(&ill_g_lock);
21866 	return (best_ipif);
21867 }
21868 
21869 
21870 /*
21871  * Save enough information so that we can recreate the IRE if
21872  * the interface goes down and then up.
21873  */
21874 static void
21875 ipif_save_ire(ipif_t *ipif, ire_t *ire)
21876 {
21877 	mblk_t	*save_mp;
21878 
21879 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
21880 	if (save_mp != NULL) {
21881 		ifrt_t	*ifrt;
21882 
21883 		save_mp->b_wptr += sizeof (ifrt_t);
21884 		ifrt = (ifrt_t *)save_mp->b_rptr;
21885 		bzero(ifrt, sizeof (ifrt_t));
21886 		ifrt->ifrt_type = ire->ire_type;
21887 		ifrt->ifrt_addr = ire->ire_addr;
21888 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
21889 		ifrt->ifrt_src_addr = ire->ire_src_addr;
21890 		ifrt->ifrt_mask = ire->ire_mask;
21891 		ifrt->ifrt_flags = ire->ire_flags;
21892 		ifrt->ifrt_max_frag = ire->ire_max_frag;
21893 		mutex_enter(&ipif->ipif_saved_ire_lock);
21894 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
21895 		ipif->ipif_saved_ire_mp = save_mp;
21896 		ipif->ipif_saved_ire_cnt++;
21897 		mutex_exit(&ipif->ipif_saved_ire_lock);
21898 	}
21899 }
21900 
21901 
21902 static void
21903 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
21904 {
21905 	mblk_t	**mpp;
21906 	mblk_t	*mp;
21907 	ifrt_t	*ifrt;
21908 
21909 	/* Remove from ipif_saved_ire_mp list if it is there */
21910 	mutex_enter(&ipif->ipif_saved_ire_lock);
21911 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
21912 	    mpp = &(*mpp)->b_cont) {
21913 		/*
21914 		 * On a given ipif, the triple of address, gateway and
21915 		 * mask is unique for each saved IRE (in the case of
21916 		 * ordinary interface routes, the gateway address is
21917 		 * all-zeroes).
21918 		 */
21919 		mp = *mpp;
21920 		ifrt = (ifrt_t *)mp->b_rptr;
21921 		if (ifrt->ifrt_addr == ire->ire_addr &&
21922 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
21923 		    ifrt->ifrt_mask == ire->ire_mask) {
21924 			*mpp = mp->b_cont;
21925 			ipif->ipif_saved_ire_cnt--;
21926 			freeb(mp);
21927 			break;
21928 		}
21929 	}
21930 	mutex_exit(&ipif->ipif_saved_ire_lock);
21931 }
21932 
21933 
21934 /*
21935  * IP multirouting broadcast routes handling
21936  * Append CGTP broadcast IREs to regular ones created
21937  * at ifconfig time.
21938  */
21939 static void
21940 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst)
21941 {
21942 	ire_t *ire_prim;
21943 
21944 	ASSERT(ire != NULL);
21945 	ASSERT(ire_dst != NULL);
21946 
21947 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
21948 	    IRE_BROADCAST, NULL, NULL, MATCH_IRE_TYPE);
21949 	if (ire_prim != NULL) {
21950 		/*
21951 		 * We are in the special case of broadcasts for
21952 		 * CGTP. We add an IRE_BROADCAST that holds
21953 		 * the RTF_MULTIRT flag, the destination
21954 		 * address of ire_dst and the low level
21955 		 * info of ire_prim. In other words, CGTP
21956 		 * broadcast is added to the redundant ipif.
21957 		 */
21958 		ipif_t *ipif_prim;
21959 		ire_t  *bcast_ire;
21960 
21961 		ipif_prim = ire_prim->ire_ipif;
21962 
21963 		ip2dbg(("ip_cgtp_filter_bcast_add: "
21964 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
21965 		    (void *)ire_dst, (void *)ire_prim,
21966 		    (void *)ipif_prim));
21967 
21968 		bcast_ire = ire_create(
21969 		    (uchar_t *)&ire->ire_addr,
21970 		    (uchar_t *)&ip_g_all_ones,
21971 		    (uchar_t *)&ire_dst->ire_src_addr,
21972 		    (uchar_t *)&ire->ire_gateway_addr,
21973 		    NULL,
21974 		    &ipif_prim->ipif_mtu,
21975 		    NULL,
21976 		    ipif_prim->ipif_rq,
21977 		    ipif_prim->ipif_wq,
21978 		    IRE_BROADCAST,
21979 		    ipif_prim->ipif_bcast_mp,
21980 		    ipif_prim,
21981 		    NULL,
21982 		    0,
21983 		    0,
21984 		    0,
21985 		    ire->ire_flags,
21986 		    &ire_uinfo_null);
21987 
21988 		if (bcast_ire != NULL) {
21989 
21990 			if (ire_add(&bcast_ire, NULL, NULL, NULL) == 0) {
21991 				ip2dbg(("ip_cgtp_filter_bcast_add: "
21992 				    "added bcast_ire %p\n",
21993 				    (void *)bcast_ire));
21994 
21995 				ipif_save_ire(bcast_ire->ire_ipif,
21996 				    bcast_ire);
21997 				ire_refrele(bcast_ire);
21998 			}
21999 		}
22000 		ire_refrele(ire_prim);
22001 	}
22002 }
22003 
22004 
22005 /*
22006  * IP multirouting broadcast routes handling
22007  * Remove the broadcast ire
22008  */
22009 static void
22010 ip_cgtp_bcast_delete(ire_t *ire)
22011 {
22012 	ire_t *ire_dst;
22013 
22014 	ASSERT(ire != NULL);
22015 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
22016 	    NULL, NULL, MATCH_IRE_TYPE);
22017 	if (ire_dst != NULL) {
22018 		ire_t *ire_prim;
22019 
22020 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
22021 		    IRE_BROADCAST, NULL, NULL, MATCH_IRE_TYPE);
22022 		if (ire_prim != NULL) {
22023 			ipif_t *ipif_prim;
22024 			ire_t  *bcast_ire;
22025 
22026 			ipif_prim = ire_prim->ire_ipif;
22027 
22028 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
22029 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
22030 			    (void *)ire_dst, (void *)ire_prim,
22031 			    (void *)ipif_prim));
22032 
22033 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
22034 			    ire->ire_gateway_addr,
22035 			    IRE_BROADCAST,
22036 			    ipif_prim,
22037 			    NULL,
22038 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
22039 			    MATCH_IRE_MASK);
22040 
22041 			if (bcast_ire != NULL) {
22042 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
22043 				    "looked up bcast_ire %p\n",
22044 				    (void *)bcast_ire));
22045 				ipif_remove_ire(bcast_ire->ire_ipif,
22046 					bcast_ire);
22047 				ire_delete(bcast_ire);
22048 			}
22049 			ire_refrele(ire_prim);
22050 		}
22051 		ire_refrele(ire_dst);
22052 	}
22053 }
22054 
22055 /*
22056  * IPsec hardware acceleration capabilities related functions.
22057  */
22058 
22059 /*
22060  * Free a per-ill IPsec capabilities structure.
22061  */
22062 static void
22063 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
22064 {
22065 	if (capab->auth_hw_algs != NULL)
22066 		kmem_free(capab->auth_hw_algs, capab->algs_size);
22067 	if (capab->encr_hw_algs != NULL)
22068 		kmem_free(capab->encr_hw_algs, capab->algs_size);
22069 	if (capab->encr_algparm != NULL)
22070 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
22071 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
22072 }
22073 
22074 /*
22075  * Allocate a new per-ill IPsec capabilities structure. This structure
22076  * is specific to an IPsec protocol (AH or ESP). It is implemented as
22077  * an array which specifies, for each algorithm, whether this algorithm
22078  * is supported by the ill or not.
22079  */
22080 static ill_ipsec_capab_t *
22081 ill_ipsec_capab_alloc(void)
22082 {
22083 	ill_ipsec_capab_t *capab;
22084 	uint_t nelems;
22085 
22086 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
22087 	if (capab == NULL)
22088 		return (NULL);
22089 
22090 	/* we need one bit per algorithm */
22091 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
22092 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
22093 
22094 	/* allocate memory to store algorithm flags */
22095 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
22096 	if (capab->encr_hw_algs == NULL)
22097 		goto nomem;
22098 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
22099 	if (capab->auth_hw_algs == NULL)
22100 		goto nomem;
22101 	/*
22102 	 * Leave encr_algparm NULL for now since we won't need it half
22103 	 * the time
22104 	 */
22105 	return (capab);
22106 
22107 nomem:
22108 	ill_ipsec_capab_free(capab);
22109 	return (NULL);
22110 }
22111 
22112 /*
22113  * Resize capability array.  Since we're exclusive, this is OK.
22114  */
22115 static boolean_t
22116 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
22117 {
22118 	ipsec_capab_algparm_t *nalp, *oalp;
22119 	uint32_t olen, nlen;
22120 
22121 	oalp = capab->encr_algparm;
22122 	olen = capab->encr_algparm_size;
22123 
22124 	if (oalp != NULL) {
22125 		if (algid < capab->encr_algparm_end)
22126 			return (B_TRUE);
22127 	}
22128 
22129 	nlen = (algid + 1) * sizeof (*nalp);
22130 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
22131 	if (nalp == NULL)
22132 		return (B_FALSE);
22133 
22134 	if (oalp != NULL) {
22135 		bcopy(oalp, nalp, olen);
22136 		kmem_free(oalp, olen);
22137 	}
22138 	capab->encr_algparm = nalp;
22139 	capab->encr_algparm_size = nlen;
22140 	capab->encr_algparm_end = algid + 1;
22141 
22142 	return (B_TRUE);
22143 }
22144 
22145 /*
22146  * Compare the capabilities of the specified ill with the protocol
22147  * and algorithms specified by the SA passed as argument.
22148  * If they match, returns B_TRUE, B_FALSE if they do not match.
22149  *
22150  * The ill can be passed as a pointer to it, or by specifying its index
22151  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
22152  *
22153  * Called by ipsec_out_is_accelerated() do decide whether an outbound
22154  * packet is eligible for hardware acceleration, and by
22155  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
22156  * to a particular ill.
22157  */
22158 boolean_t
22159 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
22160     ipsa_t *sa)
22161 {
22162 	boolean_t sa_isv6;
22163 	uint_t algid;
22164 	struct ill_ipsec_capab_s *cpp;
22165 	boolean_t need_refrele = B_FALSE;
22166 
22167 	if (ill == NULL) {
22168 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
22169 		    NULL, NULL, NULL);
22170 		if (ill == NULL) {
22171 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
22172 			return (B_FALSE);
22173 		}
22174 		need_refrele = B_TRUE;
22175 	}
22176 
22177 	/*
22178 	 * Use the address length specified by the SA to determine
22179 	 * if it corresponds to a IPv6 address, and fail the matching
22180 	 * if the isv6 flag passed as argument does not match.
22181 	 * Note: this check is used for SADB capability checking before
22182 	 * sending SA information to an ill.
22183 	 */
22184 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
22185 	if (sa_isv6 != ill_isv6)
22186 		/* protocol mismatch */
22187 		goto done;
22188 
22189 	/*
22190 	 * Check if the ill supports the protocol, algorithm(s) and
22191 	 * key size(s) specified by the SA, and get the pointers to
22192 	 * the algorithms supported by the ill.
22193 	 */
22194 	switch (sa->ipsa_type) {
22195 
22196 	case SADB_SATYPE_ESP:
22197 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
22198 			/* ill does not support ESP acceleration */
22199 			goto done;
22200 		cpp = ill->ill_ipsec_capab_esp;
22201 		algid = sa->ipsa_auth_alg;
22202 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
22203 			goto done;
22204 		algid = sa->ipsa_encr_alg;
22205 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
22206 			goto done;
22207 		if (algid < cpp->encr_algparm_end) {
22208 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
22209 			if (sa->ipsa_encrkeybits < alp->minkeylen)
22210 				goto done;
22211 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
22212 				goto done;
22213 		}
22214 		break;
22215 
22216 	case SADB_SATYPE_AH:
22217 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
22218 			/* ill does not support AH acceleration */
22219 			goto done;
22220 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
22221 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
22222 			goto done;
22223 		break;
22224 	}
22225 
22226 	if (need_refrele)
22227 		ill_refrele(ill);
22228 	return (B_TRUE);
22229 done:
22230 	if (need_refrele)
22231 		ill_refrele(ill);
22232 	return (B_FALSE);
22233 }
22234 
22235 
22236 /*
22237  * Add a new ill to the list of IPsec capable ills.
22238  * Called from ill_capability_ipsec_ack() when an ACK was received
22239  * indicating that IPsec hardware processing was enabled for an ill.
22240  *
22241  * ill must point to the ill for which acceleration was enabled.
22242  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
22243  */
22244 static void
22245 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
22246 {
22247 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
22248 	uint_t sa_type;
22249 	uint_t ipproto;
22250 
22251 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
22252 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
22253 
22254 	switch (dl_cap) {
22255 	case DL_CAPAB_IPSEC_AH:
22256 		sa_type = SADB_SATYPE_AH;
22257 		ills = &ipsec_capab_ills_ah;
22258 		ipproto = IPPROTO_AH;
22259 		break;
22260 	case DL_CAPAB_IPSEC_ESP:
22261 		sa_type = SADB_SATYPE_ESP;
22262 		ills = &ipsec_capab_ills_esp;
22263 		ipproto = IPPROTO_ESP;
22264 		break;
22265 	}
22266 
22267 	rw_enter(&ipsec_capab_ills_lock, RW_WRITER);
22268 
22269 	/*
22270 	 * Add ill index to list of hardware accelerators. If
22271 	 * already in list, do nothing.
22272 	 */
22273 	for (cur_ill = *ills; cur_ill != NULL &&
22274 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
22275 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
22276 		;
22277 
22278 	if (cur_ill == NULL) {
22279 		/* if this is a new entry for this ill */
22280 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
22281 		if (new_ill == NULL) {
22282 			rw_exit(&ipsec_capab_ills_lock);
22283 			return;
22284 		}
22285 
22286 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
22287 		new_ill->ill_isv6 = ill->ill_isv6;
22288 		new_ill->next = *ills;
22289 		*ills = new_ill;
22290 	} else if (!sadb_resync) {
22291 		/* not resync'ing SADB and an entry exists for this ill */
22292 		rw_exit(&ipsec_capab_ills_lock);
22293 		return;
22294 	}
22295 
22296 	rw_exit(&ipsec_capab_ills_lock);
22297 
22298 	if (ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
22299 		/*
22300 		 * IPsec module for protocol loaded, initiate dump
22301 		 * of the SADB to this ill.
22302 		 */
22303 		sadb_ill_download(ill, sa_type);
22304 }
22305 
22306 /*
22307  * Remove an ill from the list of IPsec capable ills.
22308  */
22309 static void
22310 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
22311 {
22312 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
22313 
22314 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
22315 	    dl_cap == DL_CAPAB_IPSEC_ESP);
22316 
22317 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipsec_capab_ills_ah :
22318 	    &ipsec_capab_ills_esp;
22319 
22320 	rw_enter(&ipsec_capab_ills_lock, RW_WRITER);
22321 
22322 	prev_ill = NULL;
22323 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
22324 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
22325 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
22326 		;
22327 	if (cur_ill == NULL) {
22328 		/* entry not found */
22329 		rw_exit(&ipsec_capab_ills_lock);
22330 		return;
22331 	}
22332 	if (prev_ill == NULL) {
22333 		/* entry at front of list */
22334 		*ills = NULL;
22335 	} else {
22336 		prev_ill->next = cur_ill->next;
22337 	}
22338 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
22339 	rw_exit(&ipsec_capab_ills_lock);
22340 }
22341 
22342 
22343 /*
22344  * Handling of DL_CONTROL_REQ messages that must be sent down to
22345  * an ill while having exclusive access.
22346  */
22347 /* ARGSUSED */
22348 static void
22349 ill_ipsec_capab_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
22350 {
22351 	ill_t *ill = (ill_t *)q->q_ptr;
22352 
22353 	ill_dlpi_send(ill, mp);
22354 }
22355 
22356 
22357 /*
22358  * Called by SADB to send a DL_CONTROL_REQ message to every ill
22359  * supporting the specified IPsec protocol acceleration.
22360  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
22361  * We free the mblk and, if sa is non-null, release the held referece.
22362  */
22363 void
22364 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa)
22365 {
22366 	ipsec_capab_ill_t *ici, *cur_ici;
22367 	ill_t *ill;
22368 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
22369 
22370 	ici = (sa_type == SADB_SATYPE_AH) ? ipsec_capab_ills_ah :
22371 	    ipsec_capab_ills_esp;
22372 
22373 	rw_enter(&ipsec_capab_ills_lock, RW_READER);
22374 
22375 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
22376 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
22377 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL);
22378 
22379 		/*
22380 		 * Handle the case where the ill goes away while the SADB is
22381 		 * attempting to send messages.  If it's going away, it's
22382 		 * nuking its shadow SADB, so we don't care..
22383 		 */
22384 
22385 		if (ill == NULL)
22386 			continue;
22387 
22388 		if (sa != NULL) {
22389 			/*
22390 			 * Make sure capabilities match before
22391 			 * sending SA to ill.
22392 			 */
22393 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
22394 			    cur_ici->ill_isv6, sa)) {
22395 				ill_refrele(ill);
22396 				continue;
22397 			}
22398 
22399 			mutex_enter(&sa->ipsa_lock);
22400 			sa->ipsa_flags |= IPSA_F_HW;
22401 			mutex_exit(&sa->ipsa_lock);
22402 		}
22403 
22404 		/*
22405 		 * Copy template message, and add it to the front
22406 		 * of the mblk ship list. We want to avoid holding
22407 		 * the ipsec_capab_ills_lock while sending the
22408 		 * message to the ills.
22409 		 *
22410 		 * The b_next and b_prev are temporarily used
22411 		 * to build a list of mblks to be sent down, and to
22412 		 * save the ill to which they must be sent.
22413 		 */
22414 		nmp = copymsg(mp);
22415 		if (nmp == NULL) {
22416 			ill_refrele(ill);
22417 			continue;
22418 		}
22419 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
22420 		nmp->b_next = mp_ship_list;
22421 		mp_ship_list = nmp;
22422 		nmp->b_prev = (mblk_t *)ill;
22423 	}
22424 
22425 	rw_exit(&ipsec_capab_ills_lock);
22426 
22427 	nmp = mp_ship_list;
22428 	while (nmp != NULL) {
22429 		/* restore the mblk to a sane state */
22430 		next_mp = nmp->b_next;
22431 		nmp->b_next = NULL;
22432 		ill = (ill_t *)nmp->b_prev;
22433 		nmp->b_prev = NULL;
22434 
22435 		/*
22436 		 * Ship the mblk to the ill, must be exclusive. Keep the
22437 		 * reference to the ill as qwriter_ip() does a ill_referele().
22438 		 */
22439 		(void) qwriter_ip(NULL, ill, ill->ill_wq, nmp,
22440 		    ill_ipsec_capab_send_writer, NEW_OP, B_TRUE);
22441 
22442 		nmp = next_mp;
22443 	}
22444 
22445 	if (sa != NULL)
22446 		IPSA_REFRELE(sa);
22447 	freemsg(mp);
22448 }
22449 
22450 
22451 /*
22452  * Derive an interface id from the link layer address.
22453  * Knows about IEEE 802 and IEEE EUI-64 mappings.
22454  */
22455 static boolean_t
22456 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
22457 {
22458 	char		*addr;
22459 
22460 	if (phys_length != ETHERADDRL)
22461 		return (B_FALSE);
22462 
22463 	/* Form EUI-64 like address */
22464 	addr = (char *)&v6addr->s6_addr32[2];
22465 	bcopy((char *)phys_addr, addr, 3);
22466 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
22467 	addr[3] = (char)0xff;
22468 	addr[4] = (char)0xfe;
22469 	bcopy((char *)phys_addr + 3, addr + 5, 3);
22470 	return (B_TRUE);
22471 }
22472 
22473 /* ARGSUSED */
22474 static boolean_t
22475 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
22476 {
22477 	return (B_FALSE);
22478 }
22479 
22480 /* ARGSUSED */
22481 static boolean_t
22482 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
22483     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
22484 {
22485 	/*
22486 	 * Multicast address mappings used over Ethernet/802.X.
22487 	 * This address is used as a base for mappings.
22488 	 */
22489 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
22490 	    0x00, 0x00, 0x00};
22491 
22492 	/*
22493 	 * Extract low order 32 bits from IPv6 multicast address.
22494 	 * Or that into the link layer address, starting from the
22495 	 * second byte.
22496 	 */
22497 	*hw_start = 2;
22498 	v6_extract_mask->s6_addr32[0] = 0;
22499 	v6_extract_mask->s6_addr32[1] = 0;
22500 	v6_extract_mask->s6_addr32[2] = 0;
22501 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
22502 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
22503 	return (B_TRUE);
22504 }
22505 
22506 /*
22507  * Indicate by return value whether multicast is supported. If not,
22508  * this code should not touch/change any parameters.
22509  */
22510 /* ARGSUSED */
22511 static boolean_t
22512 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
22513     uint32_t *hw_start, ipaddr_t *extract_mask)
22514 {
22515 	/*
22516 	 * Multicast address mappings used over Ethernet/802.X.
22517 	 * This address is used as a base for mappings.
22518 	 */
22519 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
22520 	    0x00, 0x00, 0x00 };
22521 
22522 	if (phys_length != ETHERADDRL)
22523 		return (B_FALSE);
22524 
22525 	*extract_mask = htonl(0x007fffff);
22526 	*hw_start = 2;
22527 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
22528 	return (B_TRUE);
22529 }
22530 
22531 /*
22532  * Derive IPoIB interface id from the link layer address.
22533  */
22534 static boolean_t
22535 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
22536 {
22537 	char		*addr;
22538 
22539 	if (phys_length != 20)
22540 		return (B_FALSE);
22541 	addr = (char *)&v6addr->s6_addr32[2];
22542 	bcopy(phys_addr + 12, addr, 8);
22543 	/*
22544 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
22545 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
22546 	 * rules. In these cases, the IBA considers these GUIDs to be in
22547 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
22548 	 * required; vendors are required not to assign global EUI-64's
22549 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
22550 	 * of the interface identifier. Whether the GUID is in modified
22551 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
22552 	 * bit set to 1.
22553 	 */
22554 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
22555 	return (B_TRUE);
22556 }
22557 
22558 /*
22559  * Note on mapping from multicast IP addresses to IPoIB multicast link
22560  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
22561  * The format of an IPoIB multicast address is:
22562  *
22563  *  4 byte QPN      Scope Sign.  Pkey
22564  * +--------------------------------------------+
22565  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
22566  * +--------------------------------------------+
22567  *
22568  * The Scope and Pkey components are properties of the IBA port and
22569  * network interface. They can be ascertained from the broadcast address.
22570  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
22571  */
22572 
22573 static boolean_t
22574 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
22575     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
22576 {
22577 	/*
22578 	 * Base IPoIB IPv6 multicast address used for mappings.
22579 	 * Does not contain the IBA scope/Pkey values.
22580 	 */
22581 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
22582 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
22583 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
22584 
22585 	/*
22586 	 * Extract low order 80 bits from IPv6 multicast address.
22587 	 * Or that into the link layer address, starting from the
22588 	 * sixth byte.
22589 	 */
22590 	*hw_start = 6;
22591 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
22592 
22593 	/*
22594 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
22595 	 */
22596 	*(maddr + 5) = *(bphys_addr + 5);
22597 	*(maddr + 8) = *(bphys_addr + 8);
22598 	*(maddr + 9) = *(bphys_addr + 9);
22599 
22600 	v6_extract_mask->s6_addr32[0] = 0;
22601 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
22602 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
22603 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
22604 	return (B_TRUE);
22605 }
22606 
22607 static boolean_t
22608 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
22609     uint32_t *hw_start, ipaddr_t *extract_mask)
22610 {
22611 	/*
22612 	 * Base IPoIB IPv4 multicast address used for mappings.
22613 	 * Does not contain the IBA scope/Pkey values.
22614 	 */
22615 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
22616 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
22617 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
22618 
22619 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
22620 		return (B_FALSE);
22621 
22622 	/*
22623 	 * Extract low order 28 bits from IPv4 multicast address.
22624 	 * Or that into the link layer address, starting from the
22625 	 * sixteenth byte.
22626 	 */
22627 	*extract_mask = htonl(0x0fffffff);
22628 	*hw_start = 16;
22629 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
22630 
22631 	/*
22632 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
22633 	 */
22634 	*(maddr + 5) = *(bphys_addr + 5);
22635 	*(maddr + 8) = *(bphys_addr + 8);
22636 	*(maddr + 9) = *(bphys_addr + 9);
22637 	return (B_TRUE);
22638 }
22639 
22640 /*
22641  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
22642  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
22643  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
22644  * the link-local address is preferred.
22645  */
22646 boolean_t
22647 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
22648 {
22649 	ipif_t	*ipif;
22650 	ipif_t	*maybe_ipif = NULL;
22651 
22652 	mutex_enter(&ill->ill_lock);
22653 	if (ill->ill_state_flags & ILL_CONDEMNED) {
22654 		mutex_exit(&ill->ill_lock);
22655 		if (ipifp != NULL)
22656 			*ipifp = NULL;
22657 		return (B_FALSE);
22658 	}
22659 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
22660 		if (!IPIF_CAN_LOOKUP(ipif))
22661 			continue;
22662 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid)
22663 			continue;
22664 		if ((ipif->ipif_flags & flags) != flags)
22665 			continue;
22666 
22667 		if (ipifp == NULL) {
22668 			mutex_exit(&ill->ill_lock);
22669 			ASSERT(maybe_ipif == NULL);
22670 			return (B_TRUE);
22671 		}
22672 		if (!ill->ill_isv6 ||
22673 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
22674 			ipif_refhold_locked(ipif);
22675 			mutex_exit(&ill->ill_lock);
22676 			*ipifp = ipif;
22677 			return (B_TRUE);
22678 		}
22679 		if (maybe_ipif == NULL)
22680 			maybe_ipif = ipif;
22681 	}
22682 	if (ipifp != NULL) {
22683 		if (maybe_ipif != NULL)
22684 			ipif_refhold_locked(maybe_ipif);
22685 		*ipifp = maybe_ipif;
22686 	}
22687 	mutex_exit(&ill->ill_lock);
22688 	return (maybe_ipif != NULL);
22689 }
22690 
22691 /*
22692  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
22693  */
22694 boolean_t
22695 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
22696 {
22697 	ill_t *illg;
22698 
22699 	/*
22700 	 * We look at the passed-in ill first without grabbing ill_g_lock.
22701 	 */
22702 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
22703 		return (B_TRUE);
22704 	}
22705 	rw_enter(&ill_g_lock, RW_READER);
22706 	if (ill->ill_group == NULL) {
22707 		/* ill not in a group */
22708 		rw_exit(&ill_g_lock);
22709 		return (B_FALSE);
22710 	}
22711 
22712 	/*
22713 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
22714 	 * group. We need to look for an ipif in the zone on all the ills in the
22715 	 * group.
22716 	 */
22717 	illg = ill->ill_group->illgrp_ill;
22718 	do {
22719 		/*
22720 		 * We don't call ipif_lookup_zoneid() on ill as we already know
22721 		 * that it's not there.
22722 		 */
22723 		if (illg != ill &&
22724 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
22725 			break;
22726 		}
22727 	} while ((illg = illg->ill_group_next) != NULL);
22728 	rw_exit(&ill_g_lock);
22729 	return (illg != NULL);
22730 }
22731 
22732 /*
22733  * Heuristic to check if ill has hit the FAILBACK=no case,
22734  * i.e. failover has occured from ill and later interface has recovered,
22735  * but user has configured FAILBACK=no.
22736  * Checks if ill has an usable ipif.
22737  *
22738  * Return values:
22739  *	B_FALSE - ill has no usable ipif, hit FAILBACK=no case
22740  *	B_TRUE	- ill has at least one usable ipif, FAILBACK=no case not hit
22741  */
22742 static boolean_t
22743 ill_has_usable_ipif(ill_t *ill)
22744 {
22745 	ipif_t *ipif;
22746 
22747 	/* Check whether it is in an IPMP group */
22748 	if (ill->ill_phyint->phyint_groupname == NULL)
22749 		return (B_TRUE);
22750 
22751 	if (ill->ill_ipif_up_count == 0)
22752 		return (B_FALSE);
22753 
22754 	for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
22755 		uint64_t flags = ipif->ipif_flags;
22756 
22757 		/*
22758 		 * This ipif is usable if it is IPIF_UP and not a
22759 		 * dedicated test address.  A dedicated test address
22760 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
22761 		 * (note in particular that V6 test addresses are
22762 		 * link-local data addresses and thus are marked
22763 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
22764 		 */
22765 		if ((flags & IPIF_UP) &&
22766 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
22767 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
22768 			return (B_TRUE);
22769 	}
22770 	return (B_FALSE);
22771 }
22772 
22773 /*
22774  * Check if this ill is only being used to send ICMP probes for IPMP
22775  */
22776 boolean_t
22777 ill_is_probeonly(ill_t *ill)
22778 {
22779 	/*
22780 	 * Check if the interface is FAILED, or INACTIVE
22781 	 * or has hit the FAILBACK=no case.
22782 	 */
22783 	if ((ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE)) ||
22784 	    ill_has_usable_ipif(ill) == B_FALSE) {
22785 		return (B_TRUE);
22786 	}
22787 
22788 	return (B_FALSE);
22789 }
22790