xref: /titanic_50/usr/src/uts/common/inet/ip/ip_if.c (revision c77a61a72b5ecdc507d6cf104142edd371a16c84)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This file contains the interface control functions for IP.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/stream.h>
35 #include <sys/dlpi.h>
36 #include <sys/stropts.h>
37 #include <sys/strsun.h>
38 #include <sys/sysmacros.h>
39 #include <sys/strlog.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #include <sys/cmn_err.h>
43 #include <sys/kstat.h>
44 #include <sys/debug.h>
45 #include <sys/zone.h>
46 
47 #include <sys/kmem.h>
48 #include <sys/systm.h>
49 #include <sys/param.h>
50 #include <sys/socket.h>
51 #include <sys/isa_defs.h>
52 #include <net/if.h>
53 #include <net/if_arp.h>
54 #include <net/if_types.h>
55 #include <net/if_dl.h>
56 #include <net/route.h>
57 #include <sys/sockio.h>
58 #include <netinet/in.h>
59 #include <netinet/ip6.h>
60 #include <netinet/icmp6.h>
61 #include <netinet/igmp_var.h>
62 #include <sys/strsun.h>
63 #include <sys/policy.h>
64 #include <sys/ethernet.h>
65 
66 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
67 #include <inet/mi.h>
68 #include <inet/nd.h>
69 #include <inet/arp.h>
70 #include <inet/mib2.h>
71 #include <inet/ip.h>
72 #include <inet/ip6.h>
73 #include <inet/ip6_asp.h>
74 #include <inet/tcp.h>
75 #include <inet/ip_multi.h>
76 #include <inet/ip_ire.h>
77 #include <inet/ip_ftable.h>
78 #include <inet/ip_rts.h>
79 #include <inet/ip_ndp.h>
80 #include <inet/ip_if.h>
81 #include <inet/ip_impl.h>
82 #include <inet/tun.h>
83 #include <inet/sctp_ip.h>
84 #include <inet/ip_netinfo.h>
85 #include <inet/mib2.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 <inet/ipclassifier.h>
97 #include <sys/mac.h>
98 
99 #include <sys/systeminfo.h>
100 #include <sys/bootconf.h>
101 
102 #include <sys/tsol/tndb.h>
103 #include <sys/tsol/tnet.h>
104 
105 /* The character which tells where the ill_name ends */
106 #define	IPIF_SEPARATOR_CHAR	':'
107 
108 /* IP ioctl function table entry */
109 typedef struct ipft_s {
110 	int	ipft_cmd;
111 	pfi_t	ipft_pfi;
112 	int	ipft_min_size;
113 	int	ipft_flags;
114 } ipft_t;
115 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
116 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
117 
118 typedef struct ip_sock_ar_s {
119 	union {
120 		area_t	ip_sock_area;
121 		ared_t	ip_sock_ared;
122 		areq_t	ip_sock_areq;
123 	} ip_sock_ar_u;
124 	queue_t	*ip_sock_ar_q;
125 } ip_sock_ar_t;
126 
127 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
128 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
129 		    char *value, caddr_t cp, cred_t *ioc_cr);
130 
131 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
132 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
133 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
134     mblk_t *mp, boolean_t need_up);
135 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
136     mblk_t *mp, boolean_t need_up);
137 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
138     queue_t *q, mblk_t *mp, boolean_t need_up);
139 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
140     mblk_t *mp, boolean_t need_up);
141 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
142     mblk_t *mp);
143 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
144     queue_t *q, mblk_t *mp, boolean_t need_up);
145 static int	ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp,
146     sin_t *sin, boolean_t x_arp_ioctl, boolean_t if_arp_ioctl);
147 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **);
148 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
149 static void	ipsq_flush(ill_t *ill);
150 static void	ipsq_clean_all(ill_t *ill);
151 static void	ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring);
152 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
153     queue_t *q, mblk_t *mp, boolean_t need_up);
154 static void	ipsq_delete(ipsq_t *);
155 
156 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
157 		    boolean_t initialize);
158 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
159 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
160 static void	ipif_delete_cache_ire(ire_t *, char *);
161 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
162 static void	ipif_free(ipif_t *ipif);
163 static void	ipif_free_tail(ipif_t *ipif);
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_dl_down(ill_t *ill);
184 static void	ill_down(ill_t *ill);
185 static void	ill_downi(ire_t *ire, char *ill_arg);
186 static void	ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg);
187 static void	ill_down_tail(ill_t *ill);
188 static void	ill_free_mib(ill_t *ill);
189 static void	ill_glist_delete(ill_t *);
190 static boolean_t ill_has_usable_ipif(ill_t *);
191 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
192 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
193 static void	ill_phyint_free(ill_t *ill);
194 static void	ill_phyint_reinit(ill_t *ill);
195 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
196 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
197 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
198 static void	ill_stq_cache_delete(ire_t *, char *);
199 
200 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
201 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
202 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
203     in6_addr_t *);
204 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
205     ipaddr_t *);
206 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
207 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
208     in6_addr_t *);
209 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
210     ipaddr_t *);
211 
212 static void	ipif_save_ire(ipif_t *, ire_t *);
213 static void	ipif_remove_ire(ipif_t *, ire_t *);
214 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *);
215 static void 	ip_cgtp_bcast_delete(ire_t *);
216 
217 /*
218  * Per-ill IPsec capabilities management.
219  */
220 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
221 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
222 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
223 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
224 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
225 static void ill_capability_proto(ill_t *, int, mblk_t *);
226 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
227     boolean_t);
228 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
229 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
230 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
231 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
232 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
233 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
234 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
235 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
236     dl_capability_sub_t *);
237 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
238 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
239 static void ill_capability_lso_reset(ill_t *, mblk_t **);
240 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
241 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
242 static void ill_capability_dls_reset(ill_t *, mblk_t **);
243 static void ill_capability_dls_disable(ill_t *);
244 
245 static void	illgrp_cache_delete(ire_t *, char *);
246 static void	illgrp_delete(ill_t *ill);
247 static void	illgrp_reset_schednext(ill_t *ill);
248 
249 static ill_t	*ill_prev_usesrc(ill_t *);
250 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
251 static void	ill_disband_usesrc_group(ill_t *);
252 
253 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
254 
255 /*
256  * if we go over the memory footprint limit more than once in this msec
257  * interval, we'll start pruning aggressively.
258  */
259 int ip_min_frag_prune_time = 0;
260 
261 /*
262  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
263  * and the IPsec DOI
264  */
265 #define	MAX_IPSEC_ALGS	256
266 
267 #define	BITSPERBYTE	8
268 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
269 
270 #define	IPSEC_ALG_ENABLE(algs, algid) \
271 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
272 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
273 
274 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
275 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
276 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
277 
278 typedef uint8_t ipsec_capab_elem_t;
279 
280 /*
281  * Per-algorithm parameters.  Note that at present, only encryption
282  * algorithms have variable keysize (IKE does not provide a way to negotiate
283  * auth algorithm keysize).
284  *
285  * All sizes here are in bits.
286  */
287 typedef struct
288 {
289 	uint16_t	minkeylen;
290 	uint16_t	maxkeylen;
291 } ipsec_capab_algparm_t;
292 
293 /*
294  * Per-ill capabilities.
295  */
296 struct ill_ipsec_capab_s {
297 	ipsec_capab_elem_t *encr_hw_algs;
298 	ipsec_capab_elem_t *auth_hw_algs;
299 	uint32_t algs_size;	/* size of _hw_algs in bytes */
300 	/* algorithm key lengths */
301 	ipsec_capab_algparm_t *encr_algparm;
302 	uint32_t encr_algparm_size;
303 	uint32_t encr_algparm_end;
304 };
305 
306 /*
307  * List of AH and ESP IPsec acceleration capable ills
308  */
309 typedef struct ipsec_capab_ill_s {
310 	uint_t ill_index;
311 	boolean_t ill_isv6;
312 	struct ipsec_capab_ill_s *next;
313 } ipsec_capab_ill_t;
314 
315 static ipsec_capab_ill_t *ipsec_capab_ills_ah;
316 static ipsec_capab_ill_t *ipsec_capab_ills_esp;
317 krwlock_t ipsec_capab_ills_lock;
318 
319 /*
320  * The field values are larger than strictly necessary for simple
321  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
322  */
323 static area_t	ip_area_template = {
324 	AR_ENTRY_ADD,			/* area_cmd */
325 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
326 					/* area_name_offset */
327 	/* area_name_length temporarily holds this structure length */
328 	sizeof (area_t),			/* area_name_length */
329 	IP_ARP_PROTO_TYPE,		/* area_proto */
330 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
331 	IP_ADDR_LEN,			/* area_proto_addr_length */
332 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
333 					/* area_proto_mask_offset */
334 	0,				/* area_flags */
335 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
336 					/* area_hw_addr_offset */
337 	/* Zero length hw_addr_length means 'use your idea of the address' */
338 	0				/* area_hw_addr_length */
339 };
340 
341 /*
342  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
343  * support
344  */
345 static area_t	ip6_area_template = {
346 	AR_ENTRY_ADD,			/* area_cmd */
347 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
348 					/* area_name_offset */
349 	/* area_name_length temporarily holds this structure length */
350 	sizeof (area_t),			/* area_name_length */
351 	IP_ARP_PROTO_TYPE,		/* area_proto */
352 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
353 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
354 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
355 					/* area_proto_mask_offset */
356 	0,				/* area_flags */
357 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
358 					/* area_hw_addr_offset */
359 	/* Zero length hw_addr_length means 'use your idea of the address' */
360 	0				/* area_hw_addr_length */
361 };
362 
363 static ared_t	ip_ared_template = {
364 	AR_ENTRY_DELETE,
365 	sizeof (ared_t) + IP_ADDR_LEN,
366 	sizeof (ared_t),
367 	IP_ARP_PROTO_TYPE,
368 	sizeof (ared_t),
369 	IP_ADDR_LEN
370 };
371 
372 static ared_t	ip6_ared_template = {
373 	AR_ENTRY_DELETE,
374 	sizeof (ared_t) + IPV6_ADDR_LEN,
375 	sizeof (ared_t),
376 	IP_ARP_PROTO_TYPE,
377 	sizeof (ared_t),
378 	IPV6_ADDR_LEN
379 };
380 
381 /*
382  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
383  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
384  * areq is used).
385  */
386 static areq_t	ip_areq_template = {
387 	AR_ENTRY_QUERY,			/* cmd */
388 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
389 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
390 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
391 	sizeof (areq_t),			/* target addr offset */
392 	IP_ADDR_LEN,			/* target addr_length */
393 	0,				/* flags */
394 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
395 	IP_ADDR_LEN,			/* sender addr length */
396 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
397 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
398 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
399 	/* anything else filled in by the code */
400 };
401 
402 static arc_t	ip_aru_template = {
403 	AR_INTERFACE_UP,
404 	sizeof (arc_t),		/* Name offset */
405 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
406 };
407 
408 static arc_t	ip_ard_template = {
409 	AR_INTERFACE_DOWN,
410 	sizeof (arc_t),		/* Name offset */
411 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
412 };
413 
414 static arc_t	ip_aron_template = {
415 	AR_INTERFACE_ON,
416 	sizeof (arc_t),		/* Name offset */
417 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
418 };
419 
420 static arc_t	ip_aroff_template = {
421 	AR_INTERFACE_OFF,
422 	sizeof (arc_t),		/* Name offset */
423 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
424 };
425 
426 
427 static arma_t	ip_arma_multi_template = {
428 	AR_MAPPING_ADD,
429 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
430 				/* Name offset */
431 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
432 	IP_ARP_PROTO_TYPE,
433 	sizeof (arma_t),			/* proto_addr_offset */
434 	IP_ADDR_LEN,				/* proto_addr_length */
435 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
436 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
437 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
438 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
439 	IP_MAX_HW_LEN,				/* hw_addr_length */
440 	0,					/* hw_mapping_start */
441 };
442 
443 static ipft_t	ip_ioctl_ftbl[] = {
444 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
445 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
446 		IPFT_F_NO_REPLY },
447 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
448 		IPFT_F_NO_REPLY },
449 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
450 	{ 0 }
451 };
452 
453 /* Simple ICMP IP Header Template */
454 static ipha_t icmp_ipha = {
455 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
456 };
457 
458 /* Flag descriptors for ip_ipif_report */
459 static nv_t	ipif_nv_tbl[] = {
460 	{ IPIF_UP,		"UP" },
461 	{ IPIF_BROADCAST,	"BROADCAST" },
462 	{ ILLF_DEBUG,		"DEBUG" },
463 	{ PHYI_LOOPBACK,	"LOOPBACK" },
464 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
465 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
466 	{ PHYI_RUNNING,		"RUNNING" },
467 	{ ILLF_NOARP,		"NOARP" },
468 	{ PHYI_PROMISC,		"PROMISC" },
469 	{ PHYI_ALLMULTI,	"ALLMULTI" },
470 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
471 	{ ILLF_MULTICAST,	"MULTICAST" },
472 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
473 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
474 	{ IPIF_DHCPRUNNING,	"DHCP" },
475 	{ IPIF_PRIVATE,		"PRIVATE" },
476 	{ IPIF_NOXMIT,		"NOXMIT" },
477 	{ IPIF_NOLOCAL,		"NOLOCAL" },
478 	{ IPIF_DEPRECATED,	"DEPRECATED" },
479 	{ IPIF_PREFERRED,	"PREFERRED" },
480 	{ IPIF_TEMPORARY,	"TEMPORARY" },
481 	{ IPIF_ADDRCONF,	"ADDRCONF" },
482 	{ PHYI_VIRTUAL,		"VIRTUAL" },
483 	{ ILLF_ROUTER,		"ROUTER" },
484 	{ ILLF_NONUD,		"NONUD" },
485 	{ IPIF_ANYCAST,		"ANYCAST" },
486 	{ ILLF_NORTEXCH,	"NORTEXCH" },
487 	{ ILLF_IPV4,		"IPV4" },
488 	{ ILLF_IPV6,		"IPV6" },
489 	{ IPIF_MIPRUNNING,	"MIP" },
490 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
491 	{ PHYI_FAILED,		"FAILED" },
492 	{ PHYI_STANDBY,		"STANDBY" },
493 	{ PHYI_INACTIVE,	"INACTIVE" },
494 	{ PHYI_OFFLINE,		"OFFLINE" },
495 };
496 
497 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
498 
499 static ip_m_t	ip_m_tbl[] = {
500 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
501 	    ip_ether_v6intfid },
502 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
503 	    ip_nodef_v6intfid },
504 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
505 	    ip_nodef_v6intfid },
506 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
507 	    ip_nodef_v6intfid },
508 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
509 	    ip_ether_v6intfid },
510 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
511 	    ip_ib_v6intfid },
512 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
513 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
514 	    ip_nodef_v6intfid }
515 };
516 
517 static ill_t	ill_null;		/* Empty ILL for init. */
518 char	ipif_loopback_name[] = "lo0";
519 static char *ipv4_forward_suffix = ":ip_forwarding";
520 static char *ipv6_forward_suffix = ":ip6_forwarding";
521 static kstat_t *loopback_ksp = NULL;
522 static	sin6_t	sin6_null;	/* Zero address for quick clears */
523 static	sin_t	sin_null;	/* Zero address for quick clears */
524 static	uint_t	ill_index = 1;	/* Used to assign interface indicies */
525 /* When set search for unused index */
526 static boolean_t ill_index_wrap = B_FALSE;
527 /* When set search for unused ipif_seqid */
528 static ipif_t	ipif_zero;
529 uint_t	ipif_src_random;
530 
531 /*
532  * For details on the protection offered by these locks please refer
533  * to the notes under the Synchronization section at the start of ip.c
534  */
535 krwlock_t	ill_g_lock;		/* The global ill_g_lock */
536 kmutex_t	ip_addr_avail_lock;	/* Address availability check lock */
537 ipsq_t		*ipsq_g_head;		/* List of all ipsq's on the system */
538 
539 krwlock_t	ill_g_usesrc_lock;	/* Protects usesrc related fields */
540 
541 /*
542  * illgrp_head/ifgrp_head is protected by IP's perimeter.
543  */
544 static  ill_group_t *illgrp_head_v4;	/* Head of IPv4 ill groups */
545 ill_group_t *illgrp_head_v6;		/* Head of IPv6 ill groups */
546 
547 ill_g_head_t	ill_g_heads[MAX_G_HEADS];   /* ILL List Head */
548 
549 /*
550  * ppa arena is created after these many
551  * interfaces have been plumbed.
552  */
553 uint_t	ill_no_arena = 12;
554 
555 #pragma align CACHE_ALIGN_SIZE(phyint_g_list)
556 static phyint_list_t phyint_g_list;	/* start of phyint list */
557 
558 /*
559  * Reflects value of FAILBACK variable in IPMP config file
560  * /etc/default/mpathd. Default value is B_TRUE.
561  * Set to B_FALSE if user disabled failback by configuring "FAILBACK=no"
562  * in.mpathd uses SIOCSIPMPFAILBACK ioctl to pass this information to kernel.
563  */
564 static boolean_t ipmp_enable_failback = B_TRUE;
565 
566 /*
567  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
568  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
569  * set through platform specific code (Niagara/Ontario).
570  */
571 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
572 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
573 
574 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
575 
576 static uint_t
577 ipif_rand(void)
578 {
579 	ipif_src_random = ipif_src_random * 1103515245 + 12345;
580 	return ((ipif_src_random >> 16) & 0x7fff);
581 }
582 
583 /*
584  * Allocate per-interface mibs.
585  * Returns true if ok. False otherwise.
586  *  ipsq  may not yet be allocated (loopback case ).
587  */
588 static boolean_t
589 ill_allocate_mibs(ill_t *ill)
590 {
591 	/* Already allocated? */
592 	if (ill->ill_ip_mib != NULL) {
593 		if (ill->ill_isv6)
594 			ASSERT(ill->ill_icmp6_mib != NULL);
595 		return (B_TRUE);
596 	}
597 
598 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
599 	    KM_NOSLEEP);
600 	if (ill->ill_ip_mib == NULL) {
601 		return (B_FALSE);
602 	}
603 
604 	/* Setup static information */
605 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
606 	    sizeof (mib2_ipIfStatsEntry_t));
607 	if (ill->ill_isv6) {
608 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
609 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
610 		    sizeof (mib2_ipv6AddrEntry_t));
611 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
612 		    sizeof (mib2_ipv6RouteEntry_t));
613 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
614 		    sizeof (mib2_ipv6NetToMediaEntry_t));
615 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
616 		    sizeof (ipv6_member_t));
617 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
618 		    sizeof (ipv6_grpsrc_t));
619 	} else {
620 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
621 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
622 		    sizeof (mib2_ipAddrEntry_t));
623 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
624 		    sizeof (mib2_ipRouteEntry_t));
625 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
626 		    sizeof (mib2_ipNetToMediaEntry_t));
627 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
628 		    sizeof (ip_member_t));
629 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
630 		    sizeof (ip_grpsrc_t));
631 
632 		/*
633 		 * For a v4 ill, we are done at this point, because per ill
634 		 * icmp mibs are only used for v6.
635 		 */
636 		return (B_TRUE);
637 	}
638 
639 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
640 	    KM_NOSLEEP);
641 	if (ill->ill_icmp6_mib == NULL) {
642 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
643 		ill->ill_ip_mib = NULL;
644 		return (B_FALSE);
645 	}
646 	/* static icmp info */
647 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
648 	    sizeof (mib2_ipv6IfIcmpEntry_t);
649 	/*
650 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
651 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
652 	 * -> ill_phyint_reinit
653 	 */
654 	return (B_TRUE);
655 }
656 
657 /*
658  * Common code for preparation of ARP commands.  Two points to remember:
659  * 	1) The ill_name is tacked on at the end of the allocated space so
660  *	   the templates name_offset field must contain the total space
661  *	   to allocate less the name length.
662  *
663  *	2) The templates name_length field should contain the *template*
664  *	   length.  We use it as a parameter to bcopy() and then write
665  *	   the real ill_name_length into the name_length field of the copy.
666  * (Always called as writer.)
667  */
668 mblk_t *
669 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
670 {
671 	arc_t	*arc = (arc_t *)template;
672 	char	*cp;
673 	int	len;
674 	mblk_t	*mp;
675 	uint_t	name_length = ill->ill_name_length;
676 	uint_t	template_len = arc->arc_name_length;
677 
678 	len = arc->arc_name_offset + name_length;
679 	mp = allocb(len, BPRI_HI);
680 	if (mp == NULL)
681 		return (NULL);
682 	cp = (char *)mp->b_rptr;
683 	mp->b_wptr = (uchar_t *)&cp[len];
684 	if (template_len)
685 		bcopy(template, cp, template_len);
686 	if (len > template_len)
687 		bzero(&cp[template_len], len - template_len);
688 	mp->b_datap->db_type = M_PROTO;
689 
690 	arc = (arc_t *)cp;
691 	arc->arc_name_length = name_length;
692 	cp = (char *)arc + arc->arc_name_offset;
693 	bcopy(ill->ill_name, cp, name_length);
694 
695 	if (addr) {
696 		area_t	*area = (area_t *)mp->b_rptr;
697 
698 		cp = (char *)area + area->area_proto_addr_offset;
699 		bcopy(addr, cp, area->area_proto_addr_length);
700 		if (area->area_cmd == AR_ENTRY_ADD) {
701 			cp = (char *)area;
702 			len = area->area_proto_addr_length;
703 			if (area->area_proto_mask_offset)
704 				cp += area->area_proto_mask_offset;
705 			else
706 				cp += area->area_proto_addr_offset + len;
707 			while (len-- > 0)
708 				*cp++ = (char)~0;
709 		}
710 	}
711 	return (mp);
712 }
713 
714 mblk_t *
715 ipif_area_alloc(ipif_t *ipif)
716 {
717 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
718 	    (char *)&ipif->ipif_lcl_addr));
719 }
720 
721 mblk_t *
722 ipif_ared_alloc(ipif_t *ipif)
723 {
724 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
725 	    (char *)&ipif->ipif_lcl_addr));
726 }
727 
728 mblk_t *
729 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
730 {
731 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
732 	    (char *)&addr));
733 }
734 
735 /*
736  * Completely vaporize a lower level tap and all associated interfaces.
737  * ill_delete is called only out of ip_close when the device control
738  * stream is being closed.
739  */
740 void
741 ill_delete(ill_t *ill)
742 {
743 	ipif_t	*ipif;
744 	ill_t	*prev_ill;
745 
746 	/*
747 	 * ill_delete may be forcibly entering the ipsq. The previous
748 	 * ioctl may not have completed and may need to be aborted.
749 	 * ipsq_flush takes care of it. If we don't need to enter the
750 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
751 	 * ill_delete_tail is sufficient.
752 	 */
753 	ipsq_flush(ill);
754 
755 	/*
756 	 * Nuke all interfaces.  ipif_free will take down the interface,
757 	 * remove it from the list, and free the data structure.
758 	 * Walk down the ipif list and remove the logical interfaces
759 	 * first before removing the main ipif. We can't unplumb
760 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
761 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
762 	 * POINTOPOINT.
763 	 *
764 	 * If ill_ipif was not properly initialized (i.e low on memory),
765 	 * then no interfaces to clean up. In this case just clean up the
766 	 * ill.
767 	 */
768 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
769 		ipif_free(ipif);
770 
771 	/*
772 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
773 	 * So nobody can be using this mp now. Free the mp allocated for
774 	 * honoring ILLF_NOARP
775 	 */
776 	freemsg(ill->ill_arp_on_mp);
777 	ill->ill_arp_on_mp = NULL;
778 
779 	/* Clean up msgs on pending upcalls for mrouted */
780 	reset_mrt_ill(ill);
781 
782 	/*
783 	 * ipif_free -> reset_conn_ipif will remove all multicast
784 	 * references for IPv4. For IPv6, we need to do it here as
785 	 * it points only at ills.
786 	 */
787 	reset_conn_ill(ill);
788 
789 	/*
790 	 * ill_down will arrange to blow off any IRE's dependent on this
791 	 * ILL, and shut down fragmentation reassembly.
792 	 */
793 	ill_down(ill);
794 
795 	/* Let SCTP know, so that it can remove this from its list. */
796 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
797 
798 	/*
799 	 * If an address on this ILL is being used as a source address then
800 	 * clear out the pointers in other ILLs that point to this ILL.
801 	 */
802 	rw_enter(&ill_g_usesrc_lock, RW_WRITER);
803 	if (ill->ill_usesrc_grp_next != NULL) {
804 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
805 			ill_disband_usesrc_group(ill);
806 		} else {	/* consumer of the usesrc ILL */
807 			prev_ill = ill_prev_usesrc(ill);
808 			prev_ill->ill_usesrc_grp_next =
809 			    ill->ill_usesrc_grp_next;
810 		}
811 	}
812 	rw_exit(&ill_g_usesrc_lock);
813 }
814 
815 static void
816 ipif_non_duplicate(ipif_t *ipif)
817 {
818 	ill_t *ill = ipif->ipif_ill;
819 	mutex_enter(&ill->ill_lock);
820 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
821 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
822 		ASSERT(ill->ill_ipif_dup_count > 0);
823 		ill->ill_ipif_dup_count--;
824 	}
825 	mutex_exit(&ill->ill_lock);
826 }
827 
828 /*
829  * Send all deferred messages without waiting for their ACKs.
830  */
831 void
832 ill_send_all_deferred_mp(ill_t *ill)
833 {
834 	mblk_t *mp, *next;
835 
836 	/*
837 	 * Clear ill_dlpi_pending so that the message is not queued in
838 	 * ill_dlpi_send().
839 	 */
840 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
841 
842 	for (mp = ill->ill_dlpi_deferred; mp != NULL; mp = next) {
843 		next = mp->b_next;
844 		mp->b_next = NULL;
845 		ill_dlpi_send(ill, mp);
846 	}
847 	ill->ill_dlpi_deferred = NULL;
848 }
849 
850 /*
851  * ill_delete_tail is called from ip_modclose after all references
852  * to the closing ill are gone. The wait is done in ip_modclose
853  */
854 void
855 ill_delete_tail(ill_t *ill)
856 {
857 	mblk_t	**mpp;
858 	ipif_t	*ipif;
859 
860 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
861 		ipif_non_duplicate(ipif);
862 		ipif_down_tail(ipif);
863 	}
864 
865 	ASSERT(ill->ill_ipif_dup_count == 0 &&
866 	    ill->ill_arp_down_mp == NULL &&
867 	    ill->ill_arp_del_mapping_mp == NULL);
868 
869 	/*
870 	 * If polling capability is enabled (which signifies direct
871 	 * upcall into IP and driver has ill saved as a handle),
872 	 * we need to make sure that unbind has completed before we
873 	 * let the ill disappear and driver no longer has any reference
874 	 * to this ill.
875 	 */
876 	mutex_enter(&ill->ill_lock);
877 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
878 		cv_wait(&ill->ill_cv, &ill->ill_lock);
879 	mutex_exit(&ill->ill_lock);
880 
881 	/*
882 	 * Clean up polling and soft ring capabilities
883 	 */
884 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
885 		ill_capability_dls_disable(ill);
886 
887 	/*
888 	 * Send the detach if there's one to send (i.e., if we're above a
889 	 * style 2 DLPI driver).
890 	 */
891 	if (ill->ill_detach_mp != NULL) {
892 		ill_dlpi_send(ill, ill->ill_detach_mp);
893 		ill->ill_detach_mp = NULL;
894 	}
895 
896 	if (ill->ill_net_type != IRE_LOOPBACK)
897 		qprocsoff(ill->ill_rq);
898 
899 	/*
900 	 * We do an ipsq_flush once again now. New messages could have
901 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
902 	 * could also have landed up if an ioctl thread had looked up
903 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
904 	 * enqueued the ioctl when we did the ipsq_flush last time.
905 	 */
906 	ipsq_flush(ill);
907 
908 	/*
909 	 * Free capabilities.
910 	 */
911 	if (ill->ill_ipsec_capab_ah != NULL) {
912 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
913 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
914 		ill->ill_ipsec_capab_ah = NULL;
915 	}
916 
917 	if (ill->ill_ipsec_capab_esp != NULL) {
918 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
919 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
920 		ill->ill_ipsec_capab_esp = NULL;
921 	}
922 
923 	if (ill->ill_mdt_capab != NULL) {
924 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
925 		ill->ill_mdt_capab = NULL;
926 	}
927 
928 	if (ill->ill_hcksum_capab != NULL) {
929 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
930 		ill->ill_hcksum_capab = NULL;
931 	}
932 
933 	if (ill->ill_zerocopy_capab != NULL) {
934 		kmem_free(ill->ill_zerocopy_capab,
935 		    sizeof (ill_zerocopy_capab_t));
936 		ill->ill_zerocopy_capab = NULL;
937 	}
938 
939 	if (ill->ill_lso_capab != NULL) {
940 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
941 		ill->ill_lso_capab = NULL;
942 	}
943 
944 	if (ill->ill_dls_capab != NULL) {
945 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
946 		ill->ill_dls_capab->ill_unbind_conn = NULL;
947 		kmem_free(ill->ill_dls_capab,
948 		    sizeof (ill_dls_capab_t) +
949 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
950 		ill->ill_dls_capab = NULL;
951 	}
952 
953 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
954 
955 	while (ill->ill_ipif != NULL)
956 		ipif_free_tail(ill->ill_ipif);
957 
958 	ill_down_tail(ill);
959 
960 	/*
961 	 * We have removed all references to ilm from conn and the ones joined
962 	 * within the kernel.
963 	 *
964 	 * We don't walk conns, mrts and ires because
965 	 *
966 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
967 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
968 	 *    ill references.
969 	 */
970 	ASSERT(ilm_walk_ill(ill) == 0);
971 	/*
972 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
973 	 * could free the phyint. No more reference to the phyint after this
974 	 * point.
975 	 */
976 	(void) ill_glist_delete(ill);
977 
978 	rw_enter(&ip_g_nd_lock, RW_WRITER);
979 	if (ill->ill_ndd_name != NULL)
980 		nd_unload(&ip_g_nd, ill->ill_ndd_name);
981 	rw_exit(&ip_g_nd_lock);
982 
983 
984 	if (ill->ill_frag_ptr != NULL) {
985 		uint_t count;
986 
987 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
988 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
989 		}
990 		mi_free(ill->ill_frag_ptr);
991 		ill->ill_frag_ptr = NULL;
992 		ill->ill_frag_hash_tbl = NULL;
993 	}
994 	if (ill->ill_nd_lla_mp != NULL)
995 		freemsg(ill->ill_nd_lla_mp);
996 	/* Free all retained control messages. */
997 	mpp = &ill->ill_first_mp_to_free;
998 	do {
999 		while (mpp[0]) {
1000 			mblk_t  *mp;
1001 			mblk_t  *mp1;
1002 
1003 			mp = mpp[0];
1004 			mpp[0] = mp->b_next;
1005 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
1006 				mp1->b_next = NULL;
1007 				mp1->b_prev = NULL;
1008 			}
1009 			freemsg(mp);
1010 		}
1011 	} while (mpp++ != &ill->ill_last_mp_to_free);
1012 
1013 	ill_free_mib(ill);
1014 	ILL_TRACE_CLEANUP(ill);
1015 }
1016 
1017 static void
1018 ill_free_mib(ill_t *ill)
1019 {
1020 	/*
1021 	 * MIB statistics must not be lost, so when an interface
1022 	 * goes away the counter values will be added to the global
1023 	 * MIBs.
1024 	 */
1025 	if (ill->ill_ip_mib != NULL) {
1026 		if (ill->ill_isv6)
1027 			ip_mib2_add_ip_stats(&ip6_mib, ill->ill_ip_mib);
1028 		else
1029 			ip_mib2_add_ip_stats(&ip_mib, ill->ill_ip_mib);
1030 
1031 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
1032 		ill->ill_ip_mib = NULL;
1033 	}
1034 	if (ill->ill_icmp6_mib != NULL) {
1035 		ip_mib2_add_icmp6_stats(&icmp6_mib, ill->ill_icmp6_mib);
1036 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
1037 		ill->ill_icmp6_mib = NULL;
1038 	}
1039 }
1040 
1041 /*
1042  * Concatenate together a physical address and a sap.
1043  *
1044  * Sap_lengths are interpreted as follows:
1045  *   sap_length == 0	==>	no sap
1046  *   sap_length > 0	==>	sap is at the head of the dlpi address
1047  *   sap_length < 0	==>	sap is at the tail of the dlpi address
1048  */
1049 static void
1050 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
1051     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
1052 {
1053 	uint16_t sap_addr = (uint16_t)sap_src;
1054 
1055 	if (sap_length == 0) {
1056 		if (phys_src == NULL)
1057 			bzero(dst, phys_length);
1058 		else
1059 			bcopy(phys_src, dst, phys_length);
1060 	} else if (sap_length < 0) {
1061 		if (phys_src == NULL)
1062 			bzero(dst, phys_length);
1063 		else
1064 			bcopy(phys_src, dst, phys_length);
1065 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1066 	} else {
1067 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1068 		if (phys_src == NULL)
1069 			bzero((char *)dst + sap_length, phys_length);
1070 		else
1071 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1072 	}
1073 }
1074 
1075 /*
1076  * Generate a dl_unitdata_req mblk for the device and address given.
1077  * addr_length is the length of the physical portion of the address.
1078  * If addr is NULL include an all zero address of the specified length.
1079  * TRUE? In any case, addr_length is taken to be the entire length of the
1080  * dlpi address, including the absolute value of sap_length.
1081  */
1082 mblk_t *
1083 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1084 		t_scalar_t sap_length)
1085 {
1086 	dl_unitdata_req_t *dlur;
1087 	mblk_t	*mp;
1088 	t_scalar_t	abs_sap_length;		/* absolute value */
1089 
1090 	abs_sap_length = ABS(sap_length);
1091 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1092 		DL_UNITDATA_REQ);
1093 	if (mp == NULL)
1094 		return (NULL);
1095 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1096 	/* HACK: accomodate incompatible DLPI drivers */
1097 	if (addr_length == 8)
1098 		addr_length = 6;
1099 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1100 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1101 	dlur->dl_priority.dl_min = 0;
1102 	dlur->dl_priority.dl_max = 0;
1103 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1104 	    (uchar_t *)&dlur[1]);
1105 	return (mp);
1106 }
1107 
1108 /*
1109  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1110  * Return an error if we already have 1 or more ioctls in progress.
1111  * This is used only for non-exclusive ioctls. Currently this is used
1112  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1113  * and thus need to use ipsq_pending_mp_add.
1114  */
1115 boolean_t
1116 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1117 {
1118 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1119 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1120 	/*
1121 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1122 	 */
1123 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1124 	    (add_mp->b_datap->db_type == M_IOCTL));
1125 
1126 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1127 	/*
1128 	 * Return error if the conn has started closing. The conn
1129 	 * could have finished cleaning up the pending mp list,
1130 	 * If so we should not add another mp to the list negating
1131 	 * the cleanup.
1132 	 */
1133 	if (connp->conn_state_flags & CONN_CLOSING)
1134 		return (B_FALSE);
1135 	/*
1136 	 * Add the pending mp to the head of the list, chained by b_next.
1137 	 * Note down the conn on which the ioctl request came, in b_prev.
1138 	 * This will be used to later get the conn, when we get a response
1139 	 * on the ill queue, from some other module (typically arp)
1140 	 */
1141 	add_mp->b_next = (void *)ill->ill_pending_mp;
1142 	add_mp->b_queue = CONNP_TO_WQ(connp);
1143 	ill->ill_pending_mp = add_mp;
1144 	if (connp != NULL)
1145 		connp->conn_oper_pending_ill = ill;
1146 	return (B_TRUE);
1147 }
1148 
1149 /*
1150  * Retrieve the ill_pending_mp and return it. We have to walk the list
1151  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1152  */
1153 mblk_t *
1154 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1155 {
1156 	mblk_t	*prev = NULL;
1157 	mblk_t	*curr = NULL;
1158 	uint_t	id;
1159 	conn_t	*connp;
1160 
1161 	/*
1162 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1163 	 * up the pending mp, but it does not know the ioc_id and
1164 	 * passes in a zero for it.
1165 	 */
1166 	mutex_enter(&ill->ill_lock);
1167 	if (ioc_id != 0)
1168 		*connpp = NULL;
1169 
1170 	/* Search the list for the appropriate ioctl based on ioc_id */
1171 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1172 	    prev = curr, curr = curr->b_next) {
1173 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1174 		connp = Q_TO_CONN(curr->b_queue);
1175 		/* Match based on the ioc_id or based on the conn */
1176 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1177 			break;
1178 	}
1179 
1180 	if (curr != NULL) {
1181 		/* Unlink the mblk from the pending mp list */
1182 		if (prev != NULL) {
1183 			prev->b_next = curr->b_next;
1184 		} else {
1185 			ASSERT(ill->ill_pending_mp == curr);
1186 			ill->ill_pending_mp = curr->b_next;
1187 		}
1188 
1189 		/*
1190 		 * conn refcnt must have been bumped up at the start of
1191 		 * the ioctl. So we can safely access the conn.
1192 		 */
1193 		ASSERT(CONN_Q(curr->b_queue));
1194 		*connpp = Q_TO_CONN(curr->b_queue);
1195 		curr->b_next = NULL;
1196 		curr->b_queue = NULL;
1197 	}
1198 
1199 	mutex_exit(&ill->ill_lock);
1200 
1201 	return (curr);
1202 }
1203 
1204 /*
1205  * Add the pending mp to the list. There can be only 1 pending mp
1206  * in the list. Any exclusive ioctl that needs to wait for a response
1207  * from another module or driver needs to use this function to set
1208  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1209  * the other module/driver. This is also used while waiting for the
1210  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1211  */
1212 boolean_t
1213 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1214     int waitfor)
1215 {
1216 	ipsq_t	*ipsq;
1217 
1218 	ASSERT(IAM_WRITER_IPIF(ipif));
1219 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1220 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1221 	/*
1222 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1223 	 * M_ERROR/M_HANGUP from driver
1224 	 */
1225 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1226 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP));
1227 
1228 	ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1229 	if (connp != NULL) {
1230 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1231 		/*
1232 		 * Return error if the conn has started closing. The conn
1233 		 * could have finished cleaning up the pending mp list,
1234 		 * If so we should not add another mp to the list negating
1235 		 * the cleanup.
1236 		 */
1237 		if (connp->conn_state_flags & CONN_CLOSING)
1238 			return (B_FALSE);
1239 	}
1240 	mutex_enter(&ipsq->ipsq_lock);
1241 	ipsq->ipsq_pending_ipif = ipif;
1242 	/*
1243 	 * Note down the queue in b_queue. This will be returned by
1244 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1245 	 * the processing
1246 	 */
1247 	add_mp->b_next = NULL;
1248 	add_mp->b_queue = q;
1249 	ipsq->ipsq_pending_mp = add_mp;
1250 	ipsq->ipsq_waitfor = waitfor;
1251 	/*
1252 	 * ipsq_current_ipif is needed to restart the operation from
1253 	 * ipif_ill_refrele_tail when the last reference to the ipi/ill
1254 	 * is gone. Since this is not an ioctl ipsq_current_ipif has not
1255 	 * been set until now.
1256 	 */
1257 	if (DB_TYPE(add_mp) == M_ERROR || DB_TYPE(add_mp) == M_HANGUP) {
1258 		ASSERT(ipsq->ipsq_current_ipif == NULL);
1259 		ipsq->ipsq_current_ipif = ipif;
1260 		ipsq->ipsq_last_cmd = DB_TYPE(add_mp);
1261 	}
1262 	if (connp != NULL)
1263 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1264 	mutex_exit(&ipsq->ipsq_lock);
1265 	return (B_TRUE);
1266 }
1267 
1268 /*
1269  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1270  * queued in the list.
1271  */
1272 mblk_t *
1273 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1274 {
1275 	mblk_t	*curr = NULL;
1276 
1277 	mutex_enter(&ipsq->ipsq_lock);
1278 	*connpp = NULL;
1279 	if (ipsq->ipsq_pending_mp == NULL) {
1280 		mutex_exit(&ipsq->ipsq_lock);
1281 		return (NULL);
1282 	}
1283 
1284 	/* There can be only 1 such excl message */
1285 	curr = ipsq->ipsq_pending_mp;
1286 	ASSERT(curr != NULL && curr->b_next == NULL);
1287 	ipsq->ipsq_pending_ipif = NULL;
1288 	ipsq->ipsq_pending_mp = NULL;
1289 	ipsq->ipsq_waitfor = 0;
1290 	mutex_exit(&ipsq->ipsq_lock);
1291 
1292 	if (CONN_Q(curr->b_queue)) {
1293 		/*
1294 		 * This mp did a refhold on the conn, at the start of the ioctl.
1295 		 * So we can safely return a pointer to the conn to the caller.
1296 		 */
1297 		*connpp = Q_TO_CONN(curr->b_queue);
1298 	} else {
1299 		*connpp = NULL;
1300 	}
1301 	curr->b_next = NULL;
1302 	curr->b_prev = NULL;
1303 	return (curr);
1304 }
1305 
1306 /*
1307  * Cleanup the ioctl mp queued in ipsq_pending_mp
1308  * - Called in the ill_delete path
1309  * - Called in the M_ERROR or M_HANGUP path on the ill.
1310  * - Called in the conn close path.
1311  */
1312 boolean_t
1313 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1314 {
1315 	mblk_t	*mp;
1316 	ipsq_t	*ipsq;
1317 	queue_t	*q;
1318 	ipif_t	*ipif;
1319 
1320 	ASSERT(IAM_WRITER_ILL(ill));
1321 	ipsq = ill->ill_phyint->phyint_ipsq;
1322 	mutex_enter(&ipsq->ipsq_lock);
1323 	/*
1324 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1325 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1326 	 * even if it is meant for another ill, since we have to enqueue
1327 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1328 	 * If connp is non-null we are called from the conn close path.
1329 	 */
1330 	mp = ipsq->ipsq_pending_mp;
1331 	if (mp == NULL || (connp != NULL &&
1332 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1333 		mutex_exit(&ipsq->ipsq_lock);
1334 		return (B_FALSE);
1335 	}
1336 	/* Now remove from the ipsq_pending_mp */
1337 	ipsq->ipsq_pending_mp = NULL;
1338 	q = mp->b_queue;
1339 	mp->b_next = NULL;
1340 	mp->b_prev = NULL;
1341 	mp->b_queue = NULL;
1342 
1343 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1344 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1345 	if (ill->ill_move_in_progress) {
1346 		ILL_CLEAR_MOVE(ill);
1347 	} else if (ill->ill_up_ipifs) {
1348 		ill_group_cleanup(ill);
1349 	}
1350 
1351 	ipif = ipsq->ipsq_pending_ipif;
1352 	ipsq->ipsq_pending_ipif = NULL;
1353 	ipsq->ipsq_waitfor = 0;
1354 	ipsq->ipsq_current_ipif = NULL;
1355 	mutex_exit(&ipsq->ipsq_lock);
1356 
1357 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1358 		ip_ioctl_finish(q, mp, ENXIO, connp != NULL ? CONN_CLOSE :
1359 		    NO_COPYOUT, connp != NULL ? ipif : NULL, NULL);
1360 	} else {
1361 		/*
1362 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1363 		 * be just inet_freemsg. we have to restart it
1364 		 * otherwise the thread will be stuck.
1365 		 */
1366 		inet_freemsg(mp);
1367 	}
1368 	return (B_TRUE);
1369 }
1370 
1371 /*
1372  * The ill is closing. Cleanup all the pending mps. Called exclusively
1373  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1374  * knows this ill, and hence nobody can add an mp to this list
1375  */
1376 static void
1377 ill_pending_mp_cleanup(ill_t *ill)
1378 {
1379 	mblk_t	*mp;
1380 	queue_t	*q;
1381 
1382 	ASSERT(IAM_WRITER_ILL(ill));
1383 
1384 	mutex_enter(&ill->ill_lock);
1385 	/*
1386 	 * Every mp on the pending mp list originating from an ioctl
1387 	 * added 1 to the conn refcnt, at the start of the ioctl.
1388 	 * So bump it down now.  See comments in ip_wput_nondata()
1389 	 */
1390 	while (ill->ill_pending_mp != NULL) {
1391 		mp = ill->ill_pending_mp;
1392 		ill->ill_pending_mp = mp->b_next;
1393 		mutex_exit(&ill->ill_lock);
1394 
1395 		q = mp->b_queue;
1396 		ASSERT(CONN_Q(q));
1397 		mp->b_next = NULL;
1398 		mp->b_prev = NULL;
1399 		mp->b_queue = NULL;
1400 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL, NULL);
1401 		mutex_enter(&ill->ill_lock);
1402 	}
1403 	ill->ill_pending_ipif = NULL;
1404 
1405 	mutex_exit(&ill->ill_lock);
1406 }
1407 
1408 /*
1409  * Called in the conn close path and ill delete path
1410  */
1411 static void
1412 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1413 {
1414 	ipsq_t	*ipsq;
1415 	mblk_t	*prev;
1416 	mblk_t	*curr;
1417 	mblk_t	*next;
1418 	queue_t	*q;
1419 	mblk_t	*tmp_list = NULL;
1420 
1421 	ASSERT(IAM_WRITER_ILL(ill));
1422 	if (connp != NULL)
1423 		q = CONNP_TO_WQ(connp);
1424 	else
1425 		q = ill->ill_wq;
1426 
1427 	ipsq = ill->ill_phyint->phyint_ipsq;
1428 	/*
1429 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1430 	 * In the case of ioctl from a conn, there can be only 1 mp
1431 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1432 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1433 	 * ioctls meant for this ill form conn's are not flushed. They will
1434 	 * be processed during ipsq_exit and will not find the ill and will
1435 	 * return error.
1436 	 */
1437 	mutex_enter(&ipsq->ipsq_lock);
1438 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1439 	    curr = next) {
1440 		next = curr->b_next;
1441 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1442 			/* Unlink the mblk from the pending mp list */
1443 			if (prev != NULL) {
1444 				prev->b_next = curr->b_next;
1445 			} else {
1446 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1447 				ipsq->ipsq_xopq_mphead = curr->b_next;
1448 			}
1449 			if (ipsq->ipsq_xopq_mptail == curr)
1450 				ipsq->ipsq_xopq_mptail = prev;
1451 			/*
1452 			 * Create a temporary list and release the ipsq lock
1453 			 * New elements are added to the head of the tmp_list
1454 			 */
1455 			curr->b_next = tmp_list;
1456 			tmp_list = curr;
1457 		} else {
1458 			prev = curr;
1459 		}
1460 	}
1461 	mutex_exit(&ipsq->ipsq_lock);
1462 
1463 	while (tmp_list != NULL) {
1464 		curr = tmp_list;
1465 		tmp_list = curr->b_next;
1466 		curr->b_next = NULL;
1467 		curr->b_prev = NULL;
1468 		curr->b_queue = NULL;
1469 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1470 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1471 			    CONN_CLOSE : NO_COPYOUT, NULL, NULL);
1472 		} else {
1473 			/*
1474 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1475 			 * this can't be just inet_freemsg. we have to
1476 			 * restart it otherwise the thread will be stuck.
1477 			 */
1478 			inet_freemsg(curr);
1479 		}
1480 	}
1481 }
1482 
1483 /*
1484  * This conn has started closing. Cleanup any pending ioctl from this conn.
1485  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1486  */
1487 void
1488 conn_ioctl_cleanup(conn_t *connp)
1489 {
1490 	mblk_t *curr;
1491 	ipsq_t	*ipsq;
1492 	ill_t	*ill;
1493 	boolean_t refheld;
1494 
1495 	/*
1496 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1497 	 * ioctl has not yet started, the mp is pending in the list headed by
1498 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1499 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1500 	 * is currently executing now the mp is not queued anywhere but
1501 	 * conn_oper_pending_ill is null. The conn close will wait
1502 	 * till the conn_ref drops to zero.
1503 	 */
1504 	mutex_enter(&connp->conn_lock);
1505 	ill = connp->conn_oper_pending_ill;
1506 	if (ill == NULL) {
1507 		mutex_exit(&connp->conn_lock);
1508 		return;
1509 	}
1510 
1511 	curr = ill_pending_mp_get(ill, &connp, 0);
1512 	if (curr != NULL) {
1513 		mutex_exit(&connp->conn_lock);
1514 		CONN_DEC_REF(connp);
1515 		inet_freemsg(curr);
1516 		return;
1517 	}
1518 	/*
1519 	 * We may not be able to refhold the ill if the ill/ipif
1520 	 * is changing. But we need to make sure that the ill will
1521 	 * not vanish. So we just bump up the ill_waiter count.
1522 	 */
1523 	refheld = ill_waiter_inc(ill);
1524 	mutex_exit(&connp->conn_lock);
1525 	if (refheld) {
1526 		if (ipsq_enter(ill, B_TRUE)) {
1527 			ill_waiter_dcr(ill);
1528 			/*
1529 			 * Check whether this ioctl has started and is
1530 			 * pending now in ipsq_pending_mp. If it is not
1531 			 * found there then check whether this ioctl has
1532 			 * not even started and is in the ipsq_xopq list.
1533 			 */
1534 			if (!ipsq_pending_mp_cleanup(ill, connp))
1535 				ipsq_xopq_mp_cleanup(ill, connp);
1536 			ipsq = ill->ill_phyint->phyint_ipsq;
1537 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1538 			return;
1539 		}
1540 	}
1541 
1542 	/*
1543 	 * The ill is also closing and we could not bump up the
1544 	 * ill_waiter_count or we could not enter the ipsq. Leave
1545 	 * the cleanup to ill_delete
1546 	 */
1547 	mutex_enter(&connp->conn_lock);
1548 	while (connp->conn_oper_pending_ill != NULL)
1549 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1550 	mutex_exit(&connp->conn_lock);
1551 	if (refheld)
1552 		ill_waiter_dcr(ill);
1553 }
1554 
1555 /*
1556  * ipcl_walk function for cleaning up conn_*_ill fields.
1557  */
1558 static void
1559 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1560 {
1561 	ill_t	*ill = (ill_t *)arg;
1562 	ire_t	*ire;
1563 
1564 	mutex_enter(&connp->conn_lock);
1565 	if (connp->conn_multicast_ill == ill) {
1566 		/* Revert to late binding */
1567 		connp->conn_multicast_ill = NULL;
1568 		connp->conn_orig_multicast_ifindex = 0;
1569 	}
1570 	if (connp->conn_incoming_ill == ill)
1571 		connp->conn_incoming_ill = NULL;
1572 	if (connp->conn_outgoing_ill == ill)
1573 		connp->conn_outgoing_ill = NULL;
1574 	if (connp->conn_outgoing_pill == ill)
1575 		connp->conn_outgoing_pill = NULL;
1576 	if (connp->conn_nofailover_ill == ill)
1577 		connp->conn_nofailover_ill = NULL;
1578 	if (connp->conn_xmit_if_ill == ill)
1579 		connp->conn_xmit_if_ill = NULL;
1580 	if (connp->conn_ire_cache != NULL) {
1581 		ire = connp->conn_ire_cache;
1582 		/*
1583 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1584 		 * interface X and ipif coming from interface Y, if interface
1585 		 * X and Y are part of the same IPMPgroup. Thus whenever
1586 		 * interface X goes down, remove all references to it by
1587 		 * checking both on ire_ipif and ire_stq.
1588 		 */
1589 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1590 		    (ire->ire_type == IRE_CACHE &&
1591 		    ire->ire_stq == ill->ill_wq)) {
1592 			connp->conn_ire_cache = NULL;
1593 			mutex_exit(&connp->conn_lock);
1594 			ire_refrele_notr(ire);
1595 			return;
1596 		}
1597 	}
1598 	mutex_exit(&connp->conn_lock);
1599 
1600 }
1601 
1602 /* ARGSUSED */
1603 void
1604 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1605 {
1606 	ill_t	*ill = q->q_ptr;
1607 	ipif_t	*ipif;
1608 
1609 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1610 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1611 		ipif_non_duplicate(ipif);
1612 		ipif_down_tail(ipif);
1613 	}
1614 	ill_down_tail(ill);
1615 	freemsg(mp);
1616 	ipsq->ipsq_current_ipif = NULL;
1617 }
1618 
1619 /*
1620  * ill_down_start is called when we want to down this ill and bring it up again
1621  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1622  * all interfaces, but don't tear down any plumbing.
1623  */
1624 boolean_t
1625 ill_down_start(queue_t *q, mblk_t *mp)
1626 {
1627 	ill_t	*ill;
1628 	ipif_t	*ipif;
1629 
1630 	ill = q->q_ptr;
1631 
1632 	ASSERT(IAM_WRITER_ILL(ill));
1633 
1634 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1635 		(void) ipif_down(ipif, NULL, NULL);
1636 
1637 	ill_down(ill);
1638 
1639 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1640 	mutex_enter(&ill->ill_lock);
1641 	/*
1642 	 * Atomically test and add the pending mp if references are
1643 	 * still active.
1644 	 */
1645 	if (!ill_is_quiescent(ill)) {
1646 		/*
1647 		 * Get rid of any pending mps and cleanup. Call will
1648 		 * not fail since we are passing a null connp.
1649 		 */
1650 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1651 		    mp, ILL_DOWN);
1652 		mutex_exit(&ill->ill_lock);
1653 		return (B_FALSE);
1654 	}
1655 	mutex_exit(&ill->ill_lock);
1656 	return (B_TRUE);
1657 }
1658 
1659 static void
1660 ill_down(ill_t *ill)
1661 {
1662 	/* Blow off any IREs dependent on this ILL. */
1663 	ire_walk(ill_downi, (char *)ill);
1664 
1665 	mutex_enter(&ire_mrtun_lock);
1666 	if (ire_mrtun_count != 0) {
1667 		mutex_exit(&ire_mrtun_lock);
1668 		ire_walk_ill_mrtun(0, 0, ill_downi_mrtun_srcif,
1669 		    (char *)ill, NULL);
1670 	} else {
1671 		mutex_exit(&ire_mrtun_lock);
1672 	}
1673 
1674 	/*
1675 	 * If any interface based forwarding table exists
1676 	 * Blow off the ires there dependent on this ill
1677 	 */
1678 	mutex_enter(&ire_srcif_table_lock);
1679 	if (ire_srcif_table_count > 0) {
1680 		mutex_exit(&ire_srcif_table_lock);
1681 		ire_walk_srcif_table_v4(ill_downi_mrtun_srcif, (char *)ill);
1682 	} else {
1683 		mutex_exit(&ire_srcif_table_lock);
1684 	}
1685 
1686 	/* Remove any conn_*_ill depending on this ill */
1687 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill);
1688 
1689 	if (ill->ill_group != NULL) {
1690 		illgrp_delete(ill);
1691 	}
1692 
1693 }
1694 
1695 static void
1696 ill_down_tail(ill_t *ill)
1697 {
1698 	int	i;
1699 
1700 	/* Destroy ill_srcif_table if it exists */
1701 	/* Lock not reqd really because nobody should be able to access */
1702 	mutex_enter(&ill->ill_lock);
1703 	if (ill->ill_srcif_table != NULL) {
1704 		ill->ill_srcif_refcnt = 0;
1705 		for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
1706 			rw_destroy(&ill->ill_srcif_table[i].irb_lock);
1707 		}
1708 		kmem_free(ill->ill_srcif_table,
1709 		    IP_SRCIF_TABLE_SIZE * sizeof (irb_t));
1710 		ill->ill_srcif_table = NULL;
1711 		ill->ill_srcif_refcnt = 0;
1712 		ill->ill_mrtun_refcnt = 0;
1713 	}
1714 	mutex_exit(&ill->ill_lock);
1715 }
1716 
1717 /*
1718  * ire_walk routine used to delete every IRE that depends on queues
1719  * associated with 'ill'.  (Always called as writer.)
1720  */
1721 static void
1722 ill_downi(ire_t *ire, char *ill_arg)
1723 {
1724 	ill_t	*ill = (ill_t *)ill_arg;
1725 
1726 	/*
1727 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1728 	 * interface X and ipif coming from interface Y, if interface
1729 	 * X and Y are part of the same IPMP group. Thus whenever interface
1730 	 * X goes down, remove all references to it by checking both
1731 	 * on ire_ipif and ire_stq.
1732 	 */
1733 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1734 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1735 		ire_delete(ire);
1736 	}
1737 }
1738 
1739 /*
1740  * A seperate routine for deleting revtun and srcif based routes
1741  * are needed because the ires only deleted when the interface
1742  * is unplumbed. Also these ires have ire_in_ill non-null as well.
1743  * we want to keep mobile IP specific code separate.
1744  */
1745 static void
1746 ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg)
1747 {
1748 	ill_t   *ill = (ill_t *)ill_arg;
1749 
1750 	ASSERT(ire->ire_in_ill != NULL);
1751 
1752 	if ((ire->ire_in_ill != NULL && ire->ire_in_ill == ill) ||
1753 	    (ire->ire_stq == ill->ill_wq) || (ire->ire_stq == ill->ill_rq)) {
1754 		ire_delete(ire);
1755 	}
1756 }
1757 
1758 /*
1759  * Remove ire/nce from the fastpath list.
1760  */
1761 void
1762 ill_fastpath_nack(ill_t *ill)
1763 {
1764 	if (ill->ill_isv6) {
1765 		nce_fastpath_list_dispatch(ill, NULL, NULL);
1766 	} else {
1767 		ire_fastpath_list_dispatch(ill, NULL, NULL);
1768 	}
1769 }
1770 
1771 /* Consume an M_IOCACK of the fastpath probe. */
1772 void
1773 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1774 {
1775 	mblk_t	*mp1 = mp;
1776 
1777 	/*
1778 	 * If this was the first attempt turn on the fastpath probing.
1779 	 */
1780 	mutex_enter(&ill->ill_lock);
1781 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1782 		ill->ill_dlpi_fastpath_state = IDS_OK;
1783 	mutex_exit(&ill->ill_lock);
1784 
1785 	/* Free the M_IOCACK mblk, hold on to the data */
1786 	mp = mp->b_cont;
1787 	freeb(mp1);
1788 	if (mp == NULL)
1789 		return;
1790 	if (mp->b_cont != NULL) {
1791 		/*
1792 		 * Update all IRE's or NCE's that are waiting for
1793 		 * fastpath update.
1794 		 */
1795 		if (ill->ill_isv6) {
1796 			/*
1797 			 * update nce's in the fastpath list.
1798 			 */
1799 			nce_fastpath_list_dispatch(ill,
1800 			    ndp_fastpath_update, mp);
1801 		} else {
1802 
1803 			/*
1804 			 * update ire's in the fastpath list.
1805 			 */
1806 			ire_fastpath_list_dispatch(ill,
1807 			    ire_fastpath_update, mp);
1808 			/*
1809 			 * Check if we need to traverse reverse tunnel table.
1810 			 * Since there is only single ire_type (IRE_MIPRTUN)
1811 			 * in the table, we don't need to match on ire_type.
1812 			 * We have to check ire_mrtun_count and not the
1813 			 * ill_mrtun_refcnt since ill_mrtun_refcnt is set
1814 			 * on the incoming ill and here we are dealing with
1815 			 * outgoing ill.
1816 			 */
1817 			mutex_enter(&ire_mrtun_lock);
1818 			if (ire_mrtun_count != 0) {
1819 				mutex_exit(&ire_mrtun_lock);
1820 				ire_walk_ill_mrtun(MATCH_IRE_WQ, IRE_MIPRTUN,
1821 				    (void (*)(ire_t *, void *))
1822 					ire_fastpath_update, mp, ill);
1823 			} else {
1824 				mutex_exit(&ire_mrtun_lock);
1825 			}
1826 		}
1827 		mp1 = mp->b_cont;
1828 		freeb(mp);
1829 		mp = mp1;
1830 	} else {
1831 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1832 	}
1833 
1834 	freeb(mp);
1835 }
1836 
1837 /*
1838  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1839  * The data portion of the request is a dl_unitdata_req_t template for
1840  * what we would send downstream in the absence of a fastpath confirmation.
1841  */
1842 int
1843 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1844 {
1845 	struct iocblk	*ioc;
1846 	mblk_t	*mp;
1847 
1848 	if (dlur_mp == NULL)
1849 		return (EINVAL);
1850 
1851 	mutex_enter(&ill->ill_lock);
1852 	switch (ill->ill_dlpi_fastpath_state) {
1853 	case IDS_FAILED:
1854 		/*
1855 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1856 		 * support it.
1857 		 */
1858 		mutex_exit(&ill->ill_lock);
1859 		return (ENOTSUP);
1860 	case IDS_UNKNOWN:
1861 		/* This is the first probe */
1862 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1863 		break;
1864 	default:
1865 		break;
1866 	}
1867 	mutex_exit(&ill->ill_lock);
1868 
1869 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1870 		return (EAGAIN);
1871 
1872 	mp->b_cont = copyb(dlur_mp);
1873 	if (mp->b_cont == NULL) {
1874 		freeb(mp);
1875 		return (EAGAIN);
1876 	}
1877 
1878 	ioc = (struct iocblk *)mp->b_rptr;
1879 	ioc->ioc_count = msgdsize(mp->b_cont);
1880 
1881 	putnext(ill->ill_wq, mp);
1882 	return (0);
1883 }
1884 
1885 void
1886 ill_capability_probe(ill_t *ill)
1887 {
1888 	/*
1889 	 * Do so only if negotiation is enabled, capabilities are unknown,
1890 	 * and a capability negotiation is not already in progress.
1891 	 */
1892 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN &&
1893 	    ill->ill_dlpi_capab_state != IDS_RENEG)
1894 		return;
1895 
1896 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1897 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1898 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1899 }
1900 
1901 void
1902 ill_capability_reset(ill_t *ill)
1903 {
1904 	mblk_t *sc_mp = NULL;
1905 	mblk_t *tmp;
1906 
1907 	/*
1908 	 * Note here that we reset the state to UNKNOWN, and later send
1909 	 * down the DL_CAPABILITY_REQ without first setting the state to
1910 	 * INPROGRESS.  We do this in order to distinguish the
1911 	 * DL_CAPABILITY_ACK response which may come back in response to
1912 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1913 	 * also handle the case where the driver doesn't send us back
1914 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1915 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1916 	 * features are turned off until the state reaches IDS_OK.
1917 	 */
1918 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1919 
1920 	/*
1921 	 * Disable sub-capabilities and request a list of sub-capability
1922 	 * messages which will be sent down to the driver.  Each handler
1923 	 * allocates the corresponding dl_capability_sub_t inside an
1924 	 * mblk, and links it to the existing sc_mp mblk, or return it
1925 	 * as sc_mp if it's the first sub-capability (the passed in
1926 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1927 	 * sc_mp will be pulled-up, before passing it downstream.
1928 	 */
1929 	ill_capability_mdt_reset(ill, &sc_mp);
1930 	ill_capability_hcksum_reset(ill, &sc_mp);
1931 	ill_capability_zerocopy_reset(ill, &sc_mp);
1932 	ill_capability_ipsec_reset(ill, &sc_mp);
1933 	ill_capability_dls_reset(ill, &sc_mp);
1934 	ill_capability_lso_reset(ill, &sc_mp);
1935 
1936 	/* Nothing to send down in order to disable the capabilities? */
1937 	if (sc_mp == NULL)
1938 		return;
1939 
1940 	tmp = msgpullup(sc_mp, -1);
1941 	freemsg(sc_mp);
1942 	if ((sc_mp = tmp) == NULL) {
1943 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1944 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1945 		return;
1946 	}
1947 
1948 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1949 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1950 }
1951 
1952 /*
1953  * Request or set new-style hardware capabilities supported by DLS provider.
1954  */
1955 static void
1956 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1957 {
1958 	mblk_t *mp;
1959 	dl_capability_req_t *capb;
1960 	size_t size = 0;
1961 	uint8_t *ptr;
1962 
1963 	if (reqp != NULL)
1964 		size = MBLKL(reqp);
1965 
1966 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1967 	if (mp == NULL) {
1968 		freemsg(reqp);
1969 		return;
1970 	}
1971 	ptr = mp->b_rptr;
1972 
1973 	capb = (dl_capability_req_t *)ptr;
1974 	ptr += sizeof (dl_capability_req_t);
1975 
1976 	if (reqp != NULL) {
1977 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1978 		capb->dl_sub_length = size;
1979 		bcopy(reqp->b_rptr, ptr, size);
1980 		ptr += size;
1981 		mp->b_cont = reqp->b_cont;
1982 		freeb(reqp);
1983 	}
1984 	ASSERT(ptr == mp->b_wptr);
1985 
1986 	ill_dlpi_send(ill, mp);
1987 }
1988 
1989 static void
1990 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1991 {
1992 	dl_capab_id_t *id_ic;
1993 	uint_t sub_dl_cap = outers->dl_cap;
1994 	dl_capability_sub_t *inners;
1995 	uint8_t *capend;
1996 
1997 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1998 
1999 	/*
2000 	 * Note: range checks here are not absolutely sufficient to
2001 	 * make us robust against malformed messages sent by drivers;
2002 	 * this is in keeping with the rest of IP's dlpi handling.
2003 	 * (Remember, it's coming from something else in the kernel
2004 	 * address space)
2005 	 */
2006 
2007 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
2008 	if (capend > mp->b_wptr) {
2009 		cmn_err(CE_WARN, "ill_capability_id_ack: "
2010 		    "malformed sub-capability too long for mblk");
2011 		return;
2012 	}
2013 
2014 	id_ic = (dl_capab_id_t *)(outers + 1);
2015 
2016 	if (outers->dl_length < sizeof (*id_ic) ||
2017 	    (inners = &id_ic->id_subcap,
2018 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
2019 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
2020 		    "encapsulated capab type %d too long for mblk",
2021 		    inners->dl_cap);
2022 		return;
2023 	}
2024 
2025 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
2026 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
2027 		    "isn't as expected; pass-thru module(s) detected, "
2028 		    "discarding capability\n", inners->dl_cap));
2029 		return;
2030 	}
2031 
2032 	/* Process the encapsulated sub-capability */
2033 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
2034 }
2035 
2036 /*
2037  * Process Multidata Transmit capability negotiation ack received from a
2038  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
2039  * DL_CAPABILITY_ACK message.
2040  */
2041 static void
2042 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2043 {
2044 	mblk_t *nmp = NULL;
2045 	dl_capability_req_t *oc;
2046 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
2047 	ill_mdt_capab_t **ill_mdt_capab;
2048 	uint_t sub_dl_cap = isub->dl_cap;
2049 	uint8_t *capend;
2050 
2051 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
2052 
2053 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
2054 
2055 	/*
2056 	 * Note: range checks here are not absolutely sufficient to
2057 	 * make us robust against malformed messages sent by drivers;
2058 	 * this is in keeping with the rest of IP's dlpi handling.
2059 	 * (Remember, it's coming from something else in the kernel
2060 	 * address space)
2061 	 */
2062 
2063 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2064 	if (capend > mp->b_wptr) {
2065 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2066 		    "malformed sub-capability too long for mblk");
2067 		return;
2068 	}
2069 
2070 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
2071 
2072 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
2073 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
2074 		    "unsupported MDT sub-capability (version %d, expected %d)",
2075 		    mdt_ic->mdt_version, MDT_VERSION_2);
2076 		return;
2077 	}
2078 
2079 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
2080 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
2081 		    "capability isn't as expected; pass-thru module(s) "
2082 		    "detected, discarding capability\n"));
2083 		return;
2084 	}
2085 
2086 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
2087 
2088 		if (*ill_mdt_capab == NULL) {
2089 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
2090 			    KM_NOSLEEP);
2091 
2092 			if (*ill_mdt_capab == NULL) {
2093 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2094 				    "could not enable MDT version %d "
2095 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
2096 				    ill->ill_name);
2097 				return;
2098 			}
2099 		}
2100 
2101 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
2102 		    "MDT version %d (%d bytes leading, %d bytes trailing "
2103 		    "header spaces, %d max pld bufs, %d span limit)\n",
2104 		    ill->ill_name, MDT_VERSION_2,
2105 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
2106 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
2107 
2108 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
2109 		(*ill_mdt_capab)->ill_mdt_on = 1;
2110 		/*
2111 		 * Round the following values to the nearest 32-bit; ULP
2112 		 * may further adjust them to accomodate for additional
2113 		 * protocol headers.  We pass these values to ULP during
2114 		 * bind time.
2115 		 */
2116 		(*ill_mdt_capab)->ill_mdt_hdr_head =
2117 		    roundup(mdt_ic->mdt_hdr_head, 4);
2118 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
2119 		    roundup(mdt_ic->mdt_hdr_tail, 4);
2120 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
2121 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
2122 
2123 		ill->ill_capabilities |= ILL_CAPAB_MDT;
2124 	} else {
2125 		uint_t size;
2126 		uchar_t *rptr;
2127 
2128 		size = sizeof (dl_capability_req_t) +
2129 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
2130 
2131 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2132 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2133 			    "could not enable MDT for %s (ENOMEM)\n",
2134 			    ill->ill_name);
2135 			return;
2136 		}
2137 
2138 		rptr = nmp->b_rptr;
2139 		/* initialize dl_capability_req_t */
2140 		oc = (dl_capability_req_t *)nmp->b_rptr;
2141 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2142 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2143 		    sizeof (dl_capab_mdt_t);
2144 		nmp->b_rptr += sizeof (dl_capability_req_t);
2145 
2146 		/* initialize dl_capability_sub_t */
2147 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2148 		nmp->b_rptr += sizeof (*isub);
2149 
2150 		/* initialize dl_capab_mdt_t */
2151 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2152 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2153 
2154 		nmp->b_rptr = rptr;
2155 
2156 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2157 		    "to enable MDT version %d\n", ill->ill_name,
2158 		    MDT_VERSION_2));
2159 
2160 		/* set ENABLE flag */
2161 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2162 
2163 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2164 		ill_dlpi_send(ill, nmp);
2165 	}
2166 }
2167 
2168 static void
2169 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2170 {
2171 	mblk_t *mp;
2172 	dl_capab_mdt_t *mdt_subcap;
2173 	dl_capability_sub_t *dl_subcap;
2174 	int size;
2175 
2176 	if (!ILL_MDT_CAPABLE(ill))
2177 		return;
2178 
2179 	ASSERT(ill->ill_mdt_capab != NULL);
2180 	/*
2181 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2182 	 * structure since it's possible that another thread is still
2183 	 * referring to it.  The structure only gets deallocated when
2184 	 * we destroy the ill.
2185 	 */
2186 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2187 
2188 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2189 
2190 	mp = allocb(size, BPRI_HI);
2191 	if (mp == NULL) {
2192 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2193 		    "request to disable MDT\n"));
2194 		return;
2195 	}
2196 
2197 	mp->b_wptr = mp->b_rptr + size;
2198 
2199 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2200 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2201 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2202 
2203 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2204 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2205 	mdt_subcap->mdt_flags = 0;
2206 	mdt_subcap->mdt_hdr_head = 0;
2207 	mdt_subcap->mdt_hdr_tail = 0;
2208 
2209 	if (*sc_mp != NULL)
2210 		linkb(*sc_mp, mp);
2211 	else
2212 		*sc_mp = mp;
2213 }
2214 
2215 /*
2216  * Send a DL_NOTIFY_REQ to the specified ill to enable
2217  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2218  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2219  * acceleration.
2220  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2221  */
2222 static boolean_t
2223 ill_enable_promisc_notify(ill_t *ill)
2224 {
2225 	mblk_t *mp;
2226 	dl_notify_req_t *req;
2227 
2228 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2229 
2230 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2231 	if (mp == NULL)
2232 		return (B_FALSE);
2233 
2234 	req = (dl_notify_req_t *)mp->b_rptr;
2235 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2236 	    DL_NOTE_PROMISC_OFF_PHYS;
2237 
2238 	ill_dlpi_send(ill, mp);
2239 
2240 	return (B_TRUE);
2241 }
2242 
2243 
2244 /*
2245  * Allocate an IPsec capability request which will be filled by our
2246  * caller to turn on support for one or more algorithms.
2247  */
2248 static mblk_t *
2249 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2250 {
2251 	mblk_t *nmp;
2252 	dl_capability_req_t	*ocap;
2253 	dl_capab_ipsec_t	*ocip;
2254 	dl_capab_ipsec_t	*icip;
2255 	uint8_t			*ptr;
2256 	icip = (dl_capab_ipsec_t *)(isub + 1);
2257 
2258 	/*
2259 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2260 	 * PROMISC_ON/OFF notification from the provider. We need to
2261 	 * do this before enabling the algorithms to avoid leakage of
2262 	 * cleartext packets.
2263 	 */
2264 
2265 	if (!ill_enable_promisc_notify(ill))
2266 		return (NULL);
2267 
2268 	/*
2269 	 * Allocate new mblk which will contain a new capability
2270 	 * request to enable the capabilities.
2271 	 */
2272 
2273 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2274 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2275 	if (nmp == NULL)
2276 		return (NULL);
2277 
2278 	ptr = nmp->b_rptr;
2279 
2280 	/* initialize dl_capability_req_t */
2281 	ocap = (dl_capability_req_t *)ptr;
2282 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2283 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2284 	ptr += sizeof (dl_capability_req_t);
2285 
2286 	/* initialize dl_capability_sub_t */
2287 	bcopy(isub, ptr, sizeof (*isub));
2288 	ptr += sizeof (*isub);
2289 
2290 	/* initialize dl_capab_ipsec_t */
2291 	ocip = (dl_capab_ipsec_t *)ptr;
2292 	bcopy(icip, ocip, sizeof (*icip));
2293 
2294 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2295 	return (nmp);
2296 }
2297 
2298 /*
2299  * Process an IPsec capability negotiation ack received from a DLS Provider.
2300  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2301  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2302  */
2303 static void
2304 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2305 {
2306 	dl_capab_ipsec_t	*icip;
2307 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2308 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2309 	uint_t cipher, nciphers;
2310 	mblk_t *nmp;
2311 	uint_t alg_len;
2312 	boolean_t need_sadb_dump;
2313 	uint_t sub_dl_cap = isub->dl_cap;
2314 	ill_ipsec_capab_t **ill_capab;
2315 	uint64_t ill_capab_flag;
2316 	uint8_t *capend, *ciphend;
2317 	boolean_t sadb_resync;
2318 
2319 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2320 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2321 
2322 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2323 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2324 		ill_capab_flag = ILL_CAPAB_AH;
2325 	} else {
2326 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2327 		ill_capab_flag = ILL_CAPAB_ESP;
2328 	}
2329 
2330 	/*
2331 	 * If the ill capability structure exists, then this incoming
2332 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2333 	 * If this is so, then we'd need to resynchronize the SADB
2334 	 * after re-enabling the offloaded ciphers.
2335 	 */
2336 	sadb_resync = (*ill_capab != NULL);
2337 
2338 	/*
2339 	 * Note: range checks here are not absolutely sufficient to
2340 	 * make us robust against malformed messages sent by drivers;
2341 	 * this is in keeping with the rest of IP's dlpi handling.
2342 	 * (Remember, it's coming from something else in the kernel
2343 	 * address space)
2344 	 */
2345 
2346 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2347 	if (capend > mp->b_wptr) {
2348 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2349 		    "malformed sub-capability too long for mblk");
2350 		return;
2351 	}
2352 
2353 	/*
2354 	 * There are two types of acks we process here:
2355 	 * 1. acks in reply to a (first form) generic capability req
2356 	 *    (no ENABLE flag set)
2357 	 * 2. acks in reply to a ENABLE capability req.
2358 	 *    (ENABLE flag set)
2359 	 *
2360 	 * We process the subcapability passed as argument as follows:
2361 	 * 1 do initializations
2362 	 *   1.1 initialize nmp = NULL
2363 	 *   1.2 set need_sadb_dump to B_FALSE
2364 	 * 2 for each cipher in subcapability:
2365 	 *   2.1 if ENABLE flag is set:
2366 	 *	2.1.1 update per-ill ipsec capabilities info
2367 	 *	2.1.2 set need_sadb_dump to B_TRUE
2368 	 *   2.2 if ENABLE flag is not set:
2369 	 *	2.2.1 if nmp is NULL:
2370 	 *		2.2.1.1 allocate and initialize nmp
2371 	 *		2.2.1.2 init current pos in nmp
2372 	 *	2.2.2 copy current cipher to current pos in nmp
2373 	 *	2.2.3 set ENABLE flag in nmp
2374 	 *	2.2.4 update current pos
2375 	 * 3 if nmp is not equal to NULL, send enable request
2376 	 *   3.1 send capability request
2377 	 * 4 if need_sadb_dump is B_TRUE
2378 	 *   4.1 enable promiscuous on/off notifications
2379 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2380 	 *	AH or ESP SA's to interface.
2381 	 */
2382 
2383 	nmp = NULL;
2384 	oalg = NULL;
2385 	need_sadb_dump = B_FALSE;
2386 	icip = (dl_capab_ipsec_t *)(isub + 1);
2387 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2388 
2389 	nciphers = icip->cip_nciphers;
2390 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2391 
2392 	if (ciphend > capend) {
2393 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2394 		    "too many ciphers for sub-capability len");
2395 		return;
2396 	}
2397 
2398 	for (cipher = 0; cipher < nciphers; cipher++) {
2399 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2400 
2401 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2402 			/*
2403 			 * TBD: when we provide a way to disable capabilities
2404 			 * from above, need to manage the request-pending state
2405 			 * and fail if we were not expecting this ACK.
2406 			 */
2407 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2408 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2409 
2410 			/*
2411 			 * Update IPsec capabilities for this ill
2412 			 */
2413 
2414 			if (*ill_capab == NULL) {
2415 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2416 				    ("ill_capability_ipsec_ack: "
2417 					"allocating ipsec_capab for ill\n"));
2418 				*ill_capab = ill_ipsec_capab_alloc();
2419 
2420 				if (*ill_capab == NULL) {
2421 					cmn_err(CE_WARN,
2422 					    "ill_capability_ipsec_ack: "
2423 					    "could not enable IPsec Hardware "
2424 					    "acceleration for %s (ENOMEM)\n",
2425 					    ill->ill_name);
2426 					return;
2427 				}
2428 			}
2429 
2430 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2431 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2432 
2433 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2434 				cmn_err(CE_WARN,
2435 				    "ill_capability_ipsec_ack: "
2436 				    "malformed IPsec algorithm id %d",
2437 				    ialg->alg_prim);
2438 				continue;
2439 			}
2440 
2441 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2442 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2443 				    ialg->alg_prim);
2444 			} else {
2445 				ipsec_capab_algparm_t *alp;
2446 
2447 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2448 				    ialg->alg_prim);
2449 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2450 				    ialg->alg_prim)) {
2451 					cmn_err(CE_WARN,
2452 					    "ill_capability_ipsec_ack: "
2453 					    "no space for IPsec alg id %d",
2454 					    ialg->alg_prim);
2455 					continue;
2456 				}
2457 				alp = &((*ill_capab)->encr_algparm[
2458 						ialg->alg_prim]);
2459 				alp->minkeylen = ialg->alg_minbits;
2460 				alp->maxkeylen = ialg->alg_maxbits;
2461 			}
2462 			ill->ill_capabilities |= ill_capab_flag;
2463 			/*
2464 			 * indicate that a capability was enabled, which
2465 			 * will be used below to kick off a SADB dump
2466 			 * to the ill.
2467 			 */
2468 			need_sadb_dump = B_TRUE;
2469 		} else {
2470 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2471 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2472 				ialg->alg_prim));
2473 
2474 			if (nmp == NULL) {
2475 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2476 				if (nmp == NULL) {
2477 					/*
2478 					 * Sending the PROMISC_ON/OFF
2479 					 * notification request failed.
2480 					 * We cannot enable the algorithms
2481 					 * since the Provider will not
2482 					 * notify IP of promiscous mode
2483 					 * changes, which could lead
2484 					 * to leakage of packets.
2485 					 */
2486 					cmn_err(CE_WARN,
2487 					    "ill_capability_ipsec_ack: "
2488 					    "could not enable IPsec Hardware "
2489 					    "acceleration for %s (ENOMEM)\n",
2490 					    ill->ill_name);
2491 					return;
2492 				}
2493 				/* ptr to current output alg specifier */
2494 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2495 			}
2496 
2497 			/*
2498 			 * Copy current alg specifier, set ENABLE
2499 			 * flag, and advance to next output alg.
2500 			 * For now we enable all IPsec capabilities.
2501 			 */
2502 			ASSERT(oalg != NULL);
2503 			bcopy(ialg, oalg, alg_len);
2504 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2505 			nmp->b_wptr += alg_len;
2506 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2507 		}
2508 
2509 		/* move to next input algorithm specifier */
2510 		ialg = (dl_capab_ipsec_alg_t *)
2511 		    ((char *)ialg + alg_len);
2512 	}
2513 
2514 	if (nmp != NULL)
2515 		/*
2516 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2517 		 * IPsec hardware acceleration.
2518 		 */
2519 		ill_dlpi_send(ill, nmp);
2520 
2521 	if (need_sadb_dump)
2522 		/*
2523 		 * An acknowledgement corresponding to a request to
2524 		 * enable acceleration was received, notify SADB.
2525 		 */
2526 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2527 }
2528 
2529 /*
2530  * Given an mblk with enough space in it, create sub-capability entries for
2531  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2532  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2533  * in preparation for the reset the DL_CAPABILITY_REQ message.
2534  */
2535 static void
2536 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2537     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2538 {
2539 	dl_capab_ipsec_t *oipsec;
2540 	dl_capab_ipsec_alg_t *oalg;
2541 	dl_capability_sub_t *dl_subcap;
2542 	int i, k;
2543 
2544 	ASSERT(nciphers > 0);
2545 	ASSERT(ill_cap != NULL);
2546 	ASSERT(mp != NULL);
2547 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2548 
2549 	/* dl_capability_sub_t for "stype" */
2550 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2551 	dl_subcap->dl_cap = stype;
2552 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2553 	mp->b_wptr += sizeof (dl_capability_sub_t);
2554 
2555 	/* dl_capab_ipsec_t for "stype" */
2556 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2557 	oipsec->cip_version = 1;
2558 	oipsec->cip_nciphers = nciphers;
2559 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2560 
2561 	/* create entries for "stype" AUTH ciphers */
2562 	for (i = 0; i < ill_cap->algs_size; i++) {
2563 		for (k = 0; k < BITSPERBYTE; k++) {
2564 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2565 				continue;
2566 
2567 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2568 			bzero((void *)oalg, sizeof (*oalg));
2569 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2570 			oalg->alg_prim = k + (BITSPERBYTE * i);
2571 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2572 		}
2573 	}
2574 	/* create entries for "stype" ENCR ciphers */
2575 	for (i = 0; i < ill_cap->algs_size; i++) {
2576 		for (k = 0; k < BITSPERBYTE; k++) {
2577 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2578 				continue;
2579 
2580 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2581 			bzero((void *)oalg, sizeof (*oalg));
2582 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2583 			oalg->alg_prim = k + (BITSPERBYTE * i);
2584 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2585 		}
2586 	}
2587 }
2588 
2589 /*
2590  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2591  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2592  * POPC instruction, but our macro is more flexible for an arbitrary length
2593  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2594  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2595  * stays that way, we can reduce the number of iterations required.
2596  */
2597 #define	COUNT_1S(val, sum) {					\
2598 	uint8_t x = val & 0xff;					\
2599 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2600 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2601 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2602 }
2603 
2604 /* ARGSUSED */
2605 static void
2606 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2607 {
2608 	mblk_t *mp;
2609 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2610 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2611 	uint64_t ill_capabilities = ill->ill_capabilities;
2612 	int ah_cnt = 0, esp_cnt = 0;
2613 	int ah_len = 0, esp_len = 0;
2614 	int i, size = 0;
2615 
2616 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2617 		return;
2618 
2619 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2620 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2621 
2622 	/* Find out the number of ciphers for AH */
2623 	if (cap_ah != NULL) {
2624 		for (i = 0; i < cap_ah->algs_size; i++) {
2625 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2626 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2627 		}
2628 		if (ah_cnt > 0) {
2629 			size += sizeof (dl_capability_sub_t) +
2630 			    sizeof (dl_capab_ipsec_t);
2631 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2632 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2633 			size += ah_len;
2634 		}
2635 	}
2636 
2637 	/* Find out the number of ciphers for ESP */
2638 	if (cap_esp != NULL) {
2639 		for (i = 0; i < cap_esp->algs_size; i++) {
2640 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2641 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2642 		}
2643 		if (esp_cnt > 0) {
2644 			size += sizeof (dl_capability_sub_t) +
2645 			    sizeof (dl_capab_ipsec_t);
2646 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2647 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2648 			size += esp_len;
2649 		}
2650 	}
2651 
2652 	if (size == 0) {
2653 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2654 		    "there's nothing to reset\n"));
2655 		return;
2656 	}
2657 
2658 	mp = allocb(size, BPRI_HI);
2659 	if (mp == NULL) {
2660 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2661 		    "request to disable IPSEC Hardware Acceleration\n"));
2662 		return;
2663 	}
2664 
2665 	/*
2666 	 * Clear the capability flags for IPSec HA but retain the ill
2667 	 * capability structures since it's possible that another thread
2668 	 * is still referring to them.  The structures only get deallocated
2669 	 * when we destroy the ill.
2670 	 *
2671 	 * Various places check the flags to see if the ill is capable of
2672 	 * hardware acceleration, and by clearing them we ensure that new
2673 	 * outbound IPSec packets are sent down encrypted.
2674 	 */
2675 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2676 
2677 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2678 	if (ah_cnt > 0) {
2679 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2680 		    cap_ah, mp);
2681 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2682 	}
2683 
2684 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2685 	if (esp_cnt > 0) {
2686 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2687 		    cap_esp, mp);
2688 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2689 	}
2690 
2691 	/*
2692 	 * At this point we've composed a bunch of sub-capabilities to be
2693 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2694 	 * by the caller.  Upon receiving this reset message, the driver
2695 	 * must stop inbound decryption (by destroying all inbound SAs)
2696 	 * and let the corresponding packets come in encrypted.
2697 	 */
2698 
2699 	if (*sc_mp != NULL)
2700 		linkb(*sc_mp, mp);
2701 	else
2702 		*sc_mp = mp;
2703 }
2704 
2705 static void
2706 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2707     boolean_t encapsulated)
2708 {
2709 	boolean_t legacy = B_FALSE;
2710 
2711 	/*
2712 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2713 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2714 	 * instructed the driver to disable its advertised capabilities,
2715 	 * so there's no point in accepting any response at this moment.
2716 	 */
2717 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2718 		return;
2719 
2720 	/*
2721 	 * Note that only the following two sub-capabilities may be
2722 	 * considered as "legacy", since their original definitions
2723 	 * do not incorporate the dl_mid_t module ID token, and hence
2724 	 * may require the use of the wrapper sub-capability.
2725 	 */
2726 	switch (subp->dl_cap) {
2727 	case DL_CAPAB_IPSEC_AH:
2728 	case DL_CAPAB_IPSEC_ESP:
2729 		legacy = B_TRUE;
2730 		break;
2731 	}
2732 
2733 	/*
2734 	 * For legacy sub-capabilities which don't incorporate a queue_t
2735 	 * pointer in their structures, discard them if we detect that
2736 	 * there are intermediate modules in between IP and the driver.
2737 	 */
2738 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2739 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2740 		    "%d discarded; %d module(s) present below IP\n",
2741 		    subp->dl_cap, ill->ill_lmod_cnt));
2742 		return;
2743 	}
2744 
2745 	switch (subp->dl_cap) {
2746 	case DL_CAPAB_IPSEC_AH:
2747 	case DL_CAPAB_IPSEC_ESP:
2748 		ill_capability_ipsec_ack(ill, mp, subp);
2749 		break;
2750 	case DL_CAPAB_MDT:
2751 		ill_capability_mdt_ack(ill, mp, subp);
2752 		break;
2753 	case DL_CAPAB_HCKSUM:
2754 		ill_capability_hcksum_ack(ill, mp, subp);
2755 		break;
2756 	case DL_CAPAB_ZEROCOPY:
2757 		ill_capability_zerocopy_ack(ill, mp, subp);
2758 		break;
2759 	case DL_CAPAB_POLL:
2760 		if (!SOFT_RINGS_ENABLED())
2761 			ill_capability_dls_ack(ill, mp, subp);
2762 		break;
2763 	case DL_CAPAB_SOFT_RING:
2764 		if (SOFT_RINGS_ENABLED())
2765 			ill_capability_dls_ack(ill, mp, subp);
2766 		break;
2767 	case DL_CAPAB_LSO:
2768 		ill_capability_lso_ack(ill, mp, subp);
2769 		break;
2770 	default:
2771 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2772 		    subp->dl_cap));
2773 	}
2774 }
2775 
2776 /*
2777  * As part of negotiating polling capability, the driver tells us
2778  * the default (or normal) blanking interval and packet threshold
2779  * (the receive timer fires if blanking interval is reached or
2780  * the packet threshold is reached).
2781  *
2782  * As part of manipulating the polling interval, we always use our
2783  * estimated interval (avg service time * number of packets queued
2784  * on the squeue) but we try to blank for a minimum of
2785  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2786  * packet threshold during this time. When we are not in polling mode
2787  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2788  * rr_min_blank_ratio but up the packet cnt by a ratio of
2789  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2790  * possible although for a shorter interval.
2791  */
2792 #define	RR_MAX_BLANK_RATIO	20
2793 #define	RR_MIN_BLANK_RATIO	10
2794 #define	RR_MAX_PKT_CNT_RATIO	3
2795 #define	RR_MIN_PKT_CNT_RATIO	3
2796 
2797 /*
2798  * These can be tuned via /etc/system.
2799  */
2800 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2801 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2802 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2803 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2804 
2805 static mac_resource_handle_t
2806 ill_ring_add(void *arg, mac_resource_t *mrp)
2807 {
2808 	ill_t			*ill = (ill_t *)arg;
2809 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2810 	ill_rx_ring_t		*rx_ring;
2811 	int			ip_rx_index;
2812 
2813 	ASSERT(mrp != NULL);
2814 	if (mrp->mr_type != MAC_RX_FIFO) {
2815 		return (NULL);
2816 	}
2817 	ASSERT(ill != NULL);
2818 	ASSERT(ill->ill_dls_capab != NULL);
2819 
2820 	mutex_enter(&ill->ill_lock);
2821 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2822 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2823 		ASSERT(rx_ring != NULL);
2824 
2825 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2826 			time_t normal_blank_time =
2827 			    mrfp->mrf_normal_blank_time;
2828 			uint_t normal_pkt_cnt =
2829 			    mrfp->mrf_normal_pkt_count;
2830 
2831 			bzero(rx_ring, sizeof (ill_rx_ring_t));
2832 
2833 			rx_ring->rr_blank = mrfp->mrf_blank;
2834 			rx_ring->rr_handle = mrfp->mrf_arg;
2835 			rx_ring->rr_ill = ill;
2836 			rx_ring->rr_normal_blank_time = normal_blank_time;
2837 			rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2838 
2839 			rx_ring->rr_max_blank_time =
2840 			    normal_blank_time * rr_max_blank_ratio;
2841 			rx_ring->rr_min_blank_time =
2842 			    normal_blank_time * rr_min_blank_ratio;
2843 			rx_ring->rr_max_pkt_cnt =
2844 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2845 			rx_ring->rr_min_pkt_cnt =
2846 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2847 
2848 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2849 			mutex_exit(&ill->ill_lock);
2850 
2851 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2852 			    (int), ip_rx_index);
2853 			return ((mac_resource_handle_t)rx_ring);
2854 		}
2855 	}
2856 
2857 	/*
2858 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2859 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2860 	 * should be made configurable. Meanwhile it cause no panic because
2861 	 * driver will pass ip_input a NULL handle which will make
2862 	 * IP allocate the default squeue and Polling mode will not
2863 	 * be used for this ring.
2864 	 */
2865 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2866 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2867 
2868 	mutex_exit(&ill->ill_lock);
2869 	return (NULL);
2870 }
2871 
2872 static boolean_t
2873 ill_capability_dls_init(ill_t *ill)
2874 {
2875 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2876 	conn_t 			*connp;
2877 	size_t			sz;
2878 
2879 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2880 		if (ill_dls == NULL) {
2881 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2882 			    "soft_ring enabled for ill=%s (%p) but data "
2883 			    "structs uninitialized\n", ill->ill_name,
2884 			    (void *)ill);
2885 		}
2886 		return (B_TRUE);
2887 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2888 		if (ill_dls == NULL) {
2889 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2890 			    "polling enabled for ill=%s (%p) but data "
2891 			    "structs uninitialized\n", ill->ill_name,
2892 			(void *)ill);
2893 		}
2894 		return (B_TRUE);
2895 	}
2896 
2897 	if (ill_dls != NULL) {
2898 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2899 		/* Soft_Ring or polling is being re-enabled */
2900 
2901 		connp = ill_dls->ill_unbind_conn;
2902 		ASSERT(rx_ring != NULL);
2903 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2904 		bzero((void *)rx_ring,
2905 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2906 		ill_dls->ill_ring_tbl = rx_ring;
2907 		ill_dls->ill_unbind_conn = connp;
2908 		return (B_TRUE);
2909 	}
2910 
2911 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP)) == NULL)
2912 		return (B_FALSE);
2913 
2914 	sz = sizeof (ill_dls_capab_t);
2915 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2916 
2917 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2918 	if (ill_dls == NULL) {
2919 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2920 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2921 		    (void *)ill);
2922 		CONN_DEC_REF(connp);
2923 		return (B_FALSE);
2924 	}
2925 
2926 	/* Allocate space to hold ring table */
2927 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2928 	ill->ill_dls_capab = ill_dls;
2929 	ill_dls->ill_unbind_conn = connp;
2930 	return (B_TRUE);
2931 }
2932 
2933 /*
2934  * ill_capability_dls_disable: disable soft_ring and/or polling
2935  * capability. Since any of the rings might already be in use, need
2936  * to call ipsq_clean_all() which gets behind the squeue to disable
2937  * direct calls if necessary.
2938  */
2939 static void
2940 ill_capability_dls_disable(ill_t *ill)
2941 {
2942 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2943 
2944 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2945 		ipsq_clean_all(ill);
2946 		ill_dls->ill_tx = NULL;
2947 		ill_dls->ill_tx_handle = NULL;
2948 		ill_dls->ill_dls_change_status = NULL;
2949 		ill_dls->ill_dls_bind = NULL;
2950 		ill_dls->ill_dls_unbind = NULL;
2951 	}
2952 
2953 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2954 }
2955 
2956 static void
2957 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2958     dl_capability_sub_t *isub)
2959 {
2960 	uint_t			size;
2961 	uchar_t			*rptr;
2962 	dl_capab_dls_t	dls, *odls;
2963 	ill_dls_capab_t	*ill_dls;
2964 	mblk_t			*nmp = NULL;
2965 	dl_capability_req_t	*ocap;
2966 	uint_t			sub_dl_cap = isub->dl_cap;
2967 
2968 	if (!ill_capability_dls_init(ill))
2969 		return;
2970 	ill_dls = ill->ill_dls_capab;
2971 
2972 	/* Copy locally to get the members aligned */
2973 	bcopy((void *)idls, (void *)&dls,
2974 	    sizeof (dl_capab_dls_t));
2975 
2976 	/* Get the tx function and handle from dld */
2977 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2978 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2979 
2980 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2981 		ill_dls->ill_dls_change_status =
2982 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2983 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2984 		ill_dls->ill_dls_unbind =
2985 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2986 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2987 	}
2988 
2989 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2990 	    isub->dl_length;
2991 
2992 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2993 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2994 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2995 		    ill->ill_name, (void *)ill);
2996 		return;
2997 	}
2998 
2999 	/* initialize dl_capability_req_t */
3000 	rptr = nmp->b_rptr;
3001 	ocap = (dl_capability_req_t *)rptr;
3002 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
3003 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
3004 	rptr += sizeof (dl_capability_req_t);
3005 
3006 	/* initialize dl_capability_sub_t */
3007 	bcopy(isub, rptr, sizeof (*isub));
3008 	rptr += sizeof (*isub);
3009 
3010 	odls = (dl_capab_dls_t *)rptr;
3011 	rptr += sizeof (dl_capab_dls_t);
3012 
3013 	/* initialize dl_capab_dls_t to be sent down */
3014 	dls.dls_rx_handle = (uintptr_t)ill;
3015 	dls.dls_rx = (uintptr_t)ip_input;
3016 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
3017 
3018 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
3019 		dls.dls_ring_cnt = ip_soft_rings_cnt;
3020 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
3021 		dls.dls_flags = SOFT_RING_ENABLE;
3022 	} else {
3023 		dls.dls_flags = POLL_ENABLE;
3024 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
3025 		    "to enable polling\n", ill->ill_name));
3026 	}
3027 	bcopy((void *)&dls, (void *)odls,
3028 	    sizeof (dl_capab_dls_t));
3029 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3030 	/*
3031 	 * nmp points to a DL_CAPABILITY_REQ message to
3032 	 * enable either soft_ring or polling
3033 	 */
3034 	ill_dlpi_send(ill, nmp);
3035 }
3036 
3037 static void
3038 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
3039 {
3040 	mblk_t *mp;
3041 	dl_capab_dls_t *idls;
3042 	dl_capability_sub_t *dl_subcap;
3043 	int size;
3044 
3045 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
3046 		return;
3047 
3048 	ASSERT(ill->ill_dls_capab != NULL);
3049 
3050 	size = sizeof (*dl_subcap) + sizeof (*idls);
3051 
3052 	mp = allocb(size, BPRI_HI);
3053 	if (mp == NULL) {
3054 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
3055 		    "request to disable soft_ring\n"));
3056 		return;
3057 	}
3058 
3059 	mp->b_wptr = mp->b_rptr + size;
3060 
3061 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3062 	dl_subcap->dl_length = sizeof (*idls);
3063 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
3064 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
3065 	else
3066 		dl_subcap->dl_cap = DL_CAPAB_POLL;
3067 
3068 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
3069 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
3070 		idls->dls_flags = SOFT_RING_DISABLE;
3071 	else
3072 		idls->dls_flags = POLL_DISABLE;
3073 
3074 	if (*sc_mp != NULL)
3075 		linkb(*sc_mp, mp);
3076 	else
3077 		*sc_mp = mp;
3078 }
3079 
3080 /*
3081  * Process a soft_ring/poll capability negotiation ack received
3082  * from a DLS Provider.isub must point to the sub-capability
3083  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
3084  */
3085 static void
3086 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3087 {
3088 	dl_capab_dls_t		*idls;
3089 	uint_t			sub_dl_cap = isub->dl_cap;
3090 	uint8_t			*capend;
3091 
3092 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
3093 	    sub_dl_cap == DL_CAPAB_POLL);
3094 
3095 	if (ill->ill_isv6)
3096 		return;
3097 
3098 	/*
3099 	 * Note: range checks here are not absolutely sufficient to
3100 	 * make us robust against malformed messages sent by drivers;
3101 	 * this is in keeping with the rest of IP's dlpi handling.
3102 	 * (Remember, it's coming from something else in the kernel
3103 	 * address space)
3104 	 */
3105 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3106 	if (capend > mp->b_wptr) {
3107 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
3108 		    "malformed sub-capability too long for mblk");
3109 		return;
3110 	}
3111 
3112 	/*
3113 	 * There are two types of acks we process here:
3114 	 * 1. acks in reply to a (first form) generic capability req
3115 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
3116 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
3117 	 *    capability req.
3118 	 */
3119 	idls = (dl_capab_dls_t *)(isub + 1);
3120 
3121 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
3122 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
3123 		    "capability isn't as expected; pass-thru "
3124 		    "module(s) detected, discarding capability\n"));
3125 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
3126 			/*
3127 			 * This is a capability renegotitation case.
3128 			 * The interface better be unusable at this
3129 			 * point other wise bad things will happen
3130 			 * if we disable direct calls on a running
3131 			 * and up interface.
3132 			 */
3133 			ill_capability_dls_disable(ill);
3134 		}
3135 		return;
3136 	}
3137 
3138 	switch (idls->dls_flags) {
3139 	default:
3140 		/* Disable if unknown flag */
3141 	case SOFT_RING_DISABLE:
3142 	case POLL_DISABLE:
3143 		ill_capability_dls_disable(ill);
3144 		break;
3145 	case SOFT_RING_CAPABLE:
3146 	case POLL_CAPABLE:
3147 		/*
3148 		 * If the capability was already enabled, its safe
3149 		 * to disable it first to get rid of stale information
3150 		 * and then start enabling it again.
3151 		 */
3152 		ill_capability_dls_disable(ill);
3153 		ill_capability_dls_capable(ill, idls, isub);
3154 		break;
3155 	case SOFT_RING_ENABLE:
3156 	case POLL_ENABLE:
3157 		mutex_enter(&ill->ill_lock);
3158 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3159 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3160 			ASSERT(ill->ill_dls_capab != NULL);
3161 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3162 		}
3163 		if (sub_dl_cap == DL_CAPAB_POLL &&
3164 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3165 			ASSERT(ill->ill_dls_capab != NULL);
3166 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3167 			ip1dbg(("ill_capability_dls_ack: interface %s "
3168 			    "has enabled polling\n", ill->ill_name));
3169 		}
3170 		mutex_exit(&ill->ill_lock);
3171 		break;
3172 	}
3173 }
3174 
3175 /*
3176  * Process a hardware checksum offload capability negotiation ack received
3177  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3178  * of a DL_CAPABILITY_ACK message.
3179  */
3180 static void
3181 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3182 {
3183 	dl_capability_req_t	*ocap;
3184 	dl_capab_hcksum_t	*ihck, *ohck;
3185 	ill_hcksum_capab_t	**ill_hcksum;
3186 	mblk_t			*nmp = NULL;
3187 	uint_t			sub_dl_cap = isub->dl_cap;
3188 	uint8_t			*capend;
3189 
3190 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3191 
3192 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3193 
3194 	/*
3195 	 * Note: range checks here are not absolutely sufficient to
3196 	 * make us robust against malformed messages sent by drivers;
3197 	 * this is in keeping with the rest of IP's dlpi handling.
3198 	 * (Remember, it's coming from something else in the kernel
3199 	 * address space)
3200 	 */
3201 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3202 	if (capend > mp->b_wptr) {
3203 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3204 		    "malformed sub-capability too long for mblk");
3205 		return;
3206 	}
3207 
3208 	/*
3209 	 * There are two types of acks we process here:
3210 	 * 1. acks in reply to a (first form) generic capability req
3211 	 *    (no ENABLE flag set)
3212 	 * 2. acks in reply to a ENABLE capability req.
3213 	 *    (ENABLE flag set)
3214 	 */
3215 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3216 
3217 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3218 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3219 		    "unsupported hardware checksum "
3220 		    "sub-capability (version %d, expected %d)",
3221 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3222 		return;
3223 	}
3224 
3225 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3226 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3227 		    "checksum capability isn't as expected; pass-thru "
3228 		    "module(s) detected, discarding capability\n"));
3229 		return;
3230 	}
3231 
3232 #define	CURR_HCKSUM_CAPAB				\
3233 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3234 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3235 
3236 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3237 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3238 		/* do ENABLE processing */
3239 		if (*ill_hcksum == NULL) {
3240 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3241 			    KM_NOSLEEP);
3242 
3243 			if (*ill_hcksum == NULL) {
3244 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3245 				    "could not enable hcksum version %d "
3246 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3247 				    ill->ill_name);
3248 				return;
3249 			}
3250 		}
3251 
3252 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3253 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3254 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3255 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3256 		    "has enabled hardware checksumming\n ",
3257 		    ill->ill_name));
3258 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3259 		/*
3260 		 * Enabling hardware checksum offload
3261 		 * Currently IP supports {TCP,UDP}/IPv4
3262 		 * partial and full cksum offload and
3263 		 * IPv4 header checksum offload.
3264 		 * Allocate new mblk which will
3265 		 * contain a new capability request
3266 		 * to enable hardware checksum offload.
3267 		 */
3268 		uint_t	size;
3269 		uchar_t	*rptr;
3270 
3271 		size = sizeof (dl_capability_req_t) +
3272 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3273 
3274 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3275 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3276 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3277 			    ill->ill_name);
3278 			return;
3279 		}
3280 
3281 		rptr = nmp->b_rptr;
3282 		/* initialize dl_capability_req_t */
3283 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3284 		ocap->dl_sub_offset =
3285 		    sizeof (dl_capability_req_t);
3286 		ocap->dl_sub_length =
3287 		    sizeof (dl_capability_sub_t) +
3288 		    isub->dl_length;
3289 		nmp->b_rptr += sizeof (dl_capability_req_t);
3290 
3291 		/* initialize dl_capability_sub_t */
3292 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3293 		nmp->b_rptr += sizeof (*isub);
3294 
3295 		/* initialize dl_capab_hcksum_t */
3296 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3297 		bcopy(ihck, ohck, sizeof (*ihck));
3298 
3299 		nmp->b_rptr = rptr;
3300 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3301 
3302 		/* Set ENABLE flag */
3303 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3304 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3305 
3306 		/*
3307 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3308 		 * hardware checksum acceleration.
3309 		 */
3310 		ill_dlpi_send(ill, nmp);
3311 	} else {
3312 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3313 		    "advertised %x hardware checksum capability flags\n",
3314 		    ill->ill_name, ihck->hcksum_txflags));
3315 	}
3316 }
3317 
3318 static void
3319 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3320 {
3321 	mblk_t *mp;
3322 	dl_capab_hcksum_t *hck_subcap;
3323 	dl_capability_sub_t *dl_subcap;
3324 	int size;
3325 
3326 	if (!ILL_HCKSUM_CAPABLE(ill))
3327 		return;
3328 
3329 	ASSERT(ill->ill_hcksum_capab != NULL);
3330 	/*
3331 	 * Clear the capability flag for hardware checksum offload but
3332 	 * retain the ill_hcksum_capab structure since it's possible that
3333 	 * another thread is still referring to it.  The structure only
3334 	 * gets deallocated when we destroy the ill.
3335 	 */
3336 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3337 
3338 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3339 
3340 	mp = allocb(size, BPRI_HI);
3341 	if (mp == NULL) {
3342 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3343 		    "request to disable hardware checksum offload\n"));
3344 		return;
3345 	}
3346 
3347 	mp->b_wptr = mp->b_rptr + size;
3348 
3349 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3350 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3351 	dl_subcap->dl_length = sizeof (*hck_subcap);
3352 
3353 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3354 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3355 	hck_subcap->hcksum_txflags = 0;
3356 
3357 	if (*sc_mp != NULL)
3358 		linkb(*sc_mp, mp);
3359 	else
3360 		*sc_mp = mp;
3361 }
3362 
3363 static void
3364 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3365 {
3366 	mblk_t *nmp = NULL;
3367 	dl_capability_req_t *oc;
3368 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3369 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3370 	uint_t sub_dl_cap = isub->dl_cap;
3371 	uint8_t *capend;
3372 
3373 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3374 
3375 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3376 
3377 	/*
3378 	 * Note: range checks here are not absolutely sufficient to
3379 	 * make us robust against malformed messages sent by drivers;
3380 	 * this is in keeping with the rest of IP's dlpi handling.
3381 	 * (Remember, it's coming from something else in the kernel
3382 	 * address space)
3383 	 */
3384 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3385 	if (capend > mp->b_wptr) {
3386 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3387 		    "malformed sub-capability too long for mblk");
3388 		return;
3389 	}
3390 
3391 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3392 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3393 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3394 		    "unsupported ZEROCOPY sub-capability (version %d, "
3395 		    "expected %d)", zc_ic->zerocopy_version,
3396 		    ZEROCOPY_VERSION_1);
3397 		return;
3398 	}
3399 
3400 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3401 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3402 		    "capability isn't as expected; pass-thru module(s) "
3403 		    "detected, discarding capability\n"));
3404 		return;
3405 	}
3406 
3407 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3408 		if (*ill_zerocopy_capab == NULL) {
3409 			*ill_zerocopy_capab =
3410 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3411 			    KM_NOSLEEP);
3412 
3413 			if (*ill_zerocopy_capab == NULL) {
3414 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3415 				    "could not enable Zero-copy version %d "
3416 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3417 				    ill->ill_name);
3418 				return;
3419 			}
3420 		}
3421 
3422 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3423 		    "supports Zero-copy version %d\n", ill->ill_name,
3424 		    ZEROCOPY_VERSION_1));
3425 
3426 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3427 		    zc_ic->zerocopy_version;
3428 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3429 		    zc_ic->zerocopy_flags;
3430 
3431 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3432 	} else {
3433 		uint_t size;
3434 		uchar_t *rptr;
3435 
3436 		size = sizeof (dl_capability_req_t) +
3437 		    sizeof (dl_capability_sub_t) +
3438 		    sizeof (dl_capab_zerocopy_t);
3439 
3440 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3441 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3442 			    "could not enable zerocopy for %s (ENOMEM)\n",
3443 			    ill->ill_name);
3444 			return;
3445 		}
3446 
3447 		rptr = nmp->b_rptr;
3448 		/* initialize dl_capability_req_t */
3449 		oc = (dl_capability_req_t *)rptr;
3450 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3451 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3452 		    sizeof (dl_capab_zerocopy_t);
3453 		rptr += sizeof (dl_capability_req_t);
3454 
3455 		/* initialize dl_capability_sub_t */
3456 		bcopy(isub, rptr, sizeof (*isub));
3457 		rptr += sizeof (*isub);
3458 
3459 		/* initialize dl_capab_zerocopy_t */
3460 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3461 		*zc_oc = *zc_ic;
3462 
3463 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3464 		    "to enable zero-copy version %d\n", ill->ill_name,
3465 		    ZEROCOPY_VERSION_1));
3466 
3467 		/* set VMSAFE_MEM flag */
3468 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3469 
3470 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3471 		ill_dlpi_send(ill, nmp);
3472 	}
3473 }
3474 
3475 static void
3476 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3477 {
3478 	mblk_t *mp;
3479 	dl_capab_zerocopy_t *zerocopy_subcap;
3480 	dl_capability_sub_t *dl_subcap;
3481 	int size;
3482 
3483 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3484 		return;
3485 
3486 	ASSERT(ill->ill_zerocopy_capab != NULL);
3487 	/*
3488 	 * Clear the capability flag for Zero-copy but retain the
3489 	 * ill_zerocopy_capab structure since it's possible that another
3490 	 * thread is still referring to it.  The structure only gets
3491 	 * deallocated when we destroy the ill.
3492 	 */
3493 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3494 
3495 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3496 
3497 	mp = allocb(size, BPRI_HI);
3498 	if (mp == NULL) {
3499 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3500 		    "request to disable Zero-copy\n"));
3501 		return;
3502 	}
3503 
3504 	mp->b_wptr = mp->b_rptr + size;
3505 
3506 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3507 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3508 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3509 
3510 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3511 	zerocopy_subcap->zerocopy_version =
3512 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3513 	zerocopy_subcap->zerocopy_flags = 0;
3514 
3515 	if (*sc_mp != NULL)
3516 		linkb(*sc_mp, mp);
3517 	else
3518 		*sc_mp = mp;
3519 }
3520 
3521 /*
3522  * Process Large Segment Offload capability negotiation ack received from a
3523  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3524  * DL_CAPABILITY_ACK message.
3525  */
3526 static void
3527 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3528 {
3529 	mblk_t *nmp = NULL;
3530 	dl_capability_req_t *oc;
3531 	dl_capab_lso_t *lso_ic, *lso_oc;
3532 	ill_lso_capab_t **ill_lso_capab;
3533 	uint_t sub_dl_cap = isub->dl_cap;
3534 	uint8_t *capend;
3535 
3536 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3537 
3538 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3539 
3540 	/*
3541 	 * Note: range checks here are not absolutely sufficient to
3542 	 * make us robust against malformed messages sent by drivers;
3543 	 * this is in keeping with the rest of IP's dlpi handling.
3544 	 * (Remember, it's coming from something else in the kernel
3545 	 * address space)
3546 	 */
3547 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3548 	if (capend > mp->b_wptr) {
3549 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3550 		    "malformed sub-capability too long for mblk");
3551 		return;
3552 	}
3553 
3554 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3555 
3556 	if (lso_ic->lso_version != LSO_VERSION_1) {
3557 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3558 		    "unsupported LSO sub-capability (version %d, expected %d)",
3559 		    lso_ic->lso_version, LSO_VERSION_1);
3560 		return;
3561 	}
3562 
3563 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3564 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3565 		    "capability isn't as expected; pass-thru module(s) "
3566 		    "detected, discarding capability\n"));
3567 		return;
3568 	}
3569 
3570 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3571 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3572 		if (*ill_lso_capab == NULL) {
3573 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3574 			    KM_NOSLEEP);
3575 
3576 			if (*ill_lso_capab == NULL) {
3577 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3578 				    "could not enable LSO version %d "
3579 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3580 				    ill->ill_name);
3581 				return;
3582 			}
3583 		}
3584 
3585 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3586 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3587 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3588 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3589 
3590 		ip1dbg(("ill_capability_lso_ack: interface %s "
3591 		    "has enabled LSO\n ", ill->ill_name));
3592 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3593 		uint_t size;
3594 		uchar_t *rptr;
3595 
3596 		size = sizeof (dl_capability_req_t) +
3597 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3598 
3599 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3600 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3601 			    "could not enable LSO for %s (ENOMEM)\n",
3602 			    ill->ill_name);
3603 			return;
3604 		}
3605 
3606 		rptr = nmp->b_rptr;
3607 		/* initialize dl_capability_req_t */
3608 		oc = (dl_capability_req_t *)nmp->b_rptr;
3609 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3610 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3611 		    sizeof (dl_capab_lso_t);
3612 		nmp->b_rptr += sizeof (dl_capability_req_t);
3613 
3614 		/* initialize dl_capability_sub_t */
3615 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3616 		nmp->b_rptr += sizeof (*isub);
3617 
3618 		/* initialize dl_capab_lso_t */
3619 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3620 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3621 
3622 		nmp->b_rptr = rptr;
3623 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3624 
3625 		/* set ENABLE flag */
3626 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3627 
3628 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3629 		ill_dlpi_send(ill, nmp);
3630 	} else {
3631 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3632 		    "advertised %x LSO capability flags\n",
3633 		    ill->ill_name, lso_ic->lso_flags));
3634 	}
3635 }
3636 
3637 
3638 static void
3639 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3640 {
3641 	mblk_t *mp;
3642 	dl_capab_lso_t *lso_subcap;
3643 	dl_capability_sub_t *dl_subcap;
3644 	int size;
3645 
3646 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3647 		return;
3648 
3649 	ASSERT(ill->ill_lso_capab != NULL);
3650 	/*
3651 	 * Clear the capability flag for LSO but retain the
3652 	 * ill_lso_capab structure since it's possible that another
3653 	 * thread is still referring to it.  The structure only gets
3654 	 * deallocated when we destroy the ill.
3655 	 */
3656 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3657 
3658 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3659 
3660 	mp = allocb(size, BPRI_HI);
3661 	if (mp == NULL) {
3662 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3663 		    "request to disable LSO\n"));
3664 		return;
3665 	}
3666 
3667 	mp->b_wptr = mp->b_rptr + size;
3668 
3669 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3670 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3671 	dl_subcap->dl_length = sizeof (*lso_subcap);
3672 
3673 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3674 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3675 	lso_subcap->lso_flags = 0;
3676 
3677 	if (*sc_mp != NULL)
3678 		linkb(*sc_mp, mp);
3679 	else
3680 		*sc_mp = mp;
3681 }
3682 
3683 /*
3684  * Consume a new-style hardware capabilities negotiation ack.
3685  * Called from ip_rput_dlpi_writer().
3686  */
3687 void
3688 ill_capability_ack(ill_t *ill, mblk_t *mp)
3689 {
3690 	dl_capability_ack_t *capp;
3691 	dl_capability_sub_t *subp, *endp;
3692 
3693 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3694 		ill->ill_dlpi_capab_state = IDS_OK;
3695 
3696 	capp = (dl_capability_ack_t *)mp->b_rptr;
3697 
3698 	if (capp->dl_sub_length == 0)
3699 		/* no new-style capabilities */
3700 		return;
3701 
3702 	/* make sure the driver supplied correct dl_sub_length */
3703 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3704 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3705 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3706 		return;
3707 	}
3708 
3709 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3710 	/*
3711 	 * There are sub-capabilities. Process the ones we know about.
3712 	 * Loop until we don't have room for another sub-cap header..
3713 	 */
3714 	for (subp = SC(capp, capp->dl_sub_offset),
3715 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3716 	    subp <= endp;
3717 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3718 
3719 		switch (subp->dl_cap) {
3720 		case DL_CAPAB_ID_WRAPPER:
3721 			ill_capability_id_ack(ill, mp, subp);
3722 			break;
3723 		default:
3724 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3725 			break;
3726 		}
3727 	}
3728 #undef SC
3729 }
3730 
3731 /*
3732  * This routine is called to scan the fragmentation reassembly table for
3733  * the specified ILL for any packets that are starting to smell.
3734  * dead_interval is the maximum time in seconds that will be tolerated.  It
3735  * will either be the value specified in ip_g_frag_timeout, or zero if the
3736  * ILL is shutting down and it is time to blow everything off.
3737  *
3738  * It returns the number of seconds (as a time_t) that the next frag timer
3739  * should be scheduled for, 0 meaning that the timer doesn't need to be
3740  * re-started.  Note that the method of calculating next_timeout isn't
3741  * entirely accurate since time will flow between the time we grab
3742  * current_time and the time we schedule the next timeout.  This isn't a
3743  * big problem since this is the timer for sending an ICMP reassembly time
3744  * exceeded messages, and it doesn't have to be exactly accurate.
3745  *
3746  * This function is
3747  * sometimes called as writer, although this is not required.
3748  */
3749 time_t
3750 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3751 {
3752 	ipfb_t	*ipfb;
3753 	ipfb_t	*endp;
3754 	ipf_t	*ipf;
3755 	ipf_t	*ipfnext;
3756 	mblk_t	*mp;
3757 	time_t	current_time = gethrestime_sec();
3758 	time_t	next_timeout = 0;
3759 	uint32_t	hdr_length;
3760 	mblk_t	*send_icmp_head;
3761 	mblk_t	*send_icmp_head_v6;
3762 	zoneid_t zoneid;
3763 
3764 	ipfb = ill->ill_frag_hash_tbl;
3765 	if (ipfb == NULL)
3766 		return (B_FALSE);
3767 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3768 	/* Walk the frag hash table. */
3769 	for (; ipfb < endp; ipfb++) {
3770 		send_icmp_head = NULL;
3771 		send_icmp_head_v6 = NULL;
3772 		mutex_enter(&ipfb->ipfb_lock);
3773 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3774 			time_t frag_time = current_time - ipf->ipf_timestamp;
3775 			time_t frag_timeout;
3776 
3777 			if (frag_time < dead_interval) {
3778 				/*
3779 				 * There are some outstanding fragments
3780 				 * that will timeout later.  Make note of
3781 				 * the time so that we can reschedule the
3782 				 * next timeout appropriately.
3783 				 */
3784 				frag_timeout = dead_interval - frag_time;
3785 				if (next_timeout == 0 ||
3786 				    frag_timeout < next_timeout) {
3787 					next_timeout = frag_timeout;
3788 				}
3789 				break;
3790 			}
3791 			/* Time's up.  Get it out of here. */
3792 			hdr_length = ipf->ipf_nf_hdr_len;
3793 			ipfnext = ipf->ipf_hash_next;
3794 			if (ipfnext)
3795 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3796 			*ipf->ipf_ptphn = ipfnext;
3797 			mp = ipf->ipf_mp->b_cont;
3798 			for (; mp; mp = mp->b_cont) {
3799 				/* Extra points for neatness. */
3800 				IP_REASS_SET_START(mp, 0);
3801 				IP_REASS_SET_END(mp, 0);
3802 			}
3803 			mp = ipf->ipf_mp->b_cont;
3804 			ill->ill_frag_count -= ipf->ipf_count;
3805 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3806 			ipfb->ipfb_count -= ipf->ipf_count;
3807 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3808 			ipfb->ipfb_frag_pkts--;
3809 			/*
3810 			 * We do not send any icmp message from here because
3811 			 * we currently are holding the ipfb_lock for this
3812 			 * hash chain. If we try and send any icmp messages
3813 			 * from here we may end up via a put back into ip
3814 			 * trying to get the same lock, causing a recursive
3815 			 * mutex panic. Instead we build a list and send all
3816 			 * the icmp messages after we have dropped the lock.
3817 			 */
3818 			if (ill->ill_isv6) {
3819 				if (hdr_length != 0) {
3820 					mp->b_next = send_icmp_head_v6;
3821 					send_icmp_head_v6 = mp;
3822 				} else {
3823 					freemsg(mp);
3824 				}
3825 			} else {
3826 				if (hdr_length != 0) {
3827 					mp->b_next = send_icmp_head;
3828 					send_icmp_head = mp;
3829 				} else {
3830 					freemsg(mp);
3831 				}
3832 			}
3833 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3834 			freeb(ipf->ipf_mp);
3835 		}
3836 		mutex_exit(&ipfb->ipfb_lock);
3837 		/*
3838 		 * Now need to send any icmp messages that we delayed from
3839 		 * above.
3840 		 */
3841 		while (send_icmp_head_v6 != NULL) {
3842 			ip6_t *ip6h;
3843 
3844 			mp = send_icmp_head_v6;
3845 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3846 			mp->b_next = NULL;
3847 			if (mp->b_datap->db_type == M_CTL)
3848 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3849 			else
3850 				ip6h = (ip6_t *)mp->b_rptr;
3851 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3852 			    ill);
3853 			if (zoneid == ALL_ZONES) {
3854 				freemsg(mp);
3855 			} else {
3856 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3857 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3858 				    B_FALSE, zoneid);
3859 			}
3860 		}
3861 		while (send_icmp_head != NULL) {
3862 			ipaddr_t dst;
3863 
3864 			mp = send_icmp_head;
3865 			send_icmp_head = send_icmp_head->b_next;
3866 			mp->b_next = NULL;
3867 
3868 			if (mp->b_datap->db_type == M_CTL)
3869 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3870 			else
3871 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3872 
3873 			zoneid = ipif_lookup_addr_zoneid(dst, ill);
3874 			if (zoneid == ALL_ZONES) {
3875 				freemsg(mp);
3876 			} else {
3877 				icmp_time_exceeded(ill->ill_wq, mp,
3878 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid);
3879 			}
3880 		}
3881 	}
3882 	/*
3883 	 * A non-dying ILL will use the return value to decide whether to
3884 	 * restart the frag timer, and for how long.
3885 	 */
3886 	return (next_timeout);
3887 }
3888 
3889 /*
3890  * This routine is called when the approximate count of mblk memory used
3891  * for the specified ILL has exceeded max_count.
3892  */
3893 void
3894 ill_frag_prune(ill_t *ill, uint_t max_count)
3895 {
3896 	ipfb_t	*ipfb;
3897 	ipf_t	*ipf;
3898 	size_t	count;
3899 
3900 	/*
3901 	 * If we are here within ip_min_frag_prune_time msecs remove
3902 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3903 	 * ill_frag_free_num_pkts.
3904 	 */
3905 	mutex_enter(&ill->ill_lock);
3906 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3907 	    (ip_min_frag_prune_time != 0 ?
3908 	    ip_min_frag_prune_time : msec_per_tick)) {
3909 
3910 		ill->ill_frag_free_num_pkts++;
3911 
3912 	} else {
3913 		ill->ill_frag_free_num_pkts = 0;
3914 	}
3915 	ill->ill_last_frag_clean_time = lbolt;
3916 	mutex_exit(&ill->ill_lock);
3917 
3918 	/*
3919 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3920 	 */
3921 	if (ill->ill_frag_free_num_pkts != 0) {
3922 		int ix;
3923 
3924 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3925 			ipfb = &ill->ill_frag_hash_tbl[ix];
3926 			mutex_enter(&ipfb->ipfb_lock);
3927 			if (ipfb->ipfb_ipf != NULL) {
3928 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3929 				    ill->ill_frag_free_num_pkts);
3930 			}
3931 			mutex_exit(&ipfb->ipfb_lock);
3932 		}
3933 	}
3934 	/*
3935 	 * While the reassembly list for this ILL is too big, prune a fragment
3936 	 * queue by age, oldest first.  Note that the per ILL count is
3937 	 * approximate, while the per frag hash bucket counts are accurate.
3938 	 */
3939 	while (ill->ill_frag_count > max_count) {
3940 		int	ix;
3941 		ipfb_t	*oipfb = NULL;
3942 		uint_t	oldest = UINT_MAX;
3943 
3944 		count = 0;
3945 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3946 			ipfb = &ill->ill_frag_hash_tbl[ix];
3947 			mutex_enter(&ipfb->ipfb_lock);
3948 			ipf = ipfb->ipfb_ipf;
3949 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3950 				oldest = ipf->ipf_gen;
3951 				oipfb = ipfb;
3952 			}
3953 			count += ipfb->ipfb_count;
3954 			mutex_exit(&ipfb->ipfb_lock);
3955 		}
3956 		/* Refresh the per ILL count */
3957 		ill->ill_frag_count = count;
3958 		if (oipfb == NULL) {
3959 			ill->ill_frag_count = 0;
3960 			break;
3961 		}
3962 		if (count <= max_count)
3963 			return;	/* Somebody beat us to it, nothing to do */
3964 		mutex_enter(&oipfb->ipfb_lock);
3965 		ipf = oipfb->ipfb_ipf;
3966 		if (ipf != NULL) {
3967 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3968 		}
3969 		mutex_exit(&oipfb->ipfb_lock);
3970 	}
3971 }
3972 
3973 /*
3974  * free 'free_cnt' fragmented packets starting at ipf.
3975  */
3976 void
3977 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3978 {
3979 	size_t	count;
3980 	mblk_t	*mp;
3981 	mblk_t	*tmp;
3982 	ipf_t **ipfp = ipf->ipf_ptphn;
3983 
3984 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3985 	ASSERT(ipfp != NULL);
3986 	ASSERT(ipf != NULL);
3987 
3988 	while (ipf != NULL && free_cnt-- > 0) {
3989 		count = ipf->ipf_count;
3990 		mp = ipf->ipf_mp;
3991 		ipf = ipf->ipf_hash_next;
3992 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3993 			IP_REASS_SET_START(tmp, 0);
3994 			IP_REASS_SET_END(tmp, 0);
3995 		}
3996 		ill->ill_frag_count -= count;
3997 		ASSERT(ipfb->ipfb_count >= count);
3998 		ipfb->ipfb_count -= count;
3999 		ASSERT(ipfb->ipfb_frag_pkts > 0);
4000 		ipfb->ipfb_frag_pkts--;
4001 		freemsg(mp);
4002 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
4003 	}
4004 
4005 	if (ipf)
4006 		ipf->ipf_ptphn = ipfp;
4007 	ipfp[0] = ipf;
4008 }
4009 
4010 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
4011 	"obsolete and may be removed in a future release of Solaris.  Use " \
4012 	"ifconfig(1M) to manipulate the forwarding status of an interface."
4013 
4014 /*
4015  * For obsolete per-interface forwarding configuration;
4016  * called in response to ND_GET.
4017  */
4018 /* ARGSUSED */
4019 static int
4020 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
4021 {
4022 	ill_t *ill = (ill_t *)cp;
4023 
4024 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
4025 
4026 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
4027 	return (0);
4028 }
4029 
4030 /*
4031  * For obsolete per-interface forwarding configuration;
4032  * called in response to ND_SET.
4033  */
4034 /* ARGSUSED */
4035 static int
4036 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
4037     cred_t *ioc_cr)
4038 {
4039 	long value;
4040 	int retval;
4041 
4042 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
4043 
4044 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
4045 	    value < 0 || value > 1) {
4046 		return (EINVAL);
4047 	}
4048 
4049 	rw_enter(&ill_g_lock, RW_READER);
4050 	retval = ill_forward_set(q, mp, (value != 0), cp);
4051 	rw_exit(&ill_g_lock);
4052 	return (retval);
4053 }
4054 
4055 /*
4056  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
4057  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
4058  * up RTS_IFINFO routing socket messages for each interface whose flags we
4059  * change.
4060  */
4061 /* ARGSUSED */
4062 int
4063 ill_forward_set(queue_t *q, mblk_t *mp, boolean_t enable, caddr_t cp)
4064 {
4065 	ill_t *ill = (ill_t *)cp;
4066 	ill_group_t *illgrp;
4067 
4068 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ill_g_lock));
4069 
4070 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
4071 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)) ||
4072 	    (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK))
4073 		return (EINVAL);
4074 
4075 	/*
4076 	 * If the ill is in an IPMP group, set the forwarding policy on all
4077 	 * members of the group to the same value.
4078 	 */
4079 	illgrp = ill->ill_group;
4080 	if (illgrp != NULL) {
4081 		ill_t *tmp_ill;
4082 
4083 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
4084 		    tmp_ill = tmp_ill->ill_group_next) {
4085 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
4086 			    (enable ? "Enabling" : "Disabling"),
4087 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
4088 			    tmp_ill->ill_name));
4089 			mutex_enter(&tmp_ill->ill_lock);
4090 			if (enable)
4091 				tmp_ill->ill_flags |= ILLF_ROUTER;
4092 			else
4093 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
4094 			mutex_exit(&tmp_ill->ill_lock);
4095 			if (tmp_ill->ill_isv6)
4096 				ill_set_nce_router_flags(tmp_ill, enable);
4097 			/* Notify routing socket listeners of this change. */
4098 			ip_rts_ifmsg(tmp_ill->ill_ipif);
4099 		}
4100 	} else {
4101 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
4102 		    (enable ? "Enabling" : "Disabling"),
4103 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
4104 		mutex_enter(&ill->ill_lock);
4105 		if (enable)
4106 			ill->ill_flags |= ILLF_ROUTER;
4107 		else
4108 			ill->ill_flags &= ~ILLF_ROUTER;
4109 		mutex_exit(&ill->ill_lock);
4110 		if (ill->ill_isv6)
4111 			ill_set_nce_router_flags(ill, enable);
4112 		/* Notify routing socket listeners of this change. */
4113 		ip_rts_ifmsg(ill->ill_ipif);
4114 	}
4115 
4116 	return (0);
4117 }
4118 
4119 /*
4120  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
4121  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
4122  * set or clear.
4123  */
4124 static void
4125 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
4126 {
4127 	ipif_t *ipif;
4128 	nce_t *nce;
4129 
4130 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4131 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
4132 		if (nce != NULL) {
4133 			mutex_enter(&nce->nce_lock);
4134 			if (enable)
4135 				nce->nce_flags |= NCE_F_ISROUTER;
4136 			else
4137 				nce->nce_flags &= ~NCE_F_ISROUTER;
4138 			mutex_exit(&nce->nce_lock);
4139 			NCE_REFRELE(nce);
4140 		}
4141 	}
4142 }
4143 
4144 /*
4145  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
4146  * for this ill.  Make sure the v6/v4 question has been answered about this
4147  * ill.  The creation of this ndd variable is only for backwards compatibility.
4148  * The preferred way to control per-interface IP forwarding is through the
4149  * ILLF_ROUTER interface flag.
4150  */
4151 static int
4152 ill_set_ndd_name(ill_t *ill)
4153 {
4154 	char *suffix;
4155 
4156 	ASSERT(IAM_WRITER_ILL(ill));
4157 
4158 	if (ill->ill_isv6)
4159 		suffix = ipv6_forward_suffix;
4160 	else
4161 		suffix = ipv4_forward_suffix;
4162 
4163 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4164 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4165 	/*
4166 	 * Copies over the '\0'.
4167 	 * Note that strlen(suffix) is always bounded.
4168 	 */
4169 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4170 	    strlen(suffix) + 1);
4171 
4172 	/*
4173 	 * Use of the nd table requires holding the reader lock.
4174 	 * Modifying the nd table thru nd_load/nd_unload requires
4175 	 * the writer lock.
4176 	 */
4177 	rw_enter(&ip_g_nd_lock, RW_WRITER);
4178 	if (!nd_load(&ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4179 	    nd_ill_forward_set, (caddr_t)ill)) {
4180 		/*
4181 		 * If the nd_load failed, it only meant that it could not
4182 		 * allocate a new bunch of room for further NDD expansion.
4183 		 * Because of that, the ill_ndd_name will be set to 0, and
4184 		 * this interface is at the mercy of the global ip_forwarding
4185 		 * variable.
4186 		 */
4187 		rw_exit(&ip_g_nd_lock);
4188 		ill->ill_ndd_name = NULL;
4189 		return (ENOMEM);
4190 	}
4191 	rw_exit(&ip_g_nd_lock);
4192 	return (0);
4193 }
4194 
4195 /*
4196  * Intializes the context structure and returns the first ill in the list
4197  * cuurently start_list and end_list can have values:
4198  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4199  * IP_V4_G_HEAD		Traverse IPV4 list only.
4200  * IP_V6_G_HEAD		Traverse IPV6 list only.
4201  */
4202 
4203 /*
4204  * We don't check for CONDEMNED ills here. Caller must do that if
4205  * necessary under the ill lock.
4206  */
4207 ill_t *
4208 ill_first(int start_list, int end_list, ill_walk_context_t *ctx)
4209 {
4210 	ill_if_t *ifp;
4211 	ill_t *ill;
4212 	avl_tree_t *avl_tree;
4213 
4214 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
4215 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4216 
4217 	/*
4218 	 * setup the lists to search
4219 	 */
4220 	if (end_list != MAX_G_HEADS) {
4221 		ctx->ctx_current_list = start_list;
4222 		ctx->ctx_last_list = end_list;
4223 	} else {
4224 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4225 		ctx->ctx_current_list = 0;
4226 	}
4227 
4228 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4229 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list);
4230 		if (ifp != (ill_if_t *)
4231 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list)) {
4232 			avl_tree = &ifp->illif_avl_by_ppa;
4233 			ill = avl_first(avl_tree);
4234 			/*
4235 			 * ill is guaranteed to be non NULL or ifp should have
4236 			 * not existed.
4237 			 */
4238 			ASSERT(ill != NULL);
4239 			return (ill);
4240 		}
4241 		ctx->ctx_current_list++;
4242 	}
4243 
4244 	return (NULL);
4245 }
4246 
4247 /*
4248  * returns the next ill in the list. ill_first() must have been called
4249  * before calling ill_next() or bad things will happen.
4250  */
4251 
4252 /*
4253  * We don't check for CONDEMNED ills here. Caller must do that if
4254  * necessary under the ill lock.
4255  */
4256 ill_t *
4257 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4258 {
4259 	ill_if_t *ifp;
4260 	ill_t *ill;
4261 
4262 
4263 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
4264 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4265 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list));
4266 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4267 	    AVL_AFTER)) != NULL) {
4268 		return (ill);
4269 	}
4270 
4271 	/* goto next ill_ifp in the list. */
4272 	ifp = lastill->ill_ifptr->illif_next;
4273 
4274 	/* make sure not at end of circular list */
4275 	while (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list)) {
4276 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4277 			return (NULL);
4278 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list);
4279 	}
4280 
4281 	return (avl_first(&ifp->illif_avl_by_ppa));
4282 }
4283 
4284 /*
4285  * Check interface name for correct format which is name+ppa.
4286  * name can contain characters and digits, the right most digits
4287  * make up the ppa number. use of octal is not allowed, name must contain
4288  * a ppa, return pointer to the start of ppa.
4289  * In case of error return NULL.
4290  */
4291 static char *
4292 ill_get_ppa_ptr(char *name)
4293 {
4294 	int namelen = mi_strlen(name);
4295 
4296 	int len = namelen;
4297 
4298 	name += len;
4299 	while (len > 0) {
4300 		name--;
4301 		if (*name < '0' || *name > '9')
4302 			break;
4303 		len--;
4304 	}
4305 
4306 	/* empty string, all digits, or no trailing digits */
4307 	if (len == 0 || len == (int)namelen)
4308 		return (NULL);
4309 
4310 	name++;
4311 	/* check for attempted use of octal */
4312 	if (*name == '0' && len != (int)namelen - 1)
4313 		return (NULL);
4314 	return (name);
4315 }
4316 
4317 /*
4318  * use avl tree to locate the ill.
4319  */
4320 static ill_t *
4321 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4322     ipsq_func_t func, int *error)
4323 {
4324 	char *ppa_ptr = NULL;
4325 	int len;
4326 	uint_t ppa;
4327 	ill_t *ill = NULL;
4328 	ill_if_t *ifp;
4329 	int list;
4330 	ipsq_t *ipsq;
4331 
4332 	if (error != NULL)
4333 		*error = 0;
4334 
4335 	/*
4336 	 * get ppa ptr
4337 	 */
4338 	if (isv6)
4339 		list = IP_V6_G_HEAD;
4340 	else
4341 		list = IP_V4_G_HEAD;
4342 
4343 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4344 		if (error != NULL)
4345 			*error = ENXIO;
4346 		return (NULL);
4347 	}
4348 
4349 	len = ppa_ptr - name + 1;
4350 
4351 	ppa = stoi(&ppa_ptr);
4352 
4353 	ifp = IP_VX_ILL_G_LIST(list);
4354 
4355 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list)) {
4356 		/*
4357 		 * match is done on len - 1 as the name is not null
4358 		 * terminated it contains ppa in addition to the interface
4359 		 * name.
4360 		 */
4361 		if ((ifp->illif_name_len == len) &&
4362 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4363 			break;
4364 		} else {
4365 			ifp = ifp->illif_next;
4366 		}
4367 	}
4368 
4369 
4370 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list)) {
4371 		/*
4372 		 * Even the interface type does not exist.
4373 		 */
4374 		if (error != NULL)
4375 			*error = ENXIO;
4376 		return (NULL);
4377 	}
4378 
4379 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4380 	if (ill != NULL) {
4381 		/*
4382 		 * The block comment at the start of ipif_down
4383 		 * explains the use of the macros used below
4384 		 */
4385 		GRAB_CONN_LOCK(q);
4386 		mutex_enter(&ill->ill_lock);
4387 		if (ILL_CAN_LOOKUP(ill)) {
4388 			ill_refhold_locked(ill);
4389 			mutex_exit(&ill->ill_lock);
4390 			RELEASE_CONN_LOCK(q);
4391 			return (ill);
4392 		} else if (ILL_CAN_WAIT(ill, q)) {
4393 			ipsq = ill->ill_phyint->phyint_ipsq;
4394 			mutex_enter(&ipsq->ipsq_lock);
4395 			mutex_exit(&ill->ill_lock);
4396 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4397 			mutex_exit(&ipsq->ipsq_lock);
4398 			RELEASE_CONN_LOCK(q);
4399 			*error = EINPROGRESS;
4400 			return (NULL);
4401 		}
4402 		mutex_exit(&ill->ill_lock);
4403 		RELEASE_CONN_LOCK(q);
4404 	}
4405 	if (error != NULL)
4406 		*error = ENXIO;
4407 	return (NULL);
4408 }
4409 
4410 /*
4411  * comparison function for use with avl.
4412  */
4413 static int
4414 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4415 {
4416 	uint_t ppa;
4417 	uint_t ill_ppa;
4418 
4419 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4420 
4421 	ppa = *((uint_t *)ppa_ptr);
4422 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4423 	/*
4424 	 * We want the ill with the lowest ppa to be on the
4425 	 * top.
4426 	 */
4427 	if (ill_ppa < ppa)
4428 		return (1);
4429 	if (ill_ppa > ppa)
4430 		return (-1);
4431 	return (0);
4432 }
4433 
4434 /*
4435  * remove an interface type from the global list.
4436  */
4437 static void
4438 ill_delete_interface_type(ill_if_t *interface)
4439 {
4440 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
4441 
4442 	ASSERT(interface != NULL);
4443 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4444 
4445 	avl_destroy(&interface->illif_avl_by_ppa);
4446 	if (interface->illif_ppa_arena != NULL)
4447 		vmem_destroy(interface->illif_ppa_arena);
4448 
4449 	remque(interface);
4450 
4451 	mi_free(interface);
4452 }
4453 
4454 /* Defined in ip_netinfo.c */
4455 extern ddi_taskq_t	*eventq_queue_nic;
4456 
4457 /*
4458  * remove ill from the global list.
4459  */
4460 static void
4461 ill_glist_delete(ill_t *ill)
4462 {
4463 	char *nicname;
4464 	size_t nicnamelen;
4465 	hook_nic_event_t *info;
4466 
4467 	if (ill == NULL)
4468 		return;
4469 
4470 	rw_enter(&ill_g_lock, RW_WRITER);
4471 
4472 	if (ill->ill_name != NULL) {
4473 		nicname = kmem_alloc(ill->ill_name_length, KM_NOSLEEP);
4474 		if (nicname != NULL) {
4475 			bcopy(ill->ill_name, nicname, ill->ill_name_length);
4476 			nicnamelen = ill->ill_name_length;
4477 		}
4478 	} else {
4479 		nicname = NULL;
4480 		nicnamelen = 0;
4481 	}
4482 
4483 	/*
4484 	 * If the ill was never inserted into the AVL tree
4485 	 * we skip the if branch.
4486 	 */
4487 	if (ill->ill_ifptr != NULL) {
4488 		/*
4489 		 * remove from AVL tree and free ppa number
4490 		 */
4491 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4492 
4493 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4494 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4495 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4496 		}
4497 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4498 			ill_delete_interface_type(ill->ill_ifptr);
4499 		}
4500 
4501 		/*
4502 		 * Indicate ill is no longer in the list.
4503 		 */
4504 		ill->ill_ifptr = NULL;
4505 		ill->ill_name_length = 0;
4506 		ill->ill_name[0] = '\0';
4507 		ill->ill_ppa = UINT_MAX;
4508 	}
4509 
4510 	/*
4511 	 * Run the unplumb hook after the NIC has disappeared from being
4512 	 * visible so that attempts to revalidate its existance will fail.
4513 	 *
4514 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4515 	 * that the ordering of delivered events to listeners matches the
4516 	 * order of them in the kernel.
4517 	 */
4518 	if ((info = ill->ill_nic_event_info) != NULL) {
4519 		if (info->hne_event != NE_DOWN) {
4520 			ip2dbg(("ill_glist_delete: unexpected nic event %d "
4521 			    "attached for %s\n", info->hne_event,
4522 			    ill->ill_name));
4523 			if (info->hne_data != NULL)
4524 				kmem_free(info->hne_data, info->hne_datalen);
4525 			kmem_free(info, sizeof (hook_nic_event_t));
4526 		} else {
4527 			if (ddi_taskq_dispatch(eventq_queue_nic,
4528 			    ip_ne_queue_func, (void *)info, DDI_SLEEP)
4529 			    == DDI_FAILURE) {
4530 				ip2dbg(("ill_glist_delete: ddi_taskq_dispatch "
4531 				    "failed\n"));
4532 				if (info->hne_data != NULL)
4533 					kmem_free(info->hne_data,
4534 					    info->hne_datalen);
4535 				kmem_free(info, sizeof (hook_nic_event_t));
4536 			}
4537 		}
4538 	}
4539 
4540 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
4541 	if (info != NULL) {
4542 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
4543 		info->hne_lif = 0;
4544 		info->hne_event = NE_UNPLUMB;
4545 		info->hne_data = nicname;
4546 		info->hne_datalen = nicnamelen;
4547 		info->hne_family = ill->ill_isv6 ? ipv6 : ipv4;
4548 	} else {
4549 		ip2dbg(("ill_glist_delete: could not attach UNPLUMB nic event "
4550 		    "information for %s (ENOMEM)\n", ill->ill_name));
4551 		if (nicname != NULL)
4552 			kmem_free(nicname, nicnamelen);
4553 	}
4554 
4555 	ill->ill_nic_event_info = info;
4556 
4557 	ill_phyint_free(ill);
4558 
4559 	rw_exit(&ill_g_lock);
4560 }
4561 
4562 /*
4563  * allocate a ppa, if the number of plumbed interfaces of this type are
4564  * less than ill_no_arena do a linear search to find a unused ppa.
4565  * When the number goes beyond ill_no_arena switch to using an arena.
4566  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4567  * is the return value for an error condition, so allocation starts at one
4568  * and is decremented by one.
4569  */
4570 static int
4571 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4572 {
4573 	ill_t *tmp_ill;
4574 	uint_t start, end;
4575 	int ppa;
4576 
4577 	if (ifp->illif_ppa_arena == NULL &&
4578 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4579 		/*
4580 		 * Create an arena.
4581 		 */
4582 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4583 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4584 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4585 			/* allocate what has already been assigned */
4586 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4587 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4588 		    tmp_ill, AVL_AFTER)) {
4589 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4590 			    1,		/* size */
4591 			    1,		/* align/quantum */
4592 			    0,		/* phase */
4593 			    0,		/* nocross */
4594 		(void *)((uintptr_t)tmp_ill->ill_ppa + 1), /* minaddr */
4595 		(void *)((uintptr_t)tmp_ill->ill_ppa + 2), /* maxaddr */
4596 			    VM_NOSLEEP|VM_FIRSTFIT);
4597 			if (ppa == 0) {
4598 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4599 				    " failed while switching"));
4600 				vmem_destroy(ifp->illif_ppa_arena);
4601 				ifp->illif_ppa_arena = NULL;
4602 				break;
4603 			}
4604 		}
4605 	}
4606 
4607 	if (ifp->illif_ppa_arena != NULL) {
4608 		if (ill->ill_ppa == UINT_MAX) {
4609 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4610 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4611 			if (ppa == 0)
4612 				return (EAGAIN);
4613 			ill->ill_ppa = --ppa;
4614 		} else {
4615 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4616 			    1, 		/* size */
4617 			    1, 		/* align/quantum */
4618 			    0, 		/* phase */
4619 			    0, 		/* nocross */
4620 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4621 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4622 			    VM_NOSLEEP|VM_FIRSTFIT);
4623 			/*
4624 			 * Most likely the allocation failed because
4625 			 * the requested ppa was in use.
4626 			 */
4627 			if (ppa == 0)
4628 				return (EEXIST);
4629 		}
4630 		return (0);
4631 	}
4632 
4633 	/*
4634 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4635 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4636 	 */
4637 	if (ill->ill_ppa == UINT_MAX) {
4638 		end = UINT_MAX - 1;
4639 		start = 0;
4640 	} else {
4641 		end = start = ill->ill_ppa;
4642 	}
4643 
4644 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4645 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4646 		if (start++ >= end) {
4647 			if (ill->ill_ppa == UINT_MAX)
4648 				return (EAGAIN);
4649 			else
4650 				return (EEXIST);
4651 		}
4652 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4653 	}
4654 	ill->ill_ppa = start;
4655 	return (0);
4656 }
4657 
4658 /*
4659  * Insert ill into the list of configured ill's. Once this function completes,
4660  * the ill is globally visible and is available through lookups. More precisely
4661  * this happens after the caller drops the ill_g_lock.
4662  */
4663 static int
4664 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4665 {
4666 	ill_if_t *ill_interface;
4667 	avl_index_t where = 0;
4668 	int error;
4669 	int name_length;
4670 	int index;
4671 	boolean_t check_length = B_FALSE;
4672 
4673 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
4674 
4675 	name_length = mi_strlen(name) + 1;
4676 
4677 	if (isv6)
4678 		index = IP_V6_G_HEAD;
4679 	else
4680 		index = IP_V4_G_HEAD;
4681 
4682 	ill_interface = IP_VX_ILL_G_LIST(index);
4683 	/*
4684 	 * Search for interface type based on name
4685 	 */
4686 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index)) {
4687 		if ((ill_interface->illif_name_len == name_length) &&
4688 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4689 			break;
4690 		}
4691 		ill_interface = ill_interface->illif_next;
4692 	}
4693 
4694 	/*
4695 	 * Interface type not found, create one.
4696 	 */
4697 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index)) {
4698 
4699 		ill_g_head_t ghead;
4700 
4701 		/*
4702 		 * allocate ill_if_t structure
4703 		 */
4704 
4705 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4706 		if (ill_interface == NULL) {
4707 			return (ENOMEM);
4708 		}
4709 
4710 
4711 
4712 		(void) strcpy(ill_interface->illif_name, name);
4713 		ill_interface->illif_name_len = name_length;
4714 
4715 		avl_create(&ill_interface->illif_avl_by_ppa,
4716 		    ill_compare_ppa, sizeof (ill_t),
4717 		    offsetof(struct ill_s, ill_avl_byppa));
4718 
4719 		/*
4720 		 * link the structure in the back to maintain order
4721 		 * of configuration for ifconfig output.
4722 		 */
4723 		ghead = ill_g_heads[index];
4724 		insque(ill_interface, ghead.ill_g_list_tail);
4725 
4726 	}
4727 
4728 	if (ill->ill_ppa == UINT_MAX)
4729 		check_length = B_TRUE;
4730 
4731 	error = ill_alloc_ppa(ill_interface, ill);
4732 	if (error != 0) {
4733 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4734 			ill_delete_interface_type(ill->ill_ifptr);
4735 		return (error);
4736 	}
4737 
4738 	/*
4739 	 * When the ppa is choosen by the system, check that there is
4740 	 * enough space to insert ppa. if a specific ppa was passed in this
4741 	 * check is not required as the interface name passed in will have
4742 	 * the right ppa in it.
4743 	 */
4744 	if (check_length) {
4745 		/*
4746 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4747 		 */
4748 		char buf[sizeof (uint_t) * 3];
4749 
4750 		/*
4751 		 * convert ppa to string to calculate the amount of space
4752 		 * required for it in the name.
4753 		 */
4754 		numtos(ill->ill_ppa, buf);
4755 
4756 		/* Do we have enough space to insert ppa ? */
4757 
4758 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4759 			/* Free ppa and interface type struct */
4760 			if (ill_interface->illif_ppa_arena != NULL) {
4761 				vmem_free(ill_interface->illif_ppa_arena,
4762 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4763 			}
4764 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4765 			    0) {
4766 				ill_delete_interface_type(ill->ill_ifptr);
4767 			}
4768 
4769 			return (EINVAL);
4770 		}
4771 	}
4772 
4773 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4774 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4775 
4776 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4777 	    &where);
4778 	ill->ill_ifptr = ill_interface;
4779 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4780 
4781 	ill_phyint_reinit(ill);
4782 	return (0);
4783 }
4784 
4785 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4786 static boolean_t
4787 ipsq_init(ill_t *ill)
4788 {
4789 	ipsq_t  *ipsq;
4790 
4791 	/* Init the ipsq and impicitly enter as writer */
4792 	ill->ill_phyint->phyint_ipsq =
4793 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4794 	if (ill->ill_phyint->phyint_ipsq == NULL)
4795 		return (B_FALSE);
4796 	ipsq = ill->ill_phyint->phyint_ipsq;
4797 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4798 	ill->ill_phyint->phyint_ipsq_next = NULL;
4799 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4800 	ipsq->ipsq_refs = 1;
4801 	ipsq->ipsq_writer = curthread;
4802 	ipsq->ipsq_reentry_cnt = 1;
4803 #ifdef ILL_DEBUG
4804 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack, IP_STACK_DEPTH);
4805 #endif
4806 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4807 	return (B_TRUE);
4808 }
4809 
4810 /*
4811  * ill_init is called by ip_open when a device control stream is opened.
4812  * It does a few initializations, and shoots a DL_INFO_REQ message down
4813  * to the driver.  The response is later picked up in ip_rput_dlpi and
4814  * used to set up default mechanisms for talking to the driver.  (Always
4815  * called as writer.)
4816  *
4817  * If this function returns error, ip_open will call ip_close which in
4818  * turn will call ill_delete to clean up any memory allocated here that
4819  * is not yet freed.
4820  */
4821 int
4822 ill_init(queue_t *q, ill_t *ill)
4823 {
4824 	int	count;
4825 	dl_info_req_t	*dlir;
4826 	mblk_t	*info_mp;
4827 	uchar_t *frag_ptr;
4828 
4829 	/*
4830 	 * The ill is initialized to zero by mi_alloc*(). In addition
4831 	 * some fields already contain valid values, initialized in
4832 	 * ip_open(), before we reach here.
4833 	 */
4834 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4835 
4836 	ill->ill_rq = q;
4837 	ill->ill_wq = WR(q);
4838 
4839 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4840 	    BPRI_HI);
4841 	if (info_mp == NULL)
4842 		return (ENOMEM);
4843 
4844 	/*
4845 	 * Allocate sufficient space to contain our fragment hash table and
4846 	 * the device name.
4847 	 */
4848 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4849 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4850 	if (frag_ptr == NULL) {
4851 		freemsg(info_mp);
4852 		return (ENOMEM);
4853 	}
4854 	ill->ill_frag_ptr = frag_ptr;
4855 	ill->ill_frag_free_num_pkts = 0;
4856 	ill->ill_last_frag_clean_time = 0;
4857 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4858 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4859 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4860 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4861 		    NULL, MUTEX_DEFAULT, NULL);
4862 	}
4863 
4864 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4865 	if (ill->ill_phyint == NULL) {
4866 		freemsg(info_mp);
4867 		mi_free(frag_ptr);
4868 		return (ENOMEM);
4869 	}
4870 
4871 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4872 	/*
4873 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4874 	 * at this point because of the following reason. If we can't
4875 	 * enter the ipsq at some point and cv_wait, the writer that
4876 	 * wakes us up tries to locate us using the list of all phyints
4877 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4878 	 * If we don't set it now, we risk a missed wakeup.
4879 	 */
4880 	ill->ill_phyint->phyint_illv4 = ill;
4881 	ill->ill_ppa = UINT_MAX;
4882 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4883 
4884 	if (!ipsq_init(ill)) {
4885 		freemsg(info_mp);
4886 		mi_free(frag_ptr);
4887 		mi_free(ill->ill_phyint);
4888 		return (ENOMEM);
4889 	}
4890 
4891 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4892 
4893 
4894 	/* Frag queue limit stuff */
4895 	ill->ill_frag_count = 0;
4896 	ill->ill_ipf_gen = 0;
4897 
4898 	ill->ill_global_timer = INFINITY;
4899 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
4900 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4901 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4902 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4903 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4904 
4905 	/*
4906 	 * Initialize IPv6 configuration variables.  The IP module is always
4907 	 * opened as an IPv4 module.  Instead tracking down the cases where
4908 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4909 	 * here for convenience, this has no effect until the ill is set to do
4910 	 * IPv6.
4911 	 */
4912 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4913 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4914 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4915 	ill->ill_max_buf = ND_MAX_Q;
4916 	ill->ill_refcnt = 0;
4917 
4918 	/* Send down the Info Request to the driver. */
4919 	info_mp->b_datap->db_type = M_PCPROTO;
4920 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4921 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4922 	dlir->dl_primitive = DL_INFO_REQ;
4923 
4924 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4925 
4926 	qprocson(q);
4927 	ill_dlpi_send(ill, info_mp);
4928 
4929 	return (0);
4930 }
4931 
4932 /*
4933  * ill_dls_info
4934  * creates datalink socket info from the device.
4935  */
4936 int
4937 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4938 {
4939 	size_t	length;
4940 	ill_t	*ill = ipif->ipif_ill;
4941 
4942 	sdl->sdl_family = AF_LINK;
4943 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4944 	sdl->sdl_type = ipif->ipif_type;
4945 	(void) ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4946 	length = mi_strlen(sdl->sdl_data);
4947 	ASSERT(length < 256);
4948 	sdl->sdl_nlen = (uchar_t)length;
4949 	sdl->sdl_alen = ill->ill_phys_addr_length;
4950 	mutex_enter(&ill->ill_lock);
4951 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL) {
4952 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[length],
4953 		    ill->ill_phys_addr_length);
4954 	}
4955 	mutex_exit(&ill->ill_lock);
4956 	sdl->sdl_slen = 0;
4957 	return (sizeof (struct sockaddr_dl));
4958 }
4959 
4960 /*
4961  * ill_xarp_info
4962  * creates xarp info from the device.
4963  */
4964 static int
4965 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4966 {
4967 	sdl->sdl_family = AF_LINK;
4968 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4969 	sdl->sdl_type = ill->ill_type;
4970 	(void) ipif_get_name(ill->ill_ipif, sdl->sdl_data,
4971 	    sizeof (sdl->sdl_data));
4972 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4973 	sdl->sdl_alen = ill->ill_phys_addr_length;
4974 	sdl->sdl_slen = 0;
4975 	return (sdl->sdl_nlen);
4976 }
4977 
4978 static int
4979 loopback_kstat_update(kstat_t *ksp, int rw)
4980 {
4981 	kstat_named_t *kn = KSTAT_NAMED_PTR(ksp);
4982 
4983 	if (rw == KSTAT_WRITE)
4984 		return (EACCES);
4985 	kn[0].value.ui32 = loopback_packets;
4986 	kn[1].value.ui32 = loopback_packets;
4987 	return (0);
4988 }
4989 
4990 
4991 /*
4992  * Has ifindex been plumbed already.
4993  */
4994 static boolean_t
4995 phyint_exists(uint_t index)
4996 {
4997 	phyint_t *phyi;
4998 
4999 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
5000 	/*
5001 	 * Indexes are stored in the phyint - a common structure
5002 	 * to both IPv4 and IPv6.
5003 	 */
5004 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
5005 	    (void *) &index, NULL);
5006 	return (phyi != NULL);
5007 }
5008 
5009 /*
5010  * Assign a unique interface index for the phyint.
5011  */
5012 static boolean_t
5013 phyint_assign_ifindex(phyint_t *phyi)
5014 {
5015 	uint_t starting_index;
5016 
5017 	ASSERT(phyi->phyint_ifindex == 0);
5018 	if (!ill_index_wrap) {
5019 		phyi->phyint_ifindex = ill_index++;
5020 		if (ill_index == 0) {
5021 			/* Reached the uint_t limit Next time wrap  */
5022 			ill_index_wrap = B_TRUE;
5023 		}
5024 		return (B_TRUE);
5025 	}
5026 
5027 	/*
5028 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
5029 	 * at this point and don't want to call any function that attempts
5030 	 * to get the lock again.
5031 	 */
5032 	starting_index = ill_index++;
5033 	for (; ill_index != starting_index; ill_index++) {
5034 		if (ill_index != 0 && !phyint_exists(ill_index)) {
5035 			/* found unused index - use it */
5036 			phyi->phyint_ifindex = ill_index;
5037 			return (B_TRUE);
5038 		}
5039 	}
5040 
5041 	/*
5042 	 * all interface indicies are inuse.
5043 	 */
5044 	return (B_FALSE);
5045 }
5046 
5047 /*
5048  * Return a pointer to the ill which matches the supplied name.  Note that
5049  * the ill name length includes the null termination character.  (May be
5050  * called as writer.)
5051  * If do_alloc and the interface is "lo0" it will be automatically created.
5052  * Cannot bump up reference on condemned ills. So dup detect can't be done
5053  * using this func.
5054  */
5055 ill_t *
5056 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
5057     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc)
5058 {
5059 	ill_t	*ill;
5060 	ipif_t	*ipif;
5061 	kstat_named_t	*kn;
5062 	boolean_t isloopback;
5063 	ipsq_t *old_ipsq;
5064 
5065 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
5066 
5067 	rw_enter(&ill_g_lock, RW_READER);
5068 	ill = ill_find_by_name(name, isv6, q, mp, func, error);
5069 	rw_exit(&ill_g_lock);
5070 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
5071 		return (ill);
5072 
5073 	/*
5074 	 * Couldn't find it.  Does this happen to be a lookup for the
5075 	 * loopback device and are we allowed to allocate it?
5076 	 */
5077 	if (!isloopback || !do_alloc)
5078 		return (NULL);
5079 
5080 	rw_enter(&ill_g_lock, RW_WRITER);
5081 
5082 	ill = ill_find_by_name(name, isv6, q, mp, func, error);
5083 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
5084 		rw_exit(&ill_g_lock);
5085 		return (ill);
5086 	}
5087 
5088 	/* Create the loopback device on demand */
5089 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
5090 	    sizeof (ipif_loopback_name), BPRI_MED));
5091 	if (ill == NULL)
5092 		goto done;
5093 
5094 	*ill = ill_null;
5095 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
5096 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
5097 	if (ill->ill_phyint == NULL)
5098 		goto done;
5099 
5100 	if (isv6)
5101 		ill->ill_phyint->phyint_illv6 = ill;
5102 	else
5103 		ill->ill_phyint->phyint_illv4 = ill;
5104 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
5105 	ill->ill_max_frag = IP_LOOPBACK_MTU;
5106 	/* Add room for tcp+ip headers */
5107 	if (isv6) {
5108 		ill->ill_isv6 = B_TRUE;
5109 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
5110 	} else {
5111 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
5112 	}
5113 	if (!ill_allocate_mibs(ill))
5114 		goto done;
5115 	ill->ill_max_mtu = ill->ill_max_frag;
5116 	/*
5117 	 * ipif_loopback_name can't be pointed at directly because its used
5118 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
5119 	 * from the glist, ill_glist_delete() sets the first character of
5120 	 * ill_name to '\0'.
5121 	 */
5122 	ill->ill_name = (char *)ill + sizeof (*ill);
5123 	(void) strcpy(ill->ill_name, ipif_loopback_name);
5124 	ill->ill_name_length = sizeof (ipif_loopback_name);
5125 	/* Set ill_name_set for ill_phyint_reinit to work properly */
5126 
5127 	ill->ill_global_timer = INFINITY;
5128 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
5129 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
5130 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
5131 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
5132 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
5133 
5134 	/* No resolver here. */
5135 	ill->ill_net_type = IRE_LOOPBACK;
5136 
5137 	/* Initialize the ipsq */
5138 	if (!ipsq_init(ill))
5139 		goto done;
5140 
5141 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
5142 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
5143 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
5144 #ifdef ILL_DEBUG
5145 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
5146 #endif
5147 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
5148 	if (ipif == NULL)
5149 		goto done;
5150 
5151 	ill->ill_flags = ILLF_MULTICAST;
5152 
5153 	/* Set up default loopback address and mask. */
5154 	if (!isv6) {
5155 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5156 
5157 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5158 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5159 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5160 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5161 		    ipif->ipif_v6subnet);
5162 		ill->ill_flags |= ILLF_IPV4;
5163 	} else {
5164 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5165 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5166 		ipif->ipif_v6net_mask = ipv6_all_ones;
5167 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5168 		    ipif->ipif_v6subnet);
5169 		ill->ill_flags |= ILLF_IPV6;
5170 	}
5171 
5172 	/*
5173 	 * Chain us in at the end of the ill list. hold the ill
5174 	 * before we make it globally visible. 1 for the lookup.
5175 	 */
5176 	ill->ill_refcnt = 0;
5177 	ill_refhold(ill);
5178 
5179 	ill->ill_frag_count = 0;
5180 	ill->ill_frag_free_num_pkts = 0;
5181 	ill->ill_last_frag_clean_time = 0;
5182 
5183 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5184 
5185 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5186 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5187 
5188 	/* Let SCTP know so that it can add this to its list */
5189 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5190 
5191 	/* Let SCTP know about this IPIF, so that it can add it to its list */
5192 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
5193 
5194 	/*
5195 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5196 	 */
5197 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5198 		/* Loopback ills aren't in any IPMP group */
5199 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5200 		ipsq_delete(old_ipsq);
5201 	}
5202 
5203 	/*
5204 	 * Delay this till the ipif is allocated as ipif_allocate
5205 	 * de-references ill_phyint for getting the ifindex. We
5206 	 * can't do this before ipif_allocate because ill_phyint_reinit
5207 	 * -> phyint_assign_ifindex expects ipif to be present.
5208 	 */
5209 	mutex_enter(&ill->ill_phyint->phyint_lock);
5210 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5211 	mutex_exit(&ill->ill_phyint->phyint_lock);
5212 
5213 	if (loopback_ksp == NULL) {
5214 		/* Export loopback interface statistics */
5215 		loopback_ksp = kstat_create("lo", 0, ipif_loopback_name, "net",
5216 		    KSTAT_TYPE_NAMED, 2, 0);
5217 		if (loopback_ksp != NULL) {
5218 			loopback_ksp->ks_update = loopback_kstat_update;
5219 			kn = KSTAT_NAMED_PTR(loopback_ksp);
5220 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5221 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5222 			kstat_install(loopback_ksp);
5223 		}
5224 	}
5225 
5226 	if (error != NULL)
5227 		*error = 0;
5228 	*did_alloc = B_TRUE;
5229 	rw_exit(&ill_g_lock);
5230 	return (ill);
5231 done:
5232 	if (ill != NULL) {
5233 		if (ill->ill_phyint != NULL) {
5234 			ipsq_t	*ipsq;
5235 
5236 			ipsq = ill->ill_phyint->phyint_ipsq;
5237 			if (ipsq != NULL)
5238 				kmem_free(ipsq, sizeof (ipsq_t));
5239 			mi_free(ill->ill_phyint);
5240 		}
5241 		ill_free_mib(ill);
5242 		mi_free(ill);
5243 	}
5244 	rw_exit(&ill_g_lock);
5245 	if (error != NULL)
5246 		*error = ENOMEM;
5247 	return (NULL);
5248 }
5249 
5250 /*
5251  * Return a pointer to the ill which matches the index and IP version type.
5252  */
5253 ill_t *
5254 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5255     ipsq_func_t func, int *err)
5256 {
5257 	ill_t	*ill;
5258 	ipsq_t  *ipsq;
5259 	phyint_t *phyi;
5260 
5261 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5262 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5263 
5264 	if (err != NULL)
5265 		*err = 0;
5266 
5267 	/*
5268 	 * Indexes are stored in the phyint - a common structure
5269 	 * to both IPv4 and IPv6.
5270 	 */
5271 	rw_enter(&ill_g_lock, RW_READER);
5272 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
5273 	    (void *) &index, NULL);
5274 	if (phyi != NULL) {
5275 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5276 		if (ill != NULL) {
5277 			/*
5278 			 * The block comment at the start of ipif_down
5279 			 * explains the use of the macros used below
5280 			 */
5281 			GRAB_CONN_LOCK(q);
5282 			mutex_enter(&ill->ill_lock);
5283 			if (ILL_CAN_LOOKUP(ill)) {
5284 				ill_refhold_locked(ill);
5285 				mutex_exit(&ill->ill_lock);
5286 				RELEASE_CONN_LOCK(q);
5287 				rw_exit(&ill_g_lock);
5288 				return (ill);
5289 			} else if (ILL_CAN_WAIT(ill, q)) {
5290 				ipsq = ill->ill_phyint->phyint_ipsq;
5291 				mutex_enter(&ipsq->ipsq_lock);
5292 				rw_exit(&ill_g_lock);
5293 				mutex_exit(&ill->ill_lock);
5294 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5295 				mutex_exit(&ipsq->ipsq_lock);
5296 				RELEASE_CONN_LOCK(q);
5297 				*err = EINPROGRESS;
5298 				return (NULL);
5299 			}
5300 			RELEASE_CONN_LOCK(q);
5301 			mutex_exit(&ill->ill_lock);
5302 		}
5303 	}
5304 	rw_exit(&ill_g_lock);
5305 	if (err != NULL)
5306 		*err = ENXIO;
5307 	return (NULL);
5308 }
5309 
5310 /*
5311  * Return the ifindex next in sequence after the passed in ifindex.
5312  * If there is no next ifindex for the given protocol, return 0.
5313  */
5314 uint_t
5315 ill_get_next_ifindex(uint_t index, boolean_t isv6)
5316 {
5317 	phyint_t *phyi;
5318 	phyint_t *phyi_initial;
5319 	uint_t   ifindex;
5320 
5321 	rw_enter(&ill_g_lock, RW_READER);
5322 
5323 	if (index == 0) {
5324 		phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
5325 	} else {
5326 		phyi = phyi_initial = avl_find(
5327 		    &phyint_g_list.phyint_list_avl_by_index,
5328 		    (void *) &index, NULL);
5329 	}
5330 
5331 	for (; phyi != NULL;
5332 	    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
5333 	    phyi, AVL_AFTER)) {
5334 		/*
5335 		 * If we're not returning the first interface in the tree
5336 		 * and we still haven't moved past the phyint_t that
5337 		 * corresponds to index, avl_walk needs to be called again
5338 		 */
5339 		if (!((index != 0) && (phyi == phyi_initial))) {
5340 			if (isv6) {
5341 				if ((phyi->phyint_illv6) &&
5342 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5343 				    (phyi->phyint_illv6->ill_isv6 == 1))
5344 					break;
5345 			} else {
5346 				if ((phyi->phyint_illv4) &&
5347 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5348 				    (phyi->phyint_illv4->ill_isv6 == 0))
5349 					break;
5350 			}
5351 		}
5352 	}
5353 
5354 	rw_exit(&ill_g_lock);
5355 
5356 	if (phyi != NULL)
5357 		ifindex = phyi->phyint_ifindex;
5358 	else
5359 		ifindex = 0;
5360 
5361 	return (ifindex);
5362 }
5363 
5364 
5365 /*
5366  * Return the ifindex for the named interface.
5367  * If there is no next ifindex for the interface, return 0.
5368  */
5369 uint_t
5370 ill_get_ifindex_by_name(char *name)
5371 {
5372 	phyint_t	*phyi;
5373 	avl_index_t	where = 0;
5374 	uint_t		ifindex;
5375 
5376 	rw_enter(&ill_g_lock, RW_READER);
5377 
5378 	if ((phyi = avl_find(&phyint_g_list.phyint_list_avl_by_name,
5379 	    name, &where)) == NULL) {
5380 		rw_exit(&ill_g_lock);
5381 		return (0);
5382 	}
5383 
5384 	ifindex = phyi->phyint_ifindex;
5385 
5386 	rw_exit(&ill_g_lock);
5387 
5388 	return (ifindex);
5389 }
5390 
5391 
5392 /*
5393  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5394  * that gives a running thread a reference to the ill. This reference must be
5395  * released by the thread when it is done accessing the ill and related
5396  * objects. ill_refcnt can not be used to account for static references
5397  * such as other structures pointing to an ill. Callers must generally
5398  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5399  * or be sure that the ill is not being deleted or changing state before
5400  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5401  * ill won't change any of its critical state such as address, netmask etc.
5402  */
5403 void
5404 ill_refhold(ill_t *ill)
5405 {
5406 	mutex_enter(&ill->ill_lock);
5407 	ill->ill_refcnt++;
5408 	ILL_TRACE_REF(ill);
5409 	mutex_exit(&ill->ill_lock);
5410 }
5411 
5412 void
5413 ill_refhold_locked(ill_t *ill)
5414 {
5415 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5416 	ill->ill_refcnt++;
5417 	ILL_TRACE_REF(ill);
5418 }
5419 
5420 int
5421 ill_check_and_refhold(ill_t *ill)
5422 {
5423 	mutex_enter(&ill->ill_lock);
5424 	if (ILL_CAN_LOOKUP(ill)) {
5425 		ill_refhold_locked(ill);
5426 		mutex_exit(&ill->ill_lock);
5427 		return (0);
5428 	}
5429 	mutex_exit(&ill->ill_lock);
5430 	return (ILL_LOOKUP_FAILED);
5431 }
5432 
5433 /*
5434  * Must not be called while holding any locks. Otherwise if this is
5435  * the last reference to be released, there is a chance of recursive mutex
5436  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5437  * to restart an ioctl.
5438  */
5439 void
5440 ill_refrele(ill_t *ill)
5441 {
5442 	mutex_enter(&ill->ill_lock);
5443 	ASSERT(ill->ill_refcnt != 0);
5444 	ill->ill_refcnt--;
5445 	ILL_UNTRACE_REF(ill);
5446 	if (ill->ill_refcnt != 0) {
5447 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5448 		mutex_exit(&ill->ill_lock);
5449 		return;
5450 	}
5451 
5452 	/* Drops the ill_lock */
5453 	ipif_ill_refrele_tail(ill);
5454 }
5455 
5456 /*
5457  * Obtain a weak reference count on the ill. This reference ensures the
5458  * ill won't be freed, but the ill may change any of its critical state
5459  * such as netmask, address etc. Returns an error if the ill has started
5460  * closing.
5461  */
5462 boolean_t
5463 ill_waiter_inc(ill_t *ill)
5464 {
5465 	mutex_enter(&ill->ill_lock);
5466 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5467 		mutex_exit(&ill->ill_lock);
5468 		return (B_FALSE);
5469 	}
5470 	ill->ill_waiters++;
5471 	mutex_exit(&ill->ill_lock);
5472 	return (B_TRUE);
5473 }
5474 
5475 void
5476 ill_waiter_dcr(ill_t *ill)
5477 {
5478 	mutex_enter(&ill->ill_lock);
5479 	ill->ill_waiters--;
5480 	if (ill->ill_waiters == 0)
5481 		cv_broadcast(&ill->ill_cv);
5482 	mutex_exit(&ill->ill_lock);
5483 }
5484 
5485 /*
5486  * Named Dispatch routine to produce a formatted report on all ILLs.
5487  * This report is accessed by using the ndd utility to "get" ND variable
5488  * "ip_ill_status".
5489  */
5490 /* ARGSUSED */
5491 int
5492 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5493 {
5494 	ill_t		*ill;
5495 	ill_walk_context_t ctx;
5496 
5497 	(void) mi_mpprintf(mp,
5498 	    "ILL      " MI_COL_HDRPAD_STR
5499 	/*   01234567[89ABCDEF] */
5500 	    "rq       " MI_COL_HDRPAD_STR
5501 	/*   01234567[89ABCDEF] */
5502 	    "wq       " MI_COL_HDRPAD_STR
5503 	/*   01234567[89ABCDEF] */
5504 	    "upcnt mxfrg err name");
5505 	/*   12345 12345 123 xxxxxxxx  */
5506 
5507 	rw_enter(&ill_g_lock, RW_READER);
5508 	ill = ILL_START_WALK_ALL(&ctx);
5509 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5510 		(void) mi_mpprintf(mp,
5511 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5512 		    "%05u %05u %03d %s",
5513 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5514 		    ill->ill_ipif_up_count,
5515 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5516 	}
5517 	rw_exit(&ill_g_lock);
5518 
5519 	return (0);
5520 }
5521 
5522 /*
5523  * Named Dispatch routine to produce a formatted report on all IPIFs.
5524  * This report is accessed by using the ndd utility to "get" ND variable
5525  * "ip_ipif_status".
5526  */
5527 /* ARGSUSED */
5528 int
5529 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5530 {
5531 	char	buf1[INET6_ADDRSTRLEN];
5532 	char	buf2[INET6_ADDRSTRLEN];
5533 	char	buf3[INET6_ADDRSTRLEN];
5534 	char	buf4[INET6_ADDRSTRLEN];
5535 	char	buf5[INET6_ADDRSTRLEN];
5536 	char	buf6[INET6_ADDRSTRLEN];
5537 	char	buf[LIFNAMSIZ];
5538 	ill_t	*ill;
5539 	ipif_t	*ipif;
5540 	nv_t	*nvp;
5541 	uint64_t flags;
5542 	zoneid_t zoneid;
5543 	ill_walk_context_t ctx;
5544 
5545 	(void) mi_mpprintf(mp,
5546 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5547 	    "\tlocal address\n"
5548 	    "\tsrc address\n"
5549 	    "\tsubnet\n"
5550 	    "\tmask\n"
5551 	    "\tbroadcast\n"
5552 	    "\tp-p-dst");
5553 
5554 	ASSERT(q->q_next == NULL);
5555 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5556 
5557 	rw_enter(&ill_g_lock, RW_READER);
5558 	ill = ILL_START_WALK_ALL(&ctx);
5559 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5560 		for (ipif = ill->ill_ipif; ipif != NULL;
5561 		    ipif = ipif->ipif_next) {
5562 			if (zoneid != GLOBAL_ZONEID &&
5563 			    zoneid != ipif->ipif_zoneid &&
5564 			    ipif->ipif_zoneid != ALL_ZONES)
5565 				continue;
5566 			(void) mi_mpprintf(mp,
5567 			    MI_COL_PTRFMT_STR
5568 			    "%04u %05u %u/%u/%u %s %d",
5569 			    (void *)ipif,
5570 			    ipif->ipif_metric, ipif->ipif_mtu,
5571 			    ipif->ipif_ib_pkt_count,
5572 			    ipif->ipif_ob_pkt_count,
5573 			    ipif->ipif_fo_pkt_count,
5574 			    ipif_get_name(ipif, buf, sizeof (buf)),
5575 			    ipif->ipif_zoneid);
5576 
5577 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5578 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5579 
5580 		/* Tack on text strings for any flags. */
5581 		nvp = ipif_nv_tbl;
5582 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5583 			if (nvp->nv_value & flags)
5584 				(void) mi_mpprintf_nr(mp, " %s",
5585 				    nvp->nv_name);
5586 		}
5587 		(void) mi_mpprintf(mp,
5588 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5589 		    inet_ntop(AF_INET6,
5590 			&ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5591 		    inet_ntop(AF_INET6,
5592 			&ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5593 		    inet_ntop(AF_INET6,
5594 			&ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5595 		    inet_ntop(AF_INET6,
5596 			&ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5597 		    inet_ntop(AF_INET6,
5598 			&ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5599 		    inet_ntop(AF_INET6,
5600 			&ipif->ipif_v6pp_dst_addr,
5601 			buf6, sizeof (buf6)));
5602 		}
5603 	}
5604 	rw_exit(&ill_g_lock);
5605 	return (0);
5606 }
5607 
5608 /*
5609  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5610  * driver.  We construct best guess defaults for lower level information that
5611  * we need.  If an interface is brought up without injection of any overriding
5612  * information from outside, we have to be ready to go with these defaults.
5613  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5614  * we primarely want the dl_provider_style.
5615  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5616  * at which point we assume the other part of the information is valid.
5617  */
5618 void
5619 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5620 {
5621 	uchar_t		*brdcst_addr;
5622 	uint_t		brdcst_addr_length, phys_addr_length;
5623 	t_scalar_t	sap_length;
5624 	dl_info_ack_t	*dlia;
5625 	ip_m_t		*ipm;
5626 	dl_qos_cl_sel1_t *sel1;
5627 
5628 	ASSERT(IAM_WRITER_ILL(ill));
5629 
5630 	/*
5631 	 * Till the ill is fully up ILL_CHANGING will be set and
5632 	 * the ill is not globally visible. So no need for a lock.
5633 	 */
5634 	dlia = (dl_info_ack_t *)mp->b_rptr;
5635 	ill->ill_mactype = dlia->dl_mac_type;
5636 
5637 	ipm = ip_m_lookup(dlia->dl_mac_type);
5638 	if (ipm == NULL) {
5639 		ipm = ip_m_lookup(DL_OTHER);
5640 		ASSERT(ipm != NULL);
5641 	}
5642 	ill->ill_media = ipm;
5643 
5644 	/*
5645 	 * When the new DLPI stuff is ready we'll pull lengths
5646 	 * from dlia.
5647 	 */
5648 	if (dlia->dl_version == DL_VERSION_2) {
5649 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5650 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5651 		    brdcst_addr_length);
5652 		if (brdcst_addr == NULL) {
5653 			brdcst_addr_length = 0;
5654 		}
5655 		sap_length = dlia->dl_sap_length;
5656 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5657 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5658 		    brdcst_addr_length, sap_length, phys_addr_length));
5659 	} else {
5660 		brdcst_addr_length = 6;
5661 		brdcst_addr = ip_six_byte_all_ones;
5662 		sap_length = -2;
5663 		phys_addr_length = brdcst_addr_length;
5664 	}
5665 
5666 	ill->ill_bcast_addr_length = brdcst_addr_length;
5667 	ill->ill_phys_addr_length = phys_addr_length;
5668 	ill->ill_sap_length = sap_length;
5669 	ill->ill_max_frag = dlia->dl_max_sdu;
5670 	ill->ill_max_mtu = ill->ill_max_frag;
5671 
5672 	ill->ill_type = ipm->ip_m_type;
5673 
5674 	if (!ill->ill_dlpi_style_set) {
5675 		if (dlia->dl_provider_style == DL_STYLE2)
5676 			ill->ill_needs_attach = 1;
5677 
5678 		/*
5679 		 * Allocate the first ipif on this ill. We don't delay it
5680 		 * further as ioctl handling assumes atleast one ipif to
5681 		 * be present.
5682 		 *
5683 		 * At this point we don't know whether the ill is v4 or v6.
5684 		 * We will know this whan the SIOCSLIFNAME happens and
5685 		 * the correct value for ill_isv6 will be assigned in
5686 		 * ipif_set_values(). We need to hold the ill lock and
5687 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5688 		 * the wakeup.
5689 		 */
5690 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5691 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5692 		mutex_enter(&ill->ill_lock);
5693 		ASSERT(ill->ill_dlpi_style_set == 0);
5694 		ill->ill_dlpi_style_set = 1;
5695 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5696 		cv_broadcast(&ill->ill_cv);
5697 		mutex_exit(&ill->ill_lock);
5698 		freemsg(mp);
5699 		return;
5700 	}
5701 	ASSERT(ill->ill_ipif != NULL);
5702 	/*
5703 	 * We know whether it is IPv4 or IPv6 now, as this is the
5704 	 * second DL_INFO_ACK we are recieving in response to the
5705 	 * DL_INFO_REQ sent in ipif_set_values.
5706 	 */
5707 	if (ill->ill_isv6)
5708 		ill->ill_sap = IP6_DL_SAP;
5709 	else
5710 		ill->ill_sap = IP_DL_SAP;
5711 	/*
5712 	 * Set ipif_mtu which is used to set the IRE's
5713 	 * ire_max_frag value. The driver could have sent
5714 	 * a different mtu from what it sent last time. No
5715 	 * need to call ipif_mtu_change because IREs have
5716 	 * not yet been created.
5717 	 */
5718 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5719 	/*
5720 	 * Clear all the flags that were set based on ill_bcast_addr_length
5721 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5722 	 * changed now and we need to re-evaluate.
5723 	 */
5724 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5725 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5726 
5727 	/*
5728 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5729 	 * changed now.
5730 	 */
5731 	if (ill->ill_bcast_addr_length == 0) {
5732 		if (ill->ill_resolver_mp != NULL)
5733 			freemsg(ill->ill_resolver_mp);
5734 		if (ill->ill_bcast_mp != NULL)
5735 			freemsg(ill->ill_bcast_mp);
5736 		if (ill->ill_flags & ILLF_XRESOLV)
5737 			ill->ill_net_type = IRE_IF_RESOLVER;
5738 		else
5739 			ill->ill_net_type = IRE_IF_NORESOLVER;
5740 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5741 		    ill->ill_phys_addr_length,
5742 		    ill->ill_sap,
5743 		    ill->ill_sap_length);
5744 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5745 
5746 		if (ill->ill_isv6)
5747 			/*
5748 			 * Note: xresolv interfaces will eventually need NOARP
5749 			 * set here as well, but that will require those
5750 			 * external resolvers to have some knowledge of
5751 			 * that flag and act appropriately. Not to be changed
5752 			 * at present.
5753 			 */
5754 			ill->ill_flags |= ILLF_NONUD;
5755 		else
5756 			ill->ill_flags |= ILLF_NOARP;
5757 
5758 		if (ill->ill_phys_addr_length == 0) {
5759 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5760 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5761 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5762 			} else {
5763 				/* pt-pt supports multicast. */
5764 				ill->ill_flags |= ILLF_MULTICAST;
5765 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5766 			}
5767 		}
5768 	} else {
5769 		ill->ill_net_type = IRE_IF_RESOLVER;
5770 		if (ill->ill_bcast_mp != NULL)
5771 			freemsg(ill->ill_bcast_mp);
5772 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5773 		    ill->ill_bcast_addr_length, ill->ill_sap,
5774 		    ill->ill_sap_length);
5775 		/*
5776 		 * Later detect lack of DLPI driver multicast
5777 		 * capability by catching DL_ENABMULTI errors in
5778 		 * ip_rput_dlpi.
5779 		 */
5780 		ill->ill_flags |= ILLF_MULTICAST;
5781 		if (!ill->ill_isv6)
5782 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5783 	}
5784 	/* By default an interface does not support any CoS marking */
5785 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5786 
5787 	/*
5788 	 * If we get QoS information in DL_INFO_ACK, the device supports
5789 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5790 	 */
5791 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5792 	    dlia->dl_qos_length);
5793 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5794 		ill->ill_flags |= ILLF_COS_ENABLED;
5795 	}
5796 
5797 	/* Clear any previous error indication. */
5798 	ill->ill_error = 0;
5799 	freemsg(mp);
5800 }
5801 
5802 /*
5803  * Perform various checks to verify that an address would make sense as a
5804  * local, remote, or subnet interface address.
5805  */
5806 static boolean_t
5807 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5808 {
5809 	ipaddr_t	net_mask;
5810 
5811 	/*
5812 	 * Don't allow all zeroes, all ones or experimental address, but allow
5813 	 * all ones netmask.
5814 	 */
5815 	if ((net_mask = ip_net_mask(addr)) == 0)
5816 		return (B_FALSE);
5817 	/* A given netmask overrides the "guess" netmask */
5818 	if (subnet_mask != 0)
5819 		net_mask = subnet_mask;
5820 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5821 	    (addr == (addr | ~net_mask)))) {
5822 		return (B_FALSE);
5823 	}
5824 	if (CLASSD(addr))
5825 		return (B_FALSE);
5826 
5827 	return (B_TRUE);
5828 }
5829 
5830 /*
5831  * ipif_lookup_group
5832  * Returns held ipif
5833  */
5834 ipif_t *
5835 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid)
5836 {
5837 	ire_t	*ire;
5838 	ipif_t	*ipif;
5839 
5840 	ire = ire_lookup_multi(group, zoneid);
5841 	if (ire == NULL)
5842 		return (NULL);
5843 	ipif = ire->ire_ipif;
5844 	ipif_refhold(ipif);
5845 	ire_refrele(ire);
5846 	return (ipif);
5847 }
5848 
5849 /*
5850  * Look for an ipif with the specified interface address and destination.
5851  * The destination address is used only for matching point-to-point interfaces.
5852  */
5853 ipif_t *
5854 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5855     ipsq_func_t func, int *error)
5856 {
5857 	ipif_t	*ipif;
5858 	ill_t	*ill;
5859 	ill_walk_context_t ctx;
5860 	ipsq_t	*ipsq;
5861 
5862 	if (error != NULL)
5863 		*error = 0;
5864 
5865 	/*
5866 	 * First match all the point-to-point interfaces
5867 	 * before looking at non-point-to-point interfaces.
5868 	 * This is done to avoid returning non-point-to-point
5869 	 * ipif instead of unnumbered point-to-point ipif.
5870 	 */
5871 	rw_enter(&ill_g_lock, RW_READER);
5872 	ill = ILL_START_WALK_V4(&ctx);
5873 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5874 		GRAB_CONN_LOCK(q);
5875 		mutex_enter(&ill->ill_lock);
5876 		for (ipif = ill->ill_ipif; ipif != NULL;
5877 		    ipif = ipif->ipif_next) {
5878 			/* Allow the ipif to be down */
5879 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5880 			    (ipif->ipif_lcl_addr == if_addr) &&
5881 			    (ipif->ipif_pp_dst_addr == dst)) {
5882 				/*
5883 				 * The block comment at the start of ipif_down
5884 				 * explains the use of the macros used below
5885 				 */
5886 				if (IPIF_CAN_LOOKUP(ipif)) {
5887 					ipif_refhold_locked(ipif);
5888 					mutex_exit(&ill->ill_lock);
5889 					RELEASE_CONN_LOCK(q);
5890 					rw_exit(&ill_g_lock);
5891 					return (ipif);
5892 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5893 					ipsq = ill->ill_phyint->phyint_ipsq;
5894 					mutex_enter(&ipsq->ipsq_lock);
5895 					mutex_exit(&ill->ill_lock);
5896 					rw_exit(&ill_g_lock);
5897 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5898 						ill);
5899 					mutex_exit(&ipsq->ipsq_lock);
5900 					RELEASE_CONN_LOCK(q);
5901 					*error = EINPROGRESS;
5902 					return (NULL);
5903 				}
5904 			}
5905 		}
5906 		mutex_exit(&ill->ill_lock);
5907 		RELEASE_CONN_LOCK(q);
5908 	}
5909 	rw_exit(&ill_g_lock);
5910 
5911 	/* lookup the ipif based on interface address */
5912 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error);
5913 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5914 	return (ipif);
5915 }
5916 
5917 /*
5918  * Look for an ipif with the specified address. For point-point links
5919  * we look for matches on either the destination address and the local
5920  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5921  * is set.
5922  * Matches on a specific ill if match_ill is set.
5923  */
5924 ipif_t *
5925 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5926     mblk_t *mp, ipsq_func_t func, int *error)
5927 {
5928 	ipif_t  *ipif;
5929 	ill_t   *ill;
5930 	boolean_t ptp = B_FALSE;
5931 	ipsq_t	*ipsq;
5932 	ill_walk_context_t	ctx;
5933 
5934 	if (error != NULL)
5935 		*error = 0;
5936 
5937 	rw_enter(&ill_g_lock, RW_READER);
5938 	/*
5939 	 * Repeat twice, first based on local addresses and
5940 	 * next time for pointopoint.
5941 	 */
5942 repeat:
5943 	ill = ILL_START_WALK_V4(&ctx);
5944 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5945 		if (match_ill != NULL && ill != match_ill) {
5946 			continue;
5947 		}
5948 		GRAB_CONN_LOCK(q);
5949 		mutex_enter(&ill->ill_lock);
5950 		for (ipif = ill->ill_ipif; ipif != NULL;
5951 		    ipif = ipif->ipif_next) {
5952 			if (zoneid != ALL_ZONES &&
5953 			    zoneid != ipif->ipif_zoneid &&
5954 			    ipif->ipif_zoneid != ALL_ZONES)
5955 				continue;
5956 			/* Allow the ipif to be down */
5957 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5958 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5959 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5960 			    (ipif->ipif_pp_dst_addr == addr))) {
5961 				/*
5962 				 * The block comment at the start of ipif_down
5963 				 * explains the use of the macros used below
5964 				 */
5965 				if (IPIF_CAN_LOOKUP(ipif)) {
5966 					ipif_refhold_locked(ipif);
5967 					mutex_exit(&ill->ill_lock);
5968 					RELEASE_CONN_LOCK(q);
5969 					rw_exit(&ill_g_lock);
5970 					return (ipif);
5971 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5972 					ipsq = ill->ill_phyint->phyint_ipsq;
5973 					mutex_enter(&ipsq->ipsq_lock);
5974 					mutex_exit(&ill->ill_lock);
5975 					rw_exit(&ill_g_lock);
5976 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5977 						ill);
5978 					mutex_exit(&ipsq->ipsq_lock);
5979 					RELEASE_CONN_LOCK(q);
5980 					*error = EINPROGRESS;
5981 					return (NULL);
5982 				}
5983 			}
5984 		}
5985 		mutex_exit(&ill->ill_lock);
5986 		RELEASE_CONN_LOCK(q);
5987 	}
5988 
5989 	/* If we already did the ptp case, then we are done */
5990 	if (ptp) {
5991 		rw_exit(&ill_g_lock);
5992 		if (error != NULL)
5993 			*error = ENXIO;
5994 		return (NULL);
5995 	}
5996 	ptp = B_TRUE;
5997 	goto repeat;
5998 }
5999 
6000 /*
6001  * Look for an ipif with the specified address. For point-point links
6002  * we look for matches on either the destination address and the local
6003  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6004  * is set.
6005  * Matches on a specific ill if match_ill is set.
6006  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6007  */
6008 zoneid_t
6009 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill)
6010 {
6011 	zoneid_t zoneid;
6012 	ipif_t  *ipif;
6013 	ill_t   *ill;
6014 	boolean_t ptp = B_FALSE;
6015 	ill_walk_context_t	ctx;
6016 
6017 	rw_enter(&ill_g_lock, RW_READER);
6018 	/*
6019 	 * Repeat twice, first based on local addresses and
6020 	 * next time for pointopoint.
6021 	 */
6022 repeat:
6023 	ill = ILL_START_WALK_V4(&ctx);
6024 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6025 		if (match_ill != NULL && ill != match_ill) {
6026 			continue;
6027 		}
6028 		mutex_enter(&ill->ill_lock);
6029 		for (ipif = ill->ill_ipif; ipif != NULL;
6030 		    ipif = ipif->ipif_next) {
6031 			/* Allow the ipif to be down */
6032 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6033 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6034 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6035 			    (ipif->ipif_pp_dst_addr == addr)) &&
6036 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6037 				zoneid = ipif->ipif_zoneid;
6038 				mutex_exit(&ill->ill_lock);
6039 				rw_exit(&ill_g_lock);
6040 				/*
6041 				 * If ipif_zoneid was ALL_ZONES then we have
6042 				 * a trusted extensions shared IP address.
6043 				 * In that case GLOBAL_ZONEID works to send.
6044 				 */
6045 				if (zoneid == ALL_ZONES)
6046 					zoneid = GLOBAL_ZONEID;
6047 				return (zoneid);
6048 			}
6049 		}
6050 		mutex_exit(&ill->ill_lock);
6051 	}
6052 
6053 	/* If we already did the ptp case, then we are done */
6054 	if (ptp) {
6055 		rw_exit(&ill_g_lock);
6056 		return (ALL_ZONES);
6057 	}
6058 	ptp = B_TRUE;
6059 	goto repeat;
6060 }
6061 
6062 /*
6063  * Look for an ipif that matches the specified remote address i.e. the
6064  * ipif that would receive the specified packet.
6065  * First look for directly connected interfaces and then do a recursive
6066  * IRE lookup and pick the first ipif corresponding to the source address in the
6067  * ire.
6068  * Returns: held ipif
6069  */
6070 ipif_t *
6071 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6072 {
6073 	ipif_t	*ipif;
6074 	ire_t	*ire;
6075 
6076 	ASSERT(!ill->ill_isv6);
6077 
6078 	/*
6079 	 * Someone could be changing this ipif currently or change it
6080 	 * after we return this. Thus  a few packets could use the old
6081 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6082 	 * will atomically be updated or cleaned up with the new value
6083 	 * Thus we don't need a lock to check the flags or other attrs below.
6084 	 */
6085 	mutex_enter(&ill->ill_lock);
6086 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6087 		if (!IPIF_CAN_LOOKUP(ipif))
6088 			continue;
6089 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6090 		    ipif->ipif_zoneid != ALL_ZONES)
6091 			continue;
6092 		/* Allow the ipif to be down */
6093 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6094 			if ((ipif->ipif_pp_dst_addr == addr) ||
6095 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6096 			    ipif->ipif_lcl_addr == addr)) {
6097 				ipif_refhold_locked(ipif);
6098 				mutex_exit(&ill->ill_lock);
6099 				return (ipif);
6100 			}
6101 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6102 			ipif_refhold_locked(ipif);
6103 			mutex_exit(&ill->ill_lock);
6104 			return (ipif);
6105 		}
6106 	}
6107 	mutex_exit(&ill->ill_lock);
6108 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6109 	    NULL, MATCH_IRE_RECURSIVE);
6110 	if (ire != NULL) {
6111 		/*
6112 		 * The callers of this function wants to know the
6113 		 * interface on which they have to send the replies
6114 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6115 		 * derived from different ills, we really don't care
6116 		 * what we return here.
6117 		 */
6118 		ipif = ire->ire_ipif;
6119 		if (ipif != NULL) {
6120 			ipif_refhold(ipif);
6121 			ire_refrele(ire);
6122 			return (ipif);
6123 		}
6124 		ire_refrele(ire);
6125 	}
6126 	/* Pick the first interface */
6127 	ipif = ipif_get_next_ipif(NULL, ill);
6128 	return (ipif);
6129 }
6130 
6131 /*
6132  * This func does not prevent refcnt from increasing. But if
6133  * the caller has taken steps to that effect, then this func
6134  * can be used to determine whether the ill has become quiescent
6135  */
6136 boolean_t
6137 ill_is_quiescent(ill_t *ill)
6138 {
6139 	ipif_t	*ipif;
6140 
6141 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6142 
6143 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6144 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6145 			return (B_FALSE);
6146 		}
6147 	}
6148 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
6149 	    ill->ill_nce_cnt != 0 || ill->ill_srcif_refcnt != 0 ||
6150 	    ill->ill_mrtun_refcnt != 0) {
6151 		return (B_FALSE);
6152 	}
6153 	return (B_TRUE);
6154 }
6155 
6156 /*
6157  * This func does not prevent refcnt from increasing. But if
6158  * the caller has taken steps to that effect, then this func
6159  * can be used to determine whether the ipif has become quiescent
6160  */
6161 static boolean_t
6162 ipif_is_quiescent(ipif_t *ipif)
6163 {
6164 	ill_t *ill;
6165 
6166 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6167 
6168 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6169 		return (B_FALSE);
6170 	}
6171 
6172 	ill = ipif->ipif_ill;
6173 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6174 	    ill->ill_logical_down) {
6175 		return (B_TRUE);
6176 	}
6177 
6178 	/* This is the last ipif going down or being deleted on this ill */
6179 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
6180 		return (B_FALSE);
6181 	}
6182 
6183 	return (B_TRUE);
6184 }
6185 
6186 /*
6187  * This func does not prevent refcnt from increasing. But if
6188  * the caller has taken steps to that effect, then this func
6189  * can be used to determine whether the ipifs marked with IPIF_MOVING
6190  * have become quiescent and can be moved in a failover/failback.
6191  */
6192 static ipif_t *
6193 ill_quiescent_to_move(ill_t *ill)
6194 {
6195 	ipif_t  *ipif;
6196 
6197 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6198 
6199 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6200 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6201 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6202 				return (ipif);
6203 			}
6204 		}
6205 	}
6206 	return (NULL);
6207 }
6208 
6209 /*
6210  * The ipif/ill/ire has been refreled. Do the tail processing.
6211  * Determine if the ipif or ill in question has become quiescent and if so
6212  * wakeup close and/or restart any queued pending ioctl that is waiting
6213  * for the ipif_down (or ill_down)
6214  */
6215 void
6216 ipif_ill_refrele_tail(ill_t *ill)
6217 {
6218 	mblk_t	*mp;
6219 	conn_t	*connp;
6220 	ipsq_t	*ipsq;
6221 	ipif_t	*ipif;
6222 
6223 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6224 
6225 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6226 	    ill_is_quiescent(ill)) {
6227 		/* ill_close may be waiting */
6228 		cv_broadcast(&ill->ill_cv);
6229 	}
6230 
6231 	/* ipsq can't change because ill_lock  is held */
6232 	ipsq = ill->ill_phyint->phyint_ipsq;
6233 	if (ipsq->ipsq_waitfor == 0) {
6234 		/* Not waiting for anything, just return. */
6235 		mutex_exit(&ill->ill_lock);
6236 		return;
6237 	}
6238 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6239 		ipsq->ipsq_pending_ipif != NULL);
6240 	/*
6241 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6242 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6243 	 * be zero for restarting an ioctl that ends up downing the ill.
6244 	 */
6245 	ipif = ipsq->ipsq_pending_ipif;
6246 	if (ipif->ipif_ill != ill) {
6247 		/* The ioctl is pending on some other ill. */
6248 		mutex_exit(&ill->ill_lock);
6249 		return;
6250 	}
6251 
6252 	switch (ipsq->ipsq_waitfor) {
6253 	case IPIF_DOWN:
6254 	case IPIF_FREE:
6255 		if (!ipif_is_quiescent(ipif)) {
6256 			mutex_exit(&ill->ill_lock);
6257 			return;
6258 		}
6259 		break;
6260 
6261 	case ILL_DOWN:
6262 	case ILL_FREE:
6263 		/*
6264 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6265 		 * waits synchronously in ip_close, and no message is queued in
6266 		 * ipsq_pending_mp at all in this case
6267 		 */
6268 		if (!ill_is_quiescent(ill)) {
6269 			mutex_exit(&ill->ill_lock);
6270 			return;
6271 		}
6272 
6273 		break;
6274 
6275 	case ILL_MOVE_OK:
6276 		if (ill_quiescent_to_move(ill) != NULL) {
6277 			mutex_exit(&ill->ill_lock);
6278 			return;
6279 		}
6280 
6281 		break;
6282 	default:
6283 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6284 		    (void *)ipsq, ipsq->ipsq_waitfor);
6285 	}
6286 
6287 	/*
6288 	 * Incr refcnt for the qwriter_ip call below which
6289 	 * does a refrele
6290 	 */
6291 	ill_refhold_locked(ill);
6292 	mutex_exit(&ill->ill_lock);
6293 
6294 	mp = ipsq_pending_mp_get(ipsq, &connp);
6295 	ASSERT(mp != NULL);
6296 
6297 	switch (mp->b_datap->db_type) {
6298 	case M_ERROR:
6299 	case M_HANGUP:
6300 		(void) qwriter_ip(NULL, ill, ill->ill_rq, mp,
6301 		    ipif_all_down_tail, CUR_OP, B_TRUE);
6302 		return;
6303 
6304 	case M_IOCTL:
6305 	case M_IOCDATA:
6306 		(void) qwriter_ip(NULL, ill,
6307 		    (connp != NULL ? CONNP_TO_WQ(connp) : ill->ill_wq), mp,
6308 		    ip_reprocess_ioctl, CUR_OP, B_TRUE);
6309 		return;
6310 
6311 	default:
6312 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6313 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6314 	}
6315 }
6316 
6317 #ifdef ILL_DEBUG
6318 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6319 void
6320 th_trace_rrecord(th_trace_t *th_trace)
6321 {
6322 	tr_buf_t *tr_buf;
6323 	uint_t lastref;
6324 
6325 	lastref = th_trace->th_trace_lastref;
6326 	lastref++;
6327 	if (lastref == TR_BUF_MAX)
6328 		lastref = 0;
6329 	th_trace->th_trace_lastref = lastref;
6330 	tr_buf = &th_trace->th_trbuf[lastref];
6331 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, IP_STACK_DEPTH);
6332 }
6333 
6334 th_trace_t *
6335 th_trace_ipif_lookup(ipif_t *ipif)
6336 {
6337 	int bucket_id;
6338 	th_trace_t *th_trace;
6339 
6340 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6341 
6342 	bucket_id = IP_TR_HASH(curthread);
6343 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6344 
6345 	for (th_trace = ipif->ipif_trace[bucket_id]; th_trace != NULL;
6346 	    th_trace = th_trace->th_next) {
6347 		if (th_trace->th_id == curthread)
6348 			return (th_trace);
6349 	}
6350 	return (NULL);
6351 }
6352 
6353 void
6354 ipif_trace_ref(ipif_t *ipif)
6355 {
6356 	int bucket_id;
6357 	th_trace_t *th_trace;
6358 
6359 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6360 
6361 	if (ipif->ipif_trace_disable)
6362 		return;
6363 
6364 	/*
6365 	 * Attempt to locate the trace buffer for the curthread.
6366 	 * If it does not exist, then allocate a new trace buffer
6367 	 * and link it in list of trace bufs for this ipif, at the head
6368 	 */
6369 	th_trace = th_trace_ipif_lookup(ipif);
6370 	if (th_trace == NULL) {
6371 		bucket_id = IP_TR_HASH(curthread);
6372 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6373 		    KM_NOSLEEP);
6374 		if (th_trace == NULL) {
6375 			ipif->ipif_trace_disable = B_TRUE;
6376 			ipif_trace_cleanup(ipif);
6377 			return;
6378 		}
6379 		th_trace->th_id = curthread;
6380 		th_trace->th_next = ipif->ipif_trace[bucket_id];
6381 		th_trace->th_prev = &ipif->ipif_trace[bucket_id];
6382 		if (th_trace->th_next != NULL)
6383 			th_trace->th_next->th_prev = &th_trace->th_next;
6384 		ipif->ipif_trace[bucket_id] = th_trace;
6385 	}
6386 	ASSERT(th_trace->th_refcnt >= 0 &&
6387 		th_trace->th_refcnt < TR_BUF_MAX -1);
6388 	th_trace->th_refcnt++;
6389 	th_trace_rrecord(th_trace);
6390 }
6391 
6392 void
6393 ipif_untrace_ref(ipif_t *ipif)
6394 {
6395 	th_trace_t *th_trace;
6396 
6397 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6398 
6399 	if (ipif->ipif_trace_disable)
6400 		return;
6401 	th_trace = th_trace_ipif_lookup(ipif);
6402 	ASSERT(th_trace != NULL);
6403 	ASSERT(th_trace->th_refcnt > 0);
6404 
6405 	th_trace->th_refcnt--;
6406 	th_trace_rrecord(th_trace);
6407 }
6408 
6409 th_trace_t *
6410 th_trace_ill_lookup(ill_t *ill)
6411 {
6412 	th_trace_t *th_trace;
6413 	int bucket_id;
6414 
6415 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6416 
6417 	bucket_id = IP_TR_HASH(curthread);
6418 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6419 
6420 	for (th_trace = ill->ill_trace[bucket_id]; th_trace != NULL;
6421 	    th_trace = th_trace->th_next) {
6422 		if (th_trace->th_id == curthread)
6423 			return (th_trace);
6424 	}
6425 	return (NULL);
6426 }
6427 
6428 void
6429 ill_trace_ref(ill_t *ill)
6430 {
6431 	int bucket_id;
6432 	th_trace_t *th_trace;
6433 
6434 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6435 	if (ill->ill_trace_disable)
6436 		return;
6437 	/*
6438 	 * Attempt to locate the trace buffer for the curthread.
6439 	 * If it does not exist, then allocate a new trace buffer
6440 	 * and link it in list of trace bufs for this ill, at the head
6441 	 */
6442 	th_trace = th_trace_ill_lookup(ill);
6443 	if (th_trace == NULL) {
6444 		bucket_id = IP_TR_HASH(curthread);
6445 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6446 		    KM_NOSLEEP);
6447 		if (th_trace == NULL) {
6448 			ill->ill_trace_disable = B_TRUE;
6449 			ill_trace_cleanup(ill);
6450 			return;
6451 		}
6452 		th_trace->th_id = curthread;
6453 		th_trace->th_next = ill->ill_trace[bucket_id];
6454 		th_trace->th_prev = &ill->ill_trace[bucket_id];
6455 		if (th_trace->th_next != NULL)
6456 			th_trace->th_next->th_prev = &th_trace->th_next;
6457 		ill->ill_trace[bucket_id] = th_trace;
6458 	}
6459 	ASSERT(th_trace->th_refcnt >= 0 &&
6460 		th_trace->th_refcnt < TR_BUF_MAX - 1);
6461 
6462 	th_trace->th_refcnt++;
6463 	th_trace_rrecord(th_trace);
6464 }
6465 
6466 void
6467 ill_untrace_ref(ill_t *ill)
6468 {
6469 	th_trace_t *th_trace;
6470 
6471 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6472 
6473 	if (ill->ill_trace_disable)
6474 		return;
6475 	th_trace = th_trace_ill_lookup(ill);
6476 	ASSERT(th_trace != NULL);
6477 	ASSERT(th_trace->th_refcnt > 0);
6478 
6479 	th_trace->th_refcnt--;
6480 	th_trace_rrecord(th_trace);
6481 }
6482 
6483 /*
6484  * Verify that this thread has no refs to the ipif and free
6485  * the trace buffers
6486  */
6487 /* ARGSUSED */
6488 void
6489 ipif_thread_exit(ipif_t *ipif, void *dummy)
6490 {
6491 	th_trace_t *th_trace;
6492 
6493 	mutex_enter(&ipif->ipif_ill->ill_lock);
6494 
6495 	th_trace = th_trace_ipif_lookup(ipif);
6496 	if (th_trace == NULL) {
6497 		mutex_exit(&ipif->ipif_ill->ill_lock);
6498 		return;
6499 	}
6500 	ASSERT(th_trace->th_refcnt == 0);
6501 	/* unlink th_trace and free it */
6502 	*th_trace->th_prev = th_trace->th_next;
6503 	if (th_trace->th_next != NULL)
6504 		th_trace->th_next->th_prev = th_trace->th_prev;
6505 	th_trace->th_next = NULL;
6506 	th_trace->th_prev = NULL;
6507 	kmem_free(th_trace, sizeof (th_trace_t));
6508 
6509 	mutex_exit(&ipif->ipif_ill->ill_lock);
6510 }
6511 
6512 /*
6513  * Verify that this thread has no refs to the ill and free
6514  * the trace buffers
6515  */
6516 /* ARGSUSED */
6517 void
6518 ill_thread_exit(ill_t *ill, void *dummy)
6519 {
6520 	th_trace_t *th_trace;
6521 
6522 	mutex_enter(&ill->ill_lock);
6523 
6524 	th_trace = th_trace_ill_lookup(ill);
6525 	if (th_trace == NULL) {
6526 		mutex_exit(&ill->ill_lock);
6527 		return;
6528 	}
6529 	ASSERT(th_trace->th_refcnt == 0);
6530 	/* unlink th_trace and free it */
6531 	*th_trace->th_prev = th_trace->th_next;
6532 	if (th_trace->th_next != NULL)
6533 		th_trace->th_next->th_prev = th_trace->th_prev;
6534 	th_trace->th_next = NULL;
6535 	th_trace->th_prev = NULL;
6536 	kmem_free(th_trace, sizeof (th_trace_t));
6537 
6538 	mutex_exit(&ill->ill_lock);
6539 }
6540 #endif
6541 
6542 #ifdef ILL_DEBUG
6543 void
6544 ip_thread_exit(void)
6545 {
6546 	ill_t	*ill;
6547 	ipif_t	*ipif;
6548 	ill_walk_context_t	ctx;
6549 
6550 	rw_enter(&ill_g_lock, RW_READER);
6551 	ill = ILL_START_WALK_ALL(&ctx);
6552 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6553 		for (ipif = ill->ill_ipif; ipif != NULL;
6554 		    ipif = ipif->ipif_next) {
6555 			ipif_thread_exit(ipif, NULL);
6556 		}
6557 		ill_thread_exit(ill, NULL);
6558 	}
6559 	rw_exit(&ill_g_lock);
6560 
6561 	ire_walk(ire_thread_exit, NULL);
6562 	ndp_walk_common(&ndp4, NULL, nce_thread_exit, NULL, B_FALSE);
6563 	ndp_walk_common(&ndp6, NULL, nce_thread_exit, NULL, B_FALSE);
6564 }
6565 
6566 /*
6567  * Called when ipif is unplumbed or when memory alloc fails
6568  */
6569 void
6570 ipif_trace_cleanup(ipif_t *ipif)
6571 {
6572 	int	i;
6573 	th_trace_t	*th_trace;
6574 	th_trace_t	*th_trace_next;
6575 
6576 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6577 		for (th_trace = ipif->ipif_trace[i]; th_trace != NULL;
6578 		    th_trace = th_trace_next) {
6579 			th_trace_next = th_trace->th_next;
6580 			kmem_free(th_trace, sizeof (th_trace_t));
6581 		}
6582 		ipif->ipif_trace[i] = NULL;
6583 	}
6584 }
6585 
6586 /*
6587  * Called when ill is unplumbed or when memory alloc fails
6588  */
6589 void
6590 ill_trace_cleanup(ill_t *ill)
6591 {
6592 	int	i;
6593 	th_trace_t	*th_trace;
6594 	th_trace_t	*th_trace_next;
6595 
6596 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6597 		for (th_trace = ill->ill_trace[i]; th_trace != NULL;
6598 		    th_trace = th_trace_next) {
6599 			th_trace_next = th_trace->th_next;
6600 			kmem_free(th_trace, sizeof (th_trace_t));
6601 		}
6602 		ill->ill_trace[i] = NULL;
6603 	}
6604 }
6605 
6606 #else
6607 void ip_thread_exit(void) {}
6608 #endif
6609 
6610 void
6611 ipif_refhold_locked(ipif_t *ipif)
6612 {
6613 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6614 	ipif->ipif_refcnt++;
6615 	IPIF_TRACE_REF(ipif);
6616 }
6617 
6618 void
6619 ipif_refhold(ipif_t *ipif)
6620 {
6621 	ill_t	*ill;
6622 
6623 	ill = ipif->ipif_ill;
6624 	mutex_enter(&ill->ill_lock);
6625 	ipif->ipif_refcnt++;
6626 	IPIF_TRACE_REF(ipif);
6627 	mutex_exit(&ill->ill_lock);
6628 }
6629 
6630 /*
6631  * Must not be called while holding any locks. Otherwise if this is
6632  * the last reference to be released there is a chance of recursive mutex
6633  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6634  * to restart an ioctl.
6635  */
6636 void
6637 ipif_refrele(ipif_t *ipif)
6638 {
6639 	ill_t	*ill;
6640 
6641 	ill = ipif->ipif_ill;
6642 
6643 	mutex_enter(&ill->ill_lock);
6644 	ASSERT(ipif->ipif_refcnt != 0);
6645 	ipif->ipif_refcnt--;
6646 	IPIF_UNTRACE_REF(ipif);
6647 	if (ipif->ipif_refcnt != 0) {
6648 		mutex_exit(&ill->ill_lock);
6649 		return;
6650 	}
6651 
6652 	/* Drops the ill_lock */
6653 	ipif_ill_refrele_tail(ill);
6654 }
6655 
6656 ipif_t *
6657 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6658 {
6659 	ipif_t	*ipif;
6660 
6661 	mutex_enter(&ill->ill_lock);
6662 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6663 	    ipif != NULL; ipif = ipif->ipif_next) {
6664 		if (!IPIF_CAN_LOOKUP(ipif))
6665 			continue;
6666 		ipif_refhold_locked(ipif);
6667 		mutex_exit(&ill->ill_lock);
6668 		return (ipif);
6669 	}
6670 	mutex_exit(&ill->ill_lock);
6671 	return (NULL);
6672 }
6673 
6674 /*
6675  * TODO: make this table extendible at run time
6676  * Return a pointer to the mac type info for 'mac_type'
6677  */
6678 static ip_m_t *
6679 ip_m_lookup(t_uscalar_t mac_type)
6680 {
6681 	ip_m_t	*ipm;
6682 
6683 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6684 		if (ipm->ip_m_mac_type == mac_type)
6685 			return (ipm);
6686 	return (NULL);
6687 }
6688 
6689 /*
6690  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6691  * ipif_arg is passed in to associate it with the correct interface.
6692  * We may need to restart this operation if the ipif cannot be looked up
6693  * due to an exclusive operation that is currently in progress. The restart
6694  * entry point is specified by 'func'
6695  */
6696 int
6697 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6698     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6699     ire_t **ire_arg, boolean_t ioctl_msg, queue_t *q, mblk_t *mp,
6700     ipsq_func_t func, struct rtsa_s *sp)
6701 {
6702 	ire_t	*ire;
6703 	ire_t	*gw_ire = NULL;
6704 	ipif_t	*ipif = NULL;
6705 	boolean_t ipif_refheld = B_FALSE;
6706 	uint_t	type;
6707 	int	match_flags = MATCH_IRE_TYPE;
6708 	int	error;
6709 	tsol_gc_t *gc = NULL;
6710 	tsol_gcgrp_t *gcgrp = NULL;
6711 	boolean_t gcgrp_xtraref = B_FALSE;
6712 
6713 	ip1dbg(("ip_rt_add:"));
6714 
6715 	if (ire_arg != NULL)
6716 		*ire_arg = NULL;
6717 
6718 	/*
6719 	 * If this is the case of RTF_HOST being set, then we set the netmask
6720 	 * to all ones (regardless if one was supplied).
6721 	 */
6722 	if (flags & RTF_HOST)
6723 		mask = IP_HOST_MASK;
6724 
6725 	/*
6726 	 * Prevent routes with a zero gateway from being created (since
6727 	 * interfaces can currently be plumbed and brought up no assigned
6728 	 * address).
6729 	 * For routes with RTA_SRCIFP, the gateway address can be 0.0.0.0.
6730 	 */
6731 	if (gw_addr == 0 && src_ipif == NULL)
6732 		return (ENETUNREACH);
6733 	/*
6734 	 * Get the ipif, if any, corresponding to the gw_addr
6735 	 */
6736 	if (gw_addr != 0) {
6737 		ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func,
6738 		    &error);
6739 		if (ipif != NULL) {
6740 			if (IS_VNI(ipif->ipif_ill)) {
6741 				ipif_refrele(ipif);
6742 				return (EINVAL);
6743 			}
6744 			ipif_refheld = B_TRUE;
6745 		} else if (error == EINPROGRESS) {
6746 			ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6747 			return (EINPROGRESS);
6748 		} else {
6749 			error = 0;
6750 		}
6751 	}
6752 
6753 	if (ipif != NULL) {
6754 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6755 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6756 	} else {
6757 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6758 	}
6759 
6760 	/*
6761 	 * GateD will attempt to create routes with a loopback interface
6762 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6763 	 * these routes to be added, but create them as interface routes
6764 	 * since the gateway is an interface address.
6765 	 */
6766 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6767 		flags &= ~RTF_GATEWAY;
6768 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6769 		    mask == IP_HOST_MASK) {
6770 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6771 			    ALL_ZONES, NULL, match_flags);
6772 			if (ire != NULL) {
6773 				ire_refrele(ire);
6774 				if (ipif_refheld)
6775 					ipif_refrele(ipif);
6776 				return (EEXIST);
6777 			}
6778 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6779 			    "for 0x%x\n", (void *)ipif,
6780 			    ipif->ipif_ire_type,
6781 			    ntohl(ipif->ipif_lcl_addr)));
6782 			ire = ire_create(
6783 			    (uchar_t *)&dst_addr,	/* dest address */
6784 			    (uchar_t *)&mask,		/* mask */
6785 			    (uchar_t *)&ipif->ipif_src_addr,
6786 			    NULL,			/* no gateway */
6787 			    NULL,
6788 			    &ipif->ipif_mtu,
6789 			    NULL,
6790 			    ipif->ipif_rq,		/* recv-from queue */
6791 			    NULL,			/* no send-to queue */
6792 			    ipif->ipif_ire_type,	/* LOOPBACK */
6793 			    NULL,
6794 			    ipif,
6795 			    NULL,
6796 			    0,
6797 			    0,
6798 			    0,
6799 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6800 			    RTF_PRIVATE : 0,
6801 			    &ire_uinfo_null,
6802 			    NULL,
6803 			    NULL);
6804 
6805 			if (ire == NULL) {
6806 				if (ipif_refheld)
6807 					ipif_refrele(ipif);
6808 				return (ENOMEM);
6809 			}
6810 			error = ire_add(&ire, q, mp, func, B_FALSE);
6811 			if (error == 0)
6812 				goto save_ire;
6813 			if (ipif_refheld)
6814 				ipif_refrele(ipif);
6815 			return (error);
6816 
6817 		}
6818 	}
6819 
6820 	/*
6821 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6822 	 * and the gateway address provided is one of the system's interface
6823 	 * addresses.  By using the routing socket interface and supplying an
6824 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6825 	 * specifying an interface route to be created is available which uses
6826 	 * the interface index that specifies the outgoing interface rather than
6827 	 * the address of an outgoing interface (which may not be able to
6828 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6829 	 * flag, routes can be specified which not only specify the next-hop to
6830 	 * be used when routing to a certain prefix, but also which outgoing
6831 	 * interface should be used.
6832 	 *
6833 	 * Previously, interfaces would have unique addresses assigned to them
6834 	 * and so the address assigned to a particular interface could be used
6835 	 * to identify a particular interface.  One exception to this was the
6836 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6837 	 *
6838 	 * With the advent of IPv6 and its link-local addresses, this
6839 	 * restriction was relaxed and interfaces could share addresses between
6840 	 * themselves.  In fact, typically all of the link-local interfaces on
6841 	 * an IPv6 node or router will have the same link-local address.  In
6842 	 * order to differentiate between these interfaces, the use of an
6843 	 * interface index is necessary and this index can be carried inside a
6844 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6845 	 * of using the interface index, however, is that all of the ipif's that
6846 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6847 	 * cannot be used to differentiate between ipif's (or logical
6848 	 * interfaces) that belong to the same ill (physical interface).
6849 	 *
6850 	 * For example, in the following case involving IPv4 interfaces and
6851 	 * logical interfaces
6852 	 *
6853 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6854 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6855 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6856 	 *
6857 	 * the ipif's corresponding to each of these interface routes can be
6858 	 * uniquely identified by the "gateway" (actually interface address).
6859 	 *
6860 	 * In this case involving multiple IPv6 default routes to a particular
6861 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6862 	 * default route is of interest:
6863 	 *
6864 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6865 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6866 	 */
6867 
6868 	/* RTF_GATEWAY not set */
6869 	if (!(flags & RTF_GATEWAY)) {
6870 		queue_t	*stq;
6871 		queue_t	*rfq = NULL;
6872 		ill_t	*in_ill = NULL;
6873 
6874 		if (sp != NULL) {
6875 			ip2dbg(("ip_rt_add: gateway security attributes "
6876 			    "cannot be set with interface route\n"));
6877 			if (ipif_refheld)
6878 				ipif_refrele(ipif);
6879 			return (EINVAL);
6880 		}
6881 
6882 		/*
6883 		 * As the interface index specified with the RTA_IFP sockaddr is
6884 		 * the same for all ipif's off of an ill, the matching logic
6885 		 * below uses MATCH_IRE_ILL if such an index was specified.
6886 		 * This means that routes sharing the same prefix when added
6887 		 * using a RTA_IFP sockaddr must have distinct interface
6888 		 * indices (namely, they must be on distinct ill's).
6889 		 *
6890 		 * On the other hand, since the gateway address will usually be
6891 		 * different for each ipif on the system, the matching logic
6892 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6893 		 * route.  This means that interface routes for the same prefix
6894 		 * can be created if they belong to distinct ipif's and if a
6895 		 * RTA_IFP sockaddr is not present.
6896 		 */
6897 		if (ipif_arg != NULL) {
6898 			if (ipif_refheld)  {
6899 				ipif_refrele(ipif);
6900 				ipif_refheld = B_FALSE;
6901 			}
6902 			ipif = ipif_arg;
6903 			match_flags |= MATCH_IRE_ILL;
6904 		} else {
6905 			/*
6906 			 * Check the ipif corresponding to the gw_addr
6907 			 */
6908 			if (ipif == NULL)
6909 				return (ENETUNREACH);
6910 			match_flags |= MATCH_IRE_IPIF;
6911 		}
6912 		ASSERT(ipif != NULL);
6913 		/*
6914 		 * If src_ipif is not NULL, we have to create
6915 		 * an ire with non-null ire_in_ill value
6916 		 */
6917 		if (src_ipif != NULL) {
6918 			in_ill = src_ipif->ipif_ill;
6919 		}
6920 
6921 		/*
6922 		 * We check for an existing entry at this point.
6923 		 *
6924 		 * Since a netmask isn't passed in via the ioctl interface
6925 		 * (SIOCADDRT), we don't check for a matching netmask in that
6926 		 * case.
6927 		 */
6928 		if (!ioctl_msg)
6929 			match_flags |= MATCH_IRE_MASK;
6930 		if (src_ipif != NULL) {
6931 			/* Look up in the special table */
6932 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
6933 			    ipif, src_ipif->ipif_ill, match_flags);
6934 		} else {
6935 			ire = ire_ftable_lookup(dst_addr, mask, 0,
6936 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
6937 			    NULL, match_flags);
6938 		}
6939 		if (ire != NULL) {
6940 			ire_refrele(ire);
6941 			if (ipif_refheld)
6942 				ipif_refrele(ipif);
6943 			return (EEXIST);
6944 		}
6945 
6946 		if (src_ipif != NULL) {
6947 			/*
6948 			 * Create the special ire for the IRE table
6949 			 * which hangs out of ire_in_ill. This ire
6950 			 * is in-between IRE_CACHE and IRE_INTERFACE.
6951 			 * Thus rfq is non-NULL.
6952 			 */
6953 			rfq = ipif->ipif_rq;
6954 		}
6955 		/* Create the usual interface ires */
6956 
6957 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6958 		    ? ipif->ipif_rq : ipif->ipif_wq;
6959 
6960 		/*
6961 		 * Create a copy of the IRE_LOOPBACK,
6962 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6963 		 * the modified address and netmask.
6964 		 */
6965 		ire = ire_create(
6966 		    (uchar_t *)&dst_addr,
6967 		    (uint8_t *)&mask,
6968 		    (uint8_t *)&ipif->ipif_src_addr,
6969 		    NULL,
6970 		    NULL,
6971 		    &ipif->ipif_mtu,
6972 		    NULL,
6973 		    rfq,
6974 		    stq,
6975 		    ipif->ipif_net_type,
6976 		    ipif->ipif_resolver_mp,
6977 		    ipif,
6978 		    in_ill,
6979 		    0,
6980 		    0,
6981 		    0,
6982 		    flags,
6983 		    &ire_uinfo_null,
6984 		    NULL,
6985 		    NULL);
6986 		if (ire == NULL) {
6987 			if (ipif_refheld)
6988 				ipif_refrele(ipif);
6989 			return (ENOMEM);
6990 		}
6991 
6992 		/*
6993 		 * Some software (for example, GateD and Sun Cluster) attempts
6994 		 * to create (what amount to) IRE_PREFIX routes with the
6995 		 * loopback address as the gateway.  This is primarily done to
6996 		 * set up prefixes with the RTF_REJECT flag set (for example,
6997 		 * when generating aggregate routes.)
6998 		 *
6999 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7000 		 * IRE_LOOPBACK, then we map the request into a
7001 		 * IRE_IF_NORESOLVER.
7002 		 *
7003 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7004 		 * routine, but rather using ire_create() directly.
7005 		 *
7006 		 */
7007 		if (ipif->ipif_net_type == IRE_LOOPBACK)
7008 			ire->ire_type = IRE_IF_NORESOLVER;
7009 
7010 		error = ire_add(&ire, q, mp, func, B_FALSE);
7011 		if (error == 0)
7012 			goto save_ire;
7013 
7014 		/*
7015 		 * In the result of failure, ire_add() will have already
7016 		 * deleted the ire in question, so there is no need to
7017 		 * do that here.
7018 		 */
7019 		if (ipif_refheld)
7020 			ipif_refrele(ipif);
7021 		return (error);
7022 	}
7023 	if (ipif_refheld) {
7024 		ipif_refrele(ipif);
7025 		ipif_refheld = B_FALSE;
7026 	}
7027 
7028 	if (src_ipif != NULL) {
7029 		/* RTA_SRCIFP is not supported on RTF_GATEWAY */
7030 		ip2dbg(("ip_rt_add: SRCIF cannot be set with gateway route\n"));
7031 		return (EINVAL);
7032 	}
7033 	/*
7034 	 * Get an interface IRE for the specified gateway.
7035 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7036 	 * gateway, it is currently unreachable and we fail the request
7037 	 * accordingly.
7038 	 */
7039 	ipif = ipif_arg;
7040 	if (ipif_arg != NULL)
7041 		match_flags |= MATCH_IRE_ILL;
7042 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7043 	    ALL_ZONES, 0, NULL, match_flags);
7044 	if (gw_ire == NULL)
7045 		return (ENETUNREACH);
7046 
7047 	/*
7048 	 * We create one of three types of IREs as a result of this request
7049 	 * based on the netmask.  A netmask of all ones (which is automatically
7050 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7051 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7052 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7053 	 * destination prefix.
7054 	 */
7055 	if (mask == IP_HOST_MASK)
7056 		type = IRE_HOST;
7057 	else if (mask == 0)
7058 		type = IRE_DEFAULT;
7059 	else
7060 		type = IRE_PREFIX;
7061 
7062 	/* check for a duplicate entry */
7063 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7064 	    NULL, ALL_ZONES, 0, NULL,
7065 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW);
7066 	if (ire != NULL) {
7067 		ire_refrele(gw_ire);
7068 		ire_refrele(ire);
7069 		return (EEXIST);
7070 	}
7071 
7072 	/* Security attribute exists */
7073 	if (sp != NULL) {
7074 		tsol_gcgrp_addr_t ga;
7075 
7076 		/* find or create the gateway credentials group */
7077 		ga.ga_af = AF_INET;
7078 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7079 
7080 		/* we hold reference to it upon success */
7081 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7082 		if (gcgrp == NULL) {
7083 			ire_refrele(gw_ire);
7084 			return (ENOMEM);
7085 		}
7086 
7087 		/*
7088 		 * Create and add the security attribute to the group; a
7089 		 * reference to the group is made upon allocating a new
7090 		 * entry successfully.  If it finds an already-existing
7091 		 * entry for the security attribute in the group, it simply
7092 		 * returns it and no new reference is made to the group.
7093 		 */
7094 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7095 		if (gc == NULL) {
7096 			/* release reference held by gcgrp_lookup */
7097 			GCGRP_REFRELE(gcgrp);
7098 			ire_refrele(gw_ire);
7099 			return (ENOMEM);
7100 		}
7101 	}
7102 
7103 	/* Create the IRE. */
7104 	ire = ire_create(
7105 	    (uchar_t *)&dst_addr,		/* dest address */
7106 	    (uchar_t *)&mask,			/* mask */
7107 	    /* src address assigned by the caller? */
7108 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7109 		(flags & RTF_SETSRC)) ?  &src_addr : NULL),
7110 	    (uchar_t *)&gw_addr,		/* gateway address */
7111 	    NULL,				/* no in-srcaddress */
7112 	    &gw_ire->ire_max_frag,
7113 	    NULL,				/* no Fast Path header */
7114 	    NULL,				/* no recv-from queue */
7115 	    NULL,				/* no send-to queue */
7116 	    (ushort_t)type,			/* IRE type */
7117 	    NULL,
7118 	    ipif_arg,
7119 	    NULL,
7120 	    0,
7121 	    0,
7122 	    0,
7123 	    flags,
7124 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7125 	    gc,					/* security attribute */
7126 	    NULL);
7127 	/*
7128 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7129 	 * reference to the 'gcgrp'. We can now release the extra reference
7130 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7131 	 */
7132 	if (gcgrp_xtraref)
7133 		GCGRP_REFRELE(gcgrp);
7134 	if (ire == NULL) {
7135 		if (gc != NULL)
7136 			GC_REFRELE(gc);
7137 		ire_refrele(gw_ire);
7138 		return (ENOMEM);
7139 	}
7140 
7141 	/*
7142 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7143 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7144 	 */
7145 
7146 	/* Add the new IRE. */
7147 	error = ire_add(&ire, q, mp, func, B_FALSE);
7148 	if (error != 0) {
7149 		/*
7150 		 * In the result of failure, ire_add() will have already
7151 		 * deleted the ire in question, so there is no need to
7152 		 * do that here.
7153 		 */
7154 		ire_refrele(gw_ire);
7155 		return (error);
7156 	}
7157 
7158 	if (flags & RTF_MULTIRT) {
7159 		/*
7160 		 * Invoke the CGTP (multirouting) filtering module
7161 		 * to add the dst address in the filtering database.
7162 		 * Replicated inbound packets coming from that address
7163 		 * will be filtered to discard the duplicates.
7164 		 * It is not necessary to call the CGTP filter hook
7165 		 * when the dst address is a broadcast or multicast,
7166 		 * because an IP source address cannot be a broadcast
7167 		 * or a multicast.
7168 		 */
7169 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7170 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
7171 		if (ire_dst != NULL) {
7172 			ip_cgtp_bcast_add(ire, ire_dst);
7173 			ire_refrele(ire_dst);
7174 			goto save_ire;
7175 		}
7176 		if ((ip_cgtp_filter_ops != NULL) && !CLASSD(ire->ire_addr)) {
7177 			int res = ip_cgtp_filter_ops->cfo_add_dest_v4(
7178 			    ire->ire_addr,
7179 			    ire->ire_gateway_addr,
7180 			    ire->ire_src_addr,
7181 			    gw_ire->ire_src_addr);
7182 			if (res != 0) {
7183 				ire_refrele(gw_ire);
7184 				ire_delete(ire);
7185 				return (res);
7186 			}
7187 		}
7188 	}
7189 
7190 	/*
7191 	 * Now that the prefix IRE entry has been created, delete any
7192 	 * existing gateway IRE cache entries as well as any IRE caches
7193 	 * using the gateway, and force them to be created through
7194 	 * ip_newroute.
7195 	 */
7196 	if (gc != NULL) {
7197 		ASSERT(gcgrp != NULL);
7198 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES);
7199 	}
7200 
7201 save_ire:
7202 	if (gw_ire != NULL) {
7203 		ire_refrele(gw_ire);
7204 	}
7205 	/*
7206 	 * We do not do save_ire for the routes added with RTA_SRCIFP
7207 	 * flag. This route is only added and deleted by mipagent.
7208 	 * So, for simplicity of design, we refrain from saving
7209 	 * ires that are created with srcif value. This may change
7210 	 * in future if we find more usage of srcifp feature.
7211 	 */
7212 	if (ipif != NULL && src_ipif == NULL) {
7213 		/*
7214 		 * Save enough information so that we can recreate the IRE if
7215 		 * the interface goes down and then up.  The metrics associated
7216 		 * with the route will be saved as well when rts_setmetrics() is
7217 		 * called after the IRE has been created.  In the case where
7218 		 * memory cannot be allocated, none of this information will be
7219 		 * saved.
7220 		 */
7221 		ipif_save_ire(ipif, ire);
7222 	}
7223 	if (ioctl_msg)
7224 		ip_rts_rtmsg(RTM_OLDADD, ire, 0);
7225 	if (ire_arg != NULL) {
7226 		/*
7227 		 * Store the ire that was successfully added into where ire_arg
7228 		 * points to so that callers don't have to look it up
7229 		 * themselves (but they are responsible for ire_refrele()ing
7230 		 * the ire when they are finished with it).
7231 		 */
7232 		*ire_arg = ire;
7233 	} else {
7234 		ire_refrele(ire);		/* Held in ire_add */
7235 	}
7236 	if (ipif_refheld)
7237 		ipif_refrele(ipif);
7238 	return (0);
7239 }
7240 
7241 /*
7242  * ip_rt_delete is called to delete an IPv4 route.
7243  * ipif_arg is passed in to associate it with the correct interface.
7244  * src_ipif is passed to associate the incoming interface of the packet.
7245  * We may need to restart this operation if the ipif cannot be looked up
7246  * due to an exclusive operation that is currently in progress. The restart
7247  * entry point is specified by 'func'
7248  */
7249 /* ARGSUSED4 */
7250 int
7251 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7252     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
7253     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func)
7254 {
7255 	ire_t	*ire = NULL;
7256 	ipif_t	*ipif;
7257 	boolean_t ipif_refheld = B_FALSE;
7258 	uint_t	type;
7259 	uint_t	match_flags = MATCH_IRE_TYPE;
7260 	int	err = 0;
7261 
7262 	ip1dbg(("ip_rt_delete:"));
7263 	/*
7264 	 * If this is the case of RTF_HOST being set, then we set the netmask
7265 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7266 	 */
7267 	if (flags & RTF_HOST) {
7268 		mask = IP_HOST_MASK;
7269 		match_flags |= MATCH_IRE_MASK;
7270 	} else if (rtm_addrs & RTA_NETMASK) {
7271 		match_flags |= MATCH_IRE_MASK;
7272 	}
7273 
7274 	/*
7275 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7276 	 * we check if the gateway address is one of our interfaces first,
7277 	 * and fall back on RTF_GATEWAY routes.
7278 	 *
7279 	 * This makes it possible to delete an original
7280 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7281 	 *
7282 	 * As the interface index specified with the RTA_IFP sockaddr is the
7283 	 * same for all ipif's off of an ill, the matching logic below uses
7284 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7285 	 * sharing the same prefix and interface index as the the route
7286 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7287 	 * is specified in the request.
7288 	 *
7289 	 * On the other hand, since the gateway address will usually be
7290 	 * different for each ipif on the system, the matching logic
7291 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7292 	 * route.  This means that interface routes for the same prefix can be
7293 	 * uniquely identified if they belong to distinct ipif's and if a
7294 	 * RTA_IFP sockaddr is not present.
7295 	 *
7296 	 * For more detail on specifying routes by gateway address and by
7297 	 * interface index, see the comments in ip_rt_add().
7298 	 * gw_addr could be zero in some cases when both RTA_SRCIFP and
7299 	 * RTA_IFP are specified. If RTA_SRCIFP is specified and  both
7300 	 * RTA_IFP and gateway_addr are NULL/zero, then delete will not
7301 	 * succeed.
7302 	 */
7303 	if (src_ipif != NULL) {
7304 		if (ipif_arg == NULL && gw_addr != 0) {
7305 			ipif_arg = ipif_lookup_interface(gw_addr, dst_addr,
7306 			    q, mp, func, &err);
7307 			if (ipif_arg != NULL)
7308 				ipif_refheld = B_TRUE;
7309 		}
7310 		if (ipif_arg == NULL) {
7311 			err = (err == EINPROGRESS) ? err : ESRCH;
7312 			return (err);
7313 		}
7314 		ipif = ipif_arg;
7315 	} else {
7316 		ipif = ipif_lookup_interface(gw_addr, dst_addr,
7317 			    q, mp, func, &err);
7318 		if (ipif != NULL)
7319 			ipif_refheld = B_TRUE;
7320 		else if (err == EINPROGRESS)
7321 			return (err);
7322 		else
7323 			err = 0;
7324 	}
7325 	if (ipif != NULL) {
7326 		if (ipif_arg != NULL) {
7327 			if (ipif_refheld) {
7328 				ipif_refrele(ipif);
7329 				ipif_refheld = B_FALSE;
7330 			}
7331 			ipif = ipif_arg;
7332 			match_flags |= MATCH_IRE_ILL;
7333 		} else {
7334 			match_flags |= MATCH_IRE_IPIF;
7335 		}
7336 		if (src_ipif != NULL) {
7337 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
7338 			    ipif, src_ipif->ipif_ill, match_flags);
7339 		} else {
7340 			if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7341 				ire = ire_ctable_lookup(dst_addr, 0,
7342 				    IRE_LOOPBACK, ipif, ALL_ZONES, NULL,
7343 				    match_flags);
7344 			}
7345 			if (ire == NULL) {
7346 				ire = ire_ftable_lookup(dst_addr, mask, 0,
7347 				    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
7348 				    NULL, match_flags);
7349 			}
7350 		}
7351 	}
7352 
7353 	if (ire == NULL) {
7354 		/*
7355 		 * At this point, the gateway address is not one of our own
7356 		 * addresses or a matching interface route was not found.  We
7357 		 * set the IRE type to lookup based on whether
7358 		 * this is a host route, a default route or just a prefix.
7359 		 *
7360 		 * If an ipif_arg was passed in, then the lookup is based on an
7361 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7362 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7363 		 * set as the route being looked up is not a traditional
7364 		 * interface route.
7365 		 * Since we do not add gateway route with srcipif, we don't
7366 		 * expect to find it either.
7367 		 */
7368 		if (src_ipif != NULL) {
7369 			if (ipif_refheld)
7370 				ipif_refrele(ipif);
7371 			return (ESRCH);
7372 		} else {
7373 			match_flags &= ~MATCH_IRE_IPIF;
7374 			match_flags |= MATCH_IRE_GW;
7375 			if (ipif_arg != NULL)
7376 				match_flags |= MATCH_IRE_ILL;
7377 			if (mask == IP_HOST_MASK)
7378 				type = IRE_HOST;
7379 			else if (mask == 0)
7380 				type = IRE_DEFAULT;
7381 			else
7382 				type = IRE_PREFIX;
7383 			ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type,
7384 			    ipif_arg, NULL, ALL_ZONES, 0, NULL, match_flags);
7385 		}
7386 	}
7387 
7388 	if (ipif_refheld)
7389 		ipif_refrele(ipif);
7390 
7391 	/* ipif is not refheld anymore */
7392 	if (ire == NULL)
7393 		return (ESRCH);
7394 
7395 	if (ire->ire_flags & RTF_MULTIRT) {
7396 		/*
7397 		 * Invoke the CGTP (multirouting) filtering module
7398 		 * to remove the dst address from the filtering database.
7399 		 * Packets coming from that address will no longer be
7400 		 * filtered to remove duplicates.
7401 		 */
7402 		if (ip_cgtp_filter_ops != NULL) {
7403 			err = ip_cgtp_filter_ops->cfo_del_dest_v4(ire->ire_addr,
7404 			    ire->ire_gateway_addr);
7405 		}
7406 		ip_cgtp_bcast_delete(ire);
7407 	}
7408 
7409 	ipif = ire->ire_ipif;
7410 	/*
7411 	 * Removing from ipif_saved_ire_mp is not necessary
7412 	 * when src_ipif being non-NULL. ip_rt_add does not
7413 	 * save the ires which src_ipif being non-NULL.
7414 	 */
7415 	if (ipif != NULL && src_ipif == NULL) {
7416 		ipif_remove_ire(ipif, ire);
7417 	}
7418 	if (ioctl_msg)
7419 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0);
7420 	ire_delete(ire);
7421 	ire_refrele(ire);
7422 	return (err);
7423 }
7424 
7425 /*
7426  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7427  */
7428 /* ARGSUSED */
7429 int
7430 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7431     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7432 {
7433 	ipaddr_t dst_addr;
7434 	ipaddr_t gw_addr;
7435 	ipaddr_t mask;
7436 	int error = 0;
7437 	mblk_t *mp1;
7438 	struct rtentry *rt;
7439 	ipif_t *ipif = NULL;
7440 
7441 	ip1dbg(("ip_siocaddrt:"));
7442 	/* Existence of mp1 verified in ip_wput_nondata */
7443 	mp1 = mp->b_cont->b_cont;
7444 	rt = (struct rtentry *)mp1->b_rptr;
7445 
7446 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7447 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7448 
7449 	/*
7450 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7451 	 * to a particular host address.  In this case, we set the netmask to
7452 	 * all ones for the particular destination address.  Otherwise,
7453 	 * determine the netmask to be used based on dst_addr and the interfaces
7454 	 * in use.
7455 	 */
7456 	if (rt->rt_flags & RTF_HOST) {
7457 		mask = IP_HOST_MASK;
7458 	} else {
7459 		/*
7460 		 * Note that ip_subnet_mask returns a zero mask in the case of
7461 		 * default (an all-zeroes address).
7462 		 */
7463 		mask = ip_subnet_mask(dst_addr, &ipif);
7464 	}
7465 
7466 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7467 	    NULL, B_TRUE, q, mp, ip_process_ioctl, NULL);
7468 	if (ipif != NULL)
7469 		ipif_refrele(ipif);
7470 	return (error);
7471 }
7472 
7473 /*
7474  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7475  */
7476 /* ARGSUSED */
7477 int
7478 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7479     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7480 {
7481 	ipaddr_t dst_addr;
7482 	ipaddr_t gw_addr;
7483 	ipaddr_t mask;
7484 	int error;
7485 	mblk_t *mp1;
7486 	struct rtentry *rt;
7487 	ipif_t *ipif = NULL;
7488 
7489 	ip1dbg(("ip_siocdelrt:"));
7490 	/* Existence of mp1 verified in ip_wput_nondata */
7491 	mp1 = mp->b_cont->b_cont;
7492 	rt = (struct rtentry *)mp1->b_rptr;
7493 
7494 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7495 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7496 
7497 	/*
7498 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7499 	 * to a particular host address.  In this case, we set the netmask to
7500 	 * all ones for the particular destination address.  Otherwise,
7501 	 * determine the netmask to be used based on dst_addr and the interfaces
7502 	 * in use.
7503 	 */
7504 	if (rt->rt_flags & RTF_HOST) {
7505 		mask = IP_HOST_MASK;
7506 	} else {
7507 		/*
7508 		 * Note that ip_subnet_mask returns a zero mask in the case of
7509 		 * default (an all-zeroes address).
7510 		 */
7511 		mask = ip_subnet_mask(dst_addr, &ipif);
7512 	}
7513 
7514 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7515 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, NULL,
7516 	    B_TRUE, q, mp, ip_process_ioctl);
7517 	if (ipif != NULL)
7518 		ipif_refrele(ipif);
7519 	return (error);
7520 }
7521 
7522 /*
7523  * Enqueue the mp onto the ipsq, chained by b_next.
7524  * b_prev stores the function to be executed later, and b_queue the queue
7525  * where this mp originated.
7526  */
7527 void
7528 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7529     ill_t *pending_ill)
7530 {
7531 	conn_t	*connp = NULL;
7532 
7533 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7534 	ASSERT(func != NULL);
7535 
7536 	mp->b_queue = q;
7537 	mp->b_prev = (void *)func;
7538 	mp->b_next = NULL;
7539 
7540 	switch (type) {
7541 	case CUR_OP:
7542 		if (ipsq->ipsq_mptail != NULL) {
7543 			ASSERT(ipsq->ipsq_mphead != NULL);
7544 			ipsq->ipsq_mptail->b_next = mp;
7545 		} else {
7546 			ASSERT(ipsq->ipsq_mphead == NULL);
7547 			ipsq->ipsq_mphead = mp;
7548 		}
7549 		ipsq->ipsq_mptail = mp;
7550 		break;
7551 
7552 	case NEW_OP:
7553 		if (ipsq->ipsq_xopq_mptail != NULL) {
7554 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7555 			ipsq->ipsq_xopq_mptail->b_next = mp;
7556 		} else {
7557 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7558 			ipsq->ipsq_xopq_mphead = mp;
7559 		}
7560 		ipsq->ipsq_xopq_mptail = mp;
7561 		break;
7562 	default:
7563 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7564 	}
7565 
7566 	if (CONN_Q(q) && pending_ill != NULL) {
7567 		connp = Q_TO_CONN(q);
7568 
7569 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7570 		connp->conn_oper_pending_ill = pending_ill;
7571 	}
7572 }
7573 
7574 /*
7575  * Return the mp at the head of the ipsq. After emptying the ipsq
7576  * look at the next ioctl, if this ioctl is complete. Otherwise
7577  * return, we will resume when we complete the current ioctl.
7578  * The current ioctl will wait till it gets a response from the
7579  * driver below.
7580  */
7581 static mblk_t *
7582 ipsq_dq(ipsq_t *ipsq)
7583 {
7584 	mblk_t	*mp;
7585 
7586 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7587 
7588 	mp = ipsq->ipsq_mphead;
7589 	if (mp != NULL) {
7590 		ipsq->ipsq_mphead = mp->b_next;
7591 		if (ipsq->ipsq_mphead == NULL)
7592 			ipsq->ipsq_mptail = NULL;
7593 		mp->b_next = NULL;
7594 		return (mp);
7595 	}
7596 	if (ipsq->ipsq_current_ipif != NULL)
7597 		return (NULL);
7598 	mp = ipsq->ipsq_xopq_mphead;
7599 	if (mp != NULL) {
7600 		ipsq->ipsq_xopq_mphead = mp->b_next;
7601 		if (ipsq->ipsq_xopq_mphead == NULL)
7602 			ipsq->ipsq_xopq_mptail = NULL;
7603 		mp->b_next = NULL;
7604 		return (mp);
7605 	}
7606 	return (NULL);
7607 }
7608 
7609 /*
7610  * Enter the ipsq corresponding to ill, by waiting synchronously till
7611  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7612  * will have to drain completely before ipsq_enter returns success.
7613  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7614  * and the ipsq_exit logic will start the next enqueued ioctl after
7615  * completion of the current ioctl. If 'force' is used, we don't wait
7616  * for the enqueued ioctls. This is needed when a conn_close wants to
7617  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7618  * of an ill can also use this option. But we dont' use it currently.
7619  */
7620 #define	ENTER_SQ_WAIT_TICKS 100
7621 boolean_t
7622 ipsq_enter(ill_t *ill, boolean_t force)
7623 {
7624 	ipsq_t	*ipsq;
7625 	boolean_t waited_enough = B_FALSE;
7626 
7627 	/*
7628 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7629 	 * Since the <ill-ipsq> assocs could change while we wait for the
7630 	 * writer, it is easier to wait on a fixed global rather than try to
7631 	 * cv_wait on a changing ipsq.
7632 	 */
7633 	mutex_enter(&ill->ill_lock);
7634 	for (;;) {
7635 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7636 			mutex_exit(&ill->ill_lock);
7637 			return (B_FALSE);
7638 		}
7639 
7640 		ipsq = ill->ill_phyint->phyint_ipsq;
7641 		mutex_enter(&ipsq->ipsq_lock);
7642 		if (ipsq->ipsq_writer == NULL &&
7643 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7644 			break;
7645 		} else if (ipsq->ipsq_writer != NULL) {
7646 			mutex_exit(&ipsq->ipsq_lock);
7647 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7648 		} else {
7649 			mutex_exit(&ipsq->ipsq_lock);
7650 			if (force) {
7651 				(void) cv_timedwait(&ill->ill_cv,
7652 				    &ill->ill_lock,
7653 				    lbolt + ENTER_SQ_WAIT_TICKS);
7654 				waited_enough = B_TRUE;
7655 				continue;
7656 			} else {
7657 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7658 			}
7659 		}
7660 	}
7661 
7662 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7663 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7664 	ipsq->ipsq_writer = curthread;
7665 	ipsq->ipsq_reentry_cnt++;
7666 #ifdef ILL_DEBUG
7667 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7668 #endif
7669 	mutex_exit(&ipsq->ipsq_lock);
7670 	mutex_exit(&ill->ill_lock);
7671 	return (B_TRUE);
7672 }
7673 
7674 /*
7675  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7676  * certain critical operations like plumbing (i.e. most set ioctls),
7677  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7678  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7679  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7680  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7681  * threads executing in the ipsq. Responses from the driver pertain to the
7682  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7683  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7684  *
7685  * If a thread does not want to reenter the ipsq when it is already writer,
7686  * it must make sure that the specified reentry point to be called later
7687  * when the ipsq is empty, nor any code path starting from the specified reentry
7688  * point must never ever try to enter the ipsq again. Otherwise it can lead
7689  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7690  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7691  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7692  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7693  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7694  * ioctl if the current ioctl has completed. If the current ioctl is still
7695  * in progress it simply returns. The current ioctl could be waiting for
7696  * a response from another module (arp_ or the driver or could be waiting for
7697  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7698  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7699  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7700  * ipsq_current_ipif is clear which happens only on ioctl completion.
7701  */
7702 
7703 /*
7704  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7705  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7706  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7707  * completion.
7708  */
7709 ipsq_t *
7710 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7711     ipsq_func_t func, int type, boolean_t reentry_ok)
7712 {
7713 	ipsq_t	*ipsq;
7714 
7715 	/* Only 1 of ipif or ill can be specified */
7716 	ASSERT((ipif != NULL) ^ (ill != NULL));
7717 	if (ipif != NULL)
7718 		ill = ipif->ipif_ill;
7719 
7720 	/*
7721 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7722 	 * ipsq of an ill can't change when ill_lock is held.
7723 	 */
7724 	GRAB_CONN_LOCK(q);
7725 	mutex_enter(&ill->ill_lock);
7726 	ipsq = ill->ill_phyint->phyint_ipsq;
7727 	mutex_enter(&ipsq->ipsq_lock);
7728 
7729 	/*
7730 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7731 	 *    (Note: If the caller does not specify reentry_ok then neither
7732 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7733 	 *    again. Otherwise it can lead to an infinite loop
7734 	 * 2. Enter the ipsq if there is no current writer and this attempted
7735 	 *    entry is part of the current ioctl or operation
7736 	 * 3. Enter the ipsq if there is no current writer and this is a new
7737 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7738 	 *    empty and there is no ioctl (or operation) currently in progress
7739 	 */
7740 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7741 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7742 	    ipsq->ipsq_current_ipif == NULL))) ||
7743 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7744 		/* Success. */
7745 		ipsq->ipsq_reentry_cnt++;
7746 		ipsq->ipsq_writer = curthread;
7747 		mutex_exit(&ipsq->ipsq_lock);
7748 		mutex_exit(&ill->ill_lock);
7749 		RELEASE_CONN_LOCK(q);
7750 #ifdef ILL_DEBUG
7751 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7752 #endif
7753 		return (ipsq);
7754 	}
7755 
7756 	ipsq_enq(ipsq, q, mp, func, type, ill);
7757 
7758 	mutex_exit(&ipsq->ipsq_lock);
7759 	mutex_exit(&ill->ill_lock);
7760 	RELEASE_CONN_LOCK(q);
7761 	return (NULL);
7762 }
7763 
7764 /*
7765  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7766  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7767  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7768  * completion.
7769  *
7770  * This function does a refrele on the ipif/ill.
7771  */
7772 void
7773 qwriter_ip(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7774     ipsq_func_t func, int type, boolean_t reentry_ok)
7775 {
7776 	ipsq_t	*ipsq;
7777 
7778 	ipsq = ipsq_try_enter(ipif, ill, q, mp, func, type, reentry_ok);
7779 	/*
7780 	 * Caller must have done a refhold on the ipif. ipif_refrele
7781 	 * happens on the passed ipif. We can do this since we are
7782 	 * already exclusive, or we won't access ipif henceforth, Both
7783 	 * this func and caller will just return if we ipsq_try_enter
7784 	 * fails above. This is needed because func needs to
7785 	 * see the correct refcount. Eg. removeif can work only then.
7786 	 */
7787 	if (ipif != NULL)
7788 		ipif_refrele(ipif);
7789 	else
7790 		ill_refrele(ill);
7791 	if (ipsq != NULL) {
7792 		(*func)(ipsq, q, mp, NULL);
7793 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7794 	}
7795 }
7796 
7797 /*
7798  * If there are more than ILL_GRP_CNT ills in a group,
7799  * we use kmem alloc'd buffers, else use the stack
7800  */
7801 #define	ILL_GRP_CNT	14
7802 /*
7803  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7804  * Called by a thread that is currently exclusive on this ipsq.
7805  */
7806 void
7807 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7808 {
7809 	queue_t	*q;
7810 	mblk_t	*mp;
7811 	ipsq_func_t	func;
7812 	int	next;
7813 	ill_t	**ill_list = NULL;
7814 	size_t	ill_list_size = 0;
7815 	int	cnt = 0;
7816 	boolean_t need_ipsq_free = B_FALSE;
7817 
7818 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7819 	mutex_enter(&ipsq->ipsq_lock);
7820 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7821 	if (ipsq->ipsq_reentry_cnt != 1) {
7822 		ipsq->ipsq_reentry_cnt--;
7823 		mutex_exit(&ipsq->ipsq_lock);
7824 		return;
7825 	}
7826 
7827 	mp = ipsq_dq(ipsq);
7828 	while (mp != NULL) {
7829 again:
7830 		mutex_exit(&ipsq->ipsq_lock);
7831 		func = (ipsq_func_t)mp->b_prev;
7832 		q = (queue_t *)mp->b_queue;
7833 		mp->b_prev = NULL;
7834 		mp->b_queue = NULL;
7835 
7836 		/*
7837 		 * If 'q' is an conn queue, it is valid, since we did a
7838 		 * a refhold on the connp, at the start of the ioctl.
7839 		 * If 'q' is an ill queue, it is valid, since close of an
7840 		 * ill will clean up the 'ipsq'.
7841 		 */
7842 		(*func)(ipsq, q, mp, NULL);
7843 
7844 		mutex_enter(&ipsq->ipsq_lock);
7845 		mp = ipsq_dq(ipsq);
7846 	}
7847 
7848 	mutex_exit(&ipsq->ipsq_lock);
7849 
7850 	/*
7851 	 * Need to grab the locks in the right order. Need to
7852 	 * atomically check (under ipsq_lock) that there are no
7853 	 * messages before relinquishing the ipsq. Also need to
7854 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7855 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7856 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7857 	 * to grab ill_g_lock as writer.
7858 	 */
7859 	rw_enter(&ill_g_lock, ipsq->ipsq_split ? RW_WRITER : RW_READER);
7860 
7861 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7862 	if (ipsq->ipsq_refs != 0) {
7863 		/* At most 2 ills v4/v6 per phyint */
7864 		cnt = ipsq->ipsq_refs << 1;
7865 		ill_list_size = cnt * sizeof (ill_t *);
7866 		/*
7867 		 * If memory allocation fails, we will do the split
7868 		 * the next time ipsq_exit is called for whatever reason.
7869 		 * As long as the ipsq_split flag is set the need to
7870 		 * split is remembered.
7871 		 */
7872 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7873 		if (ill_list != NULL)
7874 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7875 	}
7876 	mutex_enter(&ipsq->ipsq_lock);
7877 	mp = ipsq_dq(ipsq);
7878 	if (mp != NULL) {
7879 		/* oops, some message has landed up, we can't get out */
7880 		if (ill_list != NULL)
7881 			ill_unlock_ills(ill_list, cnt);
7882 		rw_exit(&ill_g_lock);
7883 		if (ill_list != NULL)
7884 			kmem_free(ill_list, ill_list_size);
7885 		ill_list = NULL;
7886 		ill_list_size = 0;
7887 		cnt = 0;
7888 		goto again;
7889 	}
7890 
7891 	/*
7892 	 * Split only if no ioctl is pending and if memory alloc succeeded
7893 	 * above.
7894 	 */
7895 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7896 		ill_list != NULL) {
7897 		/*
7898 		 * No new ill can join this ipsq since we are holding the
7899 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7900 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7901 		 * If so we will retry on the next ipsq_exit.
7902 		 */
7903 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7904 	}
7905 
7906 	/*
7907 	 * We are holding the ipsq lock, hence no new messages can
7908 	 * land up on the ipsq, and there are no messages currently.
7909 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7910 	 * atomically while holding ill locks.
7911 	 */
7912 	ipsq->ipsq_writer = NULL;
7913 	ipsq->ipsq_reentry_cnt--;
7914 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7915 #ifdef ILL_DEBUG
7916 	ipsq->ipsq_depth = 0;
7917 #endif
7918 	mutex_exit(&ipsq->ipsq_lock);
7919 	/*
7920 	 * For IPMP this should wake up all ills in this ipsq.
7921 	 * We need to hold the ill_lock while waking up waiters to
7922 	 * avoid missed wakeups. But there is no need to acquire all
7923 	 * the ill locks and then wakeup. If we have not acquired all
7924 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7925 	 * wakes up ills one at a time after getting the right ill_lock
7926 	 */
7927 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7928 	if (ill_list != NULL)
7929 		ill_unlock_ills(ill_list, cnt);
7930 	if (ipsq->ipsq_refs == 0)
7931 		need_ipsq_free = B_TRUE;
7932 	rw_exit(&ill_g_lock);
7933 	if (ill_list != 0)
7934 		kmem_free(ill_list, ill_list_size);
7935 
7936 	if (need_ipsq_free) {
7937 		/*
7938 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7939 		 * looked up. ipsq can be looked up only thru ill or phyint
7940 		 * and there are no ills/phyint on this ipsq.
7941 		 */
7942 		ipsq_delete(ipsq);
7943 	}
7944 	/*
7945 	 * Now start any igmp or mld timers that could not be started
7946 	 * while inside the ipsq. The timers can't be started while inside
7947 	 * the ipsq, since igmp_start_timers may need to call untimeout()
7948 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
7949 	 * there could be a deadlock since the timeout handlers
7950 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
7951 	 * wait in ipsq_enter() trying to get the ipsq.
7952 	 *
7953 	 * However there is one exception to the above. If this thread is
7954 	 * itself the igmp/mld timeout handler thread, then we don't want
7955 	 * to start any new timer until the current handler is done. The
7956 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
7957 	 * all others pass B_TRUE.
7958 	 */
7959 	if (start_igmp_timer) {
7960 		mutex_enter(&igmp_timer_lock);
7961 		next = igmp_deferred_next;
7962 		igmp_deferred_next = INFINITY;
7963 		mutex_exit(&igmp_timer_lock);
7964 
7965 		if (next != INFINITY)
7966 			igmp_start_timers(next);
7967 	}
7968 
7969 	if (start_mld_timer) {
7970 		mutex_enter(&mld_timer_lock);
7971 		next = mld_deferred_next;
7972 		mld_deferred_next = INFINITY;
7973 		mutex_exit(&mld_timer_lock);
7974 
7975 		if (next != INFINITY)
7976 			mld_start_timers(next);
7977 	}
7978 }
7979 
7980 /*
7981  * The ill is closing. Flush all messages on the ipsq that originated
7982  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
7983  * for this ill since ipsq_enter could not have entered until then.
7984  * New messages can't be queued since the CONDEMNED flag is set.
7985  */
7986 static void
7987 ipsq_flush(ill_t *ill)
7988 {
7989 	queue_t	*q;
7990 	mblk_t	*prev;
7991 	mblk_t	*mp;
7992 	mblk_t	*mp_next;
7993 	ipsq_t	*ipsq;
7994 
7995 	ASSERT(IAM_WRITER_ILL(ill));
7996 	ipsq = ill->ill_phyint->phyint_ipsq;
7997 	/*
7998 	 * Flush any messages sent up by the driver.
7999 	 */
8000 	mutex_enter(&ipsq->ipsq_lock);
8001 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8002 		mp_next = mp->b_next;
8003 		q = mp->b_queue;
8004 		if (q == ill->ill_rq || q == ill->ill_wq) {
8005 			/* Remove the mp from the ipsq */
8006 			if (prev == NULL)
8007 				ipsq->ipsq_mphead = mp->b_next;
8008 			else
8009 				prev->b_next = mp->b_next;
8010 			if (ipsq->ipsq_mptail == mp) {
8011 				ASSERT(mp_next == NULL);
8012 				ipsq->ipsq_mptail = prev;
8013 			}
8014 			inet_freemsg(mp);
8015 		} else {
8016 			prev = mp;
8017 		}
8018 	}
8019 	mutex_exit(&ipsq->ipsq_lock);
8020 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8021 	ipsq_xopq_mp_cleanup(ill, NULL);
8022 	ill_pending_mp_cleanup(ill);
8023 }
8024 
8025 /*
8026  * Clean up one squeue element. ill_inuse_ref is protected by ill_lock.
8027  * The real cleanup happens behind the squeue via ip_squeue_clean function but
8028  * we need to protect ourselfs from 2 threads trying to cleanup at the same
8029  * time (possible with one port going down for aggr and someone tearing down the
8030  * entire aggr simultaneously. So we use ill_inuse_ref protected by ill_lock
8031  * to indicate when the cleanup has started (1 ref) and when the cleanup
8032  * is done (0 ref). When a new ring gets assigned to squeue, we start by
8033  * putting 2 ref on ill_inuse_ref.
8034  */
8035 static void
8036 ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring)
8037 {
8038 	conn_t *connp;
8039 	squeue_t *sqp;
8040 	mblk_t *mp;
8041 
8042 	ASSERT(rx_ring != NULL);
8043 
8044 	/* Just clean one squeue */
8045 	mutex_enter(&ill->ill_lock);
8046 	/*
8047 	 * Reset the ILL_SOFT_RING_ASSIGN bit so that
8048 	 * ip_squeue_soft_ring_affinty() will not go
8049 	 * ahead with assigning rings.
8050 	 */
8051 	ill->ill_state_flags &= ~ILL_SOFT_RING_ASSIGN;
8052 	while (rx_ring->rr_ring_state == ILL_RING_INPROC)
8053 		/* Some operations pending on the ring. Wait */
8054 		cv_wait(&ill->ill_cv, &ill->ill_lock);
8055 
8056 	if (rx_ring->rr_ring_state != ILL_RING_INUSE) {
8057 		/*
8058 		 * Someone already trying to clean
8059 		 * this squeue or its already been cleaned.
8060 		 */
8061 		mutex_exit(&ill->ill_lock);
8062 		return;
8063 	}
8064 	sqp = rx_ring->rr_sqp;
8065 
8066 	if (sqp == NULL) {
8067 		/*
8068 		 * The rx_ring never had a squeue assigned to it.
8069 		 * We are under ill_lock so we can clean it up
8070 		 * here itself since no one can get to it.
8071 		 */
8072 		rx_ring->rr_blank = NULL;
8073 		rx_ring->rr_handle = NULL;
8074 		rx_ring->rr_sqp = NULL;
8075 		rx_ring->rr_ring_state = ILL_RING_FREE;
8076 		mutex_exit(&ill->ill_lock);
8077 		return;
8078 	}
8079 
8080 	/* Set the state that its being cleaned */
8081 	rx_ring->rr_ring_state = ILL_RING_BEING_FREED;
8082 	ASSERT(sqp != NULL);
8083 	mutex_exit(&ill->ill_lock);
8084 
8085 	/*
8086 	 * Use the preallocated ill_unbind_conn for this purpose
8087 	 */
8088 	connp = ill->ill_dls_capab->ill_unbind_conn;
8089 
8090 	ASSERT(!connp->conn_tcp->tcp_closemp.b_prev);
8091 	TCP_DEBUG_GETPCSTACK(connp->conn_tcp->tcmp_stk, 15);
8092 	if (connp->conn_tcp->tcp_closemp.b_prev == NULL)
8093 		connp->conn_tcp->tcp_closemp_used = 1;
8094 	else
8095 		connp->conn_tcp->tcp_closemp_used++;
8096 	mp = &connp->conn_tcp->tcp_closemp;
8097 	CONN_INC_REF(connp);
8098 	squeue_enter(sqp, mp, ip_squeue_clean, connp, NULL);
8099 
8100 	mutex_enter(&ill->ill_lock);
8101 	while (rx_ring->rr_ring_state != ILL_RING_FREE)
8102 		cv_wait(&ill->ill_cv, &ill->ill_lock);
8103 
8104 	mutex_exit(&ill->ill_lock);
8105 }
8106 
8107 static void
8108 ipsq_clean_all(ill_t *ill)
8109 {
8110 	int idx;
8111 
8112 	/*
8113 	 * No need to clean if poll_capab isn't set for this ill
8114 	 */
8115 	if (!(ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING)))
8116 		return;
8117 
8118 	for (idx = 0; idx < ILL_MAX_RINGS; idx++) {
8119 		ill_rx_ring_t *ipr = &ill->ill_dls_capab->ill_ring_tbl[idx];
8120 		ipsq_clean_ring(ill, ipr);
8121 	}
8122 
8123 	ill->ill_capabilities &= ~(ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING);
8124 }
8125 
8126 /* ARGSUSED */
8127 int
8128 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8129     ip_ioctl_cmd_t *ipip, void *ifreq)
8130 {
8131 	ill_t	*ill;
8132 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8133 	boolean_t isv6;
8134 	conn_t	*connp;
8135 
8136 	connp = Q_TO_CONN(q);
8137 	isv6 = connp->conn_af_isv6;
8138 	/*
8139 	 * Set original index.
8140 	 * Failover and failback move logical interfaces
8141 	 * from one physical interface to another.  The
8142 	 * original index indicates the parent of a logical
8143 	 * interface, in other words, the physical interface
8144 	 * the logical interface will be moved back to on
8145 	 * failback.
8146 	 */
8147 
8148 	/*
8149 	 * Don't allow the original index to be changed
8150 	 * for non-failover addresses, autoconfigured
8151 	 * addresses, or IPv6 link local addresses.
8152 	 */
8153 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8154 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8155 		return (EINVAL);
8156 	}
8157 	/*
8158 	 * The new original index must be in use by some
8159 	 * physical interface.
8160 	 */
8161 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8162 	    NULL, NULL);
8163 	if (ill == NULL)
8164 		return (ENXIO);
8165 	ill_refrele(ill);
8166 
8167 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8168 	/*
8169 	 * When this ipif gets failed back, don't
8170 	 * preserve the original id, as it is no
8171 	 * longer applicable.
8172 	 */
8173 	ipif->ipif_orig_ipifid = 0;
8174 	/*
8175 	 * For IPv4, change the original index of any
8176 	 * multicast addresses associated with the
8177 	 * ipif to the new value.
8178 	 */
8179 	if (!isv6) {
8180 		ilm_t *ilm;
8181 
8182 		mutex_enter(&ipif->ipif_ill->ill_lock);
8183 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8184 		    ilm = ilm->ilm_next) {
8185 			if (ilm->ilm_ipif == ipif) {
8186 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8187 			}
8188 		}
8189 		mutex_exit(&ipif->ipif_ill->ill_lock);
8190 	}
8191 	return (0);
8192 }
8193 
8194 /* ARGSUSED */
8195 int
8196 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8197     ip_ioctl_cmd_t *ipip, void *ifreq)
8198 {
8199 	struct lifreq *lifr = (struct lifreq *)ifreq;
8200 
8201 	/*
8202 	 * Get the original interface index i.e the one
8203 	 * before FAILOVER if it ever happened.
8204 	 */
8205 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8206 	return (0);
8207 }
8208 
8209 /*
8210  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8211  * refhold and return the associated ipif
8212  */
8213 int
8214 ip_extract_tunreq(queue_t *q, mblk_t *mp, ipif_t **ipifp, ipsq_func_t func)
8215 {
8216 	boolean_t exists;
8217 	struct iftun_req *ta;
8218 	ipif_t	*ipif;
8219 	ill_t	*ill;
8220 	boolean_t isv6;
8221 	mblk_t	*mp1;
8222 	int	error;
8223 	conn_t	*connp;
8224 
8225 	/* Existence verified in ip_wput_nondata */
8226 	mp1 = mp->b_cont->b_cont;
8227 	ta = (struct iftun_req *)mp1->b_rptr;
8228 	/*
8229 	 * Null terminate the string to protect against buffer
8230 	 * overrun. String was generated by user code and may not
8231 	 * be trusted.
8232 	 */
8233 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8234 
8235 	connp = Q_TO_CONN(q);
8236 	isv6 = connp->conn_af_isv6;
8237 
8238 	/* Disallows implicit create */
8239 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8240 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8241 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error);
8242 	if (ipif == NULL)
8243 		return (error);
8244 
8245 	if (ipif->ipif_id != 0) {
8246 		/*
8247 		 * We really don't want to set/get tunnel parameters
8248 		 * on virtual tunnel interfaces.  Only allow the
8249 		 * base tunnel to do these.
8250 		 */
8251 		ipif_refrele(ipif);
8252 		return (EINVAL);
8253 	}
8254 
8255 	/*
8256 	 * Send down to tunnel mod for ioctl processing.
8257 	 * Will finish ioctl in ip_rput_other().
8258 	 */
8259 	ill = ipif->ipif_ill;
8260 	if (ill->ill_net_type == IRE_LOOPBACK) {
8261 		ipif_refrele(ipif);
8262 		return (EOPNOTSUPP);
8263 	}
8264 
8265 	if (ill->ill_wq == NULL) {
8266 		ipif_refrele(ipif);
8267 		return (ENXIO);
8268 	}
8269 	/*
8270 	 * Mark the ioctl as coming from an IPv6 interface for
8271 	 * tun's convenience.
8272 	 */
8273 	if (ill->ill_isv6)
8274 		ta->ifta_flags |= 0x80000000;
8275 	*ipifp = ipif;
8276 	return (0);
8277 }
8278 
8279 /*
8280  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8281  * and return the associated ipif.
8282  * Return value:
8283  *	Non zero: An error has occurred. ci may not be filled out.
8284  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8285  *	a held ipif in ci.ci_ipif.
8286  */
8287 int
8288 ip_extract_lifreq_cmn(queue_t *q, mblk_t *mp, int cmd_type, int flags,
8289     cmd_info_t *ci, ipsq_func_t func)
8290 {
8291 	sin_t		*sin;
8292 	sin6_t		*sin6;
8293 	char		*name;
8294 	struct ifreq    *ifr;
8295 	struct lifreq    *lifr;
8296 	ipif_t		*ipif = NULL;
8297 	ill_t		*ill;
8298 	conn_t		*connp;
8299 	boolean_t	isv6;
8300 	struct iocblk   *iocp = (struct iocblk *)mp->b_rptr;
8301 	boolean_t	exists;
8302 	int		err;
8303 	mblk_t		*mp1;
8304 	zoneid_t	zoneid;
8305 
8306 	if (q->q_next != NULL) {
8307 		ill = (ill_t *)q->q_ptr;
8308 		isv6 = ill->ill_isv6;
8309 		connp = NULL;
8310 		zoneid = ALL_ZONES;
8311 	} else {
8312 		ill = NULL;
8313 		connp = Q_TO_CONN(q);
8314 		isv6 = connp->conn_af_isv6;
8315 		zoneid = connp->conn_zoneid;
8316 		if (zoneid == GLOBAL_ZONEID) {
8317 			/* global zone can access ipifs in all zones */
8318 			zoneid = ALL_ZONES;
8319 		}
8320 	}
8321 
8322 	/* Has been checked in ip_wput_nondata */
8323 	mp1 = mp->b_cont->b_cont;
8324 
8325 
8326 	if (cmd_type == IF_CMD) {
8327 		/* This a old style SIOC[GS]IF* command */
8328 		ifr = (struct ifreq *)mp1->b_rptr;
8329 		/*
8330 		 * Null terminate the string to protect against buffer
8331 		 * overrun. String was generated by user code and may not
8332 		 * be trusted.
8333 		 */
8334 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8335 		sin = (sin_t *)&ifr->ifr_addr;
8336 		name = ifr->ifr_name;
8337 		ci->ci_sin = sin;
8338 		ci->ci_sin6 = NULL;
8339 		ci->ci_lifr = (struct lifreq *)ifr;
8340 	} else {
8341 		/* This a new style SIOC[GS]LIF* command */
8342 		ASSERT(cmd_type == LIF_CMD);
8343 		lifr = (struct lifreq *)mp1->b_rptr;
8344 		/*
8345 		 * Null terminate the string to protect against buffer
8346 		 * overrun. String was generated by user code and may not
8347 		 * be trusted.
8348 		 */
8349 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8350 		name = lifr->lifr_name;
8351 		sin = (sin_t *)&lifr->lifr_addr;
8352 		sin6 = (sin6_t *)&lifr->lifr_addr;
8353 		if (iocp->ioc_cmd == SIOCSLIFGROUPNAME) {
8354 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8355 			    LIFNAMSIZ);
8356 		}
8357 		ci->ci_sin = sin;
8358 		ci->ci_sin6 = sin6;
8359 		ci->ci_lifr = lifr;
8360 	}
8361 
8362 
8363 	if (iocp->ioc_cmd == SIOCSLIFNAME) {
8364 		/*
8365 		 * The ioctl will be failed if the ioctl comes down
8366 		 * an conn stream
8367 		 */
8368 		if (ill == NULL) {
8369 			/*
8370 			 * Not an ill queue, return EINVAL same as the
8371 			 * old error code.
8372 			 */
8373 			return (ENXIO);
8374 		}
8375 		ipif = ill->ill_ipif;
8376 		ipif_refhold(ipif);
8377 	} else {
8378 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8379 		    &exists, isv6, zoneid,
8380 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err);
8381 		if (ipif == NULL) {
8382 			if (err == EINPROGRESS)
8383 				return (err);
8384 			if (iocp->ioc_cmd == SIOCLIFFAILOVER ||
8385 			    iocp->ioc_cmd == SIOCLIFFAILBACK) {
8386 				/*
8387 				 * Need to try both v4 and v6 since this
8388 				 * ioctl can come down either v4 or v6
8389 				 * socket. The lifreq.lifr_family passed
8390 				 * down by this ioctl is AF_UNSPEC.
8391 				 */
8392 				ipif = ipif_lookup_on_name(name,
8393 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8394 				    zoneid, (connp == NULL) ? q :
8395 				    CONNP_TO_WQ(connp), mp, func, &err);
8396 				if (err == EINPROGRESS)
8397 					return (err);
8398 			}
8399 			err = 0;	/* Ensure we don't use it below */
8400 		}
8401 	}
8402 
8403 	/*
8404 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8405 	 */
8406 	if (ipif != NULL && ipif->ipif_isv6 && cmd_type == IF_CMD) {
8407 		ipif_refrele(ipif);
8408 		return (ENXIO);
8409 	}
8410 
8411 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8412 	    name[0] == '\0') {
8413 		/*
8414 		 * Handle a or a SIOC?IF* with a null name
8415 		 * during plumb (on the ill queue before the I_PLINK).
8416 		 */
8417 		ipif = ill->ill_ipif;
8418 		ipif_refhold(ipif);
8419 	}
8420 
8421 	if (ipif == NULL)
8422 		return (ENXIO);
8423 
8424 	/*
8425 	 * Allow only GET operations if this ipif has been created
8426 	 * temporarily due to a MOVE operation.
8427 	 */
8428 	if (ipif->ipif_replace_zero && !(flags & IPI_REPL)) {
8429 		ipif_refrele(ipif);
8430 		return (EINVAL);
8431 	}
8432 
8433 	ci->ci_ipif = ipif;
8434 	return (0);
8435 }
8436 
8437 /*
8438  * Return the total number of ipifs.
8439  */
8440 static uint_t
8441 ip_get_numifs(zoneid_t zoneid)
8442 {
8443 	uint_t numifs = 0;
8444 	ill_t	*ill;
8445 	ill_walk_context_t	ctx;
8446 	ipif_t	*ipif;
8447 
8448 	rw_enter(&ill_g_lock, RW_READER);
8449 	ill = ILL_START_WALK_V4(&ctx);
8450 
8451 	while (ill != NULL) {
8452 		for (ipif = ill->ill_ipif; ipif != NULL;
8453 		    ipif = ipif->ipif_next) {
8454 			if (ipif->ipif_zoneid == zoneid ||
8455 			    ipif->ipif_zoneid == ALL_ZONES)
8456 				numifs++;
8457 		}
8458 		ill = ill_next(&ctx, ill);
8459 	}
8460 	rw_exit(&ill_g_lock);
8461 	return (numifs);
8462 }
8463 
8464 /*
8465  * Return the total number of ipifs.
8466  */
8467 static uint_t
8468 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid)
8469 {
8470 	uint_t numifs = 0;
8471 	ill_t	*ill;
8472 	ipif_t	*ipif;
8473 	ill_walk_context_t	ctx;
8474 
8475 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8476 
8477 	rw_enter(&ill_g_lock, RW_READER);
8478 	if (family == AF_INET)
8479 		ill = ILL_START_WALK_V4(&ctx);
8480 	else if (family == AF_INET6)
8481 		ill = ILL_START_WALK_V6(&ctx);
8482 	else
8483 		ill = ILL_START_WALK_ALL(&ctx);
8484 
8485 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8486 		for (ipif = ill->ill_ipif; ipif != NULL;
8487 		    ipif = ipif->ipif_next) {
8488 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8489 			    !(lifn_flags & LIFC_NOXMIT))
8490 				continue;
8491 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8492 			    !(lifn_flags & LIFC_TEMPORARY))
8493 				continue;
8494 			if (((ipif->ipif_flags &
8495 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8496 			    IPIF_DEPRECATED)) ||
8497 			    (ill->ill_phyint->phyint_flags &
8498 			    PHYI_LOOPBACK) ||
8499 			    !(ipif->ipif_flags & IPIF_UP)) &&
8500 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8501 				continue;
8502 
8503 			if (zoneid != ipif->ipif_zoneid &&
8504 			    ipif->ipif_zoneid != ALL_ZONES &&
8505 			    (zoneid != GLOBAL_ZONEID ||
8506 			    !(lifn_flags & LIFC_ALLZONES)))
8507 				continue;
8508 
8509 			numifs++;
8510 		}
8511 	}
8512 	rw_exit(&ill_g_lock);
8513 	return (numifs);
8514 }
8515 
8516 uint_t
8517 ip_get_lifsrcofnum(ill_t *ill)
8518 {
8519 	uint_t numifs = 0;
8520 	ill_t	*ill_head = ill;
8521 
8522 	/*
8523 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8524 	 * other thread may be trying to relink the ILLs in this usesrc group
8525 	 * and adjusting the ill_usesrc_grp_next pointers
8526 	 */
8527 	rw_enter(&ill_g_usesrc_lock, RW_READER);
8528 	if ((ill->ill_usesrc_ifindex == 0) &&
8529 	    (ill->ill_usesrc_grp_next != NULL)) {
8530 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8531 		    ill = ill->ill_usesrc_grp_next)
8532 			numifs++;
8533 	}
8534 	rw_exit(&ill_g_usesrc_lock);
8535 
8536 	return (numifs);
8537 }
8538 
8539 /* Null values are passed in for ipif, sin, and ifreq */
8540 /* ARGSUSED */
8541 int
8542 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8543     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8544 {
8545 	int *nump;
8546 
8547 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8548 
8549 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8550 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8551 
8552 	*nump = ip_get_numifs(Q_TO_CONN(q)->conn_zoneid);
8553 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8554 	return (0);
8555 }
8556 
8557 /* Null values are passed in for ipif, sin, and ifreq */
8558 /* ARGSUSED */
8559 int
8560 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8561     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8562 {
8563 	struct lifnum *lifn;
8564 	mblk_t	*mp1;
8565 
8566 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8567 
8568 	/* Existence checked in ip_wput_nondata */
8569 	mp1 = mp->b_cont->b_cont;
8570 
8571 	lifn = (struct lifnum *)mp1->b_rptr;
8572 	switch (lifn->lifn_family) {
8573 	case AF_UNSPEC:
8574 	case AF_INET:
8575 	case AF_INET6:
8576 		break;
8577 	default:
8578 		return (EAFNOSUPPORT);
8579 	}
8580 
8581 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8582 	    Q_TO_CONN(q)->conn_zoneid);
8583 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8584 	return (0);
8585 }
8586 
8587 /* ARGSUSED */
8588 int
8589 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8590     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8591 {
8592 	STRUCT_HANDLE(ifconf, ifc);
8593 	mblk_t *mp1;
8594 	struct iocblk *iocp;
8595 	struct ifreq *ifr;
8596 	ill_walk_context_t	ctx;
8597 	ill_t	*ill;
8598 	ipif_t	*ipif;
8599 	struct sockaddr_in *sin;
8600 	int32_t	ifclen;
8601 	zoneid_t zoneid;
8602 
8603 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8604 
8605 	ip1dbg(("ip_sioctl_get_ifconf"));
8606 	/* Existence verified in ip_wput_nondata */
8607 	mp1 = mp->b_cont->b_cont;
8608 	iocp = (struct iocblk *)mp->b_rptr;
8609 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8610 
8611 	/*
8612 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8613 	 * the user buffer address and length into which the list of struct
8614 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8615 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8616 	 * the SIOCGIFCONF operation was redefined to simply provide
8617 	 * a large output buffer into which we are supposed to jam the ifreq
8618 	 * array.  The same ioctl command code was used, despite the fact that
8619 	 * both the applications and the kernel code had to change, thus making
8620 	 * it impossible to support both interfaces.
8621 	 *
8622 	 * For reasons not good enough to try to explain, the following
8623 	 * algorithm is used for deciding what to do with one of these:
8624 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8625 	 * form with the output buffer coming down as the continuation message.
8626 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8627 	 * and we have to copy in the ifconf structure to find out how big the
8628 	 * output buffer is and where to copy out to.  Sure no problem...
8629 	 *
8630 	 */
8631 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8632 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8633 		int numifs = 0;
8634 		size_t ifc_bufsize;
8635 
8636 		/*
8637 		 * Must be (better be!) continuation of a TRANSPARENT
8638 		 * IOCTL.  We just copied in the ifconf structure.
8639 		 */
8640 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8641 		    (struct ifconf *)mp1->b_rptr);
8642 
8643 		/*
8644 		 * Allocate a buffer to hold requested information.
8645 		 *
8646 		 * If ifc_len is larger than what is needed, we only
8647 		 * allocate what we will use.
8648 		 *
8649 		 * If ifc_len is smaller than what is needed, return
8650 		 * EINVAL.
8651 		 *
8652 		 * XXX: the ill_t structure can hava 2 counters, for
8653 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8654 		 * number of interfaces for a device, so we don't need
8655 		 * to count them here...
8656 		 */
8657 		numifs = ip_get_numifs(zoneid);
8658 
8659 		ifclen = STRUCT_FGET(ifc, ifc_len);
8660 		ifc_bufsize = numifs * sizeof (struct ifreq);
8661 		if (ifc_bufsize > ifclen) {
8662 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8663 				/* old behaviour */
8664 				return (EINVAL);
8665 			} else {
8666 				ifc_bufsize = ifclen;
8667 			}
8668 		}
8669 
8670 		mp1 = mi_copyout_alloc(q, mp,
8671 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8672 		if (mp1 == NULL)
8673 			return (ENOMEM);
8674 
8675 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8676 	}
8677 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8678 	/*
8679 	 * the SIOCGIFCONF ioctl only knows about
8680 	 * IPv4 addresses, so don't try to tell
8681 	 * it about interfaces with IPv6-only
8682 	 * addresses. (Last parm 'isv6' is B_FALSE)
8683 	 */
8684 
8685 	ifr = (struct ifreq *)mp1->b_rptr;
8686 
8687 	rw_enter(&ill_g_lock, RW_READER);
8688 	ill = ILL_START_WALK_V4(&ctx);
8689 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8690 		for (ipif = ill->ill_ipif; ipif != NULL;
8691 		    ipif = ipif->ipif_next) {
8692 			if (zoneid != ipif->ipif_zoneid &&
8693 			    ipif->ipif_zoneid != ALL_ZONES)
8694 				continue;
8695 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8696 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8697 					/* old behaviour */
8698 					rw_exit(&ill_g_lock);
8699 					return (EINVAL);
8700 				} else {
8701 					goto if_copydone;
8702 				}
8703 			}
8704 			(void) ipif_get_name(ipif,
8705 			    ifr->ifr_name,
8706 			    sizeof (ifr->ifr_name));
8707 			sin = (sin_t *)&ifr->ifr_addr;
8708 			*sin = sin_null;
8709 			sin->sin_family = AF_INET;
8710 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8711 			ifr++;
8712 		}
8713 	}
8714 if_copydone:
8715 	rw_exit(&ill_g_lock);
8716 	mp1->b_wptr = (uchar_t *)ifr;
8717 
8718 	if (STRUCT_BUF(ifc) != NULL) {
8719 		STRUCT_FSET(ifc, ifc_len,
8720 			(int)((uchar_t *)ifr - mp1->b_rptr));
8721 	}
8722 	return (0);
8723 }
8724 
8725 /*
8726  * Get the interfaces using the address hosted on the interface passed in,
8727  * as a source adddress
8728  */
8729 /* ARGSUSED */
8730 int
8731 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8732     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8733 {
8734 	mblk_t *mp1;
8735 	ill_t	*ill, *ill_head;
8736 	ipif_t	*ipif, *orig_ipif;
8737 	int	numlifs = 0;
8738 	size_t	lifs_bufsize, lifsmaxlen;
8739 	struct	lifreq *lifr;
8740 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8741 	uint_t	ifindex;
8742 	zoneid_t zoneid;
8743 	int err = 0;
8744 	boolean_t isv6 = B_FALSE;
8745 	struct	sockaddr_in	*sin;
8746 	struct	sockaddr_in6	*sin6;
8747 
8748 	STRUCT_HANDLE(lifsrcof, lifs);
8749 
8750 	ASSERT(q->q_next == NULL);
8751 
8752 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8753 
8754 	/* Existence verified in ip_wput_nondata */
8755 	mp1 = mp->b_cont->b_cont;
8756 
8757 	/*
8758 	 * Must be (better be!) continuation of a TRANSPARENT
8759 	 * IOCTL.  We just copied in the lifsrcof structure.
8760 	 */
8761 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8762 	    (struct lifsrcof *)mp1->b_rptr);
8763 
8764 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8765 		return (EINVAL);
8766 
8767 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8768 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8769 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8770 	    ip_process_ioctl, &err);
8771 	if (ipif == NULL) {
8772 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8773 		    ifindex));
8774 		return (err);
8775 	}
8776 
8777 
8778 	/* Allocate a buffer to hold requested information */
8779 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8780 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8781 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8782 	/* The actual size needed is always returned in lifs_len */
8783 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8784 
8785 	/* If the amount we need is more than what is passed in, abort */
8786 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8787 		ipif_refrele(ipif);
8788 		return (0);
8789 	}
8790 
8791 	mp1 = mi_copyout_alloc(q, mp,
8792 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8793 	if (mp1 == NULL) {
8794 		ipif_refrele(ipif);
8795 		return (ENOMEM);
8796 	}
8797 
8798 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8799 	bzero(mp1->b_rptr, lifs_bufsize);
8800 
8801 	lifr = (struct lifreq *)mp1->b_rptr;
8802 
8803 	ill = ill_head = ipif->ipif_ill;
8804 	orig_ipif = ipif;
8805 
8806 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8807 	rw_enter(&ill_g_usesrc_lock, RW_READER);
8808 	rw_enter(&ill_g_lock, RW_READER);
8809 
8810 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8811 	for (; (ill != NULL) && (ill != ill_head);
8812 	    ill = ill->ill_usesrc_grp_next) {
8813 
8814 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8815 			break;
8816 
8817 		ipif = ill->ill_ipif;
8818 		(void) ipif_get_name(ipif,
8819 		    lifr->lifr_name, sizeof (lifr->lifr_name));
8820 		if (ipif->ipif_isv6) {
8821 			sin6 = (sin6_t *)&lifr->lifr_addr;
8822 			*sin6 = sin6_null;
8823 			sin6->sin6_family = AF_INET6;
8824 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8825 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8826 			    &ipif->ipif_v6net_mask);
8827 		} else {
8828 			sin = (sin_t *)&lifr->lifr_addr;
8829 			*sin = sin_null;
8830 			sin->sin_family = AF_INET;
8831 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8832 			lifr->lifr_addrlen = ip_mask_to_plen(
8833 			    ipif->ipif_net_mask);
8834 		}
8835 		lifr++;
8836 	}
8837 	rw_exit(&ill_g_usesrc_lock);
8838 	rw_exit(&ill_g_lock);
8839 	ipif_refrele(orig_ipif);
8840 	mp1->b_wptr = (uchar_t *)lifr;
8841 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8842 
8843 	return (0);
8844 }
8845 
8846 /* ARGSUSED */
8847 int
8848 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8849     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8850 {
8851 	mblk_t *mp1;
8852 	int	list;
8853 	ill_t	*ill;
8854 	ipif_t	*ipif;
8855 	int	flags;
8856 	int	numlifs = 0;
8857 	size_t	lifc_bufsize;
8858 	struct	lifreq *lifr;
8859 	sa_family_t	family;
8860 	struct	sockaddr_in	*sin;
8861 	struct	sockaddr_in6	*sin6;
8862 	ill_walk_context_t	ctx;
8863 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8864 	int32_t	lifclen;
8865 	zoneid_t zoneid;
8866 	STRUCT_HANDLE(lifconf, lifc);
8867 
8868 	ip1dbg(("ip_sioctl_get_lifconf"));
8869 
8870 	ASSERT(q->q_next == NULL);
8871 
8872 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8873 
8874 	/* Existence verified in ip_wput_nondata */
8875 	mp1 = mp->b_cont->b_cont;
8876 
8877 	/*
8878 	 * An extended version of SIOCGIFCONF that takes an
8879 	 * additional address family and flags field.
8880 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8881 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8882 	 * interfaces are omitted.
8883 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8884 	 * unless LIFC_TEMPORARY is specified.
8885 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8886 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8887 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8888 	 * has priority over LIFC_NOXMIT.
8889 	 */
8890 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8891 
8892 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8893 		return (EINVAL);
8894 
8895 	/*
8896 	 * Must be (better be!) continuation of a TRANSPARENT
8897 	 * IOCTL.  We just copied in the lifconf structure.
8898 	 */
8899 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8900 
8901 	family = STRUCT_FGET(lifc, lifc_family);
8902 	flags = STRUCT_FGET(lifc, lifc_flags);
8903 
8904 	switch (family) {
8905 	case AF_UNSPEC:
8906 		/*
8907 		 * walk all ILL's.
8908 		 */
8909 		list = MAX_G_HEADS;
8910 		break;
8911 	case AF_INET:
8912 		/*
8913 		 * walk only IPV4 ILL's.
8914 		 */
8915 		list = IP_V4_G_HEAD;
8916 		break;
8917 	case AF_INET6:
8918 		/*
8919 		 * walk only IPV6 ILL's.
8920 		 */
8921 		list = IP_V6_G_HEAD;
8922 		break;
8923 	default:
8924 		return (EAFNOSUPPORT);
8925 	}
8926 
8927 	/*
8928 	 * Allocate a buffer to hold requested information.
8929 	 *
8930 	 * If lifc_len is larger than what is needed, we only
8931 	 * allocate what we will use.
8932 	 *
8933 	 * If lifc_len is smaller than what is needed, return
8934 	 * EINVAL.
8935 	 */
8936 	numlifs = ip_get_numlifs(family, flags, zoneid);
8937 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8938 	lifclen = STRUCT_FGET(lifc, lifc_len);
8939 	if (lifc_bufsize > lifclen) {
8940 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8941 			return (EINVAL);
8942 		else
8943 			lifc_bufsize = lifclen;
8944 	}
8945 
8946 	mp1 = mi_copyout_alloc(q, mp,
8947 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8948 	if (mp1 == NULL)
8949 		return (ENOMEM);
8950 
8951 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8952 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8953 
8954 	lifr = (struct lifreq *)mp1->b_rptr;
8955 
8956 	rw_enter(&ill_g_lock, RW_READER);
8957 	ill = ill_first(list, list, &ctx);
8958 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8959 		for (ipif = ill->ill_ipif; ipif != NULL;
8960 		    ipif = ipif->ipif_next) {
8961 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8962 			    !(flags & LIFC_NOXMIT))
8963 				continue;
8964 
8965 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8966 			    !(flags & LIFC_TEMPORARY))
8967 				continue;
8968 
8969 			if (((ipif->ipif_flags &
8970 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8971 			    IPIF_DEPRECATED)) ||
8972 			    (ill->ill_phyint->phyint_flags &
8973 			    PHYI_LOOPBACK) ||
8974 			    !(ipif->ipif_flags & IPIF_UP)) &&
8975 			    (flags & LIFC_EXTERNAL_SOURCE))
8976 				continue;
8977 
8978 			if (zoneid != ipif->ipif_zoneid &&
8979 			    ipif->ipif_zoneid != ALL_ZONES &&
8980 			    (zoneid != GLOBAL_ZONEID ||
8981 			    !(flags & LIFC_ALLZONES)))
8982 				continue;
8983 
8984 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8985 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8986 					rw_exit(&ill_g_lock);
8987 					return (EINVAL);
8988 				} else {
8989 					goto lif_copydone;
8990 				}
8991 			}
8992 
8993 			(void) ipif_get_name(ipif,
8994 				lifr->lifr_name,
8995 				sizeof (lifr->lifr_name));
8996 			if (ipif->ipif_isv6) {
8997 				sin6 = (sin6_t *)&lifr->lifr_addr;
8998 				*sin6 = sin6_null;
8999 				sin6->sin6_family = AF_INET6;
9000 				sin6->sin6_addr =
9001 				ipif->ipif_v6lcl_addr;
9002 				lifr->lifr_addrlen =
9003 				ip_mask_to_plen_v6(
9004 				    &ipif->ipif_v6net_mask);
9005 			} else {
9006 				sin = (sin_t *)&lifr->lifr_addr;
9007 				*sin = sin_null;
9008 				sin->sin_family = AF_INET;
9009 				sin->sin_addr.s_addr =
9010 				    ipif->ipif_lcl_addr;
9011 				lifr->lifr_addrlen =
9012 				    ip_mask_to_plen(
9013 				    ipif->ipif_net_mask);
9014 			}
9015 			lifr++;
9016 		}
9017 	}
9018 lif_copydone:
9019 	rw_exit(&ill_g_lock);
9020 
9021 	mp1->b_wptr = (uchar_t *)lifr;
9022 	if (STRUCT_BUF(lifc) != NULL) {
9023 		STRUCT_FSET(lifc, lifc_len,
9024 			(int)((uchar_t *)lifr - mp1->b_rptr));
9025 	}
9026 	return (0);
9027 }
9028 
9029 /* ARGSUSED */
9030 int
9031 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
9032     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9033 {
9034 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9035 	ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9036 	return (0);
9037 }
9038 
9039 static void
9040 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9041 {
9042 	ip6_asp_t *table;
9043 	size_t table_size;
9044 	mblk_t *data_mp;
9045 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9046 
9047 	/* These two ioctls are I_STR only */
9048 	if (iocp->ioc_count == TRANSPARENT) {
9049 		miocnak(q, mp, 0, EINVAL);
9050 		return;
9051 	}
9052 
9053 	data_mp = mp->b_cont;
9054 	if (data_mp == NULL) {
9055 		/* The user passed us a NULL argument */
9056 		table = NULL;
9057 		table_size = iocp->ioc_count;
9058 	} else {
9059 		/*
9060 		 * The user provided a table.  The stream head
9061 		 * may have copied in the user data in chunks,
9062 		 * so make sure everything is pulled up
9063 		 * properly.
9064 		 */
9065 		if (MBLKL(data_mp) < iocp->ioc_count) {
9066 			mblk_t *new_data_mp;
9067 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9068 			    NULL) {
9069 				miocnak(q, mp, 0, ENOMEM);
9070 				return;
9071 			}
9072 			freemsg(data_mp);
9073 			data_mp = new_data_mp;
9074 			mp->b_cont = data_mp;
9075 		}
9076 		table = (ip6_asp_t *)data_mp->b_rptr;
9077 		table_size = iocp->ioc_count;
9078 	}
9079 
9080 	switch (iocp->ioc_cmd) {
9081 	case SIOCGIP6ADDRPOLICY:
9082 		iocp->ioc_rval = ip6_asp_get(table, table_size);
9083 		if (iocp->ioc_rval == -1)
9084 			iocp->ioc_error = EINVAL;
9085 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9086 		else if (table != NULL &&
9087 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9088 			ip6_asp_t *src = table;
9089 			ip6_asp32_t *dst = (void *)table;
9090 			int count = table_size / sizeof (ip6_asp_t);
9091 			int i;
9092 
9093 			/*
9094 			 * We need to do an in-place shrink of the array
9095 			 * to match the alignment attributes of the
9096 			 * 32-bit ABI looking at it.
9097 			 */
9098 			/* LINTED: logical expression always true: op "||" */
9099 			ASSERT(sizeof (*src) > sizeof (*dst));
9100 			for (i = 1; i < count; i++)
9101 				bcopy(src + i, dst + i, sizeof (*dst));
9102 		}
9103 #endif
9104 		break;
9105 
9106 	case SIOCSIP6ADDRPOLICY:
9107 		ASSERT(mp->b_prev == NULL);
9108 		mp->b_prev = (void *)q;
9109 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9110 		/*
9111 		 * We pass in the datamodel here so that the ip6_asp_replace()
9112 		 * routine can handle converting from 32-bit to native formats
9113 		 * where necessary.
9114 		 *
9115 		 * A better way to handle this might be to convert the inbound
9116 		 * data structure here, and hang it off a new 'mp'; thus the
9117 		 * ip6_asp_replace() logic would always be dealing with native
9118 		 * format data structures..
9119 		 *
9120 		 * (An even simpler way to handle these ioctls is to just
9121 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9122 		 * and just recompile everything that depends on it.)
9123 		 */
9124 #endif
9125 		ip6_asp_replace(mp, table, table_size, B_FALSE,
9126 		    iocp->ioc_flag & IOC_MODELS);
9127 		return;
9128 	}
9129 
9130 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9131 	qreply(q, mp);
9132 }
9133 
9134 static void
9135 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9136 {
9137 	mblk_t 		*data_mp;
9138 	struct dstinforeq	*dir;
9139 	uint8_t		*end, *cur;
9140 	in6_addr_t	*daddr, *saddr;
9141 	ipaddr_t	v4daddr;
9142 	ire_t		*ire;
9143 	char		*slabel, *dlabel;
9144 	boolean_t	isipv4;
9145 	int		match_ire;
9146 	ill_t		*dst_ill;
9147 	ipif_t		*src_ipif, *ire_ipif;
9148 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9149 	zoneid_t	zoneid;
9150 
9151 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9152 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9153 
9154 	/*
9155 	 * This ioctl is I_STR only, and must have a
9156 	 * data mblk following the M_IOCTL mblk.
9157 	 */
9158 	data_mp = mp->b_cont;
9159 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9160 		miocnak(q, mp, 0, EINVAL);
9161 		return;
9162 	}
9163 
9164 	if (MBLKL(data_mp) < iocp->ioc_count) {
9165 		mblk_t *new_data_mp;
9166 
9167 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9168 			miocnak(q, mp, 0, ENOMEM);
9169 			return;
9170 		}
9171 		freemsg(data_mp);
9172 		data_mp = new_data_mp;
9173 		mp->b_cont = data_mp;
9174 	}
9175 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9176 
9177 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9178 	    end - cur >= sizeof (struct dstinforeq);
9179 	    cur += sizeof (struct dstinforeq)) {
9180 		dir = (struct dstinforeq *)cur;
9181 		daddr = &dir->dir_daddr;
9182 		saddr = &dir->dir_saddr;
9183 
9184 		/*
9185 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9186 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9187 		 * and ipif_select_source[_v6]() do not.
9188 		 */
9189 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9190 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence);
9191 
9192 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9193 		if (isipv4) {
9194 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9195 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9196 			    0, NULL, NULL, zoneid, 0, NULL, match_ire);
9197 		} else {
9198 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9199 			    0, NULL, NULL, zoneid, 0, NULL, match_ire);
9200 		}
9201 		if (ire == NULL) {
9202 			dir->dir_dreachable = 0;
9203 
9204 			/* move on to next dst addr */
9205 			continue;
9206 		}
9207 		dir->dir_dreachable = 1;
9208 
9209 		ire_ipif = ire->ire_ipif;
9210 		if (ire_ipif == NULL)
9211 			goto next_dst;
9212 
9213 		/*
9214 		 * We expect to get back an interface ire or a
9215 		 * gateway ire cache entry.  For both types, the
9216 		 * output interface is ire_ipif->ipif_ill.
9217 		 */
9218 		dst_ill = ire_ipif->ipif_ill;
9219 		dir->dir_dmactype = dst_ill->ill_mactype;
9220 
9221 		if (isipv4) {
9222 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9223 		} else {
9224 			src_ipif = ipif_select_source_v6(dst_ill,
9225 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9226 			    zoneid);
9227 		}
9228 		if (src_ipif == NULL)
9229 			goto next_dst;
9230 
9231 		*saddr = src_ipif->ipif_v6lcl_addr;
9232 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9233 		slabel = ip6_asp_lookup(saddr, NULL);
9234 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9235 		dir->dir_sdeprecated =
9236 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9237 		ipif_refrele(src_ipif);
9238 next_dst:
9239 		ire_refrele(ire);
9240 	}
9241 	miocack(q, mp, iocp->ioc_count, 0);
9242 }
9243 
9244 
9245 /*
9246  * Check if this is an address assigned to this machine.
9247  * Skips interfaces that are down by using ire checks.
9248  * Translates mapped addresses to v4 addresses and then
9249  * treats them as such, returning true if the v4 address
9250  * associated with this mapped address is configured.
9251  * Note: Applications will have to be careful what they do
9252  * with the response; use of mapped addresses limits
9253  * what can be done with the socket, especially with
9254  * respect to socket options and ioctls - neither IPv4
9255  * options nor IPv6 sticky options/ancillary data options
9256  * may be used.
9257  */
9258 /* ARGSUSED */
9259 int
9260 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9261     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9262 {
9263 	struct sioc_addrreq *sia;
9264 	sin_t *sin;
9265 	ire_t *ire;
9266 	mblk_t *mp1;
9267 	zoneid_t zoneid;
9268 
9269 	ip1dbg(("ip_sioctl_tmyaddr"));
9270 
9271 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9272 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9273 
9274 	/* Existence verified in ip_wput_nondata */
9275 	mp1 = mp->b_cont->b_cont;
9276 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9277 	sin = (sin_t *)&sia->sa_addr;
9278 	switch (sin->sin_family) {
9279 	case AF_INET6: {
9280 		sin6_t *sin6 = (sin6_t *)sin;
9281 
9282 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9283 			ipaddr_t v4_addr;
9284 
9285 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9286 			    v4_addr);
9287 			ire = ire_ctable_lookup(v4_addr, 0,
9288 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9289 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
9290 		} else {
9291 			in6_addr_t v6addr;
9292 
9293 			v6addr = sin6->sin6_addr;
9294 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9295 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9296 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
9297 		}
9298 		break;
9299 	}
9300 	case AF_INET: {
9301 		ipaddr_t v4addr;
9302 
9303 		v4addr = sin->sin_addr.s_addr;
9304 		ire = ire_ctable_lookup(v4addr, 0,
9305 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9306 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
9307 		break;
9308 	}
9309 	default:
9310 		return (EAFNOSUPPORT);
9311 	}
9312 	if (ire != NULL) {
9313 		sia->sa_res = 1;
9314 		ire_refrele(ire);
9315 	} else {
9316 		sia->sa_res = 0;
9317 	}
9318 	return (0);
9319 }
9320 
9321 /*
9322  * Check if this is an address assigned on-link i.e. neighbor,
9323  * and makes sure it's reachable from the current zone.
9324  * Returns true for my addresses as well.
9325  * Translates mapped addresses to v4 addresses and then
9326  * treats them as such, returning true if the v4 address
9327  * associated with this mapped address is configured.
9328  * Note: Applications will have to be careful what they do
9329  * with the response; use of mapped addresses limits
9330  * what can be done with the socket, especially with
9331  * respect to socket options and ioctls - neither IPv4
9332  * options nor IPv6 sticky options/ancillary data options
9333  * may be used.
9334  */
9335 /* ARGSUSED */
9336 int
9337 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9338     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9339 {
9340 	struct sioc_addrreq *sia;
9341 	sin_t *sin;
9342 	mblk_t	*mp1;
9343 	ire_t *ire = NULL;
9344 	zoneid_t zoneid;
9345 
9346 	ip1dbg(("ip_sioctl_tonlink"));
9347 
9348 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9349 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9350 
9351 	/* Existence verified in ip_wput_nondata */
9352 	mp1 = mp->b_cont->b_cont;
9353 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9354 	sin = (sin_t *)&sia->sa_addr;
9355 
9356 	/*
9357 	 * Match addresses with a zero gateway field to avoid
9358 	 * routes going through a router.
9359 	 * Exclude broadcast and multicast addresses.
9360 	 */
9361 	switch (sin->sin_family) {
9362 	case AF_INET6: {
9363 		sin6_t *sin6 = (sin6_t *)sin;
9364 
9365 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9366 			ipaddr_t v4_addr;
9367 
9368 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9369 			    v4_addr);
9370 			if (!CLASSD(v4_addr)) {
9371 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9372 				    NULL, NULL, zoneid, NULL,
9373 				    MATCH_IRE_GW);
9374 			}
9375 		} else {
9376 			in6_addr_t v6addr;
9377 			in6_addr_t v6gw;
9378 
9379 			v6addr = sin6->sin6_addr;
9380 			v6gw = ipv6_all_zeros;
9381 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9382 				ire = ire_route_lookup_v6(&v6addr, 0,
9383 				    &v6gw, 0, NULL, NULL, zoneid,
9384 				    NULL, MATCH_IRE_GW);
9385 			}
9386 		}
9387 		break;
9388 	}
9389 	case AF_INET: {
9390 		ipaddr_t v4addr;
9391 
9392 		v4addr = sin->sin_addr.s_addr;
9393 		if (!CLASSD(v4addr)) {
9394 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9395 			    NULL, NULL, zoneid, NULL,
9396 			    MATCH_IRE_GW);
9397 		}
9398 		break;
9399 	}
9400 	default:
9401 		return (EAFNOSUPPORT);
9402 	}
9403 	sia->sa_res = 0;
9404 	if (ire != NULL) {
9405 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9406 		    IRE_LOCAL|IRE_LOOPBACK)) {
9407 			sia->sa_res = 1;
9408 		}
9409 		ire_refrele(ire);
9410 	}
9411 	return (0);
9412 }
9413 
9414 /*
9415  * TBD: implement when kernel maintaines a list of site prefixes.
9416  */
9417 /* ARGSUSED */
9418 int
9419 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9420     ip_ioctl_cmd_t *ipip, void *ifreq)
9421 {
9422 	return (ENXIO);
9423 }
9424 
9425 /* ARGSUSED */
9426 int
9427 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9428     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9429 {
9430 	ill_t  		*ill;
9431 	mblk_t		*mp1;
9432 	conn_t		*connp;
9433 	boolean_t	success;
9434 
9435 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9436 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9437 	/* ioctl comes down on an conn */
9438 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9439 	connp = Q_TO_CONN(q);
9440 
9441 	mp->b_datap->db_type = M_IOCTL;
9442 
9443 	/*
9444 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9445 	 * The original mp contains contaminated b_next values due to 'mi',
9446 	 * which is needed to do the mi_copy_done. Unfortunately if we
9447 	 * send down the original mblk itself and if we are popped due to an
9448 	 * an unplumb before the response comes back from tunnel,
9449 	 * the streamhead (which does a freemsg) will see this contaminated
9450 	 * message and the assertion in freemsg about non-null b_next/b_prev
9451 	 * will panic a DEBUG kernel.
9452 	 */
9453 	mp1 = copymsg(mp);
9454 	if (mp1 == NULL)
9455 		return (ENOMEM);
9456 
9457 	ill = ipif->ipif_ill;
9458 	mutex_enter(&connp->conn_lock);
9459 	mutex_enter(&ill->ill_lock);
9460 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9461 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9462 		    mp, 0);
9463 	} else {
9464 		success = ill_pending_mp_add(ill, connp, mp);
9465 	}
9466 	mutex_exit(&ill->ill_lock);
9467 	mutex_exit(&connp->conn_lock);
9468 
9469 	if (success) {
9470 		ip1dbg(("sending down tunparam request "));
9471 		putnext(ill->ill_wq, mp1);
9472 		return (EINPROGRESS);
9473 	} else {
9474 		/* The conn has started closing */
9475 		freemsg(mp1);
9476 		return (EINTR);
9477 	}
9478 }
9479 
9480 static int
9481 ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp, sin_t *sin,
9482     boolean_t x_arp_ioctl, boolean_t if_arp_ioctl)
9483 {
9484 	mblk_t *mp1;
9485 	mblk_t *mp2;
9486 	mblk_t *pending_mp;
9487 	ipaddr_t ipaddr;
9488 	area_t *area;
9489 	struct iocblk *iocp;
9490 	conn_t *connp;
9491 	struct arpreq *ar;
9492 	struct xarpreq *xar;
9493 	boolean_t success;
9494 	int flags, alength;
9495 	char *lladdr;
9496 
9497 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9498 	connp = Q_TO_CONN(q);
9499 
9500 	iocp = (struct iocblk *)mp->b_rptr;
9501 	/*
9502 	 * ill has already been set depending on whether
9503 	 * bsd style or interface style ioctl.
9504 	 */
9505 	ASSERT(ill != NULL);
9506 
9507 	/*
9508 	 * Is this one of the new SIOC*XARP ioctls?
9509 	 */
9510 	if (x_arp_ioctl) {
9511 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9512 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9513 		ar = NULL;
9514 
9515 		flags = xar->xarp_flags;
9516 		lladdr = LLADDR(&xar->xarp_ha);
9517 		/*
9518 		 * Validate against user's link layer address length
9519 		 * input and name and addr length limits.
9520 		 */
9521 		alength = ill->ill_phys_addr_length;
9522 		if (iocp->ioc_cmd == SIOCSXARP) {
9523 			if (alength != xar->xarp_ha.sdl_alen ||
9524 			    (alength + xar->xarp_ha.sdl_nlen >
9525 			    sizeof (xar->xarp_ha.sdl_data)))
9526 				return (EINVAL);
9527 		}
9528 	} else {
9529 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9530 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9531 		xar = NULL;
9532 
9533 		flags = ar->arp_flags;
9534 		lladdr = ar->arp_ha.sa_data;
9535 		/*
9536 		 * Theoretically, the sa_family could tell us what link
9537 		 * layer type this operation is trying to deal with. By
9538 		 * common usage AF_UNSPEC means ethernet. We'll assume
9539 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9540 		 * for now. Our new SIOC*XARP ioctls can be used more
9541 		 * generally.
9542 		 *
9543 		 * If the underlying media happens to have a non 6 byte
9544 		 * address, arp module will fail set/get, but the del
9545 		 * operation will succeed.
9546 		 */
9547 		alength = 6;
9548 		if ((iocp->ioc_cmd != SIOCDARP) &&
9549 		    (alength != ill->ill_phys_addr_length)) {
9550 			return (EINVAL);
9551 		}
9552 	}
9553 
9554 	/*
9555 	 * We are going to pass up to ARP a packet chain that looks
9556 	 * like:
9557 	 *
9558 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9559 	 *
9560 	 * Get a copy of the original IOCTL mblk to head the chain,
9561 	 * to be sent up (in mp1). Also get another copy to store
9562 	 * in the ill_pending_mp list, for matching the response
9563 	 * when it comes back from ARP.
9564 	 */
9565 	mp1 = copyb(mp);
9566 	pending_mp = copymsg(mp);
9567 	if (mp1 == NULL || pending_mp == NULL) {
9568 		if (mp1 != NULL)
9569 			freeb(mp1);
9570 		if (pending_mp != NULL)
9571 			inet_freemsg(pending_mp);
9572 		return (ENOMEM);
9573 	}
9574 
9575 	ipaddr = sin->sin_addr.s_addr;
9576 
9577 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9578 	    (caddr_t)&ipaddr);
9579 	if (mp2 == NULL) {
9580 		freeb(mp1);
9581 		inet_freemsg(pending_mp);
9582 		return (ENOMEM);
9583 	}
9584 	/* Put together the chain. */
9585 	mp1->b_cont = mp2;
9586 	mp1->b_datap->db_type = M_IOCTL;
9587 	mp2->b_cont = mp;
9588 	mp2->b_datap->db_type = M_DATA;
9589 
9590 	iocp = (struct iocblk *)mp1->b_rptr;
9591 
9592 	/*
9593 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9594 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9595 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9596 	 * ioc_count field; set ioc_count to be correct.
9597 	 */
9598 	iocp->ioc_count = MBLKL(mp1->b_cont);
9599 
9600 	/*
9601 	 * Set the proper command in the ARP message.
9602 	 * Convert the SIOC{G|S|D}ARP calls into our
9603 	 * AR_ENTRY_xxx calls.
9604 	 */
9605 	area = (area_t *)mp2->b_rptr;
9606 	switch (iocp->ioc_cmd) {
9607 	case SIOCDARP:
9608 	case SIOCDXARP:
9609 		/*
9610 		 * We defer deleting the corresponding IRE until
9611 		 * we return from arp.
9612 		 */
9613 		area->area_cmd = AR_ENTRY_DELETE;
9614 		area->area_proto_mask_offset = 0;
9615 		break;
9616 	case SIOCGARP:
9617 	case SIOCGXARP:
9618 		area->area_cmd = AR_ENTRY_SQUERY;
9619 		area->area_proto_mask_offset = 0;
9620 		break;
9621 	case SIOCSARP:
9622 	case SIOCSXARP: {
9623 		/*
9624 		 * Delete the corresponding ire to make sure IP will
9625 		 * pick up any change from arp.
9626 		 */
9627 		if (!if_arp_ioctl) {
9628 			(void) ip_ire_clookup_and_delete(ipaddr, NULL);
9629 			break;
9630 		} else {
9631 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9632 			if (ipif != NULL) {
9633 				(void) ip_ire_clookup_and_delete(ipaddr, ipif);
9634 				ipif_refrele(ipif);
9635 			}
9636 			break;
9637 		}
9638 	}
9639 	}
9640 	iocp->ioc_cmd = area->area_cmd;
9641 
9642 	/*
9643 	 * Before sending 'mp' to ARP, we have to clear the b_next
9644 	 * and b_prev. Otherwise if STREAMS encounters such a message
9645 	 * in freemsg(), (because ARP can close any time) it can cause
9646 	 * a panic. But mi code needs the b_next and b_prev values of
9647 	 * mp->b_cont, to complete the ioctl. So we store it here
9648 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9649 	 * when the response comes down from ARP.
9650 	 */
9651 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9652 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9653 	mp->b_cont->b_next = NULL;
9654 	mp->b_cont->b_prev = NULL;
9655 
9656 	mutex_enter(&connp->conn_lock);
9657 	mutex_enter(&ill->ill_lock);
9658 	/* conn has not yet started closing, hence this can't fail */
9659 	success = ill_pending_mp_add(ill, connp, pending_mp);
9660 	ASSERT(success);
9661 	mutex_exit(&ill->ill_lock);
9662 	mutex_exit(&connp->conn_lock);
9663 
9664 	/*
9665 	 * Fill in the rest of the ARP operation fields.
9666 	 */
9667 	area->area_hw_addr_length = alength;
9668 	bcopy(lladdr,
9669 	    (char *)area + area->area_hw_addr_offset,
9670 	    area->area_hw_addr_length);
9671 	/* Translate the flags. */
9672 	if (flags & ATF_PERM)
9673 		area->area_flags |= ACE_F_PERMANENT;
9674 	if (flags & ATF_PUBL)
9675 		area->area_flags |= ACE_F_PUBLISH;
9676 	if (flags & ATF_AUTHORITY)
9677 		area->area_flags |= ACE_F_AUTHORITY;
9678 
9679 	/*
9680 	 * Up to ARP it goes.  The response will come
9681 	 * back in ip_wput as an M_IOCACK message, and
9682 	 * will be handed to ip_sioctl_iocack for
9683 	 * completion.
9684 	 */
9685 	putnext(ill->ill_rq, mp1);
9686 	return (EINPROGRESS);
9687 }
9688 
9689 /* ARGSUSED */
9690 int
9691 ip_sioctl_xarp(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9692     ip_ioctl_cmd_t *ipip, void *ifreq)
9693 {
9694 	struct xarpreq *xar;
9695 	boolean_t isv6;
9696 	mblk_t	*mp1;
9697 	int	err;
9698 	conn_t	*connp;
9699 	int ifnamelen;
9700 	ire_t	*ire = NULL;
9701 	ill_t	*ill = NULL;
9702 	struct sockaddr_in *sin;
9703 	boolean_t if_arp_ioctl = B_FALSE;
9704 
9705 	/* ioctl comes down on an conn */
9706 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9707 	connp = Q_TO_CONN(q);
9708 	isv6 = connp->conn_af_isv6;
9709 
9710 	/* Existance verified in ip_wput_nondata */
9711 	mp1 = mp->b_cont->b_cont;
9712 
9713 	ASSERT(MBLKL(mp1) >= sizeof (*xar));
9714 	xar = (struct xarpreq *)mp1->b_rptr;
9715 	sin = (sin_t *)&xar->xarp_pa;
9716 
9717 	if (isv6 || (xar->xarp_ha.sdl_family != AF_LINK) ||
9718 	    (xar->xarp_pa.ss_family != AF_INET))
9719 		return (ENXIO);
9720 
9721 	ifnamelen = xar->xarp_ha.sdl_nlen;
9722 	if (ifnamelen != 0) {
9723 		char	*cptr, cval;
9724 
9725 		if (ifnamelen >= LIFNAMSIZ)
9726 			return (EINVAL);
9727 
9728 		/*
9729 		 * Instead of bcopying a bunch of bytes,
9730 		 * null-terminate the string in-situ.
9731 		 */
9732 		cptr = xar->xarp_ha.sdl_data + ifnamelen;
9733 		cval = *cptr;
9734 		*cptr = '\0';
9735 		ill = ill_lookup_on_name(xar->xarp_ha.sdl_data,
9736 		    B_FALSE, isv6, CONNP_TO_WQ(connp), mp, ip_process_ioctl,
9737 		    &err, NULL);
9738 		*cptr = cval;
9739 		if (ill == NULL)
9740 			return (err);
9741 		if (ill->ill_net_type != IRE_IF_RESOLVER) {
9742 			ill_refrele(ill);
9743 			return (ENXIO);
9744 		}
9745 
9746 		if_arp_ioctl = B_TRUE;
9747 	} else {
9748 		/*
9749 		 * PSARC 2003/088 states that if sdl_nlen == 0, it behaves
9750 		 * as an extended BSD ioctl. The kernel uses the IP address
9751 		 * to figure out the network interface.
9752 		 */
9753 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL);
9754 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9755 		    ((ill = ire_to_ill(ire)) == NULL) ||
9756 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9757 			if (ire != NULL)
9758 				ire_refrele(ire);
9759 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9760 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9761 			    NULL, MATCH_IRE_TYPE);
9762 			if ((ire == NULL) ||
9763 			    ((ill = ire_to_ill(ire)) == NULL)) {
9764 				if (ire != NULL)
9765 					ire_refrele(ire);
9766 				return (ENXIO);
9767 			}
9768 		}
9769 		ASSERT(ire != NULL && ill != NULL);
9770 	}
9771 
9772 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_TRUE, if_arp_ioctl);
9773 	if (if_arp_ioctl)
9774 		ill_refrele(ill);
9775 	if (ire != NULL)
9776 		ire_refrele(ire);
9777 
9778 	return (err);
9779 }
9780 
9781 /*
9782  * ARP IOCTLs.
9783  * How does IP get in the business of fronting ARP configuration/queries?
9784  * Well its like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9785  * are by tradition passed in through a datagram socket.  That lands in IP.
9786  * As it happens, this is just as well since the interface is quite crude in
9787  * that it passes in no information about protocol or hardware types, or
9788  * interface association.  After making the protocol assumption, IP is in
9789  * the position to look up the name of the ILL, which ARP will need, and
9790  * format a request that can be handled by ARP.	 The request is passed up
9791  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9792  * back a response.  ARP supports its own set of more general IOCTLs, in
9793  * case anyone is interested.
9794  */
9795 /* ARGSUSED */
9796 int
9797 ip_sioctl_arp(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9798     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9799 {
9800 	struct arpreq *ar;
9801 	struct sockaddr_in *sin;
9802 	ire_t	*ire;
9803 	boolean_t isv6;
9804 	mblk_t	*mp1;
9805 	int	err;
9806 	conn_t	*connp;
9807 	ill_t	*ill;
9808 
9809 	/* ioctl comes down on an conn */
9810 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9811 	connp = Q_TO_CONN(q);
9812 	isv6 = connp->conn_af_isv6;
9813 	if (isv6)
9814 		return (ENXIO);
9815 
9816 	/* Existance verified in ip_wput_nondata */
9817 	mp1 = mp->b_cont->b_cont;
9818 
9819 	ar = (struct arpreq *)mp1->b_rptr;
9820 	sin = (sin_t *)&ar->arp_pa;
9821 
9822 	/*
9823 	 * We need to let ARP know on which interface the IP
9824 	 * address has an ARP mapping. In the IPMP case, a
9825 	 * simple forwarding table lookup will return the
9826 	 * IRE_IF_RESOLVER for the first interface in the group,
9827 	 * which might not be the interface on which the
9828 	 * requested IP address was resolved due to the ill
9829 	 * selection algorithm (see ip_newroute_get_dst_ill()).
9830 	 * So we do a cache table lookup first: if the IRE cache
9831 	 * entry for the IP address is still there, it will
9832 	 * contain the ill pointer for the right interface, so
9833 	 * we use that. If the cache entry has been flushed, we
9834 	 * fall back to the forwarding table lookup. This should
9835 	 * be rare enough since IRE cache entries have a longer
9836 	 * life expectancy than ARP cache entries.
9837 	 */
9838 	ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL);
9839 	if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9840 	    ((ill = ire_to_ill(ire)) == NULL)) {
9841 		if (ire != NULL)
9842 			ire_refrele(ire);
9843 		ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9844 		    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9845 		    NULL, MATCH_IRE_TYPE);
9846 		if ((ire == NULL) || ((ill = ire_to_ill(ire)) == NULL)) {
9847 			if (ire != NULL)
9848 				ire_refrele(ire);
9849 			return (ENXIO);
9850 		}
9851 	}
9852 	ASSERT(ire != NULL && ill != NULL);
9853 
9854 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_FALSE, B_FALSE);
9855 	ire_refrele(ire);
9856 	return (err);
9857 }
9858 
9859 /*
9860  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9861  * atomically set/clear the muxids. Also complete the ioctl by acking or
9862  * naking it.  Note that the code is structured such that the link type,
9863  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9864  * its clones use the persistent link, while pppd(1M) and perhaps many
9865  * other daemons may use non-persistent link.  When combined with some
9866  * ill_t states, linking and unlinking lower streams may be used as
9867  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9868  */
9869 /* ARGSUSED */
9870 void
9871 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9872 {
9873 	mblk_t *mp1;
9874 	mblk_t *mp2;
9875 	struct linkblk *li;
9876 	queue_t	*ipwq;
9877 	char	*name;
9878 	struct qinit *qinfo;
9879 	struct ipmx_s *ipmxp;
9880 	ill_t	*ill = NULL;
9881 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9882 	int	err = 0;
9883 	boolean_t	entered_ipsq = B_FALSE;
9884 	boolean_t islink;
9885 	queue_t *dwq = NULL;
9886 
9887 	ASSERT(iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_PUNLINK ||
9888 	    iocp->ioc_cmd == I_LINK || iocp->ioc_cmd == I_UNLINK);
9889 
9890 	islink = (iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_LINK) ?
9891 	    B_TRUE : B_FALSE;
9892 
9893 	mp1 = mp->b_cont;	/* This is the linkblk info */
9894 	li = (struct linkblk *)mp1->b_rptr;
9895 
9896 	/*
9897 	 * ARP has added this special mblk, and the utility is asking us
9898 	 * to perform consistency checks, and also atomically set the
9899 	 * muxid. Ifconfig is an example.  It achieves this by using
9900 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9901 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9902 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9903 	 * and other comments in this routine for more details.
9904 	 */
9905 	mp2 = mp1->b_cont;	/* This is added by ARP */
9906 
9907 	/*
9908 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9909 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9910 	 * get the special mblk above.  For backward compatibility, we just
9911 	 * return success.  The utility will use SIOCSLIFMUXID to store
9912 	 * the muxids.  This is not atomic, and can leave the streams
9913 	 * unplumbable if the utility is interrrupted, before it does the
9914 	 * SIOCSLIFMUXID.
9915 	 */
9916 	if (mp2 == NULL) {
9917 		/*
9918 		 * At this point we don't know whether or not this is the
9919 		 * IP module stream or the ARP device stream.  We need to
9920 		 * walk the lower stream in order to find this out, since
9921 		 * the capability negotiation is done only on the IP module
9922 		 * stream.  IP module instance is identified by the module
9923 		 * name IP, non-null q_next, and it's wput not being ip_lwput.
9924 		 * STREAMS ensures that the lower stream (l_qbot) will not
9925 		 * vanish until this ioctl completes. So we can safely walk
9926 		 * the stream or refer to the q_ptr.
9927 		 */
9928 		ipwq = li->l_qbot;
9929 		while (ipwq != NULL) {
9930 			qinfo = ipwq->q_qinfo;
9931 			name = qinfo->qi_minfo->mi_idname;
9932 			if (name != NULL && name[0] != NULL &&
9933 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
9934 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
9935 			    (ipwq->q_next != NULL)) {
9936 				break;
9937 			}
9938 			ipwq = ipwq->q_next;
9939 		}
9940 		/*
9941 		 * This looks like an IP module stream, so trigger
9942 		 * the capability reset or re-negotiation if necessary.
9943 		 */
9944 		if (ipwq != NULL) {
9945 			ill = ipwq->q_ptr;
9946 			ASSERT(ill != NULL);
9947 
9948 			if (ipsq == NULL) {
9949 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
9950 				    ip_sioctl_plink, NEW_OP, B_TRUE);
9951 				if (ipsq == NULL)
9952 					return;
9953 				entered_ipsq = B_TRUE;
9954 			}
9955 			ASSERT(IAM_WRITER_ILL(ill));
9956 			/*
9957 			 * Store the upper read queue of the module
9958 			 * immediately below IP, and count the total
9959 			 * number of lower modules.  Do this only
9960 			 * for I_PLINK or I_LINK event.
9961 			 */
9962 			ill->ill_lmod_rq = NULL;
9963 			ill->ill_lmod_cnt = 0;
9964 			if (islink && (dwq = ipwq->q_next) != NULL) {
9965 				ill->ill_lmod_rq = RD(dwq);
9966 
9967 				while (dwq != NULL) {
9968 					ill->ill_lmod_cnt++;
9969 					dwq = dwq->q_next;
9970 				}
9971 			}
9972 			/*
9973 			 * There's no point in resetting or re-negotiating if
9974 			 * we are not bound to the driver, so only do this if
9975 			 * the DLPI state is idle (up); we assume such state
9976 			 * since ill_ipif_up_count gets incremented in
9977 			 * ipif_up_done(), which is after we are bound to the
9978 			 * driver.  Note that in the case of logical
9979 			 * interfaces, IP won't rebind to the driver unless
9980 			 * the ill_ipif_up_count is 0, meaning that all other
9981 			 * IP interfaces (including the main ipif) are in the
9982 			 * down state.  Because of this, we use such counter
9983 			 * as an indicator, instead of relying on the IPIF_UP
9984 			 * flag, which is per ipif instance.
9985 			 */
9986 			if (ill->ill_ipif_up_count > 0) {
9987 				if (islink)
9988 					ill_capability_probe(ill);
9989 				else
9990 					ill_capability_reset(ill);
9991 			}
9992 		}
9993 		goto done;
9994 	}
9995 
9996 	/*
9997 	 * This is an I_{P}LINK sent down by ifconfig on
9998 	 * /dev/arp. ARP has appended this last (3rd) mblk,
9999 	 * giving more info. STREAMS ensures that the lower
10000 	 * stream (l_qbot) will not vanish until this ioctl
10001 	 * completes. So we can safely walk the stream or refer
10002 	 * to the q_ptr.
10003 	 */
10004 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
10005 	if (ipmxp->ipmx_arpdev_stream) {
10006 		/*
10007 		 * The operation is occuring on the arp-device
10008 		 * stream.
10009 		 */
10010 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
10011 		    q, mp, ip_sioctl_plink, &err, NULL);
10012 		if (ill == NULL) {
10013 			if (err == EINPROGRESS) {
10014 				return;
10015 			} else {
10016 				err = EINVAL;
10017 				goto done;
10018 			}
10019 		}
10020 
10021 		if (ipsq == NULL) {
10022 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
10023 			    NEW_OP, B_TRUE);
10024 			if (ipsq == NULL) {
10025 				ill_refrele(ill);
10026 				return;
10027 			}
10028 			entered_ipsq = B_TRUE;
10029 		}
10030 		ASSERT(IAM_WRITER_ILL(ill));
10031 		ill_refrele(ill);
10032 		/*
10033 		 * To ensure consistency between IP and ARP,
10034 		 * the following LIFO scheme is used in
10035 		 * plink/punlink. (IP first, ARP last).
10036 		 * This is because the muxid's are stored
10037 		 * in the IP stream on the ill.
10038 		 *
10039 		 * I_{P}LINK: ifconfig plinks the IP stream before
10040 		 * plinking the ARP stream. On an arp-dev
10041 		 * stream, IP checks that it is not yet
10042 		 * plinked, and it also checks that the
10043 		 * corresponding IP stream is already plinked.
10044 		 *
10045 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream
10046 		 * before punlinking the IP stream. IP does
10047 		 * not allow punlink of the IP stream unless
10048 		 * the arp stream has been punlinked.
10049 		 *
10050 		 */
10051 		if ((islink &&
10052 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
10053 		    (!islink &&
10054 		    ill->ill_arp_muxid != li->l_index)) {
10055 			err = EINVAL;
10056 			goto done;
10057 		}
10058 		if (islink) {
10059 			ill->ill_arp_muxid = li->l_index;
10060 		} else {
10061 			ill->ill_arp_muxid = 0;
10062 		}
10063 	} else {
10064 		/*
10065 		 * This must be the IP module stream with or
10066 		 * without arp. Walk the stream and locate the
10067 		 * IP module. An IP module instance is
10068 		 * identified by the module name IP, non-null
10069 		 * q_next, and it's wput not being ip_lwput.
10070 		 */
10071 		ipwq = li->l_qbot;
10072 		while (ipwq != NULL) {
10073 			qinfo = ipwq->q_qinfo;
10074 			name = qinfo->qi_minfo->mi_idname;
10075 			if (name != NULL && name[0] != NULL &&
10076 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
10077 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
10078 			    (ipwq->q_next != NULL)) {
10079 				break;
10080 			}
10081 			ipwq = ipwq->q_next;
10082 		}
10083 		if (ipwq != NULL) {
10084 			ill = ipwq->q_ptr;
10085 			ASSERT(ill != NULL);
10086 
10087 			if (ipsq == NULL) {
10088 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
10089 				    ip_sioctl_plink, NEW_OP, B_TRUE);
10090 				if (ipsq == NULL)
10091 					return;
10092 				entered_ipsq = B_TRUE;
10093 			}
10094 			ASSERT(IAM_WRITER_ILL(ill));
10095 			/*
10096 			 * Return error if the ip_mux_id is
10097 			 * non-zero and command is I_{P}LINK.
10098 			 * If command is I_{P}UNLINK, return
10099 			 * error if the arp-devstr is not
10100 			 * yet punlinked.
10101 			 */
10102 			if ((islink && ill->ill_ip_muxid != 0) ||
10103 			    (!islink && ill->ill_arp_muxid != 0)) {
10104 				err = EINVAL;
10105 				goto done;
10106 			}
10107 			ill->ill_lmod_rq = NULL;
10108 			ill->ill_lmod_cnt = 0;
10109 			if (islink) {
10110 				/*
10111 				 * Store the upper read queue of the module
10112 				 * immediately below IP, and count the total
10113 				 * number of lower modules.
10114 				 */
10115 				if ((dwq = ipwq->q_next) != NULL) {
10116 					ill->ill_lmod_rq = RD(dwq);
10117 
10118 					while (dwq != NULL) {
10119 						ill->ill_lmod_cnt++;
10120 						dwq = dwq->q_next;
10121 					}
10122 				}
10123 				ill->ill_ip_muxid = li->l_index;
10124 			} else {
10125 				ill->ill_ip_muxid = 0;
10126 			}
10127 
10128 			/*
10129 			 * See comments above about resetting/re-
10130 			 * negotiating driver sub-capabilities.
10131 			 */
10132 			if (ill->ill_ipif_up_count > 0) {
10133 				if (islink)
10134 					ill_capability_probe(ill);
10135 				else
10136 					ill_capability_reset(ill);
10137 			}
10138 		}
10139 	}
10140 done:
10141 	iocp->ioc_count = 0;
10142 	iocp->ioc_error = err;
10143 	if (err == 0)
10144 		mp->b_datap->db_type = M_IOCACK;
10145 	else
10146 		mp->b_datap->db_type = M_IOCNAK;
10147 	qreply(q, mp);
10148 
10149 	/* Conn was refheld in ip_sioctl_copyin_setup */
10150 	if (CONN_Q(q))
10151 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
10152 	if (entered_ipsq)
10153 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
10154 }
10155 
10156 /*
10157  * Search the ioctl command in the ioctl tables and return a pointer
10158  * to the ioctl command information. The ioctl command tables are
10159  * static and fully populated at compile time.
10160  */
10161 ip_ioctl_cmd_t *
10162 ip_sioctl_lookup(int ioc_cmd)
10163 {
10164 	int index;
10165 	ip_ioctl_cmd_t *ipip;
10166 	ip_ioctl_cmd_t *ipip_end;
10167 
10168 	if (ioc_cmd == IPI_DONTCARE)
10169 		return (NULL);
10170 
10171 	/*
10172 	 * Do a 2 step search. First search the indexed table
10173 	 * based on the least significant byte of the ioctl cmd.
10174 	 * If we don't find a match, then search the misc table
10175 	 * serially.
10176 	 */
10177 	index = ioc_cmd & 0xFF;
10178 	if (index < ip_ndx_ioctl_count) {
10179 		ipip = &ip_ndx_ioctl_table[index];
10180 		if (ipip->ipi_cmd == ioc_cmd) {
10181 			/* Found a match in the ndx table */
10182 			return (ipip);
10183 		}
10184 	}
10185 
10186 	/* Search the misc table */
10187 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10188 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10189 		if (ipip->ipi_cmd == ioc_cmd)
10190 			/* Found a match in the misc table */
10191 			return (ipip);
10192 	}
10193 
10194 	return (NULL);
10195 }
10196 
10197 /*
10198  * Wrapper function for resuming deferred ioctl processing
10199  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10200  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10201  */
10202 /* ARGSUSED */
10203 void
10204 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10205     void *dummy_arg)
10206 {
10207 	ip_sioctl_copyin_setup(q, mp);
10208 }
10209 
10210 /*
10211  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10212  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10213  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10214  * We establish here the size of the block to be copied in.  mi_copyin
10215  * arranges for this to happen, an processing continues in ip_wput with
10216  * an M_IOCDATA message.
10217  */
10218 void
10219 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10220 {
10221 	int	copyin_size;
10222 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10223 	ip_ioctl_cmd_t *ipip;
10224 	cred_t *cr;
10225 
10226 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10227 	if (ipip == NULL) {
10228 		/*
10229 		 * The ioctl is not one we understand or own.
10230 		 * Pass it along to be processed down stream,
10231 		 * if this is a module instance of IP, else nak
10232 		 * the ioctl.
10233 		 */
10234 		if (q->q_next == NULL) {
10235 			goto nak;
10236 		} else {
10237 			putnext(q, mp);
10238 			return;
10239 		}
10240 	}
10241 
10242 	/*
10243 	 * If this is deferred, then we will do all the checks when we
10244 	 * come back.
10245 	 */
10246 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10247 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup()) {
10248 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10249 		return;
10250 	}
10251 
10252 	/*
10253 	 * Only allow a very small subset of IP ioctls on this stream if
10254 	 * IP is a module and not a driver. Allowing ioctls to be processed
10255 	 * in this case may cause assert failures or data corruption.
10256 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10257 	 * ioctls allowed on an IP module stream, after which this stream
10258 	 * normally becomes a multiplexor (at which time the stream head
10259 	 * will fail all ioctls).
10260 	 */
10261 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10262 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10263 			/*
10264 			 * Pass common Streams ioctls which the IP
10265 			 * module does not own or consume along to
10266 			 * be processed down stream.
10267 			 */
10268 			putnext(q, mp);
10269 			return;
10270 		} else {
10271 			goto nak;
10272 		}
10273 	}
10274 
10275 	/* Make sure we have ioctl data to process. */
10276 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10277 		goto nak;
10278 
10279 	/*
10280 	 * Prefer dblk credential over ioctl credential; some synthesized
10281 	 * ioctls have kcred set because there's no way to crhold()
10282 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10283 	 * the framework; the caller of ioctl needs to hold the reference
10284 	 * for the duration of the call).
10285 	 */
10286 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10287 
10288 	/* Make sure normal users don't send down privileged ioctls */
10289 	if ((ipip->ipi_flags & IPI_PRIV) &&
10290 	    (cr != NULL) && secpolicy_net_config(cr, B_TRUE) != 0) {
10291 		/* We checked the privilege earlier but log it here */
10292 		miocnak(q, mp, 0, secpolicy_net_config(cr, B_FALSE));
10293 		return;
10294 	}
10295 
10296 	/*
10297 	 * The ioctl command tables can only encode fixed length
10298 	 * ioctl data. If the length is variable, the table will
10299 	 * encode the length as zero. Such special cases are handled
10300 	 * below in the switch.
10301 	 */
10302 	if (ipip->ipi_copyin_size != 0) {
10303 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10304 		return;
10305 	}
10306 
10307 	switch (iocp->ioc_cmd) {
10308 	case O_SIOCGIFCONF:
10309 	case SIOCGIFCONF:
10310 		/*
10311 		 * This IOCTL is hilarious.  See comments in
10312 		 * ip_sioctl_get_ifconf for the story.
10313 		 */
10314 		if (iocp->ioc_count == TRANSPARENT)
10315 			copyin_size = SIZEOF_STRUCT(ifconf,
10316 			    iocp->ioc_flag);
10317 		else
10318 			copyin_size = iocp->ioc_count;
10319 		mi_copyin(q, mp, NULL, copyin_size);
10320 		return;
10321 
10322 	case O_SIOCGLIFCONF:
10323 	case SIOCGLIFCONF:
10324 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10325 		mi_copyin(q, mp, NULL, copyin_size);
10326 		return;
10327 
10328 	case SIOCGLIFSRCOF:
10329 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10330 		mi_copyin(q, mp, NULL, copyin_size);
10331 		return;
10332 	case SIOCGIP6ADDRPOLICY:
10333 		ip_sioctl_ip6addrpolicy(q, mp);
10334 		ip6_asp_table_refrele();
10335 		return;
10336 
10337 	case SIOCSIP6ADDRPOLICY:
10338 		ip_sioctl_ip6addrpolicy(q, mp);
10339 		return;
10340 
10341 	case SIOCGDSTINFO:
10342 		ip_sioctl_dstinfo(q, mp);
10343 		ip6_asp_table_refrele();
10344 		return;
10345 
10346 	case I_PLINK:
10347 	case I_PUNLINK:
10348 	case I_LINK:
10349 	case I_UNLINK:
10350 		/*
10351 		 * We treat non-persistent link similarly as the persistent
10352 		 * link case, in terms of plumbing/unplumbing, as well as
10353 		 * dynamic re-plumbing events indicator.  See comments
10354 		 * in ip_sioctl_plink() for more.
10355 		 *
10356 		 * Request can be enqueued in the 'ipsq' while waiting
10357 		 * to become exclusive. So bump up the conn ref.
10358 		 */
10359 		if (CONN_Q(q))
10360 			CONN_INC_REF(Q_TO_CONN(q));
10361 		ip_sioctl_plink(NULL, q, mp, NULL);
10362 		return;
10363 
10364 	case ND_GET:
10365 	case ND_SET:
10366 		/*
10367 		 * Use of the nd table requires holding the reader lock.
10368 		 * Modifying the nd table thru nd_load/nd_unload requires
10369 		 * the writer lock.
10370 		 */
10371 		rw_enter(&ip_g_nd_lock, RW_READER);
10372 		if (nd_getset(q, ip_g_nd, mp)) {
10373 			rw_exit(&ip_g_nd_lock);
10374 
10375 			if (iocp->ioc_error)
10376 				iocp->ioc_count = 0;
10377 			mp->b_datap->db_type = M_IOCACK;
10378 			qreply(q, mp);
10379 			return;
10380 		}
10381 		rw_exit(&ip_g_nd_lock);
10382 		/*
10383 		 * We don't understand this subioctl of ND_GET / ND_SET.
10384 		 * Maybe intended for some driver / module below us
10385 		 */
10386 		if (q->q_next) {
10387 			putnext(q, mp);
10388 		} else {
10389 			iocp->ioc_error = ENOENT;
10390 			mp->b_datap->db_type = M_IOCNAK;
10391 			iocp->ioc_count = 0;
10392 			qreply(q, mp);
10393 		}
10394 		return;
10395 
10396 	case IP_IOCTL:
10397 		ip_wput_ioctl(q, mp);
10398 		return;
10399 	default:
10400 		cmn_err(CE_PANIC, "should not happen ");
10401 	}
10402 nak:
10403 	if (mp->b_cont != NULL) {
10404 		freemsg(mp->b_cont);
10405 		mp->b_cont = NULL;
10406 	}
10407 	iocp->ioc_error = EINVAL;
10408 	mp->b_datap->db_type = M_IOCNAK;
10409 	iocp->ioc_count = 0;
10410 	qreply(q, mp);
10411 }
10412 
10413 /* ip_wput hands off ARP IOCTL responses to us */
10414 void
10415 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10416 {
10417 	struct arpreq *ar;
10418 	struct xarpreq *xar;
10419 	area_t	*area;
10420 	mblk_t	*area_mp;
10421 	struct iocblk *iocp;
10422 	mblk_t	*orig_ioc_mp, *tmp;
10423 	struct iocblk	*orig_iocp;
10424 	ill_t *ill;
10425 	conn_t *connp = NULL;
10426 	uint_t ioc_id;
10427 	mblk_t *pending_mp;
10428 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10429 	int *flagsp;
10430 	char *storage = NULL;
10431 	sin_t *sin;
10432 	ipaddr_t addr;
10433 	int err;
10434 
10435 	ill = q->q_ptr;
10436 	ASSERT(ill != NULL);
10437 
10438 	/*
10439 	 * We should get back from ARP a packet chain that looks like:
10440 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10441 	 */
10442 	if (!(area_mp = mp->b_cont) ||
10443 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10444 	    !(orig_ioc_mp = area_mp->b_cont) ||
10445 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10446 		freemsg(mp);
10447 		return;
10448 	}
10449 
10450 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10451 
10452 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10453 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10454 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10455 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10456 		x_arp_ioctl = B_TRUE;
10457 		xar = (struct xarpreq *)tmp->b_rptr;
10458 		sin = (sin_t *)&xar->xarp_pa;
10459 		flagsp = &xar->xarp_flags;
10460 		storage = xar->xarp_ha.sdl_data;
10461 		if (xar->xarp_ha.sdl_nlen != 0)
10462 			ifx_arp_ioctl = B_TRUE;
10463 	} else {
10464 		ar = (struct arpreq *)tmp->b_rptr;
10465 		sin = (sin_t *)&ar->arp_pa;
10466 		flagsp = &ar->arp_flags;
10467 		storage = ar->arp_ha.sa_data;
10468 	}
10469 
10470 	iocp = (struct iocblk *)mp->b_rptr;
10471 
10472 	/*
10473 	 * Pick out the originating queue based on the ioc_id.
10474 	 */
10475 	ioc_id = iocp->ioc_id;
10476 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10477 	if (pending_mp == NULL) {
10478 		ASSERT(connp == NULL);
10479 		inet_freemsg(mp);
10480 		return;
10481 	}
10482 	ASSERT(connp != NULL);
10483 	q = CONNP_TO_WQ(connp);
10484 
10485 	/* Uncouple the internally generated IOCTL from the original one */
10486 	area = (area_t *)area_mp->b_rptr;
10487 	area_mp->b_cont = NULL;
10488 
10489 	/*
10490 	 * Restore the b_next and b_prev used by mi code. This is needed
10491 	 * to complete the ioctl using mi* functions. We stored them in
10492 	 * the pending mp prior to sending the request to ARP.
10493 	 */
10494 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10495 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10496 	inet_freemsg(pending_mp);
10497 
10498 	/*
10499 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10500 	 * Catch the case where there is an IRE_CACHE by no entry in the
10501 	 * arp table.
10502 	 */
10503 	addr = sin->sin_addr.s_addr;
10504 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10505 		ire_t			*ire;
10506 		dl_unitdata_req_t	*dlup;
10507 		mblk_t			*llmp;
10508 		int			addr_len;
10509 		ill_t			*ipsqill = NULL;
10510 
10511 		if (ifx_arp_ioctl) {
10512 			/*
10513 			 * There's no need to lookup the ill, since
10514 			 * we've already done that when we started
10515 			 * processing the ioctl and sent the message
10516 			 * to ARP on that ill.  So use the ill that
10517 			 * is stored in q->q_ptr.
10518 			 */
10519 			ipsqill = ill;
10520 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10521 			    ipsqill->ill_ipif, ALL_ZONES,
10522 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL);
10523 		} else {
10524 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10525 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
10526 			if (ire != NULL)
10527 				ipsqill = ire_to_ill(ire);
10528 		}
10529 
10530 		if ((x_arp_ioctl) && (ipsqill != NULL))
10531 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10532 
10533 		if (ire != NULL) {
10534 			/*
10535 			 * Since the ire obtained from cachetable is used for
10536 			 * mac addr copying below, treat an incomplete ire as if
10537 			 * as if we never found it.
10538 			 */
10539 			if (ire->ire_nce != NULL &&
10540 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10541 				ire_refrele(ire);
10542 				ire = NULL;
10543 				ipsqill = NULL;
10544 				goto errack;
10545 			}
10546 			*flagsp = ATF_INUSE;
10547 			llmp = (ire->ire_nce != NULL ?
10548 			    ire->ire_nce->nce_res_mp : NULL);
10549 			if (llmp != NULL && ipsqill != NULL) {
10550 				uchar_t *macaddr;
10551 
10552 				addr_len = ipsqill->ill_phys_addr_length;
10553 				if (x_arp_ioctl && ((addr_len +
10554 				    ipsqill->ill_name_length) >
10555 				    sizeof (xar->xarp_ha.sdl_data))) {
10556 					ire_refrele(ire);
10557 					freemsg(mp);
10558 					ip_ioctl_finish(q, orig_ioc_mp,
10559 					    EINVAL, NO_COPYOUT, NULL, NULL);
10560 					return;
10561 				}
10562 				*flagsp |= ATF_COM;
10563 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10564 				if (ipsqill->ill_sap_length < 0)
10565 					macaddr = llmp->b_rptr +
10566 					    dlup->dl_dest_addr_offset;
10567 				else
10568 					macaddr = llmp->b_rptr +
10569 					    dlup->dl_dest_addr_offset +
10570 					    ipsqill->ill_sap_length;
10571 				/*
10572 				 * For SIOCGARP, MAC address length
10573 				 * validation has already been done
10574 				 * before the ioctl was issued to ARP to
10575 				 * allow it to progress only on 6 byte
10576 				 * addressable (ethernet like) media. Thus
10577 				 * the mac address copying can not overwrite
10578 				 * the sa_data area below.
10579 				 */
10580 				bcopy(macaddr, storage, addr_len);
10581 			}
10582 			/* Ditch the internal IOCTL. */
10583 			freemsg(mp);
10584 			ire_refrele(ire);
10585 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL, NULL);
10586 			return;
10587 		}
10588 	}
10589 
10590 	/*
10591 	 * Delete the coresponding IRE_CACHE if any.
10592 	 * Reset the error if there was one (in case there was no entry
10593 	 * in arp.)
10594 	 */
10595 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10596 		ipif_t *ipintf = NULL;
10597 
10598 		if (ifx_arp_ioctl) {
10599 			/*
10600 			 * There's no need to lookup the ill, since
10601 			 * we've already done that when we started
10602 			 * processing the ioctl and sent the message
10603 			 * to ARP on that ill.  So use the ill that
10604 			 * is stored in q->q_ptr.
10605 			 */
10606 			ipintf = ill->ill_ipif;
10607 		}
10608 		if (ip_ire_clookup_and_delete(addr, ipintf)) {
10609 			/*
10610 			 * The address in "addr" may be an entry for a
10611 			 * router. If that's true, then any off-net
10612 			 * IRE_CACHE entries that go through the router
10613 			 * with address "addr" must be clobbered. Use
10614 			 * ire_walk to achieve this goal.
10615 			 */
10616 			if (ifx_arp_ioctl)
10617 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10618 				    ire_delete_cache_gw, (char *)&addr, ill);
10619 			else
10620 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10621 				    ALL_ZONES);
10622 			iocp->ioc_error = 0;
10623 		}
10624 	}
10625 errack:
10626 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10627 		err = iocp->ioc_error;
10628 		freemsg(mp);
10629 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL, NULL);
10630 		return;
10631 	}
10632 
10633 	/*
10634 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10635 	 * the area_t into the struct {x}arpreq.
10636 	 */
10637 	if (x_arp_ioctl) {
10638 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10639 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10640 		    sizeof (xar->xarp_ha.sdl_data)) {
10641 			freemsg(mp);
10642 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL,
10643 			    NO_COPYOUT, NULL, NULL);
10644 			return;
10645 		}
10646 	}
10647 	*flagsp = ATF_INUSE;
10648 	if (area->area_flags & ACE_F_PERMANENT)
10649 		*flagsp |= ATF_PERM;
10650 	if (area->area_flags & ACE_F_PUBLISH)
10651 		*flagsp |= ATF_PUBL;
10652 	if (area->area_flags & ACE_F_AUTHORITY)
10653 		*flagsp |= ATF_AUTHORITY;
10654 	if (area->area_hw_addr_length != 0) {
10655 		*flagsp |= ATF_COM;
10656 		/*
10657 		 * For SIOCGARP, MAC address length validation has
10658 		 * already been done before the ioctl was issued to ARP
10659 		 * to allow it to progress only on 6 byte addressable
10660 		 * (ethernet like) media. Thus the mac address copying
10661 		 * can not overwrite the sa_data area below.
10662 		 */
10663 		bcopy((char *)area + area->area_hw_addr_offset,
10664 		    storage, area->area_hw_addr_length);
10665 	}
10666 
10667 	/* Ditch the internal IOCTL. */
10668 	freemsg(mp);
10669 	/* Complete the original. */
10670 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL, NULL);
10671 }
10672 
10673 /*
10674  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10675  * interface) create the next available logical interface for this
10676  * physical interface.
10677  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10678  * ipif with the specified name.
10679  *
10680  * If the address family is not AF_UNSPEC then set the address as well.
10681  *
10682  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10683  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10684  *
10685  * Executed as a writer on the ill or ill group.
10686  * So no lock is needed to traverse the ipif chain, or examine the
10687  * phyint flags.
10688  */
10689 /* ARGSUSED */
10690 int
10691 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10692     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10693 {
10694 	mblk_t	*mp1;
10695 	struct lifreq *lifr;
10696 	boolean_t	isv6;
10697 	boolean_t	exists;
10698 	char 	*name;
10699 	char	*endp;
10700 	char	*cp;
10701 	int	namelen;
10702 	ipif_t	*ipif;
10703 	long	id;
10704 	ipsq_t	*ipsq;
10705 	ill_t	*ill;
10706 	sin_t	*sin;
10707 	int	err = 0;
10708 	boolean_t found_sep = B_FALSE;
10709 	conn_t	*connp;
10710 	zoneid_t zoneid;
10711 	int	orig_ifindex = 0;
10712 
10713 	ip1dbg(("ip_sioctl_addif\n"));
10714 	/* Existence of mp1 has been checked in ip_wput_nondata */
10715 	mp1 = mp->b_cont->b_cont;
10716 	/*
10717 	 * Null terminate the string to protect against buffer
10718 	 * overrun. String was generated by user code and may not
10719 	 * be trusted.
10720 	 */
10721 	lifr = (struct lifreq *)mp1->b_rptr;
10722 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10723 	name = lifr->lifr_name;
10724 	ASSERT(CONN_Q(q));
10725 	connp = Q_TO_CONN(q);
10726 	isv6 = connp->conn_af_isv6;
10727 	zoneid = connp->conn_zoneid;
10728 	namelen = mi_strlen(name);
10729 	if (namelen == 0)
10730 		return (EINVAL);
10731 
10732 	exists = B_FALSE;
10733 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10734 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10735 		/*
10736 		 * Allow creating lo0 using SIOCLIFADDIF.
10737 		 * can't be any other writer thread. So can pass null below
10738 		 * for the last 4 args to ipif_lookup_name.
10739 		 */
10740 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen,
10741 		    B_TRUE, &exists, isv6, zoneid, NULL, NULL, NULL, NULL);
10742 		/* Prevent any further action */
10743 		if (ipif == NULL) {
10744 			return (ENOBUFS);
10745 		} else if (!exists) {
10746 			/* We created the ipif now and as writer */
10747 			ipif_refrele(ipif);
10748 			return (0);
10749 		} else {
10750 			ill = ipif->ipif_ill;
10751 			ill_refhold(ill);
10752 			ipif_refrele(ipif);
10753 		}
10754 	} else {
10755 		/* Look for a colon in the name. */
10756 		endp = &name[namelen];
10757 		for (cp = endp; --cp > name; ) {
10758 			if (*cp == IPIF_SEPARATOR_CHAR) {
10759 				found_sep = B_TRUE;
10760 				/*
10761 				 * Reject any non-decimal aliases for plumbing
10762 				 * of logical interfaces. Aliases with leading
10763 				 * zeroes are also rejected as they introduce
10764 				 * ambiguity in the naming of the interfaces.
10765 				 * Comparing with "0" takes care of all such
10766 				 * cases.
10767 				 */
10768 				if ((strncmp("0", cp+1, 1)) == 0)
10769 					return (EINVAL);
10770 
10771 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10772 				    id <= 0 || *endp != '\0') {
10773 					return (EINVAL);
10774 				}
10775 				*cp = '\0';
10776 				break;
10777 			}
10778 		}
10779 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10780 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL);
10781 		if (found_sep)
10782 			*cp = IPIF_SEPARATOR_CHAR;
10783 		if (ill == NULL)
10784 			return (err);
10785 	}
10786 
10787 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10788 	    B_TRUE);
10789 
10790 	/*
10791 	 * Release the refhold due to the lookup, now that we are excl
10792 	 * or we are just returning
10793 	 */
10794 	ill_refrele(ill);
10795 
10796 	if (ipsq == NULL)
10797 		return (EINPROGRESS);
10798 
10799 	/*
10800 	 * If the interface is failed, inactive or offlined, look for a working
10801 	 * interface in the ill group and create the ipif there. If we can't
10802 	 * find a good interface, create the ipif anyway so that in.mpathd can
10803 	 * move it to the first repaired interface.
10804 	 */
10805 	if ((ill->ill_phyint->phyint_flags &
10806 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10807 	    ill->ill_phyint->phyint_groupname_len != 0) {
10808 		phyint_t *phyi;
10809 		char *groupname = ill->ill_phyint->phyint_groupname;
10810 
10811 		/*
10812 		 * We're looking for a working interface, but it doesn't matter
10813 		 * if it's up or down; so instead of following the group lists,
10814 		 * we look at each physical interface and compare the groupname.
10815 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10816 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10817 		 * Otherwise we create the ipif on the failed interface.
10818 		 */
10819 		rw_enter(&ill_g_lock, RW_READER);
10820 		phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
10821 		for (; phyi != NULL;
10822 		    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
10823 		    phyi, AVL_AFTER)) {
10824 			if (phyi->phyint_groupname_len == 0)
10825 				continue;
10826 			ASSERT(phyi->phyint_groupname != NULL);
10827 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10828 			    !(phyi->phyint_flags &
10829 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10830 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10831 			    (phyi->phyint_illv4 != NULL))) {
10832 				break;
10833 			}
10834 		}
10835 		rw_exit(&ill_g_lock);
10836 
10837 		if (phyi != NULL) {
10838 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10839 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10840 			    phyi->phyint_illv4);
10841 		}
10842 	}
10843 
10844 	/*
10845 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10846 	 * before or after us.
10847 	 */
10848 	ASSERT(IAM_WRITER_ILL(ill));
10849 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10850 
10851 	if (found_sep && orig_ifindex == 0) {
10852 		/* Now see if there is an IPIF with this unit number. */
10853 		for (ipif = ill->ill_ipif; ipif != NULL;
10854 		    ipif = ipif->ipif_next) {
10855 			if (ipif->ipif_id == id) {
10856 				err = EEXIST;
10857 				goto done;
10858 			}
10859 		}
10860 	}
10861 
10862 	/*
10863 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10864 	 * of lo0. We never come here when we plumb lo0:0. It
10865 	 * happens in ipif_lookup_on_name.
10866 	 * The specified unit number is ignored when we create the ipif on a
10867 	 * different interface. However, we save it in ipif_orig_ipifid below so
10868 	 * that the ipif fails back to the right position.
10869 	 */
10870 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10871 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10872 		err = ENOBUFS;
10873 		goto done;
10874 	}
10875 
10876 	/* Return created name with ioctl */
10877 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10878 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10879 	ip1dbg(("created %s\n", lifr->lifr_name));
10880 
10881 	/* Set address */
10882 	sin = (sin_t *)&lifr->lifr_addr;
10883 	if (sin->sin_family != AF_UNSPEC) {
10884 		err = ip_sioctl_addr(ipif, sin, q, mp,
10885 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10886 	}
10887 
10888 	/* Set ifindex and unit number for failback */
10889 	if (err == 0 && orig_ifindex != 0) {
10890 		ipif->ipif_orig_ifindex = orig_ifindex;
10891 		if (found_sep) {
10892 			ipif->ipif_orig_ipifid = id;
10893 		}
10894 	}
10895 
10896 done:
10897 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10898 	return (err);
10899 }
10900 
10901 /*
10902  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10903  * interface) delete it based on the IP address (on this physical interface).
10904  * Otherwise delete it based on the ipif_id.
10905  * Also, special handling to allow a removeif of lo0.
10906  */
10907 /* ARGSUSED */
10908 int
10909 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10910     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10911 {
10912 	conn_t		*connp;
10913 	ill_t		*ill = ipif->ipif_ill;
10914 	boolean_t	 success;
10915 
10916 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10917 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10918 	ASSERT(IAM_WRITER_IPIF(ipif));
10919 
10920 	connp = Q_TO_CONN(q);
10921 	/*
10922 	 * Special case for unplumbing lo0 (the loopback physical interface).
10923 	 * If unplumbing lo0, the incoming address structure has been
10924 	 * initialized to all zeros. When unplumbing lo0, all its logical
10925 	 * interfaces must be removed too.
10926 	 *
10927 	 * Note that this interface may be called to remove a specific
10928 	 * loopback logical interface (eg, lo0:1). But in that case
10929 	 * ipif->ipif_id != 0 so that the code path for that case is the
10930 	 * same as any other interface (meaning it skips the code directly
10931 	 * below).
10932 	 */
10933 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10934 		if (sin->sin_family == AF_UNSPEC &&
10935 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10936 			/*
10937 			 * Mark it condemned. No new ref. will be made to ill.
10938 			 */
10939 			mutex_enter(&ill->ill_lock);
10940 			ill->ill_state_flags |= ILL_CONDEMNED;
10941 			for (ipif = ill->ill_ipif; ipif != NULL;
10942 			    ipif = ipif->ipif_next) {
10943 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10944 			}
10945 			mutex_exit(&ill->ill_lock);
10946 
10947 			ipif = ill->ill_ipif;
10948 			/* unplumb the loopback interface */
10949 			ill_delete(ill);
10950 			mutex_enter(&connp->conn_lock);
10951 			mutex_enter(&ill->ill_lock);
10952 			ASSERT(ill->ill_group == NULL);
10953 
10954 			/* Are any references to this ill active */
10955 			if (ill_is_quiescent(ill)) {
10956 				mutex_exit(&ill->ill_lock);
10957 				mutex_exit(&connp->conn_lock);
10958 				ill_delete_tail(ill);
10959 				mi_free(ill);
10960 				return (0);
10961 			}
10962 			success = ipsq_pending_mp_add(connp, ipif,
10963 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10964 			mutex_exit(&connp->conn_lock);
10965 			mutex_exit(&ill->ill_lock);
10966 			if (success)
10967 				return (EINPROGRESS);
10968 			else
10969 				return (EINTR);
10970 		}
10971 	}
10972 
10973 	/*
10974 	 * We are exclusive on the ipsq, so an ill move will be serialized
10975 	 * before or after us.
10976 	 */
10977 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10978 
10979 	if (ipif->ipif_id == 0) {
10980 		/* Find based on address */
10981 		if (ipif->ipif_isv6) {
10982 			sin6_t *sin6;
10983 
10984 			if (sin->sin_family != AF_INET6)
10985 				return (EAFNOSUPPORT);
10986 
10987 			sin6 = (sin6_t *)sin;
10988 			/* We are a writer, so we should be able to lookup */
10989 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10990 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL);
10991 			if (ipif == NULL) {
10992 				/*
10993 				 * Maybe the address in on another interface in
10994 				 * the same IPMP group? We check this below.
10995 				 */
10996 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10997 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL);
10998 			}
10999 		} else {
11000 			ipaddr_t addr;
11001 
11002 			if (sin->sin_family != AF_INET)
11003 				return (EAFNOSUPPORT);
11004 
11005 			addr = sin->sin_addr.s_addr;
11006 			/* We are a writer, so we should be able to lookup */
11007 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
11008 			    NULL, NULL, NULL);
11009 			if (ipif == NULL) {
11010 				/*
11011 				 * Maybe the address in on another interface in
11012 				 * the same IPMP group? We check this below.
11013 				 */
11014 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
11015 				    NULL, NULL, NULL, NULL);
11016 			}
11017 		}
11018 		if (ipif == NULL) {
11019 			return (EADDRNOTAVAIL);
11020 		}
11021 		/*
11022 		 * When the address to be removed is hosted on a different
11023 		 * interface, we check if the interface is in the same IPMP
11024 		 * group as the specified one; if so we proceed with the
11025 		 * removal.
11026 		 * ill->ill_group is NULL when the ill is down, so we have to
11027 		 * compare the group names instead.
11028 		 */
11029 		if (ipif->ipif_ill != ill &&
11030 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
11031 		    ill->ill_phyint->phyint_groupname_len == 0 ||
11032 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
11033 		    ill->ill_phyint->phyint_groupname) != 0)) {
11034 			ipif_refrele(ipif);
11035 			return (EADDRNOTAVAIL);
11036 		}
11037 
11038 		/* This is a writer */
11039 		ipif_refrele(ipif);
11040 	}
11041 
11042 	/*
11043 	 * Can not delete instance zero since it is tied to the ill.
11044 	 */
11045 	if (ipif->ipif_id == 0)
11046 		return (EBUSY);
11047 
11048 	mutex_enter(&ill->ill_lock);
11049 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
11050 	mutex_exit(&ill->ill_lock);
11051 
11052 	ipif_free(ipif);
11053 
11054 	mutex_enter(&connp->conn_lock);
11055 	mutex_enter(&ill->ill_lock);
11056 
11057 	/* Are any references to this ipif active */
11058 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
11059 		mutex_exit(&ill->ill_lock);
11060 		mutex_exit(&connp->conn_lock);
11061 		ipif_non_duplicate(ipif);
11062 		ipif_down_tail(ipif);
11063 		ipif_free_tail(ipif);
11064 		return (0);
11065 	}
11066 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
11067 	    IPIF_FREE);
11068 	mutex_exit(&ill->ill_lock);
11069 	mutex_exit(&connp->conn_lock);
11070 	if (success)
11071 		return (EINPROGRESS);
11072 	else
11073 		return (EINTR);
11074 }
11075 
11076 /*
11077  * Restart the removeif ioctl. The refcnt has gone down to 0.
11078  * The ipif is already condemned. So can't find it thru lookups.
11079  */
11080 /* ARGSUSED */
11081 int
11082 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
11083     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11084 {
11085 	ill_t *ill;
11086 
11087 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
11088 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11089 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11090 		ill = ipif->ipif_ill;
11091 		ASSERT(IAM_WRITER_ILL(ill));
11092 		ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) &&
11093 		    (ill->ill_state_flags & IPIF_CONDEMNED));
11094 		ill_delete_tail(ill);
11095 		mi_free(ill);
11096 		return (0);
11097 	}
11098 
11099 	ill = ipif->ipif_ill;
11100 	ASSERT(IAM_WRITER_IPIF(ipif));
11101 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
11102 
11103 	ipif_non_duplicate(ipif);
11104 	ipif_down_tail(ipif);
11105 	ipif_free_tail(ipif);
11106 
11107 	ILL_UNMARK_CHANGING(ill);
11108 	return (0);
11109 }
11110 
11111 /*
11112  * Set the local interface address.
11113  * Allow an address of all zero when the interface is down.
11114  */
11115 /* ARGSUSED */
11116 int
11117 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11118     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11119 {
11120 	int err = 0;
11121 	in6_addr_t v6addr;
11122 	boolean_t need_up = B_FALSE;
11123 
11124 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11125 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11126 
11127 	ASSERT(IAM_WRITER_IPIF(ipif));
11128 
11129 	if (ipif->ipif_isv6) {
11130 		sin6_t *sin6;
11131 		ill_t *ill;
11132 		phyint_t *phyi;
11133 
11134 		if (sin->sin_family != AF_INET6)
11135 			return (EAFNOSUPPORT);
11136 
11137 		sin6 = (sin6_t *)sin;
11138 		v6addr = sin6->sin6_addr;
11139 		ill = ipif->ipif_ill;
11140 		phyi = ill->ill_phyint;
11141 
11142 		/*
11143 		 * Enforce that true multicast interfaces have a link-local
11144 		 * address for logical unit 0.
11145 		 */
11146 		if (ipif->ipif_id == 0 &&
11147 		    (ill->ill_flags & ILLF_MULTICAST) &&
11148 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11149 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11150 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11151 			return (EADDRNOTAVAIL);
11152 		}
11153 
11154 		/*
11155 		 * up interfaces shouldn't have the unspecified address
11156 		 * unless they also have the IPIF_NOLOCAL flags set and
11157 		 * have a subnet assigned.
11158 		 */
11159 		if ((ipif->ipif_flags & IPIF_UP) &&
11160 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11161 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11162 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11163 			return (EADDRNOTAVAIL);
11164 		}
11165 
11166 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11167 			return (EADDRNOTAVAIL);
11168 	} else {
11169 		ipaddr_t addr;
11170 
11171 		if (sin->sin_family != AF_INET)
11172 			return (EAFNOSUPPORT);
11173 
11174 		addr = sin->sin_addr.s_addr;
11175 
11176 		/* Allow 0 as the local address. */
11177 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11178 			return (EADDRNOTAVAIL);
11179 
11180 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11181 	}
11182 
11183 
11184 	/*
11185 	 * Even if there is no change we redo things just to rerun
11186 	 * ipif_set_default.
11187 	 */
11188 	if (ipif->ipif_flags & IPIF_UP) {
11189 		/*
11190 		 * Setting a new local address, make sure
11191 		 * we have net and subnet bcast ire's for
11192 		 * the old address if we need them.
11193 		 */
11194 		if (!ipif->ipif_isv6)
11195 			ipif_check_bcast_ires(ipif);
11196 		/*
11197 		 * If the interface is already marked up,
11198 		 * we call ipif_down which will take care
11199 		 * of ditching any IREs that have been set
11200 		 * up based on the old interface address.
11201 		 */
11202 		err = ipif_logical_down(ipif, q, mp);
11203 		if (err == EINPROGRESS)
11204 			return (err);
11205 		ipif_down_tail(ipif);
11206 		need_up = 1;
11207 	}
11208 
11209 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11210 	return (err);
11211 }
11212 
11213 int
11214 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11215     boolean_t need_up)
11216 {
11217 	in6_addr_t v6addr;
11218 	ipaddr_t addr;
11219 	sin6_t	*sin6;
11220 	int	sinlen;
11221 	int	err = 0;
11222 	ill_t	*ill = ipif->ipif_ill;
11223 	boolean_t need_dl_down;
11224 	boolean_t need_arp_down;
11225 	struct iocblk *iocp;
11226 
11227 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11228 
11229 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11230 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11231 	ASSERT(IAM_WRITER_IPIF(ipif));
11232 
11233 	/* Must cancel any pending timer before taking the ill_lock */
11234 	if (ipif->ipif_recovery_id != 0)
11235 		(void) untimeout(ipif->ipif_recovery_id);
11236 	ipif->ipif_recovery_id = 0;
11237 
11238 	if (ipif->ipif_isv6) {
11239 		sin6 = (sin6_t *)sin;
11240 		v6addr = sin6->sin6_addr;
11241 		sinlen = sizeof (struct sockaddr_in6);
11242 	} else {
11243 		addr = sin->sin_addr.s_addr;
11244 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11245 		sinlen = sizeof (struct sockaddr_in);
11246 	}
11247 	mutex_enter(&ill->ill_lock);
11248 	ipif->ipif_v6lcl_addr = v6addr;
11249 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11250 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11251 	} else {
11252 		ipif->ipif_v6src_addr = v6addr;
11253 	}
11254 	ipif->ipif_addr_ready = 0;
11255 
11256 	/*
11257 	 * If the interface was previously marked as a duplicate, then since
11258 	 * we've now got a "new" address, it should no longer be considered a
11259 	 * duplicate -- even if the "new" address is the same as the old one.
11260 	 * Note that if all ipifs are down, we may have a pending ARP down
11261 	 * event to handle.  This is because we want to recover from duplicates
11262 	 * and thus delay tearing down ARP until the duplicates have been
11263 	 * removed or disabled.
11264 	 */
11265 	need_dl_down = need_arp_down = B_FALSE;
11266 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11267 		need_arp_down = !need_up;
11268 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11269 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11270 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11271 			need_dl_down = B_TRUE;
11272 		}
11273 	}
11274 
11275 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11276 	    !ill->ill_is_6to4tun) {
11277 		queue_t *wqp = ill->ill_wq;
11278 
11279 		/*
11280 		 * The local address of this interface is a 6to4 address,
11281 		 * check if this interface is in fact a 6to4 tunnel or just
11282 		 * an interface configured with a 6to4 address.  We are only
11283 		 * interested in the former.
11284 		 */
11285 		if (wqp != NULL) {
11286 			while ((wqp->q_next != NULL) &&
11287 			    (wqp->q_next->q_qinfo != NULL) &&
11288 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11289 
11290 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11291 				    == TUN6TO4_MODID) {
11292 					/* set for use in IP */
11293 					ill->ill_is_6to4tun = 1;
11294 					break;
11295 				}
11296 				wqp = wqp->q_next;
11297 			}
11298 		}
11299 	}
11300 
11301 	ipif_set_default(ipif);
11302 
11303 	/*
11304 	 * When publishing an interface address change event, we only notify
11305 	 * the event listeners of the new address.  It is assumed that if they
11306 	 * actively care about the addresses assigned that they will have
11307 	 * already discovered the previous address assigned (if there was one.)
11308 	 *
11309 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11310 	 */
11311 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11312 		hook_nic_event_t *info;
11313 		if ((info = ipif->ipif_ill->ill_nic_event_info) != NULL) {
11314 			ip2dbg(("ip_sioctl_addr_tail: unexpected nic event %d "
11315 			    "attached for %s\n", info->hne_event,
11316 			    ill->ill_name));
11317 			if (info->hne_data != NULL)
11318 				kmem_free(info->hne_data, info->hne_datalen);
11319 			kmem_free(info, sizeof (hook_nic_event_t));
11320 		}
11321 
11322 		info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
11323 		if (info != NULL) {
11324 			info->hne_nic =
11325 			    ipif->ipif_ill->ill_phyint->phyint_ifindex;
11326 			info->hne_lif = MAP_IPIF_ID(ipif->ipif_id);
11327 			info->hne_event = NE_ADDRESS_CHANGE;
11328 			info->hne_family = ipif->ipif_isv6 ? ipv6 : ipv4;
11329 			info->hne_data = kmem_alloc(sinlen, KM_NOSLEEP);
11330 			if (info->hne_data != NULL) {
11331 				info->hne_datalen = sinlen;
11332 				bcopy(sin, info->hne_data, sinlen);
11333 			} else {
11334 				ip2dbg(("ip_sioctl_addr_tail: could not attach "
11335 				    "address information for ADDRESS_CHANGE nic"
11336 				    " event of %s (ENOMEM)\n",
11337 				    ipif->ipif_ill->ill_name));
11338 				kmem_free(info, sizeof (hook_nic_event_t));
11339 			}
11340 		} else
11341 			ip2dbg(("ip_sioctl_addr_tail: could not attach "
11342 			    "ADDRESS_CHANGE nic event information for %s "
11343 			    "(ENOMEM)\n", ipif->ipif_ill->ill_name));
11344 
11345 		ipif->ipif_ill->ill_nic_event_info = info;
11346 	}
11347 
11348 	mutex_exit(&ipif->ipif_ill->ill_lock);
11349 
11350 	if (need_up) {
11351 		/*
11352 		 * Now bring the interface back up.  If this
11353 		 * is the only IPIF for the ILL, ipif_up
11354 		 * will have to re-bind to the device, so
11355 		 * we may get back EINPROGRESS, in which
11356 		 * case, this IOCTL will get completed in
11357 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11358 		 */
11359 		err = ipif_up(ipif, q, mp);
11360 	} else {
11361 		/*
11362 		 * Update the IPIF list in SCTP, ipif_up_done() will do it
11363 		 * if need_up is true.
11364 		 */
11365 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11366 	}
11367 
11368 	if (need_dl_down)
11369 		ill_dl_down(ill);
11370 	if (need_arp_down)
11371 		ipif_arp_down(ipif);
11372 
11373 	return (err);
11374 }
11375 
11376 
11377 /*
11378  * Restart entry point to restart the address set operation after the
11379  * refcounts have dropped to zero.
11380  */
11381 /* ARGSUSED */
11382 int
11383 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11384     ip_ioctl_cmd_t *ipip, void *ifreq)
11385 {
11386 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11387 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11388 	ASSERT(IAM_WRITER_IPIF(ipif));
11389 	ipif_down_tail(ipif);
11390 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11391 }
11392 
11393 /* ARGSUSED */
11394 int
11395 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11396     ip_ioctl_cmd_t *ipip, void *if_req)
11397 {
11398 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11399 	struct lifreq *lifr = (struct lifreq *)if_req;
11400 
11401 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11402 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11403 	/*
11404 	 * The net mask and address can't change since we have a
11405 	 * reference to the ipif. So no lock is necessary.
11406 	 */
11407 	if (ipif->ipif_isv6) {
11408 		*sin6 = sin6_null;
11409 		sin6->sin6_family = AF_INET6;
11410 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11411 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11412 		lifr->lifr_addrlen =
11413 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11414 	} else {
11415 		*sin = sin_null;
11416 		sin->sin_family = AF_INET;
11417 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11418 		if (ipip->ipi_cmd_type == LIF_CMD) {
11419 			lifr->lifr_addrlen =
11420 			    ip_mask_to_plen(ipif->ipif_net_mask);
11421 		}
11422 	}
11423 	return (0);
11424 }
11425 
11426 /*
11427  * Set the destination address for a pt-pt interface.
11428  */
11429 /* ARGSUSED */
11430 int
11431 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11432     ip_ioctl_cmd_t *ipip, void *if_req)
11433 {
11434 	int err = 0;
11435 	in6_addr_t v6addr;
11436 	boolean_t need_up = B_FALSE;
11437 
11438 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11439 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11440 	ASSERT(IAM_WRITER_IPIF(ipif));
11441 
11442 	if (ipif->ipif_isv6) {
11443 		sin6_t *sin6;
11444 
11445 		if (sin->sin_family != AF_INET6)
11446 			return (EAFNOSUPPORT);
11447 
11448 		sin6 = (sin6_t *)sin;
11449 		v6addr = sin6->sin6_addr;
11450 
11451 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11452 			return (EADDRNOTAVAIL);
11453 	} else {
11454 		ipaddr_t addr;
11455 
11456 		if (sin->sin_family != AF_INET)
11457 			return (EAFNOSUPPORT);
11458 
11459 		addr = sin->sin_addr.s_addr;
11460 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11461 			return (EADDRNOTAVAIL);
11462 
11463 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11464 	}
11465 
11466 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11467 		return (0);	/* No change */
11468 
11469 	if (ipif->ipif_flags & IPIF_UP) {
11470 		/*
11471 		 * If the interface is already marked up,
11472 		 * we call ipif_down which will take care
11473 		 * of ditching any IREs that have been set
11474 		 * up based on the old pp dst address.
11475 		 */
11476 		err = ipif_logical_down(ipif, q, mp);
11477 		if (err == EINPROGRESS)
11478 			return (err);
11479 		ipif_down_tail(ipif);
11480 		need_up = B_TRUE;
11481 	}
11482 	/*
11483 	 * could return EINPROGRESS. If so ioctl will complete in
11484 	 * ip_rput_dlpi_writer
11485 	 */
11486 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11487 	return (err);
11488 }
11489 
11490 static int
11491 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11492     boolean_t need_up)
11493 {
11494 	in6_addr_t v6addr;
11495 	ill_t	*ill = ipif->ipif_ill;
11496 	int	err = 0;
11497 	boolean_t need_dl_down;
11498 	boolean_t need_arp_down;
11499 
11500 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11501 	    ipif->ipif_id, (void *)ipif));
11502 
11503 	/* Must cancel any pending timer before taking the ill_lock */
11504 	if (ipif->ipif_recovery_id != 0)
11505 		(void) untimeout(ipif->ipif_recovery_id);
11506 	ipif->ipif_recovery_id = 0;
11507 
11508 	if (ipif->ipif_isv6) {
11509 		sin6_t *sin6;
11510 
11511 		sin6 = (sin6_t *)sin;
11512 		v6addr = sin6->sin6_addr;
11513 	} else {
11514 		ipaddr_t addr;
11515 
11516 		addr = sin->sin_addr.s_addr;
11517 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11518 	}
11519 	mutex_enter(&ill->ill_lock);
11520 	/* Set point to point destination address. */
11521 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11522 		/*
11523 		 * Allow this as a means of creating logical
11524 		 * pt-pt interfaces on top of e.g. an Ethernet.
11525 		 * XXX Undocumented HACK for testing.
11526 		 * pt-pt interfaces are created with NUD disabled.
11527 		 */
11528 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11529 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11530 		if (ipif->ipif_isv6)
11531 			ill->ill_flags |= ILLF_NONUD;
11532 	}
11533 
11534 	/*
11535 	 * If the interface was previously marked as a duplicate, then since
11536 	 * we've now got a "new" address, it should no longer be considered a
11537 	 * duplicate -- even if the "new" address is the same as the old one.
11538 	 * Note that if all ipifs are down, we may have a pending ARP down
11539 	 * event to handle.
11540 	 */
11541 	need_dl_down = need_arp_down = B_FALSE;
11542 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11543 		need_arp_down = !need_up;
11544 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11545 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11546 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11547 			need_dl_down = B_TRUE;
11548 		}
11549 	}
11550 
11551 	/* Set the new address. */
11552 	ipif->ipif_v6pp_dst_addr = v6addr;
11553 	/* Make sure subnet tracks pp_dst */
11554 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11555 	mutex_exit(&ill->ill_lock);
11556 
11557 	if (need_up) {
11558 		/*
11559 		 * Now bring the interface back up.  If this
11560 		 * is the only IPIF for the ILL, ipif_up
11561 		 * will have to re-bind to the device, so
11562 		 * we may get back EINPROGRESS, in which
11563 		 * case, this IOCTL will get completed in
11564 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11565 		 */
11566 		err = ipif_up(ipif, q, mp);
11567 	}
11568 
11569 	if (need_dl_down)
11570 		ill_dl_down(ill);
11571 
11572 	if (need_arp_down)
11573 		ipif_arp_down(ipif);
11574 	return (err);
11575 }
11576 
11577 /*
11578  * Restart entry point to restart the dstaddress set operation after the
11579  * refcounts have dropped to zero.
11580  */
11581 /* ARGSUSED */
11582 int
11583 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11584     ip_ioctl_cmd_t *ipip, void *ifreq)
11585 {
11586 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11587 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11588 	ipif_down_tail(ipif);
11589 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11590 }
11591 
11592 /* ARGSUSED */
11593 int
11594 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11595     ip_ioctl_cmd_t *ipip, void *if_req)
11596 {
11597 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11598 
11599 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11600 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11601 	/*
11602 	 * Get point to point destination address. The addresses can't
11603 	 * change since we hold a reference to the ipif.
11604 	 */
11605 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11606 		return (EADDRNOTAVAIL);
11607 
11608 	if (ipif->ipif_isv6) {
11609 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11610 		*sin6 = sin6_null;
11611 		sin6->sin6_family = AF_INET6;
11612 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11613 	} else {
11614 		*sin = sin_null;
11615 		sin->sin_family = AF_INET;
11616 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11617 	}
11618 	return (0);
11619 }
11620 
11621 /*
11622  * part of ipmp, make this func return the active/inactive state and
11623  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11624  */
11625 /*
11626  * This function either sets or clears the IFF_INACTIVE flag.
11627  *
11628  * As long as there are some addresses or multicast memberships on the
11629  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11630  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11631  * will be used for outbound packets.
11632  *
11633  * Caller needs to verify the validity of setting IFF_INACTIVE.
11634  */
11635 static void
11636 phyint_inactive(phyint_t *phyi)
11637 {
11638 	ill_t *ill_v4;
11639 	ill_t *ill_v6;
11640 	ipif_t *ipif;
11641 	ilm_t *ilm;
11642 
11643 	ill_v4 = phyi->phyint_illv4;
11644 	ill_v6 = phyi->phyint_illv6;
11645 
11646 	/*
11647 	 * No need for a lock while traversing the list since iam
11648 	 * a writer
11649 	 */
11650 	if (ill_v4 != NULL) {
11651 		ASSERT(IAM_WRITER_ILL(ill_v4));
11652 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11653 		    ipif = ipif->ipif_next) {
11654 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11655 				mutex_enter(&phyi->phyint_lock);
11656 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11657 				mutex_exit(&phyi->phyint_lock);
11658 				return;
11659 			}
11660 		}
11661 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11662 		    ilm = ilm->ilm_next) {
11663 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11664 				mutex_enter(&phyi->phyint_lock);
11665 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11666 				mutex_exit(&phyi->phyint_lock);
11667 				return;
11668 			}
11669 		}
11670 	}
11671 	if (ill_v6 != NULL) {
11672 		ill_v6 = phyi->phyint_illv6;
11673 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11674 		    ipif = ipif->ipif_next) {
11675 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11676 				mutex_enter(&phyi->phyint_lock);
11677 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11678 				mutex_exit(&phyi->phyint_lock);
11679 				return;
11680 			}
11681 		}
11682 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11683 		    ilm = ilm->ilm_next) {
11684 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11685 				mutex_enter(&phyi->phyint_lock);
11686 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11687 				mutex_exit(&phyi->phyint_lock);
11688 				return;
11689 			}
11690 		}
11691 	}
11692 	mutex_enter(&phyi->phyint_lock);
11693 	phyi->phyint_flags |= PHYI_INACTIVE;
11694 	mutex_exit(&phyi->phyint_lock);
11695 }
11696 
11697 /*
11698  * This function is called only when the phyint flags change. Currently
11699  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11700  * that we can select a good ill.
11701  */
11702 static void
11703 ip_redo_nomination(phyint_t *phyi)
11704 {
11705 	ill_t *ill_v4;
11706 
11707 	ill_v4 = phyi->phyint_illv4;
11708 
11709 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11710 		ASSERT(IAM_WRITER_ILL(ill_v4));
11711 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11712 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11713 	}
11714 }
11715 
11716 /*
11717  * Heuristic to check if ill is INACTIVE.
11718  * Checks if ill has an ipif with an usable ip address.
11719  *
11720  * Return values:
11721  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11722  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11723  */
11724 static boolean_t
11725 ill_is_inactive(ill_t *ill)
11726 {
11727 	ipif_t *ipif;
11728 
11729 	/* Check whether it is in an IPMP group */
11730 	if (ill->ill_phyint->phyint_groupname == NULL)
11731 		return (B_FALSE);
11732 
11733 	if (ill->ill_ipif_up_count == 0)
11734 		return (B_TRUE);
11735 
11736 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11737 		uint64_t flags = ipif->ipif_flags;
11738 
11739 		/*
11740 		 * This ipif is usable if it is IPIF_UP and not a
11741 		 * dedicated test address.  A dedicated test address
11742 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11743 		 * (note in particular that V6 test addresses are
11744 		 * link-local data addresses and thus are marked
11745 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11746 		 */
11747 		if ((flags & IPIF_UP) &&
11748 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11749 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11750 			return (B_FALSE);
11751 	}
11752 	return (B_TRUE);
11753 }
11754 
11755 /*
11756  * Set interface flags.
11757  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11758  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11759  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11760  *
11761  * NOTE : We really don't enforce that ipif_id zero should be used
11762  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11763  *	  is because applications generally does SICGLIFFLAGS and
11764  *	  ORs in the new flags (that affects the logical) and does a
11765  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11766  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11767  *	  flags that will be turned on is correct with respect to
11768  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11769  */
11770 /* ARGSUSED */
11771 int
11772 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11773     ip_ioctl_cmd_t *ipip, void *if_req)
11774 {
11775 	uint64_t turn_on;
11776 	uint64_t turn_off;
11777 	int	err;
11778 	boolean_t need_up = B_FALSE;
11779 	phyint_t *phyi;
11780 	ill_t *ill;
11781 	uint64_t intf_flags;
11782 	boolean_t phyint_flags_modified = B_FALSE;
11783 	uint64_t flags;
11784 	struct ifreq *ifr;
11785 	struct lifreq *lifr;
11786 	boolean_t set_linklocal = B_FALSE;
11787 	boolean_t zero_source = B_FALSE;
11788 
11789 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11790 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11791 
11792 	ASSERT(IAM_WRITER_IPIF(ipif));
11793 
11794 	ill = ipif->ipif_ill;
11795 	phyi = ill->ill_phyint;
11796 
11797 	if (ipip->ipi_cmd_type == IF_CMD) {
11798 		ifr = (struct ifreq *)if_req;
11799 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11800 	} else {
11801 		lifr = (struct lifreq *)if_req;
11802 		flags = lifr->lifr_flags;
11803 	}
11804 
11805 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11806 
11807 	/*
11808 	 * Has the flags been set correctly till now ?
11809 	 */
11810 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11811 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11812 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11813 	/*
11814 	 * Compare the new flags to the old, and partition
11815 	 * into those coming on and those going off.
11816 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11817 	 */
11818 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11819 		flags |= intf_flags & ~0xFFFF;
11820 
11821 	/*
11822 	 * First check which bits will change and then which will
11823 	 * go on and off
11824 	 */
11825 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11826 	if (!turn_on)
11827 		return (0);	/* No change */
11828 
11829 	turn_off = intf_flags & turn_on;
11830 	turn_on ^= turn_off;
11831 	err = 0;
11832 
11833 	/*
11834 	 * Don't allow any bits belonging to the logical interface
11835 	 * to be set or cleared on the replacement ipif that was
11836 	 * created temporarily during a MOVE.
11837 	 */
11838 	if (ipif->ipif_replace_zero &&
11839 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11840 		return (EINVAL);
11841 	}
11842 
11843 	/*
11844 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11845 	 * IPv6 interfaces.
11846 	 */
11847 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11848 		return (EINVAL);
11849 
11850 	/*
11851 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11852 	 * interfaces.  It makes no sense in that context.
11853 	 */
11854 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11855 		return (EINVAL);
11856 
11857 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11858 		zero_source = B_TRUE;
11859 
11860 	/*
11861 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11862 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11863 	 * If the link local address isn't set, and can be set, it will get
11864 	 * set later on in this function.
11865 	 */
11866 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11867 	    (flags & IFF_UP) && !zero_source &&
11868 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11869 		if (ipif_cant_setlinklocal(ipif))
11870 			return (EINVAL);
11871 		set_linklocal = B_TRUE;
11872 	}
11873 
11874 	/*
11875 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11876 	 * same time. No need to grab ill_g_usesrc_lock here, see
11877 	 * synchronization notes in ip.c
11878 	 */
11879 	if (turn_on & PHYI_STANDBY &&
11880 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11881 		return (EINVAL);
11882 	}
11883 
11884 	/*
11885 	 * If we modify physical interface flags, we'll potentially need to
11886 	 * send up two routing socket messages for the changes (one for the
11887 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11888 	 */
11889 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11890 		phyint_flags_modified = B_TRUE;
11891 
11892 	/*
11893 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11894 	 * we need to flush the IRE_CACHES belonging to this ill.
11895 	 * We handle this case here without doing the DOWN/UP dance
11896 	 * like it is done for other flags. If some other flags are
11897 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11898 	 * below will handle it by bringing it down and then
11899 	 * bringing it UP.
11900 	 */
11901 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11902 		ill_t *ill_v4, *ill_v6;
11903 
11904 		ill_v4 = phyi->phyint_illv4;
11905 		ill_v6 = phyi->phyint_illv6;
11906 
11907 		/*
11908 		 * First set the INACTIVE flag if needed. Then delete the ires.
11909 		 * ire_add will atomically prevent creating new IRE_CACHEs
11910 		 * unless hidden flag is set.
11911 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11912 		 */
11913 		if ((turn_on & PHYI_FAILED) &&
11914 		    ((intf_flags & PHYI_STANDBY) || !ipmp_enable_failback)) {
11915 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11916 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11917 		}
11918 		if ((turn_off & PHYI_FAILED) &&
11919 		    ((intf_flags & PHYI_STANDBY) ||
11920 		    (!ipmp_enable_failback && ill_is_inactive(ill)))) {
11921 			phyint_inactive(phyi);
11922 		}
11923 
11924 		if (turn_on & PHYI_STANDBY) {
11925 			/*
11926 			 * We implicitly set INACTIVE only when STANDBY is set.
11927 			 * INACTIVE is also set on non-STANDBY phyint when user
11928 			 * disables FAILBACK using configuration file.
11929 			 * Do not allow STANDBY to be set on such INACTIVE
11930 			 * phyint
11931 			 */
11932 			if (phyi->phyint_flags & PHYI_INACTIVE)
11933 				return (EINVAL);
11934 			if (!(phyi->phyint_flags & PHYI_FAILED))
11935 				phyint_inactive(phyi);
11936 		}
11937 		if (turn_off & PHYI_STANDBY) {
11938 			if (ipmp_enable_failback) {
11939 				/*
11940 				 * Reset PHYI_INACTIVE.
11941 				 */
11942 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11943 			} else if (ill_is_inactive(ill) &&
11944 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11945 				/*
11946 				 * Need to set INACTIVE, when user sets
11947 				 * STANDBY on a non-STANDBY phyint and
11948 				 * later resets STANDBY
11949 				 */
11950 				phyint_inactive(phyi);
11951 			}
11952 		}
11953 		/*
11954 		 * We should always send up a message so that the
11955 		 * daemons come to know of it. Note that the zeroth
11956 		 * interface can be down and the check below for IPIF_UP
11957 		 * will not make sense as we are actually setting
11958 		 * a phyint flag here. We assume that the ipif used
11959 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11960 		 * send up any message for non-zero ipifs).
11961 		 */
11962 		phyint_flags_modified = B_TRUE;
11963 
11964 		if (ill_v4 != NULL) {
11965 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11966 			    IRE_CACHE, ill_stq_cache_delete,
11967 			    (char *)ill_v4, ill_v4);
11968 			illgrp_reset_schednext(ill_v4);
11969 		}
11970 		if (ill_v6 != NULL) {
11971 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11972 			    IRE_CACHE, ill_stq_cache_delete,
11973 			    (char *)ill_v6, ill_v6);
11974 			illgrp_reset_schednext(ill_v6);
11975 		}
11976 	}
11977 
11978 	/*
11979 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11980 	 * status of the interface and, if the interface is part of an IPMP
11981 	 * group, all other interfaces that are part of the same IPMP
11982 	 * group.
11983 	 */
11984 	if ((turn_on | turn_off) & ILLF_ROUTER) {
11985 		(void) ill_forward_set(q, mp, ((turn_on & ILLF_ROUTER) != 0),
11986 		    (caddr_t)ill);
11987 	}
11988 
11989 	/*
11990 	 * If the interface is not UP and we are not going to
11991 	 * bring it UP, record the flags and return. When the
11992 	 * interface comes UP later, the right actions will be
11993 	 * taken.
11994 	 */
11995 	if (!(ipif->ipif_flags & IPIF_UP) &&
11996 	    !(turn_on & IPIF_UP)) {
11997 		/* Record new flags in their respective places. */
11998 		mutex_enter(&ill->ill_lock);
11999 		mutex_enter(&ill->ill_phyint->phyint_lock);
12000 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12001 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12002 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12003 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12004 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12005 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12006 		mutex_exit(&ill->ill_lock);
12007 		mutex_exit(&ill->ill_phyint->phyint_lock);
12008 
12009 		/*
12010 		 * We do the broadcast and nomination here rather
12011 		 * than waiting for a FAILOVER/FAILBACK to happen. In
12012 		 * the case of FAILBACK from INACTIVE standby to the
12013 		 * interface that has been repaired, PHYI_FAILED has not
12014 		 * been cleared yet. If there are only two interfaces in
12015 		 * that group, all we have is a FAILED and INACTIVE
12016 		 * interface. If we do the nomination soon after a failback,
12017 		 * the broadcast nomination code would select the
12018 		 * INACTIVE interface for receiving broadcasts as FAILED is
12019 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
12020 		 * receive broadcast packets, we need to redo nomination
12021 		 * when the FAILED is cleared here. Thus, in general we
12022 		 * always do the nomination here for FAILED, STANDBY
12023 		 * and OFFLINE.
12024 		 */
12025 		if (((turn_on | turn_off) &
12026 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
12027 			ip_redo_nomination(phyi);
12028 		}
12029 		if (phyint_flags_modified) {
12030 			if (phyi->phyint_illv4 != NULL) {
12031 				ip_rts_ifmsg(phyi->phyint_illv4->
12032 				    ill_ipif);
12033 			}
12034 			if (phyi->phyint_illv6 != NULL) {
12035 				ip_rts_ifmsg(phyi->phyint_illv6->
12036 				    ill_ipif);
12037 			}
12038 		}
12039 		return (0);
12040 	} else if (set_linklocal || zero_source) {
12041 		mutex_enter(&ill->ill_lock);
12042 		if (set_linklocal)
12043 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
12044 		if (zero_source)
12045 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
12046 		mutex_exit(&ill->ill_lock);
12047 	}
12048 
12049 	/*
12050 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
12051 	 * or point-to-point interfaces with an unspecified destination. We do
12052 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
12053 	 * have a subnet assigned, which is how in.ndpd currently manages its
12054 	 * onlink prefix list when no addresses are configured with those
12055 	 * prefixes.
12056 	 */
12057 	if (ipif->ipif_isv6 &&
12058 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
12059 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
12060 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
12061 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12062 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
12063 		return (EINVAL);
12064 	}
12065 
12066 	/*
12067 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
12068 	 * from being brought up.
12069 	 */
12070 	if (!ipif->ipif_isv6 &&
12071 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12072 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
12073 		return (EINVAL);
12074 	}
12075 
12076 	/*
12077 	 * The only flag changes that we currently take specific action on
12078 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
12079 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
12080 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
12081 	 * the flags and bringing it back up again.
12082 	 */
12083 	if ((turn_on|turn_off) &
12084 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
12085 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
12086 		/*
12087 		 * Taking this ipif down, make sure we have
12088 		 * valid net and subnet bcast ire's for other
12089 		 * logical interfaces, if we need them.
12090 		 */
12091 		if (!ipif->ipif_isv6)
12092 			ipif_check_bcast_ires(ipif);
12093 
12094 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
12095 		    !(turn_off & IPIF_UP)) {
12096 			need_up = B_TRUE;
12097 			if (ipif->ipif_flags & IPIF_UP)
12098 				ill->ill_logical_down = 1;
12099 			turn_on &= ~IPIF_UP;
12100 		}
12101 		err = ipif_down(ipif, q, mp);
12102 		ip1dbg(("ipif_down returns %d err ", err));
12103 		if (err == EINPROGRESS)
12104 			return (err);
12105 		ipif_down_tail(ipif);
12106 	}
12107 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
12108 }
12109 
12110 static int
12111 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
12112     boolean_t need_up)
12113 {
12114 	ill_t	*ill;
12115 	phyint_t *phyi;
12116 	uint64_t turn_on;
12117 	uint64_t turn_off;
12118 	uint64_t intf_flags;
12119 	boolean_t phyint_flags_modified = B_FALSE;
12120 	int	err = 0;
12121 	boolean_t set_linklocal = B_FALSE;
12122 	boolean_t zero_source = B_FALSE;
12123 
12124 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12125 		ipif->ipif_ill->ill_name, ipif->ipif_id));
12126 
12127 	ASSERT(IAM_WRITER_IPIF(ipif));
12128 
12129 	ill = ipif->ipif_ill;
12130 	phyi = ill->ill_phyint;
12131 
12132 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12133 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12134 
12135 	turn_off = intf_flags & turn_on;
12136 	turn_on ^= turn_off;
12137 
12138 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12139 		phyint_flags_modified = B_TRUE;
12140 
12141 	/*
12142 	 * Now we change the flags. Track current value of
12143 	 * other flags in their respective places.
12144 	 */
12145 	mutex_enter(&ill->ill_lock);
12146 	mutex_enter(&phyi->phyint_lock);
12147 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12148 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12149 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12150 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12151 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12152 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12153 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12154 		set_linklocal = B_TRUE;
12155 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12156 	}
12157 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12158 		zero_source = B_TRUE;
12159 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12160 	}
12161 	mutex_exit(&ill->ill_lock);
12162 	mutex_exit(&phyi->phyint_lock);
12163 
12164 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12165 		ip_redo_nomination(phyi);
12166 
12167 	if (set_linklocal)
12168 		(void) ipif_setlinklocal(ipif);
12169 
12170 	if (zero_source)
12171 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12172 	else
12173 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12174 
12175 	if (need_up) {
12176 		/*
12177 		 * XXX ipif_up really does not know whether a phyint flags
12178 		 * was modified or not. So, it sends up information on
12179 		 * only one routing sockets message. As we don't bring up
12180 		 * the interface and also set STANDBY/FAILED simultaneously
12181 		 * it should be okay.
12182 		 */
12183 		err = ipif_up(ipif, q, mp);
12184 	} else {
12185 		/*
12186 		 * Make sure routing socket sees all changes to the flags.
12187 		 * ipif_up_done* handles this when we use ipif_up.
12188 		 */
12189 		if (phyint_flags_modified) {
12190 			if (phyi->phyint_illv4 != NULL) {
12191 				ip_rts_ifmsg(phyi->phyint_illv4->
12192 				    ill_ipif);
12193 			}
12194 			if (phyi->phyint_illv6 != NULL) {
12195 				ip_rts_ifmsg(phyi->phyint_illv6->
12196 				    ill_ipif);
12197 			}
12198 		} else {
12199 			ip_rts_ifmsg(ipif);
12200 		}
12201 	}
12202 	return (err);
12203 }
12204 
12205 /*
12206  * Restart entry point to restart the flags restart operation after the
12207  * refcounts have dropped to zero.
12208  */
12209 /* ARGSUSED */
12210 int
12211 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12212     ip_ioctl_cmd_t *ipip, void *if_req)
12213 {
12214 	int	err;
12215 	struct ifreq *ifr = (struct ifreq *)if_req;
12216 	struct lifreq *lifr = (struct lifreq *)if_req;
12217 
12218 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12219 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12220 
12221 	ipif_down_tail(ipif);
12222 	if (ipip->ipi_cmd_type == IF_CMD) {
12223 		/*
12224 		 * Since ip_sioctl_flags expects an int and ifr_flags
12225 		 * is a short we need to cast ifr_flags into an int
12226 		 * to avoid having sign extension cause bits to get
12227 		 * set that should not be.
12228 		 */
12229 		err = ip_sioctl_flags_tail(ipif,
12230 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
12231 		    q, mp, B_TRUE);
12232 	} else {
12233 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
12234 		    q, mp, B_TRUE);
12235 	}
12236 	return (err);
12237 }
12238 
12239 /* ARGSUSED */
12240 int
12241 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12242     ip_ioctl_cmd_t *ipip, void *if_req)
12243 {
12244 	/*
12245 	 * Has the flags been set correctly till now ?
12246 	 */
12247 	ill_t *ill = ipif->ipif_ill;
12248 	phyint_t *phyi = ill->ill_phyint;
12249 
12250 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12251 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12252 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12253 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12254 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12255 
12256 	/*
12257 	 * Need a lock since some flags can be set even when there are
12258 	 * references to the ipif.
12259 	 */
12260 	mutex_enter(&ill->ill_lock);
12261 	if (ipip->ipi_cmd_type == IF_CMD) {
12262 		struct ifreq *ifr = (struct ifreq *)if_req;
12263 
12264 		/* Get interface flags (low 16 only). */
12265 		ifr->ifr_flags = ((ipif->ipif_flags |
12266 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12267 	} else {
12268 		struct lifreq *lifr = (struct lifreq *)if_req;
12269 
12270 		/* Get interface flags. */
12271 		lifr->lifr_flags = ipif->ipif_flags |
12272 		    ill->ill_flags | phyi->phyint_flags;
12273 	}
12274 	mutex_exit(&ill->ill_lock);
12275 	return (0);
12276 }
12277 
12278 /* ARGSUSED */
12279 int
12280 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12281     ip_ioctl_cmd_t *ipip, void *if_req)
12282 {
12283 	int mtu;
12284 	int ip_min_mtu;
12285 	struct ifreq	*ifr;
12286 	struct lifreq *lifr;
12287 	ire_t	*ire;
12288 
12289 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12290 	    ipif->ipif_id, (void *)ipif));
12291 	if (ipip->ipi_cmd_type == IF_CMD) {
12292 		ifr = (struct ifreq *)if_req;
12293 		mtu = ifr->ifr_metric;
12294 	} else {
12295 		lifr = (struct lifreq *)if_req;
12296 		mtu = lifr->lifr_mtu;
12297 	}
12298 
12299 	if (ipif->ipif_isv6)
12300 		ip_min_mtu = IPV6_MIN_MTU;
12301 	else
12302 		ip_min_mtu = IP_MIN_MTU;
12303 
12304 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12305 		return (EINVAL);
12306 
12307 	/*
12308 	 * Change the MTU size in all relevant ire's.
12309 	 * Mtu change Vs. new ire creation - protocol below.
12310 	 * First change ipif_mtu and the ire_max_frag of the
12311 	 * interface ire. Then do an ire walk and change the
12312 	 * ire_max_frag of all affected ires. During ire_add
12313 	 * under the bucket lock, set the ire_max_frag of the
12314 	 * new ire being created from the ipif/ire from which
12315 	 * it is being derived. If an mtu change happens after
12316 	 * the ire is added, the new ire will be cleaned up.
12317 	 * Conversely if the mtu change happens before the ire
12318 	 * is added, ire_add will see the new value of the mtu.
12319 	 */
12320 	ipif->ipif_mtu = mtu;
12321 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12322 
12323 	if (ipif->ipif_isv6)
12324 		ire = ipif_to_ire_v6(ipif);
12325 	else
12326 		ire = ipif_to_ire(ipif);
12327 	if (ire != NULL) {
12328 		ire->ire_max_frag = ipif->ipif_mtu;
12329 		ire_refrele(ire);
12330 	}
12331 	if (ipif->ipif_flags & IPIF_UP) {
12332 		if (ipif->ipif_isv6)
12333 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES);
12334 		else
12335 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES);
12336 	}
12337 	/* Update the MTU in SCTP's list */
12338 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12339 	return (0);
12340 }
12341 
12342 /* Get interface MTU. */
12343 /* ARGSUSED */
12344 int
12345 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12346 	ip_ioctl_cmd_t *ipip, void *if_req)
12347 {
12348 	struct ifreq	*ifr;
12349 	struct lifreq	*lifr;
12350 
12351 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12352 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12353 	if (ipip->ipi_cmd_type == IF_CMD) {
12354 		ifr = (struct ifreq *)if_req;
12355 		ifr->ifr_metric = ipif->ipif_mtu;
12356 	} else {
12357 		lifr = (struct lifreq *)if_req;
12358 		lifr->lifr_mtu = ipif->ipif_mtu;
12359 	}
12360 	return (0);
12361 }
12362 
12363 /* Set interface broadcast address. */
12364 /* ARGSUSED2 */
12365 int
12366 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12367 	ip_ioctl_cmd_t *ipip, void *if_req)
12368 {
12369 	ipaddr_t addr;
12370 	ire_t	*ire;
12371 
12372 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12373 	    ipif->ipif_id));
12374 
12375 	ASSERT(IAM_WRITER_IPIF(ipif));
12376 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12377 		return (EADDRNOTAVAIL);
12378 
12379 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12380 
12381 	if (sin->sin_family != AF_INET)
12382 		return (EAFNOSUPPORT);
12383 
12384 	addr = sin->sin_addr.s_addr;
12385 	if (ipif->ipif_flags & IPIF_UP) {
12386 		/*
12387 		 * If we are already up, make sure the new
12388 		 * broadcast address makes sense.  If it does,
12389 		 * there should be an IRE for it already.
12390 		 * Don't match on ipif, only on the ill
12391 		 * since we are sharing these now. Don't use
12392 		 * MATCH_IRE_ILL_GROUP as we are looking for
12393 		 * the broadcast ire on this ill and each ill
12394 		 * in the group has its own broadcast ire.
12395 		 */
12396 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12397 		    ipif, ALL_ZONES, NULL,
12398 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE));
12399 		if (ire == NULL) {
12400 			return (EINVAL);
12401 		} else {
12402 			ire_refrele(ire);
12403 		}
12404 	}
12405 	/*
12406 	 * Changing the broadcast addr for this ipif.
12407 	 * Make sure we have valid net and subnet bcast
12408 	 * ire's for other logical interfaces, if needed.
12409 	 */
12410 	if (addr != ipif->ipif_brd_addr)
12411 		ipif_check_bcast_ires(ipif);
12412 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12413 	return (0);
12414 }
12415 
12416 /* Get interface broadcast address. */
12417 /* ARGSUSED */
12418 int
12419 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12420     ip_ioctl_cmd_t *ipip, void *if_req)
12421 {
12422 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12423 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12424 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12425 		return (EADDRNOTAVAIL);
12426 
12427 	/* IPIF_BROADCAST not possible with IPv6 */
12428 	ASSERT(!ipif->ipif_isv6);
12429 	*sin = sin_null;
12430 	sin->sin_family = AF_INET;
12431 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12432 	return (0);
12433 }
12434 
12435 /*
12436  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12437  */
12438 /* ARGSUSED */
12439 int
12440 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12441     ip_ioctl_cmd_t *ipip, void *if_req)
12442 {
12443 	int err = 0;
12444 	in6_addr_t v6mask;
12445 
12446 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12447 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12448 
12449 	ASSERT(IAM_WRITER_IPIF(ipif));
12450 
12451 	if (ipif->ipif_isv6) {
12452 		sin6_t *sin6;
12453 
12454 		if (sin->sin_family != AF_INET6)
12455 			return (EAFNOSUPPORT);
12456 
12457 		sin6 = (sin6_t *)sin;
12458 		v6mask = sin6->sin6_addr;
12459 	} else {
12460 		ipaddr_t mask;
12461 
12462 		if (sin->sin_family != AF_INET)
12463 			return (EAFNOSUPPORT);
12464 
12465 		mask = sin->sin_addr.s_addr;
12466 		V4MASK_TO_V6(mask, v6mask);
12467 	}
12468 
12469 	/*
12470 	 * No big deal if the interface isn't already up, or the mask
12471 	 * isn't really changing, or this is pt-pt.
12472 	 */
12473 	if (!(ipif->ipif_flags & IPIF_UP) ||
12474 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12475 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12476 		ipif->ipif_v6net_mask = v6mask;
12477 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12478 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12479 			    ipif->ipif_v6net_mask,
12480 			    ipif->ipif_v6subnet);
12481 		}
12482 		return (0);
12483 	}
12484 	/*
12485 	 * Make sure we have valid net and subnet broadcast ire's
12486 	 * for the old netmask, if needed by other logical interfaces.
12487 	 */
12488 	if (!ipif->ipif_isv6)
12489 		ipif_check_bcast_ires(ipif);
12490 
12491 	err = ipif_logical_down(ipif, q, mp);
12492 	if (err == EINPROGRESS)
12493 		return (err);
12494 	ipif_down_tail(ipif);
12495 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12496 	return (err);
12497 }
12498 
12499 static int
12500 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12501 {
12502 	in6_addr_t v6mask;
12503 	int err = 0;
12504 
12505 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12506 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12507 
12508 	if (ipif->ipif_isv6) {
12509 		sin6_t *sin6;
12510 
12511 		sin6 = (sin6_t *)sin;
12512 		v6mask = sin6->sin6_addr;
12513 	} else {
12514 		ipaddr_t mask;
12515 
12516 		mask = sin->sin_addr.s_addr;
12517 		V4MASK_TO_V6(mask, v6mask);
12518 	}
12519 
12520 	ipif->ipif_v6net_mask = v6mask;
12521 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12522 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12523 		    ipif->ipif_v6subnet);
12524 	}
12525 	err = ipif_up(ipif, q, mp);
12526 
12527 	if (err == 0 || err == EINPROGRESS) {
12528 		/*
12529 		 * The interface must be DL_BOUND if this packet has to
12530 		 * go out on the wire. Since we only go through a logical
12531 		 * down and are bound with the driver during an internal
12532 		 * down/up that is satisfied.
12533 		 */
12534 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12535 			/* Potentially broadcast an address mask reply. */
12536 			ipif_mask_reply(ipif);
12537 		}
12538 	}
12539 	return (err);
12540 }
12541 
12542 /* ARGSUSED */
12543 int
12544 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12545     ip_ioctl_cmd_t *ipip, void *if_req)
12546 {
12547 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12548 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12549 	ipif_down_tail(ipif);
12550 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12551 }
12552 
12553 /* Get interface net mask. */
12554 /* ARGSUSED */
12555 int
12556 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12557     ip_ioctl_cmd_t *ipip, void *if_req)
12558 {
12559 	struct lifreq *lifr = (struct lifreq *)if_req;
12560 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12561 
12562 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12563 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12564 
12565 	/*
12566 	 * net mask can't change since we have a reference to the ipif.
12567 	 */
12568 	if (ipif->ipif_isv6) {
12569 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12570 		*sin6 = sin6_null;
12571 		sin6->sin6_family = AF_INET6;
12572 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12573 		lifr->lifr_addrlen =
12574 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12575 	} else {
12576 		*sin = sin_null;
12577 		sin->sin_family = AF_INET;
12578 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12579 		if (ipip->ipi_cmd_type == LIF_CMD) {
12580 			lifr->lifr_addrlen =
12581 			    ip_mask_to_plen(ipif->ipif_net_mask);
12582 		}
12583 	}
12584 	return (0);
12585 }
12586 
12587 /* ARGSUSED */
12588 int
12589 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12590     ip_ioctl_cmd_t *ipip, void *if_req)
12591 {
12592 
12593 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12594 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12595 	/*
12596 	 * Set interface metric.  We don't use this for
12597 	 * anything but we keep track of it in case it is
12598 	 * important to routing applications or such.
12599 	 */
12600 	if (ipip->ipi_cmd_type == IF_CMD) {
12601 		struct ifreq    *ifr;
12602 
12603 		ifr = (struct ifreq *)if_req;
12604 		ipif->ipif_metric = ifr->ifr_metric;
12605 	} else {
12606 		struct lifreq   *lifr;
12607 
12608 		lifr = (struct lifreq *)if_req;
12609 		ipif->ipif_metric = lifr->lifr_metric;
12610 	}
12611 	return (0);
12612 }
12613 
12614 
12615 /* ARGSUSED */
12616 int
12617 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12618     ip_ioctl_cmd_t *ipip, void *if_req)
12619 {
12620 
12621 	/* Get interface metric. */
12622 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12623 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12624 	if (ipip->ipi_cmd_type == IF_CMD) {
12625 		struct ifreq    *ifr;
12626 
12627 		ifr = (struct ifreq *)if_req;
12628 		ifr->ifr_metric = ipif->ipif_metric;
12629 	} else {
12630 		struct lifreq   *lifr;
12631 
12632 		lifr = (struct lifreq *)if_req;
12633 		lifr->lifr_metric = ipif->ipif_metric;
12634 	}
12635 
12636 	return (0);
12637 }
12638 
12639 /* ARGSUSED */
12640 int
12641 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12642     ip_ioctl_cmd_t *ipip, void *if_req)
12643 {
12644 
12645 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12646 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12647 	/*
12648 	 * Set the muxid returned from I_PLINK.
12649 	 */
12650 	if (ipip->ipi_cmd_type == IF_CMD) {
12651 		struct ifreq *ifr = (struct ifreq *)if_req;
12652 
12653 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12654 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12655 	} else {
12656 		struct lifreq *lifr = (struct lifreq *)if_req;
12657 
12658 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12659 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12660 	}
12661 	return (0);
12662 }
12663 
12664 /* ARGSUSED */
12665 int
12666 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12667     ip_ioctl_cmd_t *ipip, void *if_req)
12668 {
12669 
12670 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12671 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12672 	/*
12673 	 * Get the muxid saved in ill for I_PUNLINK.
12674 	 */
12675 	if (ipip->ipi_cmd_type == IF_CMD) {
12676 		struct ifreq *ifr = (struct ifreq *)if_req;
12677 
12678 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12679 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12680 	} else {
12681 		struct lifreq *lifr = (struct lifreq *)if_req;
12682 
12683 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12684 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12685 	}
12686 	return (0);
12687 }
12688 
12689 /*
12690  * Set the subnet prefix. Does not modify the broadcast address.
12691  */
12692 /* ARGSUSED */
12693 int
12694 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12695     ip_ioctl_cmd_t *ipip, void *if_req)
12696 {
12697 	int err = 0;
12698 	in6_addr_t v6addr;
12699 	in6_addr_t v6mask;
12700 	boolean_t need_up = B_FALSE;
12701 	int addrlen;
12702 
12703 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12704 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12705 
12706 	ASSERT(IAM_WRITER_IPIF(ipif));
12707 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12708 
12709 	if (ipif->ipif_isv6) {
12710 		sin6_t *sin6;
12711 
12712 		if (sin->sin_family != AF_INET6)
12713 			return (EAFNOSUPPORT);
12714 
12715 		sin6 = (sin6_t *)sin;
12716 		v6addr = sin6->sin6_addr;
12717 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12718 			return (EADDRNOTAVAIL);
12719 	} else {
12720 		ipaddr_t addr;
12721 
12722 		if (sin->sin_family != AF_INET)
12723 			return (EAFNOSUPPORT);
12724 
12725 		addr = sin->sin_addr.s_addr;
12726 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12727 			return (EADDRNOTAVAIL);
12728 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12729 		/* Add 96 bits */
12730 		addrlen += IPV6_ABITS - IP_ABITS;
12731 	}
12732 
12733 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12734 		return (EINVAL);
12735 
12736 	/* Check if bits in the address is set past the mask */
12737 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12738 		return (EINVAL);
12739 
12740 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12741 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12742 		return (0);	/* No change */
12743 
12744 	if (ipif->ipif_flags & IPIF_UP) {
12745 		/*
12746 		 * If the interface is already marked up,
12747 		 * we call ipif_down which will take care
12748 		 * of ditching any IREs that have been set
12749 		 * up based on the old interface address.
12750 		 */
12751 		err = ipif_logical_down(ipif, q, mp);
12752 		if (err == EINPROGRESS)
12753 			return (err);
12754 		ipif_down_tail(ipif);
12755 		need_up = B_TRUE;
12756 	}
12757 
12758 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12759 	return (err);
12760 }
12761 
12762 static int
12763 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12764     queue_t *q, mblk_t *mp, boolean_t need_up)
12765 {
12766 	ill_t	*ill = ipif->ipif_ill;
12767 	int	err = 0;
12768 
12769 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12770 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12771 
12772 	/* Set the new address. */
12773 	mutex_enter(&ill->ill_lock);
12774 	ipif->ipif_v6net_mask = v6mask;
12775 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12776 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12777 		    ipif->ipif_v6subnet);
12778 	}
12779 	mutex_exit(&ill->ill_lock);
12780 
12781 	if (need_up) {
12782 		/*
12783 		 * Now bring the interface back up.  If this
12784 		 * is the only IPIF for the ILL, ipif_up
12785 		 * will have to re-bind to the device, so
12786 		 * we may get back EINPROGRESS, in which
12787 		 * case, this IOCTL will get completed in
12788 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12789 		 */
12790 		err = ipif_up(ipif, q, mp);
12791 		if (err == EINPROGRESS)
12792 			return (err);
12793 	}
12794 	return (err);
12795 }
12796 
12797 /* ARGSUSED */
12798 int
12799 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12800     ip_ioctl_cmd_t *ipip, void *if_req)
12801 {
12802 	int	addrlen;
12803 	in6_addr_t v6addr;
12804 	in6_addr_t v6mask;
12805 	struct lifreq *lifr = (struct lifreq *)if_req;
12806 
12807 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12808 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12809 	ipif_down_tail(ipif);
12810 
12811 	addrlen = lifr->lifr_addrlen;
12812 	if (ipif->ipif_isv6) {
12813 		sin6_t *sin6;
12814 
12815 		sin6 = (sin6_t *)sin;
12816 		v6addr = sin6->sin6_addr;
12817 	} else {
12818 		ipaddr_t addr;
12819 
12820 		addr = sin->sin_addr.s_addr;
12821 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12822 		addrlen += IPV6_ABITS - IP_ABITS;
12823 	}
12824 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12825 
12826 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12827 }
12828 
12829 /* ARGSUSED */
12830 int
12831 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12832     ip_ioctl_cmd_t *ipip, void *if_req)
12833 {
12834 	struct lifreq *lifr = (struct lifreq *)if_req;
12835 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12836 
12837 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12838 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12839 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12840 
12841 	if (ipif->ipif_isv6) {
12842 		*sin6 = sin6_null;
12843 		sin6->sin6_family = AF_INET6;
12844 		sin6->sin6_addr = ipif->ipif_v6subnet;
12845 		lifr->lifr_addrlen =
12846 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12847 	} else {
12848 		*sin = sin_null;
12849 		sin->sin_family = AF_INET;
12850 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12851 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12852 	}
12853 	return (0);
12854 }
12855 
12856 /*
12857  * Set the IPv6 address token.
12858  */
12859 /* ARGSUSED */
12860 int
12861 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12862     ip_ioctl_cmd_t *ipi, void *if_req)
12863 {
12864 	ill_t *ill = ipif->ipif_ill;
12865 	int err;
12866 	in6_addr_t v6addr;
12867 	in6_addr_t v6mask;
12868 	boolean_t need_up = B_FALSE;
12869 	int i;
12870 	sin6_t *sin6 = (sin6_t *)sin;
12871 	struct lifreq *lifr = (struct lifreq *)if_req;
12872 	int addrlen;
12873 
12874 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12875 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12876 	ASSERT(IAM_WRITER_IPIF(ipif));
12877 
12878 	addrlen = lifr->lifr_addrlen;
12879 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12880 	if (ipif->ipif_id != 0)
12881 		return (EINVAL);
12882 
12883 	if (!ipif->ipif_isv6)
12884 		return (EINVAL);
12885 
12886 	if (addrlen > IPV6_ABITS)
12887 		return (EINVAL);
12888 
12889 	v6addr = sin6->sin6_addr;
12890 
12891 	/*
12892 	 * The length of the token is the length from the end.  To get
12893 	 * the proper mask for this, compute the mask of the bits not
12894 	 * in the token; ie. the prefix, and then xor to get the mask.
12895 	 */
12896 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12897 		return (EINVAL);
12898 	for (i = 0; i < 4; i++) {
12899 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12900 	}
12901 
12902 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12903 	    ill->ill_token_length == addrlen)
12904 		return (0);	/* No change */
12905 
12906 	if (ipif->ipif_flags & IPIF_UP) {
12907 		err = ipif_logical_down(ipif, q, mp);
12908 		if (err == EINPROGRESS)
12909 			return (err);
12910 		ipif_down_tail(ipif);
12911 		need_up = B_TRUE;
12912 	}
12913 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12914 	return (err);
12915 }
12916 
12917 static int
12918 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12919     mblk_t *mp, boolean_t need_up)
12920 {
12921 	in6_addr_t v6addr;
12922 	in6_addr_t v6mask;
12923 	ill_t	*ill = ipif->ipif_ill;
12924 	int	i;
12925 	int	err = 0;
12926 
12927 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12928 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12929 	v6addr = sin6->sin6_addr;
12930 	/*
12931 	 * The length of the token is the length from the end.  To get
12932 	 * the proper mask for this, compute the mask of the bits not
12933 	 * in the token; ie. the prefix, and then xor to get the mask.
12934 	 */
12935 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12936 	for (i = 0; i < 4; i++)
12937 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12938 
12939 	mutex_enter(&ill->ill_lock);
12940 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12941 	ill->ill_token_length = addrlen;
12942 	mutex_exit(&ill->ill_lock);
12943 
12944 	if (need_up) {
12945 		/*
12946 		 * Now bring the interface back up.  If this
12947 		 * is the only IPIF for the ILL, ipif_up
12948 		 * will have to re-bind to the device, so
12949 		 * we may get back EINPROGRESS, in which
12950 		 * case, this IOCTL will get completed in
12951 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12952 		 */
12953 		err = ipif_up(ipif, q, mp);
12954 		if (err == EINPROGRESS)
12955 			return (err);
12956 	}
12957 	return (err);
12958 }
12959 
12960 /* ARGSUSED */
12961 int
12962 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12963     ip_ioctl_cmd_t *ipi, void *if_req)
12964 {
12965 	ill_t *ill;
12966 	sin6_t *sin6 = (sin6_t *)sin;
12967 	struct lifreq *lifr = (struct lifreq *)if_req;
12968 
12969 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12970 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12971 	if (ipif->ipif_id != 0)
12972 		return (EINVAL);
12973 
12974 	ill = ipif->ipif_ill;
12975 	if (!ill->ill_isv6)
12976 		return (ENXIO);
12977 
12978 	*sin6 = sin6_null;
12979 	sin6->sin6_family = AF_INET6;
12980 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12981 	sin6->sin6_addr = ill->ill_token;
12982 	lifr->lifr_addrlen = ill->ill_token_length;
12983 	return (0);
12984 }
12985 
12986 /*
12987  * Set (hardware) link specific information that might override
12988  * what was acquired through the DL_INFO_ACK.
12989  * The logic is as follows.
12990  *
12991  * become exclusive
12992  * set CHANGING flag
12993  * change mtu on affected IREs
12994  * clear CHANGING flag
12995  *
12996  * An ire add that occurs before the CHANGING flag is set will have its mtu
12997  * changed by the ip_sioctl_lnkinfo.
12998  *
12999  * During the time the CHANGING flag is set, no new ires will be added to the
13000  * bucket, and ire add will fail (due the CHANGING flag).
13001  *
13002  * An ire add that occurs after the CHANGING flag is set will have the right mtu
13003  * before it is added to the bucket.
13004  *
13005  * Obviously only 1 thread can set the CHANGING flag and we need to become
13006  * exclusive to set the flag.
13007  */
13008 /* ARGSUSED */
13009 int
13010 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13011     ip_ioctl_cmd_t *ipi, void *if_req)
13012 {
13013 	ill_t		*ill = ipif->ipif_ill;
13014 	ipif_t		*nipif;
13015 	int		ip_min_mtu;
13016 	boolean_t	mtu_walk = B_FALSE;
13017 	struct lifreq	*lifr = (struct lifreq *)if_req;
13018 	lif_ifinfo_req_t *lir;
13019 	ire_t		*ire;
13020 
13021 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
13022 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13023 	lir = &lifr->lifr_ifinfo;
13024 	ASSERT(IAM_WRITER_IPIF(ipif));
13025 
13026 	/* Only allow for logical unit zero i.e. not on "le0:17" */
13027 	if (ipif->ipif_id != 0)
13028 		return (EINVAL);
13029 
13030 	/* Set interface MTU. */
13031 	if (ipif->ipif_isv6)
13032 		ip_min_mtu = IPV6_MIN_MTU;
13033 	else
13034 		ip_min_mtu = IP_MIN_MTU;
13035 
13036 	/*
13037 	 * Verify values before we set anything. Allow zero to
13038 	 * mean unspecified.
13039 	 */
13040 	if (lir->lir_maxmtu != 0 &&
13041 	    (lir->lir_maxmtu > ill->ill_max_frag ||
13042 	    lir->lir_maxmtu < ip_min_mtu))
13043 		return (EINVAL);
13044 	if (lir->lir_reachtime != 0 &&
13045 	    lir->lir_reachtime > ND_MAX_REACHTIME)
13046 		return (EINVAL);
13047 	if (lir->lir_reachretrans != 0 &&
13048 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
13049 		return (EINVAL);
13050 
13051 	mutex_enter(&ill->ill_lock);
13052 	ill->ill_state_flags |= ILL_CHANGING;
13053 	for (nipif = ill->ill_ipif; nipif != NULL;
13054 	    nipif = nipif->ipif_next) {
13055 		nipif->ipif_state_flags |= IPIF_CHANGING;
13056 	}
13057 
13058 	mutex_exit(&ill->ill_lock);
13059 
13060 	if (lir->lir_maxmtu != 0) {
13061 		ill->ill_max_mtu = lir->lir_maxmtu;
13062 		ill->ill_mtu_userspecified = 1;
13063 		mtu_walk = B_TRUE;
13064 	}
13065 
13066 	if (lir->lir_reachtime != 0)
13067 		ill->ill_reachable_time = lir->lir_reachtime;
13068 
13069 	if (lir->lir_reachretrans != 0)
13070 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
13071 
13072 	ill->ill_max_hops = lir->lir_maxhops;
13073 
13074 	ill->ill_max_buf = ND_MAX_Q;
13075 
13076 	if (mtu_walk) {
13077 		/*
13078 		 * Set the MTU on all ipifs associated with this ill except
13079 		 * for those whose MTU was fixed via SIOCSLIFMTU.
13080 		 */
13081 		for (nipif = ill->ill_ipif; nipif != NULL;
13082 		    nipif = nipif->ipif_next) {
13083 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
13084 				continue;
13085 
13086 			nipif->ipif_mtu = ill->ill_max_mtu;
13087 
13088 			if (!(nipif->ipif_flags & IPIF_UP))
13089 				continue;
13090 
13091 			if (nipif->ipif_isv6)
13092 				ire = ipif_to_ire_v6(nipif);
13093 			else
13094 				ire = ipif_to_ire(nipif);
13095 			if (ire != NULL) {
13096 				ire->ire_max_frag = ipif->ipif_mtu;
13097 				ire_refrele(ire);
13098 			}
13099 			if (ill->ill_isv6) {
13100 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
13101 				    ipif_mtu_change, (char *)nipif,
13102 				    ill);
13103 			} else {
13104 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
13105 				    ipif_mtu_change, (char *)nipif,
13106 				    ill);
13107 			}
13108 		}
13109 	}
13110 
13111 	mutex_enter(&ill->ill_lock);
13112 	for (nipif = ill->ill_ipif; nipif != NULL;
13113 	    nipif = nipif->ipif_next) {
13114 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
13115 	}
13116 	ILL_UNMARK_CHANGING(ill);
13117 	mutex_exit(&ill->ill_lock);
13118 
13119 	return (0);
13120 }
13121 
13122 /* ARGSUSED */
13123 int
13124 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13125     ip_ioctl_cmd_t *ipi, void *if_req)
13126 {
13127 	struct lif_ifinfo_req *lir;
13128 	ill_t *ill = ipif->ipif_ill;
13129 
13130 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13131 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13132 	if (ipif->ipif_id != 0)
13133 		return (EINVAL);
13134 
13135 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13136 	lir->lir_maxhops = ill->ill_max_hops;
13137 	lir->lir_reachtime = ill->ill_reachable_time;
13138 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13139 	lir->lir_maxmtu = ill->ill_max_mtu;
13140 
13141 	return (0);
13142 }
13143 
13144 /*
13145  * Return best guess as to the subnet mask for the specified address.
13146  * Based on the subnet masks for all the configured interfaces.
13147  *
13148  * We end up returning a zero mask in the case of default, multicast or
13149  * experimental.
13150  */
13151 static ipaddr_t
13152 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp)
13153 {
13154 	ipaddr_t net_mask;
13155 	ill_t	*ill;
13156 	ipif_t	*ipif;
13157 	ill_walk_context_t ctx;
13158 	ipif_t	*fallback_ipif = NULL;
13159 
13160 	net_mask = ip_net_mask(addr);
13161 	if (net_mask == 0) {
13162 		*ipifp = NULL;
13163 		return (0);
13164 	}
13165 
13166 	/* Let's check to see if this is maybe a local subnet route. */
13167 	/* this function only applies to IPv4 interfaces */
13168 	rw_enter(&ill_g_lock, RW_READER);
13169 	ill = ILL_START_WALK_V4(&ctx);
13170 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13171 		mutex_enter(&ill->ill_lock);
13172 		for (ipif = ill->ill_ipif; ipif != NULL;
13173 		    ipif = ipif->ipif_next) {
13174 			if (!IPIF_CAN_LOOKUP(ipif))
13175 				continue;
13176 			if (!(ipif->ipif_flags & IPIF_UP))
13177 				continue;
13178 			if ((ipif->ipif_subnet & net_mask) ==
13179 			    (addr & net_mask)) {
13180 				/*
13181 				 * Don't trust pt-pt interfaces if there are
13182 				 * other interfaces.
13183 				 */
13184 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13185 					if (fallback_ipif == NULL) {
13186 						ipif_refhold_locked(ipif);
13187 						fallback_ipif = ipif;
13188 					}
13189 					continue;
13190 				}
13191 
13192 				/*
13193 				 * Fine. Just assume the same net mask as the
13194 				 * directly attached subnet interface is using.
13195 				 */
13196 				ipif_refhold_locked(ipif);
13197 				mutex_exit(&ill->ill_lock);
13198 				rw_exit(&ill_g_lock);
13199 				if (fallback_ipif != NULL)
13200 					ipif_refrele(fallback_ipif);
13201 				*ipifp = ipif;
13202 				return (ipif->ipif_net_mask);
13203 			}
13204 		}
13205 		mutex_exit(&ill->ill_lock);
13206 	}
13207 	rw_exit(&ill_g_lock);
13208 
13209 	*ipifp = fallback_ipif;
13210 	return ((fallback_ipif != NULL) ?
13211 	    fallback_ipif->ipif_net_mask : net_mask);
13212 }
13213 
13214 /*
13215  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13216  */
13217 static void
13218 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13219 {
13220 	IOCP	iocp;
13221 	ipft_t	*ipft;
13222 	ipllc_t	*ipllc;
13223 	mblk_t	*mp1;
13224 	cred_t	*cr;
13225 	int	error = 0;
13226 	conn_t	*connp;
13227 
13228 	ip1dbg(("ip_wput_ioctl"));
13229 	iocp = (IOCP)mp->b_rptr;
13230 	mp1 = mp->b_cont;
13231 	if (mp1 == NULL) {
13232 		iocp->ioc_error = EINVAL;
13233 		mp->b_datap->db_type = M_IOCNAK;
13234 		iocp->ioc_count = 0;
13235 		qreply(q, mp);
13236 		return;
13237 	}
13238 
13239 	/*
13240 	 * These IOCTLs provide various control capabilities to
13241 	 * upstream agents such as ULPs and processes.	There
13242 	 * are currently two such IOCTLs implemented.  They
13243 	 * are used by TCP to provide update information for
13244 	 * existing IREs and to forcibly delete an IRE for a
13245 	 * host that is not responding, thereby forcing an
13246 	 * attempt at a new route.
13247 	 */
13248 	iocp->ioc_error = EINVAL;
13249 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13250 		goto done;
13251 
13252 	ipllc = (ipllc_t *)mp1->b_rptr;
13253 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13254 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13255 			break;
13256 	}
13257 	/*
13258 	 * prefer credential from mblk over ioctl;
13259 	 * see ip_sioctl_copyin_setup
13260 	 */
13261 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13262 
13263 	/*
13264 	 * Refhold the conn in case the request gets queued up in some lookup
13265 	 */
13266 	ASSERT(CONN_Q(q));
13267 	connp = Q_TO_CONN(q);
13268 	CONN_INC_REF(connp);
13269 	if (ipft->ipft_pfi &&
13270 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13271 		pullupmsg(mp1, ipft->ipft_min_size))) {
13272 		error = (*ipft->ipft_pfi)(q,
13273 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13274 	}
13275 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13276 		/*
13277 		 * CONN_OPER_PENDING_DONE happens in the function called
13278 		 * through ipft_pfi above.
13279 		 */
13280 		return;
13281 	}
13282 
13283 	CONN_OPER_PENDING_DONE(connp);
13284 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13285 		freemsg(mp);
13286 		return;
13287 	}
13288 	iocp->ioc_error = error;
13289 
13290 done:
13291 	mp->b_datap->db_type = M_IOCACK;
13292 	if (iocp->ioc_error)
13293 		iocp->ioc_count = 0;
13294 	qreply(q, mp);
13295 }
13296 
13297 /*
13298  * Lookup an ipif using the sequence id (ipif_seqid)
13299  */
13300 ipif_t *
13301 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13302 {
13303 	ipif_t *ipif;
13304 
13305 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13306 
13307 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13308 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13309 			return (ipif);
13310 	}
13311 	return (NULL);
13312 }
13313 
13314 uint64_t ipif_g_seqid;
13315 
13316 /*
13317  * Assign a unique id for the ipif. This is used later when we send
13318  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13319  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13320  * IRE is added, we verify that ipif has not disappeared.
13321  */
13322 
13323 static void
13324 ipif_assign_seqid(ipif_t *ipif)
13325 {
13326 	ipif->ipif_seqid = atomic_add_64_nv(&ipif_g_seqid, 1);
13327 }
13328 
13329 /*
13330  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13331  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13332  * be inserted into the first space available in the list. The value of
13333  * ipif_id will then be set to the appropriate value for its position.
13334  */
13335 static int
13336 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13337 {
13338 	ill_t *ill;
13339 	ipif_t *tipif;
13340 	ipif_t **tipifp;
13341 	int id;
13342 
13343 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13344 	    IAM_WRITER_IPIF(ipif));
13345 
13346 	ill = ipif->ipif_ill;
13347 	ASSERT(ill != NULL);
13348 
13349 	/*
13350 	 * In the case of lo0:0 we already hold the ill_g_lock.
13351 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13352 	 * ipif_insert. Another such caller is ipif_move.
13353 	 */
13354 	if (acquire_g_lock)
13355 		rw_enter(&ill_g_lock, RW_WRITER);
13356 	if (acquire_ill_lock)
13357 		mutex_enter(&ill->ill_lock);
13358 	id = ipif->ipif_id;
13359 	tipifp = &(ill->ill_ipif);
13360 	if (id == -1) {	/* need to find a real id */
13361 		id = 0;
13362 		while ((tipif = *tipifp) != NULL) {
13363 			ASSERT(tipif->ipif_id >= id);
13364 			if (tipif->ipif_id != id)
13365 				break; /* non-consecutive id */
13366 			id++;
13367 			tipifp = &(tipif->ipif_next);
13368 		}
13369 		/* limit number of logical interfaces */
13370 		if (id >= ip_addrs_per_if) {
13371 			if (acquire_ill_lock)
13372 				mutex_exit(&ill->ill_lock);
13373 			if (acquire_g_lock)
13374 				rw_exit(&ill_g_lock);
13375 			return (-1);
13376 		}
13377 		ipif->ipif_id = id; /* assign new id */
13378 	} else if (id < ip_addrs_per_if) {
13379 		/* we have a real id; insert ipif in the right place */
13380 		while ((tipif = *tipifp) != NULL) {
13381 			ASSERT(tipif->ipif_id != id);
13382 			if (tipif->ipif_id > id)
13383 				break; /* found correct location */
13384 			tipifp = &(tipif->ipif_next);
13385 		}
13386 	} else {
13387 		if (acquire_ill_lock)
13388 			mutex_exit(&ill->ill_lock);
13389 		if (acquire_g_lock)
13390 			rw_exit(&ill_g_lock);
13391 		return (-1);
13392 	}
13393 
13394 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13395 
13396 	ipif->ipif_next = tipif;
13397 	*tipifp = ipif;
13398 	if (acquire_ill_lock)
13399 		mutex_exit(&ill->ill_lock);
13400 	if (acquire_g_lock)
13401 		rw_exit(&ill_g_lock);
13402 	return (0);
13403 }
13404 
13405 /*
13406  * Allocate and initialize a new interface control structure.  (Always
13407  * called as writer.)
13408  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13409  * is not part of the global linked list of ills. ipif_seqid is unique
13410  * in the system and to preserve the uniqueness, it is assigned only
13411  * when ill becomes part of the global list. At that point ill will
13412  * have a name. If it doesn't get assigned here, it will get assigned
13413  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13414  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13415  * the interface flags or any other information from the DL_INFO_ACK for
13416  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13417  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13418  * second DL_INFO_ACK comes in from the driver.
13419  */
13420 static ipif_t *
13421 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13422 {
13423 	ipif_t	*ipif;
13424 	phyint_t *phyi;
13425 
13426 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13427 	    ill->ill_name, id, (void *)ill));
13428 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13429 
13430 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13431 		return (NULL);
13432 	*ipif = ipif_zero;	/* start clean */
13433 
13434 	ipif->ipif_ill = ill;
13435 	ipif->ipif_id = id;	/* could be -1 */
13436 	ipif->ipif_zoneid = GLOBAL_ZONEID;
13437 
13438 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13439 
13440 	ipif->ipif_refcnt = 0;
13441 	ipif->ipif_saved_ire_cnt = 0;
13442 
13443 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13444 		mi_free(ipif);
13445 		return (NULL);
13446 	}
13447 	/* -1 id should have been replaced by real id */
13448 	id = ipif->ipif_id;
13449 	ASSERT(id >= 0);
13450 
13451 	if (ill->ill_name[0] != '\0') {
13452 		ipif_assign_seqid(ipif);
13453 		if (ill->ill_phyint->phyint_ifindex != 0)
13454 			sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
13455 	}
13456 	/*
13457 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13458 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13459 	 * ioctl sets ipif_orig_ipifid to zero.
13460 	 */
13461 	ipif->ipif_orig_ipifid = id;
13462 
13463 	/*
13464 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13465 	 * The ipif is still not up and can't be looked up until the
13466 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13467 	 */
13468 	mutex_enter(&ill->ill_lock);
13469 	mutex_enter(&ill->ill_phyint->phyint_lock);
13470 	/*
13471 	 * Set the running flag when logical interface zero is created.
13472 	 * For subsequent logical interfaces, a DLPI link down
13473 	 * notification message may have cleared the running flag to
13474 	 * indicate the link is down, so we shouldn't just blindly set it.
13475 	 */
13476 	if (id == 0)
13477 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13478 	ipif->ipif_ire_type = ire_type;
13479 	phyi = ill->ill_phyint;
13480 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13481 
13482 	if (ipif->ipif_isv6) {
13483 		ill->ill_flags |= ILLF_IPV6;
13484 	} else {
13485 		ipaddr_t inaddr_any = INADDR_ANY;
13486 
13487 		ill->ill_flags |= ILLF_IPV4;
13488 
13489 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13490 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13491 		    &ipif->ipif_v6lcl_addr);
13492 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13493 		    &ipif->ipif_v6src_addr);
13494 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13495 		    &ipif->ipif_v6subnet);
13496 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13497 		    &ipif->ipif_v6net_mask);
13498 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13499 		    &ipif->ipif_v6brd_addr);
13500 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13501 		    &ipif->ipif_v6pp_dst_addr);
13502 	}
13503 
13504 	/*
13505 	 * Don't set the interface flags etc. now, will do it in
13506 	 * ip_ll_subnet_defaults.
13507 	 */
13508 	if (!initialize) {
13509 		mutex_exit(&ill->ill_lock);
13510 		mutex_exit(&ill->ill_phyint->phyint_lock);
13511 		return (ipif);
13512 	}
13513 	ipif->ipif_mtu = ill->ill_max_mtu;
13514 
13515 	if (ill->ill_bcast_addr_length != 0) {
13516 		/*
13517 		 * Later detect lack of DLPI driver multicast
13518 		 * capability by catching DL_ENABMULTI errors in
13519 		 * ip_rput_dlpi.
13520 		 */
13521 		ill->ill_flags |= ILLF_MULTICAST;
13522 		if (!ipif->ipif_isv6)
13523 			ipif->ipif_flags |= IPIF_BROADCAST;
13524 	} else {
13525 		if (ill->ill_net_type != IRE_LOOPBACK) {
13526 			if (ipif->ipif_isv6)
13527 				/*
13528 				 * Note: xresolv interfaces will eventually need
13529 				 * NOARP set here as well, but that will require
13530 				 * those external resolvers to have some
13531 				 * knowledge of that flag and act appropriately.
13532 				 * Not to be changed at present.
13533 				 */
13534 				ill->ill_flags |= ILLF_NONUD;
13535 			else
13536 				ill->ill_flags |= ILLF_NOARP;
13537 		}
13538 		if (ill->ill_phys_addr_length == 0) {
13539 			if (ill->ill_media &&
13540 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13541 				ipif->ipif_flags |= IPIF_NOXMIT;
13542 				phyi->phyint_flags |= PHYI_VIRTUAL;
13543 			} else {
13544 				/* pt-pt supports multicast. */
13545 				ill->ill_flags |= ILLF_MULTICAST;
13546 				if (ill->ill_net_type == IRE_LOOPBACK) {
13547 					phyi->phyint_flags |=
13548 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13549 				} else {
13550 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13551 				}
13552 			}
13553 		}
13554 	}
13555 	mutex_exit(&ill->ill_lock);
13556 	mutex_exit(&ill->ill_phyint->phyint_lock);
13557 	return (ipif);
13558 }
13559 
13560 /*
13561  * If appropriate, send a message up to the resolver delete the entry
13562  * for the address of this interface which is going out of business.
13563  * (Always called as writer).
13564  *
13565  * NOTE : We need to check for NULL mps as some of the fields are
13566  *	  initialized only for some interface types. See ipif_resolver_up()
13567  *	  for details.
13568  */
13569 void
13570 ipif_arp_down(ipif_t *ipif)
13571 {
13572 	mblk_t	*mp;
13573 	ill_t	*ill = ipif->ipif_ill;
13574 
13575 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13576 	ASSERT(IAM_WRITER_IPIF(ipif));
13577 
13578 	/* Delete the mapping for the local address */
13579 	mp = ipif->ipif_arp_del_mp;
13580 	if (mp != NULL) {
13581 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13582 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13583 		putnext(ill->ill_rq, mp);
13584 		ipif->ipif_arp_del_mp = NULL;
13585 	}
13586 
13587 	/*
13588 	 * If this is the last ipif that is going down and there are no
13589 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13590 	 * clean up ARP completely.
13591 	 */
13592 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13593 
13594 		/* Send up AR_INTERFACE_DOWN message */
13595 		mp = ill->ill_arp_down_mp;
13596 		if (mp != NULL) {
13597 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13598 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13599 			    ipif->ipif_id));
13600 			putnext(ill->ill_rq, mp);
13601 			ill->ill_arp_down_mp = NULL;
13602 		}
13603 
13604 		/* Tell ARP to delete the multicast mappings */
13605 		mp = ill->ill_arp_del_mapping_mp;
13606 		if (mp != NULL) {
13607 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13608 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13609 			    ipif->ipif_id));
13610 			putnext(ill->ill_rq, mp);
13611 			ill->ill_arp_del_mapping_mp = NULL;
13612 		}
13613 	}
13614 }
13615 
13616 /*
13617  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13618  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13619  * that it wants the add_mp allocated in this function to be returned
13620  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13621  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13622  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13623  * as it does a ipif_arp_down after calling this function - which will
13624  * remove what we add here.
13625  *
13626  * Returns -1 on failures and 0 on success.
13627  */
13628 int
13629 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13630 {
13631 	mblk_t	*del_mp = NULL;
13632 	mblk_t *add_mp = NULL;
13633 	mblk_t *mp;
13634 	ill_t	*ill = ipif->ipif_ill;
13635 	phyint_t *phyi = ill->ill_phyint;
13636 	ipaddr_t addr, mask, extract_mask = 0;
13637 	arma_t	*arma;
13638 	uint8_t *maddr, *bphys_addr;
13639 	uint32_t hw_start;
13640 	dl_unitdata_req_t *dlur;
13641 
13642 	ASSERT(IAM_WRITER_IPIF(ipif));
13643 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13644 		return (0);
13645 
13646 	/*
13647 	 * Delete the existing mapping from ARP. Normally ipif_down
13648 	 * -> ipif_arp_down should send this up to ARP. The only
13649 	 * reason we would find this when we are switching from
13650 	 * Multicast to Broadcast where we did not do a down.
13651 	 */
13652 	mp = ill->ill_arp_del_mapping_mp;
13653 	if (mp != NULL) {
13654 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13655 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13656 		putnext(ill->ill_rq, mp);
13657 		ill->ill_arp_del_mapping_mp = NULL;
13658 	}
13659 
13660 	if (arp_add_mapping_mp != NULL)
13661 		*arp_add_mapping_mp = NULL;
13662 
13663 	/*
13664 	 * Check that the address is not to long for the constant
13665 	 * length reserved in the template arma_t.
13666 	 */
13667 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13668 		return (-1);
13669 
13670 	/* Add mapping mblk */
13671 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13672 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13673 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13674 	    (caddr_t)&addr);
13675 	if (add_mp == NULL)
13676 		return (-1);
13677 	arma = (arma_t *)add_mp->b_rptr;
13678 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13679 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13680 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13681 
13682 	/*
13683 	 * Determine the broadcast address.
13684 	 */
13685 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13686 	if (ill->ill_sap_length < 0)
13687 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13688 	else
13689 		bphys_addr = (uchar_t *)dlur +
13690 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13691 	/*
13692 	 * Check PHYI_MULTI_BCAST and length of physical
13693 	 * address to determine if we use the mapping or the
13694 	 * broadcast address.
13695 	 */
13696 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13697 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13698 		    bphys_addr, maddr, &hw_start, &extract_mask))
13699 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13700 
13701 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13702 	    (ill->ill_flags & ILLF_MULTICAST)) {
13703 		/* Make sure this will not match the "exact" entry. */
13704 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13705 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13706 		    (caddr_t)&addr);
13707 		if (del_mp == NULL) {
13708 			freemsg(add_mp);
13709 			return (-1);
13710 		}
13711 		bcopy(&extract_mask, (char *)arma +
13712 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13713 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13714 			/* Use link-layer broadcast address for MULTI_BCAST */
13715 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13716 			ip2dbg(("ipif_arp_setup_multicast: adding"
13717 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13718 		} else {
13719 			arma->arma_hw_mapping_start = hw_start;
13720 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13721 			    " ARP setup for %s\n", ill->ill_name));
13722 		}
13723 	} else {
13724 		freemsg(add_mp);
13725 		ASSERT(del_mp == NULL);
13726 		/* It is neither MULTICAST nor MULTI_BCAST */
13727 		return (0);
13728 	}
13729 	ASSERT(add_mp != NULL && del_mp != NULL);
13730 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13731 	ill->ill_arp_del_mapping_mp = del_mp;
13732 	if (arp_add_mapping_mp != NULL) {
13733 		/* The caller just wants the mblks allocated */
13734 		*arp_add_mapping_mp = add_mp;
13735 	} else {
13736 		/* The caller wants us to send it to arp */
13737 		putnext(ill->ill_rq, add_mp);
13738 	}
13739 	return (0);
13740 }
13741 
13742 /*
13743  * Get the resolver set up for a new interface address.
13744  * (Always called as writer.)
13745  * Called both for IPv4 and IPv6 interfaces,
13746  * though it only sets up the resolver for v6
13747  * if it's an xresolv interface (one using an external resolver).
13748  * Honors ILLF_NOARP.
13749  * The enumerated value res_act is used to tune the behavior.
13750  * If set to Res_act_initial, then we set up all the resolver
13751  * structures for a new interface.  If set to Res_act_move, then
13752  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13753  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13754  * asynchronous hardware address change notification.  If set to
13755  * Res_act_defend, then we tell ARP that it needs to send a single
13756  * gratuitous message in defense of the address.
13757  * Returns error on failure.
13758  */
13759 int
13760 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13761 {
13762 	caddr_t	addr;
13763 	mblk_t	*arp_up_mp = NULL;
13764 	mblk_t	*arp_down_mp = NULL;
13765 	mblk_t	*arp_add_mp = NULL;
13766 	mblk_t	*arp_del_mp = NULL;
13767 	mblk_t	*arp_add_mapping_mp = NULL;
13768 	mblk_t	*arp_del_mapping_mp = NULL;
13769 	ill_t	*ill = ipif->ipif_ill;
13770 	uchar_t	*area_p = NULL;
13771 	uchar_t	*ared_p = NULL;
13772 	int	err = ENOMEM;
13773 	boolean_t was_dup;
13774 
13775 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13776 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13777 	ASSERT(IAM_WRITER_IPIF(ipif));
13778 
13779 	was_dup = B_FALSE;
13780 	if (res_act == Res_act_initial) {
13781 		ipif->ipif_addr_ready = 0;
13782 		/*
13783 		 * We're bringing an interface up here.  There's no way that we
13784 		 * should need to shut down ARP now.
13785 		 */
13786 		mutex_enter(&ill->ill_lock);
13787 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13788 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13789 			ill->ill_ipif_dup_count--;
13790 			was_dup = B_TRUE;
13791 		}
13792 		mutex_exit(&ill->ill_lock);
13793 	}
13794 	if (ipif->ipif_recovery_id != 0)
13795 		(void) untimeout(ipif->ipif_recovery_id);
13796 	ipif->ipif_recovery_id = 0;
13797 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13798 		ipif->ipif_addr_ready = 1;
13799 		return (0);
13800 	}
13801 	/* NDP will set the ipif_addr_ready flag when it's ready */
13802 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13803 		return (0);
13804 
13805 	if (ill->ill_isv6) {
13806 		/*
13807 		 * External resolver for IPv6
13808 		 */
13809 		ASSERT(res_act == Res_act_initial);
13810 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13811 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13812 			area_p = (uchar_t *)&ip6_area_template;
13813 			ared_p = (uchar_t *)&ip6_ared_template;
13814 		}
13815 	} else {
13816 		/*
13817 		 * IPv4 arp case. If the ARP stream has already started
13818 		 * closing, fail this request for ARP bringup. Else
13819 		 * record the fact that an ARP bringup is pending.
13820 		 */
13821 		mutex_enter(&ill->ill_lock);
13822 		if (ill->ill_arp_closing) {
13823 			mutex_exit(&ill->ill_lock);
13824 			err = EINVAL;
13825 			goto failed;
13826 		} else {
13827 			if (ill->ill_ipif_up_count == 0 &&
13828 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13829 				ill->ill_arp_bringup_pending = 1;
13830 			mutex_exit(&ill->ill_lock);
13831 		}
13832 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13833 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13834 			area_p = (uchar_t *)&ip_area_template;
13835 			ared_p = (uchar_t *)&ip_ared_template;
13836 		}
13837 	}
13838 
13839 	/*
13840 	 * Add an entry for the local address in ARP only if it
13841 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13842 	 */
13843 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13844 		area_t *area;
13845 
13846 		/* Now ask ARP to publish our address. */
13847 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13848 		if (arp_add_mp == NULL)
13849 			goto failed;
13850 		area = (area_t *)arp_add_mp->b_rptr;
13851 		if (res_act != Res_act_initial) {
13852 			/*
13853 			 * Copy the new hardware address and length into
13854 			 * arp_add_mp to be sent to ARP.
13855 			 */
13856 			area->area_hw_addr_length =
13857 			    ill->ill_phys_addr_length;
13858 			bcopy((char *)ill->ill_phys_addr,
13859 			    ((char *)area + area->area_hw_addr_offset),
13860 			    area->area_hw_addr_length);
13861 		}
13862 
13863 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13864 		    ACE_F_MYADDR;
13865 
13866 		if (res_act == Res_act_defend) {
13867 			area->area_flags |= ACE_F_DEFEND;
13868 			/*
13869 			 * If we're just defending our address now, then
13870 			 * there's no need to set up ARP multicast mappings.
13871 			 * The publish command is enough.
13872 			 */
13873 			goto done;
13874 		}
13875 
13876 		if (res_act != Res_act_initial)
13877 			goto arp_setup_multicast;
13878 
13879 		/*
13880 		 * Allocate an ARP deletion message so we know we can tell ARP
13881 		 * when the interface goes down.
13882 		 */
13883 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13884 		if (arp_del_mp == NULL)
13885 			goto failed;
13886 
13887 	} else {
13888 		if (res_act != Res_act_initial)
13889 			goto done;
13890 	}
13891 	/*
13892 	 * Need to bring up ARP or setup multicast mapping only
13893 	 * when the first interface is coming UP.
13894 	 */
13895 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13896 	    was_dup) {
13897 		goto done;
13898 	}
13899 
13900 	/*
13901 	 * Allocate an ARP down message (to be saved) and an ARP up
13902 	 * message.
13903 	 */
13904 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13905 	if (arp_down_mp == NULL)
13906 		goto failed;
13907 
13908 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13909 	if (arp_up_mp == NULL)
13910 		goto failed;
13911 
13912 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13913 		goto done;
13914 
13915 arp_setup_multicast:
13916 	/*
13917 	 * Setup the multicast mappings. This function initializes
13918 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13919 	 * IPv6.
13920 	 */
13921 	if (!ill->ill_isv6) {
13922 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13923 		if (err != 0)
13924 			goto failed;
13925 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13926 		ASSERT(arp_add_mapping_mp != NULL);
13927 	}
13928 
13929 done:
13930 	if (arp_del_mp != NULL) {
13931 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13932 		ipif->ipif_arp_del_mp = arp_del_mp;
13933 	}
13934 	if (arp_down_mp != NULL) {
13935 		ASSERT(ill->ill_arp_down_mp == NULL);
13936 		ill->ill_arp_down_mp = arp_down_mp;
13937 	}
13938 	if (arp_del_mapping_mp != NULL) {
13939 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13940 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13941 	}
13942 	if (arp_up_mp != NULL) {
13943 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13944 		    ill->ill_name, ipif->ipif_id));
13945 		putnext(ill->ill_rq, arp_up_mp);
13946 	}
13947 	if (arp_add_mp != NULL) {
13948 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13949 		    ill->ill_name, ipif->ipif_id));
13950 		/*
13951 		 * If it's an extended ARP implementation, then we'll wait to
13952 		 * hear that DAD has finished before using the interface.
13953 		 */
13954 		if (!ill->ill_arp_extend)
13955 			ipif->ipif_addr_ready = 1;
13956 		putnext(ill->ill_rq, arp_add_mp);
13957 	} else {
13958 		ipif->ipif_addr_ready = 1;
13959 	}
13960 	if (arp_add_mapping_mp != NULL) {
13961 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13962 		    ill->ill_name, ipif->ipif_id));
13963 		putnext(ill->ill_rq, arp_add_mapping_mp);
13964 	}
13965 	if (res_act != Res_act_initial)
13966 		return (0);
13967 
13968 	if (ill->ill_flags & ILLF_NOARP)
13969 		err = ill_arp_off(ill);
13970 	else
13971 		err = ill_arp_on(ill);
13972 	if (err != 0) {
13973 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13974 		freemsg(ipif->ipif_arp_del_mp);
13975 		freemsg(ill->ill_arp_down_mp);
13976 		freemsg(ill->ill_arp_del_mapping_mp);
13977 		ipif->ipif_arp_del_mp = NULL;
13978 		ill->ill_arp_down_mp = NULL;
13979 		ill->ill_arp_del_mapping_mp = NULL;
13980 		return (err);
13981 	}
13982 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13983 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13984 
13985 failed:
13986 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13987 	freemsg(arp_add_mp);
13988 	freemsg(arp_del_mp);
13989 	freemsg(arp_add_mapping_mp);
13990 	freemsg(arp_up_mp);
13991 	freemsg(arp_down_mp);
13992 	ill->ill_arp_bringup_pending = 0;
13993 	return (err);
13994 }
13995 
13996 /*
13997  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13998  * just gone back up.
13999  */
14000 static void
14001 ipif_arp_start_dad(ipif_t *ipif)
14002 {
14003 	ill_t *ill = ipif->ipif_ill;
14004 	mblk_t *arp_add_mp;
14005 	area_t *area;
14006 
14007 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
14008 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
14009 	    ipif->ipif_lcl_addr == INADDR_ANY ||
14010 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
14011 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
14012 		/*
14013 		 * If we can't contact ARP for some reason, that's not really a
14014 		 * problem.  Just send out the routing socket notification that
14015 		 * DAD completion would have done, and continue.
14016 		 */
14017 		ipif_mask_reply(ipif);
14018 		ip_rts_ifmsg(ipif);
14019 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14020 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14021 		ipif->ipif_addr_ready = 1;
14022 		return;
14023 	}
14024 
14025 	/* Setting the 'unverified' flag restarts DAD */
14026 	area = (area_t *)arp_add_mp->b_rptr;
14027 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
14028 	    ACE_F_UNVERIFIED;
14029 	putnext(ill->ill_rq, arp_add_mp);
14030 }
14031 
14032 static void
14033 ipif_ndp_start_dad(ipif_t *ipif)
14034 {
14035 	nce_t *nce;
14036 
14037 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
14038 	if (nce == NULL)
14039 		return;
14040 
14041 	if (!ndp_restart_dad(nce)) {
14042 		/*
14043 		 * If we can't restart DAD for some reason, that's not really a
14044 		 * problem.  Just send out the routing socket notification that
14045 		 * DAD completion would have done, and continue.
14046 		 */
14047 		ip_rts_ifmsg(ipif);
14048 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14049 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14050 		ipif->ipif_addr_ready = 1;
14051 	}
14052 	NCE_REFRELE(nce);
14053 }
14054 
14055 /*
14056  * Restart duplicate address detection on all interfaces on the given ill.
14057  *
14058  * This is called when an interface transitions from down to up
14059  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
14060  *
14061  * Note that since the underlying physical link has transitioned, we must cause
14062  * at least one routing socket message to be sent here, either via DAD
14063  * completion or just by default on the first ipif.  (If we don't do this, then
14064  * in.mpathd will see long delays when doing link-based failure recovery.)
14065  */
14066 void
14067 ill_restart_dad(ill_t *ill, boolean_t went_up)
14068 {
14069 	ipif_t *ipif;
14070 
14071 	if (ill == NULL)
14072 		return;
14073 
14074 	/*
14075 	 * If layer two doesn't support duplicate address detection, then just
14076 	 * send the routing socket message now and be done with it.
14077 	 */
14078 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
14079 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
14080 		ip_rts_ifmsg(ill->ill_ipif);
14081 		return;
14082 	}
14083 
14084 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14085 		if (went_up) {
14086 			if (ipif->ipif_flags & IPIF_UP) {
14087 				if (ill->ill_isv6)
14088 					ipif_ndp_start_dad(ipif);
14089 				else
14090 					ipif_arp_start_dad(ipif);
14091 			} else if (ill->ill_isv6 &&
14092 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14093 				/*
14094 				 * For IPv4, the ARP module itself will
14095 				 * automatically start the DAD process when it
14096 				 * sees DL_NOTE_LINK_UP.  We respond to the
14097 				 * AR_CN_READY at the completion of that task.
14098 				 * For IPv6, we must kick off the bring-up
14099 				 * process now.
14100 				 */
14101 				ndp_do_recovery(ipif);
14102 			} else {
14103 				/*
14104 				 * Unfortunately, the first ipif is "special"
14105 				 * and represents the underlying ill in the
14106 				 * routing socket messages.  Thus, when this
14107 				 * one ipif is down, we must still notify so
14108 				 * that the user knows the IFF_RUNNING status
14109 				 * change.  (If the first ipif is up, then
14110 				 * we'll handle eventual routing socket
14111 				 * notification via DAD completion.)
14112 				 */
14113 				if (ipif == ill->ill_ipif)
14114 					ip_rts_ifmsg(ill->ill_ipif);
14115 			}
14116 		} else {
14117 			/*
14118 			 * After link down, we'll need to send a new routing
14119 			 * message when the link comes back, so clear
14120 			 * ipif_addr_ready.
14121 			 */
14122 			ipif->ipif_addr_ready = 0;
14123 		}
14124 	}
14125 
14126 	/*
14127 	 * If we've torn down links, then notify the user right away.
14128 	 */
14129 	if (!went_up)
14130 		ip_rts_ifmsg(ill->ill_ipif);
14131 }
14132 
14133 /*
14134  * Wakeup all threads waiting to enter the ipsq, and sleeping
14135  * on any of the ills in this ipsq. The ill_lock of the ill
14136  * must be held so that waiters don't miss wakeups
14137  */
14138 static void
14139 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14140 {
14141 	phyint_t *phyint;
14142 
14143 	phyint = ipsq->ipsq_phyint_list;
14144 	while (phyint != NULL) {
14145 		if (phyint->phyint_illv4) {
14146 			if (!caller_holds_lock)
14147 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14148 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14149 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14150 			if (!caller_holds_lock)
14151 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14152 		}
14153 		if (phyint->phyint_illv6) {
14154 			if (!caller_holds_lock)
14155 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14156 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14157 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14158 			if (!caller_holds_lock)
14159 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14160 		}
14161 		phyint = phyint->phyint_ipsq_next;
14162 	}
14163 }
14164 
14165 static ipsq_t *
14166 ipsq_create(char *groupname)
14167 {
14168 	ipsq_t	*ipsq;
14169 
14170 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
14171 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14172 	if (ipsq == NULL) {
14173 		return (NULL);
14174 	}
14175 
14176 	if (groupname != NULL)
14177 		(void) strcpy(ipsq->ipsq_name, groupname);
14178 	else
14179 		ipsq->ipsq_name[0] = '\0';
14180 
14181 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14182 	ipsq->ipsq_flags |= IPSQ_GROUP;
14183 	ipsq->ipsq_next = ipsq_g_head;
14184 	ipsq_g_head = ipsq;
14185 	return (ipsq);
14186 }
14187 
14188 /*
14189  * Return an ipsq correspoding to the groupname. If 'create' is true
14190  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14191  * uniquely with an IPMP group. However during IPMP groupname operations,
14192  * multiple IPMP groups may be associated with a single ipsq. But no
14193  * IPMP group can be associated with more than 1 ipsq at any time.
14194  * For example
14195  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14196  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14197  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14198  *
14199  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14200  * status shown below during the execution of the above command.
14201  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14202  *
14203  * After the completion of the above groupname command we return to the stable
14204  * state shown below.
14205  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14206  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14207  *
14208  * Because of the above, we don't search based on the ipsq_name since that
14209  * would miss the correct ipsq during certain windows as shown above.
14210  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14211  * natural state.
14212  */
14213 static ipsq_t *
14214 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq)
14215 {
14216 	ipsq_t	*ipsq;
14217 	int	group_len;
14218 	phyint_t *phyint;
14219 
14220 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
14221 
14222 	group_len = strlen(groupname);
14223 	ASSERT(group_len != 0);
14224 	group_len++;
14225 
14226 	for (ipsq = ipsq_g_head; ipsq != NULL; ipsq = ipsq->ipsq_next) {
14227 		/*
14228 		 * When an ipsq is being split, and ill_split_ipsq
14229 		 * calls this function, we exclude it from being considered.
14230 		 */
14231 		if (ipsq == exclude_ipsq)
14232 			continue;
14233 
14234 		/*
14235 		 * Compare against the ipsq_name. The groupname change happens
14236 		 * in 2 phases. The 1st phase merges the from group into
14237 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14238 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14239 		 * ipsq_name. At this point the phyint_groupname has not been
14240 		 * updated.
14241 		 */
14242 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14243 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14244 			/*
14245 			 * Verify that an ipmp groupname is exactly
14246 			 * part of 1 ipsq and is not found in any other
14247 			 * ipsq.
14248 			 */
14249 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq) ==
14250 			    NULL);
14251 			return (ipsq);
14252 		}
14253 
14254 		/*
14255 		 * Comparison against ipsq_name alone is not sufficient.
14256 		 * In the case when groups are currently being
14257 		 * merged, the ipsq could hold other IPMP groups temporarily.
14258 		 * so we walk the phyint list and compare against the
14259 		 * phyint_groupname as well.
14260 		 */
14261 		phyint = ipsq->ipsq_phyint_list;
14262 		while (phyint != NULL) {
14263 			if ((group_len == phyint->phyint_groupname_len) &&
14264 			    (bcmp(phyint->phyint_groupname, groupname,
14265 			    group_len) == 0)) {
14266 				/*
14267 				 * Verify that an ipmp groupname is exactly
14268 				 * part of 1 ipsq and is not found in any other
14269 				 * ipsq.
14270 				 */
14271 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq)
14272 					== NULL);
14273 				return (ipsq);
14274 			}
14275 			phyint = phyint->phyint_ipsq_next;
14276 		}
14277 	}
14278 	if (create)
14279 		ipsq = ipsq_create(groupname);
14280 	return (ipsq);
14281 }
14282 
14283 static void
14284 ipsq_delete(ipsq_t *ipsq)
14285 {
14286 	ipsq_t *nipsq;
14287 	ipsq_t *pipsq = NULL;
14288 
14289 	/*
14290 	 * We don't hold the ipsq lock, but we are sure no new
14291 	 * messages can land up, since the ipsq_refs is zero.
14292 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14293 	 * is associated with this ipsq. (Lookups are based on ill_name
14294 	 * or phyint_group_name)
14295 	 */
14296 	ASSERT(ipsq->ipsq_refs == 0);
14297 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14298 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14299 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14300 		/*
14301 		 * This is not the ipsq of an IPMP group.
14302 		 */
14303 		kmem_free(ipsq, sizeof (ipsq_t));
14304 		return;
14305 	}
14306 
14307 	rw_enter(&ill_g_lock, RW_WRITER);
14308 
14309 	/*
14310 	 * Locate the ipsq  before we can remove it from
14311 	 * the singly linked list of ipsq's.
14312 	 */
14313 	for (nipsq = ipsq_g_head; nipsq != NULL; nipsq = nipsq->ipsq_next) {
14314 		if (nipsq == ipsq) {
14315 			break;
14316 		}
14317 		pipsq = nipsq;
14318 	}
14319 
14320 	ASSERT(nipsq == ipsq);
14321 
14322 	/* unlink ipsq from the list */
14323 	if (pipsq != NULL)
14324 		pipsq->ipsq_next = ipsq->ipsq_next;
14325 	else
14326 		ipsq_g_head = ipsq->ipsq_next;
14327 	kmem_free(ipsq, sizeof (ipsq_t));
14328 	rw_exit(&ill_g_lock);
14329 }
14330 
14331 static void
14332 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14333     queue_t *q)
14334 
14335 {
14336 
14337 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14338 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14339 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14340 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14341 	ASSERT(current_mp != NULL);
14342 
14343 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14344 		NEW_OP, NULL);
14345 
14346 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14347 	    new_ipsq->ipsq_xopq_mphead != NULL);
14348 
14349 	/*
14350 	 * move from old ipsq to the new ipsq.
14351 	 */
14352 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14353 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14354 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14355 
14356 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14357 }
14358 
14359 void
14360 ill_group_cleanup(ill_t *ill)
14361 {
14362 	ill_t *ill_v4;
14363 	ill_t *ill_v6;
14364 	ipif_t *ipif;
14365 
14366 	ill_v4 = ill->ill_phyint->phyint_illv4;
14367 	ill_v6 = ill->ill_phyint->phyint_illv6;
14368 
14369 	if (ill_v4 != NULL) {
14370 		mutex_enter(&ill_v4->ill_lock);
14371 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14372 		    ipif = ipif->ipif_next) {
14373 			IPIF_UNMARK_MOVING(ipif);
14374 		}
14375 		ill_v4->ill_up_ipifs = B_FALSE;
14376 		mutex_exit(&ill_v4->ill_lock);
14377 	}
14378 
14379 	if (ill_v6 != NULL) {
14380 		mutex_enter(&ill_v6->ill_lock);
14381 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14382 		    ipif = ipif->ipif_next) {
14383 			IPIF_UNMARK_MOVING(ipif);
14384 		}
14385 		ill_v6->ill_up_ipifs = B_FALSE;
14386 		mutex_exit(&ill_v6->ill_lock);
14387 	}
14388 }
14389 /*
14390  * This function is called when an ill has had a change in its group status
14391  * to bring up all the ipifs that were up before the change.
14392  */
14393 int
14394 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14395 {
14396 	ipif_t *ipif;
14397 	ill_t *ill_v4;
14398 	ill_t *ill_v6;
14399 	ill_t *from_ill;
14400 	int err = 0;
14401 
14402 
14403 	ASSERT(IAM_WRITER_ILL(ill));
14404 
14405 	/*
14406 	 * Except for ipif_state_flags and ill_state_flags the other
14407 	 * fields of the ipif/ill that are modified below are protected
14408 	 * implicitly since we are a writer. We would have tried to down
14409 	 * even an ipif that was already down, in ill_down_ipifs. So we
14410 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14411 	 */
14412 	ill_v4 = ill->ill_phyint->phyint_illv4;
14413 	ill_v6 = ill->ill_phyint->phyint_illv6;
14414 	if (ill_v4 != NULL) {
14415 		ill_v4->ill_up_ipifs = B_TRUE;
14416 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14417 		    ipif = ipif->ipif_next) {
14418 			mutex_enter(&ill_v4->ill_lock);
14419 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14420 			IPIF_UNMARK_MOVING(ipif);
14421 			mutex_exit(&ill_v4->ill_lock);
14422 			if (ipif->ipif_was_up) {
14423 				if (!(ipif->ipif_flags & IPIF_UP))
14424 					err = ipif_up(ipif, q, mp);
14425 				ipif->ipif_was_up = B_FALSE;
14426 				if (err != 0) {
14427 					/*
14428 					 * Can there be any other error ?
14429 					 */
14430 					ASSERT(err == EINPROGRESS);
14431 					return (err);
14432 				}
14433 			}
14434 		}
14435 		mutex_enter(&ill_v4->ill_lock);
14436 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14437 		mutex_exit(&ill_v4->ill_lock);
14438 		ill_v4->ill_up_ipifs = B_FALSE;
14439 		if (ill_v4->ill_move_in_progress) {
14440 			ASSERT(ill_v4->ill_move_peer != NULL);
14441 			ill_v4->ill_move_in_progress = B_FALSE;
14442 			from_ill = ill_v4->ill_move_peer;
14443 			from_ill->ill_move_in_progress = B_FALSE;
14444 			from_ill->ill_move_peer = NULL;
14445 			mutex_enter(&from_ill->ill_lock);
14446 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14447 			mutex_exit(&from_ill->ill_lock);
14448 			if (ill_v6 == NULL) {
14449 				if (from_ill->ill_phyint->phyint_flags &
14450 				    PHYI_STANDBY) {
14451 					phyint_inactive(from_ill->ill_phyint);
14452 				}
14453 				if (ill_v4->ill_phyint->phyint_flags &
14454 				    PHYI_STANDBY) {
14455 					phyint_inactive(ill_v4->ill_phyint);
14456 				}
14457 			}
14458 			ill_v4->ill_move_peer = NULL;
14459 		}
14460 	}
14461 
14462 	if (ill_v6 != NULL) {
14463 		ill_v6->ill_up_ipifs = B_TRUE;
14464 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14465 		    ipif = ipif->ipif_next) {
14466 			mutex_enter(&ill_v6->ill_lock);
14467 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14468 			IPIF_UNMARK_MOVING(ipif);
14469 			mutex_exit(&ill_v6->ill_lock);
14470 			if (ipif->ipif_was_up) {
14471 				if (!(ipif->ipif_flags & IPIF_UP))
14472 					err = ipif_up(ipif, q, mp);
14473 				ipif->ipif_was_up = B_FALSE;
14474 				if (err != 0) {
14475 					/*
14476 					 * Can there be any other error ?
14477 					 */
14478 					ASSERT(err == EINPROGRESS);
14479 					return (err);
14480 				}
14481 			}
14482 		}
14483 		mutex_enter(&ill_v6->ill_lock);
14484 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14485 		mutex_exit(&ill_v6->ill_lock);
14486 		ill_v6->ill_up_ipifs = B_FALSE;
14487 		if (ill_v6->ill_move_in_progress) {
14488 			ASSERT(ill_v6->ill_move_peer != NULL);
14489 			ill_v6->ill_move_in_progress = B_FALSE;
14490 			from_ill = ill_v6->ill_move_peer;
14491 			from_ill->ill_move_in_progress = B_FALSE;
14492 			from_ill->ill_move_peer = NULL;
14493 			mutex_enter(&from_ill->ill_lock);
14494 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14495 			mutex_exit(&from_ill->ill_lock);
14496 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14497 				phyint_inactive(from_ill->ill_phyint);
14498 			}
14499 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14500 				phyint_inactive(ill_v6->ill_phyint);
14501 			}
14502 			ill_v6->ill_move_peer = NULL;
14503 		}
14504 	}
14505 	return (0);
14506 }
14507 
14508 /*
14509  * bring down all the approriate ipifs.
14510  */
14511 /* ARGSUSED */
14512 static void
14513 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14514 {
14515 	ipif_t *ipif;
14516 
14517 	ASSERT(IAM_WRITER_ILL(ill));
14518 
14519 	/*
14520 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14521 	 * are modified below are protected implicitly since we are a writer
14522 	 */
14523 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14524 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14525 			continue;
14526 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14527 			/*
14528 			 * We go through the ipif_down logic even if the ipif
14529 			 * is already down, since routes can be added based
14530 			 * on down ipifs. Going through ipif_down once again
14531 			 * will delete any IREs created based on these routes.
14532 			 */
14533 			if (ipif->ipif_flags & IPIF_UP)
14534 				ipif->ipif_was_up = B_TRUE;
14535 			/*
14536 			 * If called with chk_nofailover true ipif is moving.
14537 			 */
14538 			mutex_enter(&ill->ill_lock);
14539 			if (chk_nofailover) {
14540 				ipif->ipif_state_flags |=
14541 					IPIF_MOVING | IPIF_CHANGING;
14542 			} else {
14543 				ipif->ipif_state_flags |= IPIF_CHANGING;
14544 			}
14545 			mutex_exit(&ill->ill_lock);
14546 			/*
14547 			 * Need to re-create net/subnet bcast ires if
14548 			 * they are dependent on ipif.
14549 			 */
14550 			if (!ipif->ipif_isv6)
14551 				ipif_check_bcast_ires(ipif);
14552 			(void) ipif_logical_down(ipif, NULL, NULL);
14553 			ipif_non_duplicate(ipif);
14554 			ipif_down_tail(ipif);
14555 			/*
14556 			 * We don't do ipif_multicast_down for IPv4 in
14557 			 * ipif_down. We need to set this so that
14558 			 * ipif_multicast_up will join the
14559 			 * ALLHOSTS_GROUP on to_ill.
14560 			 */
14561 			ipif->ipif_multicast_up = B_FALSE;
14562 		}
14563 	}
14564 }
14565 
14566 #define	IPSQ_INC_REF(ipsq)	{			\
14567 	ASSERT(RW_WRITE_HELD(&ill_g_lock));		\
14568 	(ipsq)->ipsq_refs++;				\
14569 }
14570 
14571 #define	IPSQ_DEC_REF(ipsq)	{			\
14572 	ASSERT(RW_WRITE_HELD(&ill_g_lock));		\
14573 	(ipsq)->ipsq_refs--;				\
14574 	if ((ipsq)->ipsq_refs == 0)				\
14575 		(ipsq)->ipsq_name[0] = '\0'; 		\
14576 }
14577 
14578 /*
14579  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14580  * new_ipsq.
14581  */
14582 static void
14583 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq)
14584 {
14585 	phyint_t *phyint;
14586 	phyint_t *next_phyint;
14587 
14588 	/*
14589 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14590 	 * writer and the ill_lock of the ill in question. Also the dest
14591 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14592 	 */
14593 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
14594 
14595 	phyint = cur_ipsq->ipsq_phyint_list;
14596 	cur_ipsq->ipsq_phyint_list = NULL;
14597 	while (phyint != NULL) {
14598 		next_phyint = phyint->phyint_ipsq_next;
14599 		IPSQ_DEC_REF(cur_ipsq);
14600 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14601 		new_ipsq->ipsq_phyint_list = phyint;
14602 		IPSQ_INC_REF(new_ipsq);
14603 		phyint->phyint_ipsq = new_ipsq;
14604 		phyint = next_phyint;
14605 	}
14606 }
14607 
14608 #define	SPLIT_SUCCESS		0
14609 #define	SPLIT_NOT_NEEDED	1
14610 #define	SPLIT_FAILED		2
14611 
14612 int
14613 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry)
14614 {
14615 	ipsq_t *newipsq = NULL;
14616 
14617 	/*
14618 	 * Assertions denote pre-requisites for changing the ipsq of
14619 	 * a phyint
14620 	 */
14621 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
14622 	/*
14623 	 * <ill-phyint> assocs can't change while ill_g_lock
14624 	 * is held as writer. See ill_phyint_reinit()
14625 	 */
14626 	ASSERT(phyint->phyint_illv4 == NULL ||
14627 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14628 	ASSERT(phyint->phyint_illv6 == NULL ||
14629 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14630 
14631 	if ((phyint->phyint_groupname_len !=
14632 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14633 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14634 	    phyint->phyint_groupname_len) != 0)) {
14635 		/*
14636 		 * Once we fail in creating a new ipsq due to memory shortage,
14637 		 * don't attempt to create new ipsq again, based on another
14638 		 * phyint, since we want all phyints belonging to an IPMP group
14639 		 * to be in the same ipsq even in the event of mem alloc fails.
14640 		 */
14641 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14642 		    cur_ipsq);
14643 		if (newipsq == NULL) {
14644 			/* Memory allocation failure */
14645 			return (SPLIT_FAILED);
14646 		} else {
14647 			/* ipsq_refs protected by ill_g_lock (writer) */
14648 			IPSQ_DEC_REF(cur_ipsq);
14649 			phyint->phyint_ipsq = newipsq;
14650 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14651 			newipsq->ipsq_phyint_list = phyint;
14652 			IPSQ_INC_REF(newipsq);
14653 			return (SPLIT_SUCCESS);
14654 		}
14655 	}
14656 	return (SPLIT_NOT_NEEDED);
14657 }
14658 
14659 /*
14660  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14661  * to do this split
14662  */
14663 static int
14664 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq)
14665 {
14666 	ipsq_t *newipsq;
14667 
14668 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
14669 	/*
14670 	 * <ill-phyint> assocs can't change while ill_g_lock
14671 	 * is held as writer. See ill_phyint_reinit()
14672 	 */
14673 
14674 	ASSERT(phyint->phyint_illv4 == NULL ||
14675 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14676 	ASSERT(phyint->phyint_illv6 == NULL ||
14677 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14678 
14679 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14680 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14681 		/*
14682 		 * ipsq_init failed due to no memory
14683 		 * caller will use the same ipsq
14684 		 */
14685 		return (SPLIT_FAILED);
14686 	}
14687 
14688 	/* ipsq_ref is protected by ill_g_lock (writer) */
14689 	IPSQ_DEC_REF(cur_ipsq);
14690 
14691 	/*
14692 	 * This is a new ipsq that is unknown to the world.
14693 	 * So we don't need to hold ipsq_lock,
14694 	 */
14695 	newipsq = phyint->phyint_ipsq;
14696 	newipsq->ipsq_writer = NULL;
14697 	newipsq->ipsq_reentry_cnt--;
14698 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14699 #ifdef ILL_DEBUG
14700 	newipsq->ipsq_depth = 0;
14701 #endif
14702 
14703 	return (SPLIT_SUCCESS);
14704 }
14705 
14706 /*
14707  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14708  * ipsq's representing their individual groups or themselves. Return
14709  * whether split needs to be retried again later.
14710  */
14711 static boolean_t
14712 ill_split_ipsq(ipsq_t *cur_ipsq)
14713 {
14714 	phyint_t *phyint;
14715 	phyint_t *next_phyint;
14716 	int	error;
14717 	boolean_t need_retry = B_FALSE;
14718 
14719 	phyint = cur_ipsq->ipsq_phyint_list;
14720 	cur_ipsq->ipsq_phyint_list = NULL;
14721 	while (phyint != NULL) {
14722 		next_phyint = phyint->phyint_ipsq_next;
14723 		/*
14724 		 * 'created' will tell us whether the callee actually
14725 		 * created an ipsq. Lack of memory may force the callee
14726 		 * to return without creating an ipsq.
14727 		 */
14728 		if (phyint->phyint_groupname == NULL) {
14729 			error = ill_split_to_own_ipsq(phyint, cur_ipsq);
14730 		} else {
14731 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14732 					need_retry);
14733 		}
14734 
14735 		switch (error) {
14736 		case SPLIT_FAILED:
14737 			need_retry = B_TRUE;
14738 			/* FALLTHRU */
14739 		case SPLIT_NOT_NEEDED:
14740 			/*
14741 			 * Keep it on the list.
14742 			 */
14743 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14744 			cur_ipsq->ipsq_phyint_list = phyint;
14745 			break;
14746 		case SPLIT_SUCCESS:
14747 			break;
14748 		default:
14749 			ASSERT(0);
14750 		}
14751 
14752 		phyint = next_phyint;
14753 	}
14754 	return (need_retry);
14755 }
14756 
14757 /*
14758  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14759  * and return the ills in the list. This list will be
14760  * needed to unlock all the ills later on by the caller.
14761  * The <ill-ipsq> associations could change between the
14762  * lock and unlock. Hence the unlock can't traverse the
14763  * ipsq to get the list of ills.
14764  */
14765 static int
14766 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14767 {
14768 	int	cnt = 0;
14769 	phyint_t	*phyint;
14770 
14771 	/*
14772 	 * The caller holds ill_g_lock to ensure that the ill memberships
14773 	 * of the ipsq don't change
14774 	 */
14775 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
14776 
14777 	phyint = ipsq->ipsq_phyint_list;
14778 	while (phyint != NULL) {
14779 		if (phyint->phyint_illv4 != NULL) {
14780 			ASSERT(cnt < list_max);
14781 			list[cnt++] = phyint->phyint_illv4;
14782 		}
14783 		if (phyint->phyint_illv6 != NULL) {
14784 			ASSERT(cnt < list_max);
14785 			list[cnt++] = phyint->phyint_illv6;
14786 		}
14787 		phyint = phyint->phyint_ipsq_next;
14788 	}
14789 	ill_lock_ills(list, cnt);
14790 	return (cnt);
14791 }
14792 
14793 void
14794 ill_lock_ills(ill_t **list, int cnt)
14795 {
14796 	int	i;
14797 
14798 	if (cnt > 1) {
14799 		boolean_t try_again;
14800 		do {
14801 			try_again = B_FALSE;
14802 			for (i = 0; i < cnt - 1; i++) {
14803 				if (list[i] < list[i + 1]) {
14804 					ill_t	*tmp;
14805 
14806 					/* swap the elements */
14807 					tmp = list[i];
14808 					list[i] = list[i + 1];
14809 					list[i + 1] = tmp;
14810 					try_again = B_TRUE;
14811 				}
14812 			}
14813 		} while (try_again);
14814 	}
14815 
14816 	for (i = 0; i < cnt; i++) {
14817 		if (i == 0) {
14818 			if (list[i] != NULL)
14819 				mutex_enter(&list[i]->ill_lock);
14820 			else
14821 				return;
14822 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14823 			mutex_enter(&list[i]->ill_lock);
14824 		}
14825 	}
14826 }
14827 
14828 void
14829 ill_unlock_ills(ill_t **list, int cnt)
14830 {
14831 	int	i;
14832 
14833 	for (i = 0; i < cnt; i++) {
14834 		if ((i == 0) && (list[i] != NULL)) {
14835 			mutex_exit(&list[i]->ill_lock);
14836 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14837 			mutex_exit(&list[i]->ill_lock);
14838 		}
14839 	}
14840 }
14841 
14842 /*
14843  * Merge all the ills from 1 ipsq group into another ipsq group.
14844  * The source ipsq group is specified by the ipsq associated with
14845  * 'from_ill'. The destination ipsq group is specified by the ipsq
14846  * associated with 'to_ill' or 'groupname' respectively.
14847  * Note that ipsq itself does not have a reference count mechanism
14848  * and functions don't look up an ipsq and pass it around. Instead
14849  * functions pass around an ill or groupname, and the ipsq is looked
14850  * up from the ill or groupname and the required operation performed
14851  * atomically with the lookup on the ipsq.
14852  */
14853 static int
14854 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14855     queue_t *q)
14856 {
14857 	ipsq_t *old_ipsq;
14858 	ipsq_t *new_ipsq;
14859 	ill_t	**ill_list;
14860 	int	cnt;
14861 	size_t	ill_list_size;
14862 	boolean_t became_writer_on_new_sq = B_FALSE;
14863 
14864 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14865 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14866 
14867 	/*
14868 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14869 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14870 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14871 	 */
14872 	rw_enter(&ill_g_lock, RW_WRITER);
14873 
14874 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14875 	if (groupname != NULL)
14876 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL);
14877 	else {
14878 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14879 	}
14880 
14881 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14882 
14883 	/*
14884 	 * both groups are on the same ipsq.
14885 	 */
14886 	if (old_ipsq == new_ipsq) {
14887 		rw_exit(&ill_g_lock);
14888 		return (0);
14889 	}
14890 
14891 	cnt = old_ipsq->ipsq_refs << 1;
14892 	ill_list_size = cnt * sizeof (ill_t *);
14893 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14894 	if (ill_list == NULL) {
14895 		rw_exit(&ill_g_lock);
14896 		return (ENOMEM);
14897 	}
14898 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14899 
14900 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14901 	mutex_enter(&new_ipsq->ipsq_lock);
14902 	if ((new_ipsq->ipsq_writer == NULL &&
14903 		new_ipsq->ipsq_current_ipif == NULL) ||
14904 	    (new_ipsq->ipsq_writer == curthread)) {
14905 		new_ipsq->ipsq_writer = curthread;
14906 		new_ipsq->ipsq_reentry_cnt++;
14907 		became_writer_on_new_sq = B_TRUE;
14908 	}
14909 
14910 	/*
14911 	 * We are holding ill_g_lock as writer and all the ill locks of
14912 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14913 	 * message can land up on the old ipsq even though we don't hold the
14914 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14915 	 */
14916 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14917 
14918 	/*
14919 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14920 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14921 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14922 	 */
14923 	ill_merge_ipsq(old_ipsq, new_ipsq);
14924 
14925 	/*
14926 	 * Mark the new ipsq as needing a split since it is currently
14927 	 * being shared by more than 1 IPMP group. The split will
14928 	 * occur at the end of ipsq_exit
14929 	 */
14930 	new_ipsq->ipsq_split = B_TRUE;
14931 
14932 	/* Now release all the locks */
14933 	mutex_exit(&new_ipsq->ipsq_lock);
14934 	ill_unlock_ills(ill_list, cnt);
14935 	rw_exit(&ill_g_lock);
14936 
14937 	kmem_free(ill_list, ill_list_size);
14938 
14939 	/*
14940 	 * If we succeeded in becoming writer on the new ipsq, then
14941 	 * drain the new ipsq and start processing  all enqueued messages
14942 	 * including the current ioctl we are processing which is either
14943 	 * a set groupname or failover/failback.
14944 	 */
14945 	if (became_writer_on_new_sq)
14946 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
14947 
14948 	/*
14949 	 * syncq has been changed and all the messages have been moved.
14950 	 */
14951 	mutex_enter(&old_ipsq->ipsq_lock);
14952 	old_ipsq->ipsq_current_ipif = NULL;
14953 	mutex_exit(&old_ipsq->ipsq_lock);
14954 	return (EINPROGRESS);
14955 }
14956 
14957 /*
14958  * Delete and add the loopback copy and non-loopback copy of
14959  * the BROADCAST ire corresponding to ill and addr. Used to
14960  * group broadcast ires together when ill becomes part of
14961  * a group.
14962  *
14963  * This function is also called when ill is leaving the group
14964  * so that the ires belonging to the group gets re-grouped.
14965  */
14966 static void
14967 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14968 {
14969 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14970 	ire_t **ire_ptpn = &ire_head;
14971 
14972 	/*
14973 	 * The loopback and non-loopback IREs are inserted in the order in which
14974 	 * they're found, on the basis that they are correctly ordered (loopback
14975 	 * first).
14976 	 */
14977 	for (;;) {
14978 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14979 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL);
14980 		if (ire == NULL)
14981 			break;
14982 
14983 		/*
14984 		 * we are passing in KM_SLEEP because it is not easy to
14985 		 * go back to a sane state in case of memory failure.
14986 		 */
14987 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14988 		ASSERT(nire != NULL);
14989 		bzero(nire, sizeof (ire_t));
14990 		/*
14991 		 * Don't use ire_max_frag directly since we don't
14992 		 * hold on to 'ire' until we add the new ire 'nire' and
14993 		 * we don't want the new ire to have a dangling reference
14994 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14995 		 * be in sync with the ipif_mtu of the associate ipif.
14996 		 * For eg. this happens as a result of SIOCSLIFNAME,
14997 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14998 		 * the driver. A change in ire_max_frag triggered as
14999 		 * as a result of path mtu discovery, or due to an
15000 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
15001 		 * route change -mtu command does not apply to broadcast ires.
15002 		 *
15003 		 * XXX We need a recovery strategy here if ire_init fails
15004 		 */
15005 		if (ire_init(nire,
15006 		    (uchar_t *)&ire->ire_addr,
15007 		    (uchar_t *)&ire->ire_mask,
15008 		    (uchar_t *)&ire->ire_src_addr,
15009 		    (uchar_t *)&ire->ire_gateway_addr,
15010 		    (uchar_t *)&ire->ire_in_src_addr,
15011 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
15012 			&ire->ire_ipif->ipif_mtu,
15013 		    (ire->ire_nce != NULL ? ire->ire_nce->nce_fp_mp : NULL),
15014 		    ire->ire_rfq,
15015 		    ire->ire_stq,
15016 		    ire->ire_type,
15017 		    (ire->ire_nce != NULL? ire->ire_nce->nce_res_mp : NULL),
15018 		    ire->ire_ipif,
15019 		    ire->ire_in_ill,
15020 		    ire->ire_cmask,
15021 		    ire->ire_phandle,
15022 		    ire->ire_ihandle,
15023 		    ire->ire_flags,
15024 		    &ire->ire_uinfo,
15025 		    NULL,
15026 		    NULL) == NULL) {
15027 			cmn_err(CE_PANIC, "ire_init() failed");
15028 		}
15029 		ire_delete(ire);
15030 		ire_refrele(ire);
15031 
15032 		/*
15033 		 * The newly created IREs are inserted at the tail of the list
15034 		 * starting with ire_head. As we've just allocated them no one
15035 		 * knows about them so it's safe.
15036 		 */
15037 		*ire_ptpn = nire;
15038 		ire_ptpn = &nire->ire_next;
15039 	}
15040 
15041 	for (nire = ire_head; nire != NULL; nire = nire_next) {
15042 		int error;
15043 		ire_t *oire;
15044 		/* unlink the IRE from our list before calling ire_add() */
15045 		nire_next = nire->ire_next;
15046 		nire->ire_next = NULL;
15047 
15048 		/* ire_add adds the ire at the right place in the list */
15049 		oire = nire;
15050 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
15051 		ASSERT(error == 0);
15052 		ASSERT(oire == nire);
15053 		ire_refrele(nire);	/* Held in ire_add */
15054 	}
15055 }
15056 
15057 /*
15058  * This function is usually called when an ill is inserted in
15059  * a group and all the ipifs are already UP. As all the ipifs
15060  * are already UP, the broadcast ires have already been created
15061  * and been inserted. But, ire_add_v4 would not have grouped properly.
15062  * We need to re-group for the benefit of ip_wput_ire which
15063  * expects BROADCAST ires to be grouped properly to avoid sending
15064  * more than one copy of the broadcast packet per group.
15065  *
15066  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
15067  *	  because when ipif_up_done ends up calling this, ires have
15068  *        already been added before illgrp_insert i.e before ill_group
15069  *	  has been initialized.
15070  */
15071 static void
15072 ill_group_bcast_for_xmit(ill_t *ill)
15073 {
15074 	ill_group_t *illgrp;
15075 	ipif_t *ipif;
15076 	ipaddr_t addr;
15077 	ipaddr_t net_mask;
15078 	ipaddr_t subnet_netmask;
15079 
15080 	illgrp = ill->ill_group;
15081 
15082 	/*
15083 	 * This function is called even when an ill is deleted from
15084 	 * the group. Hence, illgrp could be null.
15085 	 */
15086 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15087 		return;
15088 
15089 	/*
15090 	 * Delete all the BROADCAST ires matching this ill and add
15091 	 * them back. This time, ire_add_v4 should take care of
15092 	 * grouping them with others because ill is part of the
15093 	 * group.
15094 	 */
15095 	ill_bcast_delete_and_add(ill, 0);
15096 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15097 
15098 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15099 
15100 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15101 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15102 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15103 		} else {
15104 			net_mask = htonl(IN_CLASSA_NET);
15105 		}
15106 		addr = net_mask & ipif->ipif_subnet;
15107 		ill_bcast_delete_and_add(ill, addr);
15108 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15109 
15110 		subnet_netmask = ipif->ipif_net_mask;
15111 		addr = ipif->ipif_subnet;
15112 		ill_bcast_delete_and_add(ill, addr);
15113 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15114 	}
15115 }
15116 
15117 /*
15118  * This function is called from illgrp_delete when ill is being deleted
15119  * from the group.
15120  *
15121  * As ill is not there in the group anymore, any address belonging
15122  * to this ill should be cleared of IRE_MARK_NORECV.
15123  */
15124 static void
15125 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15126 {
15127 	ire_t *ire;
15128 	irb_t *irb;
15129 
15130 	ASSERT(ill->ill_group == NULL);
15131 
15132 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15133 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL);
15134 
15135 	if (ire != NULL) {
15136 		/*
15137 		 * IPMP and plumbing operations are serialized on the ipsq, so
15138 		 * no one will insert or delete a broadcast ire under our feet.
15139 		 */
15140 		irb = ire->ire_bucket;
15141 		rw_enter(&irb->irb_lock, RW_READER);
15142 		ire_refrele(ire);
15143 
15144 		for (; ire != NULL; ire = ire->ire_next) {
15145 			if (ire->ire_addr != addr)
15146 				break;
15147 			if (ire_to_ill(ire) != ill)
15148 				continue;
15149 
15150 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15151 			ire->ire_marks &= ~IRE_MARK_NORECV;
15152 		}
15153 		rw_exit(&irb->irb_lock);
15154 	}
15155 }
15156 
15157 /*
15158  * This function must be called only after the broadcast ires
15159  * have been grouped together. For a given address addr, nominate
15160  * only one of the ires whose interface is not FAILED or OFFLINE.
15161  *
15162  * This is also called when an ipif goes down, so that we can nominate
15163  * a different ire with the same address for receiving.
15164  */
15165 static void
15166 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr)
15167 {
15168 	irb_t *irb;
15169 	ire_t *ire;
15170 	ire_t *ire1;
15171 	ire_t *save_ire;
15172 	ire_t **irep = NULL;
15173 	boolean_t first = B_TRUE;
15174 	ire_t *clear_ire = NULL;
15175 	ire_t *start_ire = NULL;
15176 	ire_t	*new_lb_ire;
15177 	ire_t	*new_nlb_ire;
15178 	boolean_t new_lb_ire_used = B_FALSE;
15179 	boolean_t new_nlb_ire_used = B_FALSE;
15180 	uint64_t match_flags;
15181 	uint64_t phyi_flags;
15182 	boolean_t fallback = B_FALSE;
15183 
15184 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15185 	    NULL, MATCH_IRE_TYPE);
15186 	/*
15187 	 * We may not be able to find some ires if a previous
15188 	 * ire_create failed. This happens when an ipif goes
15189 	 * down and we are unable to create BROADCAST ires due
15190 	 * to memory failure. Thus, we have to check for NULL
15191 	 * below. This should handle the case for LOOPBACK,
15192 	 * POINTOPOINT and interfaces with some POINTOPOINT
15193 	 * logicals for which there are no BROADCAST ires.
15194 	 */
15195 	if (ire == NULL)
15196 		return;
15197 	/*
15198 	 * Currently IRE_BROADCASTS are deleted when an ipif
15199 	 * goes down which runs exclusively. Thus, setting
15200 	 * IRE_MARK_RCVD should not race with ire_delete marking
15201 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15202 	 * be consistent with other parts of the code that walks
15203 	 * a given bucket.
15204 	 */
15205 	save_ire = ire;
15206 	irb = ire->ire_bucket;
15207 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15208 	if (new_lb_ire == NULL) {
15209 		ire_refrele(ire);
15210 		return;
15211 	}
15212 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15213 	if (new_nlb_ire == NULL) {
15214 		ire_refrele(ire);
15215 		kmem_cache_free(ire_cache, new_lb_ire);
15216 		return;
15217 	}
15218 	IRB_REFHOLD(irb);
15219 	rw_enter(&irb->irb_lock, RW_WRITER);
15220 	/*
15221 	 * Get to the first ire matching the address and the
15222 	 * group. If the address does not match we are done
15223 	 * as we could not find the IRE. If the address matches
15224 	 * we should get to the first one matching the group.
15225 	 */
15226 	while (ire != NULL) {
15227 		if (ire->ire_addr != addr ||
15228 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15229 			break;
15230 		}
15231 		ire = ire->ire_next;
15232 	}
15233 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15234 	start_ire = ire;
15235 redo:
15236 	while (ire != NULL && ire->ire_addr == addr &&
15237 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15238 		/*
15239 		 * The first ire for any address within a group
15240 		 * should always be the one with IRE_MARK_NORECV cleared
15241 		 * so that ip_wput_ire can avoid searching for one.
15242 		 * Note down the insertion point which will be used
15243 		 * later.
15244 		 */
15245 		if (first && (irep == NULL))
15246 			irep = ire->ire_ptpn;
15247 		/*
15248 		 * PHYI_FAILED is set when the interface fails.
15249 		 * This interface might have become good, but the
15250 		 * daemon has not yet detected. We should still
15251 		 * not receive on this. PHYI_OFFLINE should never
15252 		 * be picked as this has been offlined and soon
15253 		 * be removed.
15254 		 */
15255 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15256 		if (phyi_flags & PHYI_OFFLINE) {
15257 			ire->ire_marks |= IRE_MARK_NORECV;
15258 			ire = ire->ire_next;
15259 			continue;
15260 		}
15261 		if (phyi_flags & match_flags) {
15262 			ire->ire_marks |= IRE_MARK_NORECV;
15263 			ire = ire->ire_next;
15264 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15265 			    PHYI_INACTIVE) {
15266 				fallback = B_TRUE;
15267 			}
15268 			continue;
15269 		}
15270 		if (first) {
15271 			/*
15272 			 * We will move this to the front of the list later
15273 			 * on.
15274 			 */
15275 			clear_ire = ire;
15276 			ire->ire_marks &= ~IRE_MARK_NORECV;
15277 		} else {
15278 			ire->ire_marks |= IRE_MARK_NORECV;
15279 		}
15280 		first = B_FALSE;
15281 		ire = ire->ire_next;
15282 	}
15283 	/*
15284 	 * If we never nominated anybody, try nominating at least
15285 	 * an INACTIVE, if we found one. Do it only once though.
15286 	 */
15287 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15288 	    fallback) {
15289 		match_flags = PHYI_FAILED;
15290 		ire = start_ire;
15291 		irep = NULL;
15292 		goto redo;
15293 	}
15294 	ire_refrele(save_ire);
15295 
15296 	/*
15297 	 * irep non-NULL indicates that we entered the while loop
15298 	 * above. If clear_ire is at the insertion point, we don't
15299 	 * have to do anything. clear_ire will be NULL if all the
15300 	 * interfaces are failed.
15301 	 *
15302 	 * We cannot unlink and reinsert the ire at the right place
15303 	 * in the list since there can be other walkers of this bucket.
15304 	 * Instead we delete and recreate the ire
15305 	 */
15306 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15307 		ire_t *clear_ire_stq = NULL;
15308 		mblk_t *fp_mp = NULL, *res_mp = NULL;
15309 
15310 		bzero(new_lb_ire, sizeof (ire_t));
15311 		if (clear_ire->ire_nce != NULL) {
15312 			fp_mp = clear_ire->ire_nce->nce_fp_mp;
15313 			res_mp = clear_ire->ire_nce->nce_res_mp;
15314 		}
15315 		/* XXX We need a recovery strategy here. */
15316 		if (ire_init(new_lb_ire,
15317 		    (uchar_t *)&clear_ire->ire_addr,
15318 		    (uchar_t *)&clear_ire->ire_mask,
15319 		    (uchar_t *)&clear_ire->ire_src_addr,
15320 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15321 		    (uchar_t *)&clear_ire->ire_in_src_addr,
15322 		    &clear_ire->ire_max_frag,
15323 		    fp_mp,
15324 		    clear_ire->ire_rfq,
15325 		    clear_ire->ire_stq,
15326 		    clear_ire->ire_type,
15327 		    res_mp,
15328 		    clear_ire->ire_ipif,
15329 		    clear_ire->ire_in_ill,
15330 		    clear_ire->ire_cmask,
15331 		    clear_ire->ire_phandle,
15332 		    clear_ire->ire_ihandle,
15333 		    clear_ire->ire_flags,
15334 		    &clear_ire->ire_uinfo,
15335 		    NULL,
15336 		    NULL) == NULL)
15337 			cmn_err(CE_PANIC, "ire_init() failed");
15338 		if (clear_ire->ire_stq == NULL) {
15339 			ire_t *ire_next = clear_ire->ire_next;
15340 			if (ire_next != NULL &&
15341 			    ire_next->ire_stq != NULL &&
15342 			    ire_next->ire_addr == clear_ire->ire_addr &&
15343 			    ire_next->ire_ipif->ipif_ill ==
15344 			    clear_ire->ire_ipif->ipif_ill) {
15345 				clear_ire_stq = ire_next;
15346 
15347 				bzero(new_nlb_ire, sizeof (ire_t));
15348 				if (clear_ire_stq->ire_nce != NULL) {
15349 					fp_mp =
15350 					    clear_ire_stq->ire_nce->nce_fp_mp;
15351 					res_mp =
15352 					    clear_ire_stq->ire_nce->nce_res_mp;
15353 				} else {
15354 					fp_mp = res_mp = NULL;
15355 				}
15356 				/* XXX We need a recovery strategy here. */
15357 				if (ire_init(new_nlb_ire,
15358 				    (uchar_t *)&clear_ire_stq->ire_addr,
15359 				    (uchar_t *)&clear_ire_stq->ire_mask,
15360 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15361 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15362 				    (uchar_t *)&clear_ire_stq->ire_in_src_addr,
15363 				    &clear_ire_stq->ire_max_frag,
15364 				    fp_mp,
15365 				    clear_ire_stq->ire_rfq,
15366 				    clear_ire_stq->ire_stq,
15367 				    clear_ire_stq->ire_type,
15368 				    res_mp,
15369 				    clear_ire_stq->ire_ipif,
15370 				    clear_ire_stq->ire_in_ill,
15371 				    clear_ire_stq->ire_cmask,
15372 				    clear_ire_stq->ire_phandle,
15373 				    clear_ire_stq->ire_ihandle,
15374 				    clear_ire_stq->ire_flags,
15375 				    &clear_ire_stq->ire_uinfo,
15376 				    NULL,
15377 				    NULL) == NULL)
15378 					cmn_err(CE_PANIC, "ire_init() failed");
15379 			}
15380 		}
15381 
15382 		/*
15383 		 * Delete the ire. We can't call ire_delete() since
15384 		 * we are holding the bucket lock. We can't release the
15385 		 * bucket lock since we can't allow irep to change. So just
15386 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15387 		 * ire from the list and do the refrele.
15388 		 */
15389 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15390 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15391 
15392 		if (clear_ire_stq != NULL) {
15393 			ire_fastpath_list_delete(
15394 			    (ill_t *)clear_ire_stq->ire_stq->q_ptr,
15395 			    clear_ire_stq);
15396 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15397 		}
15398 
15399 		/*
15400 		 * Also take care of otherfields like ib/ob pkt count
15401 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15402 		 */
15403 
15404 		/* Add the new ire's. Insert at *irep */
15405 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15406 		ire1 = *irep;
15407 		if (ire1 != NULL)
15408 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15409 		new_lb_ire->ire_next = ire1;
15410 		/* Link the new one in. */
15411 		new_lb_ire->ire_ptpn = irep;
15412 		membar_producer();
15413 		*irep = new_lb_ire;
15414 		new_lb_ire_used = B_TRUE;
15415 		BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
15416 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15417 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15418 
15419 		if (clear_ire_stq != NULL) {
15420 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15421 			irep = &new_lb_ire->ire_next;
15422 			/* Add the new ire. Insert at *irep */
15423 			ire1 = *irep;
15424 			if (ire1 != NULL)
15425 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15426 			new_nlb_ire->ire_next = ire1;
15427 			/* Link the new one in. */
15428 			new_nlb_ire->ire_ptpn = irep;
15429 			membar_producer();
15430 			*irep = new_nlb_ire;
15431 			new_nlb_ire_used = B_TRUE;
15432 			BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
15433 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15434 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15435 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
15436 		}
15437 	}
15438 	rw_exit(&irb->irb_lock);
15439 	if (!new_lb_ire_used)
15440 		kmem_cache_free(ire_cache, new_lb_ire);
15441 	if (!new_nlb_ire_used)
15442 		kmem_cache_free(ire_cache, new_nlb_ire);
15443 	IRB_REFRELE(irb);
15444 }
15445 
15446 /*
15447  * Whenever an ipif goes down we have to renominate a different
15448  * broadcast ire to receive. Whenever an ipif comes up, we need
15449  * to make sure that we have only one nominated to receive.
15450  */
15451 static void
15452 ipif_renominate_bcast(ipif_t *ipif)
15453 {
15454 	ill_t *ill = ipif->ipif_ill;
15455 	ipaddr_t subnet_addr;
15456 	ipaddr_t net_addr;
15457 	ipaddr_t net_mask = 0;
15458 	ipaddr_t subnet_netmask;
15459 	ipaddr_t addr;
15460 	ill_group_t *illgrp;
15461 
15462 	illgrp = ill->ill_group;
15463 	/*
15464 	 * If this is the last ipif going down, it might take
15465 	 * the ill out of the group. In that case ipif_down ->
15466 	 * illgrp_delete takes care of doing the nomination.
15467 	 * ipif_down does not call for this case.
15468 	 */
15469 	ASSERT(illgrp != NULL);
15470 
15471 	/* There could not have been any ires associated with this */
15472 	if (ipif->ipif_subnet == 0)
15473 		return;
15474 
15475 	ill_mark_bcast(illgrp, 0);
15476 	ill_mark_bcast(illgrp, INADDR_BROADCAST);
15477 
15478 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15479 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15480 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15481 	} else {
15482 		net_mask = htonl(IN_CLASSA_NET);
15483 	}
15484 	addr = net_mask & ipif->ipif_subnet;
15485 	ill_mark_bcast(illgrp, addr);
15486 
15487 	net_addr = ~net_mask | addr;
15488 	ill_mark_bcast(illgrp, net_addr);
15489 
15490 	subnet_netmask = ipif->ipif_net_mask;
15491 	addr = ipif->ipif_subnet;
15492 	ill_mark_bcast(illgrp, addr);
15493 
15494 	subnet_addr = ~subnet_netmask | addr;
15495 	ill_mark_bcast(illgrp, subnet_addr);
15496 }
15497 
15498 /*
15499  * Whenever we form or delete ill groups, we need to nominate one set of
15500  * BROADCAST ires for receiving in the group.
15501  *
15502  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15503  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15504  *    for ill_ipif_up_count to be non-zero. This is the only case where
15505  *    ill_ipif_up_count is zero and we would still find the ires.
15506  *
15507  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15508  *    ipif is UP and we just have to do the nomination.
15509  *
15510  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15511  *    from the group. So, we have to do the nomination.
15512  *
15513  * Because of (3), there could be just one ill in the group. But we have
15514  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15515  * Thus, this function does not optimize when there is only one ill as
15516  * it is not correct for (3).
15517  */
15518 static void
15519 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15520 {
15521 	ill_t *ill;
15522 	ipif_t *ipif;
15523 	ipaddr_t subnet_addr;
15524 	ipaddr_t prev_subnet_addr = 0;
15525 	ipaddr_t net_addr;
15526 	ipaddr_t prev_net_addr = 0;
15527 	ipaddr_t net_mask = 0;
15528 	ipaddr_t subnet_netmask;
15529 	ipaddr_t addr;
15530 
15531 	/*
15532 	 * When the last memeber is leaving, there is nothing to
15533 	 * nominate.
15534 	 */
15535 	if (illgrp->illgrp_ill_count == 0) {
15536 		ASSERT(illgrp->illgrp_ill == NULL);
15537 		return;
15538 	}
15539 
15540 	ill = illgrp->illgrp_ill;
15541 	ASSERT(!ill->ill_isv6);
15542 	/*
15543 	 * We assume that ires with same address and belonging to the
15544 	 * same group, has been grouped together. Nominating a *single*
15545 	 * ill in the group for sending and receiving broadcast is done
15546 	 * by making sure that the first BROADCAST ire (which will be
15547 	 * the one returned by ire_ctable_lookup for ip_rput and the
15548 	 * one that will be used in ip_wput_ire) will be the one that
15549 	 * will not have IRE_MARK_NORECV set.
15550 	 *
15551 	 * 1) ip_rput checks and discards packets received on ires marked
15552 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15553 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15554 	 *    first ire in the group for every broadcast address in the group.
15555 	 *    ip_rput will accept packets only on the first ire i.e only
15556 	 *    one copy of the ill.
15557 	 *
15558 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15559 	 *    packet for the whole group. It needs to send out on the ill
15560 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15561 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15562 	 *    the copy echoed back on other port where the ire is not marked
15563 	 *    with IRE_MARK_NORECV.
15564 	 *
15565 	 * Note that we just need to have the first IRE either loopback or
15566 	 * non-loopback (either of them may not exist if ire_create failed
15567 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15568 	 * always hit the first one and hence will always accept one copy.
15569 	 *
15570 	 * We have a broadcast ire per ill for all the unique prefixes
15571 	 * hosted on that ill. As we don't have a way of knowing the
15572 	 * unique prefixes on a given ill and hence in the whole group,
15573 	 * we just call ill_mark_bcast on all the prefixes that exist
15574 	 * in the group. For the common case of one prefix, the code
15575 	 * below optimizes by remebering the last address used for
15576 	 * markng. In the case of multiple prefixes, this will still
15577 	 * optimize depending the order of prefixes.
15578 	 *
15579 	 * The only unique address across the whole group is 0.0.0.0 and
15580 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15581 	 * the first ire in the bucket for receiving and disables the
15582 	 * others.
15583 	 */
15584 	ill_mark_bcast(illgrp, 0);
15585 	ill_mark_bcast(illgrp, INADDR_BROADCAST);
15586 	for (; ill != NULL; ill = ill->ill_group_next) {
15587 
15588 		for (ipif = ill->ill_ipif; ipif != NULL;
15589 		    ipif = ipif->ipif_next) {
15590 
15591 			if (!(ipif->ipif_flags & IPIF_UP) ||
15592 			    ipif->ipif_subnet == 0) {
15593 				continue;
15594 			}
15595 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15596 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15597 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15598 			} else {
15599 				net_mask = htonl(IN_CLASSA_NET);
15600 			}
15601 			addr = net_mask & ipif->ipif_subnet;
15602 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15603 				ill_mark_bcast(illgrp, addr);
15604 				net_addr = ~net_mask | addr;
15605 				ill_mark_bcast(illgrp, net_addr);
15606 			}
15607 			prev_net_addr = addr;
15608 
15609 			subnet_netmask = ipif->ipif_net_mask;
15610 			addr = ipif->ipif_subnet;
15611 			if (prev_subnet_addr == 0 ||
15612 			    prev_subnet_addr != addr) {
15613 				ill_mark_bcast(illgrp, addr);
15614 				subnet_addr = ~subnet_netmask | addr;
15615 				ill_mark_bcast(illgrp, subnet_addr);
15616 			}
15617 			prev_subnet_addr = addr;
15618 		}
15619 	}
15620 }
15621 
15622 /*
15623  * This function is called while forming ill groups.
15624  *
15625  * Currently, we handle only allmulti groups. We want to join
15626  * allmulti on only one of the ills in the groups. In future,
15627  * when we have link aggregation, we may have to join normal
15628  * multicast groups on multiple ills as switch does inbound load
15629  * balancing. Following are the functions that calls this
15630  * function :
15631  *
15632  * 1) ill_recover_multicast : Interface is coming back UP.
15633  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15634  *    will call ill_recover_multicast to recover all the multicast
15635  *    groups. We need to make sure that only one member is joined
15636  *    in the ill group.
15637  *
15638  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15639  *    Somebody is joining allmulti. We need to make sure that only one
15640  *    member is joined in the group.
15641  *
15642  * 3) illgrp_insert : If allmulti has already joined, we need to make
15643  *    sure that only one member is joined in the group.
15644  *
15645  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15646  *    allmulti who we have nominated. We need to pick someother ill.
15647  *
15648  * 5) illgrp_delete : The ill we nominated is leaving the group,
15649  *    we need to pick a new ill to join the group.
15650  *
15651  * For (1), (2), (5) - we just have to check whether there is
15652  * a good ill joined in the group. If we could not find any ills
15653  * joined the group, we should join.
15654  *
15655  * For (4), the one that was nominated to receive, left the group.
15656  * There could be nobody joined in the group when this function is
15657  * called.
15658  *
15659  * For (3) - we need to explicitly check whether there are multiple
15660  * ills joined in the group.
15661  *
15662  * For simplicity, we don't differentiate any of the above cases. We
15663  * just leave the group if it is joined on any of them and join on
15664  * the first good ill.
15665  */
15666 int
15667 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15668 {
15669 	ilm_t *ilm;
15670 	ill_t *ill;
15671 	ill_t *fallback_inactive_ill = NULL;
15672 	ill_t *fallback_failed_ill = NULL;
15673 	int ret = 0;
15674 
15675 	/*
15676 	 * Leave the allmulti on all the ills and start fresh.
15677 	 */
15678 	for (ill = illgrp->illgrp_ill; ill != NULL;
15679 	    ill = ill->ill_group_next) {
15680 		if (ill->ill_join_allmulti)
15681 			(void) ip_leave_allmulti(ill->ill_ipif);
15682 	}
15683 
15684 	/*
15685 	 * Choose a good ill. Fallback to inactive or failed if
15686 	 * none available. We need to fallback to FAILED in the
15687 	 * case where we have 2 interfaces in a group - where
15688 	 * one of them is failed and another is a good one and
15689 	 * the good one (not marked inactive) is leaving the group.
15690 	 */
15691 	ret = 0;
15692 	for (ill = illgrp->illgrp_ill; ill != NULL;
15693 	    ill = ill->ill_group_next) {
15694 		/* Never pick an offline interface */
15695 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15696 			continue;
15697 
15698 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15699 			fallback_failed_ill = ill;
15700 			continue;
15701 		}
15702 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15703 			fallback_inactive_ill = ill;
15704 			continue;
15705 		}
15706 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15707 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15708 				ret = ip_join_allmulti(ill->ill_ipif);
15709 				/*
15710 				 * ip_join_allmulti can fail because of memory
15711 				 * failures. So, make sure we join at least
15712 				 * on one ill.
15713 				 */
15714 				if (ill->ill_join_allmulti)
15715 					return (0);
15716 			}
15717 		}
15718 	}
15719 	if (ret != 0) {
15720 		/*
15721 		 * If we tried nominating above and failed to do so,
15722 		 * return error. We might have tried multiple times.
15723 		 * But, return the latest error.
15724 		 */
15725 		return (ret);
15726 	}
15727 	if ((ill = fallback_inactive_ill) != NULL) {
15728 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15729 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15730 				ret = ip_join_allmulti(ill->ill_ipif);
15731 				return (ret);
15732 			}
15733 		}
15734 	} else if ((ill = fallback_failed_ill) != NULL) {
15735 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15736 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15737 				ret = ip_join_allmulti(ill->ill_ipif);
15738 				return (ret);
15739 			}
15740 		}
15741 	}
15742 	return (0);
15743 }
15744 
15745 /*
15746  * This function is called from illgrp_delete after it is
15747  * deleted from the group to reschedule responsibilities
15748  * to a different ill.
15749  */
15750 static void
15751 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15752 {
15753 	ilm_t	*ilm;
15754 	ipif_t	*ipif;
15755 	ipaddr_t subnet_addr;
15756 	ipaddr_t net_addr;
15757 	ipaddr_t net_mask = 0;
15758 	ipaddr_t subnet_netmask;
15759 	ipaddr_t addr;
15760 
15761 	ASSERT(ill->ill_group == NULL);
15762 	/*
15763 	 * Broadcast Responsibility:
15764 	 *
15765 	 * 1. If this ill has been nominated for receiving broadcast
15766 	 * packets, we need to find a new one. Before we find a new
15767 	 * one, we need to re-group the ires that are part of this new
15768 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15769 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15770 	 * thing for us.
15771 	 *
15772 	 * 2. If this ill was not nominated for receiving broadcast
15773 	 * packets, we need to clear the IRE_MARK_NORECV flag
15774 	 * so that we continue to send up broadcast packets.
15775 	 */
15776 	if (!ill->ill_isv6) {
15777 		/*
15778 		 * Case 1 above : No optimization here. Just redo the
15779 		 * nomination.
15780 		 */
15781 		ill_group_bcast_for_xmit(ill);
15782 		ill_nominate_bcast_rcv(illgrp);
15783 
15784 		/*
15785 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15786 		 */
15787 		ill_clear_bcast_mark(ill, 0);
15788 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15789 
15790 		for (ipif = ill->ill_ipif; ipif != NULL;
15791 		    ipif = ipif->ipif_next) {
15792 
15793 			if (!(ipif->ipif_flags & IPIF_UP) ||
15794 			    ipif->ipif_subnet == 0) {
15795 				continue;
15796 			}
15797 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15798 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15799 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15800 			} else {
15801 				net_mask = htonl(IN_CLASSA_NET);
15802 			}
15803 			addr = net_mask & ipif->ipif_subnet;
15804 			ill_clear_bcast_mark(ill, addr);
15805 
15806 			net_addr = ~net_mask | addr;
15807 			ill_clear_bcast_mark(ill, net_addr);
15808 
15809 			subnet_netmask = ipif->ipif_net_mask;
15810 			addr = ipif->ipif_subnet;
15811 			ill_clear_bcast_mark(ill, addr);
15812 
15813 			subnet_addr = ~subnet_netmask | addr;
15814 			ill_clear_bcast_mark(ill, subnet_addr);
15815 		}
15816 	}
15817 
15818 	/*
15819 	 * Multicast Responsibility.
15820 	 *
15821 	 * If we have joined allmulti on this one, find a new member
15822 	 * in the group to join allmulti. As this ill is already part
15823 	 * of allmulti, we don't have to join on this one.
15824 	 *
15825 	 * If we have not joined allmulti on this one, there is no
15826 	 * responsibility to handoff. But we need to take new
15827 	 * responsibility i.e, join allmulti on this one if we need
15828 	 * to.
15829 	 */
15830 	if (ill->ill_join_allmulti) {
15831 		(void) ill_nominate_mcast_rcv(illgrp);
15832 	} else {
15833 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15834 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15835 				(void) ip_join_allmulti(ill->ill_ipif);
15836 				break;
15837 			}
15838 		}
15839 	}
15840 
15841 	/*
15842 	 * We intentionally do the flushing of IRE_CACHES only matching
15843 	 * on the ill and not on groups. Note that we are already deleted
15844 	 * from the group.
15845 	 *
15846 	 * This will make sure that all IRE_CACHES whose stq is pointing
15847 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15848 	 * deleted and IRE_CACHES that are not pointing at this ill will
15849 	 * be left alone.
15850 	 */
15851 	if (ill->ill_isv6) {
15852 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15853 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15854 	} else {
15855 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15856 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15857 	}
15858 
15859 	/*
15860 	 * Some conn may have cached one of the IREs deleted above. By removing
15861 	 * the ire reference, we clean up the extra reference to the ill held in
15862 	 * ire->ire_stq.
15863 	 */
15864 	ipcl_walk(conn_cleanup_stale_ire, NULL);
15865 
15866 	/*
15867 	 * Re-do source address selection for all the members in the
15868 	 * group, if they borrowed source address from one of the ipifs
15869 	 * in this ill.
15870 	 */
15871 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15872 		if (ill->ill_isv6) {
15873 			ipif_update_other_ipifs_v6(ipif, illgrp);
15874 		} else {
15875 			ipif_update_other_ipifs(ipif, illgrp);
15876 		}
15877 	}
15878 }
15879 
15880 /*
15881  * Delete the ill from the group. The caller makes sure that it is
15882  * in a group and it okay to delete from the group. So, we always
15883  * delete here.
15884  */
15885 static void
15886 illgrp_delete(ill_t *ill)
15887 {
15888 	ill_group_t *illgrp;
15889 	ill_group_t *tmpg;
15890 	ill_t *tmp_ill;
15891 
15892 	/*
15893 	 * Reset illgrp_ill_schednext if it was pointing at us.
15894 	 * We need to do this before we set ill_group to NULL.
15895 	 */
15896 	rw_enter(&ill_g_lock, RW_WRITER);
15897 	mutex_enter(&ill->ill_lock);
15898 
15899 	illgrp_reset_schednext(ill);
15900 
15901 	illgrp = ill->ill_group;
15902 
15903 	/* Delete the ill from illgrp. */
15904 	if (illgrp->illgrp_ill == ill) {
15905 		illgrp->illgrp_ill = ill->ill_group_next;
15906 	} else {
15907 		tmp_ill = illgrp->illgrp_ill;
15908 		while (tmp_ill->ill_group_next != ill) {
15909 			tmp_ill = tmp_ill->ill_group_next;
15910 			ASSERT(tmp_ill != NULL);
15911 		}
15912 		tmp_ill->ill_group_next = ill->ill_group_next;
15913 	}
15914 	ill->ill_group = NULL;
15915 	ill->ill_group_next = NULL;
15916 
15917 	illgrp->illgrp_ill_count--;
15918 	mutex_exit(&ill->ill_lock);
15919 	rw_exit(&ill_g_lock);
15920 
15921 	/*
15922 	 * As this ill is leaving the group, we need to hand off
15923 	 * the responsibilities to the other ills in the group, if
15924 	 * this ill had some responsibilities.
15925 	 */
15926 
15927 	ill_handoff_responsibility(ill, illgrp);
15928 
15929 	rw_enter(&ill_g_lock, RW_WRITER);
15930 
15931 	if (illgrp->illgrp_ill_count == 0) {
15932 
15933 		ASSERT(illgrp->illgrp_ill == NULL);
15934 		if (ill->ill_isv6) {
15935 			if (illgrp == illgrp_head_v6) {
15936 				illgrp_head_v6 = illgrp->illgrp_next;
15937 			} else {
15938 				tmpg = illgrp_head_v6;
15939 				while (tmpg->illgrp_next != illgrp) {
15940 					tmpg = tmpg->illgrp_next;
15941 					ASSERT(tmpg != NULL);
15942 				}
15943 				tmpg->illgrp_next = illgrp->illgrp_next;
15944 			}
15945 		} else {
15946 			if (illgrp == illgrp_head_v4) {
15947 				illgrp_head_v4 = illgrp->illgrp_next;
15948 			} else {
15949 				tmpg = illgrp_head_v4;
15950 				while (tmpg->illgrp_next != illgrp) {
15951 					tmpg = tmpg->illgrp_next;
15952 					ASSERT(tmpg != NULL);
15953 				}
15954 				tmpg->illgrp_next = illgrp->illgrp_next;
15955 			}
15956 		}
15957 		mutex_destroy(&illgrp->illgrp_lock);
15958 		mi_free(illgrp);
15959 	}
15960 	rw_exit(&ill_g_lock);
15961 
15962 	/*
15963 	 * Even though the ill is out of the group its not necessary
15964 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15965 	 * We will split the ipsq when phyint_groupname is set to NULL.
15966 	 */
15967 
15968 	/*
15969 	 * Send a routing sockets message if we are deleting from
15970 	 * groups with names.
15971 	 */
15972 	if (ill->ill_phyint->phyint_groupname_len != 0)
15973 		ip_rts_ifmsg(ill->ill_ipif);
15974 }
15975 
15976 /*
15977  * Re-do source address selection. This is normally called when
15978  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15979  * ipif comes up.
15980  */
15981 void
15982 ill_update_source_selection(ill_t *ill)
15983 {
15984 	ipif_t *ipif;
15985 
15986 	ASSERT(IAM_WRITER_ILL(ill));
15987 
15988 	if (ill->ill_group != NULL)
15989 		ill = ill->ill_group->illgrp_ill;
15990 
15991 	for (; ill != NULL; ill = ill->ill_group_next) {
15992 		for (ipif = ill->ill_ipif; ipif != NULL;
15993 		    ipif = ipif->ipif_next) {
15994 			if (ill->ill_isv6)
15995 				ipif_recreate_interface_routes_v6(NULL, ipif);
15996 			else
15997 				ipif_recreate_interface_routes(NULL, ipif);
15998 		}
15999 	}
16000 }
16001 
16002 /*
16003  * Insert ill in a group headed by illgrp_head. The caller can either
16004  * pass a groupname in which case we search for a group with the
16005  * same name to insert in or pass a group to insert in. This function
16006  * would only search groups with names.
16007  *
16008  * NOTE : The caller should make sure that there is at least one ipif
16009  *	  UP on this ill so that illgrp_scheduler can pick this ill
16010  *	  for outbound packets. If ill_ipif_up_count is zero, we have
16011  *	  already sent a DL_UNBIND to the driver and we don't want to
16012  *	  send anymore packets. We don't assert for ipif_up_count
16013  *	  to be greater than zero, because ipif_up_done wants to call
16014  *	  this function before bumping up the ipif_up_count. See
16015  *	  ipif_up_done() for details.
16016  */
16017 int
16018 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
16019     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
16020 {
16021 	ill_group_t *illgrp;
16022 	ill_t *prev_ill;
16023 	phyint_t *phyi;
16024 
16025 	ASSERT(ill->ill_group == NULL);
16026 
16027 	rw_enter(&ill_g_lock, RW_WRITER);
16028 	mutex_enter(&ill->ill_lock);
16029 
16030 	if (groupname != NULL) {
16031 		/*
16032 		 * Look for a group with a matching groupname to insert.
16033 		 */
16034 		for (illgrp = *illgrp_head; illgrp != NULL;
16035 		    illgrp = illgrp->illgrp_next) {
16036 
16037 			ill_t *tmp_ill;
16038 
16039 			/*
16040 			 * If we have an ill_group_t in the list which has
16041 			 * no ill_t assigned then we must be in the process of
16042 			 * removing this group. We skip this as illgrp_delete()
16043 			 * will remove it from the list.
16044 			 */
16045 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
16046 				ASSERT(illgrp->illgrp_ill_count == 0);
16047 				continue;
16048 			}
16049 
16050 			ASSERT(tmp_ill->ill_phyint != NULL);
16051 			phyi = tmp_ill->ill_phyint;
16052 			/*
16053 			 * Look at groups which has names only.
16054 			 */
16055 			if (phyi->phyint_groupname_len == 0)
16056 				continue;
16057 			/*
16058 			 * Names are stored in the phyint common to both
16059 			 * IPv4 and IPv6.
16060 			 */
16061 			if (mi_strcmp(phyi->phyint_groupname,
16062 			    groupname) == 0) {
16063 				break;
16064 			}
16065 		}
16066 	} else {
16067 		/*
16068 		 * If the caller passes in a NULL "grp_to_insert", we
16069 		 * allocate one below and insert this singleton.
16070 		 */
16071 		illgrp = grp_to_insert;
16072 	}
16073 
16074 	ill->ill_group_next = NULL;
16075 
16076 	if (illgrp == NULL) {
16077 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16078 		if (illgrp == NULL) {
16079 			return (ENOMEM);
16080 		}
16081 		illgrp->illgrp_next = *illgrp_head;
16082 		*illgrp_head = illgrp;
16083 		illgrp->illgrp_ill = ill;
16084 		illgrp->illgrp_ill_count = 1;
16085 		ill->ill_group = illgrp;
16086 		/*
16087 		 * Used in illgrp_scheduler to protect multiple threads
16088 		 * from traversing the list.
16089 		 */
16090 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16091 	} else {
16092 		ASSERT(ill->ill_net_type ==
16093 		    illgrp->illgrp_ill->ill_net_type);
16094 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16095 
16096 		/* Insert ill at tail of this group */
16097 		prev_ill = illgrp->illgrp_ill;
16098 		while (prev_ill->ill_group_next != NULL)
16099 			prev_ill = prev_ill->ill_group_next;
16100 		prev_ill->ill_group_next = ill;
16101 		ill->ill_group = illgrp;
16102 		illgrp->illgrp_ill_count++;
16103 		/*
16104 		 * Inherit group properties. Currently only forwarding
16105 		 * is the property we try to keep the same with all the
16106 		 * ills. When there are more, we will abstract this into
16107 		 * a function.
16108 		 */
16109 		ill->ill_flags &= ~ILLF_ROUTER;
16110 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16111 	}
16112 	mutex_exit(&ill->ill_lock);
16113 	rw_exit(&ill_g_lock);
16114 
16115 	/*
16116 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16117 	 *    may be zero as it has not yet been bumped. But the ires
16118 	 *    have already been added. So, we do the nomination here
16119 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16120 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16121 	 *    ill_ipif_up_count here while nominating broadcast ires for
16122 	 *    receive.
16123 	 *
16124 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16125 	 *    to group them properly as ire_add() has already happened
16126 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16127 	 *    case, we need to do it here anyway.
16128 	 */
16129 	if (!ill->ill_isv6) {
16130 		ill_group_bcast_for_xmit(ill);
16131 		ill_nominate_bcast_rcv(illgrp);
16132 	}
16133 
16134 	if (!ipif_is_coming_up) {
16135 		/*
16136 		 * When ipif_up_done() calls this function, the multicast
16137 		 * groups have not been joined yet. So, there is no point in
16138 		 * nomination. ip_join_allmulti will handle groups when
16139 		 * ill_recover_multicast is called from ipif_up_done() later.
16140 		 */
16141 		(void) ill_nominate_mcast_rcv(illgrp);
16142 		/*
16143 		 * ipif_up_done calls ill_update_source_selection
16144 		 * anyway. Moreover, we don't want to re-create
16145 		 * interface routes while ipif_up_done() still has reference
16146 		 * to them. Refer to ipif_up_done() for more details.
16147 		 */
16148 		ill_update_source_selection(ill);
16149 	}
16150 
16151 	/*
16152 	 * Send a routing sockets message if we are inserting into
16153 	 * groups with names.
16154 	 */
16155 	if (groupname != NULL)
16156 		ip_rts_ifmsg(ill->ill_ipif);
16157 	return (0);
16158 }
16159 
16160 /*
16161  * Return the first phyint matching the groupname. There could
16162  * be more than one when there are ill groups.
16163  *
16164  * Needs work: called only from ip_sioctl_groupname
16165  */
16166 static phyint_t *
16167 phyint_lookup_group(char *groupname)
16168 {
16169 	phyint_t *phyi;
16170 
16171 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
16172 	/*
16173 	 * Group names are stored in the phyint - a common structure
16174 	 * to both IPv4 and IPv6.
16175 	 */
16176 	phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
16177 	for (; phyi != NULL;
16178 	    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
16179 	    phyi, AVL_AFTER)) {
16180 		if (phyi->phyint_groupname_len == 0)
16181 			continue;
16182 		ASSERT(phyi->phyint_groupname != NULL);
16183 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16184 			return (phyi);
16185 	}
16186 	return (NULL);
16187 }
16188 
16189 
16190 
16191 /*
16192  * MT notes on creation and deletion of IPMP groups
16193  *
16194  * Creation and deletion of IPMP groups introduce the need to merge or
16195  * split the associated serialization objects i.e the ipsq's. Normally all
16196  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16197  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16198  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16199  * is a need to change the <ill-ipsq> association and we have to operate on both
16200  * the source and destination IPMP groups. For eg. attempting to set the
16201  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16202  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16203  * source or destination IPMP group are mapped to a single ipsq for executing
16204  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16205  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16206  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16207  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16208  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16209  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16210  *
16211  * In the above example the ioctl handling code locates the current ipsq of hme0
16212  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16213  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16214  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16215  * the destination ipsq. If the destination ipsq is not busy, it also enters
16216  * the destination ipsq exclusively. Now the actual groupname setting operation
16217  * can proceed. If the destination ipsq is busy, the operation is enqueued
16218  * on the destination (merged) ipsq and will be handled in the unwind from
16219  * ipsq_exit.
16220  *
16221  * To prevent other threads accessing the ill while the group name change is
16222  * in progres, we bring down the ipifs which also removes the ill from the
16223  * group. The group is changed in phyint and when the first ipif on the ill
16224  * is brought up, the ill is inserted into the right IPMP group by
16225  * illgrp_insert.
16226  */
16227 /* ARGSUSED */
16228 int
16229 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16230     ip_ioctl_cmd_t *ipip, void *ifreq)
16231 {
16232 	int i;
16233 	char *tmp;
16234 	int namelen;
16235 	ill_t *ill = ipif->ipif_ill;
16236 	ill_t *ill_v4, *ill_v6;
16237 	int err = 0;
16238 	phyint_t *phyi;
16239 	phyint_t *phyi_tmp;
16240 	struct lifreq *lifr;
16241 	mblk_t	*mp1;
16242 	char *groupname;
16243 	ipsq_t *ipsq;
16244 
16245 	ASSERT(IAM_WRITER_IPIF(ipif));
16246 
16247 	/* Existance verified in ip_wput_nondata */
16248 	mp1 = mp->b_cont->b_cont;
16249 	lifr = (struct lifreq *)mp1->b_rptr;
16250 	groupname = lifr->lifr_groupname;
16251 
16252 	if (ipif->ipif_id != 0)
16253 		return (EINVAL);
16254 
16255 	phyi = ill->ill_phyint;
16256 	ASSERT(phyi != NULL);
16257 
16258 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16259 		return (EINVAL);
16260 
16261 	tmp = groupname;
16262 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16263 		;
16264 
16265 	if (i == LIFNAMSIZ) {
16266 		/* no null termination */
16267 		return (EINVAL);
16268 	}
16269 
16270 	/*
16271 	 * Calculate the namelen exclusive of the null
16272 	 * termination character.
16273 	 */
16274 	namelen = tmp - groupname;
16275 
16276 	ill_v4 = phyi->phyint_illv4;
16277 	ill_v6 = phyi->phyint_illv6;
16278 
16279 	/*
16280 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16281 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16282 	 * synchronization notes in ip.c
16283 	 */
16284 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16285 		return (EINVAL);
16286 	}
16287 
16288 	/*
16289 	 * mark the ill as changing.
16290 	 * this should queue all new requests on the syncq.
16291 	 */
16292 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16293 
16294 	if (ill_v4 != NULL)
16295 		ill_v4->ill_state_flags |= ILL_CHANGING;
16296 	if (ill_v6 != NULL)
16297 		ill_v6->ill_state_flags |= ILL_CHANGING;
16298 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16299 
16300 	if (namelen == 0) {
16301 		/*
16302 		 * Null string means remove this interface from the
16303 		 * existing group.
16304 		 */
16305 		if (phyi->phyint_groupname_len == 0) {
16306 			/*
16307 			 * Never was in a group.
16308 			 */
16309 			err = 0;
16310 			goto done;
16311 		}
16312 
16313 		/*
16314 		 * IPv4 or IPv6 may be temporarily out of the group when all
16315 		 * the ipifs are down. Thus, we need to check for ill_group to
16316 		 * be non-NULL.
16317 		 */
16318 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16319 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16320 			mutex_enter(&ill_v4->ill_lock);
16321 			if (!ill_is_quiescent(ill_v4)) {
16322 				/*
16323 				 * ipsq_pending_mp_add will not fail since
16324 				 * connp is NULL
16325 				 */
16326 				(void) ipsq_pending_mp_add(NULL,
16327 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16328 				mutex_exit(&ill_v4->ill_lock);
16329 				err = EINPROGRESS;
16330 				goto done;
16331 			}
16332 			mutex_exit(&ill_v4->ill_lock);
16333 		}
16334 
16335 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16336 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16337 			mutex_enter(&ill_v6->ill_lock);
16338 			if (!ill_is_quiescent(ill_v6)) {
16339 				(void) ipsq_pending_mp_add(NULL,
16340 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16341 				mutex_exit(&ill_v6->ill_lock);
16342 				err = EINPROGRESS;
16343 				goto done;
16344 			}
16345 			mutex_exit(&ill_v6->ill_lock);
16346 		}
16347 
16348 		rw_enter(&ill_g_lock, RW_WRITER);
16349 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16350 		mutex_enter(&phyi->phyint_lock);
16351 		ASSERT(phyi->phyint_groupname != NULL);
16352 		mi_free(phyi->phyint_groupname);
16353 		phyi->phyint_groupname = NULL;
16354 		phyi->phyint_groupname_len = 0;
16355 		mutex_exit(&phyi->phyint_lock);
16356 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16357 		rw_exit(&ill_g_lock);
16358 		err = ill_up_ipifs(ill, q, mp);
16359 
16360 		/*
16361 		 * set the split flag so that the ipsq can be split
16362 		 */
16363 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16364 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16365 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16366 
16367 	} else {
16368 		if (phyi->phyint_groupname_len != 0) {
16369 			ASSERT(phyi->phyint_groupname != NULL);
16370 			/* Are we inserting in the same group ? */
16371 			if (mi_strcmp(groupname,
16372 			    phyi->phyint_groupname) == 0) {
16373 				err = 0;
16374 				goto done;
16375 			}
16376 		}
16377 
16378 		rw_enter(&ill_g_lock, RW_READER);
16379 		/*
16380 		 * Merge ipsq for the group's.
16381 		 * This check is here as multiple groups/ills might be
16382 		 * sharing the same ipsq.
16383 		 * If we have to merege than the operation is restarted
16384 		 * on the new ipsq.
16385 		 */
16386 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL);
16387 		if (phyi->phyint_ipsq != ipsq) {
16388 			rw_exit(&ill_g_lock);
16389 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16390 			goto done;
16391 		}
16392 		/*
16393 		 * Running exclusive on new ipsq.
16394 		 */
16395 
16396 		ASSERT(ipsq != NULL);
16397 		ASSERT(ipsq->ipsq_writer == curthread);
16398 
16399 		/*
16400 		 * Check whether the ill_type and ill_net_type matches before
16401 		 * we allocate any memory so that the cleanup is easier.
16402 		 *
16403 		 * We can't group dissimilar ones as we can't load spread
16404 		 * packets across the group because of potential link-level
16405 		 * header differences.
16406 		 */
16407 		phyi_tmp = phyint_lookup_group(groupname);
16408 		if (phyi_tmp != NULL) {
16409 			if ((ill_v4 != NULL &&
16410 			    phyi_tmp->phyint_illv4 != NULL) &&
16411 			    ((ill_v4->ill_net_type !=
16412 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16413 			    (ill_v4->ill_type !=
16414 			    phyi_tmp->phyint_illv4->ill_type))) {
16415 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16416 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16417 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16418 				rw_exit(&ill_g_lock);
16419 				return (EINVAL);
16420 			}
16421 			if ((ill_v6 != NULL &&
16422 			    phyi_tmp->phyint_illv6 != NULL) &&
16423 			    ((ill_v6->ill_net_type !=
16424 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16425 			    (ill_v6->ill_type !=
16426 			    phyi_tmp->phyint_illv6->ill_type))) {
16427 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16428 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16429 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16430 				rw_exit(&ill_g_lock);
16431 				return (EINVAL);
16432 			}
16433 		}
16434 
16435 		rw_exit(&ill_g_lock);
16436 
16437 		/*
16438 		 * bring down all v4 ipifs.
16439 		 */
16440 		if (ill_v4 != NULL) {
16441 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16442 		}
16443 
16444 		/*
16445 		 * bring down all v6 ipifs.
16446 		 */
16447 		if (ill_v6 != NULL) {
16448 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16449 		}
16450 
16451 		/*
16452 		 * make sure all ipifs are down and there are no active
16453 		 * references. Call to ipsq_pending_mp_add will not fail
16454 		 * since connp is NULL.
16455 		 */
16456 		if (ill_v4 != NULL) {
16457 			mutex_enter(&ill_v4->ill_lock);
16458 			if (!ill_is_quiescent(ill_v4)) {
16459 				(void) ipsq_pending_mp_add(NULL,
16460 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16461 				mutex_exit(&ill_v4->ill_lock);
16462 				err = EINPROGRESS;
16463 				goto done;
16464 			}
16465 			mutex_exit(&ill_v4->ill_lock);
16466 		}
16467 
16468 		if (ill_v6 != NULL) {
16469 			mutex_enter(&ill_v6->ill_lock);
16470 			if (!ill_is_quiescent(ill_v6)) {
16471 				(void) ipsq_pending_mp_add(NULL,
16472 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16473 				mutex_exit(&ill_v6->ill_lock);
16474 				err = EINPROGRESS;
16475 				goto done;
16476 			}
16477 			mutex_exit(&ill_v6->ill_lock);
16478 		}
16479 
16480 		/*
16481 		 * allocate including space for null terminator
16482 		 * before we insert.
16483 		 */
16484 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16485 		if (tmp == NULL)
16486 			return (ENOMEM);
16487 
16488 		rw_enter(&ill_g_lock, RW_WRITER);
16489 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16490 		mutex_enter(&phyi->phyint_lock);
16491 		if (phyi->phyint_groupname_len != 0) {
16492 			ASSERT(phyi->phyint_groupname != NULL);
16493 			mi_free(phyi->phyint_groupname);
16494 		}
16495 
16496 		/*
16497 		 * setup the new group name.
16498 		 */
16499 		phyi->phyint_groupname = tmp;
16500 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16501 		phyi->phyint_groupname_len = namelen + 1;
16502 		mutex_exit(&phyi->phyint_lock);
16503 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16504 		rw_exit(&ill_g_lock);
16505 
16506 		err = ill_up_ipifs(ill, q, mp);
16507 	}
16508 
16509 done:
16510 	/*
16511 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16512 	 */
16513 	if (err != EINPROGRESS) {
16514 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16515 		if (ill_v4 != NULL)
16516 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16517 		if (ill_v6 != NULL)
16518 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16519 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16520 	}
16521 	return (err);
16522 }
16523 
16524 /* ARGSUSED */
16525 int
16526 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16527     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16528 {
16529 	ill_t *ill;
16530 	phyint_t *phyi;
16531 	struct lifreq *lifr;
16532 	mblk_t	*mp1;
16533 
16534 	/* Existence verified in ip_wput_nondata */
16535 	mp1 = mp->b_cont->b_cont;
16536 	lifr = (struct lifreq *)mp1->b_rptr;
16537 	ill = ipif->ipif_ill;
16538 	phyi = ill->ill_phyint;
16539 
16540 	lifr->lifr_groupname[0] = '\0';
16541 	/*
16542 	 * ill_group may be null if all the interfaces
16543 	 * are down. But still, the phyint should always
16544 	 * hold the name.
16545 	 */
16546 	if (phyi->phyint_groupname_len != 0) {
16547 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16548 		    phyi->phyint_groupname_len);
16549 	}
16550 
16551 	return (0);
16552 }
16553 
16554 
16555 typedef struct conn_move_s {
16556 	ill_t	*cm_from_ill;
16557 	ill_t	*cm_to_ill;
16558 	int	cm_ifindex;
16559 } conn_move_t;
16560 
16561 /*
16562  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16563  */
16564 static void
16565 conn_move(conn_t *connp, caddr_t arg)
16566 {
16567 	conn_move_t *connm;
16568 	int ifindex;
16569 	int i;
16570 	ill_t *from_ill;
16571 	ill_t *to_ill;
16572 	ilg_t *ilg;
16573 	ilm_t *ret_ilm;
16574 
16575 	connm = (conn_move_t *)arg;
16576 	ifindex = connm->cm_ifindex;
16577 	from_ill = connm->cm_from_ill;
16578 	to_ill = connm->cm_to_ill;
16579 
16580 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16581 
16582 	/* All multicast fields protected by conn_lock */
16583 	mutex_enter(&connp->conn_lock);
16584 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16585 	if ((connp->conn_outgoing_ill == from_ill) &&
16586 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16587 		connp->conn_outgoing_ill = to_ill;
16588 		connp->conn_incoming_ill = to_ill;
16589 	}
16590 
16591 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16592 
16593 	if ((connp->conn_multicast_ill == from_ill) &&
16594 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16595 		connp->conn_multicast_ill = connm->cm_to_ill;
16596 	}
16597 
16598 	/* Change IP_XMIT_IF associations */
16599 	if ((connp->conn_xmit_if_ill == from_ill) &&
16600 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
16601 		connp->conn_xmit_if_ill = to_ill;
16602 	}
16603 	/*
16604 	 * Change the ilg_ill to point to the new one. This assumes
16605 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16606 	 * has been told to receive packets on this interface.
16607 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16608 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16609 	 * some ilms may not have moved. We check to see whether
16610 	 * the ilms have moved to to_ill. We can't check on from_ill
16611 	 * as in the process of moving, we could have split an ilm
16612 	 * in to two - which has the same orig_ifindex and v6group.
16613 	 *
16614 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16615 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16616 	 */
16617 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16618 		ilg = &connp->conn_ilg[i];
16619 		if ((ilg->ilg_ill == from_ill) &&
16620 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16621 			/* ifindex != 0 indicates failback */
16622 			if (ifindex != 0) {
16623 				connp->conn_ilg[i].ilg_ill = to_ill;
16624 				continue;
16625 			}
16626 
16627 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16628 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16629 			    connp->conn_zoneid);
16630 
16631 			if (ret_ilm != NULL)
16632 				connp->conn_ilg[i].ilg_ill = to_ill;
16633 		}
16634 	}
16635 	mutex_exit(&connp->conn_lock);
16636 }
16637 
16638 static void
16639 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16640 {
16641 	conn_move_t connm;
16642 
16643 	connm.cm_from_ill = from_ill;
16644 	connm.cm_to_ill = to_ill;
16645 	connm.cm_ifindex = ifindex;
16646 
16647 	ipcl_walk(conn_move, (caddr_t)&connm);
16648 }
16649 
16650 /*
16651  * ilm has been moved from from_ill to to_ill.
16652  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16653  * appropriately.
16654  *
16655  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16656  *	  the code there de-references ipif_ill to get the ill to
16657  *	  send multicast requests. It does not work as ipif is on its
16658  *	  move and already moved when this function is called.
16659  *	  Thus, we need to use from_ill and to_ill send down multicast
16660  *	  requests.
16661  */
16662 static void
16663 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16664 {
16665 	ipif_t *ipif;
16666 	ilm_t *ilm;
16667 
16668 	/*
16669 	 * See whether we need to send down DL_ENABMULTI_REQ on
16670 	 * to_ill as ilm has just been added.
16671 	 */
16672 	ASSERT(IAM_WRITER_ILL(to_ill));
16673 	ASSERT(IAM_WRITER_ILL(from_ill));
16674 
16675 	ILM_WALKER_HOLD(to_ill);
16676 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16677 
16678 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16679 			continue;
16680 		/*
16681 		 * no locks held, ill/ipif cannot dissappear as long
16682 		 * as we are writer.
16683 		 */
16684 		ipif = to_ill->ill_ipif;
16685 		/*
16686 		 * No need to hold any lock as we are the writer and this
16687 		 * can only be changed by a writer.
16688 		 */
16689 		ilm->ilm_is_new = B_FALSE;
16690 
16691 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16692 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16693 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16694 			    "resolver\n"));
16695 			continue;		/* Must be IRE_IF_NORESOLVER */
16696 		}
16697 
16698 
16699 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16700 			ip1dbg(("ilm_send_multicast_reqs: "
16701 			    "to_ill MULTI_BCAST\n"));
16702 			goto from;
16703 		}
16704 
16705 		if (to_ill->ill_isv6)
16706 			mld_joingroup(ilm);
16707 		else
16708 			igmp_joingroup(ilm);
16709 
16710 		if (to_ill->ill_ipif_up_count == 0) {
16711 			/*
16712 			 * Nobody there. All multicast addresses will be
16713 			 * re-joined when we get the DL_BIND_ACK bringing the
16714 			 * interface up.
16715 			 */
16716 			ilm->ilm_notify_driver = B_FALSE;
16717 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16718 			goto from;
16719 		}
16720 
16721 		/*
16722 		 * For allmulti address, we want to join on only one interface.
16723 		 * Checking for ilm_numentries_v6 is not correct as you may
16724 		 * find an ilm with zero address on to_ill, but we may not
16725 		 * have nominated to_ill for receiving. Thus, if we have
16726 		 * nominated from_ill (ill_join_allmulti is set), nominate
16727 		 * only if to_ill is not already nominated (to_ill normally
16728 		 * should not have been nominated if "from_ill" has already
16729 		 * been nominated. As we don't prevent failovers from happening
16730 		 * across groups, we don't assert).
16731 		 */
16732 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16733 			/*
16734 			 * There is no need to hold ill locks as we are
16735 			 * writer on both ills and when ill_join_allmulti
16736 			 * is changed the thread is always a writer.
16737 			 */
16738 			if (from_ill->ill_join_allmulti &&
16739 			    !to_ill->ill_join_allmulti) {
16740 				(void) ip_join_allmulti(to_ill->ill_ipif);
16741 			}
16742 		} else if (ilm->ilm_notify_driver) {
16743 
16744 			/*
16745 			 * This is a newly moved ilm so we need to tell the
16746 			 * driver about the new group. There can be more than
16747 			 * one ilm's for the same group in the list each with a
16748 			 * different orig_ifindex. We have to inform the driver
16749 			 * once. In ilm_move_v[4,6] we only set the flag
16750 			 * ilm_notify_driver for the first ilm.
16751 			 */
16752 
16753 			(void) ip_ll_send_enabmulti_req(to_ill,
16754 			    &ilm->ilm_v6addr);
16755 		}
16756 
16757 		ilm->ilm_notify_driver = B_FALSE;
16758 
16759 		/*
16760 		 * See whether we need to send down DL_DISABMULTI_REQ on
16761 		 * from_ill as ilm has just been removed.
16762 		 */
16763 from:
16764 		ipif = from_ill->ill_ipif;
16765 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16766 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16767 			ip1dbg(("ilm_send_multicast_reqs: "
16768 			    "from_ill not resolver\n"));
16769 			continue;		/* Must be IRE_IF_NORESOLVER */
16770 		}
16771 
16772 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16773 			ip1dbg(("ilm_send_multicast_reqs: "
16774 			    "from_ill MULTI_BCAST\n"));
16775 			continue;
16776 		}
16777 
16778 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16779 			if (from_ill->ill_join_allmulti)
16780 			    (void) ip_leave_allmulti(from_ill->ill_ipif);
16781 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16782 			(void) ip_ll_send_disabmulti_req(from_ill,
16783 		    &ilm->ilm_v6addr);
16784 		}
16785 	}
16786 	ILM_WALKER_RELE(to_ill);
16787 }
16788 
16789 /*
16790  * This function is called when all multicast memberships needs
16791  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16792  * called only once unlike the IPv4 counterpart where it is called after
16793  * every logical interface is moved. The reason is due to multicast
16794  * memberships are joined using an interface address in IPv4 while in
16795  * IPv6, interface index is used.
16796  */
16797 static void
16798 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16799 {
16800 	ilm_t	*ilm;
16801 	ilm_t	*ilm_next;
16802 	ilm_t	*new_ilm;
16803 	ilm_t	**ilmp;
16804 	int	count;
16805 	char buf[INET6_ADDRSTRLEN];
16806 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16807 
16808 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16809 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16810 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
16811 
16812 	if (ifindex == 0) {
16813 		/*
16814 		 * Form the solicited node mcast address which is used later.
16815 		 */
16816 		ipif_t *ipif;
16817 
16818 		ipif = from_ill->ill_ipif;
16819 		ASSERT(ipif->ipif_id == 0);
16820 
16821 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16822 	}
16823 
16824 	ilmp = &from_ill->ill_ilm;
16825 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16826 		ilm_next = ilm->ilm_next;
16827 
16828 		if (ilm->ilm_flags & ILM_DELETED) {
16829 			ilmp = &ilm->ilm_next;
16830 			continue;
16831 		}
16832 
16833 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16834 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16835 		ASSERT(ilm->ilm_orig_ifindex != 0);
16836 		if (ilm->ilm_orig_ifindex == ifindex) {
16837 			/*
16838 			 * We are failing back multicast memberships.
16839 			 * If the same ilm exists in to_ill, it means somebody
16840 			 * has joined the same group there e.g. ff02::1
16841 			 * is joined within the kernel when the interfaces
16842 			 * came UP.
16843 			 */
16844 			ASSERT(ilm->ilm_ipif == NULL);
16845 			if (new_ilm != NULL) {
16846 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16847 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16848 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16849 					new_ilm->ilm_is_new = B_TRUE;
16850 				}
16851 			} else {
16852 				/*
16853 				 * check if we can just move the ilm
16854 				 */
16855 				if (from_ill->ill_ilm_walker_cnt != 0) {
16856 					/*
16857 					 * We have walkers we cannot move
16858 					 * the ilm, so allocate a new ilm,
16859 					 * this (old) ilm will be marked
16860 					 * ILM_DELETED at the end of the loop
16861 					 * and will be freed when the
16862 					 * last walker exits.
16863 					 */
16864 					new_ilm = (ilm_t *)mi_zalloc
16865 					    (sizeof (ilm_t));
16866 					if (new_ilm == NULL) {
16867 						ip0dbg(("ilm_move_v6: "
16868 						    "FAILBACK of IPv6"
16869 						    " multicast address %s : "
16870 						    "from %s to"
16871 						    " %s failed : ENOMEM \n",
16872 						    inet_ntop(AF_INET6,
16873 						    &ilm->ilm_v6addr, buf,
16874 						    sizeof (buf)),
16875 						    from_ill->ill_name,
16876 						    to_ill->ill_name));
16877 
16878 							ilmp = &ilm->ilm_next;
16879 							continue;
16880 					}
16881 					*new_ilm = *ilm;
16882 					/*
16883 					 * we don't want new_ilm linked to
16884 					 * ilm's filter list.
16885 					 */
16886 					new_ilm->ilm_filter = NULL;
16887 				} else {
16888 					/*
16889 					 * No walkers we can move the ilm.
16890 					 * lets take it out of the list.
16891 					 */
16892 					*ilmp = ilm->ilm_next;
16893 					ilm->ilm_next = NULL;
16894 					new_ilm = ilm;
16895 				}
16896 
16897 				/*
16898 				 * if this is the first ilm for the group
16899 				 * set ilm_notify_driver so that we notify the
16900 				 * driver in ilm_send_multicast_reqs.
16901 				 */
16902 				if (ilm_lookup_ill_v6(to_ill,
16903 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16904 					new_ilm->ilm_notify_driver = B_TRUE;
16905 
16906 				new_ilm->ilm_ill = to_ill;
16907 				/* Add to the to_ill's list */
16908 				new_ilm->ilm_next = to_ill->ill_ilm;
16909 				to_ill->ill_ilm = new_ilm;
16910 				/*
16911 				 * set the flag so that mld_joingroup is
16912 				 * called in ilm_send_multicast_reqs().
16913 				 */
16914 				new_ilm->ilm_is_new = B_TRUE;
16915 			}
16916 			goto bottom;
16917 		} else if (ifindex != 0) {
16918 			/*
16919 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16920 			 * has not matched above, look at the next ilm.
16921 			 */
16922 			ilmp = &ilm->ilm_next;
16923 			continue;
16924 		}
16925 		/*
16926 		 * If we are here, it means ifindex is 0. Failover
16927 		 * everything.
16928 		 *
16929 		 * We need to handle solicited node mcast address
16930 		 * and all_nodes mcast address differently as they
16931 		 * are joined witin the kenrel (ipif_multicast_up)
16932 		 * and potentially from the userland. We are called
16933 		 * after the ipifs of from_ill has been moved.
16934 		 * If we still find ilms on ill with solicited node
16935 		 * mcast address or all_nodes mcast address, it must
16936 		 * belong to the UP interface that has not moved e.g.
16937 		 * ipif_id 0 with the link local prefix does not move.
16938 		 * We join this on the new ill accounting for all the
16939 		 * userland memberships so that applications don't
16940 		 * see any failure.
16941 		 *
16942 		 * We need to make sure that we account only for the
16943 		 * solicited node and all node multicast addresses
16944 		 * that was brought UP on these. In the case of
16945 		 * a failover from A to B, we might have ilms belonging
16946 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
16947 		 * for the membership from the userland. If we are failing
16948 		 * over from B to C now, we will find the ones belonging
16949 		 * to A on B. These don't account for the ill_ipif_up_count.
16950 		 * They just move from B to C. The check below on
16951 		 * ilm_orig_ifindex ensures that.
16952 		 */
16953 		if ((ilm->ilm_orig_ifindex ==
16954 		    from_ill->ill_phyint->phyint_ifindex) &&
16955 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
16956 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
16957 		    &ilm->ilm_v6addr))) {
16958 			ASSERT(ilm->ilm_refcnt > 0);
16959 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
16960 			/*
16961 			 * For indentation reasons, we are not using a
16962 			 * "else" here.
16963 			 */
16964 			if (count == 0) {
16965 				ilmp = &ilm->ilm_next;
16966 				continue;
16967 			}
16968 			ilm->ilm_refcnt -= count;
16969 			if (new_ilm != NULL) {
16970 				/*
16971 				 * Can find one with the same
16972 				 * ilm_orig_ifindex, if we are failing
16973 				 * over to a STANDBY. This happens
16974 				 * when somebody wants to join a group
16975 				 * on a STANDBY interface and we
16976 				 * internally join on a different one.
16977 				 * If we had joined on from_ill then, a
16978 				 * failover now will find a new ilm
16979 				 * with this index.
16980 				 */
16981 				ip1dbg(("ilm_move_v6: FAILOVER, found"
16982 				    " new ilm on %s, group address %s\n",
16983 				    to_ill->ill_name,
16984 				    inet_ntop(AF_INET6,
16985 				    &ilm->ilm_v6addr, buf,
16986 				    sizeof (buf))));
16987 				new_ilm->ilm_refcnt += count;
16988 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16989 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16990 					new_ilm->ilm_is_new = B_TRUE;
16991 				}
16992 			} else {
16993 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
16994 				if (new_ilm == NULL) {
16995 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
16996 					    " multicast address %s : from %s to"
16997 					    " %s failed : ENOMEM \n",
16998 					    inet_ntop(AF_INET6,
16999 					    &ilm->ilm_v6addr, buf,
17000 					    sizeof (buf)), from_ill->ill_name,
17001 					    to_ill->ill_name));
17002 					ilmp = &ilm->ilm_next;
17003 					continue;
17004 				}
17005 				*new_ilm = *ilm;
17006 				new_ilm->ilm_filter = NULL;
17007 				new_ilm->ilm_refcnt = count;
17008 				new_ilm->ilm_timer = INFINITY;
17009 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17010 				new_ilm->ilm_is_new = B_TRUE;
17011 				/*
17012 				 * If the to_ill has not joined this
17013 				 * group we need to tell the driver in
17014 				 * ill_send_multicast_reqs.
17015 				 */
17016 				if (ilm_lookup_ill_v6(to_ill,
17017 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17018 					new_ilm->ilm_notify_driver = B_TRUE;
17019 
17020 				new_ilm->ilm_ill = to_ill;
17021 				/* Add to the to_ill's list */
17022 				new_ilm->ilm_next = to_ill->ill_ilm;
17023 				to_ill->ill_ilm = new_ilm;
17024 				ASSERT(new_ilm->ilm_ipif == NULL);
17025 			}
17026 			if (ilm->ilm_refcnt == 0) {
17027 				goto bottom;
17028 			} else {
17029 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17030 				CLEAR_SLIST(new_ilm->ilm_filter);
17031 				ilmp = &ilm->ilm_next;
17032 			}
17033 			continue;
17034 		} else {
17035 			/*
17036 			 * ifindex = 0 means, move everything pointing at
17037 			 * from_ill. We are doing this becuase ill has
17038 			 * either FAILED or became INACTIVE.
17039 			 *
17040 			 * As we would like to move things later back to
17041 			 * from_ill, we want to retain the identity of this
17042 			 * ilm. Thus, we don't blindly increment the reference
17043 			 * count on the ilms matching the address alone. We
17044 			 * need to match on the ilm_orig_index also. new_ilm
17045 			 * was obtained by matching ilm_orig_index also.
17046 			 */
17047 			if (new_ilm != NULL) {
17048 				/*
17049 				 * This is possible only if a previous restore
17050 				 * was incomplete i.e restore to
17051 				 * ilm_orig_ifindex left some ilms because
17052 				 * of some failures. Thus when we are failing
17053 				 * again, we might find our old friends there.
17054 				 */
17055 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17056 				    " on %s, group address %s\n",
17057 				    to_ill->ill_name,
17058 				    inet_ntop(AF_INET6,
17059 				    &ilm->ilm_v6addr, buf,
17060 				    sizeof (buf))));
17061 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17062 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17063 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17064 					new_ilm->ilm_is_new = B_TRUE;
17065 				}
17066 			} else {
17067 				if (from_ill->ill_ilm_walker_cnt != 0) {
17068 					new_ilm = (ilm_t *)
17069 					    mi_zalloc(sizeof (ilm_t));
17070 					if (new_ilm == NULL) {
17071 						ip0dbg(("ilm_move_v6: "
17072 						    "FAILOVER of IPv6"
17073 						    " multicast address %s : "
17074 						    "from %s to"
17075 						    " %s failed : ENOMEM \n",
17076 						    inet_ntop(AF_INET6,
17077 						    &ilm->ilm_v6addr, buf,
17078 						    sizeof (buf)),
17079 						    from_ill->ill_name,
17080 						    to_ill->ill_name));
17081 
17082 							ilmp = &ilm->ilm_next;
17083 							continue;
17084 					}
17085 					*new_ilm = *ilm;
17086 					new_ilm->ilm_filter = NULL;
17087 				} else {
17088 					*ilmp = ilm->ilm_next;
17089 					new_ilm = ilm;
17090 				}
17091 				/*
17092 				 * If the to_ill has not joined this
17093 				 * group we need to tell the driver in
17094 				 * ill_send_multicast_reqs.
17095 				 */
17096 				if (ilm_lookup_ill_v6(to_ill,
17097 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17098 					new_ilm->ilm_notify_driver = B_TRUE;
17099 
17100 				/* Add to the to_ill's list */
17101 				new_ilm->ilm_next = to_ill->ill_ilm;
17102 				to_ill->ill_ilm = new_ilm;
17103 				ASSERT(ilm->ilm_ipif == NULL);
17104 				new_ilm->ilm_ill = to_ill;
17105 				new_ilm->ilm_is_new = B_TRUE;
17106 			}
17107 
17108 		}
17109 
17110 bottom:
17111 		/*
17112 		 * Revert multicast filter state to (EXCLUDE, NULL).
17113 		 * new_ilm->ilm_is_new should already be set if needed.
17114 		 */
17115 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17116 		CLEAR_SLIST(new_ilm->ilm_filter);
17117 		/*
17118 		 * We allocated/got a new ilm, free the old one.
17119 		 */
17120 		if (new_ilm != ilm) {
17121 			if (from_ill->ill_ilm_walker_cnt == 0) {
17122 				*ilmp = ilm->ilm_next;
17123 				ilm->ilm_next = NULL;
17124 				FREE_SLIST(ilm->ilm_filter);
17125 				FREE_SLIST(ilm->ilm_pendsrcs);
17126 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17127 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17128 				mi_free((char *)ilm);
17129 			} else {
17130 				ilm->ilm_flags |= ILM_DELETED;
17131 				from_ill->ill_ilm_cleanup_reqd = 1;
17132 				ilmp = &ilm->ilm_next;
17133 			}
17134 		}
17135 	}
17136 }
17137 
17138 /*
17139  * Move all the multicast memberships to to_ill. Called when
17140  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17141  * different from IPv6 counterpart as multicast memberships are associated
17142  * with ills in IPv6. This function is called after every ipif is moved
17143  * unlike IPv6, where it is moved only once.
17144  */
17145 static void
17146 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17147 {
17148 	ilm_t	*ilm;
17149 	ilm_t	*ilm_next;
17150 	ilm_t	*new_ilm;
17151 	ilm_t	**ilmp;
17152 
17153 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17154 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17155 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
17156 
17157 	ilmp = &from_ill->ill_ilm;
17158 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17159 		ilm_next = ilm->ilm_next;
17160 
17161 		if (ilm->ilm_flags & ILM_DELETED) {
17162 			ilmp = &ilm->ilm_next;
17163 			continue;
17164 		}
17165 
17166 		ASSERT(ilm->ilm_ipif != NULL);
17167 
17168 		if (ilm->ilm_ipif != ipif) {
17169 			ilmp = &ilm->ilm_next;
17170 			continue;
17171 		}
17172 
17173 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17174 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17175 			/*
17176 			 * We joined this in ipif_multicast_up
17177 			 * and we never did an ipif_multicast_down
17178 			 * for IPv4. If nobody else from the userland
17179 			 * has reference, we free the ilm, and later
17180 			 * when this ipif comes up on the new ill,
17181 			 * we will join this again.
17182 			 */
17183 			if (--ilm->ilm_refcnt == 0)
17184 				goto delete_ilm;
17185 
17186 			new_ilm = ilm_lookup_ipif(ipif,
17187 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17188 			if (new_ilm != NULL) {
17189 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17190 				/*
17191 				 * We still need to deal with the from_ill.
17192 				 */
17193 				new_ilm->ilm_is_new = B_TRUE;
17194 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17195 				CLEAR_SLIST(new_ilm->ilm_filter);
17196 				goto delete_ilm;
17197 			}
17198 			/*
17199 			 * If we could not find one e.g. ipif is
17200 			 * still down on to_ill, we add this ilm
17201 			 * on ill_new to preserve the reference
17202 			 * count.
17203 			 */
17204 		}
17205 		/*
17206 		 * When ipifs move, ilms always move with it
17207 		 * to the NEW ill. Thus we should never be
17208 		 * able to find ilm till we really move it here.
17209 		 */
17210 		ASSERT(ilm_lookup_ipif(ipif,
17211 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17212 
17213 		if (from_ill->ill_ilm_walker_cnt != 0) {
17214 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17215 			if (new_ilm == NULL) {
17216 				char buf[INET6_ADDRSTRLEN];
17217 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17218 				    " multicast address %s : "
17219 				    "from %s to"
17220 				    " %s failed : ENOMEM \n",
17221 				    inet_ntop(AF_INET,
17222 				    &ilm->ilm_v6addr, buf,
17223 				    sizeof (buf)),
17224 				    from_ill->ill_name,
17225 				    to_ill->ill_name));
17226 
17227 				ilmp = &ilm->ilm_next;
17228 				continue;
17229 			}
17230 			*new_ilm = *ilm;
17231 			/* We don't want new_ilm linked to ilm's filter list */
17232 			new_ilm->ilm_filter = NULL;
17233 		} else {
17234 			/* Remove from the list */
17235 			*ilmp = ilm->ilm_next;
17236 			new_ilm = ilm;
17237 		}
17238 
17239 		/*
17240 		 * If we have never joined this group on the to_ill
17241 		 * make sure we tell the driver.
17242 		 */
17243 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17244 		    ALL_ZONES) == NULL)
17245 			new_ilm->ilm_notify_driver = B_TRUE;
17246 
17247 		/* Add to the to_ill's list */
17248 		new_ilm->ilm_next = to_ill->ill_ilm;
17249 		to_ill->ill_ilm = new_ilm;
17250 		new_ilm->ilm_is_new = B_TRUE;
17251 
17252 		/*
17253 		 * Revert multicast filter state to (EXCLUDE, NULL)
17254 		 */
17255 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17256 		CLEAR_SLIST(new_ilm->ilm_filter);
17257 
17258 		/*
17259 		 * Delete only if we have allocated a new ilm.
17260 		 */
17261 		if (new_ilm != ilm) {
17262 delete_ilm:
17263 			if (from_ill->ill_ilm_walker_cnt == 0) {
17264 				/* Remove from the list */
17265 				*ilmp = ilm->ilm_next;
17266 				ilm->ilm_next = NULL;
17267 				FREE_SLIST(ilm->ilm_filter);
17268 				FREE_SLIST(ilm->ilm_pendsrcs);
17269 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17270 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17271 				mi_free((char *)ilm);
17272 			} else {
17273 				ilm->ilm_flags |= ILM_DELETED;
17274 				from_ill->ill_ilm_cleanup_reqd = 1;
17275 				ilmp = &ilm->ilm_next;
17276 			}
17277 		}
17278 	}
17279 }
17280 
17281 static uint_t
17282 ipif_get_id(ill_t *ill, uint_t id)
17283 {
17284 	uint_t	unit;
17285 	ipif_t	*tipif;
17286 	boolean_t found = B_FALSE;
17287 
17288 	/*
17289 	 * During failback, we want to go back to the same id
17290 	 * instead of the smallest id so that the original
17291 	 * configuration is maintained. id is non-zero in that
17292 	 * case.
17293 	 */
17294 	if (id != 0) {
17295 		/*
17296 		 * While failing back, if we still have an ipif with
17297 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17298 		 * as soon as we return from this function. It was
17299 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17300 		 * we can choose the smallest id. Thus we return zero
17301 		 * in that case ignoring the hint.
17302 		 */
17303 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17304 			return (0);
17305 		for (tipif = ill->ill_ipif; tipif != NULL;
17306 		    tipif = tipif->ipif_next) {
17307 			if (tipif->ipif_id == id) {
17308 				found = B_TRUE;
17309 				break;
17310 			}
17311 		}
17312 		/*
17313 		 * If somebody already plumbed another logical
17314 		 * with the same id, we won't be able to find it.
17315 		 */
17316 		if (!found)
17317 			return (id);
17318 	}
17319 	for (unit = 0; unit <= ip_addrs_per_if; unit++) {
17320 		found = B_FALSE;
17321 		for (tipif = ill->ill_ipif; tipif != NULL;
17322 		    tipif = tipif->ipif_next) {
17323 			if (tipif->ipif_id == unit) {
17324 				found = B_TRUE;
17325 				break;
17326 			}
17327 		}
17328 		if (!found)
17329 			break;
17330 	}
17331 	return (unit);
17332 }
17333 
17334 /* ARGSUSED */
17335 static int
17336 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17337     ipif_t **rep_ipif_ptr)
17338 {
17339 	ill_t	*from_ill;
17340 	ipif_t	*rep_ipif;
17341 	ipif_t	**ipifp;
17342 	uint_t	unit;
17343 	int err = 0;
17344 	ipif_t	*to_ipif;
17345 	struct iocblk	*iocp;
17346 	boolean_t failback_cmd;
17347 	boolean_t remove_ipif;
17348 	int	rc;
17349 
17350 	ASSERT(IAM_WRITER_ILL(to_ill));
17351 	ASSERT(IAM_WRITER_IPIF(ipif));
17352 
17353 	iocp = (struct iocblk *)mp->b_rptr;
17354 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17355 	remove_ipif = B_FALSE;
17356 
17357 	from_ill = ipif->ipif_ill;
17358 
17359 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17360 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17361 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
17362 
17363 	/*
17364 	 * Don't move LINK LOCAL addresses as they are tied to
17365 	 * physical interface.
17366 	 */
17367 	if (from_ill->ill_isv6 &&
17368 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17369 		ipif->ipif_was_up = B_FALSE;
17370 		IPIF_UNMARK_MOVING(ipif);
17371 		return (0);
17372 	}
17373 
17374 	/*
17375 	 * We set the ipif_id to maximum so that the search for
17376 	 * ipif_id will pick the lowest number i.e 0 in the
17377 	 * following 2 cases :
17378 	 *
17379 	 * 1) We have a replacement ipif at the head of to_ill.
17380 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17381 	 *    on to_ill and hence the MOVE might fail. We want to
17382 	 *    remove it only if we could move the ipif. Thus, by
17383 	 *    setting it to the MAX value, we make the search in
17384 	 *    ipif_get_id return the zeroth id.
17385 	 *
17386 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17387 	 *    we might just have a zero address plumbed on the ipif
17388 	 *    with zero id in the case of IPv4. We remove that while
17389 	 *    doing the failback. We want to remove it only if we
17390 	 *    could move the ipif. Thus, by setting it to the MAX
17391 	 *    value, we make the search in ipif_get_id return the
17392 	 *    zeroth id.
17393 	 *
17394 	 * Both (1) and (2) are done only when when we are moving
17395 	 * an ipif (either due to failover/failback) which originally
17396 	 * belonged to this interface i.e the ipif_orig_ifindex is
17397 	 * the same as to_ill's ifindex. This is needed so that
17398 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17399 	 * from B -> A (B is being removed from the group) and
17400 	 * FAILBACK from A -> B restores the original configuration.
17401 	 * Without the check for orig_ifindex, the second FAILOVER
17402 	 * could make the ipif belonging to B replace the A's zeroth
17403 	 * ipif and the subsequent failback re-creating the replacement
17404 	 * ipif again.
17405 	 *
17406 	 * NOTE : We created the replacement ipif when we did a
17407 	 * FAILOVER (See below). We could check for FAILBACK and
17408 	 * then look for replacement ipif to be removed. But we don't
17409 	 * want to do that because we wan't to allow the possibility
17410 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17411 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17412 	 * from B -> A.
17413 	 */
17414 	to_ipif = to_ill->ill_ipif;
17415 	if ((to_ill->ill_phyint->phyint_ifindex ==
17416 	    ipif->ipif_orig_ifindex) &&
17417 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17418 		ASSERT(to_ipif->ipif_id == 0);
17419 		remove_ipif = B_TRUE;
17420 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17421 	}
17422 	/*
17423 	 * Find the lowest logical unit number on the to_ill.
17424 	 * If we are failing back, try to get the original id
17425 	 * rather than the lowest one so that the original
17426 	 * configuration is maintained.
17427 	 *
17428 	 * XXX need a better scheme for this.
17429 	 */
17430 	if (failback_cmd) {
17431 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17432 	} else {
17433 		unit = ipif_get_id(to_ill, 0);
17434 	}
17435 
17436 	/* Reset back to zero in case we fail below */
17437 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17438 		to_ipif->ipif_id = 0;
17439 
17440 	if (unit == ip_addrs_per_if) {
17441 		ipif->ipif_was_up = B_FALSE;
17442 		IPIF_UNMARK_MOVING(ipif);
17443 		return (EINVAL);
17444 	}
17445 
17446 	/*
17447 	 * ipif is ready to move from "from_ill" to "to_ill".
17448 	 *
17449 	 * 1) If we are moving ipif with id zero, create a
17450 	 *    replacement ipif for this ipif on from_ill. If this fails
17451 	 *    fail the MOVE operation.
17452 	 *
17453 	 * 2) Remove the replacement ipif on to_ill if any.
17454 	 *    We could remove the replacement ipif when we are moving
17455 	 *    the ipif with id zero. But what if somebody already
17456 	 *    unplumbed it ? Thus we always remove it if it is present.
17457 	 *    We want to do it only if we are sure we are going to
17458 	 *    move the ipif to to_ill which is why there are no
17459 	 *    returns due to error till ipif is linked to to_ill.
17460 	 *    Note that the first ipif that we failback will always
17461 	 *    be zero if it is present.
17462 	 */
17463 	if (ipif->ipif_id == 0) {
17464 		ipaddr_t inaddr_any = INADDR_ANY;
17465 
17466 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17467 		if (rep_ipif == NULL) {
17468 			ipif->ipif_was_up = B_FALSE;
17469 			IPIF_UNMARK_MOVING(ipif);
17470 			return (ENOMEM);
17471 		}
17472 		*rep_ipif = ipif_zero;
17473 		/*
17474 		 * Before we put the ipif on the list, store the addresses
17475 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17476 		 * assumes so. This logic is not any different from what
17477 		 * ipif_allocate does.
17478 		 */
17479 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17480 		    &rep_ipif->ipif_v6lcl_addr);
17481 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17482 		    &rep_ipif->ipif_v6src_addr);
17483 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17484 		    &rep_ipif->ipif_v6subnet);
17485 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17486 		    &rep_ipif->ipif_v6net_mask);
17487 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17488 		    &rep_ipif->ipif_v6brd_addr);
17489 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17490 		    &rep_ipif->ipif_v6pp_dst_addr);
17491 		/*
17492 		 * We mark IPIF_NOFAILOVER so that this can never
17493 		 * move.
17494 		 */
17495 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17496 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17497 		rep_ipif->ipif_replace_zero = B_TRUE;
17498 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17499 		    MUTEX_DEFAULT, NULL);
17500 		rep_ipif->ipif_id = 0;
17501 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17502 		rep_ipif->ipif_ill = from_ill;
17503 		rep_ipif->ipif_orig_ifindex =
17504 		    from_ill->ill_phyint->phyint_ifindex;
17505 		/* Insert at head */
17506 		rep_ipif->ipif_next = from_ill->ill_ipif;
17507 		from_ill->ill_ipif = rep_ipif;
17508 		/*
17509 		 * We don't really care to let apps know about
17510 		 * this interface.
17511 		 */
17512 	}
17513 
17514 	if (remove_ipif) {
17515 		/*
17516 		 * We set to a max value above for this case to get
17517 		 * id zero. ASSERT that we did get one.
17518 		 */
17519 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17520 		rep_ipif = to_ipif;
17521 		to_ill->ill_ipif = rep_ipif->ipif_next;
17522 		rep_ipif->ipif_next = NULL;
17523 		/*
17524 		 * If some apps scanned and find this interface,
17525 		 * it is time to let them know, so that they can
17526 		 * delete it.
17527 		 */
17528 
17529 		*rep_ipif_ptr = rep_ipif;
17530 	}
17531 
17532 	/* Get it out of the ILL interface list. */
17533 	ipifp = &ipif->ipif_ill->ill_ipif;
17534 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
17535 		if (*ipifp == ipif) {
17536 			*ipifp = ipif->ipif_next;
17537 			break;
17538 		}
17539 	}
17540 
17541 	/* Assign the new ill */
17542 	ipif->ipif_ill = to_ill;
17543 	ipif->ipif_id = unit;
17544 	/* id has already been checked */
17545 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17546 	ASSERT(rc == 0);
17547 	/* Let SCTP update its list */
17548 	sctp_move_ipif(ipif, from_ill, to_ill);
17549 	/*
17550 	 * Handle the failover and failback of ipif_t between
17551 	 * ill_t that have differing maximum mtu values.
17552 	 */
17553 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17554 		if (ipif->ipif_saved_mtu == 0) {
17555 			/*
17556 			 * As this ipif_t is moving to an ill_t
17557 			 * that has a lower ill_max_mtu, its
17558 			 * ipif_mtu needs to be saved so it can
17559 			 * be restored during failback or during
17560 			 * failover to an ill_t which has a
17561 			 * higher ill_max_mtu.
17562 			 */
17563 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17564 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17565 		} else {
17566 			/*
17567 			 * The ipif_t is, once again, moving to
17568 			 * an ill_t that has a lower maximum mtu
17569 			 * value.
17570 			 */
17571 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17572 		}
17573 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17574 	    ipif->ipif_saved_mtu != 0) {
17575 		/*
17576 		 * The mtu of this ipif_t had to be reduced
17577 		 * during an earlier failover; this is an
17578 		 * opportunity for it to be increased (either as
17579 		 * part of another failover or a failback).
17580 		 */
17581 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17582 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17583 			ipif->ipif_saved_mtu = 0;
17584 		} else {
17585 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17586 		}
17587 	}
17588 
17589 	/*
17590 	 * We preserve all the other fields of the ipif including
17591 	 * ipif_saved_ire_mp. The routes that are saved here will
17592 	 * be recreated on the new interface and back on the old
17593 	 * interface when we move back.
17594 	 */
17595 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17596 
17597 	return (err);
17598 }
17599 
17600 static int
17601 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17602     int ifindex, ipif_t **rep_ipif_ptr)
17603 {
17604 	ipif_t *mipif;
17605 	ipif_t *ipif_next;
17606 	int err;
17607 
17608 	/*
17609 	 * We don't really try to MOVE back things if some of the
17610 	 * operations fail. The daemon will take care of moving again
17611 	 * later on.
17612 	 */
17613 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17614 		ipif_next = mipif->ipif_next;
17615 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17616 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17617 
17618 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17619 
17620 			/*
17621 			 * When the MOVE fails, it is the job of the
17622 			 * application to take care of this properly
17623 			 * i.e try again if it is ENOMEM.
17624 			 */
17625 			if (mipif->ipif_ill != from_ill) {
17626 				/*
17627 				 * ipif has moved.
17628 				 *
17629 				 * Move the multicast memberships associated
17630 				 * with this ipif to the new ill. For IPv6, we
17631 				 * do it once after all the ipifs are moved
17632 				 * (in ill_move) as they are not associated
17633 				 * with ipifs.
17634 				 *
17635 				 * We need to move the ilms as the ipif has
17636 				 * already been moved to a new ill even
17637 				 * in the case of errors. Neither
17638 				 * ilm_free(ipif) will find the ilm
17639 				 * when somebody unplumbs this ipif nor
17640 				 * ilm_delete(ilm) will be able to find the
17641 				 * ilm, if we don't move now.
17642 				 */
17643 				if (!from_ill->ill_isv6)
17644 					ilm_move_v4(from_ill, to_ill, mipif);
17645 			}
17646 
17647 			if (err != 0)
17648 				return (err);
17649 		}
17650 	}
17651 	return (0);
17652 }
17653 
17654 static int
17655 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17656 {
17657 	int ifindex;
17658 	int err;
17659 	struct iocblk	*iocp;
17660 	ipif_t	*ipif;
17661 	ipif_t *rep_ipif_ptr = NULL;
17662 	ipif_t	*from_ipif = NULL;
17663 	boolean_t check_rep_if = B_FALSE;
17664 
17665 	iocp = (struct iocblk *)mp->b_rptr;
17666 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17667 		/*
17668 		 * Move everything pointing at from_ill to to_ill.
17669 		 * We acheive this by passing in 0 as ifindex.
17670 		 */
17671 		ifindex = 0;
17672 	} else {
17673 		/*
17674 		 * Move everything pointing at from_ill whose original
17675 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17676 		 * We acheive this by passing in ifindex rather than 0.
17677 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17678 		 */
17679 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17680 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17681 	}
17682 
17683 	/*
17684 	 * Determine if there is at least one ipif that would move from
17685 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17686 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17687 	 * the move, in which case we need to quiesce the replacement ipif also.
17688 	 */
17689 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17690 	    from_ipif = from_ipif->ipif_next) {
17691 		if (((ifindex == 0) ||
17692 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17693 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17694 			check_rep_if = B_TRUE;
17695 			break;
17696 		}
17697 	}
17698 
17699 
17700 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17701 
17702 	GRAB_ILL_LOCKS(from_ill, to_ill);
17703 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17704 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17705 		    mp, ILL_MOVE_OK);
17706 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17707 		return (EINPROGRESS);
17708 	}
17709 
17710 	/* Check if the replacement ipif is quiescent to delete */
17711 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17712 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17713 		to_ill->ill_ipif->ipif_state_flags |=
17714 		    IPIF_MOVING | IPIF_CHANGING;
17715 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17716 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17717 			    mp, ILL_MOVE_OK);
17718 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17719 			return (EINPROGRESS);
17720 		}
17721 	}
17722 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17723 
17724 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17725 	rw_enter(&ill_g_lock, RW_WRITER);
17726 	GRAB_ILL_LOCKS(from_ill, to_ill);
17727 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17728 
17729 	/* ilm_move is done inside ipif_move for IPv4 */
17730 	if (err == 0 && from_ill->ill_isv6)
17731 		ilm_move_v6(from_ill, to_ill, ifindex);
17732 
17733 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17734 	rw_exit(&ill_g_lock);
17735 
17736 	/*
17737 	 * send rts messages and multicast messages.
17738 	 */
17739 	if (rep_ipif_ptr != NULL) {
17740 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17741 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17742 			rep_ipif_ptr->ipif_recovery_id = 0;
17743 		}
17744 		ip_rts_ifmsg(rep_ipif_ptr);
17745 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17746 		IPIF_TRACE_CLEANUP(rep_ipif_ptr);
17747 		mi_free(rep_ipif_ptr);
17748 	}
17749 
17750 	conn_move_ill(from_ill, to_ill, ifindex);
17751 
17752 	return (err);
17753 }
17754 
17755 /*
17756  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17757  * Also checks for the validity of the arguments.
17758  * Note: We are already exclusive inside the from group.
17759  * It is upto the caller to release refcnt on the to_ill's.
17760  */
17761 static int
17762 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17763     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17764 {
17765 	int dst_index;
17766 	ipif_t *ipif_v4, *ipif_v6;
17767 	struct lifreq *lifr;
17768 	mblk_t *mp1;
17769 	boolean_t exists;
17770 	sin_t	*sin;
17771 	int	err = 0;
17772 
17773 	if ((mp1 = mp->b_cont) == NULL)
17774 		return (EPROTO);
17775 
17776 	if ((mp1 = mp1->b_cont) == NULL)
17777 		return (EPROTO);
17778 
17779 	lifr = (struct lifreq *)mp1->b_rptr;
17780 	sin = (sin_t *)&lifr->lifr_addr;
17781 
17782 	/*
17783 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17784 	 * specific operations.
17785 	 */
17786 	if (sin->sin_family != AF_UNSPEC)
17787 		return (EINVAL);
17788 
17789 	/*
17790 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17791 	 * NULLs for the last 4 args and we know the lookup won't fail
17792 	 * with EINPROGRESS.
17793 	 */
17794 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17795 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17796 	    ALL_ZONES, NULL, NULL, NULL, NULL);
17797 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17798 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17799 	    ALL_ZONES, NULL, NULL, NULL, NULL);
17800 
17801 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17802 		return (ENXIO);
17803 
17804 	if (ipif_v4 != NULL) {
17805 		ASSERT(ipif_v4->ipif_refcnt != 0);
17806 		if (ipif_v4->ipif_id != 0) {
17807 			err = EINVAL;
17808 			goto done;
17809 		}
17810 
17811 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17812 		*ill_from_v4 = ipif_v4->ipif_ill;
17813 	}
17814 
17815 	if (ipif_v6 != NULL) {
17816 		ASSERT(ipif_v6->ipif_refcnt != 0);
17817 		if (ipif_v6->ipif_id != 0) {
17818 			err = EINVAL;
17819 			goto done;
17820 		}
17821 
17822 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17823 		*ill_from_v6 = ipif_v6->ipif_ill;
17824 	}
17825 
17826 	err = 0;
17827 	dst_index = lifr->lifr_movetoindex;
17828 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17829 	    q, mp, ip_process_ioctl, &err);
17830 	if (err != 0) {
17831 		/*
17832 		 * There could be only v6.
17833 		 */
17834 		if (err != ENXIO)
17835 			goto done;
17836 		err = 0;
17837 	}
17838 
17839 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17840 	    q, mp, ip_process_ioctl, &err);
17841 	if (err != 0) {
17842 		if (err != ENXIO)
17843 			goto done;
17844 		if (*ill_to_v4 == NULL) {
17845 			err = ENXIO;
17846 			goto done;
17847 		}
17848 		err = 0;
17849 	}
17850 
17851 	/*
17852 	 * If we have something to MOVE i.e "from" not NULL,
17853 	 * "to" should be non-NULL.
17854 	 */
17855 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
17856 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
17857 		err = EINVAL;
17858 	}
17859 
17860 done:
17861 	if (ipif_v4 != NULL)
17862 		ipif_refrele(ipif_v4);
17863 	if (ipif_v6 != NULL)
17864 		ipif_refrele(ipif_v6);
17865 	return (err);
17866 }
17867 
17868 /*
17869  * FAILOVER and FAILBACK are modelled as MOVE operations.
17870  *
17871  * We don't check whether the MOVE is within the same group or
17872  * not, because this ioctl can be used as a generic mechanism
17873  * to failover from interface A to B, though things will function
17874  * only if they are really part of the same group. Moreover,
17875  * all ipifs may be down and hence temporarily out of the group.
17876  *
17877  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17878  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17879  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17880  * have been deleted and there are no active references. Once quiescent the
17881  * ipif's are moved and brought up on the new ill.
17882  *
17883  * Normally the source ill and destination ill belong to the same IPMP group
17884  * and hence the same ipsq_t. In the event they don't belong to the same
17885  * same group the two ipsq's are first merged into one ipsq - that of the
17886  * to_ill. The multicast memberships on the source and destination ill cannot
17887  * change during the move operation since multicast joins/leaves also have to
17888  * execute on the same ipsq and are hence serialized.
17889  */
17890 /* ARGSUSED */
17891 int
17892 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17893     ip_ioctl_cmd_t *ipip, void *ifreq)
17894 {
17895 	ill_t *ill_to_v4 = NULL;
17896 	ill_t *ill_to_v6 = NULL;
17897 	ill_t *ill_from_v4 = NULL;
17898 	ill_t *ill_from_v6 = NULL;
17899 	int err = 0;
17900 
17901 	/*
17902 	 * setup from and to ill's, we can get EINPROGRESS only for
17903 	 * to_ill's.
17904 	 */
17905 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
17906 	    &ill_to_v4, &ill_to_v6);
17907 
17908 	if (err != 0) {
17909 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
17910 		goto done;
17911 	}
17912 
17913 	/*
17914 	 * nothing to do.
17915 	 */
17916 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
17917 		goto done;
17918 	}
17919 
17920 	/*
17921 	 * nothing to do.
17922 	 */
17923 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
17924 		goto done;
17925 	}
17926 
17927 	/*
17928 	 * Mark the ill as changing.
17929 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
17930 	 * in ill_up_ipifs in case of error they are cleared below.
17931 	 */
17932 
17933 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17934 	if (ill_from_v4 != NULL)
17935 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
17936 	if (ill_from_v6 != NULL)
17937 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
17938 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17939 
17940 	/*
17941 	 * Make sure that both src and dst are
17942 	 * in the same syncq group. If not make it happen.
17943 	 * We are not holding any locks because we are the writer
17944 	 * on the from_ipsq and we will hold locks in ill_merge_groups
17945 	 * to protect to_ipsq against changing.
17946 	 */
17947 	if (ill_from_v4 != NULL) {
17948 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
17949 		    ill_to_v4->ill_phyint->phyint_ipsq) {
17950 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
17951 			    NULL, mp, q);
17952 			goto err_ret;
17953 
17954 		}
17955 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
17956 	} else {
17957 
17958 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
17959 		    ill_to_v6->ill_phyint->phyint_ipsq) {
17960 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
17961 			    NULL, mp, q);
17962 			goto err_ret;
17963 
17964 		}
17965 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
17966 	}
17967 
17968 	/*
17969 	 * Now that the ipsq's have been merged and we are the writer
17970 	 * lets mark to_ill as changing as well.
17971 	 */
17972 
17973 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
17974 	if (ill_to_v4 != NULL)
17975 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
17976 	if (ill_to_v6 != NULL)
17977 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
17978 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
17979 
17980 	/*
17981 	 * Its ok for us to proceed with the move even if
17982 	 * ill_pending_mp is non null on one of the from ill's as the reply
17983 	 * should not be looking at the ipif, it should only care about the
17984 	 * ill itself.
17985 	 */
17986 
17987 	/*
17988 	 * lets move ipv4 first.
17989 	 */
17990 	if (ill_from_v4 != NULL) {
17991 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
17992 		ill_from_v4->ill_move_in_progress = B_TRUE;
17993 		ill_to_v4->ill_move_in_progress = B_TRUE;
17994 		ill_to_v4->ill_move_peer = ill_from_v4;
17995 		ill_from_v4->ill_move_peer = ill_to_v4;
17996 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
17997 	}
17998 
17999 	/*
18000 	 * Now lets move ipv6.
18001 	 */
18002 	if (err == 0 && ill_from_v6 != NULL) {
18003 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18004 		ill_from_v6->ill_move_in_progress = B_TRUE;
18005 		ill_to_v6->ill_move_in_progress = B_TRUE;
18006 		ill_to_v6->ill_move_peer = ill_from_v6;
18007 		ill_from_v6->ill_move_peer = ill_to_v6;
18008 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18009 	}
18010 
18011 err_ret:
18012 	/*
18013 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18014 	 * moved to become quiescent.
18015 	 */
18016 	if (err == EINPROGRESS) {
18017 		goto done;
18018 	}
18019 
18020 	/*
18021 	 * if err is set ill_up_ipifs will not be called
18022 	 * lets clear the flags.
18023 	 */
18024 
18025 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18026 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18027 	/*
18028 	 * Some of the clearing may be redundant. But it is simple
18029 	 * not making any extra checks.
18030 	 */
18031 	if (ill_from_v6 != NULL) {
18032 		ill_from_v6->ill_move_in_progress = B_FALSE;
18033 		ill_from_v6->ill_move_peer = NULL;
18034 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18035 	}
18036 	if (ill_from_v4 != NULL) {
18037 		ill_from_v4->ill_move_in_progress = B_FALSE;
18038 		ill_from_v4->ill_move_peer = NULL;
18039 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18040 	}
18041 	if (ill_to_v6 != NULL) {
18042 		ill_to_v6->ill_move_in_progress = B_FALSE;
18043 		ill_to_v6->ill_move_peer = NULL;
18044 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18045 	}
18046 	if (ill_to_v4 != NULL) {
18047 		ill_to_v4->ill_move_in_progress = B_FALSE;
18048 		ill_to_v4->ill_move_peer = NULL;
18049 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18050 	}
18051 
18052 	/*
18053 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18054 	 * Do this always to maintain proper state i.e even in case of errors.
18055 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18056 	 * we need not call on both v4 and v6 interfaces.
18057 	 */
18058 	if (ill_from_v4 != NULL) {
18059 		if ((ill_from_v4->ill_phyint->phyint_flags &
18060 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18061 			phyint_inactive(ill_from_v4->ill_phyint);
18062 		}
18063 	} else if (ill_from_v6 != NULL) {
18064 		if ((ill_from_v6->ill_phyint->phyint_flags &
18065 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18066 			phyint_inactive(ill_from_v6->ill_phyint);
18067 		}
18068 	}
18069 
18070 	if (ill_to_v4 != NULL) {
18071 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18072 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18073 		}
18074 	} else if (ill_to_v6 != NULL) {
18075 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18076 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18077 		}
18078 	}
18079 
18080 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18081 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18082 
18083 no_err:
18084 	/*
18085 	 * lets bring the interfaces up on the to_ill.
18086 	 */
18087 	if (err == 0) {
18088 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18089 		    q, mp);
18090 	}
18091 
18092 	if (err == 0) {
18093 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18094 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18095 
18096 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18097 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18098 	}
18099 done:
18100 
18101 	if (ill_to_v4 != NULL) {
18102 		ill_refrele(ill_to_v4);
18103 	}
18104 	if (ill_to_v6 != NULL) {
18105 		ill_refrele(ill_to_v6);
18106 	}
18107 
18108 	return (err);
18109 }
18110 
18111 static void
18112 ill_dl_down(ill_t *ill)
18113 {
18114 	/*
18115 	 * The ill is down; unbind but stay attached since we're still
18116 	 * associated with a PPA. If we have negotiated DLPI capabilites
18117 	 * with the data link service provider (IDS_OK) then reset them.
18118 	 * The interval between unbinding and rebinding is potentially
18119 	 * unbounded hence we cannot assume things will be the same.
18120 	 * The DLPI capabilities will be probed again when the data link
18121 	 * is brought up.
18122 	 */
18123 	mblk_t	*mp = ill->ill_unbind_mp;
18124 	hook_nic_event_t *info;
18125 
18126 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18127 
18128 	ill->ill_unbind_mp = NULL;
18129 	if (mp != NULL) {
18130 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18131 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18132 		    ill->ill_name));
18133 		mutex_enter(&ill->ill_lock);
18134 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18135 		mutex_exit(&ill->ill_lock);
18136 		if (ill->ill_dlpi_capab_state == IDS_OK)
18137 			ill_capability_reset(ill);
18138 		ill_dlpi_send(ill, mp);
18139 	}
18140 
18141 	/*
18142 	 * Toss all of our multicast memberships.  We could keep them, but
18143 	 * then we'd have to do bookkeeping of any joins and leaves performed
18144 	 * by the application while the the interface is down (we can't just
18145 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18146 	 * on a downed interface).
18147 	 */
18148 	ill_leave_multicast(ill);
18149 
18150 	mutex_enter(&ill->ill_lock);
18151 
18152 	ill->ill_dl_up = 0;
18153 
18154 	if ((info = ill->ill_nic_event_info) != NULL) {
18155 		ip2dbg(("ill_dl_down:unexpected nic event %d attached for %s\n",
18156 		    info->hne_event, ill->ill_name));
18157 		if (info->hne_data != NULL)
18158 			kmem_free(info->hne_data, info->hne_datalen);
18159 		kmem_free(info, sizeof (hook_nic_event_t));
18160 	}
18161 
18162 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
18163 	if (info != NULL) {
18164 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
18165 		info->hne_lif = 0;
18166 		info->hne_event = NE_DOWN;
18167 		info->hne_data = NULL;
18168 		info->hne_datalen = 0;
18169 		info->hne_family = ill->ill_isv6 ? ipv6 : ipv4;
18170 	} else
18171 		ip2dbg(("ill_dl_down: could not attach DOWN nic event "
18172 		    "information for %s (ENOMEM)\n", ill->ill_name));
18173 
18174 	ill->ill_nic_event_info = info;
18175 
18176 	mutex_exit(&ill->ill_lock);
18177 }
18178 
18179 void
18180 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18181 {
18182 	union DL_primitives *dlp;
18183 	t_uscalar_t prim;
18184 
18185 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18186 
18187 	dlp = (union DL_primitives *)mp->b_rptr;
18188 	prim = dlp->dl_primitive;
18189 
18190 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18191 		dlpi_prim_str(prim), prim, ill->ill_name));
18192 
18193 	switch (prim) {
18194 	case DL_PHYS_ADDR_REQ:
18195 	{
18196 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18197 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18198 		break;
18199 	}
18200 	case DL_BIND_REQ:
18201 		mutex_enter(&ill->ill_lock);
18202 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18203 		mutex_exit(&ill->ill_lock);
18204 		break;
18205 	}
18206 
18207 	/*
18208 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18209 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18210 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18211 	 */
18212 	mutex_enter(&ill->ill_lock);
18213 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18214 	    (prim == DL_UNBIND_REQ)) {
18215 		ill->ill_dlpi_pending = prim;
18216 	}
18217 	mutex_exit(&ill->ill_lock);
18218 
18219 	/*
18220 	 * Some drivers send M_FLUSH up to IP as part of unbind
18221 	 * request.  When this M_FLUSH is sent back to the driver,
18222 	 * this can go after we send the detach request if the
18223 	 * M_FLUSH ends up in IP's syncq. To avoid that, we reply
18224 	 * to the M_FLUSH in ip_rput and locally generate another
18225 	 * M_FLUSH for the correctness.  This will get freed in
18226 	 * ip_wput_nondata.
18227 	 */
18228 	if (prim == DL_UNBIND_REQ)
18229 		(void) putnextctl1(ill->ill_rq, M_FLUSH, FLUSHRW);
18230 
18231 	putnext(ill->ill_wq, mp);
18232 }
18233 
18234 /*
18235  * Send a DLPI control message to the driver but make sure there
18236  * is only one outstanding message. Uses ill_dlpi_pending to tell
18237  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18238  * when an ACK or a NAK is received to process the next queued message.
18239  *
18240  * We don't protect ill_dlpi_pending with any lock. This is okay as
18241  * every place where its accessed, ip is exclusive while accessing
18242  * ill_dlpi_pending except when this function is called from ill_init()
18243  */
18244 void
18245 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18246 {
18247 	mblk_t **mpp;
18248 
18249 	ASSERT(IAM_WRITER_ILL(ill));
18250 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18251 
18252 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18253 		/* Must queue message. Tail insertion */
18254 		mpp = &ill->ill_dlpi_deferred;
18255 		while (*mpp != NULL)
18256 			mpp = &((*mpp)->b_next);
18257 
18258 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18259 		    ill->ill_name));
18260 
18261 		*mpp = mp;
18262 		return;
18263 	}
18264 
18265 	ill_dlpi_dispatch(ill, mp);
18266 }
18267 
18268 /*
18269  * Called when an DLPI control message has been acked or nacked to
18270  * send down the next queued message (if any).
18271  */
18272 void
18273 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18274 {
18275 	mblk_t *mp;
18276 
18277 	ASSERT(IAM_WRITER_ILL(ill));
18278 
18279 	ASSERT(prim != DL_PRIM_INVAL);
18280 	if (ill->ill_dlpi_pending != prim) {
18281 		if (ill->ill_dlpi_pending == DL_PRIM_INVAL) {
18282 			(void) mi_strlog(ill->ill_rq, 1,
18283 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
18284 			    "ill_dlpi_done: unsolicited ack for %s from %s\n",
18285 			    dlpi_prim_str(prim), ill->ill_name);
18286 		} else {
18287 			(void) mi_strlog(ill->ill_rq, 1,
18288 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
18289 			    "ill_dlpi_done: unexpected ack for %s from %s "
18290 			    "(expecting ack for %s)\n",
18291 			    dlpi_prim_str(prim), ill->ill_name,
18292 			    dlpi_prim_str(ill->ill_dlpi_pending));
18293 		}
18294 		return;
18295 	}
18296 
18297 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18298 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18299 
18300 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18301 		mutex_enter(&ill->ill_lock);
18302 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18303 		cv_signal(&ill->ill_cv);
18304 		mutex_exit(&ill->ill_lock);
18305 		return;
18306 	}
18307 
18308 	ill->ill_dlpi_deferred = mp->b_next;
18309 	mp->b_next = NULL;
18310 
18311 	ill_dlpi_dispatch(ill, mp);
18312 }
18313 
18314 void
18315 conn_delete_ire(conn_t *connp, caddr_t arg)
18316 {
18317 	ipif_t	*ipif = (ipif_t *)arg;
18318 	ire_t	*ire;
18319 
18320 	/*
18321 	 * Look at the cached ires on conns which has pointers to ipifs.
18322 	 * We just call ire_refrele which clears up the reference
18323 	 * to ire. Called when a conn closes. Also called from ipif_free
18324 	 * to cleanup indirect references to the stale ipif via the cached ire.
18325 	 */
18326 	mutex_enter(&connp->conn_lock);
18327 	ire = connp->conn_ire_cache;
18328 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18329 		connp->conn_ire_cache = NULL;
18330 		mutex_exit(&connp->conn_lock);
18331 		IRE_REFRELE_NOTR(ire);
18332 		return;
18333 	}
18334 	mutex_exit(&connp->conn_lock);
18335 
18336 }
18337 
18338 /*
18339  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18340  * of IREs. Those IREs may have been previously cached in the conn structure.
18341  * This ipcl_walk() walker function releases all references to such IREs based
18342  * on the condemned flag.
18343  */
18344 /* ARGSUSED */
18345 void
18346 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18347 {
18348 	ire_t	*ire;
18349 
18350 	mutex_enter(&connp->conn_lock);
18351 	ire = connp->conn_ire_cache;
18352 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18353 		connp->conn_ire_cache = NULL;
18354 		mutex_exit(&connp->conn_lock);
18355 		IRE_REFRELE_NOTR(ire);
18356 		return;
18357 	}
18358 	mutex_exit(&connp->conn_lock);
18359 }
18360 
18361 /*
18362  * Take down a specific interface, but don't lose any information about it.
18363  * Also delete interface from its interface group (ifgrp).
18364  * (Always called as writer.)
18365  * This function goes through the down sequence even if the interface is
18366  * already down. There are 2 reasons.
18367  * a. Currently we permit interface routes that depend on down interfaces
18368  *    to be added. This behaviour itself is questionable. However it appears
18369  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18370  *    time. We go thru the cleanup in order to remove these routes.
18371  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18372  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18373  *    down, but we need to cleanup i.e. do ill_dl_down and
18374  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18375  *
18376  * IP-MT notes:
18377  *
18378  * Model of reference to interfaces.
18379  *
18380  * The following members in ipif_t track references to the ipif.
18381  *	int     ipif_refcnt;    Active reference count
18382  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18383  * The following members in ill_t track references to the ill.
18384  *	int             ill_refcnt;     active refcnt
18385  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18386  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18387  *
18388  * Reference to an ipif or ill can be obtained in any of the following ways.
18389  *
18390  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18391  * Pointers to ipif / ill from other data structures viz ire and conn.
18392  * Implicit reference to the ipif / ill by holding a reference to the ire.
18393  *
18394  * The ipif/ill lookup functions return a reference held ipif / ill.
18395  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18396  * This is a purely dynamic reference count associated with threads holding
18397  * references to the ipif / ill. Pointers from other structures do not
18398  * count towards this reference count.
18399  *
18400  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
18401  * ipif/ill. This is incremented whenever a new ire is created referencing the
18402  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
18403  * actually added to the ire hash table. The count is decremented in
18404  * ire_inactive where the ire is destroyed.
18405  *
18406  * nce's reference ill's thru nce_ill and the count of nce's associated with
18407  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18408  * ndp_add() where the nce is actually added to the table. Similarly it is
18409  * decremented in ndp_inactive where the nce is destroyed.
18410  *
18411  * Flow of ioctls involving interface down/up
18412  *
18413  * The following is the sequence of an attempt to set some critical flags on an
18414  * up interface.
18415  * ip_sioctl_flags
18416  * ipif_down
18417  * wait for ipif to be quiescent
18418  * ipif_down_tail
18419  * ip_sioctl_flags_tail
18420  *
18421  * All set ioctls that involve down/up sequence would have a skeleton similar
18422  * to the above. All the *tail functions are called after the refcounts have
18423  * dropped to the appropriate values.
18424  *
18425  * The mechanism to quiesce an ipif is as follows.
18426  *
18427  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18428  * on the ipif. Callers either pass a flag requesting wait or the lookup
18429  *  functions will return NULL.
18430  *
18431  * Delete all ires referencing this ipif
18432  *
18433  * Any thread attempting to do an ipif_refhold on an ipif that has been
18434  * obtained thru a cached pointer will first make sure that
18435  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18436  * increment the refcount.
18437  *
18438  * The above guarantees that the ipif refcount will eventually come down to
18439  * zero and the ipif will quiesce, once all threads that currently hold a
18440  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18441  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18442  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
18443  * drop to zero.
18444  *
18445  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18446  *
18447  * Threads trying to lookup an ipif or ill can pass a flag requesting
18448  * wait and restart if the ipif / ill cannot be looked up currently.
18449  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18450  * failure if the ipif is currently undergoing an exclusive operation, and
18451  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18452  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18453  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18454  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18455  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18456  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18457  * until we release the ipsq_lock, even though the the ill/ipif state flags
18458  * can change after we drop the ill_lock.
18459  *
18460  * An attempt to send out a packet using an ipif that is currently
18461  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18462  * operation and restart it later when the exclusive condition on the ipif ends.
18463  * This is an example of not passing the wait flag to the lookup functions. For
18464  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18465  * out a multicast packet on that ipif will fail while the ipif is
18466  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18467  * currently IPIF_CHANGING will also fail.
18468  */
18469 int
18470 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18471 {
18472 	ill_t		*ill = ipif->ipif_ill;
18473 	phyint_t	*phyi;
18474 	conn_t		*connp;
18475 	boolean_t	success;
18476 	boolean_t	ipif_was_up = B_FALSE;
18477 
18478 	ASSERT(IAM_WRITER_IPIF(ipif));
18479 
18480 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18481 
18482 	if (ipif->ipif_flags & IPIF_UP) {
18483 		mutex_enter(&ill->ill_lock);
18484 		ipif->ipif_flags &= ~IPIF_UP;
18485 		ASSERT(ill->ill_ipif_up_count > 0);
18486 		--ill->ill_ipif_up_count;
18487 		mutex_exit(&ill->ill_lock);
18488 		ipif_was_up = B_TRUE;
18489 		/* Update status in SCTP's list */
18490 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18491 	}
18492 
18493 	/*
18494 	 * Blow away v6 memberships we established in ipif_multicast_up(); the
18495 	 * v4 ones are left alone (as is the ipif_multicast_up flag, so we
18496 	 * know not to rejoin when the interface is brought back up).
18497 	 */
18498 	if (ipif->ipif_isv6)
18499 		ipif_multicast_down(ipif);
18500 	/*
18501 	 * Remove from the mapping for __sin6_src_id. We insert only
18502 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18503 	 * stored as mapped addresses, we need to check for mapped
18504 	 * INADDR_ANY also.
18505 	 */
18506 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18507 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18508 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18509 		int err;
18510 
18511 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18512 		    ipif->ipif_zoneid);
18513 		if (err != 0) {
18514 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18515 		}
18516 	}
18517 
18518 	/*
18519 	 * Before we delete the ill from the group (if any), we need
18520 	 * to make sure that we delete all the routes dependent on
18521 	 * this and also any ipifs dependent on this ipif for
18522 	 * source address. We need to do before we delete from
18523 	 * the group because
18524 	 *
18525 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18526 	 *
18527 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18528 	 *    for re-doing source address selection. Note that
18529 	 *    ipif_select_source[_v6] called from
18530 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18531 	 *    because we have already marked down here i.e cleared
18532 	 *    IPIF_UP.
18533 	 */
18534 	if (ipif->ipif_isv6)
18535 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES);
18536 	else
18537 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES);
18538 
18539 	/*
18540 	 * Need to add these also to be saved and restored when the
18541 	 * ipif is brought down and up
18542 	 */
18543 	mutex_enter(&ire_mrtun_lock);
18544 	if (ire_mrtun_count != 0) {
18545 		mutex_exit(&ire_mrtun_lock);
18546 		ire_walk_ill_mrtun(0, 0, ipif_down_delete_ire,
18547 		    (char *)ipif, NULL);
18548 	} else {
18549 		mutex_exit(&ire_mrtun_lock);
18550 	}
18551 
18552 	mutex_enter(&ire_srcif_table_lock);
18553 	if (ire_srcif_table_count > 0) {
18554 		mutex_exit(&ire_srcif_table_lock);
18555 		ire_walk_srcif_table_v4(ipif_down_delete_ire, (char *)ipif);
18556 	} else {
18557 		mutex_exit(&ire_srcif_table_lock);
18558 	}
18559 
18560 	/*
18561 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18562 	 * ires have been deleted above. Otherwise a thread could end up
18563 	 * caching an ire in a conn after we have finished the cleanup of the
18564 	 * conn. The caching is done after making sure that the ire is not yet
18565 	 * condemned. Also documented in the block comment above ip_output
18566 	 */
18567 	ipcl_walk(conn_cleanup_stale_ire, NULL);
18568 	/* Also, delete the ires cached in SCTP */
18569 	sctp_ire_cache_flush(ipif);
18570 
18571 	/* Resolve any IPsec/IKE NAT-T instances that depend on this ipif. */
18572 	nattymod_clean_ipif(ipif);
18573 
18574 	/*
18575 	 * Update any other ipifs which have used "our" local address as
18576 	 * a source address. This entails removing and recreating IRE_INTERFACE
18577 	 * entries for such ipifs.
18578 	 */
18579 	if (ipif->ipif_isv6)
18580 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18581 	else
18582 		ipif_update_other_ipifs(ipif, ill->ill_group);
18583 
18584 	if (ipif_was_up) {
18585 		/*
18586 		 * Check whether it is last ipif to leave this group.
18587 		 * If this is the last ipif to leave, we should remove
18588 		 * this ill from the group as ipif_select_source will not
18589 		 * be able to find any useful ipifs if this ill is selected
18590 		 * for load balancing.
18591 		 *
18592 		 * For nameless groups, we should call ifgrp_delete if this
18593 		 * belongs to some group. As this ipif is going down, we may
18594 		 * need to reconstruct groups.
18595 		 */
18596 		phyi = ill->ill_phyint;
18597 		/*
18598 		 * If the phyint_groupname_len is 0, it may or may not
18599 		 * be in the nameless group. If the phyint_groupname_len is
18600 		 * not 0, then this ill should be part of some group.
18601 		 * As we always insert this ill in the group if
18602 		 * phyint_groupname_len is not zero when the first ipif
18603 		 * comes up (in ipif_up_done), it should be in a group
18604 		 * when the namelen is not 0.
18605 		 *
18606 		 * NOTE : When we delete the ill from the group,it will
18607 		 * blow away all the IRE_CACHES pointing either at this ipif or
18608 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18609 		 * should be pointing at this ill.
18610 		 */
18611 		ASSERT(phyi->phyint_groupname_len == 0 ||
18612 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18613 
18614 		if (phyi->phyint_groupname_len != 0) {
18615 			if (ill->ill_ipif_up_count == 0)
18616 				illgrp_delete(ill);
18617 		}
18618 
18619 		/*
18620 		 * If we have deleted some of the broadcast ires associated
18621 		 * with this ipif, we need to re-nominate somebody else if
18622 		 * the ires that we deleted were the nominated ones.
18623 		 */
18624 		if (ill->ill_group != NULL && !ill->ill_isv6)
18625 			ipif_renominate_bcast(ipif);
18626 	}
18627 
18628 	/*
18629 	 * neighbor-discovery or arp entries for this interface.
18630 	 */
18631 	ipif_ndp_down(ipif);
18632 
18633 	/*
18634 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18635 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18636 	 * and ill_delete -> ipif_free -> ipif_down
18637 	 */
18638 	if (mp == NULL) {
18639 		ASSERT(q == NULL);
18640 		return (0);
18641 	}
18642 
18643 	if (CONN_Q(q)) {
18644 		connp = Q_TO_CONN(q);
18645 		mutex_enter(&connp->conn_lock);
18646 	} else {
18647 		connp = NULL;
18648 	}
18649 	mutex_enter(&ill->ill_lock);
18650 	/*
18651 	 * Are there any ire's pointing to this ipif that are still active ?
18652 	 * If this is the last ipif going down, are there any ire's pointing
18653 	 * to this ill that are still active ?
18654 	 */
18655 	if (ipif_is_quiescent(ipif)) {
18656 		mutex_exit(&ill->ill_lock);
18657 		if (connp != NULL)
18658 			mutex_exit(&connp->conn_lock);
18659 		return (0);
18660 	}
18661 
18662 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18663 	    ill->ill_name, (void *)ill));
18664 	/*
18665 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18666 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18667 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18668 	 */
18669 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18670 	if (!success) {
18671 		/* The conn is closing. So just return */
18672 		ASSERT(connp != NULL);
18673 		mutex_exit(&ill->ill_lock);
18674 		mutex_exit(&connp->conn_lock);
18675 		return (EINTR);
18676 	}
18677 
18678 	mutex_exit(&ill->ill_lock);
18679 	if (connp != NULL)
18680 		mutex_exit(&connp->conn_lock);
18681 	return (EINPROGRESS);
18682 }
18683 
18684 void
18685 ipif_down_tail(ipif_t *ipif)
18686 {
18687 	ill_t	*ill = ipif->ipif_ill;
18688 
18689 	/*
18690 	 * Skip any loopback interface (null wq).
18691 	 * If this is the last logical interface on the ill
18692 	 * have ill_dl_down tell the driver we are gone (unbind)
18693 	 * Note that lun 0 can ipif_down even though
18694 	 * there are other logical units that are up.
18695 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18696 	 */
18697 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18698 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18699 	    ill->ill_dl_up) {
18700 		ill_dl_down(ill);
18701 	}
18702 	ill->ill_logical_down = 0;
18703 
18704 	/*
18705 	 * Have to be after removing the routes in ipif_down_delete_ire.
18706 	 */
18707 	if (ipif->ipif_isv6) {
18708 		if (ill->ill_flags & ILLF_XRESOLV)
18709 			ipif_arp_down(ipif);
18710 	} else {
18711 		ipif_arp_down(ipif);
18712 	}
18713 
18714 	ip_rts_ifmsg(ipif);
18715 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18716 }
18717 
18718 /*
18719  * Bring interface logically down without bringing the physical interface
18720  * down e.g. when the netmask is changed. This avoids long lasting link
18721  * negotiations between an ethernet interface and a certain switches.
18722  */
18723 static int
18724 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18725 {
18726 	/*
18727 	 * The ill_logical_down flag is a transient flag. It is set here
18728 	 * and is cleared once the down has completed in ipif_down_tail.
18729 	 * This flag does not indicate whether the ill stream is in the
18730 	 * DL_BOUND state with the driver. Instead this flag is used by
18731 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18732 	 * the driver. The state of the ill stream i.e. whether it is
18733 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18734 	 */
18735 	ipif->ipif_ill->ill_logical_down = 1;
18736 	return (ipif_down(ipif, q, mp));
18737 }
18738 
18739 /*
18740  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18741  * If the usesrc client ILL is already part of a usesrc group or not,
18742  * in either case a ire_stq with the matching usesrc client ILL will
18743  * locate the IRE's that need to be deleted. We want IREs to be created
18744  * with the new source address.
18745  */
18746 static void
18747 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18748 {
18749 	ill_t	*ucill = (ill_t *)ill_arg;
18750 
18751 	ASSERT(IAM_WRITER_ILL(ucill));
18752 
18753 	if (ire->ire_stq == NULL)
18754 		return;
18755 
18756 	if ((ire->ire_type == IRE_CACHE) &&
18757 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18758 		ire_delete(ire);
18759 }
18760 
18761 /*
18762  * ire_walk routine to delete every IRE dependent on the interface
18763  * address that is going down.	(Always called as writer.)
18764  * Works for both v4 and v6.
18765  * In addition for checking for ire_ipif matches it also checks for
18766  * IRE_CACHE entries which have the same source address as the
18767  * disappearing ipif since ipif_select_source might have picked
18768  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18769  * care of any IRE_INTERFACE with the disappearing source address.
18770  */
18771 static void
18772 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18773 {
18774 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18775 	ill_t *ire_ill;
18776 	ill_t *ipif_ill;
18777 
18778 	ASSERT(IAM_WRITER_IPIF(ipif));
18779 	if (ire->ire_ipif == NULL)
18780 		return;
18781 
18782 	/*
18783 	 * For IPv4, we derive source addresses for an IRE from ipif's
18784 	 * belonging to the same IPMP group as the IRE's outgoing
18785 	 * interface.  If an IRE's outgoing interface isn't in the
18786 	 * same IPMP group as a particular ipif, then that ipif
18787 	 * couldn't have been used as a source address for this IRE.
18788 	 *
18789 	 * For IPv6, source addresses are only restricted to the IPMP group
18790 	 * if the IRE is for a link-local address or a multicast address.
18791 	 * Otherwise, source addresses for an IRE can be chosen from
18792 	 * interfaces other than the the outgoing interface for that IRE.
18793 	 *
18794 	 * For source address selection details, see ipif_select_source()
18795 	 * and ipif_select_source_v6().
18796 	 */
18797 	if (ire->ire_ipversion == IPV4_VERSION ||
18798 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18799 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18800 		ire_ill = ire->ire_ipif->ipif_ill;
18801 		ipif_ill = ipif->ipif_ill;
18802 
18803 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18804 			return;
18805 		}
18806 	}
18807 
18808 
18809 	if (ire->ire_ipif != ipif) {
18810 		/*
18811 		 * Look for a matching source address.
18812 		 */
18813 		if (ire->ire_type != IRE_CACHE)
18814 			return;
18815 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18816 			return;
18817 
18818 		if (ire->ire_ipversion == IPV4_VERSION) {
18819 			if (ire->ire_src_addr != ipif->ipif_src_addr)
18820 				return;
18821 		} else {
18822 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
18823 			    &ipif->ipif_v6lcl_addr))
18824 				return;
18825 		}
18826 		ire_delete(ire);
18827 		return;
18828 	}
18829 	/*
18830 	 * ire_delete() will do an ire_flush_cache which will delete
18831 	 * all ire_ipif matches
18832 	 */
18833 	ire_delete(ire);
18834 }
18835 
18836 /*
18837  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
18838  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
18839  * 2) when an interface is brought up or down (on that ill).
18840  * This ensures that the IRE_CACHE entries don't retain stale source
18841  * address selection results.
18842  */
18843 void
18844 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
18845 {
18846 	ill_t	*ill = (ill_t *)ill_arg;
18847 	ill_t	*ipif_ill;
18848 
18849 	ASSERT(IAM_WRITER_ILL(ill));
18850 	/*
18851 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18852 	 * Hence this should be IRE_CACHE.
18853 	 */
18854 	ASSERT(ire->ire_type == IRE_CACHE);
18855 
18856 	/*
18857 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
18858 	 * We are only interested in IRE_CACHES that has borrowed
18859 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
18860 	 * for which we need to look at ire_ipif->ipif_ill match
18861 	 * with ill.
18862 	 */
18863 	ASSERT(ire->ire_ipif != NULL);
18864 	ipif_ill = ire->ire_ipif->ipif_ill;
18865 	if (ipif_ill == ill || (ill->ill_group != NULL &&
18866 	    ipif_ill->ill_group == ill->ill_group)) {
18867 		ire_delete(ire);
18868 	}
18869 }
18870 
18871 /*
18872  * Delete all the ire whose stq references ill_arg.
18873  */
18874 static void
18875 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
18876 {
18877 	ill_t	*ill = (ill_t *)ill_arg;
18878 	ill_t	*ire_ill;
18879 
18880 	ASSERT(IAM_WRITER_ILL(ill));
18881 	/*
18882 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18883 	 * Hence this should be IRE_CACHE.
18884 	 */
18885 	ASSERT(ire->ire_type == IRE_CACHE);
18886 
18887 	/*
18888 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18889 	 * matches ill. We are only interested in IRE_CACHES that
18890 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
18891 	 * filtering here.
18892 	 */
18893 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
18894 
18895 	if (ire_ill == ill)
18896 		ire_delete(ire);
18897 }
18898 
18899 /*
18900  * This is called when an ill leaves the group. We want to delete
18901  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
18902  * pointing at ill.
18903  */
18904 static void
18905 illgrp_cache_delete(ire_t *ire, char *ill_arg)
18906 {
18907 	ill_t	*ill = (ill_t *)ill_arg;
18908 
18909 	ASSERT(IAM_WRITER_ILL(ill));
18910 	ASSERT(ill->ill_group == NULL);
18911 	/*
18912 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18913 	 * Hence this should be IRE_CACHE.
18914 	 */
18915 	ASSERT(ire->ire_type == IRE_CACHE);
18916 	/*
18917 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18918 	 * matches ill. We are interested in both.
18919 	 */
18920 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
18921 	    (ire->ire_ipif->ipif_ill == ill));
18922 
18923 	ire_delete(ire);
18924 }
18925 
18926 /*
18927  * Initiate deallocate of an IPIF. Always called as writer. Called by
18928  * ill_delete or ip_sioctl_removeif.
18929  */
18930 static void
18931 ipif_free(ipif_t *ipif)
18932 {
18933 	ASSERT(IAM_WRITER_IPIF(ipif));
18934 
18935 	if (ipif->ipif_recovery_id != 0)
18936 		(void) untimeout(ipif->ipif_recovery_id);
18937 	ipif->ipif_recovery_id = 0;
18938 
18939 	/* Remove conn references */
18940 	reset_conn_ipif(ipif);
18941 
18942 	/*
18943 	 * Make sure we have valid net and subnet broadcast ire's for the
18944 	 * other ipif's which share them with this ipif.
18945 	 */
18946 	if (!ipif->ipif_isv6)
18947 		ipif_check_bcast_ires(ipif);
18948 
18949 	/*
18950 	 * Take down the interface. We can be called either from ill_delete
18951 	 * or from ip_sioctl_removeif.
18952 	 */
18953 	(void) ipif_down(ipif, NULL, NULL);
18954 
18955 	/*
18956 	 * Now that the interface is down, there's no chance it can still
18957 	 * become a duplicate.  Cancel any timer that may have been set while
18958 	 * tearing down.
18959 	 */
18960 	if (ipif->ipif_recovery_id != 0)
18961 		(void) untimeout(ipif->ipif_recovery_id);
18962 	ipif->ipif_recovery_id = 0;
18963 
18964 	rw_enter(&ill_g_lock, RW_WRITER);
18965 	/* Remove pointers to this ill in the multicast routing tables */
18966 	reset_mrt_vif_ipif(ipif);
18967 	rw_exit(&ill_g_lock);
18968 }
18969 
18970 /*
18971  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
18972  * also ill_move().
18973  */
18974 static void
18975 ipif_free_tail(ipif_t *ipif)
18976 {
18977 	mblk_t	*mp;
18978 	ipif_t	**ipifp;
18979 
18980 	/*
18981 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
18982 	 */
18983 	mutex_enter(&ipif->ipif_saved_ire_lock);
18984 	mp = ipif->ipif_saved_ire_mp;
18985 	ipif->ipif_saved_ire_mp = NULL;
18986 	mutex_exit(&ipif->ipif_saved_ire_lock);
18987 	freemsg(mp);
18988 
18989 	/*
18990 	 * Need to hold both ill_g_lock and ill_lock while
18991 	 * inserting or removing an ipif from the linked list
18992 	 * of ipifs hanging off the ill.
18993 	 */
18994 	rw_enter(&ill_g_lock, RW_WRITER);
18995 	/*
18996 	 * Remove all multicast memberships on the interface now.
18997 	 * This removes IPv4 multicast memberships joined within
18998 	 * the kernel as ipif_down does not do ipif_multicast_down
18999 	 * for IPv4. IPv6 is not handled here as the multicast memberships
19000 	 * are based on ill and not on ipif.
19001 	 */
19002 	ilm_free(ipif);
19003 
19004 	/*
19005 	 * Since we held the ill_g_lock while doing the ilm_free above,
19006 	 * we can assert the ilms were really deleted and not just marked
19007 	 * ILM_DELETED.
19008 	 */
19009 	ASSERT(ilm_walk_ipif(ipif) == 0);
19010 
19011 
19012 	IPIF_TRACE_CLEANUP(ipif);
19013 
19014 	/* Ask SCTP to take it out of it list */
19015 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19016 
19017 	mutex_enter(&ipif->ipif_ill->ill_lock);
19018 	/* Get it out of the ILL interface list. */
19019 	ipifp = &ipif->ipif_ill->ill_ipif;
19020 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
19021 		if (*ipifp == ipif) {
19022 			*ipifp = ipif->ipif_next;
19023 			break;
19024 		}
19025 	}
19026 
19027 	mutex_exit(&ipif->ipif_ill->ill_lock);
19028 	rw_exit(&ill_g_lock);
19029 
19030 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19031 
19032 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19033 	ASSERT(ipif->ipif_recovery_id == 0);
19034 
19035 	/* Free the memory. */
19036 	mi_free((char *)ipif);
19037 }
19038 
19039 /*
19040  * Returns an ipif name in the form "ill_name/unit" if ipif_id is not zero,
19041  * "ill_name" otherwise.
19042  */
19043 char *
19044 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19045 {
19046 	char	lbuf[32];
19047 	char	*name;
19048 	size_t	name_len;
19049 
19050 	buf[0] = '\0';
19051 	if (!ipif)
19052 		return (buf);
19053 	name = ipif->ipif_ill->ill_name;
19054 	name_len = ipif->ipif_ill->ill_name_length;
19055 	if (ipif->ipif_id != 0) {
19056 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19057 		    ipif->ipif_id);
19058 		name = lbuf;
19059 		name_len = mi_strlen(name) + 1;
19060 	}
19061 	len -= 1;
19062 	buf[len] = '\0';
19063 	len = MIN(len, name_len);
19064 	bcopy(name, buf, len);
19065 	return (buf);
19066 }
19067 
19068 /*
19069  * Find an IPIF based on the name passed in.  Names can be of the
19070  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19071  * The <phys> string can have forms like <dev><#> (e.g., le0),
19072  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19073  * When there is no colon, the implied unit id is zero. <phys> must
19074  * correspond to the name of an ILL.  (May be called as writer.)
19075  */
19076 static ipif_t *
19077 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19078     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19079     mblk_t *mp, ipsq_func_t func, int *error)
19080 {
19081 	char	*cp;
19082 	char	*endp;
19083 	long	id;
19084 	ill_t	*ill;
19085 	ipif_t	*ipif;
19086 	uint_t	ire_type;
19087 	boolean_t did_alloc = B_FALSE;
19088 	ipsq_t	*ipsq;
19089 
19090 	if (error != NULL)
19091 		*error = 0;
19092 
19093 	/*
19094 	 * If the caller wants to us to create the ipif, make sure we have a
19095 	 * valid zoneid
19096 	 */
19097 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19098 
19099 	if (namelen == 0) {
19100 		if (error != NULL)
19101 			*error = ENXIO;
19102 		return (NULL);
19103 	}
19104 
19105 	*exists = B_FALSE;
19106 	/* Look for a colon in the name. */
19107 	endp = &name[namelen];
19108 	for (cp = endp; --cp > name; ) {
19109 		if (*cp == IPIF_SEPARATOR_CHAR)
19110 			break;
19111 	}
19112 
19113 	if (*cp == IPIF_SEPARATOR_CHAR) {
19114 		/*
19115 		 * Reject any non-decimal aliases for logical
19116 		 * interfaces. Aliases with leading zeroes
19117 		 * are also rejected as they introduce ambiguity
19118 		 * in the naming of the interfaces.
19119 		 * In order to confirm with existing semantics,
19120 		 * and to not break any programs/script relying
19121 		 * on that behaviour, if<0>:0 is considered to be
19122 		 * a valid interface.
19123 		 *
19124 		 * If alias has two or more digits and the first
19125 		 * is zero, fail.
19126 		 */
19127 		if (&cp[2] < endp && cp[1] == '0')
19128 			return (NULL);
19129 	}
19130 
19131 	if (cp <= name) {
19132 		cp = endp;
19133 	} else {
19134 		*cp = '\0';
19135 	}
19136 
19137 	/*
19138 	 * Look up the ILL, based on the portion of the name
19139 	 * before the slash. ill_lookup_on_name returns a held ill.
19140 	 * Temporary to check whether ill exists already. If so
19141 	 * ill_lookup_on_name will clear it.
19142 	 */
19143 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19144 	    q, mp, func, error, &did_alloc);
19145 	if (cp != endp)
19146 		*cp = IPIF_SEPARATOR_CHAR;
19147 	if (ill == NULL)
19148 		return (NULL);
19149 
19150 	/* Establish the unit number in the name. */
19151 	id = 0;
19152 	if (cp < endp && *endp == '\0') {
19153 		/* If there was a colon, the unit number follows. */
19154 		cp++;
19155 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19156 			ill_refrele(ill);
19157 			if (error != NULL)
19158 				*error = ENXIO;
19159 			return (NULL);
19160 		}
19161 	}
19162 
19163 	GRAB_CONN_LOCK(q);
19164 	mutex_enter(&ill->ill_lock);
19165 	/* Now see if there is an IPIF with this unit number. */
19166 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19167 		if (ipif->ipif_id == id) {
19168 			if (zoneid != ALL_ZONES &&
19169 			    zoneid != ipif->ipif_zoneid &&
19170 			    ipif->ipif_zoneid != ALL_ZONES) {
19171 				mutex_exit(&ill->ill_lock);
19172 				RELEASE_CONN_LOCK(q);
19173 				ill_refrele(ill);
19174 				if (error != NULL)
19175 					*error = ENXIO;
19176 				return (NULL);
19177 			}
19178 			/*
19179 			 * The block comment at the start of ipif_down
19180 			 * explains the use of the macros used below
19181 			 */
19182 			if (IPIF_CAN_LOOKUP(ipif)) {
19183 				ipif_refhold_locked(ipif);
19184 				mutex_exit(&ill->ill_lock);
19185 				if (!did_alloc)
19186 					*exists = B_TRUE;
19187 				/*
19188 				 * Drop locks before calling ill_refrele
19189 				 * since it can potentially call into
19190 				 * ipif_ill_refrele_tail which can end up
19191 				 * in trying to acquire any lock.
19192 				 */
19193 				RELEASE_CONN_LOCK(q);
19194 				ill_refrele(ill);
19195 				return (ipif);
19196 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19197 				ipsq = ill->ill_phyint->phyint_ipsq;
19198 				mutex_enter(&ipsq->ipsq_lock);
19199 				mutex_exit(&ill->ill_lock);
19200 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19201 				mutex_exit(&ipsq->ipsq_lock);
19202 				RELEASE_CONN_LOCK(q);
19203 				ill_refrele(ill);
19204 				*error = EINPROGRESS;
19205 				return (NULL);
19206 			}
19207 		}
19208 	}
19209 	RELEASE_CONN_LOCK(q);
19210 
19211 	if (!do_alloc) {
19212 		mutex_exit(&ill->ill_lock);
19213 		ill_refrele(ill);
19214 		if (error != NULL)
19215 			*error = ENXIO;
19216 		return (NULL);
19217 	}
19218 
19219 	/*
19220 	 * If none found, atomically allocate and return a new one.
19221 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19222 	 * to support "receive only" use of lo0:1 etc. as is still done
19223 	 * below as an initial guess.
19224 	 * However, this is now likely to be overriden later in ipif_up_done()
19225 	 * when we know for sure what address has been configured on the
19226 	 * interface, since we might have more than one loopback interface
19227 	 * with a loopback address, e.g. in the case of zones, and all the
19228 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19229 	 */
19230 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19231 		ire_type = IRE_LOOPBACK;
19232 	else
19233 		ire_type = IRE_LOCAL;
19234 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19235 	if (ipif != NULL)
19236 		ipif_refhold_locked(ipif);
19237 	else if (error != NULL)
19238 		*error = ENOMEM;
19239 	mutex_exit(&ill->ill_lock);
19240 	ill_refrele(ill);
19241 	return (ipif);
19242 }
19243 
19244 /*
19245  * This routine is called whenever a new address comes up on an ipif.  If
19246  * we are configured to respond to address mask requests, then we are supposed
19247  * to broadcast an address mask reply at this time.  This routine is also
19248  * called if we are already up, but a netmask change is made.  This is legal
19249  * but might not make the system manager very popular.	(May be called
19250  * as writer.)
19251  */
19252 void
19253 ipif_mask_reply(ipif_t *ipif)
19254 {
19255 	icmph_t	*icmph;
19256 	ipha_t	*ipha;
19257 	mblk_t	*mp;
19258 
19259 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19260 
19261 	if (!ip_respond_to_address_mask_broadcast)
19262 		return;
19263 
19264 	/* ICMP mask reply is IPv4 only */
19265 	ASSERT(!ipif->ipif_isv6);
19266 	/* ICMP mask reply is not for a loopback interface */
19267 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19268 
19269 	mp = allocb(REPLY_LEN, BPRI_HI);
19270 	if (mp == NULL)
19271 		return;
19272 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19273 
19274 	ipha = (ipha_t *)mp->b_rptr;
19275 	bzero(ipha, REPLY_LEN);
19276 	*ipha = icmp_ipha;
19277 	ipha->ipha_ttl = ip_broadcast_ttl;
19278 	ipha->ipha_src = ipif->ipif_src_addr;
19279 	ipha->ipha_dst = ipif->ipif_brd_addr;
19280 	ipha->ipha_length = htons(REPLY_LEN);
19281 	ipha->ipha_ident = 0;
19282 
19283 	icmph = (icmph_t *)&ipha[1];
19284 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19285 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19286 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19287 	if (icmph->icmph_checksum == 0)
19288 		icmph->icmph_checksum = 0xffff;
19289 
19290 	put(ipif->ipif_wq, mp);
19291 
19292 #undef	REPLY_LEN
19293 }
19294 
19295 /*
19296  * When the mtu in the ipif changes, we call this routine through ire_walk
19297  * to update all the relevant IREs.
19298  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19299  */
19300 static void
19301 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19302 {
19303 	ipif_t *ipif = (ipif_t *)ipif_arg;
19304 
19305 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19306 		return;
19307 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19308 }
19309 
19310 /*
19311  * When the mtu in the ill changes, we call this routine through ire_walk
19312  * to update all the relevant IREs.
19313  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19314  */
19315 void
19316 ill_mtu_change(ire_t *ire, char *ill_arg)
19317 {
19318 	ill_t	*ill = (ill_t *)ill_arg;
19319 
19320 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19321 		return;
19322 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19323 }
19324 
19325 /*
19326  * Join the ipif specific multicast groups.
19327  * Must be called after a mapping has been set up in the resolver.  (Always
19328  * called as writer.)
19329  */
19330 void
19331 ipif_multicast_up(ipif_t *ipif)
19332 {
19333 	int err, index;
19334 	ill_t *ill;
19335 
19336 	ASSERT(IAM_WRITER_IPIF(ipif));
19337 
19338 	ill = ipif->ipif_ill;
19339 	index = ill->ill_phyint->phyint_ifindex;
19340 
19341 	ip1dbg(("ipif_multicast_up\n"));
19342 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19343 		return;
19344 
19345 	if (ipif->ipif_isv6) {
19346 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19347 			return;
19348 
19349 		/* Join the all hosts multicast address */
19350 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19351 		/*
19352 		 * Passing B_TRUE means we have to join the multicast
19353 		 * membership on this interface even though this is
19354 		 * FAILED. If we join on a different one in the group,
19355 		 * we will not be able to delete the membership later
19356 		 * as we currently don't track where we join when we
19357 		 * join within the kernel unlike applications where
19358 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19359 		 * for more on this.
19360 		 */
19361 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19362 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19363 		if (err != 0) {
19364 			ip0dbg(("ipif_multicast_up: "
19365 			    "all_hosts_mcast failed %d\n",
19366 			    err));
19367 			return;
19368 		}
19369 		/*
19370 		 * Enable multicast for the solicited node multicast address
19371 		 */
19372 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19373 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19374 
19375 			ipv6_multi.s6_addr32[3] |=
19376 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19377 
19378 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19379 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19380 			    NULL);
19381 			if (err != 0) {
19382 				ip0dbg(("ipif_multicast_up: solicited MC"
19383 				    " failed %d\n", err));
19384 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19385 				    ill, ill->ill_phyint->phyint_ifindex,
19386 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19387 				return;
19388 			}
19389 		}
19390 	} else {
19391 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19392 			return;
19393 
19394 		/* Join the all hosts multicast address */
19395 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19396 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19397 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19398 		if (err) {
19399 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19400 			return;
19401 		}
19402 	}
19403 	ipif->ipif_multicast_up = 1;
19404 }
19405 
19406 /*
19407  * Blow away any IPv6 multicast groups that we joined in ipif_multicast_up();
19408  * any explicit memberships are blown away in ill_leave_multicast() when the
19409  * ill is brought down.
19410  */
19411 static void
19412 ipif_multicast_down(ipif_t *ipif)
19413 {
19414 	int err;
19415 
19416 	ASSERT(IAM_WRITER_IPIF(ipif));
19417 
19418 	ip1dbg(("ipif_multicast_down\n"));
19419 	if (!ipif->ipif_multicast_up)
19420 		return;
19421 
19422 	ASSERT(ipif->ipif_isv6);
19423 
19424 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19425 
19426 	/*
19427 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19428 	 * we should look for ilms on this ill rather than the ones that have
19429 	 * been failed over here.  They are here temporarily. As
19430 	 * ipif_multicast_up has joined on this ill, we should delete only
19431 	 * from this ill.
19432 	 */
19433 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19434 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19435 	    B_TRUE, B_TRUE);
19436 	if (err != 0) {
19437 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19438 		    err));
19439 	}
19440 	/*
19441 	 * Disable multicast for the solicited node multicast address
19442 	 */
19443 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19444 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19445 
19446 		ipv6_multi.s6_addr32[3] |=
19447 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19448 
19449 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19450 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19451 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19452 
19453 		if (err != 0) {
19454 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19455 			    err));
19456 		}
19457 	}
19458 
19459 	ipif->ipif_multicast_up = 0;
19460 }
19461 
19462 /*
19463  * Used when an interface comes up to recreate any extra routes on this
19464  * interface.
19465  */
19466 static ire_t **
19467 ipif_recover_ire(ipif_t *ipif)
19468 {
19469 	mblk_t	*mp;
19470 	ire_t	**ipif_saved_irep;
19471 	ire_t	**irep;
19472 
19473 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19474 	    ipif->ipif_id));
19475 
19476 	mutex_enter(&ipif->ipif_saved_ire_lock);
19477 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19478 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19479 	if (ipif_saved_irep == NULL) {
19480 		mutex_exit(&ipif->ipif_saved_ire_lock);
19481 		return (NULL);
19482 	}
19483 
19484 	irep = ipif_saved_irep;
19485 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19486 		ire_t		*ire;
19487 		queue_t		*rfq;
19488 		queue_t		*stq;
19489 		ifrt_t		*ifrt;
19490 		uchar_t		*src_addr;
19491 		uchar_t		*gateway_addr;
19492 		mblk_t		*resolver_mp;
19493 		ushort_t	type;
19494 
19495 		/*
19496 		 * When the ire was initially created and then added in
19497 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19498 		 * in the case of a traditional interface route, or as one of
19499 		 * the IRE_OFFSUBNET types (with the exception of
19500 		 * IRE_HOST types ire which is created by icmp_redirect() and
19501 		 * which we don't need to save or recover).  In the case where
19502 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19503 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19504 		 * to satisfy software like GateD and Sun Cluster which creates
19505 		 * routes using the the loopback interface's address as a
19506 		 * gateway.
19507 		 *
19508 		 * As ifrt->ifrt_type reflects the already updated ire_type and
19509 		 * since ire_create() expects that IRE_IF_NORESOLVER will have
19510 		 * a valid nce_res_mp field (which doesn't make sense for a
19511 		 * IRE_LOOPBACK), ire_create() will be called in the same way
19512 		 * here as in ip_rt_add(), namely using ipif->ipif_net_type when
19513 		 * the route looks like a traditional interface route (where
19514 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19515 		 * the saved ifrt->ifrt_type.  This means that in the case where
19516 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19517 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19518 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19519 		 */
19520 		ifrt = (ifrt_t *)mp->b_rptr;
19521 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19522 			rfq = NULL;
19523 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19524 			    ? ipif->ipif_rq : ipif->ipif_wq;
19525 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19526 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19527 			    : (uint8_t *)&ipif->ipif_src_addr;
19528 			gateway_addr = NULL;
19529 			resolver_mp = ipif->ipif_resolver_mp;
19530 			type = ipif->ipif_net_type;
19531 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19532 			/* Recover multiroute broadcast IRE. */
19533 			rfq = ipif->ipif_rq;
19534 			stq = ipif->ipif_wq;
19535 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19536 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19537 			    : (uint8_t *)&ipif->ipif_src_addr;
19538 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19539 			resolver_mp = ipif->ipif_bcast_mp;
19540 			type = ifrt->ifrt_type;
19541 		} else {
19542 			rfq = NULL;
19543 			stq = NULL;
19544 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19545 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19546 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19547 			resolver_mp = NULL;
19548 			type = ifrt->ifrt_type;
19549 		}
19550 
19551 		/*
19552 		 * Create a copy of the IRE with the saved address and netmask.
19553 		 */
19554 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19555 		    "0x%x/0x%x\n",
19556 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19557 		    ntohl(ifrt->ifrt_addr),
19558 		    ntohl(ifrt->ifrt_mask)));
19559 		ire = ire_create(
19560 		    (uint8_t *)&ifrt->ifrt_addr,
19561 		    (uint8_t *)&ifrt->ifrt_mask,
19562 		    src_addr,
19563 		    gateway_addr,
19564 		    NULL,
19565 		    &ifrt->ifrt_max_frag,
19566 		    NULL,
19567 		    rfq,
19568 		    stq,
19569 		    type,
19570 		    resolver_mp,
19571 		    ipif,
19572 		    NULL,
19573 		    0,
19574 		    0,
19575 		    0,
19576 		    ifrt->ifrt_flags,
19577 		    &ifrt->ifrt_iulp_info,
19578 		    NULL,
19579 		    NULL);
19580 
19581 		if (ire == NULL) {
19582 			mutex_exit(&ipif->ipif_saved_ire_lock);
19583 			kmem_free(ipif_saved_irep,
19584 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19585 			return (NULL);
19586 		}
19587 
19588 		/*
19589 		 * Some software (for example, GateD and Sun Cluster) attempts
19590 		 * to create (what amount to) IRE_PREFIX routes with the
19591 		 * loopback address as the gateway.  This is primarily done to
19592 		 * set up prefixes with the RTF_REJECT flag set (for example,
19593 		 * when generating aggregate routes.)
19594 		 *
19595 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19596 		 * IRE_LOOPBACK, then we map the request into a
19597 		 * IRE_IF_NORESOLVER.
19598 		 */
19599 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19600 			ire->ire_type = IRE_IF_NORESOLVER;
19601 		/*
19602 		 * ire held by ire_add, will be refreled' towards the
19603 		 * the end of ipif_up_done
19604 		 */
19605 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19606 		*irep = ire;
19607 		irep++;
19608 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19609 	}
19610 	mutex_exit(&ipif->ipif_saved_ire_lock);
19611 	return (ipif_saved_irep);
19612 }
19613 
19614 /*
19615  * Used to set the netmask and broadcast address to default values when the
19616  * interface is brought up.  (Always called as writer.)
19617  */
19618 static void
19619 ipif_set_default(ipif_t *ipif)
19620 {
19621 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19622 
19623 	if (!ipif->ipif_isv6) {
19624 		/*
19625 		 * Interface holds an IPv4 address. Default
19626 		 * mask is the natural netmask.
19627 		 */
19628 		if (!ipif->ipif_net_mask) {
19629 			ipaddr_t	v4mask;
19630 
19631 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19632 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19633 		}
19634 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19635 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19636 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19637 		} else {
19638 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19639 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19640 		}
19641 		/*
19642 		 * NOTE: SunOS 4.X does this even if the broadcast address
19643 		 * has been already set thus we do the same here.
19644 		 */
19645 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19646 			ipaddr_t	v4addr;
19647 
19648 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19649 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19650 		}
19651 	} else {
19652 		/*
19653 		 * Interface holds an IPv6-only address.  Default
19654 		 * mask is all-ones.
19655 		 */
19656 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19657 			ipif->ipif_v6net_mask = ipv6_all_ones;
19658 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19659 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19660 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19661 		} else {
19662 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19663 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19664 		}
19665 	}
19666 }
19667 
19668 /*
19669  * Return 0 if this address can be used as local address without causing
19670  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19671  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19672  * Special checks are needed to allow the same IPv6 link-local address
19673  * on different ills.
19674  * TODO: allowing the same site-local address on different ill's.
19675  */
19676 int
19677 ip_addr_availability_check(ipif_t *new_ipif)
19678 {
19679 	in6_addr_t our_v6addr;
19680 	ill_t *ill;
19681 	ipif_t *ipif;
19682 	ill_walk_context_t ctx;
19683 
19684 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19685 	ASSERT(MUTEX_HELD(&ip_addr_avail_lock));
19686 	ASSERT(RW_READ_HELD(&ill_g_lock));
19687 
19688 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19689 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19690 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19691 		return (0);
19692 
19693 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19694 
19695 	if (new_ipif->ipif_isv6)
19696 		ill = ILL_START_WALK_V6(&ctx);
19697 	else
19698 		ill = ILL_START_WALK_V4(&ctx);
19699 
19700 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19701 		for (ipif = ill->ill_ipif; ipif != NULL;
19702 		    ipif = ipif->ipif_next) {
19703 			if ((ipif == new_ipif) ||
19704 			    !(ipif->ipif_flags & IPIF_UP) ||
19705 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19706 				continue;
19707 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19708 			    &our_v6addr)) {
19709 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19710 				    new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19711 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19712 				    ipif->ipif_flags |= IPIF_UNNUMBERED;
19713 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19714 				    new_ipif->ipif_ill != ill)
19715 					continue;
19716 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19717 				    new_ipif->ipif_ill != ill)
19718 					continue;
19719 				else if (new_ipif->ipif_zoneid !=
19720 				    ipif->ipif_zoneid &&
19721 				    ipif->ipif_zoneid != ALL_ZONES &&
19722 				    (ill->ill_phyint->phyint_flags &
19723 				    PHYI_LOOPBACK))
19724 					continue;
19725 				else if (new_ipif->ipif_ill == ill)
19726 					return (EADDRINUSE);
19727 				else
19728 					return (EADDRNOTAVAIL);
19729 			}
19730 		}
19731 	}
19732 
19733 	return (0);
19734 }
19735 
19736 /*
19737  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19738  * IREs for the ipif.
19739  * When the routine returns EINPROGRESS then mp has been consumed and
19740  * the ioctl will be acked from ip_rput_dlpi.
19741  */
19742 static int
19743 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19744 {
19745 	ill_t	*ill = ipif->ipif_ill;
19746 	boolean_t isv6 = ipif->ipif_isv6;
19747 	int	err = 0;
19748 	boolean_t success;
19749 
19750 	ASSERT(IAM_WRITER_IPIF(ipif));
19751 
19752 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19753 
19754 	/* Shouldn't get here if it is already up. */
19755 	if (ipif->ipif_flags & IPIF_UP)
19756 		return (EALREADY);
19757 
19758 	/* Skip arp/ndp for any loopback interface. */
19759 	if (ill->ill_wq != NULL) {
19760 		conn_t *connp = Q_TO_CONN(q);
19761 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19762 
19763 		if (!ill->ill_dl_up) {
19764 			/*
19765 			 * ill_dl_up is not yet set. i.e. we are yet to
19766 			 * DL_BIND with the driver and this is the first
19767 			 * logical interface on the ill to become "up".
19768 			 * Tell the driver to get going (via DL_BIND_REQ).
19769 			 * Note that changing "significant" IFF_ flags
19770 			 * address/netmask etc cause a down/up dance, but
19771 			 * does not cause an unbind (DL_UNBIND) with the driver
19772 			 */
19773 			return (ill_dl_up(ill, ipif, mp, q));
19774 		}
19775 
19776 		/*
19777 		 * ipif_resolver_up may end up sending an
19778 		 * AR_INTERFACE_UP message to ARP, which would, in
19779 		 * turn send a DLPI message to the driver. ioctls are
19780 		 * serialized and so we cannot send more than one
19781 		 * interface up message at a time. If ipif_resolver_up
19782 		 * does send an interface up message to ARP, we get
19783 		 * EINPROGRESS and we will complete in ip_arp_done.
19784 		 */
19785 
19786 		ASSERT(connp != NULL);
19787 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19788 		mutex_enter(&connp->conn_lock);
19789 		mutex_enter(&ill->ill_lock);
19790 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19791 		mutex_exit(&ill->ill_lock);
19792 		mutex_exit(&connp->conn_lock);
19793 		if (!success)
19794 			return (EINTR);
19795 
19796 		/*
19797 		 * Crank up IPv6 neighbor discovery
19798 		 * Unlike ARP, this should complete when
19799 		 * ipif_ndp_up returns. However, for
19800 		 * ILLF_XRESOLV interfaces we also send a
19801 		 * AR_INTERFACE_UP to the external resolver.
19802 		 * That ioctl will complete in ip_rput.
19803 		 */
19804 		if (isv6) {
19805 			err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr,
19806 			    B_FALSE);
19807 			if (err != 0) {
19808 				if (err != EINPROGRESS)
19809 					mp = ipsq_pending_mp_get(ipsq, &connp);
19810 				return (err);
19811 			}
19812 		}
19813 		/* Now, ARP */
19814 		err = ipif_resolver_up(ipif, Res_act_initial);
19815 		if (err == EINPROGRESS) {
19816 			/* We will complete it in ip_arp_done */
19817 			return (err);
19818 		}
19819 		mp = ipsq_pending_mp_get(ipsq, &connp);
19820 		ASSERT(mp != NULL);
19821 		if (err != 0)
19822 			return (err);
19823 	} else {
19824 		/*
19825 		 * Interfaces without underlying hardware don't do duplicate
19826 		 * address detection.
19827 		 */
19828 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
19829 		ipif->ipif_addr_ready = 1;
19830 	}
19831 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
19832 }
19833 
19834 /*
19835  * Perform a bind for the physical device.
19836  * When the routine returns EINPROGRESS then mp has been consumed and
19837  * the ioctl will be acked from ip_rput_dlpi.
19838  * Allocate an unbind message and save it until ipif_down.
19839  */
19840 static int
19841 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
19842 {
19843 	mblk_t	*areq_mp = NULL;
19844 	mblk_t	*bind_mp = NULL;
19845 	mblk_t	*unbind_mp = NULL;
19846 	conn_t	*connp;
19847 	boolean_t success;
19848 
19849 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
19850 	ASSERT(IAM_WRITER_ILL(ill));
19851 
19852 	ASSERT(mp != NULL);
19853 
19854 	/* Create a resolver cookie for ARP */
19855 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
19856 		areq_t		*areq;
19857 		uint16_t	sap_addr;
19858 
19859 		areq_mp = ill_arp_alloc(ill,
19860 			(uchar_t *)&ip_areq_template, 0);
19861 		if (areq_mp == NULL) {
19862 			return (ENOMEM);
19863 		}
19864 		freemsg(ill->ill_resolver_mp);
19865 		ill->ill_resolver_mp = areq_mp;
19866 		areq = (areq_t *)areq_mp->b_rptr;
19867 		sap_addr = ill->ill_sap;
19868 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
19869 		/*
19870 		 * Wait till we call ill_pending_mp_add to determine
19871 		 * the success before we free the ill_resolver_mp and
19872 		 * attach areq_mp in it's place.
19873 		 */
19874 	}
19875 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
19876 	    DL_BIND_REQ);
19877 	if (bind_mp == NULL)
19878 		goto bad;
19879 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
19880 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
19881 
19882 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
19883 	if (unbind_mp == NULL)
19884 		goto bad;
19885 
19886 	/*
19887 	 * Record state needed to complete this operation when the
19888 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
19889 	 */
19890 	if (WR(q)->q_next == NULL) {
19891 		connp = Q_TO_CONN(q);
19892 		mutex_enter(&connp->conn_lock);
19893 	} else {
19894 		connp = NULL;
19895 	}
19896 	mutex_enter(&ipif->ipif_ill->ill_lock);
19897 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19898 	mutex_exit(&ipif->ipif_ill->ill_lock);
19899 	if (connp != NULL)
19900 		mutex_exit(&connp->conn_lock);
19901 	if (!success)
19902 		goto bad;
19903 
19904 	/*
19905 	 * Save the unbind message for ill_dl_down(); it will be consumed when
19906 	 * the interface goes down.
19907 	 */
19908 	ASSERT(ill->ill_unbind_mp == NULL);
19909 	ill->ill_unbind_mp = unbind_mp;
19910 
19911 	ill_dlpi_send(ill, bind_mp);
19912 	/* Send down link-layer capabilities probe if not already done. */
19913 	ill_capability_probe(ill);
19914 
19915 	/*
19916 	 * Sysid used to rely on the fact that netboots set domainname
19917 	 * and the like. Now that miniroot boots aren't strictly netboots
19918 	 * and miniroot network configuration is driven from userland
19919 	 * these things still need to be set. This situation can be detected
19920 	 * by comparing the interface being configured here to the one
19921 	 * dhcack was set to reference by the boot loader. Once sysid is
19922 	 * converted to use dhcp_ipc_getinfo() this call can go away.
19923 	 */
19924 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
19925 	    (strcmp(ill->ill_name, dhcack) == 0) &&
19926 	    (strlen(srpc_domain) == 0)) {
19927 		if (dhcpinit() != 0)
19928 			cmn_err(CE_WARN, "no cached dhcp response");
19929 	}
19930 
19931 	/*
19932 	 * This operation will complete in ip_rput_dlpi with either
19933 	 * a DL_BIND_ACK or DL_ERROR_ACK.
19934 	 */
19935 	return (EINPROGRESS);
19936 bad:
19937 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
19938 	/*
19939 	 * We don't have to check for possible removal from illgrp
19940 	 * as we have not yet inserted in illgrp. For groups
19941 	 * without names, this ipif is still not UP and hence
19942 	 * this could not have possibly had any influence in forming
19943 	 * groups.
19944 	 */
19945 
19946 	if (bind_mp != NULL)
19947 		freemsg(bind_mp);
19948 	if (unbind_mp != NULL)
19949 		freemsg(unbind_mp);
19950 	return (ENOMEM);
19951 }
19952 
19953 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
19954 
19955 /*
19956  * DLPI and ARP is up.
19957  * Create all the IREs associated with an interface bring up multicast.
19958  * Set the interface flag and finish other initialization
19959  * that potentially had to be differed to after DL_BIND_ACK.
19960  */
19961 int
19962 ipif_up_done(ipif_t *ipif)
19963 {
19964 	ire_t	*ire_array[20];
19965 	ire_t	**irep = ire_array;
19966 	ire_t	**irep1;
19967 	ipaddr_t net_mask = 0;
19968 	ipaddr_t subnet_mask, route_mask;
19969 	ill_t	*ill = ipif->ipif_ill;
19970 	queue_t	*stq;
19971 	ipif_t	 *src_ipif;
19972 	ipif_t   *tmp_ipif;
19973 	boolean_t	flush_ire_cache = B_TRUE;
19974 	int	err = 0;
19975 	phyint_t *phyi;
19976 	ire_t	**ipif_saved_irep = NULL;
19977 	int ipif_saved_ire_cnt;
19978 	int	cnt;
19979 	boolean_t	src_ipif_held = B_FALSE;
19980 	boolean_t	ire_added = B_FALSE;
19981 	boolean_t	loopback = B_FALSE;
19982 
19983 	ip1dbg(("ipif_up_done(%s:%u)\n",
19984 		ipif->ipif_ill->ill_name, ipif->ipif_id));
19985 	/* Check if this is a loopback interface */
19986 	if (ipif->ipif_ill->ill_wq == NULL)
19987 		loopback = B_TRUE;
19988 
19989 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19990 	/*
19991 	 * If all other interfaces for this ill are down or DEPRECATED,
19992 	 * or otherwise unsuitable for source address selection, remove
19993 	 * any IRE_CACHE entries for this ill to make sure source
19994 	 * address selection gets to take this new ipif into account.
19995 	 * No need to hold ill_lock while traversing the ipif list since
19996 	 * we are writer
19997 	 */
19998 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
19999 		tmp_ipif = tmp_ipif->ipif_next) {
20000 		if (((tmp_ipif->ipif_flags &
20001 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20002 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20003 		    (tmp_ipif == ipif))
20004 			continue;
20005 		/* first useable pre-existing interface */
20006 		flush_ire_cache = B_FALSE;
20007 		break;
20008 	}
20009 	if (flush_ire_cache)
20010 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20011 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20012 
20013 	/*
20014 	 * Figure out which way the send-to queue should go.  Only
20015 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20016 	 * should show up here.
20017 	 */
20018 	switch (ill->ill_net_type) {
20019 	case IRE_IF_RESOLVER:
20020 		stq = ill->ill_rq;
20021 		break;
20022 	case IRE_IF_NORESOLVER:
20023 	case IRE_LOOPBACK:
20024 		stq = ill->ill_wq;
20025 		break;
20026 	default:
20027 		return (EINVAL);
20028 	}
20029 
20030 	if (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) {
20031 		/*
20032 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20033 		 * ipif_lookup_on_name(), but in the case of zones we can have
20034 		 * several loopback addresses on lo0. So all the interfaces with
20035 		 * loopback addresses need to be marked IRE_LOOPBACK.
20036 		 */
20037 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20038 		    htonl(INADDR_LOOPBACK))
20039 			ipif->ipif_ire_type = IRE_LOOPBACK;
20040 		else
20041 			ipif->ipif_ire_type = IRE_LOCAL;
20042 	}
20043 
20044 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20045 		/*
20046 		 * Can't use our source address. Select a different
20047 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20048 		 */
20049 		src_ipif = ipif_select_source(ipif->ipif_ill,
20050 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20051 		if (src_ipif == NULL)
20052 			src_ipif = ipif;	/* Last resort */
20053 		else
20054 			src_ipif_held = B_TRUE;
20055 	} else {
20056 		src_ipif = ipif;
20057 	}
20058 
20059 	/* Create all the IREs associated with this interface */
20060 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20061 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20062 
20063 		/*
20064 		 * If we're on a labeled system then make sure that zone-
20065 		 * private addresses have proper remote host database entries.
20066 		 */
20067 		if (is_system_labeled() &&
20068 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20069 		    !tsol_check_interface_address(ipif))
20070 			return (EINVAL);
20071 
20072 		/* Register the source address for __sin6_src_id */
20073 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20074 		    ipif->ipif_zoneid);
20075 		if (err != 0) {
20076 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20077 			return (err);
20078 		}
20079 
20080 		/* If the interface address is set, create the local IRE. */
20081 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20082 			(void *)ipif,
20083 			ipif->ipif_ire_type,
20084 			ntohl(ipif->ipif_lcl_addr)));
20085 		*irep++ = ire_create(
20086 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20087 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20088 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20089 		    NULL,				/* no gateway */
20090 		    NULL,
20091 		    &ip_loopback_mtuplus,		/* max frag size */
20092 		    NULL,
20093 		    ipif->ipif_rq,			/* recv-from queue */
20094 		    NULL,				/* no send-to queue */
20095 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20096 		    NULL,
20097 		    ipif,
20098 		    NULL,
20099 		    0,
20100 		    0,
20101 		    0,
20102 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20103 		    RTF_PRIVATE : 0,
20104 		    &ire_uinfo_null,
20105 		    NULL,
20106 		    NULL);
20107 	} else {
20108 		ip1dbg((
20109 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20110 		    ipif->ipif_ire_type,
20111 		    ntohl(ipif->ipif_lcl_addr),
20112 		    (uint_t)ipif->ipif_flags));
20113 	}
20114 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20115 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20116 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20117 	} else {
20118 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20119 	}
20120 
20121 	subnet_mask = ipif->ipif_net_mask;
20122 
20123 	/*
20124 	 * If mask was not specified, use natural netmask of
20125 	 * interface address. Also, store this mask back into the
20126 	 * ipif struct.
20127 	 */
20128 	if (subnet_mask == 0) {
20129 		subnet_mask = net_mask;
20130 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20131 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20132 		    ipif->ipif_v6subnet);
20133 	}
20134 
20135 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20136 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20137 	    ipif->ipif_subnet != INADDR_ANY) {
20138 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20139 
20140 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20141 			route_mask = IP_HOST_MASK;
20142 		} else {
20143 			route_mask = subnet_mask;
20144 		}
20145 
20146 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20147 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20148 			(void *)ipif, (void *)ill,
20149 			ill->ill_net_type,
20150 			ntohl(ipif->ipif_subnet)));
20151 		*irep++ = ire_create(
20152 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20153 		    (uchar_t *)&route_mask,		/* mask */
20154 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20155 		    NULL,				/* no gateway */
20156 		    NULL,
20157 		    &ipif->ipif_mtu,			/* max frag */
20158 		    NULL,
20159 		    NULL,				/* no recv queue */
20160 		    stq,				/* send-to queue */
20161 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20162 		    ill->ill_resolver_mp,		/* xmit header */
20163 		    ipif,
20164 		    NULL,
20165 		    0,
20166 		    0,
20167 		    0,
20168 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20169 		    &ire_uinfo_null,
20170 		    NULL,
20171 		    NULL);
20172 	}
20173 
20174 	/*
20175 	 * If the interface address is set, create the broadcast IREs.
20176 	 *
20177 	 * ire_create_bcast checks if the proposed new IRE matches
20178 	 * any existing IRE's with the same physical interface (ILL).
20179 	 * This should get rid of duplicates.
20180 	 * ire_create_bcast also check IPIF_NOXMIT and does not create
20181 	 * any broadcast ires.
20182 	 */
20183 	if ((ipif->ipif_subnet != INADDR_ANY) &&
20184 	    (ipif->ipif_flags & IPIF_BROADCAST)) {
20185 		ipaddr_t addr;
20186 
20187 		ip1dbg(("ipif_up_done: creating broadcast IRE\n"));
20188 		irep = ire_check_and_create_bcast(ipif, 0, irep,
20189 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20190 		irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep,
20191 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20192 
20193 		/*
20194 		 * For backward compatibility, we need to create net
20195 		 * broadcast ire's based on the old "IP address class
20196 		 * system."  The reason is that some old machines only
20197 		 * respond to these class derived net broadcast.
20198 		 *
20199 		 * But we should not create these net broadcast ire's if
20200 		 * the subnet_mask is shorter than the IP address class based
20201 		 * derived netmask.  Otherwise, we may create a net
20202 		 * broadcast address which is the same as an IP address
20203 		 * on the subnet.  Then TCP will refuse to talk to that
20204 		 * address.
20205 		 *
20206 		 * Nor do we need IRE_BROADCAST ire's for the interface
20207 		 * with the netmask as 0xFFFFFFFF, as IRE_LOCAL for that
20208 		 * interface is already created.  Creating these broadcast
20209 		 * ire's will only create confusion as the "addr" is going
20210 		 * to be same as that of the IP address of the interface.
20211 		 */
20212 		if (net_mask < subnet_mask) {
20213 			addr = net_mask & ipif->ipif_subnet;
20214 			irep = ire_check_and_create_bcast(ipif, addr, irep,
20215 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20216 			irep = ire_check_and_create_bcast(ipif,
20217 			    ~net_mask | addr, irep,
20218 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20219 		}
20220 
20221 		if (subnet_mask != 0xFFFFFFFF) {
20222 			addr = ipif->ipif_subnet;
20223 			irep = ire_check_and_create_bcast(ipif, addr, irep,
20224 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20225 			irep = ire_check_and_create_bcast(ipif,
20226 			    ~subnet_mask|addr, irep,
20227 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20228 		}
20229 	}
20230 
20231 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20232 
20233 	/* If an earlier ire_create failed, get out now */
20234 	for (irep1 = irep; irep1 > ire_array; ) {
20235 		irep1--;
20236 		if (*irep1 == NULL) {
20237 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20238 			err = ENOMEM;
20239 			goto bad;
20240 		}
20241 	}
20242 
20243 	/*
20244 	 * Need to atomically check for ip_addr_availablity_check
20245 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20246 	 * from group also.The ill_g_lock is grabbed as reader
20247 	 * just to make sure no new ills or new ipifs are being added
20248 	 * to the system while we are checking the uniqueness of addresses.
20249 	 */
20250 	rw_enter(&ill_g_lock, RW_READER);
20251 	mutex_enter(&ip_addr_avail_lock);
20252 	/* Mark it up, and increment counters. */
20253 	ipif->ipif_flags |= IPIF_UP;
20254 	ill->ill_ipif_up_count++;
20255 	err = ip_addr_availability_check(ipif);
20256 	mutex_exit(&ip_addr_avail_lock);
20257 	rw_exit(&ill_g_lock);
20258 
20259 	if (err != 0) {
20260 		/*
20261 		 * Our address may already be up on the same ill. In this case,
20262 		 * the ARP entry for our ipif replaced the one for the other
20263 		 * ipif. So we don't want to delete it (otherwise the other ipif
20264 		 * would be unable to send packets).
20265 		 * ip_addr_availability_check() identifies this case for us and
20266 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20267 		 * which is the expected error code.
20268 		 */
20269 		if (err == EADDRINUSE) {
20270 			freemsg(ipif->ipif_arp_del_mp);
20271 			ipif->ipif_arp_del_mp = NULL;
20272 			err = EADDRNOTAVAIL;
20273 		}
20274 		ill->ill_ipif_up_count--;
20275 		ipif->ipif_flags &= ~IPIF_UP;
20276 		goto bad;
20277 	}
20278 
20279 	/*
20280 	 * Add in all newly created IREs.  ire_create_bcast() has
20281 	 * already checked for duplicates of the IRE_BROADCAST type.
20282 	 * We want to add before we call ifgrp_insert which wants
20283 	 * to know whether IRE_IF_RESOLVER exists or not.
20284 	 *
20285 	 * NOTE : We refrele the ire though we may branch to "bad"
20286 	 *	  later on where we do ire_delete. This is okay
20287 	 *	  because nobody can delete it as we are running
20288 	 *	  exclusively.
20289 	 */
20290 	for (irep1 = irep; irep1 > ire_array; ) {
20291 		irep1--;
20292 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20293 		/*
20294 		 * refheld by ire_add. refele towards the end of the func
20295 		 */
20296 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20297 	}
20298 	ire_added = B_TRUE;
20299 	/*
20300 	 * Form groups if possible.
20301 	 *
20302 	 * If we are supposed to be in a ill_group with a name, insert it
20303 	 * now as we know that at least one ipif is UP. Otherwise form
20304 	 * nameless groups.
20305 	 *
20306 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20307 	 * this ipif into the appropriate interface group, or create a
20308 	 * new one. If this is already in a nameless group, we try to form
20309 	 * a bigger group looking at other ills potentially sharing this
20310 	 * ipif's prefix.
20311 	 */
20312 	phyi = ill->ill_phyint;
20313 	if (phyi->phyint_groupname_len != 0) {
20314 		ASSERT(phyi->phyint_groupname != NULL);
20315 		if (ill->ill_ipif_up_count == 1) {
20316 			ASSERT(ill->ill_group == NULL);
20317 			err = illgrp_insert(&illgrp_head_v4, ill,
20318 			    phyi->phyint_groupname, NULL, B_TRUE);
20319 			if (err != 0) {
20320 				ip1dbg(("ipif_up_done: illgrp allocation "
20321 				    "failed, error %d\n", err));
20322 				goto bad;
20323 			}
20324 		}
20325 		ASSERT(ill->ill_group != NULL);
20326 	}
20327 
20328 	/*
20329 	 * When this is part of group, we need to make sure that
20330 	 * any broadcast ires created because of this ipif coming
20331 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20332 	 * so that we don't receive duplicate broadcast packets.
20333 	 */
20334 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20335 		ipif_renominate_bcast(ipif);
20336 
20337 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20338 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20339 	ipif_saved_irep = ipif_recover_ire(ipif);
20340 
20341 	if (!loopback) {
20342 		/*
20343 		 * If the broadcast address has been set, make sure it makes
20344 		 * sense based on the interface address.
20345 		 * Only match on ill since we are sharing broadcast addresses.
20346 		 */
20347 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20348 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20349 			ire_t	*ire;
20350 
20351 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20352 			    IRE_BROADCAST, ipif, ALL_ZONES,
20353 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20354 
20355 			if (ire == NULL) {
20356 				/*
20357 				 * If there isn't a matching broadcast IRE,
20358 				 * revert to the default for this netmask.
20359 				 */
20360 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20361 				mutex_enter(&ipif->ipif_ill->ill_lock);
20362 				ipif_set_default(ipif);
20363 				mutex_exit(&ipif->ipif_ill->ill_lock);
20364 			} else {
20365 				ire_refrele(ire);
20366 			}
20367 		}
20368 
20369 	}
20370 
20371 	/* This is the first interface on this ill */
20372 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20373 		/*
20374 		 * Need to recover all multicast memberships in the driver.
20375 		 * This had to be deferred until we had attached.
20376 		 */
20377 		ill_recover_multicast(ill);
20378 	}
20379 	/* Join the allhosts multicast address */
20380 	ipif_multicast_up(ipif);
20381 
20382 	if (!loopback) {
20383 		/*
20384 		 * See whether anybody else would benefit from the
20385 		 * new ipif that we added. We call this always rather
20386 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20387 		 * ipif is for the benefit of illgrp_insert (done above)
20388 		 * which does not do source address selection as it does
20389 		 * not want to re-create interface routes that we are
20390 		 * having reference to it here.
20391 		 */
20392 		ill_update_source_selection(ill);
20393 	}
20394 
20395 	for (irep1 = irep; irep1 > ire_array; ) {
20396 		irep1--;
20397 		if (*irep1 != NULL) {
20398 			/* was held in ire_add */
20399 			ire_refrele(*irep1);
20400 		}
20401 	}
20402 
20403 	cnt = ipif_saved_ire_cnt;
20404 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20405 		if (*irep1 != NULL) {
20406 			/* was held in ire_add */
20407 			ire_refrele(*irep1);
20408 		}
20409 	}
20410 
20411 	if (!loopback && ipif->ipif_addr_ready) {
20412 		/* Broadcast an address mask reply. */
20413 		ipif_mask_reply(ipif);
20414 	}
20415 	if (ipif_saved_irep != NULL) {
20416 		kmem_free(ipif_saved_irep,
20417 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20418 	}
20419 	if (src_ipif_held)
20420 		ipif_refrele(src_ipif);
20421 
20422 	/*
20423 	 * This had to be deferred until we had bound.  Tell routing sockets and
20424 	 * others that this interface is up if it looks like the address has
20425 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20426 	 * duplicate address detection to do its thing.
20427 	 */
20428 	if (ipif->ipif_addr_ready) {
20429 		ip_rts_ifmsg(ipif);
20430 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20431 		/* Let SCTP update the status for this ipif */
20432 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20433 	}
20434 	return (0);
20435 
20436 bad:
20437 	ip1dbg(("ipif_up_done: FAILED \n"));
20438 	/*
20439 	 * We don't have to bother removing from ill groups because
20440 	 *
20441 	 * 1) For groups with names, we insert only when the first ipif
20442 	 *    comes up. In that case if it fails, it will not be in any
20443 	 *    group. So, we need not try to remove for that case.
20444 	 *
20445 	 * 2) For groups without names, either we tried to insert ipif_ill
20446 	 *    in a group as singleton or found some other group to become
20447 	 *    a bigger group. For the former, if it fails we don't have
20448 	 *    anything to do as ipif_ill is not in the group and for the
20449 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20450 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20451 	 */
20452 	while (irep > ire_array) {
20453 		irep--;
20454 		if (*irep != NULL) {
20455 			ire_delete(*irep);
20456 			if (ire_added)
20457 				ire_refrele(*irep);
20458 		}
20459 	}
20460 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid);
20461 
20462 	if (ipif_saved_irep != NULL) {
20463 		kmem_free(ipif_saved_irep,
20464 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20465 	}
20466 	if (src_ipif_held)
20467 		ipif_refrele(src_ipif);
20468 
20469 	ipif_arp_down(ipif);
20470 	return (err);
20471 }
20472 
20473 /*
20474  * Turn off the ARP with the ILLF_NOARP flag.
20475  */
20476 static int
20477 ill_arp_off(ill_t *ill)
20478 {
20479 	mblk_t	*arp_off_mp = NULL;
20480 	mblk_t	*arp_on_mp = NULL;
20481 
20482 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20483 
20484 	ASSERT(IAM_WRITER_ILL(ill));
20485 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20486 
20487 	/*
20488 	 * If the on message is still around we've already done
20489 	 * an arp_off without doing an arp_on thus there is no
20490 	 * work needed.
20491 	 */
20492 	if (ill->ill_arp_on_mp != NULL)
20493 		return (0);
20494 
20495 	/*
20496 	 * Allocate an ARP on message (to be saved) and an ARP off message
20497 	 */
20498 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20499 	if (!arp_off_mp)
20500 		return (ENOMEM);
20501 
20502 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20503 	if (!arp_on_mp)
20504 		goto failed;
20505 
20506 	ASSERT(ill->ill_arp_on_mp == NULL);
20507 	ill->ill_arp_on_mp = arp_on_mp;
20508 
20509 	/* Send an AR_INTERFACE_OFF request */
20510 	putnext(ill->ill_rq, arp_off_mp);
20511 	return (0);
20512 failed:
20513 
20514 	if (arp_off_mp)
20515 		freemsg(arp_off_mp);
20516 	return (ENOMEM);
20517 }
20518 
20519 /*
20520  * Turn on ARP by turning off the ILLF_NOARP flag.
20521  */
20522 static int
20523 ill_arp_on(ill_t *ill)
20524 {
20525 	mblk_t	*mp;
20526 
20527 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20528 
20529 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20530 
20531 	ASSERT(IAM_WRITER_ILL(ill));
20532 	/*
20533 	 * Send an AR_INTERFACE_ON request if we have already done
20534 	 * an arp_off (which allocated the message).
20535 	 */
20536 	if (ill->ill_arp_on_mp != NULL) {
20537 		mp = ill->ill_arp_on_mp;
20538 		ill->ill_arp_on_mp = NULL;
20539 		putnext(ill->ill_rq, mp);
20540 	}
20541 	return (0);
20542 }
20543 
20544 /*
20545  * Called after either deleting ill from the group or when setting
20546  * FAILED or STANDBY on the interface.
20547  */
20548 static void
20549 illgrp_reset_schednext(ill_t *ill)
20550 {
20551 	ill_group_t *illgrp;
20552 	ill_t *save_ill;
20553 
20554 	ASSERT(IAM_WRITER_ILL(ill));
20555 	/*
20556 	 * When called from illgrp_delete, ill_group will be non-NULL.
20557 	 * But when called from ip_sioctl_flags, it could be NULL if
20558 	 * somebody is setting FAILED/INACTIVE on some interface which
20559 	 * is not part of a group.
20560 	 */
20561 	illgrp = ill->ill_group;
20562 	if (illgrp == NULL)
20563 		return;
20564 	if (illgrp->illgrp_ill_schednext != ill)
20565 		return;
20566 
20567 	illgrp->illgrp_ill_schednext = NULL;
20568 	save_ill = ill;
20569 	/*
20570 	 * Choose a good ill to be the next one for
20571 	 * outbound traffic. As the flags FAILED/STANDBY is
20572 	 * not yet marked when called from ip_sioctl_flags,
20573 	 * we check for ill separately.
20574 	 */
20575 	for (ill = illgrp->illgrp_ill; ill != NULL;
20576 	    ill = ill->ill_group_next) {
20577 		if ((ill != save_ill) &&
20578 		    !(ill->ill_phyint->phyint_flags &
20579 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20580 			illgrp->illgrp_ill_schednext = ill;
20581 			return;
20582 		}
20583 	}
20584 }
20585 
20586 /*
20587  * Given an ill, find the next ill in the group to be scheduled.
20588  * (This should be called by ip_newroute() before ire_create().)
20589  * The passed in ill may be pulled out of the group, after we have picked
20590  * up a different outgoing ill from the same group. However ire add will
20591  * atomically check this.
20592  */
20593 ill_t *
20594 illgrp_scheduler(ill_t *ill)
20595 {
20596 	ill_t *retill;
20597 	ill_group_t *illgrp;
20598 	int illcnt;
20599 	int i;
20600 	uint64_t flags;
20601 
20602 	/*
20603 	 * We don't use a lock to check for the ill_group. If this ill
20604 	 * is currently being inserted we may end up just returning this
20605 	 * ill itself. That is ok.
20606 	 */
20607 	if (ill->ill_group == NULL) {
20608 		ill_refhold(ill);
20609 		return (ill);
20610 	}
20611 
20612 	/*
20613 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20614 	 * a set of stable ills. No ill can be added or deleted or change
20615 	 * group while we hold the reader lock.
20616 	 */
20617 	rw_enter(&ill_g_lock, RW_READER);
20618 	if ((illgrp = ill->ill_group) == NULL) {
20619 		rw_exit(&ill_g_lock);
20620 		ill_refhold(ill);
20621 		return (ill);
20622 	}
20623 
20624 	illcnt = illgrp->illgrp_ill_count;
20625 	mutex_enter(&illgrp->illgrp_lock);
20626 	retill = illgrp->illgrp_ill_schednext;
20627 
20628 	if (retill == NULL)
20629 		retill = illgrp->illgrp_ill;
20630 
20631 	/*
20632 	 * We do a circular search beginning at illgrp_ill_schednext
20633 	 * or illgrp_ill. We don't check the flags against the ill lock
20634 	 * since it can change anytime. The ire creation will be atomic
20635 	 * and will fail if the ill is FAILED or OFFLINE.
20636 	 */
20637 	for (i = 0; i < illcnt; i++) {
20638 		flags = retill->ill_phyint->phyint_flags;
20639 
20640 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20641 		    ILL_CAN_LOOKUP(retill)) {
20642 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20643 			ill_refhold(retill);
20644 			break;
20645 		}
20646 		retill = retill->ill_group_next;
20647 		if (retill == NULL)
20648 			retill = illgrp->illgrp_ill;
20649 	}
20650 	mutex_exit(&illgrp->illgrp_lock);
20651 	rw_exit(&ill_g_lock);
20652 
20653 	return (i == illcnt ? NULL : retill);
20654 }
20655 
20656 /*
20657  * Checks for availbility of a usable source address (if there is one) when the
20658  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20659  * this selection is done regardless of the destination.
20660  */
20661 boolean_t
20662 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20663 {
20664 	uint_t	ifindex;
20665 	ipif_t	*ipif = NULL;
20666 	ill_t	*uill;
20667 	boolean_t isv6;
20668 
20669 	ASSERT(ill != NULL);
20670 
20671 	isv6 = ill->ill_isv6;
20672 	ifindex = ill->ill_usesrc_ifindex;
20673 	if (ifindex != 0) {
20674 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20675 		    NULL);
20676 		if (uill == NULL)
20677 			return (NULL);
20678 		mutex_enter(&uill->ill_lock);
20679 		for (ipif = uill->ill_ipif; ipif != NULL;
20680 		    ipif = ipif->ipif_next) {
20681 			if (!IPIF_CAN_LOOKUP(ipif))
20682 				continue;
20683 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20684 				continue;
20685 			if (!(ipif->ipif_flags & IPIF_UP))
20686 				continue;
20687 			if (ipif->ipif_zoneid != zoneid)
20688 				continue;
20689 			if ((isv6 &&
20690 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20691 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20692 				continue;
20693 			mutex_exit(&uill->ill_lock);
20694 			ill_refrele(uill);
20695 			return (B_TRUE);
20696 		}
20697 		mutex_exit(&uill->ill_lock);
20698 		ill_refrele(uill);
20699 	}
20700 	return (B_FALSE);
20701 }
20702 
20703 /*
20704  * Determine the best source address given a destination address and an ill.
20705  * Prefers non-deprecated over deprecated but will return a deprecated
20706  * address if there is no other choice. If there is a usable source address
20707  * on the interface pointed to by ill_usesrc_ifindex then that is given
20708  * first preference.
20709  *
20710  * Returns NULL if there is no suitable source address for the ill.
20711  * This only occurs when there is no valid source address for the ill.
20712  */
20713 ipif_t *
20714 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20715 {
20716 	ipif_t *ipif;
20717 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20718 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20719 	int index = 0;
20720 	boolean_t wrapped = B_FALSE;
20721 	boolean_t same_subnet_only = B_FALSE;
20722 	boolean_t ipif_same_found, ipif_other_found;
20723 	boolean_t specific_found;
20724 	ill_t	*till, *usill = NULL;
20725 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20726 
20727 	if (ill->ill_usesrc_ifindex != 0) {
20728 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex, B_FALSE,
20729 		    NULL, NULL, NULL, NULL);
20730 		if (usill != NULL)
20731 			ill = usill;	/* Select source from usesrc ILL */
20732 		else
20733 			return (NULL);
20734 	}
20735 
20736 	/*
20737 	 * If we're dealing with an unlabeled destination on a labeled system,
20738 	 * make sure that we ignore source addresses that are incompatible with
20739 	 * the destination's default label.  That destination's default label
20740 	 * must dominate the minimum label on the source address.
20741 	 */
20742 	dst_rhtp = NULL;
20743 	if (is_system_labeled()) {
20744 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20745 		if (dst_rhtp == NULL)
20746 			return (NULL);
20747 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20748 			TPC_RELE(dst_rhtp);
20749 			dst_rhtp = NULL;
20750 		}
20751 	}
20752 
20753 	/*
20754 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20755 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20756 	 * After selecting the right ipif, under ill_lock make sure ipif is
20757 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20758 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20759 	 * but not under a lock.
20760 	 */
20761 	rw_enter(&ill_g_lock, RW_READER);
20762 
20763 retry:
20764 	till = ill;
20765 	ipif_arr[0] = NULL;
20766 
20767 	if (till->ill_group != NULL)
20768 		till = till->ill_group->illgrp_ill;
20769 
20770 	/*
20771 	 * Choose one good source address from each ill across the group.
20772 	 * If possible choose a source address in the same subnet as
20773 	 * the destination address.
20774 	 *
20775 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20776 	 * This is okay because of the following.
20777 	 *
20778 	 *    If PHYI_FAILED is set and we still have non-deprecated
20779 	 *    addresses, it means the addresses have not yet been
20780 	 *    failed over to a different interface. We potentially
20781 	 *    select them to create IRE_CACHES, which will be later
20782 	 *    flushed when the addresses move over.
20783 	 *
20784 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20785 	 *    addresses, it means either the user has configured them
20786 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20787 	 *    been moved over. For the former, in.mpathd does a failover
20788 	 *    when the interface becomes INACTIVE and hence we should
20789 	 *    not find them. Once INACTIVE is set, we don't allow them
20790 	 *    to create logical interfaces anymore. For the latter, a
20791 	 *    flush will happen when INACTIVE is cleared which will
20792 	 *    flush the IRE_CACHES.
20793 	 *
20794 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20795 	 *    over soon. We potentially select them to create IRE_CACHEs,
20796 	 *    which will be later flushed when the addresses move over.
20797 	 *
20798 	 * NOTE : As ipif_select_source is called to borrow source address
20799 	 * for an ipif that is part of a group, source address selection
20800 	 * will be re-done whenever the group changes i.e either an
20801 	 * insertion/deletion in the group.
20802 	 *
20803 	 * Fill ipif_arr[] with source addresses, using these rules:
20804 	 *
20805 	 *	1. At most one source address from a given ill ends up
20806 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20807 	 *	   associated with a given ill ends up in ipif_arr[].
20808 	 *
20809 	 *	2. If there is at least one non-deprecated ipif in the
20810 	 *	   IPMP group with a source address on the same subnet as
20811 	 *	   our destination, then fill ipif_arr[] only with
20812 	 *	   source addresses on the same subnet as our destination.
20813 	 *	   Note that because of (1), only the first
20814 	 *	   non-deprecated ipif found with a source address
20815 	 *	   matching the destination ends up in ipif_arr[].
20816 	 *
20817 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20818 	 *	   addresses not in the same subnet as our destination.
20819 	 *	   Again, because of (1), only the first off-subnet source
20820 	 *	   address will be chosen.
20821 	 *
20822 	 *	4. If there are no non-deprecated ipifs, then just use
20823 	 *	   the source address associated with the last deprecated
20824 	 *	   one we find that happens to be on the same subnet,
20825 	 *	   otherwise the first one not in the same subnet.
20826 	 */
20827 	specific_found = B_FALSE;
20828 	for (; till != NULL; till = till->ill_group_next) {
20829 		ipif_same_found = B_FALSE;
20830 		ipif_other_found = B_FALSE;
20831 		for (ipif = till->ill_ipif; ipif != NULL;
20832 		    ipif = ipif->ipif_next) {
20833 			if (!IPIF_CAN_LOOKUP(ipif))
20834 				continue;
20835 			/* Always skip NOLOCAL and ANYCAST interfaces */
20836 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20837 				continue;
20838 			if (!(ipif->ipif_flags & IPIF_UP) ||
20839 			    !ipif->ipif_addr_ready)
20840 				continue;
20841 			if (ipif->ipif_zoneid != zoneid &&
20842 			    ipif->ipif_zoneid != ALL_ZONES)
20843 				continue;
20844 			/*
20845 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20846 			 * but are not valid as source addresses.
20847 			 */
20848 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20849 				continue;
20850 
20851 			/*
20852 			 * Check compatibility of local address for
20853 			 * destination's default label if we're on a labeled
20854 			 * system.  Incompatible addresses can't be used at
20855 			 * all.
20856 			 */
20857 			if (dst_rhtp != NULL) {
20858 				boolean_t incompat;
20859 
20860 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20861 				    IPV4_VERSION, B_FALSE);
20862 				if (src_rhtp == NULL)
20863 					continue;
20864 				incompat =
20865 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20866 				    src_rhtp->tpc_tp.tp_doi !=
20867 				    dst_rhtp->tpc_tp.tp_doi ||
20868 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20869 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20870 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20871 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20872 				TPC_RELE(src_rhtp);
20873 				if (incompat)
20874 					continue;
20875 			}
20876 
20877 			/*
20878 			 * We prefer not to use all all-zones addresses, if we
20879 			 * can avoid it, as they pose problems with unlabeled
20880 			 * destinations.
20881 			 */
20882 			if (ipif->ipif_zoneid != ALL_ZONES) {
20883 				if (!specific_found &&
20884 				    (!same_subnet_only ||
20885 				    (ipif->ipif_net_mask & dst) ==
20886 				    ipif->ipif_subnet)) {
20887 					index = 0;
20888 					specific_found = B_TRUE;
20889 					ipif_other_found = B_FALSE;
20890 				}
20891 			} else {
20892 				if (specific_found)
20893 					continue;
20894 			}
20895 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
20896 				if (ipif_dep == NULL ||
20897 				    (ipif->ipif_net_mask & dst) ==
20898 				    ipif->ipif_subnet)
20899 					ipif_dep = ipif;
20900 				continue;
20901 			}
20902 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
20903 				/* found a source address in the same subnet */
20904 				if (!same_subnet_only) {
20905 					same_subnet_only = B_TRUE;
20906 					index = 0;
20907 				}
20908 				ipif_same_found = B_TRUE;
20909 			} else {
20910 				if (same_subnet_only || ipif_other_found)
20911 					continue;
20912 				ipif_other_found = B_TRUE;
20913 			}
20914 			ipif_arr[index++] = ipif;
20915 			if (index == MAX_IPIF_SELECT_SOURCE) {
20916 				wrapped = B_TRUE;
20917 				index = 0;
20918 			}
20919 			if (ipif_same_found)
20920 				break;
20921 		}
20922 	}
20923 
20924 	if (ipif_arr[0] == NULL) {
20925 		ipif = ipif_dep;
20926 	} else {
20927 		if (wrapped)
20928 			index = MAX_IPIF_SELECT_SOURCE;
20929 		ipif = ipif_arr[ipif_rand() % index];
20930 		ASSERT(ipif != NULL);
20931 	}
20932 
20933 	if (ipif != NULL) {
20934 		mutex_enter(&ipif->ipif_ill->ill_lock);
20935 		if (!IPIF_CAN_LOOKUP(ipif)) {
20936 			mutex_exit(&ipif->ipif_ill->ill_lock);
20937 			goto retry;
20938 		}
20939 		ipif_refhold_locked(ipif);
20940 		mutex_exit(&ipif->ipif_ill->ill_lock);
20941 	}
20942 
20943 	rw_exit(&ill_g_lock);
20944 	if (usill != NULL)
20945 		ill_refrele(usill);
20946 	if (dst_rhtp != NULL)
20947 		TPC_RELE(dst_rhtp);
20948 
20949 #ifdef DEBUG
20950 	if (ipif == NULL) {
20951 		char buf1[INET6_ADDRSTRLEN];
20952 
20953 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
20954 		    ill->ill_name,
20955 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
20956 	} else {
20957 		char buf1[INET6_ADDRSTRLEN];
20958 		char buf2[INET6_ADDRSTRLEN];
20959 
20960 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
20961 		    ipif->ipif_ill->ill_name,
20962 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
20963 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
20964 		    buf2, sizeof (buf2))));
20965 	}
20966 #endif /* DEBUG */
20967 	return (ipif);
20968 }
20969 
20970 
20971 /*
20972  * If old_ipif is not NULL, see if ipif was derived from old
20973  * ipif and if so, recreate the interface route by re-doing
20974  * source address selection. This happens when ipif_down ->
20975  * ipif_update_other_ipifs calls us.
20976  *
20977  * If old_ipif is NULL, just redo the source address selection
20978  * if needed. This happens when illgrp_insert or ipif_up_done
20979  * calls us.
20980  */
20981 static void
20982 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
20983 {
20984 	ire_t *ire;
20985 	ire_t *ipif_ire;
20986 	queue_t *stq;
20987 	ipif_t *nipif;
20988 	ill_t *ill;
20989 	boolean_t need_rele = B_FALSE;
20990 
20991 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
20992 	ASSERT(IAM_WRITER_IPIF(ipif));
20993 
20994 	ill = ipif->ipif_ill;
20995 	if (!(ipif->ipif_flags &
20996 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
20997 		/*
20998 		 * Can't possibly have borrowed the source
20999 		 * from old_ipif.
21000 		 */
21001 		return;
21002 	}
21003 
21004 	/*
21005 	 * Is there any work to be done? No work if the address
21006 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21007 	 * ipif_select_source() does not borrow addresses from
21008 	 * NOLOCAL and ANYCAST interfaces).
21009 	 */
21010 	if ((old_ipif != NULL) &&
21011 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21012 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21013 	    (old_ipif->ipif_flags &
21014 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21015 		return;
21016 	}
21017 
21018 	/*
21019 	 * Perform the same checks as when creating the
21020 	 * IRE_INTERFACE in ipif_up_done.
21021 	 */
21022 	if (!(ipif->ipif_flags & IPIF_UP))
21023 		return;
21024 
21025 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21026 	    (ipif->ipif_subnet == INADDR_ANY))
21027 		return;
21028 
21029 	ipif_ire = ipif_to_ire(ipif);
21030 	if (ipif_ire == NULL)
21031 		return;
21032 
21033 	/*
21034 	 * We know that ipif uses some other source for its
21035 	 * IRE_INTERFACE. Is it using the source of this
21036 	 * old_ipif?
21037 	 */
21038 	if (old_ipif != NULL &&
21039 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21040 		ire_refrele(ipif_ire);
21041 		return;
21042 	}
21043 	if (ip_debug > 2) {
21044 		/* ip1dbg */
21045 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21046 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21047 	}
21048 
21049 	stq = ipif_ire->ire_stq;
21050 
21051 	/*
21052 	 * Can't use our source address. Select a different
21053 	 * source address for the IRE_INTERFACE.
21054 	 */
21055 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21056 	if (nipif == NULL) {
21057 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21058 		nipif = ipif;
21059 	} else {
21060 		need_rele = B_TRUE;
21061 	}
21062 
21063 	ire = ire_create(
21064 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21065 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21066 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21067 	    NULL,				/* no gateway */
21068 	    NULL,
21069 	    &ipif->ipif_mtu,			/* max frag */
21070 	    NULL,				/* fast path header */
21071 	    NULL,				/* no recv from queue */
21072 	    stq,				/* send-to queue */
21073 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21074 	    ill->ill_resolver_mp,		/* xmit header */
21075 	    ipif,
21076 	    NULL,
21077 	    0,
21078 	    0,
21079 	    0,
21080 	    0,
21081 	    &ire_uinfo_null,
21082 	    NULL,
21083 	    NULL);
21084 
21085 	if (ire != NULL) {
21086 		ire_t *ret_ire;
21087 		int error;
21088 
21089 		/*
21090 		 * We don't need ipif_ire anymore. We need to delete
21091 		 * before we add so that ire_add does not detect
21092 		 * duplicates.
21093 		 */
21094 		ire_delete(ipif_ire);
21095 		ret_ire = ire;
21096 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21097 		ASSERT(error == 0);
21098 		ASSERT(ire == ret_ire);
21099 		/* Held in ire_add */
21100 		ire_refrele(ret_ire);
21101 	}
21102 	/*
21103 	 * Either we are falling through from above or could not
21104 	 * allocate a replacement.
21105 	 */
21106 	ire_refrele(ipif_ire);
21107 	if (need_rele)
21108 		ipif_refrele(nipif);
21109 }
21110 
21111 /*
21112  * This old_ipif is going away.
21113  *
21114  * Determine if any other ipif's is using our address as
21115  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21116  * IPIF_DEPRECATED).
21117  * Find the IRE_INTERFACE for such ipifs and recreate them
21118  * to use an different source address following the rules in
21119  * ipif_up_done.
21120  *
21121  * This function takes an illgrp as an argument so that illgrp_delete
21122  * can call this to update source address even after deleting the
21123  * old_ipif->ipif_ill from the ill group.
21124  */
21125 static void
21126 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21127 {
21128 	ipif_t *ipif;
21129 	ill_t *ill;
21130 	char	buf[INET6_ADDRSTRLEN];
21131 
21132 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21133 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21134 
21135 	ill = old_ipif->ipif_ill;
21136 
21137 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21138 	    ill->ill_name,
21139 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21140 	    buf, sizeof (buf))));
21141 	/*
21142 	 * If this part of a group, look at all ills as ipif_select_source
21143 	 * borrows source address across all the ills in the group.
21144 	 */
21145 	if (illgrp != NULL)
21146 		ill = illgrp->illgrp_ill;
21147 
21148 	for (; ill != NULL; ill = ill->ill_group_next) {
21149 		for (ipif = ill->ill_ipif; ipif != NULL;
21150 		    ipif = ipif->ipif_next) {
21151 
21152 			if (ipif == old_ipif)
21153 				continue;
21154 
21155 			ipif_recreate_interface_routes(old_ipif, ipif);
21156 		}
21157 	}
21158 }
21159 
21160 /* ARGSUSED */
21161 int
21162 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21163 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21164 {
21165 	/*
21166 	 * ill_phyint_reinit merged the v4 and v6 into a single
21167 	 * ipsq. Could also have become part of a ipmp group in the
21168 	 * process, and we might not have been able to complete the
21169 	 * operation in ipif_set_values, if we could not become
21170 	 * exclusive.  If so restart it here.
21171 	 */
21172 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21173 }
21174 
21175 
21176 /* ARGSUSED */
21177 int
21178 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21179     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21180 {
21181 	queue_t		*q1 = q;
21182 	char 		*cp;
21183 	char		interf_name[LIFNAMSIZ];
21184 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21185 
21186 	if (!q->q_next) {
21187 		ip1dbg((
21188 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21189 		return (EINVAL);
21190 	}
21191 
21192 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21193 		return (EALREADY);
21194 
21195 	do {
21196 		q1 = q1->q_next;
21197 	} while (q1->q_next);
21198 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21199 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21200 
21201 	/*
21202 	 * Here we are not going to delay the ioack until after
21203 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21204 	 * original ioctl message before sending the requests.
21205 	 */
21206 	return (ipif_set_values(q, mp, interf_name, &ppa));
21207 }
21208 
21209 /* ARGSUSED */
21210 int
21211 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21212     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21213 {
21214 	return (ENXIO);
21215 }
21216 
21217 /*
21218  * Net and subnet broadcast ire's are now specific to the particular
21219  * physical interface (ill) and not to any one locigal interface (ipif).
21220  * However, if a particular logical interface is being taken down, it's
21221  * associated ire's will be taken down as well.  Hence, when we go to
21222  * take down or change the local address, broadcast address or netmask
21223  * of a specific logical interface, we must check to make sure that we
21224  * have valid net and subnet broadcast ire's for the other logical
21225  * interfaces which may have been shared with the logical interface
21226  * being brought down or changed.
21227  *
21228  * There is one set of 0.0.0.0 and 255.255.255.255 per ill. Usually it
21229  * is tied to the first interface coming UP. If that ipif is going down,
21230  * we need to recreate them on the next valid ipif.
21231  *
21232  * Note: assume that the ipif passed in is still up so that it's IRE
21233  * entries are still valid.
21234  */
21235 static void
21236 ipif_check_bcast_ires(ipif_t *test_ipif)
21237 {
21238 	ipif_t	*ipif;
21239 	ire_t	*test_subnet_ire, *test_net_ire;
21240 	ire_t	*test_allzero_ire, *test_allone_ire;
21241 	ire_t	*ire_array[12];
21242 	ire_t	**irep = &ire_array[0];
21243 	ire_t	**irep1;
21244 
21245 	ipaddr_t net_addr, subnet_addr, net_mask, subnet_mask;
21246 	ipaddr_t test_net_addr, test_subnet_addr;
21247 	ipaddr_t test_net_mask, test_subnet_mask;
21248 	boolean_t need_net_bcast_ire = B_FALSE;
21249 	boolean_t need_subnet_bcast_ire = B_FALSE;
21250 	boolean_t allzero_bcast_ire_created = B_FALSE;
21251 	boolean_t allone_bcast_ire_created = B_FALSE;
21252 	boolean_t net_bcast_ire_created = B_FALSE;
21253 	boolean_t subnet_bcast_ire_created = B_FALSE;
21254 
21255 	ipif_t  *backup_ipif_net = (ipif_t *)NULL;
21256 	ipif_t  *backup_ipif_subnet = (ipif_t *)NULL;
21257 	ipif_t  *backup_ipif_allzeros = (ipif_t *)NULL;
21258 	ipif_t  *backup_ipif_allones = (ipif_t *)NULL;
21259 	uint64_t check_flags = IPIF_DEPRECATED | IPIF_NOLOCAL | IPIF_ANYCAST;
21260 
21261 	ASSERT(!test_ipif->ipif_isv6);
21262 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21263 
21264 	/*
21265 	 * No broadcast IREs for the LOOPBACK interface
21266 	 * or others such as point to point and IPIF_NOXMIT.
21267 	 */
21268 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21269 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21270 		return;
21271 
21272 	test_allzero_ire = ire_ctable_lookup(0, 0, IRE_BROADCAST,
21273 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
21274 
21275 	test_allone_ire = ire_ctable_lookup(INADDR_BROADCAST, 0, IRE_BROADCAST,
21276 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
21277 
21278 	test_net_mask = ip_net_mask(test_ipif->ipif_subnet);
21279 	test_subnet_mask = test_ipif->ipif_net_mask;
21280 
21281 	/*
21282 	 * If no net mask set, assume the default based on net class.
21283 	 */
21284 	if (test_subnet_mask == 0)
21285 		test_subnet_mask = test_net_mask;
21286 
21287 	/*
21288 	 * Check if there is a network broadcast ire associated with this ipif
21289 	 */
21290 	test_net_addr = test_net_mask  & test_ipif->ipif_subnet;
21291 	test_net_ire = ire_ctable_lookup(test_net_addr, 0, IRE_BROADCAST,
21292 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
21293 
21294 	/*
21295 	 * Check if there is a subnet broadcast IRE associated with this ipif
21296 	 */
21297 	test_subnet_addr = test_subnet_mask  & test_ipif->ipif_subnet;
21298 	test_subnet_ire = ire_ctable_lookup(test_subnet_addr, 0, IRE_BROADCAST,
21299 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
21300 
21301 	/*
21302 	 * No broadcast ire's associated with this ipif.
21303 	 */
21304 	if ((test_subnet_ire == NULL) && (test_net_ire == NULL) &&
21305 	    (test_allzero_ire == NULL) && (test_allone_ire == NULL)) {
21306 		return;
21307 	}
21308 
21309 	/*
21310 	 * We have established which bcast ires have to be replaced.
21311 	 * Next we try to locate ipifs that match there ires.
21312 	 * The rules are simple: If we find an ipif that matches on the subnet
21313 	 * address it will also match on the net address, the allzeros and
21314 	 * allones address. Any ipif that matches only on the net address will
21315 	 * also match the allzeros and allones addresses.
21316 	 * The other criterion is the ipif_flags. We look for non-deprecated
21317 	 * (and non-anycast and non-nolocal) ipifs as the best choice.
21318 	 * ipifs with check_flags matching (deprecated, etc) are used only
21319 	 * if good ipifs are not available. While looping, we save existing
21320 	 * deprecated ipifs as backup_ipif.
21321 	 * We loop through all the ipifs for this ill looking for ipifs
21322 	 * whose broadcast addr match the ipif passed in, but do not have
21323 	 * their own broadcast ires. For creating 0.0.0.0 and
21324 	 * 255.255.255.255 we just need an ipif on this ill to create.
21325 	 */
21326 	for (ipif = test_ipif->ipif_ill->ill_ipif; ipif != NULL;
21327 	    ipif = ipif->ipif_next) {
21328 
21329 		ASSERT(!ipif->ipif_isv6);
21330 		/*
21331 		 * Already checked the ipif passed in.
21332 		 */
21333 		if (ipif == test_ipif) {
21334 			continue;
21335 		}
21336 
21337 		/*
21338 		 * We only need to recreate broadcast ires if another ipif in
21339 		 * the same zone uses them. The new ires must be created in the
21340 		 * same zone.
21341 		 */
21342 		if (ipif->ipif_zoneid != test_ipif->ipif_zoneid) {
21343 			continue;
21344 		}
21345 
21346 		/*
21347 		 * Only interested in logical interfaces with valid local
21348 		 * addresses or with the ability to broadcast.
21349 		 */
21350 		if ((ipif->ipif_subnet == 0) ||
21351 		    !(ipif->ipif_flags & IPIF_BROADCAST) ||
21352 		    (ipif->ipif_flags & IPIF_NOXMIT) ||
21353 		    !(ipif->ipif_flags & IPIF_UP)) {
21354 			continue;
21355 		}
21356 		/*
21357 		 * Check if there is a net broadcast ire for this
21358 		 * net address.  If it turns out that the ipif we are
21359 		 * about to take down owns this ire, we must make a
21360 		 * new one because it is potentially going away.
21361 		 */
21362 		if (test_net_ire && (!net_bcast_ire_created)) {
21363 			net_mask = ip_net_mask(ipif->ipif_subnet);
21364 			net_addr = net_mask & ipif->ipif_subnet;
21365 			if (net_addr == test_net_addr) {
21366 				need_net_bcast_ire = B_TRUE;
21367 				/*
21368 				 * Use DEPRECATED ipif only if no good
21369 				 * ires are available. subnet_addr is
21370 				 * a better match than net_addr.
21371 				 */
21372 				if ((ipif->ipif_flags & check_flags) &&
21373 				    (backup_ipif_net == NULL)) {
21374 					backup_ipif_net = ipif;
21375 				}
21376 			}
21377 		}
21378 		/*
21379 		 * Check if there is a subnet broadcast ire for this
21380 		 * net address.  If it turns out that the ipif we are
21381 		 * about to take down owns this ire, we must make a
21382 		 * new one because it is potentially going away.
21383 		 */
21384 		if (test_subnet_ire && (!subnet_bcast_ire_created)) {
21385 			subnet_mask = ipif->ipif_net_mask;
21386 			subnet_addr = ipif->ipif_subnet;
21387 			if (subnet_addr == test_subnet_addr) {
21388 				need_subnet_bcast_ire = B_TRUE;
21389 				if ((ipif->ipif_flags & check_flags) &&
21390 				    (backup_ipif_subnet == NULL)) {
21391 					backup_ipif_subnet = ipif;
21392 				}
21393 			}
21394 		}
21395 
21396 
21397 		/* Short circuit here if this ipif is deprecated */
21398 		if (ipif->ipif_flags & check_flags) {
21399 			if ((test_allzero_ire != NULL) &&
21400 			    (!allzero_bcast_ire_created) &&
21401 			    (backup_ipif_allzeros == NULL)) {
21402 				backup_ipif_allzeros = ipif;
21403 			}
21404 			if ((test_allone_ire != NULL) &&
21405 			    (!allone_bcast_ire_created) &&
21406 			    (backup_ipif_allones == NULL)) {
21407 				backup_ipif_allones = ipif;
21408 			}
21409 			continue;
21410 		}
21411 
21412 		/*
21413 		 * Found an ipif which has the same broadcast ire as the
21414 		 * ipif passed in and the ipif passed in "owns" the ire.
21415 		 * Create new broadcast ire's for this broadcast addr.
21416 		 */
21417 		if (need_net_bcast_ire && !net_bcast_ire_created) {
21418 			irep = ire_create_bcast(ipif, net_addr, irep);
21419 			irep = ire_create_bcast(ipif,
21420 			    ~net_mask | net_addr, irep);
21421 			net_bcast_ire_created = B_TRUE;
21422 		}
21423 		if (need_subnet_bcast_ire && !subnet_bcast_ire_created) {
21424 			irep = ire_create_bcast(ipif, subnet_addr, irep);
21425 			irep = ire_create_bcast(ipif,
21426 			    ~subnet_mask | subnet_addr, irep);
21427 			subnet_bcast_ire_created = B_TRUE;
21428 		}
21429 		if (test_allzero_ire != NULL && !allzero_bcast_ire_created) {
21430 			irep = ire_create_bcast(ipif, 0, irep);
21431 			allzero_bcast_ire_created = B_TRUE;
21432 		}
21433 		if (test_allone_ire != NULL && !allone_bcast_ire_created) {
21434 			irep = ire_create_bcast(ipif, INADDR_BROADCAST, irep);
21435 			allone_bcast_ire_created = B_TRUE;
21436 		}
21437 		/*
21438 		 * Once we have created all the appropriate ires, we
21439 		 * just break out of this loop to add what we have created.
21440 		 * This has been indented similar to ire_match_args for
21441 		 * readability.
21442 		 */
21443 		if (((test_net_ire == NULL) ||
21444 			(net_bcast_ire_created)) &&
21445 		    ((test_subnet_ire == NULL) ||
21446 			(subnet_bcast_ire_created)) &&
21447 		    ((test_allzero_ire == NULL) ||
21448 			(allzero_bcast_ire_created)) &&
21449 		    ((test_allone_ire == NULL) ||
21450 			(allone_bcast_ire_created))) {
21451 			break;
21452 		}
21453 	}
21454 
21455 	/*
21456 	 * Create bcast ires on deprecated ipifs if no non-deprecated ipifs
21457 	 * exist. 6 pairs of bcast ires are needed.
21458 	 * Note - the old ires are deleted in ipif_down.
21459 	 */
21460 	if (need_net_bcast_ire && !net_bcast_ire_created && backup_ipif_net) {
21461 		ipif = backup_ipif_net;
21462 		irep = ire_create_bcast(ipif, net_addr, irep);
21463 		irep = ire_create_bcast(ipif, ~net_mask | net_addr, irep);
21464 		net_bcast_ire_created = B_TRUE;
21465 	}
21466 	if (need_subnet_bcast_ire && !subnet_bcast_ire_created &&
21467 	    backup_ipif_subnet) {
21468 		ipif = backup_ipif_subnet;
21469 		irep = ire_create_bcast(ipif, subnet_addr, irep);
21470 		irep = ire_create_bcast(ipif,
21471 		    ~subnet_mask | subnet_addr, irep);
21472 		subnet_bcast_ire_created = B_TRUE;
21473 	}
21474 	if (test_allzero_ire != NULL && !allzero_bcast_ire_created &&
21475 	    backup_ipif_allzeros) {
21476 		irep = ire_create_bcast(backup_ipif_allzeros, 0, irep);
21477 		allzero_bcast_ire_created = B_TRUE;
21478 	}
21479 	if (test_allone_ire != NULL && !allone_bcast_ire_created &&
21480 	    backup_ipif_allones) {
21481 		irep = ire_create_bcast(backup_ipif_allones,
21482 		    INADDR_BROADCAST, irep);
21483 		allone_bcast_ire_created = B_TRUE;
21484 	}
21485 
21486 	/*
21487 	 * If we can't create all of them, don't add any of them.
21488 	 * Code in ip_wput_ire and ire_to_ill assumes that we
21489 	 * always have a non-loopback copy and loopback copy
21490 	 * for a given address.
21491 	 */
21492 	for (irep1 = irep; irep1 > ire_array; ) {
21493 		irep1--;
21494 		if (*irep1 == NULL) {
21495 			ip0dbg(("ipif_check_bcast_ires: can't create "
21496 			    "IRE_BROADCAST, memory allocation failure\n"));
21497 			while (irep > ire_array) {
21498 				irep--;
21499 				if (*irep != NULL)
21500 					ire_delete(*irep);
21501 			}
21502 			goto bad;
21503 		}
21504 	}
21505 	for (irep1 = irep; irep1 > ire_array; ) {
21506 		int error;
21507 
21508 		irep1--;
21509 		error = ire_add(irep1, NULL, NULL, NULL, B_FALSE);
21510 		if (error == 0) {
21511 			ire_refrele(*irep1);		/* Held in ire_add */
21512 		}
21513 	}
21514 bad:
21515 	if (test_allzero_ire != NULL)
21516 		ire_refrele(test_allzero_ire);
21517 	if (test_allone_ire != NULL)
21518 		ire_refrele(test_allone_ire);
21519 	if (test_net_ire != NULL)
21520 		ire_refrele(test_net_ire);
21521 	if (test_subnet_ire != NULL)
21522 		ire_refrele(test_subnet_ire);
21523 }
21524 
21525 /*
21526  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21527  * from lifr_flags and the name from lifr_name.
21528  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21529  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21530  * Returns EINPROGRESS when mp has been consumed by queueing it on
21531  * ill_pending_mp and the ioctl will complete in ip_rput.
21532  */
21533 /* ARGSUSED */
21534 int
21535 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21536     ip_ioctl_cmd_t *ipip, void *if_req)
21537 {
21538 	int	err;
21539 	ill_t	*ill;
21540 	struct lifreq *lifr = (struct lifreq *)if_req;
21541 
21542 	ASSERT(ipif != NULL);
21543 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21544 	ASSERT(q->q_next != NULL);
21545 
21546 	ill = (ill_t *)q->q_ptr;
21547 	/*
21548 	 * If we are not writer on 'q' then this interface exists already
21549 	 * and previous lookups (ipif_extract_lifreq_cmn) found this ipif.
21550 	 * So return EALREADY
21551 	 */
21552 	if (ill != ipif->ipif_ill)
21553 		return (EALREADY);
21554 
21555 	if (ill->ill_name[0] != '\0')
21556 		return (EALREADY);
21557 
21558 	/*
21559 	 * Set all the flags. Allows all kinds of override. Provide some
21560 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21561 	 * unless there is either multicast/broadcast support in the driver
21562 	 * or it is a pt-pt link.
21563 	 */
21564 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21565 		/* Meaningless to IP thus don't allow them to be set. */
21566 		ip1dbg(("ip_setname: EINVAL 1\n"));
21567 		return (EINVAL);
21568 	}
21569 	/*
21570 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21571 	 * ill_bcast_addr_length info.
21572 	 */
21573 	if (!ill->ill_needs_attach &&
21574 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21575 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21576 	    ill->ill_bcast_addr_length == 0)) {
21577 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21578 		ip1dbg(("ip_setname: EINVAL 2\n"));
21579 		return (EINVAL);
21580 	}
21581 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21582 	    ((lifr->lifr_flags & IFF_IPV6) ||
21583 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21584 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21585 		ip1dbg(("ip_setname: EINVAL 3\n"));
21586 		return (EINVAL);
21587 	}
21588 	if (lifr->lifr_flags & IFF_UP) {
21589 		/* Can only be set with SIOCSLIFFLAGS */
21590 		ip1dbg(("ip_setname: EINVAL 4\n"));
21591 		return (EINVAL);
21592 	}
21593 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21594 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21595 		ip1dbg(("ip_setname: EINVAL 5\n"));
21596 		return (EINVAL);
21597 	}
21598 	/*
21599 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21600 	 */
21601 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21602 	    !(lifr->lifr_flags & IFF_IPV6) &&
21603 	    !(ipif->ipif_isv6)) {
21604 		ip1dbg(("ip_setname: EINVAL 6\n"));
21605 		return (EINVAL);
21606 	}
21607 
21608 	/*
21609 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21610 	 * we have all the flags here. So, we assign rather than we OR.
21611 	 * We can't OR the flags here because we don't want to set
21612 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21613 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21614 	 * on lifr_flags value here.
21615 	 */
21616 	/*
21617 	 * This ill has not been inserted into the global list.
21618 	 * So we are still single threaded and don't need any lock
21619 	 */
21620 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS &
21621 	    ~IFF_DUPLICATE;
21622 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21623 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21624 
21625 	/* We started off as V4. */
21626 	if (ill->ill_flags & ILLF_IPV6) {
21627 		ill->ill_phyint->phyint_illv6 = ill;
21628 		ill->ill_phyint->phyint_illv4 = NULL;
21629 	}
21630 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
21631 	return (err);
21632 }
21633 
21634 /* ARGSUSED */
21635 int
21636 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21637     ip_ioctl_cmd_t *ipip, void *if_req)
21638 {
21639 	/*
21640 	 * ill_phyint_reinit merged the v4 and v6 into a single
21641 	 * ipsq. Could also have become part of a ipmp group in the
21642 	 * process, and we might not have been able to complete the
21643 	 * slifname in ipif_set_values, if we could not become
21644 	 * exclusive.  If so restart it here
21645 	 */
21646 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21647 }
21648 
21649 /*
21650  * Return a pointer to the ipif which matches the index, IP version type and
21651  * zoneid.
21652  */
21653 ipif_t *
21654 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21655     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
21656 {
21657 	ill_t	*ill;
21658 	ipsq_t  *ipsq;
21659 	phyint_t *phyi;
21660 	ipif_t	*ipif;
21661 
21662 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21663 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21664 
21665 	if (err != NULL)
21666 		*err = 0;
21667 
21668 	/*
21669 	 * Indexes are stored in the phyint - a common structure
21670 	 * to both IPv4 and IPv6.
21671 	 */
21672 
21673 	rw_enter(&ill_g_lock, RW_READER);
21674 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
21675 	    (void *) &index, NULL);
21676 	if (phyi != NULL) {
21677 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
21678 		if (ill == NULL) {
21679 			rw_exit(&ill_g_lock);
21680 			if (err != NULL)
21681 				*err = ENXIO;
21682 			return (NULL);
21683 		}
21684 		GRAB_CONN_LOCK(q);
21685 		mutex_enter(&ill->ill_lock);
21686 		if (ILL_CAN_LOOKUP(ill)) {
21687 			for (ipif = ill->ill_ipif; ipif != NULL;
21688 			    ipif = ipif->ipif_next) {
21689 				if (IPIF_CAN_LOOKUP(ipif) &&
21690 				    (zoneid == ALL_ZONES ||
21691 				    zoneid == ipif->ipif_zoneid ||
21692 				    ipif->ipif_zoneid == ALL_ZONES)) {
21693 					ipif_refhold_locked(ipif);
21694 					mutex_exit(&ill->ill_lock);
21695 					RELEASE_CONN_LOCK(q);
21696 					rw_exit(&ill_g_lock);
21697 					return (ipif);
21698 				}
21699 			}
21700 		} else if (ILL_CAN_WAIT(ill, q)) {
21701 			ipsq = ill->ill_phyint->phyint_ipsq;
21702 			mutex_enter(&ipsq->ipsq_lock);
21703 			rw_exit(&ill_g_lock);
21704 			mutex_exit(&ill->ill_lock);
21705 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
21706 			mutex_exit(&ipsq->ipsq_lock);
21707 			RELEASE_CONN_LOCK(q);
21708 			*err = EINPROGRESS;
21709 			return (NULL);
21710 		}
21711 		mutex_exit(&ill->ill_lock);
21712 		RELEASE_CONN_LOCK(q);
21713 	}
21714 	rw_exit(&ill_g_lock);
21715 	if (err != NULL)
21716 		*err = ENXIO;
21717 	return (NULL);
21718 }
21719 
21720 typedef struct conn_change_s {
21721 	uint_t cc_old_ifindex;
21722 	uint_t cc_new_ifindex;
21723 } conn_change_t;
21724 
21725 /*
21726  * ipcl_walk function for changing interface index.
21727  */
21728 static void
21729 conn_change_ifindex(conn_t *connp, caddr_t arg)
21730 {
21731 	conn_change_t *connc;
21732 	uint_t old_ifindex;
21733 	uint_t new_ifindex;
21734 	int i;
21735 	ilg_t *ilg;
21736 
21737 	connc = (conn_change_t *)arg;
21738 	old_ifindex = connc->cc_old_ifindex;
21739 	new_ifindex = connc->cc_new_ifindex;
21740 
21741 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21742 		connp->conn_orig_bound_ifindex = new_ifindex;
21743 
21744 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21745 		connp->conn_orig_multicast_ifindex = new_ifindex;
21746 
21747 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
21748 		connp->conn_orig_xmit_ifindex = new_ifindex;
21749 
21750 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21751 		ilg = &connp->conn_ilg[i];
21752 		if (ilg->ilg_orig_ifindex == old_ifindex)
21753 			ilg->ilg_orig_ifindex = new_ifindex;
21754 	}
21755 }
21756 
21757 /*
21758  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21759  * to new_index if it matches the old_index.
21760  *
21761  * Failovers typically happen within a group of ills. But somebody
21762  * can remove an ill from the group after a failover happened. If
21763  * we are setting the ifindex after this, we potentially need to
21764  * look at all the ills rather than just the ones in the group.
21765  * We cut down the work by looking at matching ill_net_types
21766  * and ill_types as we could not possibly grouped them together.
21767  */
21768 static void
21769 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21770 {
21771 	ill_t *ill;
21772 	ipif_t *ipif;
21773 	uint_t old_ifindex;
21774 	uint_t new_ifindex;
21775 	ilm_t *ilm;
21776 	ill_walk_context_t ctx;
21777 
21778 	old_ifindex = connc->cc_old_ifindex;
21779 	new_ifindex = connc->cc_new_ifindex;
21780 
21781 	rw_enter(&ill_g_lock, RW_READER);
21782 	ill = ILL_START_WALK_ALL(&ctx);
21783 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21784 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21785 			(ill_orig->ill_type != ill->ill_type)) {
21786 			continue;
21787 		}
21788 		for (ipif = ill->ill_ipif; ipif != NULL;
21789 				ipif = ipif->ipif_next) {
21790 			if (ipif->ipif_orig_ifindex == old_ifindex)
21791 				ipif->ipif_orig_ifindex = new_ifindex;
21792 		}
21793 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21794 			if (ilm->ilm_orig_ifindex == old_ifindex)
21795 				ilm->ilm_orig_ifindex = new_ifindex;
21796 		}
21797 	}
21798 	rw_exit(&ill_g_lock);
21799 }
21800 
21801 /*
21802  * We first need to ensure that the new index is unique, and
21803  * then carry the change across both v4 and v6 ill representation
21804  * of the physical interface.
21805  */
21806 /* ARGSUSED */
21807 int
21808 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21809     ip_ioctl_cmd_t *ipip, void *ifreq)
21810 {
21811 	ill_t		*ill;
21812 	ill_t		*ill_other;
21813 	phyint_t	*phyi;
21814 	int		old_index;
21815 	conn_change_t	connc;
21816 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21817 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21818 	uint_t	index;
21819 	ill_t	*ill_v4;
21820 	ill_t	*ill_v6;
21821 
21822 	if (ipip->ipi_cmd_type == IF_CMD)
21823 		index = ifr->ifr_index;
21824 	else
21825 		index = lifr->lifr_index;
21826 
21827 	/*
21828 	 * Only allow on physical interface. Also, index zero is illegal.
21829 	 *
21830 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21831 	 *
21832 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21833 	 *    implies a possible failback might have to happen. As failback
21834 	 *    depends on the old index, we should fail setting the index.
21835 	 *
21836 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21837 	 *    any addresses or multicast memberships are failed over to
21838 	 *    a non-STANDBY interface. As failback depends on the old
21839 	 *    index, we should fail setting the index for this case also.
21840 	 *
21841 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21842 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21843 	 */
21844 	ill = ipif->ipif_ill;
21845 	phyi = ill->ill_phyint;
21846 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21847 	    ipif->ipif_id != 0 || index == 0) {
21848 		return (EINVAL);
21849 	}
21850 	old_index = phyi->phyint_ifindex;
21851 
21852 	/* If the index is not changing, no work to do */
21853 	if (old_index == index)
21854 		return (0);
21855 
21856 	/*
21857 	 * Use ill_lookup_on_ifindex to determine if the
21858 	 * new index is unused and if so allow the change.
21859 	 */
21860 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL);
21861 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL);
21862 	if (ill_v6 != NULL || ill_v4 != NULL) {
21863 		if (ill_v4 != NULL)
21864 			ill_refrele(ill_v4);
21865 		if (ill_v6 != NULL)
21866 			ill_refrele(ill_v6);
21867 		return (EBUSY);
21868 	}
21869 
21870 	/*
21871 	 * The new index is unused. Set it in the phyint.
21872 	 * Locate the other ill so that we can send a routing
21873 	 * sockets message.
21874 	 */
21875 	if (ill->ill_isv6) {
21876 		ill_other = phyi->phyint_illv4;
21877 	} else {
21878 		ill_other = phyi->phyint_illv6;
21879 	}
21880 
21881 	phyi->phyint_ifindex = index;
21882 
21883 	connc.cc_old_ifindex = old_index;
21884 	connc.cc_new_ifindex = index;
21885 	ip_change_ifindex(ill, &connc);
21886 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc);
21887 
21888 	/* Send the routing sockets message */
21889 	ip_rts_ifmsg(ipif);
21890 	if (ill_other != NULL)
21891 		ip_rts_ifmsg(ill_other->ill_ipif);
21892 
21893 	return (0);
21894 }
21895 
21896 /* ARGSUSED */
21897 int
21898 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21899     ip_ioctl_cmd_t *ipip, void *ifreq)
21900 {
21901 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21902 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21903 
21904 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
21905 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21906 	/* Get the interface index */
21907 	if (ipip->ipi_cmd_type == IF_CMD) {
21908 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21909 	} else {
21910 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21911 	}
21912 	return (0);
21913 }
21914 
21915 /* ARGSUSED */
21916 int
21917 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21918     ip_ioctl_cmd_t *ipip, void *ifreq)
21919 {
21920 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21921 
21922 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
21923 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21924 	/* Get the interface zone */
21925 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21926 	lifr->lifr_zoneid = ipif->ipif_zoneid;
21927 	return (0);
21928 }
21929 
21930 /*
21931  * Set the zoneid of an interface.
21932  */
21933 /* ARGSUSED */
21934 int
21935 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21936     ip_ioctl_cmd_t *ipip, void *ifreq)
21937 {
21938 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21939 	int err = 0;
21940 	boolean_t need_up = B_FALSE;
21941 	zone_t *zptr;
21942 	zone_status_t status;
21943 	zoneid_t zoneid;
21944 
21945 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21946 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
21947 		if (!is_system_labeled())
21948 			return (ENOTSUP);
21949 		zoneid = GLOBAL_ZONEID;
21950 	}
21951 
21952 	/* cannot assign instance zero to a non-global zone */
21953 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
21954 		return (ENOTSUP);
21955 
21956 	/*
21957 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
21958 	 * the event of a race with the zone shutdown processing, since IP
21959 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
21960 	 * interface will be cleaned up even if the zone is shut down
21961 	 * immediately after the status check. If the interface can't be brought
21962 	 * down right away, and the zone is shut down before the restart
21963 	 * function is called, we resolve the possible races by rechecking the
21964 	 * zone status in the restart function.
21965 	 */
21966 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
21967 		return (EINVAL);
21968 	status = zone_status_get(zptr);
21969 	zone_rele(zptr);
21970 
21971 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
21972 		return (EINVAL);
21973 
21974 	if (ipif->ipif_flags & IPIF_UP) {
21975 		/*
21976 		 * If the interface is already marked up,
21977 		 * we call ipif_down which will take care
21978 		 * of ditching any IREs that have been set
21979 		 * up based on the old interface address.
21980 		 */
21981 		err = ipif_logical_down(ipif, q, mp);
21982 		if (err == EINPROGRESS)
21983 			return (err);
21984 		ipif_down_tail(ipif);
21985 		need_up = B_TRUE;
21986 	}
21987 
21988 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
21989 	return (err);
21990 }
21991 
21992 static int
21993 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
21994     queue_t *q, mblk_t *mp, boolean_t need_up)
21995 {
21996 	int	err = 0;
21997 
21998 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
21999 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22000 
22001 	/* Set the new zone id. */
22002 	ipif->ipif_zoneid = zoneid;
22003 
22004 	/* Update sctp list */
22005 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22006 
22007 	if (need_up) {
22008 		/*
22009 		 * Now bring the interface back up.  If this
22010 		 * is the only IPIF for the ILL, ipif_up
22011 		 * will have to re-bind to the device, so
22012 		 * we may get back EINPROGRESS, in which
22013 		 * case, this IOCTL will get completed in
22014 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22015 		 */
22016 		err = ipif_up(ipif, q, mp);
22017 	}
22018 	return (err);
22019 }
22020 
22021 /* ARGSUSED */
22022 int
22023 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22024     ip_ioctl_cmd_t *ipip, void *if_req)
22025 {
22026 	struct lifreq *lifr = (struct lifreq *)if_req;
22027 	zoneid_t zoneid;
22028 	zone_t *zptr;
22029 	zone_status_t status;
22030 
22031 	ASSERT(ipif->ipif_id != 0);
22032 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22033 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22034 		zoneid = GLOBAL_ZONEID;
22035 
22036 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22037 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22038 
22039 	/*
22040 	 * We recheck the zone status to resolve the following race condition:
22041 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22042 	 * 2) hme0:1 is up and can't be brought down right away;
22043 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22044 	 * 3) zone "myzone" is halted; the zone status switches to
22045 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22046 	 * the interfaces to remove - hme0:1 is not returned because it's not
22047 	 * yet in "myzone", so it won't be removed;
22048 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22049 	 * status check here, we would have hme0:1 in "myzone" after it's been
22050 	 * destroyed.
22051 	 * Note that if the status check fails, we need to bring the interface
22052 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22053 	 * ipif_up_done[_v6]().
22054 	 */
22055 	status = ZONE_IS_UNINITIALIZED;
22056 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22057 		status = zone_status_get(zptr);
22058 		zone_rele(zptr);
22059 	}
22060 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22061 		if (ipif->ipif_isv6) {
22062 			(void) ipif_up_done_v6(ipif);
22063 		} else {
22064 			(void) ipif_up_done(ipif);
22065 		}
22066 		return (EINVAL);
22067 	}
22068 
22069 	ipif_down_tail(ipif);
22070 
22071 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22072 	    B_TRUE));
22073 }
22074 
22075 /* ARGSUSED */
22076 int
22077 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22078 	ip_ioctl_cmd_t *ipip, void *ifreq)
22079 {
22080 	struct lifreq	*lifr = ifreq;
22081 
22082 	ASSERT(q->q_next == NULL);
22083 	ASSERT(CONN_Q(q));
22084 
22085 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22086 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22087 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22088 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22089 
22090 	return (0);
22091 }
22092 
22093 
22094 /* Find the previous ILL in this usesrc group */
22095 static ill_t *
22096 ill_prev_usesrc(ill_t *uill)
22097 {
22098 	ill_t *ill;
22099 
22100 	for (ill = uill->ill_usesrc_grp_next;
22101 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22102 	    ill = ill->ill_usesrc_grp_next)
22103 		/* do nothing */;
22104 	return (ill);
22105 }
22106 
22107 /*
22108  * Release all members of the usesrc group. This routine is called
22109  * from ill_delete when the interface being unplumbed is the
22110  * group head.
22111  */
22112 static void
22113 ill_disband_usesrc_group(ill_t *uill)
22114 {
22115 	ill_t *next_ill, *tmp_ill;
22116 	ASSERT(RW_WRITE_HELD(&ill_g_usesrc_lock));
22117 	next_ill = uill->ill_usesrc_grp_next;
22118 
22119 	do {
22120 		ASSERT(next_ill != NULL);
22121 		tmp_ill = next_ill->ill_usesrc_grp_next;
22122 		ASSERT(tmp_ill != NULL);
22123 		next_ill->ill_usesrc_grp_next = NULL;
22124 		next_ill->ill_usesrc_ifindex = 0;
22125 		next_ill = tmp_ill;
22126 	} while (next_ill->ill_usesrc_ifindex != 0);
22127 	uill->ill_usesrc_grp_next = NULL;
22128 }
22129 
22130 /*
22131  * Remove the client usesrc ILL from the list and relink to a new list
22132  */
22133 int
22134 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22135 {
22136 	ill_t *ill, *tmp_ill;
22137 
22138 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22139 	    (uill != NULL) && RW_WRITE_HELD(&ill_g_usesrc_lock));
22140 
22141 	/*
22142 	 * Check if the usesrc client ILL passed in is not already
22143 	 * in use as a usesrc ILL i.e one whose source address is
22144 	 * in use OR a usesrc ILL is not already in use as a usesrc
22145 	 * client ILL
22146 	 */
22147 	if ((ucill->ill_usesrc_ifindex == 0) ||
22148 	    (uill->ill_usesrc_ifindex != 0)) {
22149 		return (-1);
22150 	}
22151 
22152 	ill = ill_prev_usesrc(ucill);
22153 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22154 
22155 	/* Remove from the current list */
22156 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22157 		/* Only two elements in the list */
22158 		ASSERT(ill->ill_usesrc_ifindex == 0);
22159 		ill->ill_usesrc_grp_next = NULL;
22160 	} else {
22161 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22162 	}
22163 
22164 	if (ifindex == 0) {
22165 		ucill->ill_usesrc_ifindex = 0;
22166 		ucill->ill_usesrc_grp_next = NULL;
22167 		return (0);
22168 	}
22169 
22170 	ucill->ill_usesrc_ifindex = ifindex;
22171 	tmp_ill = uill->ill_usesrc_grp_next;
22172 	uill->ill_usesrc_grp_next = ucill;
22173 	ucill->ill_usesrc_grp_next =
22174 	    (tmp_ill != NULL) ? tmp_ill : uill;
22175 	return (0);
22176 }
22177 
22178 /*
22179  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22180  * ip.c for locking details.
22181  */
22182 /* ARGSUSED */
22183 int
22184 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22185     ip_ioctl_cmd_t *ipip, void *ifreq)
22186 {
22187 	struct lifreq *lifr = (struct lifreq *)ifreq;
22188 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22189 	    ill_flag_changed = B_FALSE;
22190 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22191 	int err = 0, ret;
22192 	uint_t ifindex;
22193 	phyint_t *us_phyint, *us_cli_phyint;
22194 	ipsq_t *ipsq = NULL;
22195 
22196 	ASSERT(IAM_WRITER_IPIF(ipif));
22197 	ASSERT(q->q_next == NULL);
22198 	ASSERT(CONN_Q(q));
22199 
22200 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22201 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22202 
22203 	ASSERT(us_cli_phyint != NULL);
22204 
22205 	/*
22206 	 * If the client ILL is being used for IPMP, abort.
22207 	 * Note, this can be done before ipsq_try_enter since we are already
22208 	 * exclusive on this ILL
22209 	 */
22210 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22211 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22212 		return (EINVAL);
22213 	}
22214 
22215 	ifindex = lifr->lifr_index;
22216 	if (ifindex == 0) {
22217 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22218 			/* non usesrc group interface, nothing to reset */
22219 			return (0);
22220 		}
22221 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22222 		/* valid reset request */
22223 		reset_flg = B_TRUE;
22224 	}
22225 
22226 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22227 	    ip_process_ioctl, &err);
22228 
22229 	if (usesrc_ill == NULL) {
22230 		return (err);
22231 	}
22232 
22233 	/*
22234 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22235 	 * group nor can either of the interfaces be used for standy. So
22236 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22237 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22238 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22239 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22240 	 * the usesrc_cli_ill
22241 	 */
22242 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22243 	    NEW_OP, B_TRUE);
22244 	if (ipsq == NULL) {
22245 		err = EINPROGRESS;
22246 		/* Operation enqueued on the ipsq of the usesrc ILL */
22247 		goto done;
22248 	}
22249 
22250 	/* Check if the usesrc_ill is used for IPMP */
22251 	us_phyint = usesrc_ill->ill_phyint;
22252 	if ((us_phyint->phyint_groupname != NULL) ||
22253 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22254 		err = EINVAL;
22255 		goto done;
22256 	}
22257 
22258 	/*
22259 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22260 	 * already a client then return EINVAL
22261 	 */
22262 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22263 		err = EINVAL;
22264 		goto done;
22265 	}
22266 
22267 	/*
22268 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22269 	 * be then this is a duplicate operation.
22270 	 */
22271 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22272 		err = 0;
22273 		goto done;
22274 	}
22275 
22276 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22277 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22278 	    usesrc_ill->ill_isv6));
22279 
22280 	/*
22281 	 * The next step ensures that no new ires will be created referencing
22282 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22283 	 * we go through an ire walk deleting all ire caches that reference
22284 	 * the client ill. New ires referencing the client ill that are added
22285 	 * to the ire table before the ILL_CHANGING flag is set, will be
22286 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22287 	 * the client ill while the ILL_CHANGING flag is set will be failed
22288 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22289 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22290 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22291 	 * belong to the same usesrc group.
22292 	 */
22293 	mutex_enter(&usesrc_cli_ill->ill_lock);
22294 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22295 	mutex_exit(&usesrc_cli_ill->ill_lock);
22296 	ill_flag_changed = B_TRUE;
22297 
22298 	if (ipif->ipif_isv6)
22299 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22300 		    ALL_ZONES);
22301 	else
22302 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22303 		    ALL_ZONES);
22304 
22305 	/*
22306 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22307 	 * and the ill_usesrc_ifindex fields
22308 	 */
22309 	rw_enter(&ill_g_usesrc_lock, RW_WRITER);
22310 
22311 	if (reset_flg) {
22312 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22313 		if (ret != 0) {
22314 			err = EINVAL;
22315 		}
22316 		rw_exit(&ill_g_usesrc_lock);
22317 		goto done;
22318 	}
22319 
22320 	/*
22321 	 * Four possibilities to consider:
22322 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22323 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22324 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22325 	 * 4. Both are part of their respective usesrc groups
22326 	 */
22327 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22328 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22329 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22330 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22331 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22332 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22333 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22334 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22335 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22336 		/* Insert at head of list */
22337 		usesrc_cli_ill->ill_usesrc_grp_next =
22338 		    usesrc_ill->ill_usesrc_grp_next;
22339 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22340 	} else {
22341 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22342 		    ifindex);
22343 		if (ret != 0)
22344 			err = EINVAL;
22345 	}
22346 	rw_exit(&ill_g_usesrc_lock);
22347 
22348 done:
22349 	if (ill_flag_changed) {
22350 		mutex_enter(&usesrc_cli_ill->ill_lock);
22351 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22352 		mutex_exit(&usesrc_cli_ill->ill_lock);
22353 	}
22354 	if (ipsq != NULL)
22355 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
22356 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22357 	ill_refrele(usesrc_ill);
22358 	return (err);
22359 }
22360 
22361 /*
22362  * comparison function used by avl.
22363  */
22364 static int
22365 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22366 {
22367 
22368 	uint_t index;
22369 
22370 	ASSERT(phyip != NULL && index_ptr != NULL);
22371 
22372 	index = *((uint_t *)index_ptr);
22373 	/*
22374 	 * let the phyint with the lowest index be on top.
22375 	 */
22376 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22377 		return (1);
22378 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22379 		return (-1);
22380 	return (0);
22381 }
22382 
22383 /*
22384  * comparison function used by avl.
22385  */
22386 static int
22387 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22388 {
22389 	ill_t *ill;
22390 	int res = 0;
22391 
22392 	ASSERT(phyip != NULL && name_ptr != NULL);
22393 
22394 	if (((phyint_t *)phyip)->phyint_illv4)
22395 		ill = ((phyint_t *)phyip)->phyint_illv4;
22396 	else
22397 		ill = ((phyint_t *)phyip)->phyint_illv6;
22398 	ASSERT(ill != NULL);
22399 
22400 	res = strcmp(ill->ill_name, (char *)name_ptr);
22401 	if (res > 0)
22402 		return (1);
22403 	else if (res < 0)
22404 		return (-1);
22405 	return (0);
22406 }
22407 /*
22408  * This function is called from ill_delete when the ill is being
22409  * unplumbed. We remove the reference from the phyint and we also
22410  * free the phyint when there are no more references to it.
22411  */
22412 static void
22413 ill_phyint_free(ill_t *ill)
22414 {
22415 	phyint_t *phyi;
22416 	phyint_t *next_phyint;
22417 	ipsq_t *cur_ipsq;
22418 
22419 	ASSERT(ill->ill_phyint != NULL);
22420 
22421 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
22422 	phyi = ill->ill_phyint;
22423 	ill->ill_phyint = NULL;
22424 	/*
22425 	 * ill_init allocates a phyint always to store the copy
22426 	 * of flags relevant to phyint. At that point in time, we could
22427 	 * not assign the name and hence phyint_illv4/v6 could not be
22428 	 * initialized. Later in ipif_set_values, we assign the name to
22429 	 * the ill, at which point in time we assign phyint_illv4/v6.
22430 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22431 	 */
22432 	if (ill->ill_flags & ILLF_IPV6) {
22433 		phyi->phyint_illv6 = NULL;
22434 	} else {
22435 		phyi->phyint_illv4 = NULL;
22436 	}
22437 	/*
22438 	 * ipif_down removes it from the group when the last ipif goes
22439 	 * down.
22440 	 */
22441 	ASSERT(ill->ill_group == NULL);
22442 
22443 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22444 		return;
22445 
22446 	/*
22447 	 * Make sure this phyint was put in the list.
22448 	 */
22449 	if (phyi->phyint_ifindex > 0) {
22450 		avl_remove(&phyint_g_list.phyint_list_avl_by_index,
22451 		    phyi);
22452 		avl_remove(&phyint_g_list.phyint_list_avl_by_name,
22453 		    phyi);
22454 	}
22455 	/*
22456 	 * remove phyint from the ipsq list.
22457 	 */
22458 	cur_ipsq = phyi->phyint_ipsq;
22459 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22460 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22461 	} else {
22462 		next_phyint = cur_ipsq->ipsq_phyint_list;
22463 		while (next_phyint != NULL) {
22464 			if (next_phyint->phyint_ipsq_next == phyi) {
22465 				next_phyint->phyint_ipsq_next =
22466 					phyi->phyint_ipsq_next;
22467 				break;
22468 			}
22469 			next_phyint = next_phyint->phyint_ipsq_next;
22470 		}
22471 		ASSERT(next_phyint != NULL);
22472 	}
22473 	IPSQ_DEC_REF(cur_ipsq);
22474 
22475 	if (phyi->phyint_groupname_len != 0) {
22476 		ASSERT(phyi->phyint_groupname != NULL);
22477 		mi_free(phyi->phyint_groupname);
22478 	}
22479 	mi_free(phyi);
22480 }
22481 
22482 /*
22483  * Attach the ill to the phyint structure which can be shared by both
22484  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22485  * function is called from ipif_set_values and ill_lookup_on_name (for
22486  * loopback) where we know the name of the ill. We lookup the ill and if
22487  * there is one present already with the name use that phyint. Otherwise
22488  * reuse the one allocated by ill_init.
22489  */
22490 static void
22491 ill_phyint_reinit(ill_t *ill)
22492 {
22493 	boolean_t isv6 = ill->ill_isv6;
22494 	phyint_t *phyi_old;
22495 	phyint_t *phyi;
22496 	avl_index_t where = 0;
22497 	ill_t	*ill_other = NULL;
22498 	ipsq_t	*ipsq;
22499 
22500 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
22501 
22502 	phyi_old = ill->ill_phyint;
22503 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22504 	    phyi_old->phyint_illv6 == NULL));
22505 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22506 	    phyi_old->phyint_illv4 == NULL));
22507 	ASSERT(phyi_old->phyint_ifindex == 0);
22508 
22509 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_name,
22510 	    ill->ill_name, &where);
22511 
22512 	/*
22513 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22514 	 *    the global list of ills. So no other thread could have located
22515 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22516 	 * 2. Now locate the other protocol instance of this ill.
22517 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22518 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22519 	 *    of neither ill can change.
22520 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22521 	 *    other ill.
22522 	 * 5. Release all locks.
22523 	 */
22524 
22525 	/*
22526 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22527 	 * we are initializing IPv4.
22528 	 */
22529 	if (phyi != NULL) {
22530 		ill_other = (isv6) ? phyi->phyint_illv4 :
22531 		    phyi->phyint_illv6;
22532 		ASSERT(ill_other->ill_phyint != NULL);
22533 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22534 		    (!isv6 && ill_other->ill_isv6));
22535 		GRAB_ILL_LOCKS(ill, ill_other);
22536 		/*
22537 		 * We are potentially throwing away phyint_flags which
22538 		 * could be different from the one that we obtain from
22539 		 * ill_other->ill_phyint. But it is okay as we are assuming
22540 		 * that the state maintained within IP is correct.
22541 		 */
22542 		mutex_enter(&phyi->phyint_lock);
22543 		if (isv6) {
22544 			ASSERT(phyi->phyint_illv6 == NULL);
22545 			phyi->phyint_illv6 = ill;
22546 		} else {
22547 			ASSERT(phyi->phyint_illv4 == NULL);
22548 			phyi->phyint_illv4 = ill;
22549 		}
22550 		/*
22551 		 * This is a new ill, currently undergoing SLIFNAME
22552 		 * So we could not have joined an IPMP group until now.
22553 		 */
22554 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22555 		    phyi_old->phyint_groupname == NULL);
22556 
22557 		/*
22558 		 * This phyi_old is going away. Decref ipsq_refs and
22559 		 * assert it is zero. The ipsq itself will be freed in
22560 		 * ipsq_exit
22561 		 */
22562 		ipsq = phyi_old->phyint_ipsq;
22563 		IPSQ_DEC_REF(ipsq);
22564 		ASSERT(ipsq->ipsq_refs == 0);
22565 		/* Get the singleton phyint out of the ipsq list */
22566 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22567 		ipsq->ipsq_phyint_list = NULL;
22568 		phyi_old->phyint_illv4 = NULL;
22569 		phyi_old->phyint_illv6 = NULL;
22570 		mi_free(phyi_old);
22571 	} else {
22572 		mutex_enter(&ill->ill_lock);
22573 		/*
22574 		 * We don't need to acquire any lock, since
22575 		 * the ill is not yet visible globally  and we
22576 		 * have not yet released the ill_g_lock.
22577 		 */
22578 		phyi = phyi_old;
22579 		mutex_enter(&phyi->phyint_lock);
22580 		/* XXX We need a recovery strategy here. */
22581 		if (!phyint_assign_ifindex(phyi))
22582 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22583 
22584 		avl_insert(&phyint_g_list.phyint_list_avl_by_name,
22585 		    (void *)phyi, where);
22586 
22587 		(void) avl_find(&phyint_g_list.phyint_list_avl_by_index,
22588 		    &phyi->phyint_ifindex, &where);
22589 		avl_insert(&phyint_g_list.phyint_list_avl_by_index,
22590 		    (void *)phyi, where);
22591 	}
22592 
22593 	/*
22594 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22595 	 * pending mp is not affected because that is per ill basis.
22596 	 */
22597 	ill->ill_phyint = phyi;
22598 
22599 	/*
22600 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22601 	 * We do this here as when the first ipif was allocated,
22602 	 * ipif_allocate does not know the right interface index.
22603 	 */
22604 
22605 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22606 	/*
22607 	 * Now that the phyint's ifindex has been assigned, complete the
22608 	 * remaining
22609 	 */
22610 
22611 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22612 	if (ill->ill_isv6) {
22613 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22614 		    ill->ill_phyint->phyint_ifindex;
22615 	}
22616 
22617 	/*
22618 	 * Generate an event within the hooks framework to indicate that
22619 	 * a new interface has just been added to IP.  For this event to
22620 	 * be generated, the network interface must, at least, have an
22621 	 * ifindex assigned to it.
22622 	 *
22623 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22624 	 * that the ordering of delivered events to listeners matches the
22625 	 * order of them in the kernel.
22626 	 *
22627 	 * This function could be called from ill_lookup_on_name. In that case
22628 	 * the interface is loopback "lo", which will not generate a NIC event.
22629 	 */
22630 	if (ill->ill_name_length <= 2 ||
22631 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22632 		hook_nic_event_t *info;
22633 		if ((info = ill->ill_nic_event_info) != NULL) {
22634 			ip2dbg(("ill_phyint_reinit: unexpected nic event %d "
22635 			    "attached for %s\n", info->hne_event,
22636 			    ill->ill_name));
22637 			if (info->hne_data != NULL)
22638 				kmem_free(info->hne_data, info->hne_datalen);
22639 			kmem_free(info, sizeof (hook_nic_event_t));
22640 		}
22641 
22642 		info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
22643 		if (info != NULL) {
22644 			info->hne_nic = ill->ill_phyint->phyint_ifindex;
22645 			info->hne_lif = 0;
22646 			info->hne_event = NE_PLUMB;
22647 			info->hne_family = ill->ill_isv6 ? ipv6 : ipv4;
22648 			info->hne_data = kmem_alloc(ill->ill_name_length,
22649 			    KM_NOSLEEP);
22650 			if (info->hne_data != NULL) {
22651 				info->hne_datalen = ill->ill_name_length;
22652 				bcopy(ill->ill_name, info->hne_data,
22653 				    info->hne_datalen);
22654 			} else {
22655 				ip2dbg(("ill_phyint_reinit: could not attach "
22656 				    "ill_name information for PLUMB nic event "
22657 				    "of %s (ENOMEM)\n", ill->ill_name));
22658 				kmem_free(info, sizeof (hook_nic_event_t));
22659 			}
22660 		} else
22661 			ip2dbg(("ill_phyint_reinit: could not attach PLUMB nic "
22662 			    "event information for %s (ENOMEM)\n",
22663 			    ill->ill_name));
22664 
22665 		ill->ill_nic_event_info = info;
22666 	}
22667 
22668 	RELEASE_ILL_LOCKS(ill, ill_other);
22669 	mutex_exit(&phyi->phyint_lock);
22670 }
22671 
22672 /*
22673  * Notify any downstream modules of the name of this interface.
22674  * An M_IOCTL is used even though we don't expect a successful reply.
22675  * Any reply message from the driver (presumably an M_IOCNAK) will
22676  * eventually get discarded somewhere upstream.  The message format is
22677  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22678  * to IP.
22679  */
22680 static void
22681 ip_ifname_notify(ill_t *ill, queue_t *q)
22682 {
22683 	mblk_t *mp1, *mp2;
22684 	struct iocblk *iocp;
22685 	struct lifreq *lifr;
22686 
22687 	mp1 = mkiocb(SIOCSLIFNAME);
22688 	if (mp1 == NULL)
22689 		return;
22690 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22691 	if (mp2 == NULL) {
22692 		freeb(mp1);
22693 		return;
22694 	}
22695 
22696 	mp1->b_cont = mp2;
22697 	iocp = (struct iocblk *)mp1->b_rptr;
22698 	iocp->ioc_count = sizeof (struct lifreq);
22699 
22700 	lifr = (struct lifreq *)mp2->b_rptr;
22701 	mp2->b_wptr += sizeof (struct lifreq);
22702 	bzero(lifr, sizeof (struct lifreq));
22703 
22704 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22705 	lifr->lifr_ppa = ill->ill_ppa;
22706 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22707 
22708 	putnext(q, mp1);
22709 }
22710 
22711 static boolean_t ip_trash_timer_started = B_FALSE;
22712 
22713 static int
22714 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22715 {
22716 	int err;
22717 
22718 	/* Set the obsolete NDD per-interface forwarding name. */
22719 	err = ill_set_ndd_name(ill);
22720 	if (err != 0) {
22721 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22722 		    err);
22723 	}
22724 
22725 	/* Tell downstream modules where they are. */
22726 	ip_ifname_notify(ill, q);
22727 
22728 	/*
22729 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22730 	 * Error cases are ENOMEM ...
22731 	 */
22732 	err = ill_dl_phys(ill, ipif, mp, q);
22733 
22734 	/*
22735 	 * If there is no IRE expiration timer running, get one started.
22736 	 * igmp and mld timers will be triggered by the first multicast
22737 	 */
22738 	if (!ip_trash_timer_started) {
22739 		/*
22740 		 * acquire the lock and check again.
22741 		 */
22742 		mutex_enter(&ip_trash_timer_lock);
22743 		if (!ip_trash_timer_started) {
22744 			ip_ire_expire_id = timeout(ip_trash_timer_expire, NULL,
22745 			    MSEC_TO_TICK(ip_timer_interval));
22746 			ip_trash_timer_started = B_TRUE;
22747 		}
22748 		mutex_exit(&ip_trash_timer_lock);
22749 	}
22750 
22751 	if (ill->ill_isv6) {
22752 		mutex_enter(&mld_slowtimeout_lock);
22753 		if (mld_slowtimeout_id == 0) {
22754 			mld_slowtimeout_id = timeout(mld_slowtimo, NULL,
22755 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22756 		}
22757 		mutex_exit(&mld_slowtimeout_lock);
22758 	} else {
22759 		mutex_enter(&igmp_slowtimeout_lock);
22760 		if (igmp_slowtimeout_id == 0) {
22761 			igmp_slowtimeout_id = timeout(igmp_slowtimo, NULL,
22762 				MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22763 		}
22764 		mutex_exit(&igmp_slowtimeout_lock);
22765 	}
22766 
22767 	return (err);
22768 }
22769 
22770 /*
22771  * Common routine for ppa and ifname setting. Should be called exclusive.
22772  *
22773  * Returns EINPROGRESS when mp has been consumed by queueing it on
22774  * ill_pending_mp and the ioctl will complete in ip_rput.
22775  *
22776  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22777  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22778  * For SLIFNAME, we pass these values back to the userland.
22779  */
22780 static int
22781 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22782 {
22783 	ill_t	*ill;
22784 	ipif_t	*ipif;
22785 	ipsq_t	*ipsq;
22786 	char	*ppa_ptr;
22787 	char	*old_ptr;
22788 	char	old_char;
22789 	int	error;
22790 
22791 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
22792 	ASSERT(q->q_next != NULL);
22793 	ASSERT(interf_name != NULL);
22794 
22795 	ill = (ill_t *)q->q_ptr;
22796 
22797 	ASSERT(ill->ill_name[0] == '\0');
22798 	ASSERT(IAM_WRITER_ILL(ill));
22799 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
22800 	ASSERT(ill->ill_ppa == UINT_MAX);
22801 
22802 	/* The ppa is sent down by ifconfig or is chosen */
22803 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
22804 		return (EINVAL);
22805 	}
22806 
22807 	/*
22808 	 * make sure ppa passed in is same as ppa in the name.
22809 	 * This check is not made when ppa == UINT_MAX in that case ppa
22810 	 * in the name could be anything. System will choose a ppa and
22811 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
22812 	 */
22813 	if (*new_ppa_ptr != UINT_MAX) {
22814 		/* stoi changes the pointer */
22815 		old_ptr = ppa_ptr;
22816 		/*
22817 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
22818 		 * (they don't have an externally visible ppa).  We assign one
22819 		 * here so that we can manage the interface.  Note that in
22820 		 * the past this value was always 0 for DLPI 1 drivers.
22821 		 */
22822 		if (*new_ppa_ptr == 0)
22823 			*new_ppa_ptr = stoi(&old_ptr);
22824 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
22825 			return (EINVAL);
22826 	}
22827 	/*
22828 	 * terminate string before ppa
22829 	 * save char at that location.
22830 	 */
22831 	old_char = ppa_ptr[0];
22832 	ppa_ptr[0] = '\0';
22833 
22834 	ill->ill_ppa = *new_ppa_ptr;
22835 	/*
22836 	 * Finish as much work now as possible before calling ill_glist_insert
22837 	 * which makes the ill globally visible and also merges it with the
22838 	 * other protocol instance of this phyint. The remaining work is
22839 	 * done after entering the ipsq which may happen sometime later.
22840 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
22841 	 */
22842 	ipif = ill->ill_ipif;
22843 
22844 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
22845 	ipif_assign_seqid(ipif);
22846 
22847 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
22848 		ill->ill_flags |= ILLF_IPV4;
22849 
22850 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
22851 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
22852 
22853 	if (ill->ill_flags & ILLF_IPV6) {
22854 
22855 		ill->ill_isv6 = B_TRUE;
22856 		if (ill->ill_rq != NULL) {
22857 			ill->ill_rq->q_qinfo = &rinit_ipv6;
22858 			ill->ill_wq->q_qinfo = &winit_ipv6;
22859 		}
22860 
22861 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
22862 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
22863 		ipif->ipif_v6src_addr = ipv6_all_zeros;
22864 		ipif->ipif_v6subnet = ipv6_all_zeros;
22865 		ipif->ipif_v6net_mask = ipv6_all_zeros;
22866 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
22867 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
22868 		/*
22869 		 * point-to-point or Non-mulicast capable
22870 		 * interfaces won't do NUD unless explicitly
22871 		 * configured to do so.
22872 		 */
22873 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
22874 		    !(ill->ill_flags & ILLF_MULTICAST)) {
22875 			ill->ill_flags |= ILLF_NONUD;
22876 		}
22877 		/* Make sure IPv4 specific flag is not set on IPv6 if */
22878 		if (ill->ill_flags & ILLF_NOARP) {
22879 			/*
22880 			 * Note: xresolv interfaces will eventually need
22881 			 * NOARP set here as well, but that will require
22882 			 * those external resolvers to have some
22883 			 * knowledge of that flag and act appropriately.
22884 			 * Not to be changed at present.
22885 			 */
22886 			ill->ill_flags &= ~ILLF_NOARP;
22887 		}
22888 		/*
22889 		 * Set the ILLF_ROUTER flag according to the global
22890 		 * IPv6 forwarding policy.
22891 		 */
22892 		if (ipv6_forward != 0)
22893 			ill->ill_flags |= ILLF_ROUTER;
22894 	} else if (ill->ill_flags & ILLF_IPV4) {
22895 		ill->ill_isv6 = B_FALSE;
22896 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
22897 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
22898 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
22899 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
22900 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
22901 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
22902 		/*
22903 		 * Set the ILLF_ROUTER flag according to the global
22904 		 * IPv4 forwarding policy.
22905 		 */
22906 		if (ip_g_forward != 0)
22907 			ill->ill_flags |= ILLF_ROUTER;
22908 	}
22909 
22910 	ASSERT(ill->ill_phyint != NULL);
22911 
22912 	/*
22913 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
22914 	 * be completed in ill_glist_insert -> ill_phyint_reinit
22915 	 */
22916 	if (!ill_allocate_mibs(ill))
22917 		return (ENOMEM);
22918 
22919 	/*
22920 	 * Pick a default sap until we get the DL_INFO_ACK back from
22921 	 * the driver.
22922 	 */
22923 	if (ill->ill_sap == 0) {
22924 		if (ill->ill_isv6)
22925 			ill->ill_sap  = IP6_DL_SAP;
22926 		else
22927 			ill->ill_sap  = IP_DL_SAP;
22928 	}
22929 
22930 	ill->ill_ifname_pending = 1;
22931 	ill->ill_ifname_pending_err = 0;
22932 
22933 	ill_refhold(ill);
22934 	rw_enter(&ill_g_lock, RW_WRITER);
22935 	if ((error = ill_glist_insert(ill, interf_name,
22936 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
22937 		ill->ill_ppa = UINT_MAX;
22938 		ill->ill_name[0] = '\0';
22939 		/*
22940 		 * undo null termination done above.
22941 		 */
22942 		ppa_ptr[0] = old_char;
22943 		rw_exit(&ill_g_lock);
22944 		ill_refrele(ill);
22945 		return (error);
22946 	}
22947 
22948 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
22949 
22950 	/*
22951 	 * When we return the buffer pointed to by interf_name should contain
22952 	 * the same name as in ill_name.
22953 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
22954 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
22955 	 * so copy full name and update the ppa ptr.
22956 	 * When ppa passed in != UINT_MAX all values are correct just undo
22957 	 * null termination, this saves a bcopy.
22958 	 */
22959 	if (*new_ppa_ptr == UINT_MAX) {
22960 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
22961 		*new_ppa_ptr = ill->ill_ppa;
22962 	} else {
22963 		/*
22964 		 * undo null termination done above.
22965 		 */
22966 		ppa_ptr[0] = old_char;
22967 	}
22968 
22969 	/* Let SCTP know about this ILL */
22970 	sctp_update_ill(ill, SCTP_ILL_INSERT);
22971 
22972 	/* and also about the first ipif */
22973 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
22974 
22975 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
22976 	    B_TRUE);
22977 
22978 	rw_exit(&ill_g_lock);
22979 	ill_refrele(ill);
22980 	if (ipsq == NULL)
22981 		return (EINPROGRESS);
22982 
22983 	/*
22984 	 * Need to set the ipsq_current_ipif now, if we have changed ipsq
22985 	 * due to the phyint merge in ill_phyint_reinit.
22986 	 */
22987 	ASSERT(ipsq->ipsq_current_ipif == NULL ||
22988 		ipsq->ipsq_current_ipif == ipif);
22989 	ipsq->ipsq_current_ipif = ipif;
22990 	ipsq->ipsq_last_cmd = SIOCSLIFNAME;
22991 	error = ipif_set_values_tail(ill, ipif, mp, q);
22992 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
22993 	if (error != 0 && error != EINPROGRESS) {
22994 		/*
22995 		 * restore previous values
22996 		 */
22997 		ill->ill_isv6 = B_FALSE;
22998 	}
22999 	return (error);
23000 }
23001 
23002 
23003 extern void (*ip_cleanup_func)(void);
23004 
23005 void
23006 ipif_init(void)
23007 {
23008 	hrtime_t hrt;
23009 	int i;
23010 
23011 	/*
23012 	 * Can't call drv_getparm here as it is too early in the boot.
23013 	 * As we use ipif_src_random just for picking a different
23014 	 * source address everytime, this need not be really random.
23015 	 */
23016 	hrt = gethrtime();
23017 	ipif_src_random = ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23018 
23019 	for (i = 0; i < MAX_G_HEADS; i++) {
23020 		ill_g_heads[i].ill_g_list_head = (ill_if_t *)&ill_g_heads[i];
23021 		ill_g_heads[i].ill_g_list_tail = (ill_if_t *)&ill_g_heads[i];
23022 	}
23023 
23024 	avl_create(&phyint_g_list.phyint_list_avl_by_index,
23025 	    ill_phyint_compare_index,
23026 	    sizeof (phyint_t),
23027 	    offsetof(struct phyint, phyint_avl_by_index));
23028 	avl_create(&phyint_g_list.phyint_list_avl_by_name,
23029 	    ill_phyint_compare_name,
23030 	    sizeof (phyint_t),
23031 	    offsetof(struct phyint, phyint_avl_by_name));
23032 
23033 	ip_cleanup_func = ip_thread_exit;
23034 }
23035 
23036 /*
23037  * This is called by ip_rt_add when src_addr value is other than zero.
23038  * src_addr signifies the source address of the incoming packet. For
23039  * reverse tunnel route we need to create a source addr based routing
23040  * table. This routine creates ip_mrtun_table if it's empty and then
23041  * it adds the route entry hashed by source address. It verifies that
23042  * the outgoing interface is always a non-resolver interface (tunnel).
23043  */
23044 int
23045 ip_mrtun_rt_add(ipaddr_t in_src_addr, int flags, ipif_t *ipif_arg,
23046     ipif_t *src_ipif, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func)
23047 {
23048 	ire_t   *ire;
23049 	ire_t	*save_ire;
23050 	ipif_t  *ipif;
23051 	ill_t   *in_ill = NULL;
23052 	ill_t	*out_ill;
23053 	queue_t	*stq;
23054 	mblk_t	*dlureq_mp;
23055 	int	error;
23056 
23057 	if (ire_arg != NULL)
23058 		*ire_arg = NULL;
23059 	ASSERT(in_src_addr != INADDR_ANY);
23060 
23061 	ipif = ipif_arg;
23062 	if (ipif != NULL) {
23063 		out_ill = ipif->ipif_ill;
23064 	} else {
23065 		ip1dbg(("ip_mrtun_rt_add: ipif is NULL\n"));
23066 		return (EINVAL);
23067 	}
23068 
23069 	if (src_ipif == NULL) {
23070 		ip1dbg(("ip_mrtun_rt_add: src_ipif is NULL\n"));
23071 		return (EINVAL);
23072 	}
23073 	in_ill = src_ipif->ipif_ill;
23074 
23075 	/*
23076 	 * Check for duplicates. We don't need to
23077 	 * match out_ill, because the uniqueness of
23078 	 * a route is only dependent on src_addr and
23079 	 * in_ill.
23080 	 */
23081 	ire = ire_mrtun_lookup(in_src_addr, in_ill);
23082 	if (ire != NULL) {
23083 		ire_refrele(ire);
23084 		return (EEXIST);
23085 	}
23086 	if (ipif->ipif_net_type != IRE_IF_NORESOLVER) {
23087 		ip2dbg(("ip_mrtun_rt_add: outgoing interface is type %d\n",
23088 		    ipif->ipif_net_type));
23089 		return (EINVAL);
23090 	}
23091 
23092 	stq = ipif->ipif_wq;
23093 	ASSERT(stq != NULL);
23094 
23095 	/*
23096 	 * The outgoing interface must be non-resolver
23097 	 * interface.
23098 	 */
23099 	dlureq_mp = ill_dlur_gen(NULL,
23100 	    out_ill->ill_phys_addr_length, out_ill->ill_sap,
23101 	    out_ill->ill_sap_length);
23102 
23103 	if (dlureq_mp == NULL) {
23104 		ip1dbg(("ip_newroute: dlureq_mp NULL\n"));
23105 		return (ENOMEM);
23106 	}
23107 
23108 	/* Create the IRE. */
23109 
23110 	ire = ire_create(
23111 	    NULL,				/* Zero dst addr */
23112 	    NULL,				/* Zero mask */
23113 	    NULL,				/* Zero gateway addr */
23114 	    NULL,				/* Zero ipif_src addr */
23115 	    (uint8_t *)&in_src_addr,		/* in_src-addr */
23116 	    &ipif->ipif_mtu,
23117 	    NULL,
23118 	    NULL,				/* rfq */
23119 	    stq,
23120 	    IRE_MIPRTUN,
23121 	    dlureq_mp,
23122 	    ipif,
23123 	    in_ill,
23124 	    0,
23125 	    0,
23126 	    0,
23127 	    flags,
23128 	    &ire_uinfo_null,
23129 	    NULL,
23130 	    NULL);
23131 
23132 	if (ire == NULL) {
23133 		freeb(dlureq_mp);
23134 		return (ENOMEM);
23135 	}
23136 	ip2dbg(("ip_mrtun_rt_add: mrtun route is created with type %d\n",
23137 	    ire->ire_type));
23138 	save_ire = ire;
23139 	ASSERT(save_ire != NULL);
23140 	error = ire_add_mrtun(&ire, q, mp, func);
23141 	/*
23142 	 * If ire_add_mrtun() failed, the ire passed in was freed
23143 	 * so there is no need to do so here.
23144 	 */
23145 	if (error != 0) {
23146 		return (error);
23147 	}
23148 
23149 	/* Duplicate check */
23150 	if (ire != save_ire) {
23151 		/* route already exists by now */
23152 		ire_refrele(ire);
23153 		return (EEXIST);
23154 	}
23155 
23156 	if (ire_arg != NULL) {
23157 		/*
23158 		 * Store the ire that was just added. the caller
23159 		 * ip_rts_request responsible for doing ire_refrele()
23160 		 * on it.
23161 		 */
23162 		*ire_arg = ire;
23163 	} else {
23164 		ire_refrele(ire);	/* held in ire_add_mrtun */
23165 	}
23166 
23167 	return (0);
23168 }
23169 
23170 /*
23171  * It is called by ip_rt_delete() only when mipagent requests to delete
23172  * a reverse tunnel route that was added by ip_mrtun_rt_add() before.
23173  */
23174 
23175 int
23176 ip_mrtun_rt_delete(ipaddr_t in_src_addr, ipif_t *src_ipif)
23177 {
23178 	ire_t   *ire = NULL;
23179 
23180 	if (in_src_addr == INADDR_ANY)
23181 		return (EINVAL);
23182 	if (src_ipif == NULL)
23183 		return (EINVAL);
23184 
23185 	/* search if this route exists in the ip_mrtun_table */
23186 	ire = ire_mrtun_lookup(in_src_addr, src_ipif->ipif_ill);
23187 	if (ire == NULL) {
23188 		ip2dbg(("ip_mrtun_rt_delete: ire not found\n"));
23189 		return (ESRCH);
23190 	}
23191 	ire_delete(ire);
23192 	ire_refrele(ire);
23193 	return (0);
23194 }
23195 
23196 /*
23197  * Lookup the ipif corresponding to the onlink destination address. For
23198  * point-to-point interfaces, it matches with remote endpoint destination
23199  * address. For point-to-multipoint interfaces it only tries to match the
23200  * destination with the interface's subnet address. The longest, most specific
23201  * match is found to take care of such rare network configurations like -
23202  * le0: 129.146.1.1/16
23203  * le1: 129.146.2.2/24
23204  * It is used only by SO_DONTROUTE at the moment.
23205  */
23206 ipif_t *
23207 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid)
23208 {
23209 	ipif_t	*ipif, *best_ipif;
23210 	ill_t	*ill;
23211 	ill_walk_context_t ctx;
23212 
23213 	ASSERT(zoneid != ALL_ZONES);
23214 	best_ipif = NULL;
23215 
23216 	rw_enter(&ill_g_lock, RW_READER);
23217 	ill = ILL_START_WALK_V4(&ctx);
23218 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23219 		mutex_enter(&ill->ill_lock);
23220 		for (ipif = ill->ill_ipif; ipif != NULL;
23221 		    ipif = ipif->ipif_next) {
23222 			if (!IPIF_CAN_LOOKUP(ipif))
23223 				continue;
23224 			if (ipif->ipif_zoneid != zoneid &&
23225 			    ipif->ipif_zoneid != ALL_ZONES)
23226 				continue;
23227 			/*
23228 			 * Point-to-point case. Look for exact match with
23229 			 * destination address.
23230 			 */
23231 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23232 				if (ipif->ipif_pp_dst_addr == addr) {
23233 					ipif_refhold_locked(ipif);
23234 					mutex_exit(&ill->ill_lock);
23235 					rw_exit(&ill_g_lock);
23236 					if (best_ipif != NULL)
23237 						ipif_refrele(best_ipif);
23238 					return (ipif);
23239 				}
23240 			} else if (ipif->ipif_subnet == (addr &
23241 			    ipif->ipif_net_mask)) {
23242 				/*
23243 				 * Point-to-multipoint case. Looping through to
23244 				 * find the most specific match. If there are
23245 				 * multiple best match ipif's then prefer ipif's
23246 				 * that are UP. If there is only one best match
23247 				 * ipif and it is DOWN we must still return it.
23248 				 */
23249 				if ((best_ipif == NULL) ||
23250 				    (ipif->ipif_net_mask >
23251 				    best_ipif->ipif_net_mask) ||
23252 				    ((ipif->ipif_net_mask ==
23253 				    best_ipif->ipif_net_mask) &&
23254 				    ((ipif->ipif_flags & IPIF_UP) &&
23255 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23256 					ipif_refhold_locked(ipif);
23257 					mutex_exit(&ill->ill_lock);
23258 					rw_exit(&ill_g_lock);
23259 					if (best_ipif != NULL)
23260 						ipif_refrele(best_ipif);
23261 					best_ipif = ipif;
23262 					rw_enter(&ill_g_lock, RW_READER);
23263 					mutex_enter(&ill->ill_lock);
23264 				}
23265 			}
23266 		}
23267 		mutex_exit(&ill->ill_lock);
23268 	}
23269 	rw_exit(&ill_g_lock);
23270 	return (best_ipif);
23271 }
23272 
23273 
23274 /*
23275  * Save enough information so that we can recreate the IRE if
23276  * the interface goes down and then up.
23277  */
23278 static void
23279 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23280 {
23281 	mblk_t	*save_mp;
23282 
23283 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23284 	if (save_mp != NULL) {
23285 		ifrt_t	*ifrt;
23286 
23287 		save_mp->b_wptr += sizeof (ifrt_t);
23288 		ifrt = (ifrt_t *)save_mp->b_rptr;
23289 		bzero(ifrt, sizeof (ifrt_t));
23290 		ifrt->ifrt_type = ire->ire_type;
23291 		ifrt->ifrt_addr = ire->ire_addr;
23292 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23293 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23294 		ifrt->ifrt_mask = ire->ire_mask;
23295 		ifrt->ifrt_flags = ire->ire_flags;
23296 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23297 		mutex_enter(&ipif->ipif_saved_ire_lock);
23298 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23299 		ipif->ipif_saved_ire_mp = save_mp;
23300 		ipif->ipif_saved_ire_cnt++;
23301 		mutex_exit(&ipif->ipif_saved_ire_lock);
23302 	}
23303 }
23304 
23305 
23306 static void
23307 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23308 {
23309 	mblk_t	**mpp;
23310 	mblk_t	*mp;
23311 	ifrt_t	*ifrt;
23312 
23313 	/* Remove from ipif_saved_ire_mp list if it is there */
23314 	mutex_enter(&ipif->ipif_saved_ire_lock);
23315 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23316 	    mpp = &(*mpp)->b_cont) {
23317 		/*
23318 		 * On a given ipif, the triple of address, gateway and
23319 		 * mask is unique for each saved IRE (in the case of
23320 		 * ordinary interface routes, the gateway address is
23321 		 * all-zeroes).
23322 		 */
23323 		mp = *mpp;
23324 		ifrt = (ifrt_t *)mp->b_rptr;
23325 		if (ifrt->ifrt_addr == ire->ire_addr &&
23326 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23327 		    ifrt->ifrt_mask == ire->ire_mask) {
23328 			*mpp = mp->b_cont;
23329 			ipif->ipif_saved_ire_cnt--;
23330 			freeb(mp);
23331 			break;
23332 		}
23333 	}
23334 	mutex_exit(&ipif->ipif_saved_ire_lock);
23335 }
23336 
23337 
23338 /*
23339  * IP multirouting broadcast routes handling
23340  * Append CGTP broadcast IREs to regular ones created
23341  * at ifconfig time.
23342  */
23343 static void
23344 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst)
23345 {
23346 	ire_t *ire_prim;
23347 
23348 	ASSERT(ire != NULL);
23349 	ASSERT(ire_dst != NULL);
23350 
23351 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23352 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
23353 	if (ire_prim != NULL) {
23354 		/*
23355 		 * We are in the special case of broadcasts for
23356 		 * CGTP. We add an IRE_BROADCAST that holds
23357 		 * the RTF_MULTIRT flag, the destination
23358 		 * address of ire_dst and the low level
23359 		 * info of ire_prim. In other words, CGTP
23360 		 * broadcast is added to the redundant ipif.
23361 		 */
23362 		ipif_t *ipif_prim;
23363 		ire_t  *bcast_ire;
23364 
23365 		ipif_prim = ire_prim->ire_ipif;
23366 
23367 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23368 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23369 		    (void *)ire_dst, (void *)ire_prim,
23370 		    (void *)ipif_prim));
23371 
23372 		bcast_ire = ire_create(
23373 		    (uchar_t *)&ire->ire_addr,
23374 		    (uchar_t *)&ip_g_all_ones,
23375 		    (uchar_t *)&ire_dst->ire_src_addr,
23376 		    (uchar_t *)&ire->ire_gateway_addr,
23377 		    NULL,
23378 		    &ipif_prim->ipif_mtu,
23379 		    NULL,
23380 		    ipif_prim->ipif_rq,
23381 		    ipif_prim->ipif_wq,
23382 		    IRE_BROADCAST,
23383 		    ipif_prim->ipif_bcast_mp,
23384 		    ipif_prim,
23385 		    NULL,
23386 		    0,
23387 		    0,
23388 		    0,
23389 		    ire->ire_flags,
23390 		    &ire_uinfo_null,
23391 		    NULL,
23392 		    NULL);
23393 
23394 		if (bcast_ire != NULL) {
23395 
23396 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23397 			    B_FALSE) == 0) {
23398 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23399 				    "added bcast_ire %p\n",
23400 				    (void *)bcast_ire));
23401 
23402 				ipif_save_ire(bcast_ire->ire_ipif,
23403 				    bcast_ire);
23404 				ire_refrele(bcast_ire);
23405 			}
23406 		}
23407 		ire_refrele(ire_prim);
23408 	}
23409 }
23410 
23411 
23412 /*
23413  * IP multirouting broadcast routes handling
23414  * Remove the broadcast ire
23415  */
23416 static void
23417 ip_cgtp_bcast_delete(ire_t *ire)
23418 {
23419 	ire_t *ire_dst;
23420 
23421 	ASSERT(ire != NULL);
23422 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23423 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
23424 	if (ire_dst != NULL) {
23425 		ire_t *ire_prim;
23426 
23427 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23428 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
23429 		if (ire_prim != NULL) {
23430 			ipif_t *ipif_prim;
23431 			ire_t  *bcast_ire;
23432 
23433 			ipif_prim = ire_prim->ire_ipif;
23434 
23435 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23436 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23437 			    (void *)ire_dst, (void *)ire_prim,
23438 			    (void *)ipif_prim));
23439 
23440 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23441 			    ire->ire_gateway_addr,
23442 			    IRE_BROADCAST,
23443 			    ipif_prim, ALL_ZONES,
23444 			    NULL,
23445 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23446 			    MATCH_IRE_MASK);
23447 
23448 			if (bcast_ire != NULL) {
23449 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23450 				    "looked up bcast_ire %p\n",
23451 				    (void *)bcast_ire));
23452 				ipif_remove_ire(bcast_ire->ire_ipif,
23453 					bcast_ire);
23454 				ire_delete(bcast_ire);
23455 			}
23456 			ire_refrele(ire_prim);
23457 		}
23458 		ire_refrele(ire_dst);
23459 	}
23460 }
23461 
23462 /*
23463  * IPsec hardware acceleration capabilities related functions.
23464  */
23465 
23466 /*
23467  * Free a per-ill IPsec capabilities structure.
23468  */
23469 static void
23470 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23471 {
23472 	if (capab->auth_hw_algs != NULL)
23473 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23474 	if (capab->encr_hw_algs != NULL)
23475 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23476 	if (capab->encr_algparm != NULL)
23477 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23478 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23479 }
23480 
23481 /*
23482  * Allocate a new per-ill IPsec capabilities structure. This structure
23483  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23484  * an array which specifies, for each algorithm, whether this algorithm
23485  * is supported by the ill or not.
23486  */
23487 static ill_ipsec_capab_t *
23488 ill_ipsec_capab_alloc(void)
23489 {
23490 	ill_ipsec_capab_t *capab;
23491 	uint_t nelems;
23492 
23493 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23494 	if (capab == NULL)
23495 		return (NULL);
23496 
23497 	/* we need one bit per algorithm */
23498 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23499 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23500 
23501 	/* allocate memory to store algorithm flags */
23502 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23503 	if (capab->encr_hw_algs == NULL)
23504 		goto nomem;
23505 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23506 	if (capab->auth_hw_algs == NULL)
23507 		goto nomem;
23508 	/*
23509 	 * Leave encr_algparm NULL for now since we won't need it half
23510 	 * the time
23511 	 */
23512 	return (capab);
23513 
23514 nomem:
23515 	ill_ipsec_capab_free(capab);
23516 	return (NULL);
23517 }
23518 
23519 /*
23520  * Resize capability array.  Since we're exclusive, this is OK.
23521  */
23522 static boolean_t
23523 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23524 {
23525 	ipsec_capab_algparm_t *nalp, *oalp;
23526 	uint32_t olen, nlen;
23527 
23528 	oalp = capab->encr_algparm;
23529 	olen = capab->encr_algparm_size;
23530 
23531 	if (oalp != NULL) {
23532 		if (algid < capab->encr_algparm_end)
23533 			return (B_TRUE);
23534 	}
23535 
23536 	nlen = (algid + 1) * sizeof (*nalp);
23537 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23538 	if (nalp == NULL)
23539 		return (B_FALSE);
23540 
23541 	if (oalp != NULL) {
23542 		bcopy(oalp, nalp, olen);
23543 		kmem_free(oalp, olen);
23544 	}
23545 	capab->encr_algparm = nalp;
23546 	capab->encr_algparm_size = nlen;
23547 	capab->encr_algparm_end = algid + 1;
23548 
23549 	return (B_TRUE);
23550 }
23551 
23552 /*
23553  * Compare the capabilities of the specified ill with the protocol
23554  * and algorithms specified by the SA passed as argument.
23555  * If they match, returns B_TRUE, B_FALSE if they do not match.
23556  *
23557  * The ill can be passed as a pointer to it, or by specifying its index
23558  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23559  *
23560  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23561  * packet is eligible for hardware acceleration, and by
23562  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23563  * to a particular ill.
23564  */
23565 boolean_t
23566 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23567     ipsa_t *sa)
23568 {
23569 	boolean_t sa_isv6;
23570 	uint_t algid;
23571 	struct ill_ipsec_capab_s *cpp;
23572 	boolean_t need_refrele = B_FALSE;
23573 
23574 	if (ill == NULL) {
23575 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23576 		    NULL, NULL, NULL);
23577 		if (ill == NULL) {
23578 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23579 			return (B_FALSE);
23580 		}
23581 		need_refrele = B_TRUE;
23582 	}
23583 
23584 	/*
23585 	 * Use the address length specified by the SA to determine
23586 	 * if it corresponds to a IPv6 address, and fail the matching
23587 	 * if the isv6 flag passed as argument does not match.
23588 	 * Note: this check is used for SADB capability checking before
23589 	 * sending SA information to an ill.
23590 	 */
23591 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23592 	if (sa_isv6 != ill_isv6)
23593 		/* protocol mismatch */
23594 		goto done;
23595 
23596 	/*
23597 	 * Check if the ill supports the protocol, algorithm(s) and
23598 	 * key size(s) specified by the SA, and get the pointers to
23599 	 * the algorithms supported by the ill.
23600 	 */
23601 	switch (sa->ipsa_type) {
23602 
23603 	case SADB_SATYPE_ESP:
23604 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23605 			/* ill does not support ESP acceleration */
23606 			goto done;
23607 		cpp = ill->ill_ipsec_capab_esp;
23608 		algid = sa->ipsa_auth_alg;
23609 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23610 			goto done;
23611 		algid = sa->ipsa_encr_alg;
23612 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23613 			goto done;
23614 		if (algid < cpp->encr_algparm_end) {
23615 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23616 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23617 				goto done;
23618 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23619 				goto done;
23620 		}
23621 		break;
23622 
23623 	case SADB_SATYPE_AH:
23624 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23625 			/* ill does not support AH acceleration */
23626 			goto done;
23627 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23628 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23629 			goto done;
23630 		break;
23631 	}
23632 
23633 	if (need_refrele)
23634 		ill_refrele(ill);
23635 	return (B_TRUE);
23636 done:
23637 	if (need_refrele)
23638 		ill_refrele(ill);
23639 	return (B_FALSE);
23640 }
23641 
23642 
23643 /*
23644  * Add a new ill to the list of IPsec capable ills.
23645  * Called from ill_capability_ipsec_ack() when an ACK was received
23646  * indicating that IPsec hardware processing was enabled for an ill.
23647  *
23648  * ill must point to the ill for which acceleration was enabled.
23649  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23650  */
23651 static void
23652 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23653 {
23654 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23655 	uint_t sa_type;
23656 	uint_t ipproto;
23657 
23658 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23659 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23660 
23661 	switch (dl_cap) {
23662 	case DL_CAPAB_IPSEC_AH:
23663 		sa_type = SADB_SATYPE_AH;
23664 		ills = &ipsec_capab_ills_ah;
23665 		ipproto = IPPROTO_AH;
23666 		break;
23667 	case DL_CAPAB_IPSEC_ESP:
23668 		sa_type = SADB_SATYPE_ESP;
23669 		ills = &ipsec_capab_ills_esp;
23670 		ipproto = IPPROTO_ESP;
23671 		break;
23672 	}
23673 
23674 	rw_enter(&ipsec_capab_ills_lock, RW_WRITER);
23675 
23676 	/*
23677 	 * Add ill index to list of hardware accelerators. If
23678 	 * already in list, do nothing.
23679 	 */
23680 	for (cur_ill = *ills; cur_ill != NULL &&
23681 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23682 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23683 		;
23684 
23685 	if (cur_ill == NULL) {
23686 		/* if this is a new entry for this ill */
23687 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23688 		if (new_ill == NULL) {
23689 			rw_exit(&ipsec_capab_ills_lock);
23690 			return;
23691 		}
23692 
23693 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23694 		new_ill->ill_isv6 = ill->ill_isv6;
23695 		new_ill->next = *ills;
23696 		*ills = new_ill;
23697 	} else if (!sadb_resync) {
23698 		/* not resync'ing SADB and an entry exists for this ill */
23699 		rw_exit(&ipsec_capab_ills_lock);
23700 		return;
23701 	}
23702 
23703 	rw_exit(&ipsec_capab_ills_lock);
23704 
23705 	if (ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23706 		/*
23707 		 * IPsec module for protocol loaded, initiate dump
23708 		 * of the SADB to this ill.
23709 		 */
23710 		sadb_ill_download(ill, sa_type);
23711 }
23712 
23713 /*
23714  * Remove an ill from the list of IPsec capable ills.
23715  */
23716 static void
23717 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23718 {
23719 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23720 
23721 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23722 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23723 
23724 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipsec_capab_ills_ah :
23725 	    &ipsec_capab_ills_esp;
23726 
23727 	rw_enter(&ipsec_capab_ills_lock, RW_WRITER);
23728 
23729 	prev_ill = NULL;
23730 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23731 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23732 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23733 		;
23734 	if (cur_ill == NULL) {
23735 		/* entry not found */
23736 		rw_exit(&ipsec_capab_ills_lock);
23737 		return;
23738 	}
23739 	if (prev_ill == NULL) {
23740 		/* entry at front of list */
23741 		*ills = NULL;
23742 	} else {
23743 		prev_ill->next = cur_ill->next;
23744 	}
23745 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23746 	rw_exit(&ipsec_capab_ills_lock);
23747 }
23748 
23749 
23750 /*
23751  * Handling of DL_CONTROL_REQ messages that must be sent down to
23752  * an ill while having exclusive access.
23753  */
23754 /* ARGSUSED */
23755 static void
23756 ill_ipsec_capab_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
23757 {
23758 	ill_t *ill = (ill_t *)q->q_ptr;
23759 
23760 	ill_dlpi_send(ill, mp);
23761 }
23762 
23763 
23764 /*
23765  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23766  * supporting the specified IPsec protocol acceleration.
23767  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23768  * We free the mblk and, if sa is non-null, release the held referece.
23769  */
23770 void
23771 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa)
23772 {
23773 	ipsec_capab_ill_t *ici, *cur_ici;
23774 	ill_t *ill;
23775 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23776 
23777 	ici = (sa_type == SADB_SATYPE_AH) ? ipsec_capab_ills_ah :
23778 	    ipsec_capab_ills_esp;
23779 
23780 	rw_enter(&ipsec_capab_ills_lock, RW_READER);
23781 
23782 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23783 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23784 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL);
23785 
23786 		/*
23787 		 * Handle the case where the ill goes away while the SADB is
23788 		 * attempting to send messages.  If it's going away, it's
23789 		 * nuking its shadow SADB, so we don't care..
23790 		 */
23791 
23792 		if (ill == NULL)
23793 			continue;
23794 
23795 		if (sa != NULL) {
23796 			/*
23797 			 * Make sure capabilities match before
23798 			 * sending SA to ill.
23799 			 */
23800 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23801 			    cur_ici->ill_isv6, sa)) {
23802 				ill_refrele(ill);
23803 				continue;
23804 			}
23805 
23806 			mutex_enter(&sa->ipsa_lock);
23807 			sa->ipsa_flags |= IPSA_F_HW;
23808 			mutex_exit(&sa->ipsa_lock);
23809 		}
23810 
23811 		/*
23812 		 * Copy template message, and add it to the front
23813 		 * of the mblk ship list. We want to avoid holding
23814 		 * the ipsec_capab_ills_lock while sending the
23815 		 * message to the ills.
23816 		 *
23817 		 * The b_next and b_prev are temporarily used
23818 		 * to build a list of mblks to be sent down, and to
23819 		 * save the ill to which they must be sent.
23820 		 */
23821 		nmp = copymsg(mp);
23822 		if (nmp == NULL) {
23823 			ill_refrele(ill);
23824 			continue;
23825 		}
23826 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23827 		nmp->b_next = mp_ship_list;
23828 		mp_ship_list = nmp;
23829 		nmp->b_prev = (mblk_t *)ill;
23830 	}
23831 
23832 	rw_exit(&ipsec_capab_ills_lock);
23833 
23834 	nmp = mp_ship_list;
23835 	while (nmp != NULL) {
23836 		/* restore the mblk to a sane state */
23837 		next_mp = nmp->b_next;
23838 		nmp->b_next = NULL;
23839 		ill = (ill_t *)nmp->b_prev;
23840 		nmp->b_prev = NULL;
23841 
23842 		/*
23843 		 * Ship the mblk to the ill, must be exclusive. Keep the
23844 		 * reference to the ill as qwriter_ip() does a ill_referele().
23845 		 */
23846 		(void) qwriter_ip(NULL, ill, ill->ill_wq, nmp,
23847 		    ill_ipsec_capab_send_writer, NEW_OP, B_TRUE);
23848 
23849 		nmp = next_mp;
23850 	}
23851 
23852 	if (sa != NULL)
23853 		IPSA_REFRELE(sa);
23854 	freemsg(mp);
23855 }
23856 
23857 
23858 /*
23859  * Derive an interface id from the link layer address.
23860  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23861  */
23862 static boolean_t
23863 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23864 {
23865 	char		*addr;
23866 
23867 	if (phys_length != ETHERADDRL)
23868 		return (B_FALSE);
23869 
23870 	/* Form EUI-64 like address */
23871 	addr = (char *)&v6addr->s6_addr32[2];
23872 	bcopy((char *)phys_addr, addr, 3);
23873 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23874 	addr[3] = (char)0xff;
23875 	addr[4] = (char)0xfe;
23876 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23877 	return (B_TRUE);
23878 }
23879 
23880 /* ARGSUSED */
23881 static boolean_t
23882 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23883 {
23884 	return (B_FALSE);
23885 }
23886 
23887 /* ARGSUSED */
23888 static boolean_t
23889 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23890     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23891 {
23892 	/*
23893 	 * Multicast address mappings used over Ethernet/802.X.
23894 	 * This address is used as a base for mappings.
23895 	 */
23896 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23897 	    0x00, 0x00, 0x00};
23898 
23899 	/*
23900 	 * Extract low order 32 bits from IPv6 multicast address.
23901 	 * Or that into the link layer address, starting from the
23902 	 * second byte.
23903 	 */
23904 	*hw_start = 2;
23905 	v6_extract_mask->s6_addr32[0] = 0;
23906 	v6_extract_mask->s6_addr32[1] = 0;
23907 	v6_extract_mask->s6_addr32[2] = 0;
23908 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23909 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23910 	return (B_TRUE);
23911 }
23912 
23913 /*
23914  * Indicate by return value whether multicast is supported. If not,
23915  * this code should not touch/change any parameters.
23916  */
23917 /* ARGSUSED */
23918 static boolean_t
23919 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23920     uint32_t *hw_start, ipaddr_t *extract_mask)
23921 {
23922 	/*
23923 	 * Multicast address mappings used over Ethernet/802.X.
23924 	 * This address is used as a base for mappings.
23925 	 */
23926 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23927 	    0x00, 0x00, 0x00 };
23928 
23929 	if (phys_length != ETHERADDRL)
23930 		return (B_FALSE);
23931 
23932 	*extract_mask = htonl(0x007fffff);
23933 	*hw_start = 2;
23934 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23935 	return (B_TRUE);
23936 }
23937 
23938 /*
23939  * Derive IPoIB interface id from the link layer address.
23940  */
23941 static boolean_t
23942 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23943 {
23944 	char		*addr;
23945 
23946 	if (phys_length != 20)
23947 		return (B_FALSE);
23948 	addr = (char *)&v6addr->s6_addr32[2];
23949 	bcopy(phys_addr + 12, addr, 8);
23950 	/*
23951 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23952 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23953 	 * rules. In these cases, the IBA considers these GUIDs to be in
23954 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23955 	 * required; vendors are required not to assign global EUI-64's
23956 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23957 	 * of the interface identifier. Whether the GUID is in modified
23958 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23959 	 * bit set to 1.
23960 	 */
23961 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23962 	return (B_TRUE);
23963 }
23964 
23965 /*
23966  * Note on mapping from multicast IP addresses to IPoIB multicast link
23967  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
23968  * The format of an IPoIB multicast address is:
23969  *
23970  *  4 byte QPN      Scope Sign.  Pkey
23971  * +--------------------------------------------+
23972  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
23973  * +--------------------------------------------+
23974  *
23975  * The Scope and Pkey components are properties of the IBA port and
23976  * network interface. They can be ascertained from the broadcast address.
23977  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
23978  */
23979 
23980 static boolean_t
23981 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23982     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23983 {
23984 	/*
23985 	 * Base IPoIB IPv6 multicast address used for mappings.
23986 	 * Does not contain the IBA scope/Pkey values.
23987 	 */
23988 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23989 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
23990 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23991 
23992 	/*
23993 	 * Extract low order 80 bits from IPv6 multicast address.
23994 	 * Or that into the link layer address, starting from the
23995 	 * sixth byte.
23996 	 */
23997 	*hw_start = 6;
23998 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
23999 
24000 	/*
24001 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24002 	 */
24003 	*(maddr + 5) = *(bphys_addr + 5);
24004 	*(maddr + 8) = *(bphys_addr + 8);
24005 	*(maddr + 9) = *(bphys_addr + 9);
24006 
24007 	v6_extract_mask->s6_addr32[0] = 0;
24008 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
24009 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
24010 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
24011 	return (B_TRUE);
24012 }
24013 
24014 static boolean_t
24015 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
24016     uint32_t *hw_start, ipaddr_t *extract_mask)
24017 {
24018 	/*
24019 	 * Base IPoIB IPv4 multicast address used for mappings.
24020 	 * Does not contain the IBA scope/Pkey values.
24021 	 */
24022 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
24023 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
24024 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24025 
24026 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
24027 		return (B_FALSE);
24028 
24029 	/*
24030 	 * Extract low order 28 bits from IPv4 multicast address.
24031 	 * Or that into the link layer address, starting from the
24032 	 * sixteenth byte.
24033 	 */
24034 	*extract_mask = htonl(0x0fffffff);
24035 	*hw_start = 16;
24036 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
24037 
24038 	/*
24039 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24040 	 */
24041 	*(maddr + 5) = *(bphys_addr + 5);
24042 	*(maddr + 8) = *(bphys_addr + 8);
24043 	*(maddr + 9) = *(bphys_addr + 9);
24044 	return (B_TRUE);
24045 }
24046 
24047 /*
24048  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
24049  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
24050  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
24051  * the link-local address is preferred.
24052  */
24053 boolean_t
24054 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24055 {
24056 	ipif_t	*ipif;
24057 	ipif_t	*maybe_ipif = NULL;
24058 
24059 	mutex_enter(&ill->ill_lock);
24060 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24061 		mutex_exit(&ill->ill_lock);
24062 		if (ipifp != NULL)
24063 			*ipifp = NULL;
24064 		return (B_FALSE);
24065 	}
24066 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24067 		if (!IPIF_CAN_LOOKUP(ipif))
24068 			continue;
24069 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
24070 		    ipif->ipif_zoneid != ALL_ZONES)
24071 			continue;
24072 		if ((ipif->ipif_flags & flags) != flags)
24073 			continue;
24074 
24075 		if (ipifp == NULL) {
24076 			mutex_exit(&ill->ill_lock);
24077 			ASSERT(maybe_ipif == NULL);
24078 			return (B_TRUE);
24079 		}
24080 		if (!ill->ill_isv6 ||
24081 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
24082 			ipif_refhold_locked(ipif);
24083 			mutex_exit(&ill->ill_lock);
24084 			*ipifp = ipif;
24085 			return (B_TRUE);
24086 		}
24087 		if (maybe_ipif == NULL)
24088 			maybe_ipif = ipif;
24089 	}
24090 	if (ipifp != NULL) {
24091 		if (maybe_ipif != NULL)
24092 			ipif_refhold_locked(maybe_ipif);
24093 		*ipifp = maybe_ipif;
24094 	}
24095 	mutex_exit(&ill->ill_lock);
24096 	return (maybe_ipif != NULL);
24097 }
24098 
24099 /*
24100  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
24101  */
24102 boolean_t
24103 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24104 {
24105 	ill_t *illg;
24106 
24107 	/*
24108 	 * We look at the passed-in ill first without grabbing ill_g_lock.
24109 	 */
24110 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
24111 		return (B_TRUE);
24112 	}
24113 	rw_enter(&ill_g_lock, RW_READER);
24114 	if (ill->ill_group == NULL) {
24115 		/* ill not in a group */
24116 		rw_exit(&ill_g_lock);
24117 		return (B_FALSE);
24118 	}
24119 
24120 	/*
24121 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24122 	 * group. We need to look for an ipif in the zone on all the ills in the
24123 	 * group.
24124 	 */
24125 	illg = ill->ill_group->illgrp_ill;
24126 	do {
24127 		/*
24128 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24129 		 * that it's not there.
24130 		 */
24131 		if (illg != ill &&
24132 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24133 			break;
24134 		}
24135 	} while ((illg = illg->ill_group_next) != NULL);
24136 	rw_exit(&ill_g_lock);
24137 	return (illg != NULL);
24138 }
24139 
24140 /*
24141  * Check if this ill is only being used to send ICMP probes for IPMP
24142  */
24143 boolean_t
24144 ill_is_probeonly(ill_t *ill)
24145 {
24146 	/*
24147 	 * Check if the interface is FAILED, or INACTIVE
24148 	 */
24149 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24150 		return (B_TRUE);
24151 
24152 	return (B_FALSE);
24153 }
24154 
24155 /*
24156  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24157  * If a pointer to an ipif_t is returned then the caller will need to do
24158  * an ill_refrele().
24159  */
24160 ipif_t *
24161 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6)
24162 {
24163 	ipif_t *ipif;
24164 	ill_t *ill;
24165 
24166 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL);
24167 
24168 	if (ill == NULL)
24169 		return (NULL);
24170 
24171 	mutex_enter(&ill->ill_lock);
24172 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24173 		mutex_exit(&ill->ill_lock);
24174 		ill_refrele(ill);
24175 		return (NULL);
24176 	}
24177 
24178 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24179 		if (!IPIF_CAN_LOOKUP(ipif))
24180 			continue;
24181 		if (lifidx == ipif->ipif_id) {
24182 			ipif_refhold_locked(ipif);
24183 			break;
24184 		}
24185 	}
24186 
24187 	mutex_exit(&ill->ill_lock);
24188 	ill_refrele(ill);
24189 	return (ipif);
24190 }
24191