xref: /titanic_51/usr/src/uts/common/inet/ip/ip_if.c (revision 505d05c73a6e56769f263d4803b22eddd168ee24)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /* Copyright (c) 1990 Mentat Inc. */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 /*
31  * This file contains the interface control functions for IP.
32  */
33 
34 #include <sys/types.h>
35 #include <sys/stream.h>
36 #include <sys/dlpi.h>
37 #include <sys/stropts.h>
38 #include <sys/strsun.h>
39 #include <sys/sysmacros.h>
40 #include <sys/strlog.h>
41 #include <sys/ddi.h>
42 #include <sys/sunddi.h>
43 #include <sys/cmn_err.h>
44 #include <sys/kstat.h>
45 #include <sys/debug.h>
46 #include <sys/zone.h>
47 
48 #include <sys/kmem.h>
49 #include <sys/systm.h>
50 #include <sys/param.h>
51 #include <sys/socket.h>
52 #define	_SUN_TPI_VERSION	2
53 #include <sys/tihdr.h>
54 #include <sys/isa_defs.h>
55 #include <net/if.h>
56 #include <net/if_arp.h>
57 #include <net/if_types.h>
58 #include <net/if_dl.h>
59 #include <net/route.h>
60 #include <sys/sockio.h>
61 #include <netinet/in.h>
62 #include <netinet/ip6.h>
63 #include <netinet/icmp6.h>
64 #include <netinet/igmp_var.h>
65 #include <sys/strsun.h>
66 #include <sys/policy.h>
67 #include <sys/ethernet.h>
68 
69 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
70 #include <inet/mi.h>
71 #include <inet/nd.h>
72 #include <inet/arp.h>
73 #include <inet/mib2.h>
74 #include <inet/ip.h>
75 #include <inet/ip6.h>
76 #include <inet/ip6_asp.h>
77 #include <inet/tcp.h>
78 #include <inet/ip_multi.h>
79 #include <inet/ip_ire.h>
80 #include <inet/ip_rts.h>
81 #include <inet/ip_ndp.h>
82 #include <inet/ip_if.h>
83 #include <inet/ip_impl.h>
84 #include <inet/tun.h>
85 #include <inet/sctp_ip.h>
86 
87 #include <net/pfkeyv2.h>
88 #include <inet/ipsec_info.h>
89 #include <inet/sadb.h>
90 #include <inet/ipsec_impl.h>
91 #include <sys/iphada.h>
92 
93 
94 #include <netinet/igmp.h>
95 #include <inet/ip_listutils.h>
96 #include <netinet/ip_mroute.h>
97 #include <inet/ipclassifier.h>
98 #include <sys/mac.h>
99 
100 #include <sys/systeminfo.h>
101 #include <sys/bootconf.h>
102 
103 /* The character which tells where the ill_name ends */
104 #define	IPIF_SEPARATOR_CHAR	':'
105 
106 /* IP ioctl function table entry */
107 typedef struct ipft_s {
108 	int	ipft_cmd;
109 	pfi_t	ipft_pfi;
110 	int	ipft_min_size;
111 	int	ipft_flags;
112 } ipft_t;
113 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
114 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
115 
116 typedef struct ip_sock_ar_s {
117 	union {
118 		area_t	ip_sock_area;
119 		ared_t	ip_sock_ared;
120 		areq_t	ip_sock_areq;
121 	} ip_sock_ar_u;
122 	queue_t	*ip_sock_ar_q;
123 } ip_sock_ar_t;
124 
125 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
126 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
127 		    char *value, caddr_t cp, cred_t *ioc_cr);
128 
129 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
130 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
131 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
132     mblk_t *mp, boolean_t need_up);
133 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
134     mblk_t *mp, boolean_t need_up);
135 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
136     queue_t *q, mblk_t *mp, boolean_t need_up);
137 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
138     mblk_t *mp, boolean_t need_up);
139 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
140     mblk_t *mp);
141 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
142     queue_t *q, mblk_t *mp, boolean_t need_up);
143 static int	ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp,
144     sin_t *sin, boolean_t x_arp_ioctl, boolean_t if_arp_ioctl);
145 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **);
146 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
147 static void	ipsq_flush(ill_t *ill);
148 static void	ipsq_clean_all(ill_t *ill);
149 static void	ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring);
150 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
151     queue_t *q, mblk_t *mp, boolean_t need_up);
152 static void	ipsq_delete(ipsq_t *);
153 
154 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
155 		    boolean_t initialize);
156 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
157 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
158 static void	ipif_delete_cache_ire(ire_t *, char *);
159 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
160 static void	ipif_down_tail(ipif_t *ipif);
161 static void	ipif_free(ipif_t *ipif);
162 static void	ipif_free_tail(ipif_t *ipif);
163 static void	ipif_mask_reply(ipif_t *);
164 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
165 static void	ipif_multicast_down(ipif_t *ipif);
166 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
167 static void	ipif_set_default(ipif_t *ipif);
168 static int	ipif_set_values(queue_t *q, mblk_t *mp,
169     char *interf_name, uint_t *ppa);
170 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
171     queue_t *q);
172 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
173     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
174     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error);
175 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
176 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
177 
178 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
179 static int	ill_arp_off(ill_t *ill);
180 static int	ill_arp_on(ill_t *ill);
181 static void	ill_delete_interface_type(ill_if_t *);
182 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
183 static void	ill_down(ill_t *ill);
184 static void	ill_downi(ire_t *ire, char *ill_arg);
185 static void	ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg);
186 static void	ill_down_tail(ill_t *ill);
187 static void	ill_free_mib(ill_t *ill);
188 static void	ill_glist_delete(ill_t *);
189 static boolean_t ill_has_usable_ipif(ill_t *);
190 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
191 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
192 static void	ill_phyint_free(ill_t *ill);
193 static void	ill_phyint_reinit(ill_t *ill);
194 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
195 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
196 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
197 static void	ill_stq_cache_delete(ire_t *, char *);
198 
199 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
200 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
201 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
202     in6_addr_t *);
203 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
204     ipaddr_t *);
205 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
206 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
207     in6_addr_t *);
208 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
209     ipaddr_t *);
210 
211 static void	ipif_save_ire(ipif_t *, ire_t *);
212 static void	ipif_remove_ire(ipif_t *, ire_t *);
213 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *);
214 static void 	ip_cgtp_bcast_delete(ire_t *);
215 
216 /*
217  * Per-ill IPsec capabilities management.
218  */
219 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
220 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
221 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
222 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
223 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
224 static void ill_capability_proto(ill_t *, int, mblk_t *);
225 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
226     boolean_t);
227 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
228 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
229 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
230 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
231 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
232 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
233 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
234 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
235     dl_capability_sub_t *);
236 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
237 
238 static void ill_capability_poll_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
239 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
240 static void ill_capability_poll_reset(ill_t *, mblk_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 static uint_t
564 ipif_rand(void)
565 {
566 	ipif_src_random = ipif_src_random * 1103515245 + 12345;
567 	return ((ipif_src_random >> 16) & 0x7fff);
568 }
569 
570 /*
571  * Allocate per-interface mibs. Only used for ipv6.
572  * Returns true if ok. False otherwise.
573  *  ipsq  may not yet be allocated (loopback case ).
574  */
575 static boolean_t
576 ill_allocate_mibs(ill_t *ill)
577 {
578 	ASSERT(ill->ill_isv6);
579 
580 	/* Already allocated? */
581 	if (ill->ill_ip6_mib != NULL) {
582 		ASSERT(ill->ill_icmp6_mib != NULL);
583 		return (B_TRUE);
584 	}
585 
586 	ill->ill_ip6_mib = kmem_zalloc(sizeof (*ill->ill_ip6_mib),
587 	    KM_NOSLEEP);
588 	if (ill->ill_ip6_mib == NULL) {
589 		return (B_FALSE);
590 	}
591 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
592 	    KM_NOSLEEP);
593 	if (ill->ill_icmp6_mib == NULL) {
594 		kmem_free(ill->ill_ip6_mib, sizeof (*ill->ill_ip6_mib));
595 		ill->ill_ip6_mib = NULL;
596 		return (B_FALSE);
597 	}
598 	/*
599 	 * The ipv6Ifindex and ipv6IfIcmpIndex will be assigned later
600 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
601 	 * -> ill_phyint_reinit
602 	 */
603 	return (B_TRUE);
604 }
605 
606 /*
607  * Common code for preparation of ARP commands.  Two points to remember:
608  * 	1) The ill_name is tacked on at the end of the allocated space so
609  *	   the templates name_offset field must contain the total space
610  *	   to allocate less the name length.
611  *
612  *	2) The templates name_length field should contain the *template*
613  *	   length.  We use it as a parameter to bcopy() and then write
614  *	   the real ill_name_length into the name_length field of the copy.
615  * (Always called as writer.)
616  */
617 mblk_t *
618 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
619 {
620 	arc_t	*arc = (arc_t *)template;
621 	char	*cp;
622 	int	len;
623 	mblk_t	*mp;
624 	uint_t	name_length = ill->ill_name_length;
625 	uint_t	template_len = arc->arc_name_length;
626 
627 	len = arc->arc_name_offset + name_length;
628 	mp = allocb(len, BPRI_HI);
629 	if (mp == NULL)
630 		return (NULL);
631 	cp = (char *)mp->b_rptr;
632 	mp->b_wptr = (uchar_t *)&cp[len];
633 	if (template_len)
634 		bcopy(template, cp, template_len);
635 	if (len > template_len)
636 		bzero(&cp[template_len], len - template_len);
637 	mp->b_datap->db_type = M_PROTO;
638 
639 	arc = (arc_t *)cp;
640 	arc->arc_name_length = name_length;
641 	cp = (char *)arc + arc->arc_name_offset;
642 	bcopy(ill->ill_name, cp, name_length);
643 
644 	if (addr) {
645 		area_t	*area = (area_t *)mp->b_rptr;
646 
647 		cp = (char *)area + area->area_proto_addr_offset;
648 		bcopy(addr, cp, area->area_proto_addr_length);
649 		if (area->area_cmd == AR_ENTRY_ADD) {
650 			cp = (char *)area;
651 			len = area->area_proto_addr_length;
652 			if (area->area_proto_mask_offset)
653 				cp += area->area_proto_mask_offset;
654 			else
655 				cp += area->area_proto_addr_offset + len;
656 			while (len-- > 0)
657 				*cp++ = (char)~0;
658 		}
659 	}
660 	return (mp);
661 }
662 
663 /*
664  * Completely vaporize a lower level tap and all associated interfaces.
665  * ill_delete is called only out of ip_close when the device control
666  * stream is being closed.
667  */
668 void
669 ill_delete(ill_t *ill)
670 {
671 	ipif_t	*ipif;
672 	ill_t	*prev_ill;
673 
674 	/*
675 	 * ill_delete may be forcibly entering the ipsq. The previous
676 	 * ioctl may not have completed and may need to be aborted.
677 	 * ipsq_flush takes care of it. If we don't need to enter the
678 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
679 	 * ill_delete_tail is sufficient.
680 	 */
681 	ipsq_flush(ill);
682 
683 	/*
684 	 * Nuke all interfaces.  ipif_free will take down the interface,
685 	 * remove it from the list, and free the data structure.
686 	 * Walk down the ipif list and remove the logical interfaces
687 	 * first before removing the main ipif. We can't unplumb
688 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
689 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
690 	 * POINTOPOINT.
691 	 *
692 	 * If ill_ipif was not properly initialized (i.e low on memory),
693 	 * then no interfaces to clean up. In this case just clean up the
694 	 * ill.
695 	 */
696 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
697 		ipif_free(ipif);
698 
699 	/*
700 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
701 	 * So nobody can be using this mp now. Free the mp allocated for
702 	 * honoring ILLF_NOARP
703 	 */
704 	freemsg(ill->ill_arp_on_mp);
705 	ill->ill_arp_on_mp = NULL;
706 
707 	/* Clean up msgs on pending upcalls for mrouted */
708 	reset_mrt_ill(ill);
709 
710 	/*
711 	 * ipif_free -> reset_conn_ipif will remove all multicast
712 	 * references for IPv4. For IPv6, we need to do it here as
713 	 * it points only at ills.
714 	 */
715 	reset_conn_ill(ill);
716 
717 	/*
718 	 * ill_down will arrange to blow off any IRE's dependent on this
719 	 * ILL, and shut down fragmentation reassembly.
720 	 */
721 	ill_down(ill);
722 
723 	/* Let SCTP know, so that it can remove this from its list. */
724 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
725 
726 	/*
727 	 * If an address on this ILL is being used as a source address then
728 	 * clear out the pointers in other ILLs that point to this ILL.
729 	 */
730 	rw_enter(&ill_g_usesrc_lock, RW_WRITER);
731 	if (ill->ill_usesrc_grp_next != NULL) {
732 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
733 			ill_disband_usesrc_group(ill);
734 		} else {	/* consumer of the usesrc ILL */
735 			prev_ill = ill_prev_usesrc(ill);
736 			prev_ill->ill_usesrc_grp_next =
737 			    ill->ill_usesrc_grp_next;
738 		}
739 	}
740 	rw_exit(&ill_g_usesrc_lock);
741 }
742 
743 /*
744  * ill_delete_tail is called from ip_modclose after all references
745  * to the closing ill are gone. The wait is done in ip_modclose
746  */
747 void
748 ill_delete_tail(ill_t *ill)
749 {
750 	mblk_t	**mpp;
751 	ipif_t	*ipif;
752 
753 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
754 		ipif_down_tail(ipif);
755 
756 	/*
757 	 * Send the detach if there's one to send (i.e., if we're above a
758 	 * style 2 DLPI driver).
759 	 */
760 	if (ill->ill_detach_mp != NULL) {
761 		ill_dlpi_send(ill, ill->ill_detach_mp);
762 		ill->ill_detach_mp = NULL;
763 	}
764 
765 	/*
766 	 * If polling capability is enabled (which signifies direct
767 	 * upcall into IP and driver has ill saved as a handle),
768 	 * we need to make sure that unbind has completed before we
769 	 * let the ill disappear and driver no longer has any reference
770 	 * to this ill.
771 	 */
772 	mutex_enter(&ill->ill_lock);
773 	if (ill->ill_capabilities & ILL_CAPAB_POLL) {
774 		while (!(ill->ill_state_flags & ILL_DL_UNBIND_DONE))
775 			cv_wait(&ill->ill_cv, &ill->ill_lock);
776 	}
777 	mutex_exit(&ill->ill_lock);
778 
779 	if (ill->ill_net_type != IRE_LOOPBACK)
780 		qprocsoff(ill->ill_rq);
781 
782 	/*
783 	 * We do an ipsq_flush once again now. New messages could have
784 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
785 	 * could also have landed up if an ioctl thread had looked up
786 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
787 	 * enqueued the ioctl when we did the ipsq_flush last time.
788 	 */
789 	ipsq_flush(ill);
790 
791 	/*
792 	 * Free capabilities.
793 	 */
794 	if (ill->ill_ipsec_capab_ah != NULL) {
795 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
796 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
797 		ill->ill_ipsec_capab_ah = NULL;
798 	}
799 
800 	if (ill->ill_ipsec_capab_esp != NULL) {
801 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
802 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
803 		ill->ill_ipsec_capab_esp = NULL;
804 	}
805 
806 	if (ill->ill_mdt_capab != NULL) {
807 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
808 		ill->ill_mdt_capab = NULL;
809 	}
810 
811 	if (ill->ill_hcksum_capab != NULL) {
812 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
813 		ill->ill_hcksum_capab = NULL;
814 	}
815 
816 	if (ill->ill_zerocopy_capab != NULL) {
817 		kmem_free(ill->ill_zerocopy_capab,
818 		    sizeof (ill_zerocopy_capab_t));
819 		ill->ill_zerocopy_capab = NULL;
820 	}
821 
822 	/*
823 	 * Clean up polling capabilities
824 	 */
825 	if (ill->ill_capabilities & ILL_CAPAB_POLL)
826 		ipsq_clean_all(ill);
827 
828 	if (ill->ill_poll_capab != NULL) {
829 		CONN_DEC_REF(ill->ill_poll_capab->ill_unbind_conn);
830 		ill->ill_poll_capab->ill_unbind_conn = NULL;
831 		kmem_free(ill->ill_poll_capab,
832 		    sizeof (ill_poll_capab_t) +
833 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
834 		ill->ill_poll_capab = NULL;
835 	}
836 
837 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
838 
839 	while (ill->ill_ipif != NULL)
840 		ipif_free_tail(ill->ill_ipif);
841 
842 	ill_down_tail(ill);
843 
844 	/*
845 	 * We have removed all references to ilm from conn and the ones joined
846 	 * within the kernel.
847 	 *
848 	 * We don't walk conns, mrts and ires because
849 	 *
850 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
851 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
852 	 *    ill references.
853 	 */
854 	ASSERT(ilm_walk_ill(ill) == 0);
855 	/*
856 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
857 	 * could free the phyint. No more reference to the phyint after this
858 	 * point.
859 	 */
860 	(void) ill_glist_delete(ill);
861 
862 	rw_enter(&ip_g_nd_lock, RW_WRITER);
863 	if (ill->ill_ndd_name != NULL)
864 		nd_unload(&ip_g_nd, ill->ill_ndd_name);
865 	rw_exit(&ip_g_nd_lock);
866 
867 
868 	if (ill->ill_frag_ptr != NULL) {
869 		uint_t count;
870 
871 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
872 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
873 		}
874 		mi_free(ill->ill_frag_ptr);
875 		ill->ill_frag_ptr = NULL;
876 		ill->ill_frag_hash_tbl = NULL;
877 	}
878 	if (ill->ill_nd_lla_mp != NULL)
879 		freemsg(ill->ill_nd_lla_mp);
880 	/* Free all retained control messages. */
881 	mpp = &ill->ill_first_mp_to_free;
882 	do {
883 		while (mpp[0]) {
884 			mblk_t  *mp;
885 			mblk_t  *mp1;
886 
887 			mp = mpp[0];
888 			mpp[0] = mp->b_next;
889 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
890 				mp1->b_next = NULL;
891 				mp1->b_prev = NULL;
892 			}
893 			freemsg(mp);
894 		}
895 	} while (mpp++ != &ill->ill_last_mp_to_free);
896 
897 	ill_free_mib(ill);
898 	ILL_TRACE_CLEANUP(ill);
899 }
900 
901 static void
902 ill_free_mib(ill_t *ill)
903 {
904 	if (ill->ill_ip6_mib != NULL) {
905 		kmem_free(ill->ill_ip6_mib, sizeof (*ill->ill_ip6_mib));
906 		ill->ill_ip6_mib = NULL;
907 	}
908 	if (ill->ill_icmp6_mib != NULL) {
909 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
910 		ill->ill_icmp6_mib = NULL;
911 	}
912 }
913 
914 /*
915  * Concatenate together a physical address and a sap.
916  *
917  * Sap_lengths are interpreted as follows:
918  *   sap_length == 0	==>	no sap
919  *   sap_length > 0	==>	sap is at the head of the dlpi address
920  *   sap_length < 0	==>	sap is at the tail of the dlpi address
921  */
922 static void
923 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
924     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
925 {
926 	uint16_t sap_addr = (uint16_t)sap_src;
927 
928 	if (sap_length == 0) {
929 		if (phys_src == NULL)
930 			bzero(dst, phys_length);
931 		else
932 			bcopy(phys_src, dst, phys_length);
933 	} else if (sap_length < 0) {
934 		if (phys_src == NULL)
935 			bzero(dst, phys_length);
936 		else
937 			bcopy(phys_src, dst, phys_length);
938 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
939 	} else {
940 		bcopy(&sap_addr, dst, sizeof (sap_addr));
941 		if (phys_src == NULL)
942 			bzero((char *)dst + sap_length, phys_length);
943 		else
944 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
945 	}
946 }
947 
948 /*
949  * Generate a dl_unitdata_req mblk for the device and address given.
950  * addr_length is the length of the physical portion of the address.
951  * If addr is NULL include an all zero address of the specified length.
952  * TRUE? In any case, addr_length is taken to be the entire length of the
953  * dlpi address, including the absolute value of sap_length.
954  */
955 mblk_t *
956 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
957 		t_scalar_t sap_length)
958 {
959 	dl_unitdata_req_t *dlur;
960 	mblk_t	*mp;
961 	t_scalar_t	abs_sap_length;		/* absolute value */
962 
963 	abs_sap_length = ABS(sap_length);
964 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
965 		DL_UNITDATA_REQ);
966 	if (mp == NULL)
967 		return (NULL);
968 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
969 	/* HACK: accomodate incompatible DLPI drivers */
970 	if (addr_length == 8)
971 		addr_length = 6;
972 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
973 	dlur->dl_dest_addr_offset = sizeof (*dlur);
974 	dlur->dl_priority.dl_min = 0;
975 	dlur->dl_priority.dl_max = 0;
976 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
977 	    (uchar_t *)&dlur[1]);
978 	return (mp);
979 }
980 
981 /*
982  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
983  * Return an error if we already have 1 or more ioctls in progress.
984  * This is used only for non-exclusive ioctls. Currently this is used
985  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
986  * and thus need to use ipsq_pending_mp_add.
987  */
988 boolean_t
989 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
990 {
991 	ASSERT(MUTEX_HELD(&ill->ill_lock));
992 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
993 	/*
994 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
995 	 */
996 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
997 	    (add_mp->b_datap->db_type == M_IOCTL));
998 
999 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1000 	/*
1001 	 * Return error if the conn has started closing. The conn
1002 	 * could have finished cleaning up the pending mp list,
1003 	 * If so we should not add another mp to the list negating
1004 	 * the cleanup.
1005 	 */
1006 	if (connp->conn_state_flags & CONN_CLOSING)
1007 		return (B_FALSE);
1008 	/*
1009 	 * Add the pending mp to the head of the list, chained by b_next.
1010 	 * Note down the conn on which the ioctl request came, in b_prev.
1011 	 * This will be used to later get the conn, when we get a response
1012 	 * on the ill queue, from some other module (typically arp)
1013 	 */
1014 	add_mp->b_next = (void *)ill->ill_pending_mp;
1015 	add_mp->b_queue = CONNP_TO_WQ(connp);
1016 	ill->ill_pending_mp = add_mp;
1017 	if (connp != NULL)
1018 		connp->conn_oper_pending_ill = ill;
1019 	return (B_TRUE);
1020 }
1021 
1022 /*
1023  * Retrieve the ill_pending_mp and return it. We have to walk the list
1024  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1025  */
1026 mblk_t *
1027 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1028 {
1029 	mblk_t	*prev = NULL;
1030 	mblk_t	*curr = NULL;
1031 	uint_t	id;
1032 	conn_t	*connp;
1033 
1034 	/*
1035 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1036 	 * up the pending mp, but it does not know the ioc_id and
1037 	 * passes in a zero for it.
1038 	 */
1039 	mutex_enter(&ill->ill_lock);
1040 	if (ioc_id != 0)
1041 		*connpp = NULL;
1042 
1043 	/* Search the list for the appropriate ioctl based on ioc_id */
1044 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1045 	    prev = curr, curr = curr->b_next) {
1046 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1047 		connp = Q_TO_CONN(curr->b_queue);
1048 		/* Match based on the ioc_id or based on the conn */
1049 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1050 			break;
1051 	}
1052 
1053 	if (curr != NULL) {
1054 		/* Unlink the mblk from the pending mp list */
1055 		if (prev != NULL) {
1056 			prev->b_next = curr->b_next;
1057 		} else {
1058 			ASSERT(ill->ill_pending_mp == curr);
1059 			ill->ill_pending_mp = curr->b_next;
1060 		}
1061 
1062 		/*
1063 		 * conn refcnt must have been bumped up at the start of
1064 		 * the ioctl. So we can safely access the conn.
1065 		 */
1066 		ASSERT(CONN_Q(curr->b_queue));
1067 		*connpp = Q_TO_CONN(curr->b_queue);
1068 		curr->b_next = NULL;
1069 		curr->b_queue = NULL;
1070 	}
1071 
1072 	mutex_exit(&ill->ill_lock);
1073 
1074 	return (curr);
1075 }
1076 
1077 /*
1078  * Add the pending mp to the list. There can be only 1 pending mp
1079  * in the list. Any exclusive ioctl that needs to wait for a response
1080  * from another module or driver needs to use this function to set
1081  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1082  * the other module/driver. This is also used while waiting for the
1083  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1084  */
1085 boolean_t
1086 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1087     int waitfor)
1088 {
1089 	ipsq_t	*ipsq;
1090 
1091 	ASSERT(IAM_WRITER_IPIF(ipif));
1092 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1093 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1094 	/*
1095 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1096 	 * M_ERROR/M_HANGUP from driver
1097 	 */
1098 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1099 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP));
1100 
1101 	ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1102 	if (connp != NULL) {
1103 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1104 		/*
1105 		 * Return error if the conn has started closing. The conn
1106 		 * could have finished cleaning up the pending mp list,
1107 		 * If so we should not add another mp to the list negating
1108 		 * the cleanup.
1109 		 */
1110 		if (connp->conn_state_flags & CONN_CLOSING)
1111 			return (B_FALSE);
1112 	}
1113 	mutex_enter(&ipsq->ipsq_lock);
1114 	ipsq->ipsq_pending_ipif = ipif;
1115 	/*
1116 	 * Note down the queue in b_queue. This will be returned by
1117 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1118 	 * the processing
1119 	 */
1120 	add_mp->b_next = NULL;
1121 	add_mp->b_queue = q;
1122 	ipsq->ipsq_pending_mp = add_mp;
1123 	ipsq->ipsq_waitfor = waitfor;
1124 	/*
1125 	 * ipsq_current_ipif is needed to restart the operation from
1126 	 * ipif_ill_refrele_tail when the last reference to the ipi/ill
1127 	 * is gone. Since this is not an ioctl ipsq_current_ipif has not
1128 	 * been set until now.
1129 	 */
1130 	if (DB_TYPE(add_mp) == M_ERROR || DB_TYPE(add_mp) == M_HANGUP) {
1131 		ASSERT(ipsq->ipsq_current_ipif == NULL);
1132 		ipsq->ipsq_current_ipif = ipif;
1133 		ipsq->ipsq_last_cmd = DB_TYPE(add_mp);
1134 	}
1135 	if (connp != NULL)
1136 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1137 	mutex_exit(&ipsq->ipsq_lock);
1138 	return (B_TRUE);
1139 }
1140 
1141 /*
1142  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1143  * queued in the list.
1144  */
1145 mblk_t *
1146 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1147 {
1148 	mblk_t	*curr = NULL;
1149 
1150 	mutex_enter(&ipsq->ipsq_lock);
1151 	*connpp = NULL;
1152 	if (ipsq->ipsq_pending_mp == NULL) {
1153 		mutex_exit(&ipsq->ipsq_lock);
1154 		return (NULL);
1155 	}
1156 
1157 	/* There can be only 1 such excl message */
1158 	curr = ipsq->ipsq_pending_mp;
1159 	ASSERT(curr != NULL && curr->b_next == NULL);
1160 	ipsq->ipsq_pending_ipif = NULL;
1161 	ipsq->ipsq_pending_mp = NULL;
1162 	ipsq->ipsq_waitfor = 0;
1163 	mutex_exit(&ipsq->ipsq_lock);
1164 
1165 	if (CONN_Q(curr->b_queue)) {
1166 		/*
1167 		 * This mp did a refhold on the conn, at the start of the ioctl.
1168 		 * So we can safely return a pointer to the conn to the caller.
1169 		 */
1170 		*connpp = Q_TO_CONN(curr->b_queue);
1171 	} else {
1172 		*connpp = NULL;
1173 	}
1174 	curr->b_next = NULL;
1175 	curr->b_prev = NULL;
1176 	return (curr);
1177 }
1178 
1179 /*
1180  * Cleanup the ioctl mp queued in ipsq_pending_mp
1181  * - Called in the ill_delete path
1182  * - Called in the M_ERROR or M_HANGUP path on the ill.
1183  * - Called in the conn close path.
1184  */
1185 boolean_t
1186 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1187 {
1188 	mblk_t	*mp;
1189 	ipsq_t	*ipsq;
1190 	queue_t	*q;
1191 	ipif_t	*ipif;
1192 
1193 	ASSERT(IAM_WRITER_ILL(ill));
1194 	ipsq = ill->ill_phyint->phyint_ipsq;
1195 	mutex_enter(&ipsq->ipsq_lock);
1196 	/*
1197 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1198 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1199 	 * even if it is meant for another ill, since we have to enqueue
1200 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1201 	 * If connp is non-null we are called from the conn close path.
1202 	 */
1203 	mp = ipsq->ipsq_pending_mp;
1204 	if (mp == NULL || (connp != NULL &&
1205 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1206 		mutex_exit(&ipsq->ipsq_lock);
1207 		return (B_FALSE);
1208 	}
1209 	/* Now remove from the ipsq_pending_mp */
1210 	ipsq->ipsq_pending_mp = NULL;
1211 	q = mp->b_queue;
1212 	mp->b_next = NULL;
1213 	mp->b_prev = NULL;
1214 	mp->b_queue = NULL;
1215 
1216 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1217 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1218 	if (ill->ill_move_in_progress) {
1219 		ILL_CLEAR_MOVE(ill);
1220 	} else if (ill->ill_up_ipifs) {
1221 		ill_group_cleanup(ill);
1222 	}
1223 
1224 	ipif = ipsq->ipsq_pending_ipif;
1225 	ipsq->ipsq_pending_ipif = NULL;
1226 	ipsq->ipsq_waitfor = 0;
1227 	ipsq->ipsq_current_ipif = NULL;
1228 	mutex_exit(&ipsq->ipsq_lock);
1229 
1230 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1231 		ip_ioctl_finish(q, mp, ENXIO, connp != NULL ? CONN_CLOSE :
1232 		    NO_COPYOUT, connp != NULL ? ipif : NULL, NULL);
1233 	} else {
1234 		/*
1235 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1236 		 * be just inet_freemsg. we have to restart it
1237 		 * otherwise the thread will be stuck.
1238 		 */
1239 		inet_freemsg(mp);
1240 	}
1241 	return (B_TRUE);
1242 }
1243 
1244 /*
1245  * The ill is closing. Cleanup all the pending mps. Called exclusively
1246  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1247  * knows this ill, and hence nobody can add an mp to this list
1248  */
1249 static void
1250 ill_pending_mp_cleanup(ill_t *ill)
1251 {
1252 	mblk_t	*mp;
1253 	queue_t	*q;
1254 
1255 	ASSERT(IAM_WRITER_ILL(ill));
1256 
1257 	mutex_enter(&ill->ill_lock);
1258 	/*
1259 	 * Every mp on the pending mp list originating from an ioctl
1260 	 * added 1 to the conn refcnt, at the start of the ioctl.
1261 	 * So bump it down now.  See comments in ip_wput_nondata()
1262 	 */
1263 	while (ill->ill_pending_mp != NULL) {
1264 		mp = ill->ill_pending_mp;
1265 		ill->ill_pending_mp = mp->b_next;
1266 		mutex_exit(&ill->ill_lock);
1267 
1268 		q = mp->b_queue;
1269 		ASSERT(CONN_Q(q));
1270 		mp->b_next = NULL;
1271 		mp->b_prev = NULL;
1272 		mp->b_queue = NULL;
1273 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL, NULL);
1274 		mutex_enter(&ill->ill_lock);
1275 	}
1276 	ill->ill_pending_ipif = NULL;
1277 
1278 	mutex_exit(&ill->ill_lock);
1279 }
1280 
1281 /*
1282  * Called in the conn close path and ill delete path
1283  */
1284 static void
1285 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1286 {
1287 	ipsq_t	*ipsq;
1288 	mblk_t	*prev;
1289 	mblk_t	*curr;
1290 	mblk_t	*next;
1291 	queue_t	*q;
1292 	mblk_t	*tmp_list = NULL;
1293 
1294 	ASSERT(IAM_WRITER_ILL(ill));
1295 	if (connp != NULL)
1296 		q = CONNP_TO_WQ(connp);
1297 	else
1298 		q = ill->ill_wq;
1299 
1300 	ipsq = ill->ill_phyint->phyint_ipsq;
1301 	/*
1302 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1303 	 * In the case of ioctl from a conn, there can be only 1 mp
1304 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1305 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1306 	 * ioctls meant for this ill form conn's are not flushed. They will
1307 	 * be processed during ipsq_exit and will not find the ill and will
1308 	 * return error.
1309 	 */
1310 	mutex_enter(&ipsq->ipsq_lock);
1311 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1312 	    curr = next) {
1313 		next = curr->b_next;
1314 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1315 			/* Unlink the mblk from the pending mp list */
1316 			if (prev != NULL) {
1317 				prev->b_next = curr->b_next;
1318 			} else {
1319 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1320 				ipsq->ipsq_xopq_mphead = curr->b_next;
1321 			}
1322 			if (ipsq->ipsq_xopq_mptail == curr)
1323 				ipsq->ipsq_xopq_mptail = prev;
1324 			/*
1325 			 * Create a temporary list and release the ipsq lock
1326 			 * New elements are added to the head of the tmp_list
1327 			 */
1328 			curr->b_next = tmp_list;
1329 			tmp_list = curr;
1330 		} else {
1331 			prev = curr;
1332 		}
1333 	}
1334 	mutex_exit(&ipsq->ipsq_lock);
1335 
1336 	while (tmp_list != NULL) {
1337 		curr = tmp_list;
1338 		tmp_list = curr->b_next;
1339 		curr->b_next = NULL;
1340 		curr->b_prev = NULL;
1341 		curr->b_queue = NULL;
1342 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1343 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1344 			    CONN_CLOSE : NO_COPYOUT, NULL, NULL);
1345 		} else {
1346 			/*
1347 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1348 			 * this can't be just inet_freemsg. we have to
1349 			 * restart it otherwise the thread will be stuck.
1350 			 */
1351 			inet_freemsg(curr);
1352 		}
1353 	}
1354 }
1355 
1356 /*
1357  * This conn has started closing. Cleanup any pending ioctl from this conn.
1358  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1359  */
1360 void
1361 conn_ioctl_cleanup(conn_t *connp)
1362 {
1363 	mblk_t *curr;
1364 	ipsq_t	*ipsq;
1365 	ill_t	*ill;
1366 	boolean_t refheld;
1367 
1368 	/*
1369 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1370 	 * ioctl has not yet started, the mp is pending in the list headed by
1371 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1372 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1373 	 * is currently executing now the mp is not queued anywhere but
1374 	 * conn_oper_pending_ill is null. The conn close will wait
1375 	 * till the conn_ref drops to zero.
1376 	 */
1377 	mutex_enter(&connp->conn_lock);
1378 	ill = connp->conn_oper_pending_ill;
1379 	if (ill == NULL) {
1380 		mutex_exit(&connp->conn_lock);
1381 		return;
1382 	}
1383 
1384 	curr = ill_pending_mp_get(ill, &connp, 0);
1385 	if (curr != NULL) {
1386 		mutex_exit(&connp->conn_lock);
1387 		CONN_DEC_REF(connp);
1388 		inet_freemsg(curr);
1389 		return;
1390 	}
1391 	/*
1392 	 * We may not be able to refhold the ill if the ill/ipif
1393 	 * is changing. But we need to make sure that the ill will
1394 	 * not vanish. So we just bump up the ill_waiter count.
1395 	 */
1396 	refheld = ill_waiter_inc(ill);
1397 	mutex_exit(&connp->conn_lock);
1398 	if (refheld) {
1399 		if (ipsq_enter(ill, B_TRUE)) {
1400 			ill_waiter_dcr(ill);
1401 			/*
1402 			 * Check whether this ioctl has started and is
1403 			 * pending now in ipsq_pending_mp. If it is not
1404 			 * found there then check whether this ioctl has
1405 			 * not even started and is in the ipsq_xopq list.
1406 			 */
1407 			if (!ipsq_pending_mp_cleanup(ill, connp))
1408 				ipsq_xopq_mp_cleanup(ill, connp);
1409 			ipsq = ill->ill_phyint->phyint_ipsq;
1410 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1411 			return;
1412 		}
1413 	}
1414 
1415 	/*
1416 	 * The ill is also closing and we could not bump up the
1417 	 * ill_waiter_count or we could not enter the ipsq. Leave
1418 	 * the cleanup to ill_delete
1419 	 */
1420 	mutex_enter(&connp->conn_lock);
1421 	while (connp->conn_oper_pending_ill != NULL)
1422 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1423 	mutex_exit(&connp->conn_lock);
1424 	if (refheld)
1425 		ill_waiter_dcr(ill);
1426 }
1427 
1428 /*
1429  * ipcl_walk function for cleaning up conn_*_ill fields.
1430  */
1431 static void
1432 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1433 {
1434 	ill_t	*ill = (ill_t *)arg;
1435 	ire_t	*ire;
1436 
1437 	mutex_enter(&connp->conn_lock);
1438 	if (connp->conn_multicast_ill == ill) {
1439 		/* Revert to late binding */
1440 		connp->conn_multicast_ill = NULL;
1441 		connp->conn_orig_multicast_ifindex = 0;
1442 	}
1443 	if (connp->conn_incoming_ill == ill)
1444 		connp->conn_incoming_ill = NULL;
1445 	if (connp->conn_outgoing_ill == ill)
1446 		connp->conn_outgoing_ill = NULL;
1447 	if (connp->conn_outgoing_pill == ill)
1448 		connp->conn_outgoing_pill = NULL;
1449 	if (connp->conn_nofailover_ill == ill)
1450 		connp->conn_nofailover_ill = NULL;
1451 	if (connp->conn_xmit_if_ill == ill)
1452 		connp->conn_xmit_if_ill = NULL;
1453 	if (connp->conn_ire_cache != NULL) {
1454 		ire = connp->conn_ire_cache;
1455 		/*
1456 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1457 		 * interface X and ipif coming from interface Y, if interface
1458 		 * X and Y are part of the same IPMPgroup. Thus whenever
1459 		 * interface X goes down, remove all references to it by
1460 		 * checking both on ire_ipif and ire_stq.
1461 		 */
1462 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1463 		    (ire->ire_type == IRE_CACHE &&
1464 		    ire->ire_stq == ill->ill_wq)) {
1465 			connp->conn_ire_cache = NULL;
1466 			mutex_exit(&connp->conn_lock);
1467 			ire_refrele_notr(ire);
1468 			return;
1469 		}
1470 	}
1471 	mutex_exit(&connp->conn_lock);
1472 
1473 }
1474 
1475 /* ARGSUSED */
1476 void
1477 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1478 {
1479 	ill_t	*ill = q->q_ptr;
1480 	ipif_t	*ipif;
1481 
1482 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1483 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1484 		ipif_down_tail(ipif);
1485 	ill_down_tail(ill);
1486 	freemsg(mp);
1487 	ipsq->ipsq_current_ipif = NULL;
1488 }
1489 
1490 /*
1491  * ill_down_start is called when we want to down this ill and bring it up again
1492  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1493  * all interfaces, but don't tear down any plumbing.
1494  */
1495 boolean_t
1496 ill_down_start(queue_t *q, mblk_t *mp)
1497 {
1498 	ill_t	*ill;
1499 	ipif_t	*ipif;
1500 
1501 	ill = q->q_ptr;
1502 
1503 	ASSERT(IAM_WRITER_ILL(ill));
1504 
1505 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1506 		(void) ipif_down(ipif, NULL, NULL);
1507 
1508 	ill_down(ill);
1509 
1510 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1511 	mutex_enter(&ill->ill_lock);
1512 	/*
1513 	 * Atomically test and add the pending mp if references are
1514 	 * still active.
1515 	 */
1516 	if (!ill_is_quiescent(ill)) {
1517 		/*
1518 		 * Get rid of any pending mps and cleanup. Call will
1519 		 * not fail since we are passing a null connp.
1520 		 */
1521 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1522 		    mp, ILL_DOWN);
1523 		mutex_exit(&ill->ill_lock);
1524 		return (B_FALSE);
1525 	}
1526 	mutex_exit(&ill->ill_lock);
1527 	return (B_TRUE);
1528 }
1529 
1530 static void
1531 ill_down(ill_t *ill)
1532 {
1533 	/* Blow off any IREs dependent on this ILL. */
1534 	ire_walk(ill_downi, (char *)ill);
1535 
1536 	mutex_enter(&ire_mrtun_lock);
1537 	if (ire_mrtun_count != 0) {
1538 		mutex_exit(&ire_mrtun_lock);
1539 		ire_walk_ill_mrtun(0, 0, ill_downi_mrtun_srcif,
1540 		    (char *)ill, NULL);
1541 	} else {
1542 		mutex_exit(&ire_mrtun_lock);
1543 	}
1544 
1545 	/*
1546 	 * If any interface based forwarding table exists
1547 	 * Blow off the ires there dependent on this ill
1548 	 */
1549 	mutex_enter(&ire_srcif_table_lock);
1550 	if (ire_srcif_table_count > 0) {
1551 		mutex_exit(&ire_srcif_table_lock);
1552 		ire_walk_srcif_table_v4(ill_downi_mrtun_srcif, (char *)ill);
1553 	} else {
1554 		mutex_exit(&ire_srcif_table_lock);
1555 	}
1556 
1557 	/* Remove any conn_*_ill depending on this ill */
1558 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill);
1559 
1560 	if (ill->ill_group != NULL) {
1561 		illgrp_delete(ill);
1562 	}
1563 
1564 }
1565 
1566 static void
1567 ill_down_tail(ill_t *ill)
1568 {
1569 	int	i;
1570 
1571 	/* Destroy ill_srcif_table if it exists */
1572 	/* Lock not reqd really because nobody should be able to access */
1573 	mutex_enter(&ill->ill_lock);
1574 	if (ill->ill_srcif_table != NULL) {
1575 		ill->ill_srcif_refcnt = 0;
1576 		for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
1577 			rw_destroy(&ill->ill_srcif_table[i].irb_lock);
1578 		}
1579 		kmem_free(ill->ill_srcif_table,
1580 		    IP_SRCIF_TABLE_SIZE * sizeof (irb_t));
1581 		ill->ill_srcif_table = NULL;
1582 		ill->ill_srcif_refcnt = 0;
1583 		ill->ill_mrtun_refcnt = 0;
1584 	}
1585 	mutex_exit(&ill->ill_lock);
1586 }
1587 
1588 /*
1589  * ire_walk routine used to delete every IRE that depends on queues
1590  * associated with 'ill'.  (Always called as writer.)
1591  */
1592 static void
1593 ill_downi(ire_t *ire, char *ill_arg)
1594 {
1595 	ill_t	*ill = (ill_t *)ill_arg;
1596 
1597 	/*
1598 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1599 	 * interface X and ipif coming from interface Y, if interface
1600 	 * X and Y are part of the same IPMP group. Thus whenever interface
1601 	 * X goes down, remove all references to it by checking both
1602 	 * on ire_ipif and ire_stq.
1603 	 */
1604 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1605 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1606 		ire_delete(ire);
1607 	}
1608 }
1609 
1610 /*
1611  * A seperate routine for deleting revtun and srcif based routes
1612  * are needed because the ires only deleted when the interface
1613  * is unplumbed. Also these ires have ire_in_ill non-null as well.
1614  * we want to keep mobile IP specific code separate.
1615  */
1616 static void
1617 ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg)
1618 {
1619 	ill_t   *ill = (ill_t *)ill_arg;
1620 
1621 	ASSERT(ire->ire_in_ill != NULL);
1622 
1623 	if ((ire->ire_in_ill != NULL && ire->ire_in_ill == ill) ||
1624 	    (ire->ire_stq == ill->ill_wq) || (ire->ire_stq == ill->ill_rq)) {
1625 		ire_delete(ire);
1626 	}
1627 }
1628 
1629 /*
1630  * Remove ire/nce from the fastpath list.
1631  */
1632 void
1633 ill_fastpath_nack(ill_t *ill)
1634 {
1635 	if (ill->ill_isv6) {
1636 		nce_fastpath_list_dispatch(ill, NULL, NULL);
1637 	} else {
1638 		ire_fastpath_list_dispatch(ill, NULL, NULL);
1639 	}
1640 }
1641 
1642 /* Consume an M_IOCACK of the fastpath probe. */
1643 void
1644 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1645 {
1646 	mblk_t	*mp1 = mp;
1647 
1648 	/*
1649 	 * If this was the first attempt turn on the fastpath probing.
1650 	 */
1651 	mutex_enter(&ill->ill_lock);
1652 	if (ill->ill_dlpi_fastpath_state == IDMS_INPROGRESS)
1653 		ill->ill_dlpi_fastpath_state = IDMS_OK;
1654 	mutex_exit(&ill->ill_lock);
1655 
1656 	/* Free the M_IOCACK mblk, hold on to the data */
1657 	mp = mp->b_cont;
1658 	freeb(mp1);
1659 	if (mp == NULL)
1660 		return;
1661 	if (mp->b_cont != NULL) {
1662 		/*
1663 		 * Update all IRE's or NCE's that are waiting for
1664 		 * fastpath update.
1665 		 */
1666 		if (ill->ill_isv6) {
1667 			/*
1668 			 * update nce's in the fastpath list.
1669 			 */
1670 			nce_fastpath_list_dispatch(ill,
1671 			    ndp_fastpath_update, mp);
1672 		} else {
1673 
1674 			/*
1675 			 * update ire's in the fastpath list.
1676 			 */
1677 			ire_fastpath_list_dispatch(ill,
1678 			    ire_fastpath_update, mp);
1679 			/*
1680 			 * Check if we need to traverse reverse tunnel table.
1681 			 * Since there is only single ire_type (IRE_MIPRTUN)
1682 			 * in the table, we don't need to match on ire_type.
1683 			 * We have to check ire_mrtun_count and not the
1684 			 * ill_mrtun_refcnt since ill_mrtun_refcnt is set
1685 			 * on the incoming ill and here we are dealing with
1686 			 * outgoing ill.
1687 			 */
1688 			mutex_enter(&ire_mrtun_lock);
1689 			if (ire_mrtun_count != 0) {
1690 				mutex_exit(&ire_mrtun_lock);
1691 				ire_walk_ill_mrtun(MATCH_IRE_WQ, IRE_MIPRTUN,
1692 				    (void (*)(ire_t *, void *))
1693 					ire_fastpath_update, mp, ill);
1694 			} else {
1695 				mutex_exit(&ire_mrtun_lock);
1696 			}
1697 		}
1698 		mp1 = mp->b_cont;
1699 		freeb(mp);
1700 		mp = mp1;
1701 	} else {
1702 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1703 	}
1704 
1705 	freeb(mp);
1706 }
1707 
1708 /*
1709  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1710  * The data portion of the request is a dl_unitdata_req_t template for
1711  * what we would send downstream in the absence of a fastpath confirmation.
1712  */
1713 int
1714 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1715 {
1716 	struct iocblk	*ioc;
1717 	mblk_t	*mp;
1718 
1719 	if (dlur_mp == NULL)
1720 		return (EINVAL);
1721 
1722 	mutex_enter(&ill->ill_lock);
1723 	switch (ill->ill_dlpi_fastpath_state) {
1724 	case IDMS_FAILED:
1725 		/*
1726 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1727 		 * support it.
1728 		 */
1729 		mutex_exit(&ill->ill_lock);
1730 		return (ENOTSUP);
1731 	case IDMS_UNKNOWN:
1732 		/* This is the first probe */
1733 		ill->ill_dlpi_fastpath_state = IDMS_INPROGRESS;
1734 		break;
1735 	default:
1736 		break;
1737 	}
1738 	mutex_exit(&ill->ill_lock);
1739 
1740 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1741 		return (EAGAIN);
1742 
1743 	mp->b_cont = copyb(dlur_mp);
1744 	if (mp->b_cont == NULL) {
1745 		freeb(mp);
1746 		return (EAGAIN);
1747 	}
1748 
1749 	ioc = (struct iocblk *)mp->b_rptr;
1750 	ioc->ioc_count = msgdsize(mp->b_cont);
1751 
1752 	putnext(ill->ill_wq, mp);
1753 	return (0);
1754 }
1755 
1756 void
1757 ill_capability_probe(ill_t *ill)
1758 {
1759 	/*
1760 	 * Do so only if negotiation is enabled, capabilities are unknown,
1761 	 * and a capability negotiation is not already in progress.
1762 	 */
1763 	if (ill->ill_capab_state != IDMS_UNKNOWN &&
1764 	    ill->ill_capab_state != IDMS_RENEG)
1765 		return;
1766 
1767 	ill->ill_capab_state = IDMS_INPROGRESS;
1768 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1769 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1770 }
1771 
1772 void
1773 ill_capability_reset(ill_t *ill)
1774 {
1775 	mblk_t *sc_mp = NULL;
1776 	mblk_t *tmp;
1777 
1778 	/*
1779 	 * Note here that we reset the state to UNKNOWN, and later send
1780 	 * down the DL_CAPABILITY_REQ without first setting the state to
1781 	 * INPROGRESS.  We do this in order to distinguish the
1782 	 * DL_CAPABILITY_ACK response which may come back in response to
1783 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1784 	 * also handle the case where the driver doesn't send us back
1785 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1786 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1787 	 * features are turned off until the state reaches IDMS_OK.
1788 	 */
1789 	ill->ill_capab_state = IDMS_UNKNOWN;
1790 
1791 	/*
1792 	 * Disable sub-capabilities and request a list of sub-capability
1793 	 * messages which will be sent down to the driver.  Each handler
1794 	 * allocates the corresponding dl_capability_sub_t inside an
1795 	 * mblk, and links it to the existing sc_mp mblk, or return it
1796 	 * as sc_mp if it's the first sub-capability (the passed in
1797 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1798 	 * sc_mp will be pulled-up, before passing it downstream.
1799 	 */
1800 	ill_capability_mdt_reset(ill, &sc_mp);
1801 	ill_capability_hcksum_reset(ill, &sc_mp);
1802 	ill_capability_zerocopy_reset(ill, &sc_mp);
1803 	ill_capability_ipsec_reset(ill, &sc_mp);
1804 	ill_capability_poll_reset(ill, &sc_mp);
1805 
1806 	/* Nothing to send down in order to disable the capabilities? */
1807 	if (sc_mp == NULL)
1808 		return;
1809 
1810 	tmp = msgpullup(sc_mp, -1);
1811 	freemsg(sc_mp);
1812 	if ((sc_mp = tmp) == NULL) {
1813 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1814 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1815 		return;
1816 	}
1817 
1818 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1819 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1820 }
1821 
1822 /*
1823  * Request or set new-style hardware capabilities supported by DLS provider.
1824  */
1825 static void
1826 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1827 {
1828 	mblk_t *mp;
1829 	dl_capability_req_t *capb;
1830 	size_t size = 0;
1831 	uint8_t *ptr;
1832 
1833 	if (reqp != NULL)
1834 		size = MBLKL(reqp);
1835 
1836 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1837 	if (mp == NULL) {
1838 		freemsg(reqp);
1839 		return;
1840 	}
1841 	ptr = mp->b_rptr;
1842 
1843 	capb = (dl_capability_req_t *)ptr;
1844 	ptr += sizeof (dl_capability_req_t);
1845 
1846 	if (reqp != NULL) {
1847 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1848 		capb->dl_sub_length = size;
1849 		bcopy(reqp->b_rptr, ptr, size);
1850 		ptr += size;
1851 		mp->b_cont = reqp->b_cont;
1852 		freeb(reqp);
1853 	}
1854 	ASSERT(ptr == mp->b_wptr);
1855 
1856 	ill_dlpi_send(ill, mp);
1857 }
1858 
1859 static void
1860 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1861 {
1862 	dl_capab_id_t *id_ic;
1863 	uint_t sub_dl_cap = outers->dl_cap;
1864 	dl_capability_sub_t *inners;
1865 	uint8_t *capend;
1866 
1867 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1868 
1869 	/*
1870 	 * Note: range checks here are not absolutely sufficient to
1871 	 * make us robust against malformed messages sent by drivers;
1872 	 * this is in keeping with the rest of IP's dlpi handling.
1873 	 * (Remember, it's coming from something else in the kernel
1874 	 * address space)
1875 	 */
1876 
1877 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1878 	if (capend > mp->b_wptr) {
1879 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1880 		    "malformed sub-capability too long for mblk");
1881 		return;
1882 	}
1883 
1884 	id_ic = (dl_capab_id_t *)(outers + 1);
1885 
1886 	if (outers->dl_length < sizeof (*id_ic) ||
1887 	    (inners = &id_ic->id_subcap,
1888 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1889 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1890 		    "encapsulated capab type %d too long for mblk",
1891 		    inners->dl_cap);
1892 		return;
1893 	}
1894 
1895 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1896 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1897 		    "isn't as expected; pass-thru module(s) detected, "
1898 		    "discarding capability\n", inners->dl_cap));
1899 		return;
1900 	}
1901 
1902 	/* Process the encapsulated sub-capability */
1903 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1904 }
1905 
1906 /*
1907  * Process Multidata Transmit capability negotiation ack received from a
1908  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1909  * DL_CAPABILITY_ACK message.
1910  */
1911 static void
1912 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1913 {
1914 	mblk_t *nmp = NULL;
1915 	dl_capability_req_t *oc;
1916 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1917 	ill_mdt_capab_t **ill_mdt_capab;
1918 	uint_t sub_dl_cap = isub->dl_cap;
1919 	uint8_t *capend;
1920 
1921 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1922 
1923 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1924 
1925 	/*
1926 	 * Note: range checks here are not absolutely sufficient to
1927 	 * make us robust against malformed messages sent by drivers;
1928 	 * this is in keeping with the rest of IP's dlpi handling.
1929 	 * (Remember, it's coming from something else in the kernel
1930 	 * address space)
1931 	 */
1932 
1933 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1934 	if (capend > mp->b_wptr) {
1935 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1936 		    "malformed sub-capability too long for mblk");
1937 		return;
1938 	}
1939 
1940 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1941 
1942 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1943 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1944 		    "unsupported MDT sub-capability (version %d, expected %d)",
1945 		    mdt_ic->mdt_version, MDT_VERSION_2);
1946 		return;
1947 	}
1948 
1949 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1950 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1951 		    "capability isn't as expected; pass-thru module(s) "
1952 		    "detected, discarding capability\n"));
1953 		return;
1954 	}
1955 
1956 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1957 
1958 		if (*ill_mdt_capab == NULL) {
1959 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1960 			    KM_NOSLEEP);
1961 
1962 			if (*ill_mdt_capab == NULL) {
1963 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1964 				    "could not enable MDT version %d "
1965 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1966 				    ill->ill_name);
1967 				return;
1968 			}
1969 		}
1970 
1971 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1972 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1973 		    "header spaces, %d max pld bufs, %d span limit)\n",
1974 		    ill->ill_name, MDT_VERSION_2,
1975 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1976 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1977 
1978 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1979 		(*ill_mdt_capab)->ill_mdt_on = 1;
1980 		/*
1981 		 * Round the following values to the nearest 32-bit; ULP
1982 		 * may further adjust them to accomodate for additional
1983 		 * protocol headers.  We pass these values to ULP during
1984 		 * bind time.
1985 		 */
1986 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1987 		    roundup(mdt_ic->mdt_hdr_head, 4);
1988 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1989 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1990 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1991 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1992 
1993 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1994 	} else {
1995 		uint_t size;
1996 		uchar_t *rptr;
1997 
1998 		size = sizeof (dl_capability_req_t) +
1999 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
2000 
2001 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2002 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2003 			    "could not enable MDT for %s (ENOMEM)\n",
2004 			    ill->ill_name);
2005 			return;
2006 		}
2007 
2008 		rptr = nmp->b_rptr;
2009 		/* initialize dl_capability_req_t */
2010 		oc = (dl_capability_req_t *)nmp->b_rptr;
2011 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2012 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2013 		    sizeof (dl_capab_mdt_t);
2014 		nmp->b_rptr += sizeof (dl_capability_req_t);
2015 
2016 		/* initialize dl_capability_sub_t */
2017 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2018 		nmp->b_rptr += sizeof (*isub);
2019 
2020 		/* initialize dl_capab_mdt_t */
2021 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2022 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2023 
2024 		nmp->b_rptr = rptr;
2025 
2026 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2027 		    "to enable MDT version %d\n", ill->ill_name,
2028 		    MDT_VERSION_2));
2029 
2030 		/* set ENABLE flag */
2031 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2032 
2033 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2034 		ill_dlpi_send(ill, nmp);
2035 	}
2036 }
2037 
2038 static void
2039 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2040 {
2041 	mblk_t *mp;
2042 	dl_capab_mdt_t *mdt_subcap;
2043 	dl_capability_sub_t *dl_subcap;
2044 	int size;
2045 
2046 	if (!ILL_MDT_CAPABLE(ill))
2047 		return;
2048 
2049 	ASSERT(ill->ill_mdt_capab != NULL);
2050 	/*
2051 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2052 	 * structure since it's possible that another thread is still
2053 	 * referring to it.  The structure only gets deallocated when
2054 	 * we destroy the ill.
2055 	 */
2056 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2057 
2058 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2059 
2060 	mp = allocb(size, BPRI_HI);
2061 	if (mp == NULL) {
2062 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2063 		    "request to disable MDT\n"));
2064 		return;
2065 	}
2066 
2067 	mp->b_wptr = mp->b_rptr + size;
2068 
2069 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2070 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2071 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2072 
2073 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2074 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2075 	mdt_subcap->mdt_flags = 0;
2076 	mdt_subcap->mdt_hdr_head = 0;
2077 	mdt_subcap->mdt_hdr_tail = 0;
2078 
2079 	if (*sc_mp != NULL)
2080 		linkb(*sc_mp, mp);
2081 	else
2082 		*sc_mp = mp;
2083 }
2084 
2085 /*
2086  * Send a DL_NOTIFY_REQ to the specified ill to enable
2087  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2088  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2089  * acceleration.
2090  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2091  */
2092 static boolean_t
2093 ill_enable_promisc_notify(ill_t *ill)
2094 {
2095 	mblk_t *mp;
2096 	dl_notify_req_t *req;
2097 
2098 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2099 
2100 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2101 	if (mp == NULL)
2102 		return (B_FALSE);
2103 
2104 	req = (dl_notify_req_t *)mp->b_rptr;
2105 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2106 	    DL_NOTE_PROMISC_OFF_PHYS;
2107 
2108 	ill_dlpi_send(ill, mp);
2109 
2110 	return (B_TRUE);
2111 }
2112 
2113 
2114 /*
2115  * Allocate an IPsec capability request which will be filled by our
2116  * caller to turn on support for one or more algorithms.
2117  */
2118 static mblk_t *
2119 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2120 {
2121 	mblk_t *nmp;
2122 	dl_capability_req_t	*ocap;
2123 	dl_capab_ipsec_t	*ocip;
2124 	dl_capab_ipsec_t	*icip;
2125 	uint8_t			*ptr;
2126 	icip = (dl_capab_ipsec_t *)(isub + 1);
2127 
2128 	/*
2129 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2130 	 * PROMISC_ON/OFF notification from the provider. We need to
2131 	 * do this before enabling the algorithms to avoid leakage of
2132 	 * cleartext packets.
2133 	 */
2134 
2135 	if (!ill_enable_promisc_notify(ill))
2136 		return (NULL);
2137 
2138 	/*
2139 	 * Allocate new mblk which will contain a new capability
2140 	 * request to enable the capabilities.
2141 	 */
2142 
2143 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2144 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2145 	if (nmp == NULL)
2146 		return (NULL);
2147 
2148 	ptr = nmp->b_rptr;
2149 
2150 	/* initialize dl_capability_req_t */
2151 	ocap = (dl_capability_req_t *)ptr;
2152 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2153 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2154 	ptr += sizeof (dl_capability_req_t);
2155 
2156 	/* initialize dl_capability_sub_t */
2157 	bcopy(isub, ptr, sizeof (*isub));
2158 	ptr += sizeof (*isub);
2159 
2160 	/* initialize dl_capab_ipsec_t */
2161 	ocip = (dl_capab_ipsec_t *)ptr;
2162 	bcopy(icip, ocip, sizeof (*icip));
2163 
2164 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2165 	return (nmp);
2166 }
2167 
2168 /*
2169  * Process an IPsec capability negotiation ack received from a DLS Provider.
2170  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2171  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2172  */
2173 static void
2174 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2175 {
2176 	dl_capab_ipsec_t	*icip;
2177 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2178 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2179 	uint_t cipher, nciphers;
2180 	mblk_t *nmp;
2181 	uint_t alg_len;
2182 	boolean_t need_sadb_dump;
2183 	uint_t sub_dl_cap = isub->dl_cap;
2184 	ill_ipsec_capab_t **ill_capab;
2185 	uint64_t ill_capab_flag;
2186 	uint8_t *capend, *ciphend;
2187 	boolean_t sadb_resync;
2188 
2189 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2190 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2191 
2192 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2193 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2194 		ill_capab_flag = ILL_CAPAB_AH;
2195 	} else {
2196 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2197 		ill_capab_flag = ILL_CAPAB_ESP;
2198 	}
2199 
2200 	/*
2201 	 * If the ill capability structure exists, then this incoming
2202 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2203 	 * If this is so, then we'd need to resynchronize the SADB
2204 	 * after re-enabling the offloaded ciphers.
2205 	 */
2206 	sadb_resync = (*ill_capab != NULL);
2207 
2208 	/*
2209 	 * Note: range checks here are not absolutely sufficient to
2210 	 * make us robust against malformed messages sent by drivers;
2211 	 * this is in keeping with the rest of IP's dlpi handling.
2212 	 * (Remember, it's coming from something else in the kernel
2213 	 * address space)
2214 	 */
2215 
2216 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2217 	if (capend > mp->b_wptr) {
2218 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2219 		    "malformed sub-capability too long for mblk");
2220 		return;
2221 	}
2222 
2223 	/*
2224 	 * There are two types of acks we process here:
2225 	 * 1. acks in reply to a (first form) generic capability req
2226 	 *    (no ENABLE flag set)
2227 	 * 2. acks in reply to a ENABLE capability req.
2228 	 *    (ENABLE flag set)
2229 	 *
2230 	 * We process the subcapability passed as argument as follows:
2231 	 * 1 do initializations
2232 	 *   1.1 initialize nmp = NULL
2233 	 *   1.2 set need_sadb_dump to B_FALSE
2234 	 * 2 for each cipher in subcapability:
2235 	 *   2.1 if ENABLE flag is set:
2236 	 *	2.1.1 update per-ill ipsec capabilities info
2237 	 *	2.1.2 set need_sadb_dump to B_TRUE
2238 	 *   2.2 if ENABLE flag is not set:
2239 	 *	2.2.1 if nmp is NULL:
2240 	 *		2.2.1.1 allocate and initialize nmp
2241 	 *		2.2.1.2 init current pos in nmp
2242 	 *	2.2.2 copy current cipher to current pos in nmp
2243 	 *	2.2.3 set ENABLE flag in nmp
2244 	 *	2.2.4 update current pos
2245 	 * 3 if nmp is not equal to NULL, send enable request
2246 	 *   3.1 send capability request
2247 	 * 4 if need_sadb_dump is B_TRUE
2248 	 *   4.1 enable promiscuous on/off notifications
2249 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2250 	 *	AH or ESP SA's to interface.
2251 	 */
2252 
2253 	nmp = NULL;
2254 	oalg = NULL;
2255 	need_sadb_dump = B_FALSE;
2256 	icip = (dl_capab_ipsec_t *)(isub + 1);
2257 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2258 
2259 	nciphers = icip->cip_nciphers;
2260 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2261 
2262 	if (ciphend > capend) {
2263 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2264 		    "too many ciphers for sub-capability len");
2265 		return;
2266 	}
2267 
2268 	for (cipher = 0; cipher < nciphers; cipher++) {
2269 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2270 
2271 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2272 			/*
2273 			 * TBD: when we provide a way to disable capabilities
2274 			 * from above, need to manage the request-pending state
2275 			 * and fail if we were not expecting this ACK.
2276 			 */
2277 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2278 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2279 
2280 			/*
2281 			 * Update IPsec capabilities for this ill
2282 			 */
2283 
2284 			if (*ill_capab == NULL) {
2285 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2286 				    ("ill_capability_ipsec_ack: "
2287 					"allocating ipsec_capab for ill\n"));
2288 				*ill_capab = ill_ipsec_capab_alloc();
2289 
2290 				if (*ill_capab == NULL) {
2291 					cmn_err(CE_WARN,
2292 					    "ill_capability_ipsec_ack: "
2293 					    "could not enable IPsec Hardware "
2294 					    "acceleration for %s (ENOMEM)\n",
2295 					    ill->ill_name);
2296 					return;
2297 				}
2298 			}
2299 
2300 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2301 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2302 
2303 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2304 				cmn_err(CE_WARN,
2305 				    "ill_capability_ipsec_ack: "
2306 				    "malformed IPsec algorithm id %d",
2307 				    ialg->alg_prim);
2308 				continue;
2309 			}
2310 
2311 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2312 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2313 				    ialg->alg_prim);
2314 			} else {
2315 				ipsec_capab_algparm_t *alp;
2316 
2317 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2318 				    ialg->alg_prim);
2319 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2320 				    ialg->alg_prim)) {
2321 					cmn_err(CE_WARN,
2322 					    "ill_capability_ipsec_ack: "
2323 					    "no space for IPsec alg id %d",
2324 					    ialg->alg_prim);
2325 					continue;
2326 				}
2327 				alp = &((*ill_capab)->encr_algparm[
2328 						ialg->alg_prim]);
2329 				alp->minkeylen = ialg->alg_minbits;
2330 				alp->maxkeylen = ialg->alg_maxbits;
2331 			}
2332 			ill->ill_capabilities |= ill_capab_flag;
2333 			/*
2334 			 * indicate that a capability was enabled, which
2335 			 * will be used below to kick off a SADB dump
2336 			 * to the ill.
2337 			 */
2338 			need_sadb_dump = B_TRUE;
2339 		} else {
2340 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2341 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2342 				ialg->alg_prim));
2343 
2344 			if (nmp == NULL) {
2345 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2346 				if (nmp == NULL) {
2347 					/*
2348 					 * Sending the PROMISC_ON/OFF
2349 					 * notification request failed.
2350 					 * We cannot enable the algorithms
2351 					 * since the Provider will not
2352 					 * notify IP of promiscous mode
2353 					 * changes, which could lead
2354 					 * to leakage of packets.
2355 					 */
2356 					cmn_err(CE_WARN,
2357 					    "ill_capability_ipsec_ack: "
2358 					    "could not enable IPsec Hardware "
2359 					    "acceleration for %s (ENOMEM)\n",
2360 					    ill->ill_name);
2361 					return;
2362 				}
2363 				/* ptr to current output alg specifier */
2364 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2365 			}
2366 
2367 			/*
2368 			 * Copy current alg specifier, set ENABLE
2369 			 * flag, and advance to next output alg.
2370 			 * For now we enable all IPsec capabilities.
2371 			 */
2372 			ASSERT(oalg != NULL);
2373 			bcopy(ialg, oalg, alg_len);
2374 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2375 			nmp->b_wptr += alg_len;
2376 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2377 		}
2378 
2379 		/* move to next input algorithm specifier */
2380 		ialg = (dl_capab_ipsec_alg_t *)
2381 		    ((char *)ialg + alg_len);
2382 	}
2383 
2384 	if (nmp != NULL)
2385 		/*
2386 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2387 		 * IPsec hardware acceleration.
2388 		 */
2389 		ill_dlpi_send(ill, nmp);
2390 
2391 	if (need_sadb_dump)
2392 		/*
2393 		 * An acknowledgement corresponding to a request to
2394 		 * enable acceleration was received, notify SADB.
2395 		 */
2396 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2397 }
2398 
2399 /*
2400  * Given an mblk with enough space in it, create sub-capability entries for
2401  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2402  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2403  * in preparation for the reset the DL_CAPABILITY_REQ message.
2404  */
2405 static void
2406 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2407     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2408 {
2409 	dl_capab_ipsec_t *oipsec;
2410 	dl_capab_ipsec_alg_t *oalg;
2411 	dl_capability_sub_t *dl_subcap;
2412 	int i, k;
2413 
2414 	ASSERT(nciphers > 0);
2415 	ASSERT(ill_cap != NULL);
2416 	ASSERT(mp != NULL);
2417 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2418 
2419 	/* dl_capability_sub_t for "stype" */
2420 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2421 	dl_subcap->dl_cap = stype;
2422 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2423 	mp->b_wptr += sizeof (dl_capability_sub_t);
2424 
2425 	/* dl_capab_ipsec_t for "stype" */
2426 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2427 	oipsec->cip_version = 1;
2428 	oipsec->cip_nciphers = nciphers;
2429 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2430 
2431 	/* create entries for "stype" AUTH ciphers */
2432 	for (i = 0; i < ill_cap->algs_size; i++) {
2433 		for (k = 0; k < BITSPERBYTE; k++) {
2434 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2435 				continue;
2436 
2437 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2438 			bzero((void *)oalg, sizeof (*oalg));
2439 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2440 			oalg->alg_prim = k + (BITSPERBYTE * i);
2441 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2442 		}
2443 	}
2444 	/* create entries for "stype" ENCR ciphers */
2445 	for (i = 0; i < ill_cap->algs_size; i++) {
2446 		for (k = 0; k < BITSPERBYTE; k++) {
2447 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2448 				continue;
2449 
2450 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2451 			bzero((void *)oalg, sizeof (*oalg));
2452 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2453 			oalg->alg_prim = k + (BITSPERBYTE * i);
2454 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2455 		}
2456 	}
2457 }
2458 
2459 /*
2460  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2461  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2462  * POPC instruction, but our macro is more flexible for an arbitrary length
2463  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2464  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2465  * stays that way, we can reduce the number of iterations required.
2466  */
2467 #define	COUNT_1S(val, sum) {					\
2468 	uint8_t x = val & 0xff;					\
2469 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2470 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2471 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2472 }
2473 
2474 /* ARGSUSED */
2475 static void
2476 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2477 {
2478 	mblk_t *mp;
2479 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2480 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2481 	uint64_t ill_capabilities = ill->ill_capabilities;
2482 	int ah_cnt = 0, esp_cnt = 0;
2483 	int ah_len = 0, esp_len = 0;
2484 	int i, size = 0;
2485 
2486 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2487 		return;
2488 
2489 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2490 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2491 
2492 	/* Find out the number of ciphers for AH */
2493 	if (cap_ah != NULL) {
2494 		for (i = 0; i < cap_ah->algs_size; i++) {
2495 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2496 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2497 		}
2498 		if (ah_cnt > 0) {
2499 			size += sizeof (dl_capability_sub_t) +
2500 			    sizeof (dl_capab_ipsec_t);
2501 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2502 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2503 			size += ah_len;
2504 		}
2505 	}
2506 
2507 	/* Find out the number of ciphers for ESP */
2508 	if (cap_esp != NULL) {
2509 		for (i = 0; i < cap_esp->algs_size; i++) {
2510 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2511 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2512 		}
2513 		if (esp_cnt > 0) {
2514 			size += sizeof (dl_capability_sub_t) +
2515 			    sizeof (dl_capab_ipsec_t);
2516 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2517 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2518 			size += esp_len;
2519 		}
2520 	}
2521 
2522 	if (size == 0) {
2523 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2524 		    "there's nothing to reset\n"));
2525 		return;
2526 	}
2527 
2528 	mp = allocb(size, BPRI_HI);
2529 	if (mp == NULL) {
2530 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2531 		    "request to disable IPSEC Hardware Acceleration\n"));
2532 		return;
2533 	}
2534 
2535 	/*
2536 	 * Clear the capability flags for IPSec HA but retain the ill
2537 	 * capability structures since it's possible that another thread
2538 	 * is still referring to them.  The structures only get deallocated
2539 	 * when we destroy the ill.
2540 	 *
2541 	 * Various places check the flags to see if the ill is capable of
2542 	 * hardware acceleration, and by clearing them we ensure that new
2543 	 * outbound IPSec packets are sent down encrypted.
2544 	 */
2545 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2546 
2547 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2548 	if (ah_cnt > 0) {
2549 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2550 		    cap_ah, mp);
2551 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2552 	}
2553 
2554 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2555 	if (esp_cnt > 0) {
2556 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2557 		    cap_esp, mp);
2558 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2559 	}
2560 
2561 	/*
2562 	 * At this point we've composed a bunch of sub-capabilities to be
2563 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2564 	 * by the caller.  Upon receiving this reset message, the driver
2565 	 * must stop inbound decryption (by destroying all inbound SAs)
2566 	 * and let the corresponding packets come in encrypted.
2567 	 */
2568 
2569 	if (*sc_mp != NULL)
2570 		linkb(*sc_mp, mp);
2571 	else
2572 		*sc_mp = mp;
2573 }
2574 
2575 static void
2576 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2577     boolean_t encapsulated)
2578 {
2579 	boolean_t legacy = B_FALSE;
2580 
2581 	/*
2582 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2583 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2584 	 * instructed the driver to disable its advertised capabilities,
2585 	 * so there's no point in accepting any response at this moment.
2586 	 */
2587 	if (ill->ill_capab_state == IDMS_UNKNOWN)
2588 		return;
2589 
2590 	/*
2591 	 * Note that only the following two sub-capabilities may be
2592 	 * considered as "legacy", since their original definitions
2593 	 * do not incorporate the dl_mid_t module ID token, and hence
2594 	 * may require the use of the wrapper sub-capability.
2595 	 */
2596 	switch (subp->dl_cap) {
2597 	case DL_CAPAB_IPSEC_AH:
2598 	case DL_CAPAB_IPSEC_ESP:
2599 		legacy = B_TRUE;
2600 		break;
2601 	}
2602 
2603 	/*
2604 	 * For legacy sub-capabilities which don't incorporate a queue_t
2605 	 * pointer in their structures, discard them if we detect that
2606 	 * there are intermediate modules in between IP and the driver.
2607 	 */
2608 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2609 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2610 		    "%d discarded; %d module(s) present below IP\n",
2611 		    subp->dl_cap, ill->ill_lmod_cnt));
2612 		return;
2613 	}
2614 
2615 	switch (subp->dl_cap) {
2616 	case DL_CAPAB_IPSEC_AH:
2617 	case DL_CAPAB_IPSEC_ESP:
2618 		ill_capability_ipsec_ack(ill, mp, subp);
2619 		break;
2620 	case DL_CAPAB_MDT:
2621 		ill_capability_mdt_ack(ill, mp, subp);
2622 		break;
2623 	case DL_CAPAB_HCKSUM:
2624 		ill_capability_hcksum_ack(ill, mp, subp);
2625 		break;
2626 	case DL_CAPAB_ZEROCOPY:
2627 		ill_capability_zerocopy_ack(ill, mp, subp);
2628 		break;
2629 	case DL_CAPAB_POLL:
2630 		ill_capability_poll_ack(ill, mp, subp);
2631 		break;
2632 	default:
2633 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2634 		    subp->dl_cap));
2635 	}
2636 }
2637 
2638 /*
2639  * As part of negotiating polling capability, the driver tells us
2640  * the default (or normal) blanking interval and packet threshold
2641  * (the receive timer fires if blanking interval is reached or
2642  * the packet threshold is reached).
2643  *
2644  * As part of manipulating the polling interval, we always use our
2645  * estimated interval (avg service time * number of packets queued
2646  * on the squeue) but we try to blank for a minimum of
2647  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2648  * packet threshold during this time. When we are not in polling mode
2649  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2650  * rr_min_blank_ratio but up the packet cnt by a ratio of
2651  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2652  * possible although for a shorter interval.
2653  */
2654 #define	RR_MAX_BLANK_RATIO	20
2655 #define	RR_MIN_BLANK_RATIO	10
2656 #define	RR_MAX_PKT_CNT_RATIO	3
2657 #define	RR_MIN_PKT_CNT_RATIO	3
2658 
2659 /*
2660  * These can be tuned via /etc/system.
2661  */
2662 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2663 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2664 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2665 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2666 
2667 static mac_resource_handle_t
2668 ill_ring_add(void *arg, mac_resource_t *mrp)
2669 {
2670 	ill_t			*ill = (ill_t *)arg;
2671 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2672 	ill_rx_ring_t		*rx_ring;
2673 	int			ip_rx_index;
2674 
2675 	if (mrp->mr_type != MAC_RX_FIFO) {
2676 		return (NULL);
2677 	}
2678 	ASSERT(ill != NULL);
2679 	ASSERT(ill->ill_poll_capab != NULL);
2680 	ASSERT(mrp != NULL);
2681 
2682 	mutex_enter(&ill->ill_lock);
2683 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2684 		rx_ring = &ill->ill_poll_capab->ill_ring_tbl[ip_rx_index];
2685 		ASSERT(rx_ring != NULL);
2686 
2687 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2688 			time_t normal_blank_time =
2689 			    mrfp->mrf_normal_blank_time;
2690 			uint_t normal_pkt_cnt =
2691 			    mrfp->mrf_normal_pkt_count;
2692 
2693 			bzero(rx_ring, sizeof (ill_rx_ring_t));
2694 
2695 			rx_ring->rr_blank = mrfp->mrf_blank;
2696 			rx_ring->rr_handle = mrfp->mrf_arg;
2697 			rx_ring->rr_ill = ill;
2698 			rx_ring->rr_normal_blank_time = normal_blank_time;
2699 			rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2700 
2701 			rx_ring->rr_max_blank_time =
2702 			    normal_blank_time * rr_max_blank_ratio;
2703 			rx_ring->rr_min_blank_time =
2704 			    normal_blank_time * rr_min_blank_ratio;
2705 			rx_ring->rr_max_pkt_cnt =
2706 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2707 			rx_ring->rr_min_pkt_cnt =
2708 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2709 
2710 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2711 			mutex_exit(&ill->ill_lock);
2712 
2713 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2714 			    (int), ip_rx_index);
2715 			return ((mac_resource_handle_t)rx_ring);
2716 		}
2717 	}
2718 
2719 	/*
2720 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2721 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2722 	 * should be made configurable. Meanwhile it cause no panic because
2723 	 * driver will pass ip_input a NULL handle which will make
2724 	 * IP allocate the default squeue and Polling mode will not
2725 	 * be used for this ring.
2726 	 */
2727 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2728 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2729 
2730 	mutex_exit(&ill->ill_lock);
2731 	return (NULL);
2732 }
2733 
2734 static boolean_t
2735 ill_capability_poll_init(ill_t *ill)
2736 {
2737 	ill_poll_capab_t	*ill_poll = ill->ill_poll_capab;
2738 	conn_t 			*connp;
2739 	size_t			sz;
2740 
2741 	if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2742 		if (ill_poll == NULL) {
2743 			cmn_err(CE_PANIC, "ill_capability_poll_init: "
2744 			    "polling enabled for ill=%s (%p) but data "
2745 			    "structs uninitialized\n", ill->ill_name,
2746 			    (void *)ill);
2747 		}
2748 		return (B_TRUE);
2749 	}
2750 
2751 	if (ill_poll != NULL) {
2752 		ill_rx_ring_t 	*rx_ring = ill_poll->ill_ring_tbl;
2753 		/* Polling is being re-enabled */
2754 
2755 		connp = ill_poll->ill_unbind_conn;
2756 		ASSERT(rx_ring != NULL);
2757 		bzero((void *)ill_poll, sizeof (ill_poll_capab_t));
2758 		bzero((void *)rx_ring,
2759 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2760 		ill_poll->ill_ring_tbl = rx_ring;
2761 		ill_poll->ill_unbind_conn = connp;
2762 		return (B_TRUE);
2763 	}
2764 
2765 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP)) == NULL)
2766 		return (B_FALSE);
2767 
2768 	sz = sizeof (ill_poll_capab_t);
2769 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2770 
2771 	ill_poll = kmem_zalloc(sz, KM_NOSLEEP);
2772 	if (ill_poll == NULL) {
2773 		cmn_err(CE_WARN, "ill_capability_poll_init: could not "
2774 		    "allocate poll_capab for %s (%p)\n", ill->ill_name,
2775 		    (void *)ill);
2776 		CONN_DEC_REF(connp);
2777 		return (B_FALSE);
2778 	}
2779 
2780 	/* Allocate space to hold ring table */
2781 	ill_poll->ill_ring_tbl = (ill_rx_ring_t *)&ill_poll[1];
2782 	ill->ill_poll_capab = ill_poll;
2783 	ill_poll->ill_unbind_conn = connp;
2784 	return (B_TRUE);
2785 }
2786 
2787 /*
2788  * ill_capability_poll_disable: disable polling capability. Since
2789  * any of the rings might already be in use, need to call ipsq_clean_all()
2790  * which gets behind the squeue to disable direct calls if necessary.
2791  * Clean up the direct tx function pointers as well.
2792  */
2793 static void
2794 ill_capability_poll_disable(ill_t *ill)
2795 {
2796 	ill_poll_capab_t	*ill_poll = ill->ill_poll_capab;
2797 
2798 	if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2799 		ipsq_clean_all(ill);
2800 		ill_poll->ill_tx = NULL;
2801 		ill_poll->ill_tx_handle = NULL;
2802 	}
2803 
2804 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
2805 }
2806 
2807 static void
2808 ill_capability_poll_capable(ill_t *ill, dl_capab_poll_t *ipoll,
2809     dl_capability_sub_t *isub)
2810 {
2811 	uint_t			size;
2812 	uchar_t			*rptr;
2813 	dl_capab_poll_t		poll, *opoll;
2814 	ill_poll_capab_t	*ill_poll;
2815 	mblk_t			*nmp = NULL;
2816 	dl_capability_req_t	*ocap;
2817 
2818 	if (!ill_capability_poll_init(ill))
2819 		return;
2820 	ill_poll = ill->ill_poll_capab;
2821 
2822 	/* Copy locally to get the members aligned */
2823 	bcopy((void *)ipoll, (void *)&poll, sizeof (dl_capab_poll_t));
2824 
2825 	/* Get the tx function and handle from dld */
2826 	ill_poll->ill_tx = (ip_dld_tx_t)poll.poll_tx;
2827 	ill_poll->ill_tx_handle = (void *)poll.poll_tx_handle;
2828 
2829 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2830 	    isub->dl_length;
2831 
2832 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2833 		cmn_err(CE_WARN, "ill_capability_poll_ack: could not allocate "
2834 		    "memory for CAPAB_REQ for %s (%p)\n", ill->ill_name,
2835 		    (void *)ill);
2836 		return;
2837 	}
2838 
2839 	/* initialize dl_capability_req_t */
2840 	rptr = nmp->b_rptr;
2841 	ocap = (dl_capability_req_t *)rptr;
2842 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2843 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2844 	rptr += sizeof (dl_capability_req_t);
2845 
2846 	/* initialize dl_capability_sub_t */
2847 	bcopy(isub, rptr, sizeof (*isub));
2848 	rptr += sizeof (*isub);
2849 
2850 	opoll = (dl_capab_poll_t *)rptr;
2851 	rptr += sizeof (dl_capab_poll_t);
2852 
2853 	/* initialize dl_capab_poll_t to be sent down */
2854 	poll.poll_rx_handle = (uintptr_t)ill;
2855 	poll.poll_rx = (uintptr_t)ip_input;
2856 	poll.poll_ring_add = (uintptr_t)ill_ring_add;
2857 	poll.poll_flags = POLL_ENABLE;
2858 	bcopy((void *)&poll, (void *)opoll, sizeof (dl_capab_poll_t));
2859 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2860 
2861 	ip1dbg(("ill_capability_poll_capable: asking interface %s "
2862 	    "to enable polling\n", ill->ill_name));
2863 
2864 	/* nmp points to a DL_CAPABILITY_REQ message to enable polling */
2865 	ill_dlpi_send(ill, nmp);
2866 }
2867 
2868 
2869 /*
2870  * Process a polling capability negotiation ack received
2871  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_POLL)
2872  * of a DL_CAPABILITY_ACK message.
2873  */
2874 static void
2875 ill_capability_poll_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2876 {
2877 	dl_capab_poll_t		*ipoll;
2878 	uint_t			sub_dl_cap = isub->dl_cap;
2879 	uint8_t			*capend;
2880 
2881 
2882 	ASSERT(sub_dl_cap == DL_CAPAB_POLL);
2883 
2884 	/*
2885 	 * Don't enable polling for ipv6 ill's
2886 	 */
2887 	if (ill->ill_isv6) {
2888 		return;
2889 	}
2890 
2891 	/*
2892 	 * Note: range checks here are not absolutely sufficient to
2893 	 * make us robust against malformed messages sent by drivers;
2894 	 * this is in keeping with the rest of IP's dlpi handling.
2895 	 * (Remember, it's coming from something else in the kernel
2896 	 * address space)
2897 	 */
2898 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2899 	if (capend > mp->b_wptr) {
2900 		cmn_err(CE_WARN, "ill_capability_poll_ack: "
2901 		    "malformed sub-capability too long for mblk");
2902 		return;
2903 	}
2904 
2905 	/*
2906 	 * There are two types of acks we process here:
2907 	 * 1. acks in reply to a (first form) generic capability req
2908 	 *    (poll_flag will be set to POLL_CAPABLE)
2909 	 * 2. acks in reply to a POLL_ENABLE capability req.
2910 	 *    (POLL_ENABLE flag set)
2911 	 */
2912 	ipoll = (dl_capab_poll_t *)(isub + 1);
2913 
2914 	if (!dlcapabcheckqid(&ipoll->poll_mid, ill->ill_lmod_rq)) {
2915 		ip1dbg(("ill_capability_poll_ack: mid token for polling "
2916 		    "capability isn't as expected; pass-thru "
2917 		    "module(s) detected, discarding capability\n"));
2918 		if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2919 			/*
2920 			 * This is a capability renegotitation case.
2921 			 * The interface better be unusable at this
2922 			 * point other wise bad things will happen
2923 			 * if we disable direct calls on a running
2924 			 * and up interface.
2925 			 */
2926 			ill_capability_poll_disable(ill);
2927 		}
2928 		return;
2929 	}
2930 
2931 	switch (ipoll->poll_flags) {
2932 	default:
2933 		/* Disable if unknown flag */
2934 	case POLL_DISABLE:
2935 		ill_capability_poll_disable(ill);
2936 		break;
2937 	case POLL_CAPABLE:
2938 		/*
2939 		 * If the capability was already enabled, its safe
2940 		 * to disable it first to get rid of stale information
2941 		 * and then start enabling it again.
2942 		 */
2943 		ill_capability_poll_disable(ill);
2944 		ill_capability_poll_capable(ill, ipoll, isub);
2945 		break;
2946 	case POLL_ENABLE:
2947 		if (!(ill->ill_capabilities & ILL_CAPAB_POLL)) {
2948 			ASSERT(ill->ill_poll_capab != NULL);
2949 			ill->ill_capabilities |= ILL_CAPAB_POLL;
2950 		}
2951 		ip1dbg(("ill_capability_poll_ack: interface %s "
2952 		    "has enabled polling\n", ill->ill_name));
2953 		break;
2954 	}
2955 }
2956 
2957 static void
2958 ill_capability_poll_reset(ill_t *ill, mblk_t **sc_mp)
2959 {
2960 	mblk_t *mp;
2961 	dl_capab_poll_t *ipoll;
2962 	dl_capability_sub_t *dl_subcap;
2963 	int size;
2964 
2965 	if (!(ill->ill_capabilities & ILL_CAPAB_POLL))
2966 		return;
2967 
2968 	ASSERT(ill->ill_poll_capab != NULL);
2969 
2970 	/*
2971 	 * Disable polling capability
2972 	 */
2973 	ill_capability_poll_disable(ill);
2974 
2975 	size = sizeof (*dl_subcap) + sizeof (*ipoll);
2976 
2977 	mp = allocb(size, BPRI_HI);
2978 	if (mp == NULL) {
2979 		ip1dbg(("ill_capability_poll_reset: unable to allocate "
2980 		    "request to disable polling\n"));
2981 		return;
2982 	}
2983 
2984 	mp->b_wptr = mp->b_rptr + size;
2985 
2986 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2987 	dl_subcap->dl_cap = DL_CAPAB_POLL;
2988 	dl_subcap->dl_length = sizeof (*ipoll);
2989 
2990 	ipoll = (dl_capab_poll_t *)(dl_subcap + 1);
2991 	ipoll->poll_flags = POLL_DISABLE;
2992 
2993 	if (*sc_mp != NULL)
2994 		linkb(*sc_mp, mp);
2995 	else
2996 		*sc_mp = mp;
2997 }
2998 
2999 
3000 /*
3001  * Process a hardware checksum offload capability negotiation ack received
3002  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3003  * of a DL_CAPABILITY_ACK message.
3004  */
3005 static void
3006 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3007 {
3008 	dl_capability_req_t	*ocap;
3009 	dl_capab_hcksum_t	*ihck, *ohck;
3010 	ill_hcksum_capab_t	**ill_hcksum;
3011 	mblk_t			*nmp = NULL;
3012 	uint_t			sub_dl_cap = isub->dl_cap;
3013 	uint8_t			*capend;
3014 
3015 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3016 
3017 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3018 
3019 	/*
3020 	 * Note: range checks here are not absolutely sufficient to
3021 	 * make us robust against malformed messages sent by drivers;
3022 	 * this is in keeping with the rest of IP's dlpi handling.
3023 	 * (Remember, it's coming from something else in the kernel
3024 	 * address space)
3025 	 */
3026 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3027 	if (capend > mp->b_wptr) {
3028 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3029 		    "malformed sub-capability too long for mblk");
3030 		return;
3031 	}
3032 
3033 	/*
3034 	 * There are two types of acks we process here:
3035 	 * 1. acks in reply to a (first form) generic capability req
3036 	 *    (no ENABLE flag set)
3037 	 * 2. acks in reply to a ENABLE capability req.
3038 	 *    (ENABLE flag set)
3039 	 */
3040 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3041 
3042 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3043 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3044 		    "unsupported hardware checksum "
3045 		    "sub-capability (version %d, expected %d)",
3046 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3047 		return;
3048 	}
3049 
3050 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3051 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3052 		    "checksum capability isn't as expected; pass-thru "
3053 		    "module(s) detected, discarding capability\n"));
3054 		return;
3055 	}
3056 
3057 #define	CURR_HCKSUM_CAPAB				\
3058 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3059 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3060 
3061 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3062 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3063 		/* do ENABLE processing */
3064 		if (*ill_hcksum == NULL) {
3065 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3066 			    KM_NOSLEEP);
3067 
3068 			if (*ill_hcksum == NULL) {
3069 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3070 				    "could not enable hcksum version %d "
3071 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3072 				    ill->ill_name);
3073 				return;
3074 			}
3075 		}
3076 
3077 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3078 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3079 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3080 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3081 		    "has enabled hardware checksumming\n ",
3082 		    ill->ill_name));
3083 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3084 		/*
3085 		 * Enabling hardware checksum offload
3086 		 * Currently IP supports {TCP,UDP}/IPv4
3087 		 * partial and full cksum offload and
3088 		 * IPv4 header checksum offload.
3089 		 * Allocate new mblk which will
3090 		 * contain a new capability request
3091 		 * to enable hardware checksum offload.
3092 		 */
3093 		uint_t	size;
3094 		uchar_t	*rptr;
3095 
3096 		size = sizeof (dl_capability_req_t) +
3097 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3098 
3099 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3100 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3101 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3102 			    ill->ill_name);
3103 			return;
3104 		}
3105 
3106 		rptr = nmp->b_rptr;
3107 		/* initialize dl_capability_req_t */
3108 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3109 		ocap->dl_sub_offset =
3110 		    sizeof (dl_capability_req_t);
3111 		ocap->dl_sub_length =
3112 		    sizeof (dl_capability_sub_t) +
3113 		    isub->dl_length;
3114 		nmp->b_rptr += sizeof (dl_capability_req_t);
3115 
3116 		/* initialize dl_capability_sub_t */
3117 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3118 		nmp->b_rptr += sizeof (*isub);
3119 
3120 		/* initialize dl_capab_hcksum_t */
3121 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3122 		bcopy(ihck, ohck, sizeof (*ihck));
3123 
3124 		nmp->b_rptr = rptr;
3125 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3126 
3127 		/* Set ENABLE flag */
3128 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3129 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3130 
3131 		/*
3132 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3133 		 * hardware checksum acceleration.
3134 		 */
3135 		ill_dlpi_send(ill, nmp);
3136 	} else {
3137 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3138 		    "advertised %x hardware checksum capability flags\n",
3139 		    ill->ill_name, ihck->hcksum_txflags));
3140 	}
3141 }
3142 
3143 static void
3144 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3145 {
3146 	mblk_t *mp;
3147 	dl_capab_hcksum_t *hck_subcap;
3148 	dl_capability_sub_t *dl_subcap;
3149 	int size;
3150 
3151 	if (!ILL_HCKSUM_CAPABLE(ill))
3152 		return;
3153 
3154 	ASSERT(ill->ill_hcksum_capab != NULL);
3155 	/*
3156 	 * Clear the capability flag for hardware checksum offload but
3157 	 * retain the ill_hcksum_capab structure since it's possible that
3158 	 * another thread is still referring to it.  The structure only
3159 	 * gets deallocated when we destroy the ill.
3160 	 */
3161 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3162 
3163 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3164 
3165 	mp = allocb(size, BPRI_HI);
3166 	if (mp == NULL) {
3167 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3168 		    "request to disable hardware checksum offload\n"));
3169 		return;
3170 	}
3171 
3172 	mp->b_wptr = mp->b_rptr + size;
3173 
3174 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3175 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3176 	dl_subcap->dl_length = sizeof (*hck_subcap);
3177 
3178 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3179 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3180 	hck_subcap->hcksum_txflags = 0;
3181 
3182 	if (*sc_mp != NULL)
3183 		linkb(*sc_mp, mp);
3184 	else
3185 		*sc_mp = mp;
3186 }
3187 
3188 static void
3189 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3190 {
3191 	mblk_t *nmp = NULL;
3192 	dl_capability_req_t *oc;
3193 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3194 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3195 	uint_t sub_dl_cap = isub->dl_cap;
3196 	uint8_t *capend;
3197 
3198 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3199 
3200 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3201 
3202 	/*
3203 	 * Note: range checks here are not absolutely sufficient to
3204 	 * make us robust against malformed messages sent by drivers;
3205 	 * this is in keeping with the rest of IP's dlpi handling.
3206 	 * (Remember, it's coming from something else in the kernel
3207 	 * address space)
3208 	 */
3209 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3210 	if (capend > mp->b_wptr) {
3211 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3212 		    "malformed sub-capability too long for mblk");
3213 		return;
3214 	}
3215 
3216 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3217 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3218 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3219 		    "unsupported ZEROCOPY sub-capability (version %d, "
3220 		    "expected %d)", zc_ic->zerocopy_version,
3221 		    ZEROCOPY_VERSION_1);
3222 		return;
3223 	}
3224 
3225 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3226 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3227 		    "capability isn't as expected; pass-thru module(s) "
3228 		    "detected, discarding capability\n"));
3229 		return;
3230 	}
3231 
3232 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3233 		if (*ill_zerocopy_capab == NULL) {
3234 			*ill_zerocopy_capab =
3235 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3236 			    KM_NOSLEEP);
3237 
3238 			if (*ill_zerocopy_capab == NULL) {
3239 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3240 				    "could not enable Zero-copy version %d "
3241 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3242 				    ill->ill_name);
3243 				return;
3244 			}
3245 		}
3246 
3247 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3248 		    "supports Zero-copy version %d\n", ill->ill_name,
3249 		    ZEROCOPY_VERSION_1));
3250 
3251 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3252 		    zc_ic->zerocopy_version;
3253 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3254 		    zc_ic->zerocopy_flags;
3255 
3256 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3257 	} else {
3258 		uint_t size;
3259 		uchar_t *rptr;
3260 
3261 		size = sizeof (dl_capability_req_t) +
3262 		    sizeof (dl_capability_sub_t) +
3263 		    sizeof (dl_capab_zerocopy_t);
3264 
3265 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3266 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3267 			    "could not enable zerocopy for %s (ENOMEM)\n",
3268 			    ill->ill_name);
3269 			return;
3270 		}
3271 
3272 		rptr = nmp->b_rptr;
3273 		/* initialize dl_capability_req_t */
3274 		oc = (dl_capability_req_t *)rptr;
3275 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3276 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3277 		    sizeof (dl_capab_zerocopy_t);
3278 		rptr += sizeof (dl_capability_req_t);
3279 
3280 		/* initialize dl_capability_sub_t */
3281 		bcopy(isub, rptr, sizeof (*isub));
3282 		rptr += sizeof (*isub);
3283 
3284 		/* initialize dl_capab_zerocopy_t */
3285 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3286 		*zc_oc = *zc_ic;
3287 
3288 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3289 		    "to enable zero-copy version %d\n", ill->ill_name,
3290 		    ZEROCOPY_VERSION_1));
3291 
3292 		/* set VMSAFE_MEM flag */
3293 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3294 
3295 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3296 		ill_dlpi_send(ill, nmp);
3297 	}
3298 }
3299 
3300 static void
3301 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3302 {
3303 	mblk_t *mp;
3304 	dl_capab_zerocopy_t *zerocopy_subcap;
3305 	dl_capability_sub_t *dl_subcap;
3306 	int size;
3307 
3308 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3309 		return;
3310 
3311 	ASSERT(ill->ill_zerocopy_capab != NULL);
3312 	/*
3313 	 * Clear the capability flag for Zero-copy but retain the
3314 	 * ill_zerocopy_capab structure since it's possible that another
3315 	 * thread is still referring to it.  The structure only gets
3316 	 * deallocated when we destroy the ill.
3317 	 */
3318 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3319 
3320 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3321 
3322 	mp = allocb(size, BPRI_HI);
3323 	if (mp == NULL) {
3324 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3325 		    "request to disable Zero-copy\n"));
3326 		return;
3327 	}
3328 
3329 	mp->b_wptr = mp->b_rptr + size;
3330 
3331 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3332 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3333 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3334 
3335 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3336 	zerocopy_subcap->zerocopy_version =
3337 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3338 	zerocopy_subcap->zerocopy_flags = 0;
3339 
3340 	if (*sc_mp != NULL)
3341 		linkb(*sc_mp, mp);
3342 	else
3343 		*sc_mp = mp;
3344 }
3345 
3346 /*
3347  * Consume a new-style hardware capabilities negotiation ack.
3348  * Called from ip_rput_dlpi_writer().
3349  */
3350 void
3351 ill_capability_ack(ill_t *ill, mblk_t *mp)
3352 {
3353 	dl_capability_ack_t *capp;
3354 	dl_capability_sub_t *subp, *endp;
3355 
3356 	if (ill->ill_capab_state == IDMS_INPROGRESS)
3357 		ill->ill_capab_state = IDMS_OK;
3358 
3359 	capp = (dl_capability_ack_t *)mp->b_rptr;
3360 
3361 	if (capp->dl_sub_length == 0)
3362 		/* no new-style capabilities */
3363 		return;
3364 
3365 	/* make sure the driver supplied correct dl_sub_length */
3366 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3367 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3368 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3369 		return;
3370 	}
3371 
3372 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3373 	/*
3374 	 * There are sub-capabilities. Process the ones we know about.
3375 	 * Loop until we don't have room for another sub-cap header..
3376 	 */
3377 	for (subp = SC(capp, capp->dl_sub_offset),
3378 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3379 	    subp <= endp;
3380 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3381 
3382 		switch (subp->dl_cap) {
3383 		case DL_CAPAB_ID_WRAPPER:
3384 			ill_capability_id_ack(ill, mp, subp);
3385 			break;
3386 		default:
3387 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3388 			break;
3389 		}
3390 	}
3391 #undef SC
3392 }
3393 
3394 /*
3395  * This routine is called to scan the fragmentation reassembly table for
3396  * the specified ILL for any packets that are starting to smell.
3397  * dead_interval is the maximum time in seconds that will be tolerated.  It
3398  * will either be the value specified in ip_g_frag_timeout, or zero if the
3399  * ILL is shutting down and it is time to blow everything off.
3400  *
3401  * It returns the number of seconds (as a time_t) that the next frag timer
3402  * should be scheduled for, 0 meaning that the timer doesn't need to be
3403  * re-started.  Note that the method of calculating next_timeout isn't
3404  * entirely accurate since time will flow between the time we grab
3405  * current_time and the time we schedule the next timeout.  This isn't a
3406  * big problem since this is the timer for sending an ICMP reassembly time
3407  * exceeded messages, and it doesn't have to be exactly accurate.
3408  *
3409  * This function is
3410  * sometimes called as writer, although this is not required.
3411  */
3412 time_t
3413 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3414 {
3415 	ipfb_t	*ipfb;
3416 	ipfb_t	*endp;
3417 	ipf_t	*ipf;
3418 	ipf_t	*ipfnext;
3419 	mblk_t	*mp;
3420 	time_t	current_time = gethrestime_sec();
3421 	time_t	next_timeout = 0;
3422 	uint32_t	hdr_length;
3423 	mblk_t	*send_icmp_head;
3424 	mblk_t	*send_icmp_head_v6;
3425 
3426 	ipfb = ill->ill_frag_hash_tbl;
3427 	if (ipfb == NULL)
3428 		return (B_FALSE);
3429 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3430 	/* Walk the frag hash table. */
3431 	for (; ipfb < endp; ipfb++) {
3432 		send_icmp_head = NULL;
3433 		send_icmp_head_v6 = NULL;
3434 		mutex_enter(&ipfb->ipfb_lock);
3435 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3436 			time_t frag_time = current_time - ipf->ipf_timestamp;
3437 			time_t frag_timeout;
3438 
3439 			if (frag_time < dead_interval) {
3440 				/*
3441 				 * There are some outstanding fragments
3442 				 * that will timeout later.  Make note of
3443 				 * the time so that we can reschedule the
3444 				 * next timeout appropriately.
3445 				 */
3446 				frag_timeout = dead_interval - frag_time;
3447 				if (next_timeout == 0 ||
3448 				    frag_timeout < next_timeout) {
3449 					next_timeout = frag_timeout;
3450 				}
3451 				break;
3452 			}
3453 			/* Time's up.  Get it out of here. */
3454 			hdr_length = ipf->ipf_nf_hdr_len;
3455 			ipfnext = ipf->ipf_hash_next;
3456 			if (ipfnext)
3457 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3458 			*ipf->ipf_ptphn = ipfnext;
3459 			mp = ipf->ipf_mp->b_cont;
3460 			for (; mp; mp = mp->b_cont) {
3461 				/* Extra points for neatness. */
3462 				IP_REASS_SET_START(mp, 0);
3463 				IP_REASS_SET_END(mp, 0);
3464 			}
3465 			mp = ipf->ipf_mp->b_cont;
3466 			ill->ill_frag_count -= ipf->ipf_count;
3467 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3468 			ipfb->ipfb_count -= ipf->ipf_count;
3469 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3470 			ipfb->ipfb_frag_pkts--;
3471 			/*
3472 			 * We do not send any icmp message from here because
3473 			 * we currently are holding the ipfb_lock for this
3474 			 * hash chain. If we try and send any icmp messages
3475 			 * from here we may end up via a put back into ip
3476 			 * trying to get the same lock, causing a recursive
3477 			 * mutex panic. Instead we build a list and send all
3478 			 * the icmp messages after we have dropped the lock.
3479 			 */
3480 			if (ill->ill_isv6) {
3481 				BUMP_MIB(ill->ill_ip6_mib, ipv6ReasmFails);
3482 				if (hdr_length != 0) {
3483 					mp->b_next = send_icmp_head_v6;
3484 					send_icmp_head_v6 = mp;
3485 				} else {
3486 					freemsg(mp);
3487 				}
3488 			} else {
3489 				BUMP_MIB(&ip_mib, ipReasmFails);
3490 				if (hdr_length != 0) {
3491 					mp->b_next = send_icmp_head;
3492 					send_icmp_head = mp;
3493 				} else {
3494 					freemsg(mp);
3495 				}
3496 			}
3497 			freeb(ipf->ipf_mp);
3498 		}
3499 		mutex_exit(&ipfb->ipfb_lock);
3500 		/*
3501 		 * Now need to send any icmp messages that we delayed from
3502 		 * above.
3503 		 */
3504 		while (send_icmp_head_v6 != NULL) {
3505 			mp = send_icmp_head_v6;
3506 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3507 			mp->b_next = NULL;
3508 			icmp_time_exceeded_v6(ill->ill_wq, mp,
3509 			    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE, B_FALSE);
3510 		}
3511 		while (send_icmp_head != NULL) {
3512 			mp = send_icmp_head;
3513 			send_icmp_head = send_icmp_head->b_next;
3514 			mp->b_next = NULL;
3515 			icmp_time_exceeded(ill->ill_wq, mp,
3516 			    ICMP_REASSEMBLY_TIME_EXCEEDED);
3517 		}
3518 	}
3519 	/*
3520 	 * A non-dying ILL will use the return value to decide whether to
3521 	 * restart the frag timer, and for how long.
3522 	 */
3523 	return (next_timeout);
3524 }
3525 
3526 /*
3527  * This routine is called when the approximate count of mblk memory used
3528  * for the specified ILL has exceeded max_count.
3529  */
3530 void
3531 ill_frag_prune(ill_t *ill, uint_t max_count)
3532 {
3533 	ipfb_t	*ipfb;
3534 	ipf_t	*ipf;
3535 	size_t	count;
3536 
3537 	/*
3538 	 * If we are here within ip_min_frag_prune_time msecs remove
3539 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3540 	 * ill_frag_free_num_pkts.
3541 	 */
3542 	mutex_enter(&ill->ill_lock);
3543 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3544 	    (ip_min_frag_prune_time != 0 ?
3545 	    ip_min_frag_prune_time : msec_per_tick)) {
3546 
3547 		ill->ill_frag_free_num_pkts++;
3548 
3549 	} else {
3550 		ill->ill_frag_free_num_pkts = 0;
3551 	}
3552 	ill->ill_last_frag_clean_time = lbolt;
3553 	mutex_exit(&ill->ill_lock);
3554 
3555 	/*
3556 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3557 	 */
3558 	if (ill->ill_frag_free_num_pkts != 0) {
3559 		int ix;
3560 
3561 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3562 			ipfb = &ill->ill_frag_hash_tbl[ix];
3563 			mutex_enter(&ipfb->ipfb_lock);
3564 			if (ipfb->ipfb_ipf != NULL) {
3565 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3566 				    ill->ill_frag_free_num_pkts);
3567 			}
3568 			mutex_exit(&ipfb->ipfb_lock);
3569 		}
3570 	}
3571 	/*
3572 	 * While the reassembly list for this ILL is too big, prune a fragment
3573 	 * queue by age, oldest first.  Note that the per ILL count is
3574 	 * approximate, while the per frag hash bucket counts are accurate.
3575 	 */
3576 	while (ill->ill_frag_count > max_count) {
3577 		int	ix;
3578 		ipfb_t	*oipfb = NULL;
3579 		uint_t	oldest = UINT_MAX;
3580 
3581 		count = 0;
3582 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3583 			ipfb = &ill->ill_frag_hash_tbl[ix];
3584 			mutex_enter(&ipfb->ipfb_lock);
3585 			ipf = ipfb->ipfb_ipf;
3586 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3587 				oldest = ipf->ipf_gen;
3588 				oipfb = ipfb;
3589 			}
3590 			count += ipfb->ipfb_count;
3591 			mutex_exit(&ipfb->ipfb_lock);
3592 		}
3593 		/* Refresh the per ILL count */
3594 		ill->ill_frag_count = count;
3595 		if (oipfb == NULL) {
3596 			ill->ill_frag_count = 0;
3597 			break;
3598 		}
3599 		if (count <= max_count)
3600 			return;	/* Somebody beat us to it, nothing to do */
3601 		mutex_enter(&oipfb->ipfb_lock);
3602 		ipf = oipfb->ipfb_ipf;
3603 		if (ipf != NULL) {
3604 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3605 		}
3606 		mutex_exit(&oipfb->ipfb_lock);
3607 	}
3608 }
3609 
3610 /*
3611  * free 'free_cnt' fragmented packets starting at ipf.
3612  */
3613 void
3614 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3615 {
3616 	size_t	count;
3617 	mblk_t	*mp;
3618 	mblk_t	*tmp;
3619 	ipf_t **ipfp = ipf->ipf_ptphn;
3620 
3621 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3622 	ASSERT(ipfp != NULL);
3623 	ASSERT(ipf != NULL);
3624 
3625 	while (ipf != NULL && free_cnt-- > 0) {
3626 		count = ipf->ipf_count;
3627 		mp = ipf->ipf_mp;
3628 		ipf = ipf->ipf_hash_next;
3629 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3630 			IP_REASS_SET_START(tmp, 0);
3631 			IP_REASS_SET_END(tmp, 0);
3632 		}
3633 		ill->ill_frag_count -= count;
3634 		ASSERT(ipfb->ipfb_count >= count);
3635 		ipfb->ipfb_count -= count;
3636 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3637 		ipfb->ipfb_frag_pkts--;
3638 		freemsg(mp);
3639 		BUMP_MIB(&ip_mib, ipReasmFails);
3640 	}
3641 
3642 	if (ipf)
3643 		ipf->ipf_ptphn = ipfp;
3644 	ipfp[0] = ipf;
3645 }
3646 
3647 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3648 	"obsolete and may be removed in a future release of Solaris.  Use " \
3649 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3650 
3651 /*
3652  * For obsolete per-interface forwarding configuration;
3653  * called in response to ND_GET.
3654  */
3655 /* ARGSUSED */
3656 static int
3657 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3658 {
3659 	ill_t *ill = (ill_t *)cp;
3660 
3661 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3662 
3663 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3664 	return (0);
3665 }
3666 
3667 /*
3668  * For obsolete per-interface forwarding configuration;
3669  * called in response to ND_SET.
3670  */
3671 /* ARGSUSED */
3672 static int
3673 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3674     cred_t *ioc_cr)
3675 {
3676 	long value;
3677 	int retval;
3678 
3679 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3680 
3681 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3682 	    value < 0 || value > 1) {
3683 		return (EINVAL);
3684 	}
3685 
3686 	rw_enter(&ill_g_lock, RW_READER);
3687 	retval = ill_forward_set(q, mp, (value != 0), cp);
3688 	rw_exit(&ill_g_lock);
3689 	return (retval);
3690 }
3691 
3692 /*
3693  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3694  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3695  * up RTS_IFINFO routing socket messages for each interface whose flags we
3696  * change.
3697  */
3698 /* ARGSUSED */
3699 int
3700 ill_forward_set(queue_t *q, mblk_t *mp, boolean_t enable, caddr_t cp)
3701 {
3702 	ill_t *ill = (ill_t *)cp;
3703 	ill_group_t *illgrp;
3704 
3705 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ill_g_lock));
3706 
3707 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3708 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)) ||
3709 	    (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK))
3710 		return (EINVAL);
3711 
3712 	/*
3713 	 * If the ill is in an IPMP group, set the forwarding policy on all
3714 	 * members of the group to the same value.
3715 	 */
3716 	illgrp = ill->ill_group;
3717 	if (illgrp != NULL) {
3718 		ill_t *tmp_ill;
3719 
3720 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3721 		    tmp_ill = tmp_ill->ill_group_next) {
3722 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3723 			    (enable ? "Enabling" : "Disabling"),
3724 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3725 			    tmp_ill->ill_name));
3726 			mutex_enter(&tmp_ill->ill_lock);
3727 			if (enable)
3728 				tmp_ill->ill_flags |= ILLF_ROUTER;
3729 			else
3730 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3731 			mutex_exit(&tmp_ill->ill_lock);
3732 			if (tmp_ill->ill_isv6)
3733 				ill_set_nce_router_flags(tmp_ill, enable);
3734 			/* Notify routing socket listeners of this change. */
3735 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3736 		}
3737 	} else {
3738 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3739 		    (enable ? "Enabling" : "Disabling"),
3740 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3741 		mutex_enter(&ill->ill_lock);
3742 		if (enable)
3743 			ill->ill_flags |= ILLF_ROUTER;
3744 		else
3745 			ill->ill_flags &= ~ILLF_ROUTER;
3746 		mutex_exit(&ill->ill_lock);
3747 		if (ill->ill_isv6)
3748 			ill_set_nce_router_flags(ill, enable);
3749 		/* Notify routing socket listeners of this change. */
3750 		ip_rts_ifmsg(ill->ill_ipif);
3751 	}
3752 
3753 	return (0);
3754 }
3755 
3756 /*
3757  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3758  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3759  * set or clear.
3760  */
3761 static void
3762 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3763 {
3764 	ipif_t *ipif;
3765 	nce_t *nce;
3766 
3767 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3768 		nce = ndp_lookup(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3769 		if (nce != NULL) {
3770 			mutex_enter(&nce->nce_lock);
3771 			if (enable)
3772 				nce->nce_flags |= NCE_F_ISROUTER;
3773 			else
3774 				nce->nce_flags &= ~NCE_F_ISROUTER;
3775 			mutex_exit(&nce->nce_lock);
3776 			NCE_REFRELE(nce);
3777 		}
3778 	}
3779 }
3780 
3781 /*
3782  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3783  * for this ill.  Make sure the v6/v4 question has been answered about this
3784  * ill.  The creation of this ndd variable is only for backwards compatibility.
3785  * The preferred way to control per-interface IP forwarding is through the
3786  * ILLF_ROUTER interface flag.
3787  */
3788 static int
3789 ill_set_ndd_name(ill_t *ill)
3790 {
3791 	char *suffix;
3792 
3793 	ASSERT(IAM_WRITER_ILL(ill));
3794 
3795 	if (ill->ill_isv6)
3796 		suffix = ipv6_forward_suffix;
3797 	else
3798 		suffix = ipv4_forward_suffix;
3799 
3800 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
3801 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
3802 	/*
3803 	 * Copies over the '\0'.
3804 	 * Note that strlen(suffix) is always bounded.
3805 	 */
3806 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
3807 	    strlen(suffix) + 1);
3808 
3809 	/*
3810 	 * Use of the nd table requires holding the reader lock.
3811 	 * Modifying the nd table thru nd_load/nd_unload requires
3812 	 * the writer lock.
3813 	 */
3814 	rw_enter(&ip_g_nd_lock, RW_WRITER);
3815 	if (!nd_load(&ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
3816 	    nd_ill_forward_set, (caddr_t)ill)) {
3817 		/*
3818 		 * If the nd_load failed, it only meant that it could not
3819 		 * allocate a new bunch of room for further NDD expansion.
3820 		 * Because of that, the ill_ndd_name will be set to 0, and
3821 		 * this interface is at the mercy of the global ip_forwarding
3822 		 * variable.
3823 		 */
3824 		rw_exit(&ip_g_nd_lock);
3825 		ill->ill_ndd_name = NULL;
3826 		return (ENOMEM);
3827 	}
3828 	rw_exit(&ip_g_nd_lock);
3829 	return (0);
3830 }
3831 
3832 /*
3833  * Intializes the context structure and returns the first ill in the list
3834  * cuurently start_list and end_list can have values:
3835  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
3836  * IP_V4_G_HEAD		Traverse IPV4 list only.
3837  * IP_V6_G_HEAD		Traverse IPV6 list only.
3838  */
3839 
3840 /*
3841  * We don't check for CONDEMNED ills here. Caller must do that if
3842  * necessary under the ill lock.
3843  */
3844 ill_t *
3845 ill_first(int start_list, int end_list, ill_walk_context_t *ctx)
3846 {
3847 	ill_if_t *ifp;
3848 	ill_t *ill;
3849 	avl_tree_t *avl_tree;
3850 
3851 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
3852 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
3853 
3854 	/*
3855 	 * setup the lists to search
3856 	 */
3857 	if (end_list != MAX_G_HEADS) {
3858 		ctx->ctx_current_list = start_list;
3859 		ctx->ctx_last_list = end_list;
3860 	} else {
3861 		ctx->ctx_last_list = MAX_G_HEADS - 1;
3862 		ctx->ctx_current_list = 0;
3863 	}
3864 
3865 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
3866 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list);
3867 		if (ifp != (ill_if_t *)
3868 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list)) {
3869 			avl_tree = &ifp->illif_avl_by_ppa;
3870 			ill = avl_first(avl_tree);
3871 			/*
3872 			 * ill is guaranteed to be non NULL or ifp should have
3873 			 * not existed.
3874 			 */
3875 			ASSERT(ill != NULL);
3876 			return (ill);
3877 		}
3878 		ctx->ctx_current_list++;
3879 	}
3880 
3881 	return (NULL);
3882 }
3883 
3884 /*
3885  * returns the next ill in the list. ill_first() must have been called
3886  * before calling ill_next() or bad things will happen.
3887  */
3888 
3889 /*
3890  * We don't check for CONDEMNED ills here. Caller must do that if
3891  * necessary under the ill lock.
3892  */
3893 ill_t *
3894 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
3895 {
3896 	ill_if_t *ifp;
3897 	ill_t *ill;
3898 
3899 
3900 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
3901 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
3902 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list));
3903 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
3904 	    AVL_AFTER)) != NULL) {
3905 		return (ill);
3906 	}
3907 
3908 	/* goto next ill_ifp in the list. */
3909 	ifp = lastill->ill_ifptr->illif_next;
3910 
3911 	/* make sure not at end of circular list */
3912 	while (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list)) {
3913 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
3914 			return (NULL);
3915 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list);
3916 	}
3917 
3918 	return (avl_first(&ifp->illif_avl_by_ppa));
3919 }
3920 
3921 /*
3922  * Check interface name for correct format which is name+ppa.
3923  * name can contain characters and digits, the right most digits
3924  * make up the ppa number. use of octal is not allowed, name must contain
3925  * a ppa, return pointer to the start of ppa.
3926  * In case of error return NULL.
3927  */
3928 static char *
3929 ill_get_ppa_ptr(char *name)
3930 {
3931 	int namelen = mi_strlen(name);
3932 
3933 	int len = namelen;
3934 
3935 	name += len;
3936 	while (len > 0) {
3937 		name--;
3938 		if (*name < '0' || *name > '9')
3939 			break;
3940 		len--;
3941 	}
3942 
3943 	/* empty string, all digits, or no trailing digits */
3944 	if (len == 0 || len == (int)namelen)
3945 		return (NULL);
3946 
3947 	name++;
3948 	/* check for attempted use of octal */
3949 	if (*name == '0' && len != (int)namelen - 1)
3950 		return (NULL);
3951 	return (name);
3952 }
3953 
3954 /*
3955  * use avl tree to locate the ill.
3956  */
3957 static ill_t *
3958 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
3959     ipsq_func_t func, int *error)
3960 {
3961 	char *ppa_ptr = NULL;
3962 	int len;
3963 	uint_t ppa;
3964 	ill_t *ill = NULL;
3965 	ill_if_t *ifp;
3966 	int list;
3967 	ipsq_t *ipsq;
3968 
3969 	if (error != NULL)
3970 		*error = 0;
3971 
3972 	/*
3973 	 * get ppa ptr
3974 	 */
3975 	if (isv6)
3976 		list = IP_V6_G_HEAD;
3977 	else
3978 		list = IP_V4_G_HEAD;
3979 
3980 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
3981 		if (error != NULL)
3982 			*error = ENXIO;
3983 		return (NULL);
3984 	}
3985 
3986 	len = ppa_ptr - name + 1;
3987 
3988 	ppa = stoi(&ppa_ptr);
3989 
3990 	ifp = IP_VX_ILL_G_LIST(list);
3991 
3992 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list)) {
3993 		/*
3994 		 * match is done on len - 1 as the name is not null
3995 		 * terminated it contains ppa in addition to the interface
3996 		 * name.
3997 		 */
3998 		if ((ifp->illif_name_len == len) &&
3999 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4000 			break;
4001 		} else {
4002 			ifp = ifp->illif_next;
4003 		}
4004 	}
4005 
4006 
4007 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list)) {
4008 		/*
4009 		 * Even the interface type does not exist.
4010 		 */
4011 		if (error != NULL)
4012 			*error = ENXIO;
4013 		return (NULL);
4014 	}
4015 
4016 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4017 	if (ill != NULL) {
4018 		/*
4019 		 * The block comment at the start of ipif_down
4020 		 * explains the use of the macros used below
4021 		 */
4022 		GRAB_CONN_LOCK(q);
4023 		mutex_enter(&ill->ill_lock);
4024 		if (ILL_CAN_LOOKUP(ill)) {
4025 			ill_refhold_locked(ill);
4026 			mutex_exit(&ill->ill_lock);
4027 			RELEASE_CONN_LOCK(q);
4028 			return (ill);
4029 		} else if (ILL_CAN_WAIT(ill, q)) {
4030 			ipsq = ill->ill_phyint->phyint_ipsq;
4031 			mutex_enter(&ipsq->ipsq_lock);
4032 			mutex_exit(&ill->ill_lock);
4033 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4034 			mutex_exit(&ipsq->ipsq_lock);
4035 			RELEASE_CONN_LOCK(q);
4036 			*error = EINPROGRESS;
4037 			return (NULL);
4038 		}
4039 		mutex_exit(&ill->ill_lock);
4040 		RELEASE_CONN_LOCK(q);
4041 	}
4042 	if (error != NULL)
4043 		*error = ENXIO;
4044 	return (NULL);
4045 }
4046 
4047 /*
4048  * comparison function for use with avl.
4049  */
4050 static int
4051 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4052 {
4053 	uint_t ppa;
4054 	uint_t ill_ppa;
4055 
4056 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4057 
4058 	ppa = *((uint_t *)ppa_ptr);
4059 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4060 	/*
4061 	 * We want the ill with the lowest ppa to be on the
4062 	 * top.
4063 	 */
4064 	if (ill_ppa < ppa)
4065 		return (1);
4066 	if (ill_ppa > ppa)
4067 		return (-1);
4068 	return (0);
4069 }
4070 
4071 /*
4072  * remove an interface type from the global list.
4073  */
4074 static void
4075 ill_delete_interface_type(ill_if_t *interface)
4076 {
4077 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
4078 
4079 	ASSERT(interface != NULL);
4080 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4081 
4082 	avl_destroy(&interface->illif_avl_by_ppa);
4083 	if (interface->illif_ppa_arena != NULL)
4084 		vmem_destroy(interface->illif_ppa_arena);
4085 
4086 	remque(interface);
4087 
4088 	mi_free(interface);
4089 }
4090 
4091 /*
4092  * remove ill from the global list.
4093  */
4094 static void
4095 ill_glist_delete(ill_t *ill)
4096 {
4097 	if (ill == NULL)
4098 		return;
4099 
4100 	rw_enter(&ill_g_lock, RW_WRITER);
4101 	/*
4102 	 * If the ill was never inserted into the AVL tree
4103 	 * we skip the if branch.
4104 	 */
4105 	if (ill->ill_ifptr != NULL) {
4106 		/*
4107 		 * remove from AVL tree and free ppa number
4108 		 */
4109 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4110 
4111 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4112 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4113 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4114 		}
4115 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4116 			ill_delete_interface_type(ill->ill_ifptr);
4117 		}
4118 
4119 		/*
4120 		 * Indicate ill is no longer in the list.
4121 		 */
4122 		ill->ill_ifptr = NULL;
4123 		ill->ill_name_length = 0;
4124 		ill->ill_name[0] = '\0';
4125 		ill->ill_ppa = UINT_MAX;
4126 	}
4127 	ill_phyint_free(ill);
4128 	rw_exit(&ill_g_lock);
4129 }
4130 
4131 /*
4132  * allocate a ppa, if the number of plumbed interfaces of this type are
4133  * less than ill_no_arena do a linear search to find a unused ppa.
4134  * When the number goes beyond ill_no_arena switch to using an arena.
4135  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4136  * is the return value for an error condition, so allocation starts at one
4137  * and is decremented by one.
4138  */
4139 static int
4140 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4141 {
4142 	ill_t *tmp_ill;
4143 	uint_t start, end;
4144 	int ppa;
4145 
4146 	if (ifp->illif_ppa_arena == NULL &&
4147 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4148 		/*
4149 		 * Create an arena.
4150 		 */
4151 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4152 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4153 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4154 			/* allocate what has already been assigned */
4155 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4156 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4157 		    tmp_ill, AVL_AFTER)) {
4158 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4159 			    1,		/* size */
4160 			    1,		/* align/quantum */
4161 			    0,		/* phase */
4162 			    0,		/* nocross */
4163 		(void *)((uintptr_t)tmp_ill->ill_ppa + 1), /* minaddr */
4164 		(void *)((uintptr_t)tmp_ill->ill_ppa + 2), /* maxaddr */
4165 			    VM_NOSLEEP|VM_FIRSTFIT);
4166 			if (ppa == 0) {
4167 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4168 				    " failed while switching"));
4169 				vmem_destroy(ifp->illif_ppa_arena);
4170 				ifp->illif_ppa_arena = NULL;
4171 				break;
4172 			}
4173 		}
4174 	}
4175 
4176 	if (ifp->illif_ppa_arena != NULL) {
4177 		if (ill->ill_ppa == UINT_MAX) {
4178 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4179 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4180 			if (ppa == 0)
4181 				return (EAGAIN);
4182 			ill->ill_ppa = --ppa;
4183 		} else {
4184 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4185 			    1, 		/* size */
4186 			    1, 		/* align/quantum */
4187 			    0, 		/* phase */
4188 			    0, 		/* nocross */
4189 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4190 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4191 			    VM_NOSLEEP|VM_FIRSTFIT);
4192 			/*
4193 			 * Most likely the allocation failed because
4194 			 * the requested ppa was in use.
4195 			 */
4196 			if (ppa == 0)
4197 				return (EEXIST);
4198 		}
4199 		return (0);
4200 	}
4201 
4202 	/*
4203 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4204 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4205 	 */
4206 	if (ill->ill_ppa == UINT_MAX) {
4207 		end = UINT_MAX - 1;
4208 		start = 0;
4209 	} else {
4210 		end = start = ill->ill_ppa;
4211 	}
4212 
4213 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4214 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4215 		if (start++ >= end) {
4216 			if (ill->ill_ppa == UINT_MAX)
4217 				return (EAGAIN);
4218 			else
4219 				return (EEXIST);
4220 		}
4221 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4222 	}
4223 	ill->ill_ppa = start;
4224 	return (0);
4225 }
4226 
4227 /*
4228  * Insert ill into the list of configured ill's. Once this function completes,
4229  * the ill is globally visible and is available through lookups. More precisely
4230  * this happens after the caller drops the ill_g_lock.
4231  */
4232 static int
4233 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4234 {
4235 	ill_if_t *ill_interface;
4236 	avl_index_t where = 0;
4237 	int error;
4238 	int name_length;
4239 	int index;
4240 	boolean_t check_length = B_FALSE;
4241 
4242 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
4243 
4244 	name_length = mi_strlen(name) + 1;
4245 
4246 	if (isv6)
4247 		index = IP_V6_G_HEAD;
4248 	else
4249 		index = IP_V4_G_HEAD;
4250 
4251 	ill_interface = IP_VX_ILL_G_LIST(index);
4252 	/*
4253 	 * Search for interface type based on name
4254 	 */
4255 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index)) {
4256 		if ((ill_interface->illif_name_len == name_length) &&
4257 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4258 			break;
4259 		}
4260 		ill_interface = ill_interface->illif_next;
4261 	}
4262 
4263 	/*
4264 	 * Interface type not found, create one.
4265 	 */
4266 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index)) {
4267 
4268 		ill_g_head_t ghead;
4269 
4270 		/*
4271 		 * allocate ill_if_t structure
4272 		 */
4273 
4274 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4275 		if (ill_interface == NULL) {
4276 			return (ENOMEM);
4277 		}
4278 
4279 
4280 
4281 		(void) strcpy(ill_interface->illif_name, name);
4282 		ill_interface->illif_name_len = name_length;
4283 
4284 		avl_create(&ill_interface->illif_avl_by_ppa,
4285 		    ill_compare_ppa, sizeof (ill_t),
4286 		    offsetof(struct ill_s, ill_avl_byppa));
4287 
4288 		/*
4289 		 * link the structure in the back to maintain order
4290 		 * of configuration for ifconfig output.
4291 		 */
4292 		ghead = ill_g_heads[index];
4293 		insque(ill_interface, ghead.ill_g_list_tail);
4294 
4295 	}
4296 
4297 	if (ill->ill_ppa == UINT_MAX)
4298 		check_length = B_TRUE;
4299 
4300 	error = ill_alloc_ppa(ill_interface, ill);
4301 	if (error != 0) {
4302 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4303 			ill_delete_interface_type(ill->ill_ifptr);
4304 		return (error);
4305 	}
4306 
4307 	/*
4308 	 * When the ppa is choosen by the system, check that there is
4309 	 * enough space to insert ppa. if a specific ppa was passed in this
4310 	 * check is not required as the interface name passed in will have
4311 	 * the right ppa in it.
4312 	 */
4313 	if (check_length) {
4314 		/*
4315 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4316 		 */
4317 		char buf[sizeof (uint_t) * 3];
4318 
4319 		/*
4320 		 * convert ppa to string to calculate the amount of space
4321 		 * required for it in the name.
4322 		 */
4323 		numtos(ill->ill_ppa, buf);
4324 
4325 		/* Do we have enough space to insert ppa ? */
4326 
4327 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4328 			/* Free ppa and interface type struct */
4329 			if (ill_interface->illif_ppa_arena != NULL) {
4330 				vmem_free(ill_interface->illif_ppa_arena,
4331 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4332 			}
4333 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4334 			    0) {
4335 				ill_delete_interface_type(ill->ill_ifptr);
4336 			}
4337 
4338 			return (EINVAL);
4339 		}
4340 	}
4341 
4342 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4343 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4344 
4345 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4346 	    &where);
4347 	ill->ill_ifptr = ill_interface;
4348 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4349 
4350 	ill_phyint_reinit(ill);
4351 	return (0);
4352 }
4353 
4354 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4355 static boolean_t
4356 ipsq_init(ill_t *ill)
4357 {
4358 	ipsq_t  *ipsq;
4359 
4360 	/* Init the ipsq and impicitly enter as writer */
4361 	ill->ill_phyint->phyint_ipsq =
4362 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4363 	if (ill->ill_phyint->phyint_ipsq == NULL)
4364 		return (B_FALSE);
4365 	ipsq = ill->ill_phyint->phyint_ipsq;
4366 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4367 	ill->ill_phyint->phyint_ipsq_next = NULL;
4368 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4369 	ipsq->ipsq_refs = 1;
4370 	ipsq->ipsq_writer = curthread;
4371 	ipsq->ipsq_reentry_cnt = 1;
4372 #ifdef ILL_DEBUG
4373 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack, IP_STACK_DEPTH);
4374 #endif
4375 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4376 	return (B_TRUE);
4377 }
4378 
4379 /*
4380  * ill_init is called by ip_open when a device control stream is opened.
4381  * It does a few initializations, and shoots a DL_INFO_REQ message down
4382  * to the driver.  The response is later picked up in ip_rput_dlpi and
4383  * used to set up default mechanisms for talking to the driver.  (Always
4384  * called as writer.)
4385  *
4386  * If this function returns error, ip_open will call ip_close which in
4387  * turn will call ill_delete to clean up any memory allocated here that
4388  * is not yet freed.
4389  */
4390 int
4391 ill_init(queue_t *q, ill_t *ill)
4392 {
4393 	int	count;
4394 	dl_info_req_t	*dlir;
4395 	mblk_t	*info_mp;
4396 	uchar_t *frag_ptr;
4397 
4398 	/*
4399 	 * The ill is initialized to zero by mi_alloc*(). In addition
4400 	 * some fields already contain valid values, initialized in
4401 	 * ip_open(), before we reach here.
4402 	 */
4403 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4404 
4405 	ill->ill_rq = q;
4406 	ill->ill_wq = WR(q);
4407 
4408 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4409 	    BPRI_HI);
4410 	if (info_mp == NULL)
4411 		return (ENOMEM);
4412 
4413 	/*
4414 	 * Allocate sufficient space to contain our fragment hash table and
4415 	 * the device name.
4416 	 */
4417 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4418 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4419 	if (frag_ptr == NULL) {
4420 		freemsg(info_mp);
4421 		return (ENOMEM);
4422 	}
4423 	ill->ill_frag_ptr = frag_ptr;
4424 	ill->ill_frag_free_num_pkts = 0;
4425 	ill->ill_last_frag_clean_time = 0;
4426 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4427 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4428 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4429 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4430 		    NULL, MUTEX_DEFAULT, NULL);
4431 	}
4432 
4433 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4434 	if (ill->ill_phyint == NULL) {
4435 		freemsg(info_mp);
4436 		mi_free(frag_ptr);
4437 		return (ENOMEM);
4438 	}
4439 
4440 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4441 	/*
4442 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4443 	 * at this point because of the following reason. If we can't
4444 	 * enter the ipsq at some point and cv_wait, the writer that
4445 	 * wakes us up tries to locate us using the list of all phyints
4446 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4447 	 * If we don't set it now, we risk a missed wakeup.
4448 	 */
4449 	ill->ill_phyint->phyint_illv4 = ill;
4450 	ill->ill_ppa = UINT_MAX;
4451 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4452 
4453 	if (!ipsq_init(ill)) {
4454 		freemsg(info_mp);
4455 		mi_free(frag_ptr);
4456 		mi_free(ill->ill_phyint);
4457 		return (ENOMEM);
4458 	}
4459 
4460 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4461 
4462 
4463 	/* Frag queue limit stuff */
4464 	ill->ill_frag_count = 0;
4465 	ill->ill_ipf_gen = 0;
4466 
4467 	ill->ill_global_timer = INFINITY;
4468 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
4469 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4470 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4471 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4472 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4473 
4474 	/*
4475 	 * Initialize IPv6 configuration variables.  The IP module is always
4476 	 * opened as an IPv4 module.  Instead tracking down the cases where
4477 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4478 	 * here for convenience, this has no effect until the ill is set to do
4479 	 * IPv6.
4480 	 */
4481 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4482 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4483 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4484 	ill->ill_max_buf = ND_MAX_Q;
4485 	ill->ill_refcnt = 0;
4486 
4487 	/* Send down the Info Request to the driver. */
4488 	info_mp->b_datap->db_type = M_PCPROTO;
4489 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4490 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4491 	dlir->dl_primitive = DL_INFO_REQ;
4492 
4493 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4494 
4495 	qprocson(q);
4496 	ill_dlpi_send(ill, info_mp);
4497 
4498 	return (0);
4499 }
4500 
4501 /*
4502  * ill_dls_info
4503  * creates datalink socket info from the device.
4504  */
4505 int
4506 ill_dls_info(struct sockaddr_dl *sdl, ipif_t *ipif)
4507 {
4508 	size_t	length;
4509 	ill_t	*ill = ipif->ipif_ill;
4510 
4511 	sdl->sdl_family = AF_LINK;
4512 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4513 	sdl->sdl_type = ipif->ipif_type;
4514 	(void) ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4515 	length = mi_strlen(sdl->sdl_data);
4516 	ASSERT(length < 256);
4517 	sdl->sdl_nlen = (uchar_t)length;
4518 	sdl->sdl_alen = ill->ill_phys_addr_length;
4519 	mutex_enter(&ill->ill_lock);
4520 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL) {
4521 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[length],
4522 		    ill->ill_phys_addr_length);
4523 	}
4524 	mutex_exit(&ill->ill_lock);
4525 	sdl->sdl_slen = 0;
4526 	return (sizeof (struct sockaddr_dl));
4527 }
4528 
4529 /*
4530  * ill_xarp_info
4531  * creates xarp info from the device.
4532  */
4533 static int
4534 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4535 {
4536 	sdl->sdl_family = AF_LINK;
4537 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4538 	sdl->sdl_type = ill->ill_type;
4539 	(void) ipif_get_name(ill->ill_ipif, sdl->sdl_data,
4540 	    sizeof (sdl->sdl_data));
4541 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4542 	sdl->sdl_alen = ill->ill_phys_addr_length;
4543 	sdl->sdl_slen = 0;
4544 	return (sdl->sdl_nlen);
4545 }
4546 
4547 static int
4548 loopback_kstat_update(kstat_t *ksp, int rw)
4549 {
4550 	kstat_named_t *kn = KSTAT_NAMED_PTR(ksp);
4551 
4552 	if (rw == KSTAT_WRITE)
4553 		return (EACCES);
4554 	kn[0].value.ui32 = loopback_packets;
4555 	kn[1].value.ui32 = loopback_packets;
4556 	return (0);
4557 }
4558 
4559 
4560 /*
4561  * Has ifindex been plumbed already.
4562  */
4563 static boolean_t
4564 phyint_exists(uint_t index)
4565 {
4566 	phyint_t *phyi;
4567 
4568 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
4569 	/*
4570 	 * Indexes are stored in the phyint - a common structure
4571 	 * to both IPv4 and IPv6.
4572 	 */
4573 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
4574 	    (void *) &index, NULL);
4575 	return (phyi != NULL);
4576 }
4577 
4578 /*
4579  * Assign a unique interface index for the phyint.
4580  */
4581 static boolean_t
4582 phyint_assign_ifindex(phyint_t *phyi)
4583 {
4584 	uint_t starting_index;
4585 
4586 	ASSERT(phyi->phyint_ifindex == 0);
4587 	if (!ill_index_wrap) {
4588 		phyi->phyint_ifindex = ill_index++;
4589 		if (ill_index == 0) {
4590 			/* Reached the uint_t limit Next time wrap  */
4591 			ill_index_wrap = B_TRUE;
4592 		}
4593 		return (B_TRUE);
4594 	}
4595 
4596 	/*
4597 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4598 	 * at this point and don't want to call any function that attempts
4599 	 * to get the lock again.
4600 	 */
4601 	starting_index = ill_index++;
4602 	for (; ill_index != starting_index; ill_index++) {
4603 		if (ill_index != 0 && !phyint_exists(ill_index)) {
4604 			/* found unused index - use it */
4605 			phyi->phyint_ifindex = ill_index;
4606 			return (B_TRUE);
4607 		}
4608 	}
4609 
4610 	/*
4611 	 * all interface indicies are inuse.
4612 	 */
4613 	return (B_FALSE);
4614 }
4615 
4616 /*
4617  * Return a pointer to the ill which matches the supplied name.  Note that
4618  * the ill name length includes the null termination character.  (May be
4619  * called as writer.)
4620  * If do_alloc and the interface is "lo0" it will be automatically created.
4621  * Cannot bump up reference on condemned ills. So dup detect can't be done
4622  * using this func.
4623  */
4624 ill_t *
4625 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4626     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc)
4627 {
4628 	ill_t	*ill;
4629 	ipif_t	*ipif;
4630 	kstat_named_t	*kn;
4631 	boolean_t isloopback;
4632 	ipsq_t *old_ipsq;
4633 
4634 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4635 
4636 	rw_enter(&ill_g_lock, RW_READER);
4637 	ill = ill_find_by_name(name, isv6, q, mp, func, error);
4638 	rw_exit(&ill_g_lock);
4639 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4640 		return (ill);
4641 
4642 	/*
4643 	 * Couldn't find it.  Does this happen to be a lookup for the
4644 	 * loopback device and are we allowed to allocate it?
4645 	 */
4646 	if (!isloopback || !do_alloc)
4647 		return (NULL);
4648 
4649 	rw_enter(&ill_g_lock, RW_WRITER);
4650 
4651 	ill = ill_find_by_name(name, isv6, q, mp, func, error);
4652 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4653 		rw_exit(&ill_g_lock);
4654 		return (ill);
4655 	}
4656 
4657 	/* Create the loopback device on demand */
4658 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4659 	    sizeof (ipif_loopback_name), BPRI_MED));
4660 	if (ill == NULL)
4661 		goto done;
4662 
4663 	*ill = ill_null;
4664 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4665 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4666 	if (ill->ill_phyint == NULL)
4667 		goto done;
4668 
4669 	if (isv6)
4670 		ill->ill_phyint->phyint_illv6 = ill;
4671 	else
4672 		ill->ill_phyint->phyint_illv4 = ill;
4673 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4674 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4675 	/* Add room for tcp+ip headers */
4676 	if (isv6) {
4677 		ill->ill_isv6 = B_TRUE;
4678 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4679 		if (!ill_allocate_mibs(ill))
4680 			goto done;
4681 	} else {
4682 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4683 	}
4684 	ill->ill_max_mtu = ill->ill_max_frag;
4685 	/*
4686 	 * ipif_loopback_name can't be pointed at directly because its used
4687 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4688 	 * from the glist, ill_glist_delete() sets the first character of
4689 	 * ill_name to '\0'.
4690 	 */
4691 	ill->ill_name = (char *)ill + sizeof (*ill);
4692 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4693 	ill->ill_name_length = sizeof (ipif_loopback_name);
4694 	/* Set ill_name_set for ill_phyint_reinit to work properly */
4695 
4696 	ill->ill_global_timer = INFINITY;
4697 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
4698 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4699 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4700 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4701 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4702 
4703 	/* No resolver here. */
4704 	ill->ill_net_type = IRE_LOOPBACK;
4705 
4706 	/* Initialize the ipsq */
4707 	if (!ipsq_init(ill))
4708 		goto done;
4709 
4710 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
4711 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
4712 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
4713 #ifdef ILL_DEBUG
4714 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
4715 #endif
4716 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
4717 	if (ipif == NULL)
4718 		goto done;
4719 
4720 	ill->ill_flags = ILLF_MULTICAST;
4721 
4722 	/* Set up default loopback address and mask. */
4723 	if (!isv6) {
4724 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
4725 
4726 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
4727 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4728 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
4729 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4730 		    ipif->ipif_v6subnet);
4731 		ill->ill_flags |= ILLF_IPV4;
4732 	} else {
4733 		ipif->ipif_v6lcl_addr = ipv6_loopback;
4734 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4735 		ipif->ipif_v6net_mask = ipv6_all_ones;
4736 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4737 		    ipif->ipif_v6subnet);
4738 		ill->ill_flags |= ILLF_IPV6;
4739 	}
4740 
4741 	/*
4742 	 * Chain us in at the end of the ill list. hold the ill
4743 	 * before we make it globally visible. 1 for the lookup.
4744 	 */
4745 	ill->ill_refcnt = 0;
4746 	ill_refhold(ill);
4747 
4748 	ill->ill_frag_count = 0;
4749 	ill->ill_frag_free_num_pkts = 0;
4750 	ill->ill_last_frag_clean_time = 0;
4751 
4752 	old_ipsq = ill->ill_phyint->phyint_ipsq;
4753 
4754 	if (ill_glist_insert(ill, "lo", isv6) != 0)
4755 		cmn_err(CE_PANIC, "cannot insert loopback interface");
4756 
4757 	/* Let SCTP know so that it can add this to its list */
4758 	sctp_update_ill(ill, SCTP_ILL_INSERT);
4759 
4760 	/* Let SCTP know about this IPIF, so that it can add it to its list */
4761 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
4762 
4763 	/*
4764 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
4765 	 */
4766 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
4767 		/* Loopback ills aren't in any IPMP group */
4768 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
4769 		ipsq_delete(old_ipsq);
4770 	}
4771 
4772 	/*
4773 	 * Delay this till the ipif is allocated as ipif_allocate
4774 	 * de-references ill_phyint for getting the ifindex. We
4775 	 * can't do this before ipif_allocate because ill_phyint_reinit
4776 	 * -> phyint_assign_ifindex expects ipif to be present.
4777 	 */
4778 	mutex_enter(&ill->ill_phyint->phyint_lock);
4779 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
4780 	mutex_exit(&ill->ill_phyint->phyint_lock);
4781 
4782 	if (loopback_ksp == NULL) {
4783 		/* Export loopback interface statistics */
4784 		loopback_ksp = kstat_create("lo", 0, ipif_loopback_name, "net",
4785 		    KSTAT_TYPE_NAMED, 2, 0);
4786 		if (loopback_ksp != NULL) {
4787 			loopback_ksp->ks_update = loopback_kstat_update;
4788 			kn = KSTAT_NAMED_PTR(loopback_ksp);
4789 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
4790 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
4791 			kstat_install(loopback_ksp);
4792 		}
4793 	}
4794 
4795 	if (error != NULL)
4796 		*error = 0;
4797 	*did_alloc = B_TRUE;
4798 	rw_exit(&ill_g_lock);
4799 	return (ill);
4800 done:
4801 	if (ill != NULL) {
4802 		if (ill->ill_phyint != NULL) {
4803 			ipsq_t	*ipsq;
4804 
4805 			ipsq = ill->ill_phyint->phyint_ipsq;
4806 			if (ipsq != NULL)
4807 				kmem_free(ipsq, sizeof (ipsq_t));
4808 			mi_free(ill->ill_phyint);
4809 		}
4810 		ill_free_mib(ill);
4811 		mi_free(ill);
4812 	}
4813 	rw_exit(&ill_g_lock);
4814 	if (error != NULL)
4815 		*error = ENOMEM;
4816 	return (NULL);
4817 }
4818 
4819 /*
4820  * Return a pointer to the ill which matches the index and IP version type.
4821  */
4822 ill_t *
4823 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
4824     ipsq_func_t func, int *err)
4825 {
4826 	ill_t	*ill;
4827 	ipsq_t  *ipsq;
4828 	phyint_t *phyi;
4829 
4830 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
4831 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
4832 
4833 	if (err != NULL)
4834 		*err = 0;
4835 
4836 	/*
4837 	 * Indexes are stored in the phyint - a common structure
4838 	 * to both IPv4 and IPv6.
4839 	 */
4840 	rw_enter(&ill_g_lock, RW_READER);
4841 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
4842 	    (void *) &index, NULL);
4843 	if (phyi != NULL) {
4844 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
4845 		if (ill != NULL) {
4846 			/*
4847 			 * The block comment at the start of ipif_down
4848 			 * explains the use of the macros used below
4849 			 */
4850 			GRAB_CONN_LOCK(q);
4851 			mutex_enter(&ill->ill_lock);
4852 			if (ILL_CAN_LOOKUP(ill)) {
4853 				ill_refhold_locked(ill);
4854 				mutex_exit(&ill->ill_lock);
4855 				RELEASE_CONN_LOCK(q);
4856 				rw_exit(&ill_g_lock);
4857 				return (ill);
4858 			} else if (ILL_CAN_WAIT(ill, q)) {
4859 				ipsq = ill->ill_phyint->phyint_ipsq;
4860 				mutex_enter(&ipsq->ipsq_lock);
4861 				rw_exit(&ill_g_lock);
4862 				mutex_exit(&ill->ill_lock);
4863 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4864 				mutex_exit(&ipsq->ipsq_lock);
4865 				RELEASE_CONN_LOCK(q);
4866 				*err = EINPROGRESS;
4867 				return (NULL);
4868 			}
4869 			RELEASE_CONN_LOCK(q);
4870 			mutex_exit(&ill->ill_lock);
4871 		}
4872 	}
4873 	rw_exit(&ill_g_lock);
4874 	if (err != NULL)
4875 		*err = ENXIO;
4876 	return (NULL);
4877 }
4878 
4879 /*
4880  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
4881  * that gives a running thread a reference to the ill. This reference must be
4882  * released by the thread when it is done accessing the ill and related
4883  * objects. ill_refcnt can not be used to account for static references
4884  * such as other structures pointing to an ill. Callers must generally
4885  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
4886  * or be sure that the ill is not being deleted or changing state before
4887  * calling the refhold functions. A non-zero ill_refcnt ensures that the
4888  * ill won't change any of its critical state such as address, netmask etc.
4889  */
4890 void
4891 ill_refhold(ill_t *ill)
4892 {
4893 	mutex_enter(&ill->ill_lock);
4894 	ill->ill_refcnt++;
4895 	ILL_TRACE_REF(ill);
4896 	mutex_exit(&ill->ill_lock);
4897 }
4898 
4899 void
4900 ill_refhold_locked(ill_t *ill)
4901 {
4902 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4903 	ill->ill_refcnt++;
4904 	ILL_TRACE_REF(ill);
4905 }
4906 
4907 int
4908 ill_check_and_refhold(ill_t *ill)
4909 {
4910 	mutex_enter(&ill->ill_lock);
4911 	if (ILL_CAN_LOOKUP(ill)) {
4912 		ill_refhold_locked(ill);
4913 		mutex_exit(&ill->ill_lock);
4914 		return (0);
4915 	}
4916 	mutex_exit(&ill->ill_lock);
4917 	return (ILL_LOOKUP_FAILED);
4918 }
4919 
4920 /*
4921  * Must not be called while holding any locks. Otherwise if this is
4922  * the last reference to be released, there is a chance of recursive mutex
4923  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4924  * to restart an ioctl.
4925  */
4926 void
4927 ill_refrele(ill_t *ill)
4928 {
4929 	mutex_enter(&ill->ill_lock);
4930 	ASSERT(ill->ill_refcnt != 0);
4931 	ill->ill_refcnt--;
4932 	ILL_UNTRACE_REF(ill);
4933 	if (ill->ill_refcnt != 0) {
4934 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
4935 		mutex_exit(&ill->ill_lock);
4936 		return;
4937 	}
4938 
4939 	/* Drops the ill_lock */
4940 	ipif_ill_refrele_tail(ill);
4941 }
4942 
4943 /*
4944  * Obtain a weak reference count on the ill. This reference ensures the
4945  * ill won't be freed, but the ill may change any of its critical state
4946  * such as netmask, address etc. Returns an error if the ill has started
4947  * closing.
4948  */
4949 boolean_t
4950 ill_waiter_inc(ill_t *ill)
4951 {
4952 	mutex_enter(&ill->ill_lock);
4953 	if (ill->ill_state_flags & ILL_CONDEMNED) {
4954 		mutex_exit(&ill->ill_lock);
4955 		return (B_FALSE);
4956 	}
4957 	ill->ill_waiters++;
4958 	mutex_exit(&ill->ill_lock);
4959 	return (B_TRUE);
4960 }
4961 
4962 void
4963 ill_waiter_dcr(ill_t *ill)
4964 {
4965 	mutex_enter(&ill->ill_lock);
4966 	ill->ill_waiters--;
4967 	if (ill->ill_waiters == 0)
4968 		cv_broadcast(&ill->ill_cv);
4969 	mutex_exit(&ill->ill_lock);
4970 }
4971 
4972 /*
4973  * Named Dispatch routine to produce a formatted report on all ILLs.
4974  * This report is accessed by using the ndd utility to "get" ND variable
4975  * "ip_ill_status".
4976  */
4977 /* ARGSUSED */
4978 int
4979 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
4980 {
4981 	ill_t		*ill;
4982 	ill_walk_context_t ctx;
4983 
4984 	(void) mi_mpprintf(mp,
4985 	    "ILL      " MI_COL_HDRPAD_STR
4986 	/*   01234567[89ABCDEF] */
4987 	    "rq       " MI_COL_HDRPAD_STR
4988 	/*   01234567[89ABCDEF] */
4989 	    "wq       " MI_COL_HDRPAD_STR
4990 	/*   01234567[89ABCDEF] */
4991 	    "upcnt mxfrg err name");
4992 	/*   12345 12345 123 xxxxxxxx  */
4993 
4994 	rw_enter(&ill_g_lock, RW_READER);
4995 	ill = ILL_START_WALK_ALL(&ctx);
4996 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4997 		(void) mi_mpprintf(mp,
4998 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
4999 		    "%05u %05u %03d %s",
5000 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5001 		    ill->ill_ipif_up_count,
5002 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5003 	}
5004 	rw_exit(&ill_g_lock);
5005 
5006 	return (0);
5007 }
5008 
5009 /*
5010  * Named Dispatch routine to produce a formatted report on all IPIFs.
5011  * This report is accessed by using the ndd utility to "get" ND variable
5012  * "ip_ipif_status".
5013  */
5014 /* ARGSUSED */
5015 int
5016 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5017 {
5018 	char	buf1[INET6_ADDRSTRLEN];
5019 	char	buf2[INET6_ADDRSTRLEN];
5020 	char	buf3[INET6_ADDRSTRLEN];
5021 	char	buf4[INET6_ADDRSTRLEN];
5022 	char	buf5[INET6_ADDRSTRLEN];
5023 	char	buf6[INET6_ADDRSTRLEN];
5024 	char	buf[LIFNAMSIZ];
5025 	ill_t	*ill;
5026 	ipif_t	*ipif;
5027 	nv_t	*nvp;
5028 	uint64_t flags;
5029 	zoneid_t zoneid;
5030 	ill_walk_context_t ctx;
5031 
5032 	(void) mi_mpprintf(mp,
5033 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5034 	    "\tlocal address\n"
5035 	    "\tsrc address\n"
5036 	    "\tsubnet\n"
5037 	    "\tmask\n"
5038 	    "\tbroadcast\n"
5039 	    "\tp-p-dst");
5040 
5041 	ASSERT(q->q_next == NULL);
5042 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
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 		for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
5048 			if (zoneid != GLOBAL_ZONEID &&
5049 			    zoneid != ipif->ipif_zoneid)
5050 				continue;
5051 			(void) mi_mpprintf(mp,
5052 			    MI_COL_PTRFMT_STR
5053 			    "%04u %05u %u/%u/%u %s %d",
5054 			    (void *)ipif,
5055 			    ipif->ipif_metric, ipif->ipif_mtu,
5056 			    ipif->ipif_ib_pkt_count,
5057 			    ipif->ipif_ob_pkt_count,
5058 			    ipif->ipif_fo_pkt_count,
5059 			    ipif_get_name(ipif, buf, sizeof (buf)),
5060 			    ipif->ipif_zoneid);
5061 
5062 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5063 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5064 
5065 		/* Tack on text strings for any flags. */
5066 		nvp = ipif_nv_tbl;
5067 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5068 			if (nvp->nv_value & flags)
5069 				(void) mi_mpprintf_nr(mp, " %s",
5070 				    nvp->nv_name);
5071 		}
5072 		(void) mi_mpprintf(mp,
5073 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5074 		    inet_ntop(AF_INET6,
5075 			&ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5076 		    inet_ntop(AF_INET6,
5077 			&ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5078 		    inet_ntop(AF_INET6,
5079 			&ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5080 		    inet_ntop(AF_INET6,
5081 			&ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5082 		    inet_ntop(AF_INET6,
5083 			&ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5084 		    inet_ntop(AF_INET6,
5085 			&ipif->ipif_v6pp_dst_addr,
5086 			buf6, sizeof (buf6)));
5087 		}
5088 	}
5089 	rw_exit(&ill_g_lock);
5090 	return (0);
5091 }
5092 
5093 /*
5094  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5095  * driver.  We construct best guess defaults for lower level information that
5096  * we need.  If an interface is brought up without injection of any overriding
5097  * information from outside, we have to be ready to go with these defaults.
5098  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5099  * we primarely want the dl_provider_style.
5100  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5101  * at which point we assume the other part of the information is valid.
5102  */
5103 void
5104 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5105 {
5106 	uchar_t		*brdcst_addr;
5107 	uint_t		brdcst_addr_length, phys_addr_length;
5108 	t_scalar_t	sap_length;
5109 	dl_info_ack_t	*dlia;
5110 	ip_m_t		*ipm;
5111 	dl_qos_cl_sel1_t *sel1;
5112 
5113 	ASSERT(IAM_WRITER_ILL(ill));
5114 
5115 	/*
5116 	 * Till the ill is fully up ILL_CHANGING will be set and
5117 	 * the ill is not globally visible. So no need for a lock.
5118 	 */
5119 	dlia = (dl_info_ack_t *)mp->b_rptr;
5120 	ill->ill_mactype = dlia->dl_mac_type;
5121 
5122 	ipm = ip_m_lookup(dlia->dl_mac_type);
5123 	if (ipm == NULL) {
5124 		ipm = ip_m_lookup(DL_OTHER);
5125 		ASSERT(ipm != NULL);
5126 	}
5127 	ill->ill_media = ipm;
5128 
5129 	/*
5130 	 * When the new DLPI stuff is ready we'll pull lengths
5131 	 * from dlia.
5132 	 */
5133 	if (dlia->dl_version == DL_VERSION_2) {
5134 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5135 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5136 		    brdcst_addr_length);
5137 		if (brdcst_addr == NULL) {
5138 			brdcst_addr_length = 0;
5139 		}
5140 		sap_length = dlia->dl_sap_length;
5141 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5142 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5143 		    brdcst_addr_length, sap_length, phys_addr_length));
5144 	} else {
5145 		brdcst_addr_length = 6;
5146 		brdcst_addr = ip_six_byte_all_ones;
5147 		sap_length = -2;
5148 		phys_addr_length = brdcst_addr_length;
5149 	}
5150 
5151 	ill->ill_bcast_addr_length = brdcst_addr_length;
5152 	ill->ill_phys_addr_length = phys_addr_length;
5153 	ill->ill_sap_length = sap_length;
5154 	ill->ill_max_frag = dlia->dl_max_sdu;
5155 	ill->ill_max_mtu = ill->ill_max_frag;
5156 
5157 	ill->ill_type = ipm->ip_m_type;
5158 
5159 	if (!ill->ill_dlpi_style_set) {
5160 		if (dlia->dl_provider_style == DL_STYLE2)
5161 			ill->ill_needs_attach = 1;
5162 
5163 		/*
5164 		 * Allocate the first ipif on this ill. We don't delay it
5165 		 * further as ioctl handling assumes atleast one ipif to
5166 		 * be present.
5167 		 *
5168 		 * At this point we don't know whether the ill is v4 or v6.
5169 		 * We will know this whan the SIOCSLIFNAME happens and
5170 		 * the correct value for ill_isv6 will be assigned in
5171 		 * ipif_set_values(). We need to hold the ill lock and
5172 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5173 		 * the wakeup.
5174 		 */
5175 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5176 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5177 		mutex_enter(&ill->ill_lock);
5178 		ASSERT(ill->ill_dlpi_style_set == 0);
5179 		ill->ill_dlpi_style_set = 1;
5180 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5181 		cv_broadcast(&ill->ill_cv);
5182 		mutex_exit(&ill->ill_lock);
5183 		freemsg(mp);
5184 		return;
5185 	}
5186 	ASSERT(ill->ill_ipif != NULL);
5187 	/*
5188 	 * We know whether it is IPv4 or IPv6 now, as this is the
5189 	 * second DL_INFO_ACK we are recieving in response to the
5190 	 * DL_INFO_REQ sent in ipif_set_values.
5191 	 */
5192 	if (ill->ill_isv6)
5193 		ill->ill_sap = IP6_DL_SAP;
5194 	else
5195 		ill->ill_sap = IP_DL_SAP;
5196 	/*
5197 	 * Set ipif_mtu which is used to set the IRE's
5198 	 * ire_max_frag value. The driver could have sent
5199 	 * a different mtu from what it sent last time. No
5200 	 * need to call ipif_mtu_change because IREs have
5201 	 * not yet been created.
5202 	 */
5203 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5204 	/*
5205 	 * Clear all the flags that were set based on ill_bcast_addr_length
5206 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5207 	 * changed now and we need to re-evaluate.
5208 	 */
5209 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5210 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5211 
5212 	/*
5213 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5214 	 * changed now.
5215 	 */
5216 	if (ill->ill_bcast_addr_length == 0) {
5217 		if (ill->ill_resolver_mp != NULL)
5218 			freemsg(ill->ill_resolver_mp);
5219 		if (ill->ill_bcast_mp != NULL)
5220 			freemsg(ill->ill_bcast_mp);
5221 		if (ill->ill_flags & ILLF_XRESOLV)
5222 			ill->ill_net_type = IRE_IF_RESOLVER;
5223 		else
5224 			ill->ill_net_type = IRE_IF_NORESOLVER;
5225 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5226 		    ill->ill_phys_addr_length,
5227 		    ill->ill_sap,
5228 		    ill->ill_sap_length);
5229 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5230 
5231 		if (ill->ill_isv6)
5232 			/*
5233 			 * Note: xresolv interfaces will eventually need NOARP
5234 			 * set here as well, but that will require those
5235 			 * external resolvers to have some knowledge of
5236 			 * that flag and act appropriately. Not to be changed
5237 			 * at present.
5238 			 */
5239 			ill->ill_flags |= ILLF_NONUD;
5240 		else
5241 			ill->ill_flags |= ILLF_NOARP;
5242 
5243 		if (ill->ill_phys_addr_length == 0) {
5244 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5245 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5246 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5247 			} else {
5248 				/* pt-pt supports multicast. */
5249 				ill->ill_flags |= ILLF_MULTICAST;
5250 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5251 			}
5252 		}
5253 	} else {
5254 		ill->ill_net_type = IRE_IF_RESOLVER;
5255 		if (ill->ill_bcast_mp != NULL)
5256 			freemsg(ill->ill_bcast_mp);
5257 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5258 		    ill->ill_bcast_addr_length, ill->ill_sap,
5259 		    ill->ill_sap_length);
5260 		/*
5261 		 * Later detect lack of DLPI driver multicast
5262 		 * capability by catching DL_ENABMULTI errors in
5263 		 * ip_rput_dlpi.
5264 		 */
5265 		ill->ill_flags |= ILLF_MULTICAST;
5266 		if (!ill->ill_isv6)
5267 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5268 	}
5269 	/* By default an interface does not support any CoS marking */
5270 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5271 
5272 	/*
5273 	 * If we get QoS information in DL_INFO_ACK, the device supports
5274 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5275 	 */
5276 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5277 	    dlia->dl_qos_length);
5278 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5279 		ill->ill_flags |= ILLF_COS_ENABLED;
5280 	}
5281 
5282 	/* Clear any previous error indication. */
5283 	ill->ill_error = 0;
5284 	freemsg(mp);
5285 }
5286 
5287 /*
5288  * Perform various checks to verify that an address would make sense as a
5289  * local, remote, or subnet interface address.
5290  */
5291 static boolean_t
5292 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5293 {
5294 	ipaddr_t	net_mask;
5295 
5296 	/*
5297 	 * Don't allow all zeroes, all ones or experimental address, but allow
5298 	 * all ones netmask.
5299 	 */
5300 	if ((net_mask = ip_net_mask(addr)) == 0)
5301 		return (B_FALSE);
5302 	/* A given netmask overrides the "guess" netmask */
5303 	if (subnet_mask != 0)
5304 		net_mask = subnet_mask;
5305 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5306 	    (addr == (addr | ~net_mask)))) {
5307 		return (B_FALSE);
5308 	}
5309 	if (CLASSD(addr))
5310 		return (B_FALSE);
5311 
5312 	return (B_TRUE);
5313 }
5314 
5315 /*
5316  * ipif_lookup_group
5317  * Returns held ipif
5318  */
5319 ipif_t *
5320 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid)
5321 {
5322 	ire_t	*ire;
5323 	ipif_t	*ipif;
5324 
5325 	ire = ire_lookup_multi(group, zoneid);
5326 	if (ire == NULL)
5327 		return (NULL);
5328 	ipif = ire->ire_ipif;
5329 	ipif_refhold(ipif);
5330 	ire_refrele(ire);
5331 	return (ipif);
5332 }
5333 
5334 /*
5335  * Look for an ipif with the specified interface address and destination.
5336  * The destination address is used only for matching point-to-point interfaces.
5337  */
5338 ipif_t *
5339 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5340     ipsq_func_t func, int *error)
5341 {
5342 	ipif_t	*ipif;
5343 	ill_t	*ill;
5344 	ill_walk_context_t ctx;
5345 	ipsq_t	*ipsq;
5346 
5347 	if (error != NULL)
5348 		*error = 0;
5349 
5350 	/*
5351 	 * First match all the point-to-point interfaces
5352 	 * before looking at non-point-to-point interfaces.
5353 	 * This is done to avoid returning non-point-to-point
5354 	 * ipif instead of unnumbered point-to-point ipif.
5355 	 */
5356 	rw_enter(&ill_g_lock, RW_READER);
5357 	ill = ILL_START_WALK_V4(&ctx);
5358 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5359 		GRAB_CONN_LOCK(q);
5360 		mutex_enter(&ill->ill_lock);
5361 		for (ipif = ill->ill_ipif; ipif != NULL;
5362 		    ipif = ipif->ipif_next) {
5363 			/* Allow the ipif to be down */
5364 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5365 			    (ipif->ipif_lcl_addr == if_addr) &&
5366 			    (ipif->ipif_pp_dst_addr == dst)) {
5367 				/*
5368 				 * The block comment at the start of ipif_down
5369 				 * explains the use of the macros used below
5370 				 */
5371 				if (IPIF_CAN_LOOKUP(ipif)) {
5372 					ipif_refhold_locked(ipif);
5373 					mutex_exit(&ill->ill_lock);
5374 					RELEASE_CONN_LOCK(q);
5375 					rw_exit(&ill_g_lock);
5376 					return (ipif);
5377 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5378 					ipsq = ill->ill_phyint->phyint_ipsq;
5379 					mutex_enter(&ipsq->ipsq_lock);
5380 					mutex_exit(&ill->ill_lock);
5381 					rw_exit(&ill_g_lock);
5382 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5383 						ill);
5384 					mutex_exit(&ipsq->ipsq_lock);
5385 					RELEASE_CONN_LOCK(q);
5386 					*error = EINPROGRESS;
5387 					return (NULL);
5388 				}
5389 			}
5390 		}
5391 		mutex_exit(&ill->ill_lock);
5392 		RELEASE_CONN_LOCK(q);
5393 	}
5394 	rw_exit(&ill_g_lock);
5395 
5396 	/* lookup the ipif based on interface address */
5397 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error);
5398 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5399 	return (ipif);
5400 }
5401 
5402 /*
5403  * Look for an ipif with the specified address. For point-point links
5404  * we look for matches on either the destination address and the local
5405  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5406  * is set.
5407  * Matches on a specific ill if match_ill is set.
5408  */
5409 ipif_t *
5410 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5411     mblk_t *mp, ipsq_func_t func, int *error)
5412 {
5413 	ipif_t  *ipif;
5414 	ill_t   *ill;
5415 	boolean_t ptp = B_FALSE;
5416 	ipsq_t	*ipsq;
5417 	ill_walk_context_t	ctx;
5418 
5419 	if (error != NULL)
5420 		*error = 0;
5421 
5422 	rw_enter(&ill_g_lock, RW_READER);
5423 	/*
5424 	 * Repeat twice, first based on local addresses and
5425 	 * next time for pointopoint.
5426 	 */
5427 repeat:
5428 	ill = ILL_START_WALK_V4(&ctx);
5429 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5430 		if (match_ill != NULL && ill != match_ill) {
5431 			continue;
5432 		}
5433 		GRAB_CONN_LOCK(q);
5434 		mutex_enter(&ill->ill_lock);
5435 		for (ipif = ill->ill_ipif; ipif != NULL;
5436 		    ipif = ipif->ipif_next) {
5437 			if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid)
5438 				continue;
5439 			/* Allow the ipif to be down */
5440 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5441 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5442 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5443 			    (ipif->ipif_pp_dst_addr == addr))) {
5444 				/*
5445 				 * The block comment at the start of ipif_down
5446 				 * explains the use of the macros used below
5447 				 */
5448 				if (IPIF_CAN_LOOKUP(ipif)) {
5449 					ipif_refhold_locked(ipif);
5450 					mutex_exit(&ill->ill_lock);
5451 					RELEASE_CONN_LOCK(q);
5452 					rw_exit(&ill_g_lock);
5453 					return (ipif);
5454 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5455 					ipsq = ill->ill_phyint->phyint_ipsq;
5456 					mutex_enter(&ipsq->ipsq_lock);
5457 					mutex_exit(&ill->ill_lock);
5458 					rw_exit(&ill_g_lock);
5459 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5460 						ill);
5461 					mutex_exit(&ipsq->ipsq_lock);
5462 					RELEASE_CONN_LOCK(q);
5463 					*error = EINPROGRESS;
5464 					return (NULL);
5465 				}
5466 			}
5467 		}
5468 		mutex_exit(&ill->ill_lock);
5469 		RELEASE_CONN_LOCK(q);
5470 	}
5471 
5472 	/* Now try the ptp case */
5473 	if (ptp) {
5474 		rw_exit(&ill_g_lock);
5475 		if (error != NULL)
5476 			*error = ENXIO;
5477 		return (NULL);
5478 	}
5479 	ptp = B_TRUE;
5480 	goto repeat;
5481 }
5482 
5483 /*
5484  * Look for an ipif that matches the specified remote address i.e. the
5485  * ipif that would receive the specified packet.
5486  * First look for directly connected interfaces and then do a recursive
5487  * IRE lookup and pick the first ipif corresponding to the source address in the
5488  * ire.
5489  * Returns: held ipif
5490  */
5491 ipif_t *
5492 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
5493 {
5494 	ipif_t	*ipif;
5495 	ire_t	*ire;
5496 
5497 	ASSERT(!ill->ill_isv6);
5498 
5499 	/*
5500 	 * Someone could be changing this ipif currently or change it
5501 	 * after we return this. Thus  a few packets could use the old
5502 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
5503 	 * will atomically be updated or cleaned up with the new value
5504 	 * Thus we don't need a lock to check the flags or other attrs below.
5505 	 */
5506 	mutex_enter(&ill->ill_lock);
5507 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5508 		if (!IPIF_CAN_LOOKUP(ipif))
5509 			continue;
5510 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid)
5511 			continue;
5512 		/* Allow the ipif to be down */
5513 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
5514 			if ((ipif->ipif_pp_dst_addr == addr) ||
5515 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
5516 			    ipif->ipif_lcl_addr == addr)) {
5517 				ipif_refhold_locked(ipif);
5518 				mutex_exit(&ill->ill_lock);
5519 				return (ipif);
5520 			}
5521 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
5522 			ipif_refhold_locked(ipif);
5523 			mutex_exit(&ill->ill_lock);
5524 			return (ipif);
5525 		}
5526 	}
5527 	mutex_exit(&ill->ill_lock);
5528 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
5529 	    MATCH_IRE_RECURSIVE);
5530 	if (ire != NULL) {
5531 		/*
5532 		 * The callers of this function wants to know the
5533 		 * interface on which they have to send the replies
5534 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
5535 		 * derived from different ills, we really don't care
5536 		 * what we return here.
5537 		 */
5538 		ipif = ire->ire_ipif;
5539 		if (ipif != NULL) {
5540 			ipif_refhold(ipif);
5541 			ire_refrele(ire);
5542 			return (ipif);
5543 		}
5544 		ire_refrele(ire);
5545 	}
5546 	/* Pick the first interface */
5547 	ipif = ipif_get_next_ipif(NULL, ill);
5548 	return (ipif);
5549 }
5550 
5551 /*
5552  * This func does not prevent refcnt from increasing. But if
5553  * the caller has taken steps to that effect, then this func
5554  * can be used to determine whether the ill has become quiescent
5555  */
5556 boolean_t
5557 ill_is_quiescent(ill_t *ill)
5558 {
5559 	ipif_t	*ipif;
5560 
5561 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5562 
5563 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5564 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0)
5565 			return (B_FALSE);
5566 	}
5567 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
5568 	    ill->ill_nce_cnt != 0 || ill->ill_srcif_refcnt != 0 ||
5569 	    ill->ill_mrtun_refcnt != 0)
5570 		return (B_FALSE);
5571 	return (B_TRUE);
5572 }
5573 
5574 /*
5575  * This func does not prevent refcnt from increasing. But if
5576  * the caller has taken steps to that effect, then this func
5577  * can be used to determine whether the ipif has become quiescent
5578  */
5579 static boolean_t
5580 ipif_is_quiescent(ipif_t *ipif)
5581 {
5582 	ill_t *ill;
5583 
5584 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5585 
5586 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0)
5587 		return (B_FALSE);
5588 
5589 	ill = ipif->ipif_ill;
5590 	if (ill->ill_ipif_up_count != 0 || ill->ill_logical_down)
5591 		return (B_TRUE);
5592 
5593 	/* This is the last ipif going down or being deleted on this ill */
5594 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0)
5595 		return (B_FALSE);
5596 
5597 	return (B_TRUE);
5598 }
5599 
5600 /*
5601  * This func does not prevent refcnt from increasing. But if
5602  * the caller has taken steps to that effect, then this func
5603  * can be used to determine whether the ipifs marked with IPIF_MOVING
5604  * have become quiescent and can be moved in a failover/failback.
5605  */
5606 static ipif_t *
5607 ill_quiescent_to_move(ill_t *ill)
5608 {
5609 	ipif_t  *ipif;
5610 
5611 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5612 
5613 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5614 		if (ipif->ipif_state_flags & IPIF_MOVING) {
5615 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
5616 				return (ipif);
5617 			}
5618 		}
5619 	}
5620 	return (NULL);
5621 }
5622 
5623 /*
5624  * The ipif/ill/ire has been refreled. Do the tail processing.
5625  * Determine if the ipif or ill in question has become quiescent and if so
5626  * wakeup close and/or restart any queued pending ioctl that is waiting
5627  * for the ipif_down (or ill_down)
5628  */
5629 void
5630 ipif_ill_refrele_tail(ill_t *ill)
5631 {
5632 	mblk_t	*mp;
5633 	conn_t	*connp;
5634 	ipsq_t	*ipsq;
5635 	ipif_t	*ipif;
5636 
5637 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5638 
5639 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
5640 	    ill_is_quiescent(ill)) {
5641 		/* ill_close may be waiting */
5642 		cv_broadcast(&ill->ill_cv);
5643 	}
5644 
5645 	/* ipsq can't change because ill_lock  is held */
5646 	ipsq = ill->ill_phyint->phyint_ipsq;
5647 	if (ipsq->ipsq_waitfor == 0) {
5648 		/* Not waiting for anything, just return. */
5649 		mutex_exit(&ill->ill_lock);
5650 		return;
5651 	}
5652 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
5653 		ipsq->ipsq_pending_ipif != NULL);
5654 	/*
5655 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
5656 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
5657 	 * be zero for restarting an ioctl that ends up downing the ill.
5658 	 */
5659 	ipif = ipsq->ipsq_pending_ipif;
5660 	if (ipif->ipif_ill != ill) {
5661 		/* The ioctl is pending on some other ill. */
5662 		mutex_exit(&ill->ill_lock);
5663 		return;
5664 	}
5665 
5666 	switch (ipsq->ipsq_waitfor) {
5667 	case IPIF_DOWN:
5668 	case IPIF_FREE:
5669 		if (!ipif_is_quiescent(ipif)) {
5670 			mutex_exit(&ill->ill_lock);
5671 			return;
5672 		}
5673 		break;
5674 
5675 	case ILL_DOWN:
5676 	case ILL_FREE:
5677 		/*
5678 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
5679 		 * waits synchronously in ip_close, and no message is queued in
5680 		 * ipsq_pending_mp at all in this case
5681 		 */
5682 		if (!ill_is_quiescent(ill)) {
5683 			mutex_exit(&ill->ill_lock);
5684 			return;
5685 		}
5686 
5687 		break;
5688 
5689 	case ILL_MOVE_OK:
5690 		if (ill_quiescent_to_move(ill) != NULL) {
5691 			mutex_exit(&ill->ill_lock);
5692 			return;
5693 		}
5694 
5695 		break;
5696 	default:
5697 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
5698 		    (void *)ipsq, ipsq->ipsq_waitfor);
5699 	}
5700 
5701 	/*
5702 	 * Incr refcnt for the qwriter_ip call below which
5703 	 * does a refrele
5704 	 */
5705 	ill_refhold_locked(ill);
5706 	mutex_exit(&ill->ill_lock);
5707 
5708 	mp = ipsq_pending_mp_get(ipsq, &connp);
5709 	ASSERT(mp != NULL);
5710 
5711 	switch (mp->b_datap->db_type) {
5712 	case M_ERROR:
5713 	case M_HANGUP:
5714 		(void) qwriter_ip(NULL, ill, ill->ill_rq, mp,
5715 		    ipif_all_down_tail, CUR_OP, B_TRUE);
5716 		return;
5717 
5718 	case M_IOCTL:
5719 	case M_IOCDATA:
5720 		(void) qwriter_ip(NULL, ill,
5721 		    (connp != NULL ? CONNP_TO_WQ(connp) : ill->ill_wq), mp,
5722 		    ip_reprocess_ioctl, CUR_OP, B_TRUE);
5723 		return;
5724 
5725 	default:
5726 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
5727 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
5728 	}
5729 }
5730 
5731 #ifdef ILL_DEBUG
5732 /* Reuse trace buffer from beginning (if reached the end) and record trace */
5733 void
5734 th_trace_rrecord(th_trace_t *th_trace)
5735 {
5736 	tr_buf_t *tr_buf;
5737 	uint_t lastref;
5738 
5739 	lastref = th_trace->th_trace_lastref;
5740 	lastref++;
5741 	if (lastref == TR_BUF_MAX)
5742 		lastref = 0;
5743 	th_trace->th_trace_lastref = lastref;
5744 	tr_buf = &th_trace->th_trbuf[lastref];
5745 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, IP_STACK_DEPTH);
5746 }
5747 
5748 th_trace_t *
5749 th_trace_ipif_lookup(ipif_t *ipif)
5750 {
5751 	int bucket_id;
5752 	th_trace_t *th_trace;
5753 
5754 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5755 
5756 	bucket_id = IP_TR_HASH(curthread);
5757 	ASSERT(bucket_id < IP_TR_HASH_MAX);
5758 
5759 	for (th_trace = ipif->ipif_trace[bucket_id]; th_trace != NULL;
5760 	    th_trace = th_trace->th_next) {
5761 		if (th_trace->th_id == curthread)
5762 			return (th_trace);
5763 	}
5764 	return (NULL);
5765 }
5766 
5767 void
5768 ipif_trace_ref(ipif_t *ipif)
5769 {
5770 	int bucket_id;
5771 	th_trace_t *th_trace;
5772 
5773 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5774 
5775 	if (ipif->ipif_trace_disable)
5776 		return;
5777 
5778 	/*
5779 	 * Attempt to locate the trace buffer for the curthread.
5780 	 * If it does not exist, then allocate a new trace buffer
5781 	 * and link it in list of trace bufs for this ipif, at the head
5782 	 */
5783 	th_trace = th_trace_ipif_lookup(ipif);
5784 	if (th_trace == NULL) {
5785 		bucket_id = IP_TR_HASH(curthread);
5786 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
5787 		    KM_NOSLEEP);
5788 		if (th_trace == NULL) {
5789 			ipif->ipif_trace_disable = B_TRUE;
5790 			ipif_trace_cleanup(ipif);
5791 			return;
5792 		}
5793 		th_trace->th_id = curthread;
5794 		th_trace->th_next = ipif->ipif_trace[bucket_id];
5795 		th_trace->th_prev = &ipif->ipif_trace[bucket_id];
5796 		if (th_trace->th_next != NULL)
5797 			th_trace->th_next->th_prev = &th_trace->th_next;
5798 		ipif->ipif_trace[bucket_id] = th_trace;
5799 	}
5800 	ASSERT(th_trace->th_refcnt >= 0 &&
5801 		th_trace->th_refcnt < TR_BUF_MAX -1);
5802 	th_trace->th_refcnt++;
5803 	th_trace_rrecord(th_trace);
5804 }
5805 
5806 void
5807 ipif_untrace_ref(ipif_t *ipif)
5808 {
5809 	th_trace_t *th_trace;
5810 
5811 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5812 
5813 	if (ipif->ipif_trace_disable)
5814 		return;
5815 	th_trace = th_trace_ipif_lookup(ipif);
5816 	ASSERT(th_trace != NULL);
5817 	ASSERT(th_trace->th_refcnt > 0);
5818 
5819 	th_trace->th_refcnt--;
5820 	th_trace_rrecord(th_trace);
5821 }
5822 
5823 th_trace_t *
5824 th_trace_ill_lookup(ill_t *ill)
5825 {
5826 	th_trace_t *th_trace;
5827 	int bucket_id;
5828 
5829 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5830 
5831 	bucket_id = IP_TR_HASH(curthread);
5832 	ASSERT(bucket_id < IP_TR_HASH_MAX);
5833 
5834 	for (th_trace = ill->ill_trace[bucket_id]; th_trace != NULL;
5835 	    th_trace = th_trace->th_next) {
5836 		if (th_trace->th_id == curthread)
5837 			return (th_trace);
5838 	}
5839 	return (NULL);
5840 }
5841 
5842 void
5843 ill_trace_ref(ill_t *ill)
5844 {
5845 	int bucket_id;
5846 	th_trace_t *th_trace;
5847 
5848 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5849 	if (ill->ill_trace_disable)
5850 		return;
5851 	/*
5852 	 * Attempt to locate the trace buffer for the curthread.
5853 	 * If it does not exist, then allocate a new trace buffer
5854 	 * and link it in list of trace bufs for this ill, at the head
5855 	 */
5856 	th_trace = th_trace_ill_lookup(ill);
5857 	if (th_trace == NULL) {
5858 		bucket_id = IP_TR_HASH(curthread);
5859 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
5860 		    KM_NOSLEEP);
5861 		if (th_trace == NULL) {
5862 			ill->ill_trace_disable = B_TRUE;
5863 			ill_trace_cleanup(ill);
5864 			return;
5865 		}
5866 		th_trace->th_id = curthread;
5867 		th_trace->th_next = ill->ill_trace[bucket_id];
5868 		th_trace->th_prev = &ill->ill_trace[bucket_id];
5869 		if (th_trace->th_next != NULL)
5870 			th_trace->th_next->th_prev = &th_trace->th_next;
5871 		ill->ill_trace[bucket_id] = th_trace;
5872 	}
5873 	ASSERT(th_trace->th_refcnt >= 0 &&
5874 		th_trace->th_refcnt < TR_BUF_MAX - 1);
5875 
5876 	th_trace->th_refcnt++;
5877 	th_trace_rrecord(th_trace);
5878 }
5879 
5880 void
5881 ill_untrace_ref(ill_t *ill)
5882 {
5883 	th_trace_t *th_trace;
5884 
5885 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5886 
5887 	if (ill->ill_trace_disable)
5888 		return;
5889 	th_trace = th_trace_ill_lookup(ill);
5890 	ASSERT(th_trace != NULL);
5891 	ASSERT(th_trace->th_refcnt > 0);
5892 
5893 	th_trace->th_refcnt--;
5894 	th_trace_rrecord(th_trace);
5895 }
5896 
5897 /*
5898  * Verify that this thread has no refs to the ipif and free
5899  * the trace buffers
5900  */
5901 /* ARGSUSED */
5902 void
5903 ipif_thread_exit(ipif_t *ipif, void *dummy)
5904 {
5905 	th_trace_t *th_trace;
5906 
5907 	mutex_enter(&ipif->ipif_ill->ill_lock);
5908 
5909 	th_trace = th_trace_ipif_lookup(ipif);
5910 	if (th_trace == NULL) {
5911 		mutex_exit(&ipif->ipif_ill->ill_lock);
5912 		return;
5913 	}
5914 	ASSERT(th_trace->th_refcnt == 0);
5915 	/* unlink th_trace and free it */
5916 	*th_trace->th_prev = th_trace->th_next;
5917 	if (th_trace->th_next != NULL)
5918 		th_trace->th_next->th_prev = th_trace->th_prev;
5919 	th_trace->th_next = NULL;
5920 	th_trace->th_prev = NULL;
5921 	kmem_free(th_trace, sizeof (th_trace_t));
5922 
5923 	mutex_exit(&ipif->ipif_ill->ill_lock);
5924 }
5925 
5926 /*
5927  * Verify that this thread has no refs to the ill and free
5928  * the trace buffers
5929  */
5930 /* ARGSUSED */
5931 void
5932 ill_thread_exit(ill_t *ill, void *dummy)
5933 {
5934 	th_trace_t *th_trace;
5935 
5936 	mutex_enter(&ill->ill_lock);
5937 
5938 	th_trace = th_trace_ill_lookup(ill);
5939 	if (th_trace == NULL) {
5940 		mutex_exit(&ill->ill_lock);
5941 		return;
5942 	}
5943 	ASSERT(th_trace->th_refcnt == 0);
5944 	/* unlink th_trace and free it */
5945 	*th_trace->th_prev = th_trace->th_next;
5946 	if (th_trace->th_next != NULL)
5947 		th_trace->th_next->th_prev = th_trace->th_prev;
5948 	th_trace->th_next = NULL;
5949 	th_trace->th_prev = NULL;
5950 	kmem_free(th_trace, sizeof (th_trace_t));
5951 
5952 	mutex_exit(&ill->ill_lock);
5953 }
5954 #endif
5955 
5956 #ifdef ILL_DEBUG
5957 void
5958 ip_thread_exit(void)
5959 {
5960 	ill_t	*ill;
5961 	ipif_t	*ipif;
5962 	ill_walk_context_t	ctx;
5963 
5964 	rw_enter(&ill_g_lock, RW_READER);
5965 	ill = ILL_START_WALK_ALL(&ctx);
5966 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5967 		for (ipif = ill->ill_ipif; ipif != NULL;
5968 		    ipif = ipif->ipif_next) {
5969 			ipif_thread_exit(ipif, NULL);
5970 		}
5971 		ill_thread_exit(ill, NULL);
5972 	}
5973 	rw_exit(&ill_g_lock);
5974 
5975 	ire_walk(ire_thread_exit, NULL);
5976 	ndp_walk_impl(NULL, nce_thread_exit, NULL, B_FALSE);
5977 }
5978 
5979 /*
5980  * Called when ipif is unplumbed or when memory alloc fails
5981  */
5982 void
5983 ipif_trace_cleanup(ipif_t *ipif)
5984 {
5985 	int	i;
5986 	th_trace_t	*th_trace;
5987 	th_trace_t	*th_trace_next;
5988 
5989 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
5990 		for (th_trace = ipif->ipif_trace[i]; th_trace != NULL;
5991 		    th_trace = th_trace_next) {
5992 			th_trace_next = th_trace->th_next;
5993 			kmem_free(th_trace, sizeof (th_trace_t));
5994 		}
5995 		ipif->ipif_trace[i] = NULL;
5996 	}
5997 }
5998 
5999 /*
6000  * Called when ill is unplumbed or when memory alloc fails
6001  */
6002 void
6003 ill_trace_cleanup(ill_t *ill)
6004 {
6005 	int	i;
6006 	th_trace_t	*th_trace;
6007 	th_trace_t	*th_trace_next;
6008 
6009 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6010 		for (th_trace = ill->ill_trace[i]; th_trace != NULL;
6011 		    th_trace = th_trace_next) {
6012 			th_trace_next = th_trace->th_next;
6013 			kmem_free(th_trace, sizeof (th_trace_t));
6014 		}
6015 		ill->ill_trace[i] = NULL;
6016 	}
6017 }
6018 
6019 #else
6020 void ip_thread_exit(void) {}
6021 #endif
6022 
6023 void
6024 ipif_refhold_locked(ipif_t *ipif)
6025 {
6026 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6027 	ipif->ipif_refcnt++;
6028 	IPIF_TRACE_REF(ipif);
6029 }
6030 
6031 void
6032 ipif_refhold(ipif_t *ipif)
6033 {
6034 	ill_t	*ill;
6035 
6036 	ill = ipif->ipif_ill;
6037 	mutex_enter(&ill->ill_lock);
6038 	ipif->ipif_refcnt++;
6039 	IPIF_TRACE_REF(ipif);
6040 	mutex_exit(&ill->ill_lock);
6041 }
6042 
6043 /*
6044  * Must not be called while holding any locks. Otherwise if this is
6045  * the last reference to be released there is a chance of recursive mutex
6046  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6047  * to restart an ioctl.
6048  */
6049 void
6050 ipif_refrele(ipif_t *ipif)
6051 {
6052 	ill_t	*ill;
6053 
6054 	ill = ipif->ipif_ill;
6055 
6056 	mutex_enter(&ill->ill_lock);
6057 	ASSERT(ipif->ipif_refcnt != 0);
6058 	ipif->ipif_refcnt--;
6059 	IPIF_UNTRACE_REF(ipif);
6060 	if (ipif->ipif_refcnt != 0) {
6061 		mutex_exit(&ill->ill_lock);
6062 		return;
6063 	}
6064 
6065 	/* Drops the ill_lock */
6066 	ipif_ill_refrele_tail(ill);
6067 }
6068 
6069 ipif_t *
6070 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6071 {
6072 	ipif_t	*ipif;
6073 
6074 	mutex_enter(&ill->ill_lock);
6075 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6076 	    ipif != NULL; ipif = ipif->ipif_next) {
6077 		if (!IPIF_CAN_LOOKUP(ipif))
6078 			continue;
6079 		ipif_refhold_locked(ipif);
6080 		mutex_exit(&ill->ill_lock);
6081 		return (ipif);
6082 	}
6083 	mutex_exit(&ill->ill_lock);
6084 	return (NULL);
6085 }
6086 
6087 /*
6088  * TODO: make this table extendible at run time
6089  * Return a pointer to the mac type info for 'mac_type'
6090  */
6091 static ip_m_t *
6092 ip_m_lookup(t_uscalar_t mac_type)
6093 {
6094 	ip_m_t	*ipm;
6095 
6096 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6097 		if (ipm->ip_m_mac_type == mac_type)
6098 			return (ipm);
6099 	return (NULL);
6100 }
6101 
6102 /*
6103  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6104  * ipif_arg is passed in to associate it with the correct interface.
6105  * We may need to restart this operation if the ipif cannot be looked up
6106  * due to an exclusive operation that is currently in progress. The restart
6107  * entry point is specified by 'func'
6108  */
6109 int
6110 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6111     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6112     ire_t **ire_arg, boolean_t ioctl_msg, queue_t *q, mblk_t *mp,
6113     ipsq_func_t func)
6114 {
6115 	ire_t	*ire;
6116 	ire_t	*gw_ire = NULL;
6117 	ipif_t	*ipif = NULL;
6118 	boolean_t ipif_refheld = B_FALSE;
6119 	uint_t	type;
6120 	int	match_flags = MATCH_IRE_TYPE;
6121 	int	error;
6122 
6123 	ip1dbg(("ip_rt_add:"));
6124 
6125 	if (ire_arg != NULL)
6126 		*ire_arg = NULL;
6127 
6128 	/*
6129 	 * If this is the case of RTF_HOST being set, then we set the netmask
6130 	 * to all ones (regardless if one was supplied).
6131 	 */
6132 	if (flags & RTF_HOST)
6133 		mask = IP_HOST_MASK;
6134 
6135 	/*
6136 	 * Prevent routes with a zero gateway from being created (since
6137 	 * interfaces can currently be plumbed and brought up no assigned
6138 	 * address).
6139 	 * For routes with RTA_SRCIFP, the gateway address can be 0.0.0.0.
6140 	 */
6141 	if (gw_addr == 0 && src_ipif == NULL)
6142 		return (ENETUNREACH);
6143 	/*
6144 	 * Get the ipif, if any, corresponding to the gw_addr
6145 	 */
6146 	if (gw_addr != 0) {
6147 		ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func,
6148 		    &error);
6149 		if (ipif != NULL) {
6150 			if (IS_VNI(ipif->ipif_ill)) {
6151 				ipif_refrele(ipif);
6152 				return (EINVAL);
6153 			}
6154 			ipif_refheld = B_TRUE;
6155 		} else if (error == EINPROGRESS) {
6156 			ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6157 			return (EINPROGRESS);
6158 		} else {
6159 			error = 0;
6160 		}
6161 	}
6162 
6163 	if (ipif != NULL) {
6164 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6165 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6166 	} else {
6167 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6168 	}
6169 
6170 	/*
6171 	 * GateD will attempt to create routes with a loopback interface
6172 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6173 	 * these routes to be added, but create them as interface routes
6174 	 * since the gateway is an interface address.
6175 	 */
6176 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK))
6177 		flags &= ~RTF_GATEWAY;
6178 
6179 	/*
6180 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6181 	 * and the gateway address provided is one of the system's interface
6182 	 * addresses.  By using the routing socket interface and supplying an
6183 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6184 	 * specifying an interface route to be created is available which uses
6185 	 * the interface index that specifies the outgoing interface rather than
6186 	 * the address of an outgoing interface (which may not be able to
6187 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6188 	 * flag, routes can be specified which not only specify the next-hop to
6189 	 * be used when routing to a certain prefix, but also which outgoing
6190 	 * interface should be used.
6191 	 *
6192 	 * Previously, interfaces would have unique addresses assigned to them
6193 	 * and so the address assigned to a particular interface could be used
6194 	 * to identify a particular interface.  One exception to this was the
6195 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6196 	 *
6197 	 * With the advent of IPv6 and its link-local addresses, this
6198 	 * restriction was relaxed and interfaces could share addresses between
6199 	 * themselves.  In fact, typically all of the link-local interfaces on
6200 	 * an IPv6 node or router will have the same link-local address.  In
6201 	 * order to differentiate between these interfaces, the use of an
6202 	 * interface index is necessary and this index can be carried inside a
6203 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6204 	 * of using the interface index, however, is that all of the ipif's that
6205 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6206 	 * cannot be used to differentiate between ipif's (or logical
6207 	 * interfaces) that belong to the same ill (physical interface).
6208 	 *
6209 	 * For example, in the following case involving IPv4 interfaces and
6210 	 * logical interfaces
6211 	 *
6212 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6213 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6214 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6215 	 *
6216 	 * the ipif's corresponding to each of these interface routes can be
6217 	 * uniquely identified by the "gateway" (actually interface address).
6218 	 *
6219 	 * In this case involving multiple IPv6 default routes to a particular
6220 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6221 	 * default route is of interest:
6222 	 *
6223 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6224 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6225 	 */
6226 
6227 	/* RTF_GATEWAY not set */
6228 	if (!(flags & RTF_GATEWAY)) {
6229 		queue_t	*stq;
6230 		queue_t	*rfq = NULL;
6231 		ill_t	*in_ill = NULL;
6232 
6233 		/*
6234 		 * As the interface index specified with the RTA_IFP sockaddr is
6235 		 * the same for all ipif's off of an ill, the matching logic
6236 		 * below uses MATCH_IRE_ILL if such an index was specified.
6237 		 * This means that routes sharing the same prefix when added
6238 		 * using a RTA_IFP sockaddr must have distinct interface
6239 		 * indices (namely, they must be on distinct ill's).
6240 		 *
6241 		 * On the other hand, since the gateway address will usually be
6242 		 * different for each ipif on the system, the matching logic
6243 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6244 		 * route.  This means that interface routes for the same prefix
6245 		 * can be created if they belong to distinct ipif's and if a
6246 		 * RTA_IFP sockaddr is not present.
6247 		 */
6248 		if (ipif_arg != NULL) {
6249 			if (ipif_refheld)  {
6250 				ipif_refrele(ipif);
6251 				ipif_refheld = B_FALSE;
6252 			}
6253 			ipif = ipif_arg;
6254 			match_flags |= MATCH_IRE_ILL;
6255 		} else {
6256 			/*
6257 			 * Check the ipif corresponding to the gw_addr
6258 			 */
6259 			if (ipif == NULL)
6260 				return (ENETUNREACH);
6261 			match_flags |= MATCH_IRE_IPIF;
6262 		}
6263 		ASSERT(ipif != NULL);
6264 		/*
6265 		 * If src_ipif is not NULL, we have to create
6266 		 * an ire with non-null ire_in_ill value
6267 		 */
6268 		if (src_ipif != NULL) {
6269 			in_ill = src_ipif->ipif_ill;
6270 		}
6271 
6272 		/*
6273 		 * We check for an existing entry at this point.
6274 		 *
6275 		 * Since a netmask isn't passed in via the ioctl interface
6276 		 * (SIOCADDRT), we don't check for a matching netmask in that
6277 		 * case.
6278 		 */
6279 		if (!ioctl_msg)
6280 			match_flags |= MATCH_IRE_MASK;
6281 		if (src_ipif != NULL) {
6282 			/* Look up in the special table */
6283 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
6284 			    ipif, src_ipif->ipif_ill, match_flags);
6285 		} else {
6286 			ire = ire_ftable_lookup(dst_addr, mask, 0,
6287 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
6288 			    match_flags);
6289 		}
6290 		if (ire != NULL) {
6291 			ire_refrele(ire);
6292 			if (ipif_refheld)
6293 				ipif_refrele(ipif);
6294 			return (EEXIST);
6295 		}
6296 
6297 		if (src_ipif != NULL) {
6298 			/*
6299 			 * Create the special ire for the IRE table
6300 			 * which hangs out of ire_in_ill. This ire
6301 			 * is in-between IRE_CACHE and IRE_INTERFACE.
6302 			 * Thus rfq is non-NULL.
6303 			 */
6304 			rfq = ipif->ipif_rq;
6305 		}
6306 		/* Create the usual interface ires */
6307 
6308 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6309 		    ? ipif->ipif_rq : ipif->ipif_wq;
6310 
6311 		/*
6312 		 * Create a copy of the IRE_LOOPBACK,
6313 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6314 		 * the modified address and netmask.
6315 		 */
6316 		ire = ire_create(
6317 		    (uchar_t *)&dst_addr,
6318 		    (uint8_t *)&mask,
6319 		    (uint8_t *)&ipif->ipif_src_addr,
6320 		    NULL,
6321 		    NULL,
6322 		    &ipif->ipif_mtu,
6323 		    NULL,
6324 		    rfq,
6325 		    stq,
6326 		    ipif->ipif_net_type,
6327 		    ipif->ipif_resolver_mp,
6328 		    ipif,
6329 		    in_ill,
6330 		    0,
6331 		    0,
6332 		    0,
6333 		    flags,
6334 		    &ire_uinfo_null);
6335 		if (ire == NULL) {
6336 			if (ipif_refheld)
6337 				ipif_refrele(ipif);
6338 			return (ENOMEM);
6339 		}
6340 
6341 		/*
6342 		 * Some software (for example, GateD and Sun Cluster) attempts
6343 		 * to create (what amount to) IRE_PREFIX routes with the
6344 		 * loopback address as the gateway.  This is primarily done to
6345 		 * set up prefixes with the RTF_REJECT flag set (for example,
6346 		 * when generating aggregate routes.)
6347 		 *
6348 		 * If the IRE type (as defined by ipif->ipif_net_type) is
6349 		 * IRE_LOOPBACK, then we map the request into a
6350 		 * IRE_IF_NORESOLVER.
6351 		 *
6352 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
6353 		 * routine, but rather using ire_create() directly.
6354 		 */
6355 		if (ipif->ipif_net_type == IRE_LOOPBACK)
6356 			ire->ire_type = IRE_IF_NORESOLVER;
6357 		error = ire_add(&ire, q, mp, func);
6358 		if (error == 0)
6359 			goto save_ire;
6360 
6361 		/*
6362 		 * In the result of failure, ire_add() will have already
6363 		 * deleted the ire in question, so there is no need to
6364 		 * do that here.
6365 		 */
6366 		if (ipif_refheld)
6367 			ipif_refrele(ipif);
6368 		return (error);
6369 	}
6370 	if (ipif_refheld) {
6371 		ipif_refrele(ipif);
6372 		ipif_refheld = B_FALSE;
6373 	}
6374 
6375 	if (src_ipif != NULL) {
6376 		/* RTA_SRCIFP is not supported on RTF_GATEWAY */
6377 		ip2dbg(("ip_rt_add: SRCIF cannot be set with gateway route\n"));
6378 		return (EINVAL);
6379 	}
6380 	/*
6381 	 * Get an interface IRE for the specified gateway.
6382 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
6383 	 * gateway, it is currently unreachable and we fail the request
6384 	 * accordingly.
6385 	 */
6386 	ipif = ipif_arg;
6387 	if (ipif_arg != NULL)
6388 		match_flags |= MATCH_IRE_ILL;
6389 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
6390 	    ALL_ZONES, 0, match_flags);
6391 	if (gw_ire == NULL)
6392 		return (ENETUNREACH);
6393 
6394 	/*
6395 	 * We create one of three types of IREs as a result of this request
6396 	 * based on the netmask.  A netmask of all ones (which is automatically
6397 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
6398 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
6399 	 * created.  Otherwise, an IRE_PREFIX route is created for the
6400 	 * destination prefix.
6401 	 */
6402 	if (mask == IP_HOST_MASK)
6403 		type = IRE_HOST;
6404 	else if (mask == 0)
6405 		type = IRE_DEFAULT;
6406 	else
6407 		type = IRE_PREFIX;
6408 
6409 	/* check for a duplicate entry */
6410 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
6411 	    NULL, ALL_ZONES, 0, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW);
6412 	if (ire != NULL) {
6413 		ire_refrele(gw_ire);
6414 		ire_refrele(ire);
6415 		return (EEXIST);
6416 	}
6417 
6418 	/* Create the IRE. */
6419 	ire = ire_create(
6420 	    (uchar_t *)&dst_addr,		/* dest address */
6421 	    (uchar_t *)&mask,			/* mask */
6422 	    /* src address assigned by the caller? */
6423 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
6424 		(flags & RTF_SETSRC)) ?  &src_addr : NULL),
6425 	    (uchar_t *)&gw_addr,		/* gateway address */
6426 	    NULL,				/* no in-srcaddress */
6427 	    &gw_ire->ire_max_frag,
6428 	    NULL,				/* no Fast Path header */
6429 	    NULL,				/* no recv-from queue */
6430 	    NULL,				/* no send-to queue */
6431 	    (ushort_t)type,			/* IRE type */
6432 	    NULL,
6433 	    ipif_arg,
6434 	    NULL,
6435 	    0,
6436 	    0,
6437 	    0,
6438 	    flags,
6439 	    &gw_ire->ire_uinfo);		/* Inherit ULP info from gw */
6440 	if (ire == NULL) {
6441 		ire_refrele(gw_ire);
6442 		return (ENOMEM);
6443 	}
6444 
6445 	/*
6446 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
6447 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
6448 	 */
6449 
6450 	/* Add the new IRE. */
6451 	error = ire_add(&ire, q, mp, func);
6452 	if (error != 0) {
6453 		/*
6454 		 * In the result of failure, ire_add() will have already
6455 		 * deleted the ire in question, so there is no need to
6456 		 * do that here.
6457 		 */
6458 		ire_refrele(gw_ire);
6459 		return (error);
6460 	}
6461 
6462 	if (flags & RTF_MULTIRT) {
6463 		/*
6464 		 * Invoke the CGTP (multirouting) filtering module
6465 		 * to add the dst address in the filtering database.
6466 		 * Replicated inbound packets coming from that address
6467 		 * will be filtered to discard the duplicates.
6468 		 * It is not necessary to call the CGTP filter hook
6469 		 * when the dst address is a broadcast or multicast,
6470 		 * because an IP source address cannot be a broadcast
6471 		 * or a multicast.
6472 		 */
6473 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
6474 		    IRE_BROADCAST, NULL, NULL, MATCH_IRE_TYPE);
6475 		if (ire_dst != NULL) {
6476 			ip_cgtp_bcast_add(ire, ire_dst);
6477 			ire_refrele(ire_dst);
6478 			goto save_ire;
6479 		}
6480 		if ((ip_cgtp_filter_ops != NULL) && !CLASSD(ire->ire_addr)) {
6481 			int res = ip_cgtp_filter_ops->cfo_add_dest_v4(
6482 			    ire->ire_addr,
6483 			    ire->ire_gateway_addr,
6484 			    ire->ire_src_addr,
6485 			    gw_ire->ire_src_addr);
6486 			if (res != 0) {
6487 				ire_refrele(gw_ire);
6488 				ire_delete(ire);
6489 				return (res);
6490 			}
6491 		}
6492 	}
6493 
6494 save_ire:
6495 	if (gw_ire != NULL) {
6496 		ire_refrele(gw_ire);
6497 	}
6498 	/*
6499 	 * We do not do save_ire for the routes added with RTA_SRCIFP
6500 	 * flag. This route is only added and deleted by mipagent.
6501 	 * So, for simplicity of design, we refrain from saving
6502 	 * ires that are created with srcif value. This may change
6503 	 * in future if we find more usage of srcifp feature.
6504 	 */
6505 	if (ipif != NULL && src_ipif == NULL) {
6506 		/*
6507 		 * Save enough information so that we can recreate the IRE if
6508 		 * the interface goes down and then up.  The metrics associated
6509 		 * with the route will be saved as well when rts_setmetrics() is
6510 		 * called after the IRE has been created.  In the case where
6511 		 * memory cannot be allocated, none of this information will be
6512 		 * saved.
6513 		 */
6514 		ipif_save_ire(ipif, ire);
6515 	}
6516 	if (ioctl_msg)
6517 		ip_rts_rtmsg(RTM_OLDADD, ire, 0);
6518 	if (ire_arg != NULL) {
6519 		/*
6520 		 * Store the ire that was successfully added into where ire_arg
6521 		 * points to so that callers don't have to look it up
6522 		 * themselves (but they are responsible for ire_refrele()ing
6523 		 * the ire when they are finished with it).
6524 		 */
6525 		*ire_arg = ire;
6526 	} else {
6527 		ire_refrele(ire);		/* Held in ire_add */
6528 	}
6529 	if (ipif_refheld)
6530 		ipif_refrele(ipif);
6531 	return (0);
6532 }
6533 
6534 /*
6535  * ip_rt_delete is called to delete an IPv4 route.
6536  * ipif_arg is passed in to associate it with the correct interface.
6537  * src_ipif is passed to associate the incoming interface of the packet.
6538  * We may need to restart this operation if the ipif cannot be looked up
6539  * due to an exclusive operation that is currently in progress. The restart
6540  * entry point is specified by 'func'
6541  */
6542 /* ARGSUSED4 */
6543 int
6544 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6545     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6546     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func)
6547 {
6548 	ire_t	*ire = NULL;
6549 	ipif_t	*ipif;
6550 	boolean_t ipif_refheld = B_FALSE;
6551 	uint_t	type;
6552 	uint_t	match_flags = MATCH_IRE_TYPE;
6553 	int	err = 0;
6554 
6555 	ip1dbg(("ip_rt_delete:"));
6556 	/*
6557 	 * If this is the case of RTF_HOST being set, then we set the netmask
6558 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
6559 	 */
6560 	if (flags & RTF_HOST) {
6561 		mask = IP_HOST_MASK;
6562 		match_flags |= MATCH_IRE_MASK;
6563 	} else if (rtm_addrs & RTA_NETMASK) {
6564 		match_flags |= MATCH_IRE_MASK;
6565 	}
6566 
6567 	/*
6568 	 * Note that RTF_GATEWAY is never set on a delete, therefore
6569 	 * we check if the gateway address is one of our interfaces first,
6570 	 * and fall back on RTF_GATEWAY routes.
6571 	 *
6572 	 * This makes it possible to delete an original
6573 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
6574 	 *
6575 	 * As the interface index specified with the RTA_IFP sockaddr is the
6576 	 * same for all ipif's off of an ill, the matching logic below uses
6577 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
6578 	 * sharing the same prefix and interface index as the the route
6579 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
6580 	 * is specified in the request.
6581 	 *
6582 	 * On the other hand, since the gateway address will usually be
6583 	 * different for each ipif on the system, the matching logic
6584 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
6585 	 * route.  This means that interface routes for the same prefix can be
6586 	 * uniquely identified if they belong to distinct ipif's and if a
6587 	 * RTA_IFP sockaddr is not present.
6588 	 *
6589 	 * For more detail on specifying routes by gateway address and by
6590 	 * interface index, see the comments in ip_rt_add().
6591 	 * gw_addr could be zero in some cases when both RTA_SRCIFP and
6592 	 * RTA_IFP are specified. If RTA_SRCIFP is specified and  both
6593 	 * RTA_IFP and gateway_addr are NULL/zero, then delete will not
6594 	 * succeed.
6595 	 */
6596 	if (src_ipif != NULL) {
6597 		if (ipif_arg == NULL && gw_addr != 0) {
6598 			ipif_arg = ipif_lookup_interface(gw_addr, dst_addr,
6599 			    q, mp, func, &err);
6600 			if (ipif_arg != NULL)
6601 				ipif_refheld = B_TRUE;
6602 		}
6603 		if (ipif_arg == NULL) {
6604 			err = (err == EINPROGRESS) ? err : ESRCH;
6605 			return (err);
6606 		}
6607 		ipif = ipif_arg;
6608 	} else {
6609 		ipif = ipif_lookup_interface(gw_addr, dst_addr,
6610 			    q, mp, func, &err);
6611 		if (ipif != NULL)
6612 			ipif_refheld = B_TRUE;
6613 		else if (err == EINPROGRESS)
6614 			return (err);
6615 		else
6616 			err = 0;
6617 	}
6618 	if (ipif != NULL) {
6619 		if (ipif_arg != NULL) {
6620 			if (ipif_refheld) {
6621 				ipif_refrele(ipif);
6622 				ipif_refheld = B_FALSE;
6623 			}
6624 			ipif = ipif_arg;
6625 			match_flags |= MATCH_IRE_ILL;
6626 		} else {
6627 			match_flags |= MATCH_IRE_IPIF;
6628 		}
6629 		if (src_ipif != NULL) {
6630 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
6631 			    ipif, src_ipif->ipif_ill, match_flags);
6632 		} else {
6633 			if (ipif->ipif_ire_type == IRE_LOOPBACK) {
6634 				ire = ire_ctable_lookup(dst_addr, 0,
6635 				    IRE_LOOPBACK, ipif, ALL_ZONES, match_flags);
6636 			}
6637 			if (ire == NULL) {
6638 				ire = ire_ftable_lookup(dst_addr, mask, 0,
6639 				    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
6640 				    match_flags);
6641 			}
6642 		}
6643 	}
6644 
6645 	if (ire == NULL) {
6646 		/*
6647 		 * At this point, the gateway address is not one of our own
6648 		 * addresses or a matching interface route was not found.  We
6649 		 * set the IRE type to lookup based on whether
6650 		 * this is a host route, a default route or just a prefix.
6651 		 *
6652 		 * If an ipif_arg was passed in, then the lookup is based on an
6653 		 * interface index so MATCH_IRE_ILL is added to match_flags.
6654 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
6655 		 * set as the route being looked up is not a traditional
6656 		 * interface route.
6657 		 * Since we do not add gateway route with srcipif, we don't
6658 		 * expect to find it either.
6659 		 */
6660 		if (src_ipif != NULL) {
6661 			if (ipif_refheld)
6662 				ipif_refrele(ipif);
6663 			return (ESRCH);
6664 		} else {
6665 			match_flags &= ~MATCH_IRE_IPIF;
6666 			match_flags |= MATCH_IRE_GW;
6667 			if (ipif_arg != NULL)
6668 				match_flags |= MATCH_IRE_ILL;
6669 			if (mask == IP_HOST_MASK)
6670 				type = IRE_HOST;
6671 			else if (mask == 0)
6672 				type = IRE_DEFAULT;
6673 			else
6674 				type = IRE_PREFIX;
6675 			ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type,
6676 			    ipif_arg, NULL, ALL_ZONES, 0, match_flags);
6677 			if (ire == NULL && type == IRE_HOST) {
6678 				ire = ire_ftable_lookup(dst_addr, mask, gw_addr,
6679 				    IRE_HOST_REDIRECT, ipif_arg, NULL,
6680 				    ALL_ZONES, 0, match_flags);
6681 			}
6682 		}
6683 	}
6684 
6685 	if (ipif_refheld)
6686 		ipif_refrele(ipif);
6687 
6688 	/* ipif is not refheld anymore */
6689 	if (ire == NULL)
6690 		return (ESRCH);
6691 
6692 	if (ire->ire_flags & RTF_MULTIRT) {
6693 		/*
6694 		 * Invoke the CGTP (multirouting) filtering module
6695 		 * to remove the dst address from the filtering database.
6696 		 * Packets coming from that address will no longer be
6697 		 * filtered to remove duplicates.
6698 		 */
6699 		if (ip_cgtp_filter_ops != NULL) {
6700 			err = ip_cgtp_filter_ops->cfo_del_dest_v4(ire->ire_addr,
6701 			    ire->ire_gateway_addr);
6702 		}
6703 		ip_cgtp_bcast_delete(ire);
6704 	}
6705 
6706 	ipif = ire->ire_ipif;
6707 	/*
6708 	 * Removing from ipif_saved_ire_mp is not necessary
6709 	 * when src_ipif being non-NULL. ip_rt_add does not
6710 	 * save the ires which src_ipif being non-NULL.
6711 	 */
6712 	if (ipif != NULL && src_ipif == NULL) {
6713 		ipif_remove_ire(ipif, ire);
6714 	}
6715 	if (ioctl_msg)
6716 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0);
6717 	ire_delete(ire);
6718 	ire_refrele(ire);
6719 	return (err);
6720 }
6721 
6722 /*
6723  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6724  */
6725 /* ARGSUSED */
6726 int
6727 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6728     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6729 {
6730 	ipaddr_t dst_addr;
6731 	ipaddr_t gw_addr;
6732 	ipaddr_t mask;
6733 	int error = 0;
6734 	mblk_t *mp1;
6735 	struct rtentry *rt;
6736 	ipif_t *ipif = NULL;
6737 
6738 	ip1dbg(("ip_siocaddrt:"));
6739 	/* Existence of mp1 verified in ip_wput_nondata */
6740 	mp1 = mp->b_cont->b_cont;
6741 	rt = (struct rtentry *)mp1->b_rptr;
6742 
6743 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6744 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6745 
6746 	/*
6747 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6748 	 * to a particular host address.  In this case, we set the netmask to
6749 	 * all ones for the particular destination address.  Otherwise,
6750 	 * determine the netmask to be used based on dst_addr and the interfaces
6751 	 * in use.
6752 	 */
6753 	if (rt->rt_flags & RTF_HOST) {
6754 		mask = IP_HOST_MASK;
6755 	} else {
6756 		/*
6757 		 * Note that ip_subnet_mask returns a zero mask in the case of
6758 		 * default (an all-zeroes address).
6759 		 */
6760 		mask = ip_subnet_mask(dst_addr, &ipif);
6761 	}
6762 
6763 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags,
6764 	    NULL, NULL, NULL, B_TRUE, q, mp, ip_process_ioctl);
6765 	if (ipif != NULL)
6766 		ipif_refrele(ipif);
6767 	return (error);
6768 }
6769 
6770 /*
6771  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6772  */
6773 /* ARGSUSED */
6774 int
6775 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6776     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6777 {
6778 	ipaddr_t dst_addr;
6779 	ipaddr_t gw_addr;
6780 	ipaddr_t mask;
6781 	int error;
6782 	mblk_t *mp1;
6783 	struct rtentry *rt;
6784 	ipif_t *ipif = NULL;
6785 
6786 	ip1dbg(("ip_siocdelrt:"));
6787 	/* Existence of mp1 verified in ip_wput_nondata */
6788 	mp1 = mp->b_cont->b_cont;
6789 	rt = (struct rtentry *)mp1->b_rptr;
6790 
6791 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6792 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6793 
6794 	/*
6795 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6796 	 * to a particular host address.  In this case, we set the netmask to
6797 	 * all ones for the particular destination address.  Otherwise,
6798 	 * determine the netmask to be used based on dst_addr and the interfaces
6799 	 * in use.
6800 	 */
6801 	if (rt->rt_flags & RTF_HOST) {
6802 		mask = IP_HOST_MASK;
6803 	} else {
6804 		/*
6805 		 * Note that ip_subnet_mask returns a zero mask in the case of
6806 		 * default (an all-zeroes address).
6807 		 */
6808 		mask = ip_subnet_mask(dst_addr, &ipif);
6809 	}
6810 
6811 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6812 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, NULL,
6813 	    B_TRUE, q, mp, ip_process_ioctl);
6814 	if (ipif != NULL)
6815 		ipif_refrele(ipif);
6816 	return (error);
6817 }
6818 
6819 /*
6820  * Enqueue the mp onto the ipsq, chained by b_next.
6821  * b_prev stores the function to be executed later, and b_queue the queue
6822  * where this mp originated.
6823  */
6824 void
6825 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6826     ill_t *pending_ill)
6827 {
6828 	conn_t	*connp = NULL;
6829 
6830 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6831 	ASSERT(func != NULL);
6832 
6833 	mp->b_queue = q;
6834 	mp->b_prev = (void *)func;
6835 	mp->b_next = NULL;
6836 
6837 	switch (type) {
6838 	case CUR_OP:
6839 		if (ipsq->ipsq_mptail != NULL) {
6840 			ASSERT(ipsq->ipsq_mphead != NULL);
6841 			ipsq->ipsq_mptail->b_next = mp;
6842 		} else {
6843 			ASSERT(ipsq->ipsq_mphead == NULL);
6844 			ipsq->ipsq_mphead = mp;
6845 		}
6846 		ipsq->ipsq_mptail = mp;
6847 		break;
6848 
6849 	case NEW_OP:
6850 		if (ipsq->ipsq_xopq_mptail != NULL) {
6851 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6852 			ipsq->ipsq_xopq_mptail->b_next = mp;
6853 		} else {
6854 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6855 			ipsq->ipsq_xopq_mphead = mp;
6856 		}
6857 		ipsq->ipsq_xopq_mptail = mp;
6858 		break;
6859 	default:
6860 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6861 	}
6862 
6863 	if (CONN_Q(q) && pending_ill != NULL) {
6864 		connp = Q_TO_CONN(q);
6865 
6866 		ASSERT(MUTEX_HELD(&connp->conn_lock));
6867 		connp->conn_oper_pending_ill = pending_ill;
6868 	}
6869 }
6870 
6871 /*
6872  * Return the mp at the head of the ipsq. After emptying the ipsq
6873  * look at the next ioctl, if this ioctl is complete. Otherwise
6874  * return, we will resume when we complete the current ioctl.
6875  * The current ioctl will wait till it gets a response from the
6876  * driver below.
6877  */
6878 static mblk_t *
6879 ipsq_dq(ipsq_t *ipsq)
6880 {
6881 	mblk_t	*mp;
6882 
6883 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6884 
6885 	mp = ipsq->ipsq_mphead;
6886 	if (mp != NULL) {
6887 		ipsq->ipsq_mphead = mp->b_next;
6888 		if (ipsq->ipsq_mphead == NULL)
6889 			ipsq->ipsq_mptail = NULL;
6890 		mp->b_next = NULL;
6891 		return (mp);
6892 	}
6893 	if (ipsq->ipsq_current_ipif != NULL)
6894 		return (NULL);
6895 	mp = ipsq->ipsq_xopq_mphead;
6896 	if (mp != NULL) {
6897 		ipsq->ipsq_xopq_mphead = mp->b_next;
6898 		if (ipsq->ipsq_xopq_mphead == NULL)
6899 			ipsq->ipsq_xopq_mptail = NULL;
6900 		mp->b_next = NULL;
6901 		return (mp);
6902 	}
6903 	return (NULL);
6904 }
6905 
6906 /*
6907  * Enter the ipsq corresponding to ill, by waiting synchronously till
6908  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
6909  * will have to drain completely before ipsq_enter returns success.
6910  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
6911  * and the ipsq_exit logic will start the next enqueued ioctl after
6912  * completion of the current ioctl. If 'force' is used, we don't wait
6913  * for the enqueued ioctls. This is needed when a conn_close wants to
6914  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
6915  * of an ill can also use this option. But we dont' use it currently.
6916  */
6917 #define	ENTER_SQ_WAIT_TICKS 100
6918 boolean_t
6919 ipsq_enter(ill_t *ill, boolean_t force)
6920 {
6921 	ipsq_t	*ipsq;
6922 	boolean_t waited_enough = B_FALSE;
6923 
6924 	/*
6925 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
6926 	 * Since the <ill-ipsq> assocs could change while we wait for the
6927 	 * writer, it is easier to wait on a fixed global rather than try to
6928 	 * cv_wait on a changing ipsq.
6929 	 */
6930 	mutex_enter(&ill->ill_lock);
6931 	for (;;) {
6932 		if (ill->ill_state_flags & ILL_CONDEMNED) {
6933 			mutex_exit(&ill->ill_lock);
6934 			return (B_FALSE);
6935 		}
6936 
6937 		ipsq = ill->ill_phyint->phyint_ipsq;
6938 		mutex_enter(&ipsq->ipsq_lock);
6939 		if (ipsq->ipsq_writer == NULL &&
6940 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
6941 			break;
6942 		} else if (ipsq->ipsq_writer != NULL) {
6943 			mutex_exit(&ipsq->ipsq_lock);
6944 			cv_wait(&ill->ill_cv, &ill->ill_lock);
6945 		} else {
6946 			mutex_exit(&ipsq->ipsq_lock);
6947 			if (force) {
6948 				(void) cv_timedwait(&ill->ill_cv,
6949 				    &ill->ill_lock,
6950 				    lbolt + ENTER_SQ_WAIT_TICKS);
6951 				waited_enough = B_TRUE;
6952 				continue;
6953 			} else {
6954 				cv_wait(&ill->ill_cv, &ill->ill_lock);
6955 			}
6956 		}
6957 	}
6958 
6959 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
6960 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
6961 	ipsq->ipsq_writer = curthread;
6962 	ipsq->ipsq_reentry_cnt++;
6963 #ifdef ILL_DEBUG
6964 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
6965 #endif
6966 	mutex_exit(&ipsq->ipsq_lock);
6967 	mutex_exit(&ill->ill_lock);
6968 	return (B_TRUE);
6969 }
6970 
6971 /*
6972  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
6973  * certain critical operations like plumbing (i.e. most set ioctls),
6974  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
6975  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
6976  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
6977  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
6978  * threads executing in the ipsq. Responses from the driver pertain to the
6979  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
6980  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
6981  *
6982  * If a thread does not want to reenter the ipsq when it is already writer,
6983  * it must make sure that the specified reentry point to be called later
6984  * when the ipsq is empty, nor any code path starting from the specified reentry
6985  * point must never ever try to enter the ipsq again. Otherwise it can lead
6986  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
6987  * When the thread that is currently exclusive finishes, it (ipsq_exit)
6988  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
6989  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
6990  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
6991  * ioctl if the current ioctl has completed. If the current ioctl is still
6992  * in progress it simply returns. The current ioctl could be waiting for
6993  * a response from another module (arp_ or the driver or could be waiting for
6994  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
6995  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
6996  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
6997  * ipsq_current_ipif is clear which happens only on ioctl completion.
6998  */
6999 
7000 /*
7001  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7002  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7003  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7004  * completion.
7005  */
7006 ipsq_t *
7007 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7008     ipsq_func_t func, int type, boolean_t reentry_ok)
7009 {
7010 	ipsq_t	*ipsq;
7011 
7012 	/* Only 1 of ipif or ill can be specified */
7013 	ASSERT((ipif != NULL) ^ (ill != NULL));
7014 	if (ipif != NULL)
7015 		ill = ipif->ipif_ill;
7016 
7017 	/*
7018 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7019 	 * ipsq of an ill can't change when ill_lock is held.
7020 	 */
7021 	GRAB_CONN_LOCK(q);
7022 	mutex_enter(&ill->ill_lock);
7023 	ipsq = ill->ill_phyint->phyint_ipsq;
7024 	mutex_enter(&ipsq->ipsq_lock);
7025 
7026 	/*
7027 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7028 	 *    (Note: If the caller does not specify reentry_ok then neither
7029 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7030 	 *    again. Otherwise it can lead to an infinite loop
7031 	 * 2. Enter the ipsq if there is no current writer and this attempted
7032 	 *    entry is part of the current ioctl or operation
7033 	 * 3. Enter the ipsq if there is no current writer and this is a new
7034 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7035 	 *    empty and there is no ioctl (or operation) currently in progress
7036 	 */
7037 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7038 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7039 	    ipsq->ipsq_current_ipif == NULL))) ||
7040 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7041 		/* Success. */
7042 		ipsq->ipsq_reentry_cnt++;
7043 		ipsq->ipsq_writer = curthread;
7044 		mutex_exit(&ipsq->ipsq_lock);
7045 		mutex_exit(&ill->ill_lock);
7046 		RELEASE_CONN_LOCK(q);
7047 #ifdef ILL_DEBUG
7048 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7049 #endif
7050 		return (ipsq);
7051 	}
7052 
7053 	ipsq_enq(ipsq, q, mp, func, type, ill);
7054 
7055 	mutex_exit(&ipsq->ipsq_lock);
7056 	mutex_exit(&ill->ill_lock);
7057 	RELEASE_CONN_LOCK(q);
7058 	return (NULL);
7059 }
7060 
7061 /*
7062  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7063  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7064  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7065  * completion.
7066  *
7067  * This function does a refrele on the ipif/ill.
7068  */
7069 void
7070 qwriter_ip(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7071     ipsq_func_t func, int type, boolean_t reentry_ok)
7072 {
7073 	ipsq_t	*ipsq;
7074 
7075 	ipsq = ipsq_try_enter(ipif, ill, q, mp, func, type, reentry_ok);
7076 	/*
7077 	 * Caller must have done a refhold on the ipif. ipif_refrele
7078 	 * happens on the passed ipif. We can do this since we are
7079 	 * already exclusive, or we won't access ipif henceforth, Both
7080 	 * this func and caller will just return if we ipsq_try_enter
7081 	 * fails above. This is needed because func needs to
7082 	 * see the correct refcount. Eg. removeif can work only then.
7083 	 */
7084 	if (ipif != NULL)
7085 		ipif_refrele(ipif);
7086 	else
7087 		ill_refrele(ill);
7088 	if (ipsq != NULL) {
7089 		(*func)(ipsq, q, mp, NULL);
7090 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7091 	}
7092 }
7093 
7094 /*
7095  * If there are more than ILL_GRP_CNT ills in a group,
7096  * we use kmem alloc'd buffers, else use the stack
7097  */
7098 #define	ILL_GRP_CNT	14
7099 /*
7100  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7101  * Called by a thread that is currently exclusive on this ipsq.
7102  */
7103 void
7104 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7105 {
7106 	queue_t	*q;
7107 	mblk_t	*mp;
7108 	ipsq_func_t	func;
7109 	int	next;
7110 	ill_t	**ill_list = NULL;
7111 	size_t	ill_list_size = 0;
7112 	int	cnt = 0;
7113 	boolean_t need_ipsq_free = B_FALSE;
7114 
7115 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7116 	mutex_enter(&ipsq->ipsq_lock);
7117 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7118 	if (ipsq->ipsq_reentry_cnt != 1) {
7119 		ipsq->ipsq_reentry_cnt--;
7120 		mutex_exit(&ipsq->ipsq_lock);
7121 		return;
7122 	}
7123 
7124 	mp = ipsq_dq(ipsq);
7125 	while (mp != NULL) {
7126 again:
7127 		mutex_exit(&ipsq->ipsq_lock);
7128 		func = (ipsq_func_t)mp->b_prev;
7129 		q = (queue_t *)mp->b_queue;
7130 		mp->b_prev = NULL;
7131 		mp->b_queue = NULL;
7132 
7133 		/*
7134 		 * If 'q' is an conn queue, it is valid, since we did a
7135 		 * a refhold on the connp, at the start of the ioctl.
7136 		 * If 'q' is an ill queue, it is valid, since close of an
7137 		 * ill will clean up the 'ipsq'.
7138 		 */
7139 		(*func)(ipsq, q, mp, NULL);
7140 
7141 		mutex_enter(&ipsq->ipsq_lock);
7142 		mp = ipsq_dq(ipsq);
7143 	}
7144 
7145 	mutex_exit(&ipsq->ipsq_lock);
7146 
7147 	/*
7148 	 * Need to grab the locks in the right order. Need to
7149 	 * atomically check (under ipsq_lock) that there are no
7150 	 * messages before relinquishing the ipsq. Also need to
7151 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7152 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7153 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7154 	 * to grab ill_g_lock as writer.
7155 	 */
7156 	rw_enter(&ill_g_lock, ipsq->ipsq_split ? RW_WRITER : RW_READER);
7157 
7158 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7159 	if (ipsq->ipsq_refs != 0) {
7160 		/* At most 2 ills v4/v6 per phyint */
7161 		cnt = ipsq->ipsq_refs << 1;
7162 		ill_list_size = cnt * sizeof (ill_t *);
7163 		/*
7164 		 * If memory allocation fails, we will do the split
7165 		 * the next time ipsq_exit is called for whatever reason.
7166 		 * As long as the ipsq_split flag is set the need to
7167 		 * split is remembered.
7168 		 */
7169 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7170 		if (ill_list != NULL)
7171 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7172 	}
7173 	mutex_enter(&ipsq->ipsq_lock);
7174 	mp = ipsq_dq(ipsq);
7175 	if (mp != NULL) {
7176 		/* oops, some message has landed up, we can't get out */
7177 		if (ill_list != NULL)
7178 			ill_unlock_ills(ill_list, cnt);
7179 		rw_exit(&ill_g_lock);
7180 		if (ill_list != NULL)
7181 			kmem_free(ill_list, ill_list_size);
7182 		ill_list = NULL;
7183 		ill_list_size = 0;
7184 		cnt = 0;
7185 		goto again;
7186 	}
7187 
7188 	/*
7189 	 * Split only if no ioctl is pending and if memory alloc succeeded
7190 	 * above.
7191 	 */
7192 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7193 		ill_list != NULL) {
7194 		/*
7195 		 * No new ill can join this ipsq since we are holding the
7196 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7197 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7198 		 * If so we will retry on the next ipsq_exit.
7199 		 */
7200 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7201 	}
7202 
7203 	/*
7204 	 * We are holding the ipsq lock, hence no new messages can
7205 	 * land up on the ipsq, and there are no messages currently.
7206 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7207 	 * atomically while holding ill locks.
7208 	 */
7209 	ipsq->ipsq_writer = NULL;
7210 	ipsq->ipsq_reentry_cnt--;
7211 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7212 #ifdef ILL_DEBUG
7213 	ipsq->ipsq_depth = 0;
7214 #endif
7215 	mutex_exit(&ipsq->ipsq_lock);
7216 	/*
7217 	 * For IPMP this should wake up all ills in this ipsq.
7218 	 * We need to hold the ill_lock while waking up waiters to
7219 	 * avoid missed wakeups. But there is no need to acquire all
7220 	 * the ill locks and then wakeup. If we have not acquired all
7221 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7222 	 * wakes up ills one at a time after getting the right ill_lock
7223 	 */
7224 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7225 	if (ill_list != NULL)
7226 		ill_unlock_ills(ill_list, cnt);
7227 	if (ipsq->ipsq_refs == 0)
7228 		need_ipsq_free = B_TRUE;
7229 	rw_exit(&ill_g_lock);
7230 	if (ill_list != 0)
7231 		kmem_free(ill_list, ill_list_size);
7232 
7233 	if (need_ipsq_free) {
7234 		/*
7235 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7236 		 * looked up. ipsq can be looked up only thru ill or phyint
7237 		 * and there are no ills/phyint on this ipsq.
7238 		 */
7239 		ipsq_delete(ipsq);
7240 	}
7241 	/*
7242 	 * Now start any igmp or mld timers that could not be started
7243 	 * while inside the ipsq. The timers can't be started while inside
7244 	 * the ipsq, since igmp_start_timers may need to call untimeout()
7245 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
7246 	 * there could be a deadlock since the timeout handlers
7247 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
7248 	 * wait in ipsq_enter() trying to get the ipsq.
7249 	 *
7250 	 * However there is one exception to the above. If this thread is
7251 	 * itself the igmp/mld timeout handler thread, then we don't want
7252 	 * to start any new timer until the current handler is done. The
7253 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
7254 	 * all others pass B_TRUE.
7255 	 */
7256 	if (start_igmp_timer) {
7257 		mutex_enter(&igmp_timer_lock);
7258 		next = igmp_deferred_next;
7259 		igmp_deferred_next = INFINITY;
7260 		mutex_exit(&igmp_timer_lock);
7261 
7262 		if (next != INFINITY)
7263 			igmp_start_timers(next);
7264 	}
7265 
7266 	if (start_mld_timer) {
7267 		mutex_enter(&mld_timer_lock);
7268 		next = mld_deferred_next;
7269 		mld_deferred_next = INFINITY;
7270 		mutex_exit(&mld_timer_lock);
7271 
7272 		if (next != INFINITY)
7273 			mld_start_timers(next);
7274 	}
7275 }
7276 
7277 /*
7278  * The ill is closing. Flush all messages on the ipsq that originated
7279  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
7280  * for this ill since ipsq_enter could not have entered until then.
7281  * New messages can't be queued since the CONDEMNED flag is set.
7282  */
7283 static void
7284 ipsq_flush(ill_t *ill)
7285 {
7286 	queue_t	*q;
7287 	mblk_t	*prev;
7288 	mblk_t	*mp;
7289 	mblk_t	*mp_next;
7290 	ipsq_t	*ipsq;
7291 
7292 	ASSERT(IAM_WRITER_ILL(ill));
7293 	ipsq = ill->ill_phyint->phyint_ipsq;
7294 	/*
7295 	 * Flush any messages sent up by the driver.
7296 	 */
7297 	mutex_enter(&ipsq->ipsq_lock);
7298 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
7299 		mp_next = mp->b_next;
7300 		q = mp->b_queue;
7301 		if (q == ill->ill_rq || q == ill->ill_wq) {
7302 			/* Remove the mp from the ipsq */
7303 			if (prev == NULL)
7304 				ipsq->ipsq_mphead = mp->b_next;
7305 			else
7306 				prev->b_next = mp->b_next;
7307 			if (ipsq->ipsq_mptail == mp) {
7308 				ASSERT(mp_next == NULL);
7309 				ipsq->ipsq_mptail = prev;
7310 			}
7311 			inet_freemsg(mp);
7312 		} else {
7313 			prev = mp;
7314 		}
7315 	}
7316 	mutex_exit(&ipsq->ipsq_lock);
7317 	(void) ipsq_pending_mp_cleanup(ill, NULL);
7318 	ipsq_xopq_mp_cleanup(ill, NULL);
7319 	ill_pending_mp_cleanup(ill);
7320 }
7321 
7322 /*
7323  * Clean up one squeue element. ill_inuse_ref is protected by ill_lock.
7324  * The real cleanup happens behind the squeue via ip_squeue_clean function but
7325  * we need to protect ourselfs from 2 threads trying to cleanup at the same
7326  * time (possible with one port going down for aggr and someone tearing down the
7327  * entire aggr simultaneously. So we use ill_inuse_ref protected by ill_lock
7328  * to indicate when the cleanup has started (1 ref) and when the cleanup
7329  * is done (0 ref). When a new ring gets assigned to squeue, we start by
7330  * putting 2 ref on ill_inuse_ref.
7331  */
7332 static void
7333 ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring)
7334 {
7335 	conn_t *connp;
7336 	squeue_t *sqp;
7337 	mblk_t *mp;
7338 
7339 	ASSERT(rx_ring != NULL);
7340 
7341 	/* Just clean one squeue */
7342 	mutex_enter(&ill->ill_lock);
7343 	while (rx_ring->rr_ring_state == ILL_RING_INPROC)
7344 		/* Some operations pending on the ring. Wait */
7345 		cv_wait(&ill->ill_cv, &ill->ill_lock);
7346 
7347 	if (rx_ring->rr_ring_state != ILL_RING_INUSE) {
7348 		/*
7349 		 * Someone already trying to clean
7350 		 * this squeue or its already been cleaned.
7351 		 */
7352 		mutex_exit(&ill->ill_lock);
7353 		return;
7354 	}
7355 	sqp = rx_ring->rr_sqp;
7356 
7357 	if (sqp == NULL) {
7358 		/*
7359 		 * The rx_ring never had a squeue assigned to it.
7360 		 * We are under ill_lock so we can clean it up
7361 		 * here itself since no one can get to it.
7362 		 */
7363 		rx_ring->rr_blank = NULL;
7364 		rx_ring->rr_handle = NULL;
7365 		rx_ring->rr_sqp = NULL;
7366 		rx_ring->rr_ring_state = ILL_RING_FREE;
7367 		mutex_exit(&ill->ill_lock);
7368 		return;
7369 	}
7370 
7371 	/* Set the state that its being cleaned */
7372 	rx_ring->rr_ring_state = ILL_RING_BEING_FREED;
7373 	ASSERT(sqp != NULL);
7374 	mutex_exit(&ill->ill_lock);
7375 
7376 	/*
7377 	 * Use the preallocated ill_unbind_conn for this purpose
7378 	 */
7379 	connp = ill->ill_poll_capab->ill_unbind_conn;
7380 	mp = &connp->conn_tcp->tcp_closemp;
7381 	CONN_INC_REF(connp);
7382 	squeue_enter(sqp, mp, ip_squeue_clean, connp, NULL);
7383 
7384 	mutex_enter(&ill->ill_lock);
7385 	while (rx_ring->rr_ring_state != ILL_RING_FREE)
7386 		cv_wait(&ill->ill_cv, &ill->ill_lock);
7387 
7388 	mutex_exit(&ill->ill_lock);
7389 }
7390 
7391 static void
7392 ipsq_clean_all(ill_t *ill)
7393 {
7394 	int idx;
7395 
7396 	/*
7397 	 * No need to clean if poll_capab isn't set for this ill
7398 	 */
7399 	if (!(ill->ill_capabilities & ILL_CAPAB_POLL))
7400 		return;
7401 
7402 	ill->ill_capabilities &= ~ILL_CAPAB_POLL;
7403 
7404 	for (idx = 0; idx < ILL_MAX_RINGS; idx++) {
7405 		ill_rx_ring_t *ipr = &ill->ill_poll_capab->ill_ring_tbl[idx];
7406 		ipsq_clean_ring(ill, ipr);
7407 	}
7408 }
7409 
7410 /* ARGSUSED */
7411 int
7412 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
7413     ip_ioctl_cmd_t *ipip, void *ifreq)
7414 {
7415 	ill_t	*ill;
7416 	struct lifreq	*lifr = (struct lifreq *)ifreq;
7417 	boolean_t isv6;
7418 	conn_t	*connp;
7419 
7420 	connp = Q_TO_CONN(q);
7421 	isv6 = connp->conn_af_isv6;
7422 	/*
7423 	 * Set original index.
7424 	 * Failover and failback move logical interfaces
7425 	 * from one physical interface to another.  The
7426 	 * original index indicates the parent of a logical
7427 	 * interface, in other words, the physical interface
7428 	 * the logical interface will be moved back to on
7429 	 * failback.
7430 	 */
7431 
7432 	/*
7433 	 * Don't allow the original index to be changed
7434 	 * for non-failover addresses, autoconfigured
7435 	 * addresses, or IPv6 link local addresses.
7436 	 */
7437 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
7438 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
7439 		return (EINVAL);
7440 	}
7441 	/*
7442 	 * The new original index must be in use by some
7443 	 * physical interface.
7444 	 */
7445 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
7446 	    NULL, NULL);
7447 	if (ill == NULL)
7448 		return (ENXIO);
7449 	ill_refrele(ill);
7450 
7451 	ipif->ipif_orig_ifindex = lifr->lifr_index;
7452 	/*
7453 	 * When this ipif gets failed back, don't
7454 	 * preserve the original id, as it is no
7455 	 * longer applicable.
7456 	 */
7457 	ipif->ipif_orig_ipifid = 0;
7458 	/*
7459 	 * For IPv4, change the original index of any
7460 	 * multicast addresses associated with the
7461 	 * ipif to the new value.
7462 	 */
7463 	if (!isv6) {
7464 		ilm_t *ilm;
7465 
7466 		mutex_enter(&ipif->ipif_ill->ill_lock);
7467 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
7468 		    ilm = ilm->ilm_next) {
7469 			if (ilm->ilm_ipif == ipif) {
7470 				ilm->ilm_orig_ifindex = lifr->lifr_index;
7471 			}
7472 		}
7473 		mutex_exit(&ipif->ipif_ill->ill_lock);
7474 	}
7475 	return (0);
7476 }
7477 
7478 /* ARGSUSED */
7479 int
7480 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
7481     ip_ioctl_cmd_t *ipip, void *ifreq)
7482 {
7483 	struct lifreq *lifr = (struct lifreq *)ifreq;
7484 
7485 	/*
7486 	 * Get the original interface index i.e the one
7487 	 * before FAILOVER if it ever happened.
7488 	 */
7489 	lifr->lifr_index = ipif->ipif_orig_ifindex;
7490 	return (0);
7491 }
7492 
7493 /*
7494  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
7495  * refhold and return the associated ipif
7496  */
7497 int
7498 ip_extract_tunreq(queue_t *q, mblk_t *mp, ipif_t **ipifp, ipsq_func_t func)
7499 {
7500 	boolean_t exists;
7501 	struct iftun_req *ta;
7502 	ipif_t	*ipif;
7503 	ill_t	*ill;
7504 	boolean_t isv6;
7505 	mblk_t	*mp1;
7506 	int	error;
7507 	conn_t	*connp;
7508 
7509 	/* Existence verified in ip_wput_nondata */
7510 	mp1 = mp->b_cont->b_cont;
7511 	ta = (struct iftun_req *)mp1->b_rptr;
7512 	/*
7513 	 * Null terminate the string to protect against buffer
7514 	 * overrun. String was generated by user code and may not
7515 	 * be trusted.
7516 	 */
7517 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
7518 
7519 	connp = Q_TO_CONN(q);
7520 	isv6 = connp->conn_af_isv6;
7521 
7522 	/* Disallows implicit create */
7523 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
7524 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
7525 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error);
7526 	if (ipif == NULL)
7527 		return (error);
7528 
7529 	if (ipif->ipif_id != 0) {
7530 		/*
7531 		 * We really don't want to set/get tunnel parameters
7532 		 * on virtual tunnel interfaces.  Only allow the
7533 		 * base tunnel to do these.
7534 		 */
7535 		ipif_refrele(ipif);
7536 		return (EINVAL);
7537 	}
7538 
7539 	/*
7540 	 * Send down to tunnel mod for ioctl processing.
7541 	 * Will finish ioctl in ip_rput_other().
7542 	 */
7543 	ill = ipif->ipif_ill;
7544 	if (ill->ill_net_type == IRE_LOOPBACK) {
7545 		ipif_refrele(ipif);
7546 		return (EOPNOTSUPP);
7547 	}
7548 
7549 	if (ill->ill_wq == NULL) {
7550 		ipif_refrele(ipif);
7551 		return (ENXIO);
7552 	}
7553 	/*
7554 	 * Mark the ioctl as coming from an IPv6 interface for
7555 	 * tun's convenience.
7556 	 */
7557 	if (ill->ill_isv6)
7558 		ta->ifta_flags |= 0x80000000;
7559 	*ipifp = ipif;
7560 	return (0);
7561 }
7562 
7563 /*
7564  * Parse an ifreq or lifreq struct coming down ioctls and refhold
7565  * and return the associated ipif.
7566  * Return value:
7567  *	Non zero: An error has occurred. ci may not be filled out.
7568  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
7569  *	a held ipif in ci.ci_ipif.
7570  */
7571 int
7572 ip_extract_lifreq_cmn(queue_t *q, mblk_t *mp, int cmd_type, int flags,
7573     cmd_info_t *ci, ipsq_func_t func)
7574 {
7575 	sin_t		*sin;
7576 	sin6_t		*sin6;
7577 	char		*name;
7578 	struct ifreq    *ifr;
7579 	struct lifreq    *lifr;
7580 	ipif_t		*ipif = NULL;
7581 	ill_t		*ill;
7582 	conn_t		*connp;
7583 	boolean_t	isv6;
7584 	struct iocblk   *iocp = (struct iocblk *)mp->b_rptr;
7585 	boolean_t	exists;
7586 	int		err;
7587 	mblk_t		*mp1;
7588 	zoneid_t	zoneid;
7589 
7590 	if (q->q_next != NULL) {
7591 		ill = (ill_t *)q->q_ptr;
7592 		isv6 = ill->ill_isv6;
7593 		connp = NULL;
7594 		zoneid = ALL_ZONES;
7595 	} else {
7596 		ill = NULL;
7597 		connp = Q_TO_CONN(q);
7598 		isv6 = connp->conn_af_isv6;
7599 		zoneid = connp->conn_zoneid;
7600 		if (zoneid == GLOBAL_ZONEID) {
7601 			/* global zone can access ipifs in all zones */
7602 			zoneid = ALL_ZONES;
7603 		}
7604 	}
7605 
7606 	/* Has been checked in ip_wput_nondata */
7607 	mp1 = mp->b_cont->b_cont;
7608 
7609 
7610 	if (cmd_type == IF_CMD) {
7611 		/* This a old style SIOC[GS]IF* command */
7612 		ifr = (struct ifreq *)mp1->b_rptr;
7613 		/*
7614 		 * Null terminate the string to protect against buffer
7615 		 * overrun. String was generated by user code and may not
7616 		 * be trusted.
7617 		 */
7618 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7619 		sin = (sin_t *)&ifr->ifr_addr;
7620 		name = ifr->ifr_name;
7621 		ci->ci_sin = sin;
7622 		ci->ci_sin6 = NULL;
7623 		ci->ci_lifr = (struct lifreq *)ifr;
7624 	} else {
7625 		/* This a new style SIOC[GS]LIF* command */
7626 		ASSERT(cmd_type == LIF_CMD);
7627 		lifr = (struct lifreq *)mp1->b_rptr;
7628 		/*
7629 		 * Null terminate the string to protect against buffer
7630 		 * overrun. String was generated by user code and may not
7631 		 * be trusted.
7632 		 */
7633 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7634 		name = lifr->lifr_name;
7635 		sin = (sin_t *)&lifr->lifr_addr;
7636 		sin6 = (sin6_t *)&lifr->lifr_addr;
7637 		if (iocp->ioc_cmd == SIOCSLIFGROUPNAME) {
7638 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
7639 			    LIFNAMSIZ);
7640 		}
7641 		ci->ci_sin = sin;
7642 		ci->ci_sin6 = sin6;
7643 		ci->ci_lifr = lifr;
7644 	}
7645 
7646 
7647 	if (iocp->ioc_cmd == SIOCSLIFNAME) {
7648 		/*
7649 		 * The ioctl will be failed if the ioctl comes down
7650 		 * an conn stream
7651 		 */
7652 		if (ill == NULL) {
7653 			/*
7654 			 * Not an ill queue, return EINVAL same as the
7655 			 * old error code.
7656 			 */
7657 			return (ENXIO);
7658 		}
7659 		ipif = ill->ill_ipif;
7660 		ipif_refhold(ipif);
7661 	} else {
7662 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
7663 		    &exists, isv6, zoneid,
7664 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err);
7665 		if (ipif == NULL) {
7666 			if (err == EINPROGRESS)
7667 				return (err);
7668 			if (iocp->ioc_cmd == SIOCLIFFAILOVER ||
7669 			    iocp->ioc_cmd == SIOCLIFFAILBACK) {
7670 				/*
7671 				 * Need to try both v4 and v6 since this
7672 				 * ioctl can come down either v4 or v6
7673 				 * socket. The lifreq.lifr_family passed
7674 				 * down by this ioctl is AF_UNSPEC.
7675 				 */
7676 				ipif = ipif_lookup_on_name(name,
7677 				    mi_strlen(name), B_FALSE, &exists, !isv6,
7678 				    zoneid, (connp == NULL) ? q :
7679 				    CONNP_TO_WQ(connp), mp, func, &err);
7680 				if (err == EINPROGRESS)
7681 					return (err);
7682 			}
7683 			err = 0;	/* Ensure we don't use it below */
7684 		}
7685 	}
7686 
7687 	/*
7688 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7689 	 */
7690 	if (ipif != NULL && ipif->ipif_isv6 && cmd_type == IF_CMD) {
7691 		ipif_refrele(ipif);
7692 		return (ENXIO);
7693 	}
7694 
7695 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7696 	    name[0] == '\0') {
7697 		/*
7698 		 * Handle a or a SIOC?IF* with a null name
7699 		 * during plumb (on the ill queue before the I_PLINK).
7700 		 */
7701 		ipif = ill->ill_ipif;
7702 		ipif_refhold(ipif);
7703 	}
7704 
7705 	if (ipif == NULL)
7706 		return (ENXIO);
7707 
7708 	/*
7709 	 * Allow only GET operations if this ipif has been created
7710 	 * temporarily due to a MOVE operation.
7711 	 */
7712 	if (ipif->ipif_replace_zero && !(flags & IPI_REPL)) {
7713 		ipif_refrele(ipif);
7714 		return (EINVAL);
7715 	}
7716 
7717 	ci->ci_ipif = ipif;
7718 	return (0);
7719 }
7720 
7721 /*
7722  * Return the total number of ipifs.
7723  */
7724 static uint_t
7725 ip_get_numifs(zoneid_t zoneid)
7726 {
7727 	uint_t numifs = 0;
7728 	ill_t	*ill;
7729 	ill_walk_context_t	ctx;
7730 	ipif_t	*ipif;
7731 
7732 	rw_enter(&ill_g_lock, RW_READER);
7733 	ill = ILL_START_WALK_V4(&ctx);
7734 
7735 	while (ill != NULL) {
7736 		for (ipif = ill->ill_ipif; ipif != NULL;
7737 		    ipif = ipif->ipif_next) {
7738 			if (ipif->ipif_zoneid == zoneid)
7739 				numifs++;
7740 		}
7741 		ill = ill_next(&ctx, ill);
7742 	}
7743 	rw_exit(&ill_g_lock);
7744 	return (numifs);
7745 }
7746 
7747 /*
7748  * Return the total number of ipifs.
7749  */
7750 static uint_t
7751 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid)
7752 {
7753 	uint_t numifs = 0;
7754 	ill_t	*ill;
7755 	ipif_t	*ipif;
7756 	ill_walk_context_t	ctx;
7757 
7758 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7759 
7760 	rw_enter(&ill_g_lock, RW_READER);
7761 	if (family == AF_INET)
7762 		ill = ILL_START_WALK_V4(&ctx);
7763 	else if (family == AF_INET6)
7764 		ill = ILL_START_WALK_V6(&ctx);
7765 	else
7766 		ill = ILL_START_WALK_ALL(&ctx);
7767 
7768 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7769 		for (ipif = ill->ill_ipif; ipif != NULL;
7770 		    ipif = ipif->ipif_next) {
7771 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7772 			    !(lifn_flags & LIFC_NOXMIT))
7773 				continue;
7774 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7775 			    !(lifn_flags & LIFC_TEMPORARY))
7776 				continue;
7777 			if (((ipif->ipif_flags &
7778 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7779 			    IPIF_DEPRECATED)) ||
7780 			    (ill->ill_phyint->phyint_flags &
7781 			    PHYI_LOOPBACK) ||
7782 			    !(ipif->ipif_flags & IPIF_UP)) &&
7783 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7784 				continue;
7785 
7786 			if (zoneid != ipif->ipif_zoneid &&
7787 			    (zoneid != GLOBAL_ZONEID ||
7788 			    !(lifn_flags & LIFC_ALLZONES)))
7789 				continue;
7790 
7791 			numifs++;
7792 		}
7793 	}
7794 	rw_exit(&ill_g_lock);
7795 	return (numifs);
7796 }
7797 
7798 uint_t
7799 ip_get_lifsrcofnum(ill_t *ill)
7800 {
7801 	uint_t numifs = 0;
7802 	ill_t	*ill_head = ill;
7803 
7804 	/*
7805 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7806 	 * other thread may be trying to relink the ILLs in this usesrc group
7807 	 * and adjusting the ill_usesrc_grp_next pointers
7808 	 */
7809 	rw_enter(&ill_g_usesrc_lock, RW_READER);
7810 	if ((ill->ill_usesrc_ifindex == 0) &&
7811 	    (ill->ill_usesrc_grp_next != NULL)) {
7812 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7813 		    ill = ill->ill_usesrc_grp_next)
7814 			numifs++;
7815 	}
7816 	rw_exit(&ill_g_usesrc_lock);
7817 
7818 	return (numifs);
7819 }
7820 
7821 /* Null values are passed in for ipif, sin, and ifreq */
7822 /* ARGSUSED */
7823 int
7824 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7825     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7826 {
7827 	int *nump;
7828 
7829 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7830 
7831 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
7832 	nump = (int *)mp->b_cont->b_cont->b_rptr;
7833 
7834 	*nump = ip_get_numifs(Q_TO_CONN(q)->conn_zoneid);
7835 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7836 	return (0);
7837 }
7838 
7839 /* Null values are passed in for ipif, sin, and ifreq */
7840 /* ARGSUSED */
7841 int
7842 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7843     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7844 {
7845 	struct lifnum *lifn;
7846 	mblk_t	*mp1;
7847 
7848 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7849 
7850 	/* Existence checked in ip_wput_nondata */
7851 	mp1 = mp->b_cont->b_cont;
7852 
7853 	lifn = (struct lifnum *)mp1->b_rptr;
7854 	switch (lifn->lifn_family) {
7855 	case AF_UNSPEC:
7856 	case AF_INET:
7857 	case AF_INET6:
7858 		break;
7859 	default:
7860 		return (EAFNOSUPPORT);
7861 	}
7862 
7863 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7864 	    Q_TO_CONN(q)->conn_zoneid);
7865 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7866 	return (0);
7867 }
7868 
7869 /* ARGSUSED */
7870 int
7871 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7872     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7873 {
7874 	STRUCT_HANDLE(ifconf, ifc);
7875 	mblk_t *mp1;
7876 	struct iocblk *iocp;
7877 	struct ifreq *ifr;
7878 	ill_walk_context_t	ctx;
7879 	ill_t	*ill;
7880 	ipif_t	*ipif;
7881 	struct sockaddr_in *sin;
7882 	int32_t	ifclen;
7883 	zoneid_t zoneid;
7884 
7885 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
7886 
7887 	ip1dbg(("ip_sioctl_get_ifconf"));
7888 	/* Existence verified in ip_wput_nondata */
7889 	mp1 = mp->b_cont->b_cont;
7890 	iocp = (struct iocblk *)mp->b_rptr;
7891 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7892 
7893 	/*
7894 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
7895 	 * the user buffer address and length into which the list of struct
7896 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
7897 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
7898 	 * the SIOCGIFCONF operation was redefined to simply provide
7899 	 * a large output buffer into which we are supposed to jam the ifreq
7900 	 * array.  The same ioctl command code was used, despite the fact that
7901 	 * both the applications and the kernel code had to change, thus making
7902 	 * it impossible to support both interfaces.
7903 	 *
7904 	 * For reasons not good enough to try to explain, the following
7905 	 * algorithm is used for deciding what to do with one of these:
7906 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
7907 	 * form with the output buffer coming down as the continuation message.
7908 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
7909 	 * and we have to copy in the ifconf structure to find out how big the
7910 	 * output buffer is and where to copy out to.  Sure no problem...
7911 	 *
7912 	 */
7913 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
7914 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
7915 		int numifs = 0;
7916 		size_t ifc_bufsize;
7917 
7918 		/*
7919 		 * Must be (better be!) continuation of a TRANSPARENT
7920 		 * IOCTL.  We just copied in the ifconf structure.
7921 		 */
7922 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
7923 		    (struct ifconf *)mp1->b_rptr);
7924 
7925 		/*
7926 		 * Allocate a buffer to hold requested information.
7927 		 *
7928 		 * If ifc_len is larger than what is needed, we only
7929 		 * allocate what we will use.
7930 		 *
7931 		 * If ifc_len is smaller than what is needed, return
7932 		 * EINVAL.
7933 		 *
7934 		 * XXX: the ill_t structure can hava 2 counters, for
7935 		 * v4 and v6 (not just ill_ipif_up_count) to store the
7936 		 * number of interfaces for a device, so we don't need
7937 		 * to count them here...
7938 		 */
7939 		numifs = ip_get_numifs(zoneid);
7940 
7941 		ifclen = STRUCT_FGET(ifc, ifc_len);
7942 		ifc_bufsize = numifs * sizeof (struct ifreq);
7943 		if (ifc_bufsize > ifclen) {
7944 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7945 				/* old behaviour */
7946 				return (EINVAL);
7947 			} else {
7948 				ifc_bufsize = ifclen;
7949 			}
7950 		}
7951 
7952 		mp1 = mi_copyout_alloc(q, mp,
7953 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
7954 		if (mp1 == NULL)
7955 			return (ENOMEM);
7956 
7957 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
7958 	}
7959 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7960 	/*
7961 	 * the SIOCGIFCONF ioctl only knows about
7962 	 * IPv4 addresses, so don't try to tell
7963 	 * it about interfaces with IPv6-only
7964 	 * addresses. (Last parm 'isv6' is B_FALSE)
7965 	 */
7966 
7967 	ifr = (struct ifreq *)mp1->b_rptr;
7968 
7969 	rw_enter(&ill_g_lock, RW_READER);
7970 	ill = ILL_START_WALK_V4(&ctx);
7971 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7972 		for (ipif = ill->ill_ipif; ipif;
7973 		    ipif = ipif->ipif_next) {
7974 			if (zoneid != ipif->ipif_zoneid)
7975 				continue;
7976 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
7977 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7978 					/* old behaviour */
7979 					rw_exit(&ill_g_lock);
7980 					return (EINVAL);
7981 				} else {
7982 					goto if_copydone;
7983 				}
7984 			}
7985 			(void) ipif_get_name(ipif,
7986 			    ifr->ifr_name,
7987 			    sizeof (ifr->ifr_name));
7988 			sin = (sin_t *)&ifr->ifr_addr;
7989 			*sin = sin_null;
7990 			sin->sin_family = AF_INET;
7991 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7992 			ifr++;
7993 		}
7994 	}
7995 if_copydone:
7996 	rw_exit(&ill_g_lock);
7997 	mp1->b_wptr = (uchar_t *)ifr;
7998 
7999 	if (STRUCT_BUF(ifc) != NULL) {
8000 		STRUCT_FSET(ifc, ifc_len,
8001 			(int)((uchar_t *)ifr - mp1->b_rptr));
8002 	}
8003 	return (0);
8004 }
8005 
8006 /*
8007  * Get the interfaces using the address hosted on the interface passed in,
8008  * as a source adddress
8009  */
8010 /* ARGSUSED */
8011 int
8012 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8013     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8014 {
8015 	mblk_t *mp1;
8016 	ill_t	*ill, *ill_head;
8017 	ipif_t	*ipif, *orig_ipif;
8018 	int	numlifs = 0;
8019 	size_t	lifs_bufsize, lifsmaxlen;
8020 	struct	lifreq *lifr;
8021 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8022 	uint_t	ifindex;
8023 	zoneid_t zoneid;
8024 	int err = 0;
8025 	boolean_t isv6 = B_FALSE;
8026 	struct	sockaddr_in	*sin;
8027 	struct	sockaddr_in6	*sin6;
8028 
8029 	STRUCT_HANDLE(lifsrcof, lifs);
8030 
8031 	ASSERT(q->q_next == NULL);
8032 
8033 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8034 
8035 	/* Existence verified in ip_wput_nondata */
8036 	mp1 = mp->b_cont->b_cont;
8037 
8038 	/*
8039 	 * Must be (better be!) continuation of a TRANSPARENT
8040 	 * IOCTL.  We just copied in the lifsrcof structure.
8041 	 */
8042 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8043 	    (struct lifsrcof *)mp1->b_rptr);
8044 
8045 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8046 		return (EINVAL);
8047 
8048 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8049 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8050 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8051 	    ip_process_ioctl, &err);
8052 	if (ipif == NULL) {
8053 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8054 		    ifindex));
8055 		return (err);
8056 	}
8057 
8058 
8059 	/* Allocate a buffer to hold requested information */
8060 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8061 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8062 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8063 	/* The actual size needed is always returned in lifs_len */
8064 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8065 
8066 	/* If the amount we need is more than what is passed in, abort */
8067 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8068 		ipif_refrele(ipif);
8069 		return (0);
8070 	}
8071 
8072 	mp1 = mi_copyout_alloc(q, mp,
8073 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8074 	if (mp1 == NULL) {
8075 		ipif_refrele(ipif);
8076 		return (ENOMEM);
8077 	}
8078 
8079 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8080 	bzero(mp1->b_rptr, lifs_bufsize);
8081 
8082 	lifr = (struct lifreq *)mp1->b_rptr;
8083 
8084 	ill = ill_head = ipif->ipif_ill;
8085 	orig_ipif = ipif;
8086 
8087 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8088 	rw_enter(&ill_g_usesrc_lock, RW_READER);
8089 	rw_enter(&ill_g_lock, RW_READER);
8090 
8091 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8092 	for (; (ill != NULL) && (ill != ill_head);
8093 	    ill = ill->ill_usesrc_grp_next) {
8094 
8095 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8096 			break;
8097 
8098 		ipif = ill->ill_ipif;
8099 		(void) ipif_get_name(ipif,
8100 		    lifr->lifr_name, sizeof (lifr->lifr_name));
8101 		if (ipif->ipif_isv6) {
8102 			sin6 = (sin6_t *)&lifr->lifr_addr;
8103 			*sin6 = sin6_null;
8104 			sin6->sin6_family = AF_INET6;
8105 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8106 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8107 			    &ipif->ipif_v6net_mask);
8108 		} else {
8109 			sin = (sin_t *)&lifr->lifr_addr;
8110 			*sin = sin_null;
8111 			sin->sin_family = AF_INET;
8112 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8113 			lifr->lifr_addrlen = ip_mask_to_plen(
8114 			    ipif->ipif_net_mask);
8115 		}
8116 		lifr++;
8117 	}
8118 	rw_exit(&ill_g_usesrc_lock);
8119 	rw_exit(&ill_g_lock);
8120 	ipif_refrele(orig_ipif);
8121 	mp1->b_wptr = (uchar_t *)lifr;
8122 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8123 
8124 	return (0);
8125 }
8126 
8127 /* ARGSUSED */
8128 int
8129 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8130     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8131 {
8132 	mblk_t *mp1;
8133 	int	list;
8134 	ill_t	*ill;
8135 	ipif_t	*ipif;
8136 	int	flags;
8137 	int	numlifs = 0;
8138 	size_t	lifc_bufsize;
8139 	struct	lifreq *lifr;
8140 	sa_family_t	family;
8141 	struct	sockaddr_in	*sin;
8142 	struct	sockaddr_in6	*sin6;
8143 	ill_walk_context_t	ctx;
8144 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8145 	int32_t	lifclen;
8146 	zoneid_t zoneid;
8147 	STRUCT_HANDLE(lifconf, lifc);
8148 
8149 	ip1dbg(("ip_sioctl_get_lifconf"));
8150 
8151 	ASSERT(q->q_next == NULL);
8152 
8153 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8154 
8155 	/* Existence verified in ip_wput_nondata */
8156 	mp1 = mp->b_cont->b_cont;
8157 
8158 	/*
8159 	 * An extended version of SIOCGIFCONF that takes an
8160 	 * additional address family and flags field.
8161 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8162 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8163 	 * interfaces are omitted.
8164 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8165 	 * unless LIFC_TEMPORARY is specified.
8166 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8167 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8168 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8169 	 * has priority over LIFC_NOXMIT.
8170 	 */
8171 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8172 
8173 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8174 		return (EINVAL);
8175 
8176 	/*
8177 	 * Must be (better be!) continuation of a TRANSPARENT
8178 	 * IOCTL.  We just copied in the lifconf structure.
8179 	 */
8180 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8181 
8182 	family = STRUCT_FGET(lifc, lifc_family);
8183 	flags = STRUCT_FGET(lifc, lifc_flags);
8184 
8185 	switch (family) {
8186 	case AF_UNSPEC:
8187 		/*
8188 		 * walk all ILL's.
8189 		 */
8190 		list = MAX_G_HEADS;
8191 		break;
8192 	case AF_INET:
8193 		/*
8194 		 * walk only IPV4 ILL's.
8195 		 */
8196 		list = IP_V4_G_HEAD;
8197 		break;
8198 	case AF_INET6:
8199 		/*
8200 		 * walk only IPV6 ILL's.
8201 		 */
8202 		list = IP_V6_G_HEAD;
8203 		break;
8204 	default:
8205 		return (EAFNOSUPPORT);
8206 	}
8207 
8208 	/*
8209 	 * Allocate a buffer to hold requested information.
8210 	 *
8211 	 * If lifc_len is larger than what is needed, we only
8212 	 * allocate what we will use.
8213 	 *
8214 	 * If lifc_len is smaller than what is needed, return
8215 	 * EINVAL.
8216 	 */
8217 	numlifs = ip_get_numlifs(family, flags, zoneid);
8218 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8219 	lifclen = STRUCT_FGET(lifc, lifc_len);
8220 	if (lifc_bufsize > lifclen) {
8221 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8222 			return (EINVAL);
8223 		else
8224 			lifc_bufsize = lifclen;
8225 	}
8226 
8227 	mp1 = mi_copyout_alloc(q, mp,
8228 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8229 	if (mp1 == NULL)
8230 		return (ENOMEM);
8231 
8232 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8233 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8234 
8235 	lifr = (struct lifreq *)mp1->b_rptr;
8236 
8237 	rw_enter(&ill_g_lock, RW_READER);
8238 	ill = ill_first(list, list, &ctx);
8239 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8240 		for (ipif = ill->ill_ipif; ipif != NULL;
8241 		    ipif = ipif->ipif_next) {
8242 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8243 			    !(flags & LIFC_NOXMIT))
8244 				continue;
8245 
8246 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8247 			    !(flags & LIFC_TEMPORARY))
8248 				continue;
8249 
8250 			if (((ipif->ipif_flags &
8251 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8252 			    IPIF_DEPRECATED)) ||
8253 			    (ill->ill_phyint->phyint_flags &
8254 			    PHYI_LOOPBACK) ||
8255 			    !(ipif->ipif_flags & IPIF_UP)) &&
8256 			    (flags & LIFC_EXTERNAL_SOURCE))
8257 				continue;
8258 
8259 			if (zoneid != ipif->ipif_zoneid &&
8260 			    (zoneid != GLOBAL_ZONEID ||
8261 			    !(flags & LIFC_ALLZONES)))
8262 				continue;
8263 
8264 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8265 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8266 					rw_exit(&ill_g_lock);
8267 					return (EINVAL);
8268 				} else {
8269 					goto lif_copydone;
8270 				}
8271 			}
8272 
8273 			(void) ipif_get_name(ipif,
8274 				lifr->lifr_name,
8275 				sizeof (lifr->lifr_name));
8276 			if (ipif->ipif_isv6) {
8277 				sin6 = (sin6_t *)&lifr->lifr_addr;
8278 				*sin6 = sin6_null;
8279 				sin6->sin6_family = AF_INET6;
8280 				sin6->sin6_addr =
8281 				ipif->ipif_v6lcl_addr;
8282 				lifr->lifr_addrlen =
8283 				ip_mask_to_plen_v6(
8284 				    &ipif->ipif_v6net_mask);
8285 			} else {
8286 				sin = (sin_t *)&lifr->lifr_addr;
8287 				*sin = sin_null;
8288 				sin->sin_family = AF_INET;
8289 				sin->sin_addr.s_addr =
8290 				    ipif->ipif_lcl_addr;
8291 				lifr->lifr_addrlen =
8292 				    ip_mask_to_plen(
8293 				    ipif->ipif_net_mask);
8294 			}
8295 			lifr++;
8296 		}
8297 	}
8298 lif_copydone:
8299 	rw_exit(&ill_g_lock);
8300 
8301 	mp1->b_wptr = (uchar_t *)lifr;
8302 	if (STRUCT_BUF(lifc) != NULL) {
8303 		STRUCT_FSET(lifc, lifc_len,
8304 			(int)((uchar_t *)lifr - mp1->b_rptr));
8305 	}
8306 	return (0);
8307 }
8308 
8309 /* ARGSUSED */
8310 int
8311 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
8312     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8313 {
8314 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8315 	ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
8316 	return (0);
8317 }
8318 
8319 static void
8320 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
8321 {
8322 	ip6_asp_t *table;
8323 	size_t table_size;
8324 	mblk_t *data_mp;
8325 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8326 
8327 	/* These two ioctls are I_STR only */
8328 	if (iocp->ioc_count == TRANSPARENT) {
8329 		miocnak(q, mp, 0, EINVAL);
8330 		return;
8331 	}
8332 
8333 	data_mp = mp->b_cont;
8334 	if (data_mp == NULL) {
8335 		/* The user passed us a NULL argument */
8336 		table = NULL;
8337 		table_size = iocp->ioc_count;
8338 	} else {
8339 		/*
8340 		 * The user provided a table.  The stream head
8341 		 * may have copied in the user data in chunks,
8342 		 * so make sure everything is pulled up
8343 		 * properly.
8344 		 */
8345 		if (MBLKL(data_mp) < iocp->ioc_count) {
8346 			mblk_t *new_data_mp;
8347 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
8348 			    NULL) {
8349 				miocnak(q, mp, 0, ENOMEM);
8350 				return;
8351 			}
8352 			freemsg(data_mp);
8353 			data_mp = new_data_mp;
8354 			mp->b_cont = data_mp;
8355 		}
8356 		table = (ip6_asp_t *)data_mp->b_rptr;
8357 		table_size = iocp->ioc_count;
8358 	}
8359 
8360 	switch (iocp->ioc_cmd) {
8361 	case SIOCGIP6ADDRPOLICY:
8362 		iocp->ioc_rval = ip6_asp_get(table, table_size);
8363 		if (iocp->ioc_rval == -1)
8364 			iocp->ioc_error = EINVAL;
8365 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8366 		else if (table != NULL &&
8367 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
8368 			ip6_asp_t *src = table;
8369 			ip6_asp32_t *dst = (void *)table;
8370 			int count = table_size / sizeof (ip6_asp_t);
8371 			int i;
8372 
8373 			/*
8374 			 * We need to do an in-place shrink of the array
8375 			 * to match the alignment attributes of the
8376 			 * 32-bit ABI looking at it.
8377 			 */
8378 			/* LINTED: logical expression always true: op "||" */
8379 			ASSERT(sizeof (*src) > sizeof (*dst));
8380 			for (i = 1; i < count; i++)
8381 				bcopy(src + i, dst + i, sizeof (*dst));
8382 		}
8383 #endif
8384 		break;
8385 
8386 	case SIOCSIP6ADDRPOLICY:
8387 		ASSERT(mp->b_prev == NULL);
8388 		mp->b_prev = (void *)q;
8389 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8390 		/*
8391 		 * We pass in the datamodel here so that the ip6_asp_replace()
8392 		 * routine can handle converting from 32-bit to native formats
8393 		 * where necessary.
8394 		 *
8395 		 * A better way to handle this might be to convert the inbound
8396 		 * data structure here, and hang it off a new 'mp'; thus the
8397 		 * ip6_asp_replace() logic would always be dealing with native
8398 		 * format data structures..
8399 		 *
8400 		 * (An even simpler way to handle these ioctls is to just
8401 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
8402 		 * and just recompile everything that depends on it.)
8403 		 */
8404 #endif
8405 		ip6_asp_replace(mp, table, table_size, B_FALSE,
8406 		    iocp->ioc_flag & IOC_MODELS);
8407 		return;
8408 	}
8409 
8410 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
8411 	qreply(q, mp);
8412 }
8413 
8414 static void
8415 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
8416 {
8417 	mblk_t 		*data_mp;
8418 	struct dstinforeq	*dir;
8419 	uint8_t		*end, *cur;
8420 	in6_addr_t	*daddr, *saddr;
8421 	ipaddr_t	v4daddr;
8422 	ire_t		*ire;
8423 	char		*slabel, *dlabel;
8424 	boolean_t	isipv4;
8425 	int		match_ire;
8426 	ill_t		*dst_ill;
8427 	ipif_t		*src_ipif, *ire_ipif;
8428 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8429 	zoneid_t	zoneid;
8430 
8431 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8432 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8433 
8434 	/*
8435 	 * This ioctl is I_STR only, and must have a
8436 	 * data mblk following the M_IOCTL mblk.
8437 	 */
8438 	data_mp = mp->b_cont;
8439 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
8440 		miocnak(q, mp, 0, EINVAL);
8441 		return;
8442 	}
8443 
8444 	if (MBLKL(data_mp) < iocp->ioc_count) {
8445 		mblk_t *new_data_mp;
8446 
8447 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
8448 			miocnak(q, mp, 0, ENOMEM);
8449 			return;
8450 		}
8451 		freemsg(data_mp);
8452 		data_mp = new_data_mp;
8453 		mp->b_cont = data_mp;
8454 	}
8455 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
8456 
8457 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
8458 	    end - cur >= sizeof (struct dstinforeq);
8459 	    cur += sizeof (struct dstinforeq)) {
8460 		dir = (struct dstinforeq *)cur;
8461 		daddr = &dir->dir_daddr;
8462 		saddr = &dir->dir_saddr;
8463 
8464 		/*
8465 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
8466 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
8467 		 * and ipif_select_source[_v6]() do not.
8468 		 */
8469 		dir->dir_dscope = ip_addr_scope_v6(daddr);
8470 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence);
8471 
8472 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
8473 		if (isipv4) {
8474 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
8475 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
8476 			    0, NULL, NULL, zoneid, 0, match_ire);
8477 		} else {
8478 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
8479 			    0, NULL, NULL, zoneid, 0, match_ire);
8480 		}
8481 		if (ire == NULL) {
8482 			dir->dir_dreachable = 0;
8483 
8484 			/* move on to next dst addr */
8485 			continue;
8486 		}
8487 		dir->dir_dreachable = 1;
8488 
8489 		ire_ipif = ire->ire_ipif;
8490 		if (ire_ipif == NULL)
8491 			goto next_dst;
8492 
8493 		/*
8494 		 * We expect to get back an interface ire or a
8495 		 * gateway ire cache entry.  For both types, the
8496 		 * output interface is ire_ipif->ipif_ill.
8497 		 */
8498 		dst_ill = ire_ipif->ipif_ill;
8499 		dir->dir_dmactype = dst_ill->ill_mactype;
8500 
8501 		if (isipv4) {
8502 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
8503 		} else {
8504 			src_ipif = ipif_select_source_v6(dst_ill,
8505 			    daddr, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
8506 			    zoneid);
8507 		}
8508 		if (src_ipif == NULL)
8509 			goto next_dst;
8510 
8511 		*saddr = src_ipif->ipif_v6lcl_addr;
8512 		dir->dir_sscope = ip_addr_scope_v6(saddr);
8513 		slabel = ip6_asp_lookup(saddr, NULL);
8514 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
8515 		dir->dir_sdeprecated =
8516 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
8517 		ipif_refrele(src_ipif);
8518 next_dst:
8519 		ire_refrele(ire);
8520 	}
8521 	miocack(q, mp, iocp->ioc_count, 0);
8522 }
8523 
8524 
8525 /*
8526  * Check if this is an address assigned to this machine.
8527  * Skips interfaces that are down by using ire checks.
8528  * Translates mapped addresses to v4 addresses and then
8529  * treats them as such, returning true if the v4 address
8530  * associated with this mapped address is configured.
8531  * Note: Applications will have to be careful what they do
8532  * with the response; use of mapped addresses limits
8533  * what can be done with the socket, especially with
8534  * respect to socket options and ioctls - neither IPv4
8535  * options nor IPv6 sticky options/ancillary data options
8536  * may be used.
8537  */
8538 /* ARGSUSED */
8539 int
8540 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8541     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8542 {
8543 	struct sioc_addrreq *sia;
8544 	sin_t *sin;
8545 	ire_t *ire;
8546 	mblk_t *mp1;
8547 	zoneid_t zoneid;
8548 
8549 	ip1dbg(("ip_sioctl_tmyaddr"));
8550 
8551 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8552 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8553 
8554 	/* Existence verified in ip_wput_nondata */
8555 	mp1 = mp->b_cont->b_cont;
8556 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8557 	sin = (sin_t *)&sia->sa_addr;
8558 	switch (sin->sin_family) {
8559 	case AF_INET6: {
8560 		sin6_t *sin6 = (sin6_t *)sin;
8561 
8562 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8563 			ipaddr_t v4_addr;
8564 
8565 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8566 			    v4_addr);
8567 			ire = ire_ctable_lookup(v4_addr, 0,
8568 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8569 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8570 		} else {
8571 			in6_addr_t v6addr;
8572 
8573 			v6addr = sin6->sin6_addr;
8574 			ire = ire_ctable_lookup_v6(&v6addr, 0,
8575 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8576 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8577 		}
8578 		break;
8579 	}
8580 	case AF_INET: {
8581 		ipaddr_t v4addr;
8582 
8583 		v4addr = sin->sin_addr.s_addr;
8584 		ire = ire_ctable_lookup(v4addr, 0,
8585 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8586 		    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8587 		break;
8588 	}
8589 	default:
8590 		return (EAFNOSUPPORT);
8591 	}
8592 	if (ire != NULL) {
8593 		sia->sa_res = 1;
8594 		ire_refrele(ire);
8595 	} else {
8596 		sia->sa_res = 0;
8597 	}
8598 	return (0);
8599 }
8600 
8601 /*
8602  * Check if this is an address assigned on-link i.e. neighbor,
8603  * and makes sure it's reachable from the current zone.
8604  * Returns true for my addresses as well.
8605  * Translates mapped addresses to v4 addresses and then
8606  * treats them as such, returning true if the v4 address
8607  * associated with this mapped address is configured.
8608  * Note: Applications will have to be careful what they do
8609  * with the response; use of mapped addresses limits
8610  * what can be done with the socket, especially with
8611  * respect to socket options and ioctls - neither IPv4
8612  * options nor IPv6 sticky options/ancillary data options
8613  * may be used.
8614  */
8615 /* ARGSUSED */
8616 int
8617 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8618     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8619 {
8620 	struct sioc_addrreq *sia;
8621 	sin_t *sin;
8622 	mblk_t	*mp1;
8623 	ire_t *ire = NULL;
8624 	zoneid_t zoneid;
8625 
8626 	ip1dbg(("ip_sioctl_tonlink"));
8627 
8628 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8629 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8630 
8631 	/* Existence verified in ip_wput_nondata */
8632 	mp1 = mp->b_cont->b_cont;
8633 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8634 	sin = (sin_t *)&sia->sa_addr;
8635 
8636 	/*
8637 	 * Match addresses with a zero gateway field to avoid
8638 	 * routes going through a router.
8639 	 * Exclude broadcast and multicast addresses.
8640 	 */
8641 	switch (sin->sin_family) {
8642 	case AF_INET6: {
8643 		sin6_t *sin6 = (sin6_t *)sin;
8644 
8645 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8646 			ipaddr_t v4_addr;
8647 
8648 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8649 			    v4_addr);
8650 			if (!CLASSD(v4_addr)) {
8651 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
8652 				    NULL, NULL, zoneid, MATCH_IRE_GW);
8653 			}
8654 		} else {
8655 			in6_addr_t v6addr;
8656 			in6_addr_t v6gw;
8657 
8658 			v6addr = sin6->sin6_addr;
8659 			v6gw = ipv6_all_zeros;
8660 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8661 				ire = ire_route_lookup_v6(&v6addr, 0,
8662 				    &v6gw, 0, NULL, NULL, zoneid,
8663 				    MATCH_IRE_GW);
8664 			}
8665 		}
8666 		break;
8667 	}
8668 	case AF_INET: {
8669 		ipaddr_t v4addr;
8670 
8671 		v4addr = sin->sin_addr.s_addr;
8672 		if (!CLASSD(v4addr)) {
8673 			ire = ire_route_lookup(v4addr, 0, 0, 0,
8674 			    NULL, NULL, zoneid, MATCH_IRE_GW);
8675 		}
8676 		break;
8677 	}
8678 	default:
8679 		return (EAFNOSUPPORT);
8680 	}
8681 	sia->sa_res = 0;
8682 	if (ire != NULL) {
8683 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
8684 		    IRE_LOCAL|IRE_LOOPBACK)) {
8685 			sia->sa_res = 1;
8686 		}
8687 		ire_refrele(ire);
8688 	}
8689 	return (0);
8690 }
8691 
8692 /*
8693  * TBD: implement when kernel maintaines a list of site prefixes.
8694  */
8695 /* ARGSUSED */
8696 int
8697 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8698     ip_ioctl_cmd_t *ipip, void *ifreq)
8699 {
8700 	return (ENXIO);
8701 }
8702 
8703 /* ARGSUSED */
8704 int
8705 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8706     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8707 {
8708 	ill_t  		*ill;
8709 	mblk_t		*mp1;
8710 	conn_t		*connp;
8711 	boolean_t	success;
8712 
8713 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
8714 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
8715 	/* ioctl comes down on an conn */
8716 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8717 	connp = Q_TO_CONN(q);
8718 
8719 	mp->b_datap->db_type = M_IOCTL;
8720 
8721 	/*
8722 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
8723 	 * The original mp contains contaminated b_next values due to 'mi',
8724 	 * which is needed to do the mi_copy_done. Unfortunately if we
8725 	 * send down the original mblk itself and if we are popped due to an
8726 	 * an unplumb before the response comes back from tunnel,
8727 	 * the streamhead (which does a freemsg) will see this contaminated
8728 	 * message and the assertion in freemsg about non-null b_next/b_prev
8729 	 * will panic a DEBUG kernel.
8730 	 */
8731 	mp1 = copymsg(mp);
8732 	if (mp1 == NULL)
8733 		return (ENOMEM);
8734 
8735 	ill = ipif->ipif_ill;
8736 	mutex_enter(&connp->conn_lock);
8737 	mutex_enter(&ill->ill_lock);
8738 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
8739 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
8740 		    mp, 0);
8741 	} else {
8742 		success = ill_pending_mp_add(ill, connp, mp);
8743 	}
8744 	mutex_exit(&ill->ill_lock);
8745 	mutex_exit(&connp->conn_lock);
8746 
8747 	if (success) {
8748 		ip1dbg(("sending down tunparam request "));
8749 		putnext(ill->ill_wq, mp1);
8750 		return (EINPROGRESS);
8751 	} else {
8752 		/* The conn has started closing */
8753 		freemsg(mp1);
8754 		return (EINTR);
8755 	}
8756 }
8757 
8758 static int
8759 ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp, sin_t *sin,
8760     boolean_t x_arp_ioctl, boolean_t if_arp_ioctl)
8761 {
8762 	mblk_t *mp1;
8763 	mblk_t *mp2;
8764 	mblk_t *pending_mp;
8765 	ipaddr_t ipaddr;
8766 	area_t *area;
8767 	struct iocblk *iocp;
8768 	conn_t *connp;
8769 	struct arpreq *ar;
8770 	struct xarpreq *xar;
8771 	boolean_t success;
8772 	int flags, alength;
8773 	char *lladdr;
8774 
8775 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8776 	connp = Q_TO_CONN(q);
8777 
8778 	iocp = (struct iocblk *)mp->b_rptr;
8779 	/*
8780 	 * ill has already been set depending on whether
8781 	 * bsd style or interface style ioctl.
8782 	 */
8783 	ASSERT(ill != NULL);
8784 
8785 	/*
8786 	 * Is this one of the new SIOC*XARP ioctls?
8787 	 */
8788 	if (x_arp_ioctl) {
8789 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8790 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8791 		ar = NULL;
8792 
8793 		flags = xar->xarp_flags;
8794 		lladdr = LLADDR(&xar->xarp_ha);
8795 		/*
8796 		 * Validate against user's link layer address length
8797 		 * input and name and addr length limits.
8798 		 */
8799 		alength = ill->ill_phys_addr_length;
8800 		if (iocp->ioc_cmd == SIOCSXARP) {
8801 			if (alength != xar->xarp_ha.sdl_alen ||
8802 			    (alength + xar->xarp_ha.sdl_nlen >
8803 			    sizeof (xar->xarp_ha.sdl_data)))
8804 				return (EINVAL);
8805 		}
8806 	} else {
8807 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8808 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8809 		xar = NULL;
8810 
8811 		flags = ar->arp_flags;
8812 		lladdr = ar->arp_ha.sa_data;
8813 		/*
8814 		 * Theoretically, the sa_family could tell us what link
8815 		 * layer type this operation is trying to deal with. By
8816 		 * common usage AF_UNSPEC means ethernet. We'll assume
8817 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8818 		 * for now. Our new SIOC*XARP ioctls can be used more
8819 		 * generally.
8820 		 *
8821 		 * If the underlying media happens to have a non 6 byte
8822 		 * address, arp module will fail set/get, but the del
8823 		 * operation will succeed.
8824 		 */
8825 		alength = 6;
8826 		if ((iocp->ioc_cmd != SIOCDARP) &&
8827 		    (alength != ill->ill_phys_addr_length)) {
8828 			return (EINVAL);
8829 		}
8830 	}
8831 
8832 	/*
8833 	 * We are going to pass up to ARP a packet chain that looks
8834 	 * like:
8835 	 *
8836 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
8837 	 *
8838 	 * Get a copy of the original IOCTL mblk to head the chain,
8839 	 * to be sent up (in mp1). Also get another copy to store
8840 	 * in the ill_pending_mp list, for matching the response
8841 	 * when it comes back from ARP.
8842 	 */
8843 	mp1 = copyb(mp);
8844 	pending_mp = copymsg(mp);
8845 	if (mp1 == NULL || pending_mp == NULL) {
8846 		if (mp1 != NULL)
8847 			freeb(mp1);
8848 		if (pending_mp != NULL)
8849 			inet_freemsg(pending_mp);
8850 		return (ENOMEM);
8851 	}
8852 
8853 	ipaddr = sin->sin_addr.s_addr;
8854 
8855 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
8856 	    (caddr_t)&ipaddr);
8857 	if (mp2 == NULL) {
8858 		freeb(mp1);
8859 		inet_freemsg(pending_mp);
8860 		return (ENOMEM);
8861 	}
8862 	/* Put together the chain. */
8863 	mp1->b_cont = mp2;
8864 	mp1->b_datap->db_type = M_IOCTL;
8865 	mp2->b_cont = mp;
8866 	mp2->b_datap->db_type = M_DATA;
8867 
8868 	iocp = (struct iocblk *)mp1->b_rptr;
8869 
8870 	/*
8871 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
8872 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
8873 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
8874 	 * ioc_count field; set ioc_count to be correct.
8875 	 */
8876 	iocp->ioc_count = MBLKL(mp1->b_cont);
8877 
8878 	/*
8879 	 * Set the proper command in the ARP message.
8880 	 * Convert the SIOC{G|S|D}ARP calls into our
8881 	 * AR_ENTRY_xxx calls.
8882 	 */
8883 	area = (area_t *)mp2->b_rptr;
8884 	switch (iocp->ioc_cmd) {
8885 	case SIOCDARP:
8886 	case SIOCDXARP:
8887 		/*
8888 		 * We defer deleting the corresponding IRE until
8889 		 * we return from arp.
8890 		 */
8891 		area->area_cmd = AR_ENTRY_DELETE;
8892 		area->area_proto_mask_offset = 0;
8893 		break;
8894 	case SIOCGARP:
8895 	case SIOCGXARP:
8896 		area->area_cmd = AR_ENTRY_SQUERY;
8897 		area->area_proto_mask_offset = 0;
8898 		break;
8899 	case SIOCSARP:
8900 	case SIOCSXARP: {
8901 		/*
8902 		 * Delete the corresponding ire to make sure IP will
8903 		 * pick up any change from arp.
8904 		 */
8905 		if (!if_arp_ioctl) {
8906 			(void) ip_ire_clookup_and_delete(ipaddr, NULL);
8907 			break;
8908 		} else {
8909 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
8910 			if (ipif != NULL) {
8911 				(void) ip_ire_clookup_and_delete(ipaddr, ipif);
8912 				ipif_refrele(ipif);
8913 			}
8914 			break;
8915 		}
8916 	}
8917 	}
8918 	iocp->ioc_cmd = area->area_cmd;
8919 
8920 	/*
8921 	 * Before sending 'mp' to ARP, we have to clear the b_next
8922 	 * and b_prev. Otherwise if STREAMS encounters such a message
8923 	 * in freemsg(), (because ARP can close any time) it can cause
8924 	 * a panic. But mi code needs the b_next and b_prev values of
8925 	 * mp->b_cont, to complete the ioctl. So we store it here
8926 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
8927 	 * when the response comes down from ARP.
8928 	 */
8929 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
8930 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
8931 	mp->b_cont->b_next = NULL;
8932 	mp->b_cont->b_prev = NULL;
8933 
8934 	mutex_enter(&connp->conn_lock);
8935 	mutex_enter(&ill->ill_lock);
8936 	/* conn has not yet started closing, hence this can't fail */
8937 	success = ill_pending_mp_add(ill, connp, pending_mp);
8938 	ASSERT(success);
8939 	mutex_exit(&ill->ill_lock);
8940 	mutex_exit(&connp->conn_lock);
8941 
8942 	/*
8943 	 * Fill in the rest of the ARP operation fields.
8944 	 */
8945 	area->area_hw_addr_length = alength;
8946 	bcopy(lladdr,
8947 	    (char *)area + area->area_hw_addr_offset,
8948 	    area->area_hw_addr_length);
8949 	/* Translate the flags. */
8950 	if (flags & ATF_PERM)
8951 		area->area_flags |= ACE_F_PERMANENT;
8952 	if (flags & ATF_PUBL)
8953 		area->area_flags |= ACE_F_PUBLISH;
8954 
8955 	/*
8956 	 * Up to ARP it goes.  The response will come
8957 	 * back in ip_wput as an M_IOCACK message, and
8958 	 * will be handed to ip_sioctl_iocack for
8959 	 * completion.
8960 	 */
8961 	putnext(ill->ill_rq, mp1);
8962 	return (EINPROGRESS);
8963 }
8964 
8965 /* ARGSUSED */
8966 int
8967 ip_sioctl_xarp(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8968     ip_ioctl_cmd_t *ipip, void *ifreq)
8969 {
8970 	struct xarpreq *xar;
8971 	boolean_t isv6;
8972 	mblk_t	*mp1;
8973 	int	err;
8974 	conn_t	*connp;
8975 	int ifnamelen;
8976 	ire_t	*ire = NULL;
8977 	ill_t	*ill = NULL;
8978 	struct sockaddr_in *sin;
8979 	boolean_t if_arp_ioctl = B_FALSE;
8980 
8981 	/* ioctl comes down on an conn */
8982 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8983 	connp = Q_TO_CONN(q);
8984 	isv6 = connp->conn_af_isv6;
8985 
8986 	/* Existance verified in ip_wput_nondata */
8987 	mp1 = mp->b_cont->b_cont;
8988 
8989 	ASSERT(MBLKL(mp1) >= sizeof (*xar));
8990 	xar = (struct xarpreq *)mp1->b_rptr;
8991 	sin = (sin_t *)&xar->xarp_pa;
8992 
8993 	if (isv6 || (xar->xarp_ha.sdl_family != AF_LINK) ||
8994 	    (xar->xarp_pa.ss_family != AF_INET))
8995 		return (ENXIO);
8996 
8997 	ifnamelen = xar->xarp_ha.sdl_nlen;
8998 	if (ifnamelen != 0) {
8999 		char	*cptr, cval;
9000 
9001 		if (ifnamelen >= LIFNAMSIZ)
9002 			return (EINVAL);
9003 
9004 		/*
9005 		 * Instead of bcopying a bunch of bytes,
9006 		 * null-terminate the string in-situ.
9007 		 */
9008 		cptr = xar->xarp_ha.sdl_data + ifnamelen;
9009 		cval = *cptr;
9010 		*cptr = '\0';
9011 		ill = ill_lookup_on_name(xar->xarp_ha.sdl_data,
9012 		    B_FALSE, isv6, CONNP_TO_WQ(connp), mp, ip_process_ioctl,
9013 		    &err, NULL);
9014 		*cptr = cval;
9015 		if (ill == NULL)
9016 			return (err);
9017 		if (ill->ill_net_type != IRE_IF_RESOLVER) {
9018 			ill_refrele(ill);
9019 			return (ENXIO);
9020 		}
9021 
9022 		if_arp_ioctl = B_TRUE;
9023 	} else {
9024 		/*
9025 		 * PSARC 2003/088 states that if sdl_nlen == 0, it behaves
9026 		 * as an extended BSD ioctl. The kernel uses the IP address
9027 		 * to figure out the network interface.
9028 		 */
9029 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES);
9030 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9031 		    ((ill = ire_to_ill(ire)) == NULL)) {
9032 			if (ire != NULL)
9033 				ire_refrele(ire);
9034 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9035 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9036 			    MATCH_IRE_TYPE);
9037 			if ((ire == NULL) ||
9038 			    ((ill = ire_to_ill(ire)) == NULL)) {
9039 				if (ire != NULL)
9040 					ire_refrele(ire);
9041 				return (ENXIO);
9042 			}
9043 		}
9044 		ASSERT(ire != NULL && ill != NULL);
9045 	}
9046 
9047 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_TRUE, if_arp_ioctl);
9048 	if (if_arp_ioctl)
9049 		ill_refrele(ill);
9050 	if (ire != NULL)
9051 		ire_refrele(ire);
9052 
9053 	return (err);
9054 }
9055 
9056 /*
9057  * ARP IOCTLs.
9058  * How does IP get in the business of fronting ARP configuration/queries?
9059  * Well its like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9060  * are by tradition passed in through a datagram socket.  That lands in IP.
9061  * As it happens, this is just as well since the interface is quite crude in
9062  * that it passes in no information about protocol or hardware types, or
9063  * interface association.  After making the protocol assumption, IP is in
9064  * the position to look up the name of the ILL, which ARP will need, and
9065  * format a request that can be handled by ARP.	 The request is passed up
9066  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9067  * back a response.  ARP supports its own set of more general IOCTLs, in
9068  * case anyone is interested.
9069  */
9070 /* ARGSUSED */
9071 int
9072 ip_sioctl_arp(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9073     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9074 {
9075 	struct arpreq *ar;
9076 	struct sockaddr_in *sin;
9077 	ire_t	*ire;
9078 	boolean_t isv6;
9079 	mblk_t	*mp1;
9080 	int	err;
9081 	conn_t	*connp;
9082 	ill_t	*ill;
9083 
9084 	/* ioctl comes down on an conn */
9085 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9086 	connp = Q_TO_CONN(q);
9087 	isv6 = connp->conn_af_isv6;
9088 	if (isv6)
9089 		return (ENXIO);
9090 
9091 	/* Existance verified in ip_wput_nondata */
9092 	mp1 = mp->b_cont->b_cont;
9093 
9094 	ar = (struct arpreq *)mp1->b_rptr;
9095 	sin = (sin_t *)&ar->arp_pa;
9096 
9097 	/*
9098 	 * We need to let ARP know on which interface the IP
9099 	 * address has an ARP mapping. In the IPMP case, a
9100 	 * simple forwarding table lookup will return the
9101 	 * IRE_IF_RESOLVER for the first interface in the group,
9102 	 * which might not be the interface on which the
9103 	 * requested IP address was resolved due to the ill
9104 	 * selection algorithm (see ip_newroute_get_dst_ill()).
9105 	 * So we do a cache table lookup first: if the IRE cache
9106 	 * entry for the IP address is still there, it will
9107 	 * contain the ill pointer for the right interface, so
9108 	 * we use that. If the cache entry has been flushed, we
9109 	 * fall back to the forwarding table lookup. This should
9110 	 * be rare enough since IRE cache entries have a longer
9111 	 * life expectancy than ARP cache entries.
9112 	 */
9113 	ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES);
9114 	if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9115 	    ((ill = ire_to_ill(ire)) == NULL)) {
9116 		if (ire != NULL)
9117 			ire_refrele(ire);
9118 		ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9119 		    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9120 		    MATCH_IRE_TYPE);
9121 		if ((ire == NULL) || ((ill = ire_to_ill(ire)) == NULL)) {
9122 			if (ire != NULL)
9123 				ire_refrele(ire);
9124 			return (ENXIO);
9125 		}
9126 	}
9127 	ASSERT(ire != NULL && ill != NULL);
9128 
9129 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_FALSE, B_FALSE);
9130 	ire_refrele(ire);
9131 	return (err);
9132 }
9133 
9134 /*
9135  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9136  * atomically set/clear the muxids. Also complete the ioctl by acking or
9137  * naking it.  Note that the code is structured such that the link type,
9138  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9139  * its clones use the persistent link, while pppd(1M) and perhaps many
9140  * other daemons may use non-persistent link.  When combined with some
9141  * ill_t states, linking and unlinking lower streams may be used as
9142  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9143  */
9144 /* ARGSUSED */
9145 void
9146 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9147 {
9148 	mblk_t *mp1;
9149 	mblk_t *mp2;
9150 	struct linkblk *li;
9151 	queue_t	*ipwq;
9152 	char	*name;
9153 	struct qinit *qinfo;
9154 	struct ipmx_s *ipmxp;
9155 	ill_t	*ill = NULL;
9156 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9157 	int	err = 0;
9158 	boolean_t	entered_ipsq = B_FALSE;
9159 	boolean_t islink;
9160 	queue_t *dwq = NULL;
9161 
9162 	ASSERT(iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_PUNLINK ||
9163 	    iocp->ioc_cmd == I_LINK || iocp->ioc_cmd == I_UNLINK);
9164 
9165 	islink = (iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_LINK) ?
9166 	    B_TRUE : B_FALSE;
9167 
9168 	mp1 = mp->b_cont;	/* This is the linkblk info */
9169 	li = (struct linkblk *)mp1->b_rptr;
9170 
9171 	/*
9172 	 * ARP has added this special mblk, and the utility is asking us
9173 	 * to perform consistency checks, and also atomically set the
9174 	 * muxid. Ifconfig is an example.  It achieves this by using
9175 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9176 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9177 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9178 	 * and other comments in this routine for more details.
9179 	 */
9180 	mp2 = mp1->b_cont;	/* This is added by ARP */
9181 
9182 	/*
9183 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9184 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9185 	 * get the special mblk above.  For backward compatibility, we just
9186 	 * return success.  The utility will use SIOCSLIFMUXID to store
9187 	 * the muxids.  This is not atomic, and can leave the streams
9188 	 * unplumbable if the utility is interrrupted, before it does the
9189 	 * SIOCSLIFMUXID.
9190 	 */
9191 	if (mp2 == NULL) {
9192 		/*
9193 		 * At this point we don't know whether or not this is the
9194 		 * IP module stream or the ARP device stream.  We need to
9195 		 * walk the lower stream in order to find this out, since
9196 		 * the capability negotiation is done only on the IP module
9197 		 * stream.  IP module instance is identified by the module
9198 		 * name IP, non-null q_next, and it's wput not being ip_lwput.
9199 		 * STREAMS ensures that the lower stream (l_qbot) will not
9200 		 * vanish until this ioctl completes. So we can safely walk
9201 		 * the stream or refer to the q_ptr.
9202 		 */
9203 		ipwq = li->l_qbot;
9204 		while (ipwq != NULL) {
9205 			qinfo = ipwq->q_qinfo;
9206 			name = qinfo->qi_minfo->mi_idname;
9207 			if (name != NULL && name[0] != NULL &&
9208 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
9209 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
9210 			    (ipwq->q_next != NULL)) {
9211 				break;
9212 			}
9213 			ipwq = ipwq->q_next;
9214 		}
9215 		/*
9216 		 * This looks like an IP module stream, so trigger
9217 		 * the capability reset or re-negotiation if necessary.
9218 		 */
9219 		if (ipwq != NULL) {
9220 			ill = ipwq->q_ptr;
9221 			ASSERT(ill != NULL);
9222 
9223 			if (ipsq == NULL) {
9224 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
9225 				    ip_sioctl_plink, NEW_OP, B_TRUE);
9226 				if (ipsq == NULL)
9227 					return;
9228 				entered_ipsq = B_TRUE;
9229 			}
9230 			ASSERT(IAM_WRITER_ILL(ill));
9231 			/*
9232 			 * Store the upper read queue of the module
9233 			 * immediately below IP, and count the total
9234 			 * number of lower modules.  Do this only
9235 			 * for I_PLINK or I_LINK event.
9236 			 */
9237 			ill->ill_lmod_rq = NULL;
9238 			ill->ill_lmod_cnt = 0;
9239 			if (islink && (dwq = ipwq->q_next) != NULL) {
9240 				ill->ill_lmod_rq = RD(dwq);
9241 
9242 				while (dwq != NULL) {
9243 					ill->ill_lmod_cnt++;
9244 					dwq = dwq->q_next;
9245 				}
9246 			}
9247 			/*
9248 			 * There's no point in resetting or re-negotiating if
9249 			 * we are not bound to the driver, so only do this if
9250 			 * the DLPI state is idle (up); we assume such state
9251 			 * since ill_ipif_up_count gets incremented in
9252 			 * ipif_up_done(), which is after we are bound to the
9253 			 * driver.  Note that in the case of logical
9254 			 * interfaces, IP won't rebind to the driver unless
9255 			 * the ill_ipif_up_count is 0, meaning that all other
9256 			 * IP interfaces (including the main ipif) are in the
9257 			 * down state.  Because of this, we use such counter
9258 			 * as an indicator, instead of relying on the IPIF_UP
9259 			 * flag, which is per ipif instance.
9260 			 */
9261 			if (ill->ill_ipif_up_count > 0) {
9262 				if (islink)
9263 					ill_capability_probe(ill);
9264 				else
9265 					ill_capability_reset(ill);
9266 			}
9267 		}
9268 		goto done;
9269 	}
9270 
9271 	/*
9272 	 * This is an I_{P}LINK sent down by ifconfig on
9273 	 * /dev/arp. ARP has appended this last (3rd) mblk,
9274 	 * giving more info. STREAMS ensures that the lower
9275 	 * stream (l_qbot) will not vanish until this ioctl
9276 	 * completes. So we can safely walk the stream or refer
9277 	 * to the q_ptr.
9278 	 */
9279 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9280 	if (ipmxp->ipmx_arpdev_stream) {
9281 		/*
9282 		 * The operation is occuring on the arp-device
9283 		 * stream.
9284 		 */
9285 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9286 		    q, mp, ip_sioctl_plink, &err, NULL);
9287 		if (ill == NULL) {
9288 			if (err == EINPROGRESS) {
9289 				return;
9290 			} else {
9291 				err = EINVAL;
9292 				goto done;
9293 			}
9294 		}
9295 
9296 		if (ipsq == NULL) {
9297 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9298 			    NEW_OP, B_TRUE);
9299 			if (ipsq == NULL) {
9300 				ill_refrele(ill);
9301 				return;
9302 			}
9303 			entered_ipsq = B_TRUE;
9304 		}
9305 		ASSERT(IAM_WRITER_ILL(ill));
9306 		ill_refrele(ill);
9307 		/*
9308 		 * To ensure consistency between IP and ARP,
9309 		 * the following LIFO scheme is used in
9310 		 * plink/punlink. (IP first, ARP last).
9311 		 * This is because the muxid's are stored
9312 		 * in the IP stream on the ill.
9313 		 *
9314 		 * I_{P}LINK: ifconfig plinks the IP stream before
9315 		 * plinking the ARP stream. On an arp-dev
9316 		 * stream, IP checks that it is not yet
9317 		 * plinked, and it also checks that the
9318 		 * corresponding IP stream is already plinked.
9319 		 *
9320 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream
9321 		 * before punlinking the IP stream. IP does
9322 		 * not allow punlink of the IP stream unless
9323 		 * the arp stream has been punlinked.
9324 		 *
9325 		 */
9326 		if ((islink &&
9327 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9328 		    (!islink &&
9329 		    ill->ill_arp_muxid != li->l_index)) {
9330 			err = EINVAL;
9331 			goto done;
9332 		}
9333 		if (islink) {
9334 			ill->ill_arp_muxid = li->l_index;
9335 		} else {
9336 			ill->ill_arp_muxid = 0;
9337 		}
9338 	} else {
9339 		/*
9340 		 * This must be the IP module stream with or
9341 		 * without arp. Walk the stream and locate the
9342 		 * IP module. An IP module instance is
9343 		 * identified by the module name IP, non-null
9344 		 * q_next, and it's wput not being ip_lwput.
9345 		 */
9346 		ipwq = li->l_qbot;
9347 		while (ipwq != NULL) {
9348 			qinfo = ipwq->q_qinfo;
9349 			name = qinfo->qi_minfo->mi_idname;
9350 			if (name != NULL && name[0] != NULL &&
9351 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
9352 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
9353 			    (ipwq->q_next != NULL)) {
9354 				break;
9355 			}
9356 			ipwq = ipwq->q_next;
9357 		}
9358 		if (ipwq != NULL) {
9359 			ill = ipwq->q_ptr;
9360 			ASSERT(ill != NULL);
9361 
9362 			if (ipsq == NULL) {
9363 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
9364 				    ip_sioctl_plink, NEW_OP, B_TRUE);
9365 				if (ipsq == NULL)
9366 					return;
9367 				entered_ipsq = B_TRUE;
9368 			}
9369 			ASSERT(IAM_WRITER_ILL(ill));
9370 			/*
9371 			 * Return error if the ip_mux_id is
9372 			 * non-zero and command is I_{P}LINK.
9373 			 * If command is I_{P}UNLINK, return
9374 			 * error if the arp-devstr is not
9375 			 * yet punlinked.
9376 			 */
9377 			if ((islink && ill->ill_ip_muxid != 0) ||
9378 			    (!islink && ill->ill_arp_muxid != 0)) {
9379 				err = EINVAL;
9380 				goto done;
9381 			}
9382 			ill->ill_lmod_rq = NULL;
9383 			ill->ill_lmod_cnt = 0;
9384 			if (islink) {
9385 				/*
9386 				 * Store the upper read queue of the module
9387 				 * immediately below IP, and count the total
9388 				 * number of lower modules.
9389 				 */
9390 				if ((dwq = ipwq->q_next) != NULL) {
9391 					ill->ill_lmod_rq = RD(dwq);
9392 
9393 					while (dwq != NULL) {
9394 						ill->ill_lmod_cnt++;
9395 						dwq = dwq->q_next;
9396 					}
9397 				}
9398 				ill->ill_ip_muxid = li->l_index;
9399 			} else {
9400 				ill->ill_ip_muxid = 0;
9401 			}
9402 
9403 			/*
9404 			 * See comments above about resetting/re-
9405 			 * negotiating driver sub-capabilities.
9406 			 */
9407 			if (ill->ill_ipif_up_count > 0) {
9408 				if (islink)
9409 					ill_capability_probe(ill);
9410 				else
9411 					ill_capability_reset(ill);
9412 			}
9413 		}
9414 	}
9415 done:
9416 	iocp->ioc_count = 0;
9417 	iocp->ioc_error = err;
9418 	if (err == 0)
9419 		mp->b_datap->db_type = M_IOCACK;
9420 	else
9421 		mp->b_datap->db_type = M_IOCNAK;
9422 	qreply(q, mp);
9423 
9424 	/* Conn was refheld in ip_sioctl_copyin_setup */
9425 	if (CONN_Q(q))
9426 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9427 	if (entered_ipsq)
9428 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
9429 }
9430 
9431 /*
9432  * Search the ioctl command in the ioctl tables and return a pointer
9433  * to the ioctl command information. The ioctl command tables are
9434  * static and fully populated at compile time.
9435  */
9436 ip_ioctl_cmd_t *
9437 ip_sioctl_lookup(int ioc_cmd)
9438 {
9439 	int index;
9440 	ip_ioctl_cmd_t *ipip;
9441 	ip_ioctl_cmd_t *ipip_end;
9442 
9443 	if (ioc_cmd == IPI_DONTCARE)
9444 		return (NULL);
9445 
9446 	/*
9447 	 * Do a 2 step search. First search the indexed table
9448 	 * based on the least significant byte of the ioctl cmd.
9449 	 * If we don't find a match, then search the misc table
9450 	 * serially.
9451 	 */
9452 	index = ioc_cmd & 0xFF;
9453 	if (index < ip_ndx_ioctl_count) {
9454 		ipip = &ip_ndx_ioctl_table[index];
9455 		if (ipip->ipi_cmd == ioc_cmd) {
9456 			/* Found a match in the ndx table */
9457 			return (ipip);
9458 		}
9459 	}
9460 
9461 	/* Search the misc table */
9462 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
9463 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
9464 		if (ipip->ipi_cmd == ioc_cmd)
9465 			/* Found a match in the misc table */
9466 			return (ipip);
9467 	}
9468 
9469 	return (NULL);
9470 }
9471 
9472 /*
9473  * Wrapper function for resuming deferred ioctl processing
9474  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
9475  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
9476  */
9477 /* ARGSUSED */
9478 void
9479 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
9480     void *dummy_arg)
9481 {
9482 	ip_sioctl_copyin_setup(q, mp);
9483 }
9484 
9485 /*
9486  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
9487  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
9488  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
9489  * We establish here the size of the block to be copied in.  mi_copyin
9490  * arranges for this to happen, an processing continues in ip_wput with
9491  * an M_IOCDATA message.
9492  */
9493 void
9494 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
9495 {
9496 	int	copyin_size;
9497 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9498 	ip_ioctl_cmd_t *ipip;
9499 	cred_t *cr;
9500 
9501 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
9502 	if (ipip == NULL) {
9503 		/*
9504 		 * The ioctl is not one we understand or own.
9505 		 * Pass it along to be processed down stream,
9506 		 * if this is a module instance of IP, else nak
9507 		 * the ioctl.
9508 		 */
9509 		if (q->q_next == NULL) {
9510 			goto nak;
9511 		} else {
9512 			putnext(q, mp);
9513 			return;
9514 		}
9515 	}
9516 
9517 	/*
9518 	 * If this is deferred, then we will do all the checks when we
9519 	 * come back.
9520 	 */
9521 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
9522 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup()) {
9523 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
9524 		return;
9525 	}
9526 
9527 	/*
9528 	 * Only allow a very small subset of IP ioctls on this stream if
9529 	 * IP is a module and not a driver. Allowing ioctls to be processed
9530 	 * in this case may cause assert failures or data corruption.
9531 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
9532 	 * ioctls allowed on an IP module stream, after which this stream
9533 	 * normally becomes a multiplexor (at which time the stream head
9534 	 * will fail all ioctls).
9535 	 */
9536 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
9537 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
9538 			/*
9539 			 * Pass common Streams ioctls which the IP
9540 			 * module does not own or consume along to
9541 			 * be processed down stream.
9542 			 */
9543 			putnext(q, mp);
9544 			return;
9545 		} else {
9546 			goto nak;
9547 		}
9548 	}
9549 
9550 	/* Make sure we have ioctl data to process. */
9551 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
9552 		goto nak;
9553 
9554 	/*
9555 	 * Prefer dblk credential over ioctl credential; some synthesized
9556 	 * ioctls have kcred set because there's no way to crhold()
9557 	 * a credential in some contexts.  (ioc_cr is not crfree() by
9558 	 * the framework; the caller of ioctl needs to hold the reference
9559 	 * for the duration of the call).
9560 	 */
9561 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
9562 
9563 	/* Make sure normal users don't send down privileged ioctls */
9564 	if ((ipip->ipi_flags & IPI_PRIV) &&
9565 	    (cr != NULL) && secpolicy_net_config(cr, B_TRUE) != 0) {
9566 		/* We checked the privilege earlier but log it here */
9567 		miocnak(q, mp, 0, secpolicy_net_config(cr, B_FALSE));
9568 		return;
9569 	}
9570 
9571 	/*
9572 	 * The ioctl command tables can only encode fixed length
9573 	 * ioctl data. If the length is variable, the table will
9574 	 * encode the length as zero. Such special cases are handled
9575 	 * below in the switch.
9576 	 */
9577 	if (ipip->ipi_copyin_size != 0) {
9578 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
9579 		return;
9580 	}
9581 
9582 	switch (iocp->ioc_cmd) {
9583 	case O_SIOCGIFCONF:
9584 	case SIOCGIFCONF:
9585 		/*
9586 		 * This IOCTL is hilarious.  See comments in
9587 		 * ip_sioctl_get_ifconf for the story.
9588 		 */
9589 		if (iocp->ioc_count == TRANSPARENT)
9590 			copyin_size = SIZEOF_STRUCT(ifconf,
9591 			    iocp->ioc_flag);
9592 		else
9593 			copyin_size = iocp->ioc_count;
9594 		mi_copyin(q, mp, NULL, copyin_size);
9595 		return;
9596 
9597 	case O_SIOCGLIFCONF:
9598 	case SIOCGLIFCONF:
9599 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
9600 		mi_copyin(q, mp, NULL, copyin_size);
9601 		return;
9602 
9603 	case SIOCGLIFSRCOF:
9604 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
9605 		mi_copyin(q, mp, NULL, copyin_size);
9606 		return;
9607 	case SIOCGIP6ADDRPOLICY:
9608 		ip_sioctl_ip6addrpolicy(q, mp);
9609 		ip6_asp_table_refrele();
9610 		return;
9611 
9612 	case SIOCSIP6ADDRPOLICY:
9613 		ip_sioctl_ip6addrpolicy(q, mp);
9614 		return;
9615 
9616 	case SIOCGDSTINFO:
9617 		ip_sioctl_dstinfo(q, mp);
9618 		ip6_asp_table_refrele();
9619 		return;
9620 
9621 	case I_PLINK:
9622 	case I_PUNLINK:
9623 	case I_LINK:
9624 	case I_UNLINK:
9625 		/*
9626 		 * We treat non-persistent link similarly as the persistent
9627 		 * link case, in terms of plumbing/unplumbing, as well as
9628 		 * dynamic re-plumbing events indicator.  See comments
9629 		 * in ip_sioctl_plink() for more.
9630 		 *
9631 		 * Request can be enqueued in the 'ipsq' while waiting
9632 		 * to become exclusive. So bump up the conn ref.
9633 		 */
9634 		if (CONN_Q(q))
9635 			CONN_INC_REF(Q_TO_CONN(q));
9636 		ip_sioctl_plink(NULL, q, mp, NULL);
9637 		return;
9638 
9639 	case ND_GET:
9640 	case ND_SET:
9641 		/*
9642 		 * Use of the nd table requires holding the reader lock.
9643 		 * Modifying the nd table thru nd_load/nd_unload requires
9644 		 * the writer lock.
9645 		 */
9646 		rw_enter(&ip_g_nd_lock, RW_READER);
9647 		if (nd_getset(q, ip_g_nd, mp)) {
9648 			rw_exit(&ip_g_nd_lock);
9649 
9650 			if (iocp->ioc_error)
9651 				iocp->ioc_count = 0;
9652 			mp->b_datap->db_type = M_IOCACK;
9653 			qreply(q, mp);
9654 			return;
9655 		}
9656 		rw_exit(&ip_g_nd_lock);
9657 		/*
9658 		 * We don't understand this subioctl of ND_GET / ND_SET.
9659 		 * Maybe intended for some driver / module below us
9660 		 */
9661 		if (q->q_next) {
9662 			putnext(q, mp);
9663 		} else {
9664 			iocp->ioc_error = ENOENT;
9665 			mp->b_datap->db_type = M_IOCNAK;
9666 			iocp->ioc_count = 0;
9667 			qreply(q, mp);
9668 		}
9669 		return;
9670 
9671 	case IP_IOCTL:
9672 		ip_wput_ioctl(q, mp);
9673 		return;
9674 	default:
9675 		cmn_err(CE_PANIC, "should not happen ");
9676 	}
9677 nak:
9678 	if (mp->b_cont != NULL) {
9679 		freemsg(mp->b_cont);
9680 		mp->b_cont = NULL;
9681 	}
9682 	iocp->ioc_error = EINVAL;
9683 	mp->b_datap->db_type = M_IOCNAK;
9684 	iocp->ioc_count = 0;
9685 	qreply(q, mp);
9686 }
9687 
9688 /* ip_wput hands off ARP IOCTL responses to us */
9689 void
9690 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
9691 {
9692 	struct arpreq *ar;
9693 	struct xarpreq *xar;
9694 	area_t	*area;
9695 	mblk_t	*area_mp;
9696 	struct iocblk *iocp;
9697 	mblk_t	*orig_ioc_mp, *tmp;
9698 	struct iocblk	*orig_iocp;
9699 	ill_t *ill;
9700 	conn_t *connp = NULL;
9701 	uint_t ioc_id;
9702 	mblk_t *pending_mp;
9703 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
9704 	int *flagsp;
9705 	char *storage = NULL;
9706 	sin_t *sin;
9707 	ipaddr_t addr;
9708 	int err;
9709 
9710 	ill = q->q_ptr;
9711 	ASSERT(ill != NULL);
9712 
9713 	/*
9714 	 * We should get back from ARP a packet chain that looks like:
9715 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9716 	 */
9717 	if (!(area_mp = mp->b_cont) ||
9718 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
9719 	    !(orig_ioc_mp = area_mp->b_cont) ||
9720 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
9721 		freemsg(mp);
9722 		return;
9723 	}
9724 
9725 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
9726 
9727 	tmp = (orig_ioc_mp->b_cont)->b_cont;
9728 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
9729 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
9730 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
9731 		x_arp_ioctl = B_TRUE;
9732 		xar = (struct xarpreq *)tmp->b_rptr;
9733 		sin = (sin_t *)&xar->xarp_pa;
9734 		flagsp = &xar->xarp_flags;
9735 		storage = xar->xarp_ha.sdl_data;
9736 		if (xar->xarp_ha.sdl_nlen != 0)
9737 			ifx_arp_ioctl = B_TRUE;
9738 	} else {
9739 		ar = (struct arpreq *)tmp->b_rptr;
9740 		sin = (sin_t *)&ar->arp_pa;
9741 		flagsp = &ar->arp_flags;
9742 		storage = ar->arp_ha.sa_data;
9743 	}
9744 
9745 	iocp = (struct iocblk *)mp->b_rptr;
9746 
9747 	/*
9748 	 * Pick out the originating queue based on the ioc_id.
9749 	 */
9750 	ioc_id = iocp->ioc_id;
9751 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
9752 	if (pending_mp == NULL) {
9753 		ASSERT(connp == NULL);
9754 		inet_freemsg(mp);
9755 		return;
9756 	}
9757 	ASSERT(connp != NULL);
9758 	q = CONNP_TO_WQ(connp);
9759 
9760 	/* Uncouple the internally generated IOCTL from the original one */
9761 	area = (area_t *)area_mp->b_rptr;
9762 	area_mp->b_cont = NULL;
9763 
9764 	/*
9765 	 * Restore the b_next and b_prev used by mi code. This is needed
9766 	 * to complete the ioctl using mi* functions. We stored them in
9767 	 * the pending mp prior to sending the request to ARP.
9768 	 */
9769 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
9770 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
9771 	inet_freemsg(pending_mp);
9772 
9773 	/*
9774 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
9775 	 * Catch the case where there is an IRE_CACHE by no entry in the
9776 	 * arp table.
9777 	 */
9778 	addr = sin->sin_addr.s_addr;
9779 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
9780 		ire_t			*ire;
9781 		dl_unitdata_req_t	*dlup;
9782 		mblk_t			*llmp;
9783 		int			addr_len;
9784 		ill_t			*ipsqill = NULL;
9785 
9786 		if (ifx_arp_ioctl) {
9787 			/*
9788 			 * There's no need to lookup the ill, since
9789 			 * we've already done that when we started
9790 			 * processing the ioctl and sent the message
9791 			 * to ARP on that ill.  So use the ill that
9792 			 * is stored in q->q_ptr.
9793 			 */
9794 			ipsqill = ill;
9795 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
9796 			    ipsqill->ill_ipif, ALL_ZONES,
9797 			    MATCH_IRE_TYPE | MATCH_IRE_ILL);
9798 		} else {
9799 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
9800 			    NULL, ALL_ZONES, MATCH_IRE_TYPE);
9801 			if (ire != NULL)
9802 				ipsqill = ire_to_ill(ire);
9803 		}
9804 
9805 		if ((x_arp_ioctl) && (ipsqill != NULL))
9806 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
9807 
9808 		if (ire != NULL) {
9809 			*flagsp = ATF_INUSE;
9810 			llmp = ire->ire_dlureq_mp;
9811 			if (llmp != NULL && ipsqill != NULL) {
9812 				uchar_t *macaddr;
9813 
9814 				addr_len = ipsqill->ill_phys_addr_length;
9815 				if (x_arp_ioctl && ((addr_len +
9816 				    ipsqill->ill_name_length) >
9817 				    sizeof (xar->xarp_ha.sdl_data))) {
9818 					ire_refrele(ire);
9819 					freemsg(mp);
9820 					ip_ioctl_finish(q, orig_ioc_mp,
9821 					    EINVAL, NO_COPYOUT, NULL, NULL);
9822 					return;
9823 				}
9824 				*flagsp |= ATF_COM;
9825 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
9826 				if (ipsqill->ill_sap_length < 0)
9827 					macaddr = llmp->b_rptr +
9828 					    dlup->dl_dest_addr_offset;
9829 				else
9830 					macaddr = llmp->b_rptr +
9831 					    dlup->dl_dest_addr_offset +
9832 					    ipsqill->ill_sap_length;
9833 				/*
9834 				 * For SIOCGARP, MAC address length
9835 				 * validation has already been done
9836 				 * before the ioctl was issued to ARP to
9837 				 * allow it to progress only on 6 byte
9838 				 * addressable (ethernet like) media. Thus
9839 				 * the mac address copying can not overwrite
9840 				 * the sa_data area below.
9841 				 */
9842 				bcopy(macaddr, storage, addr_len);
9843 			}
9844 			/* Ditch the internal IOCTL. */
9845 			freemsg(mp);
9846 			ire_refrele(ire);
9847 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL, NULL);
9848 			return;
9849 		}
9850 	}
9851 
9852 	/*
9853 	 * Delete the coresponding IRE_CACHE if any.
9854 	 * Reset the error if there was one (in case there was no entry
9855 	 * in arp.)
9856 	 */
9857 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
9858 		ipif_t *ipintf = NULL;
9859 
9860 		if (ifx_arp_ioctl) {
9861 			/*
9862 			 * There's no need to lookup the ill, since
9863 			 * we've already done that when we started
9864 			 * processing the ioctl and sent the message
9865 			 * to ARP on that ill.  So use the ill that
9866 			 * is stored in q->q_ptr.
9867 			 */
9868 			ipintf = ill->ill_ipif;
9869 		}
9870 		if (ip_ire_clookup_and_delete(addr, ipintf)) {
9871 			/*
9872 			 * The address in "addr" may be an entry for a
9873 			 * router. If that's true, then any off-net
9874 			 * IRE_CACHE entries that go through the router
9875 			 * with address "addr" must be clobbered. Use
9876 			 * ire_walk to achieve this goal.
9877 			 */
9878 			if (ifx_arp_ioctl)
9879 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
9880 				    ire_delete_cache_gw, (char *)&addr, ill);
9881 			else
9882 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
9883 				    ALL_ZONES);
9884 			iocp->ioc_error = 0;
9885 		}
9886 	}
9887 
9888 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
9889 		err = iocp->ioc_error;
9890 		freemsg(mp);
9891 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL, NULL);
9892 		return;
9893 	}
9894 
9895 	/*
9896 	 * Completion of an SIOCG{X}ARP.  Translate the information from
9897 	 * the area_t into the struct {x}arpreq.
9898 	 */
9899 	if (x_arp_ioctl) {
9900 		storage += ill_xarp_info(&xar->xarp_ha, ill);
9901 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
9902 		    sizeof (xar->xarp_ha.sdl_data)) {
9903 			freemsg(mp);
9904 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL,
9905 			    NO_COPYOUT, NULL, NULL);
9906 			return;
9907 		}
9908 	}
9909 	*flagsp = ATF_INUSE;
9910 	if (area->area_flags & ACE_F_PERMANENT)
9911 		*flagsp |= ATF_PERM;
9912 	if (area->area_flags & ACE_F_PUBLISH)
9913 		*flagsp |= ATF_PUBL;
9914 	if (area->area_hw_addr_length != 0) {
9915 		*flagsp |= ATF_COM;
9916 		/*
9917 		 * For SIOCGARP, MAC address length validation has
9918 		 * already been done before the ioctl was issued to ARP
9919 		 * to allow it to progress only on 6 byte addressable
9920 		 * (ethernet like) media. Thus the mac address copying
9921 		 * can not overwrite the sa_data area below.
9922 		 */
9923 		bcopy((char *)area + area->area_hw_addr_offset,
9924 		    storage, area->area_hw_addr_length);
9925 	}
9926 
9927 	/* Ditch the internal IOCTL. */
9928 	freemsg(mp);
9929 	/* Complete the original. */
9930 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL, NULL);
9931 }
9932 
9933 /*
9934  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
9935  * interface) create the next available logical interface for this
9936  * physical interface.
9937  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
9938  * ipif with the specified name.
9939  *
9940  * If the address family is not AF_UNSPEC then set the address as well.
9941  *
9942  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
9943  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
9944  *
9945  * Executed as a writer on the ill or ill group.
9946  * So no lock is needed to traverse the ipif chain, or examine the
9947  * phyint flags.
9948  */
9949 /* ARGSUSED */
9950 int
9951 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9952     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9953 {
9954 	mblk_t	*mp1;
9955 	struct lifreq *lifr;
9956 	boolean_t	isv6;
9957 	boolean_t	exists;
9958 	char 	*name;
9959 	char	*endp;
9960 	char	*cp;
9961 	int	namelen;
9962 	ipif_t	*ipif;
9963 	long	id;
9964 	ipsq_t	*ipsq;
9965 	ill_t	*ill;
9966 	sin_t	*sin;
9967 	int	err = 0;
9968 	boolean_t found_sep = B_FALSE;
9969 	conn_t	*connp;
9970 	zoneid_t zoneid;
9971 	int	orig_ifindex = 0;
9972 
9973 	ip1dbg(("ip_sioctl_addif\n"));
9974 	/* Existence of mp1 has been checked in ip_wput_nondata */
9975 	mp1 = mp->b_cont->b_cont;
9976 	/*
9977 	 * Null terminate the string to protect against buffer
9978 	 * overrun. String was generated by user code and may not
9979 	 * be trusted.
9980 	 */
9981 	lifr = (struct lifreq *)mp1->b_rptr;
9982 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
9983 	name = lifr->lifr_name;
9984 	ASSERT(CONN_Q(q));
9985 	connp = Q_TO_CONN(q);
9986 	isv6 = connp->conn_af_isv6;
9987 	zoneid = connp->conn_zoneid;
9988 	namelen = mi_strlen(name);
9989 	if (namelen == 0)
9990 		return (EINVAL);
9991 
9992 	exists = B_FALSE;
9993 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
9994 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
9995 		/*
9996 		 * Allow creating lo0 using SIOCLIFADDIF.
9997 		 * can't be any other writer thread. So can pass null below
9998 		 * for the last 4 args to ipif_lookup_name.
9999 		 */
10000 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen,
10001 		    B_TRUE, &exists, isv6, zoneid, NULL, NULL, NULL, NULL);
10002 		/* Prevent any further action */
10003 		if (ipif == NULL) {
10004 			return (ENOBUFS);
10005 		} else if (!exists) {
10006 			/* We created the ipif now and as writer */
10007 			ipif_refrele(ipif);
10008 			return (0);
10009 		} else {
10010 			ill = ipif->ipif_ill;
10011 			ill_refhold(ill);
10012 			ipif_refrele(ipif);
10013 		}
10014 	} else {
10015 		/* Look for a colon in the name. */
10016 		endp = &name[namelen];
10017 		for (cp = endp; --cp > name; ) {
10018 			if (*cp == IPIF_SEPARATOR_CHAR) {
10019 				found_sep = B_TRUE;
10020 				/*
10021 				 * Reject any non-decimal aliases for plumbing
10022 				 * of logical interfaces. Aliases with leading
10023 				 * zeroes are also rejected as they introduce
10024 				 * ambiguity in the naming of the interfaces.
10025 				 * Comparing with "0" takes care of all such
10026 				 * cases.
10027 				 */
10028 				if ((strncmp("0", cp+1, 1)) == 0)
10029 					return (EINVAL);
10030 
10031 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10032 				    id <= 0 || *endp != '\0') {
10033 					return (EINVAL);
10034 				}
10035 				*cp = '\0';
10036 				break;
10037 			}
10038 		}
10039 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10040 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL);
10041 		if (found_sep)
10042 			*cp = IPIF_SEPARATOR_CHAR;
10043 		if (ill == NULL)
10044 			return (err);
10045 	}
10046 
10047 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10048 	    B_TRUE);
10049 
10050 	/*
10051 	 * Release the refhold due to the lookup, now that we are excl
10052 	 * or we are just returning
10053 	 */
10054 	ill_refrele(ill);
10055 
10056 	if (ipsq == NULL)
10057 		return (EINPROGRESS);
10058 
10059 	/*
10060 	 * If the interface is failed, inactive or offlined, look for a working
10061 	 * interface in the ill group and create the ipif there. If we can't
10062 	 * find a good interface, create the ipif anyway so that in.mpathd can
10063 	 * move it to the first repaired interface.
10064 	 */
10065 	if ((ill->ill_phyint->phyint_flags &
10066 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10067 	    ill->ill_phyint->phyint_groupname_len != 0) {
10068 		phyint_t *phyi;
10069 		char *groupname = ill->ill_phyint->phyint_groupname;
10070 
10071 		/*
10072 		 * We're looking for a working interface, but it doesn't matter
10073 		 * if it's up or down; so instead of following the group lists,
10074 		 * we look at each physical interface and compare the groupname.
10075 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10076 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10077 		 * Otherwise we create the ipif on the failed interface.
10078 		 */
10079 		rw_enter(&ill_g_lock, RW_READER);
10080 		phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
10081 		for (; phyi != NULL;
10082 		    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
10083 		    phyi, AVL_AFTER)) {
10084 			if (phyi->phyint_groupname_len == 0)
10085 				continue;
10086 			ASSERT(phyi->phyint_groupname != NULL);
10087 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10088 			    !(phyi->phyint_flags &
10089 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10090 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10091 			    (phyi->phyint_illv4 != NULL))) {
10092 				break;
10093 			}
10094 		}
10095 		rw_exit(&ill_g_lock);
10096 
10097 		if (phyi != NULL) {
10098 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10099 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10100 			    phyi->phyint_illv4);
10101 		}
10102 	}
10103 
10104 	/*
10105 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10106 	 * before or after us.
10107 	 */
10108 	ASSERT(IAM_WRITER_ILL(ill));
10109 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10110 
10111 	if (found_sep && orig_ifindex == 0) {
10112 		/* Now see if there is an IPIF with this unit number. */
10113 		for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
10114 			if (ipif->ipif_id == id) {
10115 				err = EEXIST;
10116 				goto done;
10117 			}
10118 		}
10119 	}
10120 
10121 	/*
10122 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10123 	 * of lo0. We never come here when we plumb lo0:0. It
10124 	 * happens in ipif_lookup_on_name.
10125 	 * The specified unit number is ignored when we create the ipif on a
10126 	 * different interface. However, we save it in ipif_orig_ipifid below so
10127 	 * that the ipif fails back to the right position.
10128 	 */
10129 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10130 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10131 		err = ENOBUFS;
10132 		goto done;
10133 	}
10134 
10135 	/* Return created name with ioctl */
10136 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10137 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10138 	ip1dbg(("created %s\n", lifr->lifr_name));
10139 
10140 	/* Set address */
10141 	sin = (sin_t *)&lifr->lifr_addr;
10142 	if (sin->sin_family != AF_UNSPEC) {
10143 		err = ip_sioctl_addr(ipif, sin, q, mp,
10144 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10145 	}
10146 
10147 	/* Set ifindex and unit number for failback */
10148 	if (err == 0 && orig_ifindex != 0) {
10149 		ipif->ipif_orig_ifindex = orig_ifindex;
10150 		if (found_sep) {
10151 			ipif->ipif_orig_ipifid = id;
10152 		}
10153 	}
10154 
10155 done:
10156 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10157 	return (err);
10158 }
10159 
10160 /*
10161  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10162  * interface) delete it based on the IP address (on this physical interface).
10163  * Otherwise delete it based on the ipif_id.
10164  * Also, special handling to allow a removeif of lo0.
10165  */
10166 /* ARGSUSED */
10167 int
10168 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10169     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10170 {
10171 	conn_t		*connp;
10172 	ill_t		*ill = ipif->ipif_ill;
10173 	boolean_t	 success;
10174 
10175 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10176 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10177 	ASSERT(IAM_WRITER_IPIF(ipif));
10178 
10179 	connp = Q_TO_CONN(q);
10180 	/*
10181 	 * Special case for unplumbing lo0 (the loopback physical interface).
10182 	 * If unplumbing lo0, the incoming address structure has been
10183 	 * initialized to all zeros. When unplumbing lo0, all its logical
10184 	 * interfaces must be removed too.
10185 	 *
10186 	 * Note that this interface may be called to remove a specific
10187 	 * loopback logical interface (eg, lo0:1). But in that case
10188 	 * ipif->ipif_id != 0 so that the code path for that case is the
10189 	 * same as any other interface (meaning it skips the code directly
10190 	 * below).
10191 	 */
10192 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10193 		if (sin->sin_family == AF_UNSPEC &&
10194 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10195 			/*
10196 			 * Mark it condemned. No new ref. will be made to ill.
10197 			 */
10198 			mutex_enter(&ill->ill_lock);
10199 			ill->ill_state_flags |= ILL_CONDEMNED;
10200 			for (ipif = ill->ill_ipif; ipif != NULL;
10201 			    ipif = ipif->ipif_next) {
10202 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10203 			}
10204 			mutex_exit(&ill->ill_lock);
10205 
10206 			ipif = ill->ill_ipif;
10207 			/* unplumb the loopback interface */
10208 			ill_delete(ill);
10209 			mutex_enter(&connp->conn_lock);
10210 			mutex_enter(&ill->ill_lock);
10211 			ASSERT(ill->ill_group == NULL);
10212 
10213 			/* Are any references to this ill active */
10214 			if (ill_is_quiescent(ill)) {
10215 				mutex_exit(&ill->ill_lock);
10216 				mutex_exit(&connp->conn_lock);
10217 				ill_delete_tail(ill);
10218 				return (0);
10219 			}
10220 			success = ipsq_pending_mp_add(connp, ipif,
10221 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10222 			mutex_exit(&connp->conn_lock);
10223 			mutex_exit(&ill->ill_lock);
10224 			if (success)
10225 				return (EINPROGRESS);
10226 			else
10227 				return (EINTR);
10228 		}
10229 	}
10230 
10231 	/*
10232 	 * We are exclusive on the ipsq, so an ill move will be serialized
10233 	 * before or after us.
10234 	 */
10235 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10236 
10237 	if (ipif->ipif_id == 0) {
10238 		/* Find based on address */
10239 		if (ipif->ipif_isv6) {
10240 			sin6_t *sin6;
10241 
10242 			if (sin->sin_family != AF_INET6)
10243 				return (EAFNOSUPPORT);
10244 
10245 			sin6 = (sin6_t *)sin;
10246 			/* We are a writer, so we should be able to lookup */
10247 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10248 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL);
10249 			if (ipif == NULL) {
10250 				/*
10251 				 * Maybe the address in on another interface in
10252 				 * the same IPMP group? We check this below.
10253 				 */
10254 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10255 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL);
10256 			}
10257 		} else {
10258 			ipaddr_t addr;
10259 
10260 			if (sin->sin_family != AF_INET)
10261 				return (EAFNOSUPPORT);
10262 
10263 			addr = sin->sin_addr.s_addr;
10264 			/* We are a writer, so we should be able to lookup */
10265 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10266 			    NULL, NULL, NULL);
10267 			if (ipif == NULL) {
10268 				/*
10269 				 * Maybe the address in on another interface in
10270 				 * the same IPMP group? We check this below.
10271 				 */
10272 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10273 				    NULL, NULL, NULL, NULL);
10274 			}
10275 		}
10276 		if (ipif == NULL) {
10277 			return (EADDRNOTAVAIL);
10278 		}
10279 		/*
10280 		 * When the address to be removed is hosted on a different
10281 		 * interface, we check if the interface is in the same IPMP
10282 		 * group as the specified one; if so we proceed with the
10283 		 * removal.
10284 		 * ill->ill_group is NULL when the ill is down, so we have to
10285 		 * compare the group names instead.
10286 		 */
10287 		if (ipif->ipif_ill != ill &&
10288 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10289 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10290 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10291 		    ill->ill_phyint->phyint_groupname) != 0)) {
10292 			ipif_refrele(ipif);
10293 			return (EADDRNOTAVAIL);
10294 		}
10295 
10296 		/* This is a writer */
10297 		ipif_refrele(ipif);
10298 	}
10299 
10300 	/*
10301 	 * Can not delete instance zero since it is tied to the ill.
10302 	 */
10303 	if (ipif->ipif_id == 0)
10304 		return (EBUSY);
10305 
10306 	mutex_enter(&ill->ill_lock);
10307 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10308 	mutex_exit(&ill->ill_lock);
10309 
10310 	ipif_free(ipif);
10311 
10312 	mutex_enter(&connp->conn_lock);
10313 	mutex_enter(&ill->ill_lock);
10314 
10315 	/* Are any references to this ipif active */
10316 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
10317 		mutex_exit(&ill->ill_lock);
10318 		mutex_exit(&connp->conn_lock);
10319 		ipif_down_tail(ipif);
10320 		ipif_free_tail(ipif);
10321 		return (0);
10322 	    }
10323 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10324 	    IPIF_FREE);
10325 	mutex_exit(&ill->ill_lock);
10326 	mutex_exit(&connp->conn_lock);
10327 	if (success)
10328 		return (EINPROGRESS);
10329 	else
10330 		return (EINTR);
10331 }
10332 
10333 /*
10334  * Restart the removeif ioctl. The refcnt has gone down to 0.
10335  * The ipif is already condemned. So can't find it thru lookups.
10336  */
10337 /* ARGSUSED */
10338 int
10339 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10340     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10341 {
10342 	ill_t *ill;
10343 
10344 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10345 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10346 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10347 		ill = ipif->ipif_ill;
10348 		ASSERT(IAM_WRITER_ILL(ill));
10349 		ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) &&
10350 		    (ill->ill_state_flags & IPIF_CONDEMNED));
10351 		ill_delete_tail(ill);
10352 		return (0);
10353 	}
10354 
10355 	ill = ipif->ipif_ill;
10356 	ASSERT(IAM_WRITER_IPIF(ipif));
10357 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10358 
10359 	ipif_down_tail(ipif);
10360 	ipif_free_tail(ipif);
10361 
10362 	ILL_UNMARK_CHANGING(ill);
10363 	return (0);
10364 }
10365 
10366 /*
10367  * Set the local interface address.
10368  * Allow an address of all zero when the interface is down.
10369  */
10370 /* ARGSUSED */
10371 int
10372 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10373     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10374 {
10375 	int err = 0;
10376 	in6_addr_t v6addr;
10377 	boolean_t need_up = B_FALSE;
10378 
10379 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
10380 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10381 
10382 	ASSERT(IAM_WRITER_IPIF(ipif));
10383 
10384 	if (ipif->ipif_isv6) {
10385 		sin6_t *sin6;
10386 		ill_t *ill;
10387 		phyint_t *phyi;
10388 
10389 		if (sin->sin_family != AF_INET6)
10390 			return (EAFNOSUPPORT);
10391 
10392 		sin6 = (sin6_t *)sin;
10393 		v6addr = sin6->sin6_addr;
10394 		ill = ipif->ipif_ill;
10395 		phyi = ill->ill_phyint;
10396 
10397 		/*
10398 		 * Enforce that true multicast interfaces have a link-local
10399 		 * address for logical unit 0.
10400 		 */
10401 		if (ipif->ipif_id == 0 &&
10402 		    (ill->ill_flags & ILLF_MULTICAST) &&
10403 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
10404 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
10405 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
10406 			return (EADDRNOTAVAIL);
10407 		}
10408 
10409 		/*
10410 		 * up interfaces shouldn't have the unspecified address
10411 		 * unless they also have the IPIF_NOLOCAL flags set and
10412 		 * have a subnet assigned.
10413 		 */
10414 		if ((ipif->ipif_flags & IPIF_UP) &&
10415 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
10416 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
10417 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
10418 			return (EADDRNOTAVAIL);
10419 		}
10420 
10421 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
10422 			return (EADDRNOTAVAIL);
10423 	} else {
10424 		ipaddr_t addr;
10425 
10426 		if (sin->sin_family != AF_INET)
10427 			return (EAFNOSUPPORT);
10428 
10429 		addr = sin->sin_addr.s_addr;
10430 
10431 		/* Allow 0 as the local address. */
10432 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
10433 			return (EADDRNOTAVAIL);
10434 
10435 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10436 	}
10437 
10438 
10439 	/*
10440 	 * Even if there is no change we redo things just to rerun
10441 	 * ipif_set_default.
10442 	 */
10443 	if (ipif->ipif_flags & IPIF_UP) {
10444 		/*
10445 		 * Setting a new local address, make sure
10446 		 * we have net and subnet bcast ire's for
10447 		 * the old address if we need them.
10448 		 */
10449 		if (!ipif->ipif_isv6)
10450 			ipif_check_bcast_ires(ipif);
10451 		/*
10452 		 * If the interface is already marked up,
10453 		 * we call ipif_down which will take care
10454 		 * of ditching any IREs that have been set
10455 		 * up based on the old interface address.
10456 		 */
10457 		err = ipif_logical_down(ipif, q, mp);
10458 		if (err == EINPROGRESS)
10459 			return (err);
10460 		ipif_down_tail(ipif);
10461 		need_up = 1;
10462 	}
10463 
10464 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
10465 	return (err);
10466 }
10467 
10468 int
10469 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10470     boolean_t need_up)
10471 {
10472 	in6_addr_t v6addr;
10473 	ipaddr_t addr;
10474 	sin6_t	*sin6;
10475 	int	err = 0;
10476 
10477 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
10478 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10479 	ASSERT(IAM_WRITER_IPIF(ipif));
10480 	if (ipif->ipif_isv6) {
10481 		sin6 = (sin6_t *)sin;
10482 		v6addr = sin6->sin6_addr;
10483 	} else {
10484 		addr = sin->sin_addr.s_addr;
10485 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10486 	}
10487 	mutex_enter(&ipif->ipif_ill->ill_lock);
10488 	ipif->ipif_v6lcl_addr = v6addr;
10489 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
10490 		ipif->ipif_v6src_addr = ipv6_all_zeros;
10491 	} else {
10492 		ipif->ipif_v6src_addr = v6addr;
10493 	}
10494 
10495 	if ((ipif->ipif_isv6) && IN6_IS_ADDR_6TO4(&v6addr) &&
10496 		(!ipif->ipif_ill->ill_is_6to4tun)) {
10497 		queue_t *wqp = ipif->ipif_ill->ill_wq;
10498 
10499 		/*
10500 		 * The local address of this interface is a 6to4 address,
10501 		 * check if this interface is in fact a 6to4 tunnel or just
10502 		 * an interface configured with a 6to4 address.  We are only
10503 		 * interested in the former.
10504 		 */
10505 		if (wqp != NULL) {
10506 			while ((wqp->q_next != NULL) &&
10507 			    (wqp->q_next->q_qinfo != NULL) &&
10508 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
10509 
10510 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
10511 				    == TUN6TO4_MODID) {
10512 					/* set for use in IP */
10513 					ipif->ipif_ill->ill_is_6to4tun = 1;
10514 					break;
10515 				}
10516 				wqp = wqp->q_next;
10517 			}
10518 		}
10519 	}
10520 
10521 	ipif_set_default(ipif);
10522 	mutex_exit(&ipif->ipif_ill->ill_lock);
10523 
10524 	if (need_up) {
10525 		/*
10526 		 * Now bring the interface back up.  If this
10527 		 * is the only IPIF for the ILL, ipif_up
10528 		 * will have to re-bind to the device, so
10529 		 * we may get back EINPROGRESS, in which
10530 		 * case, this IOCTL will get completed in
10531 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10532 		 */
10533 		err = ipif_up(ipif, q, mp);
10534 	} else {
10535 		/*
10536 		 * Update the IPIF list in SCTP, ipif_up_done() will do it
10537 		 * if need_up is true.
10538 		 */
10539 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10540 	}
10541 
10542 	return (err);
10543 }
10544 
10545 
10546 /*
10547  * Restart entry point to restart the address set operation after the
10548  * refcounts have dropped to zero.
10549  */
10550 /* ARGSUSED */
10551 int
10552 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10553     ip_ioctl_cmd_t *ipip, void *ifreq)
10554 {
10555 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
10556 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10557 	ASSERT(IAM_WRITER_IPIF(ipif));
10558 	ipif_down_tail(ipif);
10559 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
10560 }
10561 
10562 /* ARGSUSED */
10563 int
10564 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10565     ip_ioctl_cmd_t *ipip, void *if_req)
10566 {
10567 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
10568 	struct lifreq *lifr = (struct lifreq *)if_req;
10569 
10570 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
10571 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10572 	/*
10573 	 * The net mask and address can't change since we have a
10574 	 * reference to the ipif. So no lock is necessary.
10575 	 */
10576 	if (ipif->ipif_isv6) {
10577 		*sin6 = sin6_null;
10578 		sin6->sin6_family = AF_INET6;
10579 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
10580 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10581 		lifr->lifr_addrlen =
10582 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10583 	} else {
10584 		*sin = sin_null;
10585 		sin->sin_family = AF_INET;
10586 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
10587 		if (ipip->ipi_cmd_type == LIF_CMD) {
10588 			lifr->lifr_addrlen =
10589 			    ip_mask_to_plen(ipif->ipif_net_mask);
10590 		}
10591 	}
10592 	return (0);
10593 }
10594 
10595 /*
10596  * Set the destination address for a pt-pt interface.
10597  */
10598 /* ARGSUSED */
10599 int
10600 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10601     ip_ioctl_cmd_t *ipip, void *if_req)
10602 {
10603 	int err = 0;
10604 	in6_addr_t v6addr;
10605 	boolean_t need_up = B_FALSE;
10606 
10607 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
10608 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10609 	ASSERT(IAM_WRITER_IPIF(ipif));
10610 
10611 	if (ipif->ipif_isv6) {
10612 		sin6_t *sin6;
10613 
10614 		if (sin->sin_family != AF_INET6)
10615 			return (EAFNOSUPPORT);
10616 
10617 		sin6 = (sin6_t *)sin;
10618 		v6addr = sin6->sin6_addr;
10619 
10620 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
10621 			return (EADDRNOTAVAIL);
10622 	} else {
10623 		ipaddr_t addr;
10624 
10625 		if (sin->sin_family != AF_INET)
10626 			return (EAFNOSUPPORT);
10627 
10628 		addr = sin->sin_addr.s_addr;
10629 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
10630 			return (EADDRNOTAVAIL);
10631 
10632 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10633 	}
10634 
10635 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
10636 		return (0);	/* No change */
10637 
10638 	if (ipif->ipif_flags & IPIF_UP) {
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 pp dst 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 = B_TRUE;
10650 	}
10651 	/*
10652 	 * could return EINPROGRESS. If so ioctl will complete in
10653 	 * ip_rput_dlpi_writer
10654 	 */
10655 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
10656 	return (err);
10657 }
10658 
10659 static int
10660 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10661     boolean_t need_up)
10662 {
10663 	in6_addr_t v6addr;
10664 	ill_t	*ill = ipif->ipif_ill;
10665 	int	err = 0;
10666 
10667 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n",
10668 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10669 	if (ipif->ipif_isv6) {
10670 		sin6_t *sin6;
10671 
10672 		sin6 = (sin6_t *)sin;
10673 		v6addr = sin6->sin6_addr;
10674 	} else {
10675 		ipaddr_t addr;
10676 
10677 		addr = sin->sin_addr.s_addr;
10678 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10679 	}
10680 	mutex_enter(&ill->ill_lock);
10681 	/* Set point to point destination address. */
10682 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10683 		/*
10684 		 * Allow this as a means of creating logical
10685 		 * pt-pt interfaces on top of e.g. an Ethernet.
10686 		 * XXX Undocumented HACK for testing.
10687 		 * pt-pt interfaces are created with NUD disabled.
10688 		 */
10689 		ipif->ipif_flags |= IPIF_POINTOPOINT;
10690 		ipif->ipif_flags &= ~IPIF_BROADCAST;
10691 		if (ipif->ipif_isv6)
10692 			ipif->ipif_ill->ill_flags |= ILLF_NONUD;
10693 	}
10694 
10695 	/* Set the new address. */
10696 	ipif->ipif_v6pp_dst_addr = v6addr;
10697 	/* Make sure subnet tracks pp_dst */
10698 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
10699 	mutex_exit(&ill->ill_lock);
10700 
10701 	if (need_up) {
10702 		/*
10703 		 * Now bring the interface back up.  If this
10704 		 * is the only IPIF for the ILL, ipif_up
10705 		 * will have to re-bind to the device, so
10706 		 * we may get back EINPROGRESS, in which
10707 		 * case, this IOCTL will get completed in
10708 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10709 		 */
10710 		err = ipif_up(ipif, q, mp);
10711 	}
10712 	return (err);
10713 }
10714 
10715 /*
10716  * Restart entry point to restart the dstaddress set operation after the
10717  * refcounts have dropped to zero.
10718  */
10719 /* ARGSUSED */
10720 int
10721 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10722     ip_ioctl_cmd_t *ipip, void *ifreq)
10723 {
10724 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
10725 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10726 	ipif_down_tail(ipif);
10727 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
10728 }
10729 
10730 /* ARGSUSED */
10731 int
10732 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10733     ip_ioctl_cmd_t *ipip, void *if_req)
10734 {
10735 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
10736 
10737 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
10738 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10739 	/*
10740 	 * Get point to point destination address. The addresses can't
10741 	 * change since we hold a reference to the ipif.
10742 	 */
10743 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
10744 		return (EADDRNOTAVAIL);
10745 
10746 	if (ipif->ipif_isv6) {
10747 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10748 		*sin6 = sin6_null;
10749 		sin6->sin6_family = AF_INET6;
10750 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
10751 	} else {
10752 		*sin = sin_null;
10753 		sin->sin_family = AF_INET;
10754 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
10755 	}
10756 	return (0);
10757 }
10758 
10759 /*
10760  * part of ipmp, make this func return the active/inactive state and
10761  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
10762  */
10763 /*
10764  * This function either sets or clears the IFF_INACTIVE flag.
10765  *
10766  * As long as there are some addresses or multicast memberships on the
10767  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
10768  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
10769  * will be used for outbound packets.
10770  *
10771  * Caller needs to verify the validity of setting IFF_INACTIVE.
10772  */
10773 static void
10774 phyint_inactive(phyint_t *phyi)
10775 {
10776 	ill_t *ill_v4;
10777 	ill_t *ill_v6;
10778 	ipif_t *ipif;
10779 	ilm_t *ilm;
10780 
10781 	ill_v4 = phyi->phyint_illv4;
10782 	ill_v6 = phyi->phyint_illv6;
10783 
10784 	/*
10785 	 * No need for a lock while traversing the list since iam
10786 	 * a writer
10787 	 */
10788 	if (ill_v4 != NULL) {
10789 		ASSERT(IAM_WRITER_ILL(ill_v4));
10790 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
10791 		    ipif = ipif->ipif_next) {
10792 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
10793 				mutex_enter(&phyi->phyint_lock);
10794 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10795 				mutex_exit(&phyi->phyint_lock);
10796 				return;
10797 			}
10798 		}
10799 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
10800 		    ilm = ilm->ilm_next) {
10801 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
10802 				mutex_enter(&phyi->phyint_lock);
10803 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10804 				mutex_exit(&phyi->phyint_lock);
10805 				return;
10806 			}
10807 		}
10808 	}
10809 	if (ill_v6 != NULL) {
10810 		ill_v6 = phyi->phyint_illv6;
10811 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
10812 		    ipif = ipif->ipif_next) {
10813 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
10814 				mutex_enter(&phyi->phyint_lock);
10815 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10816 				mutex_exit(&phyi->phyint_lock);
10817 				return;
10818 			}
10819 		}
10820 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
10821 		    ilm = ilm->ilm_next) {
10822 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
10823 				mutex_enter(&phyi->phyint_lock);
10824 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10825 				mutex_exit(&phyi->phyint_lock);
10826 				return;
10827 			}
10828 		}
10829 	}
10830 	mutex_enter(&phyi->phyint_lock);
10831 	phyi->phyint_flags |= PHYI_INACTIVE;
10832 	mutex_exit(&phyi->phyint_lock);
10833 }
10834 
10835 /*
10836  * This function is called only when the phyint flags change. Currently
10837  * called from ip_sioctl_flags. We re-do the broadcast nomination so
10838  * that we can select a good ill.
10839  */
10840 static void
10841 ip_redo_nomination(phyint_t *phyi)
10842 {
10843 	ill_t *ill_v4;
10844 
10845 	ill_v4 = phyi->phyint_illv4;
10846 
10847 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
10848 		ASSERT(IAM_WRITER_ILL(ill_v4));
10849 		if (ill_v4->ill_group->illgrp_ill_count > 1)
10850 			ill_nominate_bcast_rcv(ill_v4->ill_group);
10851 	}
10852 }
10853 
10854 /*
10855  * Heuristic to check if ill is INACTIVE.
10856  * Checks if ill has an ipif with an usable ip address.
10857  *
10858  * Return values:
10859  *	B_TRUE	- ill is INACTIVE; has no usable ipif
10860  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
10861  */
10862 static boolean_t
10863 ill_is_inactive(ill_t *ill)
10864 {
10865 	ipif_t *ipif;
10866 
10867 	/* Check whether it is in an IPMP group */
10868 	if (ill->ill_phyint->phyint_groupname == NULL)
10869 		return (B_FALSE);
10870 
10871 	if (ill->ill_ipif_up_count == 0)
10872 		return (B_TRUE);
10873 
10874 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
10875 		uint64_t flags = ipif->ipif_flags;
10876 
10877 		/*
10878 		 * This ipif is usable if it is IPIF_UP and not a
10879 		 * dedicated test address.  A dedicated test address
10880 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
10881 		 * (note in particular that V6 test addresses are
10882 		 * link-local data addresses and thus are marked
10883 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
10884 		 */
10885 		if ((flags & IPIF_UP) &&
10886 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
10887 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
10888 			return (B_FALSE);
10889 	}
10890 	return (B_TRUE);
10891 }
10892 
10893 /*
10894  * Set interface flags.
10895  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
10896  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
10897  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
10898  *
10899  * NOTE : We really don't enforce that ipif_id zero should be used
10900  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
10901  *	  is because applications generally does SICGLIFFLAGS and
10902  *	  ORs in the new flags (that affects the logical) and does a
10903  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
10904  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
10905  *	  flags that will be turned on is correct with respect to
10906  *	  ipif_id 0. For backward compatibility reasons, it is not done.
10907  */
10908 /* ARGSUSED */
10909 int
10910 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10911     ip_ioctl_cmd_t *ipip, void *if_req)
10912 {
10913 	uint64_t turn_on;
10914 	uint64_t turn_off;
10915 	int	err;
10916 	boolean_t need_up = B_FALSE;
10917 	phyint_t *phyi;
10918 	ill_t *ill;
10919 	uint64_t intf_flags;
10920 	boolean_t phyint_flags_modified = B_FALSE;
10921 	uint64_t flags;
10922 	struct ifreq *ifr;
10923 	struct lifreq *lifr;
10924 	boolean_t set_linklocal = B_FALSE;
10925 	boolean_t zero_source = B_FALSE;
10926 
10927 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
10928 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10929 
10930 	ASSERT(IAM_WRITER_IPIF(ipif));
10931 
10932 	ill = ipif->ipif_ill;
10933 	phyi = ill->ill_phyint;
10934 
10935 	if (ipip->ipi_cmd_type == IF_CMD) {
10936 		ifr = (struct ifreq *)if_req;
10937 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
10938 	} else {
10939 		lifr = (struct lifreq *)if_req;
10940 		flags = lifr->lifr_flags;
10941 	}
10942 
10943 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10944 
10945 	/*
10946 	 * Has the flags been set correctly till now ?
10947 	 */
10948 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10949 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10950 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10951 	/*
10952 	 * Compare the new flags to the old, and partition
10953 	 * into those coming on and those going off.
10954 	 * For the 16 bit command keep the bits above bit 16 unchanged.
10955 	 */
10956 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
10957 		flags |= intf_flags & ~0xFFFF;
10958 
10959 	/*
10960 	 * First check which bits will change and then which will
10961 	 * go on and off
10962 	 */
10963 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
10964 	if (!turn_on)
10965 		return (0);	/* No change */
10966 
10967 	turn_off = intf_flags & turn_on;
10968 	turn_on ^= turn_off;
10969 	err = 0;
10970 
10971 	/*
10972 	 * Don't allow any bits belonging to the logical interface
10973 	 * to be set or cleared on the replacement ipif that was
10974 	 * created temporarily during a MOVE.
10975 	 */
10976 	if (ipif->ipif_replace_zero &&
10977 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
10978 		return (EINVAL);
10979 	}
10980 
10981 	/*
10982 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
10983 	 * IPv6 interfaces.
10984 	 */
10985 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
10986 		return (EINVAL);
10987 
10988 	/*
10989 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
10990 	 * interfaces.  It makes no sense in that context.
10991 	 */
10992 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
10993 		return (EINVAL);
10994 
10995 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
10996 		zero_source = B_TRUE;
10997 
10998 	/*
10999 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11000 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11001 	 * If the link local address isn't set, and can be set, it will get
11002 	 * set later on in this function.
11003 	 */
11004 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11005 	    (flags & IFF_UP) && !zero_source &&
11006 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11007 		if (ipif_cant_setlinklocal(ipif))
11008 			return (EINVAL);
11009 		set_linklocal = B_TRUE;
11010 	}
11011 
11012 	/*
11013 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11014 	 * same time. No need to grab ill_g_usesrc_lock here, see
11015 	 * synchronization notes in ip.c
11016 	 */
11017 	if (turn_on & PHYI_STANDBY &&
11018 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11019 		return (EINVAL);
11020 	}
11021 
11022 	/*
11023 	 * If we modify physical interface flags, we'll potentially need to
11024 	 * send up two routing socket messages for the changes (one for the
11025 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11026 	 */
11027 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11028 		phyint_flags_modified = B_TRUE;
11029 
11030 	/*
11031 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11032 	 * we need to flush the IRE_CACHES belonging to this ill.
11033 	 * We handle this case here without doing the DOWN/UP dance
11034 	 * like it is done for other flags. If some other flags are
11035 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11036 	 * below will handle it by bringing it down and then
11037 	 * bringing it UP.
11038 	 */
11039 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11040 		ill_t *ill_v4, *ill_v6;
11041 
11042 		ill_v4 = phyi->phyint_illv4;
11043 		ill_v6 = phyi->phyint_illv6;
11044 
11045 		/*
11046 		 * First set the INACTIVE flag if needed. Then delete the ires.
11047 		 * ire_add will atomically prevent creating new IRE_CACHEs
11048 		 * unless hidden flag is set.
11049 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11050 		 */
11051 		if ((turn_on & PHYI_FAILED) &&
11052 		    ((intf_flags & PHYI_STANDBY) || !ipmp_enable_failback)) {
11053 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11054 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11055 		}
11056 		if ((turn_off & PHYI_FAILED) &&
11057 		    ((intf_flags & PHYI_STANDBY) ||
11058 		    (!ipmp_enable_failback && ill_is_inactive(ill)))) {
11059 			phyint_inactive(phyi);
11060 		}
11061 
11062 		if (turn_on & PHYI_STANDBY) {
11063 			/*
11064 			 * We implicitly set INACTIVE only when STANDBY is set.
11065 			 * INACTIVE is also set on non-STANDBY phyint when user
11066 			 * disables FAILBACK using configuration file.
11067 			 * Do not allow STANDBY to be set on such INACTIVE
11068 			 * phyint
11069 			 */
11070 			if (phyi->phyint_flags & PHYI_INACTIVE)
11071 				return (EINVAL);
11072 			if (!(phyi->phyint_flags & PHYI_FAILED))
11073 				phyint_inactive(phyi);
11074 		}
11075 		if (turn_off & PHYI_STANDBY) {
11076 			if (ipmp_enable_failback) {
11077 				/*
11078 				 * Reset PHYI_INACTIVE.
11079 				 */
11080 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11081 			} else if (ill_is_inactive(ill) &&
11082 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11083 				/*
11084 				 * Need to set INACTIVE, when user sets
11085 				 * STANDBY on a non-STANDBY phyint and
11086 				 * later resets STANDBY
11087 				 */
11088 				phyint_inactive(phyi);
11089 			}
11090 		}
11091 		/*
11092 		 * We should always send up a message so that the
11093 		 * daemons come to know of it. Note that the zeroth
11094 		 * interface can be down and the check below for IPIF_UP
11095 		 * will not make sense as we are actually setting
11096 		 * a phyint flag here. We assume that the ipif used
11097 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11098 		 * send up any message for non-zero ipifs).
11099 		 */
11100 		phyint_flags_modified = B_TRUE;
11101 
11102 		if (ill_v4 != NULL) {
11103 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11104 			    IRE_CACHE, ill_stq_cache_delete,
11105 			    (char *)ill_v4, ill_v4);
11106 			illgrp_reset_schednext(ill_v4);
11107 		}
11108 		if (ill_v6 != NULL) {
11109 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11110 			    IRE_CACHE, ill_stq_cache_delete,
11111 			    (char *)ill_v6, ill_v6);
11112 			illgrp_reset_schednext(ill_v6);
11113 		}
11114 	}
11115 
11116 	/*
11117 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11118 	 * status of the interface and, if the interface is part of an IPMP
11119 	 * group, all other interfaces that are part of the same IPMP
11120 	 * group.
11121 	 */
11122 	if ((turn_on | turn_off) & ILLF_ROUTER) {
11123 		(void) ill_forward_set(q, mp, ((turn_on & ILLF_ROUTER) != 0),
11124 		    (caddr_t)ill);
11125 	}
11126 
11127 	/*
11128 	 * If the interface is not UP and we are not going to
11129 	 * bring it UP, record the flags and return. When the
11130 	 * interface comes UP later, the right actions will be
11131 	 * taken.
11132 	 */
11133 	if (!(ipif->ipif_flags & IPIF_UP) &&
11134 	    !(turn_on & IPIF_UP)) {
11135 		/* Record new flags in their respective places. */
11136 		mutex_enter(&ill->ill_lock);
11137 		mutex_enter(&ill->ill_phyint->phyint_lock);
11138 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11139 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11140 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11141 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11142 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11143 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11144 		mutex_exit(&ill->ill_lock);
11145 		mutex_exit(&ill->ill_phyint->phyint_lock);
11146 
11147 		/*
11148 		 * We do the broadcast and nomination here rather
11149 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11150 		 * the case of FAILBACK from INACTIVE standby to the
11151 		 * interface that has been repaired, PHYI_FAILED has not
11152 		 * been cleared yet. If there are only two interfaces in
11153 		 * that group, all we have is a FAILED and INACTIVE
11154 		 * interface. If we do the nomination soon after a failback,
11155 		 * the broadcast nomination code would select the
11156 		 * INACTIVE interface for receiving broadcasts as FAILED is
11157 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11158 		 * receive broadcast packets, we need to redo nomination
11159 		 * when the FAILED is cleared here. Thus, in general we
11160 		 * always do the nomination here for FAILED, STANDBY
11161 		 * and OFFLINE.
11162 		 */
11163 		if (((turn_on | turn_off) &
11164 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11165 			ip_redo_nomination(phyi);
11166 		}
11167 		if (phyint_flags_modified) {
11168 			if (phyi->phyint_illv4 != NULL) {
11169 				ip_rts_ifmsg(phyi->phyint_illv4->
11170 				    ill_ipif);
11171 			}
11172 			if (phyi->phyint_illv6 != NULL) {
11173 				ip_rts_ifmsg(phyi->phyint_illv6->
11174 				    ill_ipif);
11175 			}
11176 		}
11177 		return (0);
11178 	} else if (set_linklocal || zero_source) {
11179 		mutex_enter(&ill->ill_lock);
11180 		if (set_linklocal)
11181 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11182 		if (zero_source)
11183 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11184 		mutex_exit(&ill->ill_lock);
11185 	}
11186 
11187 	/*
11188 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11189 	 * or point-to-point interfaces with an unspecified destination. We do
11190 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11191 	 * have a subnet assigned, which is how in.ndpd currently manages its
11192 	 * onlink prefix list when no addresses are configured with those
11193 	 * prefixes.
11194 	 */
11195 	if (ipif->ipif_isv6 &&
11196 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11197 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11198 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11199 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11200 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11201 		return (EINVAL);
11202 	}
11203 
11204 	/*
11205 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11206 	 * from being brought up.
11207 	 */
11208 	if (!ipif->ipif_isv6 &&
11209 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11210 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11211 		return (EINVAL);
11212 	}
11213 
11214 	/*
11215 	 * The only flag changes that we currently take specific action on
11216 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11217 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11218 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11219 	 * the flags and bringing it back up again.
11220 	 */
11221 	if ((turn_on|turn_off) &
11222 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11223 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11224 		/*
11225 		 * Taking this ipif down, make sure we have
11226 		 * valid net and subnet bcast ire's for other
11227 		 * logical interfaces, if we need them.
11228 		 */
11229 		if (!ipif->ipif_isv6)
11230 			ipif_check_bcast_ires(ipif);
11231 
11232 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11233 		    !(turn_off & IPIF_UP)) {
11234 			need_up = B_TRUE;
11235 			if (ipif->ipif_flags & IPIF_UP)
11236 				ill->ill_logical_down = 1;
11237 			turn_on &= ~IPIF_UP;
11238 		}
11239 		err = ipif_down(ipif, q, mp);
11240 		ip1dbg(("ipif_down returns %d err ", err));
11241 		if (err == EINPROGRESS)
11242 			return (err);
11243 		ipif_down_tail(ipif);
11244 	}
11245 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
11246 }
11247 
11248 static int
11249 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
11250     boolean_t need_up)
11251 {
11252 	ill_t	*ill;
11253 	phyint_t *phyi;
11254 	uint64_t turn_on;
11255 	uint64_t turn_off;
11256 	uint64_t intf_flags;
11257 	boolean_t phyint_flags_modified = B_FALSE;
11258 	int	err = 0;
11259 	boolean_t set_linklocal = B_FALSE;
11260 	boolean_t zero_source = B_FALSE;
11261 
11262 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
11263 		ipif->ipif_ill->ill_name, ipif->ipif_id));
11264 
11265 	ASSERT(IAM_WRITER_IPIF(ipif));
11266 
11267 	ill = ipif->ipif_ill;
11268 	phyi = ill->ill_phyint;
11269 
11270 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11271 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
11272 
11273 	turn_off = intf_flags & turn_on;
11274 	turn_on ^= turn_off;
11275 
11276 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
11277 		phyint_flags_modified = B_TRUE;
11278 
11279 	/*
11280 	 * Now we change the flags. Track current value of
11281 	 * other flags in their respective places.
11282 	 */
11283 	mutex_enter(&ill->ill_lock);
11284 	mutex_enter(&phyi->phyint_lock);
11285 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11286 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11287 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11288 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11289 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11290 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11291 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
11292 		set_linklocal = B_TRUE;
11293 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
11294 	}
11295 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
11296 		zero_source = B_TRUE;
11297 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
11298 	}
11299 	mutex_exit(&ill->ill_lock);
11300 	mutex_exit(&phyi->phyint_lock);
11301 
11302 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
11303 		ip_redo_nomination(phyi);
11304 
11305 	if (set_linklocal)
11306 		(void) ipif_setlinklocal(ipif);
11307 
11308 	if (zero_source)
11309 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11310 	else
11311 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
11312 
11313 	if (need_up) {
11314 		/*
11315 		 * XXX ipif_up really does not know whether a phyint flags
11316 		 * was modified or not. So, it sends up information on
11317 		 * only one routing sockets message. As we don't bring up
11318 		 * the interface and also set STANDBY/FAILED simultaneously
11319 		 * it should be okay.
11320 		 */
11321 		err = ipif_up(ipif, q, mp);
11322 	} else {
11323 		/*
11324 		 * Make sure routing socket sees all changes to the flags.
11325 		 * ipif_up_done* handles this when we use ipif_up.
11326 		 */
11327 		if (phyint_flags_modified) {
11328 			if (phyi->phyint_illv4 != NULL) {
11329 				ip_rts_ifmsg(phyi->phyint_illv4->
11330 				    ill_ipif);
11331 			}
11332 			if (phyi->phyint_illv6 != NULL) {
11333 				ip_rts_ifmsg(phyi->phyint_illv6->
11334 				    ill_ipif);
11335 			}
11336 		} else {
11337 			ip_rts_ifmsg(ipif);
11338 		}
11339 	}
11340 	return (err);
11341 }
11342 
11343 /*
11344  * Restart entry point to restart the flags restart operation after the
11345  * refcounts have dropped to zero.
11346  */
11347 /* ARGSUSED */
11348 int
11349 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11350     ip_ioctl_cmd_t *ipip, void *if_req)
11351 {
11352 	int	err;
11353 	struct ifreq *ifr = (struct ifreq *)if_req;
11354 	struct lifreq *lifr = (struct lifreq *)if_req;
11355 
11356 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
11357 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11358 
11359 	ipif_down_tail(ipif);
11360 	if (ipip->ipi_cmd_type == IF_CMD) {
11361 		/*
11362 		 * Since ip_sioctl_flags expects an int and ifr_flags
11363 		 * is a short we need to cast ifr_flags into an int
11364 		 * to avoid having sign extension cause bits to get
11365 		 * set that should not be.
11366 		 */
11367 		err = ip_sioctl_flags_tail(ipif,
11368 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
11369 		    q, mp, B_TRUE);
11370 	} else {
11371 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
11372 		    q, mp, B_TRUE);
11373 	}
11374 	return (err);
11375 }
11376 
11377 /* ARGSUSED */
11378 int
11379 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11380     ip_ioctl_cmd_t *ipip, void *if_req)
11381 {
11382 	/*
11383 	 * Has the flags been set correctly till now ?
11384 	 */
11385 	ill_t *ill = ipif->ipif_ill;
11386 	phyint_t *phyi = ill->ill_phyint;
11387 
11388 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
11389 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11390 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11391 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11392 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11393 
11394 	/*
11395 	 * Need a lock since some flags can be set even when there are
11396 	 * references to the ipif.
11397 	 */
11398 	mutex_enter(&ill->ill_lock);
11399 	if (ipip->ipi_cmd_type == IF_CMD) {
11400 		struct ifreq *ifr = (struct ifreq *)if_req;
11401 
11402 		/* Get interface flags (low 16 only). */
11403 		ifr->ifr_flags = ((ipif->ipif_flags |
11404 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
11405 	} else {
11406 		struct lifreq *lifr = (struct lifreq *)if_req;
11407 
11408 		/* Get interface flags. */
11409 		lifr->lifr_flags = ipif->ipif_flags |
11410 		    ill->ill_flags | phyi->phyint_flags;
11411 	}
11412 	mutex_exit(&ill->ill_lock);
11413 	return (0);
11414 }
11415 
11416 /* ARGSUSED */
11417 int
11418 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11419     ip_ioctl_cmd_t *ipip, void *if_req)
11420 {
11421 	int mtu;
11422 	int ip_min_mtu;
11423 	struct ifreq	*ifr;
11424 	struct lifreq *lifr;
11425 	ire_t	*ire;
11426 
11427 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
11428 	    ipif->ipif_id, (void *)ipif));
11429 	if (ipip->ipi_cmd_type == IF_CMD) {
11430 		ifr = (struct ifreq *)if_req;
11431 		mtu = ifr->ifr_metric;
11432 	} else {
11433 		lifr = (struct lifreq *)if_req;
11434 		mtu = lifr->lifr_mtu;
11435 	}
11436 
11437 	if (ipif->ipif_isv6)
11438 		ip_min_mtu = IPV6_MIN_MTU;
11439 	else
11440 		ip_min_mtu = IP_MIN_MTU;
11441 
11442 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
11443 		return (EINVAL);
11444 
11445 	/*
11446 	 * Change the MTU size in all relevant ire's.
11447 	 * Mtu change Vs. new ire creation - protocol below.
11448 	 * First change ipif_mtu and the ire_max_frag of the
11449 	 * interface ire. Then do an ire walk and change the
11450 	 * ire_max_frag of all affected ires. During ire_add
11451 	 * under the bucket lock, set the ire_max_frag of the
11452 	 * new ire being created from the ipif/ire from which
11453 	 * it is being derived. If an mtu change happens after
11454 	 * the ire is added, the new ire will be cleaned up.
11455 	 * Conversely if the mtu change happens before the ire
11456 	 * is added, ire_add will see the new value of the mtu.
11457 	 */
11458 	ipif->ipif_mtu = mtu;
11459 	ipif->ipif_flags |= IPIF_FIXEDMTU;
11460 
11461 	if (ipif->ipif_isv6)
11462 		ire = ipif_to_ire_v6(ipif);
11463 	else
11464 		ire = ipif_to_ire(ipif);
11465 	if (ire != NULL) {
11466 		ire->ire_max_frag = ipif->ipif_mtu;
11467 		ire_refrele(ire);
11468 	}
11469 	if (ipif->ipif_flags & IPIF_UP) {
11470 		if (ipif->ipif_isv6)
11471 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES);
11472 		else
11473 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES);
11474 	}
11475 	/* Update the MTU in SCTP's list */
11476 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11477 	return (0);
11478 }
11479 
11480 /* Get interface MTU. */
11481 /* ARGSUSED */
11482 int
11483 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11484 	ip_ioctl_cmd_t *ipip, void *if_req)
11485 {
11486 	struct ifreq	*ifr;
11487 	struct lifreq	*lifr;
11488 
11489 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
11490 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11491 	if (ipip->ipi_cmd_type == IF_CMD) {
11492 		ifr = (struct ifreq *)if_req;
11493 		ifr->ifr_metric = ipif->ipif_mtu;
11494 	} else {
11495 		lifr = (struct lifreq *)if_req;
11496 		lifr->lifr_mtu = ipif->ipif_mtu;
11497 	}
11498 	return (0);
11499 }
11500 
11501 /* Set interface broadcast address. */
11502 /* ARGSUSED2 */
11503 int
11504 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11505 	ip_ioctl_cmd_t *ipip, void *if_req)
11506 {
11507 	ipaddr_t addr;
11508 	ire_t	*ire;
11509 
11510 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
11511 	    ipif->ipif_id));
11512 
11513 	ASSERT(IAM_WRITER_IPIF(ipif));
11514 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
11515 		return (EADDRNOTAVAIL);
11516 
11517 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
11518 
11519 	if (sin->sin_family != AF_INET)
11520 		return (EAFNOSUPPORT);
11521 
11522 	addr = sin->sin_addr.s_addr;
11523 	if (ipif->ipif_flags & IPIF_UP) {
11524 		/*
11525 		 * If we are already up, make sure the new
11526 		 * broadcast address makes sense.  If it does,
11527 		 * there should be an IRE for it already.
11528 		 * Don't match on ipif, only on the ill
11529 		 * since we are sharing these now. Don't use
11530 		 * MATCH_IRE_ILL_GROUP as we are looking for
11531 		 * the broadcast ire on this ill and each ill
11532 		 * in the group has its own broadcast ire.
11533 		 */
11534 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
11535 		    ipif, ALL_ZONES, (MATCH_IRE_ILL | MATCH_IRE_TYPE));
11536 		if (ire == NULL) {
11537 			return (EINVAL);
11538 		} else {
11539 			ire_refrele(ire);
11540 		}
11541 	}
11542 	/*
11543 	 * Changing the broadcast addr for this ipif.
11544 	 * Make sure we have valid net and subnet bcast
11545 	 * ire's for other logical interfaces, if needed.
11546 	 */
11547 	if (addr != ipif->ipif_brd_addr)
11548 		ipif_check_bcast_ires(ipif);
11549 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
11550 	return (0);
11551 }
11552 
11553 /* Get interface broadcast address. */
11554 /* ARGSUSED */
11555 int
11556 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11557     ip_ioctl_cmd_t *ipip, void *if_req)
11558 {
11559 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
11560 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11561 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
11562 		return (EADDRNOTAVAIL);
11563 
11564 	/* IPIF_BROADCAST not possible with IPv6 */
11565 	ASSERT(!ipif->ipif_isv6);
11566 	*sin = sin_null;
11567 	sin->sin_family = AF_INET;
11568 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
11569 	return (0);
11570 }
11571 
11572 /*
11573  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
11574  */
11575 /* ARGSUSED */
11576 int
11577 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11578     ip_ioctl_cmd_t *ipip, void *if_req)
11579 {
11580 	int err = 0;
11581 	in6_addr_t v6mask;
11582 
11583 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
11584 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11585 
11586 	ASSERT(IAM_WRITER_IPIF(ipif));
11587 
11588 	if (ipif->ipif_isv6) {
11589 		sin6_t *sin6;
11590 
11591 		if (sin->sin_family != AF_INET6)
11592 			return (EAFNOSUPPORT);
11593 
11594 		sin6 = (sin6_t *)sin;
11595 		v6mask = sin6->sin6_addr;
11596 	} else {
11597 		ipaddr_t mask;
11598 
11599 		if (sin->sin_family != AF_INET)
11600 			return (EAFNOSUPPORT);
11601 
11602 		mask = sin->sin_addr.s_addr;
11603 		V4MASK_TO_V6(mask, v6mask);
11604 	}
11605 
11606 	/*
11607 	 * No big deal if the interface isn't already up, or the mask
11608 	 * isn't really changing, or this is pt-pt.
11609 	 */
11610 	if (!(ipif->ipif_flags & IPIF_UP) ||
11611 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
11612 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
11613 		ipif->ipif_v6net_mask = v6mask;
11614 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11615 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
11616 			    ipif->ipif_v6net_mask,
11617 			    ipif->ipif_v6subnet);
11618 		}
11619 		return (0);
11620 	}
11621 	/*
11622 	 * Make sure we have valid net and subnet broadcast ire's
11623 	 * for the old netmask, if needed by other logical interfaces.
11624 	 */
11625 	if (!ipif->ipif_isv6)
11626 		ipif_check_bcast_ires(ipif);
11627 
11628 	err = ipif_logical_down(ipif, q, mp);
11629 	if (err == EINPROGRESS)
11630 		return (err);
11631 	ipif_down_tail(ipif);
11632 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
11633 	return (err);
11634 }
11635 
11636 static int
11637 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
11638 {
11639 	in6_addr_t v6mask;
11640 	int err = 0;
11641 
11642 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
11643 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11644 
11645 	if (ipif->ipif_isv6) {
11646 		sin6_t *sin6;
11647 
11648 		sin6 = (sin6_t *)sin;
11649 		v6mask = sin6->sin6_addr;
11650 	} else {
11651 		ipaddr_t mask;
11652 
11653 		mask = sin->sin_addr.s_addr;
11654 		V4MASK_TO_V6(mask, v6mask);
11655 	}
11656 
11657 	ipif->ipif_v6net_mask = v6mask;
11658 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11659 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
11660 		    ipif->ipif_v6subnet);
11661 	}
11662 	err = ipif_up(ipif, q, mp);
11663 
11664 	if (err == 0 || err == EINPROGRESS) {
11665 		/*
11666 		 * The interface must be DL_BOUND if this packet has to
11667 		 * go out on the wire. Since we only go through a logical
11668 		 * down and are bound with the driver during an internal
11669 		 * down/up that is satisfied.
11670 		 */
11671 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
11672 			/* Potentially broadcast an address mask reply. */
11673 			ipif_mask_reply(ipif);
11674 		}
11675 	}
11676 	return (err);
11677 }
11678 
11679 /* ARGSUSED */
11680 int
11681 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11682     ip_ioctl_cmd_t *ipip, void *if_req)
11683 {
11684 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
11685 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11686 	ipif_down_tail(ipif);
11687 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
11688 }
11689 
11690 /* Get interface net mask. */
11691 /* ARGSUSED */
11692 int
11693 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11694     ip_ioctl_cmd_t *ipip, void *if_req)
11695 {
11696 	struct lifreq *lifr = (struct lifreq *)if_req;
11697 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
11698 
11699 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
11700 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11701 
11702 	/*
11703 	 * net mask can't change since we have a reference to the ipif.
11704 	 */
11705 	if (ipif->ipif_isv6) {
11706 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11707 		*sin6 = sin6_null;
11708 		sin6->sin6_family = AF_INET6;
11709 		sin6->sin6_addr = ipif->ipif_v6net_mask;
11710 		lifr->lifr_addrlen =
11711 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11712 	} else {
11713 		*sin = sin_null;
11714 		sin->sin_family = AF_INET;
11715 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
11716 		if (ipip->ipi_cmd_type == LIF_CMD) {
11717 			lifr->lifr_addrlen =
11718 			    ip_mask_to_plen(ipif->ipif_net_mask);
11719 		}
11720 	}
11721 	return (0);
11722 }
11723 
11724 /* ARGSUSED */
11725 int
11726 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11727     ip_ioctl_cmd_t *ipip, void *if_req)
11728 {
11729 
11730 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
11731 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11732 	/*
11733 	 * Set interface metric.  We don't use this for
11734 	 * anything but we keep track of it in case it is
11735 	 * important to routing applications or such.
11736 	 */
11737 	if (ipip->ipi_cmd_type == IF_CMD) {
11738 		struct ifreq    *ifr;
11739 
11740 		ifr = (struct ifreq *)if_req;
11741 		ipif->ipif_metric = ifr->ifr_metric;
11742 	} else {
11743 		struct lifreq   *lifr;
11744 
11745 		lifr = (struct lifreq *)if_req;
11746 		ipif->ipif_metric = lifr->lifr_metric;
11747 	}
11748 	return (0);
11749 }
11750 
11751 
11752 /* ARGSUSED */
11753 int
11754 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11755     ip_ioctl_cmd_t *ipip, void *if_req)
11756 {
11757 
11758 	/* Get interface metric. */
11759 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
11760 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11761 	if (ipip->ipi_cmd_type == IF_CMD) {
11762 		struct ifreq    *ifr;
11763 
11764 		ifr = (struct ifreq *)if_req;
11765 		ifr->ifr_metric = ipif->ipif_metric;
11766 	} else {
11767 		struct lifreq   *lifr;
11768 
11769 		lifr = (struct lifreq *)if_req;
11770 		lifr->lifr_metric = ipif->ipif_metric;
11771 	}
11772 
11773 	return (0);
11774 }
11775 
11776 /* ARGSUSED */
11777 int
11778 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11779     ip_ioctl_cmd_t *ipip, void *if_req)
11780 {
11781 
11782 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
11783 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11784 	/*
11785 	 * Set the muxid returned from I_PLINK.
11786 	 */
11787 	if (ipip->ipi_cmd_type == IF_CMD) {
11788 		struct ifreq *ifr = (struct ifreq *)if_req;
11789 
11790 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
11791 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
11792 	} else {
11793 		struct lifreq *lifr = (struct lifreq *)if_req;
11794 
11795 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
11796 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
11797 	}
11798 	return (0);
11799 }
11800 
11801 /* ARGSUSED */
11802 int
11803 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11804     ip_ioctl_cmd_t *ipip, void *if_req)
11805 {
11806 
11807 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
11808 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11809 	/*
11810 	 * Get the muxid saved in ill for I_PUNLINK.
11811 	 */
11812 	if (ipip->ipi_cmd_type == IF_CMD) {
11813 		struct ifreq *ifr = (struct ifreq *)if_req;
11814 
11815 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
11816 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
11817 	} else {
11818 		struct lifreq *lifr = (struct lifreq *)if_req;
11819 
11820 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
11821 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
11822 	}
11823 	return (0);
11824 }
11825 
11826 /*
11827  * Set the subnet prefix. Does not modify the broadcast address.
11828  */
11829 /* ARGSUSED */
11830 int
11831 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11832     ip_ioctl_cmd_t *ipip, void *if_req)
11833 {
11834 	int err = 0;
11835 	in6_addr_t v6addr;
11836 	in6_addr_t v6mask;
11837 	boolean_t need_up = B_FALSE;
11838 	int addrlen;
11839 
11840 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
11841 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11842 
11843 	ASSERT(IAM_WRITER_IPIF(ipif));
11844 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
11845 
11846 	if (ipif->ipif_isv6) {
11847 		sin6_t *sin6;
11848 
11849 		if (sin->sin_family != AF_INET6)
11850 			return (EAFNOSUPPORT);
11851 
11852 		sin6 = (sin6_t *)sin;
11853 		v6addr = sin6->sin6_addr;
11854 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
11855 			return (EADDRNOTAVAIL);
11856 	} else {
11857 		ipaddr_t addr;
11858 
11859 		if (sin->sin_family != AF_INET)
11860 			return (EAFNOSUPPORT);
11861 
11862 		addr = sin->sin_addr.s_addr;
11863 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
11864 			return (EADDRNOTAVAIL);
11865 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11866 		/* Add 96 bits */
11867 		addrlen += IPV6_ABITS - IP_ABITS;
11868 	}
11869 
11870 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
11871 		return (EINVAL);
11872 
11873 	/* Check if bits in the address is set past the mask */
11874 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
11875 		return (EINVAL);
11876 
11877 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
11878 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
11879 		return (0);	/* No change */
11880 
11881 	if (ipif->ipif_flags & IPIF_UP) {
11882 		/*
11883 		 * If the interface is already marked up,
11884 		 * we call ipif_down which will take care
11885 		 * of ditching any IREs that have been set
11886 		 * up based on the old interface address.
11887 		 */
11888 		err = ipif_logical_down(ipif, q, mp);
11889 		if (err == EINPROGRESS)
11890 			return (err);
11891 		ipif_down_tail(ipif);
11892 		need_up = B_TRUE;
11893 	}
11894 
11895 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
11896 	return (err);
11897 }
11898 
11899 static int
11900 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
11901     queue_t *q, mblk_t *mp, boolean_t need_up)
11902 {
11903 	ill_t	*ill = ipif->ipif_ill;
11904 	int	err = 0;
11905 
11906 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
11907 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11908 
11909 	/* Set the new address. */
11910 	mutex_enter(&ill->ill_lock);
11911 	ipif->ipif_v6net_mask = v6mask;
11912 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11913 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
11914 		    ipif->ipif_v6subnet);
11915 	}
11916 	mutex_exit(&ill->ill_lock);
11917 
11918 	if (need_up) {
11919 		/*
11920 		 * Now bring the interface back up.  If this
11921 		 * is the only IPIF for the ILL, ipif_up
11922 		 * will have to re-bind to the device, so
11923 		 * we may get back EINPROGRESS, in which
11924 		 * case, this IOCTL will get completed in
11925 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11926 		 */
11927 		err = ipif_up(ipif, q, mp);
11928 		if (err == EINPROGRESS)
11929 			return (err);
11930 	}
11931 	return (err);
11932 }
11933 
11934 /* ARGSUSED */
11935 int
11936 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11937     ip_ioctl_cmd_t *ipip, void *if_req)
11938 {
11939 	int	addrlen;
11940 	in6_addr_t v6addr;
11941 	in6_addr_t v6mask;
11942 	struct lifreq *lifr = (struct lifreq *)if_req;
11943 
11944 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
11945 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11946 	ipif_down_tail(ipif);
11947 
11948 	addrlen = lifr->lifr_addrlen;
11949 	if (ipif->ipif_isv6) {
11950 		sin6_t *sin6;
11951 
11952 		sin6 = (sin6_t *)sin;
11953 		v6addr = sin6->sin6_addr;
11954 	} else {
11955 		ipaddr_t addr;
11956 
11957 		addr = sin->sin_addr.s_addr;
11958 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11959 		addrlen += IPV6_ABITS - IP_ABITS;
11960 	}
11961 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
11962 
11963 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
11964 }
11965 
11966 /* ARGSUSED */
11967 int
11968 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11969     ip_ioctl_cmd_t *ipip, void *if_req)
11970 {
11971 	struct lifreq *lifr = (struct lifreq *)if_req;
11972 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
11973 
11974 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
11975 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11976 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11977 
11978 	if (ipif->ipif_isv6) {
11979 		*sin6 = sin6_null;
11980 		sin6->sin6_family = AF_INET6;
11981 		sin6->sin6_addr = ipif->ipif_v6subnet;
11982 		lifr->lifr_addrlen =
11983 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11984 	} else {
11985 		*sin = sin_null;
11986 		sin->sin_family = AF_INET;
11987 		sin->sin_addr.s_addr = ipif->ipif_subnet;
11988 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
11989 	}
11990 	return (0);
11991 }
11992 
11993 /*
11994  * Set the IPv6 address token.
11995  */
11996 /* ARGSUSED */
11997 int
11998 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11999     ip_ioctl_cmd_t *ipi, void *if_req)
12000 {
12001 	ill_t *ill = ipif->ipif_ill;
12002 	int err;
12003 	in6_addr_t v6addr;
12004 	in6_addr_t v6mask;
12005 	boolean_t need_up = B_FALSE;
12006 	int i;
12007 	sin6_t *sin6 = (sin6_t *)sin;
12008 	struct lifreq *lifr = (struct lifreq *)if_req;
12009 	int addrlen;
12010 
12011 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12012 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12013 	ASSERT(IAM_WRITER_IPIF(ipif));
12014 
12015 	addrlen = lifr->lifr_addrlen;
12016 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12017 	if (ipif->ipif_id != 0)
12018 		return (EINVAL);
12019 
12020 	if (!ipif->ipif_isv6)
12021 		return (EINVAL);
12022 
12023 	if (addrlen > IPV6_ABITS)
12024 		return (EINVAL);
12025 
12026 	v6addr = sin6->sin6_addr;
12027 
12028 	/*
12029 	 * The length of the token is the length from the end.  To get
12030 	 * the proper mask for this, compute the mask of the bits not
12031 	 * in the token; ie. the prefix, and then xor to get the mask.
12032 	 */
12033 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12034 		return (EINVAL);
12035 	for (i = 0; i < 4; i++) {
12036 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12037 	}
12038 
12039 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12040 	    ill->ill_token_length == addrlen)
12041 		return (0);	/* No change */
12042 
12043 	if (ipif->ipif_flags & IPIF_UP) {
12044 		err = ipif_logical_down(ipif, q, mp);
12045 		if (err == EINPROGRESS)
12046 			return (err);
12047 		ipif_down_tail(ipif);
12048 		need_up = B_TRUE;
12049 	}
12050 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12051 	return (err);
12052 }
12053 
12054 static int
12055 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12056     mblk_t *mp, boolean_t need_up)
12057 {
12058 	in6_addr_t v6addr;
12059 	in6_addr_t v6mask;
12060 	ill_t	*ill = ipif->ipif_ill;
12061 	int	i;
12062 	int	err = 0;
12063 
12064 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12065 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12066 	v6addr = sin6->sin6_addr;
12067 	/*
12068 	 * The length of the token is the length from the end.  To get
12069 	 * the proper mask for this, compute the mask of the bits not
12070 	 * in the token; ie. the prefix, and then xor to get the mask.
12071 	 */
12072 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12073 	for (i = 0; i < 4; i++)
12074 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12075 
12076 	mutex_enter(&ill->ill_lock);
12077 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12078 	ill->ill_token_length = addrlen;
12079 	mutex_exit(&ill->ill_lock);
12080 
12081 	if (need_up) {
12082 		/*
12083 		 * Now bring the interface back up.  If this
12084 		 * is the only IPIF for the ILL, ipif_up
12085 		 * will have to re-bind to the device, so
12086 		 * we may get back EINPROGRESS, in which
12087 		 * case, this IOCTL will get completed in
12088 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12089 		 */
12090 		err = ipif_up(ipif, q, mp);
12091 		if (err == EINPROGRESS)
12092 			return (err);
12093 	}
12094 	return (err);
12095 }
12096 
12097 /* ARGSUSED */
12098 int
12099 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12100     ip_ioctl_cmd_t *ipi, void *if_req)
12101 {
12102 	ill_t *ill;
12103 	sin6_t *sin6 = (sin6_t *)sin;
12104 	struct lifreq *lifr = (struct lifreq *)if_req;
12105 
12106 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12107 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12108 	if (ipif->ipif_id != 0)
12109 		return (EINVAL);
12110 
12111 	ill = ipif->ipif_ill;
12112 	if (!ill->ill_isv6)
12113 		return (ENXIO);
12114 
12115 	*sin6 = sin6_null;
12116 	sin6->sin6_family = AF_INET6;
12117 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12118 	sin6->sin6_addr = ill->ill_token;
12119 	lifr->lifr_addrlen = ill->ill_token_length;
12120 	return (0);
12121 }
12122 
12123 /*
12124  * Set (hardware) link specific information that might override
12125  * what was acquired through the DL_INFO_ACK.
12126  * The logic is as follows.
12127  *
12128  * become exclusive
12129  * set CHANGING flag
12130  * change mtu on affected IREs
12131  * clear CHANGING flag
12132  *
12133  * An ire add that occurs before the CHANGING flag is set will have its mtu
12134  * changed by the ip_sioctl_lnkinfo.
12135  *
12136  * During the time the CHANGING flag is set, no new ires will be added to the
12137  * bucket, and ire add will fail (due the CHANGING flag).
12138  *
12139  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12140  * before it is added to the bucket.
12141  *
12142  * Obviously only 1 thread can set the CHANGING flag and we need to become
12143  * exclusive to set the flag.
12144  */
12145 /* ARGSUSED */
12146 int
12147 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12148     ip_ioctl_cmd_t *ipi, void *if_req)
12149 {
12150 	ill_t		*ill = ipif->ipif_ill;
12151 	ipif_t		*nipif;
12152 	int		ip_min_mtu;
12153 	boolean_t	mtu_walk = B_FALSE;
12154 	struct lifreq	*lifr = (struct lifreq *)if_req;
12155 	lif_ifinfo_req_t *lir;
12156 	ire_t		*ire;
12157 
12158 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12159 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12160 	lir = &lifr->lifr_ifinfo;
12161 	ASSERT(IAM_WRITER_IPIF(ipif));
12162 
12163 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12164 	if (ipif->ipif_id != 0)
12165 		return (EINVAL);
12166 
12167 	/* Set interface MTU. */
12168 	if (ipif->ipif_isv6)
12169 		ip_min_mtu = IPV6_MIN_MTU;
12170 	else
12171 		ip_min_mtu = IP_MIN_MTU;
12172 
12173 	/*
12174 	 * Verify values before we set anything. Allow zero to
12175 	 * mean unspecified.
12176 	 */
12177 	if (lir->lir_maxmtu != 0 &&
12178 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12179 	    lir->lir_maxmtu < ip_min_mtu))
12180 		return (EINVAL);
12181 	if (lir->lir_reachtime != 0 &&
12182 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12183 		return (EINVAL);
12184 	if (lir->lir_reachretrans != 0 &&
12185 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12186 		return (EINVAL);
12187 
12188 	mutex_enter(&ill->ill_lock);
12189 	ill->ill_state_flags |= ILL_CHANGING;
12190 	for (nipif = ill->ill_ipif; nipif != NULL;
12191 	    nipif = nipif->ipif_next) {
12192 		nipif->ipif_state_flags |= IPIF_CHANGING;
12193 	}
12194 
12195 	mutex_exit(&ill->ill_lock);
12196 
12197 	if (lir->lir_maxmtu != 0) {
12198 		ill->ill_max_mtu = lir->lir_maxmtu;
12199 		ill->ill_mtu_userspecified = 1;
12200 		mtu_walk = B_TRUE;
12201 	}
12202 
12203 	if (lir->lir_reachtime != 0)
12204 		ill->ill_reachable_time = lir->lir_reachtime;
12205 
12206 	if (lir->lir_reachretrans != 0)
12207 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12208 
12209 	ill->ill_max_hops = lir->lir_maxhops;
12210 
12211 	ill->ill_max_buf = ND_MAX_Q;
12212 
12213 	if (mtu_walk) {
12214 		/*
12215 		 * Set the MTU on all ipifs associated with this ill except
12216 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12217 		 */
12218 		for (nipif = ill->ill_ipif; nipif != NULL;
12219 		    nipif = nipif->ipif_next) {
12220 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12221 				continue;
12222 
12223 			nipif->ipif_mtu = ill->ill_max_mtu;
12224 
12225 			if (!(nipif->ipif_flags & IPIF_UP))
12226 				continue;
12227 
12228 			if (nipif->ipif_isv6)
12229 				ire = ipif_to_ire_v6(nipif);
12230 			else
12231 				ire = ipif_to_ire(nipif);
12232 			if (ire != NULL) {
12233 				ire->ire_max_frag = ipif->ipif_mtu;
12234 				ire_refrele(ire);
12235 			}
12236 			if (ill->ill_isv6) {
12237 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
12238 				    ipif_mtu_change, (char *)nipif,
12239 				    ill);
12240 			} else {
12241 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
12242 				    ipif_mtu_change, (char *)nipif,
12243 				    ill);
12244 			}
12245 		}
12246 	}
12247 
12248 	mutex_enter(&ill->ill_lock);
12249 	for (nipif = ill->ill_ipif; nipif != NULL;
12250 	    nipif = nipif->ipif_next) {
12251 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12252 	}
12253 	ILL_UNMARK_CHANGING(ill);
12254 	mutex_exit(&ill->ill_lock);
12255 
12256 	return (0);
12257 }
12258 
12259 /* ARGSUSED */
12260 int
12261 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12262     ip_ioctl_cmd_t *ipi, void *if_req)
12263 {
12264 	struct lif_ifinfo_req *lir;
12265 	ill_t *ill = ipif->ipif_ill;
12266 
12267 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
12268 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12269 	if (ipif->ipif_id != 0)
12270 		return (EINVAL);
12271 
12272 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
12273 	lir->lir_maxhops = ill->ill_max_hops;
12274 	lir->lir_reachtime = ill->ill_reachable_time;
12275 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
12276 	lir->lir_maxmtu = ill->ill_max_mtu;
12277 
12278 	return (0);
12279 }
12280 
12281 /*
12282  * Return best guess as to the subnet mask for the specified address.
12283  * Based on the subnet masks for all the configured interfaces.
12284  *
12285  * We end up returning a zero mask in the case of default, multicast or
12286  * experimental.
12287  */
12288 static ipaddr_t
12289 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp)
12290 {
12291 	ipaddr_t net_mask;
12292 	ill_t	*ill;
12293 	ipif_t	*ipif;
12294 	ill_walk_context_t ctx;
12295 	ipif_t	*fallback_ipif = NULL;
12296 
12297 	net_mask = ip_net_mask(addr);
12298 	if (net_mask == 0) {
12299 		*ipifp = NULL;
12300 		return (0);
12301 	}
12302 
12303 	/* Let's check to see if this is maybe a local subnet route. */
12304 	/* this function only applies to IPv4 interfaces */
12305 	rw_enter(&ill_g_lock, RW_READER);
12306 	ill = ILL_START_WALK_V4(&ctx);
12307 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
12308 		mutex_enter(&ill->ill_lock);
12309 		for (ipif = ill->ill_ipif; ipif != NULL;
12310 		    ipif = ipif->ipif_next) {
12311 			if (!IPIF_CAN_LOOKUP(ipif))
12312 				continue;
12313 			if (!(ipif->ipif_flags & IPIF_UP))
12314 				continue;
12315 			if ((ipif->ipif_subnet & net_mask) ==
12316 			    (addr & net_mask)) {
12317 				/*
12318 				 * Don't trust pt-pt interfaces if there are
12319 				 * other interfaces.
12320 				 */
12321 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
12322 					if (fallback_ipif == NULL) {
12323 						ipif_refhold_locked(ipif);
12324 						fallback_ipif = ipif;
12325 					}
12326 					continue;
12327 				}
12328 
12329 				/*
12330 				 * Fine. Just assume the same net mask as the
12331 				 * directly attached subnet interface is using.
12332 				 */
12333 				ipif_refhold_locked(ipif);
12334 				mutex_exit(&ill->ill_lock);
12335 				rw_exit(&ill_g_lock);
12336 				if (fallback_ipif != NULL)
12337 					ipif_refrele(fallback_ipif);
12338 				*ipifp = ipif;
12339 				return (ipif->ipif_net_mask);
12340 			}
12341 		}
12342 		mutex_exit(&ill->ill_lock);
12343 	}
12344 	rw_exit(&ill_g_lock);
12345 
12346 	*ipifp = fallback_ipif;
12347 	return ((fallback_ipif != NULL) ?
12348 	    fallback_ipif->ipif_net_mask : net_mask);
12349 }
12350 
12351 /*
12352  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
12353  */
12354 static void
12355 ip_wput_ioctl(queue_t *q, mblk_t *mp)
12356 {
12357 	IOCP	iocp;
12358 	ipft_t	*ipft;
12359 	ipllc_t	*ipllc;
12360 	mblk_t	*mp1;
12361 	cred_t	*cr;
12362 	int	error = 0;
12363 	conn_t	*connp;
12364 
12365 	ip1dbg(("ip_wput_ioctl"));
12366 	iocp = (IOCP)mp->b_rptr;
12367 	mp1 = mp->b_cont;
12368 	if (mp1 == NULL) {
12369 		iocp->ioc_error = EINVAL;
12370 		mp->b_datap->db_type = M_IOCNAK;
12371 		iocp->ioc_count = 0;
12372 		qreply(q, mp);
12373 		return;
12374 	}
12375 
12376 	/*
12377 	 * These IOCTLs provide various control capabilities to
12378 	 * upstream agents such as ULPs and processes.	There
12379 	 * are currently two such IOCTLs implemented.  They
12380 	 * are used by TCP to provide update information for
12381 	 * existing IREs and to forcibly delete an IRE for a
12382 	 * host that is not responding, thereby forcing an
12383 	 * attempt at a new route.
12384 	 */
12385 	iocp->ioc_error = EINVAL;
12386 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
12387 		goto done;
12388 
12389 	ipllc = (ipllc_t *)mp1->b_rptr;
12390 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
12391 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
12392 			break;
12393 	}
12394 	/*
12395 	 * prefer credential from mblk over ioctl;
12396 	 * see ip_sioctl_copyin_setup
12397 	 */
12398 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
12399 
12400 	/*
12401 	 * Refhold the conn in case the request gets queued up in some lookup
12402 	 */
12403 	ASSERT(CONN_Q(q));
12404 	connp = Q_TO_CONN(q);
12405 	CONN_INC_REF(connp);
12406 	if (ipft->ipft_pfi &&
12407 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
12408 		pullupmsg(mp1, ipft->ipft_min_size))) {
12409 		error = (*ipft->ipft_pfi)(q,
12410 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
12411 	}
12412 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
12413 		/*
12414 		 * CONN_OPER_PENDING_DONE happens in the function called
12415 		 * through ipft_pfi above.
12416 		 */
12417 		return;
12418 	}
12419 
12420 	CONN_OPER_PENDING_DONE(connp);
12421 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
12422 		freemsg(mp);
12423 		return;
12424 	}
12425 	iocp->ioc_error = error;
12426 
12427 done:
12428 	mp->b_datap->db_type = M_IOCACK;
12429 	if (iocp->ioc_error)
12430 		iocp->ioc_count = 0;
12431 	qreply(q, mp);
12432 }
12433 
12434 /*
12435  * Lookup an ipif using the sequence id (ipif_seqid)
12436  */
12437 ipif_t *
12438 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
12439 {
12440 	ipif_t *ipif;
12441 
12442 	ASSERT(MUTEX_HELD(&ill->ill_lock));
12443 
12444 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12445 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
12446 			return (ipif);
12447 	}
12448 	return (NULL);
12449 }
12450 
12451 uint64_t ipif_g_seqid;
12452 
12453 /*
12454  * Assign a unique id for the ipif. This is used later when we send
12455  * IRES to ARP for resolution where we initialize ire_ipif_seqid
12456  * to the value pointed by ire_ipif->ipif_seqid. Later when the
12457  * IRE is added, we verify that ipif has not disappeared.
12458  */
12459 
12460 static void
12461 ipif_assign_seqid(ipif_t *ipif)
12462 {
12463 	ipif->ipif_seqid = atomic_add_64_nv(&ipif_g_seqid, 1);
12464 }
12465 
12466 /*
12467  * Insert the ipif, so that the list of ipifs on the ill will be sorted
12468  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
12469  * be inserted into the first space available in the list. The value of
12470  * ipif_id will then be set to the appropriate value for its position.
12471  */
12472 static int
12473 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
12474 {
12475 	ill_t *ill;
12476 	ipif_t *tipif;
12477 	ipif_t **tipifp;
12478 	int id;
12479 
12480 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
12481 	    IAM_WRITER_IPIF(ipif));
12482 
12483 	ill = ipif->ipif_ill;
12484 	ASSERT(ill != NULL);
12485 
12486 	/*
12487 	 * In the case of lo0:0 we already hold the ill_g_lock.
12488 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
12489 	 * ipif_insert. Another such caller is ipif_move.
12490 	 */
12491 	if (acquire_g_lock)
12492 		rw_enter(&ill_g_lock, RW_WRITER);
12493 	if (acquire_ill_lock)
12494 		mutex_enter(&ill->ill_lock);
12495 	id = ipif->ipif_id;
12496 	tipifp = &(ill->ill_ipif);
12497 	if (id == -1) {	/* need to find a real id */
12498 		id = 0;
12499 		while ((tipif = *tipifp) != NULL) {
12500 			ASSERT(tipif->ipif_id >= id);
12501 			if (tipif->ipif_id != id)
12502 				break; /* non-consecutive id */
12503 			id++;
12504 			tipifp = &(tipif->ipif_next);
12505 		}
12506 		/* limit number of logical interfaces */
12507 		if (id >= ip_addrs_per_if) {
12508 			if (acquire_ill_lock)
12509 				mutex_exit(&ill->ill_lock);
12510 			if (acquire_g_lock)
12511 				rw_exit(&ill_g_lock);
12512 			return (-1);
12513 		}
12514 		ipif->ipif_id = id; /* assign new id */
12515 	} else if (id < ip_addrs_per_if) {
12516 		/* we have a real id; insert ipif in the right place */
12517 		while ((tipif = *tipifp) != NULL) {
12518 			ASSERT(tipif->ipif_id != id);
12519 			if (tipif->ipif_id > id)
12520 				break; /* found correct location */
12521 			tipifp = &(tipif->ipif_next);
12522 		}
12523 	} else {
12524 		if (acquire_ill_lock)
12525 			mutex_exit(&ill->ill_lock);
12526 		if (acquire_g_lock)
12527 			rw_exit(&ill_g_lock);
12528 		return (-1);
12529 	}
12530 
12531 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
12532 
12533 	ipif->ipif_next = tipif;
12534 	*tipifp = ipif;
12535 	if (acquire_ill_lock)
12536 		mutex_exit(&ill->ill_lock);
12537 	if (acquire_g_lock)
12538 		rw_exit(&ill_g_lock);
12539 	return (0);
12540 }
12541 
12542 /*
12543  * Allocate and initialize a new interface control structure.  (Always
12544  * called as writer.)
12545  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
12546  * is not part of the global linked list of ills. ipif_seqid is unique
12547  * in the system and to preserve the uniqueness, it is assigned only
12548  * when ill becomes part of the global list. At that point ill will
12549  * have a name. If it doesn't get assigned here, it will get assigned
12550  * in ipif_set_values() as part of SIOCSLIFNAME processing.
12551  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
12552  * the interface flags or any other information from the DL_INFO_ACK for
12553  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
12554  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
12555  * second DL_INFO_ACK comes in from the driver.
12556  */
12557 static ipif_t *
12558 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
12559 {
12560 	ipif_t	*ipif;
12561 	phyint_t *phyi;
12562 
12563 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
12564 	    ill->ill_name, id, (void *)ill));
12565 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
12566 
12567 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
12568 		return (NULL);
12569 	*ipif = ipif_zero;	/* start clean */
12570 
12571 	ipif->ipif_ill = ill;
12572 	ipif->ipif_id = id;	/* could be -1 */
12573 	ipif->ipif_zoneid = GLOBAL_ZONEID;
12574 
12575 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
12576 
12577 	ipif->ipif_refcnt = 0;
12578 	ipif->ipif_saved_ire_cnt = 0;
12579 
12580 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
12581 		mi_free(ipif);
12582 		return (NULL);
12583 	}
12584 	/* -1 id should have been replaced by real id */
12585 	id = ipif->ipif_id;
12586 	ASSERT(id >= 0);
12587 
12588 	if (ill->ill_name[0] != '\0') {
12589 		ipif_assign_seqid(ipif);
12590 		if (ill->ill_phyint->phyint_ifindex != 0)
12591 			sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
12592 	}
12593 	/*
12594 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
12595 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
12596 	 * ioctl sets ipif_orig_ipifid to zero.
12597 	 */
12598 	ipif->ipif_orig_ipifid = id;
12599 
12600 	/*
12601 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
12602 	 * The ipif is still not up and can't be looked up until the
12603 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
12604 	 */
12605 	mutex_enter(&ill->ill_lock);
12606 	mutex_enter(&ill->ill_phyint->phyint_lock);
12607 	/*
12608 	 * Set the running flag when logical interface zero is created.
12609 	 * For subsequent logical interfaces, a DLPI link down
12610 	 * notification message may have cleared the running flag to
12611 	 * indicate the link is down, so we shouldn't just blindly set it.
12612 	 */
12613 	if (id == 0)
12614 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
12615 	ipif->ipif_ire_type = ire_type;
12616 	phyi = ill->ill_phyint;
12617 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
12618 
12619 	if (ipif->ipif_isv6) {
12620 		ill->ill_flags |= ILLF_IPV6;
12621 	} else {
12622 		ipaddr_t inaddr_any = INADDR_ANY;
12623 
12624 		ill->ill_flags |= ILLF_IPV4;
12625 
12626 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
12627 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12628 		    &ipif->ipif_v6lcl_addr);
12629 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12630 		    &ipif->ipif_v6src_addr);
12631 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12632 		    &ipif->ipif_v6subnet);
12633 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12634 		    &ipif->ipif_v6net_mask);
12635 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12636 		    &ipif->ipif_v6brd_addr);
12637 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12638 		    &ipif->ipif_v6pp_dst_addr);
12639 	}
12640 
12641 	/*
12642 	 * Don't set the interface flags etc. now, will do it in
12643 	 * ip_ll_subnet_defaults.
12644 	 */
12645 	if (!initialize) {
12646 		mutex_exit(&ill->ill_lock);
12647 		mutex_exit(&ill->ill_phyint->phyint_lock);
12648 		return (ipif);
12649 	}
12650 	ipif->ipif_mtu = ill->ill_max_mtu;
12651 
12652 	if (ill->ill_bcast_addr_length != 0) {
12653 		/*
12654 		 * Later detect lack of DLPI driver multicast
12655 		 * capability by catching DL_ENABMULTI errors in
12656 		 * ip_rput_dlpi.
12657 		 */
12658 		ill->ill_flags |= ILLF_MULTICAST;
12659 		if (!ipif->ipif_isv6)
12660 			ipif->ipif_flags |= IPIF_BROADCAST;
12661 	} else {
12662 		if (ill->ill_net_type != IRE_LOOPBACK) {
12663 			if (ipif->ipif_isv6)
12664 				/*
12665 				 * Note: xresolv interfaces will eventually need
12666 				 * NOARP set here as well, but that will require
12667 				 * those external resolvers to have some
12668 				 * knowledge of that flag and act appropriately.
12669 				 * Not to be changed at present.
12670 				 */
12671 				ill->ill_flags |= ILLF_NONUD;
12672 			else
12673 				ill->ill_flags |= ILLF_NOARP;
12674 		}
12675 		if (ill->ill_phys_addr_length == 0) {
12676 			if (ill->ill_media &&
12677 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
12678 				ipif->ipif_flags |= IPIF_NOXMIT;
12679 				phyi->phyint_flags |= PHYI_VIRTUAL;
12680 			} else {
12681 				/* pt-pt supports multicast. */
12682 				ill->ill_flags |= ILLF_MULTICAST;
12683 				if (ill->ill_net_type == IRE_LOOPBACK) {
12684 					phyi->phyint_flags |=
12685 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
12686 				} else {
12687 					ipif->ipif_flags |= IPIF_POINTOPOINT;
12688 				}
12689 			}
12690 		}
12691 	}
12692 	mutex_exit(&ill->ill_lock);
12693 	mutex_exit(&ill->ill_phyint->phyint_lock);
12694 	return (ipif);
12695 }
12696 
12697 /*
12698  * If appropriate, send a message up to the resolver delete the entry
12699  * for the address of this interface which is going out of business.
12700  * (Always called as writer).
12701  *
12702  * NOTE : We need to check for NULL mps as some of the fields are
12703  *	  initialized only for some interface types. See ipif_resolver_up()
12704  *	  for details.
12705  */
12706 void
12707 ipif_arp_down(ipif_t *ipif)
12708 {
12709 	mblk_t	*mp;
12710 
12711 	ip1dbg(("ipif_arp_down(%s:%u)\n",
12712 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12713 	ASSERT(IAM_WRITER_IPIF(ipif));
12714 
12715 	/* Delete the mapping for the local address */
12716 	mp = ipif->ipif_arp_del_mp;
12717 	if (mp != NULL) {
12718 		ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12719 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12720 		    ipif->ipif_ill->ill_name, ipif->ipif_id));
12721 		putnext(ipif->ipif_ill->ill_rq, mp);
12722 		ipif->ipif_arp_del_mp = NULL;
12723 	}
12724 
12725 	/*
12726 	 * If this is the last ipif that is going down, we need
12727 	 * to clean up ARP completely.
12728 	 */
12729 	if (ipif->ipif_ill->ill_ipif_up_count == 0) {
12730 
12731 		/* Send up AR_INTERFACE_DOWN message */
12732 		mp = ipif->ipif_ill->ill_arp_down_mp;
12733 		if (mp != NULL) {
12734 			ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12735 			    dlpi_prim_str(*(int *)mp->b_rptr),
12736 			    *(int *)mp->b_rptr, ipif->ipif_ill->ill_name,
12737 			    ipif->ipif_id));
12738 			putnext(ipif->ipif_ill->ill_rq, mp);
12739 			ipif->ipif_ill->ill_arp_down_mp = NULL;
12740 		}
12741 
12742 		/* Tell ARP to delete the multicast mappings */
12743 		mp = ipif->ipif_ill->ill_arp_del_mapping_mp;
12744 		if (mp != NULL) {
12745 			ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12746 			    dlpi_prim_str(*(int *)mp->b_rptr),
12747 			    *(int *)mp->b_rptr, ipif->ipif_ill->ill_name,
12748 			    ipif->ipif_id));
12749 			putnext(ipif->ipif_ill->ill_rq, mp);
12750 			ipif->ipif_ill->ill_arp_del_mapping_mp = NULL;
12751 		}
12752 	}
12753 }
12754 
12755 /*
12756  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
12757  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
12758  * that it wants the add_mp allocated in this function to be returned
12759  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
12760  * just re-do the multicast, it wants us to send the add_mp to ARP also.
12761  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
12762  * as it does a ipif_arp_down after calling this function - which will
12763  * remove what we add here.
12764  *
12765  * Returns -1 on failures and 0 on success.
12766  */
12767 int
12768 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
12769 {
12770 	mblk_t	*del_mp = NULL;
12771 	mblk_t *add_mp = NULL;
12772 	mblk_t *mp;
12773 	ill_t	*ill = ipif->ipif_ill;
12774 	phyint_t *phyi = ill->ill_phyint;
12775 	ipaddr_t addr, mask, extract_mask = 0;
12776 	arma_t	*arma;
12777 	uint8_t *maddr, *bphys_addr;
12778 	uint32_t hw_start;
12779 	dl_unitdata_req_t *dlur;
12780 
12781 	ASSERT(IAM_WRITER_IPIF(ipif));
12782 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
12783 		return (0);
12784 
12785 	/*
12786 	 * Delete the existing mapping from ARP. Normally ipif_down
12787 	 * -> ipif_arp_down should send this up to ARP. The only
12788 	 * reason we would find this when we are switching from
12789 	 * Multicast to Broadcast where we did not do a down.
12790 	 */
12791 	mp = ill->ill_arp_del_mapping_mp;
12792 	if (mp != NULL) {
12793 		ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12794 		    dlpi_prim_str(*(int *)mp->b_rptr),
12795 		    *(int *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
12796 		putnext(ill->ill_rq, mp);
12797 		ill->ill_arp_del_mapping_mp = NULL;
12798 	}
12799 
12800 	if (arp_add_mapping_mp != NULL)
12801 		*arp_add_mapping_mp = NULL;
12802 
12803 	/*
12804 	 * Check that the address is not to long for the constant
12805 	 * length reserved in the template arma_t.
12806 	 */
12807 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
12808 		return (-1);
12809 
12810 	/* Add mapping mblk */
12811 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
12812 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
12813 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
12814 	    (caddr_t)&addr);
12815 	if (add_mp == NULL)
12816 		return (-1);
12817 	arma = (arma_t *)add_mp->b_rptr;
12818 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
12819 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
12820 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
12821 
12822 	/*
12823 	 * Determine the broadcast address.
12824 	 */
12825 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
12826 	if (ill->ill_sap_length < 0)
12827 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
12828 	else
12829 		bphys_addr = (uchar_t *)dlur +
12830 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
12831 	/*
12832 	 * Check PHYI_MULTI_BCAST and length of physical
12833 	 * address to determine if we use the mapping or the
12834 	 * broadcast address.
12835 	 */
12836 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
12837 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
12838 		    bphys_addr, maddr, &hw_start, &extract_mask))
12839 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
12840 
12841 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
12842 	    (ill->ill_flags & ILLF_MULTICAST)) {
12843 		/* Make sure this will not match the "exact" entry. */
12844 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
12845 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
12846 		    (caddr_t)&addr);
12847 		if (del_mp == NULL) {
12848 			freemsg(add_mp);
12849 			return (-1);
12850 		}
12851 		bcopy(&extract_mask, (char *)arma +
12852 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
12853 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
12854 			/* Use link-layer broadcast address for MULTI_BCAST */
12855 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
12856 			ip2dbg(("ipif_arp_setup_multicast: adding"
12857 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
12858 		} else {
12859 			arma->arma_hw_mapping_start = hw_start;
12860 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
12861 			    " ARP setup for %s\n", ill->ill_name));
12862 		}
12863 	} else {
12864 		freemsg(add_mp);
12865 		ASSERT(del_mp == NULL);
12866 		/* It is neither MULTICAST nor MULTI_BCAST */
12867 		return (0);
12868 	}
12869 	ASSERT(add_mp != NULL && del_mp != NULL);
12870 	ill->ill_arp_del_mapping_mp = del_mp;
12871 	if (arp_add_mapping_mp != NULL) {
12872 		/* The caller just wants the mblks allocated */
12873 		*arp_add_mapping_mp = add_mp;
12874 	} else {
12875 		/* The caller wants us to send it to arp */
12876 		putnext(ill->ill_rq, add_mp);
12877 	}
12878 	return (0);
12879 }
12880 
12881 /*
12882  * Get the resolver set up for a new interface address.
12883  * (Always called as writer.)
12884  * Called both for IPv4 and IPv6 interfaces,
12885  * though it only sets up the resolver for v6
12886  * if it's an xresolv interface (one using an external resolver).
12887  * Honors ILLF_NOARP.
12888  * The boolean value arp_just_publish, if B_TRUE, indicates that
12889  * it only needs to send an AR_ENTRY_ADD message up to ARP for
12890  * IPv4 interfaces. Currently, B_TRUE is only set when this
12891  * function is called by ip_rput_dlpi_writer() to handle
12892  * asynchronous hardware address change notification.
12893  * Returns error on failure.
12894  */
12895 int
12896 ipif_resolver_up(ipif_t *ipif, boolean_t arp_just_publish)
12897 {
12898 	caddr_t	addr;
12899 	mblk_t	*arp_up_mp = NULL;
12900 	mblk_t	*arp_down_mp = NULL;
12901 	mblk_t	*arp_add_mp = NULL;
12902 	mblk_t	*arp_del_mp = NULL;
12903 	mblk_t	*arp_add_mapping_mp = NULL;
12904 	mblk_t	*arp_del_mapping_mp = NULL;
12905 	ill_t	*ill = ipif->ipif_ill;
12906 	uchar_t	*area_p = NULL;
12907 	uchar_t	*ared_p = NULL;
12908 	int	err = ENOMEM;
12909 
12910 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
12911 	    ipif->ipif_ill->ill_name, ipif->ipif_id,
12912 	    (uint_t)ipif->ipif_flags));
12913 	ASSERT(IAM_WRITER_IPIF(ipif));
12914 
12915 	if ((ill->ill_net_type != IRE_IF_RESOLVER) ||
12916 	    (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))) {
12917 		return (0);
12918 	}
12919 
12920 	if (ill->ill_isv6) {
12921 		/*
12922 		 * External resolver for IPv6
12923 		 */
12924 		ASSERT(!arp_just_publish);
12925 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
12926 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
12927 			area_p = (uchar_t *)&ip6_area_template;
12928 			ared_p = (uchar_t *)&ip6_ared_template;
12929 		}
12930 	} else {
12931 		/*
12932 		 * IPv4 arp case. If the ARP stream has already started
12933 		 * closing, fail this request for ARP bringup. Else
12934 		 * record the fact that an ARP bringup is pending.
12935 		 */
12936 		mutex_enter(&ill->ill_lock);
12937 		if (ill->ill_arp_closing) {
12938 			mutex_exit(&ill->ill_lock);
12939 			err = EINVAL;
12940 			goto failed;
12941 		} else {
12942 			if (ill->ill_ipif_up_count == 0)
12943 				ill->ill_arp_bringup_pending = 1;
12944 			mutex_exit(&ill->ill_lock);
12945 		}
12946 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
12947 			addr = (caddr_t)&ipif->ipif_lcl_addr;
12948 			area_p = (uchar_t *)&ip_area_template;
12949 			ared_p = (uchar_t *)&ip_ared_template;
12950 		}
12951 	}
12952 
12953 	/*
12954 	 * Add an entry for the local address in ARP only if it
12955 	 * is not UNNUMBERED and the address is not INADDR_ANY.
12956 	 */
12957 	if (((ipif->ipif_flags & IPIF_UNNUMBERED) == 0) && area_p != NULL) {
12958 		/* Now ask ARP to publish our address. */
12959 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
12960 		if (arp_add_mp == NULL)
12961 			goto failed;
12962 		if (arp_just_publish) {
12963 			/*
12964 			 * Copy the new hardware address and length into
12965 			 * arp_add_mp to be sent to ARP.
12966 			 */
12967 			area_t *area = (area_t *)arp_add_mp->b_rptr;
12968 			area->area_hw_addr_length =
12969 			    ill->ill_phys_addr_length;
12970 			bcopy((char *)ill->ill_phys_addr,
12971 			    ((char *)area + area->area_hw_addr_offset),
12972 			    area->area_hw_addr_length);
12973 		}
12974 
12975 		((area_t *)arp_add_mp->b_rptr)->area_flags =
12976 		    ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR;
12977 
12978 		if (arp_just_publish)
12979 			goto arp_setup_multicast;
12980 
12981 		/*
12982 		 * Allocate an ARP deletion message so we know we can tell ARP
12983 		 * when the interface goes down.
12984 		 */
12985 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
12986 		if (arp_del_mp == NULL)
12987 			goto failed;
12988 
12989 	} else {
12990 		if (arp_just_publish)
12991 			goto done;
12992 	}
12993 	/*
12994 	 * Need to bring up ARP or setup multicast mapping only
12995 	 * when the first interface is coming UP.
12996 	 */
12997 	if (ill->ill_ipif_up_count != 0)
12998 		goto done;
12999 
13000 	/*
13001 	 * Allocate an ARP down message (to be saved) and an ARP up
13002 	 * message.
13003 	 */
13004 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13005 	if (arp_down_mp == NULL)
13006 		goto failed;
13007 
13008 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13009 	if (arp_up_mp == NULL)
13010 		goto failed;
13011 
13012 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13013 		goto done;
13014 
13015 arp_setup_multicast:
13016 	/*
13017 	 * Setup the multicast mappings. This function initializes
13018 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13019 	 * IPv6.
13020 	 */
13021 	if (!ill->ill_isv6) {
13022 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13023 		if (err != 0)
13024 			goto failed;
13025 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13026 		ASSERT(arp_add_mapping_mp != NULL);
13027 	}
13028 
13029 done:;
13030 	if (arp_del_mp != NULL) {
13031 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13032 		ipif->ipif_arp_del_mp = arp_del_mp;
13033 	}
13034 	if (arp_down_mp != NULL) {
13035 		ASSERT(ill->ill_arp_down_mp == NULL);
13036 		ill->ill_arp_down_mp = arp_down_mp;
13037 	}
13038 	if (arp_del_mapping_mp != NULL) {
13039 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13040 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13041 	}
13042 	if (arp_up_mp != NULL) {
13043 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13044 			ipif->ipif_ill->ill_name, ipif->ipif_id));
13045 		putnext(ill->ill_rq, arp_up_mp);
13046 	}
13047 	if (arp_add_mp != NULL) {
13048 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13049 			ipif->ipif_ill->ill_name, ipif->ipif_id));
13050 		putnext(ill->ill_rq, arp_add_mp);
13051 	}
13052 	if (arp_add_mapping_mp != NULL) {
13053 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13054 			ipif->ipif_ill->ill_name, ipif->ipif_id));
13055 		putnext(ill->ill_rq, arp_add_mapping_mp);
13056 	}
13057 	if (arp_just_publish)
13058 		return (0);
13059 
13060 	if (ill->ill_flags & ILLF_NOARP)
13061 		err = ill_arp_off(ill);
13062 	else
13063 		err = ill_arp_on(ill);
13064 	if (err) {
13065 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13066 		freemsg(ipif->ipif_arp_del_mp);
13067 		if (arp_down_mp != NULL)
13068 			freemsg(ill->ill_arp_down_mp);
13069 		if (ill->ill_arp_del_mapping_mp != NULL)
13070 			freemsg(ill->ill_arp_del_mapping_mp);
13071 		ipif->ipif_arp_del_mp = NULL;
13072 		ill->ill_arp_down_mp = NULL;
13073 		ill->ill_arp_del_mapping_mp = NULL;
13074 		return (err);
13075 	}
13076 	return (ill->ill_ipif_up_count != 0 ? 0 : EINPROGRESS);
13077 
13078 failed:;
13079 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13080 	freemsg(arp_add_mp);
13081 	freemsg(arp_del_mp);
13082 	freemsg(arp_add_mapping_mp);
13083 	freemsg(arp_up_mp);
13084 	freemsg(arp_down_mp);
13085 	ill->ill_arp_bringup_pending = 0;
13086 	return (err);
13087 }
13088 
13089 /*
13090  * Wakeup all threads waiting to enter the ipsq, and sleeping
13091  * on any of the ills in this ipsq. The ill_lock of the ill
13092  * must be held so that waiters don't miss wakeups
13093  */
13094 static void
13095 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
13096 {
13097 	phyint_t *phyint;
13098 
13099 	phyint = ipsq->ipsq_phyint_list;
13100 	while (phyint != NULL) {
13101 		if (phyint->phyint_illv4) {
13102 			if (!caller_holds_lock)
13103 				mutex_enter(&phyint->phyint_illv4->ill_lock);
13104 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13105 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
13106 			if (!caller_holds_lock)
13107 				mutex_exit(&phyint->phyint_illv4->ill_lock);
13108 		}
13109 		if (phyint->phyint_illv6) {
13110 			if (!caller_holds_lock)
13111 				mutex_enter(&phyint->phyint_illv6->ill_lock);
13112 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13113 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
13114 			if (!caller_holds_lock)
13115 				mutex_exit(&phyint->phyint_illv6->ill_lock);
13116 		}
13117 		phyint = phyint->phyint_ipsq_next;
13118 	}
13119 }
13120 
13121 static ipsq_t *
13122 ipsq_create(char *groupname)
13123 {
13124 	ipsq_t	*ipsq;
13125 
13126 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13127 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
13128 	if (ipsq == NULL) {
13129 		return (NULL);
13130 	}
13131 
13132 	if (groupname != NULL)
13133 		(void) strcpy(ipsq->ipsq_name, groupname);
13134 	else
13135 		ipsq->ipsq_name[0] = '\0';
13136 
13137 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
13138 	ipsq->ipsq_flags |= IPSQ_GROUP;
13139 	ipsq->ipsq_next = ipsq_g_head;
13140 	ipsq_g_head = ipsq;
13141 	return (ipsq);
13142 }
13143 
13144 /*
13145  * Return an ipsq correspoding to the groupname. If 'create' is true
13146  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
13147  * uniquely with an IPMP group. However during IPMP groupname operations,
13148  * multiple IPMP groups may be associated with a single ipsq. But no
13149  * IPMP group can be associated with more than 1 ipsq at any time.
13150  * For example
13151  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
13152  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
13153  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
13154  *
13155  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
13156  * status shown below during the execution of the above command.
13157  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
13158  *
13159  * After the completion of the above groupname command we return to the stable
13160  * state shown below.
13161  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
13162  *	hme4			mpk17-85	ipsq2	mpk17-85	1
13163  *
13164  * Because of the above, we don't search based on the ipsq_name since that
13165  * would miss the correct ipsq during certain windows as shown above.
13166  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
13167  * natural state.
13168  */
13169 static ipsq_t *
13170 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq)
13171 {
13172 	ipsq_t	*ipsq;
13173 	int	group_len;
13174 	phyint_t *phyint;
13175 
13176 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
13177 
13178 	group_len = strlen(groupname);
13179 	ASSERT(group_len != 0);
13180 	group_len++;
13181 
13182 	for (ipsq = ipsq_g_head; ipsq != NULL; ipsq = ipsq->ipsq_next) {
13183 		/*
13184 		 * When an ipsq is being split, and ill_split_ipsq
13185 		 * calls this function, we exclude it from being considered.
13186 		 */
13187 		if (ipsq == exclude_ipsq)
13188 			continue;
13189 
13190 		/*
13191 		 * Compare against the ipsq_name. The groupname change happens
13192 		 * in 2 phases. The 1st phase merges the from group into
13193 		 * the to group's ipsq, by calling ill_merge_groups and restarts
13194 		 * the ioctl. The 2nd phase then locates the ipsq again thru
13195 		 * ipsq_name. At this point the phyint_groupname has not been
13196 		 * updated.
13197 		 */
13198 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
13199 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
13200 			/*
13201 			 * Verify that an ipmp groupname is exactly
13202 			 * part of 1 ipsq and is not found in any other
13203 			 * ipsq.
13204 			 */
13205 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq) ==
13206 			    NULL);
13207 			return (ipsq);
13208 		}
13209 
13210 		/*
13211 		 * Comparison against ipsq_name alone is not sufficient.
13212 		 * In the case when groups are currently being
13213 		 * merged, the ipsq could hold other IPMP groups temporarily.
13214 		 * so we walk the phyint list and compare against the
13215 		 * phyint_groupname as well.
13216 		 */
13217 		phyint = ipsq->ipsq_phyint_list;
13218 		while (phyint != NULL) {
13219 			if ((group_len == phyint->phyint_groupname_len) &&
13220 			    (bcmp(phyint->phyint_groupname, groupname,
13221 			    group_len) == 0)) {
13222 				/*
13223 				 * Verify that an ipmp groupname is exactly
13224 				 * part of 1 ipsq and is not found in any other
13225 				 * ipsq.
13226 				 */
13227 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq)
13228 					== NULL);
13229 				return (ipsq);
13230 			}
13231 			phyint = phyint->phyint_ipsq_next;
13232 		}
13233 	}
13234 	if (create)
13235 		ipsq = ipsq_create(groupname);
13236 	return (ipsq);
13237 }
13238 
13239 static void
13240 ipsq_delete(ipsq_t *ipsq)
13241 {
13242 	ipsq_t *nipsq;
13243 	ipsq_t *pipsq = NULL;
13244 
13245 	/*
13246 	 * We don't hold the ipsq lock, but we are sure no new
13247 	 * messages can land up, since the ipsq_refs is zero.
13248 	 * i.e. this ipsq is unnamed and no phyint or phyint group
13249 	 * is associated with this ipsq. (Lookups are based on ill_name
13250 	 * or phyint_group_name)
13251 	 */
13252 	ASSERT(ipsq->ipsq_refs == 0);
13253 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
13254 	ASSERT(ipsq->ipsq_pending_mp == NULL);
13255 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
13256 		/*
13257 		 * This is not the ipsq of an IPMP group.
13258 		 */
13259 		kmem_free(ipsq, sizeof (ipsq_t));
13260 		return;
13261 	}
13262 
13263 	rw_enter(&ill_g_lock, RW_WRITER);
13264 
13265 	/*
13266 	 * Locate the ipsq  before we can remove it from
13267 	 * the singly linked list of ipsq's.
13268 	 */
13269 	for (nipsq = ipsq_g_head; nipsq != NULL; nipsq = nipsq->ipsq_next) {
13270 		if (nipsq == ipsq) {
13271 			break;
13272 		}
13273 		pipsq = nipsq;
13274 	}
13275 
13276 	ASSERT(nipsq == ipsq);
13277 
13278 	/* unlink ipsq from the list */
13279 	if (pipsq != NULL)
13280 		pipsq->ipsq_next = ipsq->ipsq_next;
13281 	else
13282 		ipsq_g_head = ipsq->ipsq_next;
13283 	kmem_free(ipsq, sizeof (ipsq_t));
13284 	rw_exit(&ill_g_lock);
13285 }
13286 
13287 static void
13288 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
13289     queue_t *q)
13290 
13291 {
13292 
13293 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
13294 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
13295 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
13296 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
13297 	ASSERT(current_mp != NULL);
13298 
13299 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
13300 		NEW_OP, NULL);
13301 
13302 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
13303 	    new_ipsq->ipsq_xopq_mphead != NULL);
13304 
13305 	/*
13306 	 * move from old ipsq to the new ipsq.
13307 	 */
13308 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
13309 	if (old_ipsq->ipsq_xopq_mphead != NULL)
13310 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
13311 
13312 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
13313 }
13314 
13315 void
13316 ill_group_cleanup(ill_t *ill)
13317 {
13318 	ill_t *ill_v4;
13319 	ill_t *ill_v6;
13320 	ipif_t *ipif;
13321 
13322 	ill_v4 = ill->ill_phyint->phyint_illv4;
13323 	ill_v6 = ill->ill_phyint->phyint_illv6;
13324 
13325 	if (ill_v4 != NULL) {
13326 		mutex_enter(&ill_v4->ill_lock);
13327 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
13328 		    ipif = ipif->ipif_next) {
13329 			IPIF_UNMARK_MOVING(ipif);
13330 		}
13331 		ill_v4->ill_up_ipifs = B_FALSE;
13332 		mutex_exit(&ill_v4->ill_lock);
13333 	}
13334 
13335 	if (ill_v6 != NULL) {
13336 		mutex_enter(&ill_v6->ill_lock);
13337 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
13338 		    ipif = ipif->ipif_next) {
13339 			IPIF_UNMARK_MOVING(ipif);
13340 		}
13341 		ill_v6->ill_up_ipifs = B_FALSE;
13342 		mutex_exit(&ill_v6->ill_lock);
13343 	}
13344 }
13345 /*
13346  * This function is called when an ill has had a change in its group status
13347  * to bring up all the ipifs that were up before the change.
13348  */
13349 int
13350 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
13351 {
13352 	ipif_t *ipif;
13353 	ill_t *ill_v4;
13354 	ill_t *ill_v6;
13355 	ill_t *from_ill;
13356 	int err = 0;
13357 
13358 
13359 	ASSERT(IAM_WRITER_ILL(ill));
13360 
13361 	/*
13362 	 * Except for ipif_state_flags and ill_state_flags the other
13363 	 * fields of the ipif/ill that are modified below are protected
13364 	 * implicitly since we are a writer. We would have tried to down
13365 	 * even an ipif that was already down, in ill_down_ipifs. So we
13366 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
13367 	 */
13368 	ill_v4 = ill->ill_phyint->phyint_illv4;
13369 	ill_v6 = ill->ill_phyint->phyint_illv6;
13370 	if (ill_v4 != NULL) {
13371 		ill_v4->ill_up_ipifs = B_TRUE;
13372 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
13373 		    ipif = ipif->ipif_next) {
13374 			mutex_enter(&ill_v4->ill_lock);
13375 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
13376 			IPIF_UNMARK_MOVING(ipif);
13377 			mutex_exit(&ill_v4->ill_lock);
13378 			if (ipif->ipif_was_up) {
13379 				if (!(ipif->ipif_flags & IPIF_UP))
13380 					err = ipif_up(ipif, q, mp);
13381 				ipif->ipif_was_up = B_FALSE;
13382 				if (err != 0) {
13383 					/*
13384 					 * Can there be any other error ?
13385 					 */
13386 					ASSERT(err == EINPROGRESS);
13387 					return (err);
13388 				}
13389 			}
13390 		}
13391 		mutex_enter(&ill_v4->ill_lock);
13392 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
13393 		mutex_exit(&ill_v4->ill_lock);
13394 		ill_v4->ill_up_ipifs = B_FALSE;
13395 		if (ill_v4->ill_move_in_progress) {
13396 			ASSERT(ill_v4->ill_move_peer != NULL);
13397 			ill_v4->ill_move_in_progress = B_FALSE;
13398 			from_ill = ill_v4->ill_move_peer;
13399 			from_ill->ill_move_in_progress = B_FALSE;
13400 			from_ill->ill_move_peer = NULL;
13401 			mutex_enter(&from_ill->ill_lock);
13402 			from_ill->ill_state_flags &= ~ILL_CHANGING;
13403 			mutex_exit(&from_ill->ill_lock);
13404 			if (ill_v6 == NULL) {
13405 				if (from_ill->ill_phyint->phyint_flags &
13406 				    PHYI_STANDBY) {
13407 					phyint_inactive(from_ill->ill_phyint);
13408 				}
13409 				if (ill_v4->ill_phyint->phyint_flags &
13410 				    PHYI_STANDBY) {
13411 					phyint_inactive(ill_v4->ill_phyint);
13412 				}
13413 			}
13414 			ill_v4->ill_move_peer = NULL;
13415 		}
13416 	}
13417 
13418 	if (ill_v6 != NULL) {
13419 		ill_v6->ill_up_ipifs = B_TRUE;
13420 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
13421 		    ipif = ipif->ipif_next) {
13422 			mutex_enter(&ill_v6->ill_lock);
13423 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
13424 			IPIF_UNMARK_MOVING(ipif);
13425 			mutex_exit(&ill_v6->ill_lock);
13426 			if (ipif->ipif_was_up) {
13427 				if (!(ipif->ipif_flags & IPIF_UP))
13428 					err = ipif_up(ipif, q, mp);
13429 				ipif->ipif_was_up = B_FALSE;
13430 				if (err != 0) {
13431 					/*
13432 					 * Can there be any other error ?
13433 					 */
13434 					ASSERT(err == EINPROGRESS);
13435 					return (err);
13436 				}
13437 			}
13438 		}
13439 		mutex_enter(&ill_v6->ill_lock);
13440 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
13441 		mutex_exit(&ill_v6->ill_lock);
13442 		ill_v6->ill_up_ipifs = B_FALSE;
13443 		if (ill_v6->ill_move_in_progress) {
13444 			ASSERT(ill_v6->ill_move_peer != NULL);
13445 			ill_v6->ill_move_in_progress = B_FALSE;
13446 			from_ill = ill_v6->ill_move_peer;
13447 			from_ill->ill_move_in_progress = B_FALSE;
13448 			from_ill->ill_move_peer = NULL;
13449 			mutex_enter(&from_ill->ill_lock);
13450 			from_ill->ill_state_flags &= ~ILL_CHANGING;
13451 			mutex_exit(&from_ill->ill_lock);
13452 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
13453 				phyint_inactive(from_ill->ill_phyint);
13454 			}
13455 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
13456 				phyint_inactive(ill_v6->ill_phyint);
13457 			}
13458 			ill_v6->ill_move_peer = NULL;
13459 		}
13460 	}
13461 	return (0);
13462 }
13463 
13464 /*
13465  * bring down all the approriate ipifs.
13466  */
13467 /* ARGSUSED */
13468 static void
13469 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
13470 {
13471 	ipif_t *ipif;
13472 
13473 	ASSERT(IAM_WRITER_ILL(ill));
13474 
13475 	/*
13476 	 * Except for ipif_state_flags the other fields of the ipif/ill that
13477 	 * are modified below are protected implicitly since we are a writer
13478 	 */
13479 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13480 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
13481 			continue;
13482 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
13483 			/*
13484 			 * We go through the ipif_down logic even if the ipif
13485 			 * is already down, since routes can be added based
13486 			 * on down ipifs. Going through ipif_down once again
13487 			 * will delete any IREs created based on these routes.
13488 			 */
13489 			if (ipif->ipif_flags & IPIF_UP)
13490 				ipif->ipif_was_up = B_TRUE;
13491 			/*
13492 			 * If called with chk_nofailover true ipif is moving.
13493 			 */
13494 			mutex_enter(&ill->ill_lock);
13495 			if (chk_nofailover) {
13496 				ipif->ipif_state_flags |=
13497 					IPIF_MOVING | IPIF_CHANGING;
13498 			} else {
13499 				ipif->ipif_state_flags |= IPIF_CHANGING;
13500 			}
13501 			mutex_exit(&ill->ill_lock);
13502 			/*
13503 			 * Need to re-create net/subnet bcast ires if
13504 			 * they are dependent on ipif.
13505 			 */
13506 			if (!ipif->ipif_isv6)
13507 				ipif_check_bcast_ires(ipif);
13508 			(void) ipif_logical_down(ipif, NULL, NULL);
13509 			ipif_down_tail(ipif);
13510 			/*
13511 			 * We don't do ipif_multicast_down for IPv4 in
13512 			 * ipif_down. We need to set this so that
13513 			 * ipif_multicast_up will join the
13514 			 * ALLHOSTS_GROUP on to_ill.
13515 			 */
13516 			ipif->ipif_multicast_up = B_FALSE;
13517 		}
13518 	}
13519 }
13520 
13521 #define	IPSQ_INC_REF(ipsq)	{			\
13522 	ASSERT(RW_WRITE_HELD(&ill_g_lock));		\
13523 	(ipsq)->ipsq_refs++;				\
13524 }
13525 
13526 #define	IPSQ_DEC_REF(ipsq)	{			\
13527 	ASSERT(RW_WRITE_HELD(&ill_g_lock));		\
13528 	(ipsq)->ipsq_refs--;				\
13529 	if ((ipsq)->ipsq_refs == 0)				\
13530 		(ipsq)->ipsq_name[0] = '\0'; 		\
13531 }
13532 
13533 /*
13534  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
13535  * new_ipsq.
13536  */
13537 static void
13538 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq)
13539 {
13540 	phyint_t *phyint;
13541 	phyint_t *next_phyint;
13542 
13543 	/*
13544 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
13545 	 * writer and the ill_lock of the ill in question. Also the dest
13546 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
13547 	 */
13548 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13549 
13550 	phyint = cur_ipsq->ipsq_phyint_list;
13551 	cur_ipsq->ipsq_phyint_list = NULL;
13552 	while (phyint != NULL) {
13553 		next_phyint = phyint->phyint_ipsq_next;
13554 		IPSQ_DEC_REF(cur_ipsq);
13555 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
13556 		new_ipsq->ipsq_phyint_list = phyint;
13557 		IPSQ_INC_REF(new_ipsq);
13558 		phyint->phyint_ipsq = new_ipsq;
13559 		phyint = next_phyint;
13560 	}
13561 }
13562 
13563 #define	SPLIT_SUCCESS		0
13564 #define	SPLIT_NOT_NEEDED	1
13565 #define	SPLIT_FAILED		2
13566 
13567 int
13568 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry)
13569 {
13570 	ipsq_t *newipsq = NULL;
13571 
13572 	/*
13573 	 * Assertions denote pre-requisites for changing the ipsq of
13574 	 * a phyint
13575 	 */
13576 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13577 	/*
13578 	 * <ill-phyint> assocs can't change while ill_g_lock
13579 	 * is held as writer. See ill_phyint_reinit()
13580 	 */
13581 	ASSERT(phyint->phyint_illv4 == NULL ||
13582 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13583 	ASSERT(phyint->phyint_illv6 == NULL ||
13584 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13585 
13586 	if ((phyint->phyint_groupname_len !=
13587 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
13588 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
13589 	    phyint->phyint_groupname_len) != 0)) {
13590 		/*
13591 		 * Once we fail in creating a new ipsq due to memory shortage,
13592 		 * don't attempt to create new ipsq again, based on another
13593 		 * phyint, since we want all phyints belonging to an IPMP group
13594 		 * to be in the same ipsq even in the event of mem alloc fails.
13595 		 */
13596 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
13597 		    cur_ipsq);
13598 		if (newipsq == NULL) {
13599 			/* Memory allocation failure */
13600 			return (SPLIT_FAILED);
13601 		} else {
13602 			/* ipsq_refs protected by ill_g_lock (writer) */
13603 			IPSQ_DEC_REF(cur_ipsq);
13604 			phyint->phyint_ipsq = newipsq;
13605 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
13606 			newipsq->ipsq_phyint_list = phyint;
13607 			IPSQ_INC_REF(newipsq);
13608 			return (SPLIT_SUCCESS);
13609 		}
13610 	}
13611 	return (SPLIT_NOT_NEEDED);
13612 }
13613 
13614 /*
13615  * The ill locks of the phyint and the ill_g_lock (writer) must be held
13616  * to do this split
13617  */
13618 static int
13619 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq)
13620 {
13621 	ipsq_t *newipsq;
13622 
13623 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13624 	/*
13625 	 * <ill-phyint> assocs can't change while ill_g_lock
13626 	 * is held as writer. See ill_phyint_reinit()
13627 	 */
13628 
13629 	ASSERT(phyint->phyint_illv4 == NULL ||
13630 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13631 	ASSERT(phyint->phyint_illv6 == NULL ||
13632 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13633 
13634 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
13635 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
13636 		/*
13637 		 * ipsq_init failed due to no memory
13638 		 * caller will use the same ipsq
13639 		 */
13640 		return (SPLIT_FAILED);
13641 	}
13642 
13643 	/* ipsq_ref is protected by ill_g_lock (writer) */
13644 	IPSQ_DEC_REF(cur_ipsq);
13645 
13646 	/*
13647 	 * This is a new ipsq that is unknown to the world.
13648 	 * So we don't need to hold ipsq_lock,
13649 	 */
13650 	newipsq = phyint->phyint_ipsq;
13651 	newipsq->ipsq_writer = NULL;
13652 	newipsq->ipsq_reentry_cnt--;
13653 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
13654 #ifdef ILL_DEBUG
13655 	newipsq->ipsq_depth = 0;
13656 #endif
13657 
13658 	return (SPLIT_SUCCESS);
13659 }
13660 
13661 /*
13662  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
13663  * ipsq's representing their individual groups or themselves. Return
13664  * whether split needs to be retried again later.
13665  */
13666 static boolean_t
13667 ill_split_ipsq(ipsq_t *cur_ipsq)
13668 {
13669 	phyint_t *phyint;
13670 	phyint_t *next_phyint;
13671 	int	error;
13672 	boolean_t need_retry = B_FALSE;
13673 
13674 	phyint = cur_ipsq->ipsq_phyint_list;
13675 	cur_ipsq->ipsq_phyint_list = NULL;
13676 	while (phyint != NULL) {
13677 		next_phyint = phyint->phyint_ipsq_next;
13678 		/*
13679 		 * 'created' will tell us whether the callee actually
13680 		 * created an ipsq. Lack of memory may force the callee
13681 		 * to return without creating an ipsq.
13682 		 */
13683 		if (phyint->phyint_groupname == NULL) {
13684 			error = ill_split_to_own_ipsq(phyint, cur_ipsq);
13685 		} else {
13686 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
13687 					need_retry);
13688 		}
13689 
13690 		switch (error) {
13691 		case SPLIT_FAILED:
13692 			need_retry = B_TRUE;
13693 			/* FALLTHRU */
13694 		case SPLIT_NOT_NEEDED:
13695 			/*
13696 			 * Keep it on the list.
13697 			 */
13698 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
13699 			cur_ipsq->ipsq_phyint_list = phyint;
13700 			break;
13701 		case SPLIT_SUCCESS:
13702 			break;
13703 		default:
13704 			ASSERT(0);
13705 		}
13706 
13707 		phyint = next_phyint;
13708 	}
13709 	return (need_retry);
13710 }
13711 
13712 /*
13713  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
13714  * and return the ills in the list. This list will be
13715  * needed to unlock all the ills later on by the caller.
13716  * The <ill-ipsq> associations could change between the
13717  * lock and unlock. Hence the unlock can't traverse the
13718  * ipsq to get the list of ills.
13719  */
13720 static int
13721 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
13722 {
13723 	int	cnt = 0;
13724 	phyint_t	*phyint;
13725 
13726 	/*
13727 	 * The caller holds ill_g_lock to ensure that the ill memberships
13728 	 * of the ipsq don't change
13729 	 */
13730 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
13731 
13732 	phyint = ipsq->ipsq_phyint_list;
13733 	while (phyint != NULL) {
13734 		if (phyint->phyint_illv4 != NULL) {
13735 			ASSERT(cnt < list_max);
13736 			list[cnt++] = phyint->phyint_illv4;
13737 		}
13738 		if (phyint->phyint_illv6 != NULL) {
13739 			ASSERT(cnt < list_max);
13740 			list[cnt++] = phyint->phyint_illv6;
13741 		}
13742 		phyint = phyint->phyint_ipsq_next;
13743 	}
13744 	ill_lock_ills(list, cnt);
13745 	return (cnt);
13746 }
13747 
13748 void
13749 ill_lock_ills(ill_t **list, int cnt)
13750 {
13751 	int	i;
13752 
13753 	if (cnt > 1) {
13754 		boolean_t try_again;
13755 		do {
13756 			try_again = B_FALSE;
13757 			for (i = 0; i < cnt - 1; i++) {
13758 				if (list[i] < list[i + 1]) {
13759 					ill_t	*tmp;
13760 
13761 					/* swap the elements */
13762 					tmp = list[i];
13763 					list[i] = list[i + 1];
13764 					list[i + 1] = tmp;
13765 					try_again = B_TRUE;
13766 				}
13767 			}
13768 		} while (try_again);
13769 	}
13770 
13771 	for (i = 0; i < cnt; i++) {
13772 		if (i == 0) {
13773 			if (list[i] != NULL)
13774 				mutex_enter(&list[i]->ill_lock);
13775 			else
13776 				return;
13777 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
13778 			mutex_enter(&list[i]->ill_lock);
13779 		}
13780 	}
13781 }
13782 
13783 void
13784 ill_unlock_ills(ill_t **list, int cnt)
13785 {
13786 	int	i;
13787 
13788 	for (i = 0; i < cnt; i++) {
13789 		if ((i == 0) && (list[i] != NULL)) {
13790 			mutex_exit(&list[i]->ill_lock);
13791 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
13792 			mutex_exit(&list[i]->ill_lock);
13793 		}
13794 	}
13795 }
13796 
13797 /*
13798  * Merge all the ills from 1 ipsq group into another ipsq group.
13799  * The source ipsq group is specified by the ipsq associated with
13800  * 'from_ill'. The destination ipsq group is specified by the ipsq
13801  * associated with 'to_ill' or 'groupname' respectively.
13802  * Note that ipsq itself does not have a reference count mechanism
13803  * and functions don't look up an ipsq and pass it around. Instead
13804  * functions pass around an ill or groupname, and the ipsq is looked
13805  * up from the ill or groupname and the required operation performed
13806  * atomically with the lookup on the ipsq.
13807  */
13808 static int
13809 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
13810     queue_t *q)
13811 {
13812 	ipsq_t *old_ipsq;
13813 	ipsq_t *new_ipsq;
13814 	ill_t	**ill_list;
13815 	int	cnt;
13816 	size_t	ill_list_size;
13817 	boolean_t became_writer_on_new_sq = B_FALSE;
13818 
13819 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
13820 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
13821 
13822 	/*
13823 	 * Need to hold ill_g_lock as writer and also the ill_lock to
13824 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
13825 	 * ipsq_lock to prevent new messages from landing on an ipsq.
13826 	 */
13827 	rw_enter(&ill_g_lock, RW_WRITER);
13828 
13829 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
13830 	if (groupname != NULL)
13831 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL);
13832 	else {
13833 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
13834 	}
13835 
13836 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
13837 
13838 	/*
13839 	 * both groups are on the same ipsq.
13840 	 */
13841 	if (old_ipsq == new_ipsq) {
13842 		rw_exit(&ill_g_lock);
13843 		return (0);
13844 	}
13845 
13846 	cnt = old_ipsq->ipsq_refs << 1;
13847 	ill_list_size = cnt * sizeof (ill_t *);
13848 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
13849 	if (ill_list == NULL) {
13850 		rw_exit(&ill_g_lock);
13851 		return (ENOMEM);
13852 	}
13853 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
13854 
13855 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
13856 	mutex_enter(&new_ipsq->ipsq_lock);
13857 	if ((new_ipsq->ipsq_writer == NULL &&
13858 		new_ipsq->ipsq_current_ipif == NULL) ||
13859 	    (new_ipsq->ipsq_writer == curthread)) {
13860 		new_ipsq->ipsq_writer = curthread;
13861 		new_ipsq->ipsq_reentry_cnt++;
13862 		became_writer_on_new_sq = B_TRUE;
13863 	}
13864 
13865 	/*
13866 	 * We are holding ill_g_lock as writer and all the ill locks of
13867 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
13868 	 * message can land up on the old ipsq even though we don't hold the
13869 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
13870 	 */
13871 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
13872 
13873 	/*
13874 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
13875 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
13876 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
13877 	 */
13878 	ill_merge_ipsq(old_ipsq, new_ipsq);
13879 
13880 	/*
13881 	 * Mark the new ipsq as needing a split since it is currently
13882 	 * being shared by more than 1 IPMP group. The split will
13883 	 * occur at the end of ipsq_exit
13884 	 */
13885 	new_ipsq->ipsq_split = B_TRUE;
13886 
13887 	/* Now release all the locks */
13888 	mutex_exit(&new_ipsq->ipsq_lock);
13889 	ill_unlock_ills(ill_list, cnt);
13890 	rw_exit(&ill_g_lock);
13891 
13892 	kmem_free(ill_list, ill_list_size);
13893 
13894 	/*
13895 	 * If we succeeded in becoming writer on the new ipsq, then
13896 	 * drain the new ipsq and start processing  all enqueued messages
13897 	 * including the current ioctl we are processing which is either
13898 	 * a set groupname or failover/failback.
13899 	 */
13900 	if (became_writer_on_new_sq)
13901 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
13902 
13903 	/*
13904 	 * syncq has been changed and all the messages have been moved.
13905 	 */
13906 	mutex_enter(&old_ipsq->ipsq_lock);
13907 	old_ipsq->ipsq_current_ipif = NULL;
13908 	mutex_exit(&old_ipsq->ipsq_lock);
13909 	return (EINPROGRESS);
13910 }
13911 
13912 /*
13913  * Delete and add the loopback copy and non-loopback copy of
13914  * the BROADCAST ire corresponding to ill and addr. Used to
13915  * group broadcast ires together when ill becomes part of
13916  * a group.
13917  *
13918  * This function is also called when ill is leaving the group
13919  * so that the ires belonging to the group gets re-grouped.
13920  */
13921 static void
13922 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
13923 {
13924 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
13925 	ire_t **ire_ptpn = &ire_head;
13926 
13927 	/*
13928 	 * The loopback and non-loopback IREs are inserted in the order in which
13929 	 * they're found, on the basis that they are correctly ordered (loopback
13930 	 * first).
13931 	 */
13932 	for (;;) {
13933 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
13934 		    ALL_ZONES, MATCH_IRE_TYPE | MATCH_IRE_ILL);
13935 		if (ire == NULL)
13936 			break;
13937 
13938 		/*
13939 		 * we are passing in KM_SLEEP because it is not easy to
13940 		 * go back to a sane state in case of memory failure.
13941 		 */
13942 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
13943 		ASSERT(nire != NULL);
13944 		bzero(nire, sizeof (ire_t));
13945 		/*
13946 		 * Don't use ire_max_frag directly since we don't
13947 		 * hold on to 'ire' until we add the new ire 'nire' and
13948 		 * we don't want the new ire to have a dangling reference
13949 		 * to 'ire'. The ire_max_frag of a broadcast ire must
13950 		 * be in sync with the ipif_mtu of the associate ipif.
13951 		 * For eg. this happens as a result of SIOCSLIFNAME,
13952 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
13953 		 * the driver. A change in ire_max_frag triggered as
13954 		 * as a result of path mtu discovery, or due to an
13955 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
13956 		 * route change -mtu command does not apply to broadcast ires.
13957 		 *
13958 		 * XXX We need a recovery strategy here if ire_init fails
13959 		 */
13960 		if (ire_init(nire,
13961 		    (uchar_t *)&ire->ire_addr,
13962 		    (uchar_t *)&ire->ire_mask,
13963 		    (uchar_t *)&ire->ire_src_addr,
13964 		    (uchar_t *)&ire->ire_gateway_addr,
13965 		    (uchar_t *)&ire->ire_in_src_addr,
13966 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
13967 			&ire->ire_ipif->ipif_mtu,
13968 		    ire->ire_fp_mp,
13969 		    ire->ire_rfq,
13970 		    ire->ire_stq,
13971 		    ire->ire_type,
13972 		    ire->ire_dlureq_mp,
13973 		    ire->ire_ipif,
13974 		    ire->ire_in_ill,
13975 		    ire->ire_cmask,
13976 		    ire->ire_phandle,
13977 		    ire->ire_ihandle,
13978 		    ire->ire_flags,
13979 		    &ire->ire_uinfo) == NULL) {
13980 			cmn_err(CE_PANIC, "ire_init() failed");
13981 		}
13982 		ire_delete(ire);
13983 		ire_refrele(ire);
13984 
13985 		/*
13986 		 * The newly created IREs are inserted at the tail of the list
13987 		 * starting with ire_head. As we've just allocated them no one
13988 		 * knows about them so it's safe.
13989 		 */
13990 		*ire_ptpn = nire;
13991 		ire_ptpn = &nire->ire_next;
13992 	}
13993 
13994 	for (nire = ire_head; nire != NULL; nire = nire_next) {
13995 		int error;
13996 		ire_t *oire;
13997 		/* unlink the IRE from our list before calling ire_add() */
13998 		nire_next = nire->ire_next;
13999 		nire->ire_next = NULL;
14000 
14001 		/* ire_add adds the ire at the right place in the list */
14002 		oire = nire;
14003 		error = ire_add(&nire, NULL, NULL, NULL);
14004 		ASSERT(error == 0);
14005 		ASSERT(oire == nire);
14006 		ire_refrele(nire);	/* Held in ire_add */
14007 	}
14008 }
14009 
14010 /*
14011  * This function is usually called when an ill is inserted in
14012  * a group and all the ipifs are already UP. As all the ipifs
14013  * are already UP, the broadcast ires have already been created
14014  * and been inserted. But, ire_add_v4 would not have grouped properly.
14015  * We need to re-group for the benefit of ip_wput_ire which
14016  * expects BROADCAST ires to be grouped properly to avoid sending
14017  * more than one copy of the broadcast packet per group.
14018  *
14019  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
14020  *	  because when ipif_up_done ends up calling this, ires have
14021  *        already been added before illgrp_insert i.e before ill_group
14022  *	  has been initialized.
14023  */
14024 static void
14025 ill_group_bcast_for_xmit(ill_t *ill)
14026 {
14027 	ill_group_t *illgrp;
14028 	ipif_t *ipif;
14029 	ipaddr_t addr;
14030 	ipaddr_t net_mask;
14031 	ipaddr_t subnet_netmask;
14032 
14033 	illgrp = ill->ill_group;
14034 
14035 	/*
14036 	 * This function is called even when an ill is deleted from
14037 	 * the group. Hence, illgrp could be null.
14038 	 */
14039 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
14040 		return;
14041 
14042 	/*
14043 	 * Delete all the BROADCAST ires matching this ill and add
14044 	 * them back. This time, ire_add_v4 should take care of
14045 	 * grouping them with others because ill is part of the
14046 	 * group.
14047 	 */
14048 	ill_bcast_delete_and_add(ill, 0);
14049 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
14050 
14051 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14052 
14053 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14054 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14055 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14056 		} else {
14057 			net_mask = htonl(IN_CLASSA_NET);
14058 		}
14059 		addr = net_mask & ipif->ipif_subnet;
14060 		ill_bcast_delete_and_add(ill, addr);
14061 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
14062 
14063 		subnet_netmask = ipif->ipif_net_mask;
14064 		addr = ipif->ipif_subnet;
14065 		ill_bcast_delete_and_add(ill, addr);
14066 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
14067 	}
14068 }
14069 
14070 /*
14071  * This function is called from illgrp_delete when ill is being deleted
14072  * from the group.
14073  *
14074  * As ill is not there in the group anymore, any address belonging
14075  * to this ill should be cleared of IRE_MARK_NORECV.
14076  */
14077 static void
14078 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
14079 {
14080 	ire_t *ire;
14081 	irb_t *irb;
14082 
14083 	ASSERT(ill->ill_group == NULL);
14084 
14085 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14086 	    ALL_ZONES, MATCH_IRE_TYPE | MATCH_IRE_ILL);
14087 
14088 	if (ire != NULL) {
14089 		/*
14090 		 * IPMP and plumbing operations are serialized on the ipsq, so
14091 		 * no one will insert or delete a broadcast ire under our feet.
14092 		 */
14093 		irb = ire->ire_bucket;
14094 		rw_enter(&irb->irb_lock, RW_READER);
14095 		ire_refrele(ire);
14096 
14097 		for (; ire != NULL; ire = ire->ire_next) {
14098 			if (ire->ire_addr != addr)
14099 				break;
14100 			if (ire_to_ill(ire) != ill)
14101 				continue;
14102 
14103 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
14104 			ire->ire_marks &= ~IRE_MARK_NORECV;
14105 		}
14106 		rw_exit(&irb->irb_lock);
14107 	}
14108 }
14109 
14110 /*
14111  * This function must be called only after the broadcast ires
14112  * have been grouped together. For a given address addr, nominate
14113  * only one of the ires whose interface is not FAILED or OFFLINE.
14114  *
14115  * This is also called when an ipif goes down, so that we can nominate
14116  * a different ire with the same address for receiving.
14117  */
14118 static void
14119 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr)
14120 {
14121 	irb_t *irb;
14122 	ire_t *ire;
14123 	ire_t *ire1;
14124 	ire_t *save_ire;
14125 	ire_t **irep = NULL;
14126 	boolean_t first = B_TRUE;
14127 	ire_t *clear_ire = NULL;
14128 	ire_t *start_ire = NULL;
14129 	ire_t	*new_lb_ire;
14130 	ire_t	*new_nlb_ire;
14131 	boolean_t new_lb_ire_used = B_FALSE;
14132 	boolean_t new_nlb_ire_used = B_FALSE;
14133 	uint64_t match_flags;
14134 	uint64_t phyi_flags;
14135 	boolean_t fallback = B_FALSE;
14136 
14137 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
14138 	    MATCH_IRE_TYPE);
14139 	/*
14140 	 * We may not be able to find some ires if a previous
14141 	 * ire_create failed. This happens when an ipif goes
14142 	 * down and we are unable to create BROADCAST ires due
14143 	 * to memory failure. Thus, we have to check for NULL
14144 	 * below. This should handle the case for LOOPBACK,
14145 	 * POINTOPOINT and interfaces with some POINTOPOINT
14146 	 * logicals for which there are no BROADCAST ires.
14147 	 */
14148 	if (ire == NULL)
14149 		return;
14150 	/*
14151 	 * Currently IRE_BROADCASTS are deleted when an ipif
14152 	 * goes down which runs exclusively. Thus, setting
14153 	 * IRE_MARK_RCVD should not race with ire_delete marking
14154 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
14155 	 * be consistent with other parts of the code that walks
14156 	 * a given bucket.
14157 	 */
14158 	save_ire = ire;
14159 	irb = ire->ire_bucket;
14160 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
14161 	if (new_lb_ire == NULL) {
14162 		ire_refrele(ire);
14163 		return;
14164 	}
14165 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
14166 	if (new_nlb_ire == NULL) {
14167 		ire_refrele(ire);
14168 		kmem_cache_free(ire_cache, new_lb_ire);
14169 		return;
14170 	}
14171 	IRB_REFHOLD(irb);
14172 	rw_enter(&irb->irb_lock, RW_WRITER);
14173 	/*
14174 	 * Get to the first ire matching the address and the
14175 	 * group. If the address does not match we are done
14176 	 * as we could not find the IRE. If the address matches
14177 	 * we should get to the first one matching the group.
14178 	 */
14179 	while (ire != NULL) {
14180 		if (ire->ire_addr != addr ||
14181 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
14182 			break;
14183 		}
14184 		ire = ire->ire_next;
14185 	}
14186 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
14187 	start_ire = ire;
14188 redo:
14189 	while (ire != NULL && ire->ire_addr == addr &&
14190 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
14191 		/*
14192 		 * The first ire for any address within a group
14193 		 * should always be the one with IRE_MARK_NORECV cleared
14194 		 * so that ip_wput_ire can avoid searching for one.
14195 		 * Note down the insertion point which will be used
14196 		 * later.
14197 		 */
14198 		if (first && (irep == NULL))
14199 			irep = ire->ire_ptpn;
14200 		/*
14201 		 * PHYI_FAILED is set when the interface fails.
14202 		 * This interface might have become good, but the
14203 		 * daemon has not yet detected. We should still
14204 		 * not receive on this. PHYI_OFFLINE should never
14205 		 * be picked as this has been offlined and soon
14206 		 * be removed.
14207 		 */
14208 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
14209 		if (phyi_flags & PHYI_OFFLINE) {
14210 			ire->ire_marks |= IRE_MARK_NORECV;
14211 			ire = ire->ire_next;
14212 			continue;
14213 		}
14214 		if (phyi_flags & match_flags) {
14215 			ire->ire_marks |= IRE_MARK_NORECV;
14216 			ire = ire->ire_next;
14217 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
14218 			    PHYI_INACTIVE) {
14219 				fallback = B_TRUE;
14220 			}
14221 			continue;
14222 		}
14223 		if (first) {
14224 			/*
14225 			 * We will move this to the front of the list later
14226 			 * on.
14227 			 */
14228 			clear_ire = ire;
14229 			ire->ire_marks &= ~IRE_MARK_NORECV;
14230 		} else {
14231 			ire->ire_marks |= IRE_MARK_NORECV;
14232 		}
14233 		first = B_FALSE;
14234 		ire = ire->ire_next;
14235 	}
14236 	/*
14237 	 * If we never nominated anybody, try nominating at least
14238 	 * an INACTIVE, if we found one. Do it only once though.
14239 	 */
14240 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
14241 	    fallback) {
14242 		match_flags = PHYI_FAILED;
14243 		ire = start_ire;
14244 		irep = NULL;
14245 		goto redo;
14246 	}
14247 	ire_refrele(save_ire);
14248 
14249 	/*
14250 	 * irep non-NULL indicates that we entered the while loop
14251 	 * above. If clear_ire is at the insertion point, we don't
14252 	 * have to do anything. clear_ire will be NULL if all the
14253 	 * interfaces are failed.
14254 	 *
14255 	 * We cannot unlink and reinsert the ire at the right place
14256 	 * in the list since there can be other walkers of this bucket.
14257 	 * Instead we delete and recreate the ire
14258 	 */
14259 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
14260 		ire_t *clear_ire_stq = NULL;
14261 		bzero(new_lb_ire, sizeof (ire_t));
14262 		/* XXX We need a recovery strategy here. */
14263 		if (ire_init(new_lb_ire,
14264 		    (uchar_t *)&clear_ire->ire_addr,
14265 		    (uchar_t *)&clear_ire->ire_mask,
14266 		    (uchar_t *)&clear_ire->ire_src_addr,
14267 		    (uchar_t *)&clear_ire->ire_gateway_addr,
14268 		    (uchar_t *)&clear_ire->ire_in_src_addr,
14269 		    &clear_ire->ire_max_frag,
14270 		    clear_ire->ire_fp_mp,
14271 		    clear_ire->ire_rfq,
14272 		    clear_ire->ire_stq,
14273 		    clear_ire->ire_type,
14274 		    clear_ire->ire_dlureq_mp,
14275 		    clear_ire->ire_ipif,
14276 		    clear_ire->ire_in_ill,
14277 		    clear_ire->ire_cmask,
14278 		    clear_ire->ire_phandle,
14279 		    clear_ire->ire_ihandle,
14280 		    clear_ire->ire_flags,
14281 		    &clear_ire->ire_uinfo) == NULL)
14282 			cmn_err(CE_PANIC, "ire_init() failed");
14283 		if (clear_ire->ire_stq == NULL) {
14284 			ire_t *ire_next = clear_ire->ire_next;
14285 			if (ire_next != NULL &&
14286 			    ire_next->ire_stq != NULL &&
14287 			    ire_next->ire_addr == clear_ire->ire_addr &&
14288 			    ire_next->ire_ipif->ipif_ill ==
14289 			    clear_ire->ire_ipif->ipif_ill) {
14290 				clear_ire_stq = ire_next;
14291 
14292 				bzero(new_nlb_ire, sizeof (ire_t));
14293 				/* XXX We need a recovery strategy here. */
14294 				if (ire_init(new_nlb_ire,
14295 				    (uchar_t *)&clear_ire_stq->ire_addr,
14296 				    (uchar_t *)&clear_ire_stq->ire_mask,
14297 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
14298 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
14299 				    (uchar_t *)&clear_ire_stq->ire_in_src_addr,
14300 				    &clear_ire_stq->ire_max_frag,
14301 				    clear_ire_stq->ire_fp_mp,
14302 				    clear_ire_stq->ire_rfq,
14303 				    clear_ire_stq->ire_stq,
14304 				    clear_ire_stq->ire_type,
14305 				    clear_ire_stq->ire_dlureq_mp,
14306 				    clear_ire_stq->ire_ipif,
14307 				    clear_ire_stq->ire_in_ill,
14308 				    clear_ire_stq->ire_cmask,
14309 				    clear_ire_stq->ire_phandle,
14310 				    clear_ire_stq->ire_ihandle,
14311 				    clear_ire_stq->ire_flags,
14312 				    &clear_ire_stq->ire_uinfo) == NULL)
14313 					cmn_err(CE_PANIC, "ire_init() failed");
14314 			}
14315 		}
14316 
14317 		/*
14318 		 * Delete the ire. We can't call ire_delete() since
14319 		 * we are holding the bucket lock. We can't release the
14320 		 * bucket lock since we can't allow irep to change. So just
14321 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
14322 		 * ire from the list and do the refrele.
14323 		 */
14324 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
14325 		irb->irb_marks |= IRE_MARK_CONDEMNED;
14326 
14327 		if (clear_ire_stq != NULL) {
14328 			ire_fastpath_list_delete(
14329 			    (ill_t *)clear_ire_stq->ire_stq->q_ptr,
14330 			    clear_ire_stq);
14331 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
14332 		}
14333 
14334 		/*
14335 		 * Also take care of otherfields like ib/ob pkt count
14336 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
14337 		 */
14338 
14339 		/* Add the new ire's. Insert at *irep */
14340 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
14341 		ire1 = *irep;
14342 		if (ire1 != NULL)
14343 			ire1->ire_ptpn = &new_lb_ire->ire_next;
14344 		new_lb_ire->ire_next = ire1;
14345 		/* Link the new one in. */
14346 		new_lb_ire->ire_ptpn = irep;
14347 		membar_producer();
14348 		*irep = new_lb_ire;
14349 		new_lb_ire_used = B_TRUE;
14350 		BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
14351 		new_lb_ire->ire_bucket->irb_ire_cnt++;
14352 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
14353 
14354 		if (clear_ire_stq != NULL) {
14355 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
14356 			irep = &new_lb_ire->ire_next;
14357 			/* Add the new ire. Insert at *irep */
14358 			ire1 = *irep;
14359 			if (ire1 != NULL)
14360 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
14361 			new_nlb_ire->ire_next = ire1;
14362 			/* Link the new one in. */
14363 			new_nlb_ire->ire_ptpn = irep;
14364 			membar_producer();
14365 			*irep = new_nlb_ire;
14366 			new_nlb_ire_used = B_TRUE;
14367 			BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
14368 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
14369 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
14370 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
14371 		}
14372 	}
14373 	rw_exit(&irb->irb_lock);
14374 	if (!new_lb_ire_used)
14375 		kmem_cache_free(ire_cache, new_lb_ire);
14376 	if (!new_nlb_ire_used)
14377 		kmem_cache_free(ire_cache, new_nlb_ire);
14378 	IRB_REFRELE(irb);
14379 }
14380 
14381 /*
14382  * Whenever an ipif goes down we have to renominate a different
14383  * broadcast ire to receive. Whenever an ipif comes up, we need
14384  * to make sure that we have only one nominated to receive.
14385  */
14386 static void
14387 ipif_renominate_bcast(ipif_t *ipif)
14388 {
14389 	ill_t *ill = ipif->ipif_ill;
14390 	ipaddr_t subnet_addr;
14391 	ipaddr_t net_addr;
14392 	ipaddr_t net_mask = 0;
14393 	ipaddr_t subnet_netmask;
14394 	ipaddr_t addr;
14395 	ill_group_t *illgrp;
14396 
14397 	illgrp = ill->ill_group;
14398 	/*
14399 	 * If this is the last ipif going down, it might take
14400 	 * the ill out of the group. In that case ipif_down ->
14401 	 * illgrp_delete takes care of doing the nomination.
14402 	 * ipif_down does not call for this case.
14403 	 */
14404 	ASSERT(illgrp != NULL);
14405 
14406 	/* There could not have been any ires associated with this */
14407 	if (ipif->ipif_subnet == 0)
14408 		return;
14409 
14410 	ill_mark_bcast(illgrp, 0);
14411 	ill_mark_bcast(illgrp, INADDR_BROADCAST);
14412 
14413 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14414 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14415 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14416 	} else {
14417 		net_mask = htonl(IN_CLASSA_NET);
14418 	}
14419 	addr = net_mask & ipif->ipif_subnet;
14420 	ill_mark_bcast(illgrp, addr);
14421 
14422 	net_addr = ~net_mask | addr;
14423 	ill_mark_bcast(illgrp, net_addr);
14424 
14425 	subnet_netmask = ipif->ipif_net_mask;
14426 	addr = ipif->ipif_subnet;
14427 	ill_mark_bcast(illgrp, addr);
14428 
14429 	subnet_addr = ~subnet_netmask | addr;
14430 	ill_mark_bcast(illgrp, subnet_addr);
14431 }
14432 
14433 /*
14434  * Whenever we form or delete ill groups, we need to nominate one set of
14435  * BROADCAST ires for receiving in the group.
14436  *
14437  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
14438  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
14439  *    for ill_ipif_up_count to be non-zero. This is the only case where
14440  *    ill_ipif_up_count is zero and we would still find the ires.
14441  *
14442  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
14443  *    ipif is UP and we just have to do the nomination.
14444  *
14445  * 3) When ill_handoff_responsibility calls us, some ill has been removed
14446  *    from the group. So, we have to do the nomination.
14447  *
14448  * Because of (3), there could be just one ill in the group. But we have
14449  * to nominate still as IRE_MARK_NORCV may have been marked on this.
14450  * Thus, this function does not optimize when there is only one ill as
14451  * it is not correct for (3).
14452  */
14453 static void
14454 ill_nominate_bcast_rcv(ill_group_t *illgrp)
14455 {
14456 	ill_t *ill;
14457 	ipif_t *ipif;
14458 	ipaddr_t subnet_addr;
14459 	ipaddr_t prev_subnet_addr = 0;
14460 	ipaddr_t net_addr;
14461 	ipaddr_t prev_net_addr = 0;
14462 	ipaddr_t net_mask = 0;
14463 	ipaddr_t subnet_netmask;
14464 	ipaddr_t addr;
14465 
14466 	/*
14467 	 * When the last memeber is leaving, there is nothing to
14468 	 * nominate.
14469 	 */
14470 	if (illgrp->illgrp_ill_count == 0) {
14471 		ASSERT(illgrp->illgrp_ill == NULL);
14472 		return;
14473 	}
14474 
14475 	ill = illgrp->illgrp_ill;
14476 	ASSERT(!ill->ill_isv6);
14477 	/*
14478 	 * We assume that ires with same address and belonging to the
14479 	 * same group, has been grouped together. Nominating a *single*
14480 	 * ill in the group for sending and receiving broadcast is done
14481 	 * by making sure that the first BROADCAST ire (which will be
14482 	 * the one returned by ire_ctable_lookup for ip_rput and the
14483 	 * one that will be used in ip_wput_ire) will be the one that
14484 	 * will not have IRE_MARK_NORECV set.
14485 	 *
14486 	 * 1) ip_rput checks and discards packets received on ires marked
14487 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
14488 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
14489 	 *    first ire in the group for every broadcast address in the group.
14490 	 *    ip_rput will accept packets only on the first ire i.e only
14491 	 *    one copy of the ill.
14492 	 *
14493 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
14494 	 *    packet for the whole group. It needs to send out on the ill
14495 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
14496 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
14497 	 *    the copy echoed back on other port where the ire is not marked
14498 	 *    with IRE_MARK_NORECV.
14499 	 *
14500 	 * Note that we just need to have the first IRE either loopback or
14501 	 * non-loopback (either of them may not exist if ire_create failed
14502 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
14503 	 * always hit the first one and hence will always accept one copy.
14504 	 *
14505 	 * We have a broadcast ire per ill for all the unique prefixes
14506 	 * hosted on that ill. As we don't have a way of knowing the
14507 	 * unique prefixes on a given ill and hence in the whole group,
14508 	 * we just call ill_mark_bcast on all the prefixes that exist
14509 	 * in the group. For the common case of one prefix, the code
14510 	 * below optimizes by remebering the last address used for
14511 	 * markng. In the case of multiple prefixes, this will still
14512 	 * optimize depending the order of prefixes.
14513 	 *
14514 	 * The only unique address across the whole group is 0.0.0.0 and
14515 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
14516 	 * the first ire in the bucket for receiving and disables the
14517 	 * others.
14518 	 */
14519 	ill_mark_bcast(illgrp, 0);
14520 	ill_mark_bcast(illgrp, INADDR_BROADCAST);
14521 	for (; ill != NULL; ill = ill->ill_group_next) {
14522 
14523 		for (ipif = ill->ill_ipif; ipif != NULL;
14524 		    ipif = ipif->ipif_next) {
14525 
14526 			if (!(ipif->ipif_flags & IPIF_UP) ||
14527 			    ipif->ipif_subnet == 0) {
14528 				continue;
14529 			}
14530 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14531 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14532 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14533 			} else {
14534 				net_mask = htonl(IN_CLASSA_NET);
14535 			}
14536 			addr = net_mask & ipif->ipif_subnet;
14537 			if (prev_net_addr == 0 || prev_net_addr != addr) {
14538 				ill_mark_bcast(illgrp, addr);
14539 				net_addr = ~net_mask | addr;
14540 				ill_mark_bcast(illgrp, net_addr);
14541 			}
14542 			prev_net_addr = addr;
14543 
14544 			subnet_netmask = ipif->ipif_net_mask;
14545 			addr = ipif->ipif_subnet;
14546 			if (prev_subnet_addr == 0 ||
14547 			    prev_subnet_addr != addr) {
14548 				ill_mark_bcast(illgrp, addr);
14549 				subnet_addr = ~subnet_netmask | addr;
14550 				ill_mark_bcast(illgrp, subnet_addr);
14551 			}
14552 			prev_subnet_addr = addr;
14553 		}
14554 	}
14555 }
14556 
14557 /*
14558  * This function is called while forming ill groups.
14559  *
14560  * Currently, we handle only allmulti groups. We want to join
14561  * allmulti on only one of the ills in the groups. In future,
14562  * when we have link aggregation, we may have to join normal
14563  * multicast groups on multiple ills as switch does inbound load
14564  * balancing. Following are the functions that calls this
14565  * function :
14566  *
14567  * 1) ill_recover_multicast : Interface is coming back UP.
14568  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
14569  *    will call ill_recover_multicast to recover all the multicast
14570  *    groups. We need to make sure that only one member is joined
14571  *    in the ill group.
14572  *
14573  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
14574  *    Somebody is joining allmulti. We need to make sure that only one
14575  *    member is joined in the group.
14576  *
14577  * 3) illgrp_insert : If allmulti has already joined, we need to make
14578  *    sure that only one member is joined in the group.
14579  *
14580  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
14581  *    allmulti who we have nominated. We need to pick someother ill.
14582  *
14583  * 5) illgrp_delete : The ill we nominated is leaving the group,
14584  *    we need to pick a new ill to join the group.
14585  *
14586  * For (1), (2), (5) - we just have to check whether there is
14587  * a good ill joined in the group. If we could not find any ills
14588  * joined the group, we should join.
14589  *
14590  * For (4), the one that was nominated to receive, left the group.
14591  * There could be nobody joined in the group when this function is
14592  * called.
14593  *
14594  * For (3) - we need to explicitly check whether there are multiple
14595  * ills joined in the group.
14596  *
14597  * For simplicity, we don't differentiate any of the above cases. We
14598  * just leave the group if it is joined on any of them and join on
14599  * the first good ill.
14600  */
14601 int
14602 ill_nominate_mcast_rcv(ill_group_t *illgrp)
14603 {
14604 	ilm_t *ilm;
14605 	ill_t *ill;
14606 	ill_t *fallback_inactive_ill = NULL;
14607 	ill_t *fallback_failed_ill = NULL;
14608 	int ret = 0;
14609 
14610 	/*
14611 	 * Leave the allmulti on all the ills and start fresh.
14612 	 */
14613 	for (ill = illgrp->illgrp_ill; ill != NULL;
14614 	    ill = ill->ill_group_next) {
14615 		if (ill->ill_join_allmulti)
14616 			(void) ip_leave_allmulti(ill->ill_ipif);
14617 	}
14618 
14619 	/*
14620 	 * Choose a good ill. Fallback to inactive or failed if
14621 	 * none available. We need to fallback to FAILED in the
14622 	 * case where we have 2 interfaces in a group - where
14623 	 * one of them is failed and another is a good one and
14624 	 * the good one (not marked inactive) is leaving the group.
14625 	 */
14626 	ret = 0;
14627 	for (ill = illgrp->illgrp_ill; ill != NULL;
14628 	    ill = ill->ill_group_next) {
14629 		/* Never pick an offline interface */
14630 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
14631 			continue;
14632 
14633 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
14634 			fallback_failed_ill = ill;
14635 			continue;
14636 		}
14637 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
14638 			fallback_inactive_ill = ill;
14639 			continue;
14640 		}
14641 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14642 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14643 				ret = ip_join_allmulti(ill->ill_ipif);
14644 				/*
14645 				 * ip_join_allmulti can fail because of memory
14646 				 * failures. So, make sure we join at least
14647 				 * on one ill.
14648 				 */
14649 				if (ill->ill_join_allmulti)
14650 					return (0);
14651 			}
14652 		}
14653 	}
14654 	if (ret != 0) {
14655 		/*
14656 		 * If we tried nominating above and failed to do so,
14657 		 * return error. We might have tried multiple times.
14658 		 * But, return the latest error.
14659 		 */
14660 		return (ret);
14661 	}
14662 	if ((ill = fallback_inactive_ill) != NULL) {
14663 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14664 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14665 				ret = ip_join_allmulti(ill->ill_ipif);
14666 				return (ret);
14667 			}
14668 		}
14669 	} else if ((ill = fallback_failed_ill) != NULL) {
14670 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14671 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14672 				ret = ip_join_allmulti(ill->ill_ipif);
14673 				return (ret);
14674 			}
14675 		}
14676 	}
14677 	return (0);
14678 }
14679 
14680 /*
14681  * This function is called from illgrp_delete after it is
14682  * deleted from the group to reschedule responsibilities
14683  * to a different ill.
14684  */
14685 static void
14686 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
14687 {
14688 	ilm_t	*ilm;
14689 	ipif_t	*ipif;
14690 	ipaddr_t subnet_addr;
14691 	ipaddr_t net_addr;
14692 	ipaddr_t net_mask = 0;
14693 	ipaddr_t subnet_netmask;
14694 	ipaddr_t addr;
14695 
14696 	ASSERT(ill->ill_group == NULL);
14697 	/*
14698 	 * Broadcast Responsibility:
14699 	 *
14700 	 * 1. If this ill has been nominated for receiving broadcast
14701 	 * packets, we need to find a new one. Before we find a new
14702 	 * one, we need to re-group the ires that are part of this new
14703 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
14704 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
14705 	 * thing for us.
14706 	 *
14707 	 * 2. If this ill was not nominated for receiving broadcast
14708 	 * packets, we need to clear the IRE_MARK_NORECV flag
14709 	 * so that we continue to send up broadcast packets.
14710 	 */
14711 	if (!ill->ill_isv6) {
14712 		/*
14713 		 * Case 1 above : No optimization here. Just redo the
14714 		 * nomination.
14715 		 */
14716 		ill_group_bcast_for_xmit(ill);
14717 		ill_nominate_bcast_rcv(illgrp);
14718 
14719 		/*
14720 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
14721 		 */
14722 		ill_clear_bcast_mark(ill, 0);
14723 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
14724 
14725 		for (ipif = ill->ill_ipif; ipif != NULL;
14726 		    ipif = ipif->ipif_next) {
14727 
14728 			if (!(ipif->ipif_flags & IPIF_UP) ||
14729 			    ipif->ipif_subnet == 0) {
14730 				continue;
14731 			}
14732 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14733 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14734 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14735 			} else {
14736 				net_mask = htonl(IN_CLASSA_NET);
14737 			}
14738 			addr = net_mask & ipif->ipif_subnet;
14739 			ill_clear_bcast_mark(ill, addr);
14740 
14741 			net_addr = ~net_mask | addr;
14742 			ill_clear_bcast_mark(ill, net_addr);
14743 
14744 			subnet_netmask = ipif->ipif_net_mask;
14745 			addr = ipif->ipif_subnet;
14746 			ill_clear_bcast_mark(ill, addr);
14747 
14748 			subnet_addr = ~subnet_netmask | addr;
14749 			ill_clear_bcast_mark(ill, subnet_addr);
14750 		}
14751 	}
14752 
14753 	/*
14754 	 * Multicast Responsibility.
14755 	 *
14756 	 * If we have joined allmulti on this one, find a new member
14757 	 * in the group to join allmulti. As this ill is already part
14758 	 * of allmulti, we don't have to join on this one.
14759 	 *
14760 	 * If we have not joined allmulti on this one, there is no
14761 	 * responsibility to handoff. But we need to take new
14762 	 * responsibility i.e, join allmulti on this one if we need
14763 	 * to.
14764 	 */
14765 	if (ill->ill_join_allmulti) {
14766 		(void) ill_nominate_mcast_rcv(illgrp);
14767 	} else {
14768 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14769 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14770 				(void) ip_join_allmulti(ill->ill_ipif);
14771 				break;
14772 			}
14773 		}
14774 	}
14775 
14776 	/*
14777 	 * We intentionally do the flushing of IRE_CACHES only matching
14778 	 * on the ill and not on groups. Note that we are already deleted
14779 	 * from the group.
14780 	 *
14781 	 * This will make sure that all IRE_CACHES whose stq is pointing
14782 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
14783 	 * deleted and IRE_CACHES that are not pointing at this ill will
14784 	 * be left alone.
14785 	 */
14786 	if (ill->ill_isv6) {
14787 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
14788 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
14789 	} else {
14790 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
14791 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
14792 	}
14793 
14794 	/*
14795 	 * Some conn may have cached one of the IREs deleted above. By removing
14796 	 * the ire reference, we clean up the extra reference to the ill held in
14797 	 * ire->ire_stq.
14798 	 */
14799 	ipcl_walk(conn_cleanup_stale_ire, NULL);
14800 
14801 	/*
14802 	 * Re-do source address selection for all the members in the
14803 	 * group, if they borrowed source address from one of the ipifs
14804 	 * in this ill.
14805 	 */
14806 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14807 		if (ill->ill_isv6) {
14808 			ipif_update_other_ipifs_v6(ipif, illgrp);
14809 		} else {
14810 			ipif_update_other_ipifs(ipif, illgrp);
14811 		}
14812 	}
14813 }
14814 
14815 /*
14816  * Delete the ill from the group. The caller makes sure that it is
14817  * in a group and it okay to delete from the group. So, we always
14818  * delete here.
14819  */
14820 static void
14821 illgrp_delete(ill_t *ill)
14822 {
14823 	ill_group_t *illgrp;
14824 	ill_group_t *tmpg;
14825 	ill_t *tmp_ill;
14826 
14827 	/*
14828 	 * Reset illgrp_ill_schednext if it was pointing at us.
14829 	 * We need to do this before we set ill_group to NULL.
14830 	 */
14831 	rw_enter(&ill_g_lock, RW_WRITER);
14832 	mutex_enter(&ill->ill_lock);
14833 
14834 	illgrp_reset_schednext(ill);
14835 
14836 	illgrp = ill->ill_group;
14837 
14838 	/* Delete the ill from illgrp. */
14839 	if (illgrp->illgrp_ill == ill) {
14840 		illgrp->illgrp_ill = ill->ill_group_next;
14841 	} else {
14842 		tmp_ill = illgrp->illgrp_ill;
14843 		while (tmp_ill->ill_group_next != ill) {
14844 			tmp_ill = tmp_ill->ill_group_next;
14845 			ASSERT(tmp_ill != NULL);
14846 		}
14847 		tmp_ill->ill_group_next = ill->ill_group_next;
14848 	}
14849 	ill->ill_group = NULL;
14850 	ill->ill_group_next = NULL;
14851 
14852 	illgrp->illgrp_ill_count--;
14853 	mutex_exit(&ill->ill_lock);
14854 	rw_exit(&ill_g_lock);
14855 
14856 	/*
14857 	 * As this ill is leaving the group, we need to hand off
14858 	 * the responsibilities to the other ills in the group, if
14859 	 * this ill had some responsibilities.
14860 	 */
14861 
14862 	ill_handoff_responsibility(ill, illgrp);
14863 
14864 	rw_enter(&ill_g_lock, RW_WRITER);
14865 
14866 	if (illgrp->illgrp_ill_count == 0) {
14867 
14868 		ASSERT(illgrp->illgrp_ill == NULL);
14869 		if (ill->ill_isv6) {
14870 			if (illgrp == illgrp_head_v6) {
14871 				illgrp_head_v6 = illgrp->illgrp_next;
14872 			} else {
14873 				tmpg = illgrp_head_v6;
14874 				while (tmpg->illgrp_next != illgrp) {
14875 					tmpg = tmpg->illgrp_next;
14876 					ASSERT(tmpg != NULL);
14877 				}
14878 				tmpg->illgrp_next = illgrp->illgrp_next;
14879 			}
14880 		} else {
14881 			if (illgrp == illgrp_head_v4) {
14882 				illgrp_head_v4 = illgrp->illgrp_next;
14883 			} else {
14884 				tmpg = illgrp_head_v4;
14885 				while (tmpg->illgrp_next != illgrp) {
14886 					tmpg = tmpg->illgrp_next;
14887 					ASSERT(tmpg != NULL);
14888 				}
14889 				tmpg->illgrp_next = illgrp->illgrp_next;
14890 			}
14891 		}
14892 		mutex_destroy(&illgrp->illgrp_lock);
14893 		mi_free(illgrp);
14894 	}
14895 	rw_exit(&ill_g_lock);
14896 
14897 	/*
14898 	 * Even though the ill is out of the group its not necessary
14899 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
14900 	 * We will split the ipsq when phyint_groupname is set to NULL.
14901 	 */
14902 
14903 	/*
14904 	 * Send a routing sockets message if we are deleting from
14905 	 * groups with names.
14906 	 */
14907 	if (ill->ill_phyint->phyint_groupname_len != 0)
14908 		ip_rts_ifmsg(ill->ill_ipif);
14909 }
14910 
14911 /*
14912  * Re-do source address selection. This is normally called when
14913  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
14914  * ipif comes up.
14915  */
14916 void
14917 ill_update_source_selection(ill_t *ill)
14918 {
14919 	ipif_t *ipif;
14920 
14921 	ASSERT(IAM_WRITER_ILL(ill));
14922 
14923 	if (ill->ill_group != NULL)
14924 		ill = ill->ill_group->illgrp_ill;
14925 
14926 	for (; ill != NULL; ill = ill->ill_group_next) {
14927 		for (ipif = ill->ill_ipif; ipif != NULL;
14928 		    ipif = ipif->ipif_next) {
14929 			if (ill->ill_isv6)
14930 				ipif_recreate_interface_routes_v6(NULL, ipif);
14931 			else
14932 				ipif_recreate_interface_routes(NULL, ipif);
14933 		}
14934 	}
14935 }
14936 
14937 /*
14938  * Insert ill in a group headed by illgrp_head. The caller can either
14939  * pass a groupname in which case we search for a group with the
14940  * same name to insert in or pass a group to insert in. This function
14941  * would only search groups with names.
14942  *
14943  * NOTE : The caller should make sure that there is at least one ipif
14944  *	  UP on this ill so that illgrp_scheduler can pick this ill
14945  *	  for outbound packets. If ill_ipif_up_count is zero, we have
14946  *	  already sent a DL_UNBIND to the driver and we don't want to
14947  *	  send anymore packets. We don't assert for ipif_up_count
14948  *	  to be greater than zero, because ipif_up_done wants to call
14949  *	  this function before bumping up the ipif_up_count. See
14950  *	  ipif_up_done() for details.
14951  */
14952 int
14953 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
14954     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
14955 {
14956 	ill_group_t *illgrp;
14957 	ill_t *prev_ill;
14958 	phyint_t *phyi;
14959 
14960 	ASSERT(ill->ill_group == NULL);
14961 
14962 	rw_enter(&ill_g_lock, RW_WRITER);
14963 	mutex_enter(&ill->ill_lock);
14964 
14965 	if (groupname != NULL) {
14966 		/*
14967 		 * Look for a group with a matching groupname to insert.
14968 		 */
14969 		for (illgrp = *illgrp_head; illgrp != NULL;
14970 		    illgrp = illgrp->illgrp_next) {
14971 
14972 			ill_t *tmp_ill;
14973 
14974 			tmp_ill = illgrp->illgrp_ill;
14975 			ASSERT(tmp_ill != NULL && tmp_ill->ill_phyint != NULL);
14976 			phyi = tmp_ill->ill_phyint;
14977 			/*
14978 			 * Look at groups which has names only.
14979 			 */
14980 			if (phyi->phyint_groupname_len == 0)
14981 				continue;
14982 			/*
14983 			 * Names are stored in the phyint common to both
14984 			 * IPv4 and IPv6.
14985 			 */
14986 			if (mi_strcmp(phyi->phyint_groupname,
14987 			    groupname) == 0) {
14988 				break;
14989 			}
14990 		}
14991 	} else {
14992 		/*
14993 		 * If the caller passes in a NULL "grp_to_insert", we
14994 		 * allocate one below and insert this singleton.
14995 		 */
14996 		illgrp = grp_to_insert;
14997 	}
14998 
14999 	ill->ill_group_next = NULL;
15000 
15001 	if (illgrp == NULL) {
15002 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
15003 		if (illgrp == NULL) {
15004 			return (ENOMEM);
15005 		}
15006 		illgrp->illgrp_next = *illgrp_head;
15007 		*illgrp_head = illgrp;
15008 		illgrp->illgrp_ill = ill;
15009 		illgrp->illgrp_ill_count = 1;
15010 		ill->ill_group = illgrp;
15011 		/*
15012 		 * Used in illgrp_scheduler to protect multiple threads
15013 		 * from traversing the list.
15014 		 */
15015 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
15016 	} else {
15017 		ASSERT(ill->ill_net_type ==
15018 		    illgrp->illgrp_ill->ill_net_type);
15019 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
15020 
15021 		/* Insert ill at tail of this group */
15022 		prev_ill = illgrp->illgrp_ill;
15023 		while (prev_ill->ill_group_next != NULL)
15024 			prev_ill = prev_ill->ill_group_next;
15025 		prev_ill->ill_group_next = ill;
15026 		ill->ill_group = illgrp;
15027 		illgrp->illgrp_ill_count++;
15028 		/*
15029 		 * Inherit group properties. Currently only forwarding
15030 		 * is the property we try to keep the same with all the
15031 		 * ills. When there are more, we will abstract this into
15032 		 * a function.
15033 		 */
15034 		ill->ill_flags &= ~ILLF_ROUTER;
15035 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
15036 	}
15037 	mutex_exit(&ill->ill_lock);
15038 	rw_exit(&ill_g_lock);
15039 
15040 	/*
15041 	 * 1) When ipif_up_done() calls this function, ipif_up_count
15042 	 *    may be zero as it has not yet been bumped. But the ires
15043 	 *    have already been added. So, we do the nomination here
15044 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
15045 	 *    for ill_ipif_up_count != 0. Thus we don't check for
15046 	 *    ill_ipif_up_count here while nominating broadcast ires for
15047 	 *    receive.
15048 	 *
15049 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
15050 	 *    to group them properly as ire_add() has already happened
15051 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
15052 	 *    case, we need to do it here anyway.
15053 	 */
15054 	if (!ill->ill_isv6) {
15055 		ill_group_bcast_for_xmit(ill);
15056 		ill_nominate_bcast_rcv(illgrp);
15057 	}
15058 
15059 	if (!ipif_is_coming_up) {
15060 		/*
15061 		 * When ipif_up_done() calls this function, the multicast
15062 		 * groups have not been joined yet. So, there is no point in
15063 		 * nomination. ip_join_allmulti will handle groups when
15064 		 * ill_recover_multicast is called from ipif_up_done() later.
15065 		 */
15066 		(void) ill_nominate_mcast_rcv(illgrp);
15067 		/*
15068 		 * ipif_up_done calls ill_update_source_selection
15069 		 * anyway. Moreover, we don't want to re-create
15070 		 * interface routes while ipif_up_done() still has reference
15071 		 * to them. Refer to ipif_up_done() for more details.
15072 		 */
15073 		ill_update_source_selection(ill);
15074 	}
15075 
15076 	/*
15077 	 * Send a routing sockets message if we are inserting into
15078 	 * groups with names.
15079 	 */
15080 	if (groupname != NULL)
15081 		ip_rts_ifmsg(ill->ill_ipif);
15082 	return (0);
15083 }
15084 
15085 /*
15086  * Return the first phyint matching the groupname. There could
15087  * be more than one when there are ill groups.
15088  *
15089  * Needs work: called only from ip_sioctl_groupname
15090  */
15091 static phyint_t *
15092 phyint_lookup_group(char *groupname)
15093 {
15094 	phyint_t *phyi;
15095 
15096 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
15097 	/*
15098 	 * Group names are stored in the phyint - a common structure
15099 	 * to both IPv4 and IPv6.
15100 	 */
15101 	phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
15102 	for (; phyi != NULL;
15103 	    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
15104 	    phyi, AVL_AFTER)) {
15105 		if (phyi->phyint_groupname_len == 0)
15106 			continue;
15107 		ASSERT(phyi->phyint_groupname != NULL);
15108 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
15109 			return (phyi);
15110 	}
15111 	return (NULL);
15112 }
15113 
15114 
15115 
15116 /*
15117  * MT notes on creation and deletion of IPMP groups
15118  *
15119  * Creation and deletion of IPMP groups introduce the need to merge or
15120  * split the associated serialization objects i.e the ipsq's. Normally all
15121  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
15122  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
15123  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
15124  * is a need to change the <ill-ipsq> association and we have to operate on both
15125  * the source and destination IPMP groups. For eg. attempting to set the
15126  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
15127  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
15128  * source or destination IPMP group are mapped to a single ipsq for executing
15129  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
15130  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
15131  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
15132  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
15133  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
15134  * ipsq has to be examined for redoing the <ill-ipsq> associations.
15135  *
15136  * In the above example the ioctl handling code locates the current ipsq of hme0
15137  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
15138  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
15139  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
15140  * the destination ipsq. If the destination ipsq is not busy, it also enters
15141  * the destination ipsq exclusively. Now the actual groupname setting operation
15142  * can proceed. If the destination ipsq is busy, the operation is enqueued
15143  * on the destination (merged) ipsq and will be handled in the unwind from
15144  * ipsq_exit.
15145  *
15146  * To prevent other threads accessing the ill while the group name change is
15147  * in progres, we bring down the ipifs which also removes the ill from the
15148  * group. The group is changed in phyint and when the first ipif on the ill
15149  * is brought up, the ill is inserted into the right IPMP group by
15150  * illgrp_insert.
15151  */
15152 /* ARGSUSED */
15153 int
15154 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15155     ip_ioctl_cmd_t *ipip, void *ifreq)
15156 {
15157 	int i;
15158 	char *tmp;
15159 	int namelen;
15160 	ill_t *ill = ipif->ipif_ill;
15161 	ill_t *ill_v4, *ill_v6;
15162 	int err = 0;
15163 	phyint_t *phyi;
15164 	phyint_t *phyi_tmp;
15165 	struct lifreq *lifr;
15166 	mblk_t	*mp1;
15167 	char *groupname;
15168 	ipsq_t *ipsq;
15169 
15170 	ASSERT(IAM_WRITER_IPIF(ipif));
15171 
15172 	/* Existance verified in ip_wput_nondata */
15173 	mp1 = mp->b_cont->b_cont;
15174 	lifr = (struct lifreq *)mp1->b_rptr;
15175 	groupname = lifr->lifr_groupname;
15176 
15177 	if (ipif->ipif_id != 0)
15178 		return (EINVAL);
15179 
15180 	phyi = ill->ill_phyint;
15181 	ASSERT(phyi != NULL);
15182 
15183 	if (phyi->phyint_flags & PHYI_VIRTUAL)
15184 		return (EINVAL);
15185 
15186 	tmp = groupname;
15187 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
15188 		;
15189 
15190 	if (i == LIFNAMSIZ) {
15191 		/* no null termination */
15192 		return (EINVAL);
15193 	}
15194 
15195 	/*
15196 	 * Calculate the namelen exclusive of the null
15197 	 * termination character.
15198 	 */
15199 	namelen = tmp - groupname;
15200 
15201 	ill_v4 = phyi->phyint_illv4;
15202 	ill_v6 = phyi->phyint_illv6;
15203 
15204 	/*
15205 	 * ILL cannot be part of a usesrc group and and IPMP group at the
15206 	 * same time. No need to grab the ill_g_usesrc_lock here, see
15207 	 * synchronization notes in ip.c
15208 	 */
15209 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
15210 		return (EINVAL);
15211 	}
15212 
15213 	/*
15214 	 * mark the ill as changing.
15215 	 * this should queue all new requests on the syncq.
15216 	 */
15217 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
15218 
15219 	if (ill_v4 != NULL)
15220 		ill_v4->ill_state_flags |= ILL_CHANGING;
15221 	if (ill_v6 != NULL)
15222 		ill_v6->ill_state_flags |= ILL_CHANGING;
15223 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15224 
15225 	if (namelen == 0) {
15226 		/*
15227 		 * Null string means remove this interface from the
15228 		 * existing group.
15229 		 */
15230 		if (phyi->phyint_groupname_len == 0) {
15231 			/*
15232 			 * Never was in a group.
15233 			 */
15234 			err = 0;
15235 			goto done;
15236 		}
15237 
15238 		/*
15239 		 * IPv4 or IPv6 may be temporarily out of the group when all
15240 		 * the ipifs are down. Thus, we need to check for ill_group to
15241 		 * be non-NULL.
15242 		 */
15243 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
15244 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
15245 			mutex_enter(&ill_v4->ill_lock);
15246 			if (!ill_is_quiescent(ill_v4)) {
15247 				/*
15248 				 * ipsq_pending_mp_add will not fail since
15249 				 * connp is NULL
15250 				 */
15251 				(void) ipsq_pending_mp_add(NULL,
15252 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
15253 				mutex_exit(&ill_v4->ill_lock);
15254 				err = EINPROGRESS;
15255 				goto done;
15256 			}
15257 			mutex_exit(&ill_v4->ill_lock);
15258 		}
15259 
15260 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
15261 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
15262 			mutex_enter(&ill_v6->ill_lock);
15263 			if (!ill_is_quiescent(ill_v6)) {
15264 				(void) ipsq_pending_mp_add(NULL,
15265 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
15266 				mutex_exit(&ill_v6->ill_lock);
15267 				err = EINPROGRESS;
15268 				goto done;
15269 			}
15270 			mutex_exit(&ill_v6->ill_lock);
15271 		}
15272 
15273 		rw_enter(&ill_g_lock, RW_WRITER);
15274 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15275 		mutex_enter(&phyi->phyint_lock);
15276 		ASSERT(phyi->phyint_groupname != NULL);
15277 		mi_free(phyi->phyint_groupname);
15278 		phyi->phyint_groupname = NULL;
15279 		phyi->phyint_groupname_len = 0;
15280 		mutex_exit(&phyi->phyint_lock);
15281 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15282 		rw_exit(&ill_g_lock);
15283 		err = ill_up_ipifs(ill, q, mp);
15284 
15285 		/*
15286 		 * set the split flag so that the ipsq can be split
15287 		 */
15288 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15289 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
15290 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15291 
15292 	} else {
15293 		if (phyi->phyint_groupname_len != 0) {
15294 			ASSERT(phyi->phyint_groupname != NULL);
15295 			/* Are we inserting in the same group ? */
15296 			if (mi_strcmp(groupname,
15297 			    phyi->phyint_groupname) == 0) {
15298 				err = 0;
15299 				goto done;
15300 			}
15301 		}
15302 
15303 		rw_enter(&ill_g_lock, RW_READER);
15304 		/*
15305 		 * Merge ipsq for the group's.
15306 		 * This check is here as multiple groups/ills might be
15307 		 * sharing the same ipsq.
15308 		 * If we have to merege than the operation is restarted
15309 		 * on the new ipsq.
15310 		 */
15311 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL);
15312 		if (phyi->phyint_ipsq != ipsq) {
15313 			rw_exit(&ill_g_lock);
15314 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
15315 			goto done;
15316 		}
15317 		/*
15318 		 * Running exclusive on new ipsq.
15319 		 */
15320 
15321 		ASSERT(ipsq != NULL);
15322 		ASSERT(ipsq->ipsq_writer == curthread);
15323 
15324 		/*
15325 		 * Check whether the ill_type and ill_net_type matches before
15326 		 * we allocate any memory so that the cleanup is easier.
15327 		 *
15328 		 * We can't group dissimilar ones as we can't load spread
15329 		 * packets across the group because of potential link-level
15330 		 * header differences.
15331 		 */
15332 		phyi_tmp = phyint_lookup_group(groupname);
15333 		if (phyi_tmp != NULL) {
15334 			if ((ill_v4 != NULL &&
15335 			    phyi_tmp->phyint_illv4 != NULL) &&
15336 			    ((ill_v4->ill_net_type !=
15337 			    phyi_tmp->phyint_illv4->ill_net_type) ||
15338 			    (ill_v4->ill_type !=
15339 			    phyi_tmp->phyint_illv4->ill_type))) {
15340 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15341 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
15342 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15343 				rw_exit(&ill_g_lock);
15344 				return (EINVAL);
15345 			}
15346 			if ((ill_v6 != NULL &&
15347 			    phyi_tmp->phyint_illv6 != NULL) &&
15348 			    ((ill_v6->ill_net_type !=
15349 			    phyi_tmp->phyint_illv6->ill_net_type) ||
15350 			    (ill_v6->ill_type !=
15351 			    phyi_tmp->phyint_illv6->ill_type))) {
15352 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15353 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
15354 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15355 				rw_exit(&ill_g_lock);
15356 				return (EINVAL);
15357 			}
15358 		}
15359 
15360 		rw_exit(&ill_g_lock);
15361 
15362 		/*
15363 		 * bring down all v4 ipifs.
15364 		 */
15365 		if (ill_v4 != NULL) {
15366 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
15367 		}
15368 
15369 		/*
15370 		 * bring down all v6 ipifs.
15371 		 */
15372 		if (ill_v6 != NULL) {
15373 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
15374 		}
15375 
15376 		/*
15377 		 * make sure all ipifs are down and there are no active
15378 		 * references. Call to ipsq_pending_mp_add will not fail
15379 		 * since connp is NULL.
15380 		 */
15381 		if (ill_v4 != NULL) {
15382 			mutex_enter(&ill_v4->ill_lock);
15383 			if (!ill_is_quiescent(ill_v4)) {
15384 				(void) ipsq_pending_mp_add(NULL,
15385 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
15386 				mutex_exit(&ill_v4->ill_lock);
15387 				err = EINPROGRESS;
15388 				goto done;
15389 			}
15390 			mutex_exit(&ill_v4->ill_lock);
15391 		}
15392 
15393 		if (ill_v6 != NULL) {
15394 			mutex_enter(&ill_v6->ill_lock);
15395 			if (!ill_is_quiescent(ill_v6)) {
15396 				(void) ipsq_pending_mp_add(NULL,
15397 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
15398 				mutex_exit(&ill_v6->ill_lock);
15399 				err = EINPROGRESS;
15400 				goto done;
15401 			}
15402 			mutex_exit(&ill_v6->ill_lock);
15403 		}
15404 
15405 		/*
15406 		 * allocate including space for null terminator
15407 		 * before we insert.
15408 		 */
15409 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
15410 		if (tmp == NULL)
15411 			return (ENOMEM);
15412 
15413 		rw_enter(&ill_g_lock, RW_WRITER);
15414 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15415 		mutex_enter(&phyi->phyint_lock);
15416 		if (phyi->phyint_groupname_len != 0) {
15417 			ASSERT(phyi->phyint_groupname != NULL);
15418 			mi_free(phyi->phyint_groupname);
15419 		}
15420 
15421 		/*
15422 		 * setup the new group name.
15423 		 */
15424 		phyi->phyint_groupname = tmp;
15425 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
15426 		phyi->phyint_groupname_len = namelen + 1;
15427 		mutex_exit(&phyi->phyint_lock);
15428 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15429 		rw_exit(&ill_g_lock);
15430 
15431 		err = ill_up_ipifs(ill, q, mp);
15432 	}
15433 
15434 done:
15435 	/*
15436 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
15437 	 */
15438 	if (err != EINPROGRESS) {
15439 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15440 		if (ill_v4 != NULL)
15441 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
15442 		if (ill_v6 != NULL)
15443 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
15444 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15445 	}
15446 	return (err);
15447 }
15448 
15449 /* ARGSUSED */
15450 int
15451 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
15452     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15453 {
15454 	ill_t *ill;
15455 	phyint_t *phyi;
15456 	struct lifreq *lifr;
15457 	mblk_t	*mp1;
15458 
15459 	/* Existence verified in ip_wput_nondata */
15460 	mp1 = mp->b_cont->b_cont;
15461 	lifr = (struct lifreq *)mp1->b_rptr;
15462 	ill = ipif->ipif_ill;
15463 	phyi = ill->ill_phyint;
15464 
15465 	lifr->lifr_groupname[0] = '\0';
15466 	/*
15467 	 * ill_group may be null if all the interfaces
15468 	 * are down. But still, the phyint should always
15469 	 * hold the name.
15470 	 */
15471 	if (phyi->phyint_groupname_len != 0) {
15472 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
15473 		    phyi->phyint_groupname_len);
15474 	}
15475 
15476 	return (0);
15477 }
15478 
15479 
15480 typedef struct conn_move_s {
15481 	ill_t	*cm_from_ill;
15482 	ill_t	*cm_to_ill;
15483 	int	cm_ifindex;
15484 } conn_move_t;
15485 
15486 /*
15487  * ipcl_walk function for moving conn_multicast_ill for a given ill.
15488  */
15489 static void
15490 conn_move(conn_t *connp, caddr_t arg)
15491 {
15492 	conn_move_t *connm;
15493 	int ifindex;
15494 	int i;
15495 	ill_t *from_ill;
15496 	ill_t *to_ill;
15497 	ilg_t *ilg;
15498 	ilm_t *ret_ilm;
15499 
15500 	connm = (conn_move_t *)arg;
15501 	ifindex = connm->cm_ifindex;
15502 	from_ill = connm->cm_from_ill;
15503 	to_ill = connm->cm_to_ill;
15504 
15505 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
15506 
15507 	/* All multicast fields protected by conn_lock */
15508 	mutex_enter(&connp->conn_lock);
15509 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
15510 	if ((connp->conn_outgoing_ill == from_ill) &&
15511 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
15512 		connp->conn_outgoing_ill = to_ill;
15513 		connp->conn_incoming_ill = to_ill;
15514 	}
15515 
15516 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
15517 
15518 	if ((connp->conn_multicast_ill == from_ill) &&
15519 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
15520 		connp->conn_multicast_ill = connm->cm_to_ill;
15521 	}
15522 
15523 	/* Change IP_XMIT_IF associations */
15524 	if ((connp->conn_xmit_if_ill == from_ill) &&
15525 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
15526 		connp->conn_xmit_if_ill = to_ill;
15527 	}
15528 	/*
15529 	 * Change the ilg_ill to point to the new one. This assumes
15530 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
15531 	 * has been told to receive packets on this interface.
15532 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
15533 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
15534 	 * some ilms may not have moved. We check to see whether
15535 	 * the ilms have moved to to_ill. We can't check on from_ill
15536 	 * as in the process of moving, we could have split an ilm
15537 	 * in to two - which has the same orig_ifindex and v6group.
15538 	 *
15539 	 * For IPv4, ilg_ipif moves implicitly. The code below really
15540 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
15541 	 */
15542 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
15543 		ilg = &connp->conn_ilg[i];
15544 		if ((ilg->ilg_ill == from_ill) &&
15545 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
15546 			/* ifindex != 0 indicates failback */
15547 			if (ifindex != 0) {
15548 				connp->conn_ilg[i].ilg_ill = to_ill;
15549 				continue;
15550 			}
15551 
15552 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
15553 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
15554 			    connp->conn_zoneid);
15555 
15556 			if (ret_ilm != NULL)
15557 				connp->conn_ilg[i].ilg_ill = to_ill;
15558 		}
15559 	}
15560 	mutex_exit(&connp->conn_lock);
15561 }
15562 
15563 static void
15564 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
15565 {
15566 	conn_move_t connm;
15567 
15568 	connm.cm_from_ill = from_ill;
15569 	connm.cm_to_ill = to_ill;
15570 	connm.cm_ifindex = ifindex;
15571 
15572 	ipcl_walk(conn_move, (caddr_t)&connm);
15573 }
15574 
15575 /*
15576  * ilm has been moved from from_ill to to_ill.
15577  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
15578  * appropriately.
15579  *
15580  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
15581  *	  the code there de-references ipif_ill to get the ill to
15582  *	  send multicast requests. It does not work as ipif is on its
15583  *	  move and already moved when this function is called.
15584  *	  Thus, we need to use from_ill and to_ill send down multicast
15585  *	  requests.
15586  */
15587 static void
15588 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
15589 {
15590 	ipif_t *ipif;
15591 	ilm_t *ilm;
15592 
15593 	/*
15594 	 * See whether we need to send down DL_ENABMULTI_REQ on
15595 	 * to_ill as ilm has just been added.
15596 	 */
15597 	ASSERT(IAM_WRITER_ILL(to_ill));
15598 	ASSERT(IAM_WRITER_ILL(from_ill));
15599 
15600 	ILM_WALKER_HOLD(to_ill);
15601 	for (ilm = to_ill->ill_ilm; ilm != NULL && ilm->ilm_is_new &&
15602 	    !(ilm->ilm_flags & ILM_DELETED); ilm = ilm->ilm_next) {
15603 
15604 		/*
15605 		 * no locks held, ill/ipif cannot dissappear as long
15606 		 * as we are writer.
15607 		 */
15608 		ipif = to_ill->ill_ipif;
15609 		/*
15610 		 * No need to hold any lock as we are the writer and this
15611 		 * can only be changed by a writer.
15612 		 */
15613 		ilm->ilm_is_new = B_FALSE;
15614 
15615 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
15616 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
15617 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
15618 			    "resolver\n"));
15619 			continue;		/* Must be IRE_IF_NORESOLVER */
15620 		}
15621 
15622 
15623 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
15624 			ip1dbg(("ilm_send_multicast_reqs: "
15625 			    "to_ill MULTI_BCAST\n"));
15626 			ilm->ilm_join_mld = B_FALSE;
15627 			goto from;
15628 		}
15629 
15630 		if (ilm->ilm_join_mld) {
15631 			ASSERT(to_ill->ill_isv6);
15632 			mld_joingroup(ilm);
15633 		}
15634 
15635 		ilm->ilm_join_mld = B_FALSE;
15636 
15637 		if (to_ill->ill_ipif_up_count == 0) {
15638 			/*
15639 			 * Nobody there. All multicast addresses will be
15640 			 * re-joined when we get the DL_BIND_ACK bringing the
15641 			 * interface up.
15642 			 */
15643 			ilm->ilm_notify_driver = B_FALSE;
15644 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
15645 			goto from;
15646 		}
15647 
15648 		/*
15649 		 * For allmulti address, we want to join on only one interface.
15650 		 * Checking for ilm_numentries_v6 is not correct as you may
15651 		 * find an ilm with zero address on to_ill, but we may not
15652 		 * have nominated to_ill for receiving. Thus, if we have
15653 		 * nominated from_ill (ill_join_allmulti is set), nominate
15654 		 * only if to_ill is not already nominated (to_ill normally
15655 		 * should not have been nominated if "from_ill" has already
15656 		 * been nominated. As we don't prevent failovers from happening
15657 		 * across groups, we don't assert).
15658 		 */
15659 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15660 			/*
15661 			 * There is no need to hold ill locks as we are
15662 			 * writer on both ills and when ill_join_allmulti
15663 			 * is changed the thread is always a writer.
15664 			 */
15665 			if (from_ill->ill_join_allmulti &&
15666 			    !to_ill->ill_join_allmulti) {
15667 				(void) ip_join_allmulti(to_ill->ill_ipif);
15668 			}
15669 		} else if (ilm->ilm_notify_driver) {
15670 
15671 			/*
15672 			 * This is a newly moved ilm so we need to tell the
15673 			 * driver about the new group. There can be more than
15674 			 * one ilm's for the same group in the list each with a
15675 			 * different orig_ifindex. We have to inform the driver
15676 			 * once. In ilm_move_v[4,6] we only set the flag
15677 			 * ilm_notify_driver for the first ilm.
15678 			 */
15679 
15680 			(void) ip_ll_send_enabmulti_req(to_ill,
15681 			    &ilm->ilm_v6addr);
15682 		}
15683 
15684 		ilm->ilm_notify_driver = B_FALSE;
15685 
15686 		/*
15687 		 * See whether we need to send down DL_DISABMULTI_REQ on
15688 		 * from_ill as ilm has just been removed.
15689 		 */
15690 from:
15691 		ipif = from_ill->ill_ipif;
15692 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
15693 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
15694 			ip1dbg(("ilm_send_multicast_reqs: "
15695 			    "from_ill not resolver\n"));
15696 			continue;		/* Must be IRE_IF_NORESOLVER */
15697 		}
15698 
15699 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
15700 			ip1dbg(("ilm_send_multicast_reqs: "
15701 			    "from_ill MULTI_BCAST\n"));
15702 			continue;
15703 		}
15704 
15705 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15706 			if (from_ill->ill_join_allmulti)
15707 			    (void) ip_leave_allmulti(from_ill->ill_ipif);
15708 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
15709 			(void) ip_ll_send_disabmulti_req(from_ill,
15710 		    &ilm->ilm_v6addr);
15711 		}
15712 	}
15713 	ILM_WALKER_RELE(to_ill);
15714 }
15715 
15716 /*
15717  * This function is called when all multicast memberships needs
15718  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
15719  * called only once unlike the IPv4 counterpart where it is called after
15720  * every logical interface is moved. The reason is due to multicast
15721  * memberships are joined using an interface address in IPv4 while in
15722  * IPv6, interface index is used.
15723  */
15724 static void
15725 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
15726 {
15727 	ilm_t	*ilm;
15728 	ilm_t	*ilm_next;
15729 	ilm_t	*new_ilm;
15730 	ilm_t	**ilmp;
15731 	int	count;
15732 	char buf[INET6_ADDRSTRLEN];
15733 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
15734 
15735 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
15736 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
15737 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
15738 
15739 	if (ifindex == 0) {
15740 		/*
15741 		 * Form the solicited node mcast address which is used later.
15742 		 */
15743 		ipif_t *ipif;
15744 
15745 		ipif = from_ill->ill_ipif;
15746 		ASSERT(ipif->ipif_id == 0);
15747 
15748 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
15749 	}
15750 
15751 	ilmp = &from_ill->ill_ilm;
15752 	for (ilm = from_ill->ill_ilm; ilm != NULL &&
15753 	    !(ilm->ilm_flags & ILM_DELETED); ilm = ilm_next) {
15754 		ilm_next = ilm->ilm_next;
15755 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
15756 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
15757 		ASSERT(ilm->ilm_orig_ifindex != 0);
15758 		if (ilm->ilm_orig_ifindex == ifindex) {
15759 			/*
15760 			 * We are failing back multicast memberships.
15761 			 * If the same ilm exists in to_ill, it means somebody
15762 			 * has joined the same group there e.g. ff02::1
15763 			 * is joined within the kernel when the interfaces
15764 			 * came UP.
15765 			 */
15766 			ASSERT(ilm->ilm_ipif == NULL);
15767 			if (new_ilm != NULL) {
15768 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
15769 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
15770 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
15771 					new_ilm->ilm_join_mld = B_TRUE;
15772 				}
15773 			} else {
15774 				/*
15775 				 * check if we can just move the ilm
15776 				 */
15777 				if (from_ill->ill_ilm_walker_cnt != 0) {
15778 					/*
15779 					 * We have walkers we cannot move
15780 					 * the ilm, so allocate a new ilm,
15781 					 * this (old) ilm will be marked
15782 					 * ILM_DELETED at the end of the loop
15783 					 * and will be freed when the
15784 					 * last walker exits.
15785 					 */
15786 					new_ilm = (ilm_t *)mi_zalloc
15787 					    (sizeof (ilm_t));
15788 					if (new_ilm == NULL) {
15789 						ip0dbg(("ilm_move_v6: "
15790 						    "FAILBACK of IPv6"
15791 						    " multicast address %s : "
15792 						    "from %s to"
15793 						    " %s failed : ENOMEM \n",
15794 						    inet_ntop(AF_INET6,
15795 						    &ilm->ilm_v6addr, buf,
15796 						    sizeof (buf)),
15797 						    from_ill->ill_name,
15798 						    to_ill->ill_name));
15799 
15800 							ilmp = &ilm->ilm_next;
15801 							continue;
15802 					}
15803 					*new_ilm = *ilm;
15804 					/*
15805 					 * we don't want new_ilm linked to
15806 					 * ilm's filter list.
15807 					 */
15808 					new_ilm->ilm_filter = NULL;
15809 				} else {
15810 					/*
15811 					 * No walkers we can move the ilm.
15812 					 * lets take it out of the list.
15813 					 */
15814 					*ilmp = ilm->ilm_next;
15815 					ilm->ilm_next = NULL;
15816 					new_ilm = ilm;
15817 				}
15818 
15819 				new_ilm->ilm_ill = to_ill;
15820 				/* Add to the to_ill's list */
15821 				new_ilm->ilm_next = to_ill->ill_ilm;
15822 				to_ill->ill_ilm = new_ilm;
15823 				/*
15824 				 * set the flag so that mld_joingroup is
15825 				 * called in ilm_send_multicast_reqs().
15826 				 */
15827 				new_ilm->ilm_join_mld = B_TRUE;
15828 				/*
15829 				 * if this is the first ilm for the group
15830 				 * set ilm_notify_driver so that we notify the
15831 				 * driver in ilm_send_multicast_reqs.
15832 				 */
15833 				if (ilm_lookup_ill_v6(to_ill,
15834 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
15835 					new_ilm->ilm_notify_driver = B_TRUE;
15836 			}
15837 			goto bottom;
15838 		} else if (ifindex != 0) {
15839 			/*
15840 			 * If this is FAILBACK (ifindex != 0) and the ifindex
15841 			 * has not matched above, look at the next ilm.
15842 			 */
15843 			ilmp = &ilm->ilm_next;
15844 			continue;
15845 		}
15846 		/*
15847 		 * If we are here, it means ifindex is 0. Failover
15848 		 * everything.
15849 		 *
15850 		 * We need to handle solicited node mcast address
15851 		 * and all_nodes mcast address differently as they
15852 		 * are joined witin the kenrel (ipif_multicast_up)
15853 		 * and potentially from the userland. We are called
15854 		 * after the ipifs of from_ill has been moved.
15855 		 * If we still find ilms on ill with solicited node
15856 		 * mcast address or all_nodes mcast address, it must
15857 		 * belong to the UP interface that has not moved e.g.
15858 		 * ipif_id 0 with the link local prefix does not move.
15859 		 * We join this on the new ill accounting for all the
15860 		 * userland memberships so that applications don't
15861 		 * see any failure.
15862 		 *
15863 		 * We need to make sure that we account only for the
15864 		 * solicited node and all node multicast addresses
15865 		 * that was brought UP on these. In the case of
15866 		 * a failover from A to B, we might have ilms belonging
15867 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
15868 		 * for the membership from the userland. If we are failing
15869 		 * over from B to C now, we will find the ones belonging
15870 		 * to A on B. These don't account for the ill_ipif_up_count.
15871 		 * They just move from B to C. The check below on
15872 		 * ilm_orig_ifindex ensures that.
15873 		 */
15874 		if ((ilm->ilm_orig_ifindex ==
15875 		    from_ill->ill_phyint->phyint_ifindex) &&
15876 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
15877 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
15878 		    &ilm->ilm_v6addr))) {
15879 			ASSERT(ilm->ilm_refcnt > 0);
15880 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
15881 			/*
15882 			 * For indentation reasons, we are not using a
15883 			 * "else" here.
15884 			 */
15885 			if (count == 0) {
15886 				ilmp = &ilm->ilm_next;
15887 				continue;
15888 			}
15889 			ilm->ilm_refcnt -= count;
15890 			if (new_ilm != NULL) {
15891 				/*
15892 				 * Can find one with the same
15893 				 * ilm_orig_ifindex, if we are failing
15894 				 * over to a STANDBY. This happens
15895 				 * when somebody wants to join a group
15896 				 * on a STANDBY interface and we
15897 				 * internally join on a different one.
15898 				 * If we had joined on from_ill then, a
15899 				 * failover now will find a new ilm
15900 				 * with this index.
15901 				 */
15902 				ip1dbg(("ilm_move_v6: FAILOVER, found"
15903 				    " new ilm on %s, group address %s\n",
15904 				    to_ill->ill_name,
15905 				    inet_ntop(AF_INET6,
15906 				    &ilm->ilm_v6addr, buf,
15907 				    sizeof (buf))));
15908 				new_ilm->ilm_refcnt += count;
15909 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
15910 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
15911 					new_ilm->ilm_join_mld = B_TRUE;
15912 				}
15913 			} else {
15914 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
15915 				if (new_ilm == NULL) {
15916 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
15917 					    " multicast address %s : from %s to"
15918 					    " %s failed : ENOMEM \n",
15919 					    inet_ntop(AF_INET6,
15920 					    &ilm->ilm_v6addr, buf,
15921 					    sizeof (buf)), from_ill->ill_name,
15922 					    to_ill->ill_name));
15923 					ilmp = &ilm->ilm_next;
15924 					continue;
15925 				}
15926 				*new_ilm = *ilm;
15927 				new_ilm->ilm_filter = NULL;
15928 				new_ilm->ilm_refcnt = count;
15929 				new_ilm->ilm_ill = to_ill;
15930 				new_ilm->ilm_timer = INFINITY;
15931 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
15932 				new_ilm->ilm_join_mld = B_TRUE;
15933 				/* Add to the to_ill's list */
15934 				new_ilm->ilm_next = to_ill->ill_ilm;
15935 				to_ill->ill_ilm = new_ilm;
15936 				/*
15937 				 * If the to_ill has not joined this
15938 				 * group we need to tell the driver in
15939 				 * ill_send_multicast_reqs.
15940 				 */
15941 				if (ilm_lookup_ill_v6(to_ill,
15942 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
15943 					new_ilm->ilm_notify_driver = B_TRUE;
15944 				ASSERT(new_ilm->ilm_ipif == NULL);
15945 			}
15946 			if (ilm->ilm_refcnt == 0) {
15947 				goto bottom;
15948 			} else {
15949 				new_ilm->ilm_is_new = B_TRUE;
15950 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
15951 				CLEAR_SLIST(new_ilm->ilm_filter);
15952 				ilmp = &ilm->ilm_next;
15953 			}
15954 			continue;
15955 		} else {
15956 			/*
15957 			 * ifindex = 0 means, move everything pointing at
15958 			 * from_ill. We are doing this becuase ill has
15959 			 * either FAILED or became INACTIVE.
15960 			 *
15961 			 * As we would like to move things later back to
15962 			 * from_ill, we want to retain the identity of this
15963 			 * ilm. Thus, we don't blindly increment the reference
15964 			 * count on the ilms matching the address alone. We
15965 			 * need to match on the ilm_orig_index also. new_ilm
15966 			 * was obtained by matching ilm_orig_index also.
15967 			 */
15968 			if (new_ilm != NULL) {
15969 				/*
15970 				 * This is possible only if a previous restore
15971 				 * was incomplete i.e restore to
15972 				 * ilm_orig_ifindex left some ilms because
15973 				 * of some failures. Thus when we are failing
15974 				 * again, we might find our old friends there.
15975 				 */
15976 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
15977 				    " on %s, group address %s\n",
15978 				    to_ill->ill_name,
15979 				    inet_ntop(AF_INET6,
15980 				    &ilm->ilm_v6addr, buf,
15981 				    sizeof (buf))));
15982 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
15983 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
15984 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
15985 					new_ilm->ilm_join_mld = B_TRUE;
15986 				}
15987 			} else {
15988 				if (from_ill->ill_ilm_walker_cnt != 0) {
15989 					new_ilm = (ilm_t *)
15990 					    mi_zalloc(sizeof (ilm_t));
15991 					if (new_ilm == NULL) {
15992 						ip0dbg(("ilm_move_v6: "
15993 						    "FAILOVER of IPv6"
15994 						    " multicast address %s : "
15995 						    "from %s to"
15996 						    " %s failed : ENOMEM \n",
15997 						    inet_ntop(AF_INET6,
15998 						    &ilm->ilm_v6addr, buf,
15999 						    sizeof (buf)),
16000 						    from_ill->ill_name,
16001 						    to_ill->ill_name));
16002 
16003 							ilmp = &ilm->ilm_next;
16004 							continue;
16005 					}
16006 					*new_ilm = *ilm;
16007 					new_ilm->ilm_filter = NULL;
16008 				} else {
16009 					*ilmp = ilm->ilm_next;
16010 					new_ilm = ilm;
16011 				}
16012 				/* Add to the to_ill's list */
16013 				new_ilm->ilm_next = to_ill->ill_ilm;
16014 				to_ill->ill_ilm = new_ilm;
16015 				ASSERT(ilm->ilm_ipif == NULL);
16016 				new_ilm->ilm_ill = to_ill;
16017 				new_ilm->ilm_join_mld = B_TRUE;
16018 				/*
16019 				 * If the to_ill has not joined this
16020 				 * group we need to tell the driver in
16021 				 * ill_send_multicast_reqs.
16022 				 */
16023 				if (ilm_lookup_ill_v6(to_ill,
16024 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16025 					new_ilm->ilm_notify_driver = B_TRUE;
16026 			}
16027 
16028 		}
16029 
16030 bottom:
16031 		/*
16032 		 * set ilm_send_multicast_reqs so that we inform the
16033 		 * driver about the multicast group.
16034 		 */
16035 		new_ilm->ilm_is_new = B_TRUE;
16036 		/*
16037 		 * Revert multicast filter state to (EXCLUDE, NULL).
16038 		 * new_ilm->ilm_join_mld should already be set if needed.
16039 		 */
16040 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16041 		CLEAR_SLIST(new_ilm->ilm_filter);
16042 		/*
16043 		 * We allocated/got a new ilm, free the old one.
16044 		 */
16045 		if (new_ilm != ilm) {
16046 			if (from_ill->ill_ilm_walker_cnt == 0) {
16047 				*ilmp = ilm->ilm_next;
16048 				ilm->ilm_next = NULL;
16049 				FREE_SLIST(ilm->ilm_filter);
16050 				FREE_SLIST(ilm->ilm_pendsrcs);
16051 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
16052 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
16053 				mi_free((char *)ilm);
16054 			} else {
16055 				ilm->ilm_flags |= ILM_DELETED;
16056 				from_ill->ill_ilm_cleanup_reqd = 1;
16057 				ilmp = &ilm->ilm_next;
16058 			}
16059 		}
16060 	}
16061 }
16062 
16063 /*
16064  * Move all the multicast memberships to to_ill. Called when
16065  * an ipif moves from "from_ill" to "to_ill". This function is slightly
16066  * different from IPv6 counterpart as multicast memberships are associated
16067  * with ills in IPv6. This function is called after every ipif is moved
16068  * unlike IPv6, where it is moved only once.
16069  */
16070 static void
16071 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
16072 {
16073 	ilm_t	*ilm;
16074 	ilm_t	*ilm_next;
16075 	ilm_t	*new_ilm;
16076 	ilm_t	**ilmp;
16077 
16078 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16079 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16080 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
16081 
16082 	ilmp = &from_ill->ill_ilm;
16083 	for (ilm = from_ill->ill_ilm; ilm != NULL &&
16084 	    !(ilm->ilm_flags & ILM_DELETED); ilm = ilm_next) {
16085 		ilm_next = ilm->ilm_next;
16086 		ASSERT(ilm->ilm_ipif != NULL);
16087 
16088 		if (ilm->ilm_ipif != ipif) {
16089 			ilmp = &ilm->ilm_next;
16090 			continue;
16091 		}
16092 
16093 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
16094 		    htonl(INADDR_ALLHOSTS_GROUP)) {
16095 			/*
16096 			 * We joined this in ipif_multicast_up
16097 			 * and we never did an ipif_multicast_down
16098 			 * for IPv4. If nobody else from the userland
16099 			 * has reference, we free the ilm, and later
16100 			 * when this ipif comes up on the new ill,
16101 			 * we will join this again.
16102 			 */
16103 			if (--ilm->ilm_refcnt == 0)
16104 				goto delete_ilm;
16105 
16106 			new_ilm = ilm_lookup_ipif(ipif,
16107 			    V4_PART_OF_V6(ilm->ilm_v6addr));
16108 			if (new_ilm != NULL) {
16109 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16110 				/*
16111 				 * We still need to deal with the from_ill.
16112 				 */
16113 				new_ilm->ilm_is_new = B_TRUE;
16114 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16115 				CLEAR_SLIST(new_ilm->ilm_filter);
16116 				goto delete_ilm;
16117 			}
16118 			/*
16119 			 * If we could not find one e.g. ipif is
16120 			 * still down on to_ill, we add this ilm
16121 			 * on ill_new to preserve the reference
16122 			 * count.
16123 			 */
16124 		}
16125 		/*
16126 		 * When ipifs move, ilms always move with it
16127 		 * to the NEW ill. Thus we should never be
16128 		 * able to find ilm till we really move it here.
16129 		 */
16130 		ASSERT(ilm_lookup_ipif(ipif,
16131 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
16132 
16133 		if (from_ill->ill_ilm_walker_cnt != 0) {
16134 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
16135 			if (new_ilm == NULL) {
16136 				char buf[INET6_ADDRSTRLEN];
16137 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
16138 				    " multicast address %s : "
16139 				    "from %s to"
16140 				    " %s failed : ENOMEM \n",
16141 				    inet_ntop(AF_INET,
16142 				    &ilm->ilm_v6addr, buf,
16143 				    sizeof (buf)),
16144 				    from_ill->ill_name,
16145 				    to_ill->ill_name));
16146 
16147 				ilmp = &ilm->ilm_next;
16148 				continue;
16149 			}
16150 			*new_ilm = *ilm;
16151 			/* We don't want new_ilm linked to ilm's filter list */
16152 			new_ilm->ilm_filter = NULL;
16153 		} else {
16154 			/* Remove from the list */
16155 			*ilmp = ilm->ilm_next;
16156 			new_ilm = ilm;
16157 		}
16158 
16159 		/*
16160 		 * If we have never joined this group on the to_ill
16161 		 * make sure we tell the driver.
16162 		 */
16163 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
16164 		    ALL_ZONES) == NULL)
16165 			new_ilm->ilm_notify_driver = B_TRUE;
16166 
16167 		/* Add to the to_ill's list */
16168 		new_ilm->ilm_next = to_ill->ill_ilm;
16169 		to_ill->ill_ilm = new_ilm;
16170 		new_ilm->ilm_is_new = B_TRUE;
16171 
16172 		/*
16173 		 * Revert multicast filter state to (EXCLUDE, NULL)
16174 		 */
16175 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16176 		CLEAR_SLIST(new_ilm->ilm_filter);
16177 
16178 		/*
16179 		 * Delete only if we have allocated a new ilm.
16180 		 */
16181 		if (new_ilm != ilm) {
16182 delete_ilm:
16183 			if (from_ill->ill_ilm_walker_cnt == 0) {
16184 				/* Remove from the list */
16185 				*ilmp = ilm->ilm_next;
16186 				ilm->ilm_next = NULL;
16187 				FREE_SLIST(ilm->ilm_filter);
16188 				FREE_SLIST(ilm->ilm_pendsrcs);
16189 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
16190 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
16191 				mi_free((char *)ilm);
16192 			} else {
16193 				ilm->ilm_flags |= ILM_DELETED;
16194 				from_ill->ill_ilm_cleanup_reqd = 1;
16195 				ilmp = &ilm->ilm_next;
16196 			}
16197 		}
16198 	}
16199 }
16200 
16201 static uint_t
16202 ipif_get_id(ill_t *ill, uint_t id)
16203 {
16204 	uint_t	unit;
16205 	ipif_t	*tipif;
16206 	boolean_t found = B_FALSE;
16207 
16208 	/*
16209 	 * During failback, we want to go back to the same id
16210 	 * instead of the smallest id so that the original
16211 	 * configuration is maintained. id is non-zero in that
16212 	 * case.
16213 	 */
16214 	if (id != 0) {
16215 		/*
16216 		 * While failing back, if we still have an ipif with
16217 		 * MAX_ADDRS_PER_IF, it means this will be replaced
16218 		 * as soon as we return from this function. It was
16219 		 * to set to MAX_ADDRS_PER_IF by the caller so that
16220 		 * we can choose the smallest id. Thus we return zero
16221 		 * in that case ignoring the hint.
16222 		 */
16223 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
16224 			return (0);
16225 		for (tipif = ill->ill_ipif; tipif != NULL;
16226 		    tipif = tipif->ipif_next) {
16227 			if (tipif->ipif_id == id) {
16228 				found = B_TRUE;
16229 				break;
16230 			}
16231 		}
16232 		/*
16233 		 * If somebody already plumbed another logical
16234 		 * with the same id, we won't be able to find it.
16235 		 */
16236 		if (!found)
16237 			return (id);
16238 	}
16239 	for (unit = 0; unit <= ip_addrs_per_if; unit++) {
16240 		found = B_FALSE;
16241 		for (tipif = ill->ill_ipif; tipif != NULL;
16242 		    tipif = tipif->ipif_next) {
16243 			if (tipif->ipif_id == unit) {
16244 				found = B_TRUE;
16245 				break;
16246 			}
16247 		}
16248 		if (!found)
16249 			break;
16250 	}
16251 	return (unit);
16252 }
16253 
16254 /* ARGSUSED */
16255 static int
16256 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
16257     ipif_t **rep_ipif_ptr)
16258 {
16259 	ill_t	*from_ill;
16260 	ipif_t	*rep_ipif;
16261 	ipif_t	**ipifp;
16262 	uint_t	unit;
16263 	int err = 0;
16264 	ipif_t	*to_ipif;
16265 	struct iocblk	*iocp;
16266 	boolean_t failback_cmd;
16267 	boolean_t remove_ipif;
16268 	int	rc;
16269 
16270 	ASSERT(IAM_WRITER_ILL(to_ill));
16271 	ASSERT(IAM_WRITER_IPIF(ipif));
16272 
16273 	iocp = (struct iocblk *)mp->b_rptr;
16274 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
16275 	remove_ipif = B_FALSE;
16276 
16277 	from_ill = ipif->ipif_ill;
16278 
16279 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16280 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16281 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
16282 
16283 	/*
16284 	 * Don't move LINK LOCAL addresses as they are tied to
16285 	 * physical interface.
16286 	 */
16287 	if (from_ill->ill_isv6 &&
16288 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
16289 		ipif->ipif_was_up = B_FALSE;
16290 		IPIF_UNMARK_MOVING(ipif);
16291 		return (0);
16292 	}
16293 
16294 	/*
16295 	 * We set the ipif_id to maximum so that the search for
16296 	 * ipif_id will pick the lowest number i.e 0 in the
16297 	 * following 2 cases :
16298 	 *
16299 	 * 1) We have a replacement ipif at the head of to_ill.
16300 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
16301 	 *    on to_ill and hence the MOVE might fail. We want to
16302 	 *    remove it only if we could move the ipif. Thus, by
16303 	 *    setting it to the MAX value, we make the search in
16304 	 *    ipif_get_id return the zeroth id.
16305 	 *
16306 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
16307 	 *    we might just have a zero address plumbed on the ipif
16308 	 *    with zero id in the case of IPv4. We remove that while
16309 	 *    doing the failback. We want to remove it only if we
16310 	 *    could move the ipif. Thus, by setting it to the MAX
16311 	 *    value, we make the search in ipif_get_id return the
16312 	 *    zeroth id.
16313 	 *
16314 	 * Both (1) and (2) are done only when when we are moving
16315 	 * an ipif (either due to failover/failback) which originally
16316 	 * belonged to this interface i.e the ipif_orig_ifindex is
16317 	 * the same as to_ill's ifindex. This is needed so that
16318 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
16319 	 * from B -> A (B is being removed from the group) and
16320 	 * FAILBACK from A -> B restores the original configuration.
16321 	 * Without the check for orig_ifindex, the second FAILOVER
16322 	 * could make the ipif belonging to B replace the A's zeroth
16323 	 * ipif and the subsequent failback re-creating the replacement
16324 	 * ipif again.
16325 	 *
16326 	 * NOTE : We created the replacement ipif when we did a
16327 	 * FAILOVER (See below). We could check for FAILBACK and
16328 	 * then look for replacement ipif to be removed. But we don't
16329 	 * want to do that because we wan't to allow the possibility
16330 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
16331 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
16332 	 * from B -> A.
16333 	 */
16334 	to_ipif = to_ill->ill_ipif;
16335 	if ((to_ill->ill_phyint->phyint_ifindex ==
16336 	    ipif->ipif_orig_ifindex) &&
16337 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
16338 		ASSERT(to_ipif->ipif_id == 0);
16339 		remove_ipif = B_TRUE;
16340 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
16341 	}
16342 	/*
16343 	 * Find the lowest logical unit number on the to_ill.
16344 	 * If we are failing back, try to get the original id
16345 	 * rather than the lowest one so that the original
16346 	 * configuration is maintained.
16347 	 *
16348 	 * XXX need a better scheme for this.
16349 	 */
16350 	if (failback_cmd) {
16351 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
16352 	} else {
16353 		unit = ipif_get_id(to_ill, 0);
16354 	}
16355 
16356 	/* Reset back to zero in case we fail below */
16357 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
16358 		to_ipif->ipif_id = 0;
16359 
16360 	if (unit == ip_addrs_per_if) {
16361 		ipif->ipif_was_up = B_FALSE;
16362 		IPIF_UNMARK_MOVING(ipif);
16363 		return (EINVAL);
16364 	}
16365 
16366 	/*
16367 	 * ipif is ready to move from "from_ill" to "to_ill".
16368 	 *
16369 	 * 1) If we are moving ipif with id zero, create a
16370 	 *    replacement ipif for this ipif on from_ill. If this fails
16371 	 *    fail the MOVE operation.
16372 	 *
16373 	 * 2) Remove the replacement ipif on to_ill if any.
16374 	 *    We could remove the replacement ipif when we are moving
16375 	 *    the ipif with id zero. But what if somebody already
16376 	 *    unplumbed it ? Thus we always remove it if it is present.
16377 	 *    We want to do it only if we are sure we are going to
16378 	 *    move the ipif to to_ill which is why there are no
16379 	 *    returns due to error till ipif is linked to to_ill.
16380 	 *    Note that the first ipif that we failback will always
16381 	 *    be zero if it is present.
16382 	 */
16383 	if (ipif->ipif_id == 0) {
16384 		ipaddr_t inaddr_any = INADDR_ANY;
16385 
16386 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
16387 		if (rep_ipif == NULL) {
16388 			ipif->ipif_was_up = B_FALSE;
16389 			IPIF_UNMARK_MOVING(ipif);
16390 			return (ENOMEM);
16391 		}
16392 		*rep_ipif = ipif_zero;
16393 		/*
16394 		 * Before we put the ipif on the list, store the addresses
16395 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
16396 		 * assumes so. This logic is not any different from what
16397 		 * ipif_allocate does.
16398 		 */
16399 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16400 		    &rep_ipif->ipif_v6lcl_addr);
16401 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16402 		    &rep_ipif->ipif_v6src_addr);
16403 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16404 		    &rep_ipif->ipif_v6subnet);
16405 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16406 		    &rep_ipif->ipif_v6net_mask);
16407 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16408 		    &rep_ipif->ipif_v6brd_addr);
16409 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16410 		    &rep_ipif->ipif_v6pp_dst_addr);
16411 		/*
16412 		 * We mark IPIF_NOFAILOVER so that this can never
16413 		 * move.
16414 		 */
16415 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
16416 		rep_ipif->ipif_flags &= ~IPIF_UP;
16417 		rep_ipif->ipif_replace_zero = B_TRUE;
16418 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
16419 		    MUTEX_DEFAULT, NULL);
16420 		rep_ipif->ipif_id = 0;
16421 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
16422 		rep_ipif->ipif_ill = from_ill;
16423 		rep_ipif->ipif_orig_ifindex =
16424 		    from_ill->ill_phyint->phyint_ifindex;
16425 		/* Insert at head */
16426 		rep_ipif->ipif_next = from_ill->ill_ipif;
16427 		from_ill->ill_ipif = rep_ipif;
16428 		/*
16429 		 * We don't really care to let apps know about
16430 		 * this interface.
16431 		 */
16432 	}
16433 
16434 	if (remove_ipif) {
16435 		/*
16436 		 * We set to a max value above for this case to get
16437 		 * id zero. ASSERT that we did get one.
16438 		 */
16439 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
16440 		rep_ipif = to_ipif;
16441 		to_ill->ill_ipif = rep_ipif->ipif_next;
16442 		rep_ipif->ipif_next = NULL;
16443 		/*
16444 		 * If some apps scanned and find this interface,
16445 		 * it is time to let them know, so that they can
16446 		 * delete it.
16447 		 */
16448 
16449 		*rep_ipif_ptr = rep_ipif;
16450 	}
16451 
16452 	/* Get it out of the ILL interface list. */
16453 	ipifp = &ipif->ipif_ill->ill_ipif;
16454 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
16455 		if (*ipifp == ipif) {
16456 			*ipifp = ipif->ipif_next;
16457 			break;
16458 		}
16459 	}
16460 
16461 	/* Assign the new ill */
16462 	ipif->ipif_ill = to_ill;
16463 	ipif->ipif_id = unit;
16464 	/* id has already been checked */
16465 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
16466 	ASSERT(rc == 0);
16467 	/* Let SCTP update its list */
16468 	sctp_move_ipif(ipif, from_ill, to_ill);
16469 	/*
16470 	 * Handle the failover and failback of ipif_t between
16471 	 * ill_t that have differing maximum mtu values.
16472 	 */
16473 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
16474 		if (ipif->ipif_saved_mtu == 0) {
16475 			/*
16476 			 * As this ipif_t is moving to an ill_t
16477 			 * that has a lower ill_max_mtu, its
16478 			 * ipif_mtu needs to be saved so it can
16479 			 * be restored during failback or during
16480 			 * failover to an ill_t which has a
16481 			 * higher ill_max_mtu.
16482 			 */
16483 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
16484 			ipif->ipif_mtu = to_ill->ill_max_mtu;
16485 		} else {
16486 			/*
16487 			 * The ipif_t is, once again, moving to
16488 			 * an ill_t that has a lower maximum mtu
16489 			 * value.
16490 			 */
16491 			ipif->ipif_mtu = to_ill->ill_max_mtu;
16492 		}
16493 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
16494 	    ipif->ipif_saved_mtu != 0) {
16495 		/*
16496 		 * The mtu of this ipif_t had to be reduced
16497 		 * during an earlier failover; this is an
16498 		 * opportunity for it to be increased (either as
16499 		 * part of another failover or a failback).
16500 		 */
16501 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
16502 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
16503 			ipif->ipif_saved_mtu = 0;
16504 		} else {
16505 			ipif->ipif_mtu = to_ill->ill_max_mtu;
16506 		}
16507 	}
16508 
16509 	/*
16510 	 * We preserve all the other fields of the ipif including
16511 	 * ipif_saved_ire_mp. The routes that are saved here will
16512 	 * be recreated on the new interface and back on the old
16513 	 * interface when we move back.
16514 	 */
16515 	ASSERT(ipif->ipif_arp_del_mp == NULL);
16516 
16517 	return (err);
16518 }
16519 
16520 static int
16521 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
16522     int ifindex, ipif_t **rep_ipif_ptr)
16523 {
16524 	ipif_t *mipif;
16525 	ipif_t *ipif_next;
16526 	int err;
16527 
16528 	/*
16529 	 * We don't really try to MOVE back things if some of the
16530 	 * operations fail. The daemon will take care of moving again
16531 	 * later on.
16532 	 */
16533 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
16534 		ipif_next = mipif->ipif_next;
16535 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
16536 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
16537 
16538 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
16539 
16540 			/*
16541 			 * When the MOVE fails, it is the job of the
16542 			 * application to take care of this properly
16543 			 * i.e try again if it is ENOMEM.
16544 			 */
16545 			if (mipif->ipif_ill != from_ill) {
16546 				/*
16547 				 * ipif has moved.
16548 				 *
16549 				 * Move the multicast memberships associated
16550 				 * with this ipif to the new ill. For IPv6, we
16551 				 * do it once after all the ipifs are moved
16552 				 * (in ill_move) as they are not associated
16553 				 * with ipifs.
16554 				 *
16555 				 * We need to move the ilms as the ipif has
16556 				 * already been moved to a new ill even
16557 				 * in the case of errors. Neither
16558 				 * ilm_free(ipif) will find the ilm
16559 				 * when somebody unplumbs this ipif nor
16560 				 * ilm_delete(ilm) will be able to find the
16561 				 * ilm, if we don't move now.
16562 				 */
16563 				if (!from_ill->ill_isv6)
16564 					ilm_move_v4(from_ill, to_ill, mipif);
16565 			}
16566 
16567 			if (err != 0)
16568 				return (err);
16569 		}
16570 	}
16571 	return (0);
16572 }
16573 
16574 static int
16575 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
16576 {
16577 	int ifindex;
16578 	int err;
16579 	struct iocblk	*iocp;
16580 	ipif_t	*ipif;
16581 	ipif_t *rep_ipif_ptr = NULL;
16582 	ipif_t	*from_ipif = NULL;
16583 	boolean_t check_rep_if = B_FALSE;
16584 
16585 	iocp = (struct iocblk *)mp->b_rptr;
16586 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
16587 		/*
16588 		 * Move everything pointing at from_ill to to_ill.
16589 		 * We acheive this by passing in 0 as ifindex.
16590 		 */
16591 		ifindex = 0;
16592 	} else {
16593 		/*
16594 		 * Move everything pointing at from_ill whose original
16595 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
16596 		 * We acheive this by passing in ifindex rather than 0.
16597 		 * Multicast vifs, ilgs move implicitly because ipifs move.
16598 		 */
16599 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
16600 		ifindex = to_ill->ill_phyint->phyint_ifindex;
16601 	}
16602 
16603 	/*
16604 	 * Determine if there is at least one ipif that would move from
16605 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
16606 	 * ipif (if it exists) on the to_ill would be consumed as a result of
16607 	 * the move, in which case we need to quiesce the replacement ipif also.
16608 	 */
16609 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
16610 	    from_ipif = from_ipif->ipif_next) {
16611 		if (((ifindex == 0) ||
16612 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
16613 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
16614 			check_rep_if = B_TRUE;
16615 			break;
16616 		}
16617 	}
16618 
16619 
16620 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
16621 
16622 	GRAB_ILL_LOCKS(from_ill, to_ill);
16623 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
16624 		(void) ipsq_pending_mp_add(NULL, ipif, q,
16625 		    mp, ILL_MOVE_OK);
16626 		RELEASE_ILL_LOCKS(from_ill, to_ill);
16627 		return (EINPROGRESS);
16628 	}
16629 
16630 	/* Check if the replacement ipif is quiescent to delete */
16631 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
16632 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
16633 		to_ill->ill_ipif->ipif_state_flags |=
16634 		    IPIF_MOVING | IPIF_CHANGING;
16635 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
16636 			(void) ipsq_pending_mp_add(NULL, ipif, q,
16637 			    mp, ILL_MOVE_OK);
16638 			RELEASE_ILL_LOCKS(from_ill, to_ill);
16639 			return (EINPROGRESS);
16640 		}
16641 	}
16642 	RELEASE_ILL_LOCKS(from_ill, to_ill);
16643 
16644 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
16645 	rw_enter(&ill_g_lock, RW_WRITER);
16646 	GRAB_ILL_LOCKS(from_ill, to_ill);
16647 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
16648 
16649 	/* ilm_move is done inside ipif_move for IPv4 */
16650 	if (err == 0 && from_ill->ill_isv6)
16651 		ilm_move_v6(from_ill, to_ill, ifindex);
16652 
16653 	RELEASE_ILL_LOCKS(from_ill, to_ill);
16654 	rw_exit(&ill_g_lock);
16655 
16656 	/*
16657 	 * send rts messages and multicast messages.
16658 	 */
16659 	if (rep_ipif_ptr != NULL) {
16660 		ip_rts_ifmsg(rep_ipif_ptr);
16661 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
16662 		IPIF_TRACE_CLEANUP(rep_ipif_ptr);
16663 		mi_free(rep_ipif_ptr);
16664 	}
16665 
16666 	ilm_send_multicast_reqs(from_ill, to_ill);
16667 
16668 	conn_move_ill(from_ill, to_ill, ifindex);
16669 
16670 	return (err);
16671 }
16672 
16673 /*
16674  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
16675  * Also checks for the validity of the arguments.
16676  * Note: We are already exclusive inside the from group.
16677  * It is upto the caller to release refcnt on the to_ill's.
16678  */
16679 static int
16680 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
16681     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
16682 {
16683 	int dst_index;
16684 	ipif_t *ipif_v4, *ipif_v6;
16685 	struct lifreq *lifr;
16686 	mblk_t *mp1;
16687 	boolean_t exists;
16688 	sin_t	*sin;
16689 	int	err = 0;
16690 
16691 	if ((mp1 = mp->b_cont) == NULL)
16692 		return (EPROTO);
16693 
16694 	if ((mp1 = mp1->b_cont) == NULL)
16695 		return (EPROTO);
16696 
16697 	lifr = (struct lifreq *)mp1->b_rptr;
16698 	sin = (sin_t *)&lifr->lifr_addr;
16699 
16700 	/*
16701 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
16702 	 * specific operations.
16703 	 */
16704 	if (sin->sin_family != AF_UNSPEC)
16705 		return (EINVAL);
16706 
16707 	/*
16708 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
16709 	 * NULLs for the last 4 args and we know the lookup won't fail
16710 	 * with EINPROGRESS.
16711 	 */
16712 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
16713 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
16714 	    ALL_ZONES, NULL, NULL, NULL, NULL);
16715 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
16716 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
16717 	    ALL_ZONES, NULL, NULL, NULL, NULL);
16718 
16719 	if (ipif_v4 == NULL && ipif_v6 == NULL)
16720 		return (ENXIO);
16721 
16722 	if (ipif_v4 != NULL) {
16723 		ASSERT(ipif_v4->ipif_refcnt != 0);
16724 		if (ipif_v4->ipif_id != 0) {
16725 			err = EINVAL;
16726 			goto done;
16727 		}
16728 
16729 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
16730 		*ill_from_v4 = ipif_v4->ipif_ill;
16731 	}
16732 
16733 	if (ipif_v6 != NULL) {
16734 		ASSERT(ipif_v6->ipif_refcnt != 0);
16735 		if (ipif_v6->ipif_id != 0) {
16736 			err = EINVAL;
16737 			goto done;
16738 		}
16739 
16740 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
16741 		*ill_from_v6 = ipif_v6->ipif_ill;
16742 	}
16743 
16744 	err = 0;
16745 	dst_index = lifr->lifr_movetoindex;
16746 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
16747 	    q, mp, ip_process_ioctl, &err);
16748 	if (err != 0) {
16749 		/*
16750 		 * There could be only v6.
16751 		 */
16752 		if (err != ENXIO)
16753 			goto done;
16754 		err = 0;
16755 	}
16756 
16757 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
16758 	    q, mp, ip_process_ioctl, &err);
16759 	if (err != 0) {
16760 		if (err != ENXIO)
16761 			goto done;
16762 		if (*ill_to_v4 == NULL) {
16763 			err = ENXIO;
16764 			goto done;
16765 		}
16766 		err = 0;
16767 	}
16768 
16769 	/*
16770 	 * If we have something to MOVE i.e "from" not NULL,
16771 	 * "to" should be non-NULL.
16772 	 */
16773 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
16774 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
16775 		err = EINVAL;
16776 	}
16777 
16778 done:
16779 	if (ipif_v4 != NULL)
16780 		ipif_refrele(ipif_v4);
16781 	if (ipif_v6 != NULL)
16782 		ipif_refrele(ipif_v6);
16783 	return (err);
16784 }
16785 
16786 /*
16787  * FAILOVER and FAILBACK are modelled as MOVE operations.
16788  *
16789  * We don't check whether the MOVE is within the same group or
16790  * not, because this ioctl can be used as a generic mechanism
16791  * to failover from interface A to B, though things will function
16792  * only if they are really part of the same group. Moreover,
16793  * all ipifs may be down and hence temporarily out of the group.
16794  *
16795  * ipif's that need to be moved are first brought down; V4 ipifs are brought
16796  * down first and then V6.  For each we wait for the ipif's to become quiescent.
16797  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
16798  * have been deleted and there are no active references. Once quiescent the
16799  * ipif's are moved and brought up on the new ill.
16800  *
16801  * Normally the source ill and destination ill belong to the same IPMP group
16802  * and hence the same ipsq_t. In the event they don't belong to the same
16803  * same group the two ipsq's are first merged into one ipsq - that of the
16804  * to_ill. The multicast memberships on the source and destination ill cannot
16805  * change during the move operation since multicast joins/leaves also have to
16806  * execute on the same ipsq and are hence serialized.
16807  */
16808 /* ARGSUSED */
16809 int
16810 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16811     ip_ioctl_cmd_t *ipip, void *ifreq)
16812 {
16813 	ill_t *ill_to_v4 = NULL;
16814 	ill_t *ill_to_v6 = NULL;
16815 	ill_t *ill_from_v4 = NULL;
16816 	ill_t *ill_from_v6 = NULL;
16817 	int err = 0;
16818 
16819 	/*
16820 	 * setup from and to ill's, we can get EINPROGRESS only for
16821 	 * to_ill's.
16822 	 */
16823 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
16824 	    &ill_to_v4, &ill_to_v6);
16825 
16826 	if (err != 0) {
16827 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
16828 		goto done;
16829 	}
16830 
16831 	/*
16832 	 * nothing to do.
16833 	 */
16834 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
16835 		goto done;
16836 	}
16837 
16838 	/*
16839 	 * nothing to do.
16840 	 */
16841 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
16842 		goto done;
16843 	}
16844 
16845 	/*
16846 	 * Mark the ill as changing.
16847 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
16848 	 * in ill_up_ipifs in case of error they are cleared below.
16849 	 */
16850 
16851 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
16852 	if (ill_from_v4 != NULL)
16853 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
16854 	if (ill_from_v6 != NULL)
16855 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
16856 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
16857 
16858 	/*
16859 	 * Make sure that both src and dst are
16860 	 * in the same syncq group. If not make it happen.
16861 	 * We are not holding any locks because we are the writer
16862 	 * on the from_ipsq and we will hold locks in ill_merge_groups
16863 	 * to protect to_ipsq against changing.
16864 	 */
16865 	if (ill_from_v4 != NULL) {
16866 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
16867 		    ill_to_v4->ill_phyint->phyint_ipsq) {
16868 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
16869 			    NULL, mp, q);
16870 			goto err_ret;
16871 
16872 		}
16873 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
16874 	} else {
16875 
16876 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
16877 		    ill_to_v6->ill_phyint->phyint_ipsq) {
16878 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
16879 			    NULL, mp, q);
16880 			goto err_ret;
16881 
16882 		}
16883 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
16884 	}
16885 
16886 	/*
16887 	 * Now that the ipsq's have been merged and we are the writer
16888 	 * lets mark to_ill as changing as well.
16889 	 */
16890 
16891 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
16892 	if (ill_to_v4 != NULL)
16893 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
16894 	if (ill_to_v6 != NULL)
16895 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
16896 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
16897 
16898 	/*
16899 	 * Its ok for us to proceed with the move even if
16900 	 * ill_pending_mp is non null on one of the from ill's as the reply
16901 	 * should not be looking at the ipif, it should only care about the
16902 	 * ill itself.
16903 	 */
16904 
16905 	/*
16906 	 * lets move ipv4 first.
16907 	 */
16908 	if (ill_from_v4 != NULL) {
16909 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
16910 		ill_from_v4->ill_move_in_progress = B_TRUE;
16911 		ill_to_v4->ill_move_in_progress = B_TRUE;
16912 		ill_to_v4->ill_move_peer = ill_from_v4;
16913 		ill_from_v4->ill_move_peer = ill_to_v4;
16914 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
16915 	}
16916 
16917 	/*
16918 	 * Now lets move ipv6.
16919 	 */
16920 	if (err == 0 && ill_from_v6 != NULL) {
16921 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
16922 		ill_from_v6->ill_move_in_progress = B_TRUE;
16923 		ill_to_v6->ill_move_in_progress = B_TRUE;
16924 		ill_to_v6->ill_move_peer = ill_from_v6;
16925 		ill_from_v6->ill_move_peer = ill_to_v6;
16926 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
16927 	}
16928 
16929 err_ret:
16930 	/*
16931 	 * EINPROGRESS means we are waiting for the ipif's that need to be
16932 	 * moved to become quiescent.
16933 	 */
16934 	if (err == EINPROGRESS) {
16935 		goto done;
16936 	}
16937 
16938 	/*
16939 	 * if err is set ill_up_ipifs will not be called
16940 	 * lets clear the flags.
16941 	 */
16942 
16943 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
16944 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
16945 	/*
16946 	 * Some of the clearing may be redundant. But it is simple
16947 	 * not making any extra checks.
16948 	 */
16949 	if (ill_from_v6 != NULL) {
16950 		ill_from_v6->ill_move_in_progress = B_FALSE;
16951 		ill_from_v6->ill_move_peer = NULL;
16952 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
16953 	}
16954 	if (ill_from_v4 != NULL) {
16955 		ill_from_v4->ill_move_in_progress = B_FALSE;
16956 		ill_from_v4->ill_move_peer = NULL;
16957 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
16958 	}
16959 	if (ill_to_v6 != NULL) {
16960 		ill_to_v6->ill_move_in_progress = B_FALSE;
16961 		ill_to_v6->ill_move_peer = NULL;
16962 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
16963 	}
16964 	if (ill_to_v4 != NULL) {
16965 		ill_to_v4->ill_move_in_progress = B_FALSE;
16966 		ill_to_v4->ill_move_peer = NULL;
16967 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
16968 	}
16969 
16970 	/*
16971 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
16972 	 * Do this always to maintain proper state i.e even in case of errors.
16973 	 * As phyint_inactive looks at both v4 and v6 interfaces,
16974 	 * we need not call on both v4 and v6 interfaces.
16975 	 */
16976 	if (ill_from_v4 != NULL) {
16977 		if ((ill_from_v4->ill_phyint->phyint_flags &
16978 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
16979 			phyint_inactive(ill_from_v4->ill_phyint);
16980 		}
16981 	} else if (ill_from_v6 != NULL) {
16982 		if ((ill_from_v6->ill_phyint->phyint_flags &
16983 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
16984 			phyint_inactive(ill_from_v6->ill_phyint);
16985 		}
16986 	}
16987 
16988 	if (ill_to_v4 != NULL) {
16989 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
16990 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
16991 		}
16992 	} else if (ill_to_v6 != NULL) {
16993 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
16994 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
16995 		}
16996 	}
16997 
16998 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
16999 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17000 
17001 no_err:
17002 	/*
17003 	 * lets bring the interfaces up on the to_ill.
17004 	 */
17005 	if (err == 0) {
17006 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
17007 		    q, mp);
17008 	}
17009 done:
17010 
17011 	if (ill_to_v4 != NULL) {
17012 		ill_refrele(ill_to_v4);
17013 	}
17014 	if (ill_to_v6 != NULL) {
17015 		ill_refrele(ill_to_v6);
17016 	}
17017 
17018 	return (err);
17019 }
17020 
17021 static void
17022 ill_dl_down(ill_t *ill)
17023 {
17024 	/*
17025 	 * The ill is down; unbind but stay attached since we're still
17026 	 * associated with a PPA.
17027 	 */
17028 	mblk_t	*mp = ill->ill_unbind_mp;
17029 
17030 	ill->ill_unbind_mp = NULL;
17031 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
17032 	if (mp != NULL) {
17033 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
17034 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
17035 		    ill->ill_name));
17036 		ill_dlpi_send(ill, mp);
17037 	}
17038 
17039 	/*
17040 	 * Toss all of our multicast memberships.  We could keep them, but
17041 	 * then we'd have to do bookkeeping of any joins and leaves performed
17042 	 * by the application while the the interface is down (we can't just
17043 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
17044 	 * on a downed interface).
17045 	 */
17046 	ill_leave_multicast(ill);
17047 
17048 	mutex_enter(&ill->ill_lock);
17049 	ill->ill_dl_up = 0;
17050 	mutex_exit(&ill->ill_lock);
17051 }
17052 
17053 void
17054 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
17055 {
17056 	union DL_primitives *dlp;
17057 	t_uscalar_t prim;
17058 
17059 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
17060 
17061 	dlp = (union DL_primitives *)mp->b_rptr;
17062 	prim = dlp->dl_primitive;
17063 
17064 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
17065 		dlpi_prim_str(prim), prim, ill->ill_name));
17066 
17067 	switch (prim) {
17068 	case DL_PHYS_ADDR_REQ:
17069 	{
17070 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
17071 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
17072 		break;
17073 	}
17074 	case DL_BIND_REQ:
17075 		mutex_enter(&ill->ill_lock);
17076 		ill->ill_state_flags &= ~ILL_DL_UNBIND_DONE;
17077 		mutex_exit(&ill->ill_lock);
17078 		break;
17079 	}
17080 
17081 	ill->ill_dlpi_pending = prim;
17082 
17083 	/*
17084 	 * Some drivers send M_FLUSH up to IP as part of unbind
17085 	 * request.  When this M_FLUSH is sent back to the driver,
17086 	 * this can go after we send the detach request if the
17087 	 * M_FLUSH ends up in IP's syncq. To avoid that, we reply
17088 	 * to the M_FLUSH in ip_rput and locally generate another
17089 	 * M_FLUSH for the correctness.  This will get freed in
17090 	 * ip_wput_nondata.
17091 	 */
17092 	if (prim == DL_UNBIND_REQ)
17093 		(void) putnextctl1(ill->ill_rq, M_FLUSH, FLUSHRW);
17094 
17095 	putnext(ill->ill_wq, mp);
17096 }
17097 
17098 /*
17099  * Send a DLPI control message to the driver but make sure there
17100  * is only one outstanding message. Uses ill_dlpi_pending to tell
17101  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
17102  * when an ACK or a NAK is received to process the next queued message.
17103  *
17104  * We don't protect ill_dlpi_pending with any lock. This is okay as
17105  * every place where its accessed, ip is exclusive while accessing
17106  * ill_dlpi_pending except when this function is called from ill_init()
17107  */
17108 void
17109 ill_dlpi_send(ill_t *ill, mblk_t *mp)
17110 {
17111 	mblk_t **mpp;
17112 
17113 	ASSERT(IAM_WRITER_ILL(ill));
17114 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
17115 
17116 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
17117 		/* Must queue message. Tail insertion */
17118 		mpp = &ill->ill_dlpi_deferred;
17119 		while (*mpp != NULL)
17120 			mpp = &((*mpp)->b_next);
17121 
17122 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
17123 		    ill->ill_name));
17124 
17125 		*mpp = mp;
17126 		return;
17127 	}
17128 
17129 	ill_dlpi_dispatch(ill, mp);
17130 }
17131 
17132 /*
17133  * Called when an DLPI control message has been acked or nacked to
17134  * send down the next queued message (if any).
17135  */
17136 void
17137 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
17138 {
17139 	mblk_t *mp;
17140 
17141 	ASSERT(IAM_WRITER_ILL(ill));
17142 
17143 	ASSERT(prim != DL_PRIM_INVAL);
17144 	if (ill->ill_dlpi_pending != prim) {
17145 		if (ill->ill_dlpi_pending == DL_PRIM_INVAL) {
17146 			(void) mi_strlog(ill->ill_rq, 1,
17147 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
17148 			    "ill_dlpi_done: unsolicited ack for %s from %s\n",
17149 			    dlpi_prim_str(prim), ill->ill_name);
17150 		} else {
17151 			(void) mi_strlog(ill->ill_rq, 1,
17152 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
17153 			    "ill_dlpi_done: unexpected ack for %s from %s "
17154 			    "(expecting ack for %s)\n",
17155 			    dlpi_prim_str(prim), ill->ill_name,
17156 			    dlpi_prim_str(ill->ill_dlpi_pending));
17157 		}
17158 		return;
17159 	}
17160 
17161 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
17162 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
17163 
17164 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
17165 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
17166 		return;
17167 	}
17168 
17169 	ill->ill_dlpi_deferred = mp->b_next;
17170 	mp->b_next = NULL;
17171 
17172 	ill_dlpi_dispatch(ill, mp);
17173 }
17174 
17175 void
17176 conn_delete_ire(conn_t *connp, caddr_t arg)
17177 {
17178 	ipif_t	*ipif = (ipif_t *)arg;
17179 	ire_t	*ire;
17180 
17181 	/*
17182 	 * Look at the cached ires on conns which has pointers to ipifs.
17183 	 * We just call ire_refrele which clears up the reference
17184 	 * to ire. Called when a conn closes. Also called from ipif_free
17185 	 * to cleanup indirect references to the stale ipif via the cached ire.
17186 	 */
17187 	mutex_enter(&connp->conn_lock);
17188 	ire = connp->conn_ire_cache;
17189 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
17190 		connp->conn_ire_cache = NULL;
17191 		mutex_exit(&connp->conn_lock);
17192 		IRE_REFRELE_NOTR(ire);
17193 		return;
17194 	}
17195 	mutex_exit(&connp->conn_lock);
17196 
17197 }
17198 
17199 /*
17200  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
17201  * of IREs. Those IREs may have been previously cached in the conn structure.
17202  * This ipcl_walk() walker function releases all references to such IREs based
17203  * on the condemned flag.
17204  */
17205 /* ARGSUSED */
17206 void
17207 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
17208 {
17209 	ire_t	*ire;
17210 
17211 	mutex_enter(&connp->conn_lock);
17212 	ire = connp->conn_ire_cache;
17213 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
17214 		connp->conn_ire_cache = NULL;
17215 		mutex_exit(&connp->conn_lock);
17216 		IRE_REFRELE_NOTR(ire);
17217 		return;
17218 	}
17219 	mutex_exit(&connp->conn_lock);
17220 }
17221 
17222 /*
17223  * Take down a specific interface, but don't lose any information about it.
17224  * Also delete interface from its interface group (ifgrp).
17225  * (Always called as writer.)
17226  * This function goes through the down sequence even if the interface is
17227  * already down. There are 2 reasons.
17228  * a. Currently we permit interface routes that depend on down interfaces
17229  *    to be added. This behaviour itself is questionable. However it appears
17230  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
17231  *    time. We go thru the cleanup in order to remove these routes.
17232  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
17233  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
17234  *    down, but we need to cleanup i.e. do ill_dl_down and
17235  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
17236  *
17237  * IP-MT notes:
17238  *
17239  * Model of reference to interfaces.
17240  *
17241  * The following members in ipif_t track references to the ipif.
17242  *	int     ipif_refcnt;    Active reference count
17243  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
17244  * The following members in ill_t track references to the ill.
17245  *	int             ill_refcnt;     active refcnt
17246  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
17247  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
17248  *
17249  * Reference to an ipif or ill can be obtained in any of the following ways.
17250  *
17251  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
17252  * Pointers to ipif / ill from other data structures viz ire and conn.
17253  * Implicit reference to the ipif / ill by holding a reference to the ire.
17254  *
17255  * The ipif/ill lookup functions return a reference held ipif / ill.
17256  * ipif_refcnt and ill_refcnt track the reference counts respectively.
17257  * This is a purely dynamic reference count associated with threads holding
17258  * references to the ipif / ill. Pointers from other structures do not
17259  * count towards this reference count.
17260  *
17261  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
17262  * ipif/ill. This is incremented whenever a new ire is created referencing the
17263  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
17264  * actually added to the ire hash table. The count is decremented in
17265  * ire_inactive where the ire is destroyed.
17266  *
17267  * nce's reference ill's thru nce_ill and the count of nce's associated with
17268  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
17269  * ndp_add() where the nce is actually added to the table. Similarly it is
17270  * decremented in ndp_inactive where the nce is destroyed.
17271  *
17272  * Flow of ioctls involving interface down/up
17273  *
17274  * The following is the sequence of an attempt to set some critical flags on an
17275  * up interface.
17276  * ip_sioctl_flags
17277  * ipif_down
17278  * wait for ipif to be quiescent
17279  * ipif_down_tail
17280  * ip_sioctl_flags_tail
17281  *
17282  * All set ioctls that involve down/up sequence would have a skeleton similar
17283  * to the above. All the *tail functions are called after the refcounts have
17284  * dropped to the appropriate values.
17285  *
17286  * The mechanism to quiesce an ipif is as follows.
17287  *
17288  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
17289  * on the ipif. Callers either pass a flag requesting wait or the lookup
17290  *  functions will return NULL.
17291  *
17292  * Delete all ires referencing this ipif
17293  *
17294  * Any thread attempting to do an ipif_refhold on an ipif that has been
17295  * obtained thru a cached pointer will first make sure that
17296  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
17297  * increment the refcount.
17298  *
17299  * The above guarantees that the ipif refcount will eventually come down to
17300  * zero and the ipif will quiesce, once all threads that currently hold a
17301  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
17302  * ipif_refcount has dropped to zero and all ire's associated with this ipif
17303  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
17304  * drop to zero.
17305  *
17306  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
17307  *
17308  * Threads trying to lookup an ipif or ill can pass a flag requesting
17309  * wait and restart if the ipif / ill cannot be looked up currently.
17310  * For eg. bind, and route operations (Eg. route add / delete) cannot return
17311  * failure if the ipif is currently undergoing an exclusive operation, and
17312  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
17313  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
17314  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
17315  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
17316  * change while the ill_lock is held. Before dropping the ill_lock we acquire
17317  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
17318  * until we release the ipsq_lock, even though the the ill/ipif state flags
17319  * can change after we drop the ill_lock.
17320  *
17321  * An attempt to send out a packet using an ipif that is currently
17322  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
17323  * operation and restart it later when the exclusive condition on the ipif ends.
17324  * This is an example of not passing the wait flag to the lookup functions. For
17325  * example an attempt to refhold and use conn->conn_multicast_ipif and send
17326  * out a multicast packet on that ipif will fail while the ipif is
17327  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
17328  * currently IPIF_CHANGING will also fail.
17329  */
17330 int
17331 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
17332 {
17333 	ill_t		*ill = ipif->ipif_ill;
17334 	phyint_t	*phyi;
17335 	conn_t		*connp;
17336 	boolean_t	success;
17337 	boolean_t	ipif_was_up = B_FALSE;
17338 
17339 	ASSERT(IAM_WRITER_IPIF(ipif));
17340 
17341 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
17342 
17343 	if (ipif->ipif_flags & IPIF_UP) {
17344 		mutex_enter(&ill->ill_lock);
17345 		ipif->ipif_flags &= ~IPIF_UP;
17346 		ASSERT(ill->ill_ipif_up_count > 0);
17347 		--ill->ill_ipif_up_count;
17348 		mutex_exit(&ill->ill_lock);
17349 		ipif_was_up = B_TRUE;
17350 		/* Update status in SCTP's list */
17351 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
17352 	}
17353 
17354 	/*
17355 	 * Blow away v6 memberships we established in ipif_multicast_up(); the
17356 	 * v4 ones are left alone (as is the ipif_multicast_up flag, so we
17357 	 * know not to rejoin when the interface is brought back up).
17358 	 */
17359 	if (ipif->ipif_isv6)
17360 		ipif_multicast_down(ipif);
17361 	/*
17362 	 * Remove from the mapping for __sin6_src_id. We insert only
17363 	 * when the address is not INADDR_ANY. As IPv4 addresses are
17364 	 * stored as mapped addresses, we need to check for mapped
17365 	 * INADDR_ANY also.
17366 	 */
17367 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
17368 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
17369 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
17370 		int err;
17371 
17372 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
17373 		    ipif->ipif_zoneid);
17374 		if (err != 0) {
17375 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
17376 		}
17377 	}
17378 
17379 	/*
17380 	 * Before we delete the ill from the group (if any), we need
17381 	 * to make sure that we delete all the routes dependent on
17382 	 * this and also any ipifs dependent on this ipif for
17383 	 * source address. We need to do before we delete from
17384 	 * the group because
17385 	 *
17386 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
17387 	 *
17388 	 * 2) ipif_update_other_ipifs needs to walk the whole group
17389 	 *    for re-doing source address selection. Note that
17390 	 *    ipif_select_source[_v6] called from
17391 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
17392 	 *    because we have already marked down here i.e cleared
17393 	 *    IPIF_UP.
17394 	 */
17395 	if (ipif->ipif_isv6)
17396 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES);
17397 	else
17398 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES);
17399 
17400 	/*
17401 	 * Need to add these also to be saved and restored when the
17402 	 * ipif is brought down and up
17403 	 */
17404 	mutex_enter(&ire_mrtun_lock);
17405 	if (ire_mrtun_count != 0) {
17406 		mutex_exit(&ire_mrtun_lock);
17407 		ire_walk_ill_mrtun(0, 0, ipif_down_delete_ire,
17408 		    (char *)ipif, NULL);
17409 	} else {
17410 		mutex_exit(&ire_mrtun_lock);
17411 	}
17412 
17413 	mutex_enter(&ire_srcif_table_lock);
17414 	if (ire_srcif_table_count > 0) {
17415 		mutex_exit(&ire_srcif_table_lock);
17416 		ire_walk_srcif_table_v4(ipif_down_delete_ire, (char *)ipif);
17417 	} else {
17418 		mutex_exit(&ire_srcif_table_lock);
17419 	}
17420 
17421 	/*
17422 	 * Cleaning up the conn_ire_cache or conns must be done only after the
17423 	 * ires have been deleted above. Otherwise a thread could end up
17424 	 * caching an ire in a conn after we have finished the cleanup of the
17425 	 * conn. The caching is done after making sure that the ire is not yet
17426 	 * condemned. Also documented in the block comment above ip_output
17427 	 */
17428 	ipcl_walk(conn_cleanup_stale_ire, NULL);
17429 	/* Also, delete the ires cached in SCTP */
17430 	sctp_ire_cache_flush(ipif);
17431 
17432 	/* Resolve any IPsec/IKE NAT-T instances that depend on this ipif. */
17433 	nattymod_clean_ipif(ipif);
17434 
17435 	/*
17436 	 * Update any other ipifs which have used "our" local address as
17437 	 * a source address. This entails removing and recreating IRE_INTERFACE
17438 	 * entries for such ipifs.
17439 	 */
17440 	if (ipif->ipif_isv6)
17441 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
17442 	else
17443 		ipif_update_other_ipifs(ipif, ill->ill_group);
17444 
17445 	if (ipif_was_up) {
17446 		/*
17447 		 * Check whether it is last ipif to leave this group.
17448 		 * If this is the last ipif to leave, we should remove
17449 		 * this ill from the group as ipif_select_source will not
17450 		 * be able to find any useful ipifs if this ill is selected
17451 		 * for load balancing.
17452 		 *
17453 		 * For nameless groups, we should call ifgrp_delete if this
17454 		 * belongs to some group. As this ipif is going down, we may
17455 		 * need to reconstruct groups.
17456 		 */
17457 		phyi = ill->ill_phyint;
17458 		/*
17459 		 * If the phyint_groupname_len is 0, it may or may not
17460 		 * be in the nameless group. If the phyint_groupname_len is
17461 		 * not 0, then this ill should be part of some group.
17462 		 * As we always insert this ill in the group if
17463 		 * phyint_groupname_len is not zero when the first ipif
17464 		 * comes up (in ipif_up_done), it should be in a group
17465 		 * when the namelen is not 0.
17466 		 *
17467 		 * NOTE : When we delete the ill from the group,it will
17468 		 * blow away all the IRE_CACHES pointing either at this ipif or
17469 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
17470 		 * should be pointing at this ill.
17471 		 */
17472 		ASSERT(phyi->phyint_groupname_len == 0 ||
17473 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
17474 
17475 		if (phyi->phyint_groupname_len != 0) {
17476 			if (ill->ill_ipif_up_count == 0)
17477 				illgrp_delete(ill);
17478 		}
17479 
17480 		/*
17481 		 * If we have deleted some of the broadcast ires associated
17482 		 * with this ipif, we need to re-nominate somebody else if
17483 		 * the ires that we deleted were the nominated ones.
17484 		 */
17485 		if (ill->ill_group != NULL && !ill->ill_isv6)
17486 			ipif_renominate_bcast(ipif);
17487 	}
17488 
17489 	if (ipif->ipif_isv6)
17490 		ipif_ndp_down(ipif);
17491 
17492 	/*
17493 	 * If mp is NULL the caller will wait for the appropriate refcnt.
17494 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
17495 	 * and ill_delete -> ipif_free -> ipif_down
17496 	 */
17497 	if (mp == NULL) {
17498 		ASSERT(q == NULL);
17499 		return (0);
17500 	}
17501 
17502 	if (CONN_Q(q)) {
17503 		connp = Q_TO_CONN(q);
17504 		mutex_enter(&connp->conn_lock);
17505 	} else {
17506 		connp = NULL;
17507 	}
17508 	mutex_enter(&ill->ill_lock);
17509 	/*
17510 	 * Are there any ire's pointing to this ipif that are still active ?
17511 	 * If this is the last ipif going down, are there any ire's pointing
17512 	 * to this ill that are still active ?
17513 	 */
17514 	if (ipif_is_quiescent(ipif)) {
17515 		mutex_exit(&ill->ill_lock);
17516 		if (connp != NULL)
17517 			mutex_exit(&connp->conn_lock);
17518 		return (0);
17519 	}
17520 
17521 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
17522 	    ill->ill_name, (void *)ill));
17523 	/*
17524 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
17525 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
17526 	 * which in turn is called by the last refrele on the ipif/ill/ire.
17527 	 */
17528 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
17529 	if (!success) {
17530 		/* The conn is closing. So just return */
17531 		ASSERT(connp != NULL);
17532 		mutex_exit(&ill->ill_lock);
17533 		mutex_exit(&connp->conn_lock);
17534 		return (EINTR);
17535 	}
17536 
17537 	mutex_exit(&ill->ill_lock);
17538 	if (connp != NULL)
17539 		mutex_exit(&connp->conn_lock);
17540 	return (EINPROGRESS);
17541 }
17542 
17543 static void
17544 ipif_down_tail(ipif_t *ipif)
17545 {
17546 	ill_t	*ill = ipif->ipif_ill;
17547 
17548 	/*
17549 	 * Skip any loopback interface (null wq).
17550 	 * If this is the last logical interface on the ill
17551 	 * have ill_dl_down tell the driver we are gone (unbind)
17552 	 * Note that lun 0 can ipif_down even though
17553 	 * there are other logical units that are up.
17554 	 * This occurs e.g. when we change a "significant" IFF_ flag.
17555 	 */
17556 	if (ipif->ipif_ill->ill_wq != NULL) {
17557 		if (!ill->ill_logical_down && (ill->ill_ipif_up_count == 0) &&
17558 		    ill->ill_dl_up) {
17559 			ill_dl_down(ill);
17560 		}
17561 	}
17562 	ill->ill_logical_down = 0;
17563 
17564 	/*
17565 	 * Have to be after removing the routes in ipif_down_delete_ire.
17566 	 */
17567 	if (ipif->ipif_isv6) {
17568 		if (ipif->ipif_ill->ill_flags & ILLF_XRESOLV)
17569 			ipif_arp_down(ipif);
17570 	} else {
17571 		ipif_arp_down(ipif);
17572 	}
17573 
17574 	ip_rts_ifmsg(ipif);
17575 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
17576 }
17577 
17578 /*
17579  * Bring interface logically down without bringing the physical interface
17580  * down e.g. when the netmask is changed. This avoids long lasting link
17581  * negotiations between an ethernet interface and a certain switches.
17582  */
17583 static int
17584 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
17585 {
17586 	/*
17587 	 * The ill_logical_down flag is a transient flag. It is set here
17588 	 * and is cleared once the down has completed in ipif_down_tail.
17589 	 * This flag does not indicate whether the ill stream is in the
17590 	 * DL_BOUND state with the driver. Instead this flag is used by
17591 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
17592 	 * the driver. The state of the ill stream i.e. whether it is
17593 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
17594 	 */
17595 	ipif->ipif_ill->ill_logical_down = 1;
17596 	return (ipif_down(ipif, q, mp));
17597 }
17598 
17599 /*
17600  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
17601  * If the usesrc client ILL is already part of a usesrc group or not,
17602  * in either case a ire_stq with the matching usesrc client ILL will
17603  * locate the IRE's that need to be deleted. We want IREs to be created
17604  * with the new source address.
17605  */
17606 static void
17607 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
17608 {
17609 	ill_t	*ucill = (ill_t *)ill_arg;
17610 
17611 	ASSERT(IAM_WRITER_ILL(ucill));
17612 
17613 	if (ire->ire_stq == NULL)
17614 		return;
17615 
17616 	if ((ire->ire_type == IRE_CACHE) &&
17617 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
17618 		ire_delete(ire);
17619 }
17620 
17621 /*
17622  * ire_walk routine to delete every IRE dependent on the interface
17623  * address that is going down.	(Always called as writer.)
17624  * Works for both v4 and v6.
17625  * In addition for checking for ire_ipif matches it also checks for
17626  * IRE_CACHE entries which have the same source address as the
17627  * disappearing ipif since ipif_select_source might have picked
17628  * that source. Note that ipif_down/ipif_update_other_ipifs takes
17629  * care of any IRE_INTERFACE with the disappearing source address.
17630  */
17631 static void
17632 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
17633 {
17634 	ipif_t	*ipif = (ipif_t *)ipif_arg;
17635 	ill_t *ire_ill;
17636 	ill_t *ipif_ill;
17637 
17638 	ASSERT(IAM_WRITER_IPIF(ipif));
17639 	if (ire->ire_ipif == NULL)
17640 		return;
17641 
17642 	/*
17643 	 * For IPv4, we derive source addresses for an IRE from ipif's
17644 	 * belonging to the same IPMP group as the IRE's outgoing
17645 	 * interface.  If an IRE's outgoing interface isn't in the
17646 	 * same IPMP group as a particular ipif, then that ipif
17647 	 * couldn't have been used as a source address for this IRE.
17648 	 *
17649 	 * For IPv6, source addresses are only restricted to the IPMP group
17650 	 * if the IRE is for a link-local address or a multicast address.
17651 	 * Otherwise, source addresses for an IRE can be chosen from
17652 	 * interfaces other than the the outgoing interface for that IRE.
17653 	 *
17654 	 * For source address selection details, see ipif_select_source()
17655 	 * and ipif_select_source_v6().
17656 	 */
17657 	if (ire->ire_ipversion == IPV4_VERSION ||
17658 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
17659 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
17660 		ire_ill = ire->ire_ipif->ipif_ill;
17661 		ipif_ill = ipif->ipif_ill;
17662 
17663 		if (ire_ill->ill_group != ipif_ill->ill_group) {
17664 			return;
17665 		}
17666 	}
17667 
17668 
17669 	if (ire->ire_ipif != ipif) {
17670 		/*
17671 		 * Look for a matching source address.
17672 		 */
17673 		if (ire->ire_type != IRE_CACHE)
17674 			return;
17675 		if (ipif->ipif_flags & IPIF_NOLOCAL)
17676 			return;
17677 
17678 		if (ire->ire_ipversion == IPV4_VERSION) {
17679 			if (ire->ire_src_addr != ipif->ipif_src_addr)
17680 				return;
17681 		} else {
17682 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
17683 			    &ipif->ipif_v6lcl_addr))
17684 				return;
17685 		}
17686 		ire_delete(ire);
17687 		return;
17688 	}
17689 	/*
17690 	 * ire_delete() will do an ire_flush_cache which will delete
17691 	 * all ire_ipif matches
17692 	 */
17693 	ire_delete(ire);
17694 }
17695 
17696 /*
17697  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
17698  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
17699  * 2) when an interface is brought up or down (on that ill).
17700  * This ensures that the IRE_CACHE entries don't retain stale source
17701  * address selection results.
17702  */
17703 void
17704 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
17705 {
17706 	ill_t	*ill = (ill_t *)ill_arg;
17707 	ill_t	*ipif_ill;
17708 
17709 	ASSERT(IAM_WRITER_ILL(ill));
17710 	/*
17711 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
17712 	 * Hence this should be IRE_CACHE.
17713 	 */
17714 	ASSERT(ire->ire_type == IRE_CACHE);
17715 
17716 	/*
17717 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
17718 	 * We are only interested in IRE_CACHES that has borrowed
17719 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
17720 	 * for which we need to look at ire_ipif->ipif_ill match
17721 	 * with ill.
17722 	 */
17723 	ASSERT(ire->ire_ipif != NULL);
17724 	ipif_ill = ire->ire_ipif->ipif_ill;
17725 	if (ipif_ill == ill || (ill->ill_group != NULL &&
17726 	    ipif_ill->ill_group == ill->ill_group)) {
17727 		ire_delete(ire);
17728 	}
17729 }
17730 
17731 /*
17732  * Delete all the ire whose stq references ill_arg.
17733  */
17734 static void
17735 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
17736 {
17737 	ill_t	*ill = (ill_t *)ill_arg;
17738 	ill_t	*ire_ill;
17739 
17740 	ASSERT(IAM_WRITER_ILL(ill));
17741 	/*
17742 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
17743 	 * Hence this should be IRE_CACHE.
17744 	 */
17745 	ASSERT(ire->ire_type == IRE_CACHE);
17746 
17747 	/*
17748 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
17749 	 * matches ill. We are only interested in IRE_CACHES that
17750 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
17751 	 * filtering here.
17752 	 */
17753 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
17754 
17755 	if (ire_ill == ill)
17756 		ire_delete(ire);
17757 }
17758 
17759 /*
17760  * This is called when an ill leaves the group. We want to delete
17761  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
17762  * pointing at ill.
17763  */
17764 static void
17765 illgrp_cache_delete(ire_t *ire, char *ill_arg)
17766 {
17767 	ill_t	*ill = (ill_t *)ill_arg;
17768 
17769 	ASSERT(IAM_WRITER_ILL(ill));
17770 	ASSERT(ill->ill_group == NULL);
17771 	/*
17772 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
17773 	 * Hence this should be IRE_CACHE.
17774 	 */
17775 	ASSERT(ire->ire_type == IRE_CACHE);
17776 	/*
17777 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
17778 	 * matches ill. We are interested in both.
17779 	 */
17780 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
17781 	    (ire->ire_ipif->ipif_ill == ill));
17782 
17783 	ire_delete(ire);
17784 }
17785 
17786 /*
17787  * Initiate deallocate of an IPIF. Always called as writer. Called by
17788  * ill_delete or ip_sioctl_removeif.
17789  */
17790 static void
17791 ipif_free(ipif_t *ipif)
17792 {
17793 	ASSERT(IAM_WRITER_IPIF(ipif));
17794 
17795 	/* Remove conn references */
17796 	reset_conn_ipif(ipif);
17797 
17798 	/*
17799 	 * Make sure we have valid net and subnet broadcast ire's for the
17800 	 * other ipif's which share them with this ipif.
17801 	 */
17802 	if (!ipif->ipif_isv6)
17803 		ipif_check_bcast_ires(ipif);
17804 
17805 	/*
17806 	 * Take down the interface. We can be called either from ill_delete
17807 	 * or from ip_sioctl_removeif.
17808 	 */
17809 	(void) ipif_down(ipif, NULL, NULL);
17810 
17811 	rw_enter(&ill_g_lock, RW_WRITER);
17812 	/* Remove pointers to this ill in the multicast routing tables */
17813 	reset_mrt_vif_ipif(ipif);
17814 	rw_exit(&ill_g_lock);
17815 }
17816 
17817 static void
17818 ipif_free_tail(ipif_t *ipif)
17819 {
17820 	mblk_t	*mp;
17821 	ipif_t	**ipifp;
17822 
17823 	/*
17824 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
17825 	 */
17826 	mutex_enter(&ipif->ipif_saved_ire_lock);
17827 	mp = ipif->ipif_saved_ire_mp;
17828 	ipif->ipif_saved_ire_mp = NULL;
17829 	mutex_exit(&ipif->ipif_saved_ire_lock);
17830 	freemsg(mp);
17831 
17832 	/*
17833 	 * Need to hold both ill_g_lock and ill_lock while
17834 	 * inserting or removing an ipif from the linked list
17835 	 * of ipifs hanging off the ill.
17836 	 */
17837 	rw_enter(&ill_g_lock, RW_WRITER);
17838 	/*
17839 	 * Remove all multicast memberships on the interface now.
17840 	 * This removes IPv4 multicast memberships joined within
17841 	 * the kernel as ipif_down does not do ipif_multicast_down
17842 	 * for IPv4. IPv6 is not handled here as the multicast memberships
17843 	 * are based on ill and not on ipif.
17844 	 */
17845 	ilm_free(ipif);
17846 
17847 	/*
17848 	 * Since we held the ill_g_lock while doing the ilm_free above,
17849 	 * we can assert the ilms were really deleted and not just marked
17850 	 * ILM_DELETED.
17851 	 */
17852 	ASSERT(ilm_walk_ipif(ipif) == 0);
17853 
17854 
17855 	IPIF_TRACE_CLEANUP(ipif);
17856 
17857 	/* Ask SCTP to take it out of it list */
17858 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
17859 
17860 	mutex_enter(&ipif->ipif_ill->ill_lock);
17861 	/* Get it out of the ILL interface list. */
17862 	ipifp = &ipif->ipif_ill->ill_ipif;
17863 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
17864 		if (*ipifp == ipif) {
17865 			*ipifp = ipif->ipif_next;
17866 			break;
17867 		}
17868 	}
17869 
17870 	mutex_exit(&ipif->ipif_ill->ill_lock);
17871 	rw_exit(&ill_g_lock);
17872 
17873 	mutex_destroy(&ipif->ipif_saved_ire_lock);
17874 	/* Free the memory. */
17875 	mi_free((char *)ipif);
17876 }
17877 
17878 /*
17879  * Returns an ipif name in the form "ill_name/unit" if ipif_id is not zero,
17880  * "ill_name" otherwise.
17881  */
17882 char *
17883 ipif_get_name(ipif_t *ipif, char *buf, int len)
17884 {
17885 	char	lbuf[32];
17886 	char	*name;
17887 	size_t	name_len;
17888 
17889 	buf[0] = '\0';
17890 	if (!ipif)
17891 		return (buf);
17892 	name = ipif->ipif_ill->ill_name;
17893 	name_len = ipif->ipif_ill->ill_name_length;
17894 	if (ipif->ipif_id != 0) {
17895 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
17896 		    ipif->ipif_id);
17897 		name = lbuf;
17898 		name_len = mi_strlen(name) + 1;
17899 	}
17900 	len -= 1;
17901 	buf[len] = '\0';
17902 	len = MIN(len, name_len);
17903 	bcopy(name, buf, len);
17904 	return (buf);
17905 }
17906 
17907 /*
17908  * Find an IPIF based on the name passed in.  Names can be of the
17909  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
17910  * The <phys> string can have forms like <dev><#> (e.g., le0),
17911  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
17912  * When there is no colon, the implied unit id is zero. <phys> must
17913  * correspond to the name of an ILL.  (May be called as writer.)
17914  */
17915 static ipif_t *
17916 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
17917     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
17918     mblk_t *mp, ipsq_func_t func, int *error)
17919 {
17920 	char	*cp;
17921 	char	*endp;
17922 	long	id;
17923 	ill_t	*ill;
17924 	ipif_t	*ipif;
17925 	uint_t	ire_type;
17926 	boolean_t did_alloc = B_FALSE;
17927 	ipsq_t	*ipsq;
17928 
17929 	if (error != NULL)
17930 		*error = 0;
17931 
17932 	/*
17933 	 * If the caller wants to us to create the ipif, make sure we have a
17934 	 * valid zoneid
17935 	 */
17936 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
17937 
17938 	if (namelen == 0) {
17939 		if (error != NULL)
17940 			*error = ENXIO;
17941 		return (NULL);
17942 	}
17943 
17944 	*exists = B_FALSE;
17945 	/* Look for a colon in the name. */
17946 	endp = &name[namelen];
17947 	for (cp = endp; --cp > name; ) {
17948 		if (*cp == IPIF_SEPARATOR_CHAR)
17949 			break;
17950 	}
17951 
17952 	if (*cp == IPIF_SEPARATOR_CHAR) {
17953 		/*
17954 		 * Reject any non-decimal aliases for logical
17955 		 * interfaces. Aliases with leading zeroes
17956 		 * are also rejected as they introduce ambiguity
17957 		 * in the naming of the interfaces.
17958 		 * In order to confirm with existing semantics,
17959 		 * and to not break any programs/script relying
17960 		 * on that behaviour, if<0>:0 is considered to be
17961 		 * a valid interface.
17962 		 *
17963 		 * If alias has two or more digits and the first
17964 		 * is zero, fail.
17965 		 */
17966 		if (&cp[2] < endp && cp[1] == '0')
17967 			return (NULL);
17968 	}
17969 
17970 	if (cp <= name) {
17971 		cp = endp;
17972 	} else {
17973 		*cp = '\0';
17974 	}
17975 
17976 	/*
17977 	 * Look up the ILL, based on the portion of the name
17978 	 * before the slash. ill_lookup_on_name returns a held ill.
17979 	 * Temporary to check whether ill exists already. If so
17980 	 * ill_lookup_on_name will clear it.
17981 	 */
17982 	ill = ill_lookup_on_name(name, do_alloc, isv6,
17983 	    q, mp, func, error, &did_alloc);
17984 	if (cp != endp)
17985 		*cp = IPIF_SEPARATOR_CHAR;
17986 	if (ill == NULL)
17987 		return (NULL);
17988 
17989 	/* Establish the unit number in the name. */
17990 	id = 0;
17991 	if (cp < endp && *endp == '\0') {
17992 		/* If there was a colon, the unit number follows. */
17993 		cp++;
17994 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
17995 			ill_refrele(ill);
17996 			if (error != NULL)
17997 				*error = ENXIO;
17998 			return (NULL);
17999 		}
18000 	}
18001 
18002 	GRAB_CONN_LOCK(q);
18003 	mutex_enter(&ill->ill_lock);
18004 	/* Now see if there is an IPIF with this unit number. */
18005 	for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
18006 		if (ipif->ipif_id == id) {
18007 			if (zoneid != ALL_ZONES &&
18008 			    zoneid != ipif->ipif_zoneid) {
18009 				mutex_exit(&ill->ill_lock);
18010 				RELEASE_CONN_LOCK(q);
18011 				ill_refrele(ill);
18012 				if (error != NULL)
18013 					*error = ENXIO;
18014 				return (NULL);
18015 			}
18016 			/*
18017 			 * The block comment at the start of ipif_down
18018 			 * explains the use of the macros used below
18019 			 */
18020 			if (IPIF_CAN_LOOKUP(ipif)) {
18021 				ipif_refhold_locked(ipif);
18022 				mutex_exit(&ill->ill_lock);
18023 				if (!did_alloc)
18024 					*exists = B_TRUE;
18025 				/*
18026 				 * Drop locks before calling ill_refrele
18027 				 * since it can potentially call into
18028 				 * ipif_ill_refrele_tail which can end up
18029 				 * in trying to acquire any lock.
18030 				 */
18031 				RELEASE_CONN_LOCK(q);
18032 				ill_refrele(ill);
18033 				return (ipif);
18034 			} else if (IPIF_CAN_WAIT(ipif, q)) {
18035 				ipsq = ill->ill_phyint->phyint_ipsq;
18036 				mutex_enter(&ipsq->ipsq_lock);
18037 				mutex_exit(&ill->ill_lock);
18038 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
18039 				mutex_exit(&ipsq->ipsq_lock);
18040 				RELEASE_CONN_LOCK(q);
18041 				ill_refrele(ill);
18042 				*error = EINPROGRESS;
18043 				return (NULL);
18044 			}
18045 		}
18046 	}
18047 	RELEASE_CONN_LOCK(q);
18048 
18049 	if (!do_alloc) {
18050 		mutex_exit(&ill->ill_lock);
18051 		ill_refrele(ill);
18052 		if (error != NULL)
18053 			*error = ENXIO;
18054 		return (NULL);
18055 	}
18056 
18057 	/*
18058 	 * If none found, atomically allocate and return a new one.
18059 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
18060 	 * to support "receive only" use of lo0:1 etc. as is still done
18061 	 * below as an initial guess.
18062 	 * However, this is now likely to be overriden later in ipif_up_done()
18063 	 * when we know for sure what address has been configured on the
18064 	 * interface, since we might have more than one loopback interface
18065 	 * with a loopback address, e.g. in the case of zones, and all the
18066 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
18067 	 */
18068 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
18069 		ire_type = IRE_LOOPBACK;
18070 	else
18071 		ire_type = IRE_LOCAL;
18072 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
18073 	if (ipif != NULL)
18074 		ipif_refhold_locked(ipif);
18075 	else if (error != NULL)
18076 		*error = ENOMEM;
18077 	mutex_exit(&ill->ill_lock);
18078 	ill_refrele(ill);
18079 	return (ipif);
18080 }
18081 
18082 /*
18083  * This routine is called whenever a new address comes up on an ipif.  If
18084  * we are configured to respond to address mask requests, then we are supposed
18085  * to broadcast an address mask reply at this time.  This routine is also
18086  * called if we are already up, but a netmask change is made.  This is legal
18087  * but might not make the system manager very popular.	(May be called
18088  * as writer.)
18089  */
18090 static void
18091 ipif_mask_reply(ipif_t *ipif)
18092 {
18093 	icmph_t	*icmph;
18094 	ipha_t	*ipha;
18095 	mblk_t	*mp;
18096 
18097 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
18098 
18099 	if (!ip_respond_to_address_mask_broadcast)
18100 		return;
18101 
18102 	/* ICMP mask reply is IPv4 only */
18103 	ASSERT(!ipif->ipif_isv6);
18104 	/* ICMP mask reply is not for a loopback interface */
18105 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
18106 
18107 	mp = allocb(REPLY_LEN, BPRI_HI);
18108 	if (mp == NULL)
18109 		return;
18110 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
18111 
18112 	ipha = (ipha_t *)mp->b_rptr;
18113 	bzero(ipha, REPLY_LEN);
18114 	*ipha = icmp_ipha;
18115 	ipha->ipha_ttl = ip_broadcast_ttl;
18116 	ipha->ipha_src = ipif->ipif_src_addr;
18117 	ipha->ipha_dst = ipif->ipif_brd_addr;
18118 	ipha->ipha_length = htons(REPLY_LEN);
18119 	ipha->ipha_ident = 0;
18120 
18121 	icmph = (icmph_t *)&ipha[1];
18122 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
18123 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
18124 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
18125 	if (icmph->icmph_checksum == 0)
18126 		icmph->icmph_checksum = 0xffff;
18127 
18128 	put(ipif->ipif_wq, mp);
18129 
18130 #undef	REPLY_LEN
18131 }
18132 
18133 /*
18134  * When the mtu in the ipif changes, we call this routine through ire_walk
18135  * to update all the relevant IREs.
18136  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
18137  */
18138 static void
18139 ipif_mtu_change(ire_t *ire, char *ipif_arg)
18140 {
18141 	ipif_t *ipif = (ipif_t *)ipif_arg;
18142 
18143 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
18144 		return;
18145 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
18146 }
18147 
18148 /*
18149  * When the mtu in the ill changes, we call this routine through ire_walk
18150  * to update all the relevant IREs.
18151  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
18152  */
18153 void
18154 ill_mtu_change(ire_t *ire, char *ill_arg)
18155 {
18156 	ill_t	*ill = (ill_t *)ill_arg;
18157 
18158 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
18159 		return;
18160 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
18161 }
18162 
18163 /*
18164  * Join the ipif specific multicast groups.
18165  * Must be called after a mapping has been set up in the resolver.  (Always
18166  * called as writer.)
18167  */
18168 void
18169 ipif_multicast_up(ipif_t *ipif)
18170 {
18171 	int err, index;
18172 	ill_t *ill;
18173 
18174 	ASSERT(IAM_WRITER_IPIF(ipif));
18175 
18176 	ill = ipif->ipif_ill;
18177 	index = ill->ill_phyint->phyint_ifindex;
18178 
18179 	ip1dbg(("ipif_multicast_up\n"));
18180 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
18181 		return;
18182 
18183 	if (ipif->ipif_isv6) {
18184 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
18185 			return;
18186 
18187 		/* Join the all hosts multicast address */
18188 		ip1dbg(("ipif_multicast_up - addmulti\n"));
18189 		/*
18190 		 * Passing B_TRUE means we have to join the multicast
18191 		 * membership on this interface even though this is
18192 		 * FAILED. If we join on a different one in the group,
18193 		 * we will not be able to delete the membership later
18194 		 * as we currently don't track where we join when we
18195 		 * join within the kernel unlike applications where
18196 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
18197 		 * for more on this.
18198 		 */
18199 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
18200 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
18201 		if (err != 0) {
18202 			ip0dbg(("ipif_multicast_up: "
18203 			    "all_hosts_mcast failed %d\n",
18204 			    err));
18205 			return;
18206 		}
18207 		/*
18208 		 * Enable multicast for the solicited node multicast address
18209 		 */
18210 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
18211 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
18212 
18213 			ipv6_multi.s6_addr32[3] |=
18214 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
18215 
18216 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
18217 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
18218 			    NULL);
18219 			if (err != 0) {
18220 				ip0dbg(("ipif_multicast_up: solicited MC"
18221 				    " failed %d\n", err));
18222 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
18223 				    ill, ill->ill_phyint->phyint_ifindex,
18224 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
18225 				return;
18226 			}
18227 		}
18228 	} else {
18229 		if (ipif->ipif_lcl_addr == INADDR_ANY)
18230 			return;
18231 
18232 		/* Join the all hosts multicast address */
18233 		ip1dbg(("ipif_multicast_up - addmulti\n"));
18234 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
18235 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
18236 		if (err) {
18237 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
18238 			return;
18239 		}
18240 	}
18241 	ipif->ipif_multicast_up = 1;
18242 }
18243 
18244 /*
18245  * Blow away any IPv6 multicast groups that we joined in ipif_multicast_up();
18246  * any explicit memberships are blown away in ill_leave_multicast() when the
18247  * ill is brought down.
18248  */
18249 static void
18250 ipif_multicast_down(ipif_t *ipif)
18251 {
18252 	int err;
18253 
18254 	ASSERT(IAM_WRITER_IPIF(ipif));
18255 
18256 	ip1dbg(("ipif_multicast_down\n"));
18257 	if (!ipif->ipif_multicast_up)
18258 		return;
18259 
18260 	ASSERT(ipif->ipif_isv6);
18261 
18262 	ip1dbg(("ipif_multicast_down - delmulti\n"));
18263 
18264 	/*
18265 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
18266 	 * we should look for ilms on this ill rather than the ones that have
18267 	 * been failed over here.  They are here temporarily. As
18268 	 * ipif_multicast_up has joined on this ill, we should delete only
18269 	 * from this ill.
18270 	 */
18271 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
18272 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
18273 	    B_TRUE, B_TRUE);
18274 	if (err != 0) {
18275 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
18276 		    err));
18277 	}
18278 	/*
18279 	 * Disable multicast for the solicited node multicast address
18280 	 */
18281 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
18282 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
18283 
18284 		ipv6_multi.s6_addr32[3] |=
18285 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
18286 
18287 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
18288 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
18289 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
18290 
18291 		if (err != 0) {
18292 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
18293 			    err));
18294 		}
18295 	}
18296 
18297 	ipif->ipif_multicast_up = 0;
18298 }
18299 
18300 /*
18301  * Used when an interface comes up to recreate any extra routes on this
18302  * interface.
18303  */
18304 static ire_t **
18305 ipif_recover_ire(ipif_t *ipif)
18306 {
18307 	mblk_t	*mp;
18308 	ire_t	**ipif_saved_irep;
18309 	ire_t	**irep;
18310 
18311 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
18312 	    ipif->ipif_id));
18313 
18314 	mutex_enter(&ipif->ipif_saved_ire_lock);
18315 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
18316 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
18317 	if (ipif_saved_irep == NULL) {
18318 		mutex_exit(&ipif->ipif_saved_ire_lock);
18319 		return (NULL);
18320 	}
18321 
18322 	irep = ipif_saved_irep;
18323 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
18324 		ire_t		*ire;
18325 		queue_t		*rfq;
18326 		queue_t		*stq;
18327 		ifrt_t		*ifrt;
18328 		uchar_t		*src_addr;
18329 		uchar_t		*gateway_addr;
18330 		mblk_t		*resolver_mp;
18331 		ushort_t	type;
18332 
18333 		/*
18334 		 * When the ire was initially created and then added in
18335 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
18336 		 * in the case of a traditional interface route, or as one of
18337 		 * the IRE_OFFSUBNET types (with the exception of
18338 		 * IRE_HOST_REDIRECT which is created by icmp_redirect() and
18339 		 * which we don't need to save or recover).  In the case where
18340 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
18341 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
18342 		 * to satisfy software like GateD and Sun Cluster which creates
18343 		 * routes using the the loopback interface's address as a
18344 		 * gateway.
18345 		 *
18346 		 * As ifrt->ifrt_type reflects the already updated ire_type and
18347 		 * since ire_create() expects that IRE_IF_NORESOLVER will have
18348 		 * a valid ire_dlureq_mp field (which doesn't make sense for a
18349 		 * IRE_LOOPBACK), ire_create() will be called in the same way
18350 		 * here as in ip_rt_add(), namely using ipif->ipif_net_type when
18351 		 * the route looks like a traditional interface route (where
18352 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
18353 		 * the saved ifrt->ifrt_type.  This means that in the case where
18354 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
18355 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
18356 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
18357 		 */
18358 		ifrt = (ifrt_t *)mp->b_rptr;
18359 		if (ifrt->ifrt_type & IRE_INTERFACE) {
18360 			rfq = NULL;
18361 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
18362 			    ? ipif->ipif_rq : ipif->ipif_wq;
18363 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
18364 			    ? (uint8_t *)&ifrt->ifrt_src_addr
18365 			    : (uint8_t *)&ipif->ipif_src_addr;
18366 			gateway_addr = NULL;
18367 			resolver_mp = ipif->ipif_resolver_mp;
18368 			type = ipif->ipif_net_type;
18369 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
18370 			/* Recover multiroute broadcast IRE. */
18371 			rfq = ipif->ipif_rq;
18372 			stq = ipif->ipif_wq;
18373 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
18374 			    ? (uint8_t *)&ifrt->ifrt_src_addr
18375 			    : (uint8_t *)&ipif->ipif_src_addr;
18376 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
18377 			resolver_mp = ipif->ipif_bcast_mp;
18378 			type = ifrt->ifrt_type;
18379 		} else {
18380 			rfq = NULL;
18381 			stq = NULL;
18382 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
18383 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
18384 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
18385 			resolver_mp = NULL;
18386 			type = ifrt->ifrt_type;
18387 		}
18388 
18389 		/*
18390 		 * Create a copy of the IRE with the saved address and netmask.
18391 		 */
18392 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
18393 		    "0x%x/0x%x\n",
18394 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
18395 		    ntohl(ifrt->ifrt_addr),
18396 		    ntohl(ifrt->ifrt_mask)));
18397 		ire = ire_create(
18398 		    (uint8_t *)&ifrt->ifrt_addr,
18399 		    (uint8_t *)&ifrt->ifrt_mask,
18400 		    src_addr,
18401 		    gateway_addr,
18402 		    NULL,
18403 		    &ifrt->ifrt_max_frag,
18404 		    NULL,
18405 		    rfq,
18406 		    stq,
18407 		    type,
18408 		    resolver_mp,
18409 		    ipif,
18410 		    NULL,
18411 		    0,
18412 		    0,
18413 		    0,
18414 		    ifrt->ifrt_flags,
18415 		    &ifrt->ifrt_iulp_info);
18416 
18417 		if (ire == NULL) {
18418 			mutex_exit(&ipif->ipif_saved_ire_lock);
18419 			kmem_free(ipif_saved_irep,
18420 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
18421 			return (NULL);
18422 		}
18423 
18424 		/*
18425 		 * Some software (for example, GateD and Sun Cluster) attempts
18426 		 * to create (what amount to) IRE_PREFIX routes with the
18427 		 * loopback address as the gateway.  This is primarily done to
18428 		 * set up prefixes with the RTF_REJECT flag set (for example,
18429 		 * when generating aggregate routes.)
18430 		 *
18431 		 * If the IRE type (as defined by ipif->ipif_net_type) is
18432 		 * IRE_LOOPBACK, then we map the request into a
18433 		 * IRE_IF_NORESOLVER.
18434 		 */
18435 		if (ipif->ipif_net_type == IRE_LOOPBACK)
18436 			ire->ire_type = IRE_IF_NORESOLVER;
18437 		/*
18438 		 * ire held by ire_add, will be refreled' towards the
18439 		 * the end of ipif_up_done
18440 		 */
18441 		(void) ire_add(&ire, NULL, NULL, NULL);
18442 		*irep = ire;
18443 		irep++;
18444 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
18445 	}
18446 	mutex_exit(&ipif->ipif_saved_ire_lock);
18447 	return (ipif_saved_irep);
18448 }
18449 
18450 /*
18451  * Used to set the netmask and broadcast address to default values when the
18452  * interface is brought up.  (Always called as writer.)
18453  */
18454 static void
18455 ipif_set_default(ipif_t *ipif)
18456 {
18457 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
18458 
18459 	if (!ipif->ipif_isv6) {
18460 		/*
18461 		 * Interface holds an IPv4 address. Default
18462 		 * mask is the natural netmask.
18463 		 */
18464 		if (!ipif->ipif_net_mask) {
18465 			ipaddr_t	v4mask;
18466 
18467 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
18468 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
18469 		}
18470 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18471 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
18472 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
18473 		} else {
18474 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
18475 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
18476 		}
18477 		/*
18478 		 * NOTE: SunOS 4.X does this even if the broadcast address
18479 		 * has been already set thus we do the same here.
18480 		 */
18481 		if (ipif->ipif_flags & IPIF_BROADCAST) {
18482 			ipaddr_t	v4addr;
18483 
18484 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
18485 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
18486 		}
18487 	} else {
18488 		/*
18489 		 * Interface holds an IPv6-only address.  Default
18490 		 * mask is all-ones.
18491 		 */
18492 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
18493 			ipif->ipif_v6net_mask = ipv6_all_ones;
18494 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18495 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
18496 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
18497 		} else {
18498 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
18499 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
18500 		}
18501 	}
18502 }
18503 
18504 /*
18505  * Return 0 if this address can be used as local address without causing
18506  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
18507  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
18508  * Special checks are needed to allow the same IPv6 link-local address
18509  * on different ills.
18510  * TODO: allowing the same site-local address on different ill's.
18511  */
18512 int
18513 ip_addr_availability_check(ipif_t *new_ipif)
18514 {
18515 	in6_addr_t our_v6addr;
18516 	ill_t *ill;
18517 	ipif_t *ipif;
18518 	ill_walk_context_t ctx;
18519 
18520 	ASSERT(IAM_WRITER_IPIF(new_ipif));
18521 	ASSERT(MUTEX_HELD(&ip_addr_avail_lock));
18522 	ASSERT(RW_READ_HELD(&ill_g_lock));
18523 
18524 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
18525 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
18526 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
18527 		return (0);
18528 
18529 	our_v6addr = new_ipif->ipif_v6lcl_addr;
18530 
18531 	if (new_ipif->ipif_isv6)
18532 		ill = ILL_START_WALK_V6(&ctx);
18533 	else
18534 		ill = ILL_START_WALK_V4(&ctx);
18535 
18536 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
18537 		for (ipif = ill->ill_ipif; ipif != NULL;
18538 		    ipif = ipif->ipif_next) {
18539 			if ((ipif == new_ipif) ||
18540 			    !(ipif->ipif_flags & IPIF_UP) ||
18541 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
18542 				continue;
18543 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
18544 			    &our_v6addr)) {
18545 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
18546 				    new_ipif->ipif_flags |= IPIF_UNNUMBERED;
18547 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
18548 				    ipif->ipif_flags |= IPIF_UNNUMBERED;
18549 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
18550 				    new_ipif->ipif_ill != ill)
18551 					continue;
18552 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
18553 				    new_ipif->ipif_ill != ill)
18554 					continue;
18555 				else if (new_ipif->ipif_zoneid !=
18556 				    ipif->ipif_zoneid &&
18557 				    (ill->ill_phyint->phyint_flags &
18558 				    PHYI_LOOPBACK))
18559 					continue;
18560 				else if (new_ipif->ipif_ill == ill)
18561 					return (EADDRINUSE);
18562 				else
18563 					return (EADDRNOTAVAIL);
18564 			}
18565 		}
18566 	}
18567 
18568 	return (0);
18569 }
18570 
18571 /*
18572  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
18573  * IREs for the ipif.
18574  * When the routine returns EINPROGRESS then mp has been consumed and
18575  * the ioctl will be acked from ip_rput_dlpi.
18576  */
18577 static int
18578 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
18579 {
18580 	ill_t	*ill = ipif->ipif_ill;
18581 	boolean_t isv6 = ipif->ipif_isv6;
18582 	int	err = 0;
18583 	boolean_t success;
18584 
18585 	ASSERT(IAM_WRITER_IPIF(ipif));
18586 
18587 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18588 
18589 	/* Shouldn't get here if it is already up. */
18590 	if (ipif->ipif_flags & IPIF_UP)
18591 		return (EALREADY);
18592 
18593 	/* Skip arp/ndp for any loopback interface. */
18594 	if (ill->ill_wq != NULL) {
18595 		conn_t *connp = Q_TO_CONN(q);
18596 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
18597 
18598 		if (!ill->ill_dl_up) {
18599 			/*
18600 			 * ill_dl_up is not yet set. i.e. we are yet to
18601 			 * DL_BIND with the driver and this is the first
18602 			 * logical interface on the ill to become "up".
18603 			 * Tell the driver to get going (via DL_BIND_REQ).
18604 			 * Note that changing "significant" IFF_ flags
18605 			 * address/netmask etc cause a down/up dance, but
18606 			 * does not cause an unbind (DL_UNBIND) with the driver
18607 			 */
18608 			return (ill_dl_up(ill, ipif, mp, q));
18609 		}
18610 
18611 		/*
18612 		 * ipif_resolver_up may end up sending an
18613 		 * AR_INTERFACE_UP message to ARP, which would, in
18614 		 * turn send a DLPI message to the driver. ioctls are
18615 		 * serialized and so we cannot send more than one
18616 		 * interface up message at a time. If ipif_resolver_up
18617 		 * does send an interface up message to ARP, we get
18618 		 * EINPROGRESS and we will complete in ip_arp_done.
18619 		 */
18620 
18621 		ASSERT(connp != NULL);
18622 		ASSERT(ipsq->ipsq_pending_mp == NULL);
18623 		mutex_enter(&connp->conn_lock);
18624 		mutex_enter(&ill->ill_lock);
18625 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
18626 		mutex_exit(&ill->ill_lock);
18627 		mutex_exit(&connp->conn_lock);
18628 		if (!success)
18629 			return (EINTR);
18630 
18631 		/*
18632 		 * Crank up IPv6 neighbor discovery
18633 		 * Unlike ARP, this should complete when
18634 		 * ipif_ndp_up returns. However, for
18635 		 * ILLF_XRESOLV interfaces we also send a
18636 		 * AR_INTERFACE_UP to the external resolver.
18637 		 * That ioctl will complete in ip_rput.
18638 		 */
18639 		if (isv6) {
18640 			err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr,
18641 			    B_FALSE);
18642 			if (err != 0) {
18643 				mp = ipsq_pending_mp_get(ipsq, &connp);
18644 				return (err);
18645 			}
18646 		}
18647 		/* Now, ARP */
18648 		if ((err = ipif_resolver_up(ipif, B_FALSE)) ==
18649 		    EINPROGRESS) {
18650 			/* We will complete it in ip_arp_done */
18651 			return (err);
18652 		}
18653 		mp = ipsq_pending_mp_get(ipsq, &connp);
18654 		ASSERT(mp != NULL);
18655 		if (err != 0)
18656 			return (err);
18657 	}
18658 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
18659 }
18660 
18661 /*
18662  * Perform a bind for the physical device.
18663  * When the routine returns EINPROGRESS then mp has been consumed and
18664  * the ioctl will be acked from ip_rput_dlpi.
18665  * Allocate an unbind message and save it until ipif_down.
18666  */
18667 static int
18668 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
18669 {
18670 	mblk_t	*areq_mp = NULL;
18671 	mblk_t	*bind_mp = NULL;
18672 	mblk_t	*unbind_mp = NULL;
18673 	conn_t	*connp;
18674 	boolean_t success;
18675 
18676 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
18677 	ASSERT(IAM_WRITER_ILL(ill));
18678 
18679 	ASSERT(mp != NULL);
18680 
18681 	/* Create a resolver cookie for ARP */
18682 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
18683 		areq_t		*areq;
18684 		uint16_t	sap_addr;
18685 
18686 		areq_mp = ill_arp_alloc(ill,
18687 			(uchar_t *)&ip_areq_template, 0);
18688 		if (areq_mp == NULL) {
18689 			return (ENOMEM);
18690 		}
18691 		freemsg(ill->ill_resolver_mp);
18692 		ill->ill_resolver_mp = areq_mp;
18693 		areq = (areq_t *)areq_mp->b_rptr;
18694 		sap_addr = ill->ill_sap;
18695 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
18696 		/*
18697 		 * Wait till we call ill_pending_mp_add to determine
18698 		 * the success before we free the ill_resolver_mp and
18699 		 * attach areq_mp in it's place.
18700 		 */
18701 	}
18702 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
18703 	    DL_BIND_REQ);
18704 	if (bind_mp == NULL)
18705 		goto bad;
18706 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
18707 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
18708 
18709 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
18710 	if (unbind_mp == NULL)
18711 		goto bad;
18712 
18713 	/*
18714 	 * Record state needed to complete this operation when the
18715 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
18716 	 */
18717 	if (WR(q)->q_next == NULL) {
18718 		connp = Q_TO_CONN(q);
18719 		mutex_enter(&connp->conn_lock);
18720 	} else {
18721 		connp = NULL;
18722 	}
18723 	mutex_enter(&ipif->ipif_ill->ill_lock);
18724 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
18725 	mutex_exit(&ipif->ipif_ill->ill_lock);
18726 	if (connp != NULL)
18727 		mutex_exit(&connp->conn_lock);
18728 	if (!success)
18729 		goto bad;
18730 
18731 	/*
18732 	 * Save the unbind message for ill_dl_down(); it will be consumed when
18733 	 * the interface goes down.
18734 	 */
18735 	ASSERT(ill->ill_unbind_mp == NULL);
18736 	ill->ill_unbind_mp = unbind_mp;
18737 
18738 	ill_dlpi_send(ill, bind_mp);
18739 	/* Send down link-layer capabilities probe if not already done. */
18740 	ill_capability_probe(ill);
18741 
18742 	/*
18743 	 * Sysid used to rely on the fact that netboots set domainname
18744 	 * and the like. Now that miniroot boots aren't strictly netboots
18745 	 * and miniroot network configuration is driven from userland
18746 	 * these things still need to be set. This situation can be detected
18747 	 * by comparing the interface being configured here to the one
18748 	 * dhcack was set to reference by the boot loader. Once sysid is
18749 	 * converted to use dhcp_ipc_getinfo() this call can go away.
18750 	 */
18751 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
18752 	    (strcmp(ill->ill_name, dhcack) == 0) &&
18753 	    (strlen(srpc_domain) == 0)) {
18754 		if (dhcpinit() != 0)
18755 			cmn_err(CE_WARN, "no cached dhcp response");
18756 	}
18757 
18758 	/*
18759 	 * This operation will complete in ip_rput_dlpi with either
18760 	 * a DL_BIND_ACK or DL_ERROR_ACK.
18761 	 */
18762 	return (EINPROGRESS);
18763 bad:
18764 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
18765 	/*
18766 	 * We don't have to check for possible removal from illgrp
18767 	 * as we have not yet inserted in illgrp. For groups
18768 	 * without names, this ipif is still not UP and hence
18769 	 * this could not have possibly had any influence in forming
18770 	 * groups.
18771 	 */
18772 
18773 	if (bind_mp != NULL)
18774 		freemsg(bind_mp);
18775 	if (unbind_mp != NULL)
18776 		freemsg(unbind_mp);
18777 	return (ENOMEM);
18778 }
18779 
18780 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
18781 
18782 /*
18783  * DLPI and ARP is up.
18784  * Create all the IREs associated with an interface bring up multicast.
18785  * Set the interface flag and finish other initialization
18786  * that potentially had to be differed to after DL_BIND_ACK.
18787  */
18788 int
18789 ipif_up_done(ipif_t *ipif)
18790 {
18791 	ire_t	*ire_array[20];
18792 	ire_t	**irep = ire_array;
18793 	ire_t	**irep1;
18794 	ipaddr_t net_mask = 0;
18795 	ipaddr_t subnet_mask, route_mask;
18796 	ill_t	*ill = ipif->ipif_ill;
18797 	queue_t	*stq;
18798 	ipif_t	 *src_ipif;
18799 	ipif_t   *tmp_ipif;
18800 	boolean_t	flush_ire_cache = B_TRUE;
18801 	int	err = 0;
18802 	phyint_t *phyi;
18803 	ire_t	**ipif_saved_irep = NULL;
18804 	int ipif_saved_ire_cnt;
18805 	int	cnt;
18806 	boolean_t	src_ipif_held = B_FALSE;
18807 	boolean_t	ire_added = B_FALSE;
18808 	boolean_t	loopback = B_FALSE;
18809 
18810 	ip1dbg(("ipif_up_done(%s:%u)\n",
18811 		ipif->ipif_ill->ill_name, ipif->ipif_id));
18812 	/* Check if this is a loopback interface */
18813 	if (ipif->ipif_ill->ill_wq == NULL)
18814 		loopback = B_TRUE;
18815 
18816 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
18817 	/*
18818 	 * If all other interfaces for this ill are down or DEPRECATED,
18819 	 * or otherwise unsuitable for source address selection, remove
18820 	 * any IRE_CACHE entries for this ill to make sure source
18821 	 * address selection gets to take this new ipif into account.
18822 	 * No need to hold ill_lock while traversing the ipif list since
18823 	 * we are writer
18824 	 */
18825 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
18826 		tmp_ipif = tmp_ipif->ipif_next) {
18827 		if (((tmp_ipif->ipif_flags &
18828 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
18829 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
18830 		    (tmp_ipif == ipif))
18831 			continue;
18832 		/* first useable pre-existing interface */
18833 		flush_ire_cache = B_FALSE;
18834 		break;
18835 	}
18836 	if (flush_ire_cache)
18837 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
18838 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
18839 
18840 	/*
18841 	 * Figure out which way the send-to queue should go.  Only
18842 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
18843 	 * should show up here.
18844 	 */
18845 	switch (ill->ill_net_type) {
18846 	case IRE_IF_RESOLVER:
18847 		stq = ill->ill_rq;
18848 		break;
18849 	case IRE_IF_NORESOLVER:
18850 	case IRE_LOOPBACK:
18851 		stq = ill->ill_wq;
18852 		break;
18853 	default:
18854 		return (EINVAL);
18855 	}
18856 
18857 	if (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) {
18858 		/*
18859 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
18860 		 * ipif_lookup_on_name(), but in the case of zones we can have
18861 		 * several loopback addresses on lo0. So all the interfaces with
18862 		 * loopback addresses need to be marked IRE_LOOPBACK.
18863 		 */
18864 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
18865 		    htonl(INADDR_LOOPBACK))
18866 			ipif->ipif_ire_type = IRE_LOOPBACK;
18867 		else
18868 			ipif->ipif_ire_type = IRE_LOCAL;
18869 	}
18870 
18871 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
18872 		/*
18873 		 * Can't use our source address. Select a different
18874 		 * source address for the IRE_INTERFACE and IRE_LOCAL
18875 		 */
18876 		src_ipif = ipif_select_source(ipif->ipif_ill,
18877 		    ipif->ipif_subnet, ipif->ipif_zoneid);
18878 		if (src_ipif == NULL)
18879 			src_ipif = ipif;	/* Last resort */
18880 		else
18881 			src_ipif_held = B_TRUE;
18882 	} else {
18883 		src_ipif = ipif;
18884 	}
18885 
18886 	/* Create all the IREs associated with this interface */
18887 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
18888 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18889 		/* Register the source address for __sin6_src_id */
18890 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
18891 		    ipif->ipif_zoneid);
18892 		if (err != 0) {
18893 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
18894 			return (err);
18895 		}
18896 		/* If the interface address is set, create the local IRE. */
18897 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
18898 			(void *)ipif,
18899 			ipif->ipif_ire_type,
18900 			ntohl(ipif->ipif_lcl_addr)));
18901 		*irep++ = ire_create(
18902 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
18903 		    (uchar_t *)&ip_g_all_ones,		/* mask */
18904 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
18905 		    NULL,				/* no gateway */
18906 		    NULL,
18907 		    &ip_loopback_mtuplus,		/* max frag size */
18908 		    NULL,
18909 		    ipif->ipif_rq,			/* recv-from queue */
18910 		    NULL,				/* no send-to queue */
18911 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
18912 		    NULL,
18913 		    ipif,
18914 		    NULL,
18915 		    0,
18916 		    0,
18917 		    0,
18918 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
18919 		    RTF_PRIVATE : 0,
18920 		    &ire_uinfo_null);
18921 	} else {
18922 		ip1dbg((
18923 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
18924 		    ipif->ipif_ire_type,
18925 		    ntohl(ipif->ipif_lcl_addr),
18926 		    (uint_t)ipif->ipif_flags));
18927 	}
18928 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
18929 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18930 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
18931 	} else {
18932 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
18933 	}
18934 
18935 	subnet_mask = ipif->ipif_net_mask;
18936 
18937 	/*
18938 	 * If mask was not specified, use natural netmask of
18939 	 * interface address. Also, store this mask back into the
18940 	 * ipif struct.
18941 	 */
18942 	if (subnet_mask == 0) {
18943 		subnet_mask = net_mask;
18944 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
18945 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
18946 		    ipif->ipif_v6subnet);
18947 	}
18948 
18949 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
18950 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
18951 	    ipif->ipif_subnet != INADDR_ANY) {
18952 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
18953 
18954 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18955 			route_mask = IP_HOST_MASK;
18956 		} else {
18957 			route_mask = subnet_mask;
18958 		}
18959 
18960 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
18961 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
18962 			(void *)ipif, (void *)ill,
18963 			ill->ill_net_type,
18964 			ntohl(ipif->ipif_subnet)));
18965 		*irep++ = ire_create(
18966 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
18967 		    (uchar_t *)&route_mask,		/* mask */
18968 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
18969 		    NULL,				/* no gateway */
18970 		    NULL,
18971 		    &ipif->ipif_mtu,			/* max frag */
18972 		    NULL,
18973 		    NULL,				/* no recv queue */
18974 		    stq,				/* send-to queue */
18975 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
18976 		    ill->ill_resolver_mp,		/* xmit header */
18977 		    ipif,
18978 		    NULL,
18979 		    0,
18980 		    0,
18981 		    0,
18982 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
18983 		    &ire_uinfo_null);
18984 	}
18985 
18986 	/*
18987 	 * If the interface address is set, create the broadcast IREs.
18988 	 *
18989 	 * ire_create_bcast checks if the proposed new IRE matches
18990 	 * any existing IRE's with the same physical interface (ILL).
18991 	 * This should get rid of duplicates.
18992 	 * ire_create_bcast also check IPIF_NOXMIT and does not create
18993 	 * any broadcast ires.
18994 	 */
18995 	if ((ipif->ipif_subnet != INADDR_ANY) &&
18996 	    (ipif->ipif_flags & IPIF_BROADCAST)) {
18997 		ipaddr_t addr;
18998 
18999 		ip1dbg(("ipif_up_done: creating broadcast IRE\n"));
19000 		irep = ire_check_and_create_bcast(ipif, 0, irep,
19001 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19002 		irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep,
19003 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19004 
19005 		/*
19006 		 * For backward compatibility, we need to create net
19007 		 * broadcast ire's based on the old "IP address class
19008 		 * system."  The reason is that some old machines only
19009 		 * respond to these class derived net broadcast.
19010 		 *
19011 		 * But we should not create these net broadcast ire's if
19012 		 * the subnet_mask is shorter than the IP address class based
19013 		 * derived netmask.  Otherwise, we may create a net
19014 		 * broadcast address which is the same as an IP address
19015 		 * on the subnet.  Then TCP will refuse to talk to that
19016 		 * address.
19017 		 *
19018 		 * Nor do we need IRE_BROADCAST ire's for the interface
19019 		 * with the netmask as 0xFFFFFFFF, as IRE_LOCAL for that
19020 		 * interface is already created.  Creating these broadcast
19021 		 * ire's will only create confusion as the "addr" is going
19022 		 * to be same as that of the IP address of the interface.
19023 		 */
19024 		if (net_mask < subnet_mask) {
19025 			addr = net_mask & ipif->ipif_subnet;
19026 			irep = ire_check_and_create_bcast(ipif, addr, irep,
19027 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19028 			irep = ire_check_and_create_bcast(ipif,
19029 			    ~net_mask | addr, irep,
19030 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19031 		}
19032 
19033 		if (subnet_mask != 0xFFFFFFFF) {
19034 			addr = ipif->ipif_subnet;
19035 			irep = ire_check_and_create_bcast(ipif, addr, irep,
19036 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19037 			irep = ire_check_and_create_bcast(ipif,
19038 			    ~subnet_mask|addr, irep,
19039 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19040 		}
19041 	}
19042 
19043 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19044 
19045 	/* If an earlier ire_create failed, get out now */
19046 	for (irep1 = irep; irep1 > ire_array; ) {
19047 		irep1--;
19048 		if (*irep1 == NULL) {
19049 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
19050 			err = ENOMEM;
19051 			goto bad;
19052 		}
19053 	}
19054 
19055 	/*
19056 	 * Need to atomically check for ip_addr_availablity_check
19057 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
19058 	 * from group also.The ill_g_lock is grabbed as reader
19059 	 * just to make sure no new ills or new ipifs are being added
19060 	 * to the system while we are checking the uniqueness of addresses.
19061 	 */
19062 	rw_enter(&ill_g_lock, RW_READER);
19063 	mutex_enter(&ip_addr_avail_lock);
19064 	/* Mark it up, and increment counters. */
19065 	ill->ill_ipif_up_count++;
19066 	ipif->ipif_flags |= IPIF_UP;
19067 	err = ip_addr_availability_check(ipif);
19068 	mutex_exit(&ip_addr_avail_lock);
19069 	rw_exit(&ill_g_lock);
19070 
19071 	if (err != 0) {
19072 		/*
19073 		 * Our address may already be up on the same ill. In this case,
19074 		 * the ARP entry for our ipif replaced the one for the other
19075 		 * ipif. So we don't want to delete it (otherwise the other ipif
19076 		 * would be unable to send packets).
19077 		 * ip_addr_availability_check() identifies this case for us and
19078 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
19079 		 * which is the expected error code.
19080 		 */
19081 		if (err == EADDRINUSE) {
19082 			freemsg(ipif->ipif_arp_del_mp);
19083 			ipif->ipif_arp_del_mp = NULL;
19084 			err = EADDRNOTAVAIL;
19085 		}
19086 		ill->ill_ipif_up_count--;
19087 		ipif->ipif_flags &= ~IPIF_UP;
19088 		goto bad;
19089 	}
19090 
19091 	/*
19092 	 * Add in all newly created IREs.  ire_create_bcast() has
19093 	 * already checked for duplicates of the IRE_BROADCAST type.
19094 	 * We want to add before we call ifgrp_insert which wants
19095 	 * to know whether IRE_IF_RESOLVER exists or not.
19096 	 *
19097 	 * NOTE : We refrele the ire though we may branch to "bad"
19098 	 *	  later on where we do ire_delete. This is okay
19099 	 *	  because nobody can delete it as we are running
19100 	 *	  exclusively.
19101 	 */
19102 	for (irep1 = irep; irep1 > ire_array; ) {
19103 		irep1--;
19104 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
19105 		/*
19106 		 * refheld by ire_add. refele towards the end of the func
19107 		 */
19108 		(void) ire_add(irep1, NULL, NULL, NULL);
19109 	}
19110 	ire_added = B_TRUE;
19111 	/*
19112 	 * Form groups if possible.
19113 	 *
19114 	 * If we are supposed to be in a ill_group with a name, insert it
19115 	 * now as we know that at least one ipif is UP. Otherwise form
19116 	 * nameless groups.
19117 	 *
19118 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
19119 	 * this ipif into the appropriate interface group, or create a
19120 	 * new one. If this is already in a nameless group, we try to form
19121 	 * a bigger group looking at other ills potentially sharing this
19122 	 * ipif's prefix.
19123 	 */
19124 	phyi = ill->ill_phyint;
19125 	if (phyi->phyint_groupname_len != 0) {
19126 		ASSERT(phyi->phyint_groupname != NULL);
19127 		if (ill->ill_ipif_up_count == 1) {
19128 			ASSERT(ill->ill_group == NULL);
19129 			err = illgrp_insert(&illgrp_head_v4, ill,
19130 			    phyi->phyint_groupname, NULL, B_TRUE);
19131 			if (err != 0) {
19132 				ip1dbg(("ipif_up_done: illgrp allocation "
19133 				    "failed, error %d\n", err));
19134 				goto bad;
19135 			}
19136 		}
19137 		ASSERT(ill->ill_group != NULL);
19138 	}
19139 
19140 	/*
19141 	 * When this is part of group, we need to make sure that
19142 	 * any broadcast ires created because of this ipif coming
19143 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
19144 	 * so that we don't receive duplicate broadcast packets.
19145 	 */
19146 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
19147 		ipif_renominate_bcast(ipif);
19148 
19149 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
19150 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
19151 	ipif_saved_irep = ipif_recover_ire(ipif);
19152 
19153 	if (!loopback) {
19154 		/*
19155 		 * If the broadcast address has been set, make sure it makes
19156 		 * sense based on the interface address.
19157 		 * Only match on ill since we are sharing broadcast addresses.
19158 		 */
19159 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
19160 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
19161 			ire_t	*ire;
19162 
19163 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
19164 			    IRE_BROADCAST, ipif, ALL_ZONES,
19165 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19166 
19167 			if (ire == NULL) {
19168 				/*
19169 				 * If there isn't a matching broadcast IRE,
19170 				 * revert to the default for this netmask.
19171 				 */
19172 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
19173 				mutex_enter(&ipif->ipif_ill->ill_lock);
19174 				ipif_set_default(ipif);
19175 				mutex_exit(&ipif->ipif_ill->ill_lock);
19176 			} else {
19177 				ire_refrele(ire);
19178 			}
19179 		}
19180 
19181 	}
19182 
19183 
19184 	/* This is the first interface on this ill */
19185 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
19186 		/*
19187 		 * Need to recover all multicast memberships in the driver.
19188 		 * This had to be deferred until we had attached.
19189 		 */
19190 		ill_recover_multicast(ill);
19191 	}
19192 	/* Join the allhosts multicast address */
19193 	ipif_multicast_up(ipif);
19194 
19195 	if (!loopback) {
19196 		/*
19197 		 * See whether anybody else would benefit from the
19198 		 * new ipif that we added. We call this always rather
19199 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
19200 		 * ipif is for the benefit of illgrp_insert (done above)
19201 		 * which does not do source address selection as it does
19202 		 * not want to re-create interface routes that we are
19203 		 * having reference to it here.
19204 		 */
19205 		ill_update_source_selection(ill);
19206 	}
19207 
19208 	for (irep1 = irep; irep1 > ire_array; ) {
19209 		irep1--;
19210 		if (*irep1 != NULL) {
19211 			/* was held in ire_add */
19212 			ire_refrele(*irep1);
19213 		}
19214 	}
19215 
19216 	cnt = ipif_saved_ire_cnt;
19217 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
19218 		if (*irep1 != NULL) {
19219 			/* was held in ire_add */
19220 			ire_refrele(*irep1);
19221 		}
19222 	}
19223 
19224 	/*
19225 	 * This had to be deferred until we had bound.
19226 	 * tell routing sockets that this interface is up
19227 	 */
19228 	ip_rts_ifmsg(ipif);
19229 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
19230 
19231 	if (!loopback) {
19232 		/* Broadcast an address mask reply. */
19233 		ipif_mask_reply(ipif);
19234 	}
19235 	if (ipif_saved_irep != NULL) {
19236 		kmem_free(ipif_saved_irep,
19237 		    ipif_saved_ire_cnt * sizeof (ire_t *));
19238 	}
19239 	if (src_ipif_held)
19240 		ipif_refrele(src_ipif);
19241 	/* Let SCTP update the status for this ipif */
19242 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
19243 	return (0);
19244 
19245 bad:
19246 	ip1dbg(("ipif_up_done: FAILED \n"));
19247 	/*
19248 	 * We don't have to bother removing from ill groups because
19249 	 *
19250 	 * 1) For groups with names, we insert only when the first ipif
19251 	 *    comes up. In that case if it fails, it will not be in any
19252 	 *    group. So, we need not try to remove for that case.
19253 	 *
19254 	 * 2) For groups without names, either we tried to insert ipif_ill
19255 	 *    in a group as singleton or found some other group to become
19256 	 *    a bigger group. For the former, if it fails we don't have
19257 	 *    anything to do as ipif_ill is not in the group and for the
19258 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
19259 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
19260 	 */
19261 	while (irep > ire_array) {
19262 		irep--;
19263 		if (*irep != NULL) {
19264 			ire_delete(*irep);
19265 			if (ire_added)
19266 				ire_refrele(*irep);
19267 		}
19268 	}
19269 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid);
19270 
19271 	if (ipif_saved_irep != NULL) {
19272 		kmem_free(ipif_saved_irep,
19273 		    ipif_saved_ire_cnt * sizeof (ire_t *));
19274 	}
19275 	if (src_ipif_held)
19276 		ipif_refrele(src_ipif);
19277 
19278 	ipif_arp_down(ipif);
19279 	return (err);
19280 }
19281 
19282 /*
19283  * Turn off the ARP with the ILLF_NOARP flag.
19284  */
19285 static int
19286 ill_arp_off(ill_t *ill)
19287 {
19288 	mblk_t	*arp_off_mp = NULL;
19289 	mblk_t	*arp_on_mp = NULL;
19290 
19291 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
19292 
19293 	ASSERT(IAM_WRITER_ILL(ill));
19294 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
19295 
19296 	/*
19297 	 * If the on message is still around we've already done
19298 	 * an arp_off without doing an arp_on thus there is no
19299 	 * work needed.
19300 	 */
19301 	if (ill->ill_arp_on_mp != NULL)
19302 		return (0);
19303 
19304 	/*
19305 	 * Allocate an ARP on message (to be saved) and an ARP off message
19306 	 */
19307 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
19308 	if (!arp_off_mp)
19309 		return (ENOMEM);
19310 
19311 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
19312 	if (!arp_on_mp)
19313 		goto failed;
19314 
19315 	ASSERT(ill->ill_arp_on_mp == NULL);
19316 	ill->ill_arp_on_mp = arp_on_mp;
19317 
19318 	/* Send an AR_INTERFACE_OFF request */
19319 	putnext(ill->ill_rq, arp_off_mp);
19320 	return (0);
19321 failed:
19322 
19323 	if (arp_off_mp)
19324 		freemsg(arp_off_mp);
19325 	return (ENOMEM);
19326 }
19327 
19328 /*
19329  * Turn on ARP by turning off the ILLF_NOARP flag.
19330  */
19331 static int
19332 ill_arp_on(ill_t *ill)
19333 {
19334 	mblk_t	*mp;
19335 
19336 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
19337 
19338 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
19339 
19340 	ASSERT(IAM_WRITER_ILL(ill));
19341 	/*
19342 	 * Send an AR_INTERFACE_ON request if we have already done
19343 	 * an arp_off (which allocated the message).
19344 	 */
19345 	if (ill->ill_arp_on_mp != NULL) {
19346 		mp = ill->ill_arp_on_mp;
19347 		ill->ill_arp_on_mp = NULL;
19348 		putnext(ill->ill_rq, mp);
19349 	}
19350 	return (0);
19351 }
19352 
19353 /*
19354  * Called after either deleting ill from the group or when setting
19355  * FAILED or STANDBY on the interface.
19356  */
19357 static void
19358 illgrp_reset_schednext(ill_t *ill)
19359 {
19360 	ill_group_t *illgrp;
19361 	ill_t *save_ill;
19362 
19363 	ASSERT(IAM_WRITER_ILL(ill));
19364 	/*
19365 	 * When called from illgrp_delete, ill_group will be non-NULL.
19366 	 * But when called from ip_sioctl_flags, it could be NULL if
19367 	 * somebody is setting FAILED/INACTIVE on some interface which
19368 	 * is not part of a group.
19369 	 */
19370 	illgrp = ill->ill_group;
19371 	if (illgrp == NULL)
19372 		return;
19373 	if (illgrp->illgrp_ill_schednext != ill)
19374 		return;
19375 
19376 	illgrp->illgrp_ill_schednext = NULL;
19377 	save_ill = ill;
19378 	/*
19379 	 * Choose a good ill to be the next one for
19380 	 * outbound traffic. As the flags FAILED/STANDBY is
19381 	 * not yet marked when called from ip_sioctl_flags,
19382 	 * we check for ill separately.
19383 	 */
19384 	for (ill = illgrp->illgrp_ill; ill != NULL;
19385 	    ill = ill->ill_group_next) {
19386 		if ((ill != save_ill) &&
19387 		    !(ill->ill_phyint->phyint_flags &
19388 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
19389 			illgrp->illgrp_ill_schednext = ill;
19390 			return;
19391 		}
19392 	}
19393 }
19394 
19395 /*
19396  * Given an ill, find the next ill in the group to be scheduled.
19397  * (This should be called by ip_newroute() before ire_create().)
19398  * The passed in ill may be pulled out of the group, after we have picked
19399  * up a different outgoing ill from the same group. However ire add will
19400  * atomically check this.
19401  */
19402 ill_t *
19403 illgrp_scheduler(ill_t *ill)
19404 {
19405 	ill_t *retill;
19406 	ill_group_t *illgrp;
19407 	int illcnt;
19408 	int i;
19409 	uint64_t flags;
19410 
19411 	/*
19412 	 * We don't use a lock to check for the ill_group. If this ill
19413 	 * is currently being inserted we may end up just returning this
19414 	 * ill itself. That is ok.
19415 	 */
19416 	if (ill->ill_group == NULL) {
19417 		ill_refhold(ill);
19418 		return (ill);
19419 	}
19420 
19421 	/*
19422 	 * Grab the ill_g_lock as reader to make sure we are dealing with
19423 	 * a set of stable ills. No ill can be added or deleted or change
19424 	 * group while we hold the reader lock.
19425 	 */
19426 	rw_enter(&ill_g_lock, RW_READER);
19427 	if ((illgrp = ill->ill_group) == NULL) {
19428 		rw_exit(&ill_g_lock);
19429 		ill_refhold(ill);
19430 		return (ill);
19431 	}
19432 
19433 	illcnt = illgrp->illgrp_ill_count;
19434 	mutex_enter(&illgrp->illgrp_lock);
19435 	retill = illgrp->illgrp_ill_schednext;
19436 
19437 	if (retill == NULL)
19438 		retill = illgrp->illgrp_ill;
19439 
19440 	/*
19441 	 * We do a circular search beginning at illgrp_ill_schednext
19442 	 * or illgrp_ill. We don't check the flags against the ill lock
19443 	 * since it can change anytime. The ire creation will be atomic
19444 	 * and will fail if the ill is FAILED or OFFLINE.
19445 	 */
19446 	for (i = 0; i < illcnt; i++) {
19447 		flags = retill->ill_phyint->phyint_flags;
19448 
19449 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
19450 		    ILL_CAN_LOOKUP(retill)) {
19451 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
19452 			ill_refhold(retill);
19453 			break;
19454 		}
19455 		retill = retill->ill_group_next;
19456 		if (retill == NULL)
19457 			retill = illgrp->illgrp_ill;
19458 	}
19459 	mutex_exit(&illgrp->illgrp_lock);
19460 	rw_exit(&ill_g_lock);
19461 
19462 	return (i == illcnt ? NULL : retill);
19463 }
19464 
19465 /*
19466  * Checks for availbility of a usable source address (if there is one) when the
19467  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
19468  * this selection is done regardless of the destination.
19469  */
19470 boolean_t
19471 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
19472 {
19473 	uint_t	ifindex;
19474 	ipif_t	*ipif = NULL;
19475 	ill_t	*uill;
19476 	boolean_t isv6;
19477 
19478 	ASSERT(ill != NULL);
19479 
19480 	isv6 = ill->ill_isv6;
19481 	ifindex = ill->ill_usesrc_ifindex;
19482 	if (ifindex != 0) {
19483 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
19484 		    NULL);
19485 		if (uill == NULL)
19486 			return (NULL);
19487 		mutex_enter(&uill->ill_lock);
19488 		for (ipif = uill->ill_ipif; ipif != NULL;
19489 		    ipif = ipif->ipif_next) {
19490 			if (!IPIF_CAN_LOOKUP(ipif))
19491 				continue;
19492 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
19493 				continue;
19494 			if (!(ipif->ipif_flags & IPIF_UP))
19495 				continue;
19496 			if (ipif->ipif_zoneid != zoneid)
19497 				continue;
19498 			if ((isv6 &&
19499 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
19500 			    (ipif->ipif_lcl_addr == INADDR_ANY))
19501 				continue;
19502 			mutex_exit(&uill->ill_lock);
19503 			ill_refrele(uill);
19504 			return (B_TRUE);
19505 		}
19506 		mutex_exit(&uill->ill_lock);
19507 		ill_refrele(uill);
19508 	}
19509 	return (B_FALSE);
19510 }
19511 
19512 /*
19513  * Determine the best source address given a destination address and an ill.
19514  * Prefers non-deprecated over deprecated but will return a deprecated
19515  * address if there is no other choice. If there is a usable source address
19516  * on the interface pointed to by ill_usesrc_ifindex then that is given
19517  * first preference.
19518  *
19519  * Returns NULL if there is no suitable source address for the ill.
19520  * This only occurs when there is no valid source address for the ill.
19521  */
19522 ipif_t *
19523 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
19524 {
19525 	ipif_t *ipif;
19526 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
19527 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
19528 	int index = 0;
19529 	boolean_t wrapped = B_FALSE;
19530 	boolean_t same_subnet_only = B_FALSE;
19531 	boolean_t ipif_same_found, ipif_other_found;
19532 	ill_t	*till, *usill = NULL;
19533 
19534 	if (ill->ill_usesrc_ifindex != 0) {
19535 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex, B_FALSE,
19536 		    NULL, NULL, NULL, NULL);
19537 		if (usill != NULL)
19538 			ill = usill;	/* Select source from usesrc ILL */
19539 		else
19540 			return (NULL);
19541 	}
19542 
19543 	/*
19544 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
19545 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
19546 	 * After selecting the right ipif, under ill_lock make sure ipif is
19547 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
19548 	 * we retry. Inside the loop we still need to check for CONDEMNED,
19549 	 * but not under a lock.
19550 	 */
19551 	rw_enter(&ill_g_lock, RW_READER);
19552 
19553 retry:
19554 	till = ill;
19555 	ipif_arr[0] = NULL;
19556 
19557 	if (till->ill_group != NULL)
19558 		till = till->ill_group->illgrp_ill;
19559 
19560 	/*
19561 	 * Choose one good source address from each ill across the group.
19562 	 * If possible choose a source address in the same subnet as
19563 	 * the destination address.
19564 	 *
19565 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
19566 	 * This is okay because of the following.
19567 	 *
19568 	 *    If PHYI_FAILED is set and we still have non-deprecated
19569 	 *    addresses, it means the addresses have not yet been
19570 	 *    failed over to a different interface. We potentially
19571 	 *    select them to create IRE_CACHES, which will be later
19572 	 *    flushed when the addresses move over.
19573 	 *
19574 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
19575 	 *    addresses, it means either the user has configured them
19576 	 *    or PHYI_INACTIVE has not been cleared after the addresses
19577 	 *    been moved over. For the former, in.mpathd does a failover
19578 	 *    when the interface becomes INACTIVE and hence we should
19579 	 *    not find them. Once INACTIVE is set, we don't allow them
19580 	 *    to create logical interfaces anymore. For the latter, a
19581 	 *    flush will happen when INACTIVE is cleared which will
19582 	 *    flush the IRE_CACHES.
19583 	 *
19584 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
19585 	 *    over soon. We potentially select them to create IRE_CACHEs,
19586 	 *    which will be later flushed when the addresses move over.
19587 	 *
19588 	 * NOTE : As ipif_select_source is called to borrow source address
19589 	 * for an ipif that is part of a group, source address selection
19590 	 * will be re-done whenever the group changes i.e either an
19591 	 * insertion/deletion in the group.
19592 	 *
19593 	 * Fill ipif_arr[] with source addresses, using these rules:
19594 	 *
19595 	 *	1. At most one source address from a given ill ends up
19596 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
19597 	 *	   associated with a given ill ends up in ipif_arr[].
19598 	 *
19599 	 *	2. If there is at least one non-deprecated ipif in the
19600 	 *	   IPMP group with a source address on the same subnet as
19601 	 *	   our destination, then fill ipif_arr[] only with
19602 	 *	   source addresses on the same subnet as our destination.
19603 	 *	   Note that because of (1), only the first
19604 	 *	   non-deprecated ipif found with a source address
19605 	 *	   matching the destination ends up in ipif_arr[].
19606 	 *
19607 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
19608 	 *	   addresses not in the same subnet as our destination.
19609 	 *	   Again, because of (1), only the first off-subnet source
19610 	 *	   address will be chosen.
19611 	 *
19612 	 *	4. If there are no non-deprecated ipifs, then just use
19613 	 *	   the source address associated with the last deprecated
19614 	 *	   one we find that happens to be on the same subnet,
19615 	 *	   otherwise the first one not in the same subnet.
19616 	 */
19617 	for (; till != NULL; till = till->ill_group_next) {
19618 		ipif_same_found = B_FALSE;
19619 		ipif_other_found = B_FALSE;
19620 		for (ipif = till->ill_ipif; ipif != NULL;
19621 		    ipif = ipif->ipif_next) {
19622 			if (!IPIF_CAN_LOOKUP(ipif))
19623 				continue;
19624 			/* Always skip NOLOCAL and ANYCAST interfaces */
19625 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
19626 				continue;
19627 			if (!(ipif->ipif_flags & IPIF_UP))
19628 				continue;
19629 			if (ipif->ipif_zoneid != zoneid)
19630 				continue;
19631 			/*
19632 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
19633 			 * but are not valid as source addresses.
19634 			 */
19635 			if (ipif->ipif_lcl_addr == INADDR_ANY)
19636 				continue;
19637 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
19638 				if (ipif_dep == NULL ||
19639 				    (ipif->ipif_net_mask & dst) ==
19640 				    ipif->ipif_subnet)
19641 					ipif_dep = ipif;
19642 				continue;
19643 			}
19644 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
19645 				/* found a source address in the same subnet */
19646 				if (same_subnet_only == B_FALSE) {
19647 					same_subnet_only = B_TRUE;
19648 					index = 0;
19649 				}
19650 				ipif_same_found = B_TRUE;
19651 			} else {
19652 				if (same_subnet_only == B_TRUE ||
19653 				    ipif_other_found == B_TRUE)
19654 					continue;
19655 				ipif_other_found = B_TRUE;
19656 			}
19657 			ipif_arr[index++] = ipif;
19658 			if (index == MAX_IPIF_SELECT_SOURCE) {
19659 				wrapped = B_TRUE;
19660 				index = 0;
19661 			}
19662 			if (ipif_same_found == B_TRUE)
19663 				break;
19664 		}
19665 	}
19666 
19667 	if (ipif_arr[0] == NULL) {
19668 		ipif = ipif_dep;
19669 	} else {
19670 		if (wrapped)
19671 			index = MAX_IPIF_SELECT_SOURCE;
19672 		ipif = ipif_arr[ipif_rand() % index];
19673 		ASSERT(ipif != NULL);
19674 	}
19675 
19676 	if (ipif != NULL) {
19677 		mutex_enter(&ipif->ipif_ill->ill_lock);
19678 		if (!IPIF_CAN_LOOKUP(ipif)) {
19679 			mutex_exit(&ipif->ipif_ill->ill_lock);
19680 			goto retry;
19681 		}
19682 		ipif_refhold_locked(ipif);
19683 		mutex_exit(&ipif->ipif_ill->ill_lock);
19684 	}
19685 
19686 	rw_exit(&ill_g_lock);
19687 	if (usill != NULL)
19688 		ill_refrele(usill);
19689 
19690 #ifdef DEBUG
19691 	if (ipif == NULL) {
19692 		char buf1[INET6_ADDRSTRLEN];
19693 
19694 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
19695 		    ill->ill_name,
19696 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
19697 	} else {
19698 		char buf1[INET6_ADDRSTRLEN];
19699 		char buf2[INET6_ADDRSTRLEN];
19700 
19701 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
19702 		    ipif->ipif_ill->ill_name,
19703 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
19704 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
19705 		    buf2, sizeof (buf2))));
19706 	}
19707 #endif /* DEBUG */
19708 	return (ipif);
19709 }
19710 
19711 
19712 /*
19713  * If old_ipif is not NULL, see if ipif was derived from old
19714  * ipif and if so, recreate the interface route by re-doing
19715  * source address selection. This happens when ipif_down ->
19716  * ipif_update_other_ipifs calls us.
19717  *
19718  * If old_ipif is NULL, just redo the source address selection
19719  * if needed. This happens when illgrp_insert or ipif_up_done
19720  * calls us.
19721  */
19722 static void
19723 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
19724 {
19725 	ire_t *ire;
19726 	ire_t *ipif_ire;
19727 	queue_t *stq;
19728 	ipif_t *nipif;
19729 	ill_t *ill;
19730 	boolean_t need_rele = B_FALSE;
19731 
19732 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
19733 	ASSERT(IAM_WRITER_IPIF(ipif));
19734 
19735 	ill = ipif->ipif_ill;
19736 	if (!(ipif->ipif_flags &
19737 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
19738 		/*
19739 		 * Can't possibly have borrowed the source
19740 		 * from old_ipif.
19741 		 */
19742 		return;
19743 	}
19744 
19745 	/*
19746 	 * Is there any work to be done? No work if the address
19747 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
19748 	 * ipif_select_source() does not borrow addresses from
19749 	 * NOLOCAL and ANYCAST interfaces).
19750 	 */
19751 	if ((old_ipif != NULL) &&
19752 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
19753 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
19754 	    (old_ipif->ipif_flags &
19755 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
19756 		return;
19757 	}
19758 
19759 	/*
19760 	 * Perform the same checks as when creating the
19761 	 * IRE_INTERFACE in ipif_up_done.
19762 	 */
19763 	if (!(ipif->ipif_flags & IPIF_UP))
19764 		return;
19765 
19766 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
19767 	    (ipif->ipif_subnet == INADDR_ANY))
19768 		return;
19769 
19770 	ipif_ire = ipif_to_ire(ipif);
19771 	if (ipif_ire == NULL)
19772 		return;
19773 
19774 	/*
19775 	 * We know that ipif uses some other source for its
19776 	 * IRE_INTERFACE. Is it using the source of this
19777 	 * old_ipif?
19778 	 */
19779 	if (old_ipif != NULL &&
19780 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
19781 		ire_refrele(ipif_ire);
19782 		return;
19783 	}
19784 	if (ip_debug > 2) {
19785 		/* ip1dbg */
19786 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
19787 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
19788 	}
19789 
19790 	stq = ipif_ire->ire_stq;
19791 
19792 	/*
19793 	 * Can't use our source address. Select a different
19794 	 * source address for the IRE_INTERFACE.
19795 	 */
19796 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
19797 	if (nipif == NULL) {
19798 		/* Last resort - all ipif's have IPIF_NOLOCAL */
19799 		nipif = ipif;
19800 	} else {
19801 		need_rele = B_TRUE;
19802 	}
19803 
19804 	ire = ire_create(
19805 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
19806 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
19807 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
19808 	    NULL,				/* no gateway */
19809 	    NULL,
19810 	    &ipif->ipif_mtu,			/* max frag */
19811 	    NULL,				/* fast path header */
19812 	    NULL,				/* no recv from queue */
19813 	    stq,				/* send-to queue */
19814 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
19815 	    ill->ill_resolver_mp,		/* xmit header */
19816 	    ipif,
19817 	    NULL,
19818 	    0,
19819 	    0,
19820 	    0,
19821 	    0,
19822 	    &ire_uinfo_null);
19823 
19824 	if (ire != NULL) {
19825 		ire_t *ret_ire;
19826 		int error;
19827 
19828 		/*
19829 		 * We don't need ipif_ire anymore. We need to delete
19830 		 * before we add so that ire_add does not detect
19831 		 * duplicates.
19832 		 */
19833 		ire_delete(ipif_ire);
19834 		ret_ire = ire;
19835 		error = ire_add(&ret_ire, NULL, NULL, NULL);
19836 		ASSERT(error == 0);
19837 		ASSERT(ire == ret_ire);
19838 		/* Held in ire_add */
19839 		ire_refrele(ret_ire);
19840 	}
19841 	/*
19842 	 * Either we are falling through from above or could not
19843 	 * allocate a replacement.
19844 	 */
19845 	ire_refrele(ipif_ire);
19846 	if (need_rele)
19847 		ipif_refrele(nipif);
19848 }
19849 
19850 /*
19851  * This old_ipif is going away.
19852  *
19853  * Determine if any other ipif's is using our address as
19854  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
19855  * IPIF_DEPRECATED).
19856  * Find the IRE_INTERFACE for such ipifs and recreate them
19857  * to use an different source address following the rules in
19858  * ipif_up_done.
19859  *
19860  * This function takes an illgrp as an argument so that illgrp_delete
19861  * can call this to update source address even after deleting the
19862  * old_ipif->ipif_ill from the ill group.
19863  */
19864 static void
19865 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
19866 {
19867 	ipif_t *ipif;
19868 	ill_t *ill;
19869 	char	buf[INET6_ADDRSTRLEN];
19870 
19871 	ASSERT(IAM_WRITER_IPIF(old_ipif));
19872 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
19873 
19874 	ill = old_ipif->ipif_ill;
19875 
19876 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
19877 	    ill->ill_name,
19878 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
19879 	    buf, sizeof (buf))));
19880 	/*
19881 	 * If this part of a group, look at all ills as ipif_select_source
19882 	 * borrows source address across all the ills in the group.
19883 	 */
19884 	if (illgrp != NULL)
19885 		ill = illgrp->illgrp_ill;
19886 
19887 	for (; ill != NULL; ill = ill->ill_group_next) {
19888 		for (ipif = ill->ill_ipif; ipif != NULL;
19889 		    ipif = ipif->ipif_next) {
19890 
19891 			if (ipif == old_ipif)
19892 				continue;
19893 
19894 			ipif_recreate_interface_routes(old_ipif, ipif);
19895 		}
19896 	}
19897 }
19898 
19899 /* ARGSUSED */
19900 int
19901 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
19902 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
19903 {
19904 	/*
19905 	 * ill_phyint_reinit merged the v4 and v6 into a single
19906 	 * ipsq. Could also have become part of a ipmp group in the
19907 	 * process, and we might not have been able to complete the
19908 	 * operation in ipif_set_values, if we could not become
19909 	 * exclusive.  If so restart it here.
19910 	 */
19911 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
19912 }
19913 
19914 
19915 /* ARGSUSED */
19916 int
19917 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
19918     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
19919 {
19920 	queue_t		*q1 = q;
19921 	char 		*cp;
19922 	char		interf_name[LIFNAMSIZ];
19923 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
19924 
19925 	if (!q->q_next) {
19926 		ip1dbg((
19927 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
19928 		return (EINVAL);
19929 	}
19930 
19931 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
19932 		return (EALREADY);
19933 
19934 	do {
19935 		q1 = q1->q_next;
19936 	} while (q1->q_next);
19937 	cp = q1->q_qinfo->qi_minfo->mi_idname;
19938 	(void) sprintf(interf_name, "%s%d", cp, ppa);
19939 
19940 	/*
19941 	 * Here we are not going to delay the ioack until after
19942 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
19943 	 * original ioctl message before sending the requests.
19944 	 */
19945 	return (ipif_set_values(q, mp, interf_name, &ppa));
19946 }
19947 
19948 /* ARGSUSED */
19949 int
19950 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
19951     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
19952 {
19953 	return (ENXIO);
19954 }
19955 
19956 /*
19957  * Net and subnet broadcast ire's are now specific to the particular
19958  * physical interface (ill) and not to any one locigal interface (ipif).
19959  * However, if a particular logical interface is being taken down, it's
19960  * associated ire's will be taken down as well.  Hence, when we go to
19961  * take down or change the local address, broadcast address or netmask
19962  * of a specific logical interface, we must check to make sure that we
19963  * have valid net and subnet broadcast ire's for the other logical
19964  * interfaces which may have been shared with the logical interface
19965  * being brought down or changed.
19966  *
19967  * There is one set of 0.0.0.0 and 255.255.255.255 per ill. Usually it
19968  * is tied to the first interface coming UP. If that ipif is going down,
19969  * we need to recreate them on the next valid ipif.
19970  *
19971  * Note: assume that the ipif passed in is still up so that it's IRE
19972  * entries are still valid.
19973  */
19974 static void
19975 ipif_check_bcast_ires(ipif_t *test_ipif)
19976 {
19977 	ipif_t	*ipif;
19978 	ire_t	*test_subnet_ire, *test_net_ire;
19979 	ire_t	*test_allzero_ire, *test_allone_ire;
19980 	ire_t	*ire_array[12];
19981 	ire_t	**irep = &ire_array[0];
19982 	ire_t	**irep1;
19983 
19984 	ipaddr_t net_addr, subnet_addr, net_mask, subnet_mask;
19985 	ipaddr_t test_net_addr, test_subnet_addr;
19986 	ipaddr_t test_net_mask, test_subnet_mask;
19987 	boolean_t need_net_bcast_ire = B_FALSE;
19988 	boolean_t need_subnet_bcast_ire = B_FALSE;
19989 	boolean_t allzero_bcast_ire_created = B_FALSE;
19990 	boolean_t allone_bcast_ire_created = B_FALSE;
19991 	boolean_t net_bcast_ire_created = B_FALSE;
19992 	boolean_t subnet_bcast_ire_created = B_FALSE;
19993 
19994 	ipif_t  *backup_ipif_net = (ipif_t *)NULL;
19995 	ipif_t  *backup_ipif_subnet = (ipif_t *)NULL;
19996 	ipif_t  *backup_ipif_allzeros = (ipif_t *)NULL;
19997 	ipif_t  *backup_ipif_allones = (ipif_t *)NULL;
19998 	uint64_t check_flags = IPIF_DEPRECATED | IPIF_NOLOCAL | IPIF_ANYCAST;
19999 
20000 	ASSERT(!test_ipif->ipif_isv6);
20001 	ASSERT(IAM_WRITER_IPIF(test_ipif));
20002 
20003 	/*
20004 	 * No broadcast IREs for the LOOPBACK interface
20005 	 * or others such as point to point and IPIF_NOXMIT.
20006 	 */
20007 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
20008 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
20009 		return;
20010 
20011 	test_allzero_ire = ire_ctable_lookup(0, 0, IRE_BROADCAST,
20012 	    test_ipif, ALL_ZONES, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20013 
20014 	test_allone_ire = ire_ctable_lookup(INADDR_BROADCAST, 0, IRE_BROADCAST,
20015 	    test_ipif, ALL_ZONES, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20016 
20017 	test_net_mask = ip_net_mask(test_ipif->ipif_subnet);
20018 	test_subnet_mask = test_ipif->ipif_net_mask;
20019 
20020 	/*
20021 	 * If no net mask set, assume the default based on net class.
20022 	 */
20023 	if (test_subnet_mask == 0)
20024 		test_subnet_mask = test_net_mask;
20025 
20026 	/*
20027 	 * Check if there is a network broadcast ire associated with this ipif
20028 	 */
20029 	test_net_addr = test_net_mask  & test_ipif->ipif_subnet;
20030 	test_net_ire = ire_ctable_lookup(test_net_addr, 0, IRE_BROADCAST,
20031 	    test_ipif, ALL_ZONES, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20032 
20033 	/*
20034 	 * Check if there is a subnet broadcast IRE associated with this ipif
20035 	 */
20036 	test_subnet_addr = test_subnet_mask  & test_ipif->ipif_subnet;
20037 	test_subnet_ire = ire_ctable_lookup(test_subnet_addr, 0, IRE_BROADCAST,
20038 	    test_ipif, ALL_ZONES, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20039 
20040 	/*
20041 	 * No broadcast ire's associated with this ipif.
20042 	 */
20043 	if ((test_subnet_ire == NULL) && (test_net_ire == NULL) &&
20044 	    (test_allzero_ire == NULL) && (test_allone_ire == NULL)) {
20045 		return;
20046 	}
20047 
20048 	/*
20049 	 * We have established which bcast ires have to be replaced.
20050 	 * Next we try to locate ipifs that match there ires.
20051 	 * The rules are simple: If we find an ipif that matches on the subnet
20052 	 * address it will also match on the net address, the allzeros and
20053 	 * allones address. Any ipif that matches only on the net address will
20054 	 * also match the allzeros and allones addresses.
20055 	 * The other criterion is the ipif_flags. We look for non-deprecated
20056 	 * (and non-anycast and non-nolocal) ipifs as the best choice.
20057 	 * ipifs with check_flags matching (deprecated, etc) are used only
20058 	 * if good ipifs are not available. While looping, we save existing
20059 	 * deprecated ipifs as backup_ipif.
20060 	 * We loop through all the ipifs for this ill looking for ipifs
20061 	 * whose broadcast addr match the ipif passed in, but do not have
20062 	 * their own broadcast ires. For creating 0.0.0.0 and
20063 	 * 255.255.255.255 we just need an ipif on this ill to create.
20064 	 */
20065 	for (ipif = test_ipif->ipif_ill->ill_ipif; ipif != NULL;
20066 	    ipif = ipif->ipif_next) {
20067 
20068 		ASSERT(!ipif->ipif_isv6);
20069 		/*
20070 		 * Already checked the ipif passed in.
20071 		 */
20072 		if (ipif == test_ipif) {
20073 			continue;
20074 		}
20075 
20076 		/*
20077 		 * We only need to recreate broadcast ires if another ipif in
20078 		 * the same zone uses them. The new ires must be created in the
20079 		 * same zone.
20080 		 */
20081 		if (ipif->ipif_zoneid != test_ipif->ipif_zoneid) {
20082 			continue;
20083 		}
20084 
20085 		/*
20086 		 * Only interested in logical interfaces with valid local
20087 		 * addresses or with the ability to broadcast.
20088 		 */
20089 		if ((ipif->ipif_subnet == 0) ||
20090 		    !(ipif->ipif_flags & IPIF_BROADCAST) ||
20091 		    (ipif->ipif_flags & IPIF_NOXMIT) ||
20092 		    !(ipif->ipif_flags & IPIF_UP)) {
20093 			continue;
20094 		}
20095 		/*
20096 		 * Check if there is a net broadcast ire for this
20097 		 * net address.  If it turns out that the ipif we are
20098 		 * about to take down owns this ire, we must make a
20099 		 * new one because it is potentially going away.
20100 		 */
20101 		if (test_net_ire && (!net_bcast_ire_created)) {
20102 			net_mask = ip_net_mask(ipif->ipif_subnet);
20103 			net_addr = net_mask & ipif->ipif_subnet;
20104 			if (net_addr == test_net_addr) {
20105 				need_net_bcast_ire = B_TRUE;
20106 				/*
20107 				 * Use DEPRECATED ipif only if no good
20108 				 * ires are available. subnet_addr is
20109 				 * a better match than net_addr.
20110 				 */
20111 				if ((ipif->ipif_flags & check_flags) &&
20112 				    (backup_ipif_net == NULL)) {
20113 					backup_ipif_net = ipif;
20114 				}
20115 			}
20116 		}
20117 		/*
20118 		 * Check if there is a subnet broadcast ire for this
20119 		 * net address.  If it turns out that the ipif we are
20120 		 * about to take down owns this ire, we must make a
20121 		 * new one because it is potentially going away.
20122 		 */
20123 		if (test_subnet_ire && (!subnet_bcast_ire_created)) {
20124 			subnet_mask = ipif->ipif_net_mask;
20125 			subnet_addr = ipif->ipif_subnet;
20126 			if (subnet_addr == test_subnet_addr) {
20127 				need_subnet_bcast_ire = B_TRUE;
20128 				if ((ipif->ipif_flags & check_flags) &&
20129 				    (backup_ipif_subnet == NULL)) {
20130 					backup_ipif_subnet = ipif;
20131 				}
20132 			}
20133 		}
20134 
20135 
20136 		/* Short circuit here if this ipif is deprecated */
20137 		if (ipif->ipif_flags & check_flags) {
20138 			if ((test_allzero_ire != NULL) &&
20139 			    (!allzero_bcast_ire_created) &&
20140 			    (backup_ipif_allzeros == NULL)) {
20141 				backup_ipif_allzeros = ipif;
20142 			}
20143 			if ((test_allone_ire != NULL) &&
20144 			    (!allone_bcast_ire_created) &&
20145 			    (backup_ipif_allones == NULL)) {
20146 				backup_ipif_allones = ipif;
20147 			}
20148 			continue;
20149 		}
20150 
20151 		/*
20152 		 * Found an ipif which has the same broadcast ire as the
20153 		 * ipif passed in and the ipif passed in "owns" the ire.
20154 		 * Create new broadcast ire's for this broadcast addr.
20155 		 */
20156 		if (need_net_bcast_ire && !net_bcast_ire_created) {
20157 			irep = ire_create_bcast(ipif, net_addr, irep);
20158 			irep = ire_create_bcast(ipif,
20159 			    ~net_mask | net_addr, irep);
20160 			net_bcast_ire_created = B_TRUE;
20161 		}
20162 		if (need_subnet_bcast_ire && !subnet_bcast_ire_created) {
20163 			irep = ire_create_bcast(ipif, subnet_addr, irep);
20164 			irep = ire_create_bcast(ipif,
20165 			    ~subnet_mask | subnet_addr, irep);
20166 			subnet_bcast_ire_created = B_TRUE;
20167 		}
20168 		if (test_allzero_ire != NULL && !allzero_bcast_ire_created) {
20169 			irep = ire_create_bcast(ipif, 0, irep);
20170 			allzero_bcast_ire_created = B_TRUE;
20171 		}
20172 		if (test_allone_ire != NULL && !allone_bcast_ire_created) {
20173 			irep = ire_create_bcast(ipif, INADDR_BROADCAST, irep);
20174 			allone_bcast_ire_created = B_TRUE;
20175 		}
20176 		/*
20177 		 * Once we have created all the appropriate ires, we
20178 		 * just break out of this loop to add what we have created.
20179 		 * This has been indented similar to ire_match_args for
20180 		 * readability.
20181 		 */
20182 		if (((test_net_ire == NULL) ||
20183 			(net_bcast_ire_created)) &&
20184 		    ((test_subnet_ire == NULL) ||
20185 			(subnet_bcast_ire_created)) &&
20186 		    ((test_allzero_ire == NULL) ||
20187 			(allzero_bcast_ire_created)) &&
20188 		    ((test_allone_ire == NULL) ||
20189 			(allone_bcast_ire_created))) {
20190 			break;
20191 		}
20192 	}
20193 
20194 	/*
20195 	 * Create bcast ires on deprecated ipifs if no non-deprecated ipifs
20196 	 * exist. 6 pairs of bcast ires are needed.
20197 	 * Note - the old ires are deleted in ipif_down.
20198 	 */
20199 	if (need_net_bcast_ire && !net_bcast_ire_created && backup_ipif_net) {
20200 		ipif = backup_ipif_net;
20201 		irep = ire_create_bcast(ipif, net_addr, irep);
20202 		irep = ire_create_bcast(ipif, ~net_mask | net_addr, irep);
20203 		net_bcast_ire_created = B_TRUE;
20204 	}
20205 	if (need_subnet_bcast_ire && !subnet_bcast_ire_created &&
20206 	    backup_ipif_subnet) {
20207 		ipif = backup_ipif_subnet;
20208 		irep = ire_create_bcast(ipif, subnet_addr, irep);
20209 		irep = ire_create_bcast(ipif,
20210 		    ~subnet_mask | subnet_addr, irep);
20211 		subnet_bcast_ire_created = B_TRUE;
20212 	}
20213 	if (test_allzero_ire != NULL && !allzero_bcast_ire_created &&
20214 	    backup_ipif_allzeros) {
20215 		irep = ire_create_bcast(backup_ipif_allzeros, 0, irep);
20216 		allzero_bcast_ire_created = B_TRUE;
20217 	}
20218 	if (test_allone_ire != NULL && !allone_bcast_ire_created &&
20219 	    backup_ipif_allones) {
20220 		irep = ire_create_bcast(backup_ipif_allones,
20221 		    INADDR_BROADCAST, irep);
20222 		allone_bcast_ire_created = B_TRUE;
20223 	}
20224 
20225 	/*
20226 	 * If we can't create all of them, don't add any of them.
20227 	 * Code in ip_wput_ire and ire_to_ill assumes that we
20228 	 * always have a non-loopback copy and loopback copy
20229 	 * for a given address.
20230 	 */
20231 	for (irep1 = irep; irep1 > ire_array; ) {
20232 		irep1--;
20233 		if (*irep1 == NULL) {
20234 			ip0dbg(("ipif_check_bcast_ires: can't create "
20235 			    "IRE_BROADCAST, memory allocation failure\n"));
20236 			while (irep > ire_array) {
20237 				irep--;
20238 				if (*irep != NULL)
20239 					ire_delete(*irep);
20240 			}
20241 			goto bad;
20242 		}
20243 	}
20244 	for (irep1 = irep; irep1 > ire_array; ) {
20245 		int error;
20246 
20247 		irep1--;
20248 		error = ire_add(irep1, NULL, NULL, NULL);
20249 		if (error == 0) {
20250 			ire_refrele(*irep1);		/* Held in ire_add */
20251 		}
20252 	}
20253 bad:
20254 	if (test_allzero_ire != NULL)
20255 		ire_refrele(test_allzero_ire);
20256 	if (test_allone_ire != NULL)
20257 		ire_refrele(test_allone_ire);
20258 	if (test_net_ire != NULL)
20259 		ire_refrele(test_net_ire);
20260 	if (test_subnet_ire != NULL)
20261 		ire_refrele(test_subnet_ire);
20262 }
20263 
20264 /*
20265  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
20266  * from lifr_flags and the name from lifr_name.
20267  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
20268  * since ipif_lookup_on_name uses the _isv6 flags when matching.
20269  * Returns EINPROGRESS when mp has been consumed by queueing it on
20270  * ill_pending_mp and the ioctl will complete in ip_rput.
20271  */
20272 /* ARGSUSED */
20273 int
20274 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20275     ip_ioctl_cmd_t *ipip, void *if_req)
20276 {
20277 	int	err;
20278 	ill_t	*ill;
20279 	struct lifreq *lifr = (struct lifreq *)if_req;
20280 
20281 	ASSERT(ipif != NULL);
20282 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
20283 	ASSERT(q->q_next != NULL);
20284 
20285 	ill = (ill_t *)q->q_ptr;
20286 	/*
20287 	 * If we are not writer on 'q' then this interface exists already
20288 	 * and previous lookups (ipif_extract_lifreq_cmn) found this ipif.
20289 	 * So return EALREADY
20290 	 */
20291 	if (ill != ipif->ipif_ill)
20292 		return (EALREADY);
20293 
20294 	if (ill->ill_name[0] != '\0')
20295 		return (EALREADY);
20296 
20297 	/*
20298 	 * Set all the flags. Allows all kinds of override. Provide some
20299 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
20300 	 * unless there is either multicast/broadcast support in the driver
20301 	 * or it is a pt-pt link.
20302 	 */
20303 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
20304 		/* Meaningless to IP thus don't allow them to be set. */
20305 		ip1dbg(("ip_setname: EINVAL 1\n"));
20306 		return (EINVAL);
20307 	}
20308 	/*
20309 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
20310 	 * ill_bcast_addr_length info.
20311 	 */
20312 	if (!ill->ill_needs_attach &&
20313 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
20314 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
20315 	    ill->ill_bcast_addr_length == 0)) {
20316 		/* Link not broadcast/pt-pt capable i.e. no multicast */
20317 		ip1dbg(("ip_setname: EINVAL 2\n"));
20318 		return (EINVAL);
20319 	}
20320 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
20321 	    ((lifr->lifr_flags & IFF_IPV6) ||
20322 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
20323 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
20324 		ip1dbg(("ip_setname: EINVAL 3\n"));
20325 		return (EINVAL);
20326 	}
20327 	if (lifr->lifr_flags & IFF_UP) {
20328 		/* Can only be set with SIOCSLIFFLAGS */
20329 		ip1dbg(("ip_setname: EINVAL 4\n"));
20330 		return (EINVAL);
20331 	}
20332 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
20333 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
20334 		ip1dbg(("ip_setname: EINVAL 5\n"));
20335 		return (EINVAL);
20336 	}
20337 	/*
20338 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
20339 	 */
20340 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
20341 	    !(lifr->lifr_flags & IFF_IPV6) &&
20342 	    !(ipif->ipif_isv6)) {
20343 		ip1dbg(("ip_setname: EINVAL 6\n"));
20344 		return (EINVAL);
20345 	}
20346 
20347 	/*
20348 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
20349 	 * we have all the flags here. So, we assign rather than we OR.
20350 	 * We can't OR the flags here because we don't want to set
20351 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
20352 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
20353 	 * on lifr_flags value here.
20354 	 */
20355 	/*
20356 	 * This ill has not been inserted into the global list.
20357 	 * So we are still single threaded and don't need any lock
20358 	 */
20359 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS;
20360 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
20361 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
20362 
20363 	/* We started off as V4. */
20364 	if (ill->ill_flags & ILLF_IPV6) {
20365 		ill->ill_phyint->phyint_illv6 = ill;
20366 		ill->ill_phyint->phyint_illv4 = NULL;
20367 	}
20368 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
20369 	return (err);
20370 }
20371 
20372 /* ARGSUSED */
20373 int
20374 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20375     ip_ioctl_cmd_t *ipip, void *if_req)
20376 {
20377 	/*
20378 	 * ill_phyint_reinit merged the v4 and v6 into a single
20379 	 * ipsq. Could also have become part of a ipmp group in the
20380 	 * process, and we might not have been able to complete the
20381 	 * slifname in ipif_set_values, if we could not become
20382 	 * exclusive.  If so restart it here
20383 	 */
20384 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
20385 }
20386 
20387 /*
20388  * Return a pointer to the ipif which matches the index, IP version type and
20389  * zoneid.
20390  */
20391 ipif_t *
20392 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
20393     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
20394 {
20395 	ill_t	*ill;
20396 	ipsq_t  *ipsq;
20397 	phyint_t *phyi;
20398 	ipif_t	*ipif;
20399 
20400 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
20401 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
20402 
20403 	if (err != NULL)
20404 		*err = 0;
20405 
20406 	/*
20407 	 * Indexes are stored in the phyint - a common structure
20408 	 * to both IPv4 and IPv6.
20409 	 */
20410 
20411 	rw_enter(&ill_g_lock, RW_READER);
20412 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
20413 	    (void *) &index, NULL);
20414 	if (phyi != NULL) {
20415 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
20416 		if (ill == NULL) {
20417 			rw_exit(&ill_g_lock);
20418 			if (err != NULL)
20419 				*err = ENXIO;
20420 			return (NULL);
20421 		}
20422 		GRAB_CONN_LOCK(q);
20423 		mutex_enter(&ill->ill_lock);
20424 		if (ILL_CAN_LOOKUP(ill)) {
20425 			for (ipif = ill->ill_ipif; ipif != NULL;
20426 			    ipif = ipif->ipif_next) {
20427 				if (IPIF_CAN_LOOKUP(ipif) &&
20428 				    (zoneid == ALL_ZONES ||
20429 				    zoneid == ipif->ipif_zoneid)) {
20430 					ipif_refhold_locked(ipif);
20431 					mutex_exit(&ill->ill_lock);
20432 					RELEASE_CONN_LOCK(q);
20433 					rw_exit(&ill_g_lock);
20434 					return (ipif);
20435 				}
20436 			}
20437 		} else if (ILL_CAN_WAIT(ill, q)) {
20438 			ipsq = ill->ill_phyint->phyint_ipsq;
20439 			mutex_enter(&ipsq->ipsq_lock);
20440 			rw_exit(&ill_g_lock);
20441 			mutex_exit(&ill->ill_lock);
20442 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
20443 			mutex_exit(&ipsq->ipsq_lock);
20444 			RELEASE_CONN_LOCK(q);
20445 			*err = EINPROGRESS;
20446 			return (NULL);
20447 		}
20448 		mutex_exit(&ill->ill_lock);
20449 		RELEASE_CONN_LOCK(q);
20450 	}
20451 	rw_exit(&ill_g_lock);
20452 	if (err != NULL)
20453 		*err = ENXIO;
20454 	return (NULL);
20455 }
20456 
20457 typedef struct conn_change_s {
20458 	uint_t cc_old_ifindex;
20459 	uint_t cc_new_ifindex;
20460 } conn_change_t;
20461 
20462 /*
20463  * ipcl_walk function for changing interface index.
20464  */
20465 static void
20466 conn_change_ifindex(conn_t *connp, caddr_t arg)
20467 {
20468 	conn_change_t *connc;
20469 	uint_t old_ifindex;
20470 	uint_t new_ifindex;
20471 	int i;
20472 	ilg_t *ilg;
20473 
20474 	connc = (conn_change_t *)arg;
20475 	old_ifindex = connc->cc_old_ifindex;
20476 	new_ifindex = connc->cc_new_ifindex;
20477 
20478 	if (connp->conn_orig_bound_ifindex == old_ifindex)
20479 		connp->conn_orig_bound_ifindex = new_ifindex;
20480 
20481 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
20482 		connp->conn_orig_multicast_ifindex = new_ifindex;
20483 
20484 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
20485 		connp->conn_orig_xmit_ifindex = new_ifindex;
20486 
20487 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
20488 		ilg = &connp->conn_ilg[i];
20489 		if (ilg->ilg_orig_ifindex == old_ifindex)
20490 			ilg->ilg_orig_ifindex = new_ifindex;
20491 	}
20492 }
20493 
20494 /*
20495  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
20496  * to new_index if it matches the old_index.
20497  *
20498  * Failovers typically happen within a group of ills. But somebody
20499  * can remove an ill from the group after a failover happened. If
20500  * we are setting the ifindex after this, we potentially need to
20501  * look at all the ills rather than just the ones in the group.
20502  * We cut down the work by looking at matching ill_net_types
20503  * and ill_types as we could not possibly grouped them together.
20504  */
20505 static void
20506 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
20507 {
20508 	ill_t *ill;
20509 	ipif_t *ipif;
20510 	uint_t old_ifindex;
20511 	uint_t new_ifindex;
20512 	ilm_t *ilm;
20513 	ill_walk_context_t ctx;
20514 
20515 	old_ifindex = connc->cc_old_ifindex;
20516 	new_ifindex = connc->cc_new_ifindex;
20517 
20518 	rw_enter(&ill_g_lock, RW_READER);
20519 	ill = ILL_START_WALK_ALL(&ctx);
20520 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
20521 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
20522 			(ill_orig->ill_type != ill->ill_type)) {
20523 			continue;
20524 		}
20525 		for (ipif = ill->ill_ipif; ipif != NULL;
20526 				ipif = ipif->ipif_next) {
20527 			if (ipif->ipif_orig_ifindex == old_ifindex)
20528 				ipif->ipif_orig_ifindex = new_ifindex;
20529 		}
20530 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
20531 			if (ilm->ilm_orig_ifindex == old_ifindex)
20532 				ilm->ilm_orig_ifindex = new_ifindex;
20533 		}
20534 	}
20535 	rw_exit(&ill_g_lock);
20536 }
20537 
20538 /*
20539  * We first need to ensure that the new index is unique, and
20540  * then carry the change across both v4 and v6 ill representation
20541  * of the physical interface.
20542  */
20543 /* ARGSUSED */
20544 int
20545 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20546     ip_ioctl_cmd_t *ipip, void *ifreq)
20547 {
20548 	ill_t		*ill;
20549 	ill_t		*ill_other;
20550 	phyint_t	*phyi;
20551 	int		old_index;
20552 	conn_change_t	connc;
20553 	struct ifreq	*ifr = (struct ifreq *)ifreq;
20554 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20555 	uint_t	index;
20556 	ill_t	*ill_v4;
20557 	ill_t	*ill_v6;
20558 
20559 	if (ipip->ipi_cmd_type == IF_CMD)
20560 		index = ifr->ifr_index;
20561 	else
20562 		index = lifr->lifr_index;
20563 
20564 	/*
20565 	 * Only allow on physical interface. Also, index zero is illegal.
20566 	 *
20567 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
20568 	 *
20569 	 * 1) If PHYI_FAILED is set, a failover could have happened which
20570 	 *    implies a possible failback might have to happen. As failback
20571 	 *    depends on the old index, we should fail setting the index.
20572 	 *
20573 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
20574 	 *    any addresses or multicast memberships are failed over to
20575 	 *    a non-STANDBY interface. As failback depends on the old
20576 	 *    index, we should fail setting the index for this case also.
20577 	 *
20578 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
20579 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
20580 	 */
20581 	ill = ipif->ipif_ill;
20582 	phyi = ill->ill_phyint;
20583 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
20584 	    ipif->ipif_id != 0 || index == 0) {
20585 		return (EINVAL);
20586 	}
20587 	old_index = phyi->phyint_ifindex;
20588 
20589 	/* If the index is not changing, no work to do */
20590 	if (old_index == index)
20591 		return (0);
20592 
20593 	/*
20594 	 * Use ill_lookup_on_ifindex to determine if the
20595 	 * new index is unused and if so allow the change.
20596 	 */
20597 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL);
20598 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL);
20599 	if (ill_v6 != NULL || ill_v4 != NULL) {
20600 		if (ill_v4 != NULL)
20601 			ill_refrele(ill_v4);
20602 		if (ill_v6 != NULL)
20603 			ill_refrele(ill_v6);
20604 		return (EBUSY);
20605 	}
20606 
20607 	/*
20608 	 * The new index is unused. Set it in the phyint.
20609 	 * Locate the other ill so that we can send a routing
20610 	 * sockets message.
20611 	 */
20612 	if (ill->ill_isv6) {
20613 		ill_other = phyi->phyint_illv4;
20614 	} else {
20615 		ill_other = phyi->phyint_illv6;
20616 	}
20617 
20618 	phyi->phyint_ifindex = index;
20619 
20620 	connc.cc_old_ifindex = old_index;
20621 	connc.cc_new_ifindex = index;
20622 	ip_change_ifindex(ill, &connc);
20623 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc);
20624 
20625 	/* Send the routing sockets message */
20626 	ip_rts_ifmsg(ipif);
20627 	if (ill_other != NULL)
20628 		ip_rts_ifmsg(ill_other->ill_ipif);
20629 
20630 	return (0);
20631 }
20632 
20633 /* ARGSUSED */
20634 int
20635 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20636     ip_ioctl_cmd_t *ipip, void *ifreq)
20637 {
20638 	struct ifreq	*ifr = (struct ifreq *)ifreq;
20639 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20640 
20641 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
20642 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20643 	/* Get the interface index */
20644 	if (ipip->ipi_cmd_type == IF_CMD) {
20645 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
20646 	} else {
20647 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
20648 	}
20649 	return (0);
20650 }
20651 
20652 /* ARGSUSED */
20653 int
20654 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20655     ip_ioctl_cmd_t *ipip, void *ifreq)
20656 {
20657 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20658 
20659 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
20660 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20661 	/* Get the interface zone */
20662 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
20663 	lifr->lifr_zoneid = ipif->ipif_zoneid;
20664 	return (0);
20665 }
20666 
20667 /*
20668  * Set the zoneid of an interface.
20669  */
20670 /* ARGSUSED */
20671 int
20672 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20673     ip_ioctl_cmd_t *ipip, void *ifreq)
20674 {
20675 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20676 	int err = 0;
20677 	boolean_t need_up = B_FALSE;
20678 	zone_t *zptr;
20679 	zone_status_t status;
20680 	zoneid_t zoneid;
20681 
20682 	/* cannot assign instance zero to a non-global zone */
20683 	if (ipif->ipif_id == 0)
20684 		return (ENOTSUP);
20685 
20686 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
20687 	zoneid = lifr->lifr_zoneid;
20688 
20689 	/*
20690 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
20691 	 * the event of a race with the zone shutdown processing, since IP
20692 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
20693 	 * interface will be cleaned up even if the zone is shut down
20694 	 * immediately after the status check. If the interface can't be brought
20695 	 * down right away, and the zone is shut down before the restart
20696 	 * function is called, we resolve the possible races by rechecking the
20697 	 * zone status in the restart function.
20698 	 */
20699 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
20700 		return (EINVAL);
20701 	status = zone_status_get(zptr);
20702 	zone_rele(zptr);
20703 
20704 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
20705 		return (EINVAL);
20706 
20707 	if (ipif->ipif_flags & IPIF_UP) {
20708 		/*
20709 		 * If the interface is already marked up,
20710 		 * we call ipif_down which will take care
20711 		 * of ditching any IREs that have been set
20712 		 * up based on the old interface address.
20713 		 */
20714 		err = ipif_logical_down(ipif, q, mp);
20715 		if (err == EINPROGRESS)
20716 			return (err);
20717 		ipif_down_tail(ipif);
20718 		need_up = B_TRUE;
20719 	}
20720 
20721 	err = ip_sioctl_slifzone_tail(ipif, zoneid, q, mp, need_up);
20722 	return (err);
20723 }
20724 
20725 static int
20726 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
20727     queue_t *q, mblk_t *mp, boolean_t need_up)
20728 {
20729 	int	err = 0;
20730 
20731 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
20732 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20733 
20734 	/* Set the new zone id. */
20735 	ipif->ipif_zoneid = zoneid;
20736 
20737 	/* Update sctp list */
20738 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
20739 
20740 	if (need_up) {
20741 		/*
20742 		 * Now bring the interface back up.  If this
20743 		 * is the only IPIF for the ILL, ipif_up
20744 		 * will have to re-bind to the device, so
20745 		 * we may get back EINPROGRESS, in which
20746 		 * case, this IOCTL will get completed in
20747 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
20748 		 */
20749 		err = ipif_up(ipif, q, mp);
20750 	}
20751 	return (err);
20752 }
20753 
20754 /* ARGSUSED */
20755 int
20756 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20757     ip_ioctl_cmd_t *ipip, void *if_req)
20758 {
20759 	struct lifreq *lifr = (struct lifreq *)if_req;
20760 	zoneid_t zoneid;
20761 	zone_t *zptr;
20762 	zone_status_t status;
20763 
20764 	ASSERT(ipif->ipif_id != 0);
20765 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
20766 	zoneid = lifr->lifr_zoneid;
20767 
20768 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
20769 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20770 
20771 	/*
20772 	 * We recheck the zone status to resolve the following race condition:
20773 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
20774 	 * 2) hme0:1 is up and can't be brought down right away;
20775 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
20776 	 * 3) zone "myzone" is halted; the zone status switches to
20777 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
20778 	 * the interfaces to remove - hme0:1 is not returned because it's not
20779 	 * yet in "myzone", so it won't be removed;
20780 	 * 4) the restart function for SIOCSLIFZONE is called; without the
20781 	 * status check here, we would have hme0:1 in "myzone" after it's been
20782 	 * destroyed.
20783 	 * Note that if the status check fails, we need to bring the interface
20784 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
20785 	 * ipif_up_done[_v6]().
20786 	 */
20787 	status = ZONE_IS_UNINITIALIZED;
20788 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
20789 		status = zone_status_get(zptr);
20790 		zone_rele(zptr);
20791 	}
20792 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
20793 		if (ipif->ipif_isv6) {
20794 			(void) ipif_up_done_v6(ipif);
20795 		} else {
20796 			(void) ipif_up_done(ipif);
20797 		}
20798 		return (EINVAL);
20799 	}
20800 
20801 	ipif_down_tail(ipif);
20802 
20803 	return (ip_sioctl_slifzone_tail(ipif, zoneid, q, mp, B_TRUE));
20804 }
20805 
20806 /* ARGSUSED */
20807 int
20808 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20809 	ip_ioctl_cmd_t *ipip, void *ifreq)
20810 {
20811 	struct lifreq	*lifr = ifreq;
20812 
20813 	ASSERT(q->q_next == NULL);
20814 	ASSERT(CONN_Q(q));
20815 
20816 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
20817 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20818 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
20819 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
20820 
20821 	return (0);
20822 }
20823 
20824 
20825 /* Find the previous ILL in this usesrc group */
20826 static ill_t *
20827 ill_prev_usesrc(ill_t *uill)
20828 {
20829 	ill_t *ill;
20830 
20831 	for (ill = uill->ill_usesrc_grp_next;
20832 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
20833 	    ill = ill->ill_usesrc_grp_next)
20834 		/* do nothing */;
20835 	return (ill);
20836 }
20837 
20838 /*
20839  * Release all members of the usesrc group. This routine is called
20840  * from ill_delete when the interface being unplumbed is the
20841  * group head.
20842  */
20843 static void
20844 ill_disband_usesrc_group(ill_t *uill)
20845 {
20846 	ill_t *next_ill, *tmp_ill;
20847 	ASSERT(RW_WRITE_HELD(&ill_g_usesrc_lock));
20848 	next_ill = uill->ill_usesrc_grp_next;
20849 
20850 	do {
20851 		ASSERT(next_ill != NULL);
20852 		tmp_ill = next_ill->ill_usesrc_grp_next;
20853 		ASSERT(tmp_ill != NULL);
20854 		next_ill->ill_usesrc_grp_next = NULL;
20855 		next_ill->ill_usesrc_ifindex = 0;
20856 		next_ill = tmp_ill;
20857 	} while (next_ill->ill_usesrc_ifindex != 0);
20858 	uill->ill_usesrc_grp_next = NULL;
20859 }
20860 
20861 /*
20862  * Remove the client usesrc ILL from the list and relink to a new list
20863  */
20864 int
20865 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
20866 {
20867 	ill_t *ill, *tmp_ill;
20868 
20869 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
20870 	    (uill != NULL) && RW_WRITE_HELD(&ill_g_usesrc_lock));
20871 
20872 	/*
20873 	 * Check if the usesrc client ILL passed in is not already
20874 	 * in use as a usesrc ILL i.e one whose source address is
20875 	 * in use OR a usesrc ILL is not already in use as a usesrc
20876 	 * client ILL
20877 	 */
20878 	if ((ucill->ill_usesrc_ifindex == 0) ||
20879 	    (uill->ill_usesrc_ifindex != 0)) {
20880 		return (-1);
20881 	}
20882 
20883 	ill = ill_prev_usesrc(ucill);
20884 	ASSERT(ill->ill_usesrc_grp_next != NULL);
20885 
20886 	/* Remove from the current list */
20887 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
20888 		/* Only two elements in the list */
20889 		ASSERT(ill->ill_usesrc_ifindex == 0);
20890 		ill->ill_usesrc_grp_next = NULL;
20891 	} else {
20892 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
20893 	}
20894 
20895 	if (ifindex == 0) {
20896 		ucill->ill_usesrc_ifindex = 0;
20897 		ucill->ill_usesrc_grp_next = NULL;
20898 		return (0);
20899 	}
20900 
20901 	ucill->ill_usesrc_ifindex = ifindex;
20902 	tmp_ill = uill->ill_usesrc_grp_next;
20903 	uill->ill_usesrc_grp_next = ucill;
20904 	ucill->ill_usesrc_grp_next =
20905 	    (tmp_ill != NULL) ? tmp_ill : uill;
20906 	return (0);
20907 }
20908 
20909 /*
20910  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
20911  * ip.c for locking details.
20912  */
20913 /* ARGSUSED */
20914 int
20915 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20916     ip_ioctl_cmd_t *ipip, void *ifreq)
20917 {
20918 	struct lifreq *lifr = (struct lifreq *)ifreq;
20919 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
20920 	    ill_flag_changed = B_FALSE;
20921 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
20922 	int err = 0, ret;
20923 	uint_t ifindex;
20924 	phyint_t *us_phyint, *us_cli_phyint;
20925 	ipsq_t *ipsq = NULL;
20926 
20927 	ASSERT(IAM_WRITER_IPIF(ipif));
20928 	ASSERT(q->q_next == NULL);
20929 	ASSERT(CONN_Q(q));
20930 
20931 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
20932 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
20933 
20934 	ASSERT(us_cli_phyint != NULL);
20935 
20936 	/*
20937 	 * If the client ILL is being used for IPMP, abort.
20938 	 * Note, this can be done before ipsq_try_enter since we are already
20939 	 * exclusive on this ILL
20940 	 */
20941 	if ((us_cli_phyint->phyint_groupname != NULL) ||
20942 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
20943 		return (EINVAL);
20944 	}
20945 
20946 	ifindex = lifr->lifr_index;
20947 	if (ifindex == 0) {
20948 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
20949 			/* non usesrc group interface, nothing to reset */
20950 			return (0);
20951 		}
20952 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
20953 		/* valid reset request */
20954 		reset_flg = B_TRUE;
20955 	}
20956 
20957 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
20958 	    ip_process_ioctl, &err);
20959 
20960 	if (usesrc_ill == NULL) {
20961 		return (err);
20962 	}
20963 
20964 	/*
20965 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
20966 	 * group nor can either of the interfaces be used for standy. So
20967 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
20968 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
20969 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
20970 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
20971 	 * the usesrc_cli_ill
20972 	 */
20973 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
20974 	    NEW_OP, B_TRUE);
20975 	if (ipsq == NULL) {
20976 		err = EINPROGRESS;
20977 		/* Operation enqueued on the ipsq of the usesrc ILL */
20978 		goto done;
20979 	}
20980 
20981 	/* Check if the usesrc_ill is used for IPMP */
20982 	us_phyint = usesrc_ill->ill_phyint;
20983 	if ((us_phyint->phyint_groupname != NULL) ||
20984 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
20985 		err = EINVAL;
20986 		goto done;
20987 	}
20988 
20989 	/*
20990 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
20991 	 * already a client then return EINVAL
20992 	 */
20993 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
20994 		err = EINVAL;
20995 		goto done;
20996 	}
20997 
20998 	/*
20999 	 * If the ill_usesrc_ifindex field is already set to what it needs to
21000 	 * be then this is a duplicate operation.
21001 	 */
21002 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
21003 		err = 0;
21004 		goto done;
21005 	}
21006 
21007 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
21008 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
21009 	    usesrc_ill->ill_isv6));
21010 
21011 	/*
21012 	 * The next step ensures that no new ires will be created referencing
21013 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
21014 	 * we go through an ire walk deleting all ire caches that reference
21015 	 * the client ill. New ires referencing the client ill that are added
21016 	 * to the ire table before the ILL_CHANGING flag is set, will be
21017 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
21018 	 * the client ill while the ILL_CHANGING flag is set will be failed
21019 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
21020 	 * checks (under the ill_g_usesrc_lock) that the ire being added
21021 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
21022 	 * belong to the same usesrc group.
21023 	 */
21024 	mutex_enter(&usesrc_cli_ill->ill_lock);
21025 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
21026 	mutex_exit(&usesrc_cli_ill->ill_lock);
21027 	ill_flag_changed = B_TRUE;
21028 
21029 	if (ipif->ipif_isv6)
21030 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
21031 		    ALL_ZONES);
21032 	else
21033 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
21034 		    ALL_ZONES);
21035 
21036 	/*
21037 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
21038 	 * and the ill_usesrc_ifindex fields
21039 	 */
21040 	rw_enter(&ill_g_usesrc_lock, RW_WRITER);
21041 
21042 	if (reset_flg) {
21043 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
21044 		if (ret != 0) {
21045 			err = EINVAL;
21046 		}
21047 		rw_exit(&ill_g_usesrc_lock);
21048 		goto done;
21049 	}
21050 
21051 	/*
21052 	 * Four possibilities to consider:
21053 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
21054 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
21055 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
21056 	 * 4. Both are part of their respective usesrc groups
21057 	 */
21058 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
21059 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
21060 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
21061 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
21062 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
21063 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
21064 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
21065 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
21066 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
21067 		/* Insert at head of list */
21068 		usesrc_cli_ill->ill_usesrc_grp_next =
21069 		    usesrc_ill->ill_usesrc_grp_next;
21070 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
21071 	} else {
21072 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
21073 		    ifindex);
21074 		if (ret != 0)
21075 			err = EINVAL;
21076 	}
21077 	rw_exit(&ill_g_usesrc_lock);
21078 
21079 done:
21080 	if (ill_flag_changed) {
21081 		mutex_enter(&usesrc_cli_ill->ill_lock);
21082 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
21083 		mutex_exit(&usesrc_cli_ill->ill_lock);
21084 	}
21085 	if (ipsq != NULL)
21086 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
21087 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
21088 	ill_refrele(usesrc_ill);
21089 	return (err);
21090 }
21091 
21092 /*
21093  * comparison function used by avl.
21094  */
21095 static int
21096 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
21097 {
21098 
21099 	uint_t index;
21100 
21101 	ASSERT(phyip != NULL && index_ptr != NULL);
21102 
21103 	index = *((uint_t *)index_ptr);
21104 	/*
21105 	 * let the phyint with the lowest index be on top.
21106 	 */
21107 	if (((phyint_t *)phyip)->phyint_ifindex < index)
21108 		return (1);
21109 	if (((phyint_t *)phyip)->phyint_ifindex > index)
21110 		return (-1);
21111 	return (0);
21112 }
21113 
21114 /*
21115  * comparison function used by avl.
21116  */
21117 static int
21118 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
21119 {
21120 	ill_t *ill;
21121 	int res = 0;
21122 
21123 	ASSERT(phyip != NULL && name_ptr != NULL);
21124 
21125 	if (((phyint_t *)phyip)->phyint_illv4)
21126 		ill = ((phyint_t *)phyip)->phyint_illv4;
21127 	else
21128 		ill = ((phyint_t *)phyip)->phyint_illv6;
21129 	ASSERT(ill != NULL);
21130 
21131 	res = strcmp(ill->ill_name, (char *)name_ptr);
21132 	if (res > 0)
21133 		return (1);
21134 	else if (res < 0)
21135 		return (-1);
21136 	return (0);
21137 }
21138 /*
21139  * This function is called from ill_delete when the ill is being
21140  * unplumbed. We remove the reference from the phyint and we also
21141  * free the phyint when there are no more references to it.
21142  */
21143 static void
21144 ill_phyint_free(ill_t *ill)
21145 {
21146 	phyint_t *phyi;
21147 	phyint_t *next_phyint;
21148 	ipsq_t *cur_ipsq;
21149 
21150 	ASSERT(ill->ill_phyint != NULL);
21151 
21152 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
21153 	phyi = ill->ill_phyint;
21154 	ill->ill_phyint = NULL;
21155 	/*
21156 	 * ill_init allocates a phyint always to store the copy
21157 	 * of flags relevant to phyint. At that point in time, we could
21158 	 * not assign the name and hence phyint_illv4/v6 could not be
21159 	 * initialized. Later in ipif_set_values, we assign the name to
21160 	 * the ill, at which point in time we assign phyint_illv4/v6.
21161 	 * Thus we don't rely on phyint_illv6 to be initialized always.
21162 	 */
21163 	if (ill->ill_flags & ILLF_IPV6) {
21164 		phyi->phyint_illv6 = NULL;
21165 	} else {
21166 		phyi->phyint_illv4 = NULL;
21167 	}
21168 	/*
21169 	 * ipif_down removes it from the group when the last ipif goes
21170 	 * down.
21171 	 */
21172 	ASSERT(ill->ill_group == NULL);
21173 
21174 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
21175 		return;
21176 
21177 	/*
21178 	 * Make sure this phyint was put in the list.
21179 	 */
21180 	if (phyi->phyint_ifindex > 0) {
21181 		avl_remove(&phyint_g_list.phyint_list_avl_by_index,
21182 		    phyi);
21183 		avl_remove(&phyint_g_list.phyint_list_avl_by_name,
21184 		    phyi);
21185 	}
21186 	/*
21187 	 * remove phyint from the ipsq list.
21188 	 */
21189 	cur_ipsq = phyi->phyint_ipsq;
21190 	if (phyi == cur_ipsq->ipsq_phyint_list) {
21191 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
21192 	} else {
21193 		next_phyint = cur_ipsq->ipsq_phyint_list;
21194 		while (next_phyint != NULL) {
21195 			if (next_phyint->phyint_ipsq_next == phyi) {
21196 				next_phyint->phyint_ipsq_next =
21197 					phyi->phyint_ipsq_next;
21198 				break;
21199 			}
21200 			next_phyint = next_phyint->phyint_ipsq_next;
21201 		}
21202 		ASSERT(next_phyint != NULL);
21203 	}
21204 	IPSQ_DEC_REF(cur_ipsq);
21205 
21206 	if (phyi->phyint_groupname_len != 0) {
21207 		ASSERT(phyi->phyint_groupname != NULL);
21208 		mi_free(phyi->phyint_groupname);
21209 	}
21210 	mi_free(phyi);
21211 }
21212 
21213 /*
21214  * Attach the ill to the phyint structure which can be shared by both
21215  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
21216  * function is called from ipif_set_values and ill_lookup_on_name (for
21217  * loopback) where we know the name of the ill. We lookup the ill and if
21218  * there is one present already with the name use that phyint. Otherwise
21219  * reuse the one allocated by ill_init.
21220  */
21221 static void
21222 ill_phyint_reinit(ill_t *ill)
21223 {
21224 	boolean_t isv6 = ill->ill_isv6;
21225 	phyint_t *phyi_old;
21226 	phyint_t *phyi;
21227 	avl_index_t where = 0;
21228 	ill_t	*ill_other = NULL;
21229 	ipsq_t	*ipsq;
21230 
21231 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
21232 
21233 	phyi_old = ill->ill_phyint;
21234 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
21235 	    phyi_old->phyint_illv6 == NULL));
21236 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
21237 	    phyi_old->phyint_illv4 == NULL));
21238 	ASSERT(phyi_old->phyint_ifindex == 0);
21239 
21240 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_name,
21241 	    ill->ill_name, &where);
21242 
21243 	/*
21244 	 * 1. We grabbed the ill_g_lock before inserting this ill into
21245 	 *    the global list of ills. So no other thread could have located
21246 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
21247 	 * 2. Now locate the other protocol instance of this ill.
21248 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
21249 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
21250 	 *    of neither ill can change.
21251 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
21252 	 *    other ill.
21253 	 * 5. Release all locks.
21254 	 */
21255 
21256 	/*
21257 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
21258 	 * we are initializing IPv4.
21259 	 */
21260 	if (phyi != NULL) {
21261 		ill_other = (isv6) ? phyi->phyint_illv4 :
21262 		    phyi->phyint_illv6;
21263 		ASSERT(ill_other->ill_phyint != NULL);
21264 		ASSERT((isv6 && !ill_other->ill_isv6) ||
21265 		    (!isv6 && ill_other->ill_isv6));
21266 		GRAB_ILL_LOCKS(ill, ill_other);
21267 		/*
21268 		 * We are potentially throwing away phyint_flags which
21269 		 * could be different from the one that we obtain from
21270 		 * ill_other->ill_phyint. But it is okay as we are assuming
21271 		 * that the state maintained within IP is correct.
21272 		 */
21273 		mutex_enter(&phyi->phyint_lock);
21274 		if (isv6) {
21275 			ASSERT(phyi->phyint_illv6 == NULL);
21276 			phyi->phyint_illv6 = ill;
21277 		} else {
21278 			ASSERT(phyi->phyint_illv4 == NULL);
21279 			phyi->phyint_illv4 = ill;
21280 		}
21281 		/*
21282 		 * This is a new ill, currently undergoing SLIFNAME
21283 		 * So we could not have joined an IPMP group until now.
21284 		 */
21285 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
21286 		    phyi_old->phyint_groupname == NULL);
21287 
21288 		/*
21289 		 * This phyi_old is going away. Decref ipsq_refs and
21290 		 * assert it is zero. The ipsq itself will be freed in
21291 		 * ipsq_exit
21292 		 */
21293 		ipsq = phyi_old->phyint_ipsq;
21294 		IPSQ_DEC_REF(ipsq);
21295 		ASSERT(ipsq->ipsq_refs == 0);
21296 		/* Get the singleton phyint out of the ipsq list */
21297 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
21298 		ipsq->ipsq_phyint_list = NULL;
21299 		phyi_old->phyint_illv4 = NULL;
21300 		phyi_old->phyint_illv6 = NULL;
21301 		mi_free(phyi_old);
21302 	} else {
21303 		mutex_enter(&ill->ill_lock);
21304 		/*
21305 		 * We don't need to acquire any lock, since
21306 		 * the ill is not yet visible globally  and we
21307 		 * have not yet released the ill_g_lock.
21308 		 */
21309 		phyi = phyi_old;
21310 		mutex_enter(&phyi->phyint_lock);
21311 		/* XXX We need a recovery strategy here. */
21312 		if (!phyint_assign_ifindex(phyi))
21313 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
21314 
21315 		avl_insert(&phyint_g_list.phyint_list_avl_by_name,
21316 		    (void *)phyi, where);
21317 
21318 		(void) avl_find(&phyint_g_list.phyint_list_avl_by_index,
21319 		    &phyi->phyint_ifindex, &where);
21320 		avl_insert(&phyint_g_list.phyint_list_avl_by_index,
21321 		    (void *)phyi, where);
21322 	}
21323 
21324 	/*
21325 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
21326 	 * pending mp is not affected because that is per ill basis.
21327 	 */
21328 	ill->ill_phyint = phyi;
21329 
21330 	/*
21331 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
21332 	 * We do this here as when the first ipif was allocated,
21333 	 * ipif_allocate does not know the right interface index.
21334 	 */
21335 
21336 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
21337 	/*
21338 	 * Now that the phyint's ifindex has been assigned, complete the
21339 	 * remaining
21340 	 */
21341 	if (ill->ill_isv6) {
21342 		ill->ill_ip6_mib->ipv6IfIndex =
21343 		    ill->ill_phyint->phyint_ifindex;
21344 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
21345 		    ill->ill_phyint->phyint_ifindex;
21346 	}
21347 
21348 	RELEASE_ILL_LOCKS(ill, ill_other);
21349 	mutex_exit(&phyi->phyint_lock);
21350 }
21351 
21352 /*
21353  * Notify any downstream modules of the name of this interface.
21354  * An M_IOCTL is used even though we don't expect a successful reply.
21355  * Any reply message from the driver (presumably an M_IOCNAK) will
21356  * eventually get discarded somewhere upstream.  The message format is
21357  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
21358  * to IP.
21359  */
21360 static void
21361 ip_ifname_notify(ill_t *ill, queue_t *q)
21362 {
21363 	mblk_t *mp1, *mp2;
21364 	struct iocblk *iocp;
21365 	struct lifreq *lifr;
21366 
21367 	mp1 = mkiocb(SIOCSLIFNAME);
21368 	if (mp1 == NULL)
21369 		return;
21370 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
21371 	if (mp2 == NULL) {
21372 		freeb(mp1);
21373 		return;
21374 	}
21375 
21376 	mp1->b_cont = mp2;
21377 	iocp = (struct iocblk *)mp1->b_rptr;
21378 	iocp->ioc_count = sizeof (struct lifreq);
21379 
21380 	lifr = (struct lifreq *)mp2->b_rptr;
21381 	mp2->b_wptr += sizeof (struct lifreq);
21382 	bzero(lifr, sizeof (struct lifreq));
21383 
21384 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
21385 	lifr->lifr_ppa = ill->ill_ppa;
21386 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
21387 
21388 	putnext(q, mp1);
21389 }
21390 
21391 static boolean_t ip_trash_timer_started = B_FALSE;
21392 
21393 static int
21394 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
21395 {
21396 	int err;
21397 
21398 	/* Set the obsolete NDD per-interface forwarding name. */
21399 	err = ill_set_ndd_name(ill);
21400 	if (err != 0) {
21401 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
21402 		    err);
21403 	}
21404 
21405 	/* Tell downstream modules where they are. */
21406 	ip_ifname_notify(ill, q);
21407 
21408 	/*
21409 	 * ill_dl_phys returns EINPROGRESS in the usual case.
21410 	 * Error cases are ENOMEM ...
21411 	 */
21412 	err = ill_dl_phys(ill, ipif, mp, q);
21413 
21414 	/*
21415 	 * If there is no IRE expiration timer running, get one started.
21416 	 * igmp and mld timers will be triggered by the first multicast
21417 	 */
21418 	if (!ip_trash_timer_started) {
21419 		/*
21420 		 * acquire the lock and check again.
21421 		 */
21422 		mutex_enter(&ip_trash_timer_lock);
21423 		if (!ip_trash_timer_started) {
21424 			ip_ire_expire_id = timeout(ip_trash_timer_expire, NULL,
21425 			    MSEC_TO_TICK(ip_timer_interval));
21426 			ip_trash_timer_started = B_TRUE;
21427 		}
21428 		mutex_exit(&ip_trash_timer_lock);
21429 	}
21430 
21431 	if (ill->ill_isv6) {
21432 		mutex_enter(&mld_slowtimeout_lock);
21433 		if (mld_slowtimeout_id == 0) {
21434 			mld_slowtimeout_id = timeout(mld_slowtimo, NULL,
21435 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
21436 		}
21437 		mutex_exit(&mld_slowtimeout_lock);
21438 	} else {
21439 		mutex_enter(&igmp_slowtimeout_lock);
21440 		if (igmp_slowtimeout_id == 0) {
21441 			igmp_slowtimeout_id = timeout(igmp_slowtimo, NULL,
21442 				MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
21443 		}
21444 		mutex_exit(&igmp_slowtimeout_lock);
21445 	}
21446 
21447 	return (err);
21448 }
21449 
21450 /*
21451  * Common routine for ppa and ifname setting. Should be called exclusive.
21452  *
21453  * Returns EINPROGRESS when mp has been consumed by queueing it on
21454  * ill_pending_mp and the ioctl will complete in ip_rput.
21455  *
21456  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
21457  * the new name and new ppa in lifr_name and lifr_ppa respectively.
21458  * For SLIFNAME, we pass these values back to the userland.
21459  */
21460 static int
21461 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
21462 {
21463 	ill_t	*ill;
21464 	ipif_t	*ipif;
21465 	ipsq_t	*ipsq;
21466 	char	*ppa_ptr;
21467 	char	*old_ptr;
21468 	char	old_char;
21469 	int	error;
21470 
21471 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
21472 	ASSERT(q->q_next != NULL);
21473 	ASSERT(interf_name != NULL);
21474 
21475 	ill = (ill_t *)q->q_ptr;
21476 
21477 	ASSERT(ill->ill_name[0] == '\0');
21478 	ASSERT(IAM_WRITER_ILL(ill));
21479 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
21480 	ASSERT(ill->ill_ppa == UINT_MAX);
21481 
21482 	/* The ppa is sent down by ifconfig or is chosen */
21483 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
21484 		return (EINVAL);
21485 	}
21486 
21487 	/*
21488 	 * make sure ppa passed in is same as ppa in the name.
21489 	 * This check is not made when ppa == UINT_MAX in that case ppa
21490 	 * in the name could be anything. System will choose a ppa and
21491 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
21492 	 */
21493 	if (*new_ppa_ptr != UINT_MAX) {
21494 		/* stoi changes the pointer */
21495 		old_ptr = ppa_ptr;
21496 		/*
21497 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
21498 		 * (they don't have an externally visible ppa).  We assign one
21499 		 * here so that we can manage the interface.  Note that in
21500 		 * the past this value was always 0 for DLPI 1 drivers.
21501 		 */
21502 		if (*new_ppa_ptr == 0)
21503 			*new_ppa_ptr = stoi(&old_ptr);
21504 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
21505 			return (EINVAL);
21506 	}
21507 	/*
21508 	 * terminate string before ppa
21509 	 * save char at that location.
21510 	 */
21511 	old_char = ppa_ptr[0];
21512 	ppa_ptr[0] = '\0';
21513 
21514 	ill->ill_ppa = *new_ppa_ptr;
21515 	/*
21516 	 * Finish as much work now as possible before calling ill_glist_insert
21517 	 * which makes the ill globally visible and also merges it with the
21518 	 * other protocol instance of this phyint. The remaining work is
21519 	 * done after entering the ipsq which may happen sometime later.
21520 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
21521 	 */
21522 	ipif = ill->ill_ipif;
21523 
21524 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
21525 	ipif_assign_seqid(ipif);
21526 
21527 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
21528 		ill->ill_flags |= ILLF_IPV4;
21529 
21530 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
21531 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
21532 
21533 	if (ill->ill_flags & ILLF_IPV6) {
21534 
21535 		ill->ill_isv6 = B_TRUE;
21536 		if (ill->ill_rq != NULL) {
21537 			ill->ill_rq->q_qinfo = &rinit_ipv6;
21538 			ill->ill_wq->q_qinfo = &winit_ipv6;
21539 		}
21540 
21541 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
21542 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
21543 		ipif->ipif_v6src_addr = ipv6_all_zeros;
21544 		ipif->ipif_v6subnet = ipv6_all_zeros;
21545 		ipif->ipif_v6net_mask = ipv6_all_zeros;
21546 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
21547 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
21548 		/*
21549 		 * point-to-point or Non-mulicast capable
21550 		 * interfaces won't do NUD unless explicitly
21551 		 * configured to do so.
21552 		 */
21553 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
21554 		    !(ill->ill_flags & ILLF_MULTICAST)) {
21555 			ill->ill_flags |= ILLF_NONUD;
21556 		}
21557 		/* Make sure IPv4 specific flag is not set on IPv6 if */
21558 		if (ill->ill_flags & ILLF_NOARP) {
21559 			/*
21560 			 * Note: xresolv interfaces will eventually need
21561 			 * NOARP set here as well, but that will require
21562 			 * those external resolvers to have some
21563 			 * knowledge of that flag and act appropriately.
21564 			 * Not to be changed at present.
21565 			 */
21566 			ill->ill_flags &= ~ILLF_NOARP;
21567 		}
21568 		/*
21569 		 * Set the ILLF_ROUTER flag according to the global
21570 		 * IPv6 forwarding policy.
21571 		 */
21572 		if (ipv6_forward != 0)
21573 			ill->ill_flags |= ILLF_ROUTER;
21574 	} else if (ill->ill_flags & ILLF_IPV4) {
21575 		ill->ill_isv6 = B_FALSE;
21576 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
21577 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
21578 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
21579 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
21580 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
21581 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
21582 		/*
21583 		 * Set the ILLF_ROUTER flag according to the global
21584 		 * IPv4 forwarding policy.
21585 		 */
21586 		if (ip_g_forward != 0)
21587 			ill->ill_flags |= ILLF_ROUTER;
21588 	}
21589 
21590 	ASSERT(ill->ill_phyint != NULL);
21591 
21592 	/*
21593 	 * The ipv6Ifindex and ipv6IfIcmpIfIndex assignments will
21594 	 * be completed in ill_glist_insert -> ill_phyint_reinit
21595 	 */
21596 	if (ill->ill_isv6) {
21597 		/* allocate v6 mib */
21598 		if (!ill_allocate_mibs(ill))
21599 			return (ENOMEM);
21600 	}
21601 
21602 	/*
21603 	 * Pick a default sap until we get the DL_INFO_ACK back from
21604 	 * the driver.
21605 	 */
21606 	if (ill->ill_sap == 0) {
21607 		if (ill->ill_isv6)
21608 			ill->ill_sap  = IP6_DL_SAP;
21609 		else
21610 			ill->ill_sap  = IP_DL_SAP;
21611 	}
21612 
21613 	ill->ill_ifname_pending = 1;
21614 	ill->ill_ifname_pending_err = 0;
21615 
21616 	ill_refhold(ill);
21617 	rw_enter(&ill_g_lock, RW_WRITER);
21618 	if ((error = ill_glist_insert(ill, interf_name,
21619 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
21620 		ill->ill_ppa = UINT_MAX;
21621 		ill->ill_name[0] = '\0';
21622 		/*
21623 		 * undo null termination done above.
21624 		 */
21625 		ppa_ptr[0] = old_char;
21626 		rw_exit(&ill_g_lock);
21627 		ill_refrele(ill);
21628 		return (error);
21629 	}
21630 
21631 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
21632 
21633 	/*
21634 	 * When we return the buffer pointed to by interf_name should contain
21635 	 * the same name as in ill_name.
21636 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
21637 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
21638 	 * so copy full name and update the ppa ptr.
21639 	 * When ppa passed in != UINT_MAX all values are correct just undo
21640 	 * null termination, this saves a bcopy.
21641 	 */
21642 	if (*new_ppa_ptr == UINT_MAX) {
21643 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
21644 		*new_ppa_ptr = ill->ill_ppa;
21645 	} else {
21646 		/*
21647 		 * undo null termination done above.
21648 		 */
21649 		ppa_ptr[0] = old_char;
21650 	}
21651 
21652 	/* Let SCTP know about this ILL */
21653 	sctp_update_ill(ill, SCTP_ILL_INSERT);
21654 
21655 	/* and also about the first ipif */
21656 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
21657 
21658 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
21659 	    B_TRUE);
21660 
21661 	rw_exit(&ill_g_lock);
21662 	ill_refrele(ill);
21663 	if (ipsq == NULL)
21664 		return (EINPROGRESS);
21665 
21666 	/*
21667 	 * Need to set the ipsq_current_ipif now, if we have changed ipsq
21668 	 * due to the phyint merge in ill_phyint_reinit.
21669 	 */
21670 	ASSERT(ipsq->ipsq_current_ipif == NULL ||
21671 		ipsq->ipsq_current_ipif == ipif);
21672 	ipsq->ipsq_current_ipif = ipif;
21673 	ipsq->ipsq_last_cmd = SIOCSLIFNAME;
21674 	error = ipif_set_values_tail(ill, ipif, mp, q);
21675 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
21676 	if (error != 0 && error != EINPROGRESS) {
21677 		/*
21678 		 * restore previous values
21679 		 */
21680 		ill->ill_isv6 = B_FALSE;
21681 	}
21682 	return (error);
21683 }
21684 
21685 
21686 extern void (*ip_cleanup_func)(void);
21687 
21688 void
21689 ipif_init(void)
21690 {
21691 	hrtime_t hrt;
21692 	int i;
21693 
21694 	/*
21695 	 * Can't call drv_getparm here as it is too early in the boot.
21696 	 * As we use ipif_src_random just for picking a different
21697 	 * source address everytime, this need not be really random.
21698 	 */
21699 	hrt = gethrtime();
21700 	ipif_src_random = ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
21701 
21702 	for (i = 0; i < MAX_G_HEADS; i++) {
21703 		ill_g_heads[i].ill_g_list_head = (ill_if_t *)&ill_g_heads[i];
21704 		ill_g_heads[i].ill_g_list_tail = (ill_if_t *)&ill_g_heads[i];
21705 	}
21706 
21707 	avl_create(&phyint_g_list.phyint_list_avl_by_index,
21708 	    ill_phyint_compare_index,
21709 	    sizeof (phyint_t),
21710 	    offsetof(struct phyint, phyint_avl_by_index));
21711 	avl_create(&phyint_g_list.phyint_list_avl_by_name,
21712 	    ill_phyint_compare_name,
21713 	    sizeof (phyint_t),
21714 	    offsetof(struct phyint, phyint_avl_by_name));
21715 
21716 	ip_cleanup_func = ip_thread_exit;
21717 }
21718 
21719 /*
21720  * This is called by ip_rt_add when src_addr value is other than zero.
21721  * src_addr signifies the source address of the incoming packet. For
21722  * reverse tunnel route we need to create a source addr based routing
21723  * table. This routine creates ip_mrtun_table if it's empty and then
21724  * it adds the route entry hashed by source address. It verifies that
21725  * the outgoing interface is always a non-resolver interface (tunnel).
21726  */
21727 int
21728 ip_mrtun_rt_add(ipaddr_t in_src_addr, int flags, ipif_t *ipif_arg,
21729     ipif_t *src_ipif, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func)
21730 {
21731 	ire_t   *ire;
21732 	ire_t	*save_ire;
21733 	ipif_t  *ipif;
21734 	ill_t   *in_ill = NULL;
21735 	ill_t	*out_ill;
21736 	queue_t	*stq;
21737 	mblk_t	*dlureq_mp;
21738 	int	error;
21739 
21740 	if (ire_arg != NULL)
21741 		*ire_arg = NULL;
21742 	ASSERT(in_src_addr != INADDR_ANY);
21743 
21744 	ipif = ipif_arg;
21745 	if (ipif != NULL) {
21746 		out_ill = ipif->ipif_ill;
21747 	} else {
21748 		ip1dbg(("ip_mrtun_rt_add: ipif is NULL\n"));
21749 		return (EINVAL);
21750 	}
21751 
21752 	if (src_ipif == NULL) {
21753 		ip1dbg(("ip_mrtun_rt_add: src_ipif is NULL\n"));
21754 		return (EINVAL);
21755 	}
21756 	in_ill = src_ipif->ipif_ill;
21757 
21758 	/*
21759 	 * Check for duplicates. We don't need to
21760 	 * match out_ill, because the uniqueness of
21761 	 * a route is only dependent on src_addr and
21762 	 * in_ill.
21763 	 */
21764 	ire = ire_mrtun_lookup(in_src_addr, in_ill);
21765 	if (ire != NULL) {
21766 		ire_refrele(ire);
21767 		return (EEXIST);
21768 	}
21769 	if (ipif->ipif_net_type != IRE_IF_NORESOLVER) {
21770 		ip2dbg(("ip_mrtun_rt_add: outgoing interface is type %d\n",
21771 		    ipif->ipif_net_type));
21772 		return (EINVAL);
21773 	}
21774 
21775 	stq = ipif->ipif_wq;
21776 	ASSERT(stq != NULL);
21777 
21778 	/*
21779 	 * The outgoing interface must be non-resolver
21780 	 * interface.
21781 	 */
21782 	dlureq_mp = ill_dlur_gen(NULL,
21783 	    out_ill->ill_phys_addr_length, out_ill->ill_sap,
21784 	    out_ill->ill_sap_length);
21785 
21786 	if (dlureq_mp == NULL) {
21787 		ip1dbg(("ip_newroute: dlureq_mp NULL\n"));
21788 		return (ENOMEM);
21789 	}
21790 
21791 	/* Create the IRE. */
21792 
21793 	ire = ire_create(
21794 	    NULL,				/* Zero dst addr */
21795 	    NULL,				/* Zero mask */
21796 	    NULL,				/* Zero gateway addr */
21797 	    NULL,				/* Zero ipif_src addr */
21798 	    (uint8_t *)&in_src_addr,		/* in_src-addr */
21799 	    &ipif->ipif_mtu,
21800 	    NULL,
21801 	    NULL,				/* rfq */
21802 	    stq,
21803 	    IRE_MIPRTUN,
21804 	    dlureq_mp,
21805 	    ipif,
21806 	    in_ill,
21807 	    0,
21808 	    0,
21809 	    0,
21810 	    flags,
21811 	    &ire_uinfo_null);
21812 
21813 	if (ire == NULL)
21814 		return (ENOMEM);
21815 	ip2dbg(("ip_mrtun_rt_add: mrtun route is created with type %d\n",
21816 	    ire->ire_type));
21817 	save_ire = ire;
21818 	ASSERT(save_ire != NULL);
21819 	error = ire_add_mrtun(&ire, q, mp, func);
21820 	/*
21821 	 * If ire_add_mrtun() failed, the ire passed in was freed
21822 	 * so there is no need to do so here.
21823 	 */
21824 	if (error != 0) {
21825 		return (error);
21826 	}
21827 
21828 	/* Duplicate check */
21829 	if (ire != save_ire) {
21830 		/* route already exists by now */
21831 		ire_refrele(ire);
21832 		return (EEXIST);
21833 	}
21834 
21835 	if (ire_arg != NULL) {
21836 		/*
21837 		 * Store the ire that was just added. the caller
21838 		 * ip_rts_request responsible for doing ire_refrele()
21839 		 * on it.
21840 		 */
21841 		*ire_arg = ire;
21842 	} else {
21843 		ire_refrele(ire);	/* held in ire_add_mrtun */
21844 	}
21845 
21846 	return (0);
21847 }
21848 
21849 /*
21850  * It is called by ip_rt_delete() only when mipagent requests to delete
21851  * a reverse tunnel route that was added by ip_mrtun_rt_add() before.
21852  */
21853 
21854 int
21855 ip_mrtun_rt_delete(ipaddr_t in_src_addr, ipif_t *src_ipif)
21856 {
21857 	ire_t   *ire = NULL;
21858 
21859 	if (in_src_addr == INADDR_ANY)
21860 		return (EINVAL);
21861 	if (src_ipif == NULL)
21862 		return (EINVAL);
21863 
21864 	/* search if this route exists in the ip_mrtun_table */
21865 	ire = ire_mrtun_lookup(in_src_addr, src_ipif->ipif_ill);
21866 	if (ire == NULL) {
21867 		ip2dbg(("ip_mrtun_rt_delete: ire not found\n"));
21868 		return (ESRCH);
21869 	}
21870 	ire_delete(ire);
21871 	ire_refrele(ire);
21872 	return (0);
21873 }
21874 
21875 /*
21876  * Lookup the ipif corresponding to the onlink destination address. For
21877  * point-to-point interfaces, it matches with remote endpoint destination
21878  * address. For point-to-multipoint interfaces it only tries to match the
21879  * destination with the interface's subnet address. The longest, most specific
21880  * match is found to take care of such rare network configurations like -
21881  * le0: 129.146.1.1/16
21882  * le1: 129.146.2.2/24
21883  * It is used only by SO_DONTROUTE at the moment.
21884  */
21885 ipif_t *
21886 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid)
21887 {
21888 	ipif_t	*ipif, *best_ipif;
21889 	ill_t	*ill;
21890 	ill_walk_context_t ctx;
21891 
21892 	ASSERT(zoneid != ALL_ZONES);
21893 	best_ipif = NULL;
21894 
21895 	rw_enter(&ill_g_lock, RW_READER);
21896 	ill = ILL_START_WALK_V4(&ctx);
21897 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21898 		mutex_enter(&ill->ill_lock);
21899 		for (ipif = ill->ill_ipif; ipif != NULL;
21900 		    ipif = ipif->ipif_next) {
21901 			if (!IPIF_CAN_LOOKUP(ipif))
21902 				continue;
21903 			if (ipif->ipif_zoneid != zoneid)
21904 				continue;
21905 			/*
21906 			 * Point-to-point case. Look for exact match with
21907 			 * destination address.
21908 			 */
21909 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
21910 				if (ipif->ipif_pp_dst_addr == addr) {
21911 					ipif_refhold_locked(ipif);
21912 					mutex_exit(&ill->ill_lock);
21913 					rw_exit(&ill_g_lock);
21914 					if (best_ipif != NULL)
21915 						ipif_refrele(best_ipif);
21916 					return (ipif);
21917 				}
21918 			} else if (ipif->ipif_subnet == (addr &
21919 			    ipif->ipif_net_mask)) {
21920 				/*
21921 				 * Point-to-multipoint case. Looping through to
21922 				 * find the most specific match. If there are
21923 				 * multiple best match ipif's then prefer ipif's
21924 				 * that are UP. If there is only one best match
21925 				 * ipif and it is DOWN we must still return it.
21926 				 */
21927 				if ((best_ipif == NULL) ||
21928 				    (ipif->ipif_net_mask >
21929 				    best_ipif->ipif_net_mask) ||
21930 				    ((ipif->ipif_net_mask ==
21931 				    best_ipif->ipif_net_mask) &&
21932 				    ((ipif->ipif_flags & IPIF_UP) &&
21933 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
21934 					ipif_refhold_locked(ipif);
21935 					mutex_exit(&ill->ill_lock);
21936 					rw_exit(&ill_g_lock);
21937 					if (best_ipif != NULL)
21938 						ipif_refrele(best_ipif);
21939 					best_ipif = ipif;
21940 					rw_enter(&ill_g_lock, RW_READER);
21941 					mutex_enter(&ill->ill_lock);
21942 				}
21943 			}
21944 		}
21945 		mutex_exit(&ill->ill_lock);
21946 	}
21947 	rw_exit(&ill_g_lock);
21948 	return (best_ipif);
21949 }
21950 
21951 
21952 /*
21953  * Save enough information so that we can recreate the IRE if
21954  * the interface goes down and then up.
21955  */
21956 static void
21957 ipif_save_ire(ipif_t *ipif, ire_t *ire)
21958 {
21959 	mblk_t	*save_mp;
21960 
21961 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
21962 	if (save_mp != NULL) {
21963 		ifrt_t	*ifrt;
21964 
21965 		save_mp->b_wptr += sizeof (ifrt_t);
21966 		ifrt = (ifrt_t *)save_mp->b_rptr;
21967 		bzero(ifrt, sizeof (ifrt_t));
21968 		ifrt->ifrt_type = ire->ire_type;
21969 		ifrt->ifrt_addr = ire->ire_addr;
21970 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
21971 		ifrt->ifrt_src_addr = ire->ire_src_addr;
21972 		ifrt->ifrt_mask = ire->ire_mask;
21973 		ifrt->ifrt_flags = ire->ire_flags;
21974 		ifrt->ifrt_max_frag = ire->ire_max_frag;
21975 		mutex_enter(&ipif->ipif_saved_ire_lock);
21976 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
21977 		ipif->ipif_saved_ire_mp = save_mp;
21978 		ipif->ipif_saved_ire_cnt++;
21979 		mutex_exit(&ipif->ipif_saved_ire_lock);
21980 	}
21981 }
21982 
21983 
21984 static void
21985 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
21986 {
21987 	mblk_t	**mpp;
21988 	mblk_t	*mp;
21989 	ifrt_t	*ifrt;
21990 
21991 	/* Remove from ipif_saved_ire_mp list if it is there */
21992 	mutex_enter(&ipif->ipif_saved_ire_lock);
21993 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
21994 	    mpp = &(*mpp)->b_cont) {
21995 		/*
21996 		 * On a given ipif, the triple of address, gateway and
21997 		 * mask is unique for each saved IRE (in the case of
21998 		 * ordinary interface routes, the gateway address is
21999 		 * all-zeroes).
22000 		 */
22001 		mp = *mpp;
22002 		ifrt = (ifrt_t *)mp->b_rptr;
22003 		if (ifrt->ifrt_addr == ire->ire_addr &&
22004 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
22005 		    ifrt->ifrt_mask == ire->ire_mask) {
22006 			*mpp = mp->b_cont;
22007 			ipif->ipif_saved_ire_cnt--;
22008 			freeb(mp);
22009 			break;
22010 		}
22011 	}
22012 	mutex_exit(&ipif->ipif_saved_ire_lock);
22013 }
22014 
22015 
22016 /*
22017  * IP multirouting broadcast routes handling
22018  * Append CGTP broadcast IREs to regular ones created
22019  * at ifconfig time.
22020  */
22021 static void
22022 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst)
22023 {
22024 	ire_t *ire_prim;
22025 
22026 	ASSERT(ire != NULL);
22027 	ASSERT(ire_dst != NULL);
22028 
22029 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
22030 	    IRE_BROADCAST, NULL, NULL, MATCH_IRE_TYPE);
22031 	if (ire_prim != NULL) {
22032 		/*
22033 		 * We are in the special case of broadcasts for
22034 		 * CGTP. We add an IRE_BROADCAST that holds
22035 		 * the RTF_MULTIRT flag, the destination
22036 		 * address of ire_dst and the low level
22037 		 * info of ire_prim. In other words, CGTP
22038 		 * broadcast is added to the redundant ipif.
22039 		 */
22040 		ipif_t *ipif_prim;
22041 		ire_t  *bcast_ire;
22042 
22043 		ipif_prim = ire_prim->ire_ipif;
22044 
22045 		ip2dbg(("ip_cgtp_filter_bcast_add: "
22046 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
22047 		    (void *)ire_dst, (void *)ire_prim,
22048 		    (void *)ipif_prim));
22049 
22050 		bcast_ire = ire_create(
22051 		    (uchar_t *)&ire->ire_addr,
22052 		    (uchar_t *)&ip_g_all_ones,
22053 		    (uchar_t *)&ire_dst->ire_src_addr,
22054 		    (uchar_t *)&ire->ire_gateway_addr,
22055 		    NULL,
22056 		    &ipif_prim->ipif_mtu,
22057 		    NULL,
22058 		    ipif_prim->ipif_rq,
22059 		    ipif_prim->ipif_wq,
22060 		    IRE_BROADCAST,
22061 		    ipif_prim->ipif_bcast_mp,
22062 		    ipif_prim,
22063 		    NULL,
22064 		    0,
22065 		    0,
22066 		    0,
22067 		    ire->ire_flags,
22068 		    &ire_uinfo_null);
22069 
22070 		if (bcast_ire != NULL) {
22071 
22072 			if (ire_add(&bcast_ire, NULL, NULL, NULL) == 0) {
22073 				ip2dbg(("ip_cgtp_filter_bcast_add: "
22074 				    "added bcast_ire %p\n",
22075 				    (void *)bcast_ire));
22076 
22077 				ipif_save_ire(bcast_ire->ire_ipif,
22078 				    bcast_ire);
22079 				ire_refrele(bcast_ire);
22080 			}
22081 		}
22082 		ire_refrele(ire_prim);
22083 	}
22084 }
22085 
22086 
22087 /*
22088  * IP multirouting broadcast routes handling
22089  * Remove the broadcast ire
22090  */
22091 static void
22092 ip_cgtp_bcast_delete(ire_t *ire)
22093 {
22094 	ire_t *ire_dst;
22095 
22096 	ASSERT(ire != NULL);
22097 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
22098 	    NULL, NULL, MATCH_IRE_TYPE);
22099 	if (ire_dst != NULL) {
22100 		ire_t *ire_prim;
22101 
22102 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
22103 		    IRE_BROADCAST, NULL, NULL, MATCH_IRE_TYPE);
22104 		if (ire_prim != NULL) {
22105 			ipif_t *ipif_prim;
22106 			ire_t  *bcast_ire;
22107 
22108 			ipif_prim = ire_prim->ire_ipif;
22109 
22110 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
22111 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
22112 			    (void *)ire_dst, (void *)ire_prim,
22113 			    (void *)ipif_prim));
22114 
22115 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
22116 			    ire->ire_gateway_addr,
22117 			    IRE_BROADCAST,
22118 			    ipif_prim,
22119 			    NULL,
22120 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
22121 			    MATCH_IRE_MASK);
22122 
22123 			if (bcast_ire != NULL) {
22124 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
22125 				    "looked up bcast_ire %p\n",
22126 				    (void *)bcast_ire));
22127 				ipif_remove_ire(bcast_ire->ire_ipif,
22128 					bcast_ire);
22129 				ire_delete(bcast_ire);
22130 			}
22131 			ire_refrele(ire_prim);
22132 		}
22133 		ire_refrele(ire_dst);
22134 	}
22135 }
22136 
22137 /*
22138  * IPsec hardware acceleration capabilities related functions.
22139  */
22140 
22141 /*
22142  * Free a per-ill IPsec capabilities structure.
22143  */
22144 static void
22145 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
22146 {
22147 	if (capab->auth_hw_algs != NULL)
22148 		kmem_free(capab->auth_hw_algs, capab->algs_size);
22149 	if (capab->encr_hw_algs != NULL)
22150 		kmem_free(capab->encr_hw_algs, capab->algs_size);
22151 	if (capab->encr_algparm != NULL)
22152 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
22153 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
22154 }
22155 
22156 /*
22157  * Allocate a new per-ill IPsec capabilities structure. This structure
22158  * is specific to an IPsec protocol (AH or ESP). It is implemented as
22159  * an array which specifies, for each algorithm, whether this algorithm
22160  * is supported by the ill or not.
22161  */
22162 static ill_ipsec_capab_t *
22163 ill_ipsec_capab_alloc(void)
22164 {
22165 	ill_ipsec_capab_t *capab;
22166 	uint_t nelems;
22167 
22168 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
22169 	if (capab == NULL)
22170 		return (NULL);
22171 
22172 	/* we need one bit per algorithm */
22173 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
22174 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
22175 
22176 	/* allocate memory to store algorithm flags */
22177 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
22178 	if (capab->encr_hw_algs == NULL)
22179 		goto nomem;
22180 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
22181 	if (capab->auth_hw_algs == NULL)
22182 		goto nomem;
22183 	/*
22184 	 * Leave encr_algparm NULL for now since we won't need it half
22185 	 * the time
22186 	 */
22187 	return (capab);
22188 
22189 nomem:
22190 	ill_ipsec_capab_free(capab);
22191 	return (NULL);
22192 }
22193 
22194 /*
22195  * Resize capability array.  Since we're exclusive, this is OK.
22196  */
22197 static boolean_t
22198 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
22199 {
22200 	ipsec_capab_algparm_t *nalp, *oalp;
22201 	uint32_t olen, nlen;
22202 
22203 	oalp = capab->encr_algparm;
22204 	olen = capab->encr_algparm_size;
22205 
22206 	if (oalp != NULL) {
22207 		if (algid < capab->encr_algparm_end)
22208 			return (B_TRUE);
22209 	}
22210 
22211 	nlen = (algid + 1) * sizeof (*nalp);
22212 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
22213 	if (nalp == NULL)
22214 		return (B_FALSE);
22215 
22216 	if (oalp != NULL) {
22217 		bcopy(oalp, nalp, olen);
22218 		kmem_free(oalp, olen);
22219 	}
22220 	capab->encr_algparm = nalp;
22221 	capab->encr_algparm_size = nlen;
22222 	capab->encr_algparm_end = algid + 1;
22223 
22224 	return (B_TRUE);
22225 }
22226 
22227 /*
22228  * Compare the capabilities of the specified ill with the protocol
22229  * and algorithms specified by the SA passed as argument.
22230  * If they match, returns B_TRUE, B_FALSE if they do not match.
22231  *
22232  * The ill can be passed as a pointer to it, or by specifying its index
22233  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
22234  *
22235  * Called by ipsec_out_is_accelerated() do decide whether an outbound
22236  * packet is eligible for hardware acceleration, and by
22237  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
22238  * to a particular ill.
22239  */
22240 boolean_t
22241 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
22242     ipsa_t *sa)
22243 {
22244 	boolean_t sa_isv6;
22245 	uint_t algid;
22246 	struct ill_ipsec_capab_s *cpp;
22247 	boolean_t need_refrele = B_FALSE;
22248 
22249 	if (ill == NULL) {
22250 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
22251 		    NULL, NULL, NULL);
22252 		if (ill == NULL) {
22253 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
22254 			return (B_FALSE);
22255 		}
22256 		need_refrele = B_TRUE;
22257 	}
22258 
22259 	/*
22260 	 * Use the address length specified by the SA to determine
22261 	 * if it corresponds to a IPv6 address, and fail the matching
22262 	 * if the isv6 flag passed as argument does not match.
22263 	 * Note: this check is used for SADB capability checking before
22264 	 * sending SA information to an ill.
22265 	 */
22266 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
22267 	if (sa_isv6 != ill_isv6)
22268 		/* protocol mismatch */
22269 		goto done;
22270 
22271 	/*
22272 	 * Check if the ill supports the protocol, algorithm(s) and
22273 	 * key size(s) specified by the SA, and get the pointers to
22274 	 * the algorithms supported by the ill.
22275 	 */
22276 	switch (sa->ipsa_type) {
22277 
22278 	case SADB_SATYPE_ESP:
22279 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
22280 			/* ill does not support ESP acceleration */
22281 			goto done;
22282 		cpp = ill->ill_ipsec_capab_esp;
22283 		algid = sa->ipsa_auth_alg;
22284 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
22285 			goto done;
22286 		algid = sa->ipsa_encr_alg;
22287 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
22288 			goto done;
22289 		if (algid < cpp->encr_algparm_end) {
22290 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
22291 			if (sa->ipsa_encrkeybits < alp->minkeylen)
22292 				goto done;
22293 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
22294 				goto done;
22295 		}
22296 		break;
22297 
22298 	case SADB_SATYPE_AH:
22299 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
22300 			/* ill does not support AH acceleration */
22301 			goto done;
22302 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
22303 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
22304 			goto done;
22305 		break;
22306 	}
22307 
22308 	if (need_refrele)
22309 		ill_refrele(ill);
22310 	return (B_TRUE);
22311 done:
22312 	if (need_refrele)
22313 		ill_refrele(ill);
22314 	return (B_FALSE);
22315 }
22316 
22317 
22318 /*
22319  * Add a new ill to the list of IPsec capable ills.
22320  * Called from ill_capability_ipsec_ack() when an ACK was received
22321  * indicating that IPsec hardware processing was enabled for an ill.
22322  *
22323  * ill must point to the ill for which acceleration was enabled.
22324  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
22325  */
22326 static void
22327 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
22328 {
22329 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
22330 	uint_t sa_type;
22331 	uint_t ipproto;
22332 
22333 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
22334 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
22335 
22336 	switch (dl_cap) {
22337 	case DL_CAPAB_IPSEC_AH:
22338 		sa_type = SADB_SATYPE_AH;
22339 		ills = &ipsec_capab_ills_ah;
22340 		ipproto = IPPROTO_AH;
22341 		break;
22342 	case DL_CAPAB_IPSEC_ESP:
22343 		sa_type = SADB_SATYPE_ESP;
22344 		ills = &ipsec_capab_ills_esp;
22345 		ipproto = IPPROTO_ESP;
22346 		break;
22347 	}
22348 
22349 	rw_enter(&ipsec_capab_ills_lock, RW_WRITER);
22350 
22351 	/*
22352 	 * Add ill index to list of hardware accelerators. If
22353 	 * already in list, do nothing.
22354 	 */
22355 	for (cur_ill = *ills; cur_ill != NULL &&
22356 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
22357 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
22358 		;
22359 
22360 	if (cur_ill == NULL) {
22361 		/* if this is a new entry for this ill */
22362 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
22363 		if (new_ill == NULL) {
22364 			rw_exit(&ipsec_capab_ills_lock);
22365 			return;
22366 		}
22367 
22368 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
22369 		new_ill->ill_isv6 = ill->ill_isv6;
22370 		new_ill->next = *ills;
22371 		*ills = new_ill;
22372 	} else if (!sadb_resync) {
22373 		/* not resync'ing SADB and an entry exists for this ill */
22374 		rw_exit(&ipsec_capab_ills_lock);
22375 		return;
22376 	}
22377 
22378 	rw_exit(&ipsec_capab_ills_lock);
22379 
22380 	if (ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
22381 		/*
22382 		 * IPsec module for protocol loaded, initiate dump
22383 		 * of the SADB to this ill.
22384 		 */
22385 		sadb_ill_download(ill, sa_type);
22386 }
22387 
22388 /*
22389  * Remove an ill from the list of IPsec capable ills.
22390  */
22391 static void
22392 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
22393 {
22394 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
22395 
22396 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
22397 	    dl_cap == DL_CAPAB_IPSEC_ESP);
22398 
22399 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipsec_capab_ills_ah :
22400 	    &ipsec_capab_ills_esp;
22401 
22402 	rw_enter(&ipsec_capab_ills_lock, RW_WRITER);
22403 
22404 	prev_ill = NULL;
22405 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
22406 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
22407 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
22408 		;
22409 	if (cur_ill == NULL) {
22410 		/* entry not found */
22411 		rw_exit(&ipsec_capab_ills_lock);
22412 		return;
22413 	}
22414 	if (prev_ill == NULL) {
22415 		/* entry at front of list */
22416 		*ills = NULL;
22417 	} else {
22418 		prev_ill->next = cur_ill->next;
22419 	}
22420 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
22421 	rw_exit(&ipsec_capab_ills_lock);
22422 }
22423 
22424 
22425 /*
22426  * Handling of DL_CONTROL_REQ messages that must be sent down to
22427  * an ill while having exclusive access.
22428  */
22429 /* ARGSUSED */
22430 static void
22431 ill_ipsec_capab_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
22432 {
22433 	ill_t *ill = (ill_t *)q->q_ptr;
22434 
22435 	ill_dlpi_send(ill, mp);
22436 }
22437 
22438 
22439 /*
22440  * Called by SADB to send a DL_CONTROL_REQ message to every ill
22441  * supporting the specified IPsec protocol acceleration.
22442  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
22443  * We free the mblk and, if sa is non-null, release the held referece.
22444  */
22445 void
22446 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa)
22447 {
22448 	ipsec_capab_ill_t *ici, *cur_ici;
22449 	ill_t *ill;
22450 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
22451 
22452 	ici = (sa_type == SADB_SATYPE_AH) ? ipsec_capab_ills_ah :
22453 	    ipsec_capab_ills_esp;
22454 
22455 	rw_enter(&ipsec_capab_ills_lock, RW_READER);
22456 
22457 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
22458 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
22459 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL);
22460 
22461 		/*
22462 		 * Handle the case where the ill goes away while the SADB is
22463 		 * attempting to send messages.  If it's going away, it's
22464 		 * nuking its shadow SADB, so we don't care..
22465 		 */
22466 
22467 		if (ill == NULL)
22468 			continue;
22469 
22470 		if (sa != NULL) {
22471 			/*
22472 			 * Make sure capabilities match before
22473 			 * sending SA to ill.
22474 			 */
22475 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
22476 			    cur_ici->ill_isv6, sa)) {
22477 				ill_refrele(ill);
22478 				continue;
22479 			}
22480 
22481 			mutex_enter(&sa->ipsa_lock);
22482 			sa->ipsa_flags |= IPSA_F_HW;
22483 			mutex_exit(&sa->ipsa_lock);
22484 		}
22485 
22486 		/*
22487 		 * Copy template message, and add it to the front
22488 		 * of the mblk ship list. We want to avoid holding
22489 		 * the ipsec_capab_ills_lock while sending the
22490 		 * message to the ills.
22491 		 *
22492 		 * The b_next and b_prev are temporarily used
22493 		 * to build a list of mblks to be sent down, and to
22494 		 * save the ill to which they must be sent.
22495 		 */
22496 		nmp = copymsg(mp);
22497 		if (nmp == NULL) {
22498 			ill_refrele(ill);
22499 			continue;
22500 		}
22501 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
22502 		nmp->b_next = mp_ship_list;
22503 		mp_ship_list = nmp;
22504 		nmp->b_prev = (mblk_t *)ill;
22505 	}
22506 
22507 	rw_exit(&ipsec_capab_ills_lock);
22508 
22509 	nmp = mp_ship_list;
22510 	while (nmp != NULL) {
22511 		/* restore the mblk to a sane state */
22512 		next_mp = nmp->b_next;
22513 		nmp->b_next = NULL;
22514 		ill = (ill_t *)nmp->b_prev;
22515 		nmp->b_prev = NULL;
22516 
22517 		/*
22518 		 * Ship the mblk to the ill, must be exclusive. Keep the
22519 		 * reference to the ill as qwriter_ip() does a ill_referele().
22520 		 */
22521 		(void) qwriter_ip(NULL, ill, ill->ill_wq, nmp,
22522 		    ill_ipsec_capab_send_writer, NEW_OP, B_TRUE);
22523 
22524 		nmp = next_mp;
22525 	}
22526 
22527 	if (sa != NULL)
22528 		IPSA_REFRELE(sa);
22529 	freemsg(mp);
22530 }
22531 
22532 
22533 /*
22534  * Derive an interface id from the link layer address.
22535  * Knows about IEEE 802 and IEEE EUI-64 mappings.
22536  */
22537 static boolean_t
22538 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
22539 {
22540 	char		*addr;
22541 
22542 	if (phys_length != ETHERADDRL)
22543 		return (B_FALSE);
22544 
22545 	/* Form EUI-64 like address */
22546 	addr = (char *)&v6addr->s6_addr32[2];
22547 	bcopy((char *)phys_addr, addr, 3);
22548 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
22549 	addr[3] = (char)0xff;
22550 	addr[4] = (char)0xfe;
22551 	bcopy((char *)phys_addr + 3, addr + 5, 3);
22552 	return (B_TRUE);
22553 }
22554 
22555 /* ARGSUSED */
22556 static boolean_t
22557 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
22558 {
22559 	return (B_FALSE);
22560 }
22561 
22562 /* ARGSUSED */
22563 static boolean_t
22564 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
22565     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
22566 {
22567 	/*
22568 	 * Multicast address mappings used over Ethernet/802.X.
22569 	 * This address is used as a base for mappings.
22570 	 */
22571 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
22572 	    0x00, 0x00, 0x00};
22573 
22574 	/*
22575 	 * Extract low order 32 bits from IPv6 multicast address.
22576 	 * Or that into the link layer address, starting from the
22577 	 * second byte.
22578 	 */
22579 	*hw_start = 2;
22580 	v6_extract_mask->s6_addr32[0] = 0;
22581 	v6_extract_mask->s6_addr32[1] = 0;
22582 	v6_extract_mask->s6_addr32[2] = 0;
22583 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
22584 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
22585 	return (B_TRUE);
22586 }
22587 
22588 /*
22589  * Indicate by return value whether multicast is supported. If not,
22590  * this code should not touch/change any parameters.
22591  */
22592 /* ARGSUSED */
22593 static boolean_t
22594 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
22595     uint32_t *hw_start, ipaddr_t *extract_mask)
22596 {
22597 	/*
22598 	 * Multicast address mappings used over Ethernet/802.X.
22599 	 * This address is used as a base for mappings.
22600 	 */
22601 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
22602 	    0x00, 0x00, 0x00 };
22603 
22604 	if (phys_length != ETHERADDRL)
22605 		return (B_FALSE);
22606 
22607 	*extract_mask = htonl(0x007fffff);
22608 	*hw_start = 2;
22609 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
22610 	return (B_TRUE);
22611 }
22612 
22613 /*
22614  * Derive IPoIB interface id from the link layer address.
22615  */
22616 static boolean_t
22617 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
22618 {
22619 	char		*addr;
22620 
22621 	if (phys_length != 20)
22622 		return (B_FALSE);
22623 	addr = (char *)&v6addr->s6_addr32[2];
22624 	bcopy(phys_addr + 12, addr, 8);
22625 	/*
22626 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
22627 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
22628 	 * rules. In these cases, the IBA considers these GUIDs to be in
22629 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
22630 	 * required; vendors are required not to assign global EUI-64's
22631 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
22632 	 * of the interface identifier. Whether the GUID is in modified
22633 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
22634 	 * bit set to 1.
22635 	 */
22636 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
22637 	return (B_TRUE);
22638 }
22639 
22640 /*
22641  * Note on mapping from multicast IP addresses to IPoIB multicast link
22642  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
22643  * The format of an IPoIB multicast address is:
22644  *
22645  *  4 byte QPN      Scope Sign.  Pkey
22646  * +--------------------------------------------+
22647  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
22648  * +--------------------------------------------+
22649  *
22650  * The Scope and Pkey components are properties of the IBA port and
22651  * network interface. They can be ascertained from the broadcast address.
22652  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
22653  */
22654 
22655 static boolean_t
22656 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
22657     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
22658 {
22659 	/*
22660 	 * Base IPoIB IPv6 multicast address used for mappings.
22661 	 * Does not contain the IBA scope/Pkey values.
22662 	 */
22663 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
22664 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
22665 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
22666 
22667 	/*
22668 	 * Extract low order 80 bits from IPv6 multicast address.
22669 	 * Or that into the link layer address, starting from the
22670 	 * sixth byte.
22671 	 */
22672 	*hw_start = 6;
22673 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
22674 
22675 	/*
22676 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
22677 	 */
22678 	*(maddr + 5) = *(bphys_addr + 5);
22679 	*(maddr + 8) = *(bphys_addr + 8);
22680 	*(maddr + 9) = *(bphys_addr + 9);
22681 
22682 	v6_extract_mask->s6_addr32[0] = 0;
22683 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
22684 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
22685 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
22686 	return (B_TRUE);
22687 }
22688 
22689 static boolean_t
22690 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
22691     uint32_t *hw_start, ipaddr_t *extract_mask)
22692 {
22693 	/*
22694 	 * Base IPoIB IPv4 multicast address used for mappings.
22695 	 * Does not contain the IBA scope/Pkey values.
22696 	 */
22697 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
22698 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
22699 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
22700 
22701 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
22702 		return (B_FALSE);
22703 
22704 	/*
22705 	 * Extract low order 28 bits from IPv4 multicast address.
22706 	 * Or that into the link layer address, starting from the
22707 	 * sixteenth byte.
22708 	 */
22709 	*extract_mask = htonl(0x0fffffff);
22710 	*hw_start = 16;
22711 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
22712 
22713 	/*
22714 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
22715 	 */
22716 	*(maddr + 5) = *(bphys_addr + 5);
22717 	*(maddr + 8) = *(bphys_addr + 8);
22718 	*(maddr + 9) = *(bphys_addr + 9);
22719 	return (B_TRUE);
22720 }
22721 
22722 /*
22723  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
22724  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
22725  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
22726  * the link-local address is preferred.
22727  */
22728 boolean_t
22729 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
22730 {
22731 	ipif_t	*ipif;
22732 	ipif_t	*maybe_ipif = NULL;
22733 
22734 	mutex_enter(&ill->ill_lock);
22735 	if (ill->ill_state_flags & ILL_CONDEMNED) {
22736 		mutex_exit(&ill->ill_lock);
22737 		if (ipifp != NULL)
22738 			*ipifp = NULL;
22739 		return (B_FALSE);
22740 	}
22741 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
22742 		if (!IPIF_CAN_LOOKUP(ipif))
22743 			continue;
22744 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid)
22745 			continue;
22746 		if ((ipif->ipif_flags & flags) != flags)
22747 			continue;
22748 
22749 		if (ipifp == NULL) {
22750 			mutex_exit(&ill->ill_lock);
22751 			ASSERT(maybe_ipif == NULL);
22752 			return (B_TRUE);
22753 		}
22754 		if (!ill->ill_isv6 ||
22755 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
22756 			ipif_refhold_locked(ipif);
22757 			mutex_exit(&ill->ill_lock);
22758 			*ipifp = ipif;
22759 			return (B_TRUE);
22760 		}
22761 		if (maybe_ipif == NULL)
22762 			maybe_ipif = ipif;
22763 	}
22764 	if (ipifp != NULL) {
22765 		if (maybe_ipif != NULL)
22766 			ipif_refhold_locked(maybe_ipif);
22767 		*ipifp = maybe_ipif;
22768 	}
22769 	mutex_exit(&ill->ill_lock);
22770 	return (maybe_ipif != NULL);
22771 }
22772 
22773 /*
22774  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
22775  */
22776 boolean_t
22777 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
22778 {
22779 	ill_t *illg;
22780 
22781 	/*
22782 	 * We look at the passed-in ill first without grabbing ill_g_lock.
22783 	 */
22784 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
22785 		return (B_TRUE);
22786 	}
22787 	rw_enter(&ill_g_lock, RW_READER);
22788 	if (ill->ill_group == NULL) {
22789 		/* ill not in a group */
22790 		rw_exit(&ill_g_lock);
22791 		return (B_FALSE);
22792 	}
22793 
22794 	/*
22795 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
22796 	 * group. We need to look for an ipif in the zone on all the ills in the
22797 	 * group.
22798 	 */
22799 	illg = ill->ill_group->illgrp_ill;
22800 	do {
22801 		/*
22802 		 * We don't call ipif_lookup_zoneid() on ill as we already know
22803 		 * that it's not there.
22804 		 */
22805 		if (illg != ill &&
22806 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
22807 			break;
22808 		}
22809 	} while ((illg = illg->ill_group_next) != NULL);
22810 	rw_exit(&ill_g_lock);
22811 	return (illg != NULL);
22812 }
22813 
22814 /*
22815  * Check if this ill is only being used to send ICMP probes for IPMP
22816  */
22817 boolean_t
22818 ill_is_probeonly(ill_t *ill)
22819 {
22820 	/*
22821 	 * Check if the interface is FAILED, or INACTIVE
22822 	 */
22823 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
22824 		return (B_TRUE);
22825 
22826 	return (B_FALSE);
22827 }
22828