xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_if.c (revision 3db86aab)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This file contains the interface control functions for IP.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/stream.h>
35 #include <sys/dlpi.h>
36 #include <sys/stropts.h>
37 #include <sys/strsun.h>
38 #include <sys/sysmacros.h>
39 #include <sys/strlog.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #include <sys/cmn_err.h>
43 #include <sys/kstat.h>
44 #include <sys/debug.h>
45 #include <sys/zone.h>
46 
47 #include <sys/kmem.h>
48 #include <sys/systm.h>
49 #include <sys/param.h>
50 #include <sys/socket.h>
51 #include <sys/isa_defs.h>
52 #include <net/if.h>
53 #include <net/if_arp.h>
54 #include <net/if_types.h>
55 #include <net/if_dl.h>
56 #include <net/route.h>
57 #include <sys/sockio.h>
58 #include <netinet/in.h>
59 #include <netinet/ip6.h>
60 #include <netinet/icmp6.h>
61 #include <netinet/igmp_var.h>
62 #include <sys/strsun.h>
63 #include <sys/policy.h>
64 #include <sys/ethernet.h>
65 
66 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
67 #include <inet/mi.h>
68 #include <inet/nd.h>
69 #include <inet/arp.h>
70 #include <inet/mib2.h>
71 #include <inet/ip.h>
72 #include <inet/ip6.h>
73 #include <inet/ip6_asp.h>
74 #include <inet/tcp.h>
75 #include <inet/ip_multi.h>
76 #include <inet/ip_ire.h>
77 #include <inet/ip_rts.h>
78 #include <inet/ip_ndp.h>
79 #include <inet/ip_if.h>
80 #include <inet/ip_impl.h>
81 #include <inet/tun.h>
82 #include <inet/sctp_ip.h>
83 
84 #include <net/pfkeyv2.h>
85 #include <inet/ipsec_info.h>
86 #include <inet/sadb.h>
87 #include <inet/ipsec_impl.h>
88 #include <sys/iphada.h>
89 
90 
91 #include <netinet/igmp.h>
92 #include <inet/ip_listutils.h>
93 #include <inet/ipclassifier.h>
94 #include <sys/mac.h>
95 
96 #include <sys/systeminfo.h>
97 #include <sys/bootconf.h>
98 
99 #include <sys/tsol/tndb.h>
100 #include <sys/tsol/tnet.h>
101 
102 /* The character which tells where the ill_name ends */
103 #define	IPIF_SEPARATOR_CHAR	':'
104 
105 /* IP ioctl function table entry */
106 typedef struct ipft_s {
107 	int	ipft_cmd;
108 	pfi_t	ipft_pfi;
109 	int	ipft_min_size;
110 	int	ipft_flags;
111 } ipft_t;
112 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
113 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
114 
115 typedef struct ip_sock_ar_s {
116 	union {
117 		area_t	ip_sock_area;
118 		ared_t	ip_sock_ared;
119 		areq_t	ip_sock_areq;
120 	} ip_sock_ar_u;
121 	queue_t	*ip_sock_ar_q;
122 } ip_sock_ar_t;
123 
124 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
125 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
126 		    char *value, caddr_t cp, cred_t *ioc_cr);
127 
128 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
129 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
130 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
131     mblk_t *mp, boolean_t need_up);
132 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
133     mblk_t *mp, boolean_t need_up);
134 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
135     queue_t *q, mblk_t *mp, boolean_t need_up);
136 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
137     mblk_t *mp, boolean_t need_up);
138 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
139     mblk_t *mp);
140 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
141     queue_t *q, mblk_t *mp, boolean_t need_up);
142 static int	ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp,
143     sin_t *sin, boolean_t x_arp_ioctl, boolean_t if_arp_ioctl);
144 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **);
145 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
146 static void	ipsq_flush(ill_t *ill);
147 static void	ipsq_clean_all(ill_t *ill);
148 static void	ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring);
149 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
150     queue_t *q, mblk_t *mp, boolean_t need_up);
151 static void	ipsq_delete(ipsq_t *);
152 
153 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
154 		    boolean_t initialize);
155 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
156 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
157 static void	ipif_delete_cache_ire(ire_t *, char *);
158 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
159 static void	ipif_down_tail(ipif_t *ipif);
160 static void	ipif_free(ipif_t *ipif);
161 static void	ipif_free_tail(ipif_t *ipif);
162 static void	ipif_mask_reply(ipif_t *);
163 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
164 static void	ipif_multicast_down(ipif_t *ipif);
165 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
166 static void	ipif_set_default(ipif_t *ipif);
167 static int	ipif_set_values(queue_t *q, mblk_t *mp,
168     char *interf_name, uint_t *ppa);
169 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
170     queue_t *q);
171 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
172     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
173     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error);
174 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
175 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
176 
177 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
178 static int	ill_arp_off(ill_t *ill);
179 static int	ill_arp_on(ill_t *ill);
180 static void	ill_delete_interface_type(ill_if_t *);
181 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
182 static void	ill_down(ill_t *ill);
183 static void	ill_downi(ire_t *ire, char *ill_arg);
184 static void	ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg);
185 static void	ill_down_tail(ill_t *ill);
186 static void	ill_free_mib(ill_t *ill);
187 static void	ill_glist_delete(ill_t *);
188 static boolean_t ill_has_usable_ipif(ill_t *);
189 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
190 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
191 static void	ill_phyint_free(ill_t *ill);
192 static void	ill_phyint_reinit(ill_t *ill);
193 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
194 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
195 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
196 static void	ill_stq_cache_delete(ire_t *, char *);
197 
198 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
199 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
200 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
201     in6_addr_t *);
202 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
203     ipaddr_t *);
204 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
205 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
206     in6_addr_t *);
207 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
208     ipaddr_t *);
209 
210 static void	ipif_save_ire(ipif_t *, ire_t *);
211 static void	ipif_remove_ire(ipif_t *, ire_t *);
212 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *);
213 static void 	ip_cgtp_bcast_delete(ire_t *);
214 
215 /*
216  * Per-ill IPsec capabilities management.
217  */
218 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
219 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
220 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
221 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
222 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
223 static void ill_capability_proto(ill_t *, int, mblk_t *);
224 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
225     boolean_t);
226 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
227 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
228 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
229 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
230 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
231 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
232 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
233 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
234     dl_capability_sub_t *);
235 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
236 
237 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
238 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
239 static void ill_capability_dls_reset(ill_t *, mblk_t **);
240 static void ill_capability_dls_disable(ill_t *);
241 
242 static void	illgrp_cache_delete(ire_t *, char *);
243 static void	illgrp_delete(ill_t *ill);
244 static void	illgrp_reset_schednext(ill_t *ill);
245 
246 static ill_t	*ill_prev_usesrc(ill_t *);
247 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
248 static void	ill_disband_usesrc_group(ill_t *);
249 
250 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
251 
252 /*
253  * if we go over the memory footprint limit more than once in this msec
254  * interval, we'll start pruning aggressively.
255  */
256 int ip_min_frag_prune_time = 0;
257 
258 /*
259  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
260  * and the IPsec DOI
261  */
262 #define	MAX_IPSEC_ALGS	256
263 
264 #define	BITSPERBYTE	8
265 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
266 
267 #define	IPSEC_ALG_ENABLE(algs, algid) \
268 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
269 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
270 
271 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
272 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
273 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
274 
275 typedef uint8_t ipsec_capab_elem_t;
276 
277 /*
278  * Per-algorithm parameters.  Note that at present, only encryption
279  * algorithms have variable keysize (IKE does not provide a way to negotiate
280  * auth algorithm keysize).
281  *
282  * All sizes here are in bits.
283  */
284 typedef struct
285 {
286 	uint16_t	minkeylen;
287 	uint16_t	maxkeylen;
288 } ipsec_capab_algparm_t;
289 
290 /*
291  * Per-ill capabilities.
292  */
293 struct ill_ipsec_capab_s {
294 	ipsec_capab_elem_t *encr_hw_algs;
295 	ipsec_capab_elem_t *auth_hw_algs;
296 	uint32_t algs_size;	/* size of _hw_algs in bytes */
297 	/* algorithm key lengths */
298 	ipsec_capab_algparm_t *encr_algparm;
299 	uint32_t encr_algparm_size;
300 	uint32_t encr_algparm_end;
301 };
302 
303 /*
304  * List of AH and ESP IPsec acceleration capable ills
305  */
306 typedef struct ipsec_capab_ill_s {
307 	uint_t ill_index;
308 	boolean_t ill_isv6;
309 	struct ipsec_capab_ill_s *next;
310 } ipsec_capab_ill_t;
311 
312 static ipsec_capab_ill_t *ipsec_capab_ills_ah;
313 static ipsec_capab_ill_t *ipsec_capab_ills_esp;
314 krwlock_t ipsec_capab_ills_lock;
315 
316 /*
317  * The field values are larger than strictly necessary for simple
318  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
319  */
320 static area_t	ip_area_template = {
321 	AR_ENTRY_ADD,			/* area_cmd */
322 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
323 					/* area_name_offset */
324 	/* area_name_length temporarily holds this structure length */
325 	sizeof (area_t),			/* area_name_length */
326 	IP_ARP_PROTO_TYPE,		/* area_proto */
327 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
328 	IP_ADDR_LEN,			/* area_proto_addr_length */
329 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
330 					/* area_proto_mask_offset */
331 	0,				/* area_flags */
332 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
333 					/* area_hw_addr_offset */
334 	/* Zero length hw_addr_length means 'use your idea of the address' */
335 	0				/* area_hw_addr_length */
336 };
337 
338 /*
339  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
340  * support
341  */
342 static area_t	ip6_area_template = {
343 	AR_ENTRY_ADD,			/* area_cmd */
344 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
345 					/* area_name_offset */
346 	/* area_name_length temporarily holds this structure length */
347 	sizeof (area_t),			/* area_name_length */
348 	IP_ARP_PROTO_TYPE,		/* area_proto */
349 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
350 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
351 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
352 					/* area_proto_mask_offset */
353 	0,				/* area_flags */
354 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
355 					/* area_hw_addr_offset */
356 	/* Zero length hw_addr_length means 'use your idea of the address' */
357 	0				/* area_hw_addr_length */
358 };
359 
360 static ared_t	ip_ared_template = {
361 	AR_ENTRY_DELETE,
362 	sizeof (ared_t) + IP_ADDR_LEN,
363 	sizeof (ared_t),
364 	IP_ARP_PROTO_TYPE,
365 	sizeof (ared_t),
366 	IP_ADDR_LEN
367 };
368 
369 static ared_t	ip6_ared_template = {
370 	AR_ENTRY_DELETE,
371 	sizeof (ared_t) + IPV6_ADDR_LEN,
372 	sizeof (ared_t),
373 	IP_ARP_PROTO_TYPE,
374 	sizeof (ared_t),
375 	IPV6_ADDR_LEN
376 };
377 
378 /*
379  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
380  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
381  * areq is used).
382  */
383 static areq_t	ip_areq_template = {
384 	AR_ENTRY_QUERY,			/* cmd */
385 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
386 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
387 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
388 	sizeof (areq_t),			/* target addr offset */
389 	IP_ADDR_LEN,			/* target addr_length */
390 	0,				/* flags */
391 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
392 	IP_ADDR_LEN,			/* sender addr length */
393 	6,				/* xmit_count */
394 	1000,				/* (re)xmit_interval in milliseconds */
395 	4				/* max # of requests to buffer */
396 	/* anything else filled in by the code */
397 };
398 
399 static arc_t	ip_aru_template = {
400 	AR_INTERFACE_UP,
401 	sizeof (arc_t),		/* Name offset */
402 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
403 };
404 
405 static arc_t	ip_ard_template = {
406 	AR_INTERFACE_DOWN,
407 	sizeof (arc_t),		/* Name offset */
408 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
409 };
410 
411 static arc_t	ip_aron_template = {
412 	AR_INTERFACE_ON,
413 	sizeof (arc_t),		/* Name offset */
414 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
415 };
416 
417 static arc_t	ip_aroff_template = {
418 	AR_INTERFACE_OFF,
419 	sizeof (arc_t),		/* Name offset */
420 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
421 };
422 
423 
424 static arma_t	ip_arma_multi_template = {
425 	AR_MAPPING_ADD,
426 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
427 				/* Name offset */
428 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
429 	IP_ARP_PROTO_TYPE,
430 	sizeof (arma_t),			/* proto_addr_offset */
431 	IP_ADDR_LEN,				/* proto_addr_length */
432 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
433 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
434 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
435 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
436 	IP_MAX_HW_LEN,				/* hw_addr_length */
437 	0,					/* hw_mapping_start */
438 };
439 
440 static ipft_t	ip_ioctl_ftbl[] = {
441 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
442 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
443 		IPFT_F_NO_REPLY },
444 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
445 		IPFT_F_NO_REPLY },
446 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
447 	{ 0 }
448 };
449 
450 /* Simple ICMP IP Header Template */
451 static ipha_t icmp_ipha = {
452 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
453 };
454 
455 /* Flag descriptors for ip_ipif_report */
456 static nv_t	ipif_nv_tbl[] = {
457 	{ IPIF_UP,		"UP" },
458 	{ IPIF_BROADCAST,	"BROADCAST" },
459 	{ ILLF_DEBUG,		"DEBUG" },
460 	{ PHYI_LOOPBACK,	"LOOPBACK" },
461 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
462 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
463 	{ PHYI_RUNNING,		"RUNNING" },
464 	{ ILLF_NOARP,		"NOARP" },
465 	{ PHYI_PROMISC,		"PROMISC" },
466 	{ PHYI_ALLMULTI,	"ALLMULTI" },
467 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
468 	{ ILLF_MULTICAST,	"MULTICAST" },
469 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
470 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
471 	{ IPIF_DHCPRUNNING,	"DHCP" },
472 	{ IPIF_PRIVATE,		"PRIVATE" },
473 	{ IPIF_NOXMIT,		"NOXMIT" },
474 	{ IPIF_NOLOCAL,		"NOLOCAL" },
475 	{ IPIF_DEPRECATED,	"DEPRECATED" },
476 	{ IPIF_PREFERRED,	"PREFERRED" },
477 	{ IPIF_TEMPORARY,	"TEMPORARY" },
478 	{ IPIF_ADDRCONF,	"ADDRCONF" },
479 	{ PHYI_VIRTUAL,		"VIRTUAL" },
480 	{ ILLF_ROUTER,		"ROUTER" },
481 	{ ILLF_NONUD,		"NONUD" },
482 	{ IPIF_ANYCAST,		"ANYCAST" },
483 	{ ILLF_NORTEXCH,	"NORTEXCH" },
484 	{ ILLF_IPV4,		"IPV4" },
485 	{ ILLF_IPV6,		"IPV6" },
486 	{ IPIF_MIPRUNNING,	"MIP" },
487 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
488 	{ PHYI_FAILED,		"FAILED" },
489 	{ PHYI_STANDBY,		"STANDBY" },
490 	{ PHYI_INACTIVE,	"INACTIVE" },
491 	{ PHYI_OFFLINE,		"OFFLINE" },
492 };
493 
494 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
495 
496 static ip_m_t	ip_m_tbl[] = {
497 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
498 	    ip_ether_v6intfid },
499 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
500 	    ip_nodef_v6intfid },
501 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
502 	    ip_nodef_v6intfid },
503 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
504 	    ip_nodef_v6intfid },
505 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
506 	    ip_ether_v6intfid },
507 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
508 	    ip_ib_v6intfid },
509 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
510 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
511 	    ip_nodef_v6intfid }
512 };
513 
514 static ill_t	ill_null;		/* Empty ILL for init. */
515 char	ipif_loopback_name[] = "lo0";
516 static char *ipv4_forward_suffix = ":ip_forwarding";
517 static char *ipv6_forward_suffix = ":ip6_forwarding";
518 static kstat_t *loopback_ksp = NULL;
519 static	sin6_t	sin6_null;	/* Zero address for quick clears */
520 static	sin_t	sin_null;	/* Zero address for quick clears */
521 static	uint_t	ill_index = 1;	/* Used to assign interface indicies */
522 /* When set search for unused index */
523 static boolean_t ill_index_wrap = B_FALSE;
524 /* When set search for unused ipif_seqid */
525 static ipif_t	ipif_zero;
526 uint_t	ipif_src_random;
527 
528 /*
529  * For details on the protection offered by these locks please refer
530  * to the notes under the Synchronization section at the start of ip.c
531  */
532 krwlock_t	ill_g_lock;		/* The global ill_g_lock */
533 kmutex_t	ip_addr_avail_lock;	/* Address availability check lock */
534 ipsq_t		*ipsq_g_head;		/* List of all ipsq's on the system */
535 
536 krwlock_t	ill_g_usesrc_lock;	/* Protects usesrc related fields */
537 
538 /*
539  * illgrp_head/ifgrp_head is protected by IP's perimeter.
540  */
541 static  ill_group_t *illgrp_head_v4;	/* Head of IPv4 ill groups */
542 ill_group_t *illgrp_head_v6;		/* Head of IPv6 ill groups */
543 
544 ill_g_head_t	ill_g_heads[MAX_G_HEADS];   /* ILL List Head */
545 
546 /*
547  * ppa arena is created after these many
548  * interfaces have been plumbed.
549  */
550 uint_t	ill_no_arena = 12;
551 
552 #pragma align CACHE_ALIGN_SIZE(phyint_g_list)
553 static phyint_list_t phyint_g_list;	/* start of phyint list */
554 
555 /*
556  * Reflects value of FAILBACK variable in IPMP config file
557  * /etc/default/mpathd. Default value is B_TRUE.
558  * Set to B_FALSE if user disabled failback by configuring "FAILBACK=no"
559  * in.mpathd uses SIOCSIPMPFAILBACK ioctl to pass this information to kernel.
560  */
561 static boolean_t ipmp_enable_failback = B_TRUE;
562 
563 /*
564  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
565  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
566  * set through platform specific code (Niagara/Ontario).
567  */
568 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
569 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
570 
571 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
572 
573 static uint_t
574 ipif_rand(void)
575 {
576 	ipif_src_random = ipif_src_random * 1103515245 + 12345;
577 	return ((ipif_src_random >> 16) & 0x7fff);
578 }
579 
580 /*
581  * Allocate per-interface mibs. Only used for ipv6.
582  * Returns true if ok. False otherwise.
583  *  ipsq  may not yet be allocated (loopback case ).
584  */
585 static boolean_t
586 ill_allocate_mibs(ill_t *ill)
587 {
588 	ASSERT(ill->ill_isv6);
589 
590 	/* Already allocated? */
591 	if (ill->ill_ip6_mib != NULL) {
592 		ASSERT(ill->ill_icmp6_mib != NULL);
593 		return (B_TRUE);
594 	}
595 
596 	ill->ill_ip6_mib = kmem_zalloc(sizeof (*ill->ill_ip6_mib),
597 	    KM_NOSLEEP);
598 	if (ill->ill_ip6_mib == NULL) {
599 		return (B_FALSE);
600 	}
601 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
602 	    KM_NOSLEEP);
603 	if (ill->ill_icmp6_mib == NULL) {
604 		kmem_free(ill->ill_ip6_mib, sizeof (*ill->ill_ip6_mib));
605 		ill->ill_ip6_mib = NULL;
606 		return (B_FALSE);
607 	}
608 	/*
609 	 * The ipv6Ifindex and ipv6IfIcmpIndex will be assigned later
610 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
611 	 * -> ill_phyint_reinit
612 	 */
613 	return (B_TRUE);
614 }
615 
616 /*
617  * Common code for preparation of ARP commands.  Two points to remember:
618  * 	1) The ill_name is tacked on at the end of the allocated space so
619  *	   the templates name_offset field must contain the total space
620  *	   to allocate less the name length.
621  *
622  *	2) The templates name_length field should contain the *template*
623  *	   length.  We use it as a parameter to bcopy() and then write
624  *	   the real ill_name_length into the name_length field of the copy.
625  * (Always called as writer.)
626  */
627 mblk_t *
628 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
629 {
630 	arc_t	*arc = (arc_t *)template;
631 	char	*cp;
632 	int	len;
633 	mblk_t	*mp;
634 	uint_t	name_length = ill->ill_name_length;
635 	uint_t	template_len = arc->arc_name_length;
636 
637 	len = arc->arc_name_offset + name_length;
638 	mp = allocb(len, BPRI_HI);
639 	if (mp == NULL)
640 		return (NULL);
641 	cp = (char *)mp->b_rptr;
642 	mp->b_wptr = (uchar_t *)&cp[len];
643 	if (template_len)
644 		bcopy(template, cp, template_len);
645 	if (len > template_len)
646 		bzero(&cp[template_len], len - template_len);
647 	mp->b_datap->db_type = M_PROTO;
648 
649 	arc = (arc_t *)cp;
650 	arc->arc_name_length = name_length;
651 	cp = (char *)arc + arc->arc_name_offset;
652 	bcopy(ill->ill_name, cp, name_length);
653 
654 	if (addr) {
655 		area_t	*area = (area_t *)mp->b_rptr;
656 
657 		cp = (char *)area + area->area_proto_addr_offset;
658 		bcopy(addr, cp, area->area_proto_addr_length);
659 		if (area->area_cmd == AR_ENTRY_ADD) {
660 			cp = (char *)area;
661 			len = area->area_proto_addr_length;
662 			if (area->area_proto_mask_offset)
663 				cp += area->area_proto_mask_offset;
664 			else
665 				cp += area->area_proto_addr_offset + len;
666 			while (len-- > 0)
667 				*cp++ = (char)~0;
668 		}
669 	}
670 	return (mp);
671 }
672 
673 /*
674  * Completely vaporize a lower level tap and all associated interfaces.
675  * ill_delete is called only out of ip_close when the device control
676  * stream is being closed.
677  */
678 void
679 ill_delete(ill_t *ill)
680 {
681 	ipif_t	*ipif;
682 	ill_t	*prev_ill;
683 
684 	/*
685 	 * ill_delete may be forcibly entering the ipsq. The previous
686 	 * ioctl may not have completed and may need to be aborted.
687 	 * ipsq_flush takes care of it. If we don't need to enter the
688 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
689 	 * ill_delete_tail is sufficient.
690 	 */
691 	ipsq_flush(ill);
692 
693 	/*
694 	 * Nuke all interfaces.  ipif_free will take down the interface,
695 	 * remove it from the list, and free the data structure.
696 	 * Walk down the ipif list and remove the logical interfaces
697 	 * first before removing the main ipif. We can't unplumb
698 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
699 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
700 	 * POINTOPOINT.
701 	 *
702 	 * If ill_ipif was not properly initialized (i.e low on memory),
703 	 * then no interfaces to clean up. In this case just clean up the
704 	 * ill.
705 	 */
706 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
707 		ipif_free(ipif);
708 
709 	/*
710 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
711 	 * So nobody can be using this mp now. Free the mp allocated for
712 	 * honoring ILLF_NOARP
713 	 */
714 	freemsg(ill->ill_arp_on_mp);
715 	ill->ill_arp_on_mp = NULL;
716 
717 	/* Clean up msgs on pending upcalls for mrouted */
718 	reset_mrt_ill(ill);
719 
720 	/*
721 	 * ipif_free -> reset_conn_ipif will remove all multicast
722 	 * references for IPv4. For IPv6, we need to do it here as
723 	 * it points only at ills.
724 	 */
725 	reset_conn_ill(ill);
726 
727 	/*
728 	 * ill_down will arrange to blow off any IRE's dependent on this
729 	 * ILL, and shut down fragmentation reassembly.
730 	 */
731 	ill_down(ill);
732 
733 	/* Let SCTP know, so that it can remove this from its list. */
734 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
735 
736 	/*
737 	 * If an address on this ILL is being used as a source address then
738 	 * clear out the pointers in other ILLs that point to this ILL.
739 	 */
740 	rw_enter(&ill_g_usesrc_lock, RW_WRITER);
741 	if (ill->ill_usesrc_grp_next != NULL) {
742 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
743 			ill_disband_usesrc_group(ill);
744 		} else {	/* consumer of the usesrc ILL */
745 			prev_ill = ill_prev_usesrc(ill);
746 			prev_ill->ill_usesrc_grp_next =
747 			    ill->ill_usesrc_grp_next;
748 		}
749 	}
750 	rw_exit(&ill_g_usesrc_lock);
751 }
752 
753 /*
754  * ill_delete_tail is called from ip_modclose after all references
755  * to the closing ill are gone. The wait is done in ip_modclose
756  */
757 void
758 ill_delete_tail(ill_t *ill)
759 {
760 	mblk_t	**mpp;
761 	ipif_t	*ipif;
762 
763 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
764 		ipif_down_tail(ipif);
765 
766 	/*
767 	 * If polling capability is enabled (which signifies direct
768 	 * upcall into IP and driver has ill saved as a handle),
769 	 * we need to make sure that unbind has completed before we
770 	 * let the ill disappear and driver no longer has any reference
771 	 * to this ill.
772 	 */
773 	mutex_enter(&ill->ill_lock);
774 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
775 		cv_wait(&ill->ill_cv, &ill->ill_lock);
776 	mutex_exit(&ill->ill_lock);
777 
778 	/*
779 	 * Clean up polling and soft ring capabilities
780 	 */
781 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
782 		ill_capability_dls_disable(ill);
783 
784 	/*
785 	 * Send the detach if there's one to send (i.e., if we're above a
786 	 * style 2 DLPI driver).
787 	 */
788 	if (ill->ill_detach_mp != NULL) {
789 		ill_dlpi_send(ill, ill->ill_detach_mp);
790 		ill->ill_detach_mp = NULL;
791 	}
792 
793 	if (ill->ill_net_type != IRE_LOOPBACK)
794 		qprocsoff(ill->ill_rq);
795 
796 	/*
797 	 * We do an ipsq_flush once again now. New messages could have
798 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
799 	 * could also have landed up if an ioctl thread had looked up
800 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
801 	 * enqueued the ioctl when we did the ipsq_flush last time.
802 	 */
803 	ipsq_flush(ill);
804 
805 	/*
806 	 * Free capabilities.
807 	 */
808 	if (ill->ill_ipsec_capab_ah != NULL) {
809 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
810 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
811 		ill->ill_ipsec_capab_ah = NULL;
812 	}
813 
814 	if (ill->ill_ipsec_capab_esp != NULL) {
815 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
816 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
817 		ill->ill_ipsec_capab_esp = NULL;
818 	}
819 
820 	if (ill->ill_mdt_capab != NULL) {
821 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
822 		ill->ill_mdt_capab = NULL;
823 	}
824 
825 	if (ill->ill_hcksum_capab != NULL) {
826 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
827 		ill->ill_hcksum_capab = NULL;
828 	}
829 
830 	if (ill->ill_zerocopy_capab != NULL) {
831 		kmem_free(ill->ill_zerocopy_capab,
832 		    sizeof (ill_zerocopy_capab_t));
833 		ill->ill_zerocopy_capab = NULL;
834 	}
835 
836 	if (ill->ill_dls_capab != NULL) {
837 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
838 		ill->ill_dls_capab->ill_unbind_conn = NULL;
839 		kmem_free(ill->ill_dls_capab,
840 		    sizeof (ill_dls_capab_t) +
841 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
842 		ill->ill_dls_capab = NULL;
843 	}
844 
845 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
846 
847 	while (ill->ill_ipif != NULL)
848 		ipif_free_tail(ill->ill_ipif);
849 
850 	ill_down_tail(ill);
851 
852 	/*
853 	 * We have removed all references to ilm from conn and the ones joined
854 	 * within the kernel.
855 	 *
856 	 * We don't walk conns, mrts and ires because
857 	 *
858 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
859 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
860 	 *    ill references.
861 	 */
862 	ASSERT(ilm_walk_ill(ill) == 0);
863 	/*
864 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
865 	 * could free the phyint. No more reference to the phyint after this
866 	 * point.
867 	 */
868 	(void) ill_glist_delete(ill);
869 
870 	rw_enter(&ip_g_nd_lock, RW_WRITER);
871 	if (ill->ill_ndd_name != NULL)
872 		nd_unload(&ip_g_nd, ill->ill_ndd_name);
873 	rw_exit(&ip_g_nd_lock);
874 
875 
876 	if (ill->ill_frag_ptr != NULL) {
877 		uint_t count;
878 
879 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
880 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
881 		}
882 		mi_free(ill->ill_frag_ptr);
883 		ill->ill_frag_ptr = NULL;
884 		ill->ill_frag_hash_tbl = NULL;
885 	}
886 	if (ill->ill_nd_lla_mp != NULL)
887 		freemsg(ill->ill_nd_lla_mp);
888 	/* Free all retained control messages. */
889 	mpp = &ill->ill_first_mp_to_free;
890 	do {
891 		while (mpp[0]) {
892 			mblk_t  *mp;
893 			mblk_t  *mp1;
894 
895 			mp = mpp[0];
896 			mpp[0] = mp->b_next;
897 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
898 				mp1->b_next = NULL;
899 				mp1->b_prev = NULL;
900 			}
901 			freemsg(mp);
902 		}
903 	} while (mpp++ != &ill->ill_last_mp_to_free);
904 
905 	ill_free_mib(ill);
906 	ILL_TRACE_CLEANUP(ill);
907 }
908 
909 static void
910 ill_free_mib(ill_t *ill)
911 {
912 	if (ill->ill_ip6_mib != NULL) {
913 		kmem_free(ill->ill_ip6_mib, sizeof (*ill->ill_ip6_mib));
914 		ill->ill_ip6_mib = NULL;
915 	}
916 	if (ill->ill_icmp6_mib != NULL) {
917 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
918 		ill->ill_icmp6_mib = NULL;
919 	}
920 }
921 
922 /*
923  * Concatenate together a physical address and a sap.
924  *
925  * Sap_lengths are interpreted as follows:
926  *   sap_length == 0	==>	no sap
927  *   sap_length > 0	==>	sap is at the head of the dlpi address
928  *   sap_length < 0	==>	sap is at the tail of the dlpi address
929  */
930 static void
931 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
932     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
933 {
934 	uint16_t sap_addr = (uint16_t)sap_src;
935 
936 	if (sap_length == 0) {
937 		if (phys_src == NULL)
938 			bzero(dst, phys_length);
939 		else
940 			bcopy(phys_src, dst, phys_length);
941 	} else if (sap_length < 0) {
942 		if (phys_src == NULL)
943 			bzero(dst, phys_length);
944 		else
945 			bcopy(phys_src, dst, phys_length);
946 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
947 	} else {
948 		bcopy(&sap_addr, dst, sizeof (sap_addr));
949 		if (phys_src == NULL)
950 			bzero((char *)dst + sap_length, phys_length);
951 		else
952 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
953 	}
954 }
955 
956 /*
957  * Generate a dl_unitdata_req mblk for the device and address given.
958  * addr_length is the length of the physical portion of the address.
959  * If addr is NULL include an all zero address of the specified length.
960  * TRUE? In any case, addr_length is taken to be the entire length of the
961  * dlpi address, including the absolute value of sap_length.
962  */
963 mblk_t *
964 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
965 		t_scalar_t sap_length)
966 {
967 	dl_unitdata_req_t *dlur;
968 	mblk_t	*mp;
969 	t_scalar_t	abs_sap_length;		/* absolute value */
970 
971 	abs_sap_length = ABS(sap_length);
972 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
973 		DL_UNITDATA_REQ);
974 	if (mp == NULL)
975 		return (NULL);
976 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
977 	/* HACK: accomodate incompatible DLPI drivers */
978 	if (addr_length == 8)
979 		addr_length = 6;
980 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
981 	dlur->dl_dest_addr_offset = sizeof (*dlur);
982 	dlur->dl_priority.dl_min = 0;
983 	dlur->dl_priority.dl_max = 0;
984 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
985 	    (uchar_t *)&dlur[1]);
986 	return (mp);
987 }
988 
989 /*
990  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
991  * Return an error if we already have 1 or more ioctls in progress.
992  * This is used only for non-exclusive ioctls. Currently this is used
993  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
994  * and thus need to use ipsq_pending_mp_add.
995  */
996 boolean_t
997 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
998 {
999 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1000 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1001 	/*
1002 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1003 	 */
1004 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1005 	    (add_mp->b_datap->db_type == M_IOCTL));
1006 
1007 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1008 	/*
1009 	 * Return error if the conn has started closing. The conn
1010 	 * could have finished cleaning up the pending mp list,
1011 	 * If so we should not add another mp to the list negating
1012 	 * the cleanup.
1013 	 */
1014 	if (connp->conn_state_flags & CONN_CLOSING)
1015 		return (B_FALSE);
1016 	/*
1017 	 * Add the pending mp to the head of the list, chained by b_next.
1018 	 * Note down the conn on which the ioctl request came, in b_prev.
1019 	 * This will be used to later get the conn, when we get a response
1020 	 * on the ill queue, from some other module (typically arp)
1021 	 */
1022 	add_mp->b_next = (void *)ill->ill_pending_mp;
1023 	add_mp->b_queue = CONNP_TO_WQ(connp);
1024 	ill->ill_pending_mp = add_mp;
1025 	if (connp != NULL)
1026 		connp->conn_oper_pending_ill = ill;
1027 	return (B_TRUE);
1028 }
1029 
1030 /*
1031  * Retrieve the ill_pending_mp and return it. We have to walk the list
1032  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1033  */
1034 mblk_t *
1035 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1036 {
1037 	mblk_t	*prev = NULL;
1038 	mblk_t	*curr = NULL;
1039 	uint_t	id;
1040 	conn_t	*connp;
1041 
1042 	/*
1043 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1044 	 * up the pending mp, but it does not know the ioc_id and
1045 	 * passes in a zero for it.
1046 	 */
1047 	mutex_enter(&ill->ill_lock);
1048 	if (ioc_id != 0)
1049 		*connpp = NULL;
1050 
1051 	/* Search the list for the appropriate ioctl based on ioc_id */
1052 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1053 	    prev = curr, curr = curr->b_next) {
1054 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1055 		connp = Q_TO_CONN(curr->b_queue);
1056 		/* Match based on the ioc_id or based on the conn */
1057 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1058 			break;
1059 	}
1060 
1061 	if (curr != NULL) {
1062 		/* Unlink the mblk from the pending mp list */
1063 		if (prev != NULL) {
1064 			prev->b_next = curr->b_next;
1065 		} else {
1066 			ASSERT(ill->ill_pending_mp == curr);
1067 			ill->ill_pending_mp = curr->b_next;
1068 		}
1069 
1070 		/*
1071 		 * conn refcnt must have been bumped up at the start of
1072 		 * the ioctl. So we can safely access the conn.
1073 		 */
1074 		ASSERT(CONN_Q(curr->b_queue));
1075 		*connpp = Q_TO_CONN(curr->b_queue);
1076 		curr->b_next = NULL;
1077 		curr->b_queue = NULL;
1078 	}
1079 
1080 	mutex_exit(&ill->ill_lock);
1081 
1082 	return (curr);
1083 }
1084 
1085 /*
1086  * Add the pending mp to the list. There can be only 1 pending mp
1087  * in the list. Any exclusive ioctl that needs to wait for a response
1088  * from another module or driver needs to use this function to set
1089  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1090  * the other module/driver. This is also used while waiting for the
1091  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1092  */
1093 boolean_t
1094 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1095     int waitfor)
1096 {
1097 	ipsq_t	*ipsq;
1098 
1099 	ASSERT(IAM_WRITER_IPIF(ipif));
1100 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1101 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1102 	/*
1103 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1104 	 * M_ERROR/M_HANGUP from driver
1105 	 */
1106 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1107 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP));
1108 
1109 	ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1110 	if (connp != NULL) {
1111 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1112 		/*
1113 		 * Return error if the conn has started closing. The conn
1114 		 * could have finished cleaning up the pending mp list,
1115 		 * If so we should not add another mp to the list negating
1116 		 * the cleanup.
1117 		 */
1118 		if (connp->conn_state_flags & CONN_CLOSING)
1119 			return (B_FALSE);
1120 	}
1121 	mutex_enter(&ipsq->ipsq_lock);
1122 	ipsq->ipsq_pending_ipif = ipif;
1123 	/*
1124 	 * Note down the queue in b_queue. This will be returned by
1125 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1126 	 * the processing
1127 	 */
1128 	add_mp->b_next = NULL;
1129 	add_mp->b_queue = q;
1130 	ipsq->ipsq_pending_mp = add_mp;
1131 	ipsq->ipsq_waitfor = waitfor;
1132 	/*
1133 	 * ipsq_current_ipif is needed to restart the operation from
1134 	 * ipif_ill_refrele_tail when the last reference to the ipi/ill
1135 	 * is gone. Since this is not an ioctl ipsq_current_ipif has not
1136 	 * been set until now.
1137 	 */
1138 	if (DB_TYPE(add_mp) == M_ERROR || DB_TYPE(add_mp) == M_HANGUP) {
1139 		ASSERT(ipsq->ipsq_current_ipif == NULL);
1140 		ipsq->ipsq_current_ipif = ipif;
1141 		ipsq->ipsq_last_cmd = DB_TYPE(add_mp);
1142 	}
1143 	if (connp != NULL)
1144 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1145 	mutex_exit(&ipsq->ipsq_lock);
1146 	return (B_TRUE);
1147 }
1148 
1149 /*
1150  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1151  * queued in the list.
1152  */
1153 mblk_t *
1154 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1155 {
1156 	mblk_t	*curr = NULL;
1157 
1158 	mutex_enter(&ipsq->ipsq_lock);
1159 	*connpp = NULL;
1160 	if (ipsq->ipsq_pending_mp == NULL) {
1161 		mutex_exit(&ipsq->ipsq_lock);
1162 		return (NULL);
1163 	}
1164 
1165 	/* There can be only 1 such excl message */
1166 	curr = ipsq->ipsq_pending_mp;
1167 	ASSERT(curr != NULL && curr->b_next == NULL);
1168 	ipsq->ipsq_pending_ipif = NULL;
1169 	ipsq->ipsq_pending_mp = NULL;
1170 	ipsq->ipsq_waitfor = 0;
1171 	mutex_exit(&ipsq->ipsq_lock);
1172 
1173 	if (CONN_Q(curr->b_queue)) {
1174 		/*
1175 		 * This mp did a refhold on the conn, at the start of the ioctl.
1176 		 * So we can safely return a pointer to the conn to the caller.
1177 		 */
1178 		*connpp = Q_TO_CONN(curr->b_queue);
1179 	} else {
1180 		*connpp = NULL;
1181 	}
1182 	curr->b_next = NULL;
1183 	curr->b_prev = NULL;
1184 	return (curr);
1185 }
1186 
1187 /*
1188  * Cleanup the ioctl mp queued in ipsq_pending_mp
1189  * - Called in the ill_delete path
1190  * - Called in the M_ERROR or M_HANGUP path on the ill.
1191  * - Called in the conn close path.
1192  */
1193 boolean_t
1194 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1195 {
1196 	mblk_t	*mp;
1197 	ipsq_t	*ipsq;
1198 	queue_t	*q;
1199 	ipif_t	*ipif;
1200 
1201 	ASSERT(IAM_WRITER_ILL(ill));
1202 	ipsq = ill->ill_phyint->phyint_ipsq;
1203 	mutex_enter(&ipsq->ipsq_lock);
1204 	/*
1205 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1206 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1207 	 * even if it is meant for another ill, since we have to enqueue
1208 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1209 	 * If connp is non-null we are called from the conn close path.
1210 	 */
1211 	mp = ipsq->ipsq_pending_mp;
1212 	if (mp == NULL || (connp != NULL &&
1213 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1214 		mutex_exit(&ipsq->ipsq_lock);
1215 		return (B_FALSE);
1216 	}
1217 	/* Now remove from the ipsq_pending_mp */
1218 	ipsq->ipsq_pending_mp = NULL;
1219 	q = mp->b_queue;
1220 	mp->b_next = NULL;
1221 	mp->b_prev = NULL;
1222 	mp->b_queue = NULL;
1223 
1224 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1225 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1226 	if (ill->ill_move_in_progress) {
1227 		ILL_CLEAR_MOVE(ill);
1228 	} else if (ill->ill_up_ipifs) {
1229 		ill_group_cleanup(ill);
1230 	}
1231 
1232 	ipif = ipsq->ipsq_pending_ipif;
1233 	ipsq->ipsq_pending_ipif = NULL;
1234 	ipsq->ipsq_waitfor = 0;
1235 	ipsq->ipsq_current_ipif = NULL;
1236 	mutex_exit(&ipsq->ipsq_lock);
1237 
1238 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1239 		ip_ioctl_finish(q, mp, ENXIO, connp != NULL ? CONN_CLOSE :
1240 		    NO_COPYOUT, connp != NULL ? ipif : NULL, NULL);
1241 	} else {
1242 		/*
1243 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1244 		 * be just inet_freemsg. we have to restart it
1245 		 * otherwise the thread will be stuck.
1246 		 */
1247 		inet_freemsg(mp);
1248 	}
1249 	return (B_TRUE);
1250 }
1251 
1252 /*
1253  * The ill is closing. Cleanup all the pending mps. Called exclusively
1254  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1255  * knows this ill, and hence nobody can add an mp to this list
1256  */
1257 static void
1258 ill_pending_mp_cleanup(ill_t *ill)
1259 {
1260 	mblk_t	*mp;
1261 	queue_t	*q;
1262 
1263 	ASSERT(IAM_WRITER_ILL(ill));
1264 
1265 	mutex_enter(&ill->ill_lock);
1266 	/*
1267 	 * Every mp on the pending mp list originating from an ioctl
1268 	 * added 1 to the conn refcnt, at the start of the ioctl.
1269 	 * So bump it down now.  See comments in ip_wput_nondata()
1270 	 */
1271 	while (ill->ill_pending_mp != NULL) {
1272 		mp = ill->ill_pending_mp;
1273 		ill->ill_pending_mp = mp->b_next;
1274 		mutex_exit(&ill->ill_lock);
1275 
1276 		q = mp->b_queue;
1277 		ASSERT(CONN_Q(q));
1278 		mp->b_next = NULL;
1279 		mp->b_prev = NULL;
1280 		mp->b_queue = NULL;
1281 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL, NULL);
1282 		mutex_enter(&ill->ill_lock);
1283 	}
1284 	ill->ill_pending_ipif = NULL;
1285 
1286 	mutex_exit(&ill->ill_lock);
1287 }
1288 
1289 /*
1290  * Called in the conn close path and ill delete path
1291  */
1292 static void
1293 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1294 {
1295 	ipsq_t	*ipsq;
1296 	mblk_t	*prev;
1297 	mblk_t	*curr;
1298 	mblk_t	*next;
1299 	queue_t	*q;
1300 	mblk_t	*tmp_list = NULL;
1301 
1302 	ASSERT(IAM_WRITER_ILL(ill));
1303 	if (connp != NULL)
1304 		q = CONNP_TO_WQ(connp);
1305 	else
1306 		q = ill->ill_wq;
1307 
1308 	ipsq = ill->ill_phyint->phyint_ipsq;
1309 	/*
1310 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1311 	 * In the case of ioctl from a conn, there can be only 1 mp
1312 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1313 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1314 	 * ioctls meant for this ill form conn's are not flushed. They will
1315 	 * be processed during ipsq_exit and will not find the ill and will
1316 	 * return error.
1317 	 */
1318 	mutex_enter(&ipsq->ipsq_lock);
1319 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1320 	    curr = next) {
1321 		next = curr->b_next;
1322 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1323 			/* Unlink the mblk from the pending mp list */
1324 			if (prev != NULL) {
1325 				prev->b_next = curr->b_next;
1326 			} else {
1327 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1328 				ipsq->ipsq_xopq_mphead = curr->b_next;
1329 			}
1330 			if (ipsq->ipsq_xopq_mptail == curr)
1331 				ipsq->ipsq_xopq_mptail = prev;
1332 			/*
1333 			 * Create a temporary list and release the ipsq lock
1334 			 * New elements are added to the head of the tmp_list
1335 			 */
1336 			curr->b_next = tmp_list;
1337 			tmp_list = curr;
1338 		} else {
1339 			prev = curr;
1340 		}
1341 	}
1342 	mutex_exit(&ipsq->ipsq_lock);
1343 
1344 	while (tmp_list != NULL) {
1345 		curr = tmp_list;
1346 		tmp_list = curr->b_next;
1347 		curr->b_next = NULL;
1348 		curr->b_prev = NULL;
1349 		curr->b_queue = NULL;
1350 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1351 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1352 			    CONN_CLOSE : NO_COPYOUT, NULL, NULL);
1353 		} else {
1354 			/*
1355 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1356 			 * this can't be just inet_freemsg. we have to
1357 			 * restart it otherwise the thread will be stuck.
1358 			 */
1359 			inet_freemsg(curr);
1360 		}
1361 	}
1362 }
1363 
1364 /*
1365  * This conn has started closing. Cleanup any pending ioctl from this conn.
1366  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1367  */
1368 void
1369 conn_ioctl_cleanup(conn_t *connp)
1370 {
1371 	mblk_t *curr;
1372 	ipsq_t	*ipsq;
1373 	ill_t	*ill;
1374 	boolean_t refheld;
1375 
1376 	/*
1377 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1378 	 * ioctl has not yet started, the mp is pending in the list headed by
1379 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1380 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1381 	 * is currently executing now the mp is not queued anywhere but
1382 	 * conn_oper_pending_ill is null. The conn close will wait
1383 	 * till the conn_ref drops to zero.
1384 	 */
1385 	mutex_enter(&connp->conn_lock);
1386 	ill = connp->conn_oper_pending_ill;
1387 	if (ill == NULL) {
1388 		mutex_exit(&connp->conn_lock);
1389 		return;
1390 	}
1391 
1392 	curr = ill_pending_mp_get(ill, &connp, 0);
1393 	if (curr != NULL) {
1394 		mutex_exit(&connp->conn_lock);
1395 		CONN_DEC_REF(connp);
1396 		inet_freemsg(curr);
1397 		return;
1398 	}
1399 	/*
1400 	 * We may not be able to refhold the ill if the ill/ipif
1401 	 * is changing. But we need to make sure that the ill will
1402 	 * not vanish. So we just bump up the ill_waiter count.
1403 	 */
1404 	refheld = ill_waiter_inc(ill);
1405 	mutex_exit(&connp->conn_lock);
1406 	if (refheld) {
1407 		if (ipsq_enter(ill, B_TRUE)) {
1408 			ill_waiter_dcr(ill);
1409 			/*
1410 			 * Check whether this ioctl has started and is
1411 			 * pending now in ipsq_pending_mp. If it is not
1412 			 * found there then check whether this ioctl has
1413 			 * not even started and is in the ipsq_xopq list.
1414 			 */
1415 			if (!ipsq_pending_mp_cleanup(ill, connp))
1416 				ipsq_xopq_mp_cleanup(ill, connp);
1417 			ipsq = ill->ill_phyint->phyint_ipsq;
1418 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1419 			return;
1420 		}
1421 	}
1422 
1423 	/*
1424 	 * The ill is also closing and we could not bump up the
1425 	 * ill_waiter_count or we could not enter the ipsq. Leave
1426 	 * the cleanup to ill_delete
1427 	 */
1428 	mutex_enter(&connp->conn_lock);
1429 	while (connp->conn_oper_pending_ill != NULL)
1430 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1431 	mutex_exit(&connp->conn_lock);
1432 	if (refheld)
1433 		ill_waiter_dcr(ill);
1434 }
1435 
1436 /*
1437  * ipcl_walk function for cleaning up conn_*_ill fields.
1438  */
1439 static void
1440 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1441 {
1442 	ill_t	*ill = (ill_t *)arg;
1443 	ire_t	*ire;
1444 
1445 	mutex_enter(&connp->conn_lock);
1446 	if (connp->conn_multicast_ill == ill) {
1447 		/* Revert to late binding */
1448 		connp->conn_multicast_ill = NULL;
1449 		connp->conn_orig_multicast_ifindex = 0;
1450 	}
1451 	if (connp->conn_incoming_ill == ill)
1452 		connp->conn_incoming_ill = NULL;
1453 	if (connp->conn_outgoing_ill == ill)
1454 		connp->conn_outgoing_ill = NULL;
1455 	if (connp->conn_outgoing_pill == ill)
1456 		connp->conn_outgoing_pill = NULL;
1457 	if (connp->conn_nofailover_ill == ill)
1458 		connp->conn_nofailover_ill = NULL;
1459 	if (connp->conn_xmit_if_ill == ill)
1460 		connp->conn_xmit_if_ill = NULL;
1461 	if (connp->conn_ire_cache != NULL) {
1462 		ire = connp->conn_ire_cache;
1463 		/*
1464 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1465 		 * interface X and ipif coming from interface Y, if interface
1466 		 * X and Y are part of the same IPMPgroup. Thus whenever
1467 		 * interface X goes down, remove all references to it by
1468 		 * checking both on ire_ipif and ire_stq.
1469 		 */
1470 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1471 		    (ire->ire_type == IRE_CACHE &&
1472 		    ire->ire_stq == ill->ill_wq)) {
1473 			connp->conn_ire_cache = NULL;
1474 			mutex_exit(&connp->conn_lock);
1475 			ire_refrele_notr(ire);
1476 			return;
1477 		}
1478 	}
1479 	mutex_exit(&connp->conn_lock);
1480 
1481 }
1482 
1483 /* ARGSUSED */
1484 void
1485 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1486 {
1487 	ill_t	*ill = q->q_ptr;
1488 	ipif_t	*ipif;
1489 
1490 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1491 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1492 		ipif_down_tail(ipif);
1493 	ill_down_tail(ill);
1494 	freemsg(mp);
1495 	ipsq->ipsq_current_ipif = NULL;
1496 }
1497 
1498 /*
1499  * ill_down_start is called when we want to down this ill and bring it up again
1500  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1501  * all interfaces, but don't tear down any plumbing.
1502  */
1503 boolean_t
1504 ill_down_start(queue_t *q, mblk_t *mp)
1505 {
1506 	ill_t	*ill;
1507 	ipif_t	*ipif;
1508 
1509 	ill = q->q_ptr;
1510 
1511 	ASSERT(IAM_WRITER_ILL(ill));
1512 
1513 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1514 		(void) ipif_down(ipif, NULL, NULL);
1515 
1516 	ill_down(ill);
1517 
1518 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1519 	mutex_enter(&ill->ill_lock);
1520 	/*
1521 	 * Atomically test and add the pending mp if references are
1522 	 * still active.
1523 	 */
1524 	if (!ill_is_quiescent(ill)) {
1525 		/*
1526 		 * Get rid of any pending mps and cleanup. Call will
1527 		 * not fail since we are passing a null connp.
1528 		 */
1529 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1530 		    mp, ILL_DOWN);
1531 		mutex_exit(&ill->ill_lock);
1532 		return (B_FALSE);
1533 	}
1534 	mutex_exit(&ill->ill_lock);
1535 	return (B_TRUE);
1536 }
1537 
1538 static void
1539 ill_down(ill_t *ill)
1540 {
1541 	/* Blow off any IREs dependent on this ILL. */
1542 	ire_walk(ill_downi, (char *)ill);
1543 
1544 	mutex_enter(&ire_mrtun_lock);
1545 	if (ire_mrtun_count != 0) {
1546 		mutex_exit(&ire_mrtun_lock);
1547 		ire_walk_ill_mrtun(0, 0, ill_downi_mrtun_srcif,
1548 		    (char *)ill, NULL);
1549 	} else {
1550 		mutex_exit(&ire_mrtun_lock);
1551 	}
1552 
1553 	/*
1554 	 * If any interface based forwarding table exists
1555 	 * Blow off the ires there dependent on this ill
1556 	 */
1557 	mutex_enter(&ire_srcif_table_lock);
1558 	if (ire_srcif_table_count > 0) {
1559 		mutex_exit(&ire_srcif_table_lock);
1560 		ire_walk_srcif_table_v4(ill_downi_mrtun_srcif, (char *)ill);
1561 	} else {
1562 		mutex_exit(&ire_srcif_table_lock);
1563 	}
1564 
1565 	/* Remove any conn_*_ill depending on this ill */
1566 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill);
1567 
1568 	if (ill->ill_group != NULL) {
1569 		illgrp_delete(ill);
1570 	}
1571 
1572 }
1573 
1574 static void
1575 ill_down_tail(ill_t *ill)
1576 {
1577 	int	i;
1578 
1579 	/* Destroy ill_srcif_table if it exists */
1580 	/* Lock not reqd really because nobody should be able to access */
1581 	mutex_enter(&ill->ill_lock);
1582 	if (ill->ill_srcif_table != NULL) {
1583 		ill->ill_srcif_refcnt = 0;
1584 		for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
1585 			rw_destroy(&ill->ill_srcif_table[i].irb_lock);
1586 		}
1587 		kmem_free(ill->ill_srcif_table,
1588 		    IP_SRCIF_TABLE_SIZE * sizeof (irb_t));
1589 		ill->ill_srcif_table = NULL;
1590 		ill->ill_srcif_refcnt = 0;
1591 		ill->ill_mrtun_refcnt = 0;
1592 	}
1593 	mutex_exit(&ill->ill_lock);
1594 }
1595 
1596 /*
1597  * ire_walk routine used to delete every IRE that depends on queues
1598  * associated with 'ill'.  (Always called as writer.)
1599  */
1600 static void
1601 ill_downi(ire_t *ire, char *ill_arg)
1602 {
1603 	ill_t	*ill = (ill_t *)ill_arg;
1604 
1605 	/*
1606 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1607 	 * interface X and ipif coming from interface Y, if interface
1608 	 * X and Y are part of the same IPMP group. Thus whenever interface
1609 	 * X goes down, remove all references to it by checking both
1610 	 * on ire_ipif and ire_stq.
1611 	 */
1612 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1613 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1614 		ire_delete(ire);
1615 	}
1616 }
1617 
1618 /*
1619  * A seperate routine for deleting revtun and srcif based routes
1620  * are needed because the ires only deleted when the interface
1621  * is unplumbed. Also these ires have ire_in_ill non-null as well.
1622  * we want to keep mobile IP specific code separate.
1623  */
1624 static void
1625 ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg)
1626 {
1627 	ill_t   *ill = (ill_t *)ill_arg;
1628 
1629 	ASSERT(ire->ire_in_ill != NULL);
1630 
1631 	if ((ire->ire_in_ill != NULL && ire->ire_in_ill == ill) ||
1632 	    (ire->ire_stq == ill->ill_wq) || (ire->ire_stq == ill->ill_rq)) {
1633 		ire_delete(ire);
1634 	}
1635 }
1636 
1637 /*
1638  * Remove ire/nce from the fastpath list.
1639  */
1640 void
1641 ill_fastpath_nack(ill_t *ill)
1642 {
1643 	if (ill->ill_isv6) {
1644 		nce_fastpath_list_dispatch(ill, NULL, NULL);
1645 	} else {
1646 		ire_fastpath_list_dispatch(ill, NULL, NULL);
1647 	}
1648 }
1649 
1650 /* Consume an M_IOCACK of the fastpath probe. */
1651 void
1652 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1653 {
1654 	mblk_t	*mp1 = mp;
1655 
1656 	/*
1657 	 * If this was the first attempt turn on the fastpath probing.
1658 	 */
1659 	mutex_enter(&ill->ill_lock);
1660 	if (ill->ill_dlpi_fastpath_state == IDMS_INPROGRESS)
1661 		ill->ill_dlpi_fastpath_state = IDMS_OK;
1662 	mutex_exit(&ill->ill_lock);
1663 
1664 	/* Free the M_IOCACK mblk, hold on to the data */
1665 	mp = mp->b_cont;
1666 	freeb(mp1);
1667 	if (mp == NULL)
1668 		return;
1669 	if (mp->b_cont != NULL) {
1670 		/*
1671 		 * Update all IRE's or NCE's that are waiting for
1672 		 * fastpath update.
1673 		 */
1674 		if (ill->ill_isv6) {
1675 			/*
1676 			 * update nce's in the fastpath list.
1677 			 */
1678 			nce_fastpath_list_dispatch(ill,
1679 			    ndp_fastpath_update, mp);
1680 		} else {
1681 
1682 			/*
1683 			 * update ire's in the fastpath list.
1684 			 */
1685 			ire_fastpath_list_dispatch(ill,
1686 			    ire_fastpath_update, mp);
1687 			/*
1688 			 * Check if we need to traverse reverse tunnel table.
1689 			 * Since there is only single ire_type (IRE_MIPRTUN)
1690 			 * in the table, we don't need to match on ire_type.
1691 			 * We have to check ire_mrtun_count and not the
1692 			 * ill_mrtun_refcnt since ill_mrtun_refcnt is set
1693 			 * on the incoming ill and here we are dealing with
1694 			 * outgoing ill.
1695 			 */
1696 			mutex_enter(&ire_mrtun_lock);
1697 			if (ire_mrtun_count != 0) {
1698 				mutex_exit(&ire_mrtun_lock);
1699 				ire_walk_ill_mrtun(MATCH_IRE_WQ, IRE_MIPRTUN,
1700 				    (void (*)(ire_t *, void *))
1701 					ire_fastpath_update, mp, ill);
1702 			} else {
1703 				mutex_exit(&ire_mrtun_lock);
1704 			}
1705 		}
1706 		mp1 = mp->b_cont;
1707 		freeb(mp);
1708 		mp = mp1;
1709 	} else {
1710 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1711 	}
1712 
1713 	freeb(mp);
1714 }
1715 
1716 /*
1717  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1718  * The data portion of the request is a dl_unitdata_req_t template for
1719  * what we would send downstream in the absence of a fastpath confirmation.
1720  */
1721 int
1722 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1723 {
1724 	struct iocblk	*ioc;
1725 	mblk_t	*mp;
1726 
1727 	if (dlur_mp == NULL)
1728 		return (EINVAL);
1729 
1730 	mutex_enter(&ill->ill_lock);
1731 	switch (ill->ill_dlpi_fastpath_state) {
1732 	case IDMS_FAILED:
1733 		/*
1734 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1735 		 * support it.
1736 		 */
1737 		mutex_exit(&ill->ill_lock);
1738 		return (ENOTSUP);
1739 	case IDMS_UNKNOWN:
1740 		/* This is the first probe */
1741 		ill->ill_dlpi_fastpath_state = IDMS_INPROGRESS;
1742 		break;
1743 	default:
1744 		break;
1745 	}
1746 	mutex_exit(&ill->ill_lock);
1747 
1748 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1749 		return (EAGAIN);
1750 
1751 	mp->b_cont = copyb(dlur_mp);
1752 	if (mp->b_cont == NULL) {
1753 		freeb(mp);
1754 		return (EAGAIN);
1755 	}
1756 
1757 	ioc = (struct iocblk *)mp->b_rptr;
1758 	ioc->ioc_count = msgdsize(mp->b_cont);
1759 
1760 	putnext(ill->ill_wq, mp);
1761 	return (0);
1762 }
1763 
1764 void
1765 ill_capability_probe(ill_t *ill)
1766 {
1767 	/*
1768 	 * Do so only if negotiation is enabled, capabilities are unknown,
1769 	 * and a capability negotiation is not already in progress.
1770 	 */
1771 	if (ill->ill_capab_state != IDMS_UNKNOWN &&
1772 	    ill->ill_capab_state != IDMS_RENEG)
1773 		return;
1774 
1775 	ill->ill_capab_state = IDMS_INPROGRESS;
1776 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1777 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1778 }
1779 
1780 void
1781 ill_capability_reset(ill_t *ill)
1782 {
1783 	mblk_t *sc_mp = NULL;
1784 	mblk_t *tmp;
1785 
1786 	/*
1787 	 * Note here that we reset the state to UNKNOWN, and later send
1788 	 * down the DL_CAPABILITY_REQ without first setting the state to
1789 	 * INPROGRESS.  We do this in order to distinguish the
1790 	 * DL_CAPABILITY_ACK response which may come back in response to
1791 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1792 	 * also handle the case where the driver doesn't send us back
1793 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1794 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1795 	 * features are turned off until the state reaches IDMS_OK.
1796 	 */
1797 	ill->ill_capab_state = IDMS_UNKNOWN;
1798 
1799 	/*
1800 	 * Disable sub-capabilities and request a list of sub-capability
1801 	 * messages which will be sent down to the driver.  Each handler
1802 	 * allocates the corresponding dl_capability_sub_t inside an
1803 	 * mblk, and links it to the existing sc_mp mblk, or return it
1804 	 * as sc_mp if it's the first sub-capability (the passed in
1805 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1806 	 * sc_mp will be pulled-up, before passing it downstream.
1807 	 */
1808 	ill_capability_mdt_reset(ill, &sc_mp);
1809 	ill_capability_hcksum_reset(ill, &sc_mp);
1810 	ill_capability_zerocopy_reset(ill, &sc_mp);
1811 	ill_capability_ipsec_reset(ill, &sc_mp);
1812 	ill_capability_dls_reset(ill, &sc_mp);
1813 
1814 	/* Nothing to send down in order to disable the capabilities? */
1815 	if (sc_mp == NULL)
1816 		return;
1817 
1818 	tmp = msgpullup(sc_mp, -1);
1819 	freemsg(sc_mp);
1820 	if ((sc_mp = tmp) == NULL) {
1821 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1822 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1823 		return;
1824 	}
1825 
1826 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1827 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1828 }
1829 
1830 /*
1831  * Request or set new-style hardware capabilities supported by DLS provider.
1832  */
1833 static void
1834 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1835 {
1836 	mblk_t *mp;
1837 	dl_capability_req_t *capb;
1838 	size_t size = 0;
1839 	uint8_t *ptr;
1840 
1841 	if (reqp != NULL)
1842 		size = MBLKL(reqp);
1843 
1844 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1845 	if (mp == NULL) {
1846 		freemsg(reqp);
1847 		return;
1848 	}
1849 	ptr = mp->b_rptr;
1850 
1851 	capb = (dl_capability_req_t *)ptr;
1852 	ptr += sizeof (dl_capability_req_t);
1853 
1854 	if (reqp != NULL) {
1855 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1856 		capb->dl_sub_length = size;
1857 		bcopy(reqp->b_rptr, ptr, size);
1858 		ptr += size;
1859 		mp->b_cont = reqp->b_cont;
1860 		freeb(reqp);
1861 	}
1862 	ASSERT(ptr == mp->b_wptr);
1863 
1864 	ill_dlpi_send(ill, mp);
1865 }
1866 
1867 static void
1868 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1869 {
1870 	dl_capab_id_t *id_ic;
1871 	uint_t sub_dl_cap = outers->dl_cap;
1872 	dl_capability_sub_t *inners;
1873 	uint8_t *capend;
1874 
1875 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1876 
1877 	/*
1878 	 * Note: range checks here are not absolutely sufficient to
1879 	 * make us robust against malformed messages sent by drivers;
1880 	 * this is in keeping with the rest of IP's dlpi handling.
1881 	 * (Remember, it's coming from something else in the kernel
1882 	 * address space)
1883 	 */
1884 
1885 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1886 	if (capend > mp->b_wptr) {
1887 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1888 		    "malformed sub-capability too long for mblk");
1889 		return;
1890 	}
1891 
1892 	id_ic = (dl_capab_id_t *)(outers + 1);
1893 
1894 	if (outers->dl_length < sizeof (*id_ic) ||
1895 	    (inners = &id_ic->id_subcap,
1896 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1897 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1898 		    "encapsulated capab type %d too long for mblk",
1899 		    inners->dl_cap);
1900 		return;
1901 	}
1902 
1903 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1904 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1905 		    "isn't as expected; pass-thru module(s) detected, "
1906 		    "discarding capability\n", inners->dl_cap));
1907 		return;
1908 	}
1909 
1910 	/* Process the encapsulated sub-capability */
1911 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1912 }
1913 
1914 /*
1915  * Process Multidata Transmit capability negotiation ack received from a
1916  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1917  * DL_CAPABILITY_ACK message.
1918  */
1919 static void
1920 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1921 {
1922 	mblk_t *nmp = NULL;
1923 	dl_capability_req_t *oc;
1924 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1925 	ill_mdt_capab_t **ill_mdt_capab;
1926 	uint_t sub_dl_cap = isub->dl_cap;
1927 	uint8_t *capend;
1928 
1929 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1930 
1931 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1932 
1933 	/*
1934 	 * Note: range checks here are not absolutely sufficient to
1935 	 * make us robust against malformed messages sent by drivers;
1936 	 * this is in keeping with the rest of IP's dlpi handling.
1937 	 * (Remember, it's coming from something else in the kernel
1938 	 * address space)
1939 	 */
1940 
1941 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1942 	if (capend > mp->b_wptr) {
1943 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1944 		    "malformed sub-capability too long for mblk");
1945 		return;
1946 	}
1947 
1948 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1949 
1950 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1951 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1952 		    "unsupported MDT sub-capability (version %d, expected %d)",
1953 		    mdt_ic->mdt_version, MDT_VERSION_2);
1954 		return;
1955 	}
1956 
1957 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1958 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1959 		    "capability isn't as expected; pass-thru module(s) "
1960 		    "detected, discarding capability\n"));
1961 		return;
1962 	}
1963 
1964 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1965 
1966 		if (*ill_mdt_capab == NULL) {
1967 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1968 			    KM_NOSLEEP);
1969 
1970 			if (*ill_mdt_capab == NULL) {
1971 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1972 				    "could not enable MDT version %d "
1973 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1974 				    ill->ill_name);
1975 				return;
1976 			}
1977 		}
1978 
1979 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1980 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1981 		    "header spaces, %d max pld bufs, %d span limit)\n",
1982 		    ill->ill_name, MDT_VERSION_2,
1983 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1984 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1985 
1986 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1987 		(*ill_mdt_capab)->ill_mdt_on = 1;
1988 		/*
1989 		 * Round the following values to the nearest 32-bit; ULP
1990 		 * may further adjust them to accomodate for additional
1991 		 * protocol headers.  We pass these values to ULP during
1992 		 * bind time.
1993 		 */
1994 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1995 		    roundup(mdt_ic->mdt_hdr_head, 4);
1996 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1997 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1998 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1999 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
2000 
2001 		ill->ill_capabilities |= ILL_CAPAB_MDT;
2002 	} else {
2003 		uint_t size;
2004 		uchar_t *rptr;
2005 
2006 		size = sizeof (dl_capability_req_t) +
2007 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
2008 
2009 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2010 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2011 			    "could not enable MDT for %s (ENOMEM)\n",
2012 			    ill->ill_name);
2013 			return;
2014 		}
2015 
2016 		rptr = nmp->b_rptr;
2017 		/* initialize dl_capability_req_t */
2018 		oc = (dl_capability_req_t *)nmp->b_rptr;
2019 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2020 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2021 		    sizeof (dl_capab_mdt_t);
2022 		nmp->b_rptr += sizeof (dl_capability_req_t);
2023 
2024 		/* initialize dl_capability_sub_t */
2025 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2026 		nmp->b_rptr += sizeof (*isub);
2027 
2028 		/* initialize dl_capab_mdt_t */
2029 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2030 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2031 
2032 		nmp->b_rptr = rptr;
2033 
2034 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2035 		    "to enable MDT version %d\n", ill->ill_name,
2036 		    MDT_VERSION_2));
2037 
2038 		/* set ENABLE flag */
2039 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2040 
2041 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2042 		ill_dlpi_send(ill, nmp);
2043 	}
2044 }
2045 
2046 static void
2047 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2048 {
2049 	mblk_t *mp;
2050 	dl_capab_mdt_t *mdt_subcap;
2051 	dl_capability_sub_t *dl_subcap;
2052 	int size;
2053 
2054 	if (!ILL_MDT_CAPABLE(ill))
2055 		return;
2056 
2057 	ASSERT(ill->ill_mdt_capab != NULL);
2058 	/*
2059 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2060 	 * structure since it's possible that another thread is still
2061 	 * referring to it.  The structure only gets deallocated when
2062 	 * we destroy the ill.
2063 	 */
2064 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2065 
2066 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2067 
2068 	mp = allocb(size, BPRI_HI);
2069 	if (mp == NULL) {
2070 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2071 		    "request to disable MDT\n"));
2072 		return;
2073 	}
2074 
2075 	mp->b_wptr = mp->b_rptr + size;
2076 
2077 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2078 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2079 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2080 
2081 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2082 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2083 	mdt_subcap->mdt_flags = 0;
2084 	mdt_subcap->mdt_hdr_head = 0;
2085 	mdt_subcap->mdt_hdr_tail = 0;
2086 
2087 	if (*sc_mp != NULL)
2088 		linkb(*sc_mp, mp);
2089 	else
2090 		*sc_mp = mp;
2091 }
2092 
2093 /*
2094  * Send a DL_NOTIFY_REQ to the specified ill to enable
2095  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2096  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2097  * acceleration.
2098  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2099  */
2100 static boolean_t
2101 ill_enable_promisc_notify(ill_t *ill)
2102 {
2103 	mblk_t *mp;
2104 	dl_notify_req_t *req;
2105 
2106 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2107 
2108 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2109 	if (mp == NULL)
2110 		return (B_FALSE);
2111 
2112 	req = (dl_notify_req_t *)mp->b_rptr;
2113 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2114 	    DL_NOTE_PROMISC_OFF_PHYS;
2115 
2116 	ill_dlpi_send(ill, mp);
2117 
2118 	return (B_TRUE);
2119 }
2120 
2121 
2122 /*
2123  * Allocate an IPsec capability request which will be filled by our
2124  * caller to turn on support for one or more algorithms.
2125  */
2126 static mblk_t *
2127 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2128 {
2129 	mblk_t *nmp;
2130 	dl_capability_req_t	*ocap;
2131 	dl_capab_ipsec_t	*ocip;
2132 	dl_capab_ipsec_t	*icip;
2133 	uint8_t			*ptr;
2134 	icip = (dl_capab_ipsec_t *)(isub + 1);
2135 
2136 	/*
2137 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2138 	 * PROMISC_ON/OFF notification from the provider. We need to
2139 	 * do this before enabling the algorithms to avoid leakage of
2140 	 * cleartext packets.
2141 	 */
2142 
2143 	if (!ill_enable_promisc_notify(ill))
2144 		return (NULL);
2145 
2146 	/*
2147 	 * Allocate new mblk which will contain a new capability
2148 	 * request to enable the capabilities.
2149 	 */
2150 
2151 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2152 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2153 	if (nmp == NULL)
2154 		return (NULL);
2155 
2156 	ptr = nmp->b_rptr;
2157 
2158 	/* initialize dl_capability_req_t */
2159 	ocap = (dl_capability_req_t *)ptr;
2160 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2161 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2162 	ptr += sizeof (dl_capability_req_t);
2163 
2164 	/* initialize dl_capability_sub_t */
2165 	bcopy(isub, ptr, sizeof (*isub));
2166 	ptr += sizeof (*isub);
2167 
2168 	/* initialize dl_capab_ipsec_t */
2169 	ocip = (dl_capab_ipsec_t *)ptr;
2170 	bcopy(icip, ocip, sizeof (*icip));
2171 
2172 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2173 	return (nmp);
2174 }
2175 
2176 /*
2177  * Process an IPsec capability negotiation ack received from a DLS Provider.
2178  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2179  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2180  */
2181 static void
2182 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2183 {
2184 	dl_capab_ipsec_t	*icip;
2185 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2186 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2187 	uint_t cipher, nciphers;
2188 	mblk_t *nmp;
2189 	uint_t alg_len;
2190 	boolean_t need_sadb_dump;
2191 	uint_t sub_dl_cap = isub->dl_cap;
2192 	ill_ipsec_capab_t **ill_capab;
2193 	uint64_t ill_capab_flag;
2194 	uint8_t *capend, *ciphend;
2195 	boolean_t sadb_resync;
2196 
2197 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2198 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2199 
2200 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2201 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2202 		ill_capab_flag = ILL_CAPAB_AH;
2203 	} else {
2204 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2205 		ill_capab_flag = ILL_CAPAB_ESP;
2206 	}
2207 
2208 	/*
2209 	 * If the ill capability structure exists, then this incoming
2210 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2211 	 * If this is so, then we'd need to resynchronize the SADB
2212 	 * after re-enabling the offloaded ciphers.
2213 	 */
2214 	sadb_resync = (*ill_capab != NULL);
2215 
2216 	/*
2217 	 * Note: range checks here are not absolutely sufficient to
2218 	 * make us robust against malformed messages sent by drivers;
2219 	 * this is in keeping with the rest of IP's dlpi handling.
2220 	 * (Remember, it's coming from something else in the kernel
2221 	 * address space)
2222 	 */
2223 
2224 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2225 	if (capend > mp->b_wptr) {
2226 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2227 		    "malformed sub-capability too long for mblk");
2228 		return;
2229 	}
2230 
2231 	/*
2232 	 * There are two types of acks we process here:
2233 	 * 1. acks in reply to a (first form) generic capability req
2234 	 *    (no ENABLE flag set)
2235 	 * 2. acks in reply to a ENABLE capability req.
2236 	 *    (ENABLE flag set)
2237 	 *
2238 	 * We process the subcapability passed as argument as follows:
2239 	 * 1 do initializations
2240 	 *   1.1 initialize nmp = NULL
2241 	 *   1.2 set need_sadb_dump to B_FALSE
2242 	 * 2 for each cipher in subcapability:
2243 	 *   2.1 if ENABLE flag is set:
2244 	 *	2.1.1 update per-ill ipsec capabilities info
2245 	 *	2.1.2 set need_sadb_dump to B_TRUE
2246 	 *   2.2 if ENABLE flag is not set:
2247 	 *	2.2.1 if nmp is NULL:
2248 	 *		2.2.1.1 allocate and initialize nmp
2249 	 *		2.2.1.2 init current pos in nmp
2250 	 *	2.2.2 copy current cipher to current pos in nmp
2251 	 *	2.2.3 set ENABLE flag in nmp
2252 	 *	2.2.4 update current pos
2253 	 * 3 if nmp is not equal to NULL, send enable request
2254 	 *   3.1 send capability request
2255 	 * 4 if need_sadb_dump is B_TRUE
2256 	 *   4.1 enable promiscuous on/off notifications
2257 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2258 	 *	AH or ESP SA's to interface.
2259 	 */
2260 
2261 	nmp = NULL;
2262 	oalg = NULL;
2263 	need_sadb_dump = B_FALSE;
2264 	icip = (dl_capab_ipsec_t *)(isub + 1);
2265 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2266 
2267 	nciphers = icip->cip_nciphers;
2268 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2269 
2270 	if (ciphend > capend) {
2271 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2272 		    "too many ciphers for sub-capability len");
2273 		return;
2274 	}
2275 
2276 	for (cipher = 0; cipher < nciphers; cipher++) {
2277 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2278 
2279 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2280 			/*
2281 			 * TBD: when we provide a way to disable capabilities
2282 			 * from above, need to manage the request-pending state
2283 			 * and fail if we were not expecting this ACK.
2284 			 */
2285 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2286 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2287 
2288 			/*
2289 			 * Update IPsec capabilities for this ill
2290 			 */
2291 
2292 			if (*ill_capab == NULL) {
2293 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2294 				    ("ill_capability_ipsec_ack: "
2295 					"allocating ipsec_capab for ill\n"));
2296 				*ill_capab = ill_ipsec_capab_alloc();
2297 
2298 				if (*ill_capab == NULL) {
2299 					cmn_err(CE_WARN,
2300 					    "ill_capability_ipsec_ack: "
2301 					    "could not enable IPsec Hardware "
2302 					    "acceleration for %s (ENOMEM)\n",
2303 					    ill->ill_name);
2304 					return;
2305 				}
2306 			}
2307 
2308 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2309 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2310 
2311 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2312 				cmn_err(CE_WARN,
2313 				    "ill_capability_ipsec_ack: "
2314 				    "malformed IPsec algorithm id %d",
2315 				    ialg->alg_prim);
2316 				continue;
2317 			}
2318 
2319 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2320 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2321 				    ialg->alg_prim);
2322 			} else {
2323 				ipsec_capab_algparm_t *alp;
2324 
2325 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2326 				    ialg->alg_prim);
2327 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2328 				    ialg->alg_prim)) {
2329 					cmn_err(CE_WARN,
2330 					    "ill_capability_ipsec_ack: "
2331 					    "no space for IPsec alg id %d",
2332 					    ialg->alg_prim);
2333 					continue;
2334 				}
2335 				alp = &((*ill_capab)->encr_algparm[
2336 						ialg->alg_prim]);
2337 				alp->minkeylen = ialg->alg_minbits;
2338 				alp->maxkeylen = ialg->alg_maxbits;
2339 			}
2340 			ill->ill_capabilities |= ill_capab_flag;
2341 			/*
2342 			 * indicate that a capability was enabled, which
2343 			 * will be used below to kick off a SADB dump
2344 			 * to the ill.
2345 			 */
2346 			need_sadb_dump = B_TRUE;
2347 		} else {
2348 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2349 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2350 				ialg->alg_prim));
2351 
2352 			if (nmp == NULL) {
2353 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2354 				if (nmp == NULL) {
2355 					/*
2356 					 * Sending the PROMISC_ON/OFF
2357 					 * notification request failed.
2358 					 * We cannot enable the algorithms
2359 					 * since the Provider will not
2360 					 * notify IP of promiscous mode
2361 					 * changes, which could lead
2362 					 * to leakage of packets.
2363 					 */
2364 					cmn_err(CE_WARN,
2365 					    "ill_capability_ipsec_ack: "
2366 					    "could not enable IPsec Hardware "
2367 					    "acceleration for %s (ENOMEM)\n",
2368 					    ill->ill_name);
2369 					return;
2370 				}
2371 				/* ptr to current output alg specifier */
2372 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2373 			}
2374 
2375 			/*
2376 			 * Copy current alg specifier, set ENABLE
2377 			 * flag, and advance to next output alg.
2378 			 * For now we enable all IPsec capabilities.
2379 			 */
2380 			ASSERT(oalg != NULL);
2381 			bcopy(ialg, oalg, alg_len);
2382 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2383 			nmp->b_wptr += alg_len;
2384 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2385 		}
2386 
2387 		/* move to next input algorithm specifier */
2388 		ialg = (dl_capab_ipsec_alg_t *)
2389 		    ((char *)ialg + alg_len);
2390 	}
2391 
2392 	if (nmp != NULL)
2393 		/*
2394 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2395 		 * IPsec hardware acceleration.
2396 		 */
2397 		ill_dlpi_send(ill, nmp);
2398 
2399 	if (need_sadb_dump)
2400 		/*
2401 		 * An acknowledgement corresponding to a request to
2402 		 * enable acceleration was received, notify SADB.
2403 		 */
2404 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2405 }
2406 
2407 /*
2408  * Given an mblk with enough space in it, create sub-capability entries for
2409  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2410  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2411  * in preparation for the reset the DL_CAPABILITY_REQ message.
2412  */
2413 static void
2414 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2415     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2416 {
2417 	dl_capab_ipsec_t *oipsec;
2418 	dl_capab_ipsec_alg_t *oalg;
2419 	dl_capability_sub_t *dl_subcap;
2420 	int i, k;
2421 
2422 	ASSERT(nciphers > 0);
2423 	ASSERT(ill_cap != NULL);
2424 	ASSERT(mp != NULL);
2425 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2426 
2427 	/* dl_capability_sub_t for "stype" */
2428 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2429 	dl_subcap->dl_cap = stype;
2430 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2431 	mp->b_wptr += sizeof (dl_capability_sub_t);
2432 
2433 	/* dl_capab_ipsec_t for "stype" */
2434 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2435 	oipsec->cip_version = 1;
2436 	oipsec->cip_nciphers = nciphers;
2437 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2438 
2439 	/* create entries for "stype" AUTH ciphers */
2440 	for (i = 0; i < ill_cap->algs_size; i++) {
2441 		for (k = 0; k < BITSPERBYTE; k++) {
2442 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2443 				continue;
2444 
2445 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2446 			bzero((void *)oalg, sizeof (*oalg));
2447 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2448 			oalg->alg_prim = k + (BITSPERBYTE * i);
2449 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2450 		}
2451 	}
2452 	/* create entries for "stype" ENCR ciphers */
2453 	for (i = 0; i < ill_cap->algs_size; i++) {
2454 		for (k = 0; k < BITSPERBYTE; k++) {
2455 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2456 				continue;
2457 
2458 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2459 			bzero((void *)oalg, sizeof (*oalg));
2460 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2461 			oalg->alg_prim = k + (BITSPERBYTE * i);
2462 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2463 		}
2464 	}
2465 }
2466 
2467 /*
2468  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2469  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2470  * POPC instruction, but our macro is more flexible for an arbitrary length
2471  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2472  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2473  * stays that way, we can reduce the number of iterations required.
2474  */
2475 #define	COUNT_1S(val, sum) {					\
2476 	uint8_t x = val & 0xff;					\
2477 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2478 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2479 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2480 }
2481 
2482 /* ARGSUSED */
2483 static void
2484 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2485 {
2486 	mblk_t *mp;
2487 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2488 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2489 	uint64_t ill_capabilities = ill->ill_capabilities;
2490 	int ah_cnt = 0, esp_cnt = 0;
2491 	int ah_len = 0, esp_len = 0;
2492 	int i, size = 0;
2493 
2494 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2495 		return;
2496 
2497 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2498 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2499 
2500 	/* Find out the number of ciphers for AH */
2501 	if (cap_ah != NULL) {
2502 		for (i = 0; i < cap_ah->algs_size; i++) {
2503 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2504 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2505 		}
2506 		if (ah_cnt > 0) {
2507 			size += sizeof (dl_capability_sub_t) +
2508 			    sizeof (dl_capab_ipsec_t);
2509 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2510 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2511 			size += ah_len;
2512 		}
2513 	}
2514 
2515 	/* Find out the number of ciphers for ESP */
2516 	if (cap_esp != NULL) {
2517 		for (i = 0; i < cap_esp->algs_size; i++) {
2518 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2519 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2520 		}
2521 		if (esp_cnt > 0) {
2522 			size += sizeof (dl_capability_sub_t) +
2523 			    sizeof (dl_capab_ipsec_t);
2524 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2525 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2526 			size += esp_len;
2527 		}
2528 	}
2529 
2530 	if (size == 0) {
2531 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2532 		    "there's nothing to reset\n"));
2533 		return;
2534 	}
2535 
2536 	mp = allocb(size, BPRI_HI);
2537 	if (mp == NULL) {
2538 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2539 		    "request to disable IPSEC Hardware Acceleration\n"));
2540 		return;
2541 	}
2542 
2543 	/*
2544 	 * Clear the capability flags for IPSec HA but retain the ill
2545 	 * capability structures since it's possible that another thread
2546 	 * is still referring to them.  The structures only get deallocated
2547 	 * when we destroy the ill.
2548 	 *
2549 	 * Various places check the flags to see if the ill is capable of
2550 	 * hardware acceleration, and by clearing them we ensure that new
2551 	 * outbound IPSec packets are sent down encrypted.
2552 	 */
2553 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2554 
2555 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2556 	if (ah_cnt > 0) {
2557 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2558 		    cap_ah, mp);
2559 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2560 	}
2561 
2562 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2563 	if (esp_cnt > 0) {
2564 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2565 		    cap_esp, mp);
2566 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2567 	}
2568 
2569 	/*
2570 	 * At this point we've composed a bunch of sub-capabilities to be
2571 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2572 	 * by the caller.  Upon receiving this reset message, the driver
2573 	 * must stop inbound decryption (by destroying all inbound SAs)
2574 	 * and let the corresponding packets come in encrypted.
2575 	 */
2576 
2577 	if (*sc_mp != NULL)
2578 		linkb(*sc_mp, mp);
2579 	else
2580 		*sc_mp = mp;
2581 }
2582 
2583 static void
2584 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2585     boolean_t encapsulated)
2586 {
2587 	boolean_t legacy = B_FALSE;
2588 
2589 	/*
2590 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2591 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2592 	 * instructed the driver to disable its advertised capabilities,
2593 	 * so there's no point in accepting any response at this moment.
2594 	 */
2595 	if (ill->ill_capab_state == IDMS_UNKNOWN)
2596 		return;
2597 
2598 	/*
2599 	 * Note that only the following two sub-capabilities may be
2600 	 * considered as "legacy", since their original definitions
2601 	 * do not incorporate the dl_mid_t module ID token, and hence
2602 	 * may require the use of the wrapper sub-capability.
2603 	 */
2604 	switch (subp->dl_cap) {
2605 	case DL_CAPAB_IPSEC_AH:
2606 	case DL_CAPAB_IPSEC_ESP:
2607 		legacy = B_TRUE;
2608 		break;
2609 	}
2610 
2611 	/*
2612 	 * For legacy sub-capabilities which don't incorporate a queue_t
2613 	 * pointer in their structures, discard them if we detect that
2614 	 * there are intermediate modules in between IP and the driver.
2615 	 */
2616 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2617 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2618 		    "%d discarded; %d module(s) present below IP\n",
2619 		    subp->dl_cap, ill->ill_lmod_cnt));
2620 		return;
2621 	}
2622 
2623 	switch (subp->dl_cap) {
2624 	case DL_CAPAB_IPSEC_AH:
2625 	case DL_CAPAB_IPSEC_ESP:
2626 		ill_capability_ipsec_ack(ill, mp, subp);
2627 		break;
2628 	case DL_CAPAB_MDT:
2629 		ill_capability_mdt_ack(ill, mp, subp);
2630 		break;
2631 	case DL_CAPAB_HCKSUM:
2632 		ill_capability_hcksum_ack(ill, mp, subp);
2633 		break;
2634 	case DL_CAPAB_ZEROCOPY:
2635 		ill_capability_zerocopy_ack(ill, mp, subp);
2636 		break;
2637 	case DL_CAPAB_POLL:
2638 		if (!SOFT_RINGS_ENABLED())
2639 			ill_capability_dls_ack(ill, mp, subp);
2640 		break;
2641 	case DL_CAPAB_SOFT_RING:
2642 		if (SOFT_RINGS_ENABLED())
2643 			ill_capability_dls_ack(ill, mp, subp);
2644 		break;
2645 	default:
2646 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2647 		    subp->dl_cap));
2648 	}
2649 }
2650 
2651 /*
2652  * As part of negotiating polling capability, the driver tells us
2653  * the default (or normal) blanking interval and packet threshold
2654  * (the receive timer fires if blanking interval is reached or
2655  * the packet threshold is reached).
2656  *
2657  * As part of manipulating the polling interval, we always use our
2658  * estimated interval (avg service time * number of packets queued
2659  * on the squeue) but we try to blank for a minimum of
2660  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2661  * packet threshold during this time. When we are not in polling mode
2662  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2663  * rr_min_blank_ratio but up the packet cnt by a ratio of
2664  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2665  * possible although for a shorter interval.
2666  */
2667 #define	RR_MAX_BLANK_RATIO	20
2668 #define	RR_MIN_BLANK_RATIO	10
2669 #define	RR_MAX_PKT_CNT_RATIO	3
2670 #define	RR_MIN_PKT_CNT_RATIO	3
2671 
2672 /*
2673  * These can be tuned via /etc/system.
2674  */
2675 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2676 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2677 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2678 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2679 
2680 static mac_resource_handle_t
2681 ill_ring_add(void *arg, mac_resource_t *mrp)
2682 {
2683 	ill_t			*ill = (ill_t *)arg;
2684 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2685 	ill_rx_ring_t		*rx_ring;
2686 	int			ip_rx_index;
2687 
2688 	ASSERT(mrp != NULL);
2689 	if (mrp->mr_type != MAC_RX_FIFO) {
2690 		return (NULL);
2691 	}
2692 	ASSERT(ill != NULL);
2693 	ASSERT(ill->ill_dls_capab != NULL);
2694 
2695 	mutex_enter(&ill->ill_lock);
2696 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2697 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2698 		ASSERT(rx_ring != NULL);
2699 
2700 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2701 			time_t normal_blank_time =
2702 			    mrfp->mrf_normal_blank_time;
2703 			uint_t normal_pkt_cnt =
2704 			    mrfp->mrf_normal_pkt_count;
2705 
2706 			bzero(rx_ring, sizeof (ill_rx_ring_t));
2707 
2708 			rx_ring->rr_blank = mrfp->mrf_blank;
2709 			rx_ring->rr_handle = mrfp->mrf_arg;
2710 			rx_ring->rr_ill = ill;
2711 			rx_ring->rr_normal_blank_time = normal_blank_time;
2712 			rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2713 
2714 			rx_ring->rr_max_blank_time =
2715 			    normal_blank_time * rr_max_blank_ratio;
2716 			rx_ring->rr_min_blank_time =
2717 			    normal_blank_time * rr_min_blank_ratio;
2718 			rx_ring->rr_max_pkt_cnt =
2719 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2720 			rx_ring->rr_min_pkt_cnt =
2721 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2722 
2723 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2724 			mutex_exit(&ill->ill_lock);
2725 
2726 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2727 			    (int), ip_rx_index);
2728 			return ((mac_resource_handle_t)rx_ring);
2729 		}
2730 	}
2731 
2732 	/*
2733 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2734 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2735 	 * should be made configurable. Meanwhile it cause no panic because
2736 	 * driver will pass ip_input a NULL handle which will make
2737 	 * IP allocate the default squeue and Polling mode will not
2738 	 * be used for this ring.
2739 	 */
2740 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2741 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2742 
2743 	mutex_exit(&ill->ill_lock);
2744 	return (NULL);
2745 }
2746 
2747 static boolean_t
2748 ill_capability_dls_init(ill_t *ill)
2749 {
2750 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2751 	conn_t 			*connp;
2752 	size_t			sz;
2753 
2754 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2755 		if (ill_dls == NULL) {
2756 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2757 			    "soft_ring enabled for ill=%s (%p) but data "
2758 			    "structs uninitialized\n", ill->ill_name,
2759 			    (void *)ill);
2760 		}
2761 		return (B_TRUE);
2762 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2763 		if (ill_dls == NULL) {
2764 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2765 			    "polling enabled for ill=%s (%p) but data "
2766 			    "structs uninitialized\n", ill->ill_name,
2767 			(void *)ill);
2768 		}
2769 		return (B_TRUE);
2770 	}
2771 
2772 	if (ill_dls != NULL) {
2773 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2774 		/* Soft_Ring or polling is being re-enabled */
2775 
2776 		connp = ill_dls->ill_unbind_conn;
2777 		ASSERT(rx_ring != NULL);
2778 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2779 		bzero((void *)rx_ring,
2780 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2781 		ill_dls->ill_ring_tbl = rx_ring;
2782 		ill_dls->ill_unbind_conn = connp;
2783 		return (B_TRUE);
2784 	}
2785 
2786 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP)) == NULL)
2787 		return (B_FALSE);
2788 
2789 	sz = sizeof (ill_dls_capab_t);
2790 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2791 
2792 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2793 	if (ill_dls == NULL) {
2794 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2795 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2796 		    (void *)ill);
2797 		CONN_DEC_REF(connp);
2798 		return (B_FALSE);
2799 	}
2800 
2801 	/* Allocate space to hold ring table */
2802 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2803 	ill->ill_dls_capab = ill_dls;
2804 	ill_dls->ill_unbind_conn = connp;
2805 	return (B_TRUE);
2806 }
2807 
2808 /*
2809  * ill_capability_dls_disable: disable soft_ring and/or polling
2810  * capability. Since any of the rings might already be in use, need
2811  * to call ipsq_clean_all() which gets behind the squeue to disable
2812  * direct calls if necessary.
2813  */
2814 static void
2815 ill_capability_dls_disable(ill_t *ill)
2816 {
2817 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2818 
2819 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2820 		ipsq_clean_all(ill);
2821 		ill_dls->ill_tx = NULL;
2822 		ill_dls->ill_tx_handle = NULL;
2823 		ill_dls->ill_dls_change_status = NULL;
2824 		ill_dls->ill_dls_bind = NULL;
2825 		ill_dls->ill_dls_unbind = NULL;
2826 	}
2827 
2828 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2829 }
2830 
2831 static void
2832 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2833     dl_capability_sub_t *isub)
2834 {
2835 	uint_t			size;
2836 	uchar_t			*rptr;
2837 	dl_capab_dls_t	dls, *odls;
2838 	ill_dls_capab_t	*ill_dls;
2839 	mblk_t			*nmp = NULL;
2840 	dl_capability_req_t	*ocap;
2841 	uint_t			sub_dl_cap = isub->dl_cap;
2842 
2843 	if (!ill_capability_dls_init(ill))
2844 		return;
2845 	ill_dls = ill->ill_dls_capab;
2846 
2847 	/* Copy locally to get the members aligned */
2848 	bcopy((void *)idls, (void *)&dls,
2849 	    sizeof (dl_capab_dls_t));
2850 
2851 	/* Get the tx function and handle from dld */
2852 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2853 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2854 
2855 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2856 		ill_dls->ill_dls_change_status =
2857 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2858 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2859 		ill_dls->ill_dls_unbind =
2860 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2861 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2862 	}
2863 
2864 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2865 	    isub->dl_length;
2866 
2867 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2868 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2869 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2870 		    ill->ill_name, (void *)ill);
2871 		return;
2872 	}
2873 
2874 	/* initialize dl_capability_req_t */
2875 	rptr = nmp->b_rptr;
2876 	ocap = (dl_capability_req_t *)rptr;
2877 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2878 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2879 	rptr += sizeof (dl_capability_req_t);
2880 
2881 	/* initialize dl_capability_sub_t */
2882 	bcopy(isub, rptr, sizeof (*isub));
2883 	rptr += sizeof (*isub);
2884 
2885 	odls = (dl_capab_dls_t *)rptr;
2886 	rptr += sizeof (dl_capab_dls_t);
2887 
2888 	/* initialize dl_capab_dls_t to be sent down */
2889 	dls.dls_rx_handle = (uintptr_t)ill;
2890 	dls.dls_rx = (uintptr_t)ip_input;
2891 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2892 
2893 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2894 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2895 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2896 		dls.dls_flags = SOFT_RING_ENABLE;
2897 	} else {
2898 		dls.dls_flags = POLL_ENABLE;
2899 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2900 		    "to enable polling\n", ill->ill_name));
2901 	}
2902 	bcopy((void *)&dls, (void *)odls,
2903 	    sizeof (dl_capab_dls_t));
2904 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2905 	/*
2906 	 * nmp points to a DL_CAPABILITY_REQ message to
2907 	 * enable either soft_ring or polling
2908 	 */
2909 	ill_dlpi_send(ill, nmp);
2910 }
2911 
2912 static void
2913 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2914 {
2915 	mblk_t *mp;
2916 	dl_capab_dls_t *idls;
2917 	dl_capability_sub_t *dl_subcap;
2918 	int size;
2919 
2920 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2921 		return;
2922 
2923 	ASSERT(ill->ill_dls_capab != NULL);
2924 
2925 	size = sizeof (*dl_subcap) + sizeof (*idls);
2926 
2927 	mp = allocb(size, BPRI_HI);
2928 	if (mp == NULL) {
2929 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2930 		    "request to disable soft_ring\n"));
2931 		return;
2932 	}
2933 
2934 	mp->b_wptr = mp->b_rptr + size;
2935 
2936 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2937 	dl_subcap->dl_length = sizeof (*idls);
2938 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2939 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2940 	else
2941 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2942 
2943 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2944 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2945 		idls->dls_flags = SOFT_RING_DISABLE;
2946 	else
2947 		idls->dls_flags = POLL_DISABLE;
2948 
2949 	if (*sc_mp != NULL)
2950 		linkb(*sc_mp, mp);
2951 	else
2952 		*sc_mp = mp;
2953 }
2954 
2955 /*
2956  * Process a soft_ring/poll capability negotiation ack received
2957  * from a DLS Provider.isub must point to the sub-capability
2958  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
2959  */
2960 static void
2961 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2962 {
2963 	dl_capab_dls_t		*idls;
2964 	uint_t			sub_dl_cap = isub->dl_cap;
2965 	uint8_t			*capend;
2966 
2967 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
2968 	    sub_dl_cap == DL_CAPAB_POLL);
2969 
2970 	if (ill->ill_isv6)
2971 		return;
2972 
2973 	/*
2974 	 * Note: range checks here are not absolutely sufficient to
2975 	 * make us robust against malformed messages sent by drivers;
2976 	 * this is in keeping with the rest of IP's dlpi handling.
2977 	 * (Remember, it's coming from something else in the kernel
2978 	 * address space)
2979 	 */
2980 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2981 	if (capend > mp->b_wptr) {
2982 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
2983 		    "malformed sub-capability too long for mblk");
2984 		return;
2985 	}
2986 
2987 	/*
2988 	 * There are two types of acks we process here:
2989 	 * 1. acks in reply to a (first form) generic capability req
2990 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
2991 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
2992 	 *    capability req.
2993 	 */
2994 	idls = (dl_capab_dls_t *)(isub + 1);
2995 
2996 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
2997 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
2998 		    "capability isn't as expected; pass-thru "
2999 		    "module(s) detected, discarding capability\n"));
3000 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
3001 			/*
3002 			 * This is a capability renegotitation case.
3003 			 * The interface better be unusable at this
3004 			 * point other wise bad things will happen
3005 			 * if we disable direct calls on a running
3006 			 * and up interface.
3007 			 */
3008 			ill_capability_dls_disable(ill);
3009 		}
3010 		return;
3011 	}
3012 
3013 	switch (idls->dls_flags) {
3014 	default:
3015 		/* Disable if unknown flag */
3016 	case SOFT_RING_DISABLE:
3017 	case POLL_DISABLE:
3018 		ill_capability_dls_disable(ill);
3019 		break;
3020 	case SOFT_RING_CAPABLE:
3021 	case POLL_CAPABLE:
3022 		/*
3023 		 * If the capability was already enabled, its safe
3024 		 * to disable it first to get rid of stale information
3025 		 * and then start enabling it again.
3026 		 */
3027 		ill_capability_dls_disable(ill);
3028 		ill_capability_dls_capable(ill, idls, isub);
3029 		break;
3030 	case SOFT_RING_ENABLE:
3031 	case POLL_ENABLE:
3032 		mutex_enter(&ill->ill_lock);
3033 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3034 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3035 			ASSERT(ill->ill_dls_capab != NULL);
3036 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3037 		}
3038 		if (sub_dl_cap == DL_CAPAB_POLL &&
3039 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3040 			ASSERT(ill->ill_dls_capab != NULL);
3041 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3042 			ip1dbg(("ill_capability_dls_ack: interface %s "
3043 			    "has enabled polling\n", ill->ill_name));
3044 		}
3045 		mutex_exit(&ill->ill_lock);
3046 		break;
3047 	}
3048 }
3049 
3050 /*
3051  * Process a hardware checksum offload capability negotiation ack received
3052  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3053  * of a DL_CAPABILITY_ACK message.
3054  */
3055 static void
3056 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3057 {
3058 	dl_capability_req_t	*ocap;
3059 	dl_capab_hcksum_t	*ihck, *ohck;
3060 	ill_hcksum_capab_t	**ill_hcksum;
3061 	mblk_t			*nmp = NULL;
3062 	uint_t			sub_dl_cap = isub->dl_cap;
3063 	uint8_t			*capend;
3064 
3065 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3066 
3067 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3068 
3069 	/*
3070 	 * Note: range checks here are not absolutely sufficient to
3071 	 * make us robust against malformed messages sent by drivers;
3072 	 * this is in keeping with the rest of IP's dlpi handling.
3073 	 * (Remember, it's coming from something else in the kernel
3074 	 * address space)
3075 	 */
3076 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3077 	if (capend > mp->b_wptr) {
3078 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3079 		    "malformed sub-capability too long for mblk");
3080 		return;
3081 	}
3082 
3083 	/*
3084 	 * There are two types of acks we process here:
3085 	 * 1. acks in reply to a (first form) generic capability req
3086 	 *    (no ENABLE flag set)
3087 	 * 2. acks in reply to a ENABLE capability req.
3088 	 *    (ENABLE flag set)
3089 	 */
3090 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3091 
3092 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3093 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3094 		    "unsupported hardware checksum "
3095 		    "sub-capability (version %d, expected %d)",
3096 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3097 		return;
3098 	}
3099 
3100 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3101 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3102 		    "checksum capability isn't as expected; pass-thru "
3103 		    "module(s) detected, discarding capability\n"));
3104 		return;
3105 	}
3106 
3107 #define	CURR_HCKSUM_CAPAB				\
3108 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3109 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3110 
3111 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3112 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3113 		/* do ENABLE processing */
3114 		if (*ill_hcksum == NULL) {
3115 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3116 			    KM_NOSLEEP);
3117 
3118 			if (*ill_hcksum == NULL) {
3119 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3120 				    "could not enable hcksum version %d "
3121 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3122 				    ill->ill_name);
3123 				return;
3124 			}
3125 		}
3126 
3127 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3128 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3129 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3130 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3131 		    "has enabled hardware checksumming\n ",
3132 		    ill->ill_name));
3133 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3134 		/*
3135 		 * Enabling hardware checksum offload
3136 		 * Currently IP supports {TCP,UDP}/IPv4
3137 		 * partial and full cksum offload and
3138 		 * IPv4 header checksum offload.
3139 		 * Allocate new mblk which will
3140 		 * contain a new capability request
3141 		 * to enable hardware checksum offload.
3142 		 */
3143 		uint_t	size;
3144 		uchar_t	*rptr;
3145 
3146 		size = sizeof (dl_capability_req_t) +
3147 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3148 
3149 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3150 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3151 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3152 			    ill->ill_name);
3153 			return;
3154 		}
3155 
3156 		rptr = nmp->b_rptr;
3157 		/* initialize dl_capability_req_t */
3158 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3159 		ocap->dl_sub_offset =
3160 		    sizeof (dl_capability_req_t);
3161 		ocap->dl_sub_length =
3162 		    sizeof (dl_capability_sub_t) +
3163 		    isub->dl_length;
3164 		nmp->b_rptr += sizeof (dl_capability_req_t);
3165 
3166 		/* initialize dl_capability_sub_t */
3167 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3168 		nmp->b_rptr += sizeof (*isub);
3169 
3170 		/* initialize dl_capab_hcksum_t */
3171 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3172 		bcopy(ihck, ohck, sizeof (*ihck));
3173 
3174 		nmp->b_rptr = rptr;
3175 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3176 
3177 		/* Set ENABLE flag */
3178 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3179 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3180 
3181 		/*
3182 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3183 		 * hardware checksum acceleration.
3184 		 */
3185 		ill_dlpi_send(ill, nmp);
3186 	} else {
3187 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3188 		    "advertised %x hardware checksum capability flags\n",
3189 		    ill->ill_name, ihck->hcksum_txflags));
3190 	}
3191 }
3192 
3193 static void
3194 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3195 {
3196 	mblk_t *mp;
3197 	dl_capab_hcksum_t *hck_subcap;
3198 	dl_capability_sub_t *dl_subcap;
3199 	int size;
3200 
3201 	if (!ILL_HCKSUM_CAPABLE(ill))
3202 		return;
3203 
3204 	ASSERT(ill->ill_hcksum_capab != NULL);
3205 	/*
3206 	 * Clear the capability flag for hardware checksum offload but
3207 	 * retain the ill_hcksum_capab structure since it's possible that
3208 	 * another thread is still referring to it.  The structure only
3209 	 * gets deallocated when we destroy the ill.
3210 	 */
3211 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3212 
3213 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3214 
3215 	mp = allocb(size, BPRI_HI);
3216 	if (mp == NULL) {
3217 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3218 		    "request to disable hardware checksum offload\n"));
3219 		return;
3220 	}
3221 
3222 	mp->b_wptr = mp->b_rptr + size;
3223 
3224 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3225 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3226 	dl_subcap->dl_length = sizeof (*hck_subcap);
3227 
3228 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3229 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3230 	hck_subcap->hcksum_txflags = 0;
3231 
3232 	if (*sc_mp != NULL)
3233 		linkb(*sc_mp, mp);
3234 	else
3235 		*sc_mp = mp;
3236 }
3237 
3238 static void
3239 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3240 {
3241 	mblk_t *nmp = NULL;
3242 	dl_capability_req_t *oc;
3243 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3244 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3245 	uint_t sub_dl_cap = isub->dl_cap;
3246 	uint8_t *capend;
3247 
3248 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3249 
3250 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3251 
3252 	/*
3253 	 * Note: range checks here are not absolutely sufficient to
3254 	 * make us robust against malformed messages sent by drivers;
3255 	 * this is in keeping with the rest of IP's dlpi handling.
3256 	 * (Remember, it's coming from something else in the kernel
3257 	 * address space)
3258 	 */
3259 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3260 	if (capend > mp->b_wptr) {
3261 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3262 		    "malformed sub-capability too long for mblk");
3263 		return;
3264 	}
3265 
3266 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3267 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3268 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3269 		    "unsupported ZEROCOPY sub-capability (version %d, "
3270 		    "expected %d)", zc_ic->zerocopy_version,
3271 		    ZEROCOPY_VERSION_1);
3272 		return;
3273 	}
3274 
3275 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3276 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3277 		    "capability isn't as expected; pass-thru module(s) "
3278 		    "detected, discarding capability\n"));
3279 		return;
3280 	}
3281 
3282 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3283 		if (*ill_zerocopy_capab == NULL) {
3284 			*ill_zerocopy_capab =
3285 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3286 			    KM_NOSLEEP);
3287 
3288 			if (*ill_zerocopy_capab == NULL) {
3289 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3290 				    "could not enable Zero-copy version %d "
3291 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3292 				    ill->ill_name);
3293 				return;
3294 			}
3295 		}
3296 
3297 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3298 		    "supports Zero-copy version %d\n", ill->ill_name,
3299 		    ZEROCOPY_VERSION_1));
3300 
3301 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3302 		    zc_ic->zerocopy_version;
3303 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3304 		    zc_ic->zerocopy_flags;
3305 
3306 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3307 	} else {
3308 		uint_t size;
3309 		uchar_t *rptr;
3310 
3311 		size = sizeof (dl_capability_req_t) +
3312 		    sizeof (dl_capability_sub_t) +
3313 		    sizeof (dl_capab_zerocopy_t);
3314 
3315 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3316 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3317 			    "could not enable zerocopy for %s (ENOMEM)\n",
3318 			    ill->ill_name);
3319 			return;
3320 		}
3321 
3322 		rptr = nmp->b_rptr;
3323 		/* initialize dl_capability_req_t */
3324 		oc = (dl_capability_req_t *)rptr;
3325 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3326 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3327 		    sizeof (dl_capab_zerocopy_t);
3328 		rptr += sizeof (dl_capability_req_t);
3329 
3330 		/* initialize dl_capability_sub_t */
3331 		bcopy(isub, rptr, sizeof (*isub));
3332 		rptr += sizeof (*isub);
3333 
3334 		/* initialize dl_capab_zerocopy_t */
3335 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3336 		*zc_oc = *zc_ic;
3337 
3338 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3339 		    "to enable zero-copy version %d\n", ill->ill_name,
3340 		    ZEROCOPY_VERSION_1));
3341 
3342 		/* set VMSAFE_MEM flag */
3343 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3344 
3345 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3346 		ill_dlpi_send(ill, nmp);
3347 	}
3348 }
3349 
3350 static void
3351 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3352 {
3353 	mblk_t *mp;
3354 	dl_capab_zerocopy_t *zerocopy_subcap;
3355 	dl_capability_sub_t *dl_subcap;
3356 	int size;
3357 
3358 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3359 		return;
3360 
3361 	ASSERT(ill->ill_zerocopy_capab != NULL);
3362 	/*
3363 	 * Clear the capability flag for Zero-copy but retain the
3364 	 * ill_zerocopy_capab structure since it's possible that another
3365 	 * thread is still referring to it.  The structure only gets
3366 	 * deallocated when we destroy the ill.
3367 	 */
3368 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3369 
3370 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3371 
3372 	mp = allocb(size, BPRI_HI);
3373 	if (mp == NULL) {
3374 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3375 		    "request to disable Zero-copy\n"));
3376 		return;
3377 	}
3378 
3379 	mp->b_wptr = mp->b_rptr + size;
3380 
3381 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3382 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3383 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3384 
3385 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3386 	zerocopy_subcap->zerocopy_version =
3387 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3388 	zerocopy_subcap->zerocopy_flags = 0;
3389 
3390 	if (*sc_mp != NULL)
3391 		linkb(*sc_mp, mp);
3392 	else
3393 		*sc_mp = mp;
3394 }
3395 
3396 /*
3397  * Consume a new-style hardware capabilities negotiation ack.
3398  * Called from ip_rput_dlpi_writer().
3399  */
3400 void
3401 ill_capability_ack(ill_t *ill, mblk_t *mp)
3402 {
3403 	dl_capability_ack_t *capp;
3404 	dl_capability_sub_t *subp, *endp;
3405 
3406 	if (ill->ill_capab_state == IDMS_INPROGRESS)
3407 		ill->ill_capab_state = IDMS_OK;
3408 
3409 	capp = (dl_capability_ack_t *)mp->b_rptr;
3410 
3411 	if (capp->dl_sub_length == 0)
3412 		/* no new-style capabilities */
3413 		return;
3414 
3415 	/* make sure the driver supplied correct dl_sub_length */
3416 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3417 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3418 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3419 		return;
3420 	}
3421 
3422 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3423 	/*
3424 	 * There are sub-capabilities. Process the ones we know about.
3425 	 * Loop until we don't have room for another sub-cap header..
3426 	 */
3427 	for (subp = SC(capp, capp->dl_sub_offset),
3428 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3429 	    subp <= endp;
3430 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3431 
3432 		switch (subp->dl_cap) {
3433 		case DL_CAPAB_ID_WRAPPER:
3434 			ill_capability_id_ack(ill, mp, subp);
3435 			break;
3436 		default:
3437 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3438 			break;
3439 		}
3440 	}
3441 #undef SC
3442 }
3443 
3444 /*
3445  * This routine is called to scan the fragmentation reassembly table for
3446  * the specified ILL for any packets that are starting to smell.
3447  * dead_interval is the maximum time in seconds that will be tolerated.  It
3448  * will either be the value specified in ip_g_frag_timeout, or zero if the
3449  * ILL is shutting down and it is time to blow everything off.
3450  *
3451  * It returns the number of seconds (as a time_t) that the next frag timer
3452  * should be scheduled for, 0 meaning that the timer doesn't need to be
3453  * re-started.  Note that the method of calculating next_timeout isn't
3454  * entirely accurate since time will flow between the time we grab
3455  * current_time and the time we schedule the next timeout.  This isn't a
3456  * big problem since this is the timer for sending an ICMP reassembly time
3457  * exceeded messages, and it doesn't have to be exactly accurate.
3458  *
3459  * This function is
3460  * sometimes called as writer, although this is not required.
3461  */
3462 time_t
3463 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3464 {
3465 	ipfb_t	*ipfb;
3466 	ipfb_t	*endp;
3467 	ipf_t	*ipf;
3468 	ipf_t	*ipfnext;
3469 	mblk_t	*mp;
3470 	time_t	current_time = gethrestime_sec();
3471 	time_t	next_timeout = 0;
3472 	uint32_t	hdr_length;
3473 	mblk_t	*send_icmp_head;
3474 	mblk_t	*send_icmp_head_v6;
3475 
3476 	ipfb = ill->ill_frag_hash_tbl;
3477 	if (ipfb == NULL)
3478 		return (B_FALSE);
3479 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3480 	/* Walk the frag hash table. */
3481 	for (; ipfb < endp; ipfb++) {
3482 		send_icmp_head = NULL;
3483 		send_icmp_head_v6 = NULL;
3484 		mutex_enter(&ipfb->ipfb_lock);
3485 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3486 			time_t frag_time = current_time - ipf->ipf_timestamp;
3487 			time_t frag_timeout;
3488 
3489 			if (frag_time < dead_interval) {
3490 				/*
3491 				 * There are some outstanding fragments
3492 				 * that will timeout later.  Make note of
3493 				 * the time so that we can reschedule the
3494 				 * next timeout appropriately.
3495 				 */
3496 				frag_timeout = dead_interval - frag_time;
3497 				if (next_timeout == 0 ||
3498 				    frag_timeout < next_timeout) {
3499 					next_timeout = frag_timeout;
3500 				}
3501 				break;
3502 			}
3503 			/* Time's up.  Get it out of here. */
3504 			hdr_length = ipf->ipf_nf_hdr_len;
3505 			ipfnext = ipf->ipf_hash_next;
3506 			if (ipfnext)
3507 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3508 			*ipf->ipf_ptphn = ipfnext;
3509 			mp = ipf->ipf_mp->b_cont;
3510 			for (; mp; mp = mp->b_cont) {
3511 				/* Extra points for neatness. */
3512 				IP_REASS_SET_START(mp, 0);
3513 				IP_REASS_SET_END(mp, 0);
3514 			}
3515 			mp = ipf->ipf_mp->b_cont;
3516 			ill->ill_frag_count -= ipf->ipf_count;
3517 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3518 			ipfb->ipfb_count -= ipf->ipf_count;
3519 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3520 			ipfb->ipfb_frag_pkts--;
3521 			/*
3522 			 * We do not send any icmp message from here because
3523 			 * we currently are holding the ipfb_lock for this
3524 			 * hash chain. If we try and send any icmp messages
3525 			 * from here we may end up via a put back into ip
3526 			 * trying to get the same lock, causing a recursive
3527 			 * mutex panic. Instead we build a list and send all
3528 			 * the icmp messages after we have dropped the lock.
3529 			 */
3530 			if (ill->ill_isv6) {
3531 				BUMP_MIB(ill->ill_ip6_mib, ipv6ReasmFails);
3532 				if (hdr_length != 0) {
3533 					mp->b_next = send_icmp_head_v6;
3534 					send_icmp_head_v6 = mp;
3535 				} else {
3536 					freemsg(mp);
3537 				}
3538 			} else {
3539 				BUMP_MIB(&ip_mib, ipReasmFails);
3540 				if (hdr_length != 0) {
3541 					mp->b_next = send_icmp_head;
3542 					send_icmp_head = mp;
3543 				} else {
3544 					freemsg(mp);
3545 				}
3546 			}
3547 			freeb(ipf->ipf_mp);
3548 		}
3549 		mutex_exit(&ipfb->ipfb_lock);
3550 		/*
3551 		 * Now need to send any icmp messages that we delayed from
3552 		 * above.
3553 		 */
3554 		while (send_icmp_head_v6 != NULL) {
3555 			mp = send_icmp_head_v6;
3556 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3557 			mp->b_next = NULL;
3558 			icmp_time_exceeded_v6(ill->ill_wq, mp,
3559 			    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE, B_FALSE);
3560 		}
3561 		while (send_icmp_head != NULL) {
3562 			mp = send_icmp_head;
3563 			send_icmp_head = send_icmp_head->b_next;
3564 			mp->b_next = NULL;
3565 			icmp_time_exceeded(ill->ill_wq, mp,
3566 			    ICMP_REASSEMBLY_TIME_EXCEEDED);
3567 		}
3568 	}
3569 	/*
3570 	 * A non-dying ILL will use the return value to decide whether to
3571 	 * restart the frag timer, and for how long.
3572 	 */
3573 	return (next_timeout);
3574 }
3575 
3576 /*
3577  * This routine is called when the approximate count of mblk memory used
3578  * for the specified ILL has exceeded max_count.
3579  */
3580 void
3581 ill_frag_prune(ill_t *ill, uint_t max_count)
3582 {
3583 	ipfb_t	*ipfb;
3584 	ipf_t	*ipf;
3585 	size_t	count;
3586 
3587 	/*
3588 	 * If we are here within ip_min_frag_prune_time msecs remove
3589 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3590 	 * ill_frag_free_num_pkts.
3591 	 */
3592 	mutex_enter(&ill->ill_lock);
3593 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3594 	    (ip_min_frag_prune_time != 0 ?
3595 	    ip_min_frag_prune_time : msec_per_tick)) {
3596 
3597 		ill->ill_frag_free_num_pkts++;
3598 
3599 	} else {
3600 		ill->ill_frag_free_num_pkts = 0;
3601 	}
3602 	ill->ill_last_frag_clean_time = lbolt;
3603 	mutex_exit(&ill->ill_lock);
3604 
3605 	/*
3606 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3607 	 */
3608 	if (ill->ill_frag_free_num_pkts != 0) {
3609 		int ix;
3610 
3611 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3612 			ipfb = &ill->ill_frag_hash_tbl[ix];
3613 			mutex_enter(&ipfb->ipfb_lock);
3614 			if (ipfb->ipfb_ipf != NULL) {
3615 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3616 				    ill->ill_frag_free_num_pkts);
3617 			}
3618 			mutex_exit(&ipfb->ipfb_lock);
3619 		}
3620 	}
3621 	/*
3622 	 * While the reassembly list for this ILL is too big, prune a fragment
3623 	 * queue by age, oldest first.  Note that the per ILL count is
3624 	 * approximate, while the per frag hash bucket counts are accurate.
3625 	 */
3626 	while (ill->ill_frag_count > max_count) {
3627 		int	ix;
3628 		ipfb_t	*oipfb = NULL;
3629 		uint_t	oldest = UINT_MAX;
3630 
3631 		count = 0;
3632 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3633 			ipfb = &ill->ill_frag_hash_tbl[ix];
3634 			mutex_enter(&ipfb->ipfb_lock);
3635 			ipf = ipfb->ipfb_ipf;
3636 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3637 				oldest = ipf->ipf_gen;
3638 				oipfb = ipfb;
3639 			}
3640 			count += ipfb->ipfb_count;
3641 			mutex_exit(&ipfb->ipfb_lock);
3642 		}
3643 		/* Refresh the per ILL count */
3644 		ill->ill_frag_count = count;
3645 		if (oipfb == NULL) {
3646 			ill->ill_frag_count = 0;
3647 			break;
3648 		}
3649 		if (count <= max_count)
3650 			return;	/* Somebody beat us to it, nothing to do */
3651 		mutex_enter(&oipfb->ipfb_lock);
3652 		ipf = oipfb->ipfb_ipf;
3653 		if (ipf != NULL) {
3654 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3655 		}
3656 		mutex_exit(&oipfb->ipfb_lock);
3657 	}
3658 }
3659 
3660 /*
3661  * free 'free_cnt' fragmented packets starting at ipf.
3662  */
3663 void
3664 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3665 {
3666 	size_t	count;
3667 	mblk_t	*mp;
3668 	mblk_t	*tmp;
3669 	ipf_t **ipfp = ipf->ipf_ptphn;
3670 
3671 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3672 	ASSERT(ipfp != NULL);
3673 	ASSERT(ipf != NULL);
3674 
3675 	while (ipf != NULL && free_cnt-- > 0) {
3676 		count = ipf->ipf_count;
3677 		mp = ipf->ipf_mp;
3678 		ipf = ipf->ipf_hash_next;
3679 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3680 			IP_REASS_SET_START(tmp, 0);
3681 			IP_REASS_SET_END(tmp, 0);
3682 		}
3683 		ill->ill_frag_count -= count;
3684 		ASSERT(ipfb->ipfb_count >= count);
3685 		ipfb->ipfb_count -= count;
3686 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3687 		ipfb->ipfb_frag_pkts--;
3688 		freemsg(mp);
3689 		BUMP_MIB(&ip_mib, ipReasmFails);
3690 	}
3691 
3692 	if (ipf)
3693 		ipf->ipf_ptphn = ipfp;
3694 	ipfp[0] = ipf;
3695 }
3696 
3697 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3698 	"obsolete and may be removed in a future release of Solaris.  Use " \
3699 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3700 
3701 /*
3702  * For obsolete per-interface forwarding configuration;
3703  * called in response to ND_GET.
3704  */
3705 /* ARGSUSED */
3706 static int
3707 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3708 {
3709 	ill_t *ill = (ill_t *)cp;
3710 
3711 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3712 
3713 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3714 	return (0);
3715 }
3716 
3717 /*
3718  * For obsolete per-interface forwarding configuration;
3719  * called in response to ND_SET.
3720  */
3721 /* ARGSUSED */
3722 static int
3723 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3724     cred_t *ioc_cr)
3725 {
3726 	long value;
3727 	int retval;
3728 
3729 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3730 
3731 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3732 	    value < 0 || value > 1) {
3733 		return (EINVAL);
3734 	}
3735 
3736 	rw_enter(&ill_g_lock, RW_READER);
3737 	retval = ill_forward_set(q, mp, (value != 0), cp);
3738 	rw_exit(&ill_g_lock);
3739 	return (retval);
3740 }
3741 
3742 /*
3743  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3744  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3745  * up RTS_IFINFO routing socket messages for each interface whose flags we
3746  * change.
3747  */
3748 /* ARGSUSED */
3749 int
3750 ill_forward_set(queue_t *q, mblk_t *mp, boolean_t enable, caddr_t cp)
3751 {
3752 	ill_t *ill = (ill_t *)cp;
3753 	ill_group_t *illgrp;
3754 
3755 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ill_g_lock));
3756 
3757 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3758 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)) ||
3759 	    (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK))
3760 		return (EINVAL);
3761 
3762 	/*
3763 	 * If the ill is in an IPMP group, set the forwarding policy on all
3764 	 * members of the group to the same value.
3765 	 */
3766 	illgrp = ill->ill_group;
3767 	if (illgrp != NULL) {
3768 		ill_t *tmp_ill;
3769 
3770 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3771 		    tmp_ill = tmp_ill->ill_group_next) {
3772 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3773 			    (enable ? "Enabling" : "Disabling"),
3774 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3775 			    tmp_ill->ill_name));
3776 			mutex_enter(&tmp_ill->ill_lock);
3777 			if (enable)
3778 				tmp_ill->ill_flags |= ILLF_ROUTER;
3779 			else
3780 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3781 			mutex_exit(&tmp_ill->ill_lock);
3782 			if (tmp_ill->ill_isv6)
3783 				ill_set_nce_router_flags(tmp_ill, enable);
3784 			/* Notify routing socket listeners of this change. */
3785 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3786 		}
3787 	} else {
3788 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3789 		    (enable ? "Enabling" : "Disabling"),
3790 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3791 		mutex_enter(&ill->ill_lock);
3792 		if (enable)
3793 			ill->ill_flags |= ILLF_ROUTER;
3794 		else
3795 			ill->ill_flags &= ~ILLF_ROUTER;
3796 		mutex_exit(&ill->ill_lock);
3797 		if (ill->ill_isv6)
3798 			ill_set_nce_router_flags(ill, enable);
3799 		/* Notify routing socket listeners of this change. */
3800 		ip_rts_ifmsg(ill->ill_ipif);
3801 	}
3802 
3803 	return (0);
3804 }
3805 
3806 /*
3807  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3808  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3809  * set or clear.
3810  */
3811 static void
3812 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3813 {
3814 	ipif_t *ipif;
3815 	nce_t *nce;
3816 
3817 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3818 		nce = ndp_lookup(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3819 		if (nce != NULL) {
3820 			mutex_enter(&nce->nce_lock);
3821 			if (enable)
3822 				nce->nce_flags |= NCE_F_ISROUTER;
3823 			else
3824 				nce->nce_flags &= ~NCE_F_ISROUTER;
3825 			mutex_exit(&nce->nce_lock);
3826 			NCE_REFRELE(nce);
3827 		}
3828 	}
3829 }
3830 
3831 /*
3832  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3833  * for this ill.  Make sure the v6/v4 question has been answered about this
3834  * ill.  The creation of this ndd variable is only for backwards compatibility.
3835  * The preferred way to control per-interface IP forwarding is through the
3836  * ILLF_ROUTER interface flag.
3837  */
3838 static int
3839 ill_set_ndd_name(ill_t *ill)
3840 {
3841 	char *suffix;
3842 
3843 	ASSERT(IAM_WRITER_ILL(ill));
3844 
3845 	if (ill->ill_isv6)
3846 		suffix = ipv6_forward_suffix;
3847 	else
3848 		suffix = ipv4_forward_suffix;
3849 
3850 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
3851 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
3852 	/*
3853 	 * Copies over the '\0'.
3854 	 * Note that strlen(suffix) is always bounded.
3855 	 */
3856 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
3857 	    strlen(suffix) + 1);
3858 
3859 	/*
3860 	 * Use of the nd table requires holding the reader lock.
3861 	 * Modifying the nd table thru nd_load/nd_unload requires
3862 	 * the writer lock.
3863 	 */
3864 	rw_enter(&ip_g_nd_lock, RW_WRITER);
3865 	if (!nd_load(&ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
3866 	    nd_ill_forward_set, (caddr_t)ill)) {
3867 		/*
3868 		 * If the nd_load failed, it only meant that it could not
3869 		 * allocate a new bunch of room for further NDD expansion.
3870 		 * Because of that, the ill_ndd_name will be set to 0, and
3871 		 * this interface is at the mercy of the global ip_forwarding
3872 		 * variable.
3873 		 */
3874 		rw_exit(&ip_g_nd_lock);
3875 		ill->ill_ndd_name = NULL;
3876 		return (ENOMEM);
3877 	}
3878 	rw_exit(&ip_g_nd_lock);
3879 	return (0);
3880 }
3881 
3882 /*
3883  * Intializes the context structure and returns the first ill in the list
3884  * cuurently start_list and end_list can have values:
3885  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
3886  * IP_V4_G_HEAD		Traverse IPV4 list only.
3887  * IP_V6_G_HEAD		Traverse IPV6 list only.
3888  */
3889 
3890 /*
3891  * We don't check for CONDEMNED ills here. Caller must do that if
3892  * necessary under the ill lock.
3893  */
3894 ill_t *
3895 ill_first(int start_list, int end_list, ill_walk_context_t *ctx)
3896 {
3897 	ill_if_t *ifp;
3898 	ill_t *ill;
3899 	avl_tree_t *avl_tree;
3900 
3901 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
3902 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
3903 
3904 	/*
3905 	 * setup the lists to search
3906 	 */
3907 	if (end_list != MAX_G_HEADS) {
3908 		ctx->ctx_current_list = start_list;
3909 		ctx->ctx_last_list = end_list;
3910 	} else {
3911 		ctx->ctx_last_list = MAX_G_HEADS - 1;
3912 		ctx->ctx_current_list = 0;
3913 	}
3914 
3915 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
3916 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list);
3917 		if (ifp != (ill_if_t *)
3918 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list)) {
3919 			avl_tree = &ifp->illif_avl_by_ppa;
3920 			ill = avl_first(avl_tree);
3921 			/*
3922 			 * ill is guaranteed to be non NULL or ifp should have
3923 			 * not existed.
3924 			 */
3925 			ASSERT(ill != NULL);
3926 			return (ill);
3927 		}
3928 		ctx->ctx_current_list++;
3929 	}
3930 
3931 	return (NULL);
3932 }
3933 
3934 /*
3935  * returns the next ill in the list. ill_first() must have been called
3936  * before calling ill_next() or bad things will happen.
3937  */
3938 
3939 /*
3940  * We don't check for CONDEMNED ills here. Caller must do that if
3941  * necessary under the ill lock.
3942  */
3943 ill_t *
3944 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
3945 {
3946 	ill_if_t *ifp;
3947 	ill_t *ill;
3948 
3949 
3950 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
3951 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
3952 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list));
3953 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
3954 	    AVL_AFTER)) != NULL) {
3955 		return (ill);
3956 	}
3957 
3958 	/* goto next ill_ifp in the list. */
3959 	ifp = lastill->ill_ifptr->illif_next;
3960 
3961 	/* make sure not at end of circular list */
3962 	while (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list)) {
3963 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
3964 			return (NULL);
3965 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list);
3966 	}
3967 
3968 	return (avl_first(&ifp->illif_avl_by_ppa));
3969 }
3970 
3971 /*
3972  * Check interface name for correct format which is name+ppa.
3973  * name can contain characters and digits, the right most digits
3974  * make up the ppa number. use of octal is not allowed, name must contain
3975  * a ppa, return pointer to the start of ppa.
3976  * In case of error return NULL.
3977  */
3978 static char *
3979 ill_get_ppa_ptr(char *name)
3980 {
3981 	int namelen = mi_strlen(name);
3982 
3983 	int len = namelen;
3984 
3985 	name += len;
3986 	while (len > 0) {
3987 		name--;
3988 		if (*name < '0' || *name > '9')
3989 			break;
3990 		len--;
3991 	}
3992 
3993 	/* empty string, all digits, or no trailing digits */
3994 	if (len == 0 || len == (int)namelen)
3995 		return (NULL);
3996 
3997 	name++;
3998 	/* check for attempted use of octal */
3999 	if (*name == '0' && len != (int)namelen - 1)
4000 		return (NULL);
4001 	return (name);
4002 }
4003 
4004 /*
4005  * use avl tree to locate the ill.
4006  */
4007 static ill_t *
4008 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4009     ipsq_func_t func, int *error)
4010 {
4011 	char *ppa_ptr = NULL;
4012 	int len;
4013 	uint_t ppa;
4014 	ill_t *ill = NULL;
4015 	ill_if_t *ifp;
4016 	int list;
4017 	ipsq_t *ipsq;
4018 
4019 	if (error != NULL)
4020 		*error = 0;
4021 
4022 	/*
4023 	 * get ppa ptr
4024 	 */
4025 	if (isv6)
4026 		list = IP_V6_G_HEAD;
4027 	else
4028 		list = IP_V4_G_HEAD;
4029 
4030 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4031 		if (error != NULL)
4032 			*error = ENXIO;
4033 		return (NULL);
4034 	}
4035 
4036 	len = ppa_ptr - name + 1;
4037 
4038 	ppa = stoi(&ppa_ptr);
4039 
4040 	ifp = IP_VX_ILL_G_LIST(list);
4041 
4042 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list)) {
4043 		/*
4044 		 * match is done on len - 1 as the name is not null
4045 		 * terminated it contains ppa in addition to the interface
4046 		 * name.
4047 		 */
4048 		if ((ifp->illif_name_len == len) &&
4049 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4050 			break;
4051 		} else {
4052 			ifp = ifp->illif_next;
4053 		}
4054 	}
4055 
4056 
4057 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list)) {
4058 		/*
4059 		 * Even the interface type does not exist.
4060 		 */
4061 		if (error != NULL)
4062 			*error = ENXIO;
4063 		return (NULL);
4064 	}
4065 
4066 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4067 	if (ill != NULL) {
4068 		/*
4069 		 * The block comment at the start of ipif_down
4070 		 * explains the use of the macros used below
4071 		 */
4072 		GRAB_CONN_LOCK(q);
4073 		mutex_enter(&ill->ill_lock);
4074 		if (ILL_CAN_LOOKUP(ill)) {
4075 			ill_refhold_locked(ill);
4076 			mutex_exit(&ill->ill_lock);
4077 			RELEASE_CONN_LOCK(q);
4078 			return (ill);
4079 		} else if (ILL_CAN_WAIT(ill, q)) {
4080 			ipsq = ill->ill_phyint->phyint_ipsq;
4081 			mutex_enter(&ipsq->ipsq_lock);
4082 			mutex_exit(&ill->ill_lock);
4083 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4084 			mutex_exit(&ipsq->ipsq_lock);
4085 			RELEASE_CONN_LOCK(q);
4086 			*error = EINPROGRESS;
4087 			return (NULL);
4088 		}
4089 		mutex_exit(&ill->ill_lock);
4090 		RELEASE_CONN_LOCK(q);
4091 	}
4092 	if (error != NULL)
4093 		*error = ENXIO;
4094 	return (NULL);
4095 }
4096 
4097 /*
4098  * comparison function for use with avl.
4099  */
4100 static int
4101 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4102 {
4103 	uint_t ppa;
4104 	uint_t ill_ppa;
4105 
4106 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4107 
4108 	ppa = *((uint_t *)ppa_ptr);
4109 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4110 	/*
4111 	 * We want the ill with the lowest ppa to be on the
4112 	 * top.
4113 	 */
4114 	if (ill_ppa < ppa)
4115 		return (1);
4116 	if (ill_ppa > ppa)
4117 		return (-1);
4118 	return (0);
4119 }
4120 
4121 /*
4122  * remove an interface type from the global list.
4123  */
4124 static void
4125 ill_delete_interface_type(ill_if_t *interface)
4126 {
4127 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
4128 
4129 	ASSERT(interface != NULL);
4130 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4131 
4132 	avl_destroy(&interface->illif_avl_by_ppa);
4133 	if (interface->illif_ppa_arena != NULL)
4134 		vmem_destroy(interface->illif_ppa_arena);
4135 
4136 	remque(interface);
4137 
4138 	mi_free(interface);
4139 }
4140 
4141 /*
4142  * remove ill from the global list.
4143  */
4144 static void
4145 ill_glist_delete(ill_t *ill)
4146 {
4147 	if (ill == NULL)
4148 		return;
4149 
4150 	rw_enter(&ill_g_lock, RW_WRITER);
4151 	/*
4152 	 * If the ill was never inserted into the AVL tree
4153 	 * we skip the if branch.
4154 	 */
4155 	if (ill->ill_ifptr != NULL) {
4156 		/*
4157 		 * remove from AVL tree and free ppa number
4158 		 */
4159 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4160 
4161 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4162 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4163 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4164 		}
4165 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4166 			ill_delete_interface_type(ill->ill_ifptr);
4167 		}
4168 
4169 		/*
4170 		 * Indicate ill is no longer in the list.
4171 		 */
4172 		ill->ill_ifptr = NULL;
4173 		ill->ill_name_length = 0;
4174 		ill->ill_name[0] = '\0';
4175 		ill->ill_ppa = UINT_MAX;
4176 	}
4177 	ill_phyint_free(ill);
4178 	rw_exit(&ill_g_lock);
4179 }
4180 
4181 /*
4182  * allocate a ppa, if the number of plumbed interfaces of this type are
4183  * less than ill_no_arena do a linear search to find a unused ppa.
4184  * When the number goes beyond ill_no_arena switch to using an arena.
4185  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4186  * is the return value for an error condition, so allocation starts at one
4187  * and is decremented by one.
4188  */
4189 static int
4190 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4191 {
4192 	ill_t *tmp_ill;
4193 	uint_t start, end;
4194 	int ppa;
4195 
4196 	if (ifp->illif_ppa_arena == NULL &&
4197 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4198 		/*
4199 		 * Create an arena.
4200 		 */
4201 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4202 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4203 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4204 			/* allocate what has already been assigned */
4205 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4206 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4207 		    tmp_ill, AVL_AFTER)) {
4208 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4209 			    1,		/* size */
4210 			    1,		/* align/quantum */
4211 			    0,		/* phase */
4212 			    0,		/* nocross */
4213 		(void *)((uintptr_t)tmp_ill->ill_ppa + 1), /* minaddr */
4214 		(void *)((uintptr_t)tmp_ill->ill_ppa + 2), /* maxaddr */
4215 			    VM_NOSLEEP|VM_FIRSTFIT);
4216 			if (ppa == 0) {
4217 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4218 				    " failed while switching"));
4219 				vmem_destroy(ifp->illif_ppa_arena);
4220 				ifp->illif_ppa_arena = NULL;
4221 				break;
4222 			}
4223 		}
4224 	}
4225 
4226 	if (ifp->illif_ppa_arena != NULL) {
4227 		if (ill->ill_ppa == UINT_MAX) {
4228 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4229 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4230 			if (ppa == 0)
4231 				return (EAGAIN);
4232 			ill->ill_ppa = --ppa;
4233 		} else {
4234 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4235 			    1, 		/* size */
4236 			    1, 		/* align/quantum */
4237 			    0, 		/* phase */
4238 			    0, 		/* nocross */
4239 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4240 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4241 			    VM_NOSLEEP|VM_FIRSTFIT);
4242 			/*
4243 			 * Most likely the allocation failed because
4244 			 * the requested ppa was in use.
4245 			 */
4246 			if (ppa == 0)
4247 				return (EEXIST);
4248 		}
4249 		return (0);
4250 	}
4251 
4252 	/*
4253 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4254 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4255 	 */
4256 	if (ill->ill_ppa == UINT_MAX) {
4257 		end = UINT_MAX - 1;
4258 		start = 0;
4259 	} else {
4260 		end = start = ill->ill_ppa;
4261 	}
4262 
4263 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4264 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4265 		if (start++ >= end) {
4266 			if (ill->ill_ppa == UINT_MAX)
4267 				return (EAGAIN);
4268 			else
4269 				return (EEXIST);
4270 		}
4271 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4272 	}
4273 	ill->ill_ppa = start;
4274 	return (0);
4275 }
4276 
4277 /*
4278  * Insert ill into the list of configured ill's. Once this function completes,
4279  * the ill is globally visible and is available through lookups. More precisely
4280  * this happens after the caller drops the ill_g_lock.
4281  */
4282 static int
4283 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4284 {
4285 	ill_if_t *ill_interface;
4286 	avl_index_t where = 0;
4287 	int error;
4288 	int name_length;
4289 	int index;
4290 	boolean_t check_length = B_FALSE;
4291 
4292 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
4293 
4294 	name_length = mi_strlen(name) + 1;
4295 
4296 	if (isv6)
4297 		index = IP_V6_G_HEAD;
4298 	else
4299 		index = IP_V4_G_HEAD;
4300 
4301 	ill_interface = IP_VX_ILL_G_LIST(index);
4302 	/*
4303 	 * Search for interface type based on name
4304 	 */
4305 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index)) {
4306 		if ((ill_interface->illif_name_len == name_length) &&
4307 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4308 			break;
4309 		}
4310 		ill_interface = ill_interface->illif_next;
4311 	}
4312 
4313 	/*
4314 	 * Interface type not found, create one.
4315 	 */
4316 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index)) {
4317 
4318 		ill_g_head_t ghead;
4319 
4320 		/*
4321 		 * allocate ill_if_t structure
4322 		 */
4323 
4324 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4325 		if (ill_interface == NULL) {
4326 			return (ENOMEM);
4327 		}
4328 
4329 
4330 
4331 		(void) strcpy(ill_interface->illif_name, name);
4332 		ill_interface->illif_name_len = name_length;
4333 
4334 		avl_create(&ill_interface->illif_avl_by_ppa,
4335 		    ill_compare_ppa, sizeof (ill_t),
4336 		    offsetof(struct ill_s, ill_avl_byppa));
4337 
4338 		/*
4339 		 * link the structure in the back to maintain order
4340 		 * of configuration for ifconfig output.
4341 		 */
4342 		ghead = ill_g_heads[index];
4343 		insque(ill_interface, ghead.ill_g_list_tail);
4344 
4345 	}
4346 
4347 	if (ill->ill_ppa == UINT_MAX)
4348 		check_length = B_TRUE;
4349 
4350 	error = ill_alloc_ppa(ill_interface, ill);
4351 	if (error != 0) {
4352 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4353 			ill_delete_interface_type(ill->ill_ifptr);
4354 		return (error);
4355 	}
4356 
4357 	/*
4358 	 * When the ppa is choosen by the system, check that there is
4359 	 * enough space to insert ppa. if a specific ppa was passed in this
4360 	 * check is not required as the interface name passed in will have
4361 	 * the right ppa in it.
4362 	 */
4363 	if (check_length) {
4364 		/*
4365 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4366 		 */
4367 		char buf[sizeof (uint_t) * 3];
4368 
4369 		/*
4370 		 * convert ppa to string to calculate the amount of space
4371 		 * required for it in the name.
4372 		 */
4373 		numtos(ill->ill_ppa, buf);
4374 
4375 		/* Do we have enough space to insert ppa ? */
4376 
4377 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4378 			/* Free ppa and interface type struct */
4379 			if (ill_interface->illif_ppa_arena != NULL) {
4380 				vmem_free(ill_interface->illif_ppa_arena,
4381 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4382 			}
4383 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4384 			    0) {
4385 				ill_delete_interface_type(ill->ill_ifptr);
4386 			}
4387 
4388 			return (EINVAL);
4389 		}
4390 	}
4391 
4392 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4393 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4394 
4395 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4396 	    &where);
4397 	ill->ill_ifptr = ill_interface;
4398 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4399 
4400 	ill_phyint_reinit(ill);
4401 	return (0);
4402 }
4403 
4404 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4405 static boolean_t
4406 ipsq_init(ill_t *ill)
4407 {
4408 	ipsq_t  *ipsq;
4409 
4410 	/* Init the ipsq and impicitly enter as writer */
4411 	ill->ill_phyint->phyint_ipsq =
4412 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4413 	if (ill->ill_phyint->phyint_ipsq == NULL)
4414 		return (B_FALSE);
4415 	ipsq = ill->ill_phyint->phyint_ipsq;
4416 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4417 	ill->ill_phyint->phyint_ipsq_next = NULL;
4418 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4419 	ipsq->ipsq_refs = 1;
4420 	ipsq->ipsq_writer = curthread;
4421 	ipsq->ipsq_reentry_cnt = 1;
4422 #ifdef ILL_DEBUG
4423 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack, IP_STACK_DEPTH);
4424 #endif
4425 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4426 	return (B_TRUE);
4427 }
4428 
4429 /*
4430  * ill_init is called by ip_open when a device control stream is opened.
4431  * It does a few initializations, and shoots a DL_INFO_REQ message down
4432  * to the driver.  The response is later picked up in ip_rput_dlpi and
4433  * used to set up default mechanisms for talking to the driver.  (Always
4434  * called as writer.)
4435  *
4436  * If this function returns error, ip_open will call ip_close which in
4437  * turn will call ill_delete to clean up any memory allocated here that
4438  * is not yet freed.
4439  */
4440 int
4441 ill_init(queue_t *q, ill_t *ill)
4442 {
4443 	int	count;
4444 	dl_info_req_t	*dlir;
4445 	mblk_t	*info_mp;
4446 	uchar_t *frag_ptr;
4447 
4448 	/*
4449 	 * The ill is initialized to zero by mi_alloc*(). In addition
4450 	 * some fields already contain valid values, initialized in
4451 	 * ip_open(), before we reach here.
4452 	 */
4453 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4454 
4455 	ill->ill_rq = q;
4456 	ill->ill_wq = WR(q);
4457 
4458 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4459 	    BPRI_HI);
4460 	if (info_mp == NULL)
4461 		return (ENOMEM);
4462 
4463 	/*
4464 	 * Allocate sufficient space to contain our fragment hash table and
4465 	 * the device name.
4466 	 */
4467 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4468 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4469 	if (frag_ptr == NULL) {
4470 		freemsg(info_mp);
4471 		return (ENOMEM);
4472 	}
4473 	ill->ill_frag_ptr = frag_ptr;
4474 	ill->ill_frag_free_num_pkts = 0;
4475 	ill->ill_last_frag_clean_time = 0;
4476 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4477 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4478 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4479 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4480 		    NULL, MUTEX_DEFAULT, NULL);
4481 	}
4482 
4483 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4484 	if (ill->ill_phyint == NULL) {
4485 		freemsg(info_mp);
4486 		mi_free(frag_ptr);
4487 		return (ENOMEM);
4488 	}
4489 
4490 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4491 	/*
4492 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4493 	 * at this point because of the following reason. If we can't
4494 	 * enter the ipsq at some point and cv_wait, the writer that
4495 	 * wakes us up tries to locate us using the list of all phyints
4496 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4497 	 * If we don't set it now, we risk a missed wakeup.
4498 	 */
4499 	ill->ill_phyint->phyint_illv4 = ill;
4500 	ill->ill_ppa = UINT_MAX;
4501 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4502 
4503 	if (!ipsq_init(ill)) {
4504 		freemsg(info_mp);
4505 		mi_free(frag_ptr);
4506 		mi_free(ill->ill_phyint);
4507 		return (ENOMEM);
4508 	}
4509 
4510 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4511 
4512 
4513 	/* Frag queue limit stuff */
4514 	ill->ill_frag_count = 0;
4515 	ill->ill_ipf_gen = 0;
4516 
4517 	ill->ill_global_timer = INFINITY;
4518 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
4519 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4520 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4521 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4522 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4523 
4524 	/*
4525 	 * Initialize IPv6 configuration variables.  The IP module is always
4526 	 * opened as an IPv4 module.  Instead tracking down the cases where
4527 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4528 	 * here for convenience, this has no effect until the ill is set to do
4529 	 * IPv6.
4530 	 */
4531 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4532 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4533 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4534 	ill->ill_max_buf = ND_MAX_Q;
4535 	ill->ill_refcnt = 0;
4536 
4537 	/* Send down the Info Request to the driver. */
4538 	info_mp->b_datap->db_type = M_PCPROTO;
4539 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4540 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4541 	dlir->dl_primitive = DL_INFO_REQ;
4542 
4543 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4544 
4545 	qprocson(q);
4546 	ill_dlpi_send(ill, info_mp);
4547 
4548 	return (0);
4549 }
4550 
4551 /*
4552  * ill_dls_info
4553  * creates datalink socket info from the device.
4554  */
4555 int
4556 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4557 {
4558 	size_t	length;
4559 	ill_t	*ill = ipif->ipif_ill;
4560 
4561 	sdl->sdl_family = AF_LINK;
4562 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4563 	sdl->sdl_type = ipif->ipif_type;
4564 	(void) ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4565 	length = mi_strlen(sdl->sdl_data);
4566 	ASSERT(length < 256);
4567 	sdl->sdl_nlen = (uchar_t)length;
4568 	sdl->sdl_alen = ill->ill_phys_addr_length;
4569 	mutex_enter(&ill->ill_lock);
4570 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL) {
4571 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[length],
4572 		    ill->ill_phys_addr_length);
4573 	}
4574 	mutex_exit(&ill->ill_lock);
4575 	sdl->sdl_slen = 0;
4576 	return (sizeof (struct sockaddr_dl));
4577 }
4578 
4579 /*
4580  * ill_xarp_info
4581  * creates xarp info from the device.
4582  */
4583 static int
4584 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4585 {
4586 	sdl->sdl_family = AF_LINK;
4587 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4588 	sdl->sdl_type = ill->ill_type;
4589 	(void) ipif_get_name(ill->ill_ipif, sdl->sdl_data,
4590 	    sizeof (sdl->sdl_data));
4591 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4592 	sdl->sdl_alen = ill->ill_phys_addr_length;
4593 	sdl->sdl_slen = 0;
4594 	return (sdl->sdl_nlen);
4595 }
4596 
4597 static int
4598 loopback_kstat_update(kstat_t *ksp, int rw)
4599 {
4600 	kstat_named_t *kn = KSTAT_NAMED_PTR(ksp);
4601 
4602 	if (rw == KSTAT_WRITE)
4603 		return (EACCES);
4604 	kn[0].value.ui32 = loopback_packets;
4605 	kn[1].value.ui32 = loopback_packets;
4606 	return (0);
4607 }
4608 
4609 
4610 /*
4611  * Has ifindex been plumbed already.
4612  */
4613 static boolean_t
4614 phyint_exists(uint_t index)
4615 {
4616 	phyint_t *phyi;
4617 
4618 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
4619 	/*
4620 	 * Indexes are stored in the phyint - a common structure
4621 	 * to both IPv4 and IPv6.
4622 	 */
4623 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
4624 	    (void *) &index, NULL);
4625 	return (phyi != NULL);
4626 }
4627 
4628 /*
4629  * Assign a unique interface index for the phyint.
4630  */
4631 static boolean_t
4632 phyint_assign_ifindex(phyint_t *phyi)
4633 {
4634 	uint_t starting_index;
4635 
4636 	ASSERT(phyi->phyint_ifindex == 0);
4637 	if (!ill_index_wrap) {
4638 		phyi->phyint_ifindex = ill_index++;
4639 		if (ill_index == 0) {
4640 			/* Reached the uint_t limit Next time wrap  */
4641 			ill_index_wrap = B_TRUE;
4642 		}
4643 		return (B_TRUE);
4644 	}
4645 
4646 	/*
4647 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4648 	 * at this point and don't want to call any function that attempts
4649 	 * to get the lock again.
4650 	 */
4651 	starting_index = ill_index++;
4652 	for (; ill_index != starting_index; ill_index++) {
4653 		if (ill_index != 0 && !phyint_exists(ill_index)) {
4654 			/* found unused index - use it */
4655 			phyi->phyint_ifindex = ill_index;
4656 			return (B_TRUE);
4657 		}
4658 	}
4659 
4660 	/*
4661 	 * all interface indicies are inuse.
4662 	 */
4663 	return (B_FALSE);
4664 }
4665 
4666 /*
4667  * Return a pointer to the ill which matches the supplied name.  Note that
4668  * the ill name length includes the null termination character.  (May be
4669  * called as writer.)
4670  * If do_alloc and the interface is "lo0" it will be automatically created.
4671  * Cannot bump up reference on condemned ills. So dup detect can't be done
4672  * using this func.
4673  */
4674 ill_t *
4675 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4676     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc)
4677 {
4678 	ill_t	*ill;
4679 	ipif_t	*ipif;
4680 	kstat_named_t	*kn;
4681 	boolean_t isloopback;
4682 	ipsq_t *old_ipsq;
4683 
4684 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4685 
4686 	rw_enter(&ill_g_lock, RW_READER);
4687 	ill = ill_find_by_name(name, isv6, q, mp, func, error);
4688 	rw_exit(&ill_g_lock);
4689 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4690 		return (ill);
4691 
4692 	/*
4693 	 * Couldn't find it.  Does this happen to be a lookup for the
4694 	 * loopback device and are we allowed to allocate it?
4695 	 */
4696 	if (!isloopback || !do_alloc)
4697 		return (NULL);
4698 
4699 	rw_enter(&ill_g_lock, RW_WRITER);
4700 
4701 	ill = ill_find_by_name(name, isv6, q, mp, func, error);
4702 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4703 		rw_exit(&ill_g_lock);
4704 		return (ill);
4705 	}
4706 
4707 	/* Create the loopback device on demand */
4708 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4709 	    sizeof (ipif_loopback_name), BPRI_MED));
4710 	if (ill == NULL)
4711 		goto done;
4712 
4713 	*ill = ill_null;
4714 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4715 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4716 	if (ill->ill_phyint == NULL)
4717 		goto done;
4718 
4719 	if (isv6)
4720 		ill->ill_phyint->phyint_illv6 = ill;
4721 	else
4722 		ill->ill_phyint->phyint_illv4 = ill;
4723 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4724 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4725 	/* Add room for tcp+ip headers */
4726 	if (isv6) {
4727 		ill->ill_isv6 = B_TRUE;
4728 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4729 		if (!ill_allocate_mibs(ill))
4730 			goto done;
4731 	} else {
4732 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4733 	}
4734 	ill->ill_max_mtu = ill->ill_max_frag;
4735 	/*
4736 	 * ipif_loopback_name can't be pointed at directly because its used
4737 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4738 	 * from the glist, ill_glist_delete() sets the first character of
4739 	 * ill_name to '\0'.
4740 	 */
4741 	ill->ill_name = (char *)ill + sizeof (*ill);
4742 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4743 	ill->ill_name_length = sizeof (ipif_loopback_name);
4744 	/* Set ill_name_set for ill_phyint_reinit to work properly */
4745 
4746 	ill->ill_global_timer = INFINITY;
4747 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
4748 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4749 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4750 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4751 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4752 
4753 	/* No resolver here. */
4754 	ill->ill_net_type = IRE_LOOPBACK;
4755 
4756 	/* Initialize the ipsq */
4757 	if (!ipsq_init(ill))
4758 		goto done;
4759 
4760 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
4761 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
4762 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
4763 #ifdef ILL_DEBUG
4764 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
4765 #endif
4766 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
4767 	if (ipif == NULL)
4768 		goto done;
4769 
4770 	ill->ill_flags = ILLF_MULTICAST;
4771 
4772 	/* Set up default loopback address and mask. */
4773 	if (!isv6) {
4774 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
4775 
4776 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
4777 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4778 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
4779 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4780 		    ipif->ipif_v6subnet);
4781 		ill->ill_flags |= ILLF_IPV4;
4782 	} else {
4783 		ipif->ipif_v6lcl_addr = ipv6_loopback;
4784 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4785 		ipif->ipif_v6net_mask = ipv6_all_ones;
4786 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4787 		    ipif->ipif_v6subnet);
4788 		ill->ill_flags |= ILLF_IPV6;
4789 	}
4790 
4791 	/*
4792 	 * Chain us in at the end of the ill list. hold the ill
4793 	 * before we make it globally visible. 1 for the lookup.
4794 	 */
4795 	ill->ill_refcnt = 0;
4796 	ill_refhold(ill);
4797 
4798 	ill->ill_frag_count = 0;
4799 	ill->ill_frag_free_num_pkts = 0;
4800 	ill->ill_last_frag_clean_time = 0;
4801 
4802 	old_ipsq = ill->ill_phyint->phyint_ipsq;
4803 
4804 	if (ill_glist_insert(ill, "lo", isv6) != 0)
4805 		cmn_err(CE_PANIC, "cannot insert loopback interface");
4806 
4807 	/* Let SCTP know so that it can add this to its list */
4808 	sctp_update_ill(ill, SCTP_ILL_INSERT);
4809 
4810 	/* Let SCTP know about this IPIF, so that it can add it to its list */
4811 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
4812 
4813 	/*
4814 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
4815 	 */
4816 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
4817 		/* Loopback ills aren't in any IPMP group */
4818 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
4819 		ipsq_delete(old_ipsq);
4820 	}
4821 
4822 	/*
4823 	 * Delay this till the ipif is allocated as ipif_allocate
4824 	 * de-references ill_phyint for getting the ifindex. We
4825 	 * can't do this before ipif_allocate because ill_phyint_reinit
4826 	 * -> phyint_assign_ifindex expects ipif to be present.
4827 	 */
4828 	mutex_enter(&ill->ill_phyint->phyint_lock);
4829 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
4830 	mutex_exit(&ill->ill_phyint->phyint_lock);
4831 
4832 	if (loopback_ksp == NULL) {
4833 		/* Export loopback interface statistics */
4834 		loopback_ksp = kstat_create("lo", 0, ipif_loopback_name, "net",
4835 		    KSTAT_TYPE_NAMED, 2, 0);
4836 		if (loopback_ksp != NULL) {
4837 			loopback_ksp->ks_update = loopback_kstat_update;
4838 			kn = KSTAT_NAMED_PTR(loopback_ksp);
4839 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
4840 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
4841 			kstat_install(loopback_ksp);
4842 		}
4843 	}
4844 
4845 	if (error != NULL)
4846 		*error = 0;
4847 	*did_alloc = B_TRUE;
4848 	rw_exit(&ill_g_lock);
4849 	return (ill);
4850 done:
4851 	if (ill != NULL) {
4852 		if (ill->ill_phyint != NULL) {
4853 			ipsq_t	*ipsq;
4854 
4855 			ipsq = ill->ill_phyint->phyint_ipsq;
4856 			if (ipsq != NULL)
4857 				kmem_free(ipsq, sizeof (ipsq_t));
4858 			mi_free(ill->ill_phyint);
4859 		}
4860 		ill_free_mib(ill);
4861 		mi_free(ill);
4862 	}
4863 	rw_exit(&ill_g_lock);
4864 	if (error != NULL)
4865 		*error = ENOMEM;
4866 	return (NULL);
4867 }
4868 
4869 /*
4870  * Return a pointer to the ill which matches the index and IP version type.
4871  */
4872 ill_t *
4873 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
4874     ipsq_func_t func, int *err)
4875 {
4876 	ill_t	*ill;
4877 	ipsq_t  *ipsq;
4878 	phyint_t *phyi;
4879 
4880 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
4881 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
4882 
4883 	if (err != NULL)
4884 		*err = 0;
4885 
4886 	/*
4887 	 * Indexes are stored in the phyint - a common structure
4888 	 * to both IPv4 and IPv6.
4889 	 */
4890 	rw_enter(&ill_g_lock, RW_READER);
4891 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
4892 	    (void *) &index, NULL);
4893 	if (phyi != NULL) {
4894 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
4895 		if (ill != NULL) {
4896 			/*
4897 			 * The block comment at the start of ipif_down
4898 			 * explains the use of the macros used below
4899 			 */
4900 			GRAB_CONN_LOCK(q);
4901 			mutex_enter(&ill->ill_lock);
4902 			if (ILL_CAN_LOOKUP(ill)) {
4903 				ill_refhold_locked(ill);
4904 				mutex_exit(&ill->ill_lock);
4905 				RELEASE_CONN_LOCK(q);
4906 				rw_exit(&ill_g_lock);
4907 				return (ill);
4908 			} else if (ILL_CAN_WAIT(ill, q)) {
4909 				ipsq = ill->ill_phyint->phyint_ipsq;
4910 				mutex_enter(&ipsq->ipsq_lock);
4911 				rw_exit(&ill_g_lock);
4912 				mutex_exit(&ill->ill_lock);
4913 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4914 				mutex_exit(&ipsq->ipsq_lock);
4915 				RELEASE_CONN_LOCK(q);
4916 				*err = EINPROGRESS;
4917 				return (NULL);
4918 			}
4919 			RELEASE_CONN_LOCK(q);
4920 			mutex_exit(&ill->ill_lock);
4921 		}
4922 	}
4923 	rw_exit(&ill_g_lock);
4924 	if (err != NULL)
4925 		*err = ENXIO;
4926 	return (NULL);
4927 }
4928 
4929 /*
4930  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
4931  * that gives a running thread a reference to the ill. This reference must be
4932  * released by the thread when it is done accessing the ill and related
4933  * objects. ill_refcnt can not be used to account for static references
4934  * such as other structures pointing to an ill. Callers must generally
4935  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
4936  * or be sure that the ill is not being deleted or changing state before
4937  * calling the refhold functions. A non-zero ill_refcnt ensures that the
4938  * ill won't change any of its critical state such as address, netmask etc.
4939  */
4940 void
4941 ill_refhold(ill_t *ill)
4942 {
4943 	mutex_enter(&ill->ill_lock);
4944 	ill->ill_refcnt++;
4945 	ILL_TRACE_REF(ill);
4946 	mutex_exit(&ill->ill_lock);
4947 }
4948 
4949 void
4950 ill_refhold_locked(ill_t *ill)
4951 {
4952 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4953 	ill->ill_refcnt++;
4954 	ILL_TRACE_REF(ill);
4955 }
4956 
4957 int
4958 ill_check_and_refhold(ill_t *ill)
4959 {
4960 	mutex_enter(&ill->ill_lock);
4961 	if (ILL_CAN_LOOKUP(ill)) {
4962 		ill_refhold_locked(ill);
4963 		mutex_exit(&ill->ill_lock);
4964 		return (0);
4965 	}
4966 	mutex_exit(&ill->ill_lock);
4967 	return (ILL_LOOKUP_FAILED);
4968 }
4969 
4970 /*
4971  * Must not be called while holding any locks. Otherwise if this is
4972  * the last reference to be released, there is a chance of recursive mutex
4973  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4974  * to restart an ioctl.
4975  */
4976 void
4977 ill_refrele(ill_t *ill)
4978 {
4979 	mutex_enter(&ill->ill_lock);
4980 	ASSERT(ill->ill_refcnt != 0);
4981 	ill->ill_refcnt--;
4982 	ILL_UNTRACE_REF(ill);
4983 	if (ill->ill_refcnt != 0) {
4984 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
4985 		mutex_exit(&ill->ill_lock);
4986 		return;
4987 	}
4988 
4989 	/* Drops the ill_lock */
4990 	ipif_ill_refrele_tail(ill);
4991 }
4992 
4993 /*
4994  * Obtain a weak reference count on the ill. This reference ensures the
4995  * ill won't be freed, but the ill may change any of its critical state
4996  * such as netmask, address etc. Returns an error if the ill has started
4997  * closing.
4998  */
4999 boolean_t
5000 ill_waiter_inc(ill_t *ill)
5001 {
5002 	mutex_enter(&ill->ill_lock);
5003 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5004 		mutex_exit(&ill->ill_lock);
5005 		return (B_FALSE);
5006 	}
5007 	ill->ill_waiters++;
5008 	mutex_exit(&ill->ill_lock);
5009 	return (B_TRUE);
5010 }
5011 
5012 void
5013 ill_waiter_dcr(ill_t *ill)
5014 {
5015 	mutex_enter(&ill->ill_lock);
5016 	ill->ill_waiters--;
5017 	if (ill->ill_waiters == 0)
5018 		cv_broadcast(&ill->ill_cv);
5019 	mutex_exit(&ill->ill_lock);
5020 }
5021 
5022 /*
5023  * Named Dispatch routine to produce a formatted report on all ILLs.
5024  * This report is accessed by using the ndd utility to "get" ND variable
5025  * "ip_ill_status".
5026  */
5027 /* ARGSUSED */
5028 int
5029 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5030 {
5031 	ill_t		*ill;
5032 	ill_walk_context_t ctx;
5033 
5034 	(void) mi_mpprintf(mp,
5035 	    "ILL      " MI_COL_HDRPAD_STR
5036 	/*   01234567[89ABCDEF] */
5037 	    "rq       " MI_COL_HDRPAD_STR
5038 	/*   01234567[89ABCDEF] */
5039 	    "wq       " MI_COL_HDRPAD_STR
5040 	/*   01234567[89ABCDEF] */
5041 	    "upcnt mxfrg err name");
5042 	/*   12345 12345 123 xxxxxxxx  */
5043 
5044 	rw_enter(&ill_g_lock, RW_READER);
5045 	ill = ILL_START_WALK_ALL(&ctx);
5046 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5047 		(void) mi_mpprintf(mp,
5048 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5049 		    "%05u %05u %03d %s",
5050 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5051 		    ill->ill_ipif_up_count,
5052 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5053 	}
5054 	rw_exit(&ill_g_lock);
5055 
5056 	return (0);
5057 }
5058 
5059 /*
5060  * Named Dispatch routine to produce a formatted report on all IPIFs.
5061  * This report is accessed by using the ndd utility to "get" ND variable
5062  * "ip_ipif_status".
5063  */
5064 /* ARGSUSED */
5065 int
5066 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5067 {
5068 	char	buf1[INET6_ADDRSTRLEN];
5069 	char	buf2[INET6_ADDRSTRLEN];
5070 	char	buf3[INET6_ADDRSTRLEN];
5071 	char	buf4[INET6_ADDRSTRLEN];
5072 	char	buf5[INET6_ADDRSTRLEN];
5073 	char	buf6[INET6_ADDRSTRLEN];
5074 	char	buf[LIFNAMSIZ];
5075 	ill_t	*ill;
5076 	ipif_t	*ipif;
5077 	nv_t	*nvp;
5078 	uint64_t flags;
5079 	zoneid_t zoneid;
5080 	ill_walk_context_t ctx;
5081 
5082 	(void) mi_mpprintf(mp,
5083 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5084 	    "\tlocal address\n"
5085 	    "\tsrc address\n"
5086 	    "\tsubnet\n"
5087 	    "\tmask\n"
5088 	    "\tbroadcast\n"
5089 	    "\tp-p-dst");
5090 
5091 	ASSERT(q->q_next == NULL);
5092 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5093 
5094 	rw_enter(&ill_g_lock, RW_READER);
5095 	ill = ILL_START_WALK_ALL(&ctx);
5096 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5097 		for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
5098 			if (zoneid != GLOBAL_ZONEID &&
5099 			    zoneid != ipif->ipif_zoneid &&
5100 			    ipif->ipif_zoneid != ALL_ZONES)
5101 				continue;
5102 			(void) mi_mpprintf(mp,
5103 			    MI_COL_PTRFMT_STR
5104 			    "%04u %05u %u/%u/%u %s %d",
5105 			    (void *)ipif,
5106 			    ipif->ipif_metric, ipif->ipif_mtu,
5107 			    ipif->ipif_ib_pkt_count,
5108 			    ipif->ipif_ob_pkt_count,
5109 			    ipif->ipif_fo_pkt_count,
5110 			    ipif_get_name(ipif, buf, sizeof (buf)),
5111 			    ipif->ipif_zoneid);
5112 
5113 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5114 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5115 
5116 		/* Tack on text strings for any flags. */
5117 		nvp = ipif_nv_tbl;
5118 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5119 			if (nvp->nv_value & flags)
5120 				(void) mi_mpprintf_nr(mp, " %s",
5121 				    nvp->nv_name);
5122 		}
5123 		(void) mi_mpprintf(mp,
5124 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5125 		    inet_ntop(AF_INET6,
5126 			&ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5127 		    inet_ntop(AF_INET6,
5128 			&ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5129 		    inet_ntop(AF_INET6,
5130 			&ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5131 		    inet_ntop(AF_INET6,
5132 			&ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5133 		    inet_ntop(AF_INET6,
5134 			&ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5135 		    inet_ntop(AF_INET6,
5136 			&ipif->ipif_v6pp_dst_addr,
5137 			buf6, sizeof (buf6)));
5138 		}
5139 	}
5140 	rw_exit(&ill_g_lock);
5141 	return (0);
5142 }
5143 
5144 /*
5145  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5146  * driver.  We construct best guess defaults for lower level information that
5147  * we need.  If an interface is brought up without injection of any overriding
5148  * information from outside, we have to be ready to go with these defaults.
5149  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5150  * we primarely want the dl_provider_style.
5151  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5152  * at which point we assume the other part of the information is valid.
5153  */
5154 void
5155 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5156 {
5157 	uchar_t		*brdcst_addr;
5158 	uint_t		brdcst_addr_length, phys_addr_length;
5159 	t_scalar_t	sap_length;
5160 	dl_info_ack_t	*dlia;
5161 	ip_m_t		*ipm;
5162 	dl_qos_cl_sel1_t *sel1;
5163 
5164 	ASSERT(IAM_WRITER_ILL(ill));
5165 
5166 	/*
5167 	 * Till the ill is fully up ILL_CHANGING will be set and
5168 	 * the ill is not globally visible. So no need for a lock.
5169 	 */
5170 	dlia = (dl_info_ack_t *)mp->b_rptr;
5171 	ill->ill_mactype = dlia->dl_mac_type;
5172 
5173 	ipm = ip_m_lookup(dlia->dl_mac_type);
5174 	if (ipm == NULL) {
5175 		ipm = ip_m_lookup(DL_OTHER);
5176 		ASSERT(ipm != NULL);
5177 	}
5178 	ill->ill_media = ipm;
5179 
5180 	/*
5181 	 * When the new DLPI stuff is ready we'll pull lengths
5182 	 * from dlia.
5183 	 */
5184 	if (dlia->dl_version == DL_VERSION_2) {
5185 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5186 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5187 		    brdcst_addr_length);
5188 		if (brdcst_addr == NULL) {
5189 			brdcst_addr_length = 0;
5190 		}
5191 		sap_length = dlia->dl_sap_length;
5192 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5193 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5194 		    brdcst_addr_length, sap_length, phys_addr_length));
5195 	} else {
5196 		brdcst_addr_length = 6;
5197 		brdcst_addr = ip_six_byte_all_ones;
5198 		sap_length = -2;
5199 		phys_addr_length = brdcst_addr_length;
5200 	}
5201 
5202 	ill->ill_bcast_addr_length = brdcst_addr_length;
5203 	ill->ill_phys_addr_length = phys_addr_length;
5204 	ill->ill_sap_length = sap_length;
5205 	ill->ill_max_frag = dlia->dl_max_sdu;
5206 	ill->ill_max_mtu = ill->ill_max_frag;
5207 
5208 	ill->ill_type = ipm->ip_m_type;
5209 
5210 	if (!ill->ill_dlpi_style_set) {
5211 		if (dlia->dl_provider_style == DL_STYLE2)
5212 			ill->ill_needs_attach = 1;
5213 
5214 		/*
5215 		 * Allocate the first ipif on this ill. We don't delay it
5216 		 * further as ioctl handling assumes atleast one ipif to
5217 		 * be present.
5218 		 *
5219 		 * At this point we don't know whether the ill is v4 or v6.
5220 		 * We will know this whan the SIOCSLIFNAME happens and
5221 		 * the correct value for ill_isv6 will be assigned in
5222 		 * ipif_set_values(). We need to hold the ill lock and
5223 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5224 		 * the wakeup.
5225 		 */
5226 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5227 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5228 		mutex_enter(&ill->ill_lock);
5229 		ASSERT(ill->ill_dlpi_style_set == 0);
5230 		ill->ill_dlpi_style_set = 1;
5231 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5232 		cv_broadcast(&ill->ill_cv);
5233 		mutex_exit(&ill->ill_lock);
5234 		freemsg(mp);
5235 		return;
5236 	}
5237 	ASSERT(ill->ill_ipif != NULL);
5238 	/*
5239 	 * We know whether it is IPv4 or IPv6 now, as this is the
5240 	 * second DL_INFO_ACK we are recieving in response to the
5241 	 * DL_INFO_REQ sent in ipif_set_values.
5242 	 */
5243 	if (ill->ill_isv6)
5244 		ill->ill_sap = IP6_DL_SAP;
5245 	else
5246 		ill->ill_sap = IP_DL_SAP;
5247 	/*
5248 	 * Set ipif_mtu which is used to set the IRE's
5249 	 * ire_max_frag value. The driver could have sent
5250 	 * a different mtu from what it sent last time. No
5251 	 * need to call ipif_mtu_change because IREs have
5252 	 * not yet been created.
5253 	 */
5254 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5255 	/*
5256 	 * Clear all the flags that were set based on ill_bcast_addr_length
5257 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5258 	 * changed now and we need to re-evaluate.
5259 	 */
5260 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5261 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5262 
5263 	/*
5264 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5265 	 * changed now.
5266 	 */
5267 	if (ill->ill_bcast_addr_length == 0) {
5268 		if (ill->ill_resolver_mp != NULL)
5269 			freemsg(ill->ill_resolver_mp);
5270 		if (ill->ill_bcast_mp != NULL)
5271 			freemsg(ill->ill_bcast_mp);
5272 		if (ill->ill_flags & ILLF_XRESOLV)
5273 			ill->ill_net_type = IRE_IF_RESOLVER;
5274 		else
5275 			ill->ill_net_type = IRE_IF_NORESOLVER;
5276 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5277 		    ill->ill_phys_addr_length,
5278 		    ill->ill_sap,
5279 		    ill->ill_sap_length);
5280 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5281 
5282 		if (ill->ill_isv6)
5283 			/*
5284 			 * Note: xresolv interfaces will eventually need NOARP
5285 			 * set here as well, but that will require those
5286 			 * external resolvers to have some knowledge of
5287 			 * that flag and act appropriately. Not to be changed
5288 			 * at present.
5289 			 */
5290 			ill->ill_flags |= ILLF_NONUD;
5291 		else
5292 			ill->ill_flags |= ILLF_NOARP;
5293 
5294 		if (ill->ill_phys_addr_length == 0) {
5295 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5296 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5297 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5298 			} else {
5299 				/* pt-pt supports multicast. */
5300 				ill->ill_flags |= ILLF_MULTICAST;
5301 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5302 			}
5303 		}
5304 	} else {
5305 		ill->ill_net_type = IRE_IF_RESOLVER;
5306 		if (ill->ill_bcast_mp != NULL)
5307 			freemsg(ill->ill_bcast_mp);
5308 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5309 		    ill->ill_bcast_addr_length, ill->ill_sap,
5310 		    ill->ill_sap_length);
5311 		/*
5312 		 * Later detect lack of DLPI driver multicast
5313 		 * capability by catching DL_ENABMULTI errors in
5314 		 * ip_rput_dlpi.
5315 		 */
5316 		ill->ill_flags |= ILLF_MULTICAST;
5317 		if (!ill->ill_isv6)
5318 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5319 	}
5320 	/* By default an interface does not support any CoS marking */
5321 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5322 
5323 	/*
5324 	 * If we get QoS information in DL_INFO_ACK, the device supports
5325 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5326 	 */
5327 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5328 	    dlia->dl_qos_length);
5329 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5330 		ill->ill_flags |= ILLF_COS_ENABLED;
5331 	}
5332 
5333 	/* Clear any previous error indication. */
5334 	ill->ill_error = 0;
5335 	freemsg(mp);
5336 }
5337 
5338 /*
5339  * Perform various checks to verify that an address would make sense as a
5340  * local, remote, or subnet interface address.
5341  */
5342 static boolean_t
5343 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5344 {
5345 	ipaddr_t	net_mask;
5346 
5347 	/*
5348 	 * Don't allow all zeroes, all ones or experimental address, but allow
5349 	 * all ones netmask.
5350 	 */
5351 	if ((net_mask = ip_net_mask(addr)) == 0)
5352 		return (B_FALSE);
5353 	/* A given netmask overrides the "guess" netmask */
5354 	if (subnet_mask != 0)
5355 		net_mask = subnet_mask;
5356 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5357 	    (addr == (addr | ~net_mask)))) {
5358 		return (B_FALSE);
5359 	}
5360 	if (CLASSD(addr))
5361 		return (B_FALSE);
5362 
5363 	return (B_TRUE);
5364 }
5365 
5366 /*
5367  * ipif_lookup_group
5368  * Returns held ipif
5369  */
5370 ipif_t *
5371 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid)
5372 {
5373 	ire_t	*ire;
5374 	ipif_t	*ipif;
5375 
5376 	ire = ire_lookup_multi(group, zoneid);
5377 	if (ire == NULL)
5378 		return (NULL);
5379 	ipif = ire->ire_ipif;
5380 	ipif_refhold(ipif);
5381 	ire_refrele(ire);
5382 	return (ipif);
5383 }
5384 
5385 /*
5386  * Look for an ipif with the specified interface address and destination.
5387  * The destination address is used only for matching point-to-point interfaces.
5388  */
5389 ipif_t *
5390 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5391     ipsq_func_t func, int *error)
5392 {
5393 	ipif_t	*ipif;
5394 	ill_t	*ill;
5395 	ill_walk_context_t ctx;
5396 	ipsq_t	*ipsq;
5397 
5398 	if (error != NULL)
5399 		*error = 0;
5400 
5401 	/*
5402 	 * First match all the point-to-point interfaces
5403 	 * before looking at non-point-to-point interfaces.
5404 	 * This is done to avoid returning non-point-to-point
5405 	 * ipif instead of unnumbered point-to-point ipif.
5406 	 */
5407 	rw_enter(&ill_g_lock, RW_READER);
5408 	ill = ILL_START_WALK_V4(&ctx);
5409 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5410 		GRAB_CONN_LOCK(q);
5411 		mutex_enter(&ill->ill_lock);
5412 		for (ipif = ill->ill_ipif; ipif != NULL;
5413 		    ipif = ipif->ipif_next) {
5414 			/* Allow the ipif to be down */
5415 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5416 			    (ipif->ipif_lcl_addr == if_addr) &&
5417 			    (ipif->ipif_pp_dst_addr == dst)) {
5418 				/*
5419 				 * The block comment at the start of ipif_down
5420 				 * explains the use of the macros used below
5421 				 */
5422 				if (IPIF_CAN_LOOKUP(ipif)) {
5423 					ipif_refhold_locked(ipif);
5424 					mutex_exit(&ill->ill_lock);
5425 					RELEASE_CONN_LOCK(q);
5426 					rw_exit(&ill_g_lock);
5427 					return (ipif);
5428 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5429 					ipsq = ill->ill_phyint->phyint_ipsq;
5430 					mutex_enter(&ipsq->ipsq_lock);
5431 					mutex_exit(&ill->ill_lock);
5432 					rw_exit(&ill_g_lock);
5433 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5434 						ill);
5435 					mutex_exit(&ipsq->ipsq_lock);
5436 					RELEASE_CONN_LOCK(q);
5437 					*error = EINPROGRESS;
5438 					return (NULL);
5439 				}
5440 			}
5441 		}
5442 		mutex_exit(&ill->ill_lock);
5443 		RELEASE_CONN_LOCK(q);
5444 	}
5445 	rw_exit(&ill_g_lock);
5446 
5447 	/* lookup the ipif based on interface address */
5448 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error);
5449 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5450 	return (ipif);
5451 }
5452 
5453 /*
5454  * Look for an ipif with the specified address. For point-point links
5455  * we look for matches on either the destination address and the local
5456  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5457  * is set.
5458  * Matches on a specific ill if match_ill is set.
5459  */
5460 ipif_t *
5461 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5462     mblk_t *mp, ipsq_func_t func, int *error)
5463 {
5464 	ipif_t  *ipif;
5465 	ill_t   *ill;
5466 	boolean_t ptp = B_FALSE;
5467 	ipsq_t	*ipsq;
5468 	ill_walk_context_t	ctx;
5469 
5470 	if (error != NULL)
5471 		*error = 0;
5472 
5473 	rw_enter(&ill_g_lock, RW_READER);
5474 	/*
5475 	 * Repeat twice, first based on local addresses and
5476 	 * next time for pointopoint.
5477 	 */
5478 repeat:
5479 	ill = ILL_START_WALK_V4(&ctx);
5480 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5481 		if (match_ill != NULL && ill != match_ill) {
5482 			continue;
5483 		}
5484 		GRAB_CONN_LOCK(q);
5485 		mutex_enter(&ill->ill_lock);
5486 		for (ipif = ill->ill_ipif; ipif != NULL;
5487 		    ipif = ipif->ipif_next) {
5488 			if (zoneid != ALL_ZONES &&
5489 			    zoneid != ipif->ipif_zoneid &&
5490 			    ipif->ipif_zoneid != ALL_ZONES)
5491 				continue;
5492 			/* Allow the ipif to be down */
5493 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5494 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5495 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5496 			    (ipif->ipif_pp_dst_addr == addr))) {
5497 				/*
5498 				 * The block comment at the start of ipif_down
5499 				 * explains the use of the macros used below
5500 				 */
5501 				if (IPIF_CAN_LOOKUP(ipif)) {
5502 					ipif_refhold_locked(ipif);
5503 					mutex_exit(&ill->ill_lock);
5504 					RELEASE_CONN_LOCK(q);
5505 					rw_exit(&ill_g_lock);
5506 					return (ipif);
5507 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5508 					ipsq = ill->ill_phyint->phyint_ipsq;
5509 					mutex_enter(&ipsq->ipsq_lock);
5510 					mutex_exit(&ill->ill_lock);
5511 					rw_exit(&ill_g_lock);
5512 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5513 						ill);
5514 					mutex_exit(&ipsq->ipsq_lock);
5515 					RELEASE_CONN_LOCK(q);
5516 					*error = EINPROGRESS;
5517 					return (NULL);
5518 				}
5519 			}
5520 		}
5521 		mutex_exit(&ill->ill_lock);
5522 		RELEASE_CONN_LOCK(q);
5523 	}
5524 
5525 	/* Now try the ptp case */
5526 	if (ptp) {
5527 		rw_exit(&ill_g_lock);
5528 		if (error != NULL)
5529 			*error = ENXIO;
5530 		return (NULL);
5531 	}
5532 	ptp = B_TRUE;
5533 	goto repeat;
5534 }
5535 
5536 /*
5537  * Look for an ipif that matches the specified remote address i.e. the
5538  * ipif that would receive the specified packet.
5539  * First look for directly connected interfaces and then do a recursive
5540  * IRE lookup and pick the first ipif corresponding to the source address in the
5541  * ire.
5542  * Returns: held ipif
5543  */
5544 ipif_t *
5545 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
5546 {
5547 	ipif_t	*ipif;
5548 	ire_t	*ire;
5549 
5550 	ASSERT(!ill->ill_isv6);
5551 
5552 	/*
5553 	 * Someone could be changing this ipif currently or change it
5554 	 * after we return this. Thus  a few packets could use the old
5555 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
5556 	 * will atomically be updated or cleaned up with the new value
5557 	 * Thus we don't need a lock to check the flags or other attrs below.
5558 	 */
5559 	mutex_enter(&ill->ill_lock);
5560 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5561 		if (!IPIF_CAN_LOOKUP(ipif))
5562 			continue;
5563 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
5564 		    ipif->ipif_zoneid != ALL_ZONES)
5565 			continue;
5566 		/* Allow the ipif to be down */
5567 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
5568 			if ((ipif->ipif_pp_dst_addr == addr) ||
5569 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
5570 			    ipif->ipif_lcl_addr == addr)) {
5571 				ipif_refhold_locked(ipif);
5572 				mutex_exit(&ill->ill_lock);
5573 				return (ipif);
5574 			}
5575 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
5576 			ipif_refhold_locked(ipif);
5577 			mutex_exit(&ill->ill_lock);
5578 			return (ipif);
5579 		}
5580 	}
5581 	mutex_exit(&ill->ill_lock);
5582 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
5583 	    NULL, MATCH_IRE_RECURSIVE);
5584 	if (ire != NULL) {
5585 		/*
5586 		 * The callers of this function wants to know the
5587 		 * interface on which they have to send the replies
5588 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
5589 		 * derived from different ills, we really don't care
5590 		 * what we return here.
5591 		 */
5592 		ipif = ire->ire_ipif;
5593 		if (ipif != NULL) {
5594 			ipif_refhold(ipif);
5595 			ire_refrele(ire);
5596 			return (ipif);
5597 		}
5598 		ire_refrele(ire);
5599 	}
5600 	/* Pick the first interface */
5601 	ipif = ipif_get_next_ipif(NULL, ill);
5602 	return (ipif);
5603 }
5604 
5605 /*
5606  * This func does not prevent refcnt from increasing. But if
5607  * the caller has taken steps to that effect, then this func
5608  * can be used to determine whether the ill has become quiescent
5609  */
5610 boolean_t
5611 ill_is_quiescent(ill_t *ill)
5612 {
5613 	ipif_t	*ipif;
5614 
5615 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5616 
5617 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5618 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0)
5619 			return (B_FALSE);
5620 	}
5621 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
5622 	    ill->ill_nce_cnt != 0 || ill->ill_srcif_refcnt != 0 ||
5623 	    ill->ill_mrtun_refcnt != 0)
5624 		return (B_FALSE);
5625 	return (B_TRUE);
5626 }
5627 
5628 /*
5629  * This func does not prevent refcnt from increasing. But if
5630  * the caller has taken steps to that effect, then this func
5631  * can be used to determine whether the ipif has become quiescent
5632  */
5633 static boolean_t
5634 ipif_is_quiescent(ipif_t *ipif)
5635 {
5636 	ill_t *ill;
5637 
5638 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5639 
5640 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0)
5641 		return (B_FALSE);
5642 
5643 	ill = ipif->ipif_ill;
5644 	if (ill->ill_ipif_up_count != 0 || ill->ill_logical_down)
5645 		return (B_TRUE);
5646 
5647 	/* This is the last ipif going down or being deleted on this ill */
5648 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0)
5649 		return (B_FALSE);
5650 
5651 	return (B_TRUE);
5652 }
5653 
5654 /*
5655  * This func does not prevent refcnt from increasing. But if
5656  * the caller has taken steps to that effect, then this func
5657  * can be used to determine whether the ipifs marked with IPIF_MOVING
5658  * have become quiescent and can be moved in a failover/failback.
5659  */
5660 static ipif_t *
5661 ill_quiescent_to_move(ill_t *ill)
5662 {
5663 	ipif_t  *ipif;
5664 
5665 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5666 
5667 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5668 		if (ipif->ipif_state_flags & IPIF_MOVING) {
5669 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
5670 				return (ipif);
5671 			}
5672 		}
5673 	}
5674 	return (NULL);
5675 }
5676 
5677 /*
5678  * The ipif/ill/ire has been refreled. Do the tail processing.
5679  * Determine if the ipif or ill in question has become quiescent and if so
5680  * wakeup close and/or restart any queued pending ioctl that is waiting
5681  * for the ipif_down (or ill_down)
5682  */
5683 void
5684 ipif_ill_refrele_tail(ill_t *ill)
5685 {
5686 	mblk_t	*mp;
5687 	conn_t	*connp;
5688 	ipsq_t	*ipsq;
5689 	ipif_t	*ipif;
5690 
5691 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5692 
5693 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
5694 	    ill_is_quiescent(ill)) {
5695 		/* ill_close may be waiting */
5696 		cv_broadcast(&ill->ill_cv);
5697 	}
5698 
5699 	/* ipsq can't change because ill_lock  is held */
5700 	ipsq = ill->ill_phyint->phyint_ipsq;
5701 	if (ipsq->ipsq_waitfor == 0) {
5702 		/* Not waiting for anything, just return. */
5703 		mutex_exit(&ill->ill_lock);
5704 		return;
5705 	}
5706 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
5707 		ipsq->ipsq_pending_ipif != NULL);
5708 	/*
5709 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
5710 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
5711 	 * be zero for restarting an ioctl that ends up downing the ill.
5712 	 */
5713 	ipif = ipsq->ipsq_pending_ipif;
5714 	if (ipif->ipif_ill != ill) {
5715 		/* The ioctl is pending on some other ill. */
5716 		mutex_exit(&ill->ill_lock);
5717 		return;
5718 	}
5719 
5720 	switch (ipsq->ipsq_waitfor) {
5721 	case IPIF_DOWN:
5722 	case IPIF_FREE:
5723 		if (!ipif_is_quiescent(ipif)) {
5724 			mutex_exit(&ill->ill_lock);
5725 			return;
5726 		}
5727 		break;
5728 
5729 	case ILL_DOWN:
5730 	case ILL_FREE:
5731 		/*
5732 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
5733 		 * waits synchronously in ip_close, and no message is queued in
5734 		 * ipsq_pending_mp at all in this case
5735 		 */
5736 		if (!ill_is_quiescent(ill)) {
5737 			mutex_exit(&ill->ill_lock);
5738 			return;
5739 		}
5740 
5741 		break;
5742 
5743 	case ILL_MOVE_OK:
5744 		if (ill_quiescent_to_move(ill) != NULL) {
5745 			mutex_exit(&ill->ill_lock);
5746 			return;
5747 		}
5748 
5749 		break;
5750 	default:
5751 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
5752 		    (void *)ipsq, ipsq->ipsq_waitfor);
5753 	}
5754 
5755 	/*
5756 	 * Incr refcnt for the qwriter_ip call below which
5757 	 * does a refrele
5758 	 */
5759 	ill_refhold_locked(ill);
5760 	mutex_exit(&ill->ill_lock);
5761 
5762 	mp = ipsq_pending_mp_get(ipsq, &connp);
5763 	ASSERT(mp != NULL);
5764 
5765 	switch (mp->b_datap->db_type) {
5766 	case M_ERROR:
5767 	case M_HANGUP:
5768 		(void) qwriter_ip(NULL, ill, ill->ill_rq, mp,
5769 		    ipif_all_down_tail, CUR_OP, B_TRUE);
5770 		return;
5771 
5772 	case M_IOCTL:
5773 	case M_IOCDATA:
5774 		(void) qwriter_ip(NULL, ill,
5775 		    (connp != NULL ? CONNP_TO_WQ(connp) : ill->ill_wq), mp,
5776 		    ip_reprocess_ioctl, CUR_OP, B_TRUE);
5777 		return;
5778 
5779 	default:
5780 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
5781 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
5782 	}
5783 }
5784 
5785 #ifdef ILL_DEBUG
5786 /* Reuse trace buffer from beginning (if reached the end) and record trace */
5787 void
5788 th_trace_rrecord(th_trace_t *th_trace)
5789 {
5790 	tr_buf_t *tr_buf;
5791 	uint_t lastref;
5792 
5793 	lastref = th_trace->th_trace_lastref;
5794 	lastref++;
5795 	if (lastref == TR_BUF_MAX)
5796 		lastref = 0;
5797 	th_trace->th_trace_lastref = lastref;
5798 	tr_buf = &th_trace->th_trbuf[lastref];
5799 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, IP_STACK_DEPTH);
5800 }
5801 
5802 th_trace_t *
5803 th_trace_ipif_lookup(ipif_t *ipif)
5804 {
5805 	int bucket_id;
5806 	th_trace_t *th_trace;
5807 
5808 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5809 
5810 	bucket_id = IP_TR_HASH(curthread);
5811 	ASSERT(bucket_id < IP_TR_HASH_MAX);
5812 
5813 	for (th_trace = ipif->ipif_trace[bucket_id]; th_trace != NULL;
5814 	    th_trace = th_trace->th_next) {
5815 		if (th_trace->th_id == curthread)
5816 			return (th_trace);
5817 	}
5818 	return (NULL);
5819 }
5820 
5821 void
5822 ipif_trace_ref(ipif_t *ipif)
5823 {
5824 	int bucket_id;
5825 	th_trace_t *th_trace;
5826 
5827 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5828 
5829 	if (ipif->ipif_trace_disable)
5830 		return;
5831 
5832 	/*
5833 	 * Attempt to locate the trace buffer for the curthread.
5834 	 * If it does not exist, then allocate a new trace buffer
5835 	 * and link it in list of trace bufs for this ipif, at the head
5836 	 */
5837 	th_trace = th_trace_ipif_lookup(ipif);
5838 	if (th_trace == NULL) {
5839 		bucket_id = IP_TR_HASH(curthread);
5840 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
5841 		    KM_NOSLEEP);
5842 		if (th_trace == NULL) {
5843 			ipif->ipif_trace_disable = B_TRUE;
5844 			ipif_trace_cleanup(ipif);
5845 			return;
5846 		}
5847 		th_trace->th_id = curthread;
5848 		th_trace->th_next = ipif->ipif_trace[bucket_id];
5849 		th_trace->th_prev = &ipif->ipif_trace[bucket_id];
5850 		if (th_trace->th_next != NULL)
5851 			th_trace->th_next->th_prev = &th_trace->th_next;
5852 		ipif->ipif_trace[bucket_id] = th_trace;
5853 	}
5854 	ASSERT(th_trace->th_refcnt >= 0 &&
5855 		th_trace->th_refcnt < TR_BUF_MAX -1);
5856 	th_trace->th_refcnt++;
5857 	th_trace_rrecord(th_trace);
5858 }
5859 
5860 void
5861 ipif_untrace_ref(ipif_t *ipif)
5862 {
5863 	th_trace_t *th_trace;
5864 
5865 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5866 
5867 	if (ipif->ipif_trace_disable)
5868 		return;
5869 	th_trace = th_trace_ipif_lookup(ipif);
5870 	ASSERT(th_trace != NULL);
5871 	ASSERT(th_trace->th_refcnt > 0);
5872 
5873 	th_trace->th_refcnt--;
5874 	th_trace_rrecord(th_trace);
5875 }
5876 
5877 th_trace_t *
5878 th_trace_ill_lookup(ill_t *ill)
5879 {
5880 	th_trace_t *th_trace;
5881 	int bucket_id;
5882 
5883 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5884 
5885 	bucket_id = IP_TR_HASH(curthread);
5886 	ASSERT(bucket_id < IP_TR_HASH_MAX);
5887 
5888 	for (th_trace = ill->ill_trace[bucket_id]; th_trace != NULL;
5889 	    th_trace = th_trace->th_next) {
5890 		if (th_trace->th_id == curthread)
5891 			return (th_trace);
5892 	}
5893 	return (NULL);
5894 }
5895 
5896 void
5897 ill_trace_ref(ill_t *ill)
5898 {
5899 	int bucket_id;
5900 	th_trace_t *th_trace;
5901 
5902 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5903 	if (ill->ill_trace_disable)
5904 		return;
5905 	/*
5906 	 * Attempt to locate the trace buffer for the curthread.
5907 	 * If it does not exist, then allocate a new trace buffer
5908 	 * and link it in list of trace bufs for this ill, at the head
5909 	 */
5910 	th_trace = th_trace_ill_lookup(ill);
5911 	if (th_trace == NULL) {
5912 		bucket_id = IP_TR_HASH(curthread);
5913 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
5914 		    KM_NOSLEEP);
5915 		if (th_trace == NULL) {
5916 			ill->ill_trace_disable = B_TRUE;
5917 			ill_trace_cleanup(ill);
5918 			return;
5919 		}
5920 		th_trace->th_id = curthread;
5921 		th_trace->th_next = ill->ill_trace[bucket_id];
5922 		th_trace->th_prev = &ill->ill_trace[bucket_id];
5923 		if (th_trace->th_next != NULL)
5924 			th_trace->th_next->th_prev = &th_trace->th_next;
5925 		ill->ill_trace[bucket_id] = th_trace;
5926 	}
5927 	ASSERT(th_trace->th_refcnt >= 0 &&
5928 		th_trace->th_refcnt < TR_BUF_MAX - 1);
5929 
5930 	th_trace->th_refcnt++;
5931 	th_trace_rrecord(th_trace);
5932 }
5933 
5934 void
5935 ill_untrace_ref(ill_t *ill)
5936 {
5937 	th_trace_t *th_trace;
5938 
5939 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5940 
5941 	if (ill->ill_trace_disable)
5942 		return;
5943 	th_trace = th_trace_ill_lookup(ill);
5944 	ASSERT(th_trace != NULL);
5945 	ASSERT(th_trace->th_refcnt > 0);
5946 
5947 	th_trace->th_refcnt--;
5948 	th_trace_rrecord(th_trace);
5949 }
5950 
5951 /*
5952  * Verify that this thread has no refs to the ipif and free
5953  * the trace buffers
5954  */
5955 /* ARGSUSED */
5956 void
5957 ipif_thread_exit(ipif_t *ipif, void *dummy)
5958 {
5959 	th_trace_t *th_trace;
5960 
5961 	mutex_enter(&ipif->ipif_ill->ill_lock);
5962 
5963 	th_trace = th_trace_ipif_lookup(ipif);
5964 	if (th_trace == NULL) {
5965 		mutex_exit(&ipif->ipif_ill->ill_lock);
5966 		return;
5967 	}
5968 	ASSERT(th_trace->th_refcnt == 0);
5969 	/* unlink th_trace and free it */
5970 	*th_trace->th_prev = th_trace->th_next;
5971 	if (th_trace->th_next != NULL)
5972 		th_trace->th_next->th_prev = th_trace->th_prev;
5973 	th_trace->th_next = NULL;
5974 	th_trace->th_prev = NULL;
5975 	kmem_free(th_trace, sizeof (th_trace_t));
5976 
5977 	mutex_exit(&ipif->ipif_ill->ill_lock);
5978 }
5979 
5980 /*
5981  * Verify that this thread has no refs to the ill and free
5982  * the trace buffers
5983  */
5984 /* ARGSUSED */
5985 void
5986 ill_thread_exit(ill_t *ill, void *dummy)
5987 {
5988 	th_trace_t *th_trace;
5989 
5990 	mutex_enter(&ill->ill_lock);
5991 
5992 	th_trace = th_trace_ill_lookup(ill);
5993 	if (th_trace == NULL) {
5994 		mutex_exit(&ill->ill_lock);
5995 		return;
5996 	}
5997 	ASSERT(th_trace->th_refcnt == 0);
5998 	/* unlink th_trace and free it */
5999 	*th_trace->th_prev = th_trace->th_next;
6000 	if (th_trace->th_next != NULL)
6001 		th_trace->th_next->th_prev = th_trace->th_prev;
6002 	th_trace->th_next = NULL;
6003 	th_trace->th_prev = NULL;
6004 	kmem_free(th_trace, sizeof (th_trace_t));
6005 
6006 	mutex_exit(&ill->ill_lock);
6007 }
6008 #endif
6009 
6010 #ifdef ILL_DEBUG
6011 void
6012 ip_thread_exit(void)
6013 {
6014 	ill_t	*ill;
6015 	ipif_t	*ipif;
6016 	ill_walk_context_t	ctx;
6017 
6018 	rw_enter(&ill_g_lock, RW_READER);
6019 	ill = ILL_START_WALK_ALL(&ctx);
6020 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6021 		for (ipif = ill->ill_ipif; ipif != NULL;
6022 		    ipif = ipif->ipif_next) {
6023 			ipif_thread_exit(ipif, NULL);
6024 		}
6025 		ill_thread_exit(ill, NULL);
6026 	}
6027 	rw_exit(&ill_g_lock);
6028 
6029 	ire_walk(ire_thread_exit, NULL);
6030 	ndp_walk_impl(NULL, nce_thread_exit, NULL, B_FALSE);
6031 }
6032 
6033 /*
6034  * Called when ipif is unplumbed or when memory alloc fails
6035  */
6036 void
6037 ipif_trace_cleanup(ipif_t *ipif)
6038 {
6039 	int	i;
6040 	th_trace_t	*th_trace;
6041 	th_trace_t	*th_trace_next;
6042 
6043 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6044 		for (th_trace = ipif->ipif_trace[i]; th_trace != NULL;
6045 		    th_trace = th_trace_next) {
6046 			th_trace_next = th_trace->th_next;
6047 			kmem_free(th_trace, sizeof (th_trace_t));
6048 		}
6049 		ipif->ipif_trace[i] = NULL;
6050 	}
6051 }
6052 
6053 /*
6054  * Called when ill is unplumbed or when memory alloc fails
6055  */
6056 void
6057 ill_trace_cleanup(ill_t *ill)
6058 {
6059 	int	i;
6060 	th_trace_t	*th_trace;
6061 	th_trace_t	*th_trace_next;
6062 
6063 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6064 		for (th_trace = ill->ill_trace[i]; th_trace != NULL;
6065 		    th_trace = th_trace_next) {
6066 			th_trace_next = th_trace->th_next;
6067 			kmem_free(th_trace, sizeof (th_trace_t));
6068 		}
6069 		ill->ill_trace[i] = NULL;
6070 	}
6071 }
6072 
6073 #else
6074 void ip_thread_exit(void) {}
6075 #endif
6076 
6077 void
6078 ipif_refhold_locked(ipif_t *ipif)
6079 {
6080 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6081 	ipif->ipif_refcnt++;
6082 	IPIF_TRACE_REF(ipif);
6083 }
6084 
6085 void
6086 ipif_refhold(ipif_t *ipif)
6087 {
6088 	ill_t	*ill;
6089 
6090 	ill = ipif->ipif_ill;
6091 	mutex_enter(&ill->ill_lock);
6092 	ipif->ipif_refcnt++;
6093 	IPIF_TRACE_REF(ipif);
6094 	mutex_exit(&ill->ill_lock);
6095 }
6096 
6097 /*
6098  * Must not be called while holding any locks. Otherwise if this is
6099  * the last reference to be released there is a chance of recursive mutex
6100  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6101  * to restart an ioctl.
6102  */
6103 void
6104 ipif_refrele(ipif_t *ipif)
6105 {
6106 	ill_t	*ill;
6107 
6108 	ill = ipif->ipif_ill;
6109 
6110 	mutex_enter(&ill->ill_lock);
6111 	ASSERT(ipif->ipif_refcnt != 0);
6112 	ipif->ipif_refcnt--;
6113 	IPIF_UNTRACE_REF(ipif);
6114 	if (ipif->ipif_refcnt != 0) {
6115 		mutex_exit(&ill->ill_lock);
6116 		return;
6117 	}
6118 
6119 	/* Drops the ill_lock */
6120 	ipif_ill_refrele_tail(ill);
6121 }
6122 
6123 ipif_t *
6124 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6125 {
6126 	ipif_t	*ipif;
6127 
6128 	mutex_enter(&ill->ill_lock);
6129 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6130 	    ipif != NULL; ipif = ipif->ipif_next) {
6131 		if (!IPIF_CAN_LOOKUP(ipif))
6132 			continue;
6133 		ipif_refhold_locked(ipif);
6134 		mutex_exit(&ill->ill_lock);
6135 		return (ipif);
6136 	}
6137 	mutex_exit(&ill->ill_lock);
6138 	return (NULL);
6139 }
6140 
6141 /*
6142  * TODO: make this table extendible at run time
6143  * Return a pointer to the mac type info for 'mac_type'
6144  */
6145 static ip_m_t *
6146 ip_m_lookup(t_uscalar_t mac_type)
6147 {
6148 	ip_m_t	*ipm;
6149 
6150 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6151 		if (ipm->ip_m_mac_type == mac_type)
6152 			return (ipm);
6153 	return (NULL);
6154 }
6155 
6156 /*
6157  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6158  * ipif_arg is passed in to associate it with the correct interface.
6159  * We may need to restart this operation if the ipif cannot be looked up
6160  * due to an exclusive operation that is currently in progress. The restart
6161  * entry point is specified by 'func'
6162  */
6163 int
6164 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6165     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6166     ire_t **ire_arg, boolean_t ioctl_msg, queue_t *q, mblk_t *mp,
6167     ipsq_func_t func, struct rtsa_s *sp)
6168 {
6169 	ire_t	*ire;
6170 	ire_t	*gw_ire = NULL;
6171 	ipif_t	*ipif = NULL;
6172 	boolean_t ipif_refheld = B_FALSE;
6173 	uint_t	type;
6174 	int	match_flags = MATCH_IRE_TYPE;
6175 	int	error;
6176 	tsol_gc_t *gc = NULL;
6177 	tsol_gcgrp_t *gcgrp = NULL;
6178 	boolean_t gcgrp_xtraref = B_FALSE;
6179 
6180 	ip1dbg(("ip_rt_add:"));
6181 
6182 	if (ire_arg != NULL)
6183 		*ire_arg = NULL;
6184 
6185 	/*
6186 	 * If this is the case of RTF_HOST being set, then we set the netmask
6187 	 * to all ones (regardless if one was supplied).
6188 	 */
6189 	if (flags & RTF_HOST)
6190 		mask = IP_HOST_MASK;
6191 
6192 	/*
6193 	 * Prevent routes with a zero gateway from being created (since
6194 	 * interfaces can currently be plumbed and brought up no assigned
6195 	 * address).
6196 	 * For routes with RTA_SRCIFP, the gateway address can be 0.0.0.0.
6197 	 */
6198 	if (gw_addr == 0 && src_ipif == NULL)
6199 		return (ENETUNREACH);
6200 	/*
6201 	 * Get the ipif, if any, corresponding to the gw_addr
6202 	 */
6203 	if (gw_addr != 0) {
6204 		ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func,
6205 		    &error);
6206 		if (ipif != NULL) {
6207 			if (IS_VNI(ipif->ipif_ill)) {
6208 				ipif_refrele(ipif);
6209 				return (EINVAL);
6210 			}
6211 			ipif_refheld = B_TRUE;
6212 		} else if (error == EINPROGRESS) {
6213 			ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6214 			return (EINPROGRESS);
6215 		} else {
6216 			error = 0;
6217 		}
6218 	}
6219 
6220 	if (ipif != NULL) {
6221 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6222 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6223 	} else {
6224 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6225 	}
6226 
6227 	/*
6228 	 * GateD will attempt to create routes with a loopback interface
6229 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6230 	 * these routes to be added, but create them as interface routes
6231 	 * since the gateway is an interface address.
6232 	 */
6233 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6234 		flags &= ~RTF_GATEWAY;
6235 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6236 		    mask == IP_HOST_MASK) {
6237 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6238 			    ALL_ZONES, NULL, match_flags);
6239 			if (ire != NULL) {
6240 				ire_refrele(ire);
6241 				if (ipif_refheld)
6242 					ipif_refrele(ipif);
6243 				return (EEXIST);
6244 			}
6245 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6246 			    "for 0x%x\n", (void *)ipif,
6247 			    ipif->ipif_ire_type,
6248 			    ntohl(ipif->ipif_lcl_addr)));
6249 			ire = ire_create(
6250 			    (uchar_t *)&dst_addr,	/* dest address */
6251 			    (uchar_t *)&mask,		/* mask */
6252 			    (uchar_t *)&ipif->ipif_src_addr,
6253 			    NULL,			/* no gateway */
6254 			    NULL,
6255 			    &ipif->ipif_mtu,
6256 			    NULL,
6257 			    ipif->ipif_rq,		/* recv-from queue */
6258 			    NULL,			/* no send-to queue */
6259 			    ipif->ipif_ire_type,	/* LOOPBACK */
6260 			    NULL,
6261 			    ipif,
6262 			    NULL,
6263 			    0,
6264 			    0,
6265 			    0,
6266 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6267 			    RTF_PRIVATE : 0,
6268 			    &ire_uinfo_null,
6269 			    NULL,
6270 			    NULL);
6271 
6272 			if (ire == NULL) {
6273 				if (ipif_refheld)
6274 					ipif_refrele(ipif);
6275 				return (ENOMEM);
6276 			}
6277 			error = ire_add(&ire, q, mp, func);
6278 			if (error == 0)
6279 				goto save_ire;
6280 			if (ipif_refheld)
6281 				ipif_refrele(ipif);
6282 			return (error);
6283 
6284 		}
6285 	}
6286 
6287 	/*
6288 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6289 	 * and the gateway address provided is one of the system's interface
6290 	 * addresses.  By using the routing socket interface and supplying an
6291 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6292 	 * specifying an interface route to be created is available which uses
6293 	 * the interface index that specifies the outgoing interface rather than
6294 	 * the address of an outgoing interface (which may not be able to
6295 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6296 	 * flag, routes can be specified which not only specify the next-hop to
6297 	 * be used when routing to a certain prefix, but also which outgoing
6298 	 * interface should be used.
6299 	 *
6300 	 * Previously, interfaces would have unique addresses assigned to them
6301 	 * and so the address assigned to a particular interface could be used
6302 	 * to identify a particular interface.  One exception to this was the
6303 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6304 	 *
6305 	 * With the advent of IPv6 and its link-local addresses, this
6306 	 * restriction was relaxed and interfaces could share addresses between
6307 	 * themselves.  In fact, typically all of the link-local interfaces on
6308 	 * an IPv6 node or router will have the same link-local address.  In
6309 	 * order to differentiate between these interfaces, the use of an
6310 	 * interface index is necessary and this index can be carried inside a
6311 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6312 	 * of using the interface index, however, is that all of the ipif's that
6313 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6314 	 * cannot be used to differentiate between ipif's (or logical
6315 	 * interfaces) that belong to the same ill (physical interface).
6316 	 *
6317 	 * For example, in the following case involving IPv4 interfaces and
6318 	 * logical interfaces
6319 	 *
6320 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6321 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6322 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6323 	 *
6324 	 * the ipif's corresponding to each of these interface routes can be
6325 	 * uniquely identified by the "gateway" (actually interface address).
6326 	 *
6327 	 * In this case involving multiple IPv6 default routes to a particular
6328 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6329 	 * default route is of interest:
6330 	 *
6331 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6332 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6333 	 */
6334 
6335 	/* RTF_GATEWAY not set */
6336 	if (!(flags & RTF_GATEWAY)) {
6337 		queue_t	*stq;
6338 		queue_t	*rfq = NULL;
6339 		ill_t	*in_ill = NULL;
6340 
6341 		if (sp != NULL) {
6342 			ip2dbg(("ip_rt_add: gateway security attributes "
6343 			    "cannot be set with interface route\n"));
6344 			if (ipif_refheld)
6345 				ipif_refrele(ipif);
6346 			return (EINVAL);
6347 		}
6348 
6349 		/*
6350 		 * As the interface index specified with the RTA_IFP sockaddr is
6351 		 * the same for all ipif's off of an ill, the matching logic
6352 		 * below uses MATCH_IRE_ILL if such an index was specified.
6353 		 * This means that routes sharing the same prefix when added
6354 		 * using a RTA_IFP sockaddr must have distinct interface
6355 		 * indices (namely, they must be on distinct ill's).
6356 		 *
6357 		 * On the other hand, since the gateway address will usually be
6358 		 * different for each ipif on the system, the matching logic
6359 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6360 		 * route.  This means that interface routes for the same prefix
6361 		 * can be created if they belong to distinct ipif's and if a
6362 		 * RTA_IFP sockaddr is not present.
6363 		 */
6364 		if (ipif_arg != NULL) {
6365 			if (ipif_refheld)  {
6366 				ipif_refrele(ipif);
6367 				ipif_refheld = B_FALSE;
6368 			}
6369 			ipif = ipif_arg;
6370 			match_flags |= MATCH_IRE_ILL;
6371 		} else {
6372 			/*
6373 			 * Check the ipif corresponding to the gw_addr
6374 			 */
6375 			if (ipif == NULL)
6376 				return (ENETUNREACH);
6377 			match_flags |= MATCH_IRE_IPIF;
6378 		}
6379 		ASSERT(ipif != NULL);
6380 		/*
6381 		 * If src_ipif is not NULL, we have to create
6382 		 * an ire with non-null ire_in_ill value
6383 		 */
6384 		if (src_ipif != NULL) {
6385 			in_ill = src_ipif->ipif_ill;
6386 		}
6387 
6388 		/*
6389 		 * We check for an existing entry at this point.
6390 		 *
6391 		 * Since a netmask isn't passed in via the ioctl interface
6392 		 * (SIOCADDRT), we don't check for a matching netmask in that
6393 		 * case.
6394 		 */
6395 		if (!ioctl_msg)
6396 			match_flags |= MATCH_IRE_MASK;
6397 		if (src_ipif != NULL) {
6398 			/* Look up in the special table */
6399 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
6400 			    ipif, src_ipif->ipif_ill, match_flags);
6401 		} else {
6402 			ire = ire_ftable_lookup(dst_addr, mask, 0,
6403 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
6404 			    NULL, match_flags);
6405 		}
6406 		if (ire != NULL) {
6407 			ire_refrele(ire);
6408 			if (ipif_refheld)
6409 				ipif_refrele(ipif);
6410 			return (EEXIST);
6411 		}
6412 
6413 		if (src_ipif != NULL) {
6414 			/*
6415 			 * Create the special ire for the IRE table
6416 			 * which hangs out of ire_in_ill. This ire
6417 			 * is in-between IRE_CACHE and IRE_INTERFACE.
6418 			 * Thus rfq is non-NULL.
6419 			 */
6420 			rfq = ipif->ipif_rq;
6421 		}
6422 		/* Create the usual interface ires */
6423 
6424 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6425 		    ? ipif->ipif_rq : ipif->ipif_wq;
6426 
6427 		/*
6428 		 * Create a copy of the IRE_LOOPBACK,
6429 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6430 		 * the modified address and netmask.
6431 		 */
6432 		ire = ire_create(
6433 		    (uchar_t *)&dst_addr,
6434 		    (uint8_t *)&mask,
6435 		    (uint8_t *)&ipif->ipif_src_addr,
6436 		    NULL,
6437 		    NULL,
6438 		    &ipif->ipif_mtu,
6439 		    NULL,
6440 		    rfq,
6441 		    stq,
6442 		    ipif->ipif_net_type,
6443 		    ipif->ipif_resolver_mp,
6444 		    ipif,
6445 		    in_ill,
6446 		    0,
6447 		    0,
6448 		    0,
6449 		    flags,
6450 		    &ire_uinfo_null,
6451 		    NULL,
6452 		    NULL);
6453 		if (ire == NULL) {
6454 			if (ipif_refheld)
6455 				ipif_refrele(ipif);
6456 			return (ENOMEM);
6457 		}
6458 
6459 		/*
6460 		 * Some software (for example, GateD and Sun Cluster) attempts
6461 		 * to create (what amount to) IRE_PREFIX routes with the
6462 		 * loopback address as the gateway.  This is primarily done to
6463 		 * set up prefixes with the RTF_REJECT flag set (for example,
6464 		 * when generating aggregate routes.)
6465 		 *
6466 		 * If the IRE type (as defined by ipif->ipif_net_type) is
6467 		 * IRE_LOOPBACK, then we map the request into a
6468 		 * IRE_IF_NORESOLVER.
6469 		 *
6470 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
6471 		 * routine, but rather using ire_create() directly.
6472 		 */
6473 		if (ipif->ipif_net_type == IRE_LOOPBACK)
6474 			ire->ire_type = IRE_IF_NORESOLVER;
6475 		error = ire_add(&ire, q, mp, func);
6476 		if (error == 0)
6477 			goto save_ire;
6478 
6479 		/*
6480 		 * In the result of failure, ire_add() will have already
6481 		 * deleted the ire in question, so there is no need to
6482 		 * do that here.
6483 		 */
6484 		if (ipif_refheld)
6485 			ipif_refrele(ipif);
6486 		return (error);
6487 	}
6488 	if (ipif_refheld) {
6489 		ipif_refrele(ipif);
6490 		ipif_refheld = B_FALSE;
6491 	}
6492 
6493 	if (src_ipif != NULL) {
6494 		/* RTA_SRCIFP is not supported on RTF_GATEWAY */
6495 		ip2dbg(("ip_rt_add: SRCIF cannot be set with gateway route\n"));
6496 		return (EINVAL);
6497 	}
6498 	/*
6499 	 * Get an interface IRE for the specified gateway.
6500 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
6501 	 * gateway, it is currently unreachable and we fail the request
6502 	 * accordingly.
6503 	 */
6504 	ipif = ipif_arg;
6505 	if (ipif_arg != NULL)
6506 		match_flags |= MATCH_IRE_ILL;
6507 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
6508 	    ALL_ZONES, 0, NULL, match_flags);
6509 	if (gw_ire == NULL)
6510 		return (ENETUNREACH);
6511 
6512 	/*
6513 	 * We create one of three types of IREs as a result of this request
6514 	 * based on the netmask.  A netmask of all ones (which is automatically
6515 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
6516 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
6517 	 * created.  Otherwise, an IRE_PREFIX route is created for the
6518 	 * destination prefix.
6519 	 */
6520 	if (mask == IP_HOST_MASK)
6521 		type = IRE_HOST;
6522 	else if (mask == 0)
6523 		type = IRE_DEFAULT;
6524 	else
6525 		type = IRE_PREFIX;
6526 
6527 	/* check for a duplicate entry */
6528 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
6529 	    NULL, ALL_ZONES, 0, NULL,
6530 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW);
6531 	if (ire != NULL) {
6532 		ire_refrele(gw_ire);
6533 		ire_refrele(ire);
6534 		return (EEXIST);
6535 	}
6536 
6537 	/* Security attribute exists */
6538 	if (sp != NULL) {
6539 		tsol_gcgrp_addr_t ga;
6540 
6541 		/* find or create the gateway credentials group */
6542 		ga.ga_af = AF_INET;
6543 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
6544 
6545 		/* we hold reference to it upon success */
6546 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
6547 		if (gcgrp == NULL) {
6548 			ire_refrele(gw_ire);
6549 			return (ENOMEM);
6550 		}
6551 
6552 		/*
6553 		 * Create and add the security attribute to the group; a
6554 		 * reference to the group is made upon allocating a new
6555 		 * entry successfully.  If it finds an already-existing
6556 		 * entry for the security attribute in the group, it simply
6557 		 * returns it and no new reference is made to the group.
6558 		 */
6559 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
6560 		if (gc == NULL) {
6561 			/* release reference held by gcgrp_lookup */
6562 			GCGRP_REFRELE(gcgrp);
6563 			ire_refrele(gw_ire);
6564 			return (ENOMEM);
6565 		}
6566 	}
6567 
6568 	/* Create the IRE. */
6569 	ire = ire_create(
6570 	    (uchar_t *)&dst_addr,		/* dest address */
6571 	    (uchar_t *)&mask,			/* mask */
6572 	    /* src address assigned by the caller? */
6573 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
6574 		(flags & RTF_SETSRC)) ?  &src_addr : NULL),
6575 	    (uchar_t *)&gw_addr,		/* gateway address */
6576 	    NULL,				/* no in-srcaddress */
6577 	    &gw_ire->ire_max_frag,
6578 	    NULL,				/* no Fast Path header */
6579 	    NULL,				/* no recv-from queue */
6580 	    NULL,				/* no send-to queue */
6581 	    (ushort_t)type,			/* IRE type */
6582 	    NULL,
6583 	    ipif_arg,
6584 	    NULL,
6585 	    0,
6586 	    0,
6587 	    0,
6588 	    flags,
6589 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
6590 	    gc,					/* security attribute */
6591 	    NULL);
6592 	/*
6593 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
6594 	 * reference to the 'gcgrp'. We can now release the extra reference
6595 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
6596 	 */
6597 	if (gcgrp_xtraref)
6598 		GCGRP_REFRELE(gcgrp);
6599 	if (ire == NULL) {
6600 		if (gc != NULL)
6601 			GC_REFRELE(gc);
6602 		ire_refrele(gw_ire);
6603 		return (ENOMEM);
6604 	}
6605 
6606 	/*
6607 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
6608 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
6609 	 */
6610 
6611 	/* Add the new IRE. */
6612 	error = ire_add(&ire, q, mp, func);
6613 	if (error != 0) {
6614 		/*
6615 		 * In the result of failure, ire_add() will have already
6616 		 * deleted the ire in question, so there is no need to
6617 		 * do that here.
6618 		 */
6619 		ire_refrele(gw_ire);
6620 		return (error);
6621 	}
6622 
6623 	if (flags & RTF_MULTIRT) {
6624 		/*
6625 		 * Invoke the CGTP (multirouting) filtering module
6626 		 * to add the dst address in the filtering database.
6627 		 * Replicated inbound packets coming from that address
6628 		 * will be filtered to discard the duplicates.
6629 		 * It is not necessary to call the CGTP filter hook
6630 		 * when the dst address is a broadcast or multicast,
6631 		 * because an IP source address cannot be a broadcast
6632 		 * or a multicast.
6633 		 */
6634 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
6635 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
6636 		if (ire_dst != NULL) {
6637 			ip_cgtp_bcast_add(ire, ire_dst);
6638 			ire_refrele(ire_dst);
6639 			goto save_ire;
6640 		}
6641 		if ((ip_cgtp_filter_ops != NULL) && !CLASSD(ire->ire_addr)) {
6642 			int res = ip_cgtp_filter_ops->cfo_add_dest_v4(
6643 			    ire->ire_addr,
6644 			    ire->ire_gateway_addr,
6645 			    ire->ire_src_addr,
6646 			    gw_ire->ire_src_addr);
6647 			if (res != 0) {
6648 				ire_refrele(gw_ire);
6649 				ire_delete(ire);
6650 				return (res);
6651 			}
6652 		}
6653 	}
6654 
6655 	/*
6656 	 * Now that the prefix IRE entry has been created, delete any
6657 	 * existing gateway IRE cache entries as well as any IRE caches
6658 	 * using the gateway, and force them to be created through
6659 	 * ip_newroute.
6660 	 */
6661 	if (gc != NULL) {
6662 		ASSERT(gcgrp != NULL);
6663 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES);
6664 	}
6665 
6666 save_ire:
6667 	if (gw_ire != NULL) {
6668 		ire_refrele(gw_ire);
6669 	}
6670 	/*
6671 	 * We do not do save_ire for the routes added with RTA_SRCIFP
6672 	 * flag. This route is only added and deleted by mipagent.
6673 	 * So, for simplicity of design, we refrain from saving
6674 	 * ires that are created with srcif value. This may change
6675 	 * in future if we find more usage of srcifp feature.
6676 	 */
6677 	if (ipif != NULL && src_ipif == NULL) {
6678 		/*
6679 		 * Save enough information so that we can recreate the IRE if
6680 		 * the interface goes down and then up.  The metrics associated
6681 		 * with the route will be saved as well when rts_setmetrics() is
6682 		 * called after the IRE has been created.  In the case where
6683 		 * memory cannot be allocated, none of this information will be
6684 		 * saved.
6685 		 */
6686 		ipif_save_ire(ipif, ire);
6687 	}
6688 	if (ioctl_msg)
6689 		ip_rts_rtmsg(RTM_OLDADD, ire, 0);
6690 	if (ire_arg != NULL) {
6691 		/*
6692 		 * Store the ire that was successfully added into where ire_arg
6693 		 * points to so that callers don't have to look it up
6694 		 * themselves (but they are responsible for ire_refrele()ing
6695 		 * the ire when they are finished with it).
6696 		 */
6697 		*ire_arg = ire;
6698 	} else {
6699 		ire_refrele(ire);		/* Held in ire_add */
6700 	}
6701 	if (ipif_refheld)
6702 		ipif_refrele(ipif);
6703 	return (0);
6704 }
6705 
6706 /*
6707  * ip_rt_delete is called to delete an IPv4 route.
6708  * ipif_arg is passed in to associate it with the correct interface.
6709  * src_ipif is passed to associate the incoming interface of the packet.
6710  * We may need to restart this operation if the ipif cannot be looked up
6711  * due to an exclusive operation that is currently in progress. The restart
6712  * entry point is specified by 'func'
6713  */
6714 /* ARGSUSED4 */
6715 int
6716 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6717     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6718     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func)
6719 {
6720 	ire_t	*ire = NULL;
6721 	ipif_t	*ipif;
6722 	boolean_t ipif_refheld = B_FALSE;
6723 	uint_t	type;
6724 	uint_t	match_flags = MATCH_IRE_TYPE;
6725 	int	err = 0;
6726 
6727 	ip1dbg(("ip_rt_delete:"));
6728 	/*
6729 	 * If this is the case of RTF_HOST being set, then we set the netmask
6730 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
6731 	 */
6732 	if (flags & RTF_HOST) {
6733 		mask = IP_HOST_MASK;
6734 		match_flags |= MATCH_IRE_MASK;
6735 	} else if (rtm_addrs & RTA_NETMASK) {
6736 		match_flags |= MATCH_IRE_MASK;
6737 	}
6738 
6739 	/*
6740 	 * Note that RTF_GATEWAY is never set on a delete, therefore
6741 	 * we check if the gateway address is one of our interfaces first,
6742 	 * and fall back on RTF_GATEWAY routes.
6743 	 *
6744 	 * This makes it possible to delete an original
6745 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
6746 	 *
6747 	 * As the interface index specified with the RTA_IFP sockaddr is the
6748 	 * same for all ipif's off of an ill, the matching logic below uses
6749 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
6750 	 * sharing the same prefix and interface index as the the route
6751 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
6752 	 * is specified in the request.
6753 	 *
6754 	 * On the other hand, since the gateway address will usually be
6755 	 * different for each ipif on the system, the matching logic
6756 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
6757 	 * route.  This means that interface routes for the same prefix can be
6758 	 * uniquely identified if they belong to distinct ipif's and if a
6759 	 * RTA_IFP sockaddr is not present.
6760 	 *
6761 	 * For more detail on specifying routes by gateway address and by
6762 	 * interface index, see the comments in ip_rt_add().
6763 	 * gw_addr could be zero in some cases when both RTA_SRCIFP and
6764 	 * RTA_IFP are specified. If RTA_SRCIFP is specified and  both
6765 	 * RTA_IFP and gateway_addr are NULL/zero, then delete will not
6766 	 * succeed.
6767 	 */
6768 	if (src_ipif != NULL) {
6769 		if (ipif_arg == NULL && gw_addr != 0) {
6770 			ipif_arg = ipif_lookup_interface(gw_addr, dst_addr,
6771 			    q, mp, func, &err);
6772 			if (ipif_arg != NULL)
6773 				ipif_refheld = B_TRUE;
6774 		}
6775 		if (ipif_arg == NULL) {
6776 			err = (err == EINPROGRESS) ? err : ESRCH;
6777 			return (err);
6778 		}
6779 		ipif = ipif_arg;
6780 	} else {
6781 		ipif = ipif_lookup_interface(gw_addr, dst_addr,
6782 			    q, mp, func, &err);
6783 		if (ipif != NULL)
6784 			ipif_refheld = B_TRUE;
6785 		else if (err == EINPROGRESS)
6786 			return (err);
6787 		else
6788 			err = 0;
6789 	}
6790 	if (ipif != NULL) {
6791 		if (ipif_arg != NULL) {
6792 			if (ipif_refheld) {
6793 				ipif_refrele(ipif);
6794 				ipif_refheld = B_FALSE;
6795 			}
6796 			ipif = ipif_arg;
6797 			match_flags |= MATCH_IRE_ILL;
6798 		} else {
6799 			match_flags |= MATCH_IRE_IPIF;
6800 		}
6801 		if (src_ipif != NULL) {
6802 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
6803 			    ipif, src_ipif->ipif_ill, match_flags);
6804 		} else {
6805 			if (ipif->ipif_ire_type == IRE_LOOPBACK) {
6806 				ire = ire_ctable_lookup(dst_addr, 0,
6807 				    IRE_LOOPBACK, ipif, ALL_ZONES, NULL,
6808 				    match_flags);
6809 			}
6810 			if (ire == NULL) {
6811 				ire = ire_ftable_lookup(dst_addr, mask, 0,
6812 				    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
6813 				    NULL, match_flags);
6814 			}
6815 		}
6816 	}
6817 
6818 	if (ire == NULL) {
6819 		/*
6820 		 * At this point, the gateway address is not one of our own
6821 		 * addresses or a matching interface route was not found.  We
6822 		 * set the IRE type to lookup based on whether
6823 		 * this is a host route, a default route or just a prefix.
6824 		 *
6825 		 * If an ipif_arg was passed in, then the lookup is based on an
6826 		 * interface index so MATCH_IRE_ILL is added to match_flags.
6827 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
6828 		 * set as the route being looked up is not a traditional
6829 		 * interface route.
6830 		 * Since we do not add gateway route with srcipif, we don't
6831 		 * expect to find it either.
6832 		 */
6833 		if (src_ipif != NULL) {
6834 			if (ipif_refheld)
6835 				ipif_refrele(ipif);
6836 			return (ESRCH);
6837 		} else {
6838 			match_flags &= ~MATCH_IRE_IPIF;
6839 			match_flags |= MATCH_IRE_GW;
6840 			if (ipif_arg != NULL)
6841 				match_flags |= MATCH_IRE_ILL;
6842 			if (mask == IP_HOST_MASK)
6843 				type = IRE_HOST;
6844 			else if (mask == 0)
6845 				type = IRE_DEFAULT;
6846 			else
6847 				type = IRE_PREFIX;
6848 			ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type,
6849 			    ipif_arg, NULL, ALL_ZONES, 0, NULL, match_flags);
6850 			if (ire == NULL && type == IRE_HOST) {
6851 				ire = ire_ftable_lookup(dst_addr, mask, gw_addr,
6852 				    IRE_HOST_REDIRECT, ipif_arg, NULL,
6853 				    ALL_ZONES, 0, NULL, match_flags);
6854 			}
6855 		}
6856 	}
6857 
6858 	if (ipif_refheld)
6859 		ipif_refrele(ipif);
6860 
6861 	/* ipif is not refheld anymore */
6862 	if (ire == NULL)
6863 		return (ESRCH);
6864 
6865 	if (ire->ire_flags & RTF_MULTIRT) {
6866 		/*
6867 		 * Invoke the CGTP (multirouting) filtering module
6868 		 * to remove the dst address from the filtering database.
6869 		 * Packets coming from that address will no longer be
6870 		 * filtered to remove duplicates.
6871 		 */
6872 		if (ip_cgtp_filter_ops != NULL) {
6873 			err = ip_cgtp_filter_ops->cfo_del_dest_v4(ire->ire_addr,
6874 			    ire->ire_gateway_addr);
6875 		}
6876 		ip_cgtp_bcast_delete(ire);
6877 	}
6878 
6879 	ipif = ire->ire_ipif;
6880 	/*
6881 	 * Removing from ipif_saved_ire_mp is not necessary
6882 	 * when src_ipif being non-NULL. ip_rt_add does not
6883 	 * save the ires which src_ipif being non-NULL.
6884 	 */
6885 	if (ipif != NULL && src_ipif == NULL) {
6886 		ipif_remove_ire(ipif, ire);
6887 	}
6888 	if (ioctl_msg)
6889 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0);
6890 	ire_delete(ire);
6891 	ire_refrele(ire);
6892 	return (err);
6893 }
6894 
6895 /*
6896  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6897  */
6898 /* ARGSUSED */
6899 int
6900 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6901     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6902 {
6903 	ipaddr_t dst_addr;
6904 	ipaddr_t gw_addr;
6905 	ipaddr_t mask;
6906 	int error = 0;
6907 	mblk_t *mp1;
6908 	struct rtentry *rt;
6909 	ipif_t *ipif = NULL;
6910 
6911 	ip1dbg(("ip_siocaddrt:"));
6912 	/* Existence of mp1 verified in ip_wput_nondata */
6913 	mp1 = mp->b_cont->b_cont;
6914 	rt = (struct rtentry *)mp1->b_rptr;
6915 
6916 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6917 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6918 
6919 	/*
6920 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6921 	 * to a particular host address.  In this case, we set the netmask to
6922 	 * all ones for the particular destination address.  Otherwise,
6923 	 * determine the netmask to be used based on dst_addr and the interfaces
6924 	 * in use.
6925 	 */
6926 	if (rt->rt_flags & RTF_HOST) {
6927 		mask = IP_HOST_MASK;
6928 	} else {
6929 		/*
6930 		 * Note that ip_subnet_mask returns a zero mask in the case of
6931 		 * default (an all-zeroes address).
6932 		 */
6933 		mask = ip_subnet_mask(dst_addr, &ipif);
6934 	}
6935 
6936 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
6937 	    NULL, B_TRUE, q, mp, ip_process_ioctl, NULL);
6938 	if (ipif != NULL)
6939 		ipif_refrele(ipif);
6940 	return (error);
6941 }
6942 
6943 /*
6944  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6945  */
6946 /* ARGSUSED */
6947 int
6948 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6949     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6950 {
6951 	ipaddr_t dst_addr;
6952 	ipaddr_t gw_addr;
6953 	ipaddr_t mask;
6954 	int error;
6955 	mblk_t *mp1;
6956 	struct rtentry *rt;
6957 	ipif_t *ipif = NULL;
6958 
6959 	ip1dbg(("ip_siocdelrt:"));
6960 	/* Existence of mp1 verified in ip_wput_nondata */
6961 	mp1 = mp->b_cont->b_cont;
6962 	rt = (struct rtentry *)mp1->b_rptr;
6963 
6964 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6965 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6966 
6967 	/*
6968 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6969 	 * to a particular host address.  In this case, we set the netmask to
6970 	 * all ones for the particular destination address.  Otherwise,
6971 	 * determine the netmask to be used based on dst_addr and the interfaces
6972 	 * in use.
6973 	 */
6974 	if (rt->rt_flags & RTF_HOST) {
6975 		mask = IP_HOST_MASK;
6976 	} else {
6977 		/*
6978 		 * Note that ip_subnet_mask returns a zero mask in the case of
6979 		 * default (an all-zeroes address).
6980 		 */
6981 		mask = ip_subnet_mask(dst_addr, &ipif);
6982 	}
6983 
6984 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6985 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, NULL,
6986 	    B_TRUE, q, mp, ip_process_ioctl);
6987 	if (ipif != NULL)
6988 		ipif_refrele(ipif);
6989 	return (error);
6990 }
6991 
6992 /*
6993  * Enqueue the mp onto the ipsq, chained by b_next.
6994  * b_prev stores the function to be executed later, and b_queue the queue
6995  * where this mp originated.
6996  */
6997 void
6998 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6999     ill_t *pending_ill)
7000 {
7001 	conn_t	*connp = NULL;
7002 
7003 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7004 	ASSERT(func != NULL);
7005 
7006 	mp->b_queue = q;
7007 	mp->b_prev = (void *)func;
7008 	mp->b_next = NULL;
7009 
7010 	switch (type) {
7011 	case CUR_OP:
7012 		if (ipsq->ipsq_mptail != NULL) {
7013 			ASSERT(ipsq->ipsq_mphead != NULL);
7014 			ipsq->ipsq_mptail->b_next = mp;
7015 		} else {
7016 			ASSERT(ipsq->ipsq_mphead == NULL);
7017 			ipsq->ipsq_mphead = mp;
7018 		}
7019 		ipsq->ipsq_mptail = mp;
7020 		break;
7021 
7022 	case NEW_OP:
7023 		if (ipsq->ipsq_xopq_mptail != NULL) {
7024 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7025 			ipsq->ipsq_xopq_mptail->b_next = mp;
7026 		} else {
7027 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7028 			ipsq->ipsq_xopq_mphead = mp;
7029 		}
7030 		ipsq->ipsq_xopq_mptail = mp;
7031 		break;
7032 	default:
7033 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7034 	}
7035 
7036 	if (CONN_Q(q) && pending_ill != NULL) {
7037 		connp = Q_TO_CONN(q);
7038 
7039 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7040 		connp->conn_oper_pending_ill = pending_ill;
7041 	}
7042 }
7043 
7044 /*
7045  * Return the mp at the head of the ipsq. After emptying the ipsq
7046  * look at the next ioctl, if this ioctl is complete. Otherwise
7047  * return, we will resume when we complete the current ioctl.
7048  * The current ioctl will wait till it gets a response from the
7049  * driver below.
7050  */
7051 static mblk_t *
7052 ipsq_dq(ipsq_t *ipsq)
7053 {
7054 	mblk_t	*mp;
7055 
7056 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7057 
7058 	mp = ipsq->ipsq_mphead;
7059 	if (mp != NULL) {
7060 		ipsq->ipsq_mphead = mp->b_next;
7061 		if (ipsq->ipsq_mphead == NULL)
7062 			ipsq->ipsq_mptail = NULL;
7063 		mp->b_next = NULL;
7064 		return (mp);
7065 	}
7066 	if (ipsq->ipsq_current_ipif != NULL)
7067 		return (NULL);
7068 	mp = ipsq->ipsq_xopq_mphead;
7069 	if (mp != NULL) {
7070 		ipsq->ipsq_xopq_mphead = mp->b_next;
7071 		if (ipsq->ipsq_xopq_mphead == NULL)
7072 			ipsq->ipsq_xopq_mptail = NULL;
7073 		mp->b_next = NULL;
7074 		return (mp);
7075 	}
7076 	return (NULL);
7077 }
7078 
7079 /*
7080  * Enter the ipsq corresponding to ill, by waiting synchronously till
7081  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7082  * will have to drain completely before ipsq_enter returns success.
7083  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7084  * and the ipsq_exit logic will start the next enqueued ioctl after
7085  * completion of the current ioctl. If 'force' is used, we don't wait
7086  * for the enqueued ioctls. This is needed when a conn_close wants to
7087  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7088  * of an ill can also use this option. But we dont' use it currently.
7089  */
7090 #define	ENTER_SQ_WAIT_TICKS 100
7091 boolean_t
7092 ipsq_enter(ill_t *ill, boolean_t force)
7093 {
7094 	ipsq_t	*ipsq;
7095 	boolean_t waited_enough = B_FALSE;
7096 
7097 	/*
7098 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7099 	 * Since the <ill-ipsq> assocs could change while we wait for the
7100 	 * writer, it is easier to wait on a fixed global rather than try to
7101 	 * cv_wait on a changing ipsq.
7102 	 */
7103 	mutex_enter(&ill->ill_lock);
7104 	for (;;) {
7105 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7106 			mutex_exit(&ill->ill_lock);
7107 			return (B_FALSE);
7108 		}
7109 
7110 		ipsq = ill->ill_phyint->phyint_ipsq;
7111 		mutex_enter(&ipsq->ipsq_lock);
7112 		if (ipsq->ipsq_writer == NULL &&
7113 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7114 			break;
7115 		} else if (ipsq->ipsq_writer != NULL) {
7116 			mutex_exit(&ipsq->ipsq_lock);
7117 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7118 		} else {
7119 			mutex_exit(&ipsq->ipsq_lock);
7120 			if (force) {
7121 				(void) cv_timedwait(&ill->ill_cv,
7122 				    &ill->ill_lock,
7123 				    lbolt + ENTER_SQ_WAIT_TICKS);
7124 				waited_enough = B_TRUE;
7125 				continue;
7126 			} else {
7127 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7128 			}
7129 		}
7130 	}
7131 
7132 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7133 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7134 	ipsq->ipsq_writer = curthread;
7135 	ipsq->ipsq_reentry_cnt++;
7136 #ifdef ILL_DEBUG
7137 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7138 #endif
7139 	mutex_exit(&ipsq->ipsq_lock);
7140 	mutex_exit(&ill->ill_lock);
7141 	return (B_TRUE);
7142 }
7143 
7144 /*
7145  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7146  * certain critical operations like plumbing (i.e. most set ioctls),
7147  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7148  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7149  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7150  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7151  * threads executing in the ipsq. Responses from the driver pertain to the
7152  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7153  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7154  *
7155  * If a thread does not want to reenter the ipsq when it is already writer,
7156  * it must make sure that the specified reentry point to be called later
7157  * when the ipsq is empty, nor any code path starting from the specified reentry
7158  * point must never ever try to enter the ipsq again. Otherwise it can lead
7159  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7160  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7161  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7162  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7163  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7164  * ioctl if the current ioctl has completed. If the current ioctl is still
7165  * in progress it simply returns. The current ioctl could be waiting for
7166  * a response from another module (arp_ or the driver or could be waiting for
7167  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7168  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7169  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7170  * ipsq_current_ipif is clear which happens only on ioctl completion.
7171  */
7172 
7173 /*
7174  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7175  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7176  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7177  * completion.
7178  */
7179 ipsq_t *
7180 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7181     ipsq_func_t func, int type, boolean_t reentry_ok)
7182 {
7183 	ipsq_t	*ipsq;
7184 
7185 	/* Only 1 of ipif or ill can be specified */
7186 	ASSERT((ipif != NULL) ^ (ill != NULL));
7187 	if (ipif != NULL)
7188 		ill = ipif->ipif_ill;
7189 
7190 	/*
7191 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7192 	 * ipsq of an ill can't change when ill_lock is held.
7193 	 */
7194 	GRAB_CONN_LOCK(q);
7195 	mutex_enter(&ill->ill_lock);
7196 	ipsq = ill->ill_phyint->phyint_ipsq;
7197 	mutex_enter(&ipsq->ipsq_lock);
7198 
7199 	/*
7200 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7201 	 *    (Note: If the caller does not specify reentry_ok then neither
7202 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7203 	 *    again. Otherwise it can lead to an infinite loop
7204 	 * 2. Enter the ipsq if there is no current writer and this attempted
7205 	 *    entry is part of the current ioctl or operation
7206 	 * 3. Enter the ipsq if there is no current writer and this is a new
7207 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7208 	 *    empty and there is no ioctl (or operation) currently in progress
7209 	 */
7210 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7211 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7212 	    ipsq->ipsq_current_ipif == NULL))) ||
7213 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7214 		/* Success. */
7215 		ipsq->ipsq_reentry_cnt++;
7216 		ipsq->ipsq_writer = curthread;
7217 		mutex_exit(&ipsq->ipsq_lock);
7218 		mutex_exit(&ill->ill_lock);
7219 		RELEASE_CONN_LOCK(q);
7220 #ifdef ILL_DEBUG
7221 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7222 #endif
7223 		return (ipsq);
7224 	}
7225 
7226 	ipsq_enq(ipsq, q, mp, func, type, ill);
7227 
7228 	mutex_exit(&ipsq->ipsq_lock);
7229 	mutex_exit(&ill->ill_lock);
7230 	RELEASE_CONN_LOCK(q);
7231 	return (NULL);
7232 }
7233 
7234 /*
7235  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7236  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7237  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7238  * completion.
7239  *
7240  * This function does a refrele on the ipif/ill.
7241  */
7242 void
7243 qwriter_ip(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7244     ipsq_func_t func, int type, boolean_t reentry_ok)
7245 {
7246 	ipsq_t	*ipsq;
7247 
7248 	ipsq = ipsq_try_enter(ipif, ill, q, mp, func, type, reentry_ok);
7249 	/*
7250 	 * Caller must have done a refhold on the ipif. ipif_refrele
7251 	 * happens on the passed ipif. We can do this since we are
7252 	 * already exclusive, or we won't access ipif henceforth, Both
7253 	 * this func and caller will just return if we ipsq_try_enter
7254 	 * fails above. This is needed because func needs to
7255 	 * see the correct refcount. Eg. removeif can work only then.
7256 	 */
7257 	if (ipif != NULL)
7258 		ipif_refrele(ipif);
7259 	else
7260 		ill_refrele(ill);
7261 	if (ipsq != NULL) {
7262 		(*func)(ipsq, q, mp, NULL);
7263 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7264 	}
7265 }
7266 
7267 /*
7268  * If there are more than ILL_GRP_CNT ills in a group,
7269  * we use kmem alloc'd buffers, else use the stack
7270  */
7271 #define	ILL_GRP_CNT	14
7272 /*
7273  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7274  * Called by a thread that is currently exclusive on this ipsq.
7275  */
7276 void
7277 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7278 {
7279 	queue_t	*q;
7280 	mblk_t	*mp;
7281 	ipsq_func_t	func;
7282 	int	next;
7283 	ill_t	**ill_list = NULL;
7284 	size_t	ill_list_size = 0;
7285 	int	cnt = 0;
7286 	boolean_t need_ipsq_free = B_FALSE;
7287 
7288 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7289 	mutex_enter(&ipsq->ipsq_lock);
7290 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7291 	if (ipsq->ipsq_reentry_cnt != 1) {
7292 		ipsq->ipsq_reentry_cnt--;
7293 		mutex_exit(&ipsq->ipsq_lock);
7294 		return;
7295 	}
7296 
7297 	mp = ipsq_dq(ipsq);
7298 	while (mp != NULL) {
7299 again:
7300 		mutex_exit(&ipsq->ipsq_lock);
7301 		func = (ipsq_func_t)mp->b_prev;
7302 		q = (queue_t *)mp->b_queue;
7303 		mp->b_prev = NULL;
7304 		mp->b_queue = NULL;
7305 
7306 		/*
7307 		 * If 'q' is an conn queue, it is valid, since we did a
7308 		 * a refhold on the connp, at the start of the ioctl.
7309 		 * If 'q' is an ill queue, it is valid, since close of an
7310 		 * ill will clean up the 'ipsq'.
7311 		 */
7312 		(*func)(ipsq, q, mp, NULL);
7313 
7314 		mutex_enter(&ipsq->ipsq_lock);
7315 		mp = ipsq_dq(ipsq);
7316 	}
7317 
7318 	mutex_exit(&ipsq->ipsq_lock);
7319 
7320 	/*
7321 	 * Need to grab the locks in the right order. Need to
7322 	 * atomically check (under ipsq_lock) that there are no
7323 	 * messages before relinquishing the ipsq. Also need to
7324 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7325 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7326 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7327 	 * to grab ill_g_lock as writer.
7328 	 */
7329 	rw_enter(&ill_g_lock, ipsq->ipsq_split ? RW_WRITER : RW_READER);
7330 
7331 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7332 	if (ipsq->ipsq_refs != 0) {
7333 		/* At most 2 ills v4/v6 per phyint */
7334 		cnt = ipsq->ipsq_refs << 1;
7335 		ill_list_size = cnt * sizeof (ill_t *);
7336 		/*
7337 		 * If memory allocation fails, we will do the split
7338 		 * the next time ipsq_exit is called for whatever reason.
7339 		 * As long as the ipsq_split flag is set the need to
7340 		 * split is remembered.
7341 		 */
7342 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7343 		if (ill_list != NULL)
7344 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7345 	}
7346 	mutex_enter(&ipsq->ipsq_lock);
7347 	mp = ipsq_dq(ipsq);
7348 	if (mp != NULL) {
7349 		/* oops, some message has landed up, we can't get out */
7350 		if (ill_list != NULL)
7351 			ill_unlock_ills(ill_list, cnt);
7352 		rw_exit(&ill_g_lock);
7353 		if (ill_list != NULL)
7354 			kmem_free(ill_list, ill_list_size);
7355 		ill_list = NULL;
7356 		ill_list_size = 0;
7357 		cnt = 0;
7358 		goto again;
7359 	}
7360 
7361 	/*
7362 	 * Split only if no ioctl is pending and if memory alloc succeeded
7363 	 * above.
7364 	 */
7365 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7366 		ill_list != NULL) {
7367 		/*
7368 		 * No new ill can join this ipsq since we are holding the
7369 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7370 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7371 		 * If so we will retry on the next ipsq_exit.
7372 		 */
7373 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7374 	}
7375 
7376 	/*
7377 	 * We are holding the ipsq lock, hence no new messages can
7378 	 * land up on the ipsq, and there are no messages currently.
7379 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7380 	 * atomically while holding ill locks.
7381 	 */
7382 	ipsq->ipsq_writer = NULL;
7383 	ipsq->ipsq_reentry_cnt--;
7384 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7385 #ifdef ILL_DEBUG
7386 	ipsq->ipsq_depth = 0;
7387 #endif
7388 	mutex_exit(&ipsq->ipsq_lock);
7389 	/*
7390 	 * For IPMP this should wake up all ills in this ipsq.
7391 	 * We need to hold the ill_lock while waking up waiters to
7392 	 * avoid missed wakeups. But there is no need to acquire all
7393 	 * the ill locks and then wakeup. If we have not acquired all
7394 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7395 	 * wakes up ills one at a time after getting the right ill_lock
7396 	 */
7397 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7398 	if (ill_list != NULL)
7399 		ill_unlock_ills(ill_list, cnt);
7400 	if (ipsq->ipsq_refs == 0)
7401 		need_ipsq_free = B_TRUE;
7402 	rw_exit(&ill_g_lock);
7403 	if (ill_list != 0)
7404 		kmem_free(ill_list, ill_list_size);
7405 
7406 	if (need_ipsq_free) {
7407 		/*
7408 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7409 		 * looked up. ipsq can be looked up only thru ill or phyint
7410 		 * and there are no ills/phyint on this ipsq.
7411 		 */
7412 		ipsq_delete(ipsq);
7413 	}
7414 	/*
7415 	 * Now start any igmp or mld timers that could not be started
7416 	 * while inside the ipsq. The timers can't be started while inside
7417 	 * the ipsq, since igmp_start_timers may need to call untimeout()
7418 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
7419 	 * there could be a deadlock since the timeout handlers
7420 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
7421 	 * wait in ipsq_enter() trying to get the ipsq.
7422 	 *
7423 	 * However there is one exception to the above. If this thread is
7424 	 * itself the igmp/mld timeout handler thread, then we don't want
7425 	 * to start any new timer until the current handler is done. The
7426 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
7427 	 * all others pass B_TRUE.
7428 	 */
7429 	if (start_igmp_timer) {
7430 		mutex_enter(&igmp_timer_lock);
7431 		next = igmp_deferred_next;
7432 		igmp_deferred_next = INFINITY;
7433 		mutex_exit(&igmp_timer_lock);
7434 
7435 		if (next != INFINITY)
7436 			igmp_start_timers(next);
7437 	}
7438 
7439 	if (start_mld_timer) {
7440 		mutex_enter(&mld_timer_lock);
7441 		next = mld_deferred_next;
7442 		mld_deferred_next = INFINITY;
7443 		mutex_exit(&mld_timer_lock);
7444 
7445 		if (next != INFINITY)
7446 			mld_start_timers(next);
7447 	}
7448 }
7449 
7450 /*
7451  * The ill is closing. Flush all messages on the ipsq that originated
7452  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
7453  * for this ill since ipsq_enter could not have entered until then.
7454  * New messages can't be queued since the CONDEMNED flag is set.
7455  */
7456 static void
7457 ipsq_flush(ill_t *ill)
7458 {
7459 	queue_t	*q;
7460 	mblk_t	*prev;
7461 	mblk_t	*mp;
7462 	mblk_t	*mp_next;
7463 	ipsq_t	*ipsq;
7464 
7465 	ASSERT(IAM_WRITER_ILL(ill));
7466 	ipsq = ill->ill_phyint->phyint_ipsq;
7467 	/*
7468 	 * Flush any messages sent up by the driver.
7469 	 */
7470 	mutex_enter(&ipsq->ipsq_lock);
7471 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
7472 		mp_next = mp->b_next;
7473 		q = mp->b_queue;
7474 		if (q == ill->ill_rq || q == ill->ill_wq) {
7475 			/* Remove the mp from the ipsq */
7476 			if (prev == NULL)
7477 				ipsq->ipsq_mphead = mp->b_next;
7478 			else
7479 				prev->b_next = mp->b_next;
7480 			if (ipsq->ipsq_mptail == mp) {
7481 				ASSERT(mp_next == NULL);
7482 				ipsq->ipsq_mptail = prev;
7483 			}
7484 			inet_freemsg(mp);
7485 		} else {
7486 			prev = mp;
7487 		}
7488 	}
7489 	mutex_exit(&ipsq->ipsq_lock);
7490 	(void) ipsq_pending_mp_cleanup(ill, NULL);
7491 	ipsq_xopq_mp_cleanup(ill, NULL);
7492 	ill_pending_mp_cleanup(ill);
7493 }
7494 
7495 /*
7496  * Clean up one squeue element. ill_inuse_ref is protected by ill_lock.
7497  * The real cleanup happens behind the squeue via ip_squeue_clean function but
7498  * we need to protect ourselfs from 2 threads trying to cleanup at the same
7499  * time (possible with one port going down for aggr and someone tearing down the
7500  * entire aggr simultaneously. So we use ill_inuse_ref protected by ill_lock
7501  * to indicate when the cleanup has started (1 ref) and when the cleanup
7502  * is done (0 ref). When a new ring gets assigned to squeue, we start by
7503  * putting 2 ref on ill_inuse_ref.
7504  */
7505 static void
7506 ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring)
7507 {
7508 	conn_t *connp;
7509 	squeue_t *sqp;
7510 	mblk_t *mp;
7511 
7512 	ASSERT(rx_ring != NULL);
7513 
7514 	/* Just clean one squeue */
7515 	mutex_enter(&ill->ill_lock);
7516 	/*
7517 	 * Reset the ILL_SOFT_RING_ASSIGN bit so that
7518 	 * ip_squeue_soft_ring_affinty() will not go
7519 	 * ahead with assigning rings.
7520 	 */
7521 	ill->ill_state_flags &= ~ILL_SOFT_RING_ASSIGN;
7522 	while (rx_ring->rr_ring_state == ILL_RING_INPROC)
7523 		/* Some operations pending on the ring. Wait */
7524 		cv_wait(&ill->ill_cv, &ill->ill_lock);
7525 
7526 	if (rx_ring->rr_ring_state != ILL_RING_INUSE) {
7527 		/*
7528 		 * Someone already trying to clean
7529 		 * this squeue or its already been cleaned.
7530 		 */
7531 		mutex_exit(&ill->ill_lock);
7532 		return;
7533 	}
7534 	sqp = rx_ring->rr_sqp;
7535 
7536 	if (sqp == NULL) {
7537 		/*
7538 		 * The rx_ring never had a squeue assigned to it.
7539 		 * We are under ill_lock so we can clean it up
7540 		 * here itself since no one can get to it.
7541 		 */
7542 		rx_ring->rr_blank = NULL;
7543 		rx_ring->rr_handle = NULL;
7544 		rx_ring->rr_sqp = NULL;
7545 		rx_ring->rr_ring_state = ILL_RING_FREE;
7546 		mutex_exit(&ill->ill_lock);
7547 		return;
7548 	}
7549 
7550 	/* Set the state that its being cleaned */
7551 	rx_ring->rr_ring_state = ILL_RING_BEING_FREED;
7552 	ASSERT(sqp != NULL);
7553 	mutex_exit(&ill->ill_lock);
7554 
7555 	/*
7556 	 * Use the preallocated ill_unbind_conn for this purpose
7557 	 */
7558 	connp = ill->ill_dls_capab->ill_unbind_conn;
7559 	mp = &connp->conn_tcp->tcp_closemp;
7560 	CONN_INC_REF(connp);
7561 	squeue_enter(sqp, mp, ip_squeue_clean, connp, NULL);
7562 
7563 	mutex_enter(&ill->ill_lock);
7564 	while (rx_ring->rr_ring_state != ILL_RING_FREE)
7565 		cv_wait(&ill->ill_cv, &ill->ill_lock);
7566 
7567 	mutex_exit(&ill->ill_lock);
7568 }
7569 
7570 static void
7571 ipsq_clean_all(ill_t *ill)
7572 {
7573 	int idx;
7574 
7575 	/*
7576 	 * No need to clean if poll_capab isn't set for this ill
7577 	 */
7578 	if (!(ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING)))
7579 		return;
7580 
7581 	for (idx = 0; idx < ILL_MAX_RINGS; idx++) {
7582 		ill_rx_ring_t *ipr = &ill->ill_dls_capab->ill_ring_tbl[idx];
7583 		ipsq_clean_ring(ill, ipr);
7584 	}
7585 
7586 	ill->ill_capabilities &= ~(ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING);
7587 }
7588 
7589 /* ARGSUSED */
7590 int
7591 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
7592     ip_ioctl_cmd_t *ipip, void *ifreq)
7593 {
7594 	ill_t	*ill;
7595 	struct lifreq	*lifr = (struct lifreq *)ifreq;
7596 	boolean_t isv6;
7597 	conn_t	*connp;
7598 
7599 	connp = Q_TO_CONN(q);
7600 	isv6 = connp->conn_af_isv6;
7601 	/*
7602 	 * Set original index.
7603 	 * Failover and failback move logical interfaces
7604 	 * from one physical interface to another.  The
7605 	 * original index indicates the parent of a logical
7606 	 * interface, in other words, the physical interface
7607 	 * the logical interface will be moved back to on
7608 	 * failback.
7609 	 */
7610 
7611 	/*
7612 	 * Don't allow the original index to be changed
7613 	 * for non-failover addresses, autoconfigured
7614 	 * addresses, or IPv6 link local addresses.
7615 	 */
7616 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
7617 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
7618 		return (EINVAL);
7619 	}
7620 	/*
7621 	 * The new original index must be in use by some
7622 	 * physical interface.
7623 	 */
7624 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
7625 	    NULL, NULL);
7626 	if (ill == NULL)
7627 		return (ENXIO);
7628 	ill_refrele(ill);
7629 
7630 	ipif->ipif_orig_ifindex = lifr->lifr_index;
7631 	/*
7632 	 * When this ipif gets failed back, don't
7633 	 * preserve the original id, as it is no
7634 	 * longer applicable.
7635 	 */
7636 	ipif->ipif_orig_ipifid = 0;
7637 	/*
7638 	 * For IPv4, change the original index of any
7639 	 * multicast addresses associated with the
7640 	 * ipif to the new value.
7641 	 */
7642 	if (!isv6) {
7643 		ilm_t *ilm;
7644 
7645 		mutex_enter(&ipif->ipif_ill->ill_lock);
7646 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
7647 		    ilm = ilm->ilm_next) {
7648 			if (ilm->ilm_ipif == ipif) {
7649 				ilm->ilm_orig_ifindex = lifr->lifr_index;
7650 			}
7651 		}
7652 		mutex_exit(&ipif->ipif_ill->ill_lock);
7653 	}
7654 	return (0);
7655 }
7656 
7657 /* ARGSUSED */
7658 int
7659 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
7660     ip_ioctl_cmd_t *ipip, void *ifreq)
7661 {
7662 	struct lifreq *lifr = (struct lifreq *)ifreq;
7663 
7664 	/*
7665 	 * Get the original interface index i.e the one
7666 	 * before FAILOVER if it ever happened.
7667 	 */
7668 	lifr->lifr_index = ipif->ipif_orig_ifindex;
7669 	return (0);
7670 }
7671 
7672 /*
7673  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
7674  * refhold and return the associated ipif
7675  */
7676 int
7677 ip_extract_tunreq(queue_t *q, mblk_t *mp, ipif_t **ipifp, ipsq_func_t func)
7678 {
7679 	boolean_t exists;
7680 	struct iftun_req *ta;
7681 	ipif_t	*ipif;
7682 	ill_t	*ill;
7683 	boolean_t isv6;
7684 	mblk_t	*mp1;
7685 	int	error;
7686 	conn_t	*connp;
7687 
7688 	/* Existence verified in ip_wput_nondata */
7689 	mp1 = mp->b_cont->b_cont;
7690 	ta = (struct iftun_req *)mp1->b_rptr;
7691 	/*
7692 	 * Null terminate the string to protect against buffer
7693 	 * overrun. String was generated by user code and may not
7694 	 * be trusted.
7695 	 */
7696 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
7697 
7698 	connp = Q_TO_CONN(q);
7699 	isv6 = connp->conn_af_isv6;
7700 
7701 	/* Disallows implicit create */
7702 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
7703 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
7704 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error);
7705 	if (ipif == NULL)
7706 		return (error);
7707 
7708 	if (ipif->ipif_id != 0) {
7709 		/*
7710 		 * We really don't want to set/get tunnel parameters
7711 		 * on virtual tunnel interfaces.  Only allow the
7712 		 * base tunnel to do these.
7713 		 */
7714 		ipif_refrele(ipif);
7715 		return (EINVAL);
7716 	}
7717 
7718 	/*
7719 	 * Send down to tunnel mod for ioctl processing.
7720 	 * Will finish ioctl in ip_rput_other().
7721 	 */
7722 	ill = ipif->ipif_ill;
7723 	if (ill->ill_net_type == IRE_LOOPBACK) {
7724 		ipif_refrele(ipif);
7725 		return (EOPNOTSUPP);
7726 	}
7727 
7728 	if (ill->ill_wq == NULL) {
7729 		ipif_refrele(ipif);
7730 		return (ENXIO);
7731 	}
7732 	/*
7733 	 * Mark the ioctl as coming from an IPv6 interface for
7734 	 * tun's convenience.
7735 	 */
7736 	if (ill->ill_isv6)
7737 		ta->ifta_flags |= 0x80000000;
7738 	*ipifp = ipif;
7739 	return (0);
7740 }
7741 
7742 /*
7743  * Parse an ifreq or lifreq struct coming down ioctls and refhold
7744  * and return the associated ipif.
7745  * Return value:
7746  *	Non zero: An error has occurred. ci may not be filled out.
7747  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
7748  *	a held ipif in ci.ci_ipif.
7749  */
7750 int
7751 ip_extract_lifreq_cmn(queue_t *q, mblk_t *mp, int cmd_type, int flags,
7752     cmd_info_t *ci, ipsq_func_t func)
7753 {
7754 	sin_t		*sin;
7755 	sin6_t		*sin6;
7756 	char		*name;
7757 	struct ifreq    *ifr;
7758 	struct lifreq    *lifr;
7759 	ipif_t		*ipif = NULL;
7760 	ill_t		*ill;
7761 	conn_t		*connp;
7762 	boolean_t	isv6;
7763 	struct iocblk   *iocp = (struct iocblk *)mp->b_rptr;
7764 	boolean_t	exists;
7765 	int		err;
7766 	mblk_t		*mp1;
7767 	zoneid_t	zoneid;
7768 
7769 	if (q->q_next != NULL) {
7770 		ill = (ill_t *)q->q_ptr;
7771 		isv6 = ill->ill_isv6;
7772 		connp = NULL;
7773 		zoneid = ALL_ZONES;
7774 	} else {
7775 		ill = NULL;
7776 		connp = Q_TO_CONN(q);
7777 		isv6 = connp->conn_af_isv6;
7778 		zoneid = connp->conn_zoneid;
7779 		if (zoneid == GLOBAL_ZONEID) {
7780 			/* global zone can access ipifs in all zones */
7781 			zoneid = ALL_ZONES;
7782 		}
7783 	}
7784 
7785 	/* Has been checked in ip_wput_nondata */
7786 	mp1 = mp->b_cont->b_cont;
7787 
7788 
7789 	if (cmd_type == IF_CMD) {
7790 		/* This a old style SIOC[GS]IF* command */
7791 		ifr = (struct ifreq *)mp1->b_rptr;
7792 		/*
7793 		 * Null terminate the string to protect against buffer
7794 		 * overrun. String was generated by user code and may not
7795 		 * be trusted.
7796 		 */
7797 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7798 		sin = (sin_t *)&ifr->ifr_addr;
7799 		name = ifr->ifr_name;
7800 		ci->ci_sin = sin;
7801 		ci->ci_sin6 = NULL;
7802 		ci->ci_lifr = (struct lifreq *)ifr;
7803 	} else {
7804 		/* This a new style SIOC[GS]LIF* command */
7805 		ASSERT(cmd_type == LIF_CMD);
7806 		lifr = (struct lifreq *)mp1->b_rptr;
7807 		/*
7808 		 * Null terminate the string to protect against buffer
7809 		 * overrun. String was generated by user code and may not
7810 		 * be trusted.
7811 		 */
7812 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7813 		name = lifr->lifr_name;
7814 		sin = (sin_t *)&lifr->lifr_addr;
7815 		sin6 = (sin6_t *)&lifr->lifr_addr;
7816 		if (iocp->ioc_cmd == SIOCSLIFGROUPNAME) {
7817 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
7818 			    LIFNAMSIZ);
7819 		}
7820 		ci->ci_sin = sin;
7821 		ci->ci_sin6 = sin6;
7822 		ci->ci_lifr = lifr;
7823 	}
7824 
7825 
7826 	if (iocp->ioc_cmd == SIOCSLIFNAME) {
7827 		/*
7828 		 * The ioctl will be failed if the ioctl comes down
7829 		 * an conn stream
7830 		 */
7831 		if (ill == NULL) {
7832 			/*
7833 			 * Not an ill queue, return EINVAL same as the
7834 			 * old error code.
7835 			 */
7836 			return (ENXIO);
7837 		}
7838 		ipif = ill->ill_ipif;
7839 		ipif_refhold(ipif);
7840 	} else {
7841 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
7842 		    &exists, isv6, zoneid,
7843 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err);
7844 		if (ipif == NULL) {
7845 			if (err == EINPROGRESS)
7846 				return (err);
7847 			if (iocp->ioc_cmd == SIOCLIFFAILOVER ||
7848 			    iocp->ioc_cmd == SIOCLIFFAILBACK) {
7849 				/*
7850 				 * Need to try both v4 and v6 since this
7851 				 * ioctl can come down either v4 or v6
7852 				 * socket. The lifreq.lifr_family passed
7853 				 * down by this ioctl is AF_UNSPEC.
7854 				 */
7855 				ipif = ipif_lookup_on_name(name,
7856 				    mi_strlen(name), B_FALSE, &exists, !isv6,
7857 				    zoneid, (connp == NULL) ? q :
7858 				    CONNP_TO_WQ(connp), mp, func, &err);
7859 				if (err == EINPROGRESS)
7860 					return (err);
7861 			}
7862 			err = 0;	/* Ensure we don't use it below */
7863 		}
7864 	}
7865 
7866 	/*
7867 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7868 	 */
7869 	if (ipif != NULL && ipif->ipif_isv6 && cmd_type == IF_CMD) {
7870 		ipif_refrele(ipif);
7871 		return (ENXIO);
7872 	}
7873 
7874 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7875 	    name[0] == '\0') {
7876 		/*
7877 		 * Handle a or a SIOC?IF* with a null name
7878 		 * during plumb (on the ill queue before the I_PLINK).
7879 		 */
7880 		ipif = ill->ill_ipif;
7881 		ipif_refhold(ipif);
7882 	}
7883 
7884 	if (ipif == NULL)
7885 		return (ENXIO);
7886 
7887 	/*
7888 	 * Allow only GET operations if this ipif has been created
7889 	 * temporarily due to a MOVE operation.
7890 	 */
7891 	if (ipif->ipif_replace_zero && !(flags & IPI_REPL)) {
7892 		ipif_refrele(ipif);
7893 		return (EINVAL);
7894 	}
7895 
7896 	ci->ci_ipif = ipif;
7897 	return (0);
7898 }
7899 
7900 /*
7901  * Return the total number of ipifs.
7902  */
7903 static uint_t
7904 ip_get_numifs(zoneid_t zoneid)
7905 {
7906 	uint_t numifs = 0;
7907 	ill_t	*ill;
7908 	ill_walk_context_t	ctx;
7909 	ipif_t	*ipif;
7910 
7911 	rw_enter(&ill_g_lock, RW_READER);
7912 	ill = ILL_START_WALK_V4(&ctx);
7913 
7914 	while (ill != NULL) {
7915 		for (ipif = ill->ill_ipif; ipif != NULL;
7916 		    ipif = ipif->ipif_next) {
7917 			if (ipif->ipif_zoneid == zoneid ||
7918 			    ipif->ipif_zoneid == ALL_ZONES)
7919 				numifs++;
7920 		}
7921 		ill = ill_next(&ctx, ill);
7922 	}
7923 	rw_exit(&ill_g_lock);
7924 	return (numifs);
7925 }
7926 
7927 /*
7928  * Return the total number of ipifs.
7929  */
7930 static uint_t
7931 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid)
7932 {
7933 	uint_t numifs = 0;
7934 	ill_t	*ill;
7935 	ipif_t	*ipif;
7936 	ill_walk_context_t	ctx;
7937 
7938 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7939 
7940 	rw_enter(&ill_g_lock, RW_READER);
7941 	if (family == AF_INET)
7942 		ill = ILL_START_WALK_V4(&ctx);
7943 	else if (family == AF_INET6)
7944 		ill = ILL_START_WALK_V6(&ctx);
7945 	else
7946 		ill = ILL_START_WALK_ALL(&ctx);
7947 
7948 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7949 		for (ipif = ill->ill_ipif; ipif != NULL;
7950 		    ipif = ipif->ipif_next) {
7951 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7952 			    !(lifn_flags & LIFC_NOXMIT))
7953 				continue;
7954 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7955 			    !(lifn_flags & LIFC_TEMPORARY))
7956 				continue;
7957 			if (((ipif->ipif_flags &
7958 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7959 			    IPIF_DEPRECATED)) ||
7960 			    (ill->ill_phyint->phyint_flags &
7961 			    PHYI_LOOPBACK) ||
7962 			    !(ipif->ipif_flags & IPIF_UP)) &&
7963 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7964 				continue;
7965 
7966 			if (zoneid != ipif->ipif_zoneid &&
7967 			    ipif->ipif_zoneid != ALL_ZONES &&
7968 			    (zoneid != GLOBAL_ZONEID ||
7969 			    !(lifn_flags & LIFC_ALLZONES)))
7970 				continue;
7971 
7972 			numifs++;
7973 		}
7974 	}
7975 	rw_exit(&ill_g_lock);
7976 	return (numifs);
7977 }
7978 
7979 uint_t
7980 ip_get_lifsrcofnum(ill_t *ill)
7981 {
7982 	uint_t numifs = 0;
7983 	ill_t	*ill_head = ill;
7984 
7985 	/*
7986 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7987 	 * other thread may be trying to relink the ILLs in this usesrc group
7988 	 * and adjusting the ill_usesrc_grp_next pointers
7989 	 */
7990 	rw_enter(&ill_g_usesrc_lock, RW_READER);
7991 	if ((ill->ill_usesrc_ifindex == 0) &&
7992 	    (ill->ill_usesrc_grp_next != NULL)) {
7993 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7994 		    ill = ill->ill_usesrc_grp_next)
7995 			numifs++;
7996 	}
7997 	rw_exit(&ill_g_usesrc_lock);
7998 
7999 	return (numifs);
8000 }
8001 
8002 /* Null values are passed in for ipif, sin, and ifreq */
8003 /* ARGSUSED */
8004 int
8005 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8006     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8007 {
8008 	int *nump;
8009 
8010 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8011 
8012 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8013 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8014 
8015 	*nump = ip_get_numifs(Q_TO_CONN(q)->conn_zoneid);
8016 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8017 	return (0);
8018 }
8019 
8020 /* Null values are passed in for ipif, sin, and ifreq */
8021 /* ARGSUSED */
8022 int
8023 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8024     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8025 {
8026 	struct lifnum *lifn;
8027 	mblk_t	*mp1;
8028 
8029 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8030 
8031 	/* Existence checked in ip_wput_nondata */
8032 	mp1 = mp->b_cont->b_cont;
8033 
8034 	lifn = (struct lifnum *)mp1->b_rptr;
8035 	switch (lifn->lifn_family) {
8036 	case AF_UNSPEC:
8037 	case AF_INET:
8038 	case AF_INET6:
8039 		break;
8040 	default:
8041 		return (EAFNOSUPPORT);
8042 	}
8043 
8044 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8045 	    Q_TO_CONN(q)->conn_zoneid);
8046 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8047 	return (0);
8048 }
8049 
8050 /* ARGSUSED */
8051 int
8052 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8053     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8054 {
8055 	STRUCT_HANDLE(ifconf, ifc);
8056 	mblk_t *mp1;
8057 	struct iocblk *iocp;
8058 	struct ifreq *ifr;
8059 	ill_walk_context_t	ctx;
8060 	ill_t	*ill;
8061 	ipif_t	*ipif;
8062 	struct sockaddr_in *sin;
8063 	int32_t	ifclen;
8064 	zoneid_t zoneid;
8065 
8066 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8067 
8068 	ip1dbg(("ip_sioctl_get_ifconf"));
8069 	/* Existence verified in ip_wput_nondata */
8070 	mp1 = mp->b_cont->b_cont;
8071 	iocp = (struct iocblk *)mp->b_rptr;
8072 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8073 
8074 	/*
8075 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8076 	 * the user buffer address and length into which the list of struct
8077 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8078 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8079 	 * the SIOCGIFCONF operation was redefined to simply provide
8080 	 * a large output buffer into which we are supposed to jam the ifreq
8081 	 * array.  The same ioctl command code was used, despite the fact that
8082 	 * both the applications and the kernel code had to change, thus making
8083 	 * it impossible to support both interfaces.
8084 	 *
8085 	 * For reasons not good enough to try to explain, the following
8086 	 * algorithm is used for deciding what to do with one of these:
8087 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8088 	 * form with the output buffer coming down as the continuation message.
8089 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8090 	 * and we have to copy in the ifconf structure to find out how big the
8091 	 * output buffer is and where to copy out to.  Sure no problem...
8092 	 *
8093 	 */
8094 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8095 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8096 		int numifs = 0;
8097 		size_t ifc_bufsize;
8098 
8099 		/*
8100 		 * Must be (better be!) continuation of a TRANSPARENT
8101 		 * IOCTL.  We just copied in the ifconf structure.
8102 		 */
8103 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8104 		    (struct ifconf *)mp1->b_rptr);
8105 
8106 		/*
8107 		 * Allocate a buffer to hold requested information.
8108 		 *
8109 		 * If ifc_len is larger than what is needed, we only
8110 		 * allocate what we will use.
8111 		 *
8112 		 * If ifc_len is smaller than what is needed, return
8113 		 * EINVAL.
8114 		 *
8115 		 * XXX: the ill_t structure can hava 2 counters, for
8116 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8117 		 * number of interfaces for a device, so we don't need
8118 		 * to count them here...
8119 		 */
8120 		numifs = ip_get_numifs(zoneid);
8121 
8122 		ifclen = STRUCT_FGET(ifc, ifc_len);
8123 		ifc_bufsize = numifs * sizeof (struct ifreq);
8124 		if (ifc_bufsize > ifclen) {
8125 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8126 				/* old behaviour */
8127 				return (EINVAL);
8128 			} else {
8129 				ifc_bufsize = ifclen;
8130 			}
8131 		}
8132 
8133 		mp1 = mi_copyout_alloc(q, mp,
8134 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8135 		if (mp1 == NULL)
8136 			return (ENOMEM);
8137 
8138 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8139 	}
8140 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8141 	/*
8142 	 * the SIOCGIFCONF ioctl only knows about
8143 	 * IPv4 addresses, so don't try to tell
8144 	 * it about interfaces with IPv6-only
8145 	 * addresses. (Last parm 'isv6' is B_FALSE)
8146 	 */
8147 
8148 	ifr = (struct ifreq *)mp1->b_rptr;
8149 
8150 	rw_enter(&ill_g_lock, RW_READER);
8151 	ill = ILL_START_WALK_V4(&ctx);
8152 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8153 		for (ipif = ill->ill_ipif; ipif;
8154 		    ipif = ipif->ipif_next) {
8155 			if (zoneid != ipif->ipif_zoneid &&
8156 			    ipif->ipif_zoneid != ALL_ZONES)
8157 				continue;
8158 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8159 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8160 					/* old behaviour */
8161 					rw_exit(&ill_g_lock);
8162 					return (EINVAL);
8163 				} else {
8164 					goto if_copydone;
8165 				}
8166 			}
8167 			(void) ipif_get_name(ipif,
8168 			    ifr->ifr_name,
8169 			    sizeof (ifr->ifr_name));
8170 			sin = (sin_t *)&ifr->ifr_addr;
8171 			*sin = sin_null;
8172 			sin->sin_family = AF_INET;
8173 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8174 			ifr++;
8175 		}
8176 	}
8177 if_copydone:
8178 	rw_exit(&ill_g_lock);
8179 	mp1->b_wptr = (uchar_t *)ifr;
8180 
8181 	if (STRUCT_BUF(ifc) != NULL) {
8182 		STRUCT_FSET(ifc, ifc_len,
8183 			(int)((uchar_t *)ifr - mp1->b_rptr));
8184 	}
8185 	return (0);
8186 }
8187 
8188 /*
8189  * Get the interfaces using the address hosted on the interface passed in,
8190  * as a source adddress
8191  */
8192 /* ARGSUSED */
8193 int
8194 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8195     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8196 {
8197 	mblk_t *mp1;
8198 	ill_t	*ill, *ill_head;
8199 	ipif_t	*ipif, *orig_ipif;
8200 	int	numlifs = 0;
8201 	size_t	lifs_bufsize, lifsmaxlen;
8202 	struct	lifreq *lifr;
8203 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8204 	uint_t	ifindex;
8205 	zoneid_t zoneid;
8206 	int err = 0;
8207 	boolean_t isv6 = B_FALSE;
8208 	struct	sockaddr_in	*sin;
8209 	struct	sockaddr_in6	*sin6;
8210 
8211 	STRUCT_HANDLE(lifsrcof, lifs);
8212 
8213 	ASSERT(q->q_next == NULL);
8214 
8215 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8216 
8217 	/* Existence verified in ip_wput_nondata */
8218 	mp1 = mp->b_cont->b_cont;
8219 
8220 	/*
8221 	 * Must be (better be!) continuation of a TRANSPARENT
8222 	 * IOCTL.  We just copied in the lifsrcof structure.
8223 	 */
8224 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8225 	    (struct lifsrcof *)mp1->b_rptr);
8226 
8227 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8228 		return (EINVAL);
8229 
8230 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8231 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8232 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8233 	    ip_process_ioctl, &err);
8234 	if (ipif == NULL) {
8235 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8236 		    ifindex));
8237 		return (err);
8238 	}
8239 
8240 
8241 	/* Allocate a buffer to hold requested information */
8242 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8243 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8244 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8245 	/* The actual size needed is always returned in lifs_len */
8246 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8247 
8248 	/* If the amount we need is more than what is passed in, abort */
8249 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8250 		ipif_refrele(ipif);
8251 		return (0);
8252 	}
8253 
8254 	mp1 = mi_copyout_alloc(q, mp,
8255 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8256 	if (mp1 == NULL) {
8257 		ipif_refrele(ipif);
8258 		return (ENOMEM);
8259 	}
8260 
8261 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8262 	bzero(mp1->b_rptr, lifs_bufsize);
8263 
8264 	lifr = (struct lifreq *)mp1->b_rptr;
8265 
8266 	ill = ill_head = ipif->ipif_ill;
8267 	orig_ipif = ipif;
8268 
8269 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8270 	rw_enter(&ill_g_usesrc_lock, RW_READER);
8271 	rw_enter(&ill_g_lock, RW_READER);
8272 
8273 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8274 	for (; (ill != NULL) && (ill != ill_head);
8275 	    ill = ill->ill_usesrc_grp_next) {
8276 
8277 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8278 			break;
8279 
8280 		ipif = ill->ill_ipif;
8281 		(void) ipif_get_name(ipif,
8282 		    lifr->lifr_name, sizeof (lifr->lifr_name));
8283 		if (ipif->ipif_isv6) {
8284 			sin6 = (sin6_t *)&lifr->lifr_addr;
8285 			*sin6 = sin6_null;
8286 			sin6->sin6_family = AF_INET6;
8287 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8288 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8289 			    &ipif->ipif_v6net_mask);
8290 		} else {
8291 			sin = (sin_t *)&lifr->lifr_addr;
8292 			*sin = sin_null;
8293 			sin->sin_family = AF_INET;
8294 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8295 			lifr->lifr_addrlen = ip_mask_to_plen(
8296 			    ipif->ipif_net_mask);
8297 		}
8298 		lifr++;
8299 	}
8300 	rw_exit(&ill_g_usesrc_lock);
8301 	rw_exit(&ill_g_lock);
8302 	ipif_refrele(orig_ipif);
8303 	mp1->b_wptr = (uchar_t *)lifr;
8304 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8305 
8306 	return (0);
8307 }
8308 
8309 /* ARGSUSED */
8310 int
8311 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8312     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8313 {
8314 	mblk_t *mp1;
8315 	int	list;
8316 	ill_t	*ill;
8317 	ipif_t	*ipif;
8318 	int	flags;
8319 	int	numlifs = 0;
8320 	size_t	lifc_bufsize;
8321 	struct	lifreq *lifr;
8322 	sa_family_t	family;
8323 	struct	sockaddr_in	*sin;
8324 	struct	sockaddr_in6	*sin6;
8325 	ill_walk_context_t	ctx;
8326 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8327 	int32_t	lifclen;
8328 	zoneid_t zoneid;
8329 	STRUCT_HANDLE(lifconf, lifc);
8330 
8331 	ip1dbg(("ip_sioctl_get_lifconf"));
8332 
8333 	ASSERT(q->q_next == NULL);
8334 
8335 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8336 
8337 	/* Existence verified in ip_wput_nondata */
8338 	mp1 = mp->b_cont->b_cont;
8339 
8340 	/*
8341 	 * An extended version of SIOCGIFCONF that takes an
8342 	 * additional address family and flags field.
8343 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8344 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8345 	 * interfaces are omitted.
8346 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8347 	 * unless LIFC_TEMPORARY is specified.
8348 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8349 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8350 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8351 	 * has priority over LIFC_NOXMIT.
8352 	 */
8353 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8354 
8355 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8356 		return (EINVAL);
8357 
8358 	/*
8359 	 * Must be (better be!) continuation of a TRANSPARENT
8360 	 * IOCTL.  We just copied in the lifconf structure.
8361 	 */
8362 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8363 
8364 	family = STRUCT_FGET(lifc, lifc_family);
8365 	flags = STRUCT_FGET(lifc, lifc_flags);
8366 
8367 	switch (family) {
8368 	case AF_UNSPEC:
8369 		/*
8370 		 * walk all ILL's.
8371 		 */
8372 		list = MAX_G_HEADS;
8373 		break;
8374 	case AF_INET:
8375 		/*
8376 		 * walk only IPV4 ILL's.
8377 		 */
8378 		list = IP_V4_G_HEAD;
8379 		break;
8380 	case AF_INET6:
8381 		/*
8382 		 * walk only IPV6 ILL's.
8383 		 */
8384 		list = IP_V6_G_HEAD;
8385 		break;
8386 	default:
8387 		return (EAFNOSUPPORT);
8388 	}
8389 
8390 	/*
8391 	 * Allocate a buffer to hold requested information.
8392 	 *
8393 	 * If lifc_len is larger than what is needed, we only
8394 	 * allocate what we will use.
8395 	 *
8396 	 * If lifc_len is smaller than what is needed, return
8397 	 * EINVAL.
8398 	 */
8399 	numlifs = ip_get_numlifs(family, flags, zoneid);
8400 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8401 	lifclen = STRUCT_FGET(lifc, lifc_len);
8402 	if (lifc_bufsize > lifclen) {
8403 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8404 			return (EINVAL);
8405 		else
8406 			lifc_bufsize = lifclen;
8407 	}
8408 
8409 	mp1 = mi_copyout_alloc(q, mp,
8410 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8411 	if (mp1 == NULL)
8412 		return (ENOMEM);
8413 
8414 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8415 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8416 
8417 	lifr = (struct lifreq *)mp1->b_rptr;
8418 
8419 	rw_enter(&ill_g_lock, RW_READER);
8420 	ill = ill_first(list, list, &ctx);
8421 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8422 		for (ipif = ill->ill_ipif; ipif != NULL;
8423 		    ipif = ipif->ipif_next) {
8424 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8425 			    !(flags & LIFC_NOXMIT))
8426 				continue;
8427 
8428 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8429 			    !(flags & LIFC_TEMPORARY))
8430 				continue;
8431 
8432 			if (((ipif->ipif_flags &
8433 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8434 			    IPIF_DEPRECATED)) ||
8435 			    (ill->ill_phyint->phyint_flags &
8436 			    PHYI_LOOPBACK) ||
8437 			    !(ipif->ipif_flags & IPIF_UP)) &&
8438 			    (flags & LIFC_EXTERNAL_SOURCE))
8439 				continue;
8440 
8441 			if (zoneid != ipif->ipif_zoneid &&
8442 			    ipif->ipif_zoneid != ALL_ZONES &&
8443 			    (zoneid != GLOBAL_ZONEID ||
8444 			    !(flags & LIFC_ALLZONES)))
8445 				continue;
8446 
8447 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8448 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8449 					rw_exit(&ill_g_lock);
8450 					return (EINVAL);
8451 				} else {
8452 					goto lif_copydone;
8453 				}
8454 			}
8455 
8456 			(void) ipif_get_name(ipif,
8457 				lifr->lifr_name,
8458 				sizeof (lifr->lifr_name));
8459 			if (ipif->ipif_isv6) {
8460 				sin6 = (sin6_t *)&lifr->lifr_addr;
8461 				*sin6 = sin6_null;
8462 				sin6->sin6_family = AF_INET6;
8463 				sin6->sin6_addr =
8464 				ipif->ipif_v6lcl_addr;
8465 				lifr->lifr_addrlen =
8466 				ip_mask_to_plen_v6(
8467 				    &ipif->ipif_v6net_mask);
8468 			} else {
8469 				sin = (sin_t *)&lifr->lifr_addr;
8470 				*sin = sin_null;
8471 				sin->sin_family = AF_INET;
8472 				sin->sin_addr.s_addr =
8473 				    ipif->ipif_lcl_addr;
8474 				lifr->lifr_addrlen =
8475 				    ip_mask_to_plen(
8476 				    ipif->ipif_net_mask);
8477 			}
8478 			lifr++;
8479 		}
8480 	}
8481 lif_copydone:
8482 	rw_exit(&ill_g_lock);
8483 
8484 	mp1->b_wptr = (uchar_t *)lifr;
8485 	if (STRUCT_BUF(lifc) != NULL) {
8486 		STRUCT_FSET(lifc, lifc_len,
8487 			(int)((uchar_t *)lifr - mp1->b_rptr));
8488 	}
8489 	return (0);
8490 }
8491 
8492 /* ARGSUSED */
8493 int
8494 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
8495     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8496 {
8497 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8498 	ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
8499 	return (0);
8500 }
8501 
8502 static void
8503 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
8504 {
8505 	ip6_asp_t *table;
8506 	size_t table_size;
8507 	mblk_t *data_mp;
8508 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8509 
8510 	/* These two ioctls are I_STR only */
8511 	if (iocp->ioc_count == TRANSPARENT) {
8512 		miocnak(q, mp, 0, EINVAL);
8513 		return;
8514 	}
8515 
8516 	data_mp = mp->b_cont;
8517 	if (data_mp == NULL) {
8518 		/* The user passed us a NULL argument */
8519 		table = NULL;
8520 		table_size = iocp->ioc_count;
8521 	} else {
8522 		/*
8523 		 * The user provided a table.  The stream head
8524 		 * may have copied in the user data in chunks,
8525 		 * so make sure everything is pulled up
8526 		 * properly.
8527 		 */
8528 		if (MBLKL(data_mp) < iocp->ioc_count) {
8529 			mblk_t *new_data_mp;
8530 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
8531 			    NULL) {
8532 				miocnak(q, mp, 0, ENOMEM);
8533 				return;
8534 			}
8535 			freemsg(data_mp);
8536 			data_mp = new_data_mp;
8537 			mp->b_cont = data_mp;
8538 		}
8539 		table = (ip6_asp_t *)data_mp->b_rptr;
8540 		table_size = iocp->ioc_count;
8541 	}
8542 
8543 	switch (iocp->ioc_cmd) {
8544 	case SIOCGIP6ADDRPOLICY:
8545 		iocp->ioc_rval = ip6_asp_get(table, table_size);
8546 		if (iocp->ioc_rval == -1)
8547 			iocp->ioc_error = EINVAL;
8548 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8549 		else if (table != NULL &&
8550 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
8551 			ip6_asp_t *src = table;
8552 			ip6_asp32_t *dst = (void *)table;
8553 			int count = table_size / sizeof (ip6_asp_t);
8554 			int i;
8555 
8556 			/*
8557 			 * We need to do an in-place shrink of the array
8558 			 * to match the alignment attributes of the
8559 			 * 32-bit ABI looking at it.
8560 			 */
8561 			/* LINTED: logical expression always true: op "||" */
8562 			ASSERT(sizeof (*src) > sizeof (*dst));
8563 			for (i = 1; i < count; i++)
8564 				bcopy(src + i, dst + i, sizeof (*dst));
8565 		}
8566 #endif
8567 		break;
8568 
8569 	case SIOCSIP6ADDRPOLICY:
8570 		ASSERT(mp->b_prev == NULL);
8571 		mp->b_prev = (void *)q;
8572 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8573 		/*
8574 		 * We pass in the datamodel here so that the ip6_asp_replace()
8575 		 * routine can handle converting from 32-bit to native formats
8576 		 * where necessary.
8577 		 *
8578 		 * A better way to handle this might be to convert the inbound
8579 		 * data structure here, and hang it off a new 'mp'; thus the
8580 		 * ip6_asp_replace() logic would always be dealing with native
8581 		 * format data structures..
8582 		 *
8583 		 * (An even simpler way to handle these ioctls is to just
8584 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
8585 		 * and just recompile everything that depends on it.)
8586 		 */
8587 #endif
8588 		ip6_asp_replace(mp, table, table_size, B_FALSE,
8589 		    iocp->ioc_flag & IOC_MODELS);
8590 		return;
8591 	}
8592 
8593 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
8594 	qreply(q, mp);
8595 }
8596 
8597 static void
8598 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
8599 {
8600 	mblk_t 		*data_mp;
8601 	struct dstinforeq	*dir;
8602 	uint8_t		*end, *cur;
8603 	in6_addr_t	*daddr, *saddr;
8604 	ipaddr_t	v4daddr;
8605 	ire_t		*ire;
8606 	char		*slabel, *dlabel;
8607 	boolean_t	isipv4;
8608 	int		match_ire;
8609 	ill_t		*dst_ill;
8610 	ipif_t		*src_ipif, *ire_ipif;
8611 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8612 	zoneid_t	zoneid;
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 	/*
8618 	 * This ioctl is I_STR only, and must have a
8619 	 * data mblk following the M_IOCTL mblk.
8620 	 */
8621 	data_mp = mp->b_cont;
8622 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
8623 		miocnak(q, mp, 0, EINVAL);
8624 		return;
8625 	}
8626 
8627 	if (MBLKL(data_mp) < iocp->ioc_count) {
8628 		mblk_t *new_data_mp;
8629 
8630 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
8631 			miocnak(q, mp, 0, ENOMEM);
8632 			return;
8633 		}
8634 		freemsg(data_mp);
8635 		data_mp = new_data_mp;
8636 		mp->b_cont = data_mp;
8637 	}
8638 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
8639 
8640 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
8641 	    end - cur >= sizeof (struct dstinforeq);
8642 	    cur += sizeof (struct dstinforeq)) {
8643 		dir = (struct dstinforeq *)cur;
8644 		daddr = &dir->dir_daddr;
8645 		saddr = &dir->dir_saddr;
8646 
8647 		/*
8648 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
8649 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
8650 		 * and ipif_select_source[_v6]() do not.
8651 		 */
8652 		dir->dir_dscope = ip_addr_scope_v6(daddr);
8653 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence);
8654 
8655 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
8656 		if (isipv4) {
8657 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
8658 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
8659 			    0, NULL, NULL, zoneid, 0, NULL, match_ire);
8660 		} else {
8661 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
8662 			    0, NULL, NULL, zoneid, 0, NULL, match_ire);
8663 		}
8664 		if (ire == NULL) {
8665 			dir->dir_dreachable = 0;
8666 
8667 			/* move on to next dst addr */
8668 			continue;
8669 		}
8670 		dir->dir_dreachable = 1;
8671 
8672 		ire_ipif = ire->ire_ipif;
8673 		if (ire_ipif == NULL)
8674 			goto next_dst;
8675 
8676 		/*
8677 		 * We expect to get back an interface ire or a
8678 		 * gateway ire cache entry.  For both types, the
8679 		 * output interface is ire_ipif->ipif_ill.
8680 		 */
8681 		dst_ill = ire_ipif->ipif_ill;
8682 		dir->dir_dmactype = dst_ill->ill_mactype;
8683 
8684 		if (isipv4) {
8685 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
8686 		} else {
8687 			src_ipif = ipif_select_source_v6(dst_ill,
8688 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
8689 			    zoneid);
8690 		}
8691 		if (src_ipif == NULL)
8692 			goto next_dst;
8693 
8694 		*saddr = src_ipif->ipif_v6lcl_addr;
8695 		dir->dir_sscope = ip_addr_scope_v6(saddr);
8696 		slabel = ip6_asp_lookup(saddr, NULL);
8697 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
8698 		dir->dir_sdeprecated =
8699 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
8700 		ipif_refrele(src_ipif);
8701 next_dst:
8702 		ire_refrele(ire);
8703 	}
8704 	miocack(q, mp, iocp->ioc_count, 0);
8705 }
8706 
8707 
8708 /*
8709  * Check if this is an address assigned to this machine.
8710  * Skips interfaces that are down by using ire checks.
8711  * Translates mapped addresses to v4 addresses and then
8712  * treats them as such, returning true if the v4 address
8713  * associated with this mapped address is configured.
8714  * Note: Applications will have to be careful what they do
8715  * with the response; use of mapped addresses limits
8716  * what can be done with the socket, especially with
8717  * respect to socket options and ioctls - neither IPv4
8718  * options nor IPv6 sticky options/ancillary data options
8719  * may be used.
8720  */
8721 /* ARGSUSED */
8722 int
8723 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8724     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8725 {
8726 	struct sioc_addrreq *sia;
8727 	sin_t *sin;
8728 	ire_t *ire;
8729 	mblk_t *mp1;
8730 	zoneid_t zoneid;
8731 
8732 	ip1dbg(("ip_sioctl_tmyaddr"));
8733 
8734 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8735 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8736 
8737 	/* Existence verified in ip_wput_nondata */
8738 	mp1 = mp->b_cont->b_cont;
8739 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8740 	sin = (sin_t *)&sia->sa_addr;
8741 	switch (sin->sin_family) {
8742 	case AF_INET6: {
8743 		sin6_t *sin6 = (sin6_t *)sin;
8744 
8745 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8746 			ipaddr_t v4_addr;
8747 
8748 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8749 			    v4_addr);
8750 			ire = ire_ctable_lookup(v4_addr, 0,
8751 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8752 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8753 		} else {
8754 			in6_addr_t v6addr;
8755 
8756 			v6addr = sin6->sin6_addr;
8757 			ire = ire_ctable_lookup_v6(&v6addr, 0,
8758 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8759 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8760 		}
8761 		break;
8762 	}
8763 	case AF_INET: {
8764 		ipaddr_t v4addr;
8765 
8766 		v4addr = sin->sin_addr.s_addr;
8767 		ire = ire_ctable_lookup(v4addr, 0,
8768 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8769 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8770 		break;
8771 	}
8772 	default:
8773 		return (EAFNOSUPPORT);
8774 	}
8775 	if (ire != NULL) {
8776 		sia->sa_res = 1;
8777 		ire_refrele(ire);
8778 	} else {
8779 		sia->sa_res = 0;
8780 	}
8781 	return (0);
8782 }
8783 
8784 /*
8785  * Check if this is an address assigned on-link i.e. neighbor,
8786  * and makes sure it's reachable from the current zone.
8787  * Returns true for my addresses as well.
8788  * Translates mapped addresses to v4 addresses and then
8789  * treats them as such, returning true if the v4 address
8790  * associated with this mapped address is configured.
8791  * Note: Applications will have to be careful what they do
8792  * with the response; use of mapped addresses limits
8793  * what can be done with the socket, especially with
8794  * respect to socket options and ioctls - neither IPv4
8795  * options nor IPv6 sticky options/ancillary data options
8796  * may be used.
8797  */
8798 /* ARGSUSED */
8799 int
8800 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8801     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8802 {
8803 	struct sioc_addrreq *sia;
8804 	sin_t *sin;
8805 	mblk_t	*mp1;
8806 	ire_t *ire = NULL;
8807 	zoneid_t zoneid;
8808 
8809 	ip1dbg(("ip_sioctl_tonlink"));
8810 
8811 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8812 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8813 
8814 	/* Existence verified in ip_wput_nondata */
8815 	mp1 = mp->b_cont->b_cont;
8816 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8817 	sin = (sin_t *)&sia->sa_addr;
8818 
8819 	/*
8820 	 * Match addresses with a zero gateway field to avoid
8821 	 * routes going through a router.
8822 	 * Exclude broadcast and multicast addresses.
8823 	 */
8824 	switch (sin->sin_family) {
8825 	case AF_INET6: {
8826 		sin6_t *sin6 = (sin6_t *)sin;
8827 
8828 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8829 			ipaddr_t v4_addr;
8830 
8831 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8832 			    v4_addr);
8833 			if (!CLASSD(v4_addr)) {
8834 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
8835 				    NULL, NULL, zoneid, NULL,
8836 				    MATCH_IRE_GW);
8837 			}
8838 		} else {
8839 			in6_addr_t v6addr;
8840 			in6_addr_t v6gw;
8841 
8842 			v6addr = sin6->sin6_addr;
8843 			v6gw = ipv6_all_zeros;
8844 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8845 				ire = ire_route_lookup_v6(&v6addr, 0,
8846 				    &v6gw, 0, NULL, NULL, zoneid,
8847 				    NULL, MATCH_IRE_GW);
8848 			}
8849 		}
8850 		break;
8851 	}
8852 	case AF_INET: {
8853 		ipaddr_t v4addr;
8854 
8855 		v4addr = sin->sin_addr.s_addr;
8856 		if (!CLASSD(v4addr)) {
8857 			ire = ire_route_lookup(v4addr, 0, 0, 0,
8858 			    NULL, NULL, zoneid, NULL,
8859 			    MATCH_IRE_GW);
8860 		}
8861 		break;
8862 	}
8863 	default:
8864 		return (EAFNOSUPPORT);
8865 	}
8866 	sia->sa_res = 0;
8867 	if (ire != NULL) {
8868 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
8869 		    IRE_LOCAL|IRE_LOOPBACK)) {
8870 			sia->sa_res = 1;
8871 		}
8872 		ire_refrele(ire);
8873 	}
8874 	return (0);
8875 }
8876 
8877 /*
8878  * TBD: implement when kernel maintaines a list of site prefixes.
8879  */
8880 /* ARGSUSED */
8881 int
8882 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8883     ip_ioctl_cmd_t *ipip, void *ifreq)
8884 {
8885 	return (ENXIO);
8886 }
8887 
8888 /* ARGSUSED */
8889 int
8890 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8891     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8892 {
8893 	ill_t  		*ill;
8894 	mblk_t		*mp1;
8895 	conn_t		*connp;
8896 	boolean_t	success;
8897 
8898 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
8899 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
8900 	/* ioctl comes down on an conn */
8901 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8902 	connp = Q_TO_CONN(q);
8903 
8904 	mp->b_datap->db_type = M_IOCTL;
8905 
8906 	/*
8907 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
8908 	 * The original mp contains contaminated b_next values due to 'mi',
8909 	 * which is needed to do the mi_copy_done. Unfortunately if we
8910 	 * send down the original mblk itself and if we are popped due to an
8911 	 * an unplumb before the response comes back from tunnel,
8912 	 * the streamhead (which does a freemsg) will see this contaminated
8913 	 * message and the assertion in freemsg about non-null b_next/b_prev
8914 	 * will panic a DEBUG kernel.
8915 	 */
8916 	mp1 = copymsg(mp);
8917 	if (mp1 == NULL)
8918 		return (ENOMEM);
8919 
8920 	ill = ipif->ipif_ill;
8921 	mutex_enter(&connp->conn_lock);
8922 	mutex_enter(&ill->ill_lock);
8923 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
8924 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
8925 		    mp, 0);
8926 	} else {
8927 		success = ill_pending_mp_add(ill, connp, mp);
8928 	}
8929 	mutex_exit(&ill->ill_lock);
8930 	mutex_exit(&connp->conn_lock);
8931 
8932 	if (success) {
8933 		ip1dbg(("sending down tunparam request "));
8934 		putnext(ill->ill_wq, mp1);
8935 		return (EINPROGRESS);
8936 	} else {
8937 		/* The conn has started closing */
8938 		freemsg(mp1);
8939 		return (EINTR);
8940 	}
8941 }
8942 
8943 static int
8944 ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp, sin_t *sin,
8945     boolean_t x_arp_ioctl, boolean_t if_arp_ioctl)
8946 {
8947 	mblk_t *mp1;
8948 	mblk_t *mp2;
8949 	mblk_t *pending_mp;
8950 	ipaddr_t ipaddr;
8951 	area_t *area;
8952 	struct iocblk *iocp;
8953 	conn_t *connp;
8954 	struct arpreq *ar;
8955 	struct xarpreq *xar;
8956 	boolean_t success;
8957 	int flags, alength;
8958 	char *lladdr;
8959 
8960 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8961 	connp = Q_TO_CONN(q);
8962 
8963 	iocp = (struct iocblk *)mp->b_rptr;
8964 	/*
8965 	 * ill has already been set depending on whether
8966 	 * bsd style or interface style ioctl.
8967 	 */
8968 	ASSERT(ill != NULL);
8969 
8970 	/*
8971 	 * Is this one of the new SIOC*XARP ioctls?
8972 	 */
8973 	if (x_arp_ioctl) {
8974 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8975 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8976 		ar = NULL;
8977 
8978 		flags = xar->xarp_flags;
8979 		lladdr = LLADDR(&xar->xarp_ha);
8980 		/*
8981 		 * Validate against user's link layer address length
8982 		 * input and name and addr length limits.
8983 		 */
8984 		alength = ill->ill_phys_addr_length;
8985 		if (iocp->ioc_cmd == SIOCSXARP) {
8986 			if (alength != xar->xarp_ha.sdl_alen ||
8987 			    (alength + xar->xarp_ha.sdl_nlen >
8988 			    sizeof (xar->xarp_ha.sdl_data)))
8989 				return (EINVAL);
8990 		}
8991 	} else {
8992 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8993 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8994 		xar = NULL;
8995 
8996 		flags = ar->arp_flags;
8997 		lladdr = ar->arp_ha.sa_data;
8998 		/*
8999 		 * Theoretically, the sa_family could tell us what link
9000 		 * layer type this operation is trying to deal with. By
9001 		 * common usage AF_UNSPEC means ethernet. We'll assume
9002 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9003 		 * for now. Our new SIOC*XARP ioctls can be used more
9004 		 * generally.
9005 		 *
9006 		 * If the underlying media happens to have a non 6 byte
9007 		 * address, arp module will fail set/get, but the del
9008 		 * operation will succeed.
9009 		 */
9010 		alength = 6;
9011 		if ((iocp->ioc_cmd != SIOCDARP) &&
9012 		    (alength != ill->ill_phys_addr_length)) {
9013 			return (EINVAL);
9014 		}
9015 	}
9016 
9017 	/*
9018 	 * We are going to pass up to ARP a packet chain that looks
9019 	 * like:
9020 	 *
9021 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9022 	 *
9023 	 * Get a copy of the original IOCTL mblk to head the chain,
9024 	 * to be sent up (in mp1). Also get another copy to store
9025 	 * in the ill_pending_mp list, for matching the response
9026 	 * when it comes back from ARP.
9027 	 */
9028 	mp1 = copyb(mp);
9029 	pending_mp = copymsg(mp);
9030 	if (mp1 == NULL || pending_mp == NULL) {
9031 		if (mp1 != NULL)
9032 			freeb(mp1);
9033 		if (pending_mp != NULL)
9034 			inet_freemsg(pending_mp);
9035 		return (ENOMEM);
9036 	}
9037 
9038 	ipaddr = sin->sin_addr.s_addr;
9039 
9040 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9041 	    (caddr_t)&ipaddr);
9042 	if (mp2 == NULL) {
9043 		freeb(mp1);
9044 		inet_freemsg(pending_mp);
9045 		return (ENOMEM);
9046 	}
9047 	/* Put together the chain. */
9048 	mp1->b_cont = mp2;
9049 	mp1->b_datap->db_type = M_IOCTL;
9050 	mp2->b_cont = mp;
9051 	mp2->b_datap->db_type = M_DATA;
9052 
9053 	iocp = (struct iocblk *)mp1->b_rptr;
9054 
9055 	/*
9056 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9057 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9058 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9059 	 * ioc_count field; set ioc_count to be correct.
9060 	 */
9061 	iocp->ioc_count = MBLKL(mp1->b_cont);
9062 
9063 	/*
9064 	 * Set the proper command in the ARP message.
9065 	 * Convert the SIOC{G|S|D}ARP calls into our
9066 	 * AR_ENTRY_xxx calls.
9067 	 */
9068 	area = (area_t *)mp2->b_rptr;
9069 	switch (iocp->ioc_cmd) {
9070 	case SIOCDARP:
9071 	case SIOCDXARP:
9072 		/*
9073 		 * We defer deleting the corresponding IRE until
9074 		 * we return from arp.
9075 		 */
9076 		area->area_cmd = AR_ENTRY_DELETE;
9077 		area->area_proto_mask_offset = 0;
9078 		break;
9079 	case SIOCGARP:
9080 	case SIOCGXARP:
9081 		area->area_cmd = AR_ENTRY_SQUERY;
9082 		area->area_proto_mask_offset = 0;
9083 		break;
9084 	case SIOCSARP:
9085 	case SIOCSXARP: {
9086 		/*
9087 		 * Delete the corresponding ire to make sure IP will
9088 		 * pick up any change from arp.
9089 		 */
9090 		if (!if_arp_ioctl) {
9091 			(void) ip_ire_clookup_and_delete(ipaddr, NULL);
9092 			break;
9093 		} else {
9094 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9095 			if (ipif != NULL) {
9096 				(void) ip_ire_clookup_and_delete(ipaddr, ipif);
9097 				ipif_refrele(ipif);
9098 			}
9099 			break;
9100 		}
9101 	}
9102 	}
9103 	iocp->ioc_cmd = area->area_cmd;
9104 
9105 	/*
9106 	 * Before sending 'mp' to ARP, we have to clear the b_next
9107 	 * and b_prev. Otherwise if STREAMS encounters such a message
9108 	 * in freemsg(), (because ARP can close any time) it can cause
9109 	 * a panic. But mi code needs the b_next and b_prev values of
9110 	 * mp->b_cont, to complete the ioctl. So we store it here
9111 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9112 	 * when the response comes down from ARP.
9113 	 */
9114 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9115 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9116 	mp->b_cont->b_next = NULL;
9117 	mp->b_cont->b_prev = NULL;
9118 
9119 	mutex_enter(&connp->conn_lock);
9120 	mutex_enter(&ill->ill_lock);
9121 	/* conn has not yet started closing, hence this can't fail */
9122 	success = ill_pending_mp_add(ill, connp, pending_mp);
9123 	ASSERT(success);
9124 	mutex_exit(&ill->ill_lock);
9125 	mutex_exit(&connp->conn_lock);
9126 
9127 	/*
9128 	 * Fill in the rest of the ARP operation fields.
9129 	 */
9130 	area->area_hw_addr_length = alength;
9131 	bcopy(lladdr,
9132 	    (char *)area + area->area_hw_addr_offset,
9133 	    area->area_hw_addr_length);
9134 	/* Translate the flags. */
9135 	if (flags & ATF_PERM)
9136 		area->area_flags |= ACE_F_PERMANENT;
9137 	if (flags & ATF_PUBL)
9138 		area->area_flags |= ACE_F_PUBLISH;
9139 
9140 	/*
9141 	 * Up to ARP it goes.  The response will come
9142 	 * back in ip_wput as an M_IOCACK message, and
9143 	 * will be handed to ip_sioctl_iocack for
9144 	 * completion.
9145 	 */
9146 	putnext(ill->ill_rq, mp1);
9147 	return (EINPROGRESS);
9148 }
9149 
9150 /* ARGSUSED */
9151 int
9152 ip_sioctl_xarp(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9153     ip_ioctl_cmd_t *ipip, void *ifreq)
9154 {
9155 	struct xarpreq *xar;
9156 	boolean_t isv6;
9157 	mblk_t	*mp1;
9158 	int	err;
9159 	conn_t	*connp;
9160 	int ifnamelen;
9161 	ire_t	*ire = NULL;
9162 	ill_t	*ill = NULL;
9163 	struct sockaddr_in *sin;
9164 	boolean_t if_arp_ioctl = B_FALSE;
9165 
9166 	/* ioctl comes down on an conn */
9167 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9168 	connp = Q_TO_CONN(q);
9169 	isv6 = connp->conn_af_isv6;
9170 
9171 	/* Existance verified in ip_wput_nondata */
9172 	mp1 = mp->b_cont->b_cont;
9173 
9174 	ASSERT(MBLKL(mp1) >= sizeof (*xar));
9175 	xar = (struct xarpreq *)mp1->b_rptr;
9176 	sin = (sin_t *)&xar->xarp_pa;
9177 
9178 	if (isv6 || (xar->xarp_ha.sdl_family != AF_LINK) ||
9179 	    (xar->xarp_pa.ss_family != AF_INET))
9180 		return (ENXIO);
9181 
9182 	ifnamelen = xar->xarp_ha.sdl_nlen;
9183 	if (ifnamelen != 0) {
9184 		char	*cptr, cval;
9185 
9186 		if (ifnamelen >= LIFNAMSIZ)
9187 			return (EINVAL);
9188 
9189 		/*
9190 		 * Instead of bcopying a bunch of bytes,
9191 		 * null-terminate the string in-situ.
9192 		 */
9193 		cptr = xar->xarp_ha.sdl_data + ifnamelen;
9194 		cval = *cptr;
9195 		*cptr = '\0';
9196 		ill = ill_lookup_on_name(xar->xarp_ha.sdl_data,
9197 		    B_FALSE, isv6, CONNP_TO_WQ(connp), mp, ip_process_ioctl,
9198 		    &err, NULL);
9199 		*cptr = cval;
9200 		if (ill == NULL)
9201 			return (err);
9202 		if (ill->ill_net_type != IRE_IF_RESOLVER) {
9203 			ill_refrele(ill);
9204 			return (ENXIO);
9205 		}
9206 
9207 		if_arp_ioctl = B_TRUE;
9208 	} else {
9209 		/*
9210 		 * PSARC 2003/088 states that if sdl_nlen == 0, it behaves
9211 		 * as an extended BSD ioctl. The kernel uses the IP address
9212 		 * to figure out the network interface.
9213 		 */
9214 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL);
9215 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9216 		    ((ill = ire_to_ill(ire)) == NULL) ||
9217 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9218 			if (ire != NULL)
9219 				ire_refrele(ire);
9220 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9221 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9222 			    NULL, MATCH_IRE_TYPE);
9223 			if ((ire == NULL) ||
9224 			    ((ill = ire_to_ill(ire)) == NULL)) {
9225 				if (ire != NULL)
9226 					ire_refrele(ire);
9227 				return (ENXIO);
9228 			}
9229 		}
9230 		ASSERT(ire != NULL && ill != NULL);
9231 	}
9232 
9233 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_TRUE, if_arp_ioctl);
9234 	if (if_arp_ioctl)
9235 		ill_refrele(ill);
9236 	if (ire != NULL)
9237 		ire_refrele(ire);
9238 
9239 	return (err);
9240 }
9241 
9242 /*
9243  * ARP IOCTLs.
9244  * How does IP get in the business of fronting ARP configuration/queries?
9245  * Well its like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9246  * are by tradition passed in through a datagram socket.  That lands in IP.
9247  * As it happens, this is just as well since the interface is quite crude in
9248  * that it passes in no information about protocol or hardware types, or
9249  * interface association.  After making the protocol assumption, IP is in
9250  * the position to look up the name of the ILL, which ARP will need, and
9251  * format a request that can be handled by ARP.	 The request is passed up
9252  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9253  * back a response.  ARP supports its own set of more general IOCTLs, in
9254  * case anyone is interested.
9255  */
9256 /* ARGSUSED */
9257 int
9258 ip_sioctl_arp(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9259     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9260 {
9261 	struct arpreq *ar;
9262 	struct sockaddr_in *sin;
9263 	ire_t	*ire;
9264 	boolean_t isv6;
9265 	mblk_t	*mp1;
9266 	int	err;
9267 	conn_t	*connp;
9268 	ill_t	*ill;
9269 
9270 	/* ioctl comes down on an conn */
9271 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9272 	connp = Q_TO_CONN(q);
9273 	isv6 = connp->conn_af_isv6;
9274 	if (isv6)
9275 		return (ENXIO);
9276 
9277 	/* Existance verified in ip_wput_nondata */
9278 	mp1 = mp->b_cont->b_cont;
9279 
9280 	ar = (struct arpreq *)mp1->b_rptr;
9281 	sin = (sin_t *)&ar->arp_pa;
9282 
9283 	/*
9284 	 * We need to let ARP know on which interface the IP
9285 	 * address has an ARP mapping. In the IPMP case, a
9286 	 * simple forwarding table lookup will return the
9287 	 * IRE_IF_RESOLVER for the first interface in the group,
9288 	 * which might not be the interface on which the
9289 	 * requested IP address was resolved due to the ill
9290 	 * selection algorithm (see ip_newroute_get_dst_ill()).
9291 	 * So we do a cache table lookup first: if the IRE cache
9292 	 * entry for the IP address is still there, it will
9293 	 * contain the ill pointer for the right interface, so
9294 	 * we use that. If the cache entry has been flushed, we
9295 	 * fall back to the forwarding table lookup. This should
9296 	 * be rare enough since IRE cache entries have a longer
9297 	 * life expectancy than ARP cache entries.
9298 	 */
9299 	ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL);
9300 	if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9301 	    ((ill = ire_to_ill(ire)) == NULL)) {
9302 		if (ire != NULL)
9303 			ire_refrele(ire);
9304 		ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9305 		    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9306 		    NULL, MATCH_IRE_TYPE);
9307 		if ((ire == NULL) || ((ill = ire_to_ill(ire)) == NULL)) {
9308 			if (ire != NULL)
9309 				ire_refrele(ire);
9310 			return (ENXIO);
9311 		}
9312 	}
9313 	ASSERT(ire != NULL && ill != NULL);
9314 
9315 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_FALSE, B_FALSE);
9316 	ire_refrele(ire);
9317 	return (err);
9318 }
9319 
9320 /*
9321  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9322  * atomically set/clear the muxids. Also complete the ioctl by acking or
9323  * naking it.  Note that the code is structured such that the link type,
9324  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9325  * its clones use the persistent link, while pppd(1M) and perhaps many
9326  * other daemons may use non-persistent link.  When combined with some
9327  * ill_t states, linking and unlinking lower streams may be used as
9328  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9329  */
9330 /* ARGSUSED */
9331 void
9332 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9333 {
9334 	mblk_t *mp1;
9335 	mblk_t *mp2;
9336 	struct linkblk *li;
9337 	queue_t	*ipwq;
9338 	char	*name;
9339 	struct qinit *qinfo;
9340 	struct ipmx_s *ipmxp;
9341 	ill_t	*ill = NULL;
9342 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9343 	int	err = 0;
9344 	boolean_t	entered_ipsq = B_FALSE;
9345 	boolean_t islink;
9346 	queue_t *dwq = NULL;
9347 
9348 	ASSERT(iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_PUNLINK ||
9349 	    iocp->ioc_cmd == I_LINK || iocp->ioc_cmd == I_UNLINK);
9350 
9351 	islink = (iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_LINK) ?
9352 	    B_TRUE : B_FALSE;
9353 
9354 	mp1 = mp->b_cont;	/* This is the linkblk info */
9355 	li = (struct linkblk *)mp1->b_rptr;
9356 
9357 	/*
9358 	 * ARP has added this special mblk, and the utility is asking us
9359 	 * to perform consistency checks, and also atomically set the
9360 	 * muxid. Ifconfig is an example.  It achieves this by using
9361 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9362 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9363 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9364 	 * and other comments in this routine for more details.
9365 	 */
9366 	mp2 = mp1->b_cont;	/* This is added by ARP */
9367 
9368 	/*
9369 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9370 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9371 	 * get the special mblk above.  For backward compatibility, we just
9372 	 * return success.  The utility will use SIOCSLIFMUXID to store
9373 	 * the muxids.  This is not atomic, and can leave the streams
9374 	 * unplumbable if the utility is interrrupted, before it does the
9375 	 * SIOCSLIFMUXID.
9376 	 */
9377 	if (mp2 == NULL) {
9378 		/*
9379 		 * At this point we don't know whether or not this is the
9380 		 * IP module stream or the ARP device stream.  We need to
9381 		 * walk the lower stream in order to find this out, since
9382 		 * the capability negotiation is done only on the IP module
9383 		 * stream.  IP module instance is identified by the module
9384 		 * name IP, non-null q_next, and it's wput not being ip_lwput.
9385 		 * STREAMS ensures that the lower stream (l_qbot) will not
9386 		 * vanish until this ioctl completes. So we can safely walk
9387 		 * the stream or refer to the q_ptr.
9388 		 */
9389 		ipwq = li->l_qbot;
9390 		while (ipwq != NULL) {
9391 			qinfo = ipwq->q_qinfo;
9392 			name = qinfo->qi_minfo->mi_idname;
9393 			if (name != NULL && name[0] != NULL &&
9394 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
9395 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
9396 			    (ipwq->q_next != NULL)) {
9397 				break;
9398 			}
9399 			ipwq = ipwq->q_next;
9400 		}
9401 		/*
9402 		 * This looks like an IP module stream, so trigger
9403 		 * the capability reset or re-negotiation if necessary.
9404 		 */
9405 		if (ipwq != NULL) {
9406 			ill = ipwq->q_ptr;
9407 			ASSERT(ill != NULL);
9408 
9409 			if (ipsq == NULL) {
9410 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
9411 				    ip_sioctl_plink, NEW_OP, B_TRUE);
9412 				if (ipsq == NULL)
9413 					return;
9414 				entered_ipsq = B_TRUE;
9415 			}
9416 			ASSERT(IAM_WRITER_ILL(ill));
9417 			/*
9418 			 * Store the upper read queue of the module
9419 			 * immediately below IP, and count the total
9420 			 * number of lower modules.  Do this only
9421 			 * for I_PLINK or I_LINK event.
9422 			 */
9423 			ill->ill_lmod_rq = NULL;
9424 			ill->ill_lmod_cnt = 0;
9425 			if (islink && (dwq = ipwq->q_next) != NULL) {
9426 				ill->ill_lmod_rq = RD(dwq);
9427 
9428 				while (dwq != NULL) {
9429 					ill->ill_lmod_cnt++;
9430 					dwq = dwq->q_next;
9431 				}
9432 			}
9433 			/*
9434 			 * There's no point in resetting or re-negotiating if
9435 			 * we are not bound to the driver, so only do this if
9436 			 * the DLPI state is idle (up); we assume such state
9437 			 * since ill_ipif_up_count gets incremented in
9438 			 * ipif_up_done(), which is after we are bound to the
9439 			 * driver.  Note that in the case of logical
9440 			 * interfaces, IP won't rebind to the driver unless
9441 			 * the ill_ipif_up_count is 0, meaning that all other
9442 			 * IP interfaces (including the main ipif) are in the
9443 			 * down state.  Because of this, we use such counter
9444 			 * as an indicator, instead of relying on the IPIF_UP
9445 			 * flag, which is per ipif instance.
9446 			 */
9447 			if (ill->ill_ipif_up_count > 0) {
9448 				if (islink)
9449 					ill_capability_probe(ill);
9450 				else
9451 					ill_capability_reset(ill);
9452 			}
9453 		}
9454 		goto done;
9455 	}
9456 
9457 	/*
9458 	 * This is an I_{P}LINK sent down by ifconfig on
9459 	 * /dev/arp. ARP has appended this last (3rd) mblk,
9460 	 * giving more info. STREAMS ensures that the lower
9461 	 * stream (l_qbot) will not vanish until this ioctl
9462 	 * completes. So we can safely walk the stream or refer
9463 	 * to the q_ptr.
9464 	 */
9465 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9466 	if (ipmxp->ipmx_arpdev_stream) {
9467 		/*
9468 		 * The operation is occuring on the arp-device
9469 		 * stream.
9470 		 */
9471 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9472 		    q, mp, ip_sioctl_plink, &err, NULL);
9473 		if (ill == NULL) {
9474 			if (err == EINPROGRESS) {
9475 				return;
9476 			} else {
9477 				err = EINVAL;
9478 				goto done;
9479 			}
9480 		}
9481 
9482 		if (ipsq == NULL) {
9483 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9484 			    NEW_OP, B_TRUE);
9485 			if (ipsq == NULL) {
9486 				ill_refrele(ill);
9487 				return;
9488 			}
9489 			entered_ipsq = B_TRUE;
9490 		}
9491 		ASSERT(IAM_WRITER_ILL(ill));
9492 		ill_refrele(ill);
9493 		/*
9494 		 * To ensure consistency between IP and ARP,
9495 		 * the following LIFO scheme is used in
9496 		 * plink/punlink. (IP first, ARP last).
9497 		 * This is because the muxid's are stored
9498 		 * in the IP stream on the ill.
9499 		 *
9500 		 * I_{P}LINK: ifconfig plinks the IP stream before
9501 		 * plinking the ARP stream. On an arp-dev
9502 		 * stream, IP checks that it is not yet
9503 		 * plinked, and it also checks that the
9504 		 * corresponding IP stream is already plinked.
9505 		 *
9506 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream
9507 		 * before punlinking the IP stream. IP does
9508 		 * not allow punlink of the IP stream unless
9509 		 * the arp stream has been punlinked.
9510 		 *
9511 		 */
9512 		if ((islink &&
9513 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9514 		    (!islink &&
9515 		    ill->ill_arp_muxid != li->l_index)) {
9516 			err = EINVAL;
9517 			goto done;
9518 		}
9519 		if (islink) {
9520 			ill->ill_arp_muxid = li->l_index;
9521 		} else {
9522 			ill->ill_arp_muxid = 0;
9523 		}
9524 	} else {
9525 		/*
9526 		 * This must be the IP module stream with or
9527 		 * without arp. Walk the stream and locate the
9528 		 * IP module. An IP module instance is
9529 		 * identified by the module name IP, non-null
9530 		 * q_next, and it's wput not being ip_lwput.
9531 		 */
9532 		ipwq = li->l_qbot;
9533 		while (ipwq != NULL) {
9534 			qinfo = ipwq->q_qinfo;
9535 			name = qinfo->qi_minfo->mi_idname;
9536 			if (name != NULL && name[0] != NULL &&
9537 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
9538 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
9539 			    (ipwq->q_next != NULL)) {
9540 				break;
9541 			}
9542 			ipwq = ipwq->q_next;
9543 		}
9544 		if (ipwq != NULL) {
9545 			ill = ipwq->q_ptr;
9546 			ASSERT(ill != NULL);
9547 
9548 			if (ipsq == NULL) {
9549 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
9550 				    ip_sioctl_plink, NEW_OP, B_TRUE);
9551 				if (ipsq == NULL)
9552 					return;
9553 				entered_ipsq = B_TRUE;
9554 			}
9555 			ASSERT(IAM_WRITER_ILL(ill));
9556 			/*
9557 			 * Return error if the ip_mux_id is
9558 			 * non-zero and command is I_{P}LINK.
9559 			 * If command is I_{P}UNLINK, return
9560 			 * error if the arp-devstr is not
9561 			 * yet punlinked.
9562 			 */
9563 			if ((islink && ill->ill_ip_muxid != 0) ||
9564 			    (!islink && ill->ill_arp_muxid != 0)) {
9565 				err = EINVAL;
9566 				goto done;
9567 			}
9568 			ill->ill_lmod_rq = NULL;
9569 			ill->ill_lmod_cnt = 0;
9570 			if (islink) {
9571 				/*
9572 				 * Store the upper read queue of the module
9573 				 * immediately below IP, and count the total
9574 				 * number of lower modules.
9575 				 */
9576 				if ((dwq = ipwq->q_next) != NULL) {
9577 					ill->ill_lmod_rq = RD(dwq);
9578 
9579 					while (dwq != NULL) {
9580 						ill->ill_lmod_cnt++;
9581 						dwq = dwq->q_next;
9582 					}
9583 				}
9584 				ill->ill_ip_muxid = li->l_index;
9585 			} else {
9586 				ill->ill_ip_muxid = 0;
9587 			}
9588 
9589 			/*
9590 			 * See comments above about resetting/re-
9591 			 * negotiating driver sub-capabilities.
9592 			 */
9593 			if (ill->ill_ipif_up_count > 0) {
9594 				if (islink)
9595 					ill_capability_probe(ill);
9596 				else
9597 					ill_capability_reset(ill);
9598 			}
9599 		}
9600 	}
9601 done:
9602 	iocp->ioc_count = 0;
9603 	iocp->ioc_error = err;
9604 	if (err == 0)
9605 		mp->b_datap->db_type = M_IOCACK;
9606 	else
9607 		mp->b_datap->db_type = M_IOCNAK;
9608 	qreply(q, mp);
9609 
9610 	/* Conn was refheld in ip_sioctl_copyin_setup */
9611 	if (CONN_Q(q))
9612 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9613 	if (entered_ipsq)
9614 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
9615 }
9616 
9617 /*
9618  * Search the ioctl command in the ioctl tables and return a pointer
9619  * to the ioctl command information. The ioctl command tables are
9620  * static and fully populated at compile time.
9621  */
9622 ip_ioctl_cmd_t *
9623 ip_sioctl_lookup(int ioc_cmd)
9624 {
9625 	int index;
9626 	ip_ioctl_cmd_t *ipip;
9627 	ip_ioctl_cmd_t *ipip_end;
9628 
9629 	if (ioc_cmd == IPI_DONTCARE)
9630 		return (NULL);
9631 
9632 	/*
9633 	 * Do a 2 step search. First search the indexed table
9634 	 * based on the least significant byte of the ioctl cmd.
9635 	 * If we don't find a match, then search the misc table
9636 	 * serially.
9637 	 */
9638 	index = ioc_cmd & 0xFF;
9639 	if (index < ip_ndx_ioctl_count) {
9640 		ipip = &ip_ndx_ioctl_table[index];
9641 		if (ipip->ipi_cmd == ioc_cmd) {
9642 			/* Found a match in the ndx table */
9643 			return (ipip);
9644 		}
9645 	}
9646 
9647 	/* Search the misc table */
9648 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
9649 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
9650 		if (ipip->ipi_cmd == ioc_cmd)
9651 			/* Found a match in the misc table */
9652 			return (ipip);
9653 	}
9654 
9655 	return (NULL);
9656 }
9657 
9658 /*
9659  * Wrapper function for resuming deferred ioctl processing
9660  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
9661  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
9662  */
9663 /* ARGSUSED */
9664 void
9665 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
9666     void *dummy_arg)
9667 {
9668 	ip_sioctl_copyin_setup(q, mp);
9669 }
9670 
9671 /*
9672  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
9673  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
9674  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
9675  * We establish here the size of the block to be copied in.  mi_copyin
9676  * arranges for this to happen, an processing continues in ip_wput with
9677  * an M_IOCDATA message.
9678  */
9679 void
9680 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
9681 {
9682 	int	copyin_size;
9683 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9684 	ip_ioctl_cmd_t *ipip;
9685 	cred_t *cr;
9686 
9687 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
9688 	if (ipip == NULL) {
9689 		/*
9690 		 * The ioctl is not one we understand or own.
9691 		 * Pass it along to be processed down stream,
9692 		 * if this is a module instance of IP, else nak
9693 		 * the ioctl.
9694 		 */
9695 		if (q->q_next == NULL) {
9696 			goto nak;
9697 		} else {
9698 			putnext(q, mp);
9699 			return;
9700 		}
9701 	}
9702 
9703 	/*
9704 	 * If this is deferred, then we will do all the checks when we
9705 	 * come back.
9706 	 */
9707 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
9708 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup()) {
9709 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
9710 		return;
9711 	}
9712 
9713 	/*
9714 	 * Only allow a very small subset of IP ioctls on this stream if
9715 	 * IP is a module and not a driver. Allowing ioctls to be processed
9716 	 * in this case may cause assert failures or data corruption.
9717 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
9718 	 * ioctls allowed on an IP module stream, after which this stream
9719 	 * normally becomes a multiplexor (at which time the stream head
9720 	 * will fail all ioctls).
9721 	 */
9722 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
9723 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
9724 			/*
9725 			 * Pass common Streams ioctls which the IP
9726 			 * module does not own or consume along to
9727 			 * be processed down stream.
9728 			 */
9729 			putnext(q, mp);
9730 			return;
9731 		} else {
9732 			goto nak;
9733 		}
9734 	}
9735 
9736 	/* Make sure we have ioctl data to process. */
9737 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
9738 		goto nak;
9739 
9740 	/*
9741 	 * Prefer dblk credential over ioctl credential; some synthesized
9742 	 * ioctls have kcred set because there's no way to crhold()
9743 	 * a credential in some contexts.  (ioc_cr is not crfree() by
9744 	 * the framework; the caller of ioctl needs to hold the reference
9745 	 * for the duration of the call).
9746 	 */
9747 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
9748 
9749 	/* Make sure normal users don't send down privileged ioctls */
9750 	if ((ipip->ipi_flags & IPI_PRIV) &&
9751 	    (cr != NULL) && secpolicy_net_config(cr, B_TRUE) != 0) {
9752 		/* We checked the privilege earlier but log it here */
9753 		miocnak(q, mp, 0, secpolicy_net_config(cr, B_FALSE));
9754 		return;
9755 	}
9756 
9757 	/*
9758 	 * The ioctl command tables can only encode fixed length
9759 	 * ioctl data. If the length is variable, the table will
9760 	 * encode the length as zero. Such special cases are handled
9761 	 * below in the switch.
9762 	 */
9763 	if (ipip->ipi_copyin_size != 0) {
9764 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
9765 		return;
9766 	}
9767 
9768 	switch (iocp->ioc_cmd) {
9769 	case O_SIOCGIFCONF:
9770 	case SIOCGIFCONF:
9771 		/*
9772 		 * This IOCTL is hilarious.  See comments in
9773 		 * ip_sioctl_get_ifconf for the story.
9774 		 */
9775 		if (iocp->ioc_count == TRANSPARENT)
9776 			copyin_size = SIZEOF_STRUCT(ifconf,
9777 			    iocp->ioc_flag);
9778 		else
9779 			copyin_size = iocp->ioc_count;
9780 		mi_copyin(q, mp, NULL, copyin_size);
9781 		return;
9782 
9783 	case O_SIOCGLIFCONF:
9784 	case SIOCGLIFCONF:
9785 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
9786 		mi_copyin(q, mp, NULL, copyin_size);
9787 		return;
9788 
9789 	case SIOCGLIFSRCOF:
9790 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
9791 		mi_copyin(q, mp, NULL, copyin_size);
9792 		return;
9793 	case SIOCGIP6ADDRPOLICY:
9794 		ip_sioctl_ip6addrpolicy(q, mp);
9795 		ip6_asp_table_refrele();
9796 		return;
9797 
9798 	case SIOCSIP6ADDRPOLICY:
9799 		ip_sioctl_ip6addrpolicy(q, mp);
9800 		return;
9801 
9802 	case SIOCGDSTINFO:
9803 		ip_sioctl_dstinfo(q, mp);
9804 		ip6_asp_table_refrele();
9805 		return;
9806 
9807 	case I_PLINK:
9808 	case I_PUNLINK:
9809 	case I_LINK:
9810 	case I_UNLINK:
9811 		/*
9812 		 * We treat non-persistent link similarly as the persistent
9813 		 * link case, in terms of plumbing/unplumbing, as well as
9814 		 * dynamic re-plumbing events indicator.  See comments
9815 		 * in ip_sioctl_plink() for more.
9816 		 *
9817 		 * Request can be enqueued in the 'ipsq' while waiting
9818 		 * to become exclusive. So bump up the conn ref.
9819 		 */
9820 		if (CONN_Q(q))
9821 			CONN_INC_REF(Q_TO_CONN(q));
9822 		ip_sioctl_plink(NULL, q, mp, NULL);
9823 		return;
9824 
9825 	case ND_GET:
9826 	case ND_SET:
9827 		/*
9828 		 * Use of the nd table requires holding the reader lock.
9829 		 * Modifying the nd table thru nd_load/nd_unload requires
9830 		 * the writer lock.
9831 		 */
9832 		rw_enter(&ip_g_nd_lock, RW_READER);
9833 		if (nd_getset(q, ip_g_nd, mp)) {
9834 			rw_exit(&ip_g_nd_lock);
9835 
9836 			if (iocp->ioc_error)
9837 				iocp->ioc_count = 0;
9838 			mp->b_datap->db_type = M_IOCACK;
9839 			qreply(q, mp);
9840 			return;
9841 		}
9842 		rw_exit(&ip_g_nd_lock);
9843 		/*
9844 		 * We don't understand this subioctl of ND_GET / ND_SET.
9845 		 * Maybe intended for some driver / module below us
9846 		 */
9847 		if (q->q_next) {
9848 			putnext(q, mp);
9849 		} else {
9850 			iocp->ioc_error = ENOENT;
9851 			mp->b_datap->db_type = M_IOCNAK;
9852 			iocp->ioc_count = 0;
9853 			qreply(q, mp);
9854 		}
9855 		return;
9856 
9857 	case IP_IOCTL:
9858 		ip_wput_ioctl(q, mp);
9859 		return;
9860 	default:
9861 		cmn_err(CE_PANIC, "should not happen ");
9862 	}
9863 nak:
9864 	if (mp->b_cont != NULL) {
9865 		freemsg(mp->b_cont);
9866 		mp->b_cont = NULL;
9867 	}
9868 	iocp->ioc_error = EINVAL;
9869 	mp->b_datap->db_type = M_IOCNAK;
9870 	iocp->ioc_count = 0;
9871 	qreply(q, mp);
9872 }
9873 
9874 /* ip_wput hands off ARP IOCTL responses to us */
9875 void
9876 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
9877 {
9878 	struct arpreq *ar;
9879 	struct xarpreq *xar;
9880 	area_t	*area;
9881 	mblk_t	*area_mp;
9882 	struct iocblk *iocp;
9883 	mblk_t	*orig_ioc_mp, *tmp;
9884 	struct iocblk	*orig_iocp;
9885 	ill_t *ill;
9886 	conn_t *connp = NULL;
9887 	uint_t ioc_id;
9888 	mblk_t *pending_mp;
9889 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
9890 	int *flagsp;
9891 	char *storage = NULL;
9892 	sin_t *sin;
9893 	ipaddr_t addr;
9894 	int err;
9895 
9896 	ill = q->q_ptr;
9897 	ASSERT(ill != NULL);
9898 
9899 	/*
9900 	 * We should get back from ARP a packet chain that looks like:
9901 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9902 	 */
9903 	if (!(area_mp = mp->b_cont) ||
9904 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
9905 	    !(orig_ioc_mp = area_mp->b_cont) ||
9906 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
9907 		freemsg(mp);
9908 		return;
9909 	}
9910 
9911 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
9912 
9913 	tmp = (orig_ioc_mp->b_cont)->b_cont;
9914 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
9915 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
9916 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
9917 		x_arp_ioctl = B_TRUE;
9918 		xar = (struct xarpreq *)tmp->b_rptr;
9919 		sin = (sin_t *)&xar->xarp_pa;
9920 		flagsp = &xar->xarp_flags;
9921 		storage = xar->xarp_ha.sdl_data;
9922 		if (xar->xarp_ha.sdl_nlen != 0)
9923 			ifx_arp_ioctl = B_TRUE;
9924 	} else {
9925 		ar = (struct arpreq *)tmp->b_rptr;
9926 		sin = (sin_t *)&ar->arp_pa;
9927 		flagsp = &ar->arp_flags;
9928 		storage = ar->arp_ha.sa_data;
9929 	}
9930 
9931 	iocp = (struct iocblk *)mp->b_rptr;
9932 
9933 	/*
9934 	 * Pick out the originating queue based on the ioc_id.
9935 	 */
9936 	ioc_id = iocp->ioc_id;
9937 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
9938 	if (pending_mp == NULL) {
9939 		ASSERT(connp == NULL);
9940 		inet_freemsg(mp);
9941 		return;
9942 	}
9943 	ASSERT(connp != NULL);
9944 	q = CONNP_TO_WQ(connp);
9945 
9946 	/* Uncouple the internally generated IOCTL from the original one */
9947 	area = (area_t *)area_mp->b_rptr;
9948 	area_mp->b_cont = NULL;
9949 
9950 	/*
9951 	 * Restore the b_next and b_prev used by mi code. This is needed
9952 	 * to complete the ioctl using mi* functions. We stored them in
9953 	 * the pending mp prior to sending the request to ARP.
9954 	 */
9955 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
9956 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
9957 	inet_freemsg(pending_mp);
9958 
9959 	/*
9960 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
9961 	 * Catch the case where there is an IRE_CACHE by no entry in the
9962 	 * arp table.
9963 	 */
9964 	addr = sin->sin_addr.s_addr;
9965 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
9966 		ire_t			*ire;
9967 		dl_unitdata_req_t	*dlup;
9968 		mblk_t			*llmp;
9969 		int			addr_len;
9970 		ill_t			*ipsqill = NULL;
9971 
9972 		if (ifx_arp_ioctl) {
9973 			/*
9974 			 * There's no need to lookup the ill, since
9975 			 * we've already done that when we started
9976 			 * processing the ioctl and sent the message
9977 			 * to ARP on that ill.  So use the ill that
9978 			 * is stored in q->q_ptr.
9979 			 */
9980 			ipsqill = ill;
9981 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
9982 			    ipsqill->ill_ipif, ALL_ZONES,
9983 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL);
9984 		} else {
9985 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
9986 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
9987 			if (ire != NULL)
9988 				ipsqill = ire_to_ill(ire);
9989 		}
9990 
9991 		if ((x_arp_ioctl) && (ipsqill != NULL))
9992 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
9993 
9994 		if (ire != NULL) {
9995 			*flagsp = ATF_INUSE;
9996 			llmp = ire->ire_dlureq_mp;
9997 			if (llmp != NULL && ipsqill != NULL) {
9998 				uchar_t *macaddr;
9999 
10000 				addr_len = ipsqill->ill_phys_addr_length;
10001 				if (x_arp_ioctl && ((addr_len +
10002 				    ipsqill->ill_name_length) >
10003 				    sizeof (xar->xarp_ha.sdl_data))) {
10004 					ire_refrele(ire);
10005 					freemsg(mp);
10006 					ip_ioctl_finish(q, orig_ioc_mp,
10007 					    EINVAL, NO_COPYOUT, NULL, NULL);
10008 					return;
10009 				}
10010 				*flagsp |= ATF_COM;
10011 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10012 				if (ipsqill->ill_sap_length < 0)
10013 					macaddr = llmp->b_rptr +
10014 					    dlup->dl_dest_addr_offset;
10015 				else
10016 					macaddr = llmp->b_rptr +
10017 					    dlup->dl_dest_addr_offset +
10018 					    ipsqill->ill_sap_length;
10019 				/*
10020 				 * For SIOCGARP, MAC address length
10021 				 * validation has already been done
10022 				 * before the ioctl was issued to ARP to
10023 				 * allow it to progress only on 6 byte
10024 				 * addressable (ethernet like) media. Thus
10025 				 * the mac address copying can not overwrite
10026 				 * the sa_data area below.
10027 				 */
10028 				bcopy(macaddr, storage, addr_len);
10029 			}
10030 			/* Ditch the internal IOCTL. */
10031 			freemsg(mp);
10032 			ire_refrele(ire);
10033 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL, NULL);
10034 			return;
10035 		}
10036 	}
10037 
10038 	/*
10039 	 * Delete the coresponding IRE_CACHE if any.
10040 	 * Reset the error if there was one (in case there was no entry
10041 	 * in arp.)
10042 	 */
10043 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10044 		ipif_t *ipintf = NULL;
10045 
10046 		if (ifx_arp_ioctl) {
10047 			/*
10048 			 * There's no need to lookup the ill, since
10049 			 * we've already done that when we started
10050 			 * processing the ioctl and sent the message
10051 			 * to ARP on that ill.  So use the ill that
10052 			 * is stored in q->q_ptr.
10053 			 */
10054 			ipintf = ill->ill_ipif;
10055 		}
10056 		if (ip_ire_clookup_and_delete(addr, ipintf)) {
10057 			/*
10058 			 * The address in "addr" may be an entry for a
10059 			 * router. If that's true, then any off-net
10060 			 * IRE_CACHE entries that go through the router
10061 			 * with address "addr" must be clobbered. Use
10062 			 * ire_walk to achieve this goal.
10063 			 */
10064 			if (ifx_arp_ioctl)
10065 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10066 				    ire_delete_cache_gw, (char *)&addr, ill);
10067 			else
10068 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10069 				    ALL_ZONES);
10070 			iocp->ioc_error = 0;
10071 		}
10072 	}
10073 
10074 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10075 		err = iocp->ioc_error;
10076 		freemsg(mp);
10077 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL, NULL);
10078 		return;
10079 	}
10080 
10081 	/*
10082 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10083 	 * the area_t into the struct {x}arpreq.
10084 	 */
10085 	if (x_arp_ioctl) {
10086 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10087 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10088 		    sizeof (xar->xarp_ha.sdl_data)) {
10089 			freemsg(mp);
10090 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL,
10091 			    NO_COPYOUT, NULL, NULL);
10092 			return;
10093 		}
10094 	}
10095 	*flagsp = ATF_INUSE;
10096 	if (area->area_flags & ACE_F_PERMANENT)
10097 		*flagsp |= ATF_PERM;
10098 	if (area->area_flags & ACE_F_PUBLISH)
10099 		*flagsp |= ATF_PUBL;
10100 	if (area->area_hw_addr_length != 0) {
10101 		*flagsp |= ATF_COM;
10102 		/*
10103 		 * For SIOCGARP, MAC address length validation has
10104 		 * already been done before the ioctl was issued to ARP
10105 		 * to allow it to progress only on 6 byte addressable
10106 		 * (ethernet like) media. Thus the mac address copying
10107 		 * can not overwrite the sa_data area below.
10108 		 */
10109 		bcopy((char *)area + area->area_hw_addr_offset,
10110 		    storage, area->area_hw_addr_length);
10111 	}
10112 
10113 	/* Ditch the internal IOCTL. */
10114 	freemsg(mp);
10115 	/* Complete the original. */
10116 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL, NULL);
10117 }
10118 
10119 /*
10120  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10121  * interface) create the next available logical interface for this
10122  * physical interface.
10123  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10124  * ipif with the specified name.
10125  *
10126  * If the address family is not AF_UNSPEC then set the address as well.
10127  *
10128  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10129  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10130  *
10131  * Executed as a writer on the ill or ill group.
10132  * So no lock is needed to traverse the ipif chain, or examine the
10133  * phyint flags.
10134  */
10135 /* ARGSUSED */
10136 int
10137 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10138     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10139 {
10140 	mblk_t	*mp1;
10141 	struct lifreq *lifr;
10142 	boolean_t	isv6;
10143 	boolean_t	exists;
10144 	char 	*name;
10145 	char	*endp;
10146 	char	*cp;
10147 	int	namelen;
10148 	ipif_t	*ipif;
10149 	long	id;
10150 	ipsq_t	*ipsq;
10151 	ill_t	*ill;
10152 	sin_t	*sin;
10153 	int	err = 0;
10154 	boolean_t found_sep = B_FALSE;
10155 	conn_t	*connp;
10156 	zoneid_t zoneid;
10157 	int	orig_ifindex = 0;
10158 
10159 	ip1dbg(("ip_sioctl_addif\n"));
10160 	/* Existence of mp1 has been checked in ip_wput_nondata */
10161 	mp1 = mp->b_cont->b_cont;
10162 	/*
10163 	 * Null terminate the string to protect against buffer
10164 	 * overrun. String was generated by user code and may not
10165 	 * be trusted.
10166 	 */
10167 	lifr = (struct lifreq *)mp1->b_rptr;
10168 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10169 	name = lifr->lifr_name;
10170 	ASSERT(CONN_Q(q));
10171 	connp = Q_TO_CONN(q);
10172 	isv6 = connp->conn_af_isv6;
10173 	zoneid = connp->conn_zoneid;
10174 	namelen = mi_strlen(name);
10175 	if (namelen == 0)
10176 		return (EINVAL);
10177 
10178 	exists = B_FALSE;
10179 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10180 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10181 		/*
10182 		 * Allow creating lo0 using SIOCLIFADDIF.
10183 		 * can't be any other writer thread. So can pass null below
10184 		 * for the last 4 args to ipif_lookup_name.
10185 		 */
10186 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen,
10187 		    B_TRUE, &exists, isv6, zoneid, NULL, NULL, NULL, NULL);
10188 		/* Prevent any further action */
10189 		if (ipif == NULL) {
10190 			return (ENOBUFS);
10191 		} else if (!exists) {
10192 			/* We created the ipif now and as writer */
10193 			ipif_refrele(ipif);
10194 			return (0);
10195 		} else {
10196 			ill = ipif->ipif_ill;
10197 			ill_refhold(ill);
10198 			ipif_refrele(ipif);
10199 		}
10200 	} else {
10201 		/* Look for a colon in the name. */
10202 		endp = &name[namelen];
10203 		for (cp = endp; --cp > name; ) {
10204 			if (*cp == IPIF_SEPARATOR_CHAR) {
10205 				found_sep = B_TRUE;
10206 				/*
10207 				 * Reject any non-decimal aliases for plumbing
10208 				 * of logical interfaces. Aliases with leading
10209 				 * zeroes are also rejected as they introduce
10210 				 * ambiguity in the naming of the interfaces.
10211 				 * Comparing with "0" takes care of all such
10212 				 * cases.
10213 				 */
10214 				if ((strncmp("0", cp+1, 1)) == 0)
10215 					return (EINVAL);
10216 
10217 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10218 				    id <= 0 || *endp != '\0') {
10219 					return (EINVAL);
10220 				}
10221 				*cp = '\0';
10222 				break;
10223 			}
10224 		}
10225 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10226 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL);
10227 		if (found_sep)
10228 			*cp = IPIF_SEPARATOR_CHAR;
10229 		if (ill == NULL)
10230 			return (err);
10231 	}
10232 
10233 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10234 	    B_TRUE);
10235 
10236 	/*
10237 	 * Release the refhold due to the lookup, now that we are excl
10238 	 * or we are just returning
10239 	 */
10240 	ill_refrele(ill);
10241 
10242 	if (ipsq == NULL)
10243 		return (EINPROGRESS);
10244 
10245 	/*
10246 	 * If the interface is failed, inactive or offlined, look for a working
10247 	 * interface in the ill group and create the ipif there. If we can't
10248 	 * find a good interface, create the ipif anyway so that in.mpathd can
10249 	 * move it to the first repaired interface.
10250 	 */
10251 	if ((ill->ill_phyint->phyint_flags &
10252 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10253 	    ill->ill_phyint->phyint_groupname_len != 0) {
10254 		phyint_t *phyi;
10255 		char *groupname = ill->ill_phyint->phyint_groupname;
10256 
10257 		/*
10258 		 * We're looking for a working interface, but it doesn't matter
10259 		 * if it's up or down; so instead of following the group lists,
10260 		 * we look at each physical interface and compare the groupname.
10261 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10262 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10263 		 * Otherwise we create the ipif on the failed interface.
10264 		 */
10265 		rw_enter(&ill_g_lock, RW_READER);
10266 		phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
10267 		for (; phyi != NULL;
10268 		    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
10269 		    phyi, AVL_AFTER)) {
10270 			if (phyi->phyint_groupname_len == 0)
10271 				continue;
10272 			ASSERT(phyi->phyint_groupname != NULL);
10273 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10274 			    !(phyi->phyint_flags &
10275 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10276 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10277 			    (phyi->phyint_illv4 != NULL))) {
10278 				break;
10279 			}
10280 		}
10281 		rw_exit(&ill_g_lock);
10282 
10283 		if (phyi != NULL) {
10284 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10285 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10286 			    phyi->phyint_illv4);
10287 		}
10288 	}
10289 
10290 	/*
10291 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10292 	 * before or after us.
10293 	 */
10294 	ASSERT(IAM_WRITER_ILL(ill));
10295 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10296 
10297 	if (found_sep && orig_ifindex == 0) {
10298 		/* Now see if there is an IPIF with this unit number. */
10299 		for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
10300 			if (ipif->ipif_id == id) {
10301 				err = EEXIST;
10302 				goto done;
10303 			}
10304 		}
10305 	}
10306 
10307 	/*
10308 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10309 	 * of lo0. We never come here when we plumb lo0:0. It
10310 	 * happens in ipif_lookup_on_name.
10311 	 * The specified unit number is ignored when we create the ipif on a
10312 	 * different interface. However, we save it in ipif_orig_ipifid below so
10313 	 * that the ipif fails back to the right position.
10314 	 */
10315 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10316 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10317 		err = ENOBUFS;
10318 		goto done;
10319 	}
10320 
10321 	/* Return created name with ioctl */
10322 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10323 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10324 	ip1dbg(("created %s\n", lifr->lifr_name));
10325 
10326 	/* Set address */
10327 	sin = (sin_t *)&lifr->lifr_addr;
10328 	if (sin->sin_family != AF_UNSPEC) {
10329 		err = ip_sioctl_addr(ipif, sin, q, mp,
10330 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10331 	}
10332 
10333 	/* Set ifindex and unit number for failback */
10334 	if (err == 0 && orig_ifindex != 0) {
10335 		ipif->ipif_orig_ifindex = orig_ifindex;
10336 		if (found_sep) {
10337 			ipif->ipif_orig_ipifid = id;
10338 		}
10339 	}
10340 
10341 done:
10342 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10343 	return (err);
10344 }
10345 
10346 /*
10347  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10348  * interface) delete it based on the IP address (on this physical interface).
10349  * Otherwise delete it based on the ipif_id.
10350  * Also, special handling to allow a removeif of lo0.
10351  */
10352 /* ARGSUSED */
10353 int
10354 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10355     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10356 {
10357 	conn_t		*connp;
10358 	ill_t		*ill = ipif->ipif_ill;
10359 	boolean_t	 success;
10360 
10361 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10362 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10363 	ASSERT(IAM_WRITER_IPIF(ipif));
10364 
10365 	connp = Q_TO_CONN(q);
10366 	/*
10367 	 * Special case for unplumbing lo0 (the loopback physical interface).
10368 	 * If unplumbing lo0, the incoming address structure has been
10369 	 * initialized to all zeros. When unplumbing lo0, all its logical
10370 	 * interfaces must be removed too.
10371 	 *
10372 	 * Note that this interface may be called to remove a specific
10373 	 * loopback logical interface (eg, lo0:1). But in that case
10374 	 * ipif->ipif_id != 0 so that the code path for that case is the
10375 	 * same as any other interface (meaning it skips the code directly
10376 	 * below).
10377 	 */
10378 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10379 		if (sin->sin_family == AF_UNSPEC &&
10380 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10381 			/*
10382 			 * Mark it condemned. No new ref. will be made to ill.
10383 			 */
10384 			mutex_enter(&ill->ill_lock);
10385 			ill->ill_state_flags |= ILL_CONDEMNED;
10386 			for (ipif = ill->ill_ipif; ipif != NULL;
10387 			    ipif = ipif->ipif_next) {
10388 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10389 			}
10390 			mutex_exit(&ill->ill_lock);
10391 
10392 			ipif = ill->ill_ipif;
10393 			/* unplumb the loopback interface */
10394 			ill_delete(ill);
10395 			mutex_enter(&connp->conn_lock);
10396 			mutex_enter(&ill->ill_lock);
10397 			ASSERT(ill->ill_group == NULL);
10398 
10399 			/* Are any references to this ill active */
10400 			if (ill_is_quiescent(ill)) {
10401 				mutex_exit(&ill->ill_lock);
10402 				mutex_exit(&connp->conn_lock);
10403 				ill_delete_tail(ill);
10404 				mi_free(ill);
10405 				return (0);
10406 			}
10407 			success = ipsq_pending_mp_add(connp, ipif,
10408 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10409 			mutex_exit(&connp->conn_lock);
10410 			mutex_exit(&ill->ill_lock);
10411 			if (success)
10412 				return (EINPROGRESS);
10413 			else
10414 				return (EINTR);
10415 		}
10416 	}
10417 
10418 	/*
10419 	 * We are exclusive on the ipsq, so an ill move will be serialized
10420 	 * before or after us.
10421 	 */
10422 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10423 
10424 	if (ipif->ipif_id == 0) {
10425 		/* Find based on address */
10426 		if (ipif->ipif_isv6) {
10427 			sin6_t *sin6;
10428 
10429 			if (sin->sin_family != AF_INET6)
10430 				return (EAFNOSUPPORT);
10431 
10432 			sin6 = (sin6_t *)sin;
10433 			/* We are a writer, so we should be able to lookup */
10434 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10435 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL);
10436 			if (ipif == NULL) {
10437 				/*
10438 				 * Maybe the address in on another interface in
10439 				 * the same IPMP group? We check this below.
10440 				 */
10441 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10442 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL);
10443 			}
10444 		} else {
10445 			ipaddr_t addr;
10446 
10447 			if (sin->sin_family != AF_INET)
10448 				return (EAFNOSUPPORT);
10449 
10450 			addr = sin->sin_addr.s_addr;
10451 			/* We are a writer, so we should be able to lookup */
10452 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10453 			    NULL, NULL, NULL);
10454 			if (ipif == NULL) {
10455 				/*
10456 				 * Maybe the address in on another interface in
10457 				 * the same IPMP group? We check this below.
10458 				 */
10459 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10460 				    NULL, NULL, NULL, NULL);
10461 			}
10462 		}
10463 		if (ipif == NULL) {
10464 			return (EADDRNOTAVAIL);
10465 		}
10466 		/*
10467 		 * When the address to be removed is hosted on a different
10468 		 * interface, we check if the interface is in the same IPMP
10469 		 * group as the specified one; if so we proceed with the
10470 		 * removal.
10471 		 * ill->ill_group is NULL when the ill is down, so we have to
10472 		 * compare the group names instead.
10473 		 */
10474 		if (ipif->ipif_ill != ill &&
10475 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10476 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10477 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10478 		    ill->ill_phyint->phyint_groupname) != 0)) {
10479 			ipif_refrele(ipif);
10480 			return (EADDRNOTAVAIL);
10481 		}
10482 
10483 		/* This is a writer */
10484 		ipif_refrele(ipif);
10485 	}
10486 
10487 	/*
10488 	 * Can not delete instance zero since it is tied to the ill.
10489 	 */
10490 	if (ipif->ipif_id == 0)
10491 		return (EBUSY);
10492 
10493 	mutex_enter(&ill->ill_lock);
10494 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10495 	mutex_exit(&ill->ill_lock);
10496 
10497 	ipif_free(ipif);
10498 
10499 	mutex_enter(&connp->conn_lock);
10500 	mutex_enter(&ill->ill_lock);
10501 
10502 	/* Are any references to this ipif active */
10503 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
10504 		mutex_exit(&ill->ill_lock);
10505 		mutex_exit(&connp->conn_lock);
10506 		ipif_down_tail(ipif);
10507 		ipif_free_tail(ipif);
10508 		return (0);
10509 	    }
10510 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10511 	    IPIF_FREE);
10512 	mutex_exit(&ill->ill_lock);
10513 	mutex_exit(&connp->conn_lock);
10514 	if (success)
10515 		return (EINPROGRESS);
10516 	else
10517 		return (EINTR);
10518 }
10519 
10520 /*
10521  * Restart the removeif ioctl. The refcnt has gone down to 0.
10522  * The ipif is already condemned. So can't find it thru lookups.
10523  */
10524 /* ARGSUSED */
10525 int
10526 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10527     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10528 {
10529 	ill_t *ill;
10530 
10531 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10532 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10533 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10534 		ill = ipif->ipif_ill;
10535 		ASSERT(IAM_WRITER_ILL(ill));
10536 		ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) &&
10537 		    (ill->ill_state_flags & IPIF_CONDEMNED));
10538 		ill_delete_tail(ill);
10539 		mi_free(ill);
10540 		return (0);
10541 	}
10542 
10543 	ill = ipif->ipif_ill;
10544 	ASSERT(IAM_WRITER_IPIF(ipif));
10545 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10546 
10547 	ipif_down_tail(ipif);
10548 	ipif_free_tail(ipif);
10549 
10550 	ILL_UNMARK_CHANGING(ill);
10551 	return (0);
10552 }
10553 
10554 /*
10555  * Set the local interface address.
10556  * Allow an address of all zero when the interface is down.
10557  */
10558 /* ARGSUSED */
10559 int
10560 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10561     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10562 {
10563 	int err = 0;
10564 	in6_addr_t v6addr;
10565 	boolean_t need_up = B_FALSE;
10566 
10567 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
10568 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10569 
10570 	ASSERT(IAM_WRITER_IPIF(ipif));
10571 
10572 	if (ipif->ipif_isv6) {
10573 		sin6_t *sin6;
10574 		ill_t *ill;
10575 		phyint_t *phyi;
10576 
10577 		if (sin->sin_family != AF_INET6)
10578 			return (EAFNOSUPPORT);
10579 
10580 		sin6 = (sin6_t *)sin;
10581 		v6addr = sin6->sin6_addr;
10582 		ill = ipif->ipif_ill;
10583 		phyi = ill->ill_phyint;
10584 
10585 		/*
10586 		 * Enforce that true multicast interfaces have a link-local
10587 		 * address for logical unit 0.
10588 		 */
10589 		if (ipif->ipif_id == 0 &&
10590 		    (ill->ill_flags & ILLF_MULTICAST) &&
10591 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
10592 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
10593 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
10594 			return (EADDRNOTAVAIL);
10595 		}
10596 
10597 		/*
10598 		 * up interfaces shouldn't have the unspecified address
10599 		 * unless they also have the IPIF_NOLOCAL flags set and
10600 		 * have a subnet assigned.
10601 		 */
10602 		if ((ipif->ipif_flags & IPIF_UP) &&
10603 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
10604 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
10605 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
10606 			return (EADDRNOTAVAIL);
10607 		}
10608 
10609 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
10610 			return (EADDRNOTAVAIL);
10611 	} else {
10612 		ipaddr_t addr;
10613 
10614 		if (sin->sin_family != AF_INET)
10615 			return (EAFNOSUPPORT);
10616 
10617 		addr = sin->sin_addr.s_addr;
10618 
10619 		/* Allow 0 as the local address. */
10620 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
10621 			return (EADDRNOTAVAIL);
10622 
10623 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10624 	}
10625 
10626 
10627 	/*
10628 	 * Even if there is no change we redo things just to rerun
10629 	 * ipif_set_default.
10630 	 */
10631 	if (ipif->ipif_flags & IPIF_UP) {
10632 		/*
10633 		 * Setting a new local address, make sure
10634 		 * we have net and subnet bcast ire's for
10635 		 * the old address if we need them.
10636 		 */
10637 		if (!ipif->ipif_isv6)
10638 			ipif_check_bcast_ires(ipif);
10639 		/*
10640 		 * If the interface is already marked up,
10641 		 * we call ipif_down which will take care
10642 		 * of ditching any IREs that have been set
10643 		 * up based on the old interface address.
10644 		 */
10645 		err = ipif_logical_down(ipif, q, mp);
10646 		if (err == EINPROGRESS)
10647 			return (err);
10648 		ipif_down_tail(ipif);
10649 		need_up = 1;
10650 	}
10651 
10652 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
10653 	return (err);
10654 }
10655 
10656 int
10657 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10658     boolean_t need_up)
10659 {
10660 	in6_addr_t v6addr;
10661 	ipaddr_t addr;
10662 	sin6_t	*sin6;
10663 	int	err = 0;
10664 
10665 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
10666 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10667 	ASSERT(IAM_WRITER_IPIF(ipif));
10668 	if (ipif->ipif_isv6) {
10669 		sin6 = (sin6_t *)sin;
10670 		v6addr = sin6->sin6_addr;
10671 	} else {
10672 		addr = sin->sin_addr.s_addr;
10673 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10674 	}
10675 	mutex_enter(&ipif->ipif_ill->ill_lock);
10676 	ipif->ipif_v6lcl_addr = v6addr;
10677 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
10678 		ipif->ipif_v6src_addr = ipv6_all_zeros;
10679 	} else {
10680 		ipif->ipif_v6src_addr = v6addr;
10681 	}
10682 
10683 	if ((ipif->ipif_isv6) && IN6_IS_ADDR_6TO4(&v6addr) &&
10684 		(!ipif->ipif_ill->ill_is_6to4tun)) {
10685 		queue_t *wqp = ipif->ipif_ill->ill_wq;
10686 
10687 		/*
10688 		 * The local address of this interface is a 6to4 address,
10689 		 * check if this interface is in fact a 6to4 tunnel or just
10690 		 * an interface configured with a 6to4 address.  We are only
10691 		 * interested in the former.
10692 		 */
10693 		if (wqp != NULL) {
10694 			while ((wqp->q_next != NULL) &&
10695 			    (wqp->q_next->q_qinfo != NULL) &&
10696 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
10697 
10698 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
10699 				    == TUN6TO4_MODID) {
10700 					/* set for use in IP */
10701 					ipif->ipif_ill->ill_is_6to4tun = 1;
10702 					break;
10703 				}
10704 				wqp = wqp->q_next;
10705 			}
10706 		}
10707 	}
10708 
10709 	ipif_set_default(ipif);
10710 	mutex_exit(&ipif->ipif_ill->ill_lock);
10711 
10712 	if (need_up) {
10713 		/*
10714 		 * Now bring the interface back up.  If this
10715 		 * is the only IPIF for the ILL, ipif_up
10716 		 * will have to re-bind to the device, so
10717 		 * we may get back EINPROGRESS, in which
10718 		 * case, this IOCTL will get completed in
10719 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10720 		 */
10721 		err = ipif_up(ipif, q, mp);
10722 	} else {
10723 		/*
10724 		 * Update the IPIF list in SCTP, ipif_up_done() will do it
10725 		 * if need_up is true.
10726 		 */
10727 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10728 	}
10729 
10730 	return (err);
10731 }
10732 
10733 
10734 /*
10735  * Restart entry point to restart the address set operation after the
10736  * refcounts have dropped to zero.
10737  */
10738 /* ARGSUSED */
10739 int
10740 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10741     ip_ioctl_cmd_t *ipip, void *ifreq)
10742 {
10743 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
10744 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10745 	ASSERT(IAM_WRITER_IPIF(ipif));
10746 	ipif_down_tail(ipif);
10747 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
10748 }
10749 
10750 /* ARGSUSED */
10751 int
10752 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10753     ip_ioctl_cmd_t *ipip, void *if_req)
10754 {
10755 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
10756 	struct lifreq *lifr = (struct lifreq *)if_req;
10757 
10758 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
10759 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10760 	/*
10761 	 * The net mask and address can't change since we have a
10762 	 * reference to the ipif. So no lock is necessary.
10763 	 */
10764 	if (ipif->ipif_isv6) {
10765 		*sin6 = sin6_null;
10766 		sin6->sin6_family = AF_INET6;
10767 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
10768 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10769 		lifr->lifr_addrlen =
10770 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10771 	} else {
10772 		*sin = sin_null;
10773 		sin->sin_family = AF_INET;
10774 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
10775 		if (ipip->ipi_cmd_type == LIF_CMD) {
10776 			lifr->lifr_addrlen =
10777 			    ip_mask_to_plen(ipif->ipif_net_mask);
10778 		}
10779 	}
10780 	return (0);
10781 }
10782 
10783 /*
10784  * Set the destination address for a pt-pt interface.
10785  */
10786 /* ARGSUSED */
10787 int
10788 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10789     ip_ioctl_cmd_t *ipip, void *if_req)
10790 {
10791 	int err = 0;
10792 	in6_addr_t v6addr;
10793 	boolean_t need_up = B_FALSE;
10794 
10795 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
10796 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10797 	ASSERT(IAM_WRITER_IPIF(ipif));
10798 
10799 	if (ipif->ipif_isv6) {
10800 		sin6_t *sin6;
10801 
10802 		if (sin->sin_family != AF_INET6)
10803 			return (EAFNOSUPPORT);
10804 
10805 		sin6 = (sin6_t *)sin;
10806 		v6addr = sin6->sin6_addr;
10807 
10808 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
10809 			return (EADDRNOTAVAIL);
10810 	} else {
10811 		ipaddr_t addr;
10812 
10813 		if (sin->sin_family != AF_INET)
10814 			return (EAFNOSUPPORT);
10815 
10816 		addr = sin->sin_addr.s_addr;
10817 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
10818 			return (EADDRNOTAVAIL);
10819 
10820 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10821 	}
10822 
10823 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
10824 		return (0);	/* No change */
10825 
10826 	if (ipif->ipif_flags & IPIF_UP) {
10827 		/*
10828 		 * If the interface is already marked up,
10829 		 * we call ipif_down which will take care
10830 		 * of ditching any IREs that have been set
10831 		 * up based on the old pp dst address.
10832 		 */
10833 		err = ipif_logical_down(ipif, q, mp);
10834 		if (err == EINPROGRESS)
10835 			return (err);
10836 		ipif_down_tail(ipif);
10837 		need_up = B_TRUE;
10838 	}
10839 	/*
10840 	 * could return EINPROGRESS. If so ioctl will complete in
10841 	 * ip_rput_dlpi_writer
10842 	 */
10843 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
10844 	return (err);
10845 }
10846 
10847 static int
10848 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10849     boolean_t need_up)
10850 {
10851 	in6_addr_t v6addr;
10852 	ill_t	*ill = ipif->ipif_ill;
10853 	int	err = 0;
10854 
10855 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n",
10856 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10857 	if (ipif->ipif_isv6) {
10858 		sin6_t *sin6;
10859 
10860 		sin6 = (sin6_t *)sin;
10861 		v6addr = sin6->sin6_addr;
10862 	} else {
10863 		ipaddr_t addr;
10864 
10865 		addr = sin->sin_addr.s_addr;
10866 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10867 	}
10868 	mutex_enter(&ill->ill_lock);
10869 	/* Set point to point destination address. */
10870 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10871 		/*
10872 		 * Allow this as a means of creating logical
10873 		 * pt-pt interfaces on top of e.g. an Ethernet.
10874 		 * XXX Undocumented HACK for testing.
10875 		 * pt-pt interfaces are created with NUD disabled.
10876 		 */
10877 		ipif->ipif_flags |= IPIF_POINTOPOINT;
10878 		ipif->ipif_flags &= ~IPIF_BROADCAST;
10879 		if (ipif->ipif_isv6)
10880 			ipif->ipif_ill->ill_flags |= ILLF_NONUD;
10881 	}
10882 
10883 	/* Set the new address. */
10884 	ipif->ipif_v6pp_dst_addr = v6addr;
10885 	/* Make sure subnet tracks pp_dst */
10886 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
10887 	mutex_exit(&ill->ill_lock);
10888 
10889 	if (need_up) {
10890 		/*
10891 		 * Now bring the interface back up.  If this
10892 		 * is the only IPIF for the ILL, ipif_up
10893 		 * will have to re-bind to the device, so
10894 		 * we may get back EINPROGRESS, in which
10895 		 * case, this IOCTL will get completed in
10896 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10897 		 */
10898 		err = ipif_up(ipif, q, mp);
10899 	}
10900 	return (err);
10901 }
10902 
10903 /*
10904  * Restart entry point to restart the dstaddress set operation after the
10905  * refcounts have dropped to zero.
10906  */
10907 /* ARGSUSED */
10908 int
10909 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10910     ip_ioctl_cmd_t *ipip, void *ifreq)
10911 {
10912 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
10913 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10914 	ipif_down_tail(ipif);
10915 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
10916 }
10917 
10918 /* ARGSUSED */
10919 int
10920 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10921     ip_ioctl_cmd_t *ipip, void *if_req)
10922 {
10923 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
10924 
10925 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
10926 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10927 	/*
10928 	 * Get point to point destination address. The addresses can't
10929 	 * change since we hold a reference to the ipif.
10930 	 */
10931 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
10932 		return (EADDRNOTAVAIL);
10933 
10934 	if (ipif->ipif_isv6) {
10935 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10936 		*sin6 = sin6_null;
10937 		sin6->sin6_family = AF_INET6;
10938 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
10939 	} else {
10940 		*sin = sin_null;
10941 		sin->sin_family = AF_INET;
10942 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
10943 	}
10944 	return (0);
10945 }
10946 
10947 /*
10948  * part of ipmp, make this func return the active/inactive state and
10949  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
10950  */
10951 /*
10952  * This function either sets or clears the IFF_INACTIVE flag.
10953  *
10954  * As long as there are some addresses or multicast memberships on the
10955  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
10956  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
10957  * will be used for outbound packets.
10958  *
10959  * Caller needs to verify the validity of setting IFF_INACTIVE.
10960  */
10961 static void
10962 phyint_inactive(phyint_t *phyi)
10963 {
10964 	ill_t *ill_v4;
10965 	ill_t *ill_v6;
10966 	ipif_t *ipif;
10967 	ilm_t *ilm;
10968 
10969 	ill_v4 = phyi->phyint_illv4;
10970 	ill_v6 = phyi->phyint_illv6;
10971 
10972 	/*
10973 	 * No need for a lock while traversing the list since iam
10974 	 * a writer
10975 	 */
10976 	if (ill_v4 != NULL) {
10977 		ASSERT(IAM_WRITER_ILL(ill_v4));
10978 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
10979 		    ipif = ipif->ipif_next) {
10980 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
10981 				mutex_enter(&phyi->phyint_lock);
10982 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10983 				mutex_exit(&phyi->phyint_lock);
10984 				return;
10985 			}
10986 		}
10987 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
10988 		    ilm = ilm->ilm_next) {
10989 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
10990 				mutex_enter(&phyi->phyint_lock);
10991 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10992 				mutex_exit(&phyi->phyint_lock);
10993 				return;
10994 			}
10995 		}
10996 	}
10997 	if (ill_v6 != NULL) {
10998 		ill_v6 = phyi->phyint_illv6;
10999 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11000 		    ipif = ipif->ipif_next) {
11001 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11002 				mutex_enter(&phyi->phyint_lock);
11003 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11004 				mutex_exit(&phyi->phyint_lock);
11005 				return;
11006 			}
11007 		}
11008 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11009 		    ilm = ilm->ilm_next) {
11010 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11011 				mutex_enter(&phyi->phyint_lock);
11012 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11013 				mutex_exit(&phyi->phyint_lock);
11014 				return;
11015 			}
11016 		}
11017 	}
11018 	mutex_enter(&phyi->phyint_lock);
11019 	phyi->phyint_flags |= PHYI_INACTIVE;
11020 	mutex_exit(&phyi->phyint_lock);
11021 }
11022 
11023 /*
11024  * This function is called only when the phyint flags change. Currently
11025  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11026  * that we can select a good ill.
11027  */
11028 static void
11029 ip_redo_nomination(phyint_t *phyi)
11030 {
11031 	ill_t *ill_v4;
11032 
11033 	ill_v4 = phyi->phyint_illv4;
11034 
11035 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11036 		ASSERT(IAM_WRITER_ILL(ill_v4));
11037 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11038 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11039 	}
11040 }
11041 
11042 /*
11043  * Heuristic to check if ill is INACTIVE.
11044  * Checks if ill has an ipif with an usable ip address.
11045  *
11046  * Return values:
11047  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11048  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11049  */
11050 static boolean_t
11051 ill_is_inactive(ill_t *ill)
11052 {
11053 	ipif_t *ipif;
11054 
11055 	/* Check whether it is in an IPMP group */
11056 	if (ill->ill_phyint->phyint_groupname == NULL)
11057 		return (B_FALSE);
11058 
11059 	if (ill->ill_ipif_up_count == 0)
11060 		return (B_TRUE);
11061 
11062 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11063 		uint64_t flags = ipif->ipif_flags;
11064 
11065 		/*
11066 		 * This ipif is usable if it is IPIF_UP and not a
11067 		 * dedicated test address.  A dedicated test address
11068 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11069 		 * (note in particular that V6 test addresses are
11070 		 * link-local data addresses and thus are marked
11071 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11072 		 */
11073 		if ((flags & IPIF_UP) &&
11074 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11075 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11076 			return (B_FALSE);
11077 	}
11078 	return (B_TRUE);
11079 }
11080 
11081 /*
11082  * Set interface flags.
11083  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11084  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11085  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11086  *
11087  * NOTE : We really don't enforce that ipif_id zero should be used
11088  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11089  *	  is because applications generally does SICGLIFFLAGS and
11090  *	  ORs in the new flags (that affects the logical) and does a
11091  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11092  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11093  *	  flags that will be turned on is correct with respect to
11094  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11095  */
11096 /* ARGSUSED */
11097 int
11098 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11099     ip_ioctl_cmd_t *ipip, void *if_req)
11100 {
11101 	uint64_t turn_on;
11102 	uint64_t turn_off;
11103 	int	err;
11104 	boolean_t need_up = B_FALSE;
11105 	phyint_t *phyi;
11106 	ill_t *ill;
11107 	uint64_t intf_flags;
11108 	boolean_t phyint_flags_modified = B_FALSE;
11109 	uint64_t flags;
11110 	struct ifreq *ifr;
11111 	struct lifreq *lifr;
11112 	boolean_t set_linklocal = B_FALSE;
11113 	boolean_t zero_source = B_FALSE;
11114 
11115 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11116 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11117 
11118 	ASSERT(IAM_WRITER_IPIF(ipif));
11119 
11120 	ill = ipif->ipif_ill;
11121 	phyi = ill->ill_phyint;
11122 
11123 	if (ipip->ipi_cmd_type == IF_CMD) {
11124 		ifr = (struct ifreq *)if_req;
11125 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11126 	} else {
11127 		lifr = (struct lifreq *)if_req;
11128 		flags = lifr->lifr_flags;
11129 	}
11130 
11131 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11132 
11133 	/*
11134 	 * Has the flags been set correctly till now ?
11135 	 */
11136 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11137 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11138 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11139 	/*
11140 	 * Compare the new flags to the old, and partition
11141 	 * into those coming on and those going off.
11142 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11143 	 */
11144 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11145 		flags |= intf_flags & ~0xFFFF;
11146 
11147 	/*
11148 	 * First check which bits will change and then which will
11149 	 * go on and off
11150 	 */
11151 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11152 	if (!turn_on)
11153 		return (0);	/* No change */
11154 
11155 	turn_off = intf_flags & turn_on;
11156 	turn_on ^= turn_off;
11157 	err = 0;
11158 
11159 	/*
11160 	 * Don't allow any bits belonging to the logical interface
11161 	 * to be set or cleared on the replacement ipif that was
11162 	 * created temporarily during a MOVE.
11163 	 */
11164 	if (ipif->ipif_replace_zero &&
11165 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11166 		return (EINVAL);
11167 	}
11168 
11169 	/*
11170 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11171 	 * IPv6 interfaces.
11172 	 */
11173 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11174 		return (EINVAL);
11175 
11176 	/*
11177 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11178 	 * interfaces.  It makes no sense in that context.
11179 	 */
11180 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11181 		return (EINVAL);
11182 
11183 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11184 		zero_source = B_TRUE;
11185 
11186 	/*
11187 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11188 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11189 	 * If the link local address isn't set, and can be set, it will get
11190 	 * set later on in this function.
11191 	 */
11192 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11193 	    (flags & IFF_UP) && !zero_source &&
11194 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11195 		if (ipif_cant_setlinklocal(ipif))
11196 			return (EINVAL);
11197 		set_linklocal = B_TRUE;
11198 	}
11199 
11200 	/*
11201 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11202 	 * same time. No need to grab ill_g_usesrc_lock here, see
11203 	 * synchronization notes in ip.c
11204 	 */
11205 	if (turn_on & PHYI_STANDBY &&
11206 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11207 		return (EINVAL);
11208 	}
11209 
11210 	/*
11211 	 * If we modify physical interface flags, we'll potentially need to
11212 	 * send up two routing socket messages for the changes (one for the
11213 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11214 	 */
11215 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11216 		phyint_flags_modified = B_TRUE;
11217 
11218 	/*
11219 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11220 	 * we need to flush the IRE_CACHES belonging to this ill.
11221 	 * We handle this case here without doing the DOWN/UP dance
11222 	 * like it is done for other flags. If some other flags are
11223 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11224 	 * below will handle it by bringing it down and then
11225 	 * bringing it UP.
11226 	 */
11227 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11228 		ill_t *ill_v4, *ill_v6;
11229 
11230 		ill_v4 = phyi->phyint_illv4;
11231 		ill_v6 = phyi->phyint_illv6;
11232 
11233 		/*
11234 		 * First set the INACTIVE flag if needed. Then delete the ires.
11235 		 * ire_add will atomically prevent creating new IRE_CACHEs
11236 		 * unless hidden flag is set.
11237 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11238 		 */
11239 		if ((turn_on & PHYI_FAILED) &&
11240 		    ((intf_flags & PHYI_STANDBY) || !ipmp_enable_failback)) {
11241 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11242 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11243 		}
11244 		if ((turn_off & PHYI_FAILED) &&
11245 		    ((intf_flags & PHYI_STANDBY) ||
11246 		    (!ipmp_enable_failback && ill_is_inactive(ill)))) {
11247 			phyint_inactive(phyi);
11248 		}
11249 
11250 		if (turn_on & PHYI_STANDBY) {
11251 			/*
11252 			 * We implicitly set INACTIVE only when STANDBY is set.
11253 			 * INACTIVE is also set on non-STANDBY phyint when user
11254 			 * disables FAILBACK using configuration file.
11255 			 * Do not allow STANDBY to be set on such INACTIVE
11256 			 * phyint
11257 			 */
11258 			if (phyi->phyint_flags & PHYI_INACTIVE)
11259 				return (EINVAL);
11260 			if (!(phyi->phyint_flags & PHYI_FAILED))
11261 				phyint_inactive(phyi);
11262 		}
11263 		if (turn_off & PHYI_STANDBY) {
11264 			if (ipmp_enable_failback) {
11265 				/*
11266 				 * Reset PHYI_INACTIVE.
11267 				 */
11268 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11269 			} else if (ill_is_inactive(ill) &&
11270 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11271 				/*
11272 				 * Need to set INACTIVE, when user sets
11273 				 * STANDBY on a non-STANDBY phyint and
11274 				 * later resets STANDBY
11275 				 */
11276 				phyint_inactive(phyi);
11277 			}
11278 		}
11279 		/*
11280 		 * We should always send up a message so that the
11281 		 * daemons come to know of it. Note that the zeroth
11282 		 * interface can be down and the check below for IPIF_UP
11283 		 * will not make sense as we are actually setting
11284 		 * a phyint flag here. We assume that the ipif used
11285 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11286 		 * send up any message for non-zero ipifs).
11287 		 */
11288 		phyint_flags_modified = B_TRUE;
11289 
11290 		if (ill_v4 != NULL) {
11291 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11292 			    IRE_CACHE, ill_stq_cache_delete,
11293 			    (char *)ill_v4, ill_v4);
11294 			illgrp_reset_schednext(ill_v4);
11295 		}
11296 		if (ill_v6 != NULL) {
11297 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11298 			    IRE_CACHE, ill_stq_cache_delete,
11299 			    (char *)ill_v6, ill_v6);
11300 			illgrp_reset_schednext(ill_v6);
11301 		}
11302 	}
11303 
11304 	/*
11305 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11306 	 * status of the interface and, if the interface is part of an IPMP
11307 	 * group, all other interfaces that are part of the same IPMP
11308 	 * group.
11309 	 */
11310 	if ((turn_on | turn_off) & ILLF_ROUTER) {
11311 		(void) ill_forward_set(q, mp, ((turn_on & ILLF_ROUTER) != 0),
11312 		    (caddr_t)ill);
11313 	}
11314 
11315 	/*
11316 	 * If the interface is not UP and we are not going to
11317 	 * bring it UP, record the flags and return. When the
11318 	 * interface comes UP later, the right actions will be
11319 	 * taken.
11320 	 */
11321 	if (!(ipif->ipif_flags & IPIF_UP) &&
11322 	    !(turn_on & IPIF_UP)) {
11323 		/* Record new flags in their respective places. */
11324 		mutex_enter(&ill->ill_lock);
11325 		mutex_enter(&ill->ill_phyint->phyint_lock);
11326 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11327 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11328 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11329 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11330 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11331 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11332 		mutex_exit(&ill->ill_lock);
11333 		mutex_exit(&ill->ill_phyint->phyint_lock);
11334 
11335 		/*
11336 		 * We do the broadcast and nomination here rather
11337 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11338 		 * the case of FAILBACK from INACTIVE standby to the
11339 		 * interface that has been repaired, PHYI_FAILED has not
11340 		 * been cleared yet. If there are only two interfaces in
11341 		 * that group, all we have is a FAILED and INACTIVE
11342 		 * interface. If we do the nomination soon after a failback,
11343 		 * the broadcast nomination code would select the
11344 		 * INACTIVE interface for receiving broadcasts as FAILED is
11345 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11346 		 * receive broadcast packets, we need to redo nomination
11347 		 * when the FAILED is cleared here. Thus, in general we
11348 		 * always do the nomination here for FAILED, STANDBY
11349 		 * and OFFLINE.
11350 		 */
11351 		if (((turn_on | turn_off) &
11352 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11353 			ip_redo_nomination(phyi);
11354 		}
11355 		if (phyint_flags_modified) {
11356 			if (phyi->phyint_illv4 != NULL) {
11357 				ip_rts_ifmsg(phyi->phyint_illv4->
11358 				    ill_ipif);
11359 			}
11360 			if (phyi->phyint_illv6 != NULL) {
11361 				ip_rts_ifmsg(phyi->phyint_illv6->
11362 				    ill_ipif);
11363 			}
11364 		}
11365 		return (0);
11366 	} else if (set_linklocal || zero_source) {
11367 		mutex_enter(&ill->ill_lock);
11368 		if (set_linklocal)
11369 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11370 		if (zero_source)
11371 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11372 		mutex_exit(&ill->ill_lock);
11373 	}
11374 
11375 	/*
11376 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11377 	 * or point-to-point interfaces with an unspecified destination. We do
11378 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11379 	 * have a subnet assigned, which is how in.ndpd currently manages its
11380 	 * onlink prefix list when no addresses are configured with those
11381 	 * prefixes.
11382 	 */
11383 	if (ipif->ipif_isv6 &&
11384 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11385 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11386 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11387 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11388 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11389 		return (EINVAL);
11390 	}
11391 
11392 	/*
11393 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11394 	 * from being brought up.
11395 	 */
11396 	if (!ipif->ipif_isv6 &&
11397 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11398 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11399 		return (EINVAL);
11400 	}
11401 
11402 	/*
11403 	 * The only flag changes that we currently take specific action on
11404 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11405 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11406 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11407 	 * the flags and bringing it back up again.
11408 	 */
11409 	if ((turn_on|turn_off) &
11410 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11411 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11412 		/*
11413 		 * Taking this ipif down, make sure we have
11414 		 * valid net and subnet bcast ire's for other
11415 		 * logical interfaces, if we need them.
11416 		 */
11417 		if (!ipif->ipif_isv6)
11418 			ipif_check_bcast_ires(ipif);
11419 
11420 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11421 		    !(turn_off & IPIF_UP)) {
11422 			need_up = B_TRUE;
11423 			if (ipif->ipif_flags & IPIF_UP)
11424 				ill->ill_logical_down = 1;
11425 			turn_on &= ~IPIF_UP;
11426 		}
11427 		err = ipif_down(ipif, q, mp);
11428 		ip1dbg(("ipif_down returns %d err ", err));
11429 		if (err == EINPROGRESS)
11430 			return (err);
11431 		ipif_down_tail(ipif);
11432 	}
11433 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
11434 }
11435 
11436 static int
11437 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
11438     boolean_t need_up)
11439 {
11440 	ill_t	*ill;
11441 	phyint_t *phyi;
11442 	uint64_t turn_on;
11443 	uint64_t turn_off;
11444 	uint64_t intf_flags;
11445 	boolean_t phyint_flags_modified = B_FALSE;
11446 	int	err = 0;
11447 	boolean_t set_linklocal = B_FALSE;
11448 	boolean_t zero_source = B_FALSE;
11449 
11450 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
11451 		ipif->ipif_ill->ill_name, ipif->ipif_id));
11452 
11453 	ASSERT(IAM_WRITER_IPIF(ipif));
11454 
11455 	ill = ipif->ipif_ill;
11456 	phyi = ill->ill_phyint;
11457 
11458 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11459 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
11460 
11461 	turn_off = intf_flags & turn_on;
11462 	turn_on ^= turn_off;
11463 
11464 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
11465 		phyint_flags_modified = B_TRUE;
11466 
11467 	/*
11468 	 * Now we change the flags. Track current value of
11469 	 * other flags in their respective places.
11470 	 */
11471 	mutex_enter(&ill->ill_lock);
11472 	mutex_enter(&phyi->phyint_lock);
11473 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11474 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11475 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11476 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11477 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11478 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11479 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
11480 		set_linklocal = B_TRUE;
11481 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
11482 	}
11483 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
11484 		zero_source = B_TRUE;
11485 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
11486 	}
11487 	mutex_exit(&ill->ill_lock);
11488 	mutex_exit(&phyi->phyint_lock);
11489 
11490 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
11491 		ip_redo_nomination(phyi);
11492 
11493 	if (set_linklocal)
11494 		(void) ipif_setlinklocal(ipif);
11495 
11496 	if (zero_source)
11497 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11498 	else
11499 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
11500 
11501 	if (need_up) {
11502 		/*
11503 		 * XXX ipif_up really does not know whether a phyint flags
11504 		 * was modified or not. So, it sends up information on
11505 		 * only one routing sockets message. As we don't bring up
11506 		 * the interface and also set STANDBY/FAILED simultaneously
11507 		 * it should be okay.
11508 		 */
11509 		err = ipif_up(ipif, q, mp);
11510 	} else {
11511 		/*
11512 		 * Make sure routing socket sees all changes to the flags.
11513 		 * ipif_up_done* handles this when we use ipif_up.
11514 		 */
11515 		if (phyint_flags_modified) {
11516 			if (phyi->phyint_illv4 != NULL) {
11517 				ip_rts_ifmsg(phyi->phyint_illv4->
11518 				    ill_ipif);
11519 			}
11520 			if (phyi->phyint_illv6 != NULL) {
11521 				ip_rts_ifmsg(phyi->phyint_illv6->
11522 				    ill_ipif);
11523 			}
11524 		} else {
11525 			ip_rts_ifmsg(ipif);
11526 		}
11527 	}
11528 	return (err);
11529 }
11530 
11531 /*
11532  * Restart entry point to restart the flags restart operation after the
11533  * refcounts have dropped to zero.
11534  */
11535 /* ARGSUSED */
11536 int
11537 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11538     ip_ioctl_cmd_t *ipip, void *if_req)
11539 {
11540 	int	err;
11541 	struct ifreq *ifr = (struct ifreq *)if_req;
11542 	struct lifreq *lifr = (struct lifreq *)if_req;
11543 
11544 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
11545 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11546 
11547 	ipif_down_tail(ipif);
11548 	if (ipip->ipi_cmd_type == IF_CMD) {
11549 		/*
11550 		 * Since ip_sioctl_flags expects an int and ifr_flags
11551 		 * is a short we need to cast ifr_flags into an int
11552 		 * to avoid having sign extension cause bits to get
11553 		 * set that should not be.
11554 		 */
11555 		err = ip_sioctl_flags_tail(ipif,
11556 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
11557 		    q, mp, B_TRUE);
11558 	} else {
11559 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
11560 		    q, mp, B_TRUE);
11561 	}
11562 	return (err);
11563 }
11564 
11565 /* ARGSUSED */
11566 int
11567 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11568     ip_ioctl_cmd_t *ipip, void *if_req)
11569 {
11570 	/*
11571 	 * Has the flags been set correctly till now ?
11572 	 */
11573 	ill_t *ill = ipif->ipif_ill;
11574 	phyint_t *phyi = ill->ill_phyint;
11575 
11576 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
11577 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11578 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11579 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11580 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11581 
11582 	/*
11583 	 * Need a lock since some flags can be set even when there are
11584 	 * references to the ipif.
11585 	 */
11586 	mutex_enter(&ill->ill_lock);
11587 	if (ipip->ipi_cmd_type == IF_CMD) {
11588 		struct ifreq *ifr = (struct ifreq *)if_req;
11589 
11590 		/* Get interface flags (low 16 only). */
11591 		ifr->ifr_flags = ((ipif->ipif_flags |
11592 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
11593 	} else {
11594 		struct lifreq *lifr = (struct lifreq *)if_req;
11595 
11596 		/* Get interface flags. */
11597 		lifr->lifr_flags = ipif->ipif_flags |
11598 		    ill->ill_flags | phyi->phyint_flags;
11599 	}
11600 	mutex_exit(&ill->ill_lock);
11601 	return (0);
11602 }
11603 
11604 /* ARGSUSED */
11605 int
11606 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11607     ip_ioctl_cmd_t *ipip, void *if_req)
11608 {
11609 	int mtu;
11610 	int ip_min_mtu;
11611 	struct ifreq	*ifr;
11612 	struct lifreq *lifr;
11613 	ire_t	*ire;
11614 
11615 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
11616 	    ipif->ipif_id, (void *)ipif));
11617 	if (ipip->ipi_cmd_type == IF_CMD) {
11618 		ifr = (struct ifreq *)if_req;
11619 		mtu = ifr->ifr_metric;
11620 	} else {
11621 		lifr = (struct lifreq *)if_req;
11622 		mtu = lifr->lifr_mtu;
11623 	}
11624 
11625 	if (ipif->ipif_isv6)
11626 		ip_min_mtu = IPV6_MIN_MTU;
11627 	else
11628 		ip_min_mtu = IP_MIN_MTU;
11629 
11630 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
11631 		return (EINVAL);
11632 
11633 	/*
11634 	 * Change the MTU size in all relevant ire's.
11635 	 * Mtu change Vs. new ire creation - protocol below.
11636 	 * First change ipif_mtu and the ire_max_frag of the
11637 	 * interface ire. Then do an ire walk and change the
11638 	 * ire_max_frag of all affected ires. During ire_add
11639 	 * under the bucket lock, set the ire_max_frag of the
11640 	 * new ire being created from the ipif/ire from which
11641 	 * it is being derived. If an mtu change happens after
11642 	 * the ire is added, the new ire will be cleaned up.
11643 	 * Conversely if the mtu change happens before the ire
11644 	 * is added, ire_add will see the new value of the mtu.
11645 	 */
11646 	ipif->ipif_mtu = mtu;
11647 	ipif->ipif_flags |= IPIF_FIXEDMTU;
11648 
11649 	if (ipif->ipif_isv6)
11650 		ire = ipif_to_ire_v6(ipif);
11651 	else
11652 		ire = ipif_to_ire(ipif);
11653 	if (ire != NULL) {
11654 		ire->ire_max_frag = ipif->ipif_mtu;
11655 		ire_refrele(ire);
11656 	}
11657 	if (ipif->ipif_flags & IPIF_UP) {
11658 		if (ipif->ipif_isv6)
11659 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES);
11660 		else
11661 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES);
11662 	}
11663 	/* Update the MTU in SCTP's list */
11664 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11665 	return (0);
11666 }
11667 
11668 /* Get interface MTU. */
11669 /* ARGSUSED */
11670 int
11671 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11672 	ip_ioctl_cmd_t *ipip, void *if_req)
11673 {
11674 	struct ifreq	*ifr;
11675 	struct lifreq	*lifr;
11676 
11677 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
11678 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11679 	if (ipip->ipi_cmd_type == IF_CMD) {
11680 		ifr = (struct ifreq *)if_req;
11681 		ifr->ifr_metric = ipif->ipif_mtu;
11682 	} else {
11683 		lifr = (struct lifreq *)if_req;
11684 		lifr->lifr_mtu = ipif->ipif_mtu;
11685 	}
11686 	return (0);
11687 }
11688 
11689 /* Set interface broadcast address. */
11690 /* ARGSUSED2 */
11691 int
11692 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11693 	ip_ioctl_cmd_t *ipip, void *if_req)
11694 {
11695 	ipaddr_t addr;
11696 	ire_t	*ire;
11697 
11698 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
11699 	    ipif->ipif_id));
11700 
11701 	ASSERT(IAM_WRITER_IPIF(ipif));
11702 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
11703 		return (EADDRNOTAVAIL);
11704 
11705 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
11706 
11707 	if (sin->sin_family != AF_INET)
11708 		return (EAFNOSUPPORT);
11709 
11710 	addr = sin->sin_addr.s_addr;
11711 	if (ipif->ipif_flags & IPIF_UP) {
11712 		/*
11713 		 * If we are already up, make sure the new
11714 		 * broadcast address makes sense.  If it does,
11715 		 * there should be an IRE for it already.
11716 		 * Don't match on ipif, only on the ill
11717 		 * since we are sharing these now. Don't use
11718 		 * MATCH_IRE_ILL_GROUP as we are looking for
11719 		 * the broadcast ire on this ill and each ill
11720 		 * in the group has its own broadcast ire.
11721 		 */
11722 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
11723 		    ipif, ALL_ZONES, NULL,
11724 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE));
11725 		if (ire == NULL) {
11726 			return (EINVAL);
11727 		} else {
11728 			ire_refrele(ire);
11729 		}
11730 	}
11731 	/*
11732 	 * Changing the broadcast addr for this ipif.
11733 	 * Make sure we have valid net and subnet bcast
11734 	 * ire's for other logical interfaces, if needed.
11735 	 */
11736 	if (addr != ipif->ipif_brd_addr)
11737 		ipif_check_bcast_ires(ipif);
11738 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
11739 	return (0);
11740 }
11741 
11742 /* Get interface broadcast address. */
11743 /* ARGSUSED */
11744 int
11745 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11746     ip_ioctl_cmd_t *ipip, void *if_req)
11747 {
11748 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
11749 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11750 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
11751 		return (EADDRNOTAVAIL);
11752 
11753 	/* IPIF_BROADCAST not possible with IPv6 */
11754 	ASSERT(!ipif->ipif_isv6);
11755 	*sin = sin_null;
11756 	sin->sin_family = AF_INET;
11757 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
11758 	return (0);
11759 }
11760 
11761 /*
11762  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
11763  */
11764 /* ARGSUSED */
11765 int
11766 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11767     ip_ioctl_cmd_t *ipip, void *if_req)
11768 {
11769 	int err = 0;
11770 	in6_addr_t v6mask;
11771 
11772 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
11773 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11774 
11775 	ASSERT(IAM_WRITER_IPIF(ipif));
11776 
11777 	if (ipif->ipif_isv6) {
11778 		sin6_t *sin6;
11779 
11780 		if (sin->sin_family != AF_INET6)
11781 			return (EAFNOSUPPORT);
11782 
11783 		sin6 = (sin6_t *)sin;
11784 		v6mask = sin6->sin6_addr;
11785 	} else {
11786 		ipaddr_t mask;
11787 
11788 		if (sin->sin_family != AF_INET)
11789 			return (EAFNOSUPPORT);
11790 
11791 		mask = sin->sin_addr.s_addr;
11792 		V4MASK_TO_V6(mask, v6mask);
11793 	}
11794 
11795 	/*
11796 	 * No big deal if the interface isn't already up, or the mask
11797 	 * isn't really changing, or this is pt-pt.
11798 	 */
11799 	if (!(ipif->ipif_flags & IPIF_UP) ||
11800 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
11801 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
11802 		ipif->ipif_v6net_mask = v6mask;
11803 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11804 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
11805 			    ipif->ipif_v6net_mask,
11806 			    ipif->ipif_v6subnet);
11807 		}
11808 		return (0);
11809 	}
11810 	/*
11811 	 * Make sure we have valid net and subnet broadcast ire's
11812 	 * for the old netmask, if needed by other logical interfaces.
11813 	 */
11814 	if (!ipif->ipif_isv6)
11815 		ipif_check_bcast_ires(ipif);
11816 
11817 	err = ipif_logical_down(ipif, q, mp);
11818 	if (err == EINPROGRESS)
11819 		return (err);
11820 	ipif_down_tail(ipif);
11821 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
11822 	return (err);
11823 }
11824 
11825 static int
11826 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
11827 {
11828 	in6_addr_t v6mask;
11829 	int err = 0;
11830 
11831 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
11832 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11833 
11834 	if (ipif->ipif_isv6) {
11835 		sin6_t *sin6;
11836 
11837 		sin6 = (sin6_t *)sin;
11838 		v6mask = sin6->sin6_addr;
11839 	} else {
11840 		ipaddr_t mask;
11841 
11842 		mask = sin->sin_addr.s_addr;
11843 		V4MASK_TO_V6(mask, v6mask);
11844 	}
11845 
11846 	ipif->ipif_v6net_mask = v6mask;
11847 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11848 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
11849 		    ipif->ipif_v6subnet);
11850 	}
11851 	err = ipif_up(ipif, q, mp);
11852 
11853 	if (err == 0 || err == EINPROGRESS) {
11854 		/*
11855 		 * The interface must be DL_BOUND if this packet has to
11856 		 * go out on the wire. Since we only go through a logical
11857 		 * down and are bound with the driver during an internal
11858 		 * down/up that is satisfied.
11859 		 */
11860 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
11861 			/* Potentially broadcast an address mask reply. */
11862 			ipif_mask_reply(ipif);
11863 		}
11864 	}
11865 	return (err);
11866 }
11867 
11868 /* ARGSUSED */
11869 int
11870 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11871     ip_ioctl_cmd_t *ipip, void *if_req)
11872 {
11873 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
11874 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11875 	ipif_down_tail(ipif);
11876 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
11877 }
11878 
11879 /* Get interface net mask. */
11880 /* ARGSUSED */
11881 int
11882 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11883     ip_ioctl_cmd_t *ipip, void *if_req)
11884 {
11885 	struct lifreq *lifr = (struct lifreq *)if_req;
11886 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
11887 
11888 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
11889 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11890 
11891 	/*
11892 	 * net mask can't change since we have a reference to the ipif.
11893 	 */
11894 	if (ipif->ipif_isv6) {
11895 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11896 		*sin6 = sin6_null;
11897 		sin6->sin6_family = AF_INET6;
11898 		sin6->sin6_addr = ipif->ipif_v6net_mask;
11899 		lifr->lifr_addrlen =
11900 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11901 	} else {
11902 		*sin = sin_null;
11903 		sin->sin_family = AF_INET;
11904 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
11905 		if (ipip->ipi_cmd_type == LIF_CMD) {
11906 			lifr->lifr_addrlen =
11907 			    ip_mask_to_plen(ipif->ipif_net_mask);
11908 		}
11909 	}
11910 	return (0);
11911 }
11912 
11913 /* ARGSUSED */
11914 int
11915 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11916     ip_ioctl_cmd_t *ipip, void *if_req)
11917 {
11918 
11919 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
11920 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11921 	/*
11922 	 * Set interface metric.  We don't use this for
11923 	 * anything but we keep track of it in case it is
11924 	 * important to routing applications or such.
11925 	 */
11926 	if (ipip->ipi_cmd_type == IF_CMD) {
11927 		struct ifreq    *ifr;
11928 
11929 		ifr = (struct ifreq *)if_req;
11930 		ipif->ipif_metric = ifr->ifr_metric;
11931 	} else {
11932 		struct lifreq   *lifr;
11933 
11934 		lifr = (struct lifreq *)if_req;
11935 		ipif->ipif_metric = lifr->lifr_metric;
11936 	}
11937 	return (0);
11938 }
11939 
11940 
11941 /* ARGSUSED */
11942 int
11943 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11944     ip_ioctl_cmd_t *ipip, void *if_req)
11945 {
11946 
11947 	/* Get interface metric. */
11948 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
11949 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11950 	if (ipip->ipi_cmd_type == IF_CMD) {
11951 		struct ifreq    *ifr;
11952 
11953 		ifr = (struct ifreq *)if_req;
11954 		ifr->ifr_metric = ipif->ipif_metric;
11955 	} else {
11956 		struct lifreq   *lifr;
11957 
11958 		lifr = (struct lifreq *)if_req;
11959 		lifr->lifr_metric = ipif->ipif_metric;
11960 	}
11961 
11962 	return (0);
11963 }
11964 
11965 /* ARGSUSED */
11966 int
11967 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11968     ip_ioctl_cmd_t *ipip, void *if_req)
11969 {
11970 
11971 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
11972 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11973 	/*
11974 	 * Set the muxid returned from I_PLINK.
11975 	 */
11976 	if (ipip->ipi_cmd_type == IF_CMD) {
11977 		struct ifreq *ifr = (struct ifreq *)if_req;
11978 
11979 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
11980 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
11981 	} else {
11982 		struct lifreq *lifr = (struct lifreq *)if_req;
11983 
11984 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
11985 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
11986 	}
11987 	return (0);
11988 }
11989 
11990 /* ARGSUSED */
11991 int
11992 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11993     ip_ioctl_cmd_t *ipip, void *if_req)
11994 {
11995 
11996 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
11997 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11998 	/*
11999 	 * Get the muxid saved in ill for I_PUNLINK.
12000 	 */
12001 	if (ipip->ipi_cmd_type == IF_CMD) {
12002 		struct ifreq *ifr = (struct ifreq *)if_req;
12003 
12004 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12005 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12006 	} else {
12007 		struct lifreq *lifr = (struct lifreq *)if_req;
12008 
12009 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12010 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12011 	}
12012 	return (0);
12013 }
12014 
12015 /*
12016  * Set the subnet prefix. Does not modify the broadcast address.
12017  */
12018 /* ARGSUSED */
12019 int
12020 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12021     ip_ioctl_cmd_t *ipip, void *if_req)
12022 {
12023 	int err = 0;
12024 	in6_addr_t v6addr;
12025 	in6_addr_t v6mask;
12026 	boolean_t need_up = B_FALSE;
12027 	int addrlen;
12028 
12029 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12030 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12031 
12032 	ASSERT(IAM_WRITER_IPIF(ipif));
12033 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12034 
12035 	if (ipif->ipif_isv6) {
12036 		sin6_t *sin6;
12037 
12038 		if (sin->sin_family != AF_INET6)
12039 			return (EAFNOSUPPORT);
12040 
12041 		sin6 = (sin6_t *)sin;
12042 		v6addr = sin6->sin6_addr;
12043 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12044 			return (EADDRNOTAVAIL);
12045 	} else {
12046 		ipaddr_t addr;
12047 
12048 		if (sin->sin_family != AF_INET)
12049 			return (EAFNOSUPPORT);
12050 
12051 		addr = sin->sin_addr.s_addr;
12052 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12053 			return (EADDRNOTAVAIL);
12054 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12055 		/* Add 96 bits */
12056 		addrlen += IPV6_ABITS - IP_ABITS;
12057 	}
12058 
12059 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12060 		return (EINVAL);
12061 
12062 	/* Check if bits in the address is set past the mask */
12063 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12064 		return (EINVAL);
12065 
12066 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12067 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12068 		return (0);	/* No change */
12069 
12070 	if (ipif->ipif_flags & IPIF_UP) {
12071 		/*
12072 		 * If the interface is already marked up,
12073 		 * we call ipif_down which will take care
12074 		 * of ditching any IREs that have been set
12075 		 * up based on the old interface address.
12076 		 */
12077 		err = ipif_logical_down(ipif, q, mp);
12078 		if (err == EINPROGRESS)
12079 			return (err);
12080 		ipif_down_tail(ipif);
12081 		need_up = B_TRUE;
12082 	}
12083 
12084 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12085 	return (err);
12086 }
12087 
12088 static int
12089 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12090     queue_t *q, mblk_t *mp, boolean_t need_up)
12091 {
12092 	ill_t	*ill = ipif->ipif_ill;
12093 	int	err = 0;
12094 
12095 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12096 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12097 
12098 	/* Set the new address. */
12099 	mutex_enter(&ill->ill_lock);
12100 	ipif->ipif_v6net_mask = v6mask;
12101 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12102 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12103 		    ipif->ipif_v6subnet);
12104 	}
12105 	mutex_exit(&ill->ill_lock);
12106 
12107 	if (need_up) {
12108 		/*
12109 		 * Now bring the interface back up.  If this
12110 		 * is the only IPIF for the ILL, ipif_up
12111 		 * will have to re-bind to the device, so
12112 		 * we may get back EINPROGRESS, in which
12113 		 * case, this IOCTL will get completed in
12114 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12115 		 */
12116 		err = ipif_up(ipif, q, mp);
12117 		if (err == EINPROGRESS)
12118 			return (err);
12119 	}
12120 	return (err);
12121 }
12122 
12123 /* ARGSUSED */
12124 int
12125 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12126     ip_ioctl_cmd_t *ipip, void *if_req)
12127 {
12128 	int	addrlen;
12129 	in6_addr_t v6addr;
12130 	in6_addr_t v6mask;
12131 	struct lifreq *lifr = (struct lifreq *)if_req;
12132 
12133 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12134 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12135 	ipif_down_tail(ipif);
12136 
12137 	addrlen = lifr->lifr_addrlen;
12138 	if (ipif->ipif_isv6) {
12139 		sin6_t *sin6;
12140 
12141 		sin6 = (sin6_t *)sin;
12142 		v6addr = sin6->sin6_addr;
12143 	} else {
12144 		ipaddr_t addr;
12145 
12146 		addr = sin->sin_addr.s_addr;
12147 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12148 		addrlen += IPV6_ABITS - IP_ABITS;
12149 	}
12150 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12151 
12152 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12153 }
12154 
12155 /* ARGSUSED */
12156 int
12157 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12158     ip_ioctl_cmd_t *ipip, void *if_req)
12159 {
12160 	struct lifreq *lifr = (struct lifreq *)if_req;
12161 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12162 
12163 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12164 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12165 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12166 
12167 	if (ipif->ipif_isv6) {
12168 		*sin6 = sin6_null;
12169 		sin6->sin6_family = AF_INET6;
12170 		sin6->sin6_addr = ipif->ipif_v6subnet;
12171 		lifr->lifr_addrlen =
12172 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12173 	} else {
12174 		*sin = sin_null;
12175 		sin->sin_family = AF_INET;
12176 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12177 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12178 	}
12179 	return (0);
12180 }
12181 
12182 /*
12183  * Set the IPv6 address token.
12184  */
12185 /* ARGSUSED */
12186 int
12187 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12188     ip_ioctl_cmd_t *ipi, void *if_req)
12189 {
12190 	ill_t *ill = ipif->ipif_ill;
12191 	int err;
12192 	in6_addr_t v6addr;
12193 	in6_addr_t v6mask;
12194 	boolean_t need_up = B_FALSE;
12195 	int i;
12196 	sin6_t *sin6 = (sin6_t *)sin;
12197 	struct lifreq *lifr = (struct lifreq *)if_req;
12198 	int addrlen;
12199 
12200 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12201 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12202 	ASSERT(IAM_WRITER_IPIF(ipif));
12203 
12204 	addrlen = lifr->lifr_addrlen;
12205 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12206 	if (ipif->ipif_id != 0)
12207 		return (EINVAL);
12208 
12209 	if (!ipif->ipif_isv6)
12210 		return (EINVAL);
12211 
12212 	if (addrlen > IPV6_ABITS)
12213 		return (EINVAL);
12214 
12215 	v6addr = sin6->sin6_addr;
12216 
12217 	/*
12218 	 * The length of the token is the length from the end.  To get
12219 	 * the proper mask for this, compute the mask of the bits not
12220 	 * in the token; ie. the prefix, and then xor to get the mask.
12221 	 */
12222 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12223 		return (EINVAL);
12224 	for (i = 0; i < 4; i++) {
12225 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12226 	}
12227 
12228 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12229 	    ill->ill_token_length == addrlen)
12230 		return (0);	/* No change */
12231 
12232 	if (ipif->ipif_flags & IPIF_UP) {
12233 		err = ipif_logical_down(ipif, q, mp);
12234 		if (err == EINPROGRESS)
12235 			return (err);
12236 		ipif_down_tail(ipif);
12237 		need_up = B_TRUE;
12238 	}
12239 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12240 	return (err);
12241 }
12242 
12243 static int
12244 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12245     mblk_t *mp, boolean_t need_up)
12246 {
12247 	in6_addr_t v6addr;
12248 	in6_addr_t v6mask;
12249 	ill_t	*ill = ipif->ipif_ill;
12250 	int	i;
12251 	int	err = 0;
12252 
12253 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12254 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12255 	v6addr = sin6->sin6_addr;
12256 	/*
12257 	 * The length of the token is the length from the end.  To get
12258 	 * the proper mask for this, compute the mask of the bits not
12259 	 * in the token; ie. the prefix, and then xor to get the mask.
12260 	 */
12261 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12262 	for (i = 0; i < 4; i++)
12263 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12264 
12265 	mutex_enter(&ill->ill_lock);
12266 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12267 	ill->ill_token_length = addrlen;
12268 	mutex_exit(&ill->ill_lock);
12269 
12270 	if (need_up) {
12271 		/*
12272 		 * Now bring the interface back up.  If this
12273 		 * is the only IPIF for the ILL, ipif_up
12274 		 * will have to re-bind to the device, so
12275 		 * we may get back EINPROGRESS, in which
12276 		 * case, this IOCTL will get completed in
12277 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12278 		 */
12279 		err = ipif_up(ipif, q, mp);
12280 		if (err == EINPROGRESS)
12281 			return (err);
12282 	}
12283 	return (err);
12284 }
12285 
12286 /* ARGSUSED */
12287 int
12288 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12289     ip_ioctl_cmd_t *ipi, void *if_req)
12290 {
12291 	ill_t *ill;
12292 	sin6_t *sin6 = (sin6_t *)sin;
12293 	struct lifreq *lifr = (struct lifreq *)if_req;
12294 
12295 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12296 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12297 	if (ipif->ipif_id != 0)
12298 		return (EINVAL);
12299 
12300 	ill = ipif->ipif_ill;
12301 	if (!ill->ill_isv6)
12302 		return (ENXIO);
12303 
12304 	*sin6 = sin6_null;
12305 	sin6->sin6_family = AF_INET6;
12306 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12307 	sin6->sin6_addr = ill->ill_token;
12308 	lifr->lifr_addrlen = ill->ill_token_length;
12309 	return (0);
12310 }
12311 
12312 /*
12313  * Set (hardware) link specific information that might override
12314  * what was acquired through the DL_INFO_ACK.
12315  * The logic is as follows.
12316  *
12317  * become exclusive
12318  * set CHANGING flag
12319  * change mtu on affected IREs
12320  * clear CHANGING flag
12321  *
12322  * An ire add that occurs before the CHANGING flag is set will have its mtu
12323  * changed by the ip_sioctl_lnkinfo.
12324  *
12325  * During the time the CHANGING flag is set, no new ires will be added to the
12326  * bucket, and ire add will fail (due the CHANGING flag).
12327  *
12328  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12329  * before it is added to the bucket.
12330  *
12331  * Obviously only 1 thread can set the CHANGING flag and we need to become
12332  * exclusive to set the flag.
12333  */
12334 /* ARGSUSED */
12335 int
12336 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12337     ip_ioctl_cmd_t *ipi, void *if_req)
12338 {
12339 	ill_t		*ill = ipif->ipif_ill;
12340 	ipif_t		*nipif;
12341 	int		ip_min_mtu;
12342 	boolean_t	mtu_walk = B_FALSE;
12343 	struct lifreq	*lifr = (struct lifreq *)if_req;
12344 	lif_ifinfo_req_t *lir;
12345 	ire_t		*ire;
12346 
12347 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12348 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12349 	lir = &lifr->lifr_ifinfo;
12350 	ASSERT(IAM_WRITER_IPIF(ipif));
12351 
12352 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12353 	if (ipif->ipif_id != 0)
12354 		return (EINVAL);
12355 
12356 	/* Set interface MTU. */
12357 	if (ipif->ipif_isv6)
12358 		ip_min_mtu = IPV6_MIN_MTU;
12359 	else
12360 		ip_min_mtu = IP_MIN_MTU;
12361 
12362 	/*
12363 	 * Verify values before we set anything. Allow zero to
12364 	 * mean unspecified.
12365 	 */
12366 	if (lir->lir_maxmtu != 0 &&
12367 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12368 	    lir->lir_maxmtu < ip_min_mtu))
12369 		return (EINVAL);
12370 	if (lir->lir_reachtime != 0 &&
12371 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12372 		return (EINVAL);
12373 	if (lir->lir_reachretrans != 0 &&
12374 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12375 		return (EINVAL);
12376 
12377 	mutex_enter(&ill->ill_lock);
12378 	ill->ill_state_flags |= ILL_CHANGING;
12379 	for (nipif = ill->ill_ipif; nipif != NULL;
12380 	    nipif = nipif->ipif_next) {
12381 		nipif->ipif_state_flags |= IPIF_CHANGING;
12382 	}
12383 
12384 	mutex_exit(&ill->ill_lock);
12385 
12386 	if (lir->lir_maxmtu != 0) {
12387 		ill->ill_max_mtu = lir->lir_maxmtu;
12388 		ill->ill_mtu_userspecified = 1;
12389 		mtu_walk = B_TRUE;
12390 	}
12391 
12392 	if (lir->lir_reachtime != 0)
12393 		ill->ill_reachable_time = lir->lir_reachtime;
12394 
12395 	if (lir->lir_reachretrans != 0)
12396 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12397 
12398 	ill->ill_max_hops = lir->lir_maxhops;
12399 
12400 	ill->ill_max_buf = ND_MAX_Q;
12401 
12402 	if (mtu_walk) {
12403 		/*
12404 		 * Set the MTU on all ipifs associated with this ill except
12405 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12406 		 */
12407 		for (nipif = ill->ill_ipif; nipif != NULL;
12408 		    nipif = nipif->ipif_next) {
12409 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12410 				continue;
12411 
12412 			nipif->ipif_mtu = ill->ill_max_mtu;
12413 
12414 			if (!(nipif->ipif_flags & IPIF_UP))
12415 				continue;
12416 
12417 			if (nipif->ipif_isv6)
12418 				ire = ipif_to_ire_v6(nipif);
12419 			else
12420 				ire = ipif_to_ire(nipif);
12421 			if (ire != NULL) {
12422 				ire->ire_max_frag = ipif->ipif_mtu;
12423 				ire_refrele(ire);
12424 			}
12425 			if (ill->ill_isv6) {
12426 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
12427 				    ipif_mtu_change, (char *)nipif,
12428 				    ill);
12429 			} else {
12430 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
12431 				    ipif_mtu_change, (char *)nipif,
12432 				    ill);
12433 			}
12434 		}
12435 	}
12436 
12437 	mutex_enter(&ill->ill_lock);
12438 	for (nipif = ill->ill_ipif; nipif != NULL;
12439 	    nipif = nipif->ipif_next) {
12440 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12441 	}
12442 	ILL_UNMARK_CHANGING(ill);
12443 	mutex_exit(&ill->ill_lock);
12444 
12445 	return (0);
12446 }
12447 
12448 /* ARGSUSED */
12449 int
12450 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12451     ip_ioctl_cmd_t *ipi, void *if_req)
12452 {
12453 	struct lif_ifinfo_req *lir;
12454 	ill_t *ill = ipif->ipif_ill;
12455 
12456 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
12457 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12458 	if (ipif->ipif_id != 0)
12459 		return (EINVAL);
12460 
12461 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
12462 	lir->lir_maxhops = ill->ill_max_hops;
12463 	lir->lir_reachtime = ill->ill_reachable_time;
12464 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
12465 	lir->lir_maxmtu = ill->ill_max_mtu;
12466 
12467 	return (0);
12468 }
12469 
12470 /*
12471  * Return best guess as to the subnet mask for the specified address.
12472  * Based on the subnet masks for all the configured interfaces.
12473  *
12474  * We end up returning a zero mask in the case of default, multicast or
12475  * experimental.
12476  */
12477 static ipaddr_t
12478 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp)
12479 {
12480 	ipaddr_t net_mask;
12481 	ill_t	*ill;
12482 	ipif_t	*ipif;
12483 	ill_walk_context_t ctx;
12484 	ipif_t	*fallback_ipif = NULL;
12485 
12486 	net_mask = ip_net_mask(addr);
12487 	if (net_mask == 0) {
12488 		*ipifp = NULL;
12489 		return (0);
12490 	}
12491 
12492 	/* Let's check to see if this is maybe a local subnet route. */
12493 	/* this function only applies to IPv4 interfaces */
12494 	rw_enter(&ill_g_lock, RW_READER);
12495 	ill = ILL_START_WALK_V4(&ctx);
12496 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
12497 		mutex_enter(&ill->ill_lock);
12498 		for (ipif = ill->ill_ipif; ipif != NULL;
12499 		    ipif = ipif->ipif_next) {
12500 			if (!IPIF_CAN_LOOKUP(ipif))
12501 				continue;
12502 			if (!(ipif->ipif_flags & IPIF_UP))
12503 				continue;
12504 			if ((ipif->ipif_subnet & net_mask) ==
12505 			    (addr & net_mask)) {
12506 				/*
12507 				 * Don't trust pt-pt interfaces if there are
12508 				 * other interfaces.
12509 				 */
12510 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
12511 					if (fallback_ipif == NULL) {
12512 						ipif_refhold_locked(ipif);
12513 						fallback_ipif = ipif;
12514 					}
12515 					continue;
12516 				}
12517 
12518 				/*
12519 				 * Fine. Just assume the same net mask as the
12520 				 * directly attached subnet interface is using.
12521 				 */
12522 				ipif_refhold_locked(ipif);
12523 				mutex_exit(&ill->ill_lock);
12524 				rw_exit(&ill_g_lock);
12525 				if (fallback_ipif != NULL)
12526 					ipif_refrele(fallback_ipif);
12527 				*ipifp = ipif;
12528 				return (ipif->ipif_net_mask);
12529 			}
12530 		}
12531 		mutex_exit(&ill->ill_lock);
12532 	}
12533 	rw_exit(&ill_g_lock);
12534 
12535 	*ipifp = fallback_ipif;
12536 	return ((fallback_ipif != NULL) ?
12537 	    fallback_ipif->ipif_net_mask : net_mask);
12538 }
12539 
12540 /*
12541  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
12542  */
12543 static void
12544 ip_wput_ioctl(queue_t *q, mblk_t *mp)
12545 {
12546 	IOCP	iocp;
12547 	ipft_t	*ipft;
12548 	ipllc_t	*ipllc;
12549 	mblk_t	*mp1;
12550 	cred_t	*cr;
12551 	int	error = 0;
12552 	conn_t	*connp;
12553 
12554 	ip1dbg(("ip_wput_ioctl"));
12555 	iocp = (IOCP)mp->b_rptr;
12556 	mp1 = mp->b_cont;
12557 	if (mp1 == NULL) {
12558 		iocp->ioc_error = EINVAL;
12559 		mp->b_datap->db_type = M_IOCNAK;
12560 		iocp->ioc_count = 0;
12561 		qreply(q, mp);
12562 		return;
12563 	}
12564 
12565 	/*
12566 	 * These IOCTLs provide various control capabilities to
12567 	 * upstream agents such as ULPs and processes.	There
12568 	 * are currently two such IOCTLs implemented.  They
12569 	 * are used by TCP to provide update information for
12570 	 * existing IREs and to forcibly delete an IRE for a
12571 	 * host that is not responding, thereby forcing an
12572 	 * attempt at a new route.
12573 	 */
12574 	iocp->ioc_error = EINVAL;
12575 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
12576 		goto done;
12577 
12578 	ipllc = (ipllc_t *)mp1->b_rptr;
12579 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
12580 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
12581 			break;
12582 	}
12583 	/*
12584 	 * prefer credential from mblk over ioctl;
12585 	 * see ip_sioctl_copyin_setup
12586 	 */
12587 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
12588 
12589 	/*
12590 	 * Refhold the conn in case the request gets queued up in some lookup
12591 	 */
12592 	ASSERT(CONN_Q(q));
12593 	connp = Q_TO_CONN(q);
12594 	CONN_INC_REF(connp);
12595 	if (ipft->ipft_pfi &&
12596 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
12597 		pullupmsg(mp1, ipft->ipft_min_size))) {
12598 		error = (*ipft->ipft_pfi)(q,
12599 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
12600 	}
12601 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
12602 		/*
12603 		 * CONN_OPER_PENDING_DONE happens in the function called
12604 		 * through ipft_pfi above.
12605 		 */
12606 		return;
12607 	}
12608 
12609 	CONN_OPER_PENDING_DONE(connp);
12610 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
12611 		freemsg(mp);
12612 		return;
12613 	}
12614 	iocp->ioc_error = error;
12615 
12616 done:
12617 	mp->b_datap->db_type = M_IOCACK;
12618 	if (iocp->ioc_error)
12619 		iocp->ioc_count = 0;
12620 	qreply(q, mp);
12621 }
12622 
12623 /*
12624  * Lookup an ipif using the sequence id (ipif_seqid)
12625  */
12626 ipif_t *
12627 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
12628 {
12629 	ipif_t *ipif;
12630 
12631 	ASSERT(MUTEX_HELD(&ill->ill_lock));
12632 
12633 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12634 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
12635 			return (ipif);
12636 	}
12637 	return (NULL);
12638 }
12639 
12640 uint64_t ipif_g_seqid;
12641 
12642 /*
12643  * Assign a unique id for the ipif. This is used later when we send
12644  * IRES to ARP for resolution where we initialize ire_ipif_seqid
12645  * to the value pointed by ire_ipif->ipif_seqid. Later when the
12646  * IRE is added, we verify that ipif has not disappeared.
12647  */
12648 
12649 static void
12650 ipif_assign_seqid(ipif_t *ipif)
12651 {
12652 	ipif->ipif_seqid = atomic_add_64_nv(&ipif_g_seqid, 1);
12653 }
12654 
12655 /*
12656  * Insert the ipif, so that the list of ipifs on the ill will be sorted
12657  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
12658  * be inserted into the first space available in the list. The value of
12659  * ipif_id will then be set to the appropriate value for its position.
12660  */
12661 static int
12662 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
12663 {
12664 	ill_t *ill;
12665 	ipif_t *tipif;
12666 	ipif_t **tipifp;
12667 	int id;
12668 
12669 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
12670 	    IAM_WRITER_IPIF(ipif));
12671 
12672 	ill = ipif->ipif_ill;
12673 	ASSERT(ill != NULL);
12674 
12675 	/*
12676 	 * In the case of lo0:0 we already hold the ill_g_lock.
12677 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
12678 	 * ipif_insert. Another such caller is ipif_move.
12679 	 */
12680 	if (acquire_g_lock)
12681 		rw_enter(&ill_g_lock, RW_WRITER);
12682 	if (acquire_ill_lock)
12683 		mutex_enter(&ill->ill_lock);
12684 	id = ipif->ipif_id;
12685 	tipifp = &(ill->ill_ipif);
12686 	if (id == -1) {	/* need to find a real id */
12687 		id = 0;
12688 		while ((tipif = *tipifp) != NULL) {
12689 			ASSERT(tipif->ipif_id >= id);
12690 			if (tipif->ipif_id != id)
12691 				break; /* non-consecutive id */
12692 			id++;
12693 			tipifp = &(tipif->ipif_next);
12694 		}
12695 		/* limit number of logical interfaces */
12696 		if (id >= ip_addrs_per_if) {
12697 			if (acquire_ill_lock)
12698 				mutex_exit(&ill->ill_lock);
12699 			if (acquire_g_lock)
12700 				rw_exit(&ill_g_lock);
12701 			return (-1);
12702 		}
12703 		ipif->ipif_id = id; /* assign new id */
12704 	} else if (id < ip_addrs_per_if) {
12705 		/* we have a real id; insert ipif in the right place */
12706 		while ((tipif = *tipifp) != NULL) {
12707 			ASSERT(tipif->ipif_id != id);
12708 			if (tipif->ipif_id > id)
12709 				break; /* found correct location */
12710 			tipifp = &(tipif->ipif_next);
12711 		}
12712 	} else {
12713 		if (acquire_ill_lock)
12714 			mutex_exit(&ill->ill_lock);
12715 		if (acquire_g_lock)
12716 			rw_exit(&ill_g_lock);
12717 		return (-1);
12718 	}
12719 
12720 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
12721 
12722 	ipif->ipif_next = tipif;
12723 	*tipifp = ipif;
12724 	if (acquire_ill_lock)
12725 		mutex_exit(&ill->ill_lock);
12726 	if (acquire_g_lock)
12727 		rw_exit(&ill_g_lock);
12728 	return (0);
12729 }
12730 
12731 /*
12732  * Allocate and initialize a new interface control structure.  (Always
12733  * called as writer.)
12734  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
12735  * is not part of the global linked list of ills. ipif_seqid is unique
12736  * in the system and to preserve the uniqueness, it is assigned only
12737  * when ill becomes part of the global list. At that point ill will
12738  * have a name. If it doesn't get assigned here, it will get assigned
12739  * in ipif_set_values() as part of SIOCSLIFNAME processing.
12740  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
12741  * the interface flags or any other information from the DL_INFO_ACK for
12742  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
12743  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
12744  * second DL_INFO_ACK comes in from the driver.
12745  */
12746 static ipif_t *
12747 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
12748 {
12749 	ipif_t	*ipif;
12750 	phyint_t *phyi;
12751 
12752 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
12753 	    ill->ill_name, id, (void *)ill));
12754 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
12755 
12756 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
12757 		return (NULL);
12758 	*ipif = ipif_zero;	/* start clean */
12759 
12760 	ipif->ipif_ill = ill;
12761 	ipif->ipif_id = id;	/* could be -1 */
12762 	ipif->ipif_zoneid = GLOBAL_ZONEID;
12763 
12764 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
12765 
12766 	ipif->ipif_refcnt = 0;
12767 	ipif->ipif_saved_ire_cnt = 0;
12768 
12769 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
12770 		mi_free(ipif);
12771 		return (NULL);
12772 	}
12773 	/* -1 id should have been replaced by real id */
12774 	id = ipif->ipif_id;
12775 	ASSERT(id >= 0);
12776 
12777 	if (ill->ill_name[0] != '\0') {
12778 		ipif_assign_seqid(ipif);
12779 		if (ill->ill_phyint->phyint_ifindex != 0)
12780 			sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
12781 	}
12782 	/*
12783 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
12784 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
12785 	 * ioctl sets ipif_orig_ipifid to zero.
12786 	 */
12787 	ipif->ipif_orig_ipifid = id;
12788 
12789 	/*
12790 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
12791 	 * The ipif is still not up and can't be looked up until the
12792 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
12793 	 */
12794 	mutex_enter(&ill->ill_lock);
12795 	mutex_enter(&ill->ill_phyint->phyint_lock);
12796 	/*
12797 	 * Set the running flag when logical interface zero is created.
12798 	 * For subsequent logical interfaces, a DLPI link down
12799 	 * notification message may have cleared the running flag to
12800 	 * indicate the link is down, so we shouldn't just blindly set it.
12801 	 */
12802 	if (id == 0)
12803 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
12804 	ipif->ipif_ire_type = ire_type;
12805 	phyi = ill->ill_phyint;
12806 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
12807 
12808 	if (ipif->ipif_isv6) {
12809 		ill->ill_flags |= ILLF_IPV6;
12810 	} else {
12811 		ipaddr_t inaddr_any = INADDR_ANY;
12812 
12813 		ill->ill_flags |= ILLF_IPV4;
12814 
12815 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
12816 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12817 		    &ipif->ipif_v6lcl_addr);
12818 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12819 		    &ipif->ipif_v6src_addr);
12820 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12821 		    &ipif->ipif_v6subnet);
12822 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12823 		    &ipif->ipif_v6net_mask);
12824 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12825 		    &ipif->ipif_v6brd_addr);
12826 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12827 		    &ipif->ipif_v6pp_dst_addr);
12828 	}
12829 
12830 	/*
12831 	 * Don't set the interface flags etc. now, will do it in
12832 	 * ip_ll_subnet_defaults.
12833 	 */
12834 	if (!initialize) {
12835 		mutex_exit(&ill->ill_lock);
12836 		mutex_exit(&ill->ill_phyint->phyint_lock);
12837 		return (ipif);
12838 	}
12839 	ipif->ipif_mtu = ill->ill_max_mtu;
12840 
12841 	if (ill->ill_bcast_addr_length != 0) {
12842 		/*
12843 		 * Later detect lack of DLPI driver multicast
12844 		 * capability by catching DL_ENABMULTI errors in
12845 		 * ip_rput_dlpi.
12846 		 */
12847 		ill->ill_flags |= ILLF_MULTICAST;
12848 		if (!ipif->ipif_isv6)
12849 			ipif->ipif_flags |= IPIF_BROADCAST;
12850 	} else {
12851 		if (ill->ill_net_type != IRE_LOOPBACK) {
12852 			if (ipif->ipif_isv6)
12853 				/*
12854 				 * Note: xresolv interfaces will eventually need
12855 				 * NOARP set here as well, but that will require
12856 				 * those external resolvers to have some
12857 				 * knowledge of that flag and act appropriately.
12858 				 * Not to be changed at present.
12859 				 */
12860 				ill->ill_flags |= ILLF_NONUD;
12861 			else
12862 				ill->ill_flags |= ILLF_NOARP;
12863 		}
12864 		if (ill->ill_phys_addr_length == 0) {
12865 			if (ill->ill_media &&
12866 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
12867 				ipif->ipif_flags |= IPIF_NOXMIT;
12868 				phyi->phyint_flags |= PHYI_VIRTUAL;
12869 			} else {
12870 				/* pt-pt supports multicast. */
12871 				ill->ill_flags |= ILLF_MULTICAST;
12872 				if (ill->ill_net_type == IRE_LOOPBACK) {
12873 					phyi->phyint_flags |=
12874 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
12875 				} else {
12876 					ipif->ipif_flags |= IPIF_POINTOPOINT;
12877 				}
12878 			}
12879 		}
12880 	}
12881 	mutex_exit(&ill->ill_lock);
12882 	mutex_exit(&ill->ill_phyint->phyint_lock);
12883 	return (ipif);
12884 }
12885 
12886 /*
12887  * If appropriate, send a message up to the resolver delete the entry
12888  * for the address of this interface which is going out of business.
12889  * (Always called as writer).
12890  *
12891  * NOTE : We need to check for NULL mps as some of the fields are
12892  *	  initialized only for some interface types. See ipif_resolver_up()
12893  *	  for details.
12894  */
12895 void
12896 ipif_arp_down(ipif_t *ipif)
12897 {
12898 	mblk_t	*mp;
12899 
12900 	ip1dbg(("ipif_arp_down(%s:%u)\n",
12901 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12902 	ASSERT(IAM_WRITER_IPIF(ipif));
12903 
12904 	/* Delete the mapping for the local address */
12905 	mp = ipif->ipif_arp_del_mp;
12906 	if (mp != NULL) {
12907 		ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12908 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12909 		    ipif->ipif_ill->ill_name, ipif->ipif_id));
12910 		putnext(ipif->ipif_ill->ill_rq, mp);
12911 		ipif->ipif_arp_del_mp = NULL;
12912 	}
12913 
12914 	/*
12915 	 * If this is the last ipif that is going down, we need
12916 	 * to clean up ARP completely.
12917 	 */
12918 	if (ipif->ipif_ill->ill_ipif_up_count == 0) {
12919 
12920 		/* Send up AR_INTERFACE_DOWN message */
12921 		mp = ipif->ipif_ill->ill_arp_down_mp;
12922 		if (mp != NULL) {
12923 			ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12924 			    dlpi_prim_str(*(int *)mp->b_rptr),
12925 			    *(int *)mp->b_rptr, ipif->ipif_ill->ill_name,
12926 			    ipif->ipif_id));
12927 			putnext(ipif->ipif_ill->ill_rq, mp);
12928 			ipif->ipif_ill->ill_arp_down_mp = NULL;
12929 		}
12930 
12931 		/* Tell ARP to delete the multicast mappings */
12932 		mp = ipif->ipif_ill->ill_arp_del_mapping_mp;
12933 		if (mp != NULL) {
12934 			ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12935 			    dlpi_prim_str(*(int *)mp->b_rptr),
12936 			    *(int *)mp->b_rptr, ipif->ipif_ill->ill_name,
12937 			    ipif->ipif_id));
12938 			putnext(ipif->ipif_ill->ill_rq, mp);
12939 			ipif->ipif_ill->ill_arp_del_mapping_mp = NULL;
12940 		}
12941 	}
12942 }
12943 
12944 /*
12945  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
12946  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
12947  * that it wants the add_mp allocated in this function to be returned
12948  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
12949  * just re-do the multicast, it wants us to send the add_mp to ARP also.
12950  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
12951  * as it does a ipif_arp_down after calling this function - which will
12952  * remove what we add here.
12953  *
12954  * Returns -1 on failures and 0 on success.
12955  */
12956 int
12957 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
12958 {
12959 	mblk_t	*del_mp = NULL;
12960 	mblk_t *add_mp = NULL;
12961 	mblk_t *mp;
12962 	ill_t	*ill = ipif->ipif_ill;
12963 	phyint_t *phyi = ill->ill_phyint;
12964 	ipaddr_t addr, mask, extract_mask = 0;
12965 	arma_t	*arma;
12966 	uint8_t *maddr, *bphys_addr;
12967 	uint32_t hw_start;
12968 	dl_unitdata_req_t *dlur;
12969 
12970 	ASSERT(IAM_WRITER_IPIF(ipif));
12971 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
12972 		return (0);
12973 
12974 	/*
12975 	 * Delete the existing mapping from ARP. Normally ipif_down
12976 	 * -> ipif_arp_down should send this up to ARP. The only
12977 	 * reason we would find this when we are switching from
12978 	 * Multicast to Broadcast where we did not do a down.
12979 	 */
12980 	mp = ill->ill_arp_del_mapping_mp;
12981 	if (mp != NULL) {
12982 		ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12983 		    dlpi_prim_str(*(int *)mp->b_rptr),
12984 		    *(int *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
12985 		putnext(ill->ill_rq, mp);
12986 		ill->ill_arp_del_mapping_mp = NULL;
12987 	}
12988 
12989 	if (arp_add_mapping_mp != NULL)
12990 		*arp_add_mapping_mp = NULL;
12991 
12992 	/*
12993 	 * Check that the address is not to long for the constant
12994 	 * length reserved in the template arma_t.
12995 	 */
12996 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
12997 		return (-1);
12998 
12999 	/* Add mapping mblk */
13000 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13001 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13002 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13003 	    (caddr_t)&addr);
13004 	if (add_mp == NULL)
13005 		return (-1);
13006 	arma = (arma_t *)add_mp->b_rptr;
13007 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13008 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13009 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13010 
13011 	/*
13012 	 * Determine the broadcast address.
13013 	 */
13014 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13015 	if (ill->ill_sap_length < 0)
13016 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13017 	else
13018 		bphys_addr = (uchar_t *)dlur +
13019 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13020 	/*
13021 	 * Check PHYI_MULTI_BCAST and length of physical
13022 	 * address to determine if we use the mapping or the
13023 	 * broadcast address.
13024 	 */
13025 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13026 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13027 		    bphys_addr, maddr, &hw_start, &extract_mask))
13028 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13029 
13030 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13031 	    (ill->ill_flags & ILLF_MULTICAST)) {
13032 		/* Make sure this will not match the "exact" entry. */
13033 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13034 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13035 		    (caddr_t)&addr);
13036 		if (del_mp == NULL) {
13037 			freemsg(add_mp);
13038 			return (-1);
13039 		}
13040 		bcopy(&extract_mask, (char *)arma +
13041 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13042 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13043 			/* Use link-layer broadcast address for MULTI_BCAST */
13044 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13045 			ip2dbg(("ipif_arp_setup_multicast: adding"
13046 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13047 		} else {
13048 			arma->arma_hw_mapping_start = hw_start;
13049 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13050 			    " ARP setup for %s\n", ill->ill_name));
13051 		}
13052 	} else {
13053 		freemsg(add_mp);
13054 		ASSERT(del_mp == NULL);
13055 		/* It is neither MULTICAST nor MULTI_BCAST */
13056 		return (0);
13057 	}
13058 	ASSERT(add_mp != NULL && del_mp != NULL);
13059 	ill->ill_arp_del_mapping_mp = del_mp;
13060 	if (arp_add_mapping_mp != NULL) {
13061 		/* The caller just wants the mblks allocated */
13062 		*arp_add_mapping_mp = add_mp;
13063 	} else {
13064 		/* The caller wants us to send it to arp */
13065 		putnext(ill->ill_rq, add_mp);
13066 	}
13067 	return (0);
13068 }
13069 
13070 /*
13071  * Get the resolver set up for a new interface address.
13072  * (Always called as writer.)
13073  * Called both for IPv4 and IPv6 interfaces,
13074  * though it only sets up the resolver for v6
13075  * if it's an xresolv interface (one using an external resolver).
13076  * Honors ILLF_NOARP.
13077  * The boolean value arp_just_publish, if B_TRUE, indicates that
13078  * it only needs to send an AR_ENTRY_ADD message up to ARP for
13079  * IPv4 interfaces. Currently, B_TRUE is only set when this
13080  * function is called by ip_rput_dlpi_writer() to handle
13081  * asynchronous hardware address change notification.
13082  * Returns error on failure.
13083  */
13084 int
13085 ipif_resolver_up(ipif_t *ipif, boolean_t arp_just_publish)
13086 {
13087 	caddr_t	addr;
13088 	mblk_t	*arp_up_mp = NULL;
13089 	mblk_t	*arp_down_mp = NULL;
13090 	mblk_t	*arp_add_mp = NULL;
13091 	mblk_t	*arp_del_mp = NULL;
13092 	mblk_t	*arp_add_mapping_mp = NULL;
13093 	mblk_t	*arp_del_mapping_mp = NULL;
13094 	ill_t	*ill = ipif->ipif_ill;
13095 	uchar_t	*area_p = NULL;
13096 	uchar_t	*ared_p = NULL;
13097 	int	err = ENOMEM;
13098 
13099 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13100 	    ipif->ipif_ill->ill_name, ipif->ipif_id,
13101 	    (uint_t)ipif->ipif_flags));
13102 	ASSERT(IAM_WRITER_IPIF(ipif));
13103 
13104 	if ((ill->ill_net_type != IRE_IF_RESOLVER) ||
13105 	    (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))) {
13106 		return (0);
13107 	}
13108 
13109 	if (ill->ill_isv6) {
13110 		/*
13111 		 * External resolver for IPv6
13112 		 */
13113 		ASSERT(!arp_just_publish);
13114 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13115 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13116 			area_p = (uchar_t *)&ip6_area_template;
13117 			ared_p = (uchar_t *)&ip6_ared_template;
13118 		}
13119 	} else {
13120 		/*
13121 		 * IPv4 arp case. If the ARP stream has already started
13122 		 * closing, fail this request for ARP bringup. Else
13123 		 * record the fact that an ARP bringup is pending.
13124 		 */
13125 		mutex_enter(&ill->ill_lock);
13126 		if (ill->ill_arp_closing) {
13127 			mutex_exit(&ill->ill_lock);
13128 			err = EINVAL;
13129 			goto failed;
13130 		} else {
13131 			if (ill->ill_ipif_up_count == 0)
13132 				ill->ill_arp_bringup_pending = 1;
13133 			mutex_exit(&ill->ill_lock);
13134 		}
13135 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13136 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13137 			area_p = (uchar_t *)&ip_area_template;
13138 			ared_p = (uchar_t *)&ip_ared_template;
13139 		}
13140 	}
13141 
13142 	/*
13143 	 * Add an entry for the local address in ARP only if it
13144 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13145 	 */
13146 	if (((ipif->ipif_flags & IPIF_UNNUMBERED) == 0) && area_p != NULL) {
13147 		/* Now ask ARP to publish our address. */
13148 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13149 		if (arp_add_mp == NULL)
13150 			goto failed;
13151 		if (arp_just_publish) {
13152 			/*
13153 			 * Copy the new hardware address and length into
13154 			 * arp_add_mp to be sent to ARP.
13155 			 */
13156 			area_t *area = (area_t *)arp_add_mp->b_rptr;
13157 			area->area_hw_addr_length =
13158 			    ill->ill_phys_addr_length;
13159 			bcopy((char *)ill->ill_phys_addr,
13160 			    ((char *)area + area->area_hw_addr_offset),
13161 			    area->area_hw_addr_length);
13162 		}
13163 
13164 		((area_t *)arp_add_mp->b_rptr)->area_flags =
13165 		    ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR;
13166 
13167 		if (arp_just_publish)
13168 			goto arp_setup_multicast;
13169 
13170 		/*
13171 		 * Allocate an ARP deletion message so we know we can tell ARP
13172 		 * when the interface goes down.
13173 		 */
13174 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13175 		if (arp_del_mp == NULL)
13176 			goto failed;
13177 
13178 	} else {
13179 		if (arp_just_publish)
13180 			goto done;
13181 	}
13182 	/*
13183 	 * Need to bring up ARP or setup multicast mapping only
13184 	 * when the first interface is coming UP.
13185 	 */
13186 	if (ill->ill_ipif_up_count != 0)
13187 		goto done;
13188 
13189 	/*
13190 	 * Allocate an ARP down message (to be saved) and an ARP up
13191 	 * message.
13192 	 */
13193 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13194 	if (arp_down_mp == NULL)
13195 		goto failed;
13196 
13197 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13198 	if (arp_up_mp == NULL)
13199 		goto failed;
13200 
13201 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13202 		goto done;
13203 
13204 arp_setup_multicast:
13205 	/*
13206 	 * Setup the multicast mappings. This function initializes
13207 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13208 	 * IPv6.
13209 	 */
13210 	if (!ill->ill_isv6) {
13211 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13212 		if (err != 0)
13213 			goto failed;
13214 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13215 		ASSERT(arp_add_mapping_mp != NULL);
13216 	}
13217 
13218 done:;
13219 	if (arp_del_mp != NULL) {
13220 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13221 		ipif->ipif_arp_del_mp = arp_del_mp;
13222 	}
13223 	if (arp_down_mp != NULL) {
13224 		ASSERT(ill->ill_arp_down_mp == NULL);
13225 		ill->ill_arp_down_mp = arp_down_mp;
13226 	}
13227 	if (arp_del_mapping_mp != NULL) {
13228 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13229 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13230 	}
13231 	if (arp_up_mp != NULL) {
13232 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13233 			ipif->ipif_ill->ill_name, ipif->ipif_id));
13234 		putnext(ill->ill_rq, arp_up_mp);
13235 	}
13236 	if (arp_add_mp != NULL) {
13237 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13238 			ipif->ipif_ill->ill_name, ipif->ipif_id));
13239 		putnext(ill->ill_rq, arp_add_mp);
13240 	}
13241 	if (arp_add_mapping_mp != NULL) {
13242 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13243 			ipif->ipif_ill->ill_name, ipif->ipif_id));
13244 		putnext(ill->ill_rq, arp_add_mapping_mp);
13245 	}
13246 	if (arp_just_publish)
13247 		return (0);
13248 
13249 	if (ill->ill_flags & ILLF_NOARP)
13250 		err = ill_arp_off(ill);
13251 	else
13252 		err = ill_arp_on(ill);
13253 	if (err) {
13254 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13255 		freemsg(ipif->ipif_arp_del_mp);
13256 		if (arp_down_mp != NULL)
13257 			freemsg(ill->ill_arp_down_mp);
13258 		if (ill->ill_arp_del_mapping_mp != NULL)
13259 			freemsg(ill->ill_arp_del_mapping_mp);
13260 		ipif->ipif_arp_del_mp = NULL;
13261 		ill->ill_arp_down_mp = NULL;
13262 		ill->ill_arp_del_mapping_mp = NULL;
13263 		return (err);
13264 	}
13265 	return (ill->ill_ipif_up_count != 0 ? 0 : EINPROGRESS);
13266 
13267 failed:;
13268 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13269 	freemsg(arp_add_mp);
13270 	freemsg(arp_del_mp);
13271 	freemsg(arp_add_mapping_mp);
13272 	freemsg(arp_up_mp);
13273 	freemsg(arp_down_mp);
13274 	ill->ill_arp_bringup_pending = 0;
13275 	return (err);
13276 }
13277 
13278 /*
13279  * Wakeup all threads waiting to enter the ipsq, and sleeping
13280  * on any of the ills in this ipsq. The ill_lock of the ill
13281  * must be held so that waiters don't miss wakeups
13282  */
13283 static void
13284 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
13285 {
13286 	phyint_t *phyint;
13287 
13288 	phyint = ipsq->ipsq_phyint_list;
13289 	while (phyint != NULL) {
13290 		if (phyint->phyint_illv4) {
13291 			if (!caller_holds_lock)
13292 				mutex_enter(&phyint->phyint_illv4->ill_lock);
13293 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13294 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
13295 			if (!caller_holds_lock)
13296 				mutex_exit(&phyint->phyint_illv4->ill_lock);
13297 		}
13298 		if (phyint->phyint_illv6) {
13299 			if (!caller_holds_lock)
13300 				mutex_enter(&phyint->phyint_illv6->ill_lock);
13301 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13302 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
13303 			if (!caller_holds_lock)
13304 				mutex_exit(&phyint->phyint_illv6->ill_lock);
13305 		}
13306 		phyint = phyint->phyint_ipsq_next;
13307 	}
13308 }
13309 
13310 static ipsq_t *
13311 ipsq_create(char *groupname)
13312 {
13313 	ipsq_t	*ipsq;
13314 
13315 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13316 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
13317 	if (ipsq == NULL) {
13318 		return (NULL);
13319 	}
13320 
13321 	if (groupname != NULL)
13322 		(void) strcpy(ipsq->ipsq_name, groupname);
13323 	else
13324 		ipsq->ipsq_name[0] = '\0';
13325 
13326 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
13327 	ipsq->ipsq_flags |= IPSQ_GROUP;
13328 	ipsq->ipsq_next = ipsq_g_head;
13329 	ipsq_g_head = ipsq;
13330 	return (ipsq);
13331 }
13332 
13333 /*
13334  * Return an ipsq correspoding to the groupname. If 'create' is true
13335  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
13336  * uniquely with an IPMP group. However during IPMP groupname operations,
13337  * multiple IPMP groups may be associated with a single ipsq. But no
13338  * IPMP group can be associated with more than 1 ipsq at any time.
13339  * For example
13340  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
13341  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
13342  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
13343  *
13344  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
13345  * status shown below during the execution of the above command.
13346  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
13347  *
13348  * After the completion of the above groupname command we return to the stable
13349  * state shown below.
13350  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
13351  *	hme4			mpk17-85	ipsq2	mpk17-85	1
13352  *
13353  * Because of the above, we don't search based on the ipsq_name since that
13354  * would miss the correct ipsq during certain windows as shown above.
13355  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
13356  * natural state.
13357  */
13358 static ipsq_t *
13359 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq)
13360 {
13361 	ipsq_t	*ipsq;
13362 	int	group_len;
13363 	phyint_t *phyint;
13364 
13365 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
13366 
13367 	group_len = strlen(groupname);
13368 	ASSERT(group_len != 0);
13369 	group_len++;
13370 
13371 	for (ipsq = ipsq_g_head; ipsq != NULL; ipsq = ipsq->ipsq_next) {
13372 		/*
13373 		 * When an ipsq is being split, and ill_split_ipsq
13374 		 * calls this function, we exclude it from being considered.
13375 		 */
13376 		if (ipsq == exclude_ipsq)
13377 			continue;
13378 
13379 		/*
13380 		 * Compare against the ipsq_name. The groupname change happens
13381 		 * in 2 phases. The 1st phase merges the from group into
13382 		 * the to group's ipsq, by calling ill_merge_groups and restarts
13383 		 * the ioctl. The 2nd phase then locates the ipsq again thru
13384 		 * ipsq_name. At this point the phyint_groupname has not been
13385 		 * updated.
13386 		 */
13387 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
13388 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
13389 			/*
13390 			 * Verify that an ipmp groupname is exactly
13391 			 * part of 1 ipsq and is not found in any other
13392 			 * ipsq.
13393 			 */
13394 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq) ==
13395 			    NULL);
13396 			return (ipsq);
13397 		}
13398 
13399 		/*
13400 		 * Comparison against ipsq_name alone is not sufficient.
13401 		 * In the case when groups are currently being
13402 		 * merged, the ipsq could hold other IPMP groups temporarily.
13403 		 * so we walk the phyint list and compare against the
13404 		 * phyint_groupname as well.
13405 		 */
13406 		phyint = ipsq->ipsq_phyint_list;
13407 		while (phyint != NULL) {
13408 			if ((group_len == phyint->phyint_groupname_len) &&
13409 			    (bcmp(phyint->phyint_groupname, groupname,
13410 			    group_len) == 0)) {
13411 				/*
13412 				 * Verify that an ipmp groupname is exactly
13413 				 * part of 1 ipsq and is not found in any other
13414 				 * ipsq.
13415 				 */
13416 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq)
13417 					== NULL);
13418 				return (ipsq);
13419 			}
13420 			phyint = phyint->phyint_ipsq_next;
13421 		}
13422 	}
13423 	if (create)
13424 		ipsq = ipsq_create(groupname);
13425 	return (ipsq);
13426 }
13427 
13428 static void
13429 ipsq_delete(ipsq_t *ipsq)
13430 {
13431 	ipsq_t *nipsq;
13432 	ipsq_t *pipsq = NULL;
13433 
13434 	/*
13435 	 * We don't hold the ipsq lock, but we are sure no new
13436 	 * messages can land up, since the ipsq_refs is zero.
13437 	 * i.e. this ipsq is unnamed and no phyint or phyint group
13438 	 * is associated with this ipsq. (Lookups are based on ill_name
13439 	 * or phyint_group_name)
13440 	 */
13441 	ASSERT(ipsq->ipsq_refs == 0);
13442 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
13443 	ASSERT(ipsq->ipsq_pending_mp == NULL);
13444 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
13445 		/*
13446 		 * This is not the ipsq of an IPMP group.
13447 		 */
13448 		kmem_free(ipsq, sizeof (ipsq_t));
13449 		return;
13450 	}
13451 
13452 	rw_enter(&ill_g_lock, RW_WRITER);
13453 
13454 	/*
13455 	 * Locate the ipsq  before we can remove it from
13456 	 * the singly linked list of ipsq's.
13457 	 */
13458 	for (nipsq = ipsq_g_head; nipsq != NULL; nipsq = nipsq->ipsq_next) {
13459 		if (nipsq == ipsq) {
13460 			break;
13461 		}
13462 		pipsq = nipsq;
13463 	}
13464 
13465 	ASSERT(nipsq == ipsq);
13466 
13467 	/* unlink ipsq from the list */
13468 	if (pipsq != NULL)
13469 		pipsq->ipsq_next = ipsq->ipsq_next;
13470 	else
13471 		ipsq_g_head = ipsq->ipsq_next;
13472 	kmem_free(ipsq, sizeof (ipsq_t));
13473 	rw_exit(&ill_g_lock);
13474 }
13475 
13476 static void
13477 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
13478     queue_t *q)
13479 
13480 {
13481 
13482 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
13483 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
13484 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
13485 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
13486 	ASSERT(current_mp != NULL);
13487 
13488 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
13489 		NEW_OP, NULL);
13490 
13491 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
13492 	    new_ipsq->ipsq_xopq_mphead != NULL);
13493 
13494 	/*
13495 	 * move from old ipsq to the new ipsq.
13496 	 */
13497 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
13498 	if (old_ipsq->ipsq_xopq_mphead != NULL)
13499 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
13500 
13501 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
13502 }
13503 
13504 void
13505 ill_group_cleanup(ill_t *ill)
13506 {
13507 	ill_t *ill_v4;
13508 	ill_t *ill_v6;
13509 	ipif_t *ipif;
13510 
13511 	ill_v4 = ill->ill_phyint->phyint_illv4;
13512 	ill_v6 = ill->ill_phyint->phyint_illv6;
13513 
13514 	if (ill_v4 != NULL) {
13515 		mutex_enter(&ill_v4->ill_lock);
13516 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
13517 		    ipif = ipif->ipif_next) {
13518 			IPIF_UNMARK_MOVING(ipif);
13519 		}
13520 		ill_v4->ill_up_ipifs = B_FALSE;
13521 		mutex_exit(&ill_v4->ill_lock);
13522 	}
13523 
13524 	if (ill_v6 != NULL) {
13525 		mutex_enter(&ill_v6->ill_lock);
13526 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
13527 		    ipif = ipif->ipif_next) {
13528 			IPIF_UNMARK_MOVING(ipif);
13529 		}
13530 		ill_v6->ill_up_ipifs = B_FALSE;
13531 		mutex_exit(&ill_v6->ill_lock);
13532 	}
13533 }
13534 /*
13535  * This function is called when an ill has had a change in its group status
13536  * to bring up all the ipifs that were up before the change.
13537  */
13538 int
13539 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
13540 {
13541 	ipif_t *ipif;
13542 	ill_t *ill_v4;
13543 	ill_t *ill_v6;
13544 	ill_t *from_ill;
13545 	int err = 0;
13546 
13547 
13548 	ASSERT(IAM_WRITER_ILL(ill));
13549 
13550 	/*
13551 	 * Except for ipif_state_flags and ill_state_flags the other
13552 	 * fields of the ipif/ill that are modified below are protected
13553 	 * implicitly since we are a writer. We would have tried to down
13554 	 * even an ipif that was already down, in ill_down_ipifs. So we
13555 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
13556 	 */
13557 	ill_v4 = ill->ill_phyint->phyint_illv4;
13558 	ill_v6 = ill->ill_phyint->phyint_illv6;
13559 	if (ill_v4 != NULL) {
13560 		ill_v4->ill_up_ipifs = B_TRUE;
13561 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
13562 		    ipif = ipif->ipif_next) {
13563 			mutex_enter(&ill_v4->ill_lock);
13564 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
13565 			IPIF_UNMARK_MOVING(ipif);
13566 			mutex_exit(&ill_v4->ill_lock);
13567 			if (ipif->ipif_was_up) {
13568 				if (!(ipif->ipif_flags & IPIF_UP))
13569 					err = ipif_up(ipif, q, mp);
13570 				ipif->ipif_was_up = B_FALSE;
13571 				if (err != 0) {
13572 					/*
13573 					 * Can there be any other error ?
13574 					 */
13575 					ASSERT(err == EINPROGRESS);
13576 					return (err);
13577 				}
13578 			}
13579 		}
13580 		mutex_enter(&ill_v4->ill_lock);
13581 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
13582 		mutex_exit(&ill_v4->ill_lock);
13583 		ill_v4->ill_up_ipifs = B_FALSE;
13584 		if (ill_v4->ill_move_in_progress) {
13585 			ASSERT(ill_v4->ill_move_peer != NULL);
13586 			ill_v4->ill_move_in_progress = B_FALSE;
13587 			from_ill = ill_v4->ill_move_peer;
13588 			from_ill->ill_move_in_progress = B_FALSE;
13589 			from_ill->ill_move_peer = NULL;
13590 			mutex_enter(&from_ill->ill_lock);
13591 			from_ill->ill_state_flags &= ~ILL_CHANGING;
13592 			mutex_exit(&from_ill->ill_lock);
13593 			if (ill_v6 == NULL) {
13594 				if (from_ill->ill_phyint->phyint_flags &
13595 				    PHYI_STANDBY) {
13596 					phyint_inactive(from_ill->ill_phyint);
13597 				}
13598 				if (ill_v4->ill_phyint->phyint_flags &
13599 				    PHYI_STANDBY) {
13600 					phyint_inactive(ill_v4->ill_phyint);
13601 				}
13602 			}
13603 			ill_v4->ill_move_peer = NULL;
13604 		}
13605 	}
13606 
13607 	if (ill_v6 != NULL) {
13608 		ill_v6->ill_up_ipifs = B_TRUE;
13609 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
13610 		    ipif = ipif->ipif_next) {
13611 			mutex_enter(&ill_v6->ill_lock);
13612 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
13613 			IPIF_UNMARK_MOVING(ipif);
13614 			mutex_exit(&ill_v6->ill_lock);
13615 			if (ipif->ipif_was_up) {
13616 				if (!(ipif->ipif_flags & IPIF_UP))
13617 					err = ipif_up(ipif, q, mp);
13618 				ipif->ipif_was_up = B_FALSE;
13619 				if (err != 0) {
13620 					/*
13621 					 * Can there be any other error ?
13622 					 */
13623 					ASSERT(err == EINPROGRESS);
13624 					return (err);
13625 				}
13626 			}
13627 		}
13628 		mutex_enter(&ill_v6->ill_lock);
13629 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
13630 		mutex_exit(&ill_v6->ill_lock);
13631 		ill_v6->ill_up_ipifs = B_FALSE;
13632 		if (ill_v6->ill_move_in_progress) {
13633 			ASSERT(ill_v6->ill_move_peer != NULL);
13634 			ill_v6->ill_move_in_progress = B_FALSE;
13635 			from_ill = ill_v6->ill_move_peer;
13636 			from_ill->ill_move_in_progress = B_FALSE;
13637 			from_ill->ill_move_peer = NULL;
13638 			mutex_enter(&from_ill->ill_lock);
13639 			from_ill->ill_state_flags &= ~ILL_CHANGING;
13640 			mutex_exit(&from_ill->ill_lock);
13641 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
13642 				phyint_inactive(from_ill->ill_phyint);
13643 			}
13644 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
13645 				phyint_inactive(ill_v6->ill_phyint);
13646 			}
13647 			ill_v6->ill_move_peer = NULL;
13648 		}
13649 	}
13650 	return (0);
13651 }
13652 
13653 /*
13654  * bring down all the approriate ipifs.
13655  */
13656 /* ARGSUSED */
13657 static void
13658 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
13659 {
13660 	ipif_t *ipif;
13661 
13662 	ASSERT(IAM_WRITER_ILL(ill));
13663 
13664 	/*
13665 	 * Except for ipif_state_flags the other fields of the ipif/ill that
13666 	 * are modified below are protected implicitly since we are a writer
13667 	 */
13668 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13669 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
13670 			continue;
13671 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
13672 			/*
13673 			 * We go through the ipif_down logic even if the ipif
13674 			 * is already down, since routes can be added based
13675 			 * on down ipifs. Going through ipif_down once again
13676 			 * will delete any IREs created based on these routes.
13677 			 */
13678 			if (ipif->ipif_flags & IPIF_UP)
13679 				ipif->ipif_was_up = B_TRUE;
13680 			/*
13681 			 * If called with chk_nofailover true ipif is moving.
13682 			 */
13683 			mutex_enter(&ill->ill_lock);
13684 			if (chk_nofailover) {
13685 				ipif->ipif_state_flags |=
13686 					IPIF_MOVING | IPIF_CHANGING;
13687 			} else {
13688 				ipif->ipif_state_flags |= IPIF_CHANGING;
13689 			}
13690 			mutex_exit(&ill->ill_lock);
13691 			/*
13692 			 * Need to re-create net/subnet bcast ires if
13693 			 * they are dependent on ipif.
13694 			 */
13695 			if (!ipif->ipif_isv6)
13696 				ipif_check_bcast_ires(ipif);
13697 			(void) ipif_logical_down(ipif, NULL, NULL);
13698 			ipif_down_tail(ipif);
13699 			/*
13700 			 * We don't do ipif_multicast_down for IPv4 in
13701 			 * ipif_down. We need to set this so that
13702 			 * ipif_multicast_up will join the
13703 			 * ALLHOSTS_GROUP on to_ill.
13704 			 */
13705 			ipif->ipif_multicast_up = B_FALSE;
13706 		}
13707 	}
13708 }
13709 
13710 #define	IPSQ_INC_REF(ipsq)	{			\
13711 	ASSERT(RW_WRITE_HELD(&ill_g_lock));		\
13712 	(ipsq)->ipsq_refs++;				\
13713 }
13714 
13715 #define	IPSQ_DEC_REF(ipsq)	{			\
13716 	ASSERT(RW_WRITE_HELD(&ill_g_lock));		\
13717 	(ipsq)->ipsq_refs--;				\
13718 	if ((ipsq)->ipsq_refs == 0)				\
13719 		(ipsq)->ipsq_name[0] = '\0'; 		\
13720 }
13721 
13722 /*
13723  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
13724  * new_ipsq.
13725  */
13726 static void
13727 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq)
13728 {
13729 	phyint_t *phyint;
13730 	phyint_t *next_phyint;
13731 
13732 	/*
13733 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
13734 	 * writer and the ill_lock of the ill in question. Also the dest
13735 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
13736 	 */
13737 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13738 
13739 	phyint = cur_ipsq->ipsq_phyint_list;
13740 	cur_ipsq->ipsq_phyint_list = NULL;
13741 	while (phyint != NULL) {
13742 		next_phyint = phyint->phyint_ipsq_next;
13743 		IPSQ_DEC_REF(cur_ipsq);
13744 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
13745 		new_ipsq->ipsq_phyint_list = phyint;
13746 		IPSQ_INC_REF(new_ipsq);
13747 		phyint->phyint_ipsq = new_ipsq;
13748 		phyint = next_phyint;
13749 	}
13750 }
13751 
13752 #define	SPLIT_SUCCESS		0
13753 #define	SPLIT_NOT_NEEDED	1
13754 #define	SPLIT_FAILED		2
13755 
13756 int
13757 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry)
13758 {
13759 	ipsq_t *newipsq = NULL;
13760 
13761 	/*
13762 	 * Assertions denote pre-requisites for changing the ipsq of
13763 	 * a phyint
13764 	 */
13765 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13766 	/*
13767 	 * <ill-phyint> assocs can't change while ill_g_lock
13768 	 * is held as writer. See ill_phyint_reinit()
13769 	 */
13770 	ASSERT(phyint->phyint_illv4 == NULL ||
13771 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13772 	ASSERT(phyint->phyint_illv6 == NULL ||
13773 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13774 
13775 	if ((phyint->phyint_groupname_len !=
13776 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
13777 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
13778 	    phyint->phyint_groupname_len) != 0)) {
13779 		/*
13780 		 * Once we fail in creating a new ipsq due to memory shortage,
13781 		 * don't attempt to create new ipsq again, based on another
13782 		 * phyint, since we want all phyints belonging to an IPMP group
13783 		 * to be in the same ipsq even in the event of mem alloc fails.
13784 		 */
13785 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
13786 		    cur_ipsq);
13787 		if (newipsq == NULL) {
13788 			/* Memory allocation failure */
13789 			return (SPLIT_FAILED);
13790 		} else {
13791 			/* ipsq_refs protected by ill_g_lock (writer) */
13792 			IPSQ_DEC_REF(cur_ipsq);
13793 			phyint->phyint_ipsq = newipsq;
13794 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
13795 			newipsq->ipsq_phyint_list = phyint;
13796 			IPSQ_INC_REF(newipsq);
13797 			return (SPLIT_SUCCESS);
13798 		}
13799 	}
13800 	return (SPLIT_NOT_NEEDED);
13801 }
13802 
13803 /*
13804  * The ill locks of the phyint and the ill_g_lock (writer) must be held
13805  * to do this split
13806  */
13807 static int
13808 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq)
13809 {
13810 	ipsq_t *newipsq;
13811 
13812 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13813 	/*
13814 	 * <ill-phyint> assocs can't change while ill_g_lock
13815 	 * is held as writer. See ill_phyint_reinit()
13816 	 */
13817 
13818 	ASSERT(phyint->phyint_illv4 == NULL ||
13819 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13820 	ASSERT(phyint->phyint_illv6 == NULL ||
13821 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13822 
13823 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
13824 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
13825 		/*
13826 		 * ipsq_init failed due to no memory
13827 		 * caller will use the same ipsq
13828 		 */
13829 		return (SPLIT_FAILED);
13830 	}
13831 
13832 	/* ipsq_ref is protected by ill_g_lock (writer) */
13833 	IPSQ_DEC_REF(cur_ipsq);
13834 
13835 	/*
13836 	 * This is a new ipsq that is unknown to the world.
13837 	 * So we don't need to hold ipsq_lock,
13838 	 */
13839 	newipsq = phyint->phyint_ipsq;
13840 	newipsq->ipsq_writer = NULL;
13841 	newipsq->ipsq_reentry_cnt--;
13842 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
13843 #ifdef ILL_DEBUG
13844 	newipsq->ipsq_depth = 0;
13845 #endif
13846 
13847 	return (SPLIT_SUCCESS);
13848 }
13849 
13850 /*
13851  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
13852  * ipsq's representing their individual groups or themselves. Return
13853  * whether split needs to be retried again later.
13854  */
13855 static boolean_t
13856 ill_split_ipsq(ipsq_t *cur_ipsq)
13857 {
13858 	phyint_t *phyint;
13859 	phyint_t *next_phyint;
13860 	int	error;
13861 	boolean_t need_retry = B_FALSE;
13862 
13863 	phyint = cur_ipsq->ipsq_phyint_list;
13864 	cur_ipsq->ipsq_phyint_list = NULL;
13865 	while (phyint != NULL) {
13866 		next_phyint = phyint->phyint_ipsq_next;
13867 		/*
13868 		 * 'created' will tell us whether the callee actually
13869 		 * created an ipsq. Lack of memory may force the callee
13870 		 * to return without creating an ipsq.
13871 		 */
13872 		if (phyint->phyint_groupname == NULL) {
13873 			error = ill_split_to_own_ipsq(phyint, cur_ipsq);
13874 		} else {
13875 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
13876 					need_retry);
13877 		}
13878 
13879 		switch (error) {
13880 		case SPLIT_FAILED:
13881 			need_retry = B_TRUE;
13882 			/* FALLTHRU */
13883 		case SPLIT_NOT_NEEDED:
13884 			/*
13885 			 * Keep it on the list.
13886 			 */
13887 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
13888 			cur_ipsq->ipsq_phyint_list = phyint;
13889 			break;
13890 		case SPLIT_SUCCESS:
13891 			break;
13892 		default:
13893 			ASSERT(0);
13894 		}
13895 
13896 		phyint = next_phyint;
13897 	}
13898 	return (need_retry);
13899 }
13900 
13901 /*
13902  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
13903  * and return the ills in the list. This list will be
13904  * needed to unlock all the ills later on by the caller.
13905  * The <ill-ipsq> associations could change between the
13906  * lock and unlock. Hence the unlock can't traverse the
13907  * ipsq to get the list of ills.
13908  */
13909 static int
13910 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
13911 {
13912 	int	cnt = 0;
13913 	phyint_t	*phyint;
13914 
13915 	/*
13916 	 * The caller holds ill_g_lock to ensure that the ill memberships
13917 	 * of the ipsq don't change
13918 	 */
13919 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
13920 
13921 	phyint = ipsq->ipsq_phyint_list;
13922 	while (phyint != NULL) {
13923 		if (phyint->phyint_illv4 != NULL) {
13924 			ASSERT(cnt < list_max);
13925 			list[cnt++] = phyint->phyint_illv4;
13926 		}
13927 		if (phyint->phyint_illv6 != NULL) {
13928 			ASSERT(cnt < list_max);
13929 			list[cnt++] = phyint->phyint_illv6;
13930 		}
13931 		phyint = phyint->phyint_ipsq_next;
13932 	}
13933 	ill_lock_ills(list, cnt);
13934 	return (cnt);
13935 }
13936 
13937 void
13938 ill_lock_ills(ill_t **list, int cnt)
13939 {
13940 	int	i;
13941 
13942 	if (cnt > 1) {
13943 		boolean_t try_again;
13944 		do {
13945 			try_again = B_FALSE;
13946 			for (i = 0; i < cnt - 1; i++) {
13947 				if (list[i] < list[i + 1]) {
13948 					ill_t	*tmp;
13949 
13950 					/* swap the elements */
13951 					tmp = list[i];
13952 					list[i] = list[i + 1];
13953 					list[i + 1] = tmp;
13954 					try_again = B_TRUE;
13955 				}
13956 			}
13957 		} while (try_again);
13958 	}
13959 
13960 	for (i = 0; i < cnt; i++) {
13961 		if (i == 0) {
13962 			if (list[i] != NULL)
13963 				mutex_enter(&list[i]->ill_lock);
13964 			else
13965 				return;
13966 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
13967 			mutex_enter(&list[i]->ill_lock);
13968 		}
13969 	}
13970 }
13971 
13972 void
13973 ill_unlock_ills(ill_t **list, int cnt)
13974 {
13975 	int	i;
13976 
13977 	for (i = 0; i < cnt; i++) {
13978 		if ((i == 0) && (list[i] != NULL)) {
13979 			mutex_exit(&list[i]->ill_lock);
13980 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
13981 			mutex_exit(&list[i]->ill_lock);
13982 		}
13983 	}
13984 }
13985 
13986 /*
13987  * Merge all the ills from 1 ipsq group into another ipsq group.
13988  * The source ipsq group is specified by the ipsq associated with
13989  * 'from_ill'. The destination ipsq group is specified by the ipsq
13990  * associated with 'to_ill' or 'groupname' respectively.
13991  * Note that ipsq itself does not have a reference count mechanism
13992  * and functions don't look up an ipsq and pass it around. Instead
13993  * functions pass around an ill or groupname, and the ipsq is looked
13994  * up from the ill or groupname and the required operation performed
13995  * atomically with the lookup on the ipsq.
13996  */
13997 static int
13998 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
13999     queue_t *q)
14000 {
14001 	ipsq_t *old_ipsq;
14002 	ipsq_t *new_ipsq;
14003 	ill_t	**ill_list;
14004 	int	cnt;
14005 	size_t	ill_list_size;
14006 	boolean_t became_writer_on_new_sq = B_FALSE;
14007 
14008 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14009 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14010 
14011 	/*
14012 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14013 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14014 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14015 	 */
14016 	rw_enter(&ill_g_lock, RW_WRITER);
14017 
14018 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14019 	if (groupname != NULL)
14020 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL);
14021 	else {
14022 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14023 	}
14024 
14025 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14026 
14027 	/*
14028 	 * both groups are on the same ipsq.
14029 	 */
14030 	if (old_ipsq == new_ipsq) {
14031 		rw_exit(&ill_g_lock);
14032 		return (0);
14033 	}
14034 
14035 	cnt = old_ipsq->ipsq_refs << 1;
14036 	ill_list_size = cnt * sizeof (ill_t *);
14037 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14038 	if (ill_list == NULL) {
14039 		rw_exit(&ill_g_lock);
14040 		return (ENOMEM);
14041 	}
14042 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14043 
14044 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14045 	mutex_enter(&new_ipsq->ipsq_lock);
14046 	if ((new_ipsq->ipsq_writer == NULL &&
14047 		new_ipsq->ipsq_current_ipif == NULL) ||
14048 	    (new_ipsq->ipsq_writer == curthread)) {
14049 		new_ipsq->ipsq_writer = curthread;
14050 		new_ipsq->ipsq_reentry_cnt++;
14051 		became_writer_on_new_sq = B_TRUE;
14052 	}
14053 
14054 	/*
14055 	 * We are holding ill_g_lock as writer and all the ill locks of
14056 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14057 	 * message can land up on the old ipsq even though we don't hold the
14058 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14059 	 */
14060 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14061 
14062 	/*
14063 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14064 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14065 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14066 	 */
14067 	ill_merge_ipsq(old_ipsq, new_ipsq);
14068 
14069 	/*
14070 	 * Mark the new ipsq as needing a split since it is currently
14071 	 * being shared by more than 1 IPMP group. The split will
14072 	 * occur at the end of ipsq_exit
14073 	 */
14074 	new_ipsq->ipsq_split = B_TRUE;
14075 
14076 	/* Now release all the locks */
14077 	mutex_exit(&new_ipsq->ipsq_lock);
14078 	ill_unlock_ills(ill_list, cnt);
14079 	rw_exit(&ill_g_lock);
14080 
14081 	kmem_free(ill_list, ill_list_size);
14082 
14083 	/*
14084 	 * If we succeeded in becoming writer on the new ipsq, then
14085 	 * drain the new ipsq and start processing  all enqueued messages
14086 	 * including the current ioctl we are processing which is either
14087 	 * a set groupname or failover/failback.
14088 	 */
14089 	if (became_writer_on_new_sq)
14090 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
14091 
14092 	/*
14093 	 * syncq has been changed and all the messages have been moved.
14094 	 */
14095 	mutex_enter(&old_ipsq->ipsq_lock);
14096 	old_ipsq->ipsq_current_ipif = NULL;
14097 	mutex_exit(&old_ipsq->ipsq_lock);
14098 	return (EINPROGRESS);
14099 }
14100 
14101 /*
14102  * Delete and add the loopback copy and non-loopback copy of
14103  * the BROADCAST ire corresponding to ill and addr. Used to
14104  * group broadcast ires together when ill becomes part of
14105  * a group.
14106  *
14107  * This function is also called when ill is leaving the group
14108  * so that the ires belonging to the group gets re-grouped.
14109  */
14110 static void
14111 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14112 {
14113 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14114 	ire_t **ire_ptpn = &ire_head;
14115 
14116 	/*
14117 	 * The loopback and non-loopback IREs are inserted in the order in which
14118 	 * they're found, on the basis that they are correctly ordered (loopback
14119 	 * first).
14120 	 */
14121 	for (;;) {
14122 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14123 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL);
14124 		if (ire == NULL)
14125 			break;
14126 
14127 		/*
14128 		 * we are passing in KM_SLEEP because it is not easy to
14129 		 * go back to a sane state in case of memory failure.
14130 		 */
14131 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14132 		ASSERT(nire != NULL);
14133 		bzero(nire, sizeof (ire_t));
14134 		/*
14135 		 * Don't use ire_max_frag directly since we don't
14136 		 * hold on to 'ire' until we add the new ire 'nire' and
14137 		 * we don't want the new ire to have a dangling reference
14138 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14139 		 * be in sync with the ipif_mtu of the associate ipif.
14140 		 * For eg. this happens as a result of SIOCSLIFNAME,
14141 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14142 		 * the driver. A change in ire_max_frag triggered as
14143 		 * as a result of path mtu discovery, or due to an
14144 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14145 		 * route change -mtu command does not apply to broadcast ires.
14146 		 *
14147 		 * XXX We need a recovery strategy here if ire_init fails
14148 		 */
14149 		if (ire_init(nire,
14150 		    (uchar_t *)&ire->ire_addr,
14151 		    (uchar_t *)&ire->ire_mask,
14152 		    (uchar_t *)&ire->ire_src_addr,
14153 		    (uchar_t *)&ire->ire_gateway_addr,
14154 		    (uchar_t *)&ire->ire_in_src_addr,
14155 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
14156 			&ire->ire_ipif->ipif_mtu,
14157 		    ire->ire_fp_mp,
14158 		    ire->ire_rfq,
14159 		    ire->ire_stq,
14160 		    ire->ire_type,
14161 		    ire->ire_dlureq_mp,
14162 		    ire->ire_ipif,
14163 		    ire->ire_in_ill,
14164 		    ire->ire_cmask,
14165 		    ire->ire_phandle,
14166 		    ire->ire_ihandle,
14167 		    ire->ire_flags,
14168 		    &ire->ire_uinfo,
14169 		    NULL,
14170 		    NULL) == NULL) {
14171 			cmn_err(CE_PANIC, "ire_init() failed");
14172 		}
14173 		ire_delete(ire);
14174 		ire_refrele(ire);
14175 
14176 		/*
14177 		 * The newly created IREs are inserted at the tail of the list
14178 		 * starting with ire_head. As we've just allocated them no one
14179 		 * knows about them so it's safe.
14180 		 */
14181 		*ire_ptpn = nire;
14182 		ire_ptpn = &nire->ire_next;
14183 	}
14184 
14185 	for (nire = ire_head; nire != NULL; nire = nire_next) {
14186 		int error;
14187 		ire_t *oire;
14188 		/* unlink the IRE from our list before calling ire_add() */
14189 		nire_next = nire->ire_next;
14190 		nire->ire_next = NULL;
14191 
14192 		/* ire_add adds the ire at the right place in the list */
14193 		oire = nire;
14194 		error = ire_add(&nire, NULL, NULL, NULL);
14195 		ASSERT(error == 0);
14196 		ASSERT(oire == nire);
14197 		ire_refrele(nire);	/* Held in ire_add */
14198 	}
14199 }
14200 
14201 /*
14202  * This function is usually called when an ill is inserted in
14203  * a group and all the ipifs are already UP. As all the ipifs
14204  * are already UP, the broadcast ires have already been created
14205  * and been inserted. But, ire_add_v4 would not have grouped properly.
14206  * We need to re-group for the benefit of ip_wput_ire which
14207  * expects BROADCAST ires to be grouped properly to avoid sending
14208  * more than one copy of the broadcast packet per group.
14209  *
14210  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
14211  *	  because when ipif_up_done ends up calling this, ires have
14212  *        already been added before illgrp_insert i.e before ill_group
14213  *	  has been initialized.
14214  */
14215 static void
14216 ill_group_bcast_for_xmit(ill_t *ill)
14217 {
14218 	ill_group_t *illgrp;
14219 	ipif_t *ipif;
14220 	ipaddr_t addr;
14221 	ipaddr_t net_mask;
14222 	ipaddr_t subnet_netmask;
14223 
14224 	illgrp = ill->ill_group;
14225 
14226 	/*
14227 	 * This function is called even when an ill is deleted from
14228 	 * the group. Hence, illgrp could be null.
14229 	 */
14230 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
14231 		return;
14232 
14233 	/*
14234 	 * Delete all the BROADCAST ires matching this ill and add
14235 	 * them back. This time, ire_add_v4 should take care of
14236 	 * grouping them with others because ill is part of the
14237 	 * group.
14238 	 */
14239 	ill_bcast_delete_and_add(ill, 0);
14240 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
14241 
14242 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14243 
14244 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14245 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14246 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14247 		} else {
14248 			net_mask = htonl(IN_CLASSA_NET);
14249 		}
14250 		addr = net_mask & ipif->ipif_subnet;
14251 		ill_bcast_delete_and_add(ill, addr);
14252 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
14253 
14254 		subnet_netmask = ipif->ipif_net_mask;
14255 		addr = ipif->ipif_subnet;
14256 		ill_bcast_delete_and_add(ill, addr);
14257 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
14258 	}
14259 }
14260 
14261 /*
14262  * This function is called from illgrp_delete when ill is being deleted
14263  * from the group.
14264  *
14265  * As ill is not there in the group anymore, any address belonging
14266  * to this ill should be cleared of IRE_MARK_NORECV.
14267  */
14268 static void
14269 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
14270 {
14271 	ire_t *ire;
14272 	irb_t *irb;
14273 
14274 	ASSERT(ill->ill_group == NULL);
14275 
14276 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14277 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL);
14278 
14279 	if (ire != NULL) {
14280 		/*
14281 		 * IPMP and plumbing operations are serialized on the ipsq, so
14282 		 * no one will insert or delete a broadcast ire under our feet.
14283 		 */
14284 		irb = ire->ire_bucket;
14285 		rw_enter(&irb->irb_lock, RW_READER);
14286 		ire_refrele(ire);
14287 
14288 		for (; ire != NULL; ire = ire->ire_next) {
14289 			if (ire->ire_addr != addr)
14290 				break;
14291 			if (ire_to_ill(ire) != ill)
14292 				continue;
14293 
14294 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
14295 			ire->ire_marks &= ~IRE_MARK_NORECV;
14296 		}
14297 		rw_exit(&irb->irb_lock);
14298 	}
14299 }
14300 
14301 /*
14302  * This function must be called only after the broadcast ires
14303  * have been grouped together. For a given address addr, nominate
14304  * only one of the ires whose interface is not FAILED or OFFLINE.
14305  *
14306  * This is also called when an ipif goes down, so that we can nominate
14307  * a different ire with the same address for receiving.
14308  */
14309 static void
14310 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr)
14311 {
14312 	irb_t *irb;
14313 	ire_t *ire;
14314 	ire_t *ire1;
14315 	ire_t *save_ire;
14316 	ire_t **irep = NULL;
14317 	boolean_t first = B_TRUE;
14318 	ire_t *clear_ire = NULL;
14319 	ire_t *start_ire = NULL;
14320 	ire_t	*new_lb_ire;
14321 	ire_t	*new_nlb_ire;
14322 	boolean_t new_lb_ire_used = B_FALSE;
14323 	boolean_t new_nlb_ire_used = B_FALSE;
14324 	uint64_t match_flags;
14325 	uint64_t phyi_flags;
14326 	boolean_t fallback = B_FALSE;
14327 
14328 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
14329 	    NULL, MATCH_IRE_TYPE);
14330 	/*
14331 	 * We may not be able to find some ires if a previous
14332 	 * ire_create failed. This happens when an ipif goes
14333 	 * down and we are unable to create BROADCAST ires due
14334 	 * to memory failure. Thus, we have to check for NULL
14335 	 * below. This should handle the case for LOOPBACK,
14336 	 * POINTOPOINT and interfaces with some POINTOPOINT
14337 	 * logicals for which there are no BROADCAST ires.
14338 	 */
14339 	if (ire == NULL)
14340 		return;
14341 	/*
14342 	 * Currently IRE_BROADCASTS are deleted when an ipif
14343 	 * goes down which runs exclusively. Thus, setting
14344 	 * IRE_MARK_RCVD should not race with ire_delete marking
14345 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
14346 	 * be consistent with other parts of the code that walks
14347 	 * a given bucket.
14348 	 */
14349 	save_ire = ire;
14350 	irb = ire->ire_bucket;
14351 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
14352 	if (new_lb_ire == NULL) {
14353 		ire_refrele(ire);
14354 		return;
14355 	}
14356 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
14357 	if (new_nlb_ire == NULL) {
14358 		ire_refrele(ire);
14359 		kmem_cache_free(ire_cache, new_lb_ire);
14360 		return;
14361 	}
14362 	IRB_REFHOLD(irb);
14363 	rw_enter(&irb->irb_lock, RW_WRITER);
14364 	/*
14365 	 * Get to the first ire matching the address and the
14366 	 * group. If the address does not match we are done
14367 	 * as we could not find the IRE. If the address matches
14368 	 * we should get to the first one matching the group.
14369 	 */
14370 	while (ire != NULL) {
14371 		if (ire->ire_addr != addr ||
14372 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
14373 			break;
14374 		}
14375 		ire = ire->ire_next;
14376 	}
14377 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
14378 	start_ire = ire;
14379 redo:
14380 	while (ire != NULL && ire->ire_addr == addr &&
14381 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
14382 		/*
14383 		 * The first ire for any address within a group
14384 		 * should always be the one with IRE_MARK_NORECV cleared
14385 		 * so that ip_wput_ire can avoid searching for one.
14386 		 * Note down the insertion point which will be used
14387 		 * later.
14388 		 */
14389 		if (first && (irep == NULL))
14390 			irep = ire->ire_ptpn;
14391 		/*
14392 		 * PHYI_FAILED is set when the interface fails.
14393 		 * This interface might have become good, but the
14394 		 * daemon has not yet detected. We should still
14395 		 * not receive on this. PHYI_OFFLINE should never
14396 		 * be picked as this has been offlined and soon
14397 		 * be removed.
14398 		 */
14399 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
14400 		if (phyi_flags & PHYI_OFFLINE) {
14401 			ire->ire_marks |= IRE_MARK_NORECV;
14402 			ire = ire->ire_next;
14403 			continue;
14404 		}
14405 		if (phyi_flags & match_flags) {
14406 			ire->ire_marks |= IRE_MARK_NORECV;
14407 			ire = ire->ire_next;
14408 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
14409 			    PHYI_INACTIVE) {
14410 				fallback = B_TRUE;
14411 			}
14412 			continue;
14413 		}
14414 		if (first) {
14415 			/*
14416 			 * We will move this to the front of the list later
14417 			 * on.
14418 			 */
14419 			clear_ire = ire;
14420 			ire->ire_marks &= ~IRE_MARK_NORECV;
14421 		} else {
14422 			ire->ire_marks |= IRE_MARK_NORECV;
14423 		}
14424 		first = B_FALSE;
14425 		ire = ire->ire_next;
14426 	}
14427 	/*
14428 	 * If we never nominated anybody, try nominating at least
14429 	 * an INACTIVE, if we found one. Do it only once though.
14430 	 */
14431 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
14432 	    fallback) {
14433 		match_flags = PHYI_FAILED;
14434 		ire = start_ire;
14435 		irep = NULL;
14436 		goto redo;
14437 	}
14438 	ire_refrele(save_ire);
14439 
14440 	/*
14441 	 * irep non-NULL indicates that we entered the while loop
14442 	 * above. If clear_ire is at the insertion point, we don't
14443 	 * have to do anything. clear_ire will be NULL if all the
14444 	 * interfaces are failed.
14445 	 *
14446 	 * We cannot unlink and reinsert the ire at the right place
14447 	 * in the list since there can be other walkers of this bucket.
14448 	 * Instead we delete and recreate the ire
14449 	 */
14450 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
14451 		ire_t *clear_ire_stq = NULL;
14452 		bzero(new_lb_ire, sizeof (ire_t));
14453 		/* XXX We need a recovery strategy here. */
14454 		if (ire_init(new_lb_ire,
14455 		    (uchar_t *)&clear_ire->ire_addr,
14456 		    (uchar_t *)&clear_ire->ire_mask,
14457 		    (uchar_t *)&clear_ire->ire_src_addr,
14458 		    (uchar_t *)&clear_ire->ire_gateway_addr,
14459 		    (uchar_t *)&clear_ire->ire_in_src_addr,
14460 		    &clear_ire->ire_max_frag,
14461 		    clear_ire->ire_fp_mp,
14462 		    clear_ire->ire_rfq,
14463 		    clear_ire->ire_stq,
14464 		    clear_ire->ire_type,
14465 		    clear_ire->ire_dlureq_mp,
14466 		    clear_ire->ire_ipif,
14467 		    clear_ire->ire_in_ill,
14468 		    clear_ire->ire_cmask,
14469 		    clear_ire->ire_phandle,
14470 		    clear_ire->ire_ihandle,
14471 		    clear_ire->ire_flags,
14472 		    &clear_ire->ire_uinfo,
14473 		    NULL,
14474 		    NULL) == NULL)
14475 			cmn_err(CE_PANIC, "ire_init() failed");
14476 		if (clear_ire->ire_stq == NULL) {
14477 			ire_t *ire_next = clear_ire->ire_next;
14478 			if (ire_next != NULL &&
14479 			    ire_next->ire_stq != NULL &&
14480 			    ire_next->ire_addr == clear_ire->ire_addr &&
14481 			    ire_next->ire_ipif->ipif_ill ==
14482 			    clear_ire->ire_ipif->ipif_ill) {
14483 				clear_ire_stq = ire_next;
14484 
14485 				bzero(new_nlb_ire, sizeof (ire_t));
14486 				/* XXX We need a recovery strategy here. */
14487 				if (ire_init(new_nlb_ire,
14488 				    (uchar_t *)&clear_ire_stq->ire_addr,
14489 				    (uchar_t *)&clear_ire_stq->ire_mask,
14490 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
14491 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
14492 				    (uchar_t *)&clear_ire_stq->ire_in_src_addr,
14493 				    &clear_ire_stq->ire_max_frag,
14494 				    clear_ire_stq->ire_fp_mp,
14495 				    clear_ire_stq->ire_rfq,
14496 				    clear_ire_stq->ire_stq,
14497 				    clear_ire_stq->ire_type,
14498 				    clear_ire_stq->ire_dlureq_mp,
14499 				    clear_ire_stq->ire_ipif,
14500 				    clear_ire_stq->ire_in_ill,
14501 				    clear_ire_stq->ire_cmask,
14502 				    clear_ire_stq->ire_phandle,
14503 				    clear_ire_stq->ire_ihandle,
14504 				    clear_ire_stq->ire_flags,
14505 				    &clear_ire_stq->ire_uinfo,
14506 				    NULL,
14507 				    NULL) == NULL)
14508 					cmn_err(CE_PANIC, "ire_init() failed");
14509 			}
14510 		}
14511 
14512 		/*
14513 		 * Delete the ire. We can't call ire_delete() since
14514 		 * we are holding the bucket lock. We can't release the
14515 		 * bucket lock since we can't allow irep to change. So just
14516 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
14517 		 * ire from the list and do the refrele.
14518 		 */
14519 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
14520 		irb->irb_marks |= IRE_MARK_CONDEMNED;
14521 
14522 		if (clear_ire_stq != NULL) {
14523 			ire_fastpath_list_delete(
14524 			    (ill_t *)clear_ire_stq->ire_stq->q_ptr,
14525 			    clear_ire_stq);
14526 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
14527 		}
14528 
14529 		/*
14530 		 * Also take care of otherfields like ib/ob pkt count
14531 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
14532 		 */
14533 
14534 		/* Add the new ire's. Insert at *irep */
14535 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
14536 		ire1 = *irep;
14537 		if (ire1 != NULL)
14538 			ire1->ire_ptpn = &new_lb_ire->ire_next;
14539 		new_lb_ire->ire_next = ire1;
14540 		/* Link the new one in. */
14541 		new_lb_ire->ire_ptpn = irep;
14542 		membar_producer();
14543 		*irep = new_lb_ire;
14544 		new_lb_ire_used = B_TRUE;
14545 		BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
14546 		new_lb_ire->ire_bucket->irb_ire_cnt++;
14547 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
14548 
14549 		if (clear_ire_stq != NULL) {
14550 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
14551 			irep = &new_lb_ire->ire_next;
14552 			/* Add the new ire. Insert at *irep */
14553 			ire1 = *irep;
14554 			if (ire1 != NULL)
14555 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
14556 			new_nlb_ire->ire_next = ire1;
14557 			/* Link the new one in. */
14558 			new_nlb_ire->ire_ptpn = irep;
14559 			membar_producer();
14560 			*irep = new_nlb_ire;
14561 			new_nlb_ire_used = B_TRUE;
14562 			BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
14563 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
14564 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
14565 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
14566 		}
14567 	}
14568 	rw_exit(&irb->irb_lock);
14569 	if (!new_lb_ire_used)
14570 		kmem_cache_free(ire_cache, new_lb_ire);
14571 	if (!new_nlb_ire_used)
14572 		kmem_cache_free(ire_cache, new_nlb_ire);
14573 	IRB_REFRELE(irb);
14574 }
14575 
14576 /*
14577  * Whenever an ipif goes down we have to renominate a different
14578  * broadcast ire to receive. Whenever an ipif comes up, we need
14579  * to make sure that we have only one nominated to receive.
14580  */
14581 static void
14582 ipif_renominate_bcast(ipif_t *ipif)
14583 {
14584 	ill_t *ill = ipif->ipif_ill;
14585 	ipaddr_t subnet_addr;
14586 	ipaddr_t net_addr;
14587 	ipaddr_t net_mask = 0;
14588 	ipaddr_t subnet_netmask;
14589 	ipaddr_t addr;
14590 	ill_group_t *illgrp;
14591 
14592 	illgrp = ill->ill_group;
14593 	/*
14594 	 * If this is the last ipif going down, it might take
14595 	 * the ill out of the group. In that case ipif_down ->
14596 	 * illgrp_delete takes care of doing the nomination.
14597 	 * ipif_down does not call for this case.
14598 	 */
14599 	ASSERT(illgrp != NULL);
14600 
14601 	/* There could not have been any ires associated with this */
14602 	if (ipif->ipif_subnet == 0)
14603 		return;
14604 
14605 	ill_mark_bcast(illgrp, 0);
14606 	ill_mark_bcast(illgrp, INADDR_BROADCAST);
14607 
14608 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14609 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14610 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14611 	} else {
14612 		net_mask = htonl(IN_CLASSA_NET);
14613 	}
14614 	addr = net_mask & ipif->ipif_subnet;
14615 	ill_mark_bcast(illgrp, addr);
14616 
14617 	net_addr = ~net_mask | addr;
14618 	ill_mark_bcast(illgrp, net_addr);
14619 
14620 	subnet_netmask = ipif->ipif_net_mask;
14621 	addr = ipif->ipif_subnet;
14622 	ill_mark_bcast(illgrp, addr);
14623 
14624 	subnet_addr = ~subnet_netmask | addr;
14625 	ill_mark_bcast(illgrp, subnet_addr);
14626 }
14627 
14628 /*
14629  * Whenever we form or delete ill groups, we need to nominate one set of
14630  * BROADCAST ires for receiving in the group.
14631  *
14632  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
14633  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
14634  *    for ill_ipif_up_count to be non-zero. This is the only case where
14635  *    ill_ipif_up_count is zero and we would still find the ires.
14636  *
14637  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
14638  *    ipif is UP and we just have to do the nomination.
14639  *
14640  * 3) When ill_handoff_responsibility calls us, some ill has been removed
14641  *    from the group. So, we have to do the nomination.
14642  *
14643  * Because of (3), there could be just one ill in the group. But we have
14644  * to nominate still as IRE_MARK_NORCV may have been marked on this.
14645  * Thus, this function does not optimize when there is only one ill as
14646  * it is not correct for (3).
14647  */
14648 static void
14649 ill_nominate_bcast_rcv(ill_group_t *illgrp)
14650 {
14651 	ill_t *ill;
14652 	ipif_t *ipif;
14653 	ipaddr_t subnet_addr;
14654 	ipaddr_t prev_subnet_addr = 0;
14655 	ipaddr_t net_addr;
14656 	ipaddr_t prev_net_addr = 0;
14657 	ipaddr_t net_mask = 0;
14658 	ipaddr_t subnet_netmask;
14659 	ipaddr_t addr;
14660 
14661 	/*
14662 	 * When the last memeber is leaving, there is nothing to
14663 	 * nominate.
14664 	 */
14665 	if (illgrp->illgrp_ill_count == 0) {
14666 		ASSERT(illgrp->illgrp_ill == NULL);
14667 		return;
14668 	}
14669 
14670 	ill = illgrp->illgrp_ill;
14671 	ASSERT(!ill->ill_isv6);
14672 	/*
14673 	 * We assume that ires with same address and belonging to the
14674 	 * same group, has been grouped together. Nominating a *single*
14675 	 * ill in the group for sending and receiving broadcast is done
14676 	 * by making sure that the first BROADCAST ire (which will be
14677 	 * the one returned by ire_ctable_lookup for ip_rput and the
14678 	 * one that will be used in ip_wput_ire) will be the one that
14679 	 * will not have IRE_MARK_NORECV set.
14680 	 *
14681 	 * 1) ip_rput checks and discards packets received on ires marked
14682 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
14683 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
14684 	 *    first ire in the group for every broadcast address in the group.
14685 	 *    ip_rput will accept packets only on the first ire i.e only
14686 	 *    one copy of the ill.
14687 	 *
14688 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
14689 	 *    packet for the whole group. It needs to send out on the ill
14690 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
14691 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
14692 	 *    the copy echoed back on other port where the ire is not marked
14693 	 *    with IRE_MARK_NORECV.
14694 	 *
14695 	 * Note that we just need to have the first IRE either loopback or
14696 	 * non-loopback (either of them may not exist if ire_create failed
14697 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
14698 	 * always hit the first one and hence will always accept one copy.
14699 	 *
14700 	 * We have a broadcast ire per ill for all the unique prefixes
14701 	 * hosted on that ill. As we don't have a way of knowing the
14702 	 * unique prefixes on a given ill and hence in the whole group,
14703 	 * we just call ill_mark_bcast on all the prefixes that exist
14704 	 * in the group. For the common case of one prefix, the code
14705 	 * below optimizes by remebering the last address used for
14706 	 * markng. In the case of multiple prefixes, this will still
14707 	 * optimize depending the order of prefixes.
14708 	 *
14709 	 * The only unique address across the whole group is 0.0.0.0 and
14710 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
14711 	 * the first ire in the bucket for receiving and disables the
14712 	 * others.
14713 	 */
14714 	ill_mark_bcast(illgrp, 0);
14715 	ill_mark_bcast(illgrp, INADDR_BROADCAST);
14716 	for (; ill != NULL; ill = ill->ill_group_next) {
14717 
14718 		for (ipif = ill->ill_ipif; ipif != NULL;
14719 		    ipif = ipif->ipif_next) {
14720 
14721 			if (!(ipif->ipif_flags & IPIF_UP) ||
14722 			    ipif->ipif_subnet == 0) {
14723 				continue;
14724 			}
14725 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14726 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14727 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14728 			} else {
14729 				net_mask = htonl(IN_CLASSA_NET);
14730 			}
14731 			addr = net_mask & ipif->ipif_subnet;
14732 			if (prev_net_addr == 0 || prev_net_addr != addr) {
14733 				ill_mark_bcast(illgrp, addr);
14734 				net_addr = ~net_mask | addr;
14735 				ill_mark_bcast(illgrp, net_addr);
14736 			}
14737 			prev_net_addr = addr;
14738 
14739 			subnet_netmask = ipif->ipif_net_mask;
14740 			addr = ipif->ipif_subnet;
14741 			if (prev_subnet_addr == 0 ||
14742 			    prev_subnet_addr != addr) {
14743 				ill_mark_bcast(illgrp, addr);
14744 				subnet_addr = ~subnet_netmask | addr;
14745 				ill_mark_bcast(illgrp, subnet_addr);
14746 			}
14747 			prev_subnet_addr = addr;
14748 		}
14749 	}
14750 }
14751 
14752 /*
14753  * This function is called while forming ill groups.
14754  *
14755  * Currently, we handle only allmulti groups. We want to join
14756  * allmulti on only one of the ills in the groups. In future,
14757  * when we have link aggregation, we may have to join normal
14758  * multicast groups on multiple ills as switch does inbound load
14759  * balancing. Following are the functions that calls this
14760  * function :
14761  *
14762  * 1) ill_recover_multicast : Interface is coming back UP.
14763  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
14764  *    will call ill_recover_multicast to recover all the multicast
14765  *    groups. We need to make sure that only one member is joined
14766  *    in the ill group.
14767  *
14768  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
14769  *    Somebody is joining allmulti. We need to make sure that only one
14770  *    member is joined in the group.
14771  *
14772  * 3) illgrp_insert : If allmulti has already joined, we need to make
14773  *    sure that only one member is joined in the group.
14774  *
14775  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
14776  *    allmulti who we have nominated. We need to pick someother ill.
14777  *
14778  * 5) illgrp_delete : The ill we nominated is leaving the group,
14779  *    we need to pick a new ill to join the group.
14780  *
14781  * For (1), (2), (5) - we just have to check whether there is
14782  * a good ill joined in the group. If we could not find any ills
14783  * joined the group, we should join.
14784  *
14785  * For (4), the one that was nominated to receive, left the group.
14786  * There could be nobody joined in the group when this function is
14787  * called.
14788  *
14789  * For (3) - we need to explicitly check whether there are multiple
14790  * ills joined in the group.
14791  *
14792  * For simplicity, we don't differentiate any of the above cases. We
14793  * just leave the group if it is joined on any of them and join on
14794  * the first good ill.
14795  */
14796 int
14797 ill_nominate_mcast_rcv(ill_group_t *illgrp)
14798 {
14799 	ilm_t *ilm;
14800 	ill_t *ill;
14801 	ill_t *fallback_inactive_ill = NULL;
14802 	ill_t *fallback_failed_ill = NULL;
14803 	int ret = 0;
14804 
14805 	/*
14806 	 * Leave the allmulti on all the ills and start fresh.
14807 	 */
14808 	for (ill = illgrp->illgrp_ill; ill != NULL;
14809 	    ill = ill->ill_group_next) {
14810 		if (ill->ill_join_allmulti)
14811 			(void) ip_leave_allmulti(ill->ill_ipif);
14812 	}
14813 
14814 	/*
14815 	 * Choose a good ill. Fallback to inactive or failed if
14816 	 * none available. We need to fallback to FAILED in the
14817 	 * case where we have 2 interfaces in a group - where
14818 	 * one of them is failed and another is a good one and
14819 	 * the good one (not marked inactive) is leaving the group.
14820 	 */
14821 	ret = 0;
14822 	for (ill = illgrp->illgrp_ill; ill != NULL;
14823 	    ill = ill->ill_group_next) {
14824 		/* Never pick an offline interface */
14825 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
14826 			continue;
14827 
14828 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
14829 			fallback_failed_ill = ill;
14830 			continue;
14831 		}
14832 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
14833 			fallback_inactive_ill = ill;
14834 			continue;
14835 		}
14836 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14837 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14838 				ret = ip_join_allmulti(ill->ill_ipif);
14839 				/*
14840 				 * ip_join_allmulti can fail because of memory
14841 				 * failures. So, make sure we join at least
14842 				 * on one ill.
14843 				 */
14844 				if (ill->ill_join_allmulti)
14845 					return (0);
14846 			}
14847 		}
14848 	}
14849 	if (ret != 0) {
14850 		/*
14851 		 * If we tried nominating above and failed to do so,
14852 		 * return error. We might have tried multiple times.
14853 		 * But, return the latest error.
14854 		 */
14855 		return (ret);
14856 	}
14857 	if ((ill = fallback_inactive_ill) != NULL) {
14858 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14859 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14860 				ret = ip_join_allmulti(ill->ill_ipif);
14861 				return (ret);
14862 			}
14863 		}
14864 	} else if ((ill = fallback_failed_ill) != NULL) {
14865 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14866 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14867 				ret = ip_join_allmulti(ill->ill_ipif);
14868 				return (ret);
14869 			}
14870 		}
14871 	}
14872 	return (0);
14873 }
14874 
14875 /*
14876  * This function is called from illgrp_delete after it is
14877  * deleted from the group to reschedule responsibilities
14878  * to a different ill.
14879  */
14880 static void
14881 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
14882 {
14883 	ilm_t	*ilm;
14884 	ipif_t	*ipif;
14885 	ipaddr_t subnet_addr;
14886 	ipaddr_t net_addr;
14887 	ipaddr_t net_mask = 0;
14888 	ipaddr_t subnet_netmask;
14889 	ipaddr_t addr;
14890 
14891 	ASSERT(ill->ill_group == NULL);
14892 	/*
14893 	 * Broadcast Responsibility:
14894 	 *
14895 	 * 1. If this ill has been nominated for receiving broadcast
14896 	 * packets, we need to find a new one. Before we find a new
14897 	 * one, we need to re-group the ires that are part of this new
14898 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
14899 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
14900 	 * thing for us.
14901 	 *
14902 	 * 2. If this ill was not nominated for receiving broadcast
14903 	 * packets, we need to clear the IRE_MARK_NORECV flag
14904 	 * so that we continue to send up broadcast packets.
14905 	 */
14906 	if (!ill->ill_isv6) {
14907 		/*
14908 		 * Case 1 above : No optimization here. Just redo the
14909 		 * nomination.
14910 		 */
14911 		ill_group_bcast_for_xmit(ill);
14912 		ill_nominate_bcast_rcv(illgrp);
14913 
14914 		/*
14915 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
14916 		 */
14917 		ill_clear_bcast_mark(ill, 0);
14918 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
14919 
14920 		for (ipif = ill->ill_ipif; ipif != NULL;
14921 		    ipif = ipif->ipif_next) {
14922 
14923 			if (!(ipif->ipif_flags & IPIF_UP) ||
14924 			    ipif->ipif_subnet == 0) {
14925 				continue;
14926 			}
14927 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14928 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14929 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14930 			} else {
14931 				net_mask = htonl(IN_CLASSA_NET);
14932 			}
14933 			addr = net_mask & ipif->ipif_subnet;
14934 			ill_clear_bcast_mark(ill, addr);
14935 
14936 			net_addr = ~net_mask | addr;
14937 			ill_clear_bcast_mark(ill, net_addr);
14938 
14939 			subnet_netmask = ipif->ipif_net_mask;
14940 			addr = ipif->ipif_subnet;
14941 			ill_clear_bcast_mark(ill, addr);
14942 
14943 			subnet_addr = ~subnet_netmask | addr;
14944 			ill_clear_bcast_mark(ill, subnet_addr);
14945 		}
14946 	}
14947 
14948 	/*
14949 	 * Multicast Responsibility.
14950 	 *
14951 	 * If we have joined allmulti on this one, find a new member
14952 	 * in the group to join allmulti. As this ill is already part
14953 	 * of allmulti, we don't have to join on this one.
14954 	 *
14955 	 * If we have not joined allmulti on this one, there is no
14956 	 * responsibility to handoff. But we need to take new
14957 	 * responsibility i.e, join allmulti on this one if we need
14958 	 * to.
14959 	 */
14960 	if (ill->ill_join_allmulti) {
14961 		(void) ill_nominate_mcast_rcv(illgrp);
14962 	} else {
14963 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14964 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14965 				(void) ip_join_allmulti(ill->ill_ipif);
14966 				break;
14967 			}
14968 		}
14969 	}
14970 
14971 	/*
14972 	 * We intentionally do the flushing of IRE_CACHES only matching
14973 	 * on the ill and not on groups. Note that we are already deleted
14974 	 * from the group.
14975 	 *
14976 	 * This will make sure that all IRE_CACHES whose stq is pointing
14977 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
14978 	 * deleted and IRE_CACHES that are not pointing at this ill will
14979 	 * be left alone.
14980 	 */
14981 	if (ill->ill_isv6) {
14982 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
14983 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
14984 	} else {
14985 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
14986 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
14987 	}
14988 
14989 	/*
14990 	 * Some conn may have cached one of the IREs deleted above. By removing
14991 	 * the ire reference, we clean up the extra reference to the ill held in
14992 	 * ire->ire_stq.
14993 	 */
14994 	ipcl_walk(conn_cleanup_stale_ire, NULL);
14995 
14996 	/*
14997 	 * Re-do source address selection for all the members in the
14998 	 * group, if they borrowed source address from one of the ipifs
14999 	 * in this ill.
15000 	 */
15001 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15002 		if (ill->ill_isv6) {
15003 			ipif_update_other_ipifs_v6(ipif, illgrp);
15004 		} else {
15005 			ipif_update_other_ipifs(ipif, illgrp);
15006 		}
15007 	}
15008 }
15009 
15010 /*
15011  * Delete the ill from the group. The caller makes sure that it is
15012  * in a group and it okay to delete from the group. So, we always
15013  * delete here.
15014  */
15015 static void
15016 illgrp_delete(ill_t *ill)
15017 {
15018 	ill_group_t *illgrp;
15019 	ill_group_t *tmpg;
15020 	ill_t *tmp_ill;
15021 
15022 	/*
15023 	 * Reset illgrp_ill_schednext if it was pointing at us.
15024 	 * We need to do this before we set ill_group to NULL.
15025 	 */
15026 	rw_enter(&ill_g_lock, RW_WRITER);
15027 	mutex_enter(&ill->ill_lock);
15028 
15029 	illgrp_reset_schednext(ill);
15030 
15031 	illgrp = ill->ill_group;
15032 
15033 	/* Delete the ill from illgrp. */
15034 	if (illgrp->illgrp_ill == ill) {
15035 		illgrp->illgrp_ill = ill->ill_group_next;
15036 	} else {
15037 		tmp_ill = illgrp->illgrp_ill;
15038 		while (tmp_ill->ill_group_next != ill) {
15039 			tmp_ill = tmp_ill->ill_group_next;
15040 			ASSERT(tmp_ill != NULL);
15041 		}
15042 		tmp_ill->ill_group_next = ill->ill_group_next;
15043 	}
15044 	ill->ill_group = NULL;
15045 	ill->ill_group_next = NULL;
15046 
15047 	illgrp->illgrp_ill_count--;
15048 	mutex_exit(&ill->ill_lock);
15049 	rw_exit(&ill_g_lock);
15050 
15051 	/*
15052 	 * As this ill is leaving the group, we need to hand off
15053 	 * the responsibilities to the other ills in the group, if
15054 	 * this ill had some responsibilities.
15055 	 */
15056 
15057 	ill_handoff_responsibility(ill, illgrp);
15058 
15059 	rw_enter(&ill_g_lock, RW_WRITER);
15060 
15061 	if (illgrp->illgrp_ill_count == 0) {
15062 
15063 		ASSERT(illgrp->illgrp_ill == NULL);
15064 		if (ill->ill_isv6) {
15065 			if (illgrp == illgrp_head_v6) {
15066 				illgrp_head_v6 = illgrp->illgrp_next;
15067 			} else {
15068 				tmpg = illgrp_head_v6;
15069 				while (tmpg->illgrp_next != illgrp) {
15070 					tmpg = tmpg->illgrp_next;
15071 					ASSERT(tmpg != NULL);
15072 				}
15073 				tmpg->illgrp_next = illgrp->illgrp_next;
15074 			}
15075 		} else {
15076 			if (illgrp == illgrp_head_v4) {
15077 				illgrp_head_v4 = illgrp->illgrp_next;
15078 			} else {
15079 				tmpg = illgrp_head_v4;
15080 				while (tmpg->illgrp_next != illgrp) {
15081 					tmpg = tmpg->illgrp_next;
15082 					ASSERT(tmpg != NULL);
15083 				}
15084 				tmpg->illgrp_next = illgrp->illgrp_next;
15085 			}
15086 		}
15087 		mutex_destroy(&illgrp->illgrp_lock);
15088 		mi_free(illgrp);
15089 	}
15090 	rw_exit(&ill_g_lock);
15091 
15092 	/*
15093 	 * Even though the ill is out of the group its not necessary
15094 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15095 	 * We will split the ipsq when phyint_groupname is set to NULL.
15096 	 */
15097 
15098 	/*
15099 	 * Send a routing sockets message if we are deleting from
15100 	 * groups with names.
15101 	 */
15102 	if (ill->ill_phyint->phyint_groupname_len != 0)
15103 		ip_rts_ifmsg(ill->ill_ipif);
15104 }
15105 
15106 /*
15107  * Re-do source address selection. This is normally called when
15108  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15109  * ipif comes up.
15110  */
15111 void
15112 ill_update_source_selection(ill_t *ill)
15113 {
15114 	ipif_t *ipif;
15115 
15116 	ASSERT(IAM_WRITER_ILL(ill));
15117 
15118 	if (ill->ill_group != NULL)
15119 		ill = ill->ill_group->illgrp_ill;
15120 
15121 	for (; ill != NULL; ill = ill->ill_group_next) {
15122 		for (ipif = ill->ill_ipif; ipif != NULL;
15123 		    ipif = ipif->ipif_next) {
15124 			if (ill->ill_isv6)
15125 				ipif_recreate_interface_routes_v6(NULL, ipif);
15126 			else
15127 				ipif_recreate_interface_routes(NULL, ipif);
15128 		}
15129 	}
15130 }
15131 
15132 /*
15133  * Insert ill in a group headed by illgrp_head. The caller can either
15134  * pass a groupname in which case we search for a group with the
15135  * same name to insert in or pass a group to insert in. This function
15136  * would only search groups with names.
15137  *
15138  * NOTE : The caller should make sure that there is at least one ipif
15139  *	  UP on this ill so that illgrp_scheduler can pick this ill
15140  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15141  *	  already sent a DL_UNBIND to the driver and we don't want to
15142  *	  send anymore packets. We don't assert for ipif_up_count
15143  *	  to be greater than zero, because ipif_up_done wants to call
15144  *	  this function before bumping up the ipif_up_count. See
15145  *	  ipif_up_done() for details.
15146  */
15147 int
15148 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
15149     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
15150 {
15151 	ill_group_t *illgrp;
15152 	ill_t *prev_ill;
15153 	phyint_t *phyi;
15154 
15155 	ASSERT(ill->ill_group == NULL);
15156 
15157 	rw_enter(&ill_g_lock, RW_WRITER);
15158 	mutex_enter(&ill->ill_lock);
15159 
15160 	if (groupname != NULL) {
15161 		/*
15162 		 * Look for a group with a matching groupname to insert.
15163 		 */
15164 		for (illgrp = *illgrp_head; illgrp != NULL;
15165 		    illgrp = illgrp->illgrp_next) {
15166 
15167 			ill_t *tmp_ill;
15168 
15169 			/*
15170 			 * If we have an ill_group_t in the list which has
15171 			 * no ill_t assigned then we must be in the process of
15172 			 * removing this group. We skip this as illgrp_delete()
15173 			 * will remove it from the list.
15174 			 */
15175 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
15176 				ASSERT(illgrp->illgrp_ill_count == 0);
15177 				continue;
15178 			}
15179 
15180 			ASSERT(tmp_ill->ill_phyint != NULL);
15181 			phyi = tmp_ill->ill_phyint;
15182 			/*
15183 			 * Look at groups which has names only.
15184 			 */
15185 			if (phyi->phyint_groupname_len == 0)
15186 				continue;
15187 			/*
15188 			 * Names are stored in the phyint common to both
15189 			 * IPv4 and IPv6.
15190 			 */
15191 			if (mi_strcmp(phyi->phyint_groupname,
15192 			    groupname) == 0) {
15193 				break;
15194 			}
15195 		}
15196 	} else {
15197 		/*
15198 		 * If the caller passes in a NULL "grp_to_insert", we
15199 		 * allocate one below and insert this singleton.
15200 		 */
15201 		illgrp = grp_to_insert;
15202 	}
15203 
15204 	ill->ill_group_next = NULL;
15205 
15206 	if (illgrp == NULL) {
15207 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
15208 		if (illgrp == NULL) {
15209 			return (ENOMEM);
15210 		}
15211 		illgrp->illgrp_next = *illgrp_head;
15212 		*illgrp_head = illgrp;
15213 		illgrp->illgrp_ill = ill;
15214 		illgrp->illgrp_ill_count = 1;
15215 		ill->ill_group = illgrp;
15216 		/*
15217 		 * Used in illgrp_scheduler to protect multiple threads
15218 		 * from traversing the list.
15219 		 */
15220 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
15221 	} else {
15222 		ASSERT(ill->ill_net_type ==
15223 		    illgrp->illgrp_ill->ill_net_type);
15224 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
15225 
15226 		/* Insert ill at tail of this group */
15227 		prev_ill = illgrp->illgrp_ill;
15228 		while (prev_ill->ill_group_next != NULL)
15229 			prev_ill = prev_ill->ill_group_next;
15230 		prev_ill->ill_group_next = ill;
15231 		ill->ill_group = illgrp;
15232 		illgrp->illgrp_ill_count++;
15233 		/*
15234 		 * Inherit group properties. Currently only forwarding
15235 		 * is the property we try to keep the same with all the
15236 		 * ills. When there are more, we will abstract this into
15237 		 * a function.
15238 		 */
15239 		ill->ill_flags &= ~ILLF_ROUTER;
15240 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
15241 	}
15242 	mutex_exit(&ill->ill_lock);
15243 	rw_exit(&ill_g_lock);
15244 
15245 	/*
15246 	 * 1) When ipif_up_done() calls this function, ipif_up_count
15247 	 *    may be zero as it has not yet been bumped. But the ires
15248 	 *    have already been added. So, we do the nomination here
15249 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
15250 	 *    for ill_ipif_up_count != 0. Thus we don't check for
15251 	 *    ill_ipif_up_count here while nominating broadcast ires for
15252 	 *    receive.
15253 	 *
15254 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
15255 	 *    to group them properly as ire_add() has already happened
15256 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
15257 	 *    case, we need to do it here anyway.
15258 	 */
15259 	if (!ill->ill_isv6) {
15260 		ill_group_bcast_for_xmit(ill);
15261 		ill_nominate_bcast_rcv(illgrp);
15262 	}
15263 
15264 	if (!ipif_is_coming_up) {
15265 		/*
15266 		 * When ipif_up_done() calls this function, the multicast
15267 		 * groups have not been joined yet. So, there is no point in
15268 		 * nomination. ip_join_allmulti will handle groups when
15269 		 * ill_recover_multicast is called from ipif_up_done() later.
15270 		 */
15271 		(void) ill_nominate_mcast_rcv(illgrp);
15272 		/*
15273 		 * ipif_up_done calls ill_update_source_selection
15274 		 * anyway. Moreover, we don't want to re-create
15275 		 * interface routes while ipif_up_done() still has reference
15276 		 * to them. Refer to ipif_up_done() for more details.
15277 		 */
15278 		ill_update_source_selection(ill);
15279 	}
15280 
15281 	/*
15282 	 * Send a routing sockets message if we are inserting into
15283 	 * groups with names.
15284 	 */
15285 	if (groupname != NULL)
15286 		ip_rts_ifmsg(ill->ill_ipif);
15287 	return (0);
15288 }
15289 
15290 /*
15291  * Return the first phyint matching the groupname. There could
15292  * be more than one when there are ill groups.
15293  *
15294  * Needs work: called only from ip_sioctl_groupname
15295  */
15296 static phyint_t *
15297 phyint_lookup_group(char *groupname)
15298 {
15299 	phyint_t *phyi;
15300 
15301 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
15302 	/*
15303 	 * Group names are stored in the phyint - a common structure
15304 	 * to both IPv4 and IPv6.
15305 	 */
15306 	phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
15307 	for (; phyi != NULL;
15308 	    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
15309 	    phyi, AVL_AFTER)) {
15310 		if (phyi->phyint_groupname_len == 0)
15311 			continue;
15312 		ASSERT(phyi->phyint_groupname != NULL);
15313 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
15314 			return (phyi);
15315 	}
15316 	return (NULL);
15317 }
15318 
15319 
15320 
15321 /*
15322  * MT notes on creation and deletion of IPMP groups
15323  *
15324  * Creation and deletion of IPMP groups introduce the need to merge or
15325  * split the associated serialization objects i.e the ipsq's. Normally all
15326  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
15327  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
15328  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
15329  * is a need to change the <ill-ipsq> association and we have to operate on both
15330  * the source and destination IPMP groups. For eg. attempting to set the
15331  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
15332  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
15333  * source or destination IPMP group are mapped to a single ipsq for executing
15334  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
15335  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
15336  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
15337  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
15338  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
15339  * ipsq has to be examined for redoing the <ill-ipsq> associations.
15340  *
15341  * In the above example the ioctl handling code locates the current ipsq of hme0
15342  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
15343  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
15344  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
15345  * the destination ipsq. If the destination ipsq is not busy, it also enters
15346  * the destination ipsq exclusively. Now the actual groupname setting operation
15347  * can proceed. If the destination ipsq is busy, the operation is enqueued
15348  * on the destination (merged) ipsq and will be handled in the unwind from
15349  * ipsq_exit.
15350  *
15351  * To prevent other threads accessing the ill while the group name change is
15352  * in progres, we bring down the ipifs which also removes the ill from the
15353  * group. The group is changed in phyint and when the first ipif on the ill
15354  * is brought up, the ill is inserted into the right IPMP group by
15355  * illgrp_insert.
15356  */
15357 /* ARGSUSED */
15358 int
15359 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15360     ip_ioctl_cmd_t *ipip, void *ifreq)
15361 {
15362 	int i;
15363 	char *tmp;
15364 	int namelen;
15365 	ill_t *ill = ipif->ipif_ill;
15366 	ill_t *ill_v4, *ill_v6;
15367 	int err = 0;
15368 	phyint_t *phyi;
15369 	phyint_t *phyi_tmp;
15370 	struct lifreq *lifr;
15371 	mblk_t	*mp1;
15372 	char *groupname;
15373 	ipsq_t *ipsq;
15374 
15375 	ASSERT(IAM_WRITER_IPIF(ipif));
15376 
15377 	/* Existance verified in ip_wput_nondata */
15378 	mp1 = mp->b_cont->b_cont;
15379 	lifr = (struct lifreq *)mp1->b_rptr;
15380 	groupname = lifr->lifr_groupname;
15381 
15382 	if (ipif->ipif_id != 0)
15383 		return (EINVAL);
15384 
15385 	phyi = ill->ill_phyint;
15386 	ASSERT(phyi != NULL);
15387 
15388 	if (phyi->phyint_flags & PHYI_VIRTUAL)
15389 		return (EINVAL);
15390 
15391 	tmp = groupname;
15392 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
15393 		;
15394 
15395 	if (i == LIFNAMSIZ) {
15396 		/* no null termination */
15397 		return (EINVAL);
15398 	}
15399 
15400 	/*
15401 	 * Calculate the namelen exclusive of the null
15402 	 * termination character.
15403 	 */
15404 	namelen = tmp - groupname;
15405 
15406 	ill_v4 = phyi->phyint_illv4;
15407 	ill_v6 = phyi->phyint_illv6;
15408 
15409 	/*
15410 	 * ILL cannot be part of a usesrc group and and IPMP group at the
15411 	 * same time. No need to grab the ill_g_usesrc_lock here, see
15412 	 * synchronization notes in ip.c
15413 	 */
15414 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
15415 		return (EINVAL);
15416 	}
15417 
15418 	/*
15419 	 * mark the ill as changing.
15420 	 * this should queue all new requests on the syncq.
15421 	 */
15422 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
15423 
15424 	if (ill_v4 != NULL)
15425 		ill_v4->ill_state_flags |= ILL_CHANGING;
15426 	if (ill_v6 != NULL)
15427 		ill_v6->ill_state_flags |= ILL_CHANGING;
15428 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15429 
15430 	if (namelen == 0) {
15431 		/*
15432 		 * Null string means remove this interface from the
15433 		 * existing group.
15434 		 */
15435 		if (phyi->phyint_groupname_len == 0) {
15436 			/*
15437 			 * Never was in a group.
15438 			 */
15439 			err = 0;
15440 			goto done;
15441 		}
15442 
15443 		/*
15444 		 * IPv4 or IPv6 may be temporarily out of the group when all
15445 		 * the ipifs are down. Thus, we need to check for ill_group to
15446 		 * be non-NULL.
15447 		 */
15448 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
15449 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
15450 			mutex_enter(&ill_v4->ill_lock);
15451 			if (!ill_is_quiescent(ill_v4)) {
15452 				/*
15453 				 * ipsq_pending_mp_add will not fail since
15454 				 * connp is NULL
15455 				 */
15456 				(void) ipsq_pending_mp_add(NULL,
15457 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
15458 				mutex_exit(&ill_v4->ill_lock);
15459 				err = EINPROGRESS;
15460 				goto done;
15461 			}
15462 			mutex_exit(&ill_v4->ill_lock);
15463 		}
15464 
15465 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
15466 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
15467 			mutex_enter(&ill_v6->ill_lock);
15468 			if (!ill_is_quiescent(ill_v6)) {
15469 				(void) ipsq_pending_mp_add(NULL,
15470 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
15471 				mutex_exit(&ill_v6->ill_lock);
15472 				err = EINPROGRESS;
15473 				goto done;
15474 			}
15475 			mutex_exit(&ill_v6->ill_lock);
15476 		}
15477 
15478 		rw_enter(&ill_g_lock, RW_WRITER);
15479 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15480 		mutex_enter(&phyi->phyint_lock);
15481 		ASSERT(phyi->phyint_groupname != NULL);
15482 		mi_free(phyi->phyint_groupname);
15483 		phyi->phyint_groupname = NULL;
15484 		phyi->phyint_groupname_len = 0;
15485 		mutex_exit(&phyi->phyint_lock);
15486 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15487 		rw_exit(&ill_g_lock);
15488 		err = ill_up_ipifs(ill, q, mp);
15489 
15490 		/*
15491 		 * set the split flag so that the ipsq can be split
15492 		 */
15493 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15494 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
15495 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15496 
15497 	} else {
15498 		if (phyi->phyint_groupname_len != 0) {
15499 			ASSERT(phyi->phyint_groupname != NULL);
15500 			/* Are we inserting in the same group ? */
15501 			if (mi_strcmp(groupname,
15502 			    phyi->phyint_groupname) == 0) {
15503 				err = 0;
15504 				goto done;
15505 			}
15506 		}
15507 
15508 		rw_enter(&ill_g_lock, RW_READER);
15509 		/*
15510 		 * Merge ipsq for the group's.
15511 		 * This check is here as multiple groups/ills might be
15512 		 * sharing the same ipsq.
15513 		 * If we have to merege than the operation is restarted
15514 		 * on the new ipsq.
15515 		 */
15516 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL);
15517 		if (phyi->phyint_ipsq != ipsq) {
15518 			rw_exit(&ill_g_lock);
15519 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
15520 			goto done;
15521 		}
15522 		/*
15523 		 * Running exclusive on new ipsq.
15524 		 */
15525 
15526 		ASSERT(ipsq != NULL);
15527 		ASSERT(ipsq->ipsq_writer == curthread);
15528 
15529 		/*
15530 		 * Check whether the ill_type and ill_net_type matches before
15531 		 * we allocate any memory so that the cleanup is easier.
15532 		 *
15533 		 * We can't group dissimilar ones as we can't load spread
15534 		 * packets across the group because of potential link-level
15535 		 * header differences.
15536 		 */
15537 		phyi_tmp = phyint_lookup_group(groupname);
15538 		if (phyi_tmp != NULL) {
15539 			if ((ill_v4 != NULL &&
15540 			    phyi_tmp->phyint_illv4 != NULL) &&
15541 			    ((ill_v4->ill_net_type !=
15542 			    phyi_tmp->phyint_illv4->ill_net_type) ||
15543 			    (ill_v4->ill_type !=
15544 			    phyi_tmp->phyint_illv4->ill_type))) {
15545 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15546 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
15547 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15548 				rw_exit(&ill_g_lock);
15549 				return (EINVAL);
15550 			}
15551 			if ((ill_v6 != NULL &&
15552 			    phyi_tmp->phyint_illv6 != NULL) &&
15553 			    ((ill_v6->ill_net_type !=
15554 			    phyi_tmp->phyint_illv6->ill_net_type) ||
15555 			    (ill_v6->ill_type !=
15556 			    phyi_tmp->phyint_illv6->ill_type))) {
15557 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15558 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
15559 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15560 				rw_exit(&ill_g_lock);
15561 				return (EINVAL);
15562 			}
15563 		}
15564 
15565 		rw_exit(&ill_g_lock);
15566 
15567 		/*
15568 		 * bring down all v4 ipifs.
15569 		 */
15570 		if (ill_v4 != NULL) {
15571 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
15572 		}
15573 
15574 		/*
15575 		 * bring down all v6 ipifs.
15576 		 */
15577 		if (ill_v6 != NULL) {
15578 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
15579 		}
15580 
15581 		/*
15582 		 * make sure all ipifs are down and there are no active
15583 		 * references. Call to ipsq_pending_mp_add will not fail
15584 		 * since connp is NULL.
15585 		 */
15586 		if (ill_v4 != NULL) {
15587 			mutex_enter(&ill_v4->ill_lock);
15588 			if (!ill_is_quiescent(ill_v4)) {
15589 				(void) ipsq_pending_mp_add(NULL,
15590 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
15591 				mutex_exit(&ill_v4->ill_lock);
15592 				err = EINPROGRESS;
15593 				goto done;
15594 			}
15595 			mutex_exit(&ill_v4->ill_lock);
15596 		}
15597 
15598 		if (ill_v6 != NULL) {
15599 			mutex_enter(&ill_v6->ill_lock);
15600 			if (!ill_is_quiescent(ill_v6)) {
15601 				(void) ipsq_pending_mp_add(NULL,
15602 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
15603 				mutex_exit(&ill_v6->ill_lock);
15604 				err = EINPROGRESS;
15605 				goto done;
15606 			}
15607 			mutex_exit(&ill_v6->ill_lock);
15608 		}
15609 
15610 		/*
15611 		 * allocate including space for null terminator
15612 		 * before we insert.
15613 		 */
15614 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
15615 		if (tmp == NULL)
15616 			return (ENOMEM);
15617 
15618 		rw_enter(&ill_g_lock, RW_WRITER);
15619 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15620 		mutex_enter(&phyi->phyint_lock);
15621 		if (phyi->phyint_groupname_len != 0) {
15622 			ASSERT(phyi->phyint_groupname != NULL);
15623 			mi_free(phyi->phyint_groupname);
15624 		}
15625 
15626 		/*
15627 		 * setup the new group name.
15628 		 */
15629 		phyi->phyint_groupname = tmp;
15630 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
15631 		phyi->phyint_groupname_len = namelen + 1;
15632 		mutex_exit(&phyi->phyint_lock);
15633 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15634 		rw_exit(&ill_g_lock);
15635 
15636 		err = ill_up_ipifs(ill, q, mp);
15637 	}
15638 
15639 done:
15640 	/*
15641 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
15642 	 */
15643 	if (err != EINPROGRESS) {
15644 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15645 		if (ill_v4 != NULL)
15646 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
15647 		if (ill_v6 != NULL)
15648 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
15649 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15650 	}
15651 	return (err);
15652 }
15653 
15654 /* ARGSUSED */
15655 int
15656 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
15657     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15658 {
15659 	ill_t *ill;
15660 	phyint_t *phyi;
15661 	struct lifreq *lifr;
15662 	mblk_t	*mp1;
15663 
15664 	/* Existence verified in ip_wput_nondata */
15665 	mp1 = mp->b_cont->b_cont;
15666 	lifr = (struct lifreq *)mp1->b_rptr;
15667 	ill = ipif->ipif_ill;
15668 	phyi = ill->ill_phyint;
15669 
15670 	lifr->lifr_groupname[0] = '\0';
15671 	/*
15672 	 * ill_group may be null if all the interfaces
15673 	 * are down. But still, the phyint should always
15674 	 * hold the name.
15675 	 */
15676 	if (phyi->phyint_groupname_len != 0) {
15677 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
15678 		    phyi->phyint_groupname_len);
15679 	}
15680 
15681 	return (0);
15682 }
15683 
15684 
15685 typedef struct conn_move_s {
15686 	ill_t	*cm_from_ill;
15687 	ill_t	*cm_to_ill;
15688 	int	cm_ifindex;
15689 } conn_move_t;
15690 
15691 /*
15692  * ipcl_walk function for moving conn_multicast_ill for a given ill.
15693  */
15694 static void
15695 conn_move(conn_t *connp, caddr_t arg)
15696 {
15697 	conn_move_t *connm;
15698 	int ifindex;
15699 	int i;
15700 	ill_t *from_ill;
15701 	ill_t *to_ill;
15702 	ilg_t *ilg;
15703 	ilm_t *ret_ilm;
15704 
15705 	connm = (conn_move_t *)arg;
15706 	ifindex = connm->cm_ifindex;
15707 	from_ill = connm->cm_from_ill;
15708 	to_ill = connm->cm_to_ill;
15709 
15710 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
15711 
15712 	/* All multicast fields protected by conn_lock */
15713 	mutex_enter(&connp->conn_lock);
15714 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
15715 	if ((connp->conn_outgoing_ill == from_ill) &&
15716 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
15717 		connp->conn_outgoing_ill = to_ill;
15718 		connp->conn_incoming_ill = to_ill;
15719 	}
15720 
15721 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
15722 
15723 	if ((connp->conn_multicast_ill == from_ill) &&
15724 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
15725 		connp->conn_multicast_ill = connm->cm_to_ill;
15726 	}
15727 
15728 	/* Change IP_XMIT_IF associations */
15729 	if ((connp->conn_xmit_if_ill == from_ill) &&
15730 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
15731 		connp->conn_xmit_if_ill = to_ill;
15732 	}
15733 	/*
15734 	 * Change the ilg_ill to point to the new one. This assumes
15735 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
15736 	 * has been told to receive packets on this interface.
15737 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
15738 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
15739 	 * some ilms may not have moved. We check to see whether
15740 	 * the ilms have moved to to_ill. We can't check on from_ill
15741 	 * as in the process of moving, we could have split an ilm
15742 	 * in to two - which has the same orig_ifindex and v6group.
15743 	 *
15744 	 * For IPv4, ilg_ipif moves implicitly. The code below really
15745 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
15746 	 */
15747 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
15748 		ilg = &connp->conn_ilg[i];
15749 		if ((ilg->ilg_ill == from_ill) &&
15750 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
15751 			/* ifindex != 0 indicates failback */
15752 			if (ifindex != 0) {
15753 				connp->conn_ilg[i].ilg_ill = to_ill;
15754 				continue;
15755 			}
15756 
15757 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
15758 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
15759 			    connp->conn_zoneid);
15760 
15761 			if (ret_ilm != NULL)
15762 				connp->conn_ilg[i].ilg_ill = to_ill;
15763 		}
15764 	}
15765 	mutex_exit(&connp->conn_lock);
15766 }
15767 
15768 static void
15769 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
15770 {
15771 	conn_move_t connm;
15772 
15773 	connm.cm_from_ill = from_ill;
15774 	connm.cm_to_ill = to_ill;
15775 	connm.cm_ifindex = ifindex;
15776 
15777 	ipcl_walk(conn_move, (caddr_t)&connm);
15778 }
15779 
15780 /*
15781  * ilm has been moved from from_ill to to_ill.
15782  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
15783  * appropriately.
15784  *
15785  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
15786  *	  the code there de-references ipif_ill to get the ill to
15787  *	  send multicast requests. It does not work as ipif is on its
15788  *	  move and already moved when this function is called.
15789  *	  Thus, we need to use from_ill and to_ill send down multicast
15790  *	  requests.
15791  */
15792 static void
15793 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
15794 {
15795 	ipif_t *ipif;
15796 	ilm_t *ilm;
15797 
15798 	/*
15799 	 * See whether we need to send down DL_ENABMULTI_REQ on
15800 	 * to_ill as ilm has just been added.
15801 	 */
15802 	ASSERT(IAM_WRITER_ILL(to_ill));
15803 	ASSERT(IAM_WRITER_ILL(from_ill));
15804 
15805 	ILM_WALKER_HOLD(to_ill);
15806 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15807 
15808 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
15809 			continue;
15810 		/*
15811 		 * no locks held, ill/ipif cannot dissappear as long
15812 		 * as we are writer.
15813 		 */
15814 		ipif = to_ill->ill_ipif;
15815 		/*
15816 		 * No need to hold any lock as we are the writer and this
15817 		 * can only be changed by a writer.
15818 		 */
15819 		ilm->ilm_is_new = B_FALSE;
15820 
15821 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
15822 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
15823 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
15824 			    "resolver\n"));
15825 			continue;		/* Must be IRE_IF_NORESOLVER */
15826 		}
15827 
15828 
15829 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
15830 			ip1dbg(("ilm_send_multicast_reqs: "
15831 			    "to_ill MULTI_BCAST\n"));
15832 			goto from;
15833 		}
15834 
15835 		if (to_ill->ill_isv6)
15836 			mld_joingroup(ilm);
15837 		else
15838 			igmp_joingroup(ilm);
15839 
15840 		if (to_ill->ill_ipif_up_count == 0) {
15841 			/*
15842 			 * Nobody there. All multicast addresses will be
15843 			 * re-joined when we get the DL_BIND_ACK bringing the
15844 			 * interface up.
15845 			 */
15846 			ilm->ilm_notify_driver = B_FALSE;
15847 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
15848 			goto from;
15849 		}
15850 
15851 		/*
15852 		 * For allmulti address, we want to join on only one interface.
15853 		 * Checking for ilm_numentries_v6 is not correct as you may
15854 		 * find an ilm with zero address on to_ill, but we may not
15855 		 * have nominated to_ill for receiving. Thus, if we have
15856 		 * nominated from_ill (ill_join_allmulti is set), nominate
15857 		 * only if to_ill is not already nominated (to_ill normally
15858 		 * should not have been nominated if "from_ill" has already
15859 		 * been nominated. As we don't prevent failovers from happening
15860 		 * across groups, we don't assert).
15861 		 */
15862 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15863 			/*
15864 			 * There is no need to hold ill locks as we are
15865 			 * writer on both ills and when ill_join_allmulti
15866 			 * is changed the thread is always a writer.
15867 			 */
15868 			if (from_ill->ill_join_allmulti &&
15869 			    !to_ill->ill_join_allmulti) {
15870 				(void) ip_join_allmulti(to_ill->ill_ipif);
15871 			}
15872 		} else if (ilm->ilm_notify_driver) {
15873 
15874 			/*
15875 			 * This is a newly moved ilm so we need to tell the
15876 			 * driver about the new group. There can be more than
15877 			 * one ilm's for the same group in the list each with a
15878 			 * different orig_ifindex. We have to inform the driver
15879 			 * once. In ilm_move_v[4,6] we only set the flag
15880 			 * ilm_notify_driver for the first ilm.
15881 			 */
15882 
15883 			(void) ip_ll_send_enabmulti_req(to_ill,
15884 			    &ilm->ilm_v6addr);
15885 		}
15886 
15887 		ilm->ilm_notify_driver = B_FALSE;
15888 
15889 		/*
15890 		 * See whether we need to send down DL_DISABMULTI_REQ on
15891 		 * from_ill as ilm has just been removed.
15892 		 */
15893 from:
15894 		ipif = from_ill->ill_ipif;
15895 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
15896 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
15897 			ip1dbg(("ilm_send_multicast_reqs: "
15898 			    "from_ill not resolver\n"));
15899 			continue;		/* Must be IRE_IF_NORESOLVER */
15900 		}
15901 
15902 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
15903 			ip1dbg(("ilm_send_multicast_reqs: "
15904 			    "from_ill MULTI_BCAST\n"));
15905 			continue;
15906 		}
15907 
15908 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15909 			if (from_ill->ill_join_allmulti)
15910 			    (void) ip_leave_allmulti(from_ill->ill_ipif);
15911 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
15912 			(void) ip_ll_send_disabmulti_req(from_ill,
15913 		    &ilm->ilm_v6addr);
15914 		}
15915 	}
15916 	ILM_WALKER_RELE(to_ill);
15917 }
15918 
15919 /*
15920  * This function is called when all multicast memberships needs
15921  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
15922  * called only once unlike the IPv4 counterpart where it is called after
15923  * every logical interface is moved. The reason is due to multicast
15924  * memberships are joined using an interface address in IPv4 while in
15925  * IPv6, interface index is used.
15926  */
15927 static void
15928 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
15929 {
15930 	ilm_t	*ilm;
15931 	ilm_t	*ilm_next;
15932 	ilm_t	*new_ilm;
15933 	ilm_t	**ilmp;
15934 	int	count;
15935 	char buf[INET6_ADDRSTRLEN];
15936 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
15937 
15938 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
15939 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
15940 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
15941 
15942 	if (ifindex == 0) {
15943 		/*
15944 		 * Form the solicited node mcast address which is used later.
15945 		 */
15946 		ipif_t *ipif;
15947 
15948 		ipif = from_ill->ill_ipif;
15949 		ASSERT(ipif->ipif_id == 0);
15950 
15951 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
15952 	}
15953 
15954 	ilmp = &from_ill->ill_ilm;
15955 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
15956 		ilm_next = ilm->ilm_next;
15957 
15958 		if (ilm->ilm_flags & ILM_DELETED) {
15959 			ilmp = &ilm->ilm_next;
15960 			continue;
15961 		}
15962 
15963 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
15964 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
15965 		ASSERT(ilm->ilm_orig_ifindex != 0);
15966 		if (ilm->ilm_orig_ifindex == ifindex) {
15967 			/*
15968 			 * We are failing back multicast memberships.
15969 			 * If the same ilm exists in to_ill, it means somebody
15970 			 * has joined the same group there e.g. ff02::1
15971 			 * is joined within the kernel when the interfaces
15972 			 * came UP.
15973 			 */
15974 			ASSERT(ilm->ilm_ipif == NULL);
15975 			if (new_ilm != NULL) {
15976 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
15977 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
15978 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
15979 					new_ilm->ilm_is_new = B_TRUE;
15980 				}
15981 			} else {
15982 				/*
15983 				 * check if we can just move the ilm
15984 				 */
15985 				if (from_ill->ill_ilm_walker_cnt != 0) {
15986 					/*
15987 					 * We have walkers we cannot move
15988 					 * the ilm, so allocate a new ilm,
15989 					 * this (old) ilm will be marked
15990 					 * ILM_DELETED at the end of the loop
15991 					 * and will be freed when the
15992 					 * last walker exits.
15993 					 */
15994 					new_ilm = (ilm_t *)mi_zalloc
15995 					    (sizeof (ilm_t));
15996 					if (new_ilm == NULL) {
15997 						ip0dbg(("ilm_move_v6: "
15998 						    "FAILBACK of IPv6"
15999 						    " multicast address %s : "
16000 						    "from %s to"
16001 						    " %s failed : ENOMEM \n",
16002 						    inet_ntop(AF_INET6,
16003 						    &ilm->ilm_v6addr, buf,
16004 						    sizeof (buf)),
16005 						    from_ill->ill_name,
16006 						    to_ill->ill_name));
16007 
16008 							ilmp = &ilm->ilm_next;
16009 							continue;
16010 					}
16011 					*new_ilm = *ilm;
16012 					/*
16013 					 * we don't want new_ilm linked to
16014 					 * ilm's filter list.
16015 					 */
16016 					new_ilm->ilm_filter = NULL;
16017 				} else {
16018 					/*
16019 					 * No walkers we can move the ilm.
16020 					 * lets take it out of the list.
16021 					 */
16022 					*ilmp = ilm->ilm_next;
16023 					ilm->ilm_next = NULL;
16024 					new_ilm = ilm;
16025 				}
16026 
16027 				/*
16028 				 * if this is the first ilm for the group
16029 				 * set ilm_notify_driver so that we notify the
16030 				 * driver in ilm_send_multicast_reqs.
16031 				 */
16032 				if (ilm_lookup_ill_v6(to_ill,
16033 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16034 					new_ilm->ilm_notify_driver = B_TRUE;
16035 
16036 				new_ilm->ilm_ill = to_ill;
16037 				/* Add to the to_ill's list */
16038 				new_ilm->ilm_next = to_ill->ill_ilm;
16039 				to_ill->ill_ilm = new_ilm;
16040 				/*
16041 				 * set the flag so that mld_joingroup is
16042 				 * called in ilm_send_multicast_reqs().
16043 				 */
16044 				new_ilm->ilm_is_new = B_TRUE;
16045 			}
16046 			goto bottom;
16047 		} else if (ifindex != 0) {
16048 			/*
16049 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16050 			 * has not matched above, look at the next ilm.
16051 			 */
16052 			ilmp = &ilm->ilm_next;
16053 			continue;
16054 		}
16055 		/*
16056 		 * If we are here, it means ifindex is 0. Failover
16057 		 * everything.
16058 		 *
16059 		 * We need to handle solicited node mcast address
16060 		 * and all_nodes mcast address differently as they
16061 		 * are joined witin the kenrel (ipif_multicast_up)
16062 		 * and potentially from the userland. We are called
16063 		 * after the ipifs of from_ill has been moved.
16064 		 * If we still find ilms on ill with solicited node
16065 		 * mcast address or all_nodes mcast address, it must
16066 		 * belong to the UP interface that has not moved e.g.
16067 		 * ipif_id 0 with the link local prefix does not move.
16068 		 * We join this on the new ill accounting for all the
16069 		 * userland memberships so that applications don't
16070 		 * see any failure.
16071 		 *
16072 		 * We need to make sure that we account only for the
16073 		 * solicited node and all node multicast addresses
16074 		 * that was brought UP on these. In the case of
16075 		 * a failover from A to B, we might have ilms belonging
16076 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
16077 		 * for the membership from the userland. If we are failing
16078 		 * over from B to C now, we will find the ones belonging
16079 		 * to A on B. These don't account for the ill_ipif_up_count.
16080 		 * They just move from B to C. The check below on
16081 		 * ilm_orig_ifindex ensures that.
16082 		 */
16083 		if ((ilm->ilm_orig_ifindex ==
16084 		    from_ill->ill_phyint->phyint_ifindex) &&
16085 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
16086 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
16087 		    &ilm->ilm_v6addr))) {
16088 			ASSERT(ilm->ilm_refcnt > 0);
16089 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
16090 			/*
16091 			 * For indentation reasons, we are not using a
16092 			 * "else" here.
16093 			 */
16094 			if (count == 0) {
16095 				ilmp = &ilm->ilm_next;
16096 				continue;
16097 			}
16098 			ilm->ilm_refcnt -= count;
16099 			if (new_ilm != NULL) {
16100 				/*
16101 				 * Can find one with the same
16102 				 * ilm_orig_ifindex, if we are failing
16103 				 * over to a STANDBY. This happens
16104 				 * when somebody wants to join a group
16105 				 * on a STANDBY interface and we
16106 				 * internally join on a different one.
16107 				 * If we had joined on from_ill then, a
16108 				 * failover now will find a new ilm
16109 				 * with this index.
16110 				 */
16111 				ip1dbg(("ilm_move_v6: FAILOVER, found"
16112 				    " new ilm on %s, group address %s\n",
16113 				    to_ill->ill_name,
16114 				    inet_ntop(AF_INET6,
16115 				    &ilm->ilm_v6addr, buf,
16116 				    sizeof (buf))));
16117 				new_ilm->ilm_refcnt += count;
16118 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16119 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16120 					new_ilm->ilm_is_new = B_TRUE;
16121 				}
16122 			} else {
16123 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
16124 				if (new_ilm == NULL) {
16125 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
16126 					    " multicast address %s : from %s to"
16127 					    " %s failed : ENOMEM \n",
16128 					    inet_ntop(AF_INET6,
16129 					    &ilm->ilm_v6addr, buf,
16130 					    sizeof (buf)), from_ill->ill_name,
16131 					    to_ill->ill_name));
16132 					ilmp = &ilm->ilm_next;
16133 					continue;
16134 				}
16135 				*new_ilm = *ilm;
16136 				new_ilm->ilm_filter = NULL;
16137 				new_ilm->ilm_refcnt = count;
16138 				new_ilm->ilm_timer = INFINITY;
16139 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
16140 				new_ilm->ilm_is_new = B_TRUE;
16141 				/*
16142 				 * If the to_ill has not joined this
16143 				 * group we need to tell the driver in
16144 				 * ill_send_multicast_reqs.
16145 				 */
16146 				if (ilm_lookup_ill_v6(to_ill,
16147 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16148 					new_ilm->ilm_notify_driver = B_TRUE;
16149 
16150 				new_ilm->ilm_ill = to_ill;
16151 				/* Add to the to_ill's list */
16152 				new_ilm->ilm_next = to_ill->ill_ilm;
16153 				to_ill->ill_ilm = new_ilm;
16154 				ASSERT(new_ilm->ilm_ipif == NULL);
16155 			}
16156 			if (ilm->ilm_refcnt == 0) {
16157 				goto bottom;
16158 			} else {
16159 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16160 				CLEAR_SLIST(new_ilm->ilm_filter);
16161 				ilmp = &ilm->ilm_next;
16162 			}
16163 			continue;
16164 		} else {
16165 			/*
16166 			 * ifindex = 0 means, move everything pointing at
16167 			 * from_ill. We are doing this becuase ill has
16168 			 * either FAILED or became INACTIVE.
16169 			 *
16170 			 * As we would like to move things later back to
16171 			 * from_ill, we want to retain the identity of this
16172 			 * ilm. Thus, we don't blindly increment the reference
16173 			 * count on the ilms matching the address alone. We
16174 			 * need to match on the ilm_orig_index also. new_ilm
16175 			 * was obtained by matching ilm_orig_index also.
16176 			 */
16177 			if (new_ilm != NULL) {
16178 				/*
16179 				 * This is possible only if a previous restore
16180 				 * was incomplete i.e restore to
16181 				 * ilm_orig_ifindex left some ilms because
16182 				 * of some failures. Thus when we are failing
16183 				 * again, we might find our old friends there.
16184 				 */
16185 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
16186 				    " on %s, group address %s\n",
16187 				    to_ill->ill_name,
16188 				    inet_ntop(AF_INET6,
16189 				    &ilm->ilm_v6addr, buf,
16190 				    sizeof (buf))));
16191 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16192 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16193 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16194 					new_ilm->ilm_is_new = B_TRUE;
16195 				}
16196 			} else {
16197 				if (from_ill->ill_ilm_walker_cnt != 0) {
16198 					new_ilm = (ilm_t *)
16199 					    mi_zalloc(sizeof (ilm_t));
16200 					if (new_ilm == NULL) {
16201 						ip0dbg(("ilm_move_v6: "
16202 						    "FAILOVER of IPv6"
16203 						    " multicast address %s : "
16204 						    "from %s to"
16205 						    " %s failed : ENOMEM \n",
16206 						    inet_ntop(AF_INET6,
16207 						    &ilm->ilm_v6addr, buf,
16208 						    sizeof (buf)),
16209 						    from_ill->ill_name,
16210 						    to_ill->ill_name));
16211 
16212 							ilmp = &ilm->ilm_next;
16213 							continue;
16214 					}
16215 					*new_ilm = *ilm;
16216 					new_ilm->ilm_filter = NULL;
16217 				} else {
16218 					*ilmp = ilm->ilm_next;
16219 					new_ilm = ilm;
16220 				}
16221 				/*
16222 				 * If the to_ill has not joined this
16223 				 * group we need to tell the driver in
16224 				 * ill_send_multicast_reqs.
16225 				 */
16226 				if (ilm_lookup_ill_v6(to_ill,
16227 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16228 					new_ilm->ilm_notify_driver = B_TRUE;
16229 
16230 				/* Add to the to_ill's list */
16231 				new_ilm->ilm_next = to_ill->ill_ilm;
16232 				to_ill->ill_ilm = new_ilm;
16233 				ASSERT(ilm->ilm_ipif == NULL);
16234 				new_ilm->ilm_ill = to_ill;
16235 				new_ilm->ilm_is_new = B_TRUE;
16236 			}
16237 
16238 		}
16239 
16240 bottom:
16241 		/*
16242 		 * Revert multicast filter state to (EXCLUDE, NULL).
16243 		 * new_ilm->ilm_is_new should already be set if needed.
16244 		 */
16245 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16246 		CLEAR_SLIST(new_ilm->ilm_filter);
16247 		/*
16248 		 * We allocated/got a new ilm, free the old one.
16249 		 */
16250 		if (new_ilm != ilm) {
16251 			if (from_ill->ill_ilm_walker_cnt == 0) {
16252 				*ilmp = ilm->ilm_next;
16253 				ilm->ilm_next = NULL;
16254 				FREE_SLIST(ilm->ilm_filter);
16255 				FREE_SLIST(ilm->ilm_pendsrcs);
16256 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
16257 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
16258 				mi_free((char *)ilm);
16259 			} else {
16260 				ilm->ilm_flags |= ILM_DELETED;
16261 				from_ill->ill_ilm_cleanup_reqd = 1;
16262 				ilmp = &ilm->ilm_next;
16263 			}
16264 		}
16265 	}
16266 }
16267 
16268 /*
16269  * Move all the multicast memberships to to_ill. Called when
16270  * an ipif moves from "from_ill" to "to_ill". This function is slightly
16271  * different from IPv6 counterpart as multicast memberships are associated
16272  * with ills in IPv6. This function is called after every ipif is moved
16273  * unlike IPv6, where it is moved only once.
16274  */
16275 static void
16276 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
16277 {
16278 	ilm_t	*ilm;
16279 	ilm_t	*ilm_next;
16280 	ilm_t	*new_ilm;
16281 	ilm_t	**ilmp;
16282 
16283 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16284 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16285 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
16286 
16287 	ilmp = &from_ill->ill_ilm;
16288 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16289 		ilm_next = ilm->ilm_next;
16290 
16291 		if (ilm->ilm_flags & ILM_DELETED) {
16292 			ilmp = &ilm->ilm_next;
16293 			continue;
16294 		}
16295 
16296 		ASSERT(ilm->ilm_ipif != NULL);
16297 
16298 		if (ilm->ilm_ipif != ipif) {
16299 			ilmp = &ilm->ilm_next;
16300 			continue;
16301 		}
16302 
16303 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
16304 		    htonl(INADDR_ALLHOSTS_GROUP)) {
16305 			/*
16306 			 * We joined this in ipif_multicast_up
16307 			 * and we never did an ipif_multicast_down
16308 			 * for IPv4. If nobody else from the userland
16309 			 * has reference, we free the ilm, and later
16310 			 * when this ipif comes up on the new ill,
16311 			 * we will join this again.
16312 			 */
16313 			if (--ilm->ilm_refcnt == 0)
16314 				goto delete_ilm;
16315 
16316 			new_ilm = ilm_lookup_ipif(ipif,
16317 			    V4_PART_OF_V6(ilm->ilm_v6addr));
16318 			if (new_ilm != NULL) {
16319 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16320 				/*
16321 				 * We still need to deal with the from_ill.
16322 				 */
16323 				new_ilm->ilm_is_new = B_TRUE;
16324 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16325 				CLEAR_SLIST(new_ilm->ilm_filter);
16326 				goto delete_ilm;
16327 			}
16328 			/*
16329 			 * If we could not find one e.g. ipif is
16330 			 * still down on to_ill, we add this ilm
16331 			 * on ill_new to preserve the reference
16332 			 * count.
16333 			 */
16334 		}
16335 		/*
16336 		 * When ipifs move, ilms always move with it
16337 		 * to the NEW ill. Thus we should never be
16338 		 * able to find ilm till we really move it here.
16339 		 */
16340 		ASSERT(ilm_lookup_ipif(ipif,
16341 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
16342 
16343 		if (from_ill->ill_ilm_walker_cnt != 0) {
16344 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
16345 			if (new_ilm == NULL) {
16346 				char buf[INET6_ADDRSTRLEN];
16347 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
16348 				    " multicast address %s : "
16349 				    "from %s to"
16350 				    " %s failed : ENOMEM \n",
16351 				    inet_ntop(AF_INET,
16352 				    &ilm->ilm_v6addr, buf,
16353 				    sizeof (buf)),
16354 				    from_ill->ill_name,
16355 				    to_ill->ill_name));
16356 
16357 				ilmp = &ilm->ilm_next;
16358 				continue;
16359 			}
16360 			*new_ilm = *ilm;
16361 			/* We don't want new_ilm linked to ilm's filter list */
16362 			new_ilm->ilm_filter = NULL;
16363 		} else {
16364 			/* Remove from the list */
16365 			*ilmp = ilm->ilm_next;
16366 			new_ilm = ilm;
16367 		}
16368 
16369 		/*
16370 		 * If we have never joined this group on the to_ill
16371 		 * make sure we tell the driver.
16372 		 */
16373 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
16374 		    ALL_ZONES) == NULL)
16375 			new_ilm->ilm_notify_driver = B_TRUE;
16376 
16377 		/* Add to the to_ill's list */
16378 		new_ilm->ilm_next = to_ill->ill_ilm;
16379 		to_ill->ill_ilm = new_ilm;
16380 		new_ilm->ilm_is_new = B_TRUE;
16381 
16382 		/*
16383 		 * Revert multicast filter state to (EXCLUDE, NULL)
16384 		 */
16385 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16386 		CLEAR_SLIST(new_ilm->ilm_filter);
16387 
16388 		/*
16389 		 * Delete only if we have allocated a new ilm.
16390 		 */
16391 		if (new_ilm != ilm) {
16392 delete_ilm:
16393 			if (from_ill->ill_ilm_walker_cnt == 0) {
16394 				/* Remove from the list */
16395 				*ilmp = ilm->ilm_next;
16396 				ilm->ilm_next = NULL;
16397 				FREE_SLIST(ilm->ilm_filter);
16398 				FREE_SLIST(ilm->ilm_pendsrcs);
16399 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
16400 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
16401 				mi_free((char *)ilm);
16402 			} else {
16403 				ilm->ilm_flags |= ILM_DELETED;
16404 				from_ill->ill_ilm_cleanup_reqd = 1;
16405 				ilmp = &ilm->ilm_next;
16406 			}
16407 		}
16408 	}
16409 }
16410 
16411 static uint_t
16412 ipif_get_id(ill_t *ill, uint_t id)
16413 {
16414 	uint_t	unit;
16415 	ipif_t	*tipif;
16416 	boolean_t found = B_FALSE;
16417 
16418 	/*
16419 	 * During failback, we want to go back to the same id
16420 	 * instead of the smallest id so that the original
16421 	 * configuration is maintained. id is non-zero in that
16422 	 * case.
16423 	 */
16424 	if (id != 0) {
16425 		/*
16426 		 * While failing back, if we still have an ipif with
16427 		 * MAX_ADDRS_PER_IF, it means this will be replaced
16428 		 * as soon as we return from this function. It was
16429 		 * to set to MAX_ADDRS_PER_IF by the caller so that
16430 		 * we can choose the smallest id. Thus we return zero
16431 		 * in that case ignoring the hint.
16432 		 */
16433 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
16434 			return (0);
16435 		for (tipif = ill->ill_ipif; tipif != NULL;
16436 		    tipif = tipif->ipif_next) {
16437 			if (tipif->ipif_id == id) {
16438 				found = B_TRUE;
16439 				break;
16440 			}
16441 		}
16442 		/*
16443 		 * If somebody already plumbed another logical
16444 		 * with the same id, we won't be able to find it.
16445 		 */
16446 		if (!found)
16447 			return (id);
16448 	}
16449 	for (unit = 0; unit <= ip_addrs_per_if; unit++) {
16450 		found = B_FALSE;
16451 		for (tipif = ill->ill_ipif; tipif != NULL;
16452 		    tipif = tipif->ipif_next) {
16453 			if (tipif->ipif_id == unit) {
16454 				found = B_TRUE;
16455 				break;
16456 			}
16457 		}
16458 		if (!found)
16459 			break;
16460 	}
16461 	return (unit);
16462 }
16463 
16464 /* ARGSUSED */
16465 static int
16466 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
16467     ipif_t **rep_ipif_ptr)
16468 {
16469 	ill_t	*from_ill;
16470 	ipif_t	*rep_ipif;
16471 	ipif_t	**ipifp;
16472 	uint_t	unit;
16473 	int err = 0;
16474 	ipif_t	*to_ipif;
16475 	struct iocblk	*iocp;
16476 	boolean_t failback_cmd;
16477 	boolean_t remove_ipif;
16478 	int	rc;
16479 
16480 	ASSERT(IAM_WRITER_ILL(to_ill));
16481 	ASSERT(IAM_WRITER_IPIF(ipif));
16482 
16483 	iocp = (struct iocblk *)mp->b_rptr;
16484 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
16485 	remove_ipif = B_FALSE;
16486 
16487 	from_ill = ipif->ipif_ill;
16488 
16489 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16490 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16491 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
16492 
16493 	/*
16494 	 * Don't move LINK LOCAL addresses as they are tied to
16495 	 * physical interface.
16496 	 */
16497 	if (from_ill->ill_isv6 &&
16498 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
16499 		ipif->ipif_was_up = B_FALSE;
16500 		IPIF_UNMARK_MOVING(ipif);
16501 		return (0);
16502 	}
16503 
16504 	/*
16505 	 * We set the ipif_id to maximum so that the search for
16506 	 * ipif_id will pick the lowest number i.e 0 in the
16507 	 * following 2 cases :
16508 	 *
16509 	 * 1) We have a replacement ipif at the head of to_ill.
16510 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
16511 	 *    on to_ill and hence the MOVE might fail. We want to
16512 	 *    remove it only if we could move the ipif. Thus, by
16513 	 *    setting it to the MAX value, we make the search in
16514 	 *    ipif_get_id return the zeroth id.
16515 	 *
16516 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
16517 	 *    we might just have a zero address plumbed on the ipif
16518 	 *    with zero id in the case of IPv4. We remove that while
16519 	 *    doing the failback. We want to remove it only if we
16520 	 *    could move the ipif. Thus, by setting it to the MAX
16521 	 *    value, we make the search in ipif_get_id return the
16522 	 *    zeroth id.
16523 	 *
16524 	 * Both (1) and (2) are done only when when we are moving
16525 	 * an ipif (either due to failover/failback) which originally
16526 	 * belonged to this interface i.e the ipif_orig_ifindex is
16527 	 * the same as to_ill's ifindex. This is needed so that
16528 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
16529 	 * from B -> A (B is being removed from the group) and
16530 	 * FAILBACK from A -> B restores the original configuration.
16531 	 * Without the check for orig_ifindex, the second FAILOVER
16532 	 * could make the ipif belonging to B replace the A's zeroth
16533 	 * ipif and the subsequent failback re-creating the replacement
16534 	 * ipif again.
16535 	 *
16536 	 * NOTE : We created the replacement ipif when we did a
16537 	 * FAILOVER (See below). We could check for FAILBACK and
16538 	 * then look for replacement ipif to be removed. But we don't
16539 	 * want to do that because we wan't to allow the possibility
16540 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
16541 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
16542 	 * from B -> A.
16543 	 */
16544 	to_ipif = to_ill->ill_ipif;
16545 	if ((to_ill->ill_phyint->phyint_ifindex ==
16546 	    ipif->ipif_orig_ifindex) &&
16547 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
16548 		ASSERT(to_ipif->ipif_id == 0);
16549 		remove_ipif = B_TRUE;
16550 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
16551 	}
16552 	/*
16553 	 * Find the lowest logical unit number on the to_ill.
16554 	 * If we are failing back, try to get the original id
16555 	 * rather than the lowest one so that the original
16556 	 * configuration is maintained.
16557 	 *
16558 	 * XXX need a better scheme for this.
16559 	 */
16560 	if (failback_cmd) {
16561 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
16562 	} else {
16563 		unit = ipif_get_id(to_ill, 0);
16564 	}
16565 
16566 	/* Reset back to zero in case we fail below */
16567 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
16568 		to_ipif->ipif_id = 0;
16569 
16570 	if (unit == ip_addrs_per_if) {
16571 		ipif->ipif_was_up = B_FALSE;
16572 		IPIF_UNMARK_MOVING(ipif);
16573 		return (EINVAL);
16574 	}
16575 
16576 	/*
16577 	 * ipif is ready to move from "from_ill" to "to_ill".
16578 	 *
16579 	 * 1) If we are moving ipif with id zero, create a
16580 	 *    replacement ipif for this ipif on from_ill. If this fails
16581 	 *    fail the MOVE operation.
16582 	 *
16583 	 * 2) Remove the replacement ipif on to_ill if any.
16584 	 *    We could remove the replacement ipif when we are moving
16585 	 *    the ipif with id zero. But what if somebody already
16586 	 *    unplumbed it ? Thus we always remove it if it is present.
16587 	 *    We want to do it only if we are sure we are going to
16588 	 *    move the ipif to to_ill which is why there are no
16589 	 *    returns due to error till ipif is linked to to_ill.
16590 	 *    Note that the first ipif that we failback will always
16591 	 *    be zero if it is present.
16592 	 */
16593 	if (ipif->ipif_id == 0) {
16594 		ipaddr_t inaddr_any = INADDR_ANY;
16595 
16596 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
16597 		if (rep_ipif == NULL) {
16598 			ipif->ipif_was_up = B_FALSE;
16599 			IPIF_UNMARK_MOVING(ipif);
16600 			return (ENOMEM);
16601 		}
16602 		*rep_ipif = ipif_zero;
16603 		/*
16604 		 * Before we put the ipif on the list, store the addresses
16605 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
16606 		 * assumes so. This logic is not any different from what
16607 		 * ipif_allocate does.
16608 		 */
16609 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16610 		    &rep_ipif->ipif_v6lcl_addr);
16611 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16612 		    &rep_ipif->ipif_v6src_addr);
16613 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16614 		    &rep_ipif->ipif_v6subnet);
16615 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16616 		    &rep_ipif->ipif_v6net_mask);
16617 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16618 		    &rep_ipif->ipif_v6brd_addr);
16619 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16620 		    &rep_ipif->ipif_v6pp_dst_addr);
16621 		/*
16622 		 * We mark IPIF_NOFAILOVER so that this can never
16623 		 * move.
16624 		 */
16625 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
16626 		rep_ipif->ipif_flags &= ~IPIF_UP;
16627 		rep_ipif->ipif_replace_zero = B_TRUE;
16628 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
16629 		    MUTEX_DEFAULT, NULL);
16630 		rep_ipif->ipif_id = 0;
16631 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
16632 		rep_ipif->ipif_ill = from_ill;
16633 		rep_ipif->ipif_orig_ifindex =
16634 		    from_ill->ill_phyint->phyint_ifindex;
16635 		/* Insert at head */
16636 		rep_ipif->ipif_next = from_ill->ill_ipif;
16637 		from_ill->ill_ipif = rep_ipif;
16638 		/*
16639 		 * We don't really care to let apps know about
16640 		 * this interface.
16641 		 */
16642 	}
16643 
16644 	if (remove_ipif) {
16645 		/*
16646 		 * We set to a max value above for this case to get
16647 		 * id zero. ASSERT that we did get one.
16648 		 */
16649 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
16650 		rep_ipif = to_ipif;
16651 		to_ill->ill_ipif = rep_ipif->ipif_next;
16652 		rep_ipif->ipif_next = NULL;
16653 		/*
16654 		 * If some apps scanned and find this interface,
16655 		 * it is time to let them know, so that they can
16656 		 * delete it.
16657 		 */
16658 
16659 		*rep_ipif_ptr = rep_ipif;
16660 	}
16661 
16662 	/* Get it out of the ILL interface list. */
16663 	ipifp = &ipif->ipif_ill->ill_ipif;
16664 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
16665 		if (*ipifp == ipif) {
16666 			*ipifp = ipif->ipif_next;
16667 			break;
16668 		}
16669 	}
16670 
16671 	/* Assign the new ill */
16672 	ipif->ipif_ill = to_ill;
16673 	ipif->ipif_id = unit;
16674 	/* id has already been checked */
16675 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
16676 	ASSERT(rc == 0);
16677 	/* Let SCTP update its list */
16678 	sctp_move_ipif(ipif, from_ill, to_ill);
16679 	/*
16680 	 * Handle the failover and failback of ipif_t between
16681 	 * ill_t that have differing maximum mtu values.
16682 	 */
16683 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
16684 		if (ipif->ipif_saved_mtu == 0) {
16685 			/*
16686 			 * As this ipif_t is moving to an ill_t
16687 			 * that has a lower ill_max_mtu, its
16688 			 * ipif_mtu needs to be saved so it can
16689 			 * be restored during failback or during
16690 			 * failover to an ill_t which has a
16691 			 * higher ill_max_mtu.
16692 			 */
16693 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
16694 			ipif->ipif_mtu = to_ill->ill_max_mtu;
16695 		} else {
16696 			/*
16697 			 * The ipif_t is, once again, moving to
16698 			 * an ill_t that has a lower maximum mtu
16699 			 * value.
16700 			 */
16701 			ipif->ipif_mtu = to_ill->ill_max_mtu;
16702 		}
16703 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
16704 	    ipif->ipif_saved_mtu != 0) {
16705 		/*
16706 		 * The mtu of this ipif_t had to be reduced
16707 		 * during an earlier failover; this is an
16708 		 * opportunity for it to be increased (either as
16709 		 * part of another failover or a failback).
16710 		 */
16711 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
16712 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
16713 			ipif->ipif_saved_mtu = 0;
16714 		} else {
16715 			ipif->ipif_mtu = to_ill->ill_max_mtu;
16716 		}
16717 	}
16718 
16719 	/*
16720 	 * We preserve all the other fields of the ipif including
16721 	 * ipif_saved_ire_mp. The routes that are saved here will
16722 	 * be recreated on the new interface and back on the old
16723 	 * interface when we move back.
16724 	 */
16725 	ASSERT(ipif->ipif_arp_del_mp == NULL);
16726 
16727 	return (err);
16728 }
16729 
16730 static int
16731 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
16732     int ifindex, ipif_t **rep_ipif_ptr)
16733 {
16734 	ipif_t *mipif;
16735 	ipif_t *ipif_next;
16736 	int err;
16737 
16738 	/*
16739 	 * We don't really try to MOVE back things if some of the
16740 	 * operations fail. The daemon will take care of moving again
16741 	 * later on.
16742 	 */
16743 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
16744 		ipif_next = mipif->ipif_next;
16745 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
16746 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
16747 
16748 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
16749 
16750 			/*
16751 			 * When the MOVE fails, it is the job of the
16752 			 * application to take care of this properly
16753 			 * i.e try again if it is ENOMEM.
16754 			 */
16755 			if (mipif->ipif_ill != from_ill) {
16756 				/*
16757 				 * ipif has moved.
16758 				 *
16759 				 * Move the multicast memberships associated
16760 				 * with this ipif to the new ill. For IPv6, we
16761 				 * do it once after all the ipifs are moved
16762 				 * (in ill_move) as they are not associated
16763 				 * with ipifs.
16764 				 *
16765 				 * We need to move the ilms as the ipif has
16766 				 * already been moved to a new ill even
16767 				 * in the case of errors. Neither
16768 				 * ilm_free(ipif) will find the ilm
16769 				 * when somebody unplumbs this ipif nor
16770 				 * ilm_delete(ilm) will be able to find the
16771 				 * ilm, if we don't move now.
16772 				 */
16773 				if (!from_ill->ill_isv6)
16774 					ilm_move_v4(from_ill, to_ill, mipif);
16775 			}
16776 
16777 			if (err != 0)
16778 				return (err);
16779 		}
16780 	}
16781 	return (0);
16782 }
16783 
16784 static int
16785 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
16786 {
16787 	int ifindex;
16788 	int err;
16789 	struct iocblk	*iocp;
16790 	ipif_t	*ipif;
16791 	ipif_t *rep_ipif_ptr = NULL;
16792 	ipif_t	*from_ipif = NULL;
16793 	boolean_t check_rep_if = B_FALSE;
16794 
16795 	iocp = (struct iocblk *)mp->b_rptr;
16796 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
16797 		/*
16798 		 * Move everything pointing at from_ill to to_ill.
16799 		 * We acheive this by passing in 0 as ifindex.
16800 		 */
16801 		ifindex = 0;
16802 	} else {
16803 		/*
16804 		 * Move everything pointing at from_ill whose original
16805 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
16806 		 * We acheive this by passing in ifindex rather than 0.
16807 		 * Multicast vifs, ilgs move implicitly because ipifs move.
16808 		 */
16809 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
16810 		ifindex = to_ill->ill_phyint->phyint_ifindex;
16811 	}
16812 
16813 	/*
16814 	 * Determine if there is at least one ipif that would move from
16815 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
16816 	 * ipif (if it exists) on the to_ill would be consumed as a result of
16817 	 * the move, in which case we need to quiesce the replacement ipif also.
16818 	 */
16819 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
16820 	    from_ipif = from_ipif->ipif_next) {
16821 		if (((ifindex == 0) ||
16822 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
16823 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
16824 			check_rep_if = B_TRUE;
16825 			break;
16826 		}
16827 	}
16828 
16829 
16830 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
16831 
16832 	GRAB_ILL_LOCKS(from_ill, to_ill);
16833 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
16834 		(void) ipsq_pending_mp_add(NULL, ipif, q,
16835 		    mp, ILL_MOVE_OK);
16836 		RELEASE_ILL_LOCKS(from_ill, to_ill);
16837 		return (EINPROGRESS);
16838 	}
16839 
16840 	/* Check if the replacement ipif is quiescent to delete */
16841 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
16842 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
16843 		to_ill->ill_ipif->ipif_state_flags |=
16844 		    IPIF_MOVING | IPIF_CHANGING;
16845 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
16846 			(void) ipsq_pending_mp_add(NULL, ipif, q,
16847 			    mp, ILL_MOVE_OK);
16848 			RELEASE_ILL_LOCKS(from_ill, to_ill);
16849 			return (EINPROGRESS);
16850 		}
16851 	}
16852 	RELEASE_ILL_LOCKS(from_ill, to_ill);
16853 
16854 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
16855 	rw_enter(&ill_g_lock, RW_WRITER);
16856 	GRAB_ILL_LOCKS(from_ill, to_ill);
16857 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
16858 
16859 	/* ilm_move is done inside ipif_move for IPv4 */
16860 	if (err == 0 && from_ill->ill_isv6)
16861 		ilm_move_v6(from_ill, to_ill, ifindex);
16862 
16863 	RELEASE_ILL_LOCKS(from_ill, to_ill);
16864 	rw_exit(&ill_g_lock);
16865 
16866 	/*
16867 	 * send rts messages and multicast messages.
16868 	 */
16869 	if (rep_ipif_ptr != NULL) {
16870 		ip_rts_ifmsg(rep_ipif_ptr);
16871 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
16872 		IPIF_TRACE_CLEANUP(rep_ipif_ptr);
16873 		mi_free(rep_ipif_ptr);
16874 	}
16875 
16876 	conn_move_ill(from_ill, to_ill, ifindex);
16877 
16878 	return (err);
16879 }
16880 
16881 /*
16882  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
16883  * Also checks for the validity of the arguments.
16884  * Note: We are already exclusive inside the from group.
16885  * It is upto the caller to release refcnt on the to_ill's.
16886  */
16887 static int
16888 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
16889     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
16890 {
16891 	int dst_index;
16892 	ipif_t *ipif_v4, *ipif_v6;
16893 	struct lifreq *lifr;
16894 	mblk_t *mp1;
16895 	boolean_t exists;
16896 	sin_t	*sin;
16897 	int	err = 0;
16898 
16899 	if ((mp1 = mp->b_cont) == NULL)
16900 		return (EPROTO);
16901 
16902 	if ((mp1 = mp1->b_cont) == NULL)
16903 		return (EPROTO);
16904 
16905 	lifr = (struct lifreq *)mp1->b_rptr;
16906 	sin = (sin_t *)&lifr->lifr_addr;
16907 
16908 	/*
16909 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
16910 	 * specific operations.
16911 	 */
16912 	if (sin->sin_family != AF_UNSPEC)
16913 		return (EINVAL);
16914 
16915 	/*
16916 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
16917 	 * NULLs for the last 4 args and we know the lookup won't fail
16918 	 * with EINPROGRESS.
16919 	 */
16920 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
16921 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
16922 	    ALL_ZONES, NULL, NULL, NULL, NULL);
16923 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
16924 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
16925 	    ALL_ZONES, NULL, NULL, NULL, NULL);
16926 
16927 	if (ipif_v4 == NULL && ipif_v6 == NULL)
16928 		return (ENXIO);
16929 
16930 	if (ipif_v4 != NULL) {
16931 		ASSERT(ipif_v4->ipif_refcnt != 0);
16932 		if (ipif_v4->ipif_id != 0) {
16933 			err = EINVAL;
16934 			goto done;
16935 		}
16936 
16937 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
16938 		*ill_from_v4 = ipif_v4->ipif_ill;
16939 	}
16940 
16941 	if (ipif_v6 != NULL) {
16942 		ASSERT(ipif_v6->ipif_refcnt != 0);
16943 		if (ipif_v6->ipif_id != 0) {
16944 			err = EINVAL;
16945 			goto done;
16946 		}
16947 
16948 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
16949 		*ill_from_v6 = ipif_v6->ipif_ill;
16950 	}
16951 
16952 	err = 0;
16953 	dst_index = lifr->lifr_movetoindex;
16954 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
16955 	    q, mp, ip_process_ioctl, &err);
16956 	if (err != 0) {
16957 		/*
16958 		 * There could be only v6.
16959 		 */
16960 		if (err != ENXIO)
16961 			goto done;
16962 		err = 0;
16963 	}
16964 
16965 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
16966 	    q, mp, ip_process_ioctl, &err);
16967 	if (err != 0) {
16968 		if (err != ENXIO)
16969 			goto done;
16970 		if (*ill_to_v4 == NULL) {
16971 			err = ENXIO;
16972 			goto done;
16973 		}
16974 		err = 0;
16975 	}
16976 
16977 	/*
16978 	 * If we have something to MOVE i.e "from" not NULL,
16979 	 * "to" should be non-NULL.
16980 	 */
16981 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
16982 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
16983 		err = EINVAL;
16984 	}
16985 
16986 done:
16987 	if (ipif_v4 != NULL)
16988 		ipif_refrele(ipif_v4);
16989 	if (ipif_v6 != NULL)
16990 		ipif_refrele(ipif_v6);
16991 	return (err);
16992 }
16993 
16994 /*
16995  * FAILOVER and FAILBACK are modelled as MOVE operations.
16996  *
16997  * We don't check whether the MOVE is within the same group or
16998  * not, because this ioctl can be used as a generic mechanism
16999  * to failover from interface A to B, though things will function
17000  * only if they are really part of the same group. Moreover,
17001  * all ipifs may be down and hence temporarily out of the group.
17002  *
17003  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17004  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17005  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17006  * have been deleted and there are no active references. Once quiescent the
17007  * ipif's are moved and brought up on the new ill.
17008  *
17009  * Normally the source ill and destination ill belong to the same IPMP group
17010  * and hence the same ipsq_t. In the event they don't belong to the same
17011  * same group the two ipsq's are first merged into one ipsq - that of the
17012  * to_ill. The multicast memberships on the source and destination ill cannot
17013  * change during the move operation since multicast joins/leaves also have to
17014  * execute on the same ipsq and are hence serialized.
17015  */
17016 /* ARGSUSED */
17017 int
17018 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17019     ip_ioctl_cmd_t *ipip, void *ifreq)
17020 {
17021 	ill_t *ill_to_v4 = NULL;
17022 	ill_t *ill_to_v6 = NULL;
17023 	ill_t *ill_from_v4 = NULL;
17024 	ill_t *ill_from_v6 = NULL;
17025 	int err = 0;
17026 
17027 	/*
17028 	 * setup from and to ill's, we can get EINPROGRESS only for
17029 	 * to_ill's.
17030 	 */
17031 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
17032 	    &ill_to_v4, &ill_to_v6);
17033 
17034 	if (err != 0) {
17035 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
17036 		goto done;
17037 	}
17038 
17039 	/*
17040 	 * nothing to do.
17041 	 */
17042 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
17043 		goto done;
17044 	}
17045 
17046 	/*
17047 	 * nothing to do.
17048 	 */
17049 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
17050 		goto done;
17051 	}
17052 
17053 	/*
17054 	 * Mark the ill as changing.
17055 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
17056 	 * in ill_up_ipifs in case of error they are cleared below.
17057 	 */
17058 
17059 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17060 	if (ill_from_v4 != NULL)
17061 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
17062 	if (ill_from_v6 != NULL)
17063 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
17064 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17065 
17066 	/*
17067 	 * Make sure that both src and dst are
17068 	 * in the same syncq group. If not make it happen.
17069 	 * We are not holding any locks because we are the writer
17070 	 * on the from_ipsq and we will hold locks in ill_merge_groups
17071 	 * to protect to_ipsq against changing.
17072 	 */
17073 	if (ill_from_v4 != NULL) {
17074 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
17075 		    ill_to_v4->ill_phyint->phyint_ipsq) {
17076 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
17077 			    NULL, mp, q);
17078 			goto err_ret;
17079 
17080 		}
17081 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
17082 	} else {
17083 
17084 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
17085 		    ill_to_v6->ill_phyint->phyint_ipsq) {
17086 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
17087 			    NULL, mp, q);
17088 			goto err_ret;
17089 
17090 		}
17091 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
17092 	}
17093 
17094 	/*
17095 	 * Now that the ipsq's have been merged and we are the writer
17096 	 * lets mark to_ill as changing as well.
17097 	 */
17098 
17099 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
17100 	if (ill_to_v4 != NULL)
17101 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
17102 	if (ill_to_v6 != NULL)
17103 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
17104 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
17105 
17106 	/*
17107 	 * Its ok for us to proceed with the move even if
17108 	 * ill_pending_mp is non null on one of the from ill's as the reply
17109 	 * should not be looking at the ipif, it should only care about the
17110 	 * ill itself.
17111 	 */
17112 
17113 	/*
17114 	 * lets move ipv4 first.
17115 	 */
17116 	if (ill_from_v4 != NULL) {
17117 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
17118 		ill_from_v4->ill_move_in_progress = B_TRUE;
17119 		ill_to_v4->ill_move_in_progress = B_TRUE;
17120 		ill_to_v4->ill_move_peer = ill_from_v4;
17121 		ill_from_v4->ill_move_peer = ill_to_v4;
17122 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
17123 	}
17124 
17125 	/*
17126 	 * Now lets move ipv6.
17127 	 */
17128 	if (err == 0 && ill_from_v6 != NULL) {
17129 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
17130 		ill_from_v6->ill_move_in_progress = B_TRUE;
17131 		ill_to_v6->ill_move_in_progress = B_TRUE;
17132 		ill_to_v6->ill_move_peer = ill_from_v6;
17133 		ill_from_v6->ill_move_peer = ill_to_v6;
17134 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
17135 	}
17136 
17137 err_ret:
17138 	/*
17139 	 * EINPROGRESS means we are waiting for the ipif's that need to be
17140 	 * moved to become quiescent.
17141 	 */
17142 	if (err == EINPROGRESS) {
17143 		goto done;
17144 	}
17145 
17146 	/*
17147 	 * if err is set ill_up_ipifs will not be called
17148 	 * lets clear the flags.
17149 	 */
17150 
17151 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
17152 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17153 	/*
17154 	 * Some of the clearing may be redundant. But it is simple
17155 	 * not making any extra checks.
17156 	 */
17157 	if (ill_from_v6 != NULL) {
17158 		ill_from_v6->ill_move_in_progress = B_FALSE;
17159 		ill_from_v6->ill_move_peer = NULL;
17160 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
17161 	}
17162 	if (ill_from_v4 != NULL) {
17163 		ill_from_v4->ill_move_in_progress = B_FALSE;
17164 		ill_from_v4->ill_move_peer = NULL;
17165 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
17166 	}
17167 	if (ill_to_v6 != NULL) {
17168 		ill_to_v6->ill_move_in_progress = B_FALSE;
17169 		ill_to_v6->ill_move_peer = NULL;
17170 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
17171 	}
17172 	if (ill_to_v4 != NULL) {
17173 		ill_to_v4->ill_move_in_progress = B_FALSE;
17174 		ill_to_v4->ill_move_peer = NULL;
17175 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
17176 	}
17177 
17178 	/*
17179 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
17180 	 * Do this always to maintain proper state i.e even in case of errors.
17181 	 * As phyint_inactive looks at both v4 and v6 interfaces,
17182 	 * we need not call on both v4 and v6 interfaces.
17183 	 */
17184 	if (ill_from_v4 != NULL) {
17185 		if ((ill_from_v4->ill_phyint->phyint_flags &
17186 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
17187 			phyint_inactive(ill_from_v4->ill_phyint);
17188 		}
17189 	} else if (ill_from_v6 != NULL) {
17190 		if ((ill_from_v6->ill_phyint->phyint_flags &
17191 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
17192 			phyint_inactive(ill_from_v6->ill_phyint);
17193 		}
17194 	}
17195 
17196 	if (ill_to_v4 != NULL) {
17197 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
17198 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
17199 		}
17200 	} else if (ill_to_v6 != NULL) {
17201 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
17202 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
17203 		}
17204 	}
17205 
17206 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
17207 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17208 
17209 no_err:
17210 	/*
17211 	 * lets bring the interfaces up on the to_ill.
17212 	 */
17213 	if (err == 0) {
17214 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
17215 		    q, mp);
17216 	}
17217 
17218 	if (err == 0) {
17219 		if (ill_to_v4 != NULL)
17220 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
17221 
17222 		if (ill_to_v6 != NULL)
17223 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
17224 	}
17225 done:
17226 
17227 	if (ill_to_v4 != NULL) {
17228 		ill_refrele(ill_to_v4);
17229 	}
17230 	if (ill_to_v6 != NULL) {
17231 		ill_refrele(ill_to_v6);
17232 	}
17233 
17234 	return (err);
17235 }
17236 
17237 static void
17238 ill_dl_down(ill_t *ill)
17239 {
17240 	/*
17241 	 * The ill is down; unbind but stay attached since we're still
17242 	 * associated with a PPA.
17243 	 */
17244 	mblk_t	*mp = ill->ill_unbind_mp;
17245 
17246 	ill->ill_unbind_mp = NULL;
17247 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
17248 	if (mp != NULL) {
17249 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
17250 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
17251 		    ill->ill_name));
17252 		mutex_enter(&ill->ill_lock);
17253 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
17254 		mutex_exit(&ill->ill_lock);
17255 		ill_dlpi_send(ill, mp);
17256 	}
17257 
17258 	/*
17259 	 * Toss all of our multicast memberships.  We could keep them, but
17260 	 * then we'd have to do bookkeeping of any joins and leaves performed
17261 	 * by the application while the the interface is down (we can't just
17262 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
17263 	 * on a downed interface).
17264 	 */
17265 	ill_leave_multicast(ill);
17266 
17267 	mutex_enter(&ill->ill_lock);
17268 	ill->ill_dl_up = 0;
17269 	mutex_exit(&ill->ill_lock);
17270 }
17271 
17272 void
17273 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
17274 {
17275 	union DL_primitives *dlp;
17276 	t_uscalar_t prim;
17277 
17278 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
17279 
17280 	dlp = (union DL_primitives *)mp->b_rptr;
17281 	prim = dlp->dl_primitive;
17282 
17283 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
17284 		dlpi_prim_str(prim), prim, ill->ill_name));
17285 
17286 	switch (prim) {
17287 	case DL_PHYS_ADDR_REQ:
17288 	{
17289 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
17290 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
17291 		break;
17292 	}
17293 	case DL_BIND_REQ:
17294 		mutex_enter(&ill->ill_lock);
17295 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
17296 		mutex_exit(&ill->ill_lock);
17297 		break;
17298 	}
17299 
17300 	ill->ill_dlpi_pending = prim;
17301 
17302 	/*
17303 	 * Some drivers send M_FLUSH up to IP as part of unbind
17304 	 * request.  When this M_FLUSH is sent back to the driver,
17305 	 * this can go after we send the detach request if the
17306 	 * M_FLUSH ends up in IP's syncq. To avoid that, we reply
17307 	 * to the M_FLUSH in ip_rput and locally generate another
17308 	 * M_FLUSH for the correctness.  This will get freed in
17309 	 * ip_wput_nondata.
17310 	 */
17311 	if (prim == DL_UNBIND_REQ)
17312 		(void) putnextctl1(ill->ill_rq, M_FLUSH, FLUSHRW);
17313 
17314 	putnext(ill->ill_wq, mp);
17315 }
17316 
17317 /*
17318  * Send a DLPI control message to the driver but make sure there
17319  * is only one outstanding message. Uses ill_dlpi_pending to tell
17320  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
17321  * when an ACK or a NAK is received to process the next queued message.
17322  *
17323  * We don't protect ill_dlpi_pending with any lock. This is okay as
17324  * every place where its accessed, ip is exclusive while accessing
17325  * ill_dlpi_pending except when this function is called from ill_init()
17326  */
17327 void
17328 ill_dlpi_send(ill_t *ill, mblk_t *mp)
17329 {
17330 	mblk_t **mpp;
17331 
17332 	ASSERT(IAM_WRITER_ILL(ill));
17333 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
17334 
17335 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
17336 		/* Must queue message. Tail insertion */
17337 		mpp = &ill->ill_dlpi_deferred;
17338 		while (*mpp != NULL)
17339 			mpp = &((*mpp)->b_next);
17340 
17341 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
17342 		    ill->ill_name));
17343 
17344 		*mpp = mp;
17345 		return;
17346 	}
17347 
17348 	ill_dlpi_dispatch(ill, mp);
17349 }
17350 
17351 /*
17352  * Called when an DLPI control message has been acked or nacked to
17353  * send down the next queued message (if any).
17354  */
17355 void
17356 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
17357 {
17358 	mblk_t *mp;
17359 
17360 	ASSERT(IAM_WRITER_ILL(ill));
17361 
17362 	ASSERT(prim != DL_PRIM_INVAL);
17363 	if (ill->ill_dlpi_pending != prim) {
17364 		if (ill->ill_dlpi_pending == DL_PRIM_INVAL) {
17365 			(void) mi_strlog(ill->ill_rq, 1,
17366 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
17367 			    "ill_dlpi_done: unsolicited ack for %s from %s\n",
17368 			    dlpi_prim_str(prim), ill->ill_name);
17369 		} else {
17370 			(void) mi_strlog(ill->ill_rq, 1,
17371 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
17372 			    "ill_dlpi_done: unexpected ack for %s from %s "
17373 			    "(expecting ack for %s)\n",
17374 			    dlpi_prim_str(prim), ill->ill_name,
17375 			    dlpi_prim_str(ill->ill_dlpi_pending));
17376 		}
17377 		return;
17378 	}
17379 
17380 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
17381 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
17382 
17383 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
17384 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
17385 		return;
17386 	}
17387 
17388 	ill->ill_dlpi_deferred = mp->b_next;
17389 	mp->b_next = NULL;
17390 
17391 	ill_dlpi_dispatch(ill, mp);
17392 }
17393 
17394 void
17395 conn_delete_ire(conn_t *connp, caddr_t arg)
17396 {
17397 	ipif_t	*ipif = (ipif_t *)arg;
17398 	ire_t	*ire;
17399 
17400 	/*
17401 	 * Look at the cached ires on conns which has pointers to ipifs.
17402 	 * We just call ire_refrele which clears up the reference
17403 	 * to ire. Called when a conn closes. Also called from ipif_free
17404 	 * to cleanup indirect references to the stale ipif via the cached ire.
17405 	 */
17406 	mutex_enter(&connp->conn_lock);
17407 	ire = connp->conn_ire_cache;
17408 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
17409 		connp->conn_ire_cache = NULL;
17410 		mutex_exit(&connp->conn_lock);
17411 		IRE_REFRELE_NOTR(ire);
17412 		return;
17413 	}
17414 	mutex_exit(&connp->conn_lock);
17415 
17416 }
17417 
17418 /*
17419  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
17420  * of IREs. Those IREs may have been previously cached in the conn structure.
17421  * This ipcl_walk() walker function releases all references to such IREs based
17422  * on the condemned flag.
17423  */
17424 /* ARGSUSED */
17425 void
17426 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
17427 {
17428 	ire_t	*ire;
17429 
17430 	mutex_enter(&connp->conn_lock);
17431 	ire = connp->conn_ire_cache;
17432 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
17433 		connp->conn_ire_cache = NULL;
17434 		mutex_exit(&connp->conn_lock);
17435 		IRE_REFRELE_NOTR(ire);
17436 		return;
17437 	}
17438 	mutex_exit(&connp->conn_lock);
17439 }
17440 
17441 /*
17442  * Take down a specific interface, but don't lose any information about it.
17443  * Also delete interface from its interface group (ifgrp).
17444  * (Always called as writer.)
17445  * This function goes through the down sequence even if the interface is
17446  * already down. There are 2 reasons.
17447  * a. Currently we permit interface routes that depend on down interfaces
17448  *    to be added. This behaviour itself is questionable. However it appears
17449  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
17450  *    time. We go thru the cleanup in order to remove these routes.
17451  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
17452  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
17453  *    down, but we need to cleanup i.e. do ill_dl_down and
17454  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
17455  *
17456  * IP-MT notes:
17457  *
17458  * Model of reference to interfaces.
17459  *
17460  * The following members in ipif_t track references to the ipif.
17461  *	int     ipif_refcnt;    Active reference count
17462  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
17463  * The following members in ill_t track references to the ill.
17464  *	int             ill_refcnt;     active refcnt
17465  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
17466  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
17467  *
17468  * Reference to an ipif or ill can be obtained in any of the following ways.
17469  *
17470  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
17471  * Pointers to ipif / ill from other data structures viz ire and conn.
17472  * Implicit reference to the ipif / ill by holding a reference to the ire.
17473  *
17474  * The ipif/ill lookup functions return a reference held ipif / ill.
17475  * ipif_refcnt and ill_refcnt track the reference counts respectively.
17476  * This is a purely dynamic reference count associated with threads holding
17477  * references to the ipif / ill. Pointers from other structures do not
17478  * count towards this reference count.
17479  *
17480  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
17481  * ipif/ill. This is incremented whenever a new ire is created referencing the
17482  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
17483  * actually added to the ire hash table. The count is decremented in
17484  * ire_inactive where the ire is destroyed.
17485  *
17486  * nce's reference ill's thru nce_ill and the count of nce's associated with
17487  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
17488  * ndp_add() where the nce is actually added to the table. Similarly it is
17489  * decremented in ndp_inactive where the nce is destroyed.
17490  *
17491  * Flow of ioctls involving interface down/up
17492  *
17493  * The following is the sequence of an attempt to set some critical flags on an
17494  * up interface.
17495  * ip_sioctl_flags
17496  * ipif_down
17497  * wait for ipif to be quiescent
17498  * ipif_down_tail
17499  * ip_sioctl_flags_tail
17500  *
17501  * All set ioctls that involve down/up sequence would have a skeleton similar
17502  * to the above. All the *tail functions are called after the refcounts have
17503  * dropped to the appropriate values.
17504  *
17505  * The mechanism to quiesce an ipif is as follows.
17506  *
17507  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
17508  * on the ipif. Callers either pass a flag requesting wait or the lookup
17509  *  functions will return NULL.
17510  *
17511  * Delete all ires referencing this ipif
17512  *
17513  * Any thread attempting to do an ipif_refhold on an ipif that has been
17514  * obtained thru a cached pointer will first make sure that
17515  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
17516  * increment the refcount.
17517  *
17518  * The above guarantees that the ipif refcount will eventually come down to
17519  * zero and the ipif will quiesce, once all threads that currently hold a
17520  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
17521  * ipif_refcount has dropped to zero and all ire's associated with this ipif
17522  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
17523  * drop to zero.
17524  *
17525  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
17526  *
17527  * Threads trying to lookup an ipif or ill can pass a flag requesting
17528  * wait and restart if the ipif / ill cannot be looked up currently.
17529  * For eg. bind, and route operations (Eg. route add / delete) cannot return
17530  * failure if the ipif is currently undergoing an exclusive operation, and
17531  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
17532  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
17533  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
17534  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
17535  * change while the ill_lock is held. Before dropping the ill_lock we acquire
17536  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
17537  * until we release the ipsq_lock, even though the the ill/ipif state flags
17538  * can change after we drop the ill_lock.
17539  *
17540  * An attempt to send out a packet using an ipif that is currently
17541  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
17542  * operation and restart it later when the exclusive condition on the ipif ends.
17543  * This is an example of not passing the wait flag to the lookup functions. For
17544  * example an attempt to refhold and use conn->conn_multicast_ipif and send
17545  * out a multicast packet on that ipif will fail while the ipif is
17546  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
17547  * currently IPIF_CHANGING will also fail.
17548  */
17549 int
17550 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
17551 {
17552 	ill_t		*ill = ipif->ipif_ill;
17553 	phyint_t	*phyi;
17554 	conn_t		*connp;
17555 	boolean_t	success;
17556 	boolean_t	ipif_was_up = B_FALSE;
17557 
17558 	ASSERT(IAM_WRITER_IPIF(ipif));
17559 
17560 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
17561 
17562 	if (ipif->ipif_flags & IPIF_UP) {
17563 		mutex_enter(&ill->ill_lock);
17564 		ipif->ipif_flags &= ~IPIF_UP;
17565 		ASSERT(ill->ill_ipif_up_count > 0);
17566 		--ill->ill_ipif_up_count;
17567 		mutex_exit(&ill->ill_lock);
17568 		ipif_was_up = B_TRUE;
17569 		/* Update status in SCTP's list */
17570 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
17571 	}
17572 
17573 	/*
17574 	 * Blow away v6 memberships we established in ipif_multicast_up(); the
17575 	 * v4 ones are left alone (as is the ipif_multicast_up flag, so we
17576 	 * know not to rejoin when the interface is brought back up).
17577 	 */
17578 	if (ipif->ipif_isv6)
17579 		ipif_multicast_down(ipif);
17580 	/*
17581 	 * Remove from the mapping for __sin6_src_id. We insert only
17582 	 * when the address is not INADDR_ANY. As IPv4 addresses are
17583 	 * stored as mapped addresses, we need to check for mapped
17584 	 * INADDR_ANY also.
17585 	 */
17586 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
17587 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
17588 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
17589 		int err;
17590 
17591 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
17592 		    ipif->ipif_zoneid);
17593 		if (err != 0) {
17594 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
17595 		}
17596 	}
17597 
17598 	/*
17599 	 * Before we delete the ill from the group (if any), we need
17600 	 * to make sure that we delete all the routes dependent on
17601 	 * this and also any ipifs dependent on this ipif for
17602 	 * source address. We need to do before we delete from
17603 	 * the group because
17604 	 *
17605 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
17606 	 *
17607 	 * 2) ipif_update_other_ipifs needs to walk the whole group
17608 	 *    for re-doing source address selection. Note that
17609 	 *    ipif_select_source[_v6] called from
17610 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
17611 	 *    because we have already marked down here i.e cleared
17612 	 *    IPIF_UP.
17613 	 */
17614 	if (ipif->ipif_isv6)
17615 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES);
17616 	else
17617 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES);
17618 
17619 	/*
17620 	 * Need to add these also to be saved and restored when the
17621 	 * ipif is brought down and up
17622 	 */
17623 	mutex_enter(&ire_mrtun_lock);
17624 	if (ire_mrtun_count != 0) {
17625 		mutex_exit(&ire_mrtun_lock);
17626 		ire_walk_ill_mrtun(0, 0, ipif_down_delete_ire,
17627 		    (char *)ipif, NULL);
17628 	} else {
17629 		mutex_exit(&ire_mrtun_lock);
17630 	}
17631 
17632 	mutex_enter(&ire_srcif_table_lock);
17633 	if (ire_srcif_table_count > 0) {
17634 		mutex_exit(&ire_srcif_table_lock);
17635 		ire_walk_srcif_table_v4(ipif_down_delete_ire, (char *)ipif);
17636 	} else {
17637 		mutex_exit(&ire_srcif_table_lock);
17638 	}
17639 
17640 	/*
17641 	 * Cleaning up the conn_ire_cache or conns must be done only after the
17642 	 * ires have been deleted above. Otherwise a thread could end up
17643 	 * caching an ire in a conn after we have finished the cleanup of the
17644 	 * conn. The caching is done after making sure that the ire is not yet
17645 	 * condemned. Also documented in the block comment above ip_output
17646 	 */
17647 	ipcl_walk(conn_cleanup_stale_ire, NULL);
17648 	/* Also, delete the ires cached in SCTP */
17649 	sctp_ire_cache_flush(ipif);
17650 
17651 	/* Resolve any IPsec/IKE NAT-T instances that depend on this ipif. */
17652 	nattymod_clean_ipif(ipif);
17653 
17654 	/*
17655 	 * Update any other ipifs which have used "our" local address as
17656 	 * a source address. This entails removing and recreating IRE_INTERFACE
17657 	 * entries for such ipifs.
17658 	 */
17659 	if (ipif->ipif_isv6)
17660 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
17661 	else
17662 		ipif_update_other_ipifs(ipif, ill->ill_group);
17663 
17664 	if (ipif_was_up) {
17665 		/*
17666 		 * Check whether it is last ipif to leave this group.
17667 		 * If this is the last ipif to leave, we should remove
17668 		 * this ill from the group as ipif_select_source will not
17669 		 * be able to find any useful ipifs if this ill is selected
17670 		 * for load balancing.
17671 		 *
17672 		 * For nameless groups, we should call ifgrp_delete if this
17673 		 * belongs to some group. As this ipif is going down, we may
17674 		 * need to reconstruct groups.
17675 		 */
17676 		phyi = ill->ill_phyint;
17677 		/*
17678 		 * If the phyint_groupname_len is 0, it may or may not
17679 		 * be in the nameless group. If the phyint_groupname_len is
17680 		 * not 0, then this ill should be part of some group.
17681 		 * As we always insert this ill in the group if
17682 		 * phyint_groupname_len is not zero when the first ipif
17683 		 * comes up (in ipif_up_done), it should be in a group
17684 		 * when the namelen is not 0.
17685 		 *
17686 		 * NOTE : When we delete the ill from the group,it will
17687 		 * blow away all the IRE_CACHES pointing either at this ipif or
17688 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
17689 		 * should be pointing at this ill.
17690 		 */
17691 		ASSERT(phyi->phyint_groupname_len == 0 ||
17692 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
17693 
17694 		if (phyi->phyint_groupname_len != 0) {
17695 			if (ill->ill_ipif_up_count == 0)
17696 				illgrp_delete(ill);
17697 		}
17698 
17699 		/*
17700 		 * If we have deleted some of the broadcast ires associated
17701 		 * with this ipif, we need to re-nominate somebody else if
17702 		 * the ires that we deleted were the nominated ones.
17703 		 */
17704 		if (ill->ill_group != NULL && !ill->ill_isv6)
17705 			ipif_renominate_bcast(ipif);
17706 	}
17707 
17708 	if (ipif->ipif_isv6)
17709 		ipif_ndp_down(ipif);
17710 
17711 	/*
17712 	 * If mp is NULL the caller will wait for the appropriate refcnt.
17713 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
17714 	 * and ill_delete -> ipif_free -> ipif_down
17715 	 */
17716 	if (mp == NULL) {
17717 		ASSERT(q == NULL);
17718 		return (0);
17719 	}
17720 
17721 	if (CONN_Q(q)) {
17722 		connp = Q_TO_CONN(q);
17723 		mutex_enter(&connp->conn_lock);
17724 	} else {
17725 		connp = NULL;
17726 	}
17727 	mutex_enter(&ill->ill_lock);
17728 	/*
17729 	 * Are there any ire's pointing to this ipif that are still active ?
17730 	 * If this is the last ipif going down, are there any ire's pointing
17731 	 * to this ill that are still active ?
17732 	 */
17733 	if (ipif_is_quiescent(ipif)) {
17734 		mutex_exit(&ill->ill_lock);
17735 		if (connp != NULL)
17736 			mutex_exit(&connp->conn_lock);
17737 		return (0);
17738 	}
17739 
17740 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
17741 	    ill->ill_name, (void *)ill));
17742 	/*
17743 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
17744 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
17745 	 * which in turn is called by the last refrele on the ipif/ill/ire.
17746 	 */
17747 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
17748 	if (!success) {
17749 		/* The conn is closing. So just return */
17750 		ASSERT(connp != NULL);
17751 		mutex_exit(&ill->ill_lock);
17752 		mutex_exit(&connp->conn_lock);
17753 		return (EINTR);
17754 	}
17755 
17756 	mutex_exit(&ill->ill_lock);
17757 	if (connp != NULL)
17758 		mutex_exit(&connp->conn_lock);
17759 	return (EINPROGRESS);
17760 }
17761 
17762 static void
17763 ipif_down_tail(ipif_t *ipif)
17764 {
17765 	ill_t	*ill = ipif->ipif_ill;
17766 
17767 	/*
17768 	 * Skip any loopback interface (null wq).
17769 	 * If this is the last logical interface on the ill
17770 	 * have ill_dl_down tell the driver we are gone (unbind)
17771 	 * Note that lun 0 can ipif_down even though
17772 	 * there are other logical units that are up.
17773 	 * This occurs e.g. when we change a "significant" IFF_ flag.
17774 	 */
17775 	if (ipif->ipif_ill->ill_wq != NULL) {
17776 		if (!ill->ill_logical_down && (ill->ill_ipif_up_count == 0) &&
17777 		    ill->ill_dl_up) {
17778 			ill_dl_down(ill);
17779 		}
17780 	}
17781 	ill->ill_logical_down = 0;
17782 
17783 	/*
17784 	 * Have to be after removing the routes in ipif_down_delete_ire.
17785 	 */
17786 	if (ipif->ipif_isv6) {
17787 		if (ipif->ipif_ill->ill_flags & ILLF_XRESOLV)
17788 			ipif_arp_down(ipif);
17789 	} else {
17790 		ipif_arp_down(ipif);
17791 	}
17792 
17793 	ip_rts_ifmsg(ipif);
17794 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
17795 }
17796 
17797 /*
17798  * Bring interface logically down without bringing the physical interface
17799  * down e.g. when the netmask is changed. This avoids long lasting link
17800  * negotiations between an ethernet interface and a certain switches.
17801  */
17802 static int
17803 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
17804 {
17805 	/*
17806 	 * The ill_logical_down flag is a transient flag. It is set here
17807 	 * and is cleared once the down has completed in ipif_down_tail.
17808 	 * This flag does not indicate whether the ill stream is in the
17809 	 * DL_BOUND state with the driver. Instead this flag is used by
17810 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
17811 	 * the driver. The state of the ill stream i.e. whether it is
17812 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
17813 	 */
17814 	ipif->ipif_ill->ill_logical_down = 1;
17815 	return (ipif_down(ipif, q, mp));
17816 }
17817 
17818 /*
17819  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
17820  * If the usesrc client ILL is already part of a usesrc group or not,
17821  * in either case a ire_stq with the matching usesrc client ILL will
17822  * locate the IRE's that need to be deleted. We want IREs to be created
17823  * with the new source address.
17824  */
17825 static void
17826 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
17827 {
17828 	ill_t	*ucill = (ill_t *)ill_arg;
17829 
17830 	ASSERT(IAM_WRITER_ILL(ucill));
17831 
17832 	if (ire->ire_stq == NULL)
17833 		return;
17834 
17835 	if ((ire->ire_type == IRE_CACHE) &&
17836 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
17837 		ire_delete(ire);
17838 }
17839 
17840 /*
17841  * ire_walk routine to delete every IRE dependent on the interface
17842  * address that is going down.	(Always called as writer.)
17843  * Works for both v4 and v6.
17844  * In addition for checking for ire_ipif matches it also checks for
17845  * IRE_CACHE entries which have the same source address as the
17846  * disappearing ipif since ipif_select_source might have picked
17847  * that source. Note that ipif_down/ipif_update_other_ipifs takes
17848  * care of any IRE_INTERFACE with the disappearing source address.
17849  */
17850 static void
17851 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
17852 {
17853 	ipif_t	*ipif = (ipif_t *)ipif_arg;
17854 	ill_t *ire_ill;
17855 	ill_t *ipif_ill;
17856 
17857 	ASSERT(IAM_WRITER_IPIF(ipif));
17858 	if (ire->ire_ipif == NULL)
17859 		return;
17860 
17861 	/*
17862 	 * For IPv4, we derive source addresses for an IRE from ipif's
17863 	 * belonging to the same IPMP group as the IRE's outgoing
17864 	 * interface.  If an IRE's outgoing interface isn't in the
17865 	 * same IPMP group as a particular ipif, then that ipif
17866 	 * couldn't have been used as a source address for this IRE.
17867 	 *
17868 	 * For IPv6, source addresses are only restricted to the IPMP group
17869 	 * if the IRE is for a link-local address or a multicast address.
17870 	 * Otherwise, source addresses for an IRE can be chosen from
17871 	 * interfaces other than the the outgoing interface for that IRE.
17872 	 *
17873 	 * For source address selection details, see ipif_select_source()
17874 	 * and ipif_select_source_v6().
17875 	 */
17876 	if (ire->ire_ipversion == IPV4_VERSION ||
17877 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
17878 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
17879 		ire_ill = ire->ire_ipif->ipif_ill;
17880 		ipif_ill = ipif->ipif_ill;
17881 
17882 		if (ire_ill->ill_group != ipif_ill->ill_group) {
17883 			return;
17884 		}
17885 	}
17886 
17887 
17888 	if (ire->ire_ipif != ipif) {
17889 		/*
17890 		 * Look for a matching source address.
17891 		 */
17892 		if (ire->ire_type != IRE_CACHE)
17893 			return;
17894 		if (ipif->ipif_flags & IPIF_NOLOCAL)
17895 			return;
17896 
17897 		if (ire->ire_ipversion == IPV4_VERSION) {
17898 			if (ire->ire_src_addr != ipif->ipif_src_addr)
17899 				return;
17900 		} else {
17901 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
17902 			    &ipif->ipif_v6lcl_addr))
17903 				return;
17904 		}
17905 		ire_delete(ire);
17906 		return;
17907 	}
17908 	/*
17909 	 * ire_delete() will do an ire_flush_cache which will delete
17910 	 * all ire_ipif matches
17911 	 */
17912 	ire_delete(ire);
17913 }
17914 
17915 /*
17916  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
17917  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
17918  * 2) when an interface is brought up or down (on that ill).
17919  * This ensures that the IRE_CACHE entries don't retain stale source
17920  * address selection results.
17921  */
17922 void
17923 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
17924 {
17925 	ill_t	*ill = (ill_t *)ill_arg;
17926 	ill_t	*ipif_ill;
17927 
17928 	ASSERT(IAM_WRITER_ILL(ill));
17929 	/*
17930 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
17931 	 * Hence this should be IRE_CACHE.
17932 	 */
17933 	ASSERT(ire->ire_type == IRE_CACHE);
17934 
17935 	/*
17936 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
17937 	 * We are only interested in IRE_CACHES that has borrowed
17938 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
17939 	 * for which we need to look at ire_ipif->ipif_ill match
17940 	 * with ill.
17941 	 */
17942 	ASSERT(ire->ire_ipif != NULL);
17943 	ipif_ill = ire->ire_ipif->ipif_ill;
17944 	if (ipif_ill == ill || (ill->ill_group != NULL &&
17945 	    ipif_ill->ill_group == ill->ill_group)) {
17946 		ire_delete(ire);
17947 	}
17948 }
17949 
17950 /*
17951  * Delete all the ire whose stq references ill_arg.
17952  */
17953 static void
17954 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
17955 {
17956 	ill_t	*ill = (ill_t *)ill_arg;
17957 	ill_t	*ire_ill;
17958 
17959 	ASSERT(IAM_WRITER_ILL(ill));
17960 	/*
17961 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
17962 	 * Hence this should be IRE_CACHE.
17963 	 */
17964 	ASSERT(ire->ire_type == IRE_CACHE);
17965 
17966 	/*
17967 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
17968 	 * matches ill. We are only interested in IRE_CACHES that
17969 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
17970 	 * filtering here.
17971 	 */
17972 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
17973 
17974 	if (ire_ill == ill)
17975 		ire_delete(ire);
17976 }
17977 
17978 /*
17979  * This is called when an ill leaves the group. We want to delete
17980  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
17981  * pointing at ill.
17982  */
17983 static void
17984 illgrp_cache_delete(ire_t *ire, char *ill_arg)
17985 {
17986 	ill_t	*ill = (ill_t *)ill_arg;
17987 
17988 	ASSERT(IAM_WRITER_ILL(ill));
17989 	ASSERT(ill->ill_group == NULL);
17990 	/*
17991 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
17992 	 * Hence this should be IRE_CACHE.
17993 	 */
17994 	ASSERT(ire->ire_type == IRE_CACHE);
17995 	/*
17996 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
17997 	 * matches ill. We are interested in both.
17998 	 */
17999 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
18000 	    (ire->ire_ipif->ipif_ill == ill));
18001 
18002 	ire_delete(ire);
18003 }
18004 
18005 /*
18006  * Initiate deallocate of an IPIF. Always called as writer. Called by
18007  * ill_delete or ip_sioctl_removeif.
18008  */
18009 static void
18010 ipif_free(ipif_t *ipif)
18011 {
18012 	ASSERT(IAM_WRITER_IPIF(ipif));
18013 
18014 	/* Remove conn references */
18015 	reset_conn_ipif(ipif);
18016 
18017 	/*
18018 	 * Make sure we have valid net and subnet broadcast ire's for the
18019 	 * other ipif's which share them with this ipif.
18020 	 */
18021 	if (!ipif->ipif_isv6)
18022 		ipif_check_bcast_ires(ipif);
18023 
18024 	/*
18025 	 * Take down the interface. We can be called either from ill_delete
18026 	 * or from ip_sioctl_removeif.
18027 	 */
18028 	(void) ipif_down(ipif, NULL, NULL);
18029 
18030 	rw_enter(&ill_g_lock, RW_WRITER);
18031 	/* Remove pointers to this ill in the multicast routing tables */
18032 	reset_mrt_vif_ipif(ipif);
18033 	rw_exit(&ill_g_lock);
18034 }
18035 
18036 static void
18037 ipif_free_tail(ipif_t *ipif)
18038 {
18039 	mblk_t	*mp;
18040 	ipif_t	**ipifp;
18041 
18042 	/*
18043 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
18044 	 */
18045 	mutex_enter(&ipif->ipif_saved_ire_lock);
18046 	mp = ipif->ipif_saved_ire_mp;
18047 	ipif->ipif_saved_ire_mp = NULL;
18048 	mutex_exit(&ipif->ipif_saved_ire_lock);
18049 	freemsg(mp);
18050 
18051 	/*
18052 	 * Need to hold both ill_g_lock and ill_lock while
18053 	 * inserting or removing an ipif from the linked list
18054 	 * of ipifs hanging off the ill.
18055 	 */
18056 	rw_enter(&ill_g_lock, RW_WRITER);
18057 	/*
18058 	 * Remove all multicast memberships on the interface now.
18059 	 * This removes IPv4 multicast memberships joined within
18060 	 * the kernel as ipif_down does not do ipif_multicast_down
18061 	 * for IPv4. IPv6 is not handled here as the multicast memberships
18062 	 * are based on ill and not on ipif.
18063 	 */
18064 	ilm_free(ipif);
18065 
18066 	/*
18067 	 * Since we held the ill_g_lock while doing the ilm_free above,
18068 	 * we can assert the ilms were really deleted and not just marked
18069 	 * ILM_DELETED.
18070 	 */
18071 	ASSERT(ilm_walk_ipif(ipif) == 0);
18072 
18073 
18074 	IPIF_TRACE_CLEANUP(ipif);
18075 
18076 	/* Ask SCTP to take it out of it list */
18077 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
18078 
18079 	mutex_enter(&ipif->ipif_ill->ill_lock);
18080 	/* Get it out of the ILL interface list. */
18081 	ipifp = &ipif->ipif_ill->ill_ipif;
18082 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
18083 		if (*ipifp == ipif) {
18084 			*ipifp = ipif->ipif_next;
18085 			break;
18086 		}
18087 	}
18088 
18089 	mutex_exit(&ipif->ipif_ill->ill_lock);
18090 	rw_exit(&ill_g_lock);
18091 
18092 	mutex_destroy(&ipif->ipif_saved_ire_lock);
18093 	/* Free the memory. */
18094 	mi_free((char *)ipif);
18095 }
18096 
18097 /*
18098  * Returns an ipif name in the form "ill_name/unit" if ipif_id is not zero,
18099  * "ill_name" otherwise.
18100  */
18101 char *
18102 ipif_get_name(const ipif_t *ipif, char *buf, int len)
18103 {
18104 	char	lbuf[32];
18105 	char	*name;
18106 	size_t	name_len;
18107 
18108 	buf[0] = '\0';
18109 	if (!ipif)
18110 		return (buf);
18111 	name = ipif->ipif_ill->ill_name;
18112 	name_len = ipif->ipif_ill->ill_name_length;
18113 	if (ipif->ipif_id != 0) {
18114 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
18115 		    ipif->ipif_id);
18116 		name = lbuf;
18117 		name_len = mi_strlen(name) + 1;
18118 	}
18119 	len -= 1;
18120 	buf[len] = '\0';
18121 	len = MIN(len, name_len);
18122 	bcopy(name, buf, len);
18123 	return (buf);
18124 }
18125 
18126 /*
18127  * Find an IPIF based on the name passed in.  Names can be of the
18128  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
18129  * The <phys> string can have forms like <dev><#> (e.g., le0),
18130  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
18131  * When there is no colon, the implied unit id is zero. <phys> must
18132  * correspond to the name of an ILL.  (May be called as writer.)
18133  */
18134 static ipif_t *
18135 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
18136     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
18137     mblk_t *mp, ipsq_func_t func, int *error)
18138 {
18139 	char	*cp;
18140 	char	*endp;
18141 	long	id;
18142 	ill_t	*ill;
18143 	ipif_t	*ipif;
18144 	uint_t	ire_type;
18145 	boolean_t did_alloc = B_FALSE;
18146 	ipsq_t	*ipsq;
18147 
18148 	if (error != NULL)
18149 		*error = 0;
18150 
18151 	/*
18152 	 * If the caller wants to us to create the ipif, make sure we have a
18153 	 * valid zoneid
18154 	 */
18155 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
18156 
18157 	if (namelen == 0) {
18158 		if (error != NULL)
18159 			*error = ENXIO;
18160 		return (NULL);
18161 	}
18162 
18163 	*exists = B_FALSE;
18164 	/* Look for a colon in the name. */
18165 	endp = &name[namelen];
18166 	for (cp = endp; --cp > name; ) {
18167 		if (*cp == IPIF_SEPARATOR_CHAR)
18168 			break;
18169 	}
18170 
18171 	if (*cp == IPIF_SEPARATOR_CHAR) {
18172 		/*
18173 		 * Reject any non-decimal aliases for logical
18174 		 * interfaces. Aliases with leading zeroes
18175 		 * are also rejected as they introduce ambiguity
18176 		 * in the naming of the interfaces.
18177 		 * In order to confirm with existing semantics,
18178 		 * and to not break any programs/script relying
18179 		 * on that behaviour, if<0>:0 is considered to be
18180 		 * a valid interface.
18181 		 *
18182 		 * If alias has two or more digits and the first
18183 		 * is zero, fail.
18184 		 */
18185 		if (&cp[2] < endp && cp[1] == '0')
18186 			return (NULL);
18187 	}
18188 
18189 	if (cp <= name) {
18190 		cp = endp;
18191 	} else {
18192 		*cp = '\0';
18193 	}
18194 
18195 	/*
18196 	 * Look up the ILL, based on the portion of the name
18197 	 * before the slash. ill_lookup_on_name returns a held ill.
18198 	 * Temporary to check whether ill exists already. If so
18199 	 * ill_lookup_on_name will clear it.
18200 	 */
18201 	ill = ill_lookup_on_name(name, do_alloc, isv6,
18202 	    q, mp, func, error, &did_alloc);
18203 	if (cp != endp)
18204 		*cp = IPIF_SEPARATOR_CHAR;
18205 	if (ill == NULL)
18206 		return (NULL);
18207 
18208 	/* Establish the unit number in the name. */
18209 	id = 0;
18210 	if (cp < endp && *endp == '\0') {
18211 		/* If there was a colon, the unit number follows. */
18212 		cp++;
18213 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
18214 			ill_refrele(ill);
18215 			if (error != NULL)
18216 				*error = ENXIO;
18217 			return (NULL);
18218 		}
18219 	}
18220 
18221 	GRAB_CONN_LOCK(q);
18222 	mutex_enter(&ill->ill_lock);
18223 	/* Now see if there is an IPIF with this unit number. */
18224 	for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
18225 		if (ipif->ipif_id == id) {
18226 			if (zoneid != ALL_ZONES &&
18227 			    zoneid != ipif->ipif_zoneid &&
18228 			    ipif->ipif_zoneid != ALL_ZONES) {
18229 				mutex_exit(&ill->ill_lock);
18230 				RELEASE_CONN_LOCK(q);
18231 				ill_refrele(ill);
18232 				if (error != NULL)
18233 					*error = ENXIO;
18234 				return (NULL);
18235 			}
18236 			/*
18237 			 * The block comment at the start of ipif_down
18238 			 * explains the use of the macros used below
18239 			 */
18240 			if (IPIF_CAN_LOOKUP(ipif)) {
18241 				ipif_refhold_locked(ipif);
18242 				mutex_exit(&ill->ill_lock);
18243 				if (!did_alloc)
18244 					*exists = B_TRUE;
18245 				/*
18246 				 * Drop locks before calling ill_refrele
18247 				 * since it can potentially call into
18248 				 * ipif_ill_refrele_tail which can end up
18249 				 * in trying to acquire any lock.
18250 				 */
18251 				RELEASE_CONN_LOCK(q);
18252 				ill_refrele(ill);
18253 				return (ipif);
18254 			} else if (IPIF_CAN_WAIT(ipif, q)) {
18255 				ipsq = ill->ill_phyint->phyint_ipsq;
18256 				mutex_enter(&ipsq->ipsq_lock);
18257 				mutex_exit(&ill->ill_lock);
18258 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
18259 				mutex_exit(&ipsq->ipsq_lock);
18260 				RELEASE_CONN_LOCK(q);
18261 				ill_refrele(ill);
18262 				*error = EINPROGRESS;
18263 				return (NULL);
18264 			}
18265 		}
18266 	}
18267 	RELEASE_CONN_LOCK(q);
18268 
18269 	if (!do_alloc) {
18270 		mutex_exit(&ill->ill_lock);
18271 		ill_refrele(ill);
18272 		if (error != NULL)
18273 			*error = ENXIO;
18274 		return (NULL);
18275 	}
18276 
18277 	/*
18278 	 * If none found, atomically allocate and return a new one.
18279 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
18280 	 * to support "receive only" use of lo0:1 etc. as is still done
18281 	 * below as an initial guess.
18282 	 * However, this is now likely to be overriden later in ipif_up_done()
18283 	 * when we know for sure what address has been configured on the
18284 	 * interface, since we might have more than one loopback interface
18285 	 * with a loopback address, e.g. in the case of zones, and all the
18286 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
18287 	 */
18288 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
18289 		ire_type = IRE_LOOPBACK;
18290 	else
18291 		ire_type = IRE_LOCAL;
18292 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
18293 	if (ipif != NULL)
18294 		ipif_refhold_locked(ipif);
18295 	else if (error != NULL)
18296 		*error = ENOMEM;
18297 	mutex_exit(&ill->ill_lock);
18298 	ill_refrele(ill);
18299 	return (ipif);
18300 }
18301 
18302 /*
18303  * This routine is called whenever a new address comes up on an ipif.  If
18304  * we are configured to respond to address mask requests, then we are supposed
18305  * to broadcast an address mask reply at this time.  This routine is also
18306  * called if we are already up, but a netmask change is made.  This is legal
18307  * but might not make the system manager very popular.	(May be called
18308  * as writer.)
18309  */
18310 static void
18311 ipif_mask_reply(ipif_t *ipif)
18312 {
18313 	icmph_t	*icmph;
18314 	ipha_t	*ipha;
18315 	mblk_t	*mp;
18316 
18317 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
18318 
18319 	if (!ip_respond_to_address_mask_broadcast)
18320 		return;
18321 
18322 	/* ICMP mask reply is IPv4 only */
18323 	ASSERT(!ipif->ipif_isv6);
18324 	/* ICMP mask reply is not for a loopback interface */
18325 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
18326 
18327 	mp = allocb(REPLY_LEN, BPRI_HI);
18328 	if (mp == NULL)
18329 		return;
18330 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
18331 
18332 	ipha = (ipha_t *)mp->b_rptr;
18333 	bzero(ipha, REPLY_LEN);
18334 	*ipha = icmp_ipha;
18335 	ipha->ipha_ttl = ip_broadcast_ttl;
18336 	ipha->ipha_src = ipif->ipif_src_addr;
18337 	ipha->ipha_dst = ipif->ipif_brd_addr;
18338 	ipha->ipha_length = htons(REPLY_LEN);
18339 	ipha->ipha_ident = 0;
18340 
18341 	icmph = (icmph_t *)&ipha[1];
18342 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
18343 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
18344 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
18345 	if (icmph->icmph_checksum == 0)
18346 		icmph->icmph_checksum = 0xffff;
18347 
18348 	put(ipif->ipif_wq, mp);
18349 
18350 #undef	REPLY_LEN
18351 }
18352 
18353 /*
18354  * When the mtu in the ipif changes, we call this routine through ire_walk
18355  * to update all the relevant IREs.
18356  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
18357  */
18358 static void
18359 ipif_mtu_change(ire_t *ire, char *ipif_arg)
18360 {
18361 	ipif_t *ipif = (ipif_t *)ipif_arg;
18362 
18363 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
18364 		return;
18365 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
18366 }
18367 
18368 /*
18369  * When the mtu in the ill changes, we call this routine through ire_walk
18370  * to update all the relevant IREs.
18371  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
18372  */
18373 void
18374 ill_mtu_change(ire_t *ire, char *ill_arg)
18375 {
18376 	ill_t	*ill = (ill_t *)ill_arg;
18377 
18378 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
18379 		return;
18380 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
18381 }
18382 
18383 /*
18384  * Join the ipif specific multicast groups.
18385  * Must be called after a mapping has been set up in the resolver.  (Always
18386  * called as writer.)
18387  */
18388 void
18389 ipif_multicast_up(ipif_t *ipif)
18390 {
18391 	int err, index;
18392 	ill_t *ill;
18393 
18394 	ASSERT(IAM_WRITER_IPIF(ipif));
18395 
18396 	ill = ipif->ipif_ill;
18397 	index = ill->ill_phyint->phyint_ifindex;
18398 
18399 	ip1dbg(("ipif_multicast_up\n"));
18400 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
18401 		return;
18402 
18403 	if (ipif->ipif_isv6) {
18404 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
18405 			return;
18406 
18407 		/* Join the all hosts multicast address */
18408 		ip1dbg(("ipif_multicast_up - addmulti\n"));
18409 		/*
18410 		 * Passing B_TRUE means we have to join the multicast
18411 		 * membership on this interface even though this is
18412 		 * FAILED. If we join on a different one in the group,
18413 		 * we will not be able to delete the membership later
18414 		 * as we currently don't track where we join when we
18415 		 * join within the kernel unlike applications where
18416 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
18417 		 * for more on this.
18418 		 */
18419 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
18420 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
18421 		if (err != 0) {
18422 			ip0dbg(("ipif_multicast_up: "
18423 			    "all_hosts_mcast failed %d\n",
18424 			    err));
18425 			return;
18426 		}
18427 		/*
18428 		 * Enable multicast for the solicited node multicast address
18429 		 */
18430 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
18431 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
18432 
18433 			ipv6_multi.s6_addr32[3] |=
18434 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
18435 
18436 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
18437 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
18438 			    NULL);
18439 			if (err != 0) {
18440 				ip0dbg(("ipif_multicast_up: solicited MC"
18441 				    " failed %d\n", err));
18442 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
18443 				    ill, ill->ill_phyint->phyint_ifindex,
18444 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
18445 				return;
18446 			}
18447 		}
18448 	} else {
18449 		if (ipif->ipif_lcl_addr == INADDR_ANY)
18450 			return;
18451 
18452 		/* Join the all hosts multicast address */
18453 		ip1dbg(("ipif_multicast_up - addmulti\n"));
18454 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
18455 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
18456 		if (err) {
18457 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
18458 			return;
18459 		}
18460 	}
18461 	ipif->ipif_multicast_up = 1;
18462 }
18463 
18464 /*
18465  * Blow away any IPv6 multicast groups that we joined in ipif_multicast_up();
18466  * any explicit memberships are blown away in ill_leave_multicast() when the
18467  * ill is brought down.
18468  */
18469 static void
18470 ipif_multicast_down(ipif_t *ipif)
18471 {
18472 	int err;
18473 
18474 	ASSERT(IAM_WRITER_IPIF(ipif));
18475 
18476 	ip1dbg(("ipif_multicast_down\n"));
18477 	if (!ipif->ipif_multicast_up)
18478 		return;
18479 
18480 	ASSERT(ipif->ipif_isv6);
18481 
18482 	ip1dbg(("ipif_multicast_down - delmulti\n"));
18483 
18484 	/*
18485 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
18486 	 * we should look for ilms on this ill rather than the ones that have
18487 	 * been failed over here.  They are here temporarily. As
18488 	 * ipif_multicast_up has joined on this ill, we should delete only
18489 	 * from this ill.
18490 	 */
18491 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
18492 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
18493 	    B_TRUE, B_TRUE);
18494 	if (err != 0) {
18495 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
18496 		    err));
18497 	}
18498 	/*
18499 	 * Disable multicast for the solicited node multicast address
18500 	 */
18501 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
18502 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
18503 
18504 		ipv6_multi.s6_addr32[3] |=
18505 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
18506 
18507 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
18508 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
18509 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
18510 
18511 		if (err != 0) {
18512 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
18513 			    err));
18514 		}
18515 	}
18516 
18517 	ipif->ipif_multicast_up = 0;
18518 }
18519 
18520 /*
18521  * Used when an interface comes up to recreate any extra routes on this
18522  * interface.
18523  */
18524 static ire_t **
18525 ipif_recover_ire(ipif_t *ipif)
18526 {
18527 	mblk_t	*mp;
18528 	ire_t	**ipif_saved_irep;
18529 	ire_t	**irep;
18530 
18531 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
18532 	    ipif->ipif_id));
18533 
18534 	mutex_enter(&ipif->ipif_saved_ire_lock);
18535 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
18536 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
18537 	if (ipif_saved_irep == NULL) {
18538 		mutex_exit(&ipif->ipif_saved_ire_lock);
18539 		return (NULL);
18540 	}
18541 
18542 	irep = ipif_saved_irep;
18543 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
18544 		ire_t		*ire;
18545 		queue_t		*rfq;
18546 		queue_t		*stq;
18547 		ifrt_t		*ifrt;
18548 		uchar_t		*src_addr;
18549 		uchar_t		*gateway_addr;
18550 		mblk_t		*resolver_mp;
18551 		ushort_t	type;
18552 
18553 		/*
18554 		 * When the ire was initially created and then added in
18555 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
18556 		 * in the case of a traditional interface route, or as one of
18557 		 * the IRE_OFFSUBNET types (with the exception of
18558 		 * IRE_HOST_REDIRECT which is created by icmp_redirect() and
18559 		 * which we don't need to save or recover).  In the case where
18560 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
18561 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
18562 		 * to satisfy software like GateD and Sun Cluster which creates
18563 		 * routes using the the loopback interface's address as a
18564 		 * gateway.
18565 		 *
18566 		 * As ifrt->ifrt_type reflects the already updated ire_type and
18567 		 * since ire_create() expects that IRE_IF_NORESOLVER will have
18568 		 * a valid ire_dlureq_mp field (which doesn't make sense for a
18569 		 * IRE_LOOPBACK), ire_create() will be called in the same way
18570 		 * here as in ip_rt_add(), namely using ipif->ipif_net_type when
18571 		 * the route looks like a traditional interface route (where
18572 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
18573 		 * the saved ifrt->ifrt_type.  This means that in the case where
18574 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
18575 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
18576 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
18577 		 */
18578 		ifrt = (ifrt_t *)mp->b_rptr;
18579 		if (ifrt->ifrt_type & IRE_INTERFACE) {
18580 			rfq = NULL;
18581 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
18582 			    ? ipif->ipif_rq : ipif->ipif_wq;
18583 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
18584 			    ? (uint8_t *)&ifrt->ifrt_src_addr
18585 			    : (uint8_t *)&ipif->ipif_src_addr;
18586 			gateway_addr = NULL;
18587 			resolver_mp = ipif->ipif_resolver_mp;
18588 			type = ipif->ipif_net_type;
18589 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
18590 			/* Recover multiroute broadcast IRE. */
18591 			rfq = ipif->ipif_rq;
18592 			stq = ipif->ipif_wq;
18593 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
18594 			    ? (uint8_t *)&ifrt->ifrt_src_addr
18595 			    : (uint8_t *)&ipif->ipif_src_addr;
18596 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
18597 			resolver_mp = ipif->ipif_bcast_mp;
18598 			type = ifrt->ifrt_type;
18599 		} else {
18600 			rfq = NULL;
18601 			stq = NULL;
18602 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
18603 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
18604 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
18605 			resolver_mp = NULL;
18606 			type = ifrt->ifrt_type;
18607 		}
18608 
18609 		/*
18610 		 * Create a copy of the IRE with the saved address and netmask.
18611 		 */
18612 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
18613 		    "0x%x/0x%x\n",
18614 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
18615 		    ntohl(ifrt->ifrt_addr),
18616 		    ntohl(ifrt->ifrt_mask)));
18617 		ire = ire_create(
18618 		    (uint8_t *)&ifrt->ifrt_addr,
18619 		    (uint8_t *)&ifrt->ifrt_mask,
18620 		    src_addr,
18621 		    gateway_addr,
18622 		    NULL,
18623 		    &ifrt->ifrt_max_frag,
18624 		    NULL,
18625 		    rfq,
18626 		    stq,
18627 		    type,
18628 		    resolver_mp,
18629 		    ipif,
18630 		    NULL,
18631 		    0,
18632 		    0,
18633 		    0,
18634 		    ifrt->ifrt_flags,
18635 		    &ifrt->ifrt_iulp_info,
18636 		    NULL,
18637 		    NULL);
18638 
18639 		if (ire == NULL) {
18640 			mutex_exit(&ipif->ipif_saved_ire_lock);
18641 			kmem_free(ipif_saved_irep,
18642 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
18643 			return (NULL);
18644 		}
18645 
18646 		/*
18647 		 * Some software (for example, GateD and Sun Cluster) attempts
18648 		 * to create (what amount to) IRE_PREFIX routes with the
18649 		 * loopback address as the gateway.  This is primarily done to
18650 		 * set up prefixes with the RTF_REJECT flag set (for example,
18651 		 * when generating aggregate routes.)
18652 		 *
18653 		 * If the IRE type (as defined by ipif->ipif_net_type) is
18654 		 * IRE_LOOPBACK, then we map the request into a
18655 		 * IRE_IF_NORESOLVER.
18656 		 */
18657 		if (ipif->ipif_net_type == IRE_LOOPBACK)
18658 			ire->ire_type = IRE_IF_NORESOLVER;
18659 		/*
18660 		 * ire held by ire_add, will be refreled' towards the
18661 		 * the end of ipif_up_done
18662 		 */
18663 		(void) ire_add(&ire, NULL, NULL, NULL);
18664 		*irep = ire;
18665 		irep++;
18666 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
18667 	}
18668 	mutex_exit(&ipif->ipif_saved_ire_lock);
18669 	return (ipif_saved_irep);
18670 }
18671 
18672 /*
18673  * Used to set the netmask and broadcast address to default values when the
18674  * interface is brought up.  (Always called as writer.)
18675  */
18676 static void
18677 ipif_set_default(ipif_t *ipif)
18678 {
18679 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
18680 
18681 	if (!ipif->ipif_isv6) {
18682 		/*
18683 		 * Interface holds an IPv4 address. Default
18684 		 * mask is the natural netmask.
18685 		 */
18686 		if (!ipif->ipif_net_mask) {
18687 			ipaddr_t	v4mask;
18688 
18689 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
18690 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
18691 		}
18692 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18693 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
18694 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
18695 		} else {
18696 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
18697 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
18698 		}
18699 		/*
18700 		 * NOTE: SunOS 4.X does this even if the broadcast address
18701 		 * has been already set thus we do the same here.
18702 		 */
18703 		if (ipif->ipif_flags & IPIF_BROADCAST) {
18704 			ipaddr_t	v4addr;
18705 
18706 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
18707 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
18708 		}
18709 	} else {
18710 		/*
18711 		 * Interface holds an IPv6-only address.  Default
18712 		 * mask is all-ones.
18713 		 */
18714 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
18715 			ipif->ipif_v6net_mask = ipv6_all_ones;
18716 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18717 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
18718 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
18719 		} else {
18720 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
18721 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
18722 		}
18723 	}
18724 }
18725 
18726 /*
18727  * Return 0 if this address can be used as local address without causing
18728  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
18729  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
18730  * Special checks are needed to allow the same IPv6 link-local address
18731  * on different ills.
18732  * TODO: allowing the same site-local address on different ill's.
18733  */
18734 int
18735 ip_addr_availability_check(ipif_t *new_ipif)
18736 {
18737 	in6_addr_t our_v6addr;
18738 	ill_t *ill;
18739 	ipif_t *ipif;
18740 	ill_walk_context_t ctx;
18741 
18742 	ASSERT(IAM_WRITER_IPIF(new_ipif));
18743 	ASSERT(MUTEX_HELD(&ip_addr_avail_lock));
18744 	ASSERT(RW_READ_HELD(&ill_g_lock));
18745 
18746 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
18747 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
18748 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
18749 		return (0);
18750 
18751 	our_v6addr = new_ipif->ipif_v6lcl_addr;
18752 
18753 	if (new_ipif->ipif_isv6)
18754 		ill = ILL_START_WALK_V6(&ctx);
18755 	else
18756 		ill = ILL_START_WALK_V4(&ctx);
18757 
18758 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
18759 		for (ipif = ill->ill_ipif; ipif != NULL;
18760 		    ipif = ipif->ipif_next) {
18761 			if ((ipif == new_ipif) ||
18762 			    !(ipif->ipif_flags & IPIF_UP) ||
18763 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
18764 				continue;
18765 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
18766 			    &our_v6addr)) {
18767 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
18768 				    new_ipif->ipif_flags |= IPIF_UNNUMBERED;
18769 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
18770 				    ipif->ipif_flags |= IPIF_UNNUMBERED;
18771 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
18772 				    new_ipif->ipif_ill != ill)
18773 					continue;
18774 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
18775 				    new_ipif->ipif_ill != ill)
18776 					continue;
18777 				else if (new_ipif->ipif_zoneid !=
18778 				    ipif->ipif_zoneid &&
18779 				    ipif->ipif_zoneid != ALL_ZONES &&
18780 				    (ill->ill_phyint->phyint_flags &
18781 				    PHYI_LOOPBACK))
18782 					continue;
18783 				else if (new_ipif->ipif_ill == ill)
18784 					return (EADDRINUSE);
18785 				else
18786 					return (EADDRNOTAVAIL);
18787 			}
18788 		}
18789 	}
18790 
18791 	return (0);
18792 }
18793 
18794 /*
18795  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
18796  * IREs for the ipif.
18797  * When the routine returns EINPROGRESS then mp has been consumed and
18798  * the ioctl will be acked from ip_rput_dlpi.
18799  */
18800 static int
18801 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
18802 {
18803 	ill_t	*ill = ipif->ipif_ill;
18804 	boolean_t isv6 = ipif->ipif_isv6;
18805 	int	err = 0;
18806 	boolean_t success;
18807 
18808 	ASSERT(IAM_WRITER_IPIF(ipif));
18809 
18810 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18811 
18812 	/* Shouldn't get here if it is already up. */
18813 	if (ipif->ipif_flags & IPIF_UP)
18814 		return (EALREADY);
18815 
18816 	/* Skip arp/ndp for any loopback interface. */
18817 	if (ill->ill_wq != NULL) {
18818 		conn_t *connp = Q_TO_CONN(q);
18819 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
18820 
18821 		if (!ill->ill_dl_up) {
18822 			/*
18823 			 * ill_dl_up is not yet set. i.e. we are yet to
18824 			 * DL_BIND with the driver and this is the first
18825 			 * logical interface on the ill to become "up".
18826 			 * Tell the driver to get going (via DL_BIND_REQ).
18827 			 * Note that changing "significant" IFF_ flags
18828 			 * address/netmask etc cause a down/up dance, but
18829 			 * does not cause an unbind (DL_UNBIND) with the driver
18830 			 */
18831 			return (ill_dl_up(ill, ipif, mp, q));
18832 		}
18833 
18834 		/*
18835 		 * ipif_resolver_up may end up sending an
18836 		 * AR_INTERFACE_UP message to ARP, which would, in
18837 		 * turn send a DLPI message to the driver. ioctls are
18838 		 * serialized and so we cannot send more than one
18839 		 * interface up message at a time. If ipif_resolver_up
18840 		 * does send an interface up message to ARP, we get
18841 		 * EINPROGRESS and we will complete in ip_arp_done.
18842 		 */
18843 
18844 		ASSERT(connp != NULL);
18845 		ASSERT(ipsq->ipsq_pending_mp == NULL);
18846 		mutex_enter(&connp->conn_lock);
18847 		mutex_enter(&ill->ill_lock);
18848 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
18849 		mutex_exit(&ill->ill_lock);
18850 		mutex_exit(&connp->conn_lock);
18851 		if (!success)
18852 			return (EINTR);
18853 
18854 		/*
18855 		 * Crank up IPv6 neighbor discovery
18856 		 * Unlike ARP, this should complete when
18857 		 * ipif_ndp_up returns. However, for
18858 		 * ILLF_XRESOLV interfaces we also send a
18859 		 * AR_INTERFACE_UP to the external resolver.
18860 		 * That ioctl will complete in ip_rput.
18861 		 */
18862 		if (isv6) {
18863 			err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr,
18864 			    B_FALSE);
18865 			if (err != 0) {
18866 				mp = ipsq_pending_mp_get(ipsq, &connp);
18867 				return (err);
18868 			}
18869 		}
18870 		/* Now, ARP */
18871 		if ((err = ipif_resolver_up(ipif, B_FALSE)) ==
18872 		    EINPROGRESS) {
18873 			/* We will complete it in ip_arp_done */
18874 			return (err);
18875 		}
18876 		mp = ipsq_pending_mp_get(ipsq, &connp);
18877 		ASSERT(mp != NULL);
18878 		if (err != 0)
18879 			return (err);
18880 	}
18881 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
18882 }
18883 
18884 /*
18885  * Perform a bind for the physical device.
18886  * When the routine returns EINPROGRESS then mp has been consumed and
18887  * the ioctl will be acked from ip_rput_dlpi.
18888  * Allocate an unbind message and save it until ipif_down.
18889  */
18890 static int
18891 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
18892 {
18893 	mblk_t	*areq_mp = NULL;
18894 	mblk_t	*bind_mp = NULL;
18895 	mblk_t	*unbind_mp = NULL;
18896 	conn_t	*connp;
18897 	boolean_t success;
18898 
18899 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
18900 	ASSERT(IAM_WRITER_ILL(ill));
18901 
18902 	ASSERT(mp != NULL);
18903 
18904 	/* Create a resolver cookie for ARP */
18905 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
18906 		areq_t		*areq;
18907 		uint16_t	sap_addr;
18908 
18909 		areq_mp = ill_arp_alloc(ill,
18910 			(uchar_t *)&ip_areq_template, 0);
18911 		if (areq_mp == NULL) {
18912 			return (ENOMEM);
18913 		}
18914 		freemsg(ill->ill_resolver_mp);
18915 		ill->ill_resolver_mp = areq_mp;
18916 		areq = (areq_t *)areq_mp->b_rptr;
18917 		sap_addr = ill->ill_sap;
18918 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
18919 		/*
18920 		 * Wait till we call ill_pending_mp_add to determine
18921 		 * the success before we free the ill_resolver_mp and
18922 		 * attach areq_mp in it's place.
18923 		 */
18924 	}
18925 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
18926 	    DL_BIND_REQ);
18927 	if (bind_mp == NULL)
18928 		goto bad;
18929 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
18930 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
18931 
18932 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
18933 	if (unbind_mp == NULL)
18934 		goto bad;
18935 
18936 	/*
18937 	 * Record state needed to complete this operation when the
18938 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
18939 	 */
18940 	if (WR(q)->q_next == NULL) {
18941 		connp = Q_TO_CONN(q);
18942 		mutex_enter(&connp->conn_lock);
18943 	} else {
18944 		connp = NULL;
18945 	}
18946 	mutex_enter(&ipif->ipif_ill->ill_lock);
18947 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
18948 	mutex_exit(&ipif->ipif_ill->ill_lock);
18949 	if (connp != NULL)
18950 		mutex_exit(&connp->conn_lock);
18951 	if (!success)
18952 		goto bad;
18953 
18954 	/*
18955 	 * Save the unbind message for ill_dl_down(); it will be consumed when
18956 	 * the interface goes down.
18957 	 */
18958 	ASSERT(ill->ill_unbind_mp == NULL);
18959 	ill->ill_unbind_mp = unbind_mp;
18960 
18961 	ill_dlpi_send(ill, bind_mp);
18962 	/* Send down link-layer capabilities probe if not already done. */
18963 	ill_capability_probe(ill);
18964 
18965 	/*
18966 	 * Sysid used to rely on the fact that netboots set domainname
18967 	 * and the like. Now that miniroot boots aren't strictly netboots
18968 	 * and miniroot network configuration is driven from userland
18969 	 * these things still need to be set. This situation can be detected
18970 	 * by comparing the interface being configured here to the one
18971 	 * dhcack was set to reference by the boot loader. Once sysid is
18972 	 * converted to use dhcp_ipc_getinfo() this call can go away.
18973 	 */
18974 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
18975 	    (strcmp(ill->ill_name, dhcack) == 0) &&
18976 	    (strlen(srpc_domain) == 0)) {
18977 		if (dhcpinit() != 0)
18978 			cmn_err(CE_WARN, "no cached dhcp response");
18979 	}
18980 
18981 	/*
18982 	 * This operation will complete in ip_rput_dlpi with either
18983 	 * a DL_BIND_ACK or DL_ERROR_ACK.
18984 	 */
18985 	return (EINPROGRESS);
18986 bad:
18987 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
18988 	/*
18989 	 * We don't have to check for possible removal from illgrp
18990 	 * as we have not yet inserted in illgrp. For groups
18991 	 * without names, this ipif is still not UP and hence
18992 	 * this could not have possibly had any influence in forming
18993 	 * groups.
18994 	 */
18995 
18996 	if (bind_mp != NULL)
18997 		freemsg(bind_mp);
18998 	if (unbind_mp != NULL)
18999 		freemsg(unbind_mp);
19000 	return (ENOMEM);
19001 }
19002 
19003 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
19004 
19005 /*
19006  * DLPI and ARP is up.
19007  * Create all the IREs associated with an interface bring up multicast.
19008  * Set the interface flag and finish other initialization
19009  * that potentially had to be differed to after DL_BIND_ACK.
19010  */
19011 int
19012 ipif_up_done(ipif_t *ipif)
19013 {
19014 	ire_t	*ire_array[20];
19015 	ire_t	**irep = ire_array;
19016 	ire_t	**irep1;
19017 	ipaddr_t net_mask = 0;
19018 	ipaddr_t subnet_mask, route_mask;
19019 	ill_t	*ill = ipif->ipif_ill;
19020 	queue_t	*stq;
19021 	ipif_t	 *src_ipif;
19022 	ipif_t   *tmp_ipif;
19023 	boolean_t	flush_ire_cache = B_TRUE;
19024 	int	err = 0;
19025 	phyint_t *phyi;
19026 	ire_t	**ipif_saved_irep = NULL;
19027 	int ipif_saved_ire_cnt;
19028 	int	cnt;
19029 	boolean_t	src_ipif_held = B_FALSE;
19030 	boolean_t	ire_added = B_FALSE;
19031 	boolean_t	loopback = B_FALSE;
19032 
19033 	ip1dbg(("ipif_up_done(%s:%u)\n",
19034 		ipif->ipif_ill->ill_name, ipif->ipif_id));
19035 	/* Check if this is a loopback interface */
19036 	if (ipif->ipif_ill->ill_wq == NULL)
19037 		loopback = B_TRUE;
19038 
19039 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19040 	/*
19041 	 * If all other interfaces for this ill are down or DEPRECATED,
19042 	 * or otherwise unsuitable for source address selection, remove
19043 	 * any IRE_CACHE entries for this ill to make sure source
19044 	 * address selection gets to take this new ipif into account.
19045 	 * No need to hold ill_lock while traversing the ipif list since
19046 	 * we are writer
19047 	 */
19048 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
19049 		tmp_ipif = tmp_ipif->ipif_next) {
19050 		if (((tmp_ipif->ipif_flags &
19051 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
19052 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
19053 		    (tmp_ipif == ipif))
19054 			continue;
19055 		/* first useable pre-existing interface */
19056 		flush_ire_cache = B_FALSE;
19057 		break;
19058 	}
19059 	if (flush_ire_cache)
19060 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
19061 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
19062 
19063 	/*
19064 	 * Figure out which way the send-to queue should go.  Only
19065 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
19066 	 * should show up here.
19067 	 */
19068 	switch (ill->ill_net_type) {
19069 	case IRE_IF_RESOLVER:
19070 		stq = ill->ill_rq;
19071 		break;
19072 	case IRE_IF_NORESOLVER:
19073 	case IRE_LOOPBACK:
19074 		stq = ill->ill_wq;
19075 		break;
19076 	default:
19077 		return (EINVAL);
19078 	}
19079 
19080 	if (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) {
19081 		/*
19082 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
19083 		 * ipif_lookup_on_name(), but in the case of zones we can have
19084 		 * several loopback addresses on lo0. So all the interfaces with
19085 		 * loopback addresses need to be marked IRE_LOOPBACK.
19086 		 */
19087 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
19088 		    htonl(INADDR_LOOPBACK))
19089 			ipif->ipif_ire_type = IRE_LOOPBACK;
19090 		else
19091 			ipif->ipif_ire_type = IRE_LOCAL;
19092 	}
19093 
19094 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
19095 		/*
19096 		 * Can't use our source address. Select a different
19097 		 * source address for the IRE_INTERFACE and IRE_LOCAL
19098 		 */
19099 		src_ipif = ipif_select_source(ipif->ipif_ill,
19100 		    ipif->ipif_subnet, ipif->ipif_zoneid);
19101 		if (src_ipif == NULL)
19102 			src_ipif = ipif;	/* Last resort */
19103 		else
19104 			src_ipif_held = B_TRUE;
19105 	} else {
19106 		src_ipif = ipif;
19107 	}
19108 
19109 	/* Create all the IREs associated with this interface */
19110 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
19111 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
19112 
19113 		/*
19114 		 * If we're on a labeled system then make sure that zone-
19115 		 * private addresses have proper remote host database entries.
19116 		 */
19117 		if (is_system_labeled() &&
19118 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
19119 		    !tsol_check_interface_address(ipif))
19120 			return (EINVAL);
19121 
19122 		/* Register the source address for __sin6_src_id */
19123 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
19124 		    ipif->ipif_zoneid);
19125 		if (err != 0) {
19126 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
19127 			return (err);
19128 		}
19129 
19130 		/* If the interface address is set, create the local IRE. */
19131 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
19132 			(void *)ipif,
19133 			ipif->ipif_ire_type,
19134 			ntohl(ipif->ipif_lcl_addr)));
19135 		*irep++ = ire_create(
19136 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
19137 		    (uchar_t *)&ip_g_all_ones,		/* mask */
19138 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
19139 		    NULL,				/* no gateway */
19140 		    NULL,
19141 		    &ip_loopback_mtuplus,		/* max frag size */
19142 		    NULL,
19143 		    ipif->ipif_rq,			/* recv-from queue */
19144 		    NULL,				/* no send-to queue */
19145 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
19146 		    NULL,
19147 		    ipif,
19148 		    NULL,
19149 		    0,
19150 		    0,
19151 		    0,
19152 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
19153 		    RTF_PRIVATE : 0,
19154 		    &ire_uinfo_null,
19155 		    NULL,
19156 		    NULL);
19157 	} else {
19158 		ip1dbg((
19159 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
19160 		    ipif->ipif_ire_type,
19161 		    ntohl(ipif->ipif_lcl_addr),
19162 		    (uint_t)ipif->ipif_flags));
19163 	}
19164 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
19165 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
19166 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
19167 	} else {
19168 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
19169 	}
19170 
19171 	subnet_mask = ipif->ipif_net_mask;
19172 
19173 	/*
19174 	 * If mask was not specified, use natural netmask of
19175 	 * interface address. Also, store this mask back into the
19176 	 * ipif struct.
19177 	 */
19178 	if (subnet_mask == 0) {
19179 		subnet_mask = net_mask;
19180 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
19181 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
19182 		    ipif->ipif_v6subnet);
19183 	}
19184 
19185 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
19186 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
19187 	    ipif->ipif_subnet != INADDR_ANY) {
19188 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19189 
19190 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19191 			route_mask = IP_HOST_MASK;
19192 		} else {
19193 			route_mask = subnet_mask;
19194 		}
19195 
19196 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
19197 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
19198 			(void *)ipif, (void *)ill,
19199 			ill->ill_net_type,
19200 			ntohl(ipif->ipif_subnet)));
19201 		*irep++ = ire_create(
19202 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
19203 		    (uchar_t *)&route_mask,		/* mask */
19204 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
19205 		    NULL,				/* no gateway */
19206 		    NULL,
19207 		    &ipif->ipif_mtu,			/* max frag */
19208 		    NULL,
19209 		    NULL,				/* no recv queue */
19210 		    stq,				/* send-to queue */
19211 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
19212 		    ill->ill_resolver_mp,		/* xmit header */
19213 		    ipif,
19214 		    NULL,
19215 		    0,
19216 		    0,
19217 		    0,
19218 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
19219 		    &ire_uinfo_null,
19220 		    NULL,
19221 		    NULL);
19222 	}
19223 
19224 	/*
19225 	 * If the interface address is set, create the broadcast IREs.
19226 	 *
19227 	 * ire_create_bcast checks if the proposed new IRE matches
19228 	 * any existing IRE's with the same physical interface (ILL).
19229 	 * This should get rid of duplicates.
19230 	 * ire_create_bcast also check IPIF_NOXMIT and does not create
19231 	 * any broadcast ires.
19232 	 */
19233 	if ((ipif->ipif_subnet != INADDR_ANY) &&
19234 	    (ipif->ipif_flags & IPIF_BROADCAST)) {
19235 		ipaddr_t addr;
19236 
19237 		ip1dbg(("ipif_up_done: creating broadcast IRE\n"));
19238 		irep = ire_check_and_create_bcast(ipif, 0, irep,
19239 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19240 		irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep,
19241 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19242 
19243 		/*
19244 		 * For backward compatibility, we need to create net
19245 		 * broadcast ire's based on the old "IP address class
19246 		 * system."  The reason is that some old machines only
19247 		 * respond to these class derived net broadcast.
19248 		 *
19249 		 * But we should not create these net broadcast ire's if
19250 		 * the subnet_mask is shorter than the IP address class based
19251 		 * derived netmask.  Otherwise, we may create a net
19252 		 * broadcast address which is the same as an IP address
19253 		 * on the subnet.  Then TCP will refuse to talk to that
19254 		 * address.
19255 		 *
19256 		 * Nor do we need IRE_BROADCAST ire's for the interface
19257 		 * with the netmask as 0xFFFFFFFF, as IRE_LOCAL for that
19258 		 * interface is already created.  Creating these broadcast
19259 		 * ire's will only create confusion as the "addr" is going
19260 		 * to be same as that of the IP address of the interface.
19261 		 */
19262 		if (net_mask < subnet_mask) {
19263 			addr = net_mask & ipif->ipif_subnet;
19264 			irep = ire_check_and_create_bcast(ipif, addr, irep,
19265 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19266 			irep = ire_check_and_create_bcast(ipif,
19267 			    ~net_mask | addr, irep,
19268 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19269 		}
19270 
19271 		if (subnet_mask != 0xFFFFFFFF) {
19272 			addr = ipif->ipif_subnet;
19273 			irep = ire_check_and_create_bcast(ipif, addr, irep,
19274 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19275 			irep = ire_check_and_create_bcast(ipif,
19276 			    ~subnet_mask|addr, irep,
19277 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19278 		}
19279 	}
19280 
19281 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19282 
19283 	/* If an earlier ire_create failed, get out now */
19284 	for (irep1 = irep; irep1 > ire_array; ) {
19285 		irep1--;
19286 		if (*irep1 == NULL) {
19287 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
19288 			err = ENOMEM;
19289 			goto bad;
19290 		}
19291 	}
19292 
19293 	/*
19294 	 * Need to atomically check for ip_addr_availablity_check
19295 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
19296 	 * from group also.The ill_g_lock is grabbed as reader
19297 	 * just to make sure no new ills or new ipifs are being added
19298 	 * to the system while we are checking the uniqueness of addresses.
19299 	 */
19300 	rw_enter(&ill_g_lock, RW_READER);
19301 	mutex_enter(&ip_addr_avail_lock);
19302 	/* Mark it up, and increment counters. */
19303 	ill->ill_ipif_up_count++;
19304 	ipif->ipif_flags |= IPIF_UP;
19305 	err = ip_addr_availability_check(ipif);
19306 	mutex_exit(&ip_addr_avail_lock);
19307 	rw_exit(&ill_g_lock);
19308 
19309 	if (err != 0) {
19310 		/*
19311 		 * Our address may already be up on the same ill. In this case,
19312 		 * the ARP entry for our ipif replaced the one for the other
19313 		 * ipif. So we don't want to delete it (otherwise the other ipif
19314 		 * would be unable to send packets).
19315 		 * ip_addr_availability_check() identifies this case for us and
19316 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
19317 		 * which is the expected error code.
19318 		 */
19319 		if (err == EADDRINUSE) {
19320 			freemsg(ipif->ipif_arp_del_mp);
19321 			ipif->ipif_arp_del_mp = NULL;
19322 			err = EADDRNOTAVAIL;
19323 		}
19324 		ill->ill_ipif_up_count--;
19325 		ipif->ipif_flags &= ~IPIF_UP;
19326 		goto bad;
19327 	}
19328 
19329 	/*
19330 	 * Add in all newly created IREs.  ire_create_bcast() has
19331 	 * already checked for duplicates of the IRE_BROADCAST type.
19332 	 * We want to add before we call ifgrp_insert which wants
19333 	 * to know whether IRE_IF_RESOLVER exists or not.
19334 	 *
19335 	 * NOTE : We refrele the ire though we may branch to "bad"
19336 	 *	  later on where we do ire_delete. This is okay
19337 	 *	  because nobody can delete it as we are running
19338 	 *	  exclusively.
19339 	 */
19340 	for (irep1 = irep; irep1 > ire_array; ) {
19341 		irep1--;
19342 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
19343 		/*
19344 		 * refheld by ire_add. refele towards the end of the func
19345 		 */
19346 		(void) ire_add(irep1, NULL, NULL, NULL);
19347 	}
19348 	ire_added = B_TRUE;
19349 	/*
19350 	 * Form groups if possible.
19351 	 *
19352 	 * If we are supposed to be in a ill_group with a name, insert it
19353 	 * now as we know that at least one ipif is UP. Otherwise form
19354 	 * nameless groups.
19355 	 *
19356 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
19357 	 * this ipif into the appropriate interface group, or create a
19358 	 * new one. If this is already in a nameless group, we try to form
19359 	 * a bigger group looking at other ills potentially sharing this
19360 	 * ipif's prefix.
19361 	 */
19362 	phyi = ill->ill_phyint;
19363 	if (phyi->phyint_groupname_len != 0) {
19364 		ASSERT(phyi->phyint_groupname != NULL);
19365 		if (ill->ill_ipif_up_count == 1) {
19366 			ASSERT(ill->ill_group == NULL);
19367 			err = illgrp_insert(&illgrp_head_v4, ill,
19368 			    phyi->phyint_groupname, NULL, B_TRUE);
19369 			if (err != 0) {
19370 				ip1dbg(("ipif_up_done: illgrp allocation "
19371 				    "failed, error %d\n", err));
19372 				goto bad;
19373 			}
19374 		}
19375 		ASSERT(ill->ill_group != NULL);
19376 	}
19377 
19378 	/*
19379 	 * When this is part of group, we need to make sure that
19380 	 * any broadcast ires created because of this ipif coming
19381 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
19382 	 * so that we don't receive duplicate broadcast packets.
19383 	 */
19384 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
19385 		ipif_renominate_bcast(ipif);
19386 
19387 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
19388 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
19389 	ipif_saved_irep = ipif_recover_ire(ipif);
19390 
19391 	if (!loopback) {
19392 		/*
19393 		 * If the broadcast address has been set, make sure it makes
19394 		 * sense based on the interface address.
19395 		 * Only match on ill since we are sharing broadcast addresses.
19396 		 */
19397 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
19398 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
19399 			ire_t	*ire;
19400 
19401 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
19402 			    IRE_BROADCAST, ipif, ALL_ZONES,
19403 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19404 
19405 			if (ire == NULL) {
19406 				/*
19407 				 * If there isn't a matching broadcast IRE,
19408 				 * revert to the default for this netmask.
19409 				 */
19410 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
19411 				mutex_enter(&ipif->ipif_ill->ill_lock);
19412 				ipif_set_default(ipif);
19413 				mutex_exit(&ipif->ipif_ill->ill_lock);
19414 			} else {
19415 				ire_refrele(ire);
19416 			}
19417 		}
19418 
19419 	}
19420 
19421 
19422 	/* This is the first interface on this ill */
19423 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
19424 		/*
19425 		 * Need to recover all multicast memberships in the driver.
19426 		 * This had to be deferred until we had attached.
19427 		 */
19428 		ill_recover_multicast(ill);
19429 	}
19430 	/* Join the allhosts multicast address */
19431 	ipif_multicast_up(ipif);
19432 
19433 	if (!loopback) {
19434 		/*
19435 		 * See whether anybody else would benefit from the
19436 		 * new ipif that we added. We call this always rather
19437 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
19438 		 * ipif is for the benefit of illgrp_insert (done above)
19439 		 * which does not do source address selection as it does
19440 		 * not want to re-create interface routes that we are
19441 		 * having reference to it here.
19442 		 */
19443 		ill_update_source_selection(ill);
19444 	}
19445 
19446 	for (irep1 = irep; irep1 > ire_array; ) {
19447 		irep1--;
19448 		if (*irep1 != NULL) {
19449 			/* was held in ire_add */
19450 			ire_refrele(*irep1);
19451 		}
19452 	}
19453 
19454 	cnt = ipif_saved_ire_cnt;
19455 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
19456 		if (*irep1 != NULL) {
19457 			/* was held in ire_add */
19458 			ire_refrele(*irep1);
19459 		}
19460 	}
19461 
19462 	/*
19463 	 * This had to be deferred until we had bound.
19464 	 * tell routing sockets that this interface is up
19465 	 */
19466 	ip_rts_ifmsg(ipif);
19467 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
19468 
19469 	if (!loopback) {
19470 		/* Broadcast an address mask reply. */
19471 		ipif_mask_reply(ipif);
19472 	}
19473 	if (ipif_saved_irep != NULL) {
19474 		kmem_free(ipif_saved_irep,
19475 		    ipif_saved_ire_cnt * sizeof (ire_t *));
19476 	}
19477 	if (src_ipif_held)
19478 		ipif_refrele(src_ipif);
19479 	/* Let SCTP update the status for this ipif */
19480 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
19481 	return (0);
19482 
19483 bad:
19484 	ip1dbg(("ipif_up_done: FAILED \n"));
19485 	/*
19486 	 * We don't have to bother removing from ill groups because
19487 	 *
19488 	 * 1) For groups with names, we insert only when the first ipif
19489 	 *    comes up. In that case if it fails, it will not be in any
19490 	 *    group. So, we need not try to remove for that case.
19491 	 *
19492 	 * 2) For groups without names, either we tried to insert ipif_ill
19493 	 *    in a group as singleton or found some other group to become
19494 	 *    a bigger group. For the former, if it fails we don't have
19495 	 *    anything to do as ipif_ill is not in the group and for the
19496 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
19497 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
19498 	 */
19499 	while (irep > ire_array) {
19500 		irep--;
19501 		if (*irep != NULL) {
19502 			ire_delete(*irep);
19503 			if (ire_added)
19504 				ire_refrele(*irep);
19505 		}
19506 	}
19507 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid);
19508 
19509 	if (ipif_saved_irep != NULL) {
19510 		kmem_free(ipif_saved_irep,
19511 		    ipif_saved_ire_cnt * sizeof (ire_t *));
19512 	}
19513 	if (src_ipif_held)
19514 		ipif_refrele(src_ipif);
19515 
19516 	ipif_arp_down(ipif);
19517 	return (err);
19518 }
19519 
19520 /*
19521  * Turn off the ARP with the ILLF_NOARP flag.
19522  */
19523 static int
19524 ill_arp_off(ill_t *ill)
19525 {
19526 	mblk_t	*arp_off_mp = NULL;
19527 	mblk_t	*arp_on_mp = NULL;
19528 
19529 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
19530 
19531 	ASSERT(IAM_WRITER_ILL(ill));
19532 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
19533 
19534 	/*
19535 	 * If the on message is still around we've already done
19536 	 * an arp_off without doing an arp_on thus there is no
19537 	 * work needed.
19538 	 */
19539 	if (ill->ill_arp_on_mp != NULL)
19540 		return (0);
19541 
19542 	/*
19543 	 * Allocate an ARP on message (to be saved) and an ARP off message
19544 	 */
19545 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
19546 	if (!arp_off_mp)
19547 		return (ENOMEM);
19548 
19549 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
19550 	if (!arp_on_mp)
19551 		goto failed;
19552 
19553 	ASSERT(ill->ill_arp_on_mp == NULL);
19554 	ill->ill_arp_on_mp = arp_on_mp;
19555 
19556 	/* Send an AR_INTERFACE_OFF request */
19557 	putnext(ill->ill_rq, arp_off_mp);
19558 	return (0);
19559 failed:
19560 
19561 	if (arp_off_mp)
19562 		freemsg(arp_off_mp);
19563 	return (ENOMEM);
19564 }
19565 
19566 /*
19567  * Turn on ARP by turning off the ILLF_NOARP flag.
19568  */
19569 static int
19570 ill_arp_on(ill_t *ill)
19571 {
19572 	mblk_t	*mp;
19573 
19574 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
19575 
19576 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
19577 
19578 	ASSERT(IAM_WRITER_ILL(ill));
19579 	/*
19580 	 * Send an AR_INTERFACE_ON request if we have already done
19581 	 * an arp_off (which allocated the message).
19582 	 */
19583 	if (ill->ill_arp_on_mp != NULL) {
19584 		mp = ill->ill_arp_on_mp;
19585 		ill->ill_arp_on_mp = NULL;
19586 		putnext(ill->ill_rq, mp);
19587 	}
19588 	return (0);
19589 }
19590 
19591 /*
19592  * Called after either deleting ill from the group or when setting
19593  * FAILED or STANDBY on the interface.
19594  */
19595 static void
19596 illgrp_reset_schednext(ill_t *ill)
19597 {
19598 	ill_group_t *illgrp;
19599 	ill_t *save_ill;
19600 
19601 	ASSERT(IAM_WRITER_ILL(ill));
19602 	/*
19603 	 * When called from illgrp_delete, ill_group will be non-NULL.
19604 	 * But when called from ip_sioctl_flags, it could be NULL if
19605 	 * somebody is setting FAILED/INACTIVE on some interface which
19606 	 * is not part of a group.
19607 	 */
19608 	illgrp = ill->ill_group;
19609 	if (illgrp == NULL)
19610 		return;
19611 	if (illgrp->illgrp_ill_schednext != ill)
19612 		return;
19613 
19614 	illgrp->illgrp_ill_schednext = NULL;
19615 	save_ill = ill;
19616 	/*
19617 	 * Choose a good ill to be the next one for
19618 	 * outbound traffic. As the flags FAILED/STANDBY is
19619 	 * not yet marked when called from ip_sioctl_flags,
19620 	 * we check for ill separately.
19621 	 */
19622 	for (ill = illgrp->illgrp_ill; ill != NULL;
19623 	    ill = ill->ill_group_next) {
19624 		if ((ill != save_ill) &&
19625 		    !(ill->ill_phyint->phyint_flags &
19626 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
19627 			illgrp->illgrp_ill_schednext = ill;
19628 			return;
19629 		}
19630 	}
19631 }
19632 
19633 /*
19634  * Given an ill, find the next ill in the group to be scheduled.
19635  * (This should be called by ip_newroute() before ire_create().)
19636  * The passed in ill may be pulled out of the group, after we have picked
19637  * up a different outgoing ill from the same group. However ire add will
19638  * atomically check this.
19639  */
19640 ill_t *
19641 illgrp_scheduler(ill_t *ill)
19642 {
19643 	ill_t *retill;
19644 	ill_group_t *illgrp;
19645 	int illcnt;
19646 	int i;
19647 	uint64_t flags;
19648 
19649 	/*
19650 	 * We don't use a lock to check for the ill_group. If this ill
19651 	 * is currently being inserted we may end up just returning this
19652 	 * ill itself. That is ok.
19653 	 */
19654 	if (ill->ill_group == NULL) {
19655 		ill_refhold(ill);
19656 		return (ill);
19657 	}
19658 
19659 	/*
19660 	 * Grab the ill_g_lock as reader to make sure we are dealing with
19661 	 * a set of stable ills. No ill can be added or deleted or change
19662 	 * group while we hold the reader lock.
19663 	 */
19664 	rw_enter(&ill_g_lock, RW_READER);
19665 	if ((illgrp = ill->ill_group) == NULL) {
19666 		rw_exit(&ill_g_lock);
19667 		ill_refhold(ill);
19668 		return (ill);
19669 	}
19670 
19671 	illcnt = illgrp->illgrp_ill_count;
19672 	mutex_enter(&illgrp->illgrp_lock);
19673 	retill = illgrp->illgrp_ill_schednext;
19674 
19675 	if (retill == NULL)
19676 		retill = illgrp->illgrp_ill;
19677 
19678 	/*
19679 	 * We do a circular search beginning at illgrp_ill_schednext
19680 	 * or illgrp_ill. We don't check the flags against the ill lock
19681 	 * since it can change anytime. The ire creation will be atomic
19682 	 * and will fail if the ill is FAILED or OFFLINE.
19683 	 */
19684 	for (i = 0; i < illcnt; i++) {
19685 		flags = retill->ill_phyint->phyint_flags;
19686 
19687 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
19688 		    ILL_CAN_LOOKUP(retill)) {
19689 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
19690 			ill_refhold(retill);
19691 			break;
19692 		}
19693 		retill = retill->ill_group_next;
19694 		if (retill == NULL)
19695 			retill = illgrp->illgrp_ill;
19696 	}
19697 	mutex_exit(&illgrp->illgrp_lock);
19698 	rw_exit(&ill_g_lock);
19699 
19700 	return (i == illcnt ? NULL : retill);
19701 }
19702 
19703 /*
19704  * Checks for availbility of a usable source address (if there is one) when the
19705  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
19706  * this selection is done regardless of the destination.
19707  */
19708 boolean_t
19709 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
19710 {
19711 	uint_t	ifindex;
19712 	ipif_t	*ipif = NULL;
19713 	ill_t	*uill;
19714 	boolean_t isv6;
19715 
19716 	ASSERT(ill != NULL);
19717 
19718 	isv6 = ill->ill_isv6;
19719 	ifindex = ill->ill_usesrc_ifindex;
19720 	if (ifindex != 0) {
19721 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
19722 		    NULL);
19723 		if (uill == NULL)
19724 			return (NULL);
19725 		mutex_enter(&uill->ill_lock);
19726 		for (ipif = uill->ill_ipif; ipif != NULL;
19727 		    ipif = ipif->ipif_next) {
19728 			if (!IPIF_CAN_LOOKUP(ipif))
19729 				continue;
19730 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
19731 				continue;
19732 			if (!(ipif->ipif_flags & IPIF_UP))
19733 				continue;
19734 			if (ipif->ipif_zoneid != zoneid)
19735 				continue;
19736 			if ((isv6 &&
19737 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
19738 			    (ipif->ipif_lcl_addr == INADDR_ANY))
19739 				continue;
19740 			mutex_exit(&uill->ill_lock);
19741 			ill_refrele(uill);
19742 			return (B_TRUE);
19743 		}
19744 		mutex_exit(&uill->ill_lock);
19745 		ill_refrele(uill);
19746 	}
19747 	return (B_FALSE);
19748 }
19749 
19750 /*
19751  * Determine the best source address given a destination address and an ill.
19752  * Prefers non-deprecated over deprecated but will return a deprecated
19753  * address if there is no other choice. If there is a usable source address
19754  * on the interface pointed to by ill_usesrc_ifindex then that is given
19755  * first preference.
19756  *
19757  * Returns NULL if there is no suitable source address for the ill.
19758  * This only occurs when there is no valid source address for the ill.
19759  */
19760 ipif_t *
19761 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
19762 {
19763 	ipif_t *ipif;
19764 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
19765 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
19766 	int index = 0;
19767 	boolean_t wrapped = B_FALSE;
19768 	boolean_t same_subnet_only = B_FALSE;
19769 	boolean_t ipif_same_found, ipif_other_found;
19770 	boolean_t specific_found;
19771 	ill_t	*till, *usill = NULL;
19772 	tsol_tpc_t *src_rhtp, *dst_rhtp;
19773 
19774 	if (ill->ill_usesrc_ifindex != 0) {
19775 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex, B_FALSE,
19776 		    NULL, NULL, NULL, NULL);
19777 		if (usill != NULL)
19778 			ill = usill;	/* Select source from usesrc ILL */
19779 		else
19780 			return (NULL);
19781 	}
19782 
19783 	/*
19784 	 * If we're dealing with an unlabeled destination on a labeled system,
19785 	 * make sure that we ignore source addresses that are incompatible with
19786 	 * the destination's default label.  That destination's default label
19787 	 * must dominate the minimum label on the source address.
19788 	 */
19789 	dst_rhtp = NULL;
19790 	if (is_system_labeled()) {
19791 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
19792 		if (dst_rhtp == NULL)
19793 			return (NULL);
19794 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
19795 			TPC_RELE(dst_rhtp);
19796 			dst_rhtp = NULL;
19797 		}
19798 	}
19799 
19800 	/*
19801 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
19802 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
19803 	 * After selecting the right ipif, under ill_lock make sure ipif is
19804 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
19805 	 * we retry. Inside the loop we still need to check for CONDEMNED,
19806 	 * but not under a lock.
19807 	 */
19808 	rw_enter(&ill_g_lock, RW_READER);
19809 
19810 retry:
19811 	till = ill;
19812 	ipif_arr[0] = NULL;
19813 
19814 	if (till->ill_group != NULL)
19815 		till = till->ill_group->illgrp_ill;
19816 
19817 	/*
19818 	 * Choose one good source address from each ill across the group.
19819 	 * If possible choose a source address in the same subnet as
19820 	 * the destination address.
19821 	 *
19822 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
19823 	 * This is okay because of the following.
19824 	 *
19825 	 *    If PHYI_FAILED is set and we still have non-deprecated
19826 	 *    addresses, it means the addresses have not yet been
19827 	 *    failed over to a different interface. We potentially
19828 	 *    select them to create IRE_CACHES, which will be later
19829 	 *    flushed when the addresses move over.
19830 	 *
19831 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
19832 	 *    addresses, it means either the user has configured them
19833 	 *    or PHYI_INACTIVE has not been cleared after the addresses
19834 	 *    been moved over. For the former, in.mpathd does a failover
19835 	 *    when the interface becomes INACTIVE and hence we should
19836 	 *    not find them. Once INACTIVE is set, we don't allow them
19837 	 *    to create logical interfaces anymore. For the latter, a
19838 	 *    flush will happen when INACTIVE is cleared which will
19839 	 *    flush the IRE_CACHES.
19840 	 *
19841 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
19842 	 *    over soon. We potentially select them to create IRE_CACHEs,
19843 	 *    which will be later flushed when the addresses move over.
19844 	 *
19845 	 * NOTE : As ipif_select_source is called to borrow source address
19846 	 * for an ipif that is part of a group, source address selection
19847 	 * will be re-done whenever the group changes i.e either an
19848 	 * insertion/deletion in the group.
19849 	 *
19850 	 * Fill ipif_arr[] with source addresses, using these rules:
19851 	 *
19852 	 *	1. At most one source address from a given ill ends up
19853 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
19854 	 *	   associated with a given ill ends up in ipif_arr[].
19855 	 *
19856 	 *	2. If there is at least one non-deprecated ipif in the
19857 	 *	   IPMP group with a source address on the same subnet as
19858 	 *	   our destination, then fill ipif_arr[] only with
19859 	 *	   source addresses on the same subnet as our destination.
19860 	 *	   Note that because of (1), only the first
19861 	 *	   non-deprecated ipif found with a source address
19862 	 *	   matching the destination ends up in ipif_arr[].
19863 	 *
19864 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
19865 	 *	   addresses not in the same subnet as our destination.
19866 	 *	   Again, because of (1), only the first off-subnet source
19867 	 *	   address will be chosen.
19868 	 *
19869 	 *	4. If there are no non-deprecated ipifs, then just use
19870 	 *	   the source address associated with the last deprecated
19871 	 *	   one we find that happens to be on the same subnet,
19872 	 *	   otherwise the first one not in the same subnet.
19873 	 */
19874 	specific_found = B_FALSE;
19875 	for (; till != NULL; till = till->ill_group_next) {
19876 		ipif_same_found = B_FALSE;
19877 		ipif_other_found = B_FALSE;
19878 		for (ipif = till->ill_ipif; ipif != NULL;
19879 		    ipif = ipif->ipif_next) {
19880 			if (!IPIF_CAN_LOOKUP(ipif))
19881 				continue;
19882 			/* Always skip NOLOCAL and ANYCAST interfaces */
19883 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
19884 				continue;
19885 			if (!(ipif->ipif_flags & IPIF_UP))
19886 				continue;
19887 			if (ipif->ipif_zoneid != zoneid &&
19888 			    ipif->ipif_zoneid != ALL_ZONES)
19889 				continue;
19890 			/*
19891 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
19892 			 * but are not valid as source addresses.
19893 			 */
19894 			if (ipif->ipif_lcl_addr == INADDR_ANY)
19895 				continue;
19896 
19897 			/*
19898 			 * Check compatibility of local address for
19899 			 * destination's default label if we're on a labeled
19900 			 * system.  Incompatible addresses can't be used at
19901 			 * all.
19902 			 */
19903 			if (dst_rhtp != NULL) {
19904 				boolean_t incompat;
19905 
19906 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
19907 				    IPV4_VERSION, B_FALSE);
19908 				if (src_rhtp == NULL)
19909 					continue;
19910 				incompat =
19911 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
19912 				    src_rhtp->tpc_tp.tp_doi !=
19913 				    dst_rhtp->tpc_tp.tp_doi ||
19914 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
19915 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
19916 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
19917 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
19918 				TPC_RELE(src_rhtp);
19919 				if (incompat)
19920 					continue;
19921 			}
19922 
19923 			/*
19924 			 * We prefer not to use all all-zones addresses, if we
19925 			 * can avoid it, as they pose problems with unlabeled
19926 			 * destinations.
19927 			 */
19928 			if (ipif->ipif_zoneid != ALL_ZONES) {
19929 				if (!specific_found &&
19930 				    (!same_subnet_only ||
19931 				    (ipif->ipif_net_mask & dst) ==
19932 				    ipif->ipif_subnet)) {
19933 					index = 0;
19934 					specific_found = B_TRUE;
19935 					ipif_other_found = B_FALSE;
19936 				}
19937 			} else {
19938 				if (specific_found)
19939 					continue;
19940 			}
19941 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
19942 				if (ipif_dep == NULL ||
19943 				    (ipif->ipif_net_mask & dst) ==
19944 				    ipif->ipif_subnet)
19945 					ipif_dep = ipif;
19946 				continue;
19947 			}
19948 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
19949 				/* found a source address in the same subnet */
19950 				if (!same_subnet_only) {
19951 					same_subnet_only = B_TRUE;
19952 					index = 0;
19953 				}
19954 				ipif_same_found = B_TRUE;
19955 			} else {
19956 				if (same_subnet_only || ipif_other_found)
19957 					continue;
19958 				ipif_other_found = B_TRUE;
19959 			}
19960 			ipif_arr[index++] = ipif;
19961 			if (index == MAX_IPIF_SELECT_SOURCE) {
19962 				wrapped = B_TRUE;
19963 				index = 0;
19964 			}
19965 			if (ipif_same_found)
19966 				break;
19967 		}
19968 	}
19969 
19970 	if (ipif_arr[0] == NULL) {
19971 		ipif = ipif_dep;
19972 	} else {
19973 		if (wrapped)
19974 			index = MAX_IPIF_SELECT_SOURCE;
19975 		ipif = ipif_arr[ipif_rand() % index];
19976 		ASSERT(ipif != NULL);
19977 	}
19978 
19979 	if (ipif != NULL) {
19980 		mutex_enter(&ipif->ipif_ill->ill_lock);
19981 		if (!IPIF_CAN_LOOKUP(ipif)) {
19982 			mutex_exit(&ipif->ipif_ill->ill_lock);
19983 			goto retry;
19984 		}
19985 		ipif_refhold_locked(ipif);
19986 		mutex_exit(&ipif->ipif_ill->ill_lock);
19987 	}
19988 
19989 	rw_exit(&ill_g_lock);
19990 	if (usill != NULL)
19991 		ill_refrele(usill);
19992 	if (dst_rhtp != NULL)
19993 		TPC_RELE(dst_rhtp);
19994 
19995 #ifdef DEBUG
19996 	if (ipif == NULL) {
19997 		char buf1[INET6_ADDRSTRLEN];
19998 
19999 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
20000 		    ill->ill_name,
20001 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
20002 	} else {
20003 		char buf1[INET6_ADDRSTRLEN];
20004 		char buf2[INET6_ADDRSTRLEN];
20005 
20006 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
20007 		    ipif->ipif_ill->ill_name,
20008 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
20009 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
20010 		    buf2, sizeof (buf2))));
20011 	}
20012 #endif /* DEBUG */
20013 	return (ipif);
20014 }
20015 
20016 
20017 /*
20018  * If old_ipif is not NULL, see if ipif was derived from old
20019  * ipif and if so, recreate the interface route by re-doing
20020  * source address selection. This happens when ipif_down ->
20021  * ipif_update_other_ipifs calls us.
20022  *
20023  * If old_ipif is NULL, just redo the source address selection
20024  * if needed. This happens when illgrp_insert or ipif_up_done
20025  * calls us.
20026  */
20027 static void
20028 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
20029 {
20030 	ire_t *ire;
20031 	ire_t *ipif_ire;
20032 	queue_t *stq;
20033 	ipif_t *nipif;
20034 	ill_t *ill;
20035 	boolean_t need_rele = B_FALSE;
20036 
20037 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
20038 	ASSERT(IAM_WRITER_IPIF(ipif));
20039 
20040 	ill = ipif->ipif_ill;
20041 	if (!(ipif->ipif_flags &
20042 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
20043 		/*
20044 		 * Can't possibly have borrowed the source
20045 		 * from old_ipif.
20046 		 */
20047 		return;
20048 	}
20049 
20050 	/*
20051 	 * Is there any work to be done? No work if the address
20052 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
20053 	 * ipif_select_source() does not borrow addresses from
20054 	 * NOLOCAL and ANYCAST interfaces).
20055 	 */
20056 	if ((old_ipif != NULL) &&
20057 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
20058 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
20059 	    (old_ipif->ipif_flags &
20060 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
20061 		return;
20062 	}
20063 
20064 	/*
20065 	 * Perform the same checks as when creating the
20066 	 * IRE_INTERFACE in ipif_up_done.
20067 	 */
20068 	if (!(ipif->ipif_flags & IPIF_UP))
20069 		return;
20070 
20071 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
20072 	    (ipif->ipif_subnet == INADDR_ANY))
20073 		return;
20074 
20075 	ipif_ire = ipif_to_ire(ipif);
20076 	if (ipif_ire == NULL)
20077 		return;
20078 
20079 	/*
20080 	 * We know that ipif uses some other source for its
20081 	 * IRE_INTERFACE. Is it using the source of this
20082 	 * old_ipif?
20083 	 */
20084 	if (old_ipif != NULL &&
20085 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
20086 		ire_refrele(ipif_ire);
20087 		return;
20088 	}
20089 	if (ip_debug > 2) {
20090 		/* ip1dbg */
20091 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
20092 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
20093 	}
20094 
20095 	stq = ipif_ire->ire_stq;
20096 
20097 	/*
20098 	 * Can't use our source address. Select a different
20099 	 * source address for the IRE_INTERFACE.
20100 	 */
20101 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
20102 	if (nipif == NULL) {
20103 		/* Last resort - all ipif's have IPIF_NOLOCAL */
20104 		nipif = ipif;
20105 	} else {
20106 		need_rele = B_TRUE;
20107 	}
20108 
20109 	ire = ire_create(
20110 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
20111 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
20112 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
20113 	    NULL,				/* no gateway */
20114 	    NULL,
20115 	    &ipif->ipif_mtu,			/* max frag */
20116 	    NULL,				/* fast path header */
20117 	    NULL,				/* no recv from queue */
20118 	    stq,				/* send-to queue */
20119 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20120 	    ill->ill_resolver_mp,		/* xmit header */
20121 	    ipif,
20122 	    NULL,
20123 	    0,
20124 	    0,
20125 	    0,
20126 	    0,
20127 	    &ire_uinfo_null,
20128 	    NULL,
20129 	    NULL);
20130 
20131 	if (ire != NULL) {
20132 		ire_t *ret_ire;
20133 		int error;
20134 
20135 		/*
20136 		 * We don't need ipif_ire anymore. We need to delete
20137 		 * before we add so that ire_add does not detect
20138 		 * duplicates.
20139 		 */
20140 		ire_delete(ipif_ire);
20141 		ret_ire = ire;
20142 		error = ire_add(&ret_ire, NULL, NULL, NULL);
20143 		ASSERT(error == 0);
20144 		ASSERT(ire == ret_ire);
20145 		/* Held in ire_add */
20146 		ire_refrele(ret_ire);
20147 	}
20148 	/*
20149 	 * Either we are falling through from above or could not
20150 	 * allocate a replacement.
20151 	 */
20152 	ire_refrele(ipif_ire);
20153 	if (need_rele)
20154 		ipif_refrele(nipif);
20155 }
20156 
20157 /*
20158  * This old_ipif is going away.
20159  *
20160  * Determine if any other ipif's is using our address as
20161  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
20162  * IPIF_DEPRECATED).
20163  * Find the IRE_INTERFACE for such ipifs and recreate them
20164  * to use an different source address following the rules in
20165  * ipif_up_done.
20166  *
20167  * This function takes an illgrp as an argument so that illgrp_delete
20168  * can call this to update source address even after deleting the
20169  * old_ipif->ipif_ill from the ill group.
20170  */
20171 static void
20172 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
20173 {
20174 	ipif_t *ipif;
20175 	ill_t *ill;
20176 	char	buf[INET6_ADDRSTRLEN];
20177 
20178 	ASSERT(IAM_WRITER_IPIF(old_ipif));
20179 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
20180 
20181 	ill = old_ipif->ipif_ill;
20182 
20183 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
20184 	    ill->ill_name,
20185 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
20186 	    buf, sizeof (buf))));
20187 	/*
20188 	 * If this part of a group, look at all ills as ipif_select_source
20189 	 * borrows source address across all the ills in the group.
20190 	 */
20191 	if (illgrp != NULL)
20192 		ill = illgrp->illgrp_ill;
20193 
20194 	for (; ill != NULL; ill = ill->ill_group_next) {
20195 		for (ipif = ill->ill_ipif; ipif != NULL;
20196 		    ipif = ipif->ipif_next) {
20197 
20198 			if (ipif == old_ipif)
20199 				continue;
20200 
20201 			ipif_recreate_interface_routes(old_ipif, ipif);
20202 		}
20203 	}
20204 }
20205 
20206 /* ARGSUSED */
20207 int
20208 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
20209 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
20210 {
20211 	/*
20212 	 * ill_phyint_reinit merged the v4 and v6 into a single
20213 	 * ipsq. Could also have become part of a ipmp group in the
20214 	 * process, and we might not have been able to complete the
20215 	 * operation in ipif_set_values, if we could not become
20216 	 * exclusive.  If so restart it here.
20217 	 */
20218 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
20219 }
20220 
20221 
20222 /* ARGSUSED */
20223 int
20224 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
20225     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
20226 {
20227 	queue_t		*q1 = q;
20228 	char 		*cp;
20229 	char		interf_name[LIFNAMSIZ];
20230 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
20231 
20232 	if (!q->q_next) {
20233 		ip1dbg((
20234 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
20235 		return (EINVAL);
20236 	}
20237 
20238 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
20239 		return (EALREADY);
20240 
20241 	do {
20242 		q1 = q1->q_next;
20243 	} while (q1->q_next);
20244 	cp = q1->q_qinfo->qi_minfo->mi_idname;
20245 	(void) sprintf(interf_name, "%s%d", cp, ppa);
20246 
20247 	/*
20248 	 * Here we are not going to delay the ioack until after
20249 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
20250 	 * original ioctl message before sending the requests.
20251 	 */
20252 	return (ipif_set_values(q, mp, interf_name, &ppa));
20253 }
20254 
20255 /* ARGSUSED */
20256 int
20257 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
20258     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
20259 {
20260 	return (ENXIO);
20261 }
20262 
20263 /*
20264  * Net and subnet broadcast ire's are now specific to the particular
20265  * physical interface (ill) and not to any one locigal interface (ipif).
20266  * However, if a particular logical interface is being taken down, it's
20267  * associated ire's will be taken down as well.  Hence, when we go to
20268  * take down or change the local address, broadcast address or netmask
20269  * of a specific logical interface, we must check to make sure that we
20270  * have valid net and subnet broadcast ire's for the other logical
20271  * interfaces which may have been shared with the logical interface
20272  * being brought down or changed.
20273  *
20274  * There is one set of 0.0.0.0 and 255.255.255.255 per ill. Usually it
20275  * is tied to the first interface coming UP. If that ipif is going down,
20276  * we need to recreate them on the next valid ipif.
20277  *
20278  * Note: assume that the ipif passed in is still up so that it's IRE
20279  * entries are still valid.
20280  */
20281 static void
20282 ipif_check_bcast_ires(ipif_t *test_ipif)
20283 {
20284 	ipif_t	*ipif;
20285 	ire_t	*test_subnet_ire, *test_net_ire;
20286 	ire_t	*test_allzero_ire, *test_allone_ire;
20287 	ire_t	*ire_array[12];
20288 	ire_t	**irep = &ire_array[0];
20289 	ire_t	**irep1;
20290 
20291 	ipaddr_t net_addr, subnet_addr, net_mask, subnet_mask;
20292 	ipaddr_t test_net_addr, test_subnet_addr;
20293 	ipaddr_t test_net_mask, test_subnet_mask;
20294 	boolean_t need_net_bcast_ire = B_FALSE;
20295 	boolean_t need_subnet_bcast_ire = B_FALSE;
20296 	boolean_t allzero_bcast_ire_created = B_FALSE;
20297 	boolean_t allone_bcast_ire_created = B_FALSE;
20298 	boolean_t net_bcast_ire_created = B_FALSE;
20299 	boolean_t subnet_bcast_ire_created = B_FALSE;
20300 
20301 	ipif_t  *backup_ipif_net = (ipif_t *)NULL;
20302 	ipif_t  *backup_ipif_subnet = (ipif_t *)NULL;
20303 	ipif_t  *backup_ipif_allzeros = (ipif_t *)NULL;
20304 	ipif_t  *backup_ipif_allones = (ipif_t *)NULL;
20305 	uint64_t check_flags = IPIF_DEPRECATED | IPIF_NOLOCAL | IPIF_ANYCAST;
20306 
20307 	ASSERT(!test_ipif->ipif_isv6);
20308 	ASSERT(IAM_WRITER_IPIF(test_ipif));
20309 
20310 	/*
20311 	 * No broadcast IREs for the LOOPBACK interface
20312 	 * or others such as point to point and IPIF_NOXMIT.
20313 	 */
20314 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
20315 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
20316 		return;
20317 
20318 	test_allzero_ire = ire_ctable_lookup(0, 0, IRE_BROADCAST,
20319 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20320 
20321 	test_allone_ire = ire_ctable_lookup(INADDR_BROADCAST, 0, IRE_BROADCAST,
20322 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20323 
20324 	test_net_mask = ip_net_mask(test_ipif->ipif_subnet);
20325 	test_subnet_mask = test_ipif->ipif_net_mask;
20326 
20327 	/*
20328 	 * If no net mask set, assume the default based on net class.
20329 	 */
20330 	if (test_subnet_mask == 0)
20331 		test_subnet_mask = test_net_mask;
20332 
20333 	/*
20334 	 * Check if there is a network broadcast ire associated with this ipif
20335 	 */
20336 	test_net_addr = test_net_mask  & test_ipif->ipif_subnet;
20337 	test_net_ire = ire_ctable_lookup(test_net_addr, 0, IRE_BROADCAST,
20338 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20339 
20340 	/*
20341 	 * Check if there is a subnet broadcast IRE associated with this ipif
20342 	 */
20343 	test_subnet_addr = test_subnet_mask  & test_ipif->ipif_subnet;
20344 	test_subnet_ire = ire_ctable_lookup(test_subnet_addr, 0, IRE_BROADCAST,
20345 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20346 
20347 	/*
20348 	 * No broadcast ire's associated with this ipif.
20349 	 */
20350 	if ((test_subnet_ire == NULL) && (test_net_ire == NULL) &&
20351 	    (test_allzero_ire == NULL) && (test_allone_ire == NULL)) {
20352 		return;
20353 	}
20354 
20355 	/*
20356 	 * We have established which bcast ires have to be replaced.
20357 	 * Next we try to locate ipifs that match there ires.
20358 	 * The rules are simple: If we find an ipif that matches on the subnet
20359 	 * address it will also match on the net address, the allzeros and
20360 	 * allones address. Any ipif that matches only on the net address will
20361 	 * also match the allzeros and allones addresses.
20362 	 * The other criterion is the ipif_flags. We look for non-deprecated
20363 	 * (and non-anycast and non-nolocal) ipifs as the best choice.
20364 	 * ipifs with check_flags matching (deprecated, etc) are used only
20365 	 * if good ipifs are not available. While looping, we save existing
20366 	 * deprecated ipifs as backup_ipif.
20367 	 * We loop through all the ipifs for this ill looking for ipifs
20368 	 * whose broadcast addr match the ipif passed in, but do not have
20369 	 * their own broadcast ires. For creating 0.0.0.0 and
20370 	 * 255.255.255.255 we just need an ipif on this ill to create.
20371 	 */
20372 	for (ipif = test_ipif->ipif_ill->ill_ipif; ipif != NULL;
20373 	    ipif = ipif->ipif_next) {
20374 
20375 		ASSERT(!ipif->ipif_isv6);
20376 		/*
20377 		 * Already checked the ipif passed in.
20378 		 */
20379 		if (ipif == test_ipif) {
20380 			continue;
20381 		}
20382 
20383 		/*
20384 		 * We only need to recreate broadcast ires if another ipif in
20385 		 * the same zone uses them. The new ires must be created in the
20386 		 * same zone.
20387 		 */
20388 		if (ipif->ipif_zoneid != test_ipif->ipif_zoneid) {
20389 			continue;
20390 		}
20391 
20392 		/*
20393 		 * Only interested in logical interfaces with valid local
20394 		 * addresses or with the ability to broadcast.
20395 		 */
20396 		if ((ipif->ipif_subnet == 0) ||
20397 		    !(ipif->ipif_flags & IPIF_BROADCAST) ||
20398 		    (ipif->ipif_flags & IPIF_NOXMIT) ||
20399 		    !(ipif->ipif_flags & IPIF_UP)) {
20400 			continue;
20401 		}
20402 		/*
20403 		 * Check if there is a net broadcast ire for this
20404 		 * net address.  If it turns out that the ipif we are
20405 		 * about to take down owns this ire, we must make a
20406 		 * new one because it is potentially going away.
20407 		 */
20408 		if (test_net_ire && (!net_bcast_ire_created)) {
20409 			net_mask = ip_net_mask(ipif->ipif_subnet);
20410 			net_addr = net_mask & ipif->ipif_subnet;
20411 			if (net_addr == test_net_addr) {
20412 				need_net_bcast_ire = B_TRUE;
20413 				/*
20414 				 * Use DEPRECATED ipif only if no good
20415 				 * ires are available. subnet_addr is
20416 				 * a better match than net_addr.
20417 				 */
20418 				if ((ipif->ipif_flags & check_flags) &&
20419 				    (backup_ipif_net == NULL)) {
20420 					backup_ipif_net = ipif;
20421 				}
20422 			}
20423 		}
20424 		/*
20425 		 * Check if there is a subnet broadcast ire for this
20426 		 * net address.  If it turns out that the ipif we are
20427 		 * about to take down owns this ire, we must make a
20428 		 * new one because it is potentially going away.
20429 		 */
20430 		if (test_subnet_ire && (!subnet_bcast_ire_created)) {
20431 			subnet_mask = ipif->ipif_net_mask;
20432 			subnet_addr = ipif->ipif_subnet;
20433 			if (subnet_addr == test_subnet_addr) {
20434 				need_subnet_bcast_ire = B_TRUE;
20435 				if ((ipif->ipif_flags & check_flags) &&
20436 				    (backup_ipif_subnet == NULL)) {
20437 					backup_ipif_subnet = ipif;
20438 				}
20439 			}
20440 		}
20441 
20442 
20443 		/* Short circuit here if this ipif is deprecated */
20444 		if (ipif->ipif_flags & check_flags) {
20445 			if ((test_allzero_ire != NULL) &&
20446 			    (!allzero_bcast_ire_created) &&
20447 			    (backup_ipif_allzeros == NULL)) {
20448 				backup_ipif_allzeros = ipif;
20449 			}
20450 			if ((test_allone_ire != NULL) &&
20451 			    (!allone_bcast_ire_created) &&
20452 			    (backup_ipif_allones == NULL)) {
20453 				backup_ipif_allones = ipif;
20454 			}
20455 			continue;
20456 		}
20457 
20458 		/*
20459 		 * Found an ipif which has the same broadcast ire as the
20460 		 * ipif passed in and the ipif passed in "owns" the ire.
20461 		 * Create new broadcast ire's for this broadcast addr.
20462 		 */
20463 		if (need_net_bcast_ire && !net_bcast_ire_created) {
20464 			irep = ire_create_bcast(ipif, net_addr, irep);
20465 			irep = ire_create_bcast(ipif,
20466 			    ~net_mask | net_addr, irep);
20467 			net_bcast_ire_created = B_TRUE;
20468 		}
20469 		if (need_subnet_bcast_ire && !subnet_bcast_ire_created) {
20470 			irep = ire_create_bcast(ipif, subnet_addr, irep);
20471 			irep = ire_create_bcast(ipif,
20472 			    ~subnet_mask | subnet_addr, irep);
20473 			subnet_bcast_ire_created = B_TRUE;
20474 		}
20475 		if (test_allzero_ire != NULL && !allzero_bcast_ire_created) {
20476 			irep = ire_create_bcast(ipif, 0, irep);
20477 			allzero_bcast_ire_created = B_TRUE;
20478 		}
20479 		if (test_allone_ire != NULL && !allone_bcast_ire_created) {
20480 			irep = ire_create_bcast(ipif, INADDR_BROADCAST, irep);
20481 			allone_bcast_ire_created = B_TRUE;
20482 		}
20483 		/*
20484 		 * Once we have created all the appropriate ires, we
20485 		 * just break out of this loop to add what we have created.
20486 		 * This has been indented similar to ire_match_args for
20487 		 * readability.
20488 		 */
20489 		if (((test_net_ire == NULL) ||
20490 			(net_bcast_ire_created)) &&
20491 		    ((test_subnet_ire == NULL) ||
20492 			(subnet_bcast_ire_created)) &&
20493 		    ((test_allzero_ire == NULL) ||
20494 			(allzero_bcast_ire_created)) &&
20495 		    ((test_allone_ire == NULL) ||
20496 			(allone_bcast_ire_created))) {
20497 			break;
20498 		}
20499 	}
20500 
20501 	/*
20502 	 * Create bcast ires on deprecated ipifs if no non-deprecated ipifs
20503 	 * exist. 6 pairs of bcast ires are needed.
20504 	 * Note - the old ires are deleted in ipif_down.
20505 	 */
20506 	if (need_net_bcast_ire && !net_bcast_ire_created && backup_ipif_net) {
20507 		ipif = backup_ipif_net;
20508 		irep = ire_create_bcast(ipif, net_addr, irep);
20509 		irep = ire_create_bcast(ipif, ~net_mask | net_addr, irep);
20510 		net_bcast_ire_created = B_TRUE;
20511 	}
20512 	if (need_subnet_bcast_ire && !subnet_bcast_ire_created &&
20513 	    backup_ipif_subnet) {
20514 		ipif = backup_ipif_subnet;
20515 		irep = ire_create_bcast(ipif, subnet_addr, irep);
20516 		irep = ire_create_bcast(ipif,
20517 		    ~subnet_mask | subnet_addr, irep);
20518 		subnet_bcast_ire_created = B_TRUE;
20519 	}
20520 	if (test_allzero_ire != NULL && !allzero_bcast_ire_created &&
20521 	    backup_ipif_allzeros) {
20522 		irep = ire_create_bcast(backup_ipif_allzeros, 0, irep);
20523 		allzero_bcast_ire_created = B_TRUE;
20524 	}
20525 	if (test_allone_ire != NULL && !allone_bcast_ire_created &&
20526 	    backup_ipif_allones) {
20527 		irep = ire_create_bcast(backup_ipif_allones,
20528 		    INADDR_BROADCAST, irep);
20529 		allone_bcast_ire_created = B_TRUE;
20530 	}
20531 
20532 	/*
20533 	 * If we can't create all of them, don't add any of them.
20534 	 * Code in ip_wput_ire and ire_to_ill assumes that we
20535 	 * always have a non-loopback copy and loopback copy
20536 	 * for a given address.
20537 	 */
20538 	for (irep1 = irep; irep1 > ire_array; ) {
20539 		irep1--;
20540 		if (*irep1 == NULL) {
20541 			ip0dbg(("ipif_check_bcast_ires: can't create "
20542 			    "IRE_BROADCAST, memory allocation failure\n"));
20543 			while (irep > ire_array) {
20544 				irep--;
20545 				if (*irep != NULL)
20546 					ire_delete(*irep);
20547 			}
20548 			goto bad;
20549 		}
20550 	}
20551 	for (irep1 = irep; irep1 > ire_array; ) {
20552 		int error;
20553 
20554 		irep1--;
20555 		error = ire_add(irep1, NULL, NULL, NULL);
20556 		if (error == 0) {
20557 			ire_refrele(*irep1);		/* Held in ire_add */
20558 		}
20559 	}
20560 bad:
20561 	if (test_allzero_ire != NULL)
20562 		ire_refrele(test_allzero_ire);
20563 	if (test_allone_ire != NULL)
20564 		ire_refrele(test_allone_ire);
20565 	if (test_net_ire != NULL)
20566 		ire_refrele(test_net_ire);
20567 	if (test_subnet_ire != NULL)
20568 		ire_refrele(test_subnet_ire);
20569 }
20570 
20571 /*
20572  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
20573  * from lifr_flags and the name from lifr_name.
20574  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
20575  * since ipif_lookup_on_name uses the _isv6 flags when matching.
20576  * Returns EINPROGRESS when mp has been consumed by queueing it on
20577  * ill_pending_mp and the ioctl will complete in ip_rput.
20578  */
20579 /* ARGSUSED */
20580 int
20581 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20582     ip_ioctl_cmd_t *ipip, void *if_req)
20583 {
20584 	int	err;
20585 	ill_t	*ill;
20586 	struct lifreq *lifr = (struct lifreq *)if_req;
20587 
20588 	ASSERT(ipif != NULL);
20589 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
20590 	ASSERT(q->q_next != NULL);
20591 
20592 	ill = (ill_t *)q->q_ptr;
20593 	/*
20594 	 * If we are not writer on 'q' then this interface exists already
20595 	 * and previous lookups (ipif_extract_lifreq_cmn) found this ipif.
20596 	 * So return EALREADY
20597 	 */
20598 	if (ill != ipif->ipif_ill)
20599 		return (EALREADY);
20600 
20601 	if (ill->ill_name[0] != '\0')
20602 		return (EALREADY);
20603 
20604 	/*
20605 	 * Set all the flags. Allows all kinds of override. Provide some
20606 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
20607 	 * unless there is either multicast/broadcast support in the driver
20608 	 * or it is a pt-pt link.
20609 	 */
20610 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
20611 		/* Meaningless to IP thus don't allow them to be set. */
20612 		ip1dbg(("ip_setname: EINVAL 1\n"));
20613 		return (EINVAL);
20614 	}
20615 	/*
20616 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
20617 	 * ill_bcast_addr_length info.
20618 	 */
20619 	if (!ill->ill_needs_attach &&
20620 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
20621 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
20622 	    ill->ill_bcast_addr_length == 0)) {
20623 		/* Link not broadcast/pt-pt capable i.e. no multicast */
20624 		ip1dbg(("ip_setname: EINVAL 2\n"));
20625 		return (EINVAL);
20626 	}
20627 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
20628 	    ((lifr->lifr_flags & IFF_IPV6) ||
20629 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
20630 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
20631 		ip1dbg(("ip_setname: EINVAL 3\n"));
20632 		return (EINVAL);
20633 	}
20634 	if (lifr->lifr_flags & IFF_UP) {
20635 		/* Can only be set with SIOCSLIFFLAGS */
20636 		ip1dbg(("ip_setname: EINVAL 4\n"));
20637 		return (EINVAL);
20638 	}
20639 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
20640 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
20641 		ip1dbg(("ip_setname: EINVAL 5\n"));
20642 		return (EINVAL);
20643 	}
20644 	/*
20645 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
20646 	 */
20647 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
20648 	    !(lifr->lifr_flags & IFF_IPV6) &&
20649 	    !(ipif->ipif_isv6)) {
20650 		ip1dbg(("ip_setname: EINVAL 6\n"));
20651 		return (EINVAL);
20652 	}
20653 
20654 	/*
20655 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
20656 	 * we have all the flags here. So, we assign rather than we OR.
20657 	 * We can't OR the flags here because we don't want to set
20658 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
20659 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
20660 	 * on lifr_flags value here.
20661 	 */
20662 	/*
20663 	 * This ill has not been inserted into the global list.
20664 	 * So we are still single threaded and don't need any lock
20665 	 */
20666 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS;
20667 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
20668 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
20669 
20670 	/* We started off as V4. */
20671 	if (ill->ill_flags & ILLF_IPV6) {
20672 		ill->ill_phyint->phyint_illv6 = ill;
20673 		ill->ill_phyint->phyint_illv4 = NULL;
20674 	}
20675 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
20676 	return (err);
20677 }
20678 
20679 /* ARGSUSED */
20680 int
20681 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20682     ip_ioctl_cmd_t *ipip, void *if_req)
20683 {
20684 	/*
20685 	 * ill_phyint_reinit merged the v4 and v6 into a single
20686 	 * ipsq. Could also have become part of a ipmp group in the
20687 	 * process, and we might not have been able to complete the
20688 	 * slifname in ipif_set_values, if we could not become
20689 	 * exclusive.  If so restart it here
20690 	 */
20691 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
20692 }
20693 
20694 /*
20695  * Return a pointer to the ipif which matches the index, IP version type and
20696  * zoneid.
20697  */
20698 ipif_t *
20699 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
20700     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
20701 {
20702 	ill_t	*ill;
20703 	ipsq_t  *ipsq;
20704 	phyint_t *phyi;
20705 	ipif_t	*ipif;
20706 
20707 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
20708 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
20709 
20710 	if (err != NULL)
20711 		*err = 0;
20712 
20713 	/*
20714 	 * Indexes are stored in the phyint - a common structure
20715 	 * to both IPv4 and IPv6.
20716 	 */
20717 
20718 	rw_enter(&ill_g_lock, RW_READER);
20719 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
20720 	    (void *) &index, NULL);
20721 	if (phyi != NULL) {
20722 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
20723 		if (ill == NULL) {
20724 			rw_exit(&ill_g_lock);
20725 			if (err != NULL)
20726 				*err = ENXIO;
20727 			return (NULL);
20728 		}
20729 		GRAB_CONN_LOCK(q);
20730 		mutex_enter(&ill->ill_lock);
20731 		if (ILL_CAN_LOOKUP(ill)) {
20732 			for (ipif = ill->ill_ipif; ipif != NULL;
20733 			    ipif = ipif->ipif_next) {
20734 				if (IPIF_CAN_LOOKUP(ipif) &&
20735 				    (zoneid == ALL_ZONES ||
20736 				    zoneid == ipif->ipif_zoneid ||
20737 				    ipif->ipif_zoneid == ALL_ZONES)) {
20738 					ipif_refhold_locked(ipif);
20739 					mutex_exit(&ill->ill_lock);
20740 					RELEASE_CONN_LOCK(q);
20741 					rw_exit(&ill_g_lock);
20742 					return (ipif);
20743 				}
20744 			}
20745 		} else if (ILL_CAN_WAIT(ill, q)) {
20746 			ipsq = ill->ill_phyint->phyint_ipsq;
20747 			mutex_enter(&ipsq->ipsq_lock);
20748 			rw_exit(&ill_g_lock);
20749 			mutex_exit(&ill->ill_lock);
20750 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
20751 			mutex_exit(&ipsq->ipsq_lock);
20752 			RELEASE_CONN_LOCK(q);
20753 			*err = EINPROGRESS;
20754 			return (NULL);
20755 		}
20756 		mutex_exit(&ill->ill_lock);
20757 		RELEASE_CONN_LOCK(q);
20758 	}
20759 	rw_exit(&ill_g_lock);
20760 	if (err != NULL)
20761 		*err = ENXIO;
20762 	return (NULL);
20763 }
20764 
20765 typedef struct conn_change_s {
20766 	uint_t cc_old_ifindex;
20767 	uint_t cc_new_ifindex;
20768 } conn_change_t;
20769 
20770 /*
20771  * ipcl_walk function for changing interface index.
20772  */
20773 static void
20774 conn_change_ifindex(conn_t *connp, caddr_t arg)
20775 {
20776 	conn_change_t *connc;
20777 	uint_t old_ifindex;
20778 	uint_t new_ifindex;
20779 	int i;
20780 	ilg_t *ilg;
20781 
20782 	connc = (conn_change_t *)arg;
20783 	old_ifindex = connc->cc_old_ifindex;
20784 	new_ifindex = connc->cc_new_ifindex;
20785 
20786 	if (connp->conn_orig_bound_ifindex == old_ifindex)
20787 		connp->conn_orig_bound_ifindex = new_ifindex;
20788 
20789 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
20790 		connp->conn_orig_multicast_ifindex = new_ifindex;
20791 
20792 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
20793 		connp->conn_orig_xmit_ifindex = new_ifindex;
20794 
20795 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
20796 		ilg = &connp->conn_ilg[i];
20797 		if (ilg->ilg_orig_ifindex == old_ifindex)
20798 			ilg->ilg_orig_ifindex = new_ifindex;
20799 	}
20800 }
20801 
20802 /*
20803  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
20804  * to new_index if it matches the old_index.
20805  *
20806  * Failovers typically happen within a group of ills. But somebody
20807  * can remove an ill from the group after a failover happened. If
20808  * we are setting the ifindex after this, we potentially need to
20809  * look at all the ills rather than just the ones in the group.
20810  * We cut down the work by looking at matching ill_net_types
20811  * and ill_types as we could not possibly grouped them together.
20812  */
20813 static void
20814 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
20815 {
20816 	ill_t *ill;
20817 	ipif_t *ipif;
20818 	uint_t old_ifindex;
20819 	uint_t new_ifindex;
20820 	ilm_t *ilm;
20821 	ill_walk_context_t ctx;
20822 
20823 	old_ifindex = connc->cc_old_ifindex;
20824 	new_ifindex = connc->cc_new_ifindex;
20825 
20826 	rw_enter(&ill_g_lock, RW_READER);
20827 	ill = ILL_START_WALK_ALL(&ctx);
20828 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
20829 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
20830 			(ill_orig->ill_type != ill->ill_type)) {
20831 			continue;
20832 		}
20833 		for (ipif = ill->ill_ipif; ipif != NULL;
20834 				ipif = ipif->ipif_next) {
20835 			if (ipif->ipif_orig_ifindex == old_ifindex)
20836 				ipif->ipif_orig_ifindex = new_ifindex;
20837 		}
20838 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
20839 			if (ilm->ilm_orig_ifindex == old_ifindex)
20840 				ilm->ilm_orig_ifindex = new_ifindex;
20841 		}
20842 	}
20843 	rw_exit(&ill_g_lock);
20844 }
20845 
20846 /*
20847  * We first need to ensure that the new index is unique, and
20848  * then carry the change across both v4 and v6 ill representation
20849  * of the physical interface.
20850  */
20851 /* ARGSUSED */
20852 int
20853 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20854     ip_ioctl_cmd_t *ipip, void *ifreq)
20855 {
20856 	ill_t		*ill;
20857 	ill_t		*ill_other;
20858 	phyint_t	*phyi;
20859 	int		old_index;
20860 	conn_change_t	connc;
20861 	struct ifreq	*ifr = (struct ifreq *)ifreq;
20862 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20863 	uint_t	index;
20864 	ill_t	*ill_v4;
20865 	ill_t	*ill_v6;
20866 
20867 	if (ipip->ipi_cmd_type == IF_CMD)
20868 		index = ifr->ifr_index;
20869 	else
20870 		index = lifr->lifr_index;
20871 
20872 	/*
20873 	 * Only allow on physical interface. Also, index zero is illegal.
20874 	 *
20875 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
20876 	 *
20877 	 * 1) If PHYI_FAILED is set, a failover could have happened which
20878 	 *    implies a possible failback might have to happen. As failback
20879 	 *    depends on the old index, we should fail setting the index.
20880 	 *
20881 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
20882 	 *    any addresses or multicast memberships are failed over to
20883 	 *    a non-STANDBY interface. As failback depends on the old
20884 	 *    index, we should fail setting the index for this case also.
20885 	 *
20886 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
20887 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
20888 	 */
20889 	ill = ipif->ipif_ill;
20890 	phyi = ill->ill_phyint;
20891 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
20892 	    ipif->ipif_id != 0 || index == 0) {
20893 		return (EINVAL);
20894 	}
20895 	old_index = phyi->phyint_ifindex;
20896 
20897 	/* If the index is not changing, no work to do */
20898 	if (old_index == index)
20899 		return (0);
20900 
20901 	/*
20902 	 * Use ill_lookup_on_ifindex to determine if the
20903 	 * new index is unused and if so allow the change.
20904 	 */
20905 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL);
20906 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL);
20907 	if (ill_v6 != NULL || ill_v4 != NULL) {
20908 		if (ill_v4 != NULL)
20909 			ill_refrele(ill_v4);
20910 		if (ill_v6 != NULL)
20911 			ill_refrele(ill_v6);
20912 		return (EBUSY);
20913 	}
20914 
20915 	/*
20916 	 * The new index is unused. Set it in the phyint.
20917 	 * Locate the other ill so that we can send a routing
20918 	 * sockets message.
20919 	 */
20920 	if (ill->ill_isv6) {
20921 		ill_other = phyi->phyint_illv4;
20922 	} else {
20923 		ill_other = phyi->phyint_illv6;
20924 	}
20925 
20926 	phyi->phyint_ifindex = index;
20927 
20928 	connc.cc_old_ifindex = old_index;
20929 	connc.cc_new_ifindex = index;
20930 	ip_change_ifindex(ill, &connc);
20931 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc);
20932 
20933 	/* Send the routing sockets message */
20934 	ip_rts_ifmsg(ipif);
20935 	if (ill_other != NULL)
20936 		ip_rts_ifmsg(ill_other->ill_ipif);
20937 
20938 	return (0);
20939 }
20940 
20941 /* ARGSUSED */
20942 int
20943 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20944     ip_ioctl_cmd_t *ipip, void *ifreq)
20945 {
20946 	struct ifreq	*ifr = (struct ifreq *)ifreq;
20947 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20948 
20949 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
20950 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20951 	/* Get the interface index */
20952 	if (ipip->ipi_cmd_type == IF_CMD) {
20953 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
20954 	} else {
20955 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
20956 	}
20957 	return (0);
20958 }
20959 
20960 /* ARGSUSED */
20961 int
20962 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20963     ip_ioctl_cmd_t *ipip, void *ifreq)
20964 {
20965 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20966 
20967 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
20968 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20969 	/* Get the interface zone */
20970 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
20971 	lifr->lifr_zoneid = ipif->ipif_zoneid;
20972 	return (0);
20973 }
20974 
20975 /*
20976  * Set the zoneid of an interface.
20977  */
20978 /* ARGSUSED */
20979 int
20980 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20981     ip_ioctl_cmd_t *ipip, void *ifreq)
20982 {
20983 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20984 	int err = 0;
20985 	boolean_t need_up = B_FALSE;
20986 	zone_t *zptr;
20987 	zone_status_t status;
20988 	zoneid_t zoneid;
20989 
20990 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
20991 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
20992 		if (!is_system_labeled())
20993 			return (ENOTSUP);
20994 		zoneid = GLOBAL_ZONEID;
20995 	}
20996 
20997 	/* cannot assign instance zero to a non-global zone */
20998 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
20999 		return (ENOTSUP);
21000 
21001 	/*
21002 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
21003 	 * the event of a race with the zone shutdown processing, since IP
21004 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
21005 	 * interface will be cleaned up even if the zone is shut down
21006 	 * immediately after the status check. If the interface can't be brought
21007 	 * down right away, and the zone is shut down before the restart
21008 	 * function is called, we resolve the possible races by rechecking the
21009 	 * zone status in the restart function.
21010 	 */
21011 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
21012 		return (EINVAL);
21013 	status = zone_status_get(zptr);
21014 	zone_rele(zptr);
21015 
21016 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
21017 		return (EINVAL);
21018 
21019 	if (ipif->ipif_flags & IPIF_UP) {
21020 		/*
21021 		 * If the interface is already marked up,
21022 		 * we call ipif_down which will take care
21023 		 * of ditching any IREs that have been set
21024 		 * up based on the old interface address.
21025 		 */
21026 		err = ipif_logical_down(ipif, q, mp);
21027 		if (err == EINPROGRESS)
21028 			return (err);
21029 		ipif_down_tail(ipif);
21030 		need_up = B_TRUE;
21031 	}
21032 
21033 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
21034 	return (err);
21035 }
21036 
21037 static int
21038 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
21039     queue_t *q, mblk_t *mp, boolean_t need_up)
21040 {
21041 	int	err = 0;
21042 
21043 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
21044 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21045 
21046 	/* Set the new zone id. */
21047 	ipif->ipif_zoneid = zoneid;
21048 
21049 	/* Update sctp list */
21050 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
21051 
21052 	if (need_up) {
21053 		/*
21054 		 * Now bring the interface back up.  If this
21055 		 * is the only IPIF for the ILL, ipif_up
21056 		 * will have to re-bind to the device, so
21057 		 * we may get back EINPROGRESS, in which
21058 		 * case, this IOCTL will get completed in
21059 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
21060 		 */
21061 		err = ipif_up(ipif, q, mp);
21062 	}
21063 	return (err);
21064 }
21065 
21066 /* ARGSUSED */
21067 int
21068 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21069     ip_ioctl_cmd_t *ipip, void *if_req)
21070 {
21071 	struct lifreq *lifr = (struct lifreq *)if_req;
21072 	zoneid_t zoneid;
21073 	zone_t *zptr;
21074 	zone_status_t status;
21075 
21076 	ASSERT(ipif->ipif_id != 0);
21077 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21078 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
21079 		zoneid = GLOBAL_ZONEID;
21080 
21081 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
21082 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21083 
21084 	/*
21085 	 * We recheck the zone status to resolve the following race condition:
21086 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
21087 	 * 2) hme0:1 is up and can't be brought down right away;
21088 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
21089 	 * 3) zone "myzone" is halted; the zone status switches to
21090 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
21091 	 * the interfaces to remove - hme0:1 is not returned because it's not
21092 	 * yet in "myzone", so it won't be removed;
21093 	 * 4) the restart function for SIOCSLIFZONE is called; without the
21094 	 * status check here, we would have hme0:1 in "myzone" after it's been
21095 	 * destroyed.
21096 	 * Note that if the status check fails, we need to bring the interface
21097 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
21098 	 * ipif_up_done[_v6]().
21099 	 */
21100 	status = ZONE_IS_UNINITIALIZED;
21101 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
21102 		status = zone_status_get(zptr);
21103 		zone_rele(zptr);
21104 	}
21105 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
21106 		if (ipif->ipif_isv6) {
21107 			(void) ipif_up_done_v6(ipif);
21108 		} else {
21109 			(void) ipif_up_done(ipif);
21110 		}
21111 		return (EINVAL);
21112 	}
21113 
21114 	ipif_down_tail(ipif);
21115 
21116 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
21117 	    B_TRUE));
21118 }
21119 
21120 /* ARGSUSED */
21121 int
21122 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21123 	ip_ioctl_cmd_t *ipip, void *ifreq)
21124 {
21125 	struct lifreq	*lifr = ifreq;
21126 
21127 	ASSERT(q->q_next == NULL);
21128 	ASSERT(CONN_Q(q));
21129 
21130 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
21131 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21132 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
21133 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
21134 
21135 	return (0);
21136 }
21137 
21138 
21139 /* Find the previous ILL in this usesrc group */
21140 static ill_t *
21141 ill_prev_usesrc(ill_t *uill)
21142 {
21143 	ill_t *ill;
21144 
21145 	for (ill = uill->ill_usesrc_grp_next;
21146 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
21147 	    ill = ill->ill_usesrc_grp_next)
21148 		/* do nothing */;
21149 	return (ill);
21150 }
21151 
21152 /*
21153  * Release all members of the usesrc group. This routine is called
21154  * from ill_delete when the interface being unplumbed is the
21155  * group head.
21156  */
21157 static void
21158 ill_disband_usesrc_group(ill_t *uill)
21159 {
21160 	ill_t *next_ill, *tmp_ill;
21161 	ASSERT(RW_WRITE_HELD(&ill_g_usesrc_lock));
21162 	next_ill = uill->ill_usesrc_grp_next;
21163 
21164 	do {
21165 		ASSERT(next_ill != NULL);
21166 		tmp_ill = next_ill->ill_usesrc_grp_next;
21167 		ASSERT(tmp_ill != NULL);
21168 		next_ill->ill_usesrc_grp_next = NULL;
21169 		next_ill->ill_usesrc_ifindex = 0;
21170 		next_ill = tmp_ill;
21171 	} while (next_ill->ill_usesrc_ifindex != 0);
21172 	uill->ill_usesrc_grp_next = NULL;
21173 }
21174 
21175 /*
21176  * Remove the client usesrc ILL from the list and relink to a new list
21177  */
21178 int
21179 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
21180 {
21181 	ill_t *ill, *tmp_ill;
21182 
21183 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
21184 	    (uill != NULL) && RW_WRITE_HELD(&ill_g_usesrc_lock));
21185 
21186 	/*
21187 	 * Check if the usesrc client ILL passed in is not already
21188 	 * in use as a usesrc ILL i.e one whose source address is
21189 	 * in use OR a usesrc ILL is not already in use as a usesrc
21190 	 * client ILL
21191 	 */
21192 	if ((ucill->ill_usesrc_ifindex == 0) ||
21193 	    (uill->ill_usesrc_ifindex != 0)) {
21194 		return (-1);
21195 	}
21196 
21197 	ill = ill_prev_usesrc(ucill);
21198 	ASSERT(ill->ill_usesrc_grp_next != NULL);
21199 
21200 	/* Remove from the current list */
21201 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
21202 		/* Only two elements in the list */
21203 		ASSERT(ill->ill_usesrc_ifindex == 0);
21204 		ill->ill_usesrc_grp_next = NULL;
21205 	} else {
21206 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
21207 	}
21208 
21209 	if (ifindex == 0) {
21210 		ucill->ill_usesrc_ifindex = 0;
21211 		ucill->ill_usesrc_grp_next = NULL;
21212 		return (0);
21213 	}
21214 
21215 	ucill->ill_usesrc_ifindex = ifindex;
21216 	tmp_ill = uill->ill_usesrc_grp_next;
21217 	uill->ill_usesrc_grp_next = ucill;
21218 	ucill->ill_usesrc_grp_next =
21219 	    (tmp_ill != NULL) ? tmp_ill : uill;
21220 	return (0);
21221 }
21222 
21223 /*
21224  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
21225  * ip.c for locking details.
21226  */
21227 /* ARGSUSED */
21228 int
21229 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21230     ip_ioctl_cmd_t *ipip, void *ifreq)
21231 {
21232 	struct lifreq *lifr = (struct lifreq *)ifreq;
21233 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
21234 	    ill_flag_changed = B_FALSE;
21235 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
21236 	int err = 0, ret;
21237 	uint_t ifindex;
21238 	phyint_t *us_phyint, *us_cli_phyint;
21239 	ipsq_t *ipsq = NULL;
21240 
21241 	ASSERT(IAM_WRITER_IPIF(ipif));
21242 	ASSERT(q->q_next == NULL);
21243 	ASSERT(CONN_Q(q));
21244 
21245 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
21246 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
21247 
21248 	ASSERT(us_cli_phyint != NULL);
21249 
21250 	/*
21251 	 * If the client ILL is being used for IPMP, abort.
21252 	 * Note, this can be done before ipsq_try_enter since we are already
21253 	 * exclusive on this ILL
21254 	 */
21255 	if ((us_cli_phyint->phyint_groupname != NULL) ||
21256 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
21257 		return (EINVAL);
21258 	}
21259 
21260 	ifindex = lifr->lifr_index;
21261 	if (ifindex == 0) {
21262 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
21263 			/* non usesrc group interface, nothing to reset */
21264 			return (0);
21265 		}
21266 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
21267 		/* valid reset request */
21268 		reset_flg = B_TRUE;
21269 	}
21270 
21271 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
21272 	    ip_process_ioctl, &err);
21273 
21274 	if (usesrc_ill == NULL) {
21275 		return (err);
21276 	}
21277 
21278 	/*
21279 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
21280 	 * group nor can either of the interfaces be used for standy. So
21281 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
21282 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
21283 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
21284 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
21285 	 * the usesrc_cli_ill
21286 	 */
21287 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
21288 	    NEW_OP, B_TRUE);
21289 	if (ipsq == NULL) {
21290 		err = EINPROGRESS;
21291 		/* Operation enqueued on the ipsq of the usesrc ILL */
21292 		goto done;
21293 	}
21294 
21295 	/* Check if the usesrc_ill is used for IPMP */
21296 	us_phyint = usesrc_ill->ill_phyint;
21297 	if ((us_phyint->phyint_groupname != NULL) ||
21298 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
21299 		err = EINVAL;
21300 		goto done;
21301 	}
21302 
21303 	/*
21304 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
21305 	 * already a client then return EINVAL
21306 	 */
21307 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
21308 		err = EINVAL;
21309 		goto done;
21310 	}
21311 
21312 	/*
21313 	 * If the ill_usesrc_ifindex field is already set to what it needs to
21314 	 * be then this is a duplicate operation.
21315 	 */
21316 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
21317 		err = 0;
21318 		goto done;
21319 	}
21320 
21321 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
21322 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
21323 	    usesrc_ill->ill_isv6));
21324 
21325 	/*
21326 	 * The next step ensures that no new ires will be created referencing
21327 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
21328 	 * we go through an ire walk deleting all ire caches that reference
21329 	 * the client ill. New ires referencing the client ill that are added
21330 	 * to the ire table before the ILL_CHANGING flag is set, will be
21331 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
21332 	 * the client ill while the ILL_CHANGING flag is set will be failed
21333 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
21334 	 * checks (under the ill_g_usesrc_lock) that the ire being added
21335 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
21336 	 * belong to the same usesrc group.
21337 	 */
21338 	mutex_enter(&usesrc_cli_ill->ill_lock);
21339 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
21340 	mutex_exit(&usesrc_cli_ill->ill_lock);
21341 	ill_flag_changed = B_TRUE;
21342 
21343 	if (ipif->ipif_isv6)
21344 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
21345 		    ALL_ZONES);
21346 	else
21347 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
21348 		    ALL_ZONES);
21349 
21350 	/*
21351 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
21352 	 * and the ill_usesrc_ifindex fields
21353 	 */
21354 	rw_enter(&ill_g_usesrc_lock, RW_WRITER);
21355 
21356 	if (reset_flg) {
21357 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
21358 		if (ret != 0) {
21359 			err = EINVAL;
21360 		}
21361 		rw_exit(&ill_g_usesrc_lock);
21362 		goto done;
21363 	}
21364 
21365 	/*
21366 	 * Four possibilities to consider:
21367 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
21368 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
21369 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
21370 	 * 4. Both are part of their respective usesrc groups
21371 	 */
21372 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
21373 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
21374 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
21375 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
21376 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
21377 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
21378 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
21379 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
21380 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
21381 		/* Insert at head of list */
21382 		usesrc_cli_ill->ill_usesrc_grp_next =
21383 		    usesrc_ill->ill_usesrc_grp_next;
21384 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
21385 	} else {
21386 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
21387 		    ifindex);
21388 		if (ret != 0)
21389 			err = EINVAL;
21390 	}
21391 	rw_exit(&ill_g_usesrc_lock);
21392 
21393 done:
21394 	if (ill_flag_changed) {
21395 		mutex_enter(&usesrc_cli_ill->ill_lock);
21396 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
21397 		mutex_exit(&usesrc_cli_ill->ill_lock);
21398 	}
21399 	if (ipsq != NULL)
21400 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
21401 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
21402 	ill_refrele(usesrc_ill);
21403 	return (err);
21404 }
21405 
21406 /*
21407  * comparison function used by avl.
21408  */
21409 static int
21410 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
21411 {
21412 
21413 	uint_t index;
21414 
21415 	ASSERT(phyip != NULL && index_ptr != NULL);
21416 
21417 	index = *((uint_t *)index_ptr);
21418 	/*
21419 	 * let the phyint with the lowest index be on top.
21420 	 */
21421 	if (((phyint_t *)phyip)->phyint_ifindex < index)
21422 		return (1);
21423 	if (((phyint_t *)phyip)->phyint_ifindex > index)
21424 		return (-1);
21425 	return (0);
21426 }
21427 
21428 /*
21429  * comparison function used by avl.
21430  */
21431 static int
21432 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
21433 {
21434 	ill_t *ill;
21435 	int res = 0;
21436 
21437 	ASSERT(phyip != NULL && name_ptr != NULL);
21438 
21439 	if (((phyint_t *)phyip)->phyint_illv4)
21440 		ill = ((phyint_t *)phyip)->phyint_illv4;
21441 	else
21442 		ill = ((phyint_t *)phyip)->phyint_illv6;
21443 	ASSERT(ill != NULL);
21444 
21445 	res = strcmp(ill->ill_name, (char *)name_ptr);
21446 	if (res > 0)
21447 		return (1);
21448 	else if (res < 0)
21449 		return (-1);
21450 	return (0);
21451 }
21452 /*
21453  * This function is called from ill_delete when the ill is being
21454  * unplumbed. We remove the reference from the phyint and we also
21455  * free the phyint when there are no more references to it.
21456  */
21457 static void
21458 ill_phyint_free(ill_t *ill)
21459 {
21460 	phyint_t *phyi;
21461 	phyint_t *next_phyint;
21462 	ipsq_t *cur_ipsq;
21463 
21464 	ASSERT(ill->ill_phyint != NULL);
21465 
21466 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
21467 	phyi = ill->ill_phyint;
21468 	ill->ill_phyint = NULL;
21469 	/*
21470 	 * ill_init allocates a phyint always to store the copy
21471 	 * of flags relevant to phyint. At that point in time, we could
21472 	 * not assign the name and hence phyint_illv4/v6 could not be
21473 	 * initialized. Later in ipif_set_values, we assign the name to
21474 	 * the ill, at which point in time we assign phyint_illv4/v6.
21475 	 * Thus we don't rely on phyint_illv6 to be initialized always.
21476 	 */
21477 	if (ill->ill_flags & ILLF_IPV6) {
21478 		phyi->phyint_illv6 = NULL;
21479 	} else {
21480 		phyi->phyint_illv4 = NULL;
21481 	}
21482 	/*
21483 	 * ipif_down removes it from the group when the last ipif goes
21484 	 * down.
21485 	 */
21486 	ASSERT(ill->ill_group == NULL);
21487 
21488 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
21489 		return;
21490 
21491 	/*
21492 	 * Make sure this phyint was put in the list.
21493 	 */
21494 	if (phyi->phyint_ifindex > 0) {
21495 		avl_remove(&phyint_g_list.phyint_list_avl_by_index,
21496 		    phyi);
21497 		avl_remove(&phyint_g_list.phyint_list_avl_by_name,
21498 		    phyi);
21499 	}
21500 	/*
21501 	 * remove phyint from the ipsq list.
21502 	 */
21503 	cur_ipsq = phyi->phyint_ipsq;
21504 	if (phyi == cur_ipsq->ipsq_phyint_list) {
21505 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
21506 	} else {
21507 		next_phyint = cur_ipsq->ipsq_phyint_list;
21508 		while (next_phyint != NULL) {
21509 			if (next_phyint->phyint_ipsq_next == phyi) {
21510 				next_phyint->phyint_ipsq_next =
21511 					phyi->phyint_ipsq_next;
21512 				break;
21513 			}
21514 			next_phyint = next_phyint->phyint_ipsq_next;
21515 		}
21516 		ASSERT(next_phyint != NULL);
21517 	}
21518 	IPSQ_DEC_REF(cur_ipsq);
21519 
21520 	if (phyi->phyint_groupname_len != 0) {
21521 		ASSERT(phyi->phyint_groupname != NULL);
21522 		mi_free(phyi->phyint_groupname);
21523 	}
21524 	mi_free(phyi);
21525 }
21526 
21527 /*
21528  * Attach the ill to the phyint structure which can be shared by both
21529  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
21530  * function is called from ipif_set_values and ill_lookup_on_name (for
21531  * loopback) where we know the name of the ill. We lookup the ill and if
21532  * there is one present already with the name use that phyint. Otherwise
21533  * reuse the one allocated by ill_init.
21534  */
21535 static void
21536 ill_phyint_reinit(ill_t *ill)
21537 {
21538 	boolean_t isv6 = ill->ill_isv6;
21539 	phyint_t *phyi_old;
21540 	phyint_t *phyi;
21541 	avl_index_t where = 0;
21542 	ill_t	*ill_other = NULL;
21543 	ipsq_t	*ipsq;
21544 
21545 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
21546 
21547 	phyi_old = ill->ill_phyint;
21548 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
21549 	    phyi_old->phyint_illv6 == NULL));
21550 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
21551 	    phyi_old->phyint_illv4 == NULL));
21552 	ASSERT(phyi_old->phyint_ifindex == 0);
21553 
21554 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_name,
21555 	    ill->ill_name, &where);
21556 
21557 	/*
21558 	 * 1. We grabbed the ill_g_lock before inserting this ill into
21559 	 *    the global list of ills. So no other thread could have located
21560 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
21561 	 * 2. Now locate the other protocol instance of this ill.
21562 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
21563 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
21564 	 *    of neither ill can change.
21565 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
21566 	 *    other ill.
21567 	 * 5. Release all locks.
21568 	 */
21569 
21570 	/*
21571 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
21572 	 * we are initializing IPv4.
21573 	 */
21574 	if (phyi != NULL) {
21575 		ill_other = (isv6) ? phyi->phyint_illv4 :
21576 		    phyi->phyint_illv6;
21577 		ASSERT(ill_other->ill_phyint != NULL);
21578 		ASSERT((isv6 && !ill_other->ill_isv6) ||
21579 		    (!isv6 && ill_other->ill_isv6));
21580 		GRAB_ILL_LOCKS(ill, ill_other);
21581 		/*
21582 		 * We are potentially throwing away phyint_flags which
21583 		 * could be different from the one that we obtain from
21584 		 * ill_other->ill_phyint. But it is okay as we are assuming
21585 		 * that the state maintained within IP is correct.
21586 		 */
21587 		mutex_enter(&phyi->phyint_lock);
21588 		if (isv6) {
21589 			ASSERT(phyi->phyint_illv6 == NULL);
21590 			phyi->phyint_illv6 = ill;
21591 		} else {
21592 			ASSERT(phyi->phyint_illv4 == NULL);
21593 			phyi->phyint_illv4 = ill;
21594 		}
21595 		/*
21596 		 * This is a new ill, currently undergoing SLIFNAME
21597 		 * So we could not have joined an IPMP group until now.
21598 		 */
21599 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
21600 		    phyi_old->phyint_groupname == NULL);
21601 
21602 		/*
21603 		 * This phyi_old is going away. Decref ipsq_refs and
21604 		 * assert it is zero. The ipsq itself will be freed in
21605 		 * ipsq_exit
21606 		 */
21607 		ipsq = phyi_old->phyint_ipsq;
21608 		IPSQ_DEC_REF(ipsq);
21609 		ASSERT(ipsq->ipsq_refs == 0);
21610 		/* Get the singleton phyint out of the ipsq list */
21611 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
21612 		ipsq->ipsq_phyint_list = NULL;
21613 		phyi_old->phyint_illv4 = NULL;
21614 		phyi_old->phyint_illv6 = NULL;
21615 		mi_free(phyi_old);
21616 	} else {
21617 		mutex_enter(&ill->ill_lock);
21618 		/*
21619 		 * We don't need to acquire any lock, since
21620 		 * the ill is not yet visible globally  and we
21621 		 * have not yet released the ill_g_lock.
21622 		 */
21623 		phyi = phyi_old;
21624 		mutex_enter(&phyi->phyint_lock);
21625 		/* XXX We need a recovery strategy here. */
21626 		if (!phyint_assign_ifindex(phyi))
21627 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
21628 
21629 		avl_insert(&phyint_g_list.phyint_list_avl_by_name,
21630 		    (void *)phyi, where);
21631 
21632 		(void) avl_find(&phyint_g_list.phyint_list_avl_by_index,
21633 		    &phyi->phyint_ifindex, &where);
21634 		avl_insert(&phyint_g_list.phyint_list_avl_by_index,
21635 		    (void *)phyi, where);
21636 	}
21637 
21638 	/*
21639 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
21640 	 * pending mp is not affected because that is per ill basis.
21641 	 */
21642 	ill->ill_phyint = phyi;
21643 
21644 	/*
21645 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
21646 	 * We do this here as when the first ipif was allocated,
21647 	 * ipif_allocate does not know the right interface index.
21648 	 */
21649 
21650 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
21651 	/*
21652 	 * Now that the phyint's ifindex has been assigned, complete the
21653 	 * remaining
21654 	 */
21655 	if (ill->ill_isv6) {
21656 		ill->ill_ip6_mib->ipv6IfIndex =
21657 		    ill->ill_phyint->phyint_ifindex;
21658 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
21659 		    ill->ill_phyint->phyint_ifindex;
21660 	}
21661 
21662 	RELEASE_ILL_LOCKS(ill, ill_other);
21663 	mutex_exit(&phyi->phyint_lock);
21664 }
21665 
21666 /*
21667  * Notify any downstream modules of the name of this interface.
21668  * An M_IOCTL is used even though we don't expect a successful reply.
21669  * Any reply message from the driver (presumably an M_IOCNAK) will
21670  * eventually get discarded somewhere upstream.  The message format is
21671  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
21672  * to IP.
21673  */
21674 static void
21675 ip_ifname_notify(ill_t *ill, queue_t *q)
21676 {
21677 	mblk_t *mp1, *mp2;
21678 	struct iocblk *iocp;
21679 	struct lifreq *lifr;
21680 
21681 	mp1 = mkiocb(SIOCSLIFNAME);
21682 	if (mp1 == NULL)
21683 		return;
21684 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
21685 	if (mp2 == NULL) {
21686 		freeb(mp1);
21687 		return;
21688 	}
21689 
21690 	mp1->b_cont = mp2;
21691 	iocp = (struct iocblk *)mp1->b_rptr;
21692 	iocp->ioc_count = sizeof (struct lifreq);
21693 
21694 	lifr = (struct lifreq *)mp2->b_rptr;
21695 	mp2->b_wptr += sizeof (struct lifreq);
21696 	bzero(lifr, sizeof (struct lifreq));
21697 
21698 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
21699 	lifr->lifr_ppa = ill->ill_ppa;
21700 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
21701 
21702 	putnext(q, mp1);
21703 }
21704 
21705 static boolean_t ip_trash_timer_started = B_FALSE;
21706 
21707 static int
21708 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
21709 {
21710 	int err;
21711 
21712 	/* Set the obsolete NDD per-interface forwarding name. */
21713 	err = ill_set_ndd_name(ill);
21714 	if (err != 0) {
21715 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
21716 		    err);
21717 	}
21718 
21719 	/* Tell downstream modules where they are. */
21720 	ip_ifname_notify(ill, q);
21721 
21722 	/*
21723 	 * ill_dl_phys returns EINPROGRESS in the usual case.
21724 	 * Error cases are ENOMEM ...
21725 	 */
21726 	err = ill_dl_phys(ill, ipif, mp, q);
21727 
21728 	/*
21729 	 * If there is no IRE expiration timer running, get one started.
21730 	 * igmp and mld timers will be triggered by the first multicast
21731 	 */
21732 	if (!ip_trash_timer_started) {
21733 		/*
21734 		 * acquire the lock and check again.
21735 		 */
21736 		mutex_enter(&ip_trash_timer_lock);
21737 		if (!ip_trash_timer_started) {
21738 			ip_ire_expire_id = timeout(ip_trash_timer_expire, NULL,
21739 			    MSEC_TO_TICK(ip_timer_interval));
21740 			ip_trash_timer_started = B_TRUE;
21741 		}
21742 		mutex_exit(&ip_trash_timer_lock);
21743 	}
21744 
21745 	if (ill->ill_isv6) {
21746 		mutex_enter(&mld_slowtimeout_lock);
21747 		if (mld_slowtimeout_id == 0) {
21748 			mld_slowtimeout_id = timeout(mld_slowtimo, NULL,
21749 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
21750 		}
21751 		mutex_exit(&mld_slowtimeout_lock);
21752 	} else {
21753 		mutex_enter(&igmp_slowtimeout_lock);
21754 		if (igmp_slowtimeout_id == 0) {
21755 			igmp_slowtimeout_id = timeout(igmp_slowtimo, NULL,
21756 				MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
21757 		}
21758 		mutex_exit(&igmp_slowtimeout_lock);
21759 	}
21760 
21761 	return (err);
21762 }
21763 
21764 /*
21765  * Common routine for ppa and ifname setting. Should be called exclusive.
21766  *
21767  * Returns EINPROGRESS when mp has been consumed by queueing it on
21768  * ill_pending_mp and the ioctl will complete in ip_rput.
21769  *
21770  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
21771  * the new name and new ppa in lifr_name and lifr_ppa respectively.
21772  * For SLIFNAME, we pass these values back to the userland.
21773  */
21774 static int
21775 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
21776 {
21777 	ill_t	*ill;
21778 	ipif_t	*ipif;
21779 	ipsq_t	*ipsq;
21780 	char	*ppa_ptr;
21781 	char	*old_ptr;
21782 	char	old_char;
21783 	int	error;
21784 
21785 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
21786 	ASSERT(q->q_next != NULL);
21787 	ASSERT(interf_name != NULL);
21788 
21789 	ill = (ill_t *)q->q_ptr;
21790 
21791 	ASSERT(ill->ill_name[0] == '\0');
21792 	ASSERT(IAM_WRITER_ILL(ill));
21793 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
21794 	ASSERT(ill->ill_ppa == UINT_MAX);
21795 
21796 	/* The ppa is sent down by ifconfig or is chosen */
21797 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
21798 		return (EINVAL);
21799 	}
21800 
21801 	/*
21802 	 * make sure ppa passed in is same as ppa in the name.
21803 	 * This check is not made when ppa == UINT_MAX in that case ppa
21804 	 * in the name could be anything. System will choose a ppa and
21805 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
21806 	 */
21807 	if (*new_ppa_ptr != UINT_MAX) {
21808 		/* stoi changes the pointer */
21809 		old_ptr = ppa_ptr;
21810 		/*
21811 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
21812 		 * (they don't have an externally visible ppa).  We assign one
21813 		 * here so that we can manage the interface.  Note that in
21814 		 * the past this value was always 0 for DLPI 1 drivers.
21815 		 */
21816 		if (*new_ppa_ptr == 0)
21817 			*new_ppa_ptr = stoi(&old_ptr);
21818 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
21819 			return (EINVAL);
21820 	}
21821 	/*
21822 	 * terminate string before ppa
21823 	 * save char at that location.
21824 	 */
21825 	old_char = ppa_ptr[0];
21826 	ppa_ptr[0] = '\0';
21827 
21828 	ill->ill_ppa = *new_ppa_ptr;
21829 	/*
21830 	 * Finish as much work now as possible before calling ill_glist_insert
21831 	 * which makes the ill globally visible and also merges it with the
21832 	 * other protocol instance of this phyint. The remaining work is
21833 	 * done after entering the ipsq which may happen sometime later.
21834 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
21835 	 */
21836 	ipif = ill->ill_ipif;
21837 
21838 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
21839 	ipif_assign_seqid(ipif);
21840 
21841 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
21842 		ill->ill_flags |= ILLF_IPV4;
21843 
21844 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
21845 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
21846 
21847 	if (ill->ill_flags & ILLF_IPV6) {
21848 
21849 		ill->ill_isv6 = B_TRUE;
21850 		if (ill->ill_rq != NULL) {
21851 			ill->ill_rq->q_qinfo = &rinit_ipv6;
21852 			ill->ill_wq->q_qinfo = &winit_ipv6;
21853 		}
21854 
21855 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
21856 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
21857 		ipif->ipif_v6src_addr = ipv6_all_zeros;
21858 		ipif->ipif_v6subnet = ipv6_all_zeros;
21859 		ipif->ipif_v6net_mask = ipv6_all_zeros;
21860 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
21861 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
21862 		/*
21863 		 * point-to-point or Non-mulicast capable
21864 		 * interfaces won't do NUD unless explicitly
21865 		 * configured to do so.
21866 		 */
21867 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
21868 		    !(ill->ill_flags & ILLF_MULTICAST)) {
21869 			ill->ill_flags |= ILLF_NONUD;
21870 		}
21871 		/* Make sure IPv4 specific flag is not set on IPv6 if */
21872 		if (ill->ill_flags & ILLF_NOARP) {
21873 			/*
21874 			 * Note: xresolv interfaces will eventually need
21875 			 * NOARP set here as well, but that will require
21876 			 * those external resolvers to have some
21877 			 * knowledge of that flag and act appropriately.
21878 			 * Not to be changed at present.
21879 			 */
21880 			ill->ill_flags &= ~ILLF_NOARP;
21881 		}
21882 		/*
21883 		 * Set the ILLF_ROUTER flag according to the global
21884 		 * IPv6 forwarding policy.
21885 		 */
21886 		if (ipv6_forward != 0)
21887 			ill->ill_flags |= ILLF_ROUTER;
21888 	} else if (ill->ill_flags & ILLF_IPV4) {
21889 		ill->ill_isv6 = B_FALSE;
21890 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
21891 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
21892 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
21893 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
21894 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
21895 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
21896 		/*
21897 		 * Set the ILLF_ROUTER flag according to the global
21898 		 * IPv4 forwarding policy.
21899 		 */
21900 		if (ip_g_forward != 0)
21901 			ill->ill_flags |= ILLF_ROUTER;
21902 	}
21903 
21904 	ASSERT(ill->ill_phyint != NULL);
21905 
21906 	/*
21907 	 * The ipv6Ifindex and ipv6IfIcmpIfIndex assignments will
21908 	 * be completed in ill_glist_insert -> ill_phyint_reinit
21909 	 */
21910 	if (ill->ill_isv6) {
21911 		/* allocate v6 mib */
21912 		if (!ill_allocate_mibs(ill))
21913 			return (ENOMEM);
21914 	}
21915 
21916 	/*
21917 	 * Pick a default sap until we get the DL_INFO_ACK back from
21918 	 * the driver.
21919 	 */
21920 	if (ill->ill_sap == 0) {
21921 		if (ill->ill_isv6)
21922 			ill->ill_sap  = IP6_DL_SAP;
21923 		else
21924 			ill->ill_sap  = IP_DL_SAP;
21925 	}
21926 
21927 	ill->ill_ifname_pending = 1;
21928 	ill->ill_ifname_pending_err = 0;
21929 
21930 	ill_refhold(ill);
21931 	rw_enter(&ill_g_lock, RW_WRITER);
21932 	if ((error = ill_glist_insert(ill, interf_name,
21933 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
21934 		ill->ill_ppa = UINT_MAX;
21935 		ill->ill_name[0] = '\0';
21936 		/*
21937 		 * undo null termination done above.
21938 		 */
21939 		ppa_ptr[0] = old_char;
21940 		rw_exit(&ill_g_lock);
21941 		ill_refrele(ill);
21942 		return (error);
21943 	}
21944 
21945 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
21946 
21947 	/*
21948 	 * When we return the buffer pointed to by interf_name should contain
21949 	 * the same name as in ill_name.
21950 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
21951 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
21952 	 * so copy full name and update the ppa ptr.
21953 	 * When ppa passed in != UINT_MAX all values are correct just undo
21954 	 * null termination, this saves a bcopy.
21955 	 */
21956 	if (*new_ppa_ptr == UINT_MAX) {
21957 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
21958 		*new_ppa_ptr = ill->ill_ppa;
21959 	} else {
21960 		/*
21961 		 * undo null termination done above.
21962 		 */
21963 		ppa_ptr[0] = old_char;
21964 	}
21965 
21966 	/* Let SCTP know about this ILL */
21967 	sctp_update_ill(ill, SCTP_ILL_INSERT);
21968 
21969 	/* and also about the first ipif */
21970 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
21971 
21972 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
21973 	    B_TRUE);
21974 
21975 	rw_exit(&ill_g_lock);
21976 	ill_refrele(ill);
21977 	if (ipsq == NULL)
21978 		return (EINPROGRESS);
21979 
21980 	/*
21981 	 * Need to set the ipsq_current_ipif now, if we have changed ipsq
21982 	 * due to the phyint merge in ill_phyint_reinit.
21983 	 */
21984 	ASSERT(ipsq->ipsq_current_ipif == NULL ||
21985 		ipsq->ipsq_current_ipif == ipif);
21986 	ipsq->ipsq_current_ipif = ipif;
21987 	ipsq->ipsq_last_cmd = SIOCSLIFNAME;
21988 	error = ipif_set_values_tail(ill, ipif, mp, q);
21989 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
21990 	if (error != 0 && error != EINPROGRESS) {
21991 		/*
21992 		 * restore previous values
21993 		 */
21994 		ill->ill_isv6 = B_FALSE;
21995 	}
21996 	return (error);
21997 }
21998 
21999 
22000 extern void (*ip_cleanup_func)(void);
22001 
22002 void
22003 ipif_init(void)
22004 {
22005 	hrtime_t hrt;
22006 	int i;
22007 
22008 	/*
22009 	 * Can't call drv_getparm here as it is too early in the boot.
22010 	 * As we use ipif_src_random just for picking a different
22011 	 * source address everytime, this need not be really random.
22012 	 */
22013 	hrt = gethrtime();
22014 	ipif_src_random = ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
22015 
22016 	for (i = 0; i < MAX_G_HEADS; i++) {
22017 		ill_g_heads[i].ill_g_list_head = (ill_if_t *)&ill_g_heads[i];
22018 		ill_g_heads[i].ill_g_list_tail = (ill_if_t *)&ill_g_heads[i];
22019 	}
22020 
22021 	avl_create(&phyint_g_list.phyint_list_avl_by_index,
22022 	    ill_phyint_compare_index,
22023 	    sizeof (phyint_t),
22024 	    offsetof(struct phyint, phyint_avl_by_index));
22025 	avl_create(&phyint_g_list.phyint_list_avl_by_name,
22026 	    ill_phyint_compare_name,
22027 	    sizeof (phyint_t),
22028 	    offsetof(struct phyint, phyint_avl_by_name));
22029 
22030 	ip_cleanup_func = ip_thread_exit;
22031 }
22032 
22033 /*
22034  * This is called by ip_rt_add when src_addr value is other than zero.
22035  * src_addr signifies the source address of the incoming packet. For
22036  * reverse tunnel route we need to create a source addr based routing
22037  * table. This routine creates ip_mrtun_table if it's empty and then
22038  * it adds the route entry hashed by source address. It verifies that
22039  * the outgoing interface is always a non-resolver interface (tunnel).
22040  */
22041 int
22042 ip_mrtun_rt_add(ipaddr_t in_src_addr, int flags, ipif_t *ipif_arg,
22043     ipif_t *src_ipif, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func)
22044 {
22045 	ire_t   *ire;
22046 	ire_t	*save_ire;
22047 	ipif_t  *ipif;
22048 	ill_t   *in_ill = NULL;
22049 	ill_t	*out_ill;
22050 	queue_t	*stq;
22051 	mblk_t	*dlureq_mp;
22052 	int	error;
22053 
22054 	if (ire_arg != NULL)
22055 		*ire_arg = NULL;
22056 	ASSERT(in_src_addr != INADDR_ANY);
22057 
22058 	ipif = ipif_arg;
22059 	if (ipif != NULL) {
22060 		out_ill = ipif->ipif_ill;
22061 	} else {
22062 		ip1dbg(("ip_mrtun_rt_add: ipif is NULL\n"));
22063 		return (EINVAL);
22064 	}
22065 
22066 	if (src_ipif == NULL) {
22067 		ip1dbg(("ip_mrtun_rt_add: src_ipif is NULL\n"));
22068 		return (EINVAL);
22069 	}
22070 	in_ill = src_ipif->ipif_ill;
22071 
22072 	/*
22073 	 * Check for duplicates. We don't need to
22074 	 * match out_ill, because the uniqueness of
22075 	 * a route is only dependent on src_addr and
22076 	 * in_ill.
22077 	 */
22078 	ire = ire_mrtun_lookup(in_src_addr, in_ill);
22079 	if (ire != NULL) {
22080 		ire_refrele(ire);
22081 		return (EEXIST);
22082 	}
22083 	if (ipif->ipif_net_type != IRE_IF_NORESOLVER) {
22084 		ip2dbg(("ip_mrtun_rt_add: outgoing interface is type %d\n",
22085 		    ipif->ipif_net_type));
22086 		return (EINVAL);
22087 	}
22088 
22089 	stq = ipif->ipif_wq;
22090 	ASSERT(stq != NULL);
22091 
22092 	/*
22093 	 * The outgoing interface must be non-resolver
22094 	 * interface.
22095 	 */
22096 	dlureq_mp = ill_dlur_gen(NULL,
22097 	    out_ill->ill_phys_addr_length, out_ill->ill_sap,
22098 	    out_ill->ill_sap_length);
22099 
22100 	if (dlureq_mp == NULL) {
22101 		ip1dbg(("ip_newroute: dlureq_mp NULL\n"));
22102 		return (ENOMEM);
22103 	}
22104 
22105 	/* Create the IRE. */
22106 
22107 	ire = ire_create(
22108 	    NULL,				/* Zero dst addr */
22109 	    NULL,				/* Zero mask */
22110 	    NULL,				/* Zero gateway addr */
22111 	    NULL,				/* Zero ipif_src addr */
22112 	    (uint8_t *)&in_src_addr,		/* in_src-addr */
22113 	    &ipif->ipif_mtu,
22114 	    NULL,
22115 	    NULL,				/* rfq */
22116 	    stq,
22117 	    IRE_MIPRTUN,
22118 	    dlureq_mp,
22119 	    ipif,
22120 	    in_ill,
22121 	    0,
22122 	    0,
22123 	    0,
22124 	    flags,
22125 	    &ire_uinfo_null,
22126 	    NULL,
22127 	    NULL);
22128 
22129 	if (ire == NULL)
22130 		return (ENOMEM);
22131 	ip2dbg(("ip_mrtun_rt_add: mrtun route is created with type %d\n",
22132 	    ire->ire_type));
22133 	save_ire = ire;
22134 	ASSERT(save_ire != NULL);
22135 	error = ire_add_mrtun(&ire, q, mp, func);
22136 	/*
22137 	 * If ire_add_mrtun() failed, the ire passed in was freed
22138 	 * so there is no need to do so here.
22139 	 */
22140 	if (error != 0) {
22141 		return (error);
22142 	}
22143 
22144 	/* Duplicate check */
22145 	if (ire != save_ire) {
22146 		/* route already exists by now */
22147 		ire_refrele(ire);
22148 		return (EEXIST);
22149 	}
22150 
22151 	if (ire_arg != NULL) {
22152 		/*
22153 		 * Store the ire that was just added. the caller
22154 		 * ip_rts_request responsible for doing ire_refrele()
22155 		 * on it.
22156 		 */
22157 		*ire_arg = ire;
22158 	} else {
22159 		ire_refrele(ire);	/* held in ire_add_mrtun */
22160 	}
22161 
22162 	return (0);
22163 }
22164 
22165 /*
22166  * It is called by ip_rt_delete() only when mipagent requests to delete
22167  * a reverse tunnel route that was added by ip_mrtun_rt_add() before.
22168  */
22169 
22170 int
22171 ip_mrtun_rt_delete(ipaddr_t in_src_addr, ipif_t *src_ipif)
22172 {
22173 	ire_t   *ire = NULL;
22174 
22175 	if (in_src_addr == INADDR_ANY)
22176 		return (EINVAL);
22177 	if (src_ipif == NULL)
22178 		return (EINVAL);
22179 
22180 	/* search if this route exists in the ip_mrtun_table */
22181 	ire = ire_mrtun_lookup(in_src_addr, src_ipif->ipif_ill);
22182 	if (ire == NULL) {
22183 		ip2dbg(("ip_mrtun_rt_delete: ire not found\n"));
22184 		return (ESRCH);
22185 	}
22186 	ire_delete(ire);
22187 	ire_refrele(ire);
22188 	return (0);
22189 }
22190 
22191 /*
22192  * Lookup the ipif corresponding to the onlink destination address. For
22193  * point-to-point interfaces, it matches with remote endpoint destination
22194  * address. For point-to-multipoint interfaces it only tries to match the
22195  * destination with the interface's subnet address. The longest, most specific
22196  * match is found to take care of such rare network configurations like -
22197  * le0: 129.146.1.1/16
22198  * le1: 129.146.2.2/24
22199  * It is used only by SO_DONTROUTE at the moment.
22200  */
22201 ipif_t *
22202 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid)
22203 {
22204 	ipif_t	*ipif, *best_ipif;
22205 	ill_t	*ill;
22206 	ill_walk_context_t ctx;
22207 
22208 	ASSERT(zoneid != ALL_ZONES);
22209 	best_ipif = NULL;
22210 
22211 	rw_enter(&ill_g_lock, RW_READER);
22212 	ill = ILL_START_WALK_V4(&ctx);
22213 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
22214 		mutex_enter(&ill->ill_lock);
22215 		for (ipif = ill->ill_ipif; ipif != NULL;
22216 		    ipif = ipif->ipif_next) {
22217 			if (!IPIF_CAN_LOOKUP(ipif))
22218 				continue;
22219 			if (ipif->ipif_zoneid != zoneid &&
22220 			    ipif->ipif_zoneid != ALL_ZONES)
22221 				continue;
22222 			/*
22223 			 * Point-to-point case. Look for exact match with
22224 			 * destination address.
22225 			 */
22226 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
22227 				if (ipif->ipif_pp_dst_addr == addr) {
22228 					ipif_refhold_locked(ipif);
22229 					mutex_exit(&ill->ill_lock);
22230 					rw_exit(&ill_g_lock);
22231 					if (best_ipif != NULL)
22232 						ipif_refrele(best_ipif);
22233 					return (ipif);
22234 				}
22235 			} else if (ipif->ipif_subnet == (addr &
22236 			    ipif->ipif_net_mask)) {
22237 				/*
22238 				 * Point-to-multipoint case. Looping through to
22239 				 * find the most specific match. If there are
22240 				 * multiple best match ipif's then prefer ipif's
22241 				 * that are UP. If there is only one best match
22242 				 * ipif and it is DOWN we must still return it.
22243 				 */
22244 				if ((best_ipif == NULL) ||
22245 				    (ipif->ipif_net_mask >
22246 				    best_ipif->ipif_net_mask) ||
22247 				    ((ipif->ipif_net_mask ==
22248 				    best_ipif->ipif_net_mask) &&
22249 				    ((ipif->ipif_flags & IPIF_UP) &&
22250 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
22251 					ipif_refhold_locked(ipif);
22252 					mutex_exit(&ill->ill_lock);
22253 					rw_exit(&ill_g_lock);
22254 					if (best_ipif != NULL)
22255 						ipif_refrele(best_ipif);
22256 					best_ipif = ipif;
22257 					rw_enter(&ill_g_lock, RW_READER);
22258 					mutex_enter(&ill->ill_lock);
22259 				}
22260 			}
22261 		}
22262 		mutex_exit(&ill->ill_lock);
22263 	}
22264 	rw_exit(&ill_g_lock);
22265 	return (best_ipif);
22266 }
22267 
22268 
22269 /*
22270  * Save enough information so that we can recreate the IRE if
22271  * the interface goes down and then up.
22272  */
22273 static void
22274 ipif_save_ire(ipif_t *ipif, ire_t *ire)
22275 {
22276 	mblk_t	*save_mp;
22277 
22278 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
22279 	if (save_mp != NULL) {
22280 		ifrt_t	*ifrt;
22281 
22282 		save_mp->b_wptr += sizeof (ifrt_t);
22283 		ifrt = (ifrt_t *)save_mp->b_rptr;
22284 		bzero(ifrt, sizeof (ifrt_t));
22285 		ifrt->ifrt_type = ire->ire_type;
22286 		ifrt->ifrt_addr = ire->ire_addr;
22287 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
22288 		ifrt->ifrt_src_addr = ire->ire_src_addr;
22289 		ifrt->ifrt_mask = ire->ire_mask;
22290 		ifrt->ifrt_flags = ire->ire_flags;
22291 		ifrt->ifrt_max_frag = ire->ire_max_frag;
22292 		mutex_enter(&ipif->ipif_saved_ire_lock);
22293 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
22294 		ipif->ipif_saved_ire_mp = save_mp;
22295 		ipif->ipif_saved_ire_cnt++;
22296 		mutex_exit(&ipif->ipif_saved_ire_lock);
22297 	}
22298 }
22299 
22300 
22301 static void
22302 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
22303 {
22304 	mblk_t	**mpp;
22305 	mblk_t	*mp;
22306 	ifrt_t	*ifrt;
22307 
22308 	/* Remove from ipif_saved_ire_mp list if it is there */
22309 	mutex_enter(&ipif->ipif_saved_ire_lock);
22310 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
22311 	    mpp = &(*mpp)->b_cont) {
22312 		/*
22313 		 * On a given ipif, the triple of address, gateway and
22314 		 * mask is unique for each saved IRE (in the case of
22315 		 * ordinary interface routes, the gateway address is
22316 		 * all-zeroes).
22317 		 */
22318 		mp = *mpp;
22319 		ifrt = (ifrt_t *)mp->b_rptr;
22320 		if (ifrt->ifrt_addr == ire->ire_addr &&
22321 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
22322 		    ifrt->ifrt_mask == ire->ire_mask) {
22323 			*mpp = mp->b_cont;
22324 			ipif->ipif_saved_ire_cnt--;
22325 			freeb(mp);
22326 			break;
22327 		}
22328 	}
22329 	mutex_exit(&ipif->ipif_saved_ire_lock);
22330 }
22331 
22332 
22333 /*
22334  * IP multirouting broadcast routes handling
22335  * Append CGTP broadcast IREs to regular ones created
22336  * at ifconfig time.
22337  */
22338 static void
22339 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst)
22340 {
22341 	ire_t *ire_prim;
22342 
22343 	ASSERT(ire != NULL);
22344 	ASSERT(ire_dst != NULL);
22345 
22346 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
22347 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
22348 	if (ire_prim != NULL) {
22349 		/*
22350 		 * We are in the special case of broadcasts for
22351 		 * CGTP. We add an IRE_BROADCAST that holds
22352 		 * the RTF_MULTIRT flag, the destination
22353 		 * address of ire_dst and the low level
22354 		 * info of ire_prim. In other words, CGTP
22355 		 * broadcast is added to the redundant ipif.
22356 		 */
22357 		ipif_t *ipif_prim;
22358 		ire_t  *bcast_ire;
22359 
22360 		ipif_prim = ire_prim->ire_ipif;
22361 
22362 		ip2dbg(("ip_cgtp_filter_bcast_add: "
22363 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
22364 		    (void *)ire_dst, (void *)ire_prim,
22365 		    (void *)ipif_prim));
22366 
22367 		bcast_ire = ire_create(
22368 		    (uchar_t *)&ire->ire_addr,
22369 		    (uchar_t *)&ip_g_all_ones,
22370 		    (uchar_t *)&ire_dst->ire_src_addr,
22371 		    (uchar_t *)&ire->ire_gateway_addr,
22372 		    NULL,
22373 		    &ipif_prim->ipif_mtu,
22374 		    NULL,
22375 		    ipif_prim->ipif_rq,
22376 		    ipif_prim->ipif_wq,
22377 		    IRE_BROADCAST,
22378 		    ipif_prim->ipif_bcast_mp,
22379 		    ipif_prim,
22380 		    NULL,
22381 		    0,
22382 		    0,
22383 		    0,
22384 		    ire->ire_flags,
22385 		    &ire_uinfo_null,
22386 		    NULL,
22387 		    NULL);
22388 
22389 		if (bcast_ire != NULL) {
22390 
22391 			if (ire_add(&bcast_ire, NULL, NULL, NULL) == 0) {
22392 				ip2dbg(("ip_cgtp_filter_bcast_add: "
22393 				    "added bcast_ire %p\n",
22394 				    (void *)bcast_ire));
22395 
22396 				ipif_save_ire(bcast_ire->ire_ipif,
22397 				    bcast_ire);
22398 				ire_refrele(bcast_ire);
22399 			}
22400 		}
22401 		ire_refrele(ire_prim);
22402 	}
22403 }
22404 
22405 
22406 /*
22407  * IP multirouting broadcast routes handling
22408  * Remove the broadcast ire
22409  */
22410 static void
22411 ip_cgtp_bcast_delete(ire_t *ire)
22412 {
22413 	ire_t *ire_dst;
22414 
22415 	ASSERT(ire != NULL);
22416 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
22417 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
22418 	if (ire_dst != NULL) {
22419 		ire_t *ire_prim;
22420 
22421 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
22422 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
22423 		if (ire_prim != NULL) {
22424 			ipif_t *ipif_prim;
22425 			ire_t  *bcast_ire;
22426 
22427 			ipif_prim = ire_prim->ire_ipif;
22428 
22429 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
22430 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
22431 			    (void *)ire_dst, (void *)ire_prim,
22432 			    (void *)ipif_prim));
22433 
22434 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
22435 			    ire->ire_gateway_addr,
22436 			    IRE_BROADCAST,
22437 			    ipif_prim, ALL_ZONES,
22438 			    NULL,
22439 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
22440 			    MATCH_IRE_MASK);
22441 
22442 			if (bcast_ire != NULL) {
22443 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
22444 				    "looked up bcast_ire %p\n",
22445 				    (void *)bcast_ire));
22446 				ipif_remove_ire(bcast_ire->ire_ipif,
22447 					bcast_ire);
22448 				ire_delete(bcast_ire);
22449 			}
22450 			ire_refrele(ire_prim);
22451 		}
22452 		ire_refrele(ire_dst);
22453 	}
22454 }
22455 
22456 /*
22457  * IPsec hardware acceleration capabilities related functions.
22458  */
22459 
22460 /*
22461  * Free a per-ill IPsec capabilities structure.
22462  */
22463 static void
22464 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
22465 {
22466 	if (capab->auth_hw_algs != NULL)
22467 		kmem_free(capab->auth_hw_algs, capab->algs_size);
22468 	if (capab->encr_hw_algs != NULL)
22469 		kmem_free(capab->encr_hw_algs, capab->algs_size);
22470 	if (capab->encr_algparm != NULL)
22471 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
22472 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
22473 }
22474 
22475 /*
22476  * Allocate a new per-ill IPsec capabilities structure. This structure
22477  * is specific to an IPsec protocol (AH or ESP). It is implemented as
22478  * an array which specifies, for each algorithm, whether this algorithm
22479  * is supported by the ill or not.
22480  */
22481 static ill_ipsec_capab_t *
22482 ill_ipsec_capab_alloc(void)
22483 {
22484 	ill_ipsec_capab_t *capab;
22485 	uint_t nelems;
22486 
22487 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
22488 	if (capab == NULL)
22489 		return (NULL);
22490 
22491 	/* we need one bit per algorithm */
22492 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
22493 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
22494 
22495 	/* allocate memory to store algorithm flags */
22496 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
22497 	if (capab->encr_hw_algs == NULL)
22498 		goto nomem;
22499 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
22500 	if (capab->auth_hw_algs == NULL)
22501 		goto nomem;
22502 	/*
22503 	 * Leave encr_algparm NULL for now since we won't need it half
22504 	 * the time
22505 	 */
22506 	return (capab);
22507 
22508 nomem:
22509 	ill_ipsec_capab_free(capab);
22510 	return (NULL);
22511 }
22512 
22513 /*
22514  * Resize capability array.  Since we're exclusive, this is OK.
22515  */
22516 static boolean_t
22517 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
22518 {
22519 	ipsec_capab_algparm_t *nalp, *oalp;
22520 	uint32_t olen, nlen;
22521 
22522 	oalp = capab->encr_algparm;
22523 	olen = capab->encr_algparm_size;
22524 
22525 	if (oalp != NULL) {
22526 		if (algid < capab->encr_algparm_end)
22527 			return (B_TRUE);
22528 	}
22529 
22530 	nlen = (algid + 1) * sizeof (*nalp);
22531 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
22532 	if (nalp == NULL)
22533 		return (B_FALSE);
22534 
22535 	if (oalp != NULL) {
22536 		bcopy(oalp, nalp, olen);
22537 		kmem_free(oalp, olen);
22538 	}
22539 	capab->encr_algparm = nalp;
22540 	capab->encr_algparm_size = nlen;
22541 	capab->encr_algparm_end = algid + 1;
22542 
22543 	return (B_TRUE);
22544 }
22545 
22546 /*
22547  * Compare the capabilities of the specified ill with the protocol
22548  * and algorithms specified by the SA passed as argument.
22549  * If they match, returns B_TRUE, B_FALSE if they do not match.
22550  *
22551  * The ill can be passed as a pointer to it, or by specifying its index
22552  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
22553  *
22554  * Called by ipsec_out_is_accelerated() do decide whether an outbound
22555  * packet is eligible for hardware acceleration, and by
22556  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
22557  * to a particular ill.
22558  */
22559 boolean_t
22560 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
22561     ipsa_t *sa)
22562 {
22563 	boolean_t sa_isv6;
22564 	uint_t algid;
22565 	struct ill_ipsec_capab_s *cpp;
22566 	boolean_t need_refrele = B_FALSE;
22567 
22568 	if (ill == NULL) {
22569 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
22570 		    NULL, NULL, NULL);
22571 		if (ill == NULL) {
22572 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
22573 			return (B_FALSE);
22574 		}
22575 		need_refrele = B_TRUE;
22576 	}
22577 
22578 	/*
22579 	 * Use the address length specified by the SA to determine
22580 	 * if it corresponds to a IPv6 address, and fail the matching
22581 	 * if the isv6 flag passed as argument does not match.
22582 	 * Note: this check is used for SADB capability checking before
22583 	 * sending SA information to an ill.
22584 	 */
22585 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
22586 	if (sa_isv6 != ill_isv6)
22587 		/* protocol mismatch */
22588 		goto done;
22589 
22590 	/*
22591 	 * Check if the ill supports the protocol, algorithm(s) and
22592 	 * key size(s) specified by the SA, and get the pointers to
22593 	 * the algorithms supported by the ill.
22594 	 */
22595 	switch (sa->ipsa_type) {
22596 
22597 	case SADB_SATYPE_ESP:
22598 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
22599 			/* ill does not support ESP acceleration */
22600 			goto done;
22601 		cpp = ill->ill_ipsec_capab_esp;
22602 		algid = sa->ipsa_auth_alg;
22603 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
22604 			goto done;
22605 		algid = sa->ipsa_encr_alg;
22606 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
22607 			goto done;
22608 		if (algid < cpp->encr_algparm_end) {
22609 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
22610 			if (sa->ipsa_encrkeybits < alp->minkeylen)
22611 				goto done;
22612 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
22613 				goto done;
22614 		}
22615 		break;
22616 
22617 	case SADB_SATYPE_AH:
22618 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
22619 			/* ill does not support AH acceleration */
22620 			goto done;
22621 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
22622 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
22623 			goto done;
22624 		break;
22625 	}
22626 
22627 	if (need_refrele)
22628 		ill_refrele(ill);
22629 	return (B_TRUE);
22630 done:
22631 	if (need_refrele)
22632 		ill_refrele(ill);
22633 	return (B_FALSE);
22634 }
22635 
22636 
22637 /*
22638  * Add a new ill to the list of IPsec capable ills.
22639  * Called from ill_capability_ipsec_ack() when an ACK was received
22640  * indicating that IPsec hardware processing was enabled for an ill.
22641  *
22642  * ill must point to the ill for which acceleration was enabled.
22643  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
22644  */
22645 static void
22646 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
22647 {
22648 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
22649 	uint_t sa_type;
22650 	uint_t ipproto;
22651 
22652 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
22653 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
22654 
22655 	switch (dl_cap) {
22656 	case DL_CAPAB_IPSEC_AH:
22657 		sa_type = SADB_SATYPE_AH;
22658 		ills = &ipsec_capab_ills_ah;
22659 		ipproto = IPPROTO_AH;
22660 		break;
22661 	case DL_CAPAB_IPSEC_ESP:
22662 		sa_type = SADB_SATYPE_ESP;
22663 		ills = &ipsec_capab_ills_esp;
22664 		ipproto = IPPROTO_ESP;
22665 		break;
22666 	}
22667 
22668 	rw_enter(&ipsec_capab_ills_lock, RW_WRITER);
22669 
22670 	/*
22671 	 * Add ill index to list of hardware accelerators. If
22672 	 * already in list, do nothing.
22673 	 */
22674 	for (cur_ill = *ills; cur_ill != NULL &&
22675 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
22676 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
22677 		;
22678 
22679 	if (cur_ill == NULL) {
22680 		/* if this is a new entry for this ill */
22681 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
22682 		if (new_ill == NULL) {
22683 			rw_exit(&ipsec_capab_ills_lock);
22684 			return;
22685 		}
22686 
22687 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
22688 		new_ill->ill_isv6 = ill->ill_isv6;
22689 		new_ill->next = *ills;
22690 		*ills = new_ill;
22691 	} else if (!sadb_resync) {
22692 		/* not resync'ing SADB and an entry exists for this ill */
22693 		rw_exit(&ipsec_capab_ills_lock);
22694 		return;
22695 	}
22696 
22697 	rw_exit(&ipsec_capab_ills_lock);
22698 
22699 	if (ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
22700 		/*
22701 		 * IPsec module for protocol loaded, initiate dump
22702 		 * of the SADB to this ill.
22703 		 */
22704 		sadb_ill_download(ill, sa_type);
22705 }
22706 
22707 /*
22708  * Remove an ill from the list of IPsec capable ills.
22709  */
22710 static void
22711 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
22712 {
22713 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
22714 
22715 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
22716 	    dl_cap == DL_CAPAB_IPSEC_ESP);
22717 
22718 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipsec_capab_ills_ah :
22719 	    &ipsec_capab_ills_esp;
22720 
22721 	rw_enter(&ipsec_capab_ills_lock, RW_WRITER);
22722 
22723 	prev_ill = NULL;
22724 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
22725 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
22726 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
22727 		;
22728 	if (cur_ill == NULL) {
22729 		/* entry not found */
22730 		rw_exit(&ipsec_capab_ills_lock);
22731 		return;
22732 	}
22733 	if (prev_ill == NULL) {
22734 		/* entry at front of list */
22735 		*ills = NULL;
22736 	} else {
22737 		prev_ill->next = cur_ill->next;
22738 	}
22739 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
22740 	rw_exit(&ipsec_capab_ills_lock);
22741 }
22742 
22743 
22744 /*
22745  * Handling of DL_CONTROL_REQ messages that must be sent down to
22746  * an ill while having exclusive access.
22747  */
22748 /* ARGSUSED */
22749 static void
22750 ill_ipsec_capab_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
22751 {
22752 	ill_t *ill = (ill_t *)q->q_ptr;
22753 
22754 	ill_dlpi_send(ill, mp);
22755 }
22756 
22757 
22758 /*
22759  * Called by SADB to send a DL_CONTROL_REQ message to every ill
22760  * supporting the specified IPsec protocol acceleration.
22761  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
22762  * We free the mblk and, if sa is non-null, release the held referece.
22763  */
22764 void
22765 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa)
22766 {
22767 	ipsec_capab_ill_t *ici, *cur_ici;
22768 	ill_t *ill;
22769 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
22770 
22771 	ici = (sa_type == SADB_SATYPE_AH) ? ipsec_capab_ills_ah :
22772 	    ipsec_capab_ills_esp;
22773 
22774 	rw_enter(&ipsec_capab_ills_lock, RW_READER);
22775 
22776 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
22777 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
22778 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL);
22779 
22780 		/*
22781 		 * Handle the case where the ill goes away while the SADB is
22782 		 * attempting to send messages.  If it's going away, it's
22783 		 * nuking its shadow SADB, so we don't care..
22784 		 */
22785 
22786 		if (ill == NULL)
22787 			continue;
22788 
22789 		if (sa != NULL) {
22790 			/*
22791 			 * Make sure capabilities match before
22792 			 * sending SA to ill.
22793 			 */
22794 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
22795 			    cur_ici->ill_isv6, sa)) {
22796 				ill_refrele(ill);
22797 				continue;
22798 			}
22799 
22800 			mutex_enter(&sa->ipsa_lock);
22801 			sa->ipsa_flags |= IPSA_F_HW;
22802 			mutex_exit(&sa->ipsa_lock);
22803 		}
22804 
22805 		/*
22806 		 * Copy template message, and add it to the front
22807 		 * of the mblk ship list. We want to avoid holding
22808 		 * the ipsec_capab_ills_lock while sending the
22809 		 * message to the ills.
22810 		 *
22811 		 * The b_next and b_prev are temporarily used
22812 		 * to build a list of mblks to be sent down, and to
22813 		 * save the ill to which they must be sent.
22814 		 */
22815 		nmp = copymsg(mp);
22816 		if (nmp == NULL) {
22817 			ill_refrele(ill);
22818 			continue;
22819 		}
22820 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
22821 		nmp->b_next = mp_ship_list;
22822 		mp_ship_list = nmp;
22823 		nmp->b_prev = (mblk_t *)ill;
22824 	}
22825 
22826 	rw_exit(&ipsec_capab_ills_lock);
22827 
22828 	nmp = mp_ship_list;
22829 	while (nmp != NULL) {
22830 		/* restore the mblk to a sane state */
22831 		next_mp = nmp->b_next;
22832 		nmp->b_next = NULL;
22833 		ill = (ill_t *)nmp->b_prev;
22834 		nmp->b_prev = NULL;
22835 
22836 		/*
22837 		 * Ship the mblk to the ill, must be exclusive. Keep the
22838 		 * reference to the ill as qwriter_ip() does a ill_referele().
22839 		 */
22840 		(void) qwriter_ip(NULL, ill, ill->ill_wq, nmp,
22841 		    ill_ipsec_capab_send_writer, NEW_OP, B_TRUE);
22842 
22843 		nmp = next_mp;
22844 	}
22845 
22846 	if (sa != NULL)
22847 		IPSA_REFRELE(sa);
22848 	freemsg(mp);
22849 }
22850 
22851 
22852 /*
22853  * Derive an interface id from the link layer address.
22854  * Knows about IEEE 802 and IEEE EUI-64 mappings.
22855  */
22856 static boolean_t
22857 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
22858 {
22859 	char		*addr;
22860 
22861 	if (phys_length != ETHERADDRL)
22862 		return (B_FALSE);
22863 
22864 	/* Form EUI-64 like address */
22865 	addr = (char *)&v6addr->s6_addr32[2];
22866 	bcopy((char *)phys_addr, addr, 3);
22867 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
22868 	addr[3] = (char)0xff;
22869 	addr[4] = (char)0xfe;
22870 	bcopy((char *)phys_addr + 3, addr + 5, 3);
22871 	return (B_TRUE);
22872 }
22873 
22874 /* ARGSUSED */
22875 static boolean_t
22876 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
22877 {
22878 	return (B_FALSE);
22879 }
22880 
22881 /* ARGSUSED */
22882 static boolean_t
22883 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
22884     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
22885 {
22886 	/*
22887 	 * Multicast address mappings used over Ethernet/802.X.
22888 	 * This address is used as a base for mappings.
22889 	 */
22890 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
22891 	    0x00, 0x00, 0x00};
22892 
22893 	/*
22894 	 * Extract low order 32 bits from IPv6 multicast address.
22895 	 * Or that into the link layer address, starting from the
22896 	 * second byte.
22897 	 */
22898 	*hw_start = 2;
22899 	v6_extract_mask->s6_addr32[0] = 0;
22900 	v6_extract_mask->s6_addr32[1] = 0;
22901 	v6_extract_mask->s6_addr32[2] = 0;
22902 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
22903 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
22904 	return (B_TRUE);
22905 }
22906 
22907 /*
22908  * Indicate by return value whether multicast is supported. If not,
22909  * this code should not touch/change any parameters.
22910  */
22911 /* ARGSUSED */
22912 static boolean_t
22913 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
22914     uint32_t *hw_start, ipaddr_t *extract_mask)
22915 {
22916 	/*
22917 	 * Multicast address mappings used over Ethernet/802.X.
22918 	 * This address is used as a base for mappings.
22919 	 */
22920 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
22921 	    0x00, 0x00, 0x00 };
22922 
22923 	if (phys_length != ETHERADDRL)
22924 		return (B_FALSE);
22925 
22926 	*extract_mask = htonl(0x007fffff);
22927 	*hw_start = 2;
22928 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
22929 	return (B_TRUE);
22930 }
22931 
22932 /*
22933  * Derive IPoIB interface id from the link layer address.
22934  */
22935 static boolean_t
22936 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
22937 {
22938 	char		*addr;
22939 
22940 	if (phys_length != 20)
22941 		return (B_FALSE);
22942 	addr = (char *)&v6addr->s6_addr32[2];
22943 	bcopy(phys_addr + 12, addr, 8);
22944 	/*
22945 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
22946 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
22947 	 * rules. In these cases, the IBA considers these GUIDs to be in
22948 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
22949 	 * required; vendors are required not to assign global EUI-64's
22950 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
22951 	 * of the interface identifier. Whether the GUID is in modified
22952 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
22953 	 * bit set to 1.
22954 	 */
22955 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
22956 	return (B_TRUE);
22957 }
22958 
22959 /*
22960  * Note on mapping from multicast IP addresses to IPoIB multicast link
22961  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
22962  * The format of an IPoIB multicast address is:
22963  *
22964  *  4 byte QPN      Scope Sign.  Pkey
22965  * +--------------------------------------------+
22966  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
22967  * +--------------------------------------------+
22968  *
22969  * The Scope and Pkey components are properties of the IBA port and
22970  * network interface. They can be ascertained from the broadcast address.
22971  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
22972  */
22973 
22974 static boolean_t
22975 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
22976     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
22977 {
22978 	/*
22979 	 * Base IPoIB IPv6 multicast address used for mappings.
22980 	 * Does not contain the IBA scope/Pkey values.
22981 	 */
22982 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
22983 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
22984 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
22985 
22986 	/*
22987 	 * Extract low order 80 bits from IPv6 multicast address.
22988 	 * Or that into the link layer address, starting from the
22989 	 * sixth byte.
22990 	 */
22991 	*hw_start = 6;
22992 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
22993 
22994 	/*
22995 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
22996 	 */
22997 	*(maddr + 5) = *(bphys_addr + 5);
22998 	*(maddr + 8) = *(bphys_addr + 8);
22999 	*(maddr + 9) = *(bphys_addr + 9);
23000 
23001 	v6_extract_mask->s6_addr32[0] = 0;
23002 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
23003 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
23004 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23005 	return (B_TRUE);
23006 }
23007 
23008 static boolean_t
23009 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23010     uint32_t *hw_start, ipaddr_t *extract_mask)
23011 {
23012 	/*
23013 	 * Base IPoIB IPv4 multicast address used for mappings.
23014 	 * Does not contain the IBA scope/Pkey values.
23015 	 */
23016 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23017 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
23018 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23019 
23020 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
23021 		return (B_FALSE);
23022 
23023 	/*
23024 	 * Extract low order 28 bits from IPv4 multicast address.
23025 	 * Or that into the link layer address, starting from the
23026 	 * sixteenth byte.
23027 	 */
23028 	*extract_mask = htonl(0x0fffffff);
23029 	*hw_start = 16;
23030 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
23031 
23032 	/*
23033 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23034 	 */
23035 	*(maddr + 5) = *(bphys_addr + 5);
23036 	*(maddr + 8) = *(bphys_addr + 8);
23037 	*(maddr + 9) = *(bphys_addr + 9);
23038 	return (B_TRUE);
23039 }
23040 
23041 /*
23042  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
23043  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
23044  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
23045  * the link-local address is preferred.
23046  */
23047 boolean_t
23048 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23049 {
23050 	ipif_t	*ipif;
23051 	ipif_t	*maybe_ipif = NULL;
23052 
23053 	mutex_enter(&ill->ill_lock);
23054 	if (ill->ill_state_flags & ILL_CONDEMNED) {
23055 		mutex_exit(&ill->ill_lock);
23056 		if (ipifp != NULL)
23057 			*ipifp = NULL;
23058 		return (B_FALSE);
23059 	}
23060 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23061 		if (!IPIF_CAN_LOOKUP(ipif))
23062 			continue;
23063 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
23064 		    ipif->ipif_zoneid != ALL_ZONES)
23065 			continue;
23066 		if ((ipif->ipif_flags & flags) != flags)
23067 			continue;
23068 
23069 		if (ipifp == NULL) {
23070 			mutex_exit(&ill->ill_lock);
23071 			ASSERT(maybe_ipif == NULL);
23072 			return (B_TRUE);
23073 		}
23074 		if (!ill->ill_isv6 ||
23075 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
23076 			ipif_refhold_locked(ipif);
23077 			mutex_exit(&ill->ill_lock);
23078 			*ipifp = ipif;
23079 			return (B_TRUE);
23080 		}
23081 		if (maybe_ipif == NULL)
23082 			maybe_ipif = ipif;
23083 	}
23084 	if (ipifp != NULL) {
23085 		if (maybe_ipif != NULL)
23086 			ipif_refhold_locked(maybe_ipif);
23087 		*ipifp = maybe_ipif;
23088 	}
23089 	mutex_exit(&ill->ill_lock);
23090 	return (maybe_ipif != NULL);
23091 }
23092 
23093 /*
23094  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
23095  */
23096 boolean_t
23097 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23098 {
23099 	ill_t *illg;
23100 
23101 	/*
23102 	 * We look at the passed-in ill first without grabbing ill_g_lock.
23103 	 */
23104 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
23105 		return (B_TRUE);
23106 	}
23107 	rw_enter(&ill_g_lock, RW_READER);
23108 	if (ill->ill_group == NULL) {
23109 		/* ill not in a group */
23110 		rw_exit(&ill_g_lock);
23111 		return (B_FALSE);
23112 	}
23113 
23114 	/*
23115 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
23116 	 * group. We need to look for an ipif in the zone on all the ills in the
23117 	 * group.
23118 	 */
23119 	illg = ill->ill_group->illgrp_ill;
23120 	do {
23121 		/*
23122 		 * We don't call ipif_lookup_zoneid() on ill as we already know
23123 		 * that it's not there.
23124 		 */
23125 		if (illg != ill &&
23126 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
23127 			break;
23128 		}
23129 	} while ((illg = illg->ill_group_next) != NULL);
23130 	rw_exit(&ill_g_lock);
23131 	return (illg != NULL);
23132 }
23133 
23134 /*
23135  * Check if this ill is only being used to send ICMP probes for IPMP
23136  */
23137 boolean_t
23138 ill_is_probeonly(ill_t *ill)
23139 {
23140 	/*
23141 	 * Check if the interface is FAILED, or INACTIVE
23142 	 */
23143 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
23144 		return (B_TRUE);
23145 
23146 	return (B_FALSE);
23147 }
23148