xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_if.c (revision 4fceebdf03eeac0d7c58a4f70cc19b00a8c40a73)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This file contains the interface control functions for IP.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/stream.h>
35 #include <sys/dlpi.h>
36 #include <sys/stropts.h>
37 #include <sys/strsun.h>
38 #include <sys/sysmacros.h>
39 #include <sys/strlog.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #include <sys/cmn_err.h>
43 #include <sys/kstat.h>
44 #include <sys/debug.h>
45 #include <sys/zone.h>
46 
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_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
197 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
198 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
199 static void	ill_stq_cache_delete(ire_t *, char *);
200 
201 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
202 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
203 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
204     in6_addr_t *);
205 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
206     ipaddr_t *);
207 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
208 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
209     in6_addr_t *);
210 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
211     ipaddr_t *);
212 
213 static void	ipif_save_ire(ipif_t *, ire_t *);
214 static void	ipif_remove_ire(ipif_t *, ire_t *);
215 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *);
216 static void 	ip_cgtp_bcast_delete(ire_t *);
217 
218 /*
219  * Per-ill IPsec capabilities management.
220  */
221 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
222 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
223 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
224 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
225 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
226 static void ill_capability_proto(ill_t *, int, mblk_t *);
227 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
228     boolean_t);
229 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
230 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
231 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
232 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
233 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
234 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
235 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
236 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
237     dl_capability_sub_t *);
238 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
239 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
240 static void ill_capability_lso_reset(ill_t *, mblk_t **);
241 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
242 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
243 static void ill_capability_dls_reset(ill_t *, mblk_t **);
244 static void ill_capability_dls_disable(ill_t *);
245 
246 static void	illgrp_cache_delete(ire_t *, char *);
247 static void	illgrp_delete(ill_t *ill);
248 static void	illgrp_reset_schednext(ill_t *ill);
249 
250 static ill_t	*ill_prev_usesrc(ill_t *);
251 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
252 static void	ill_disband_usesrc_group(ill_t *);
253 
254 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
255 
256 /*
257  * if we go over the memory footprint limit more than once in this msec
258  * interval, we'll start pruning aggressively.
259  */
260 int ip_min_frag_prune_time = 0;
261 
262 /*
263  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
264  * and the IPsec DOI
265  */
266 #define	MAX_IPSEC_ALGS	256
267 
268 #define	BITSPERBYTE	8
269 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
270 
271 #define	IPSEC_ALG_ENABLE(algs, algid) \
272 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
273 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
274 
275 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
276 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
277 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
278 
279 typedef uint8_t ipsec_capab_elem_t;
280 
281 /*
282  * Per-algorithm parameters.  Note that at present, only encryption
283  * algorithms have variable keysize (IKE does not provide a way to negotiate
284  * auth algorithm keysize).
285  *
286  * All sizes here are in bits.
287  */
288 typedef struct
289 {
290 	uint16_t	minkeylen;
291 	uint16_t	maxkeylen;
292 } ipsec_capab_algparm_t;
293 
294 /*
295  * Per-ill capabilities.
296  */
297 struct ill_ipsec_capab_s {
298 	ipsec_capab_elem_t *encr_hw_algs;
299 	ipsec_capab_elem_t *auth_hw_algs;
300 	uint32_t algs_size;	/* size of _hw_algs in bytes */
301 	/* algorithm key lengths */
302 	ipsec_capab_algparm_t *encr_algparm;
303 	uint32_t encr_algparm_size;
304 	uint32_t encr_algparm_end;
305 };
306 
307 /*
308  * List of AH and ESP IPsec acceleration capable ills
309  */
310 typedef struct ipsec_capab_ill_s {
311 	uint_t ill_index;
312 	boolean_t ill_isv6;
313 	struct ipsec_capab_ill_s *next;
314 } ipsec_capab_ill_t;
315 
316 static ipsec_capab_ill_t *ipsec_capab_ills_ah;
317 static ipsec_capab_ill_t *ipsec_capab_ills_esp;
318 krwlock_t ipsec_capab_ills_lock;
319 
320 /*
321  * The field values are larger than strictly necessary for simple
322  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
323  */
324 static area_t	ip_area_template = {
325 	AR_ENTRY_ADD,			/* area_cmd */
326 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
327 					/* area_name_offset */
328 	/* area_name_length temporarily holds this structure length */
329 	sizeof (area_t),			/* area_name_length */
330 	IP_ARP_PROTO_TYPE,		/* area_proto */
331 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
332 	IP_ADDR_LEN,			/* area_proto_addr_length */
333 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
334 					/* area_proto_mask_offset */
335 	0,				/* area_flags */
336 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
337 					/* area_hw_addr_offset */
338 	/* Zero length hw_addr_length means 'use your idea of the address' */
339 	0				/* area_hw_addr_length */
340 };
341 
342 /*
343  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
344  * support
345  */
346 static area_t	ip6_area_template = {
347 	AR_ENTRY_ADD,			/* area_cmd */
348 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
349 					/* area_name_offset */
350 	/* area_name_length temporarily holds this structure length */
351 	sizeof (area_t),			/* area_name_length */
352 	IP_ARP_PROTO_TYPE,		/* area_proto */
353 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
354 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
355 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
356 					/* area_proto_mask_offset */
357 	0,				/* area_flags */
358 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
359 					/* area_hw_addr_offset */
360 	/* Zero length hw_addr_length means 'use your idea of the address' */
361 	0				/* area_hw_addr_length */
362 };
363 
364 static ared_t	ip_ared_template = {
365 	AR_ENTRY_DELETE,
366 	sizeof (ared_t) + IP_ADDR_LEN,
367 	sizeof (ared_t),
368 	IP_ARP_PROTO_TYPE,
369 	sizeof (ared_t),
370 	IP_ADDR_LEN
371 };
372 
373 static ared_t	ip6_ared_template = {
374 	AR_ENTRY_DELETE,
375 	sizeof (ared_t) + IPV6_ADDR_LEN,
376 	sizeof (ared_t),
377 	IP_ARP_PROTO_TYPE,
378 	sizeof (ared_t),
379 	IPV6_ADDR_LEN
380 };
381 
382 /*
383  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
384  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
385  * areq is used).
386  */
387 static areq_t	ip_areq_template = {
388 	AR_ENTRY_QUERY,			/* cmd */
389 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
390 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
391 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
392 	sizeof (areq_t),			/* target addr offset */
393 	IP_ADDR_LEN,			/* target addr_length */
394 	0,				/* flags */
395 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
396 	IP_ADDR_LEN,			/* sender addr length */
397 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
398 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
399 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
400 	/* anything else filled in by the code */
401 };
402 
403 static arc_t	ip_aru_template = {
404 	AR_INTERFACE_UP,
405 	sizeof (arc_t),		/* Name offset */
406 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
407 };
408 
409 static arc_t	ip_ard_template = {
410 	AR_INTERFACE_DOWN,
411 	sizeof (arc_t),		/* Name offset */
412 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
413 };
414 
415 static arc_t	ip_aron_template = {
416 	AR_INTERFACE_ON,
417 	sizeof (arc_t),		/* Name offset */
418 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
419 };
420 
421 static arc_t	ip_aroff_template = {
422 	AR_INTERFACE_OFF,
423 	sizeof (arc_t),		/* Name offset */
424 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
425 };
426 
427 
428 static arma_t	ip_arma_multi_template = {
429 	AR_MAPPING_ADD,
430 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
431 				/* Name offset */
432 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
433 	IP_ARP_PROTO_TYPE,
434 	sizeof (arma_t),			/* proto_addr_offset */
435 	IP_ADDR_LEN,				/* proto_addr_length */
436 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
437 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
438 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
439 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
440 	IP_MAX_HW_LEN,				/* hw_addr_length */
441 	0,					/* hw_mapping_start */
442 };
443 
444 static ipft_t	ip_ioctl_ftbl[] = {
445 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
446 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
447 		IPFT_F_NO_REPLY },
448 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
449 		IPFT_F_NO_REPLY },
450 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
451 	{ 0 }
452 };
453 
454 /* Simple ICMP IP Header Template */
455 static ipha_t icmp_ipha = {
456 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
457 };
458 
459 /* Flag descriptors for ip_ipif_report */
460 static nv_t	ipif_nv_tbl[] = {
461 	{ IPIF_UP,		"UP" },
462 	{ IPIF_BROADCAST,	"BROADCAST" },
463 	{ ILLF_DEBUG,		"DEBUG" },
464 	{ PHYI_LOOPBACK,	"LOOPBACK" },
465 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
466 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
467 	{ PHYI_RUNNING,		"RUNNING" },
468 	{ ILLF_NOARP,		"NOARP" },
469 	{ PHYI_PROMISC,		"PROMISC" },
470 	{ PHYI_ALLMULTI,	"ALLMULTI" },
471 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
472 	{ ILLF_MULTICAST,	"MULTICAST" },
473 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
474 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
475 	{ IPIF_DHCPRUNNING,	"DHCP" },
476 	{ IPIF_PRIVATE,		"PRIVATE" },
477 	{ IPIF_NOXMIT,		"NOXMIT" },
478 	{ IPIF_NOLOCAL,		"NOLOCAL" },
479 	{ IPIF_DEPRECATED,	"DEPRECATED" },
480 	{ IPIF_PREFERRED,	"PREFERRED" },
481 	{ IPIF_TEMPORARY,	"TEMPORARY" },
482 	{ IPIF_ADDRCONF,	"ADDRCONF" },
483 	{ PHYI_VIRTUAL,		"VIRTUAL" },
484 	{ ILLF_ROUTER,		"ROUTER" },
485 	{ ILLF_NONUD,		"NONUD" },
486 	{ IPIF_ANYCAST,		"ANYCAST" },
487 	{ ILLF_NORTEXCH,	"NORTEXCH" },
488 	{ ILLF_IPV4,		"IPV4" },
489 	{ ILLF_IPV6,		"IPV6" },
490 	{ IPIF_MIPRUNNING,	"MIP" },
491 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
492 	{ PHYI_FAILED,		"FAILED" },
493 	{ PHYI_STANDBY,		"STANDBY" },
494 	{ PHYI_INACTIVE,	"INACTIVE" },
495 	{ PHYI_OFFLINE,		"OFFLINE" },
496 };
497 
498 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
499 
500 static ip_m_t	ip_m_tbl[] = {
501 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
502 	    ip_ether_v6intfid },
503 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
504 	    ip_nodef_v6intfid },
505 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
506 	    ip_nodef_v6intfid },
507 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
508 	    ip_nodef_v6intfid },
509 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
510 	    ip_ether_v6intfid },
511 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
512 	    ip_ib_v6intfid },
513 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
514 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
515 	    ip_nodef_v6intfid }
516 };
517 
518 static ill_t	ill_null;		/* Empty ILL for init. */
519 char	ipif_loopback_name[] = "lo0";
520 static char *ipv4_forward_suffix = ":ip_forwarding";
521 static char *ipv6_forward_suffix = ":ip6_forwarding";
522 static kstat_t *loopback_ksp = NULL;
523 static	sin6_t	sin6_null;	/* Zero address for quick clears */
524 static	sin_t	sin_null;	/* Zero address for quick clears */
525 static	uint_t	ill_index = 1;	/* Used to assign interface indicies */
526 /* When set search for unused index */
527 static boolean_t ill_index_wrap = B_FALSE;
528 /* When set search for unused ipif_seqid */
529 static ipif_t	ipif_zero;
530 uint_t	ipif_src_random;
531 
532 /*
533  * For details on the protection offered by these locks please refer
534  * to the notes under the Synchronization section at the start of ip.c
535  */
536 krwlock_t	ill_g_lock;		/* The global ill_g_lock */
537 kmutex_t	ip_addr_avail_lock;	/* Address availability check lock */
538 ipsq_t		*ipsq_g_head;		/* List of all ipsq's on the system */
539 
540 krwlock_t	ill_g_usesrc_lock;	/* Protects usesrc related fields */
541 
542 /*
543  * illgrp_head/ifgrp_head is protected by IP's perimeter.
544  */
545 static  ill_group_t *illgrp_head_v4;	/* Head of IPv4 ill groups */
546 ill_group_t *illgrp_head_v6;		/* Head of IPv6 ill groups */
547 
548 ill_g_head_t	ill_g_heads[MAX_G_HEADS];   /* ILL List Head */
549 
550 /*
551  * ppa arena is created after these many
552  * interfaces have been plumbed.
553  */
554 uint_t	ill_no_arena = 12;
555 
556 #pragma align CACHE_ALIGN_SIZE(phyint_g_list)
557 static phyint_list_t phyint_g_list;	/* start of phyint list */
558 
559 /*
560  * Reflects value of FAILBACK variable in IPMP config file
561  * /etc/default/mpathd. Default value is B_TRUE.
562  * Set to B_FALSE if user disabled failback by configuring "FAILBACK=no"
563  * in.mpathd uses SIOCSIPMPFAILBACK ioctl to pass this information to kernel.
564  */
565 static boolean_t ipmp_enable_failback = B_TRUE;
566 
567 /*
568  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
569  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
570  * set through platform specific code (Niagara/Ontario).
571  */
572 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
573 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
574 
575 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
576 
577 static uint_t
578 ipif_rand(void)
579 {
580 	ipif_src_random = ipif_src_random * 1103515245 + 12345;
581 	return ((ipif_src_random >> 16) & 0x7fff);
582 }
583 
584 /*
585  * Allocate per-interface mibs.
586  * Returns true if ok. False otherwise.
587  *  ipsq  may not yet be allocated (loopback case ).
588  */
589 static boolean_t
590 ill_allocate_mibs(ill_t *ill)
591 {
592 	/* Already allocated? */
593 	if (ill->ill_ip_mib != NULL) {
594 		if (ill->ill_isv6)
595 			ASSERT(ill->ill_icmp6_mib != NULL);
596 		return (B_TRUE);
597 	}
598 
599 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
600 	    KM_NOSLEEP);
601 	if (ill->ill_ip_mib == NULL) {
602 		return (B_FALSE);
603 	}
604 
605 	/* Setup static information */
606 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
607 	    sizeof (mib2_ipIfStatsEntry_t));
608 	if (ill->ill_isv6) {
609 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
610 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
611 		    sizeof (mib2_ipv6AddrEntry_t));
612 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
613 		    sizeof (mib2_ipv6RouteEntry_t));
614 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
615 		    sizeof (mib2_ipv6NetToMediaEntry_t));
616 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
617 		    sizeof (ipv6_member_t));
618 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
619 		    sizeof (ipv6_grpsrc_t));
620 	} else {
621 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
622 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
623 		    sizeof (mib2_ipAddrEntry_t));
624 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
625 		    sizeof (mib2_ipRouteEntry_t));
626 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
627 		    sizeof (mib2_ipNetToMediaEntry_t));
628 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
629 		    sizeof (ip_member_t));
630 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
631 		    sizeof (ip_grpsrc_t));
632 
633 		/*
634 		 * For a v4 ill, we are done at this point, because per ill
635 		 * icmp mibs are only used for v6.
636 		 */
637 		return (B_TRUE);
638 	}
639 
640 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
641 	    KM_NOSLEEP);
642 	if (ill->ill_icmp6_mib == NULL) {
643 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
644 		ill->ill_ip_mib = NULL;
645 		return (B_FALSE);
646 	}
647 	/* static icmp info */
648 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
649 	    sizeof (mib2_ipv6IfIcmpEntry_t);
650 	/*
651 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
652 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
653 	 * -> ill_phyint_reinit
654 	 */
655 	return (B_TRUE);
656 }
657 
658 /*
659  * Common code for preparation of ARP commands.  Two points to remember:
660  * 	1) The ill_name is tacked on at the end of the allocated space so
661  *	   the templates name_offset field must contain the total space
662  *	   to allocate less the name length.
663  *
664  *	2) The templates name_length field should contain the *template*
665  *	   length.  We use it as a parameter to bcopy() and then write
666  *	   the real ill_name_length into the name_length field of the copy.
667  * (Always called as writer.)
668  */
669 mblk_t *
670 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
671 {
672 	arc_t	*arc = (arc_t *)template;
673 	char	*cp;
674 	int	len;
675 	mblk_t	*mp;
676 	uint_t	name_length = ill->ill_name_length;
677 	uint_t	template_len = arc->arc_name_length;
678 
679 	len = arc->arc_name_offset + name_length;
680 	mp = allocb(len, BPRI_HI);
681 	if (mp == NULL)
682 		return (NULL);
683 	cp = (char *)mp->b_rptr;
684 	mp->b_wptr = (uchar_t *)&cp[len];
685 	if (template_len)
686 		bcopy(template, cp, template_len);
687 	if (len > template_len)
688 		bzero(&cp[template_len], len - template_len);
689 	mp->b_datap->db_type = M_PROTO;
690 
691 	arc = (arc_t *)cp;
692 	arc->arc_name_length = name_length;
693 	cp = (char *)arc + arc->arc_name_offset;
694 	bcopy(ill->ill_name, cp, name_length);
695 
696 	if (addr) {
697 		area_t	*area = (area_t *)mp->b_rptr;
698 
699 		cp = (char *)area + area->area_proto_addr_offset;
700 		bcopy(addr, cp, area->area_proto_addr_length);
701 		if (area->area_cmd == AR_ENTRY_ADD) {
702 			cp = (char *)area;
703 			len = area->area_proto_addr_length;
704 			if (area->area_proto_mask_offset)
705 				cp += area->area_proto_mask_offset;
706 			else
707 				cp += area->area_proto_addr_offset + len;
708 			while (len-- > 0)
709 				*cp++ = (char)~0;
710 		}
711 	}
712 	return (mp);
713 }
714 
715 mblk_t *
716 ipif_area_alloc(ipif_t *ipif)
717 {
718 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
719 	    (char *)&ipif->ipif_lcl_addr));
720 }
721 
722 mblk_t *
723 ipif_ared_alloc(ipif_t *ipif)
724 {
725 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
726 	    (char *)&ipif->ipif_lcl_addr));
727 }
728 
729 mblk_t *
730 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
731 {
732 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
733 	    (char *)&addr));
734 }
735 
736 /*
737  * Completely vaporize a lower level tap and all associated interfaces.
738  * ill_delete is called only out of ip_close when the device control
739  * stream is being closed.
740  */
741 void
742 ill_delete(ill_t *ill)
743 {
744 	ipif_t	*ipif;
745 	ill_t	*prev_ill;
746 
747 	/*
748 	 * ill_delete may be forcibly entering the ipsq. The previous
749 	 * ioctl may not have completed and may need to be aborted.
750 	 * ipsq_flush takes care of it. If we don't need to enter the
751 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
752 	 * ill_delete_tail is sufficient.
753 	 */
754 	ipsq_flush(ill);
755 
756 	/*
757 	 * Nuke all interfaces.  ipif_free will take down the interface,
758 	 * remove it from the list, and free the data structure.
759 	 * Walk down the ipif list and remove the logical interfaces
760 	 * first before removing the main ipif. We can't unplumb
761 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
762 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
763 	 * POINTOPOINT.
764 	 *
765 	 * If ill_ipif was not properly initialized (i.e low on memory),
766 	 * then no interfaces to clean up. In this case just clean up the
767 	 * ill.
768 	 */
769 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
770 		ipif_free(ipif);
771 
772 	/*
773 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
774 	 * So nobody can be using this mp now. Free the mp allocated for
775 	 * honoring ILLF_NOARP
776 	 */
777 	freemsg(ill->ill_arp_on_mp);
778 	ill->ill_arp_on_mp = NULL;
779 
780 	/* Clean up msgs on pending upcalls for mrouted */
781 	reset_mrt_ill(ill);
782 
783 	/*
784 	 * ipif_free -> reset_conn_ipif will remove all multicast
785 	 * references for IPv4. For IPv6, we need to do it here as
786 	 * it points only at ills.
787 	 */
788 	reset_conn_ill(ill);
789 
790 	/*
791 	 * ill_down will arrange to blow off any IRE's dependent on this
792 	 * ILL, and shut down fragmentation reassembly.
793 	 */
794 	ill_down(ill);
795 
796 	/* Let SCTP know, so that it can remove this from its list. */
797 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
798 
799 	/*
800 	 * If an address on this ILL is being used as a source address then
801 	 * clear out the pointers in other ILLs that point to this ILL.
802 	 */
803 	rw_enter(&ill_g_usesrc_lock, RW_WRITER);
804 	if (ill->ill_usesrc_grp_next != NULL) {
805 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
806 			ill_disband_usesrc_group(ill);
807 		} else {	/* consumer of the usesrc ILL */
808 			prev_ill = ill_prev_usesrc(ill);
809 			prev_ill->ill_usesrc_grp_next =
810 			    ill->ill_usesrc_grp_next;
811 		}
812 	}
813 	rw_exit(&ill_g_usesrc_lock);
814 }
815 
816 static void
817 ipif_non_duplicate(ipif_t *ipif)
818 {
819 	ill_t *ill = ipif->ipif_ill;
820 	mutex_enter(&ill->ill_lock);
821 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
822 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
823 		ASSERT(ill->ill_ipif_dup_count > 0);
824 		ill->ill_ipif_dup_count--;
825 	}
826 	mutex_exit(&ill->ill_lock);
827 }
828 
829 /*
830  * Send all deferred messages without waiting for their ACKs.
831  */
832 void
833 ill_send_all_deferred_mp(ill_t *ill)
834 {
835 	mblk_t *mp, *next;
836 
837 	/*
838 	 * Clear ill_dlpi_pending so that the message is not queued in
839 	 * ill_dlpi_send().
840 	 */
841 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
842 
843 	for (mp = ill->ill_dlpi_deferred; mp != NULL; mp = next) {
844 		next = mp->b_next;
845 		mp->b_next = NULL;
846 		ill_dlpi_send(ill, mp);
847 	}
848 	ill->ill_dlpi_deferred = NULL;
849 }
850 
851 /*
852  * ill_delete_tail is called from ip_modclose after all references
853  * to the closing ill are gone. The wait is done in ip_modclose
854  */
855 void
856 ill_delete_tail(ill_t *ill)
857 {
858 	mblk_t	**mpp;
859 	ipif_t	*ipif;
860 
861 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
862 		ipif_non_duplicate(ipif);
863 		ipif_down_tail(ipif);
864 	}
865 
866 	ASSERT(ill->ill_ipif_dup_count == 0 &&
867 	    ill->ill_arp_down_mp == NULL &&
868 	    ill->ill_arp_del_mapping_mp == NULL);
869 
870 	/*
871 	 * If polling capability is enabled (which signifies direct
872 	 * upcall into IP and driver has ill saved as a handle),
873 	 * we need to make sure that unbind has completed before we
874 	 * let the ill disappear and driver no longer has any reference
875 	 * to this ill.
876 	 */
877 	mutex_enter(&ill->ill_lock);
878 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
879 		cv_wait(&ill->ill_cv, &ill->ill_lock);
880 	mutex_exit(&ill->ill_lock);
881 
882 	/*
883 	 * Clean up polling and soft ring capabilities
884 	 */
885 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
886 		ill_capability_dls_disable(ill);
887 
888 	/*
889 	 * Send the detach if there's one to send (i.e., if we're above a
890 	 * style 2 DLPI driver).
891 	 */
892 	if (ill->ill_detach_mp != NULL) {
893 		ill_dlpi_send(ill, ill->ill_detach_mp);
894 		ill->ill_detach_mp = NULL;
895 	}
896 
897 	if (ill->ill_net_type != IRE_LOOPBACK)
898 		qprocsoff(ill->ill_rq);
899 
900 	/*
901 	 * We do an ipsq_flush once again now. New messages could have
902 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
903 	 * could also have landed up if an ioctl thread had looked up
904 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
905 	 * enqueued the ioctl when we did the ipsq_flush last time.
906 	 */
907 	ipsq_flush(ill);
908 
909 	/*
910 	 * Free capabilities.
911 	 */
912 	if (ill->ill_ipsec_capab_ah != NULL) {
913 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
914 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
915 		ill->ill_ipsec_capab_ah = NULL;
916 	}
917 
918 	if (ill->ill_ipsec_capab_esp != NULL) {
919 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
920 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
921 		ill->ill_ipsec_capab_esp = NULL;
922 	}
923 
924 	if (ill->ill_mdt_capab != NULL) {
925 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
926 		ill->ill_mdt_capab = NULL;
927 	}
928 
929 	if (ill->ill_hcksum_capab != NULL) {
930 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
931 		ill->ill_hcksum_capab = NULL;
932 	}
933 
934 	if (ill->ill_zerocopy_capab != NULL) {
935 		kmem_free(ill->ill_zerocopy_capab,
936 		    sizeof (ill_zerocopy_capab_t));
937 		ill->ill_zerocopy_capab = NULL;
938 	}
939 
940 	if (ill->ill_lso_capab != NULL) {
941 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
942 		ill->ill_lso_capab = NULL;
943 	}
944 
945 	if (ill->ill_dls_capab != NULL) {
946 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
947 		ill->ill_dls_capab->ill_unbind_conn = NULL;
948 		kmem_free(ill->ill_dls_capab,
949 		    sizeof (ill_dls_capab_t) +
950 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
951 		ill->ill_dls_capab = NULL;
952 	}
953 
954 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
955 
956 	while (ill->ill_ipif != NULL)
957 		ipif_free_tail(ill->ill_ipif);
958 
959 	ill_down_tail(ill);
960 
961 	/*
962 	 * We have removed all references to ilm from conn and the ones joined
963 	 * within the kernel.
964 	 *
965 	 * We don't walk conns, mrts and ires because
966 	 *
967 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
968 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
969 	 *    ill references.
970 	 */
971 	ASSERT(ilm_walk_ill(ill) == 0);
972 	/*
973 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
974 	 * could free the phyint. No more reference to the phyint after this
975 	 * point.
976 	 */
977 	(void) ill_glist_delete(ill);
978 
979 	rw_enter(&ip_g_nd_lock, RW_WRITER);
980 	if (ill->ill_ndd_name != NULL)
981 		nd_unload(&ip_g_nd, ill->ill_ndd_name);
982 	rw_exit(&ip_g_nd_lock);
983 
984 
985 	if (ill->ill_frag_ptr != NULL) {
986 		uint_t count;
987 
988 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
989 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
990 		}
991 		mi_free(ill->ill_frag_ptr);
992 		ill->ill_frag_ptr = NULL;
993 		ill->ill_frag_hash_tbl = NULL;
994 	}
995 
996 	freemsg(ill->ill_nd_lla_mp);
997 	/* Free all retained control messages. */
998 	mpp = &ill->ill_first_mp_to_free;
999 	do {
1000 		while (mpp[0]) {
1001 			mblk_t  *mp;
1002 			mblk_t  *mp1;
1003 
1004 			mp = mpp[0];
1005 			mpp[0] = mp->b_next;
1006 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
1007 				mp1->b_next = NULL;
1008 				mp1->b_prev = NULL;
1009 			}
1010 			freemsg(mp);
1011 		}
1012 	} while (mpp++ != &ill->ill_last_mp_to_free);
1013 
1014 	ill_free_mib(ill);
1015 	ILL_TRACE_CLEANUP(ill);
1016 }
1017 
1018 static void
1019 ill_free_mib(ill_t *ill)
1020 {
1021 	/*
1022 	 * MIB statistics must not be lost, so when an interface
1023 	 * goes away the counter values will be added to the global
1024 	 * MIBs.
1025 	 */
1026 	if (ill->ill_ip_mib != NULL) {
1027 		if (ill->ill_isv6)
1028 			ip_mib2_add_ip_stats(&ip6_mib, ill->ill_ip_mib);
1029 		else
1030 			ip_mib2_add_ip_stats(&ip_mib, ill->ill_ip_mib);
1031 
1032 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
1033 		ill->ill_ip_mib = NULL;
1034 	}
1035 	if (ill->ill_icmp6_mib != NULL) {
1036 		ip_mib2_add_icmp6_stats(&icmp6_mib, ill->ill_icmp6_mib);
1037 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
1038 		ill->ill_icmp6_mib = NULL;
1039 	}
1040 }
1041 
1042 /*
1043  * Concatenate together a physical address and a sap.
1044  *
1045  * Sap_lengths are interpreted as follows:
1046  *   sap_length == 0	==>	no sap
1047  *   sap_length > 0	==>	sap is at the head of the dlpi address
1048  *   sap_length < 0	==>	sap is at the tail of the dlpi address
1049  */
1050 static void
1051 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
1052     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
1053 {
1054 	uint16_t sap_addr = (uint16_t)sap_src;
1055 
1056 	if (sap_length == 0) {
1057 		if (phys_src == NULL)
1058 			bzero(dst, phys_length);
1059 		else
1060 			bcopy(phys_src, dst, phys_length);
1061 	} else if (sap_length < 0) {
1062 		if (phys_src == NULL)
1063 			bzero(dst, phys_length);
1064 		else
1065 			bcopy(phys_src, dst, phys_length);
1066 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1067 	} else {
1068 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1069 		if (phys_src == NULL)
1070 			bzero((char *)dst + sap_length, phys_length);
1071 		else
1072 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1073 	}
1074 }
1075 
1076 /*
1077  * Generate a dl_unitdata_req mblk for the device and address given.
1078  * addr_length is the length of the physical portion of the address.
1079  * If addr is NULL include an all zero address of the specified length.
1080  * TRUE? In any case, addr_length is taken to be the entire length of the
1081  * dlpi address, including the absolute value of sap_length.
1082  */
1083 mblk_t *
1084 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1085 		t_scalar_t sap_length)
1086 {
1087 	dl_unitdata_req_t *dlur;
1088 	mblk_t	*mp;
1089 	t_scalar_t	abs_sap_length;		/* absolute value */
1090 
1091 	abs_sap_length = ABS(sap_length);
1092 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1093 		DL_UNITDATA_REQ);
1094 	if (mp == NULL)
1095 		return (NULL);
1096 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1097 	/* HACK: accomodate incompatible DLPI drivers */
1098 	if (addr_length == 8)
1099 		addr_length = 6;
1100 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1101 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1102 	dlur->dl_priority.dl_min = 0;
1103 	dlur->dl_priority.dl_max = 0;
1104 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1105 	    (uchar_t *)&dlur[1]);
1106 	return (mp);
1107 }
1108 
1109 /*
1110  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1111  * Return an error if we already have 1 or more ioctls in progress.
1112  * This is used only for non-exclusive ioctls. Currently this is used
1113  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1114  * and thus need to use ipsq_pending_mp_add.
1115  */
1116 boolean_t
1117 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1118 {
1119 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1120 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1121 	/*
1122 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1123 	 */
1124 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1125 	    (add_mp->b_datap->db_type == M_IOCTL));
1126 
1127 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1128 	/*
1129 	 * Return error if the conn has started closing. The conn
1130 	 * could have finished cleaning up the pending mp list,
1131 	 * If so we should not add another mp to the list negating
1132 	 * the cleanup.
1133 	 */
1134 	if (connp->conn_state_flags & CONN_CLOSING)
1135 		return (B_FALSE);
1136 	/*
1137 	 * Add the pending mp to the head of the list, chained by b_next.
1138 	 * Note down the conn on which the ioctl request came, in b_prev.
1139 	 * This will be used to later get the conn, when we get a response
1140 	 * on the ill queue, from some other module (typically arp)
1141 	 */
1142 	add_mp->b_next = (void *)ill->ill_pending_mp;
1143 	add_mp->b_queue = CONNP_TO_WQ(connp);
1144 	ill->ill_pending_mp = add_mp;
1145 	if (connp != NULL)
1146 		connp->conn_oper_pending_ill = ill;
1147 	return (B_TRUE);
1148 }
1149 
1150 /*
1151  * Retrieve the ill_pending_mp and return it. We have to walk the list
1152  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1153  */
1154 mblk_t *
1155 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1156 {
1157 	mblk_t	*prev = NULL;
1158 	mblk_t	*curr = NULL;
1159 	uint_t	id;
1160 	conn_t	*connp;
1161 
1162 	/*
1163 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1164 	 * up the pending mp, but it does not know the ioc_id and
1165 	 * passes in a zero for it.
1166 	 */
1167 	mutex_enter(&ill->ill_lock);
1168 	if (ioc_id != 0)
1169 		*connpp = NULL;
1170 
1171 	/* Search the list for the appropriate ioctl based on ioc_id */
1172 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1173 	    prev = curr, curr = curr->b_next) {
1174 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1175 		connp = Q_TO_CONN(curr->b_queue);
1176 		/* Match based on the ioc_id or based on the conn */
1177 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1178 			break;
1179 	}
1180 
1181 	if (curr != NULL) {
1182 		/* Unlink the mblk from the pending mp list */
1183 		if (prev != NULL) {
1184 			prev->b_next = curr->b_next;
1185 		} else {
1186 			ASSERT(ill->ill_pending_mp == curr);
1187 			ill->ill_pending_mp = curr->b_next;
1188 		}
1189 
1190 		/*
1191 		 * conn refcnt must have been bumped up at the start of
1192 		 * the ioctl. So we can safely access the conn.
1193 		 */
1194 		ASSERT(CONN_Q(curr->b_queue));
1195 		*connpp = Q_TO_CONN(curr->b_queue);
1196 		curr->b_next = NULL;
1197 		curr->b_queue = NULL;
1198 	}
1199 
1200 	mutex_exit(&ill->ill_lock);
1201 
1202 	return (curr);
1203 }
1204 
1205 /*
1206  * Add the pending mp to the list. There can be only 1 pending mp
1207  * in the list. Any exclusive ioctl that needs to wait for a response
1208  * from another module or driver needs to use this function to set
1209  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1210  * the other module/driver. This is also used while waiting for the
1211  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1212  */
1213 boolean_t
1214 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1215     int waitfor)
1216 {
1217 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1218 
1219 	ASSERT(IAM_WRITER_IPIF(ipif));
1220 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1221 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1222 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1223 	/*
1224 	 * The caller may be using a different ipif than the one passed into
1225 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1226 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1227 	 * that `ipsq_current_ipif == ipif'.
1228 	 */
1229 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1230 
1231 	/*
1232 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1233 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1234 	 */
1235 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1236 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1237 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1238 
1239 	if (connp != NULL) {
1240 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1241 		/*
1242 		 * Return error if the conn has started closing. The conn
1243 		 * could have finished cleaning up the pending mp list,
1244 		 * If so we should not add another mp to the list negating
1245 		 * the cleanup.
1246 		 */
1247 		if (connp->conn_state_flags & CONN_CLOSING)
1248 			return (B_FALSE);
1249 	}
1250 	mutex_enter(&ipsq->ipsq_lock);
1251 	ipsq->ipsq_pending_ipif = ipif;
1252 	/*
1253 	 * Note down the queue in b_queue. This will be returned by
1254 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1255 	 * the processing
1256 	 */
1257 	add_mp->b_next = NULL;
1258 	add_mp->b_queue = q;
1259 	ipsq->ipsq_pending_mp = add_mp;
1260 	ipsq->ipsq_waitfor = waitfor;
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 	ipsq->ipsq_current_ioctl = 0;
1356 	mutex_exit(&ipsq->ipsq_lock);
1357 
1358 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1359 		if (connp == NULL) {
1360 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1361 		} else {
1362 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1363 			mutex_enter(&ipif->ipif_ill->ill_lock);
1364 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1365 			mutex_exit(&ipif->ipif_ill->ill_lock);
1366 		}
1367 	} else {
1368 		/*
1369 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1370 		 * be just inet_freemsg. we have to restart it
1371 		 * otherwise the thread will be stuck.
1372 		 */
1373 		inet_freemsg(mp);
1374 	}
1375 	return (B_TRUE);
1376 }
1377 
1378 /*
1379  * The ill is closing. Cleanup all the pending mps. Called exclusively
1380  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1381  * knows this ill, and hence nobody can add an mp to this list
1382  */
1383 static void
1384 ill_pending_mp_cleanup(ill_t *ill)
1385 {
1386 	mblk_t	*mp;
1387 	queue_t	*q;
1388 
1389 	ASSERT(IAM_WRITER_ILL(ill));
1390 
1391 	mutex_enter(&ill->ill_lock);
1392 	/*
1393 	 * Every mp on the pending mp list originating from an ioctl
1394 	 * added 1 to the conn refcnt, at the start of the ioctl.
1395 	 * So bump it down now.  See comments in ip_wput_nondata()
1396 	 */
1397 	while (ill->ill_pending_mp != NULL) {
1398 		mp = ill->ill_pending_mp;
1399 		ill->ill_pending_mp = mp->b_next;
1400 		mutex_exit(&ill->ill_lock);
1401 
1402 		q = mp->b_queue;
1403 		ASSERT(CONN_Q(q));
1404 		mp->b_next = NULL;
1405 		mp->b_prev = NULL;
1406 		mp->b_queue = NULL;
1407 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1408 		mutex_enter(&ill->ill_lock);
1409 	}
1410 	ill->ill_pending_ipif = NULL;
1411 
1412 	mutex_exit(&ill->ill_lock);
1413 }
1414 
1415 /*
1416  * Called in the conn close path and ill delete path
1417  */
1418 static void
1419 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1420 {
1421 	ipsq_t	*ipsq;
1422 	mblk_t	*prev;
1423 	mblk_t	*curr;
1424 	mblk_t	*next;
1425 	queue_t	*q;
1426 	mblk_t	*tmp_list = NULL;
1427 
1428 	ASSERT(IAM_WRITER_ILL(ill));
1429 	if (connp != NULL)
1430 		q = CONNP_TO_WQ(connp);
1431 	else
1432 		q = ill->ill_wq;
1433 
1434 	ipsq = ill->ill_phyint->phyint_ipsq;
1435 	/*
1436 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1437 	 * In the case of ioctl from a conn, there can be only 1 mp
1438 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1439 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1440 	 * ioctls meant for this ill form conn's are not flushed. They will
1441 	 * be processed during ipsq_exit and will not find the ill and will
1442 	 * return error.
1443 	 */
1444 	mutex_enter(&ipsq->ipsq_lock);
1445 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1446 	    curr = next) {
1447 		next = curr->b_next;
1448 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1449 			/* Unlink the mblk from the pending mp list */
1450 			if (prev != NULL) {
1451 				prev->b_next = curr->b_next;
1452 			} else {
1453 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1454 				ipsq->ipsq_xopq_mphead = curr->b_next;
1455 			}
1456 			if (ipsq->ipsq_xopq_mptail == curr)
1457 				ipsq->ipsq_xopq_mptail = prev;
1458 			/*
1459 			 * Create a temporary list and release the ipsq lock
1460 			 * New elements are added to the head of the tmp_list
1461 			 */
1462 			curr->b_next = tmp_list;
1463 			tmp_list = curr;
1464 		} else {
1465 			prev = curr;
1466 		}
1467 	}
1468 	mutex_exit(&ipsq->ipsq_lock);
1469 
1470 	while (tmp_list != NULL) {
1471 		curr = tmp_list;
1472 		tmp_list = curr->b_next;
1473 		curr->b_next = NULL;
1474 		curr->b_prev = NULL;
1475 		curr->b_queue = NULL;
1476 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1477 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1478 			    CONN_CLOSE : NO_COPYOUT, NULL);
1479 		} else {
1480 			/*
1481 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1482 			 * this can't be just inet_freemsg. we have to
1483 			 * restart it otherwise the thread will be stuck.
1484 			 */
1485 			inet_freemsg(curr);
1486 		}
1487 	}
1488 }
1489 
1490 /*
1491  * This conn has started closing. Cleanup any pending ioctl from this conn.
1492  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1493  */
1494 void
1495 conn_ioctl_cleanup(conn_t *connp)
1496 {
1497 	mblk_t *curr;
1498 	ipsq_t	*ipsq;
1499 	ill_t	*ill;
1500 	boolean_t refheld;
1501 
1502 	/*
1503 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1504 	 * ioctl has not yet started, the mp is pending in the list headed by
1505 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1506 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1507 	 * is currently executing now the mp is not queued anywhere but
1508 	 * conn_oper_pending_ill is null. The conn close will wait
1509 	 * till the conn_ref drops to zero.
1510 	 */
1511 	mutex_enter(&connp->conn_lock);
1512 	ill = connp->conn_oper_pending_ill;
1513 	if (ill == NULL) {
1514 		mutex_exit(&connp->conn_lock);
1515 		return;
1516 	}
1517 
1518 	curr = ill_pending_mp_get(ill, &connp, 0);
1519 	if (curr != NULL) {
1520 		mutex_exit(&connp->conn_lock);
1521 		CONN_DEC_REF(connp);
1522 		inet_freemsg(curr);
1523 		return;
1524 	}
1525 	/*
1526 	 * We may not be able to refhold the ill if the ill/ipif
1527 	 * is changing. But we need to make sure that the ill will
1528 	 * not vanish. So we just bump up the ill_waiter count.
1529 	 */
1530 	refheld = ill_waiter_inc(ill);
1531 	mutex_exit(&connp->conn_lock);
1532 	if (refheld) {
1533 		if (ipsq_enter(ill, B_TRUE)) {
1534 			ill_waiter_dcr(ill);
1535 			/*
1536 			 * Check whether this ioctl has started and is
1537 			 * pending now in ipsq_pending_mp. If it is not
1538 			 * found there then check whether this ioctl has
1539 			 * not even started and is in the ipsq_xopq list.
1540 			 */
1541 			if (!ipsq_pending_mp_cleanup(ill, connp))
1542 				ipsq_xopq_mp_cleanup(ill, connp);
1543 			ipsq = ill->ill_phyint->phyint_ipsq;
1544 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1545 			return;
1546 		}
1547 	}
1548 
1549 	/*
1550 	 * The ill is also closing and we could not bump up the
1551 	 * ill_waiter_count or we could not enter the ipsq. Leave
1552 	 * the cleanup to ill_delete
1553 	 */
1554 	mutex_enter(&connp->conn_lock);
1555 	while (connp->conn_oper_pending_ill != NULL)
1556 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1557 	mutex_exit(&connp->conn_lock);
1558 	if (refheld)
1559 		ill_waiter_dcr(ill);
1560 }
1561 
1562 /*
1563  * ipcl_walk function for cleaning up conn_*_ill fields.
1564  */
1565 static void
1566 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1567 {
1568 	ill_t	*ill = (ill_t *)arg;
1569 	ire_t	*ire;
1570 
1571 	mutex_enter(&connp->conn_lock);
1572 	if (connp->conn_multicast_ill == ill) {
1573 		/* Revert to late binding */
1574 		connp->conn_multicast_ill = NULL;
1575 		connp->conn_orig_multicast_ifindex = 0;
1576 	}
1577 	if (connp->conn_incoming_ill == ill)
1578 		connp->conn_incoming_ill = NULL;
1579 	if (connp->conn_outgoing_ill == ill)
1580 		connp->conn_outgoing_ill = NULL;
1581 	if (connp->conn_outgoing_pill == ill)
1582 		connp->conn_outgoing_pill = NULL;
1583 	if (connp->conn_nofailover_ill == ill)
1584 		connp->conn_nofailover_ill = NULL;
1585 	if (connp->conn_xmit_if_ill == ill)
1586 		connp->conn_xmit_if_ill = NULL;
1587 	if (connp->conn_ire_cache != NULL) {
1588 		ire = connp->conn_ire_cache;
1589 		/*
1590 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1591 		 * interface X and ipif coming from interface Y, if interface
1592 		 * X and Y are part of the same IPMPgroup. Thus whenever
1593 		 * interface X goes down, remove all references to it by
1594 		 * checking both on ire_ipif and ire_stq.
1595 		 */
1596 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1597 		    (ire->ire_type == IRE_CACHE &&
1598 		    ire->ire_stq == ill->ill_wq)) {
1599 			connp->conn_ire_cache = NULL;
1600 			mutex_exit(&connp->conn_lock);
1601 			ire_refrele_notr(ire);
1602 			return;
1603 		}
1604 	}
1605 	mutex_exit(&connp->conn_lock);
1606 
1607 }
1608 
1609 /* ARGSUSED */
1610 void
1611 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1612 {
1613 	ill_t	*ill = q->q_ptr;
1614 	ipif_t	*ipif;
1615 
1616 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1617 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1618 		ipif_non_duplicate(ipif);
1619 		ipif_down_tail(ipif);
1620 	}
1621 	ill_down_tail(ill);
1622 	freemsg(mp);
1623 	ipsq_current_finish(ipsq);
1624 }
1625 
1626 /*
1627  * ill_down_start is called when we want to down this ill and bring it up again
1628  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1629  * all interfaces, but don't tear down any plumbing.
1630  */
1631 boolean_t
1632 ill_down_start(queue_t *q, mblk_t *mp)
1633 {
1634 	ill_t	*ill = q->q_ptr;
1635 	ipif_t	*ipif;
1636 
1637 	ASSERT(IAM_WRITER_ILL(ill));
1638 
1639 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1640 		(void) ipif_down(ipif, NULL, NULL);
1641 
1642 	ill_down(ill);
1643 
1644 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1645 
1646 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1647 
1648 	/*
1649 	 * Atomically test and add the pending mp if references are active.
1650 	 */
1651 	mutex_enter(&ill->ill_lock);
1652 	if (!ill_is_quiescent(ill)) {
1653 		/* call cannot fail since `conn_t *' argument is NULL */
1654 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1655 		    mp, ILL_DOWN);
1656 		mutex_exit(&ill->ill_lock);
1657 		return (B_FALSE);
1658 	}
1659 	mutex_exit(&ill->ill_lock);
1660 	return (B_TRUE);
1661 }
1662 
1663 static void
1664 ill_down(ill_t *ill)
1665 {
1666 	/* Blow off any IREs dependent on this ILL. */
1667 	ire_walk(ill_downi, (char *)ill);
1668 
1669 	mutex_enter(&ire_mrtun_lock);
1670 	if (ire_mrtun_count != 0) {
1671 		mutex_exit(&ire_mrtun_lock);
1672 		ire_walk_ill_mrtun(0, 0, ill_downi_mrtun_srcif,
1673 		    (char *)ill, NULL);
1674 	} else {
1675 		mutex_exit(&ire_mrtun_lock);
1676 	}
1677 
1678 	/*
1679 	 * If any interface based forwarding table exists
1680 	 * Blow off the ires there dependent on this ill
1681 	 */
1682 	mutex_enter(&ire_srcif_table_lock);
1683 	if (ire_srcif_table_count > 0) {
1684 		mutex_exit(&ire_srcif_table_lock);
1685 		ire_walk_srcif_table_v4(ill_downi_mrtun_srcif, (char *)ill);
1686 	} else {
1687 		mutex_exit(&ire_srcif_table_lock);
1688 	}
1689 
1690 	/* Remove any conn_*_ill depending on this ill */
1691 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill);
1692 
1693 	if (ill->ill_group != NULL) {
1694 		illgrp_delete(ill);
1695 	}
1696 
1697 }
1698 
1699 static void
1700 ill_down_tail(ill_t *ill)
1701 {
1702 	int	i;
1703 
1704 	/* Destroy ill_srcif_table if it exists */
1705 	/* Lock not reqd really because nobody should be able to access */
1706 	mutex_enter(&ill->ill_lock);
1707 	if (ill->ill_srcif_table != NULL) {
1708 		ill->ill_srcif_refcnt = 0;
1709 		for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
1710 			rw_destroy(&ill->ill_srcif_table[i].irb_lock);
1711 		}
1712 		kmem_free(ill->ill_srcif_table,
1713 		    IP_SRCIF_TABLE_SIZE * sizeof (irb_t));
1714 		ill->ill_srcif_table = NULL;
1715 		ill->ill_srcif_refcnt = 0;
1716 		ill->ill_mrtun_refcnt = 0;
1717 	}
1718 	mutex_exit(&ill->ill_lock);
1719 }
1720 
1721 /*
1722  * ire_walk routine used to delete every IRE that depends on queues
1723  * associated with 'ill'.  (Always called as writer.)
1724  */
1725 static void
1726 ill_downi(ire_t *ire, char *ill_arg)
1727 {
1728 	ill_t	*ill = (ill_t *)ill_arg;
1729 
1730 	/*
1731 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1732 	 * interface X and ipif coming from interface Y, if interface
1733 	 * X and Y are part of the same IPMP group. Thus whenever interface
1734 	 * X goes down, remove all references to it by checking both
1735 	 * on ire_ipif and ire_stq.
1736 	 */
1737 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1738 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1739 		ire_delete(ire);
1740 	}
1741 }
1742 
1743 /*
1744  * A seperate routine for deleting revtun and srcif based routes
1745  * are needed because the ires only deleted when the interface
1746  * is unplumbed. Also these ires have ire_in_ill non-null as well.
1747  * we want to keep mobile IP specific code separate.
1748  */
1749 static void
1750 ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg)
1751 {
1752 	ill_t   *ill = (ill_t *)ill_arg;
1753 
1754 	ASSERT(ire->ire_in_ill != NULL);
1755 
1756 	if ((ire->ire_in_ill != NULL && ire->ire_in_ill == ill) ||
1757 	    (ire->ire_stq == ill->ill_wq) || (ire->ire_stq == ill->ill_rq)) {
1758 		ire_delete(ire);
1759 	}
1760 }
1761 
1762 /*
1763  * Remove ire/nce from the fastpath list.
1764  */
1765 void
1766 ill_fastpath_nack(ill_t *ill)
1767 {
1768 	nce_fastpath_list_dispatch(ill, NULL, NULL);
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 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1796 		mp1 = mp->b_cont;
1797 		freeb(mp);
1798 		mp = mp1;
1799 	} else {
1800 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1801 	}
1802 
1803 	freeb(mp);
1804 }
1805 
1806 /*
1807  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1808  * The data portion of the request is a dl_unitdata_req_t template for
1809  * what we would send downstream in the absence of a fastpath confirmation.
1810  */
1811 int
1812 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1813 {
1814 	struct iocblk	*ioc;
1815 	mblk_t	*mp;
1816 
1817 	if (dlur_mp == NULL)
1818 		return (EINVAL);
1819 
1820 	mutex_enter(&ill->ill_lock);
1821 	switch (ill->ill_dlpi_fastpath_state) {
1822 	case IDS_FAILED:
1823 		/*
1824 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1825 		 * support it.
1826 		 */
1827 		mutex_exit(&ill->ill_lock);
1828 		return (ENOTSUP);
1829 	case IDS_UNKNOWN:
1830 		/* This is the first probe */
1831 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1832 		break;
1833 	default:
1834 		break;
1835 	}
1836 	mutex_exit(&ill->ill_lock);
1837 
1838 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1839 		return (EAGAIN);
1840 
1841 	mp->b_cont = copyb(dlur_mp);
1842 	if (mp->b_cont == NULL) {
1843 		freeb(mp);
1844 		return (EAGAIN);
1845 	}
1846 
1847 	ioc = (struct iocblk *)mp->b_rptr;
1848 	ioc->ioc_count = msgdsize(mp->b_cont);
1849 
1850 	putnext(ill->ill_wq, mp);
1851 	return (0);
1852 }
1853 
1854 void
1855 ill_capability_probe(ill_t *ill)
1856 {
1857 	/*
1858 	 * Do so only if negotiation is enabled, capabilities are unknown,
1859 	 * and a capability negotiation is not already in progress.
1860 	 */
1861 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN &&
1862 	    ill->ill_dlpi_capab_state != IDS_RENEG)
1863 		return;
1864 
1865 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1866 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1867 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1868 }
1869 
1870 void
1871 ill_capability_reset(ill_t *ill)
1872 {
1873 	mblk_t *sc_mp = NULL;
1874 	mblk_t *tmp;
1875 
1876 	/*
1877 	 * Note here that we reset the state to UNKNOWN, and later send
1878 	 * down the DL_CAPABILITY_REQ without first setting the state to
1879 	 * INPROGRESS.  We do this in order to distinguish the
1880 	 * DL_CAPABILITY_ACK response which may come back in response to
1881 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1882 	 * also handle the case where the driver doesn't send us back
1883 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1884 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1885 	 * features are turned off until the state reaches IDS_OK.
1886 	 */
1887 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1888 
1889 	/*
1890 	 * Disable sub-capabilities and request a list of sub-capability
1891 	 * messages which will be sent down to the driver.  Each handler
1892 	 * allocates the corresponding dl_capability_sub_t inside an
1893 	 * mblk, and links it to the existing sc_mp mblk, or return it
1894 	 * as sc_mp if it's the first sub-capability (the passed in
1895 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1896 	 * sc_mp will be pulled-up, before passing it downstream.
1897 	 */
1898 	ill_capability_mdt_reset(ill, &sc_mp);
1899 	ill_capability_hcksum_reset(ill, &sc_mp);
1900 	ill_capability_zerocopy_reset(ill, &sc_mp);
1901 	ill_capability_ipsec_reset(ill, &sc_mp);
1902 	ill_capability_dls_reset(ill, &sc_mp);
1903 	ill_capability_lso_reset(ill, &sc_mp);
1904 
1905 	/* Nothing to send down in order to disable the capabilities? */
1906 	if (sc_mp == NULL)
1907 		return;
1908 
1909 	tmp = msgpullup(sc_mp, -1);
1910 	freemsg(sc_mp);
1911 	if ((sc_mp = tmp) == NULL) {
1912 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1913 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1914 		return;
1915 	}
1916 
1917 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1918 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1919 }
1920 
1921 /*
1922  * Request or set new-style hardware capabilities supported by DLS provider.
1923  */
1924 static void
1925 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1926 {
1927 	mblk_t *mp;
1928 	dl_capability_req_t *capb;
1929 	size_t size = 0;
1930 	uint8_t *ptr;
1931 
1932 	if (reqp != NULL)
1933 		size = MBLKL(reqp);
1934 
1935 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1936 	if (mp == NULL) {
1937 		freemsg(reqp);
1938 		return;
1939 	}
1940 	ptr = mp->b_rptr;
1941 
1942 	capb = (dl_capability_req_t *)ptr;
1943 	ptr += sizeof (dl_capability_req_t);
1944 
1945 	if (reqp != NULL) {
1946 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1947 		capb->dl_sub_length = size;
1948 		bcopy(reqp->b_rptr, ptr, size);
1949 		ptr += size;
1950 		mp->b_cont = reqp->b_cont;
1951 		freeb(reqp);
1952 	}
1953 	ASSERT(ptr == mp->b_wptr);
1954 
1955 	ill_dlpi_send(ill, mp);
1956 }
1957 
1958 static void
1959 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1960 {
1961 	dl_capab_id_t *id_ic;
1962 	uint_t sub_dl_cap = outers->dl_cap;
1963 	dl_capability_sub_t *inners;
1964 	uint8_t *capend;
1965 
1966 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1967 
1968 	/*
1969 	 * Note: range checks here are not absolutely sufficient to
1970 	 * make us robust against malformed messages sent by drivers;
1971 	 * this is in keeping with the rest of IP's dlpi handling.
1972 	 * (Remember, it's coming from something else in the kernel
1973 	 * address space)
1974 	 */
1975 
1976 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1977 	if (capend > mp->b_wptr) {
1978 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1979 		    "malformed sub-capability too long for mblk");
1980 		return;
1981 	}
1982 
1983 	id_ic = (dl_capab_id_t *)(outers + 1);
1984 
1985 	if (outers->dl_length < sizeof (*id_ic) ||
1986 	    (inners = &id_ic->id_subcap,
1987 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1988 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1989 		    "encapsulated capab type %d too long for mblk",
1990 		    inners->dl_cap);
1991 		return;
1992 	}
1993 
1994 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1995 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1996 		    "isn't as expected; pass-thru module(s) detected, "
1997 		    "discarding capability\n", inners->dl_cap));
1998 		return;
1999 	}
2000 
2001 	/* Process the encapsulated sub-capability */
2002 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
2003 }
2004 
2005 /*
2006  * Process Multidata Transmit capability negotiation ack received from a
2007  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
2008  * DL_CAPABILITY_ACK message.
2009  */
2010 static void
2011 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2012 {
2013 	mblk_t *nmp = NULL;
2014 	dl_capability_req_t *oc;
2015 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
2016 	ill_mdt_capab_t **ill_mdt_capab;
2017 	uint_t sub_dl_cap = isub->dl_cap;
2018 	uint8_t *capend;
2019 
2020 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
2021 
2022 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
2023 
2024 	/*
2025 	 * Note: range checks here are not absolutely sufficient to
2026 	 * make us robust against malformed messages sent by drivers;
2027 	 * this is in keeping with the rest of IP's dlpi handling.
2028 	 * (Remember, it's coming from something else in the kernel
2029 	 * address space)
2030 	 */
2031 
2032 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2033 	if (capend > mp->b_wptr) {
2034 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2035 		    "malformed sub-capability too long for mblk");
2036 		return;
2037 	}
2038 
2039 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
2040 
2041 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
2042 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
2043 		    "unsupported MDT sub-capability (version %d, expected %d)",
2044 		    mdt_ic->mdt_version, MDT_VERSION_2);
2045 		return;
2046 	}
2047 
2048 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
2049 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
2050 		    "capability isn't as expected; pass-thru module(s) "
2051 		    "detected, discarding capability\n"));
2052 		return;
2053 	}
2054 
2055 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
2056 
2057 		if (*ill_mdt_capab == NULL) {
2058 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
2059 			    KM_NOSLEEP);
2060 
2061 			if (*ill_mdt_capab == NULL) {
2062 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2063 				    "could not enable MDT version %d "
2064 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
2065 				    ill->ill_name);
2066 				return;
2067 			}
2068 		}
2069 
2070 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
2071 		    "MDT version %d (%d bytes leading, %d bytes trailing "
2072 		    "header spaces, %d max pld bufs, %d span limit)\n",
2073 		    ill->ill_name, MDT_VERSION_2,
2074 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
2075 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
2076 
2077 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
2078 		(*ill_mdt_capab)->ill_mdt_on = 1;
2079 		/*
2080 		 * Round the following values to the nearest 32-bit; ULP
2081 		 * may further adjust them to accomodate for additional
2082 		 * protocol headers.  We pass these values to ULP during
2083 		 * bind time.
2084 		 */
2085 		(*ill_mdt_capab)->ill_mdt_hdr_head =
2086 		    roundup(mdt_ic->mdt_hdr_head, 4);
2087 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
2088 		    roundup(mdt_ic->mdt_hdr_tail, 4);
2089 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
2090 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
2091 
2092 		ill->ill_capabilities |= ILL_CAPAB_MDT;
2093 	} else {
2094 		uint_t size;
2095 		uchar_t *rptr;
2096 
2097 		size = sizeof (dl_capability_req_t) +
2098 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
2099 
2100 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2101 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2102 			    "could not enable MDT for %s (ENOMEM)\n",
2103 			    ill->ill_name);
2104 			return;
2105 		}
2106 
2107 		rptr = nmp->b_rptr;
2108 		/* initialize dl_capability_req_t */
2109 		oc = (dl_capability_req_t *)nmp->b_rptr;
2110 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2111 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2112 		    sizeof (dl_capab_mdt_t);
2113 		nmp->b_rptr += sizeof (dl_capability_req_t);
2114 
2115 		/* initialize dl_capability_sub_t */
2116 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2117 		nmp->b_rptr += sizeof (*isub);
2118 
2119 		/* initialize dl_capab_mdt_t */
2120 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2121 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2122 
2123 		nmp->b_rptr = rptr;
2124 
2125 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2126 		    "to enable MDT version %d\n", ill->ill_name,
2127 		    MDT_VERSION_2));
2128 
2129 		/* set ENABLE flag */
2130 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2131 
2132 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2133 		ill_dlpi_send(ill, nmp);
2134 	}
2135 }
2136 
2137 static void
2138 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2139 {
2140 	mblk_t *mp;
2141 	dl_capab_mdt_t *mdt_subcap;
2142 	dl_capability_sub_t *dl_subcap;
2143 	int size;
2144 
2145 	if (!ILL_MDT_CAPABLE(ill))
2146 		return;
2147 
2148 	ASSERT(ill->ill_mdt_capab != NULL);
2149 	/*
2150 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2151 	 * structure since it's possible that another thread is still
2152 	 * referring to it.  The structure only gets deallocated when
2153 	 * we destroy the ill.
2154 	 */
2155 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2156 
2157 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2158 
2159 	mp = allocb(size, BPRI_HI);
2160 	if (mp == NULL) {
2161 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2162 		    "request to disable MDT\n"));
2163 		return;
2164 	}
2165 
2166 	mp->b_wptr = mp->b_rptr + size;
2167 
2168 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2169 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2170 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2171 
2172 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2173 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2174 	mdt_subcap->mdt_flags = 0;
2175 	mdt_subcap->mdt_hdr_head = 0;
2176 	mdt_subcap->mdt_hdr_tail = 0;
2177 
2178 	if (*sc_mp != NULL)
2179 		linkb(*sc_mp, mp);
2180 	else
2181 		*sc_mp = mp;
2182 }
2183 
2184 /*
2185  * Send a DL_NOTIFY_REQ to the specified ill to enable
2186  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2187  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2188  * acceleration.
2189  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2190  */
2191 static boolean_t
2192 ill_enable_promisc_notify(ill_t *ill)
2193 {
2194 	mblk_t *mp;
2195 	dl_notify_req_t *req;
2196 
2197 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2198 
2199 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2200 	if (mp == NULL)
2201 		return (B_FALSE);
2202 
2203 	req = (dl_notify_req_t *)mp->b_rptr;
2204 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2205 	    DL_NOTE_PROMISC_OFF_PHYS;
2206 
2207 	ill_dlpi_send(ill, mp);
2208 
2209 	return (B_TRUE);
2210 }
2211 
2212 
2213 /*
2214  * Allocate an IPsec capability request which will be filled by our
2215  * caller to turn on support for one or more algorithms.
2216  */
2217 static mblk_t *
2218 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2219 {
2220 	mblk_t *nmp;
2221 	dl_capability_req_t	*ocap;
2222 	dl_capab_ipsec_t	*ocip;
2223 	dl_capab_ipsec_t	*icip;
2224 	uint8_t			*ptr;
2225 	icip = (dl_capab_ipsec_t *)(isub + 1);
2226 
2227 	/*
2228 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2229 	 * PROMISC_ON/OFF notification from the provider. We need to
2230 	 * do this before enabling the algorithms to avoid leakage of
2231 	 * cleartext packets.
2232 	 */
2233 
2234 	if (!ill_enable_promisc_notify(ill))
2235 		return (NULL);
2236 
2237 	/*
2238 	 * Allocate new mblk which will contain a new capability
2239 	 * request to enable the capabilities.
2240 	 */
2241 
2242 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2243 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2244 	if (nmp == NULL)
2245 		return (NULL);
2246 
2247 	ptr = nmp->b_rptr;
2248 
2249 	/* initialize dl_capability_req_t */
2250 	ocap = (dl_capability_req_t *)ptr;
2251 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2252 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2253 	ptr += sizeof (dl_capability_req_t);
2254 
2255 	/* initialize dl_capability_sub_t */
2256 	bcopy(isub, ptr, sizeof (*isub));
2257 	ptr += sizeof (*isub);
2258 
2259 	/* initialize dl_capab_ipsec_t */
2260 	ocip = (dl_capab_ipsec_t *)ptr;
2261 	bcopy(icip, ocip, sizeof (*icip));
2262 
2263 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2264 	return (nmp);
2265 }
2266 
2267 /*
2268  * Process an IPsec capability negotiation ack received from a DLS Provider.
2269  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2270  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2271  */
2272 static void
2273 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2274 {
2275 	dl_capab_ipsec_t	*icip;
2276 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2277 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2278 	uint_t cipher, nciphers;
2279 	mblk_t *nmp;
2280 	uint_t alg_len;
2281 	boolean_t need_sadb_dump;
2282 	uint_t sub_dl_cap = isub->dl_cap;
2283 	ill_ipsec_capab_t **ill_capab;
2284 	uint64_t ill_capab_flag;
2285 	uint8_t *capend, *ciphend;
2286 	boolean_t sadb_resync;
2287 
2288 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2289 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2290 
2291 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2292 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2293 		ill_capab_flag = ILL_CAPAB_AH;
2294 	} else {
2295 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2296 		ill_capab_flag = ILL_CAPAB_ESP;
2297 	}
2298 
2299 	/*
2300 	 * If the ill capability structure exists, then this incoming
2301 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2302 	 * If this is so, then we'd need to resynchronize the SADB
2303 	 * after re-enabling the offloaded ciphers.
2304 	 */
2305 	sadb_resync = (*ill_capab != NULL);
2306 
2307 	/*
2308 	 * Note: range checks here are not absolutely sufficient to
2309 	 * make us robust against malformed messages sent by drivers;
2310 	 * this is in keeping with the rest of IP's dlpi handling.
2311 	 * (Remember, it's coming from something else in the kernel
2312 	 * address space)
2313 	 */
2314 
2315 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2316 	if (capend > mp->b_wptr) {
2317 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2318 		    "malformed sub-capability too long for mblk");
2319 		return;
2320 	}
2321 
2322 	/*
2323 	 * There are two types of acks we process here:
2324 	 * 1. acks in reply to a (first form) generic capability req
2325 	 *    (no ENABLE flag set)
2326 	 * 2. acks in reply to a ENABLE capability req.
2327 	 *    (ENABLE flag set)
2328 	 *
2329 	 * We process the subcapability passed as argument as follows:
2330 	 * 1 do initializations
2331 	 *   1.1 initialize nmp = NULL
2332 	 *   1.2 set need_sadb_dump to B_FALSE
2333 	 * 2 for each cipher in subcapability:
2334 	 *   2.1 if ENABLE flag is set:
2335 	 *	2.1.1 update per-ill ipsec capabilities info
2336 	 *	2.1.2 set need_sadb_dump to B_TRUE
2337 	 *   2.2 if ENABLE flag is not set:
2338 	 *	2.2.1 if nmp is NULL:
2339 	 *		2.2.1.1 allocate and initialize nmp
2340 	 *		2.2.1.2 init current pos in nmp
2341 	 *	2.2.2 copy current cipher to current pos in nmp
2342 	 *	2.2.3 set ENABLE flag in nmp
2343 	 *	2.2.4 update current pos
2344 	 * 3 if nmp is not equal to NULL, send enable request
2345 	 *   3.1 send capability request
2346 	 * 4 if need_sadb_dump is B_TRUE
2347 	 *   4.1 enable promiscuous on/off notifications
2348 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2349 	 *	AH or ESP SA's to interface.
2350 	 */
2351 
2352 	nmp = NULL;
2353 	oalg = NULL;
2354 	need_sadb_dump = B_FALSE;
2355 	icip = (dl_capab_ipsec_t *)(isub + 1);
2356 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2357 
2358 	nciphers = icip->cip_nciphers;
2359 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2360 
2361 	if (ciphend > capend) {
2362 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2363 		    "too many ciphers for sub-capability len");
2364 		return;
2365 	}
2366 
2367 	for (cipher = 0; cipher < nciphers; cipher++) {
2368 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2369 
2370 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2371 			/*
2372 			 * TBD: when we provide a way to disable capabilities
2373 			 * from above, need to manage the request-pending state
2374 			 * and fail if we were not expecting this ACK.
2375 			 */
2376 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2377 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2378 
2379 			/*
2380 			 * Update IPsec capabilities for this ill
2381 			 */
2382 
2383 			if (*ill_capab == NULL) {
2384 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2385 				    ("ill_capability_ipsec_ack: "
2386 					"allocating ipsec_capab for ill\n"));
2387 				*ill_capab = ill_ipsec_capab_alloc();
2388 
2389 				if (*ill_capab == NULL) {
2390 					cmn_err(CE_WARN,
2391 					    "ill_capability_ipsec_ack: "
2392 					    "could not enable IPsec Hardware "
2393 					    "acceleration for %s (ENOMEM)\n",
2394 					    ill->ill_name);
2395 					return;
2396 				}
2397 			}
2398 
2399 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2400 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2401 
2402 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2403 				cmn_err(CE_WARN,
2404 				    "ill_capability_ipsec_ack: "
2405 				    "malformed IPsec algorithm id %d",
2406 				    ialg->alg_prim);
2407 				continue;
2408 			}
2409 
2410 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2411 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2412 				    ialg->alg_prim);
2413 			} else {
2414 				ipsec_capab_algparm_t *alp;
2415 
2416 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2417 				    ialg->alg_prim);
2418 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2419 				    ialg->alg_prim)) {
2420 					cmn_err(CE_WARN,
2421 					    "ill_capability_ipsec_ack: "
2422 					    "no space for IPsec alg id %d",
2423 					    ialg->alg_prim);
2424 					continue;
2425 				}
2426 				alp = &((*ill_capab)->encr_algparm[
2427 						ialg->alg_prim]);
2428 				alp->minkeylen = ialg->alg_minbits;
2429 				alp->maxkeylen = ialg->alg_maxbits;
2430 			}
2431 			ill->ill_capabilities |= ill_capab_flag;
2432 			/*
2433 			 * indicate that a capability was enabled, which
2434 			 * will be used below to kick off a SADB dump
2435 			 * to the ill.
2436 			 */
2437 			need_sadb_dump = B_TRUE;
2438 		} else {
2439 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2440 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2441 				ialg->alg_prim));
2442 
2443 			if (nmp == NULL) {
2444 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2445 				if (nmp == NULL) {
2446 					/*
2447 					 * Sending the PROMISC_ON/OFF
2448 					 * notification request failed.
2449 					 * We cannot enable the algorithms
2450 					 * since the Provider will not
2451 					 * notify IP of promiscous mode
2452 					 * changes, which could lead
2453 					 * to leakage of packets.
2454 					 */
2455 					cmn_err(CE_WARN,
2456 					    "ill_capability_ipsec_ack: "
2457 					    "could not enable IPsec Hardware "
2458 					    "acceleration for %s (ENOMEM)\n",
2459 					    ill->ill_name);
2460 					return;
2461 				}
2462 				/* ptr to current output alg specifier */
2463 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2464 			}
2465 
2466 			/*
2467 			 * Copy current alg specifier, set ENABLE
2468 			 * flag, and advance to next output alg.
2469 			 * For now we enable all IPsec capabilities.
2470 			 */
2471 			ASSERT(oalg != NULL);
2472 			bcopy(ialg, oalg, alg_len);
2473 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2474 			nmp->b_wptr += alg_len;
2475 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2476 		}
2477 
2478 		/* move to next input algorithm specifier */
2479 		ialg = (dl_capab_ipsec_alg_t *)
2480 		    ((char *)ialg + alg_len);
2481 	}
2482 
2483 	if (nmp != NULL)
2484 		/*
2485 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2486 		 * IPsec hardware acceleration.
2487 		 */
2488 		ill_dlpi_send(ill, nmp);
2489 
2490 	if (need_sadb_dump)
2491 		/*
2492 		 * An acknowledgement corresponding to a request to
2493 		 * enable acceleration was received, notify SADB.
2494 		 */
2495 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2496 }
2497 
2498 /*
2499  * Given an mblk with enough space in it, create sub-capability entries for
2500  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2501  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2502  * in preparation for the reset the DL_CAPABILITY_REQ message.
2503  */
2504 static void
2505 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2506     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2507 {
2508 	dl_capab_ipsec_t *oipsec;
2509 	dl_capab_ipsec_alg_t *oalg;
2510 	dl_capability_sub_t *dl_subcap;
2511 	int i, k;
2512 
2513 	ASSERT(nciphers > 0);
2514 	ASSERT(ill_cap != NULL);
2515 	ASSERT(mp != NULL);
2516 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2517 
2518 	/* dl_capability_sub_t for "stype" */
2519 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2520 	dl_subcap->dl_cap = stype;
2521 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2522 	mp->b_wptr += sizeof (dl_capability_sub_t);
2523 
2524 	/* dl_capab_ipsec_t for "stype" */
2525 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2526 	oipsec->cip_version = 1;
2527 	oipsec->cip_nciphers = nciphers;
2528 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2529 
2530 	/* create entries for "stype" AUTH ciphers */
2531 	for (i = 0; i < ill_cap->algs_size; i++) {
2532 		for (k = 0; k < BITSPERBYTE; k++) {
2533 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2534 				continue;
2535 
2536 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2537 			bzero((void *)oalg, sizeof (*oalg));
2538 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2539 			oalg->alg_prim = k + (BITSPERBYTE * i);
2540 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2541 		}
2542 	}
2543 	/* create entries for "stype" ENCR ciphers */
2544 	for (i = 0; i < ill_cap->algs_size; i++) {
2545 		for (k = 0; k < BITSPERBYTE; k++) {
2546 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2547 				continue;
2548 
2549 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2550 			bzero((void *)oalg, sizeof (*oalg));
2551 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2552 			oalg->alg_prim = k + (BITSPERBYTE * i);
2553 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2554 		}
2555 	}
2556 }
2557 
2558 /*
2559  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2560  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2561  * POPC instruction, but our macro is more flexible for an arbitrary length
2562  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2563  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2564  * stays that way, we can reduce the number of iterations required.
2565  */
2566 #define	COUNT_1S(val, sum) {					\
2567 	uint8_t x = val & 0xff;					\
2568 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2569 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2570 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2571 }
2572 
2573 /* ARGSUSED */
2574 static void
2575 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2576 {
2577 	mblk_t *mp;
2578 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2579 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2580 	uint64_t ill_capabilities = ill->ill_capabilities;
2581 	int ah_cnt = 0, esp_cnt = 0;
2582 	int ah_len = 0, esp_len = 0;
2583 	int i, size = 0;
2584 
2585 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2586 		return;
2587 
2588 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2589 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2590 
2591 	/* Find out the number of ciphers for AH */
2592 	if (cap_ah != NULL) {
2593 		for (i = 0; i < cap_ah->algs_size; i++) {
2594 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2595 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2596 		}
2597 		if (ah_cnt > 0) {
2598 			size += sizeof (dl_capability_sub_t) +
2599 			    sizeof (dl_capab_ipsec_t);
2600 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2601 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2602 			size += ah_len;
2603 		}
2604 	}
2605 
2606 	/* Find out the number of ciphers for ESP */
2607 	if (cap_esp != NULL) {
2608 		for (i = 0; i < cap_esp->algs_size; i++) {
2609 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2610 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2611 		}
2612 		if (esp_cnt > 0) {
2613 			size += sizeof (dl_capability_sub_t) +
2614 			    sizeof (dl_capab_ipsec_t);
2615 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2616 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2617 			size += esp_len;
2618 		}
2619 	}
2620 
2621 	if (size == 0) {
2622 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2623 		    "there's nothing to reset\n"));
2624 		return;
2625 	}
2626 
2627 	mp = allocb(size, BPRI_HI);
2628 	if (mp == NULL) {
2629 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2630 		    "request to disable IPSEC Hardware Acceleration\n"));
2631 		return;
2632 	}
2633 
2634 	/*
2635 	 * Clear the capability flags for IPSec HA but retain the ill
2636 	 * capability structures since it's possible that another thread
2637 	 * is still referring to them.  The structures only get deallocated
2638 	 * when we destroy the ill.
2639 	 *
2640 	 * Various places check the flags to see if the ill is capable of
2641 	 * hardware acceleration, and by clearing them we ensure that new
2642 	 * outbound IPSec packets are sent down encrypted.
2643 	 */
2644 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2645 
2646 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2647 	if (ah_cnt > 0) {
2648 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2649 		    cap_ah, mp);
2650 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2651 	}
2652 
2653 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2654 	if (esp_cnt > 0) {
2655 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2656 		    cap_esp, mp);
2657 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2658 	}
2659 
2660 	/*
2661 	 * At this point we've composed a bunch of sub-capabilities to be
2662 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2663 	 * by the caller.  Upon receiving this reset message, the driver
2664 	 * must stop inbound decryption (by destroying all inbound SAs)
2665 	 * and let the corresponding packets come in encrypted.
2666 	 */
2667 
2668 	if (*sc_mp != NULL)
2669 		linkb(*sc_mp, mp);
2670 	else
2671 		*sc_mp = mp;
2672 }
2673 
2674 static void
2675 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2676     boolean_t encapsulated)
2677 {
2678 	boolean_t legacy = B_FALSE;
2679 
2680 	/*
2681 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2682 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2683 	 * instructed the driver to disable its advertised capabilities,
2684 	 * so there's no point in accepting any response at this moment.
2685 	 */
2686 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2687 		return;
2688 
2689 	/*
2690 	 * Note that only the following two sub-capabilities may be
2691 	 * considered as "legacy", since their original definitions
2692 	 * do not incorporate the dl_mid_t module ID token, and hence
2693 	 * may require the use of the wrapper sub-capability.
2694 	 */
2695 	switch (subp->dl_cap) {
2696 	case DL_CAPAB_IPSEC_AH:
2697 	case DL_CAPAB_IPSEC_ESP:
2698 		legacy = B_TRUE;
2699 		break;
2700 	}
2701 
2702 	/*
2703 	 * For legacy sub-capabilities which don't incorporate a queue_t
2704 	 * pointer in their structures, discard them if we detect that
2705 	 * there are intermediate modules in between IP and the driver.
2706 	 */
2707 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2708 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2709 		    "%d discarded; %d module(s) present below IP\n",
2710 		    subp->dl_cap, ill->ill_lmod_cnt));
2711 		return;
2712 	}
2713 
2714 	switch (subp->dl_cap) {
2715 	case DL_CAPAB_IPSEC_AH:
2716 	case DL_CAPAB_IPSEC_ESP:
2717 		ill_capability_ipsec_ack(ill, mp, subp);
2718 		break;
2719 	case DL_CAPAB_MDT:
2720 		ill_capability_mdt_ack(ill, mp, subp);
2721 		break;
2722 	case DL_CAPAB_HCKSUM:
2723 		ill_capability_hcksum_ack(ill, mp, subp);
2724 		break;
2725 	case DL_CAPAB_ZEROCOPY:
2726 		ill_capability_zerocopy_ack(ill, mp, subp);
2727 		break;
2728 	case DL_CAPAB_POLL:
2729 		if (!SOFT_RINGS_ENABLED())
2730 			ill_capability_dls_ack(ill, mp, subp);
2731 		break;
2732 	case DL_CAPAB_SOFT_RING:
2733 		if (SOFT_RINGS_ENABLED())
2734 			ill_capability_dls_ack(ill, mp, subp);
2735 		break;
2736 	case DL_CAPAB_LSO:
2737 		ill_capability_lso_ack(ill, mp, subp);
2738 		break;
2739 	default:
2740 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2741 		    subp->dl_cap));
2742 	}
2743 }
2744 
2745 /*
2746  * As part of negotiating polling capability, the driver tells us
2747  * the default (or normal) blanking interval and packet threshold
2748  * (the receive timer fires if blanking interval is reached or
2749  * the packet threshold is reached).
2750  *
2751  * As part of manipulating the polling interval, we always use our
2752  * estimated interval (avg service time * number of packets queued
2753  * on the squeue) but we try to blank for a minimum of
2754  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2755  * packet threshold during this time. When we are not in polling mode
2756  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2757  * rr_min_blank_ratio but up the packet cnt by a ratio of
2758  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2759  * possible although for a shorter interval.
2760  */
2761 #define	RR_MAX_BLANK_RATIO	20
2762 #define	RR_MIN_BLANK_RATIO	10
2763 #define	RR_MAX_PKT_CNT_RATIO	3
2764 #define	RR_MIN_PKT_CNT_RATIO	3
2765 
2766 /*
2767  * These can be tuned via /etc/system.
2768  */
2769 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2770 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2771 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2772 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2773 
2774 static mac_resource_handle_t
2775 ill_ring_add(void *arg, mac_resource_t *mrp)
2776 {
2777 	ill_t			*ill = (ill_t *)arg;
2778 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2779 	ill_rx_ring_t		*rx_ring;
2780 	int			ip_rx_index;
2781 
2782 	ASSERT(mrp != NULL);
2783 	if (mrp->mr_type != MAC_RX_FIFO) {
2784 		return (NULL);
2785 	}
2786 	ASSERT(ill != NULL);
2787 	ASSERT(ill->ill_dls_capab != NULL);
2788 
2789 	mutex_enter(&ill->ill_lock);
2790 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2791 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2792 		ASSERT(rx_ring != NULL);
2793 
2794 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2795 			time_t normal_blank_time =
2796 			    mrfp->mrf_normal_blank_time;
2797 			uint_t normal_pkt_cnt =
2798 			    mrfp->mrf_normal_pkt_count;
2799 
2800 			bzero(rx_ring, sizeof (ill_rx_ring_t));
2801 
2802 			rx_ring->rr_blank = mrfp->mrf_blank;
2803 			rx_ring->rr_handle = mrfp->mrf_arg;
2804 			rx_ring->rr_ill = ill;
2805 			rx_ring->rr_normal_blank_time = normal_blank_time;
2806 			rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2807 
2808 			rx_ring->rr_max_blank_time =
2809 			    normal_blank_time * rr_max_blank_ratio;
2810 			rx_ring->rr_min_blank_time =
2811 			    normal_blank_time * rr_min_blank_ratio;
2812 			rx_ring->rr_max_pkt_cnt =
2813 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2814 			rx_ring->rr_min_pkt_cnt =
2815 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2816 
2817 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2818 			mutex_exit(&ill->ill_lock);
2819 
2820 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2821 			    (int), ip_rx_index);
2822 			return ((mac_resource_handle_t)rx_ring);
2823 		}
2824 	}
2825 
2826 	/*
2827 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2828 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2829 	 * should be made configurable. Meanwhile it cause no panic because
2830 	 * driver will pass ip_input a NULL handle which will make
2831 	 * IP allocate the default squeue and Polling mode will not
2832 	 * be used for this ring.
2833 	 */
2834 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2835 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2836 
2837 	mutex_exit(&ill->ill_lock);
2838 	return (NULL);
2839 }
2840 
2841 static boolean_t
2842 ill_capability_dls_init(ill_t *ill)
2843 {
2844 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2845 	conn_t 			*connp;
2846 	size_t			sz;
2847 
2848 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2849 		if (ill_dls == NULL) {
2850 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2851 			    "soft_ring enabled for ill=%s (%p) but data "
2852 			    "structs uninitialized\n", ill->ill_name,
2853 			    (void *)ill);
2854 		}
2855 		return (B_TRUE);
2856 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2857 		if (ill_dls == NULL) {
2858 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2859 			    "polling enabled for ill=%s (%p) but data "
2860 			    "structs uninitialized\n", ill->ill_name,
2861 			(void *)ill);
2862 		}
2863 		return (B_TRUE);
2864 	}
2865 
2866 	if (ill_dls != NULL) {
2867 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2868 		/* Soft_Ring or polling is being re-enabled */
2869 
2870 		connp = ill_dls->ill_unbind_conn;
2871 		ASSERT(rx_ring != NULL);
2872 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2873 		bzero((void *)rx_ring,
2874 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2875 		ill_dls->ill_ring_tbl = rx_ring;
2876 		ill_dls->ill_unbind_conn = connp;
2877 		return (B_TRUE);
2878 	}
2879 
2880 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP)) == NULL)
2881 		return (B_FALSE);
2882 
2883 	sz = sizeof (ill_dls_capab_t);
2884 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2885 
2886 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2887 	if (ill_dls == NULL) {
2888 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2889 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2890 		    (void *)ill);
2891 		CONN_DEC_REF(connp);
2892 		return (B_FALSE);
2893 	}
2894 
2895 	/* Allocate space to hold ring table */
2896 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2897 	ill->ill_dls_capab = ill_dls;
2898 	ill_dls->ill_unbind_conn = connp;
2899 	return (B_TRUE);
2900 }
2901 
2902 /*
2903  * ill_capability_dls_disable: disable soft_ring and/or polling
2904  * capability. Since any of the rings might already be in use, need
2905  * to call ipsq_clean_all() which gets behind the squeue to disable
2906  * direct calls if necessary.
2907  */
2908 static void
2909 ill_capability_dls_disable(ill_t *ill)
2910 {
2911 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2912 
2913 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2914 		ipsq_clean_all(ill);
2915 		ill_dls->ill_tx = NULL;
2916 		ill_dls->ill_tx_handle = NULL;
2917 		ill_dls->ill_dls_change_status = NULL;
2918 		ill_dls->ill_dls_bind = NULL;
2919 		ill_dls->ill_dls_unbind = NULL;
2920 	}
2921 
2922 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2923 }
2924 
2925 static void
2926 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2927     dl_capability_sub_t *isub)
2928 {
2929 	uint_t			size;
2930 	uchar_t			*rptr;
2931 	dl_capab_dls_t	dls, *odls;
2932 	ill_dls_capab_t	*ill_dls;
2933 	mblk_t			*nmp = NULL;
2934 	dl_capability_req_t	*ocap;
2935 	uint_t			sub_dl_cap = isub->dl_cap;
2936 
2937 	if (!ill_capability_dls_init(ill))
2938 		return;
2939 	ill_dls = ill->ill_dls_capab;
2940 
2941 	/* Copy locally to get the members aligned */
2942 	bcopy((void *)idls, (void *)&dls,
2943 	    sizeof (dl_capab_dls_t));
2944 
2945 	/* Get the tx function and handle from dld */
2946 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2947 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2948 
2949 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2950 		ill_dls->ill_dls_change_status =
2951 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2952 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2953 		ill_dls->ill_dls_unbind =
2954 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2955 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2956 	}
2957 
2958 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2959 	    isub->dl_length;
2960 
2961 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2962 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2963 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2964 		    ill->ill_name, (void *)ill);
2965 		return;
2966 	}
2967 
2968 	/* initialize dl_capability_req_t */
2969 	rptr = nmp->b_rptr;
2970 	ocap = (dl_capability_req_t *)rptr;
2971 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2972 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2973 	rptr += sizeof (dl_capability_req_t);
2974 
2975 	/* initialize dl_capability_sub_t */
2976 	bcopy(isub, rptr, sizeof (*isub));
2977 	rptr += sizeof (*isub);
2978 
2979 	odls = (dl_capab_dls_t *)rptr;
2980 	rptr += sizeof (dl_capab_dls_t);
2981 
2982 	/* initialize dl_capab_dls_t to be sent down */
2983 	dls.dls_rx_handle = (uintptr_t)ill;
2984 	dls.dls_rx = (uintptr_t)ip_input;
2985 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2986 
2987 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2988 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2989 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2990 		dls.dls_flags = SOFT_RING_ENABLE;
2991 	} else {
2992 		dls.dls_flags = POLL_ENABLE;
2993 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2994 		    "to enable polling\n", ill->ill_name));
2995 	}
2996 	bcopy((void *)&dls, (void *)odls,
2997 	    sizeof (dl_capab_dls_t));
2998 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2999 	/*
3000 	 * nmp points to a DL_CAPABILITY_REQ message to
3001 	 * enable either soft_ring or polling
3002 	 */
3003 	ill_dlpi_send(ill, nmp);
3004 }
3005 
3006 static void
3007 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
3008 {
3009 	mblk_t *mp;
3010 	dl_capab_dls_t *idls;
3011 	dl_capability_sub_t *dl_subcap;
3012 	int size;
3013 
3014 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
3015 		return;
3016 
3017 	ASSERT(ill->ill_dls_capab != NULL);
3018 
3019 	size = sizeof (*dl_subcap) + sizeof (*idls);
3020 
3021 	mp = allocb(size, BPRI_HI);
3022 	if (mp == NULL) {
3023 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
3024 		    "request to disable soft_ring\n"));
3025 		return;
3026 	}
3027 
3028 	mp->b_wptr = mp->b_rptr + size;
3029 
3030 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3031 	dl_subcap->dl_length = sizeof (*idls);
3032 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
3033 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
3034 	else
3035 		dl_subcap->dl_cap = DL_CAPAB_POLL;
3036 
3037 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
3038 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
3039 		idls->dls_flags = SOFT_RING_DISABLE;
3040 	else
3041 		idls->dls_flags = POLL_DISABLE;
3042 
3043 	if (*sc_mp != NULL)
3044 		linkb(*sc_mp, mp);
3045 	else
3046 		*sc_mp = mp;
3047 }
3048 
3049 /*
3050  * Process a soft_ring/poll capability negotiation ack received
3051  * from a DLS Provider.isub must point to the sub-capability
3052  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
3053  */
3054 static void
3055 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3056 {
3057 	dl_capab_dls_t		*idls;
3058 	uint_t			sub_dl_cap = isub->dl_cap;
3059 	uint8_t			*capend;
3060 
3061 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
3062 	    sub_dl_cap == DL_CAPAB_POLL);
3063 
3064 	if (ill->ill_isv6)
3065 		return;
3066 
3067 	/*
3068 	 * Note: range checks here are not absolutely sufficient to
3069 	 * make us robust against malformed messages sent by drivers;
3070 	 * this is in keeping with the rest of IP's dlpi handling.
3071 	 * (Remember, it's coming from something else in the kernel
3072 	 * address space)
3073 	 */
3074 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3075 	if (capend > mp->b_wptr) {
3076 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
3077 		    "malformed sub-capability too long for mblk");
3078 		return;
3079 	}
3080 
3081 	/*
3082 	 * There are two types of acks we process here:
3083 	 * 1. acks in reply to a (first form) generic capability req
3084 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
3085 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
3086 	 *    capability req.
3087 	 */
3088 	idls = (dl_capab_dls_t *)(isub + 1);
3089 
3090 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
3091 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
3092 		    "capability isn't as expected; pass-thru "
3093 		    "module(s) detected, discarding capability\n"));
3094 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
3095 			/*
3096 			 * This is a capability renegotitation case.
3097 			 * The interface better be unusable at this
3098 			 * point other wise bad things will happen
3099 			 * if we disable direct calls on a running
3100 			 * and up interface.
3101 			 */
3102 			ill_capability_dls_disable(ill);
3103 		}
3104 		return;
3105 	}
3106 
3107 	switch (idls->dls_flags) {
3108 	default:
3109 		/* Disable if unknown flag */
3110 	case SOFT_RING_DISABLE:
3111 	case POLL_DISABLE:
3112 		ill_capability_dls_disable(ill);
3113 		break;
3114 	case SOFT_RING_CAPABLE:
3115 	case POLL_CAPABLE:
3116 		/*
3117 		 * If the capability was already enabled, its safe
3118 		 * to disable it first to get rid of stale information
3119 		 * and then start enabling it again.
3120 		 */
3121 		ill_capability_dls_disable(ill);
3122 		ill_capability_dls_capable(ill, idls, isub);
3123 		break;
3124 	case SOFT_RING_ENABLE:
3125 	case POLL_ENABLE:
3126 		mutex_enter(&ill->ill_lock);
3127 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3128 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3129 			ASSERT(ill->ill_dls_capab != NULL);
3130 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3131 		}
3132 		if (sub_dl_cap == DL_CAPAB_POLL &&
3133 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3134 			ASSERT(ill->ill_dls_capab != NULL);
3135 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3136 			ip1dbg(("ill_capability_dls_ack: interface %s "
3137 			    "has enabled polling\n", ill->ill_name));
3138 		}
3139 		mutex_exit(&ill->ill_lock);
3140 		break;
3141 	}
3142 }
3143 
3144 /*
3145  * Process a hardware checksum offload capability negotiation ack received
3146  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3147  * of a DL_CAPABILITY_ACK message.
3148  */
3149 static void
3150 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3151 {
3152 	dl_capability_req_t	*ocap;
3153 	dl_capab_hcksum_t	*ihck, *ohck;
3154 	ill_hcksum_capab_t	**ill_hcksum;
3155 	mblk_t			*nmp = NULL;
3156 	uint_t			sub_dl_cap = isub->dl_cap;
3157 	uint8_t			*capend;
3158 
3159 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3160 
3161 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3162 
3163 	/*
3164 	 * Note: range checks here are not absolutely sufficient to
3165 	 * make us robust against malformed messages sent by drivers;
3166 	 * this is in keeping with the rest of IP's dlpi handling.
3167 	 * (Remember, it's coming from something else in the kernel
3168 	 * address space)
3169 	 */
3170 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3171 	if (capend > mp->b_wptr) {
3172 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3173 		    "malformed sub-capability too long for mblk");
3174 		return;
3175 	}
3176 
3177 	/*
3178 	 * There are two types of acks we process here:
3179 	 * 1. acks in reply to a (first form) generic capability req
3180 	 *    (no ENABLE flag set)
3181 	 * 2. acks in reply to a ENABLE capability req.
3182 	 *    (ENABLE flag set)
3183 	 */
3184 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3185 
3186 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3187 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3188 		    "unsupported hardware checksum "
3189 		    "sub-capability (version %d, expected %d)",
3190 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3191 		return;
3192 	}
3193 
3194 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3195 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3196 		    "checksum capability isn't as expected; pass-thru "
3197 		    "module(s) detected, discarding capability\n"));
3198 		return;
3199 	}
3200 
3201 #define	CURR_HCKSUM_CAPAB				\
3202 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3203 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3204 
3205 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3206 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3207 		/* do ENABLE processing */
3208 		if (*ill_hcksum == NULL) {
3209 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3210 			    KM_NOSLEEP);
3211 
3212 			if (*ill_hcksum == NULL) {
3213 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3214 				    "could not enable hcksum version %d "
3215 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3216 				    ill->ill_name);
3217 				return;
3218 			}
3219 		}
3220 
3221 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3222 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3223 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3224 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3225 		    "has enabled hardware checksumming\n ",
3226 		    ill->ill_name));
3227 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3228 		/*
3229 		 * Enabling hardware checksum offload
3230 		 * Currently IP supports {TCP,UDP}/IPv4
3231 		 * partial and full cksum offload and
3232 		 * IPv4 header checksum offload.
3233 		 * Allocate new mblk which will
3234 		 * contain a new capability request
3235 		 * to enable hardware checksum offload.
3236 		 */
3237 		uint_t	size;
3238 		uchar_t	*rptr;
3239 
3240 		size = sizeof (dl_capability_req_t) +
3241 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3242 
3243 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3244 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3245 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3246 			    ill->ill_name);
3247 			return;
3248 		}
3249 
3250 		rptr = nmp->b_rptr;
3251 		/* initialize dl_capability_req_t */
3252 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3253 		ocap->dl_sub_offset =
3254 		    sizeof (dl_capability_req_t);
3255 		ocap->dl_sub_length =
3256 		    sizeof (dl_capability_sub_t) +
3257 		    isub->dl_length;
3258 		nmp->b_rptr += sizeof (dl_capability_req_t);
3259 
3260 		/* initialize dl_capability_sub_t */
3261 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3262 		nmp->b_rptr += sizeof (*isub);
3263 
3264 		/* initialize dl_capab_hcksum_t */
3265 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3266 		bcopy(ihck, ohck, sizeof (*ihck));
3267 
3268 		nmp->b_rptr = rptr;
3269 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3270 
3271 		/* Set ENABLE flag */
3272 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3273 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3274 
3275 		/*
3276 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3277 		 * hardware checksum acceleration.
3278 		 */
3279 		ill_dlpi_send(ill, nmp);
3280 	} else {
3281 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3282 		    "advertised %x hardware checksum capability flags\n",
3283 		    ill->ill_name, ihck->hcksum_txflags));
3284 	}
3285 }
3286 
3287 static void
3288 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3289 {
3290 	mblk_t *mp;
3291 	dl_capab_hcksum_t *hck_subcap;
3292 	dl_capability_sub_t *dl_subcap;
3293 	int size;
3294 
3295 	if (!ILL_HCKSUM_CAPABLE(ill))
3296 		return;
3297 
3298 	ASSERT(ill->ill_hcksum_capab != NULL);
3299 	/*
3300 	 * Clear the capability flag for hardware checksum offload but
3301 	 * retain the ill_hcksum_capab structure since it's possible that
3302 	 * another thread is still referring to it.  The structure only
3303 	 * gets deallocated when we destroy the ill.
3304 	 */
3305 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3306 
3307 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3308 
3309 	mp = allocb(size, BPRI_HI);
3310 	if (mp == NULL) {
3311 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3312 		    "request to disable hardware checksum offload\n"));
3313 		return;
3314 	}
3315 
3316 	mp->b_wptr = mp->b_rptr + size;
3317 
3318 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3319 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3320 	dl_subcap->dl_length = sizeof (*hck_subcap);
3321 
3322 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3323 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3324 	hck_subcap->hcksum_txflags = 0;
3325 
3326 	if (*sc_mp != NULL)
3327 		linkb(*sc_mp, mp);
3328 	else
3329 		*sc_mp = mp;
3330 }
3331 
3332 static void
3333 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3334 {
3335 	mblk_t *nmp = NULL;
3336 	dl_capability_req_t *oc;
3337 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3338 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3339 	uint_t sub_dl_cap = isub->dl_cap;
3340 	uint8_t *capend;
3341 
3342 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3343 
3344 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3345 
3346 	/*
3347 	 * Note: range checks here are not absolutely sufficient to
3348 	 * make us robust against malformed messages sent by drivers;
3349 	 * this is in keeping with the rest of IP's dlpi handling.
3350 	 * (Remember, it's coming from something else in the kernel
3351 	 * address space)
3352 	 */
3353 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3354 	if (capend > mp->b_wptr) {
3355 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3356 		    "malformed sub-capability too long for mblk");
3357 		return;
3358 	}
3359 
3360 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3361 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3362 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3363 		    "unsupported ZEROCOPY sub-capability (version %d, "
3364 		    "expected %d)", zc_ic->zerocopy_version,
3365 		    ZEROCOPY_VERSION_1);
3366 		return;
3367 	}
3368 
3369 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3370 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3371 		    "capability isn't as expected; pass-thru module(s) "
3372 		    "detected, discarding capability\n"));
3373 		return;
3374 	}
3375 
3376 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3377 		if (*ill_zerocopy_capab == NULL) {
3378 			*ill_zerocopy_capab =
3379 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3380 			    KM_NOSLEEP);
3381 
3382 			if (*ill_zerocopy_capab == NULL) {
3383 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3384 				    "could not enable Zero-copy version %d "
3385 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3386 				    ill->ill_name);
3387 				return;
3388 			}
3389 		}
3390 
3391 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3392 		    "supports Zero-copy version %d\n", ill->ill_name,
3393 		    ZEROCOPY_VERSION_1));
3394 
3395 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3396 		    zc_ic->zerocopy_version;
3397 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3398 		    zc_ic->zerocopy_flags;
3399 
3400 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3401 	} else {
3402 		uint_t size;
3403 		uchar_t *rptr;
3404 
3405 		size = sizeof (dl_capability_req_t) +
3406 		    sizeof (dl_capability_sub_t) +
3407 		    sizeof (dl_capab_zerocopy_t);
3408 
3409 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3410 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3411 			    "could not enable zerocopy for %s (ENOMEM)\n",
3412 			    ill->ill_name);
3413 			return;
3414 		}
3415 
3416 		rptr = nmp->b_rptr;
3417 		/* initialize dl_capability_req_t */
3418 		oc = (dl_capability_req_t *)rptr;
3419 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3420 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3421 		    sizeof (dl_capab_zerocopy_t);
3422 		rptr += sizeof (dl_capability_req_t);
3423 
3424 		/* initialize dl_capability_sub_t */
3425 		bcopy(isub, rptr, sizeof (*isub));
3426 		rptr += sizeof (*isub);
3427 
3428 		/* initialize dl_capab_zerocopy_t */
3429 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3430 		*zc_oc = *zc_ic;
3431 
3432 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3433 		    "to enable zero-copy version %d\n", ill->ill_name,
3434 		    ZEROCOPY_VERSION_1));
3435 
3436 		/* set VMSAFE_MEM flag */
3437 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3438 
3439 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3440 		ill_dlpi_send(ill, nmp);
3441 	}
3442 }
3443 
3444 static void
3445 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3446 {
3447 	mblk_t *mp;
3448 	dl_capab_zerocopy_t *zerocopy_subcap;
3449 	dl_capability_sub_t *dl_subcap;
3450 	int size;
3451 
3452 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3453 		return;
3454 
3455 	ASSERT(ill->ill_zerocopy_capab != NULL);
3456 	/*
3457 	 * Clear the capability flag for Zero-copy but retain the
3458 	 * ill_zerocopy_capab structure since it's possible that another
3459 	 * thread is still referring to it.  The structure only gets
3460 	 * deallocated when we destroy the ill.
3461 	 */
3462 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3463 
3464 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3465 
3466 	mp = allocb(size, BPRI_HI);
3467 	if (mp == NULL) {
3468 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3469 		    "request to disable Zero-copy\n"));
3470 		return;
3471 	}
3472 
3473 	mp->b_wptr = mp->b_rptr + size;
3474 
3475 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3476 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3477 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3478 
3479 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3480 	zerocopy_subcap->zerocopy_version =
3481 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3482 	zerocopy_subcap->zerocopy_flags = 0;
3483 
3484 	if (*sc_mp != NULL)
3485 		linkb(*sc_mp, mp);
3486 	else
3487 		*sc_mp = mp;
3488 }
3489 
3490 /*
3491  * Process Large Segment Offload capability negotiation ack received from a
3492  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3493  * DL_CAPABILITY_ACK message.
3494  */
3495 static void
3496 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3497 {
3498 	mblk_t *nmp = NULL;
3499 	dl_capability_req_t *oc;
3500 	dl_capab_lso_t *lso_ic, *lso_oc;
3501 	ill_lso_capab_t **ill_lso_capab;
3502 	uint_t sub_dl_cap = isub->dl_cap;
3503 	uint8_t *capend;
3504 
3505 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3506 
3507 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3508 
3509 	/*
3510 	 * Note: range checks here are not absolutely sufficient to
3511 	 * make us robust against malformed messages sent by drivers;
3512 	 * this is in keeping with the rest of IP's dlpi handling.
3513 	 * (Remember, it's coming from something else in the kernel
3514 	 * address space)
3515 	 */
3516 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3517 	if (capend > mp->b_wptr) {
3518 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3519 		    "malformed sub-capability too long for mblk");
3520 		return;
3521 	}
3522 
3523 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3524 
3525 	if (lso_ic->lso_version != LSO_VERSION_1) {
3526 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3527 		    "unsupported LSO sub-capability (version %d, expected %d)",
3528 		    lso_ic->lso_version, LSO_VERSION_1);
3529 		return;
3530 	}
3531 
3532 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3533 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3534 		    "capability isn't as expected; pass-thru module(s) "
3535 		    "detected, discarding capability\n"));
3536 		return;
3537 	}
3538 
3539 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3540 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3541 		if (*ill_lso_capab == NULL) {
3542 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3543 			    KM_NOSLEEP);
3544 
3545 			if (*ill_lso_capab == NULL) {
3546 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3547 				    "could not enable LSO version %d "
3548 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3549 				    ill->ill_name);
3550 				return;
3551 			}
3552 		}
3553 
3554 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3555 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3556 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3557 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3558 
3559 		ip1dbg(("ill_capability_lso_ack: interface %s "
3560 		    "has enabled LSO\n ", ill->ill_name));
3561 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3562 		uint_t size;
3563 		uchar_t *rptr;
3564 
3565 		size = sizeof (dl_capability_req_t) +
3566 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3567 
3568 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3569 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3570 			    "could not enable LSO for %s (ENOMEM)\n",
3571 			    ill->ill_name);
3572 			return;
3573 		}
3574 
3575 		rptr = nmp->b_rptr;
3576 		/* initialize dl_capability_req_t */
3577 		oc = (dl_capability_req_t *)nmp->b_rptr;
3578 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3579 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3580 		    sizeof (dl_capab_lso_t);
3581 		nmp->b_rptr += sizeof (dl_capability_req_t);
3582 
3583 		/* initialize dl_capability_sub_t */
3584 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3585 		nmp->b_rptr += sizeof (*isub);
3586 
3587 		/* initialize dl_capab_lso_t */
3588 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3589 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3590 
3591 		nmp->b_rptr = rptr;
3592 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3593 
3594 		/* set ENABLE flag */
3595 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3596 
3597 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3598 		ill_dlpi_send(ill, nmp);
3599 	} else {
3600 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3601 		    "advertised %x LSO capability flags\n",
3602 		    ill->ill_name, lso_ic->lso_flags));
3603 	}
3604 }
3605 
3606 
3607 static void
3608 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3609 {
3610 	mblk_t *mp;
3611 	dl_capab_lso_t *lso_subcap;
3612 	dl_capability_sub_t *dl_subcap;
3613 	int size;
3614 
3615 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3616 		return;
3617 
3618 	ASSERT(ill->ill_lso_capab != NULL);
3619 	/*
3620 	 * Clear the capability flag for LSO but retain the
3621 	 * ill_lso_capab structure since it's possible that another
3622 	 * thread is still referring to it.  The structure only gets
3623 	 * deallocated when we destroy the ill.
3624 	 */
3625 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3626 
3627 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3628 
3629 	mp = allocb(size, BPRI_HI);
3630 	if (mp == NULL) {
3631 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3632 		    "request to disable LSO\n"));
3633 		return;
3634 	}
3635 
3636 	mp->b_wptr = mp->b_rptr + size;
3637 
3638 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3639 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3640 	dl_subcap->dl_length = sizeof (*lso_subcap);
3641 
3642 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3643 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3644 	lso_subcap->lso_flags = 0;
3645 
3646 	if (*sc_mp != NULL)
3647 		linkb(*sc_mp, mp);
3648 	else
3649 		*sc_mp = mp;
3650 }
3651 
3652 /*
3653  * Consume a new-style hardware capabilities negotiation ack.
3654  * Called from ip_rput_dlpi_writer().
3655  */
3656 void
3657 ill_capability_ack(ill_t *ill, mblk_t *mp)
3658 {
3659 	dl_capability_ack_t *capp;
3660 	dl_capability_sub_t *subp, *endp;
3661 
3662 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3663 		ill->ill_dlpi_capab_state = IDS_OK;
3664 
3665 	capp = (dl_capability_ack_t *)mp->b_rptr;
3666 
3667 	if (capp->dl_sub_length == 0)
3668 		/* no new-style capabilities */
3669 		return;
3670 
3671 	/* make sure the driver supplied correct dl_sub_length */
3672 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3673 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3674 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3675 		return;
3676 	}
3677 
3678 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3679 	/*
3680 	 * There are sub-capabilities. Process the ones we know about.
3681 	 * Loop until we don't have room for another sub-cap header..
3682 	 */
3683 	for (subp = SC(capp, capp->dl_sub_offset),
3684 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3685 	    subp <= endp;
3686 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3687 
3688 		switch (subp->dl_cap) {
3689 		case DL_CAPAB_ID_WRAPPER:
3690 			ill_capability_id_ack(ill, mp, subp);
3691 			break;
3692 		default:
3693 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3694 			break;
3695 		}
3696 	}
3697 #undef SC
3698 }
3699 
3700 /*
3701  * This routine is called to scan the fragmentation reassembly table for
3702  * the specified ILL for any packets that are starting to smell.
3703  * dead_interval is the maximum time in seconds that will be tolerated.  It
3704  * will either be the value specified in ip_g_frag_timeout, or zero if the
3705  * ILL is shutting down and it is time to blow everything off.
3706  *
3707  * It returns the number of seconds (as a time_t) that the next frag timer
3708  * should be scheduled for, 0 meaning that the timer doesn't need to be
3709  * re-started.  Note that the method of calculating next_timeout isn't
3710  * entirely accurate since time will flow between the time we grab
3711  * current_time and the time we schedule the next timeout.  This isn't a
3712  * big problem since this is the timer for sending an ICMP reassembly time
3713  * exceeded messages, and it doesn't have to be exactly accurate.
3714  *
3715  * This function is
3716  * sometimes called as writer, although this is not required.
3717  */
3718 time_t
3719 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3720 {
3721 	ipfb_t	*ipfb;
3722 	ipfb_t	*endp;
3723 	ipf_t	*ipf;
3724 	ipf_t	*ipfnext;
3725 	mblk_t	*mp;
3726 	time_t	current_time = gethrestime_sec();
3727 	time_t	next_timeout = 0;
3728 	uint32_t	hdr_length;
3729 	mblk_t	*send_icmp_head;
3730 	mblk_t	*send_icmp_head_v6;
3731 	zoneid_t zoneid;
3732 
3733 	ipfb = ill->ill_frag_hash_tbl;
3734 	if (ipfb == NULL)
3735 		return (B_FALSE);
3736 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3737 	/* Walk the frag hash table. */
3738 	for (; ipfb < endp; ipfb++) {
3739 		send_icmp_head = NULL;
3740 		send_icmp_head_v6 = NULL;
3741 		mutex_enter(&ipfb->ipfb_lock);
3742 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3743 			time_t frag_time = current_time - ipf->ipf_timestamp;
3744 			time_t frag_timeout;
3745 
3746 			if (frag_time < dead_interval) {
3747 				/*
3748 				 * There are some outstanding fragments
3749 				 * that will timeout later.  Make note of
3750 				 * the time so that we can reschedule the
3751 				 * next timeout appropriately.
3752 				 */
3753 				frag_timeout = dead_interval - frag_time;
3754 				if (next_timeout == 0 ||
3755 				    frag_timeout < next_timeout) {
3756 					next_timeout = frag_timeout;
3757 				}
3758 				break;
3759 			}
3760 			/* Time's up.  Get it out of here. */
3761 			hdr_length = ipf->ipf_nf_hdr_len;
3762 			ipfnext = ipf->ipf_hash_next;
3763 			if (ipfnext)
3764 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3765 			*ipf->ipf_ptphn = ipfnext;
3766 			mp = ipf->ipf_mp->b_cont;
3767 			for (; mp; mp = mp->b_cont) {
3768 				/* Extra points for neatness. */
3769 				IP_REASS_SET_START(mp, 0);
3770 				IP_REASS_SET_END(mp, 0);
3771 			}
3772 			mp = ipf->ipf_mp->b_cont;
3773 			ill->ill_frag_count -= ipf->ipf_count;
3774 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3775 			ipfb->ipfb_count -= ipf->ipf_count;
3776 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3777 			ipfb->ipfb_frag_pkts--;
3778 			/*
3779 			 * We do not send any icmp message from here because
3780 			 * we currently are holding the ipfb_lock for this
3781 			 * hash chain. If we try and send any icmp messages
3782 			 * from here we may end up via a put back into ip
3783 			 * trying to get the same lock, causing a recursive
3784 			 * mutex panic. Instead we build a list and send all
3785 			 * the icmp messages after we have dropped the lock.
3786 			 */
3787 			if (ill->ill_isv6) {
3788 				if (hdr_length != 0) {
3789 					mp->b_next = send_icmp_head_v6;
3790 					send_icmp_head_v6 = mp;
3791 				} else {
3792 					freemsg(mp);
3793 				}
3794 			} else {
3795 				if (hdr_length != 0) {
3796 					mp->b_next = send_icmp_head;
3797 					send_icmp_head = mp;
3798 				} else {
3799 					freemsg(mp);
3800 				}
3801 			}
3802 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3803 			freeb(ipf->ipf_mp);
3804 		}
3805 		mutex_exit(&ipfb->ipfb_lock);
3806 		/*
3807 		 * Now need to send any icmp messages that we delayed from
3808 		 * above.
3809 		 */
3810 		while (send_icmp_head_v6 != NULL) {
3811 			ip6_t *ip6h;
3812 
3813 			mp = send_icmp_head_v6;
3814 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3815 			mp->b_next = NULL;
3816 			if (mp->b_datap->db_type == M_CTL)
3817 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3818 			else
3819 				ip6h = (ip6_t *)mp->b_rptr;
3820 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3821 			    ill);
3822 			if (zoneid == ALL_ZONES) {
3823 				freemsg(mp);
3824 			} else {
3825 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3826 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3827 				    B_FALSE, zoneid);
3828 			}
3829 		}
3830 		while (send_icmp_head != NULL) {
3831 			ipaddr_t dst;
3832 
3833 			mp = send_icmp_head;
3834 			send_icmp_head = send_icmp_head->b_next;
3835 			mp->b_next = NULL;
3836 
3837 			if (mp->b_datap->db_type == M_CTL)
3838 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3839 			else
3840 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3841 
3842 			zoneid = ipif_lookup_addr_zoneid(dst, ill);
3843 			if (zoneid == ALL_ZONES) {
3844 				freemsg(mp);
3845 			} else {
3846 				icmp_time_exceeded(ill->ill_wq, mp,
3847 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid);
3848 			}
3849 		}
3850 	}
3851 	/*
3852 	 * A non-dying ILL will use the return value to decide whether to
3853 	 * restart the frag timer, and for how long.
3854 	 */
3855 	return (next_timeout);
3856 }
3857 
3858 /*
3859  * This routine is called when the approximate count of mblk memory used
3860  * for the specified ILL has exceeded max_count.
3861  */
3862 void
3863 ill_frag_prune(ill_t *ill, uint_t max_count)
3864 {
3865 	ipfb_t	*ipfb;
3866 	ipf_t	*ipf;
3867 	size_t	count;
3868 
3869 	/*
3870 	 * If we are here within ip_min_frag_prune_time msecs remove
3871 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3872 	 * ill_frag_free_num_pkts.
3873 	 */
3874 	mutex_enter(&ill->ill_lock);
3875 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3876 	    (ip_min_frag_prune_time != 0 ?
3877 	    ip_min_frag_prune_time : msec_per_tick)) {
3878 
3879 		ill->ill_frag_free_num_pkts++;
3880 
3881 	} else {
3882 		ill->ill_frag_free_num_pkts = 0;
3883 	}
3884 	ill->ill_last_frag_clean_time = lbolt;
3885 	mutex_exit(&ill->ill_lock);
3886 
3887 	/*
3888 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3889 	 */
3890 	if (ill->ill_frag_free_num_pkts != 0) {
3891 		int ix;
3892 
3893 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3894 			ipfb = &ill->ill_frag_hash_tbl[ix];
3895 			mutex_enter(&ipfb->ipfb_lock);
3896 			if (ipfb->ipfb_ipf != NULL) {
3897 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3898 				    ill->ill_frag_free_num_pkts);
3899 			}
3900 			mutex_exit(&ipfb->ipfb_lock);
3901 		}
3902 	}
3903 	/*
3904 	 * While the reassembly list for this ILL is too big, prune a fragment
3905 	 * queue by age, oldest first.  Note that the per ILL count is
3906 	 * approximate, while the per frag hash bucket counts are accurate.
3907 	 */
3908 	while (ill->ill_frag_count > max_count) {
3909 		int	ix;
3910 		ipfb_t	*oipfb = NULL;
3911 		uint_t	oldest = UINT_MAX;
3912 
3913 		count = 0;
3914 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3915 			ipfb = &ill->ill_frag_hash_tbl[ix];
3916 			mutex_enter(&ipfb->ipfb_lock);
3917 			ipf = ipfb->ipfb_ipf;
3918 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3919 				oldest = ipf->ipf_gen;
3920 				oipfb = ipfb;
3921 			}
3922 			count += ipfb->ipfb_count;
3923 			mutex_exit(&ipfb->ipfb_lock);
3924 		}
3925 		/* Refresh the per ILL count */
3926 		ill->ill_frag_count = count;
3927 		if (oipfb == NULL) {
3928 			ill->ill_frag_count = 0;
3929 			break;
3930 		}
3931 		if (count <= max_count)
3932 			return;	/* Somebody beat us to it, nothing to do */
3933 		mutex_enter(&oipfb->ipfb_lock);
3934 		ipf = oipfb->ipfb_ipf;
3935 		if (ipf != NULL) {
3936 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3937 		}
3938 		mutex_exit(&oipfb->ipfb_lock);
3939 	}
3940 }
3941 
3942 /*
3943  * free 'free_cnt' fragmented packets starting at ipf.
3944  */
3945 void
3946 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3947 {
3948 	size_t	count;
3949 	mblk_t	*mp;
3950 	mblk_t	*tmp;
3951 	ipf_t **ipfp = ipf->ipf_ptphn;
3952 
3953 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3954 	ASSERT(ipfp != NULL);
3955 	ASSERT(ipf != NULL);
3956 
3957 	while (ipf != NULL && free_cnt-- > 0) {
3958 		count = ipf->ipf_count;
3959 		mp = ipf->ipf_mp;
3960 		ipf = ipf->ipf_hash_next;
3961 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3962 			IP_REASS_SET_START(tmp, 0);
3963 			IP_REASS_SET_END(tmp, 0);
3964 		}
3965 		ill->ill_frag_count -= count;
3966 		ASSERT(ipfb->ipfb_count >= count);
3967 		ipfb->ipfb_count -= count;
3968 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3969 		ipfb->ipfb_frag_pkts--;
3970 		freemsg(mp);
3971 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3972 	}
3973 
3974 	if (ipf)
3975 		ipf->ipf_ptphn = ipfp;
3976 	ipfp[0] = ipf;
3977 }
3978 
3979 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3980 	"obsolete and may be removed in a future release of Solaris.  Use " \
3981 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3982 
3983 /*
3984  * For obsolete per-interface forwarding configuration;
3985  * called in response to ND_GET.
3986  */
3987 /* ARGSUSED */
3988 static int
3989 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3990 {
3991 	ill_t *ill = (ill_t *)cp;
3992 
3993 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3994 
3995 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3996 	return (0);
3997 }
3998 
3999 /*
4000  * For obsolete per-interface forwarding configuration;
4001  * called in response to ND_SET.
4002  */
4003 /* ARGSUSED */
4004 static int
4005 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
4006     cred_t *ioc_cr)
4007 {
4008 	long value;
4009 	int retval;
4010 
4011 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
4012 
4013 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
4014 	    value < 0 || value > 1) {
4015 		return (EINVAL);
4016 	}
4017 
4018 	rw_enter(&ill_g_lock, RW_READER);
4019 	retval = ill_forward_set(q, mp, (value != 0), cp);
4020 	rw_exit(&ill_g_lock);
4021 	return (retval);
4022 }
4023 
4024 /*
4025  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
4026  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
4027  * up RTS_IFINFO routing socket messages for each interface whose flags we
4028  * change.
4029  */
4030 /* ARGSUSED */
4031 int
4032 ill_forward_set(queue_t *q, mblk_t *mp, boolean_t enable, caddr_t cp)
4033 {
4034 	ill_t *ill = (ill_t *)cp;
4035 	ill_group_t *illgrp;
4036 
4037 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ill_g_lock));
4038 
4039 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
4040 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)) ||
4041 	    (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK))
4042 		return (EINVAL);
4043 
4044 	/*
4045 	 * If the ill is in an IPMP group, set the forwarding policy on all
4046 	 * members of the group to the same value.
4047 	 */
4048 	illgrp = ill->ill_group;
4049 	if (illgrp != NULL) {
4050 		ill_t *tmp_ill;
4051 
4052 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
4053 		    tmp_ill = tmp_ill->ill_group_next) {
4054 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
4055 			    (enable ? "Enabling" : "Disabling"),
4056 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
4057 			    tmp_ill->ill_name));
4058 			mutex_enter(&tmp_ill->ill_lock);
4059 			if (enable)
4060 				tmp_ill->ill_flags |= ILLF_ROUTER;
4061 			else
4062 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
4063 			mutex_exit(&tmp_ill->ill_lock);
4064 			if (tmp_ill->ill_isv6)
4065 				ill_set_nce_router_flags(tmp_ill, enable);
4066 			/* Notify routing socket listeners of this change. */
4067 			ip_rts_ifmsg(tmp_ill->ill_ipif);
4068 		}
4069 	} else {
4070 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
4071 		    (enable ? "Enabling" : "Disabling"),
4072 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
4073 		mutex_enter(&ill->ill_lock);
4074 		if (enable)
4075 			ill->ill_flags |= ILLF_ROUTER;
4076 		else
4077 			ill->ill_flags &= ~ILLF_ROUTER;
4078 		mutex_exit(&ill->ill_lock);
4079 		if (ill->ill_isv6)
4080 			ill_set_nce_router_flags(ill, enable);
4081 		/* Notify routing socket listeners of this change. */
4082 		ip_rts_ifmsg(ill->ill_ipif);
4083 	}
4084 
4085 	return (0);
4086 }
4087 
4088 /*
4089  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
4090  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
4091  * set or clear.
4092  */
4093 static void
4094 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
4095 {
4096 	ipif_t *ipif;
4097 	nce_t *nce;
4098 
4099 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4100 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
4101 		if (nce != NULL) {
4102 			mutex_enter(&nce->nce_lock);
4103 			if (enable)
4104 				nce->nce_flags |= NCE_F_ISROUTER;
4105 			else
4106 				nce->nce_flags &= ~NCE_F_ISROUTER;
4107 			mutex_exit(&nce->nce_lock);
4108 			NCE_REFRELE(nce);
4109 		}
4110 	}
4111 }
4112 
4113 /*
4114  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
4115  * for this ill.  Make sure the v6/v4 question has been answered about this
4116  * ill.  The creation of this ndd variable is only for backwards compatibility.
4117  * The preferred way to control per-interface IP forwarding is through the
4118  * ILLF_ROUTER interface flag.
4119  */
4120 static int
4121 ill_set_ndd_name(ill_t *ill)
4122 {
4123 	char *suffix;
4124 
4125 	ASSERT(IAM_WRITER_ILL(ill));
4126 
4127 	if (ill->ill_isv6)
4128 		suffix = ipv6_forward_suffix;
4129 	else
4130 		suffix = ipv4_forward_suffix;
4131 
4132 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4133 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4134 	/*
4135 	 * Copies over the '\0'.
4136 	 * Note that strlen(suffix) is always bounded.
4137 	 */
4138 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4139 	    strlen(suffix) + 1);
4140 
4141 	/*
4142 	 * Use of the nd table requires holding the reader lock.
4143 	 * Modifying the nd table thru nd_load/nd_unload requires
4144 	 * the writer lock.
4145 	 */
4146 	rw_enter(&ip_g_nd_lock, RW_WRITER);
4147 	if (!nd_load(&ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4148 	    nd_ill_forward_set, (caddr_t)ill)) {
4149 		/*
4150 		 * If the nd_load failed, it only meant that it could not
4151 		 * allocate a new bunch of room for further NDD expansion.
4152 		 * Because of that, the ill_ndd_name will be set to 0, and
4153 		 * this interface is at the mercy of the global ip_forwarding
4154 		 * variable.
4155 		 */
4156 		rw_exit(&ip_g_nd_lock);
4157 		ill->ill_ndd_name = NULL;
4158 		return (ENOMEM);
4159 	}
4160 	rw_exit(&ip_g_nd_lock);
4161 	return (0);
4162 }
4163 
4164 /*
4165  * Intializes the context structure and returns the first ill in the list
4166  * cuurently start_list and end_list can have values:
4167  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4168  * IP_V4_G_HEAD		Traverse IPV4 list only.
4169  * IP_V6_G_HEAD		Traverse IPV6 list only.
4170  */
4171 
4172 /*
4173  * We don't check for CONDEMNED ills here. Caller must do that if
4174  * necessary under the ill lock.
4175  */
4176 ill_t *
4177 ill_first(int start_list, int end_list, ill_walk_context_t *ctx)
4178 {
4179 	ill_if_t *ifp;
4180 	ill_t *ill;
4181 	avl_tree_t *avl_tree;
4182 
4183 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
4184 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4185 
4186 	/*
4187 	 * setup the lists to search
4188 	 */
4189 	if (end_list != MAX_G_HEADS) {
4190 		ctx->ctx_current_list = start_list;
4191 		ctx->ctx_last_list = end_list;
4192 	} else {
4193 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4194 		ctx->ctx_current_list = 0;
4195 	}
4196 
4197 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4198 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list);
4199 		if (ifp != (ill_if_t *)
4200 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list)) {
4201 			avl_tree = &ifp->illif_avl_by_ppa;
4202 			ill = avl_first(avl_tree);
4203 			/*
4204 			 * ill is guaranteed to be non NULL or ifp should have
4205 			 * not existed.
4206 			 */
4207 			ASSERT(ill != NULL);
4208 			return (ill);
4209 		}
4210 		ctx->ctx_current_list++;
4211 	}
4212 
4213 	return (NULL);
4214 }
4215 
4216 /*
4217  * returns the next ill in the list. ill_first() must have been called
4218  * before calling ill_next() or bad things will happen.
4219  */
4220 
4221 /*
4222  * We don't check for CONDEMNED ills here. Caller must do that if
4223  * necessary under the ill lock.
4224  */
4225 ill_t *
4226 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4227 {
4228 	ill_if_t *ifp;
4229 	ill_t *ill;
4230 
4231 
4232 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
4233 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4234 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list));
4235 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4236 	    AVL_AFTER)) != NULL) {
4237 		return (ill);
4238 	}
4239 
4240 	/* goto next ill_ifp in the list. */
4241 	ifp = lastill->ill_ifptr->illif_next;
4242 
4243 	/* make sure not at end of circular list */
4244 	while (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list)) {
4245 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4246 			return (NULL);
4247 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list);
4248 	}
4249 
4250 	return (avl_first(&ifp->illif_avl_by_ppa));
4251 }
4252 
4253 /*
4254  * Check interface name for correct format which is name+ppa.
4255  * name can contain characters and digits, the right most digits
4256  * make up the ppa number. use of octal is not allowed, name must contain
4257  * a ppa, return pointer to the start of ppa.
4258  * In case of error return NULL.
4259  */
4260 static char *
4261 ill_get_ppa_ptr(char *name)
4262 {
4263 	int namelen = mi_strlen(name);
4264 
4265 	int len = namelen;
4266 
4267 	name += len;
4268 	while (len > 0) {
4269 		name--;
4270 		if (*name < '0' || *name > '9')
4271 			break;
4272 		len--;
4273 	}
4274 
4275 	/* empty string, all digits, or no trailing digits */
4276 	if (len == 0 || len == (int)namelen)
4277 		return (NULL);
4278 
4279 	name++;
4280 	/* check for attempted use of octal */
4281 	if (*name == '0' && len != (int)namelen - 1)
4282 		return (NULL);
4283 	return (name);
4284 }
4285 
4286 /*
4287  * use avl tree to locate the ill.
4288  */
4289 static ill_t *
4290 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4291     ipsq_func_t func, int *error)
4292 {
4293 	char *ppa_ptr = NULL;
4294 	int len;
4295 	uint_t ppa;
4296 	ill_t *ill = NULL;
4297 	ill_if_t *ifp;
4298 	int list;
4299 	ipsq_t *ipsq;
4300 
4301 	if (error != NULL)
4302 		*error = 0;
4303 
4304 	/*
4305 	 * get ppa ptr
4306 	 */
4307 	if (isv6)
4308 		list = IP_V6_G_HEAD;
4309 	else
4310 		list = IP_V4_G_HEAD;
4311 
4312 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4313 		if (error != NULL)
4314 			*error = ENXIO;
4315 		return (NULL);
4316 	}
4317 
4318 	len = ppa_ptr - name + 1;
4319 
4320 	ppa = stoi(&ppa_ptr);
4321 
4322 	ifp = IP_VX_ILL_G_LIST(list);
4323 
4324 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list)) {
4325 		/*
4326 		 * match is done on len - 1 as the name is not null
4327 		 * terminated it contains ppa in addition to the interface
4328 		 * name.
4329 		 */
4330 		if ((ifp->illif_name_len == len) &&
4331 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4332 			break;
4333 		} else {
4334 			ifp = ifp->illif_next;
4335 		}
4336 	}
4337 
4338 
4339 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list)) {
4340 		/*
4341 		 * Even the interface type does not exist.
4342 		 */
4343 		if (error != NULL)
4344 			*error = ENXIO;
4345 		return (NULL);
4346 	}
4347 
4348 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4349 	if (ill != NULL) {
4350 		/*
4351 		 * The block comment at the start of ipif_down
4352 		 * explains the use of the macros used below
4353 		 */
4354 		GRAB_CONN_LOCK(q);
4355 		mutex_enter(&ill->ill_lock);
4356 		if (ILL_CAN_LOOKUP(ill)) {
4357 			ill_refhold_locked(ill);
4358 			mutex_exit(&ill->ill_lock);
4359 			RELEASE_CONN_LOCK(q);
4360 			return (ill);
4361 		} else if (ILL_CAN_WAIT(ill, q)) {
4362 			ipsq = ill->ill_phyint->phyint_ipsq;
4363 			mutex_enter(&ipsq->ipsq_lock);
4364 			mutex_exit(&ill->ill_lock);
4365 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4366 			mutex_exit(&ipsq->ipsq_lock);
4367 			RELEASE_CONN_LOCK(q);
4368 			*error = EINPROGRESS;
4369 			return (NULL);
4370 		}
4371 		mutex_exit(&ill->ill_lock);
4372 		RELEASE_CONN_LOCK(q);
4373 	}
4374 	if (error != NULL)
4375 		*error = ENXIO;
4376 	return (NULL);
4377 }
4378 
4379 /*
4380  * comparison function for use with avl.
4381  */
4382 static int
4383 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4384 {
4385 	uint_t ppa;
4386 	uint_t ill_ppa;
4387 
4388 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4389 
4390 	ppa = *((uint_t *)ppa_ptr);
4391 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4392 	/*
4393 	 * We want the ill with the lowest ppa to be on the
4394 	 * top.
4395 	 */
4396 	if (ill_ppa < ppa)
4397 		return (1);
4398 	if (ill_ppa > ppa)
4399 		return (-1);
4400 	return (0);
4401 }
4402 
4403 /*
4404  * remove an interface type from the global list.
4405  */
4406 static void
4407 ill_delete_interface_type(ill_if_t *interface)
4408 {
4409 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
4410 
4411 	ASSERT(interface != NULL);
4412 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4413 
4414 	avl_destroy(&interface->illif_avl_by_ppa);
4415 	if (interface->illif_ppa_arena != NULL)
4416 		vmem_destroy(interface->illif_ppa_arena);
4417 
4418 	remque(interface);
4419 
4420 	mi_free(interface);
4421 }
4422 
4423 /* Defined in ip_netinfo.c */
4424 extern ddi_taskq_t	*eventq_queue_nic;
4425 
4426 /*
4427  * remove ill from the global list.
4428  */
4429 static void
4430 ill_glist_delete(ill_t *ill)
4431 {
4432 	char *nicname;
4433 	size_t nicnamelen;
4434 	hook_nic_event_t *info;
4435 
4436 	if (ill == NULL)
4437 		return;
4438 
4439 	rw_enter(&ill_g_lock, RW_WRITER);
4440 
4441 	if (ill->ill_name != NULL) {
4442 		nicname = kmem_alloc(ill->ill_name_length, KM_NOSLEEP);
4443 		if (nicname != NULL) {
4444 			bcopy(ill->ill_name, nicname, ill->ill_name_length);
4445 			nicnamelen = ill->ill_name_length;
4446 		}
4447 	} else {
4448 		nicname = NULL;
4449 		nicnamelen = 0;
4450 	}
4451 
4452 	/*
4453 	 * If the ill was never inserted into the AVL tree
4454 	 * we skip the if branch.
4455 	 */
4456 	if (ill->ill_ifptr != NULL) {
4457 		/*
4458 		 * remove from AVL tree and free ppa number
4459 		 */
4460 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4461 
4462 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4463 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4464 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4465 		}
4466 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4467 			ill_delete_interface_type(ill->ill_ifptr);
4468 		}
4469 
4470 		/*
4471 		 * Indicate ill is no longer in the list.
4472 		 */
4473 		ill->ill_ifptr = NULL;
4474 		ill->ill_name_length = 0;
4475 		ill->ill_name[0] = '\0';
4476 		ill->ill_ppa = UINT_MAX;
4477 	}
4478 
4479 	/*
4480 	 * Run the unplumb hook after the NIC has disappeared from being
4481 	 * visible so that attempts to revalidate its existance will fail.
4482 	 *
4483 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4484 	 * that the ordering of delivered events to listeners matches the
4485 	 * order of them in the kernel.
4486 	 */
4487 	if ((info = ill->ill_nic_event_info) != NULL) {
4488 		if (info->hne_event != NE_DOWN) {
4489 			ip2dbg(("ill_glist_delete: unexpected nic event %d "
4490 			    "attached for %s\n", info->hne_event,
4491 			    ill->ill_name));
4492 			if (info->hne_data != NULL)
4493 				kmem_free(info->hne_data, info->hne_datalen);
4494 			kmem_free(info, sizeof (hook_nic_event_t));
4495 		} else {
4496 			if (ddi_taskq_dispatch(eventq_queue_nic,
4497 			    ip_ne_queue_func, (void *)info, DDI_SLEEP)
4498 			    == DDI_FAILURE) {
4499 				ip2dbg(("ill_glist_delete: ddi_taskq_dispatch "
4500 				    "failed\n"));
4501 				if (info->hne_data != NULL)
4502 					kmem_free(info->hne_data,
4503 					    info->hne_datalen);
4504 				kmem_free(info, sizeof (hook_nic_event_t));
4505 			}
4506 		}
4507 	}
4508 
4509 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
4510 	if (info != NULL) {
4511 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
4512 		info->hne_lif = 0;
4513 		info->hne_event = NE_UNPLUMB;
4514 		info->hne_data = nicname;
4515 		info->hne_datalen = nicnamelen;
4516 		info->hne_family = ill->ill_isv6 ? ipv6 : ipv4;
4517 	} else {
4518 		ip2dbg(("ill_glist_delete: could not attach UNPLUMB nic event "
4519 		    "information for %s (ENOMEM)\n", ill->ill_name));
4520 		if (nicname != NULL)
4521 			kmem_free(nicname, nicnamelen);
4522 	}
4523 
4524 	ill->ill_nic_event_info = info;
4525 
4526 	ill_phyint_free(ill);
4527 
4528 	rw_exit(&ill_g_lock);
4529 }
4530 
4531 /*
4532  * allocate a ppa, if the number of plumbed interfaces of this type are
4533  * less than ill_no_arena do a linear search to find a unused ppa.
4534  * When the number goes beyond ill_no_arena switch to using an arena.
4535  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4536  * is the return value for an error condition, so allocation starts at one
4537  * and is decremented by one.
4538  */
4539 static int
4540 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4541 {
4542 	ill_t *tmp_ill;
4543 	uint_t start, end;
4544 	int ppa;
4545 
4546 	if (ifp->illif_ppa_arena == NULL &&
4547 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4548 		/*
4549 		 * Create an arena.
4550 		 */
4551 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4552 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4553 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4554 			/* allocate what has already been assigned */
4555 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4556 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4557 		    tmp_ill, AVL_AFTER)) {
4558 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4559 			    1,		/* size */
4560 			    1,		/* align/quantum */
4561 			    0,		/* phase */
4562 			    0,		/* nocross */
4563 		(void *)((uintptr_t)tmp_ill->ill_ppa + 1), /* minaddr */
4564 		(void *)((uintptr_t)tmp_ill->ill_ppa + 2), /* maxaddr */
4565 			    VM_NOSLEEP|VM_FIRSTFIT);
4566 			if (ppa == 0) {
4567 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4568 				    " failed while switching"));
4569 				vmem_destroy(ifp->illif_ppa_arena);
4570 				ifp->illif_ppa_arena = NULL;
4571 				break;
4572 			}
4573 		}
4574 	}
4575 
4576 	if (ifp->illif_ppa_arena != NULL) {
4577 		if (ill->ill_ppa == UINT_MAX) {
4578 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4579 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4580 			if (ppa == 0)
4581 				return (EAGAIN);
4582 			ill->ill_ppa = --ppa;
4583 		} else {
4584 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4585 			    1, 		/* size */
4586 			    1, 		/* align/quantum */
4587 			    0, 		/* phase */
4588 			    0, 		/* nocross */
4589 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4590 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4591 			    VM_NOSLEEP|VM_FIRSTFIT);
4592 			/*
4593 			 * Most likely the allocation failed because
4594 			 * the requested ppa was in use.
4595 			 */
4596 			if (ppa == 0)
4597 				return (EEXIST);
4598 		}
4599 		return (0);
4600 	}
4601 
4602 	/*
4603 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4604 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4605 	 */
4606 	if (ill->ill_ppa == UINT_MAX) {
4607 		end = UINT_MAX - 1;
4608 		start = 0;
4609 	} else {
4610 		end = start = ill->ill_ppa;
4611 	}
4612 
4613 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4614 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4615 		if (start++ >= end) {
4616 			if (ill->ill_ppa == UINT_MAX)
4617 				return (EAGAIN);
4618 			else
4619 				return (EEXIST);
4620 		}
4621 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4622 	}
4623 	ill->ill_ppa = start;
4624 	return (0);
4625 }
4626 
4627 /*
4628  * Insert ill into the list of configured ill's. Once this function completes,
4629  * the ill is globally visible and is available through lookups. More precisely
4630  * this happens after the caller drops the ill_g_lock.
4631  */
4632 static int
4633 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4634 {
4635 	ill_if_t *ill_interface;
4636 	avl_index_t where = 0;
4637 	int error;
4638 	int name_length;
4639 	int index;
4640 	boolean_t check_length = B_FALSE;
4641 
4642 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
4643 
4644 	name_length = mi_strlen(name) + 1;
4645 
4646 	if (isv6)
4647 		index = IP_V6_G_HEAD;
4648 	else
4649 		index = IP_V4_G_HEAD;
4650 
4651 	ill_interface = IP_VX_ILL_G_LIST(index);
4652 	/*
4653 	 * Search for interface type based on name
4654 	 */
4655 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index)) {
4656 		if ((ill_interface->illif_name_len == name_length) &&
4657 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4658 			break;
4659 		}
4660 		ill_interface = ill_interface->illif_next;
4661 	}
4662 
4663 	/*
4664 	 * Interface type not found, create one.
4665 	 */
4666 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index)) {
4667 
4668 		ill_g_head_t ghead;
4669 
4670 		/*
4671 		 * allocate ill_if_t structure
4672 		 */
4673 
4674 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4675 		if (ill_interface == NULL) {
4676 			return (ENOMEM);
4677 		}
4678 
4679 
4680 
4681 		(void) strcpy(ill_interface->illif_name, name);
4682 		ill_interface->illif_name_len = name_length;
4683 
4684 		avl_create(&ill_interface->illif_avl_by_ppa,
4685 		    ill_compare_ppa, sizeof (ill_t),
4686 		    offsetof(struct ill_s, ill_avl_byppa));
4687 
4688 		/*
4689 		 * link the structure in the back to maintain order
4690 		 * of configuration for ifconfig output.
4691 		 */
4692 		ghead = ill_g_heads[index];
4693 		insque(ill_interface, ghead.ill_g_list_tail);
4694 
4695 	}
4696 
4697 	if (ill->ill_ppa == UINT_MAX)
4698 		check_length = B_TRUE;
4699 
4700 	error = ill_alloc_ppa(ill_interface, ill);
4701 	if (error != 0) {
4702 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4703 			ill_delete_interface_type(ill->ill_ifptr);
4704 		return (error);
4705 	}
4706 
4707 	/*
4708 	 * When the ppa is choosen by the system, check that there is
4709 	 * enough space to insert ppa. if a specific ppa was passed in this
4710 	 * check is not required as the interface name passed in will have
4711 	 * the right ppa in it.
4712 	 */
4713 	if (check_length) {
4714 		/*
4715 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4716 		 */
4717 		char buf[sizeof (uint_t) * 3];
4718 
4719 		/*
4720 		 * convert ppa to string to calculate the amount of space
4721 		 * required for it in the name.
4722 		 */
4723 		numtos(ill->ill_ppa, buf);
4724 
4725 		/* Do we have enough space to insert ppa ? */
4726 
4727 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4728 			/* Free ppa and interface type struct */
4729 			if (ill_interface->illif_ppa_arena != NULL) {
4730 				vmem_free(ill_interface->illif_ppa_arena,
4731 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4732 			}
4733 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4734 			    0) {
4735 				ill_delete_interface_type(ill->ill_ifptr);
4736 			}
4737 
4738 			return (EINVAL);
4739 		}
4740 	}
4741 
4742 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4743 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4744 
4745 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4746 	    &where);
4747 	ill->ill_ifptr = ill_interface;
4748 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4749 
4750 	ill_phyint_reinit(ill);
4751 	return (0);
4752 }
4753 
4754 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4755 static boolean_t
4756 ipsq_init(ill_t *ill)
4757 {
4758 	ipsq_t  *ipsq;
4759 
4760 	/* Init the ipsq and impicitly enter as writer */
4761 	ill->ill_phyint->phyint_ipsq =
4762 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4763 	if (ill->ill_phyint->phyint_ipsq == NULL)
4764 		return (B_FALSE);
4765 	ipsq = ill->ill_phyint->phyint_ipsq;
4766 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4767 	ill->ill_phyint->phyint_ipsq_next = NULL;
4768 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4769 	ipsq->ipsq_refs = 1;
4770 	ipsq->ipsq_writer = curthread;
4771 	ipsq->ipsq_reentry_cnt = 1;
4772 #ifdef ILL_DEBUG
4773 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack, IP_STACK_DEPTH);
4774 #endif
4775 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4776 	return (B_TRUE);
4777 }
4778 
4779 /*
4780  * ill_init is called by ip_open when a device control stream is opened.
4781  * It does a few initializations, and shoots a DL_INFO_REQ message down
4782  * to the driver.  The response is later picked up in ip_rput_dlpi and
4783  * used to set up default mechanisms for talking to the driver.  (Always
4784  * called as writer.)
4785  *
4786  * If this function returns error, ip_open will call ip_close which in
4787  * turn will call ill_delete to clean up any memory allocated here that
4788  * is not yet freed.
4789  */
4790 int
4791 ill_init(queue_t *q, ill_t *ill)
4792 {
4793 	int	count;
4794 	dl_info_req_t	*dlir;
4795 	mblk_t	*info_mp;
4796 	uchar_t *frag_ptr;
4797 
4798 	/*
4799 	 * The ill is initialized to zero by mi_alloc*(). In addition
4800 	 * some fields already contain valid values, initialized in
4801 	 * ip_open(), before we reach here.
4802 	 */
4803 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4804 
4805 	ill->ill_rq = q;
4806 	ill->ill_wq = WR(q);
4807 
4808 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4809 	    BPRI_HI);
4810 	if (info_mp == NULL)
4811 		return (ENOMEM);
4812 
4813 	/*
4814 	 * Allocate sufficient space to contain our fragment hash table and
4815 	 * the device name.
4816 	 */
4817 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4818 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4819 	if (frag_ptr == NULL) {
4820 		freemsg(info_mp);
4821 		return (ENOMEM);
4822 	}
4823 	ill->ill_frag_ptr = frag_ptr;
4824 	ill->ill_frag_free_num_pkts = 0;
4825 	ill->ill_last_frag_clean_time = 0;
4826 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4827 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4828 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4829 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4830 		    NULL, MUTEX_DEFAULT, NULL);
4831 	}
4832 
4833 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4834 	if (ill->ill_phyint == NULL) {
4835 		freemsg(info_mp);
4836 		mi_free(frag_ptr);
4837 		return (ENOMEM);
4838 	}
4839 
4840 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4841 	/*
4842 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4843 	 * at this point because of the following reason. If we can't
4844 	 * enter the ipsq at some point and cv_wait, the writer that
4845 	 * wakes us up tries to locate us using the list of all phyints
4846 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4847 	 * If we don't set it now, we risk a missed wakeup.
4848 	 */
4849 	ill->ill_phyint->phyint_illv4 = ill;
4850 	ill->ill_ppa = UINT_MAX;
4851 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4852 
4853 	if (!ipsq_init(ill)) {
4854 		freemsg(info_mp);
4855 		mi_free(frag_ptr);
4856 		mi_free(ill->ill_phyint);
4857 		return (ENOMEM);
4858 	}
4859 
4860 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4861 
4862 
4863 	/* Frag queue limit stuff */
4864 	ill->ill_frag_count = 0;
4865 	ill->ill_ipf_gen = 0;
4866 
4867 	ill->ill_global_timer = INFINITY;
4868 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
4869 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4870 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4871 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4872 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4873 
4874 	/*
4875 	 * Initialize IPv6 configuration variables.  The IP module is always
4876 	 * opened as an IPv4 module.  Instead tracking down the cases where
4877 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4878 	 * here for convenience, this has no effect until the ill is set to do
4879 	 * IPv6.
4880 	 */
4881 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4882 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4883 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4884 	ill->ill_max_buf = ND_MAX_Q;
4885 	ill->ill_refcnt = 0;
4886 
4887 	/* Send down the Info Request to the driver. */
4888 	info_mp->b_datap->db_type = M_PCPROTO;
4889 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4890 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4891 	dlir->dl_primitive = DL_INFO_REQ;
4892 
4893 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4894 
4895 	qprocson(q);
4896 	ill_dlpi_send(ill, info_mp);
4897 
4898 	return (0);
4899 }
4900 
4901 /*
4902  * ill_dls_info
4903  * creates datalink socket info from the device.
4904  */
4905 int
4906 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4907 {
4908 	size_t	len;
4909 	ill_t	*ill = ipif->ipif_ill;
4910 
4911 	sdl->sdl_family = AF_LINK;
4912 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4913 	sdl->sdl_type = ill->ill_type;
4914 	(void) ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4915 	len = strlen(sdl->sdl_data);
4916 	ASSERT(len < 256);
4917 	sdl->sdl_nlen = (uchar_t)len;
4918 	sdl->sdl_alen = ill->ill_phys_addr_length;
4919 	sdl->sdl_slen = 0;
4920 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4921 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4922 
4923 	return (sizeof (struct sockaddr_dl));
4924 }
4925 
4926 /*
4927  * ill_xarp_info
4928  * creates xarp info from the device.
4929  */
4930 static int
4931 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4932 {
4933 	sdl->sdl_family = AF_LINK;
4934 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4935 	sdl->sdl_type = ill->ill_type;
4936 	(void) ipif_get_name(ill->ill_ipif, sdl->sdl_data,
4937 	    sizeof (sdl->sdl_data));
4938 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4939 	sdl->sdl_alen = ill->ill_phys_addr_length;
4940 	sdl->sdl_slen = 0;
4941 	return (sdl->sdl_nlen);
4942 }
4943 
4944 static int
4945 loopback_kstat_update(kstat_t *ksp, int rw)
4946 {
4947 	kstat_named_t *kn = KSTAT_NAMED_PTR(ksp);
4948 
4949 	if (rw == KSTAT_WRITE)
4950 		return (EACCES);
4951 	kn[0].value.ui32 = loopback_packets;
4952 	kn[1].value.ui32 = loopback_packets;
4953 	return (0);
4954 }
4955 
4956 
4957 /*
4958  * Has ifindex been plumbed already.
4959  */
4960 static boolean_t
4961 phyint_exists(uint_t index)
4962 {
4963 	phyint_t *phyi;
4964 
4965 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
4966 	/*
4967 	 * Indexes are stored in the phyint - a common structure
4968 	 * to both IPv4 and IPv6.
4969 	 */
4970 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
4971 	    (void *) &index, NULL);
4972 	return (phyi != NULL);
4973 }
4974 
4975 /*
4976  * Assign a unique interface index for the phyint.
4977  */
4978 static boolean_t
4979 phyint_assign_ifindex(phyint_t *phyi)
4980 {
4981 	uint_t starting_index;
4982 
4983 	ASSERT(phyi->phyint_ifindex == 0);
4984 	if (!ill_index_wrap) {
4985 		phyi->phyint_ifindex = ill_index++;
4986 		if (ill_index == 0) {
4987 			/* Reached the uint_t limit Next time wrap  */
4988 			ill_index_wrap = B_TRUE;
4989 		}
4990 		return (B_TRUE);
4991 	}
4992 
4993 	/*
4994 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4995 	 * at this point and don't want to call any function that attempts
4996 	 * to get the lock again.
4997 	 */
4998 	starting_index = ill_index++;
4999 	for (; ill_index != starting_index; ill_index++) {
5000 		if (ill_index != 0 && !phyint_exists(ill_index)) {
5001 			/* found unused index - use it */
5002 			phyi->phyint_ifindex = ill_index;
5003 			return (B_TRUE);
5004 		}
5005 	}
5006 
5007 	/*
5008 	 * all interface indicies are inuse.
5009 	 */
5010 	return (B_FALSE);
5011 }
5012 
5013 /*
5014  * Return a pointer to the ill which matches the supplied name.  Note that
5015  * the ill name length includes the null termination character.  (May be
5016  * called as writer.)
5017  * If do_alloc and the interface is "lo0" it will be automatically created.
5018  * Cannot bump up reference on condemned ills. So dup detect can't be done
5019  * using this func.
5020  */
5021 ill_t *
5022 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
5023     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc)
5024 {
5025 	ill_t	*ill;
5026 	ipif_t	*ipif;
5027 	kstat_named_t	*kn;
5028 	boolean_t isloopback;
5029 	ipsq_t *old_ipsq;
5030 
5031 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
5032 
5033 	rw_enter(&ill_g_lock, RW_READER);
5034 	ill = ill_find_by_name(name, isv6, q, mp, func, error);
5035 	rw_exit(&ill_g_lock);
5036 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
5037 		return (ill);
5038 
5039 	/*
5040 	 * Couldn't find it.  Does this happen to be a lookup for the
5041 	 * loopback device and are we allowed to allocate it?
5042 	 */
5043 	if (!isloopback || !do_alloc)
5044 		return (NULL);
5045 
5046 	rw_enter(&ill_g_lock, RW_WRITER);
5047 
5048 	ill = ill_find_by_name(name, isv6, q, mp, func, error);
5049 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
5050 		rw_exit(&ill_g_lock);
5051 		return (ill);
5052 	}
5053 
5054 	/* Create the loopback device on demand */
5055 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
5056 	    sizeof (ipif_loopback_name), BPRI_MED));
5057 	if (ill == NULL)
5058 		goto done;
5059 
5060 	*ill = ill_null;
5061 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
5062 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
5063 	if (ill->ill_phyint == NULL)
5064 		goto done;
5065 
5066 	if (isv6)
5067 		ill->ill_phyint->phyint_illv6 = ill;
5068 	else
5069 		ill->ill_phyint->phyint_illv4 = ill;
5070 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
5071 	ill->ill_max_frag = IP_LOOPBACK_MTU;
5072 	/* Add room for tcp+ip headers */
5073 	if (isv6) {
5074 		ill->ill_isv6 = B_TRUE;
5075 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
5076 	} else {
5077 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
5078 	}
5079 	if (!ill_allocate_mibs(ill))
5080 		goto done;
5081 	ill->ill_max_mtu = ill->ill_max_frag;
5082 	/*
5083 	 * ipif_loopback_name can't be pointed at directly because its used
5084 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
5085 	 * from the glist, ill_glist_delete() sets the first character of
5086 	 * ill_name to '\0'.
5087 	 */
5088 	ill->ill_name = (char *)ill + sizeof (*ill);
5089 	(void) strcpy(ill->ill_name, ipif_loopback_name);
5090 	ill->ill_name_length = sizeof (ipif_loopback_name);
5091 	/* Set ill_name_set for ill_phyint_reinit to work properly */
5092 
5093 	ill->ill_global_timer = INFINITY;
5094 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
5095 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
5096 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
5097 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
5098 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
5099 
5100 	/* No resolver here. */
5101 	ill->ill_net_type = IRE_LOOPBACK;
5102 
5103 	/* Initialize the ipsq */
5104 	if (!ipsq_init(ill))
5105 		goto done;
5106 
5107 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
5108 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
5109 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
5110 #ifdef ILL_DEBUG
5111 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
5112 #endif
5113 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
5114 	if (ipif == NULL)
5115 		goto done;
5116 
5117 	ill->ill_flags = ILLF_MULTICAST;
5118 
5119 	/* Set up default loopback address and mask. */
5120 	if (!isv6) {
5121 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5122 
5123 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5124 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5125 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5126 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5127 		    ipif->ipif_v6subnet);
5128 		ill->ill_flags |= ILLF_IPV4;
5129 	} else {
5130 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5131 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5132 		ipif->ipif_v6net_mask = ipv6_all_ones;
5133 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5134 		    ipif->ipif_v6subnet);
5135 		ill->ill_flags |= ILLF_IPV6;
5136 	}
5137 
5138 	/*
5139 	 * Chain us in at the end of the ill list. hold the ill
5140 	 * before we make it globally visible. 1 for the lookup.
5141 	 */
5142 	ill->ill_refcnt = 0;
5143 	ill_refhold(ill);
5144 
5145 	ill->ill_frag_count = 0;
5146 	ill->ill_frag_free_num_pkts = 0;
5147 	ill->ill_last_frag_clean_time = 0;
5148 
5149 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5150 
5151 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5152 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5153 
5154 	/* Let SCTP know so that it can add this to its list */
5155 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5156 
5157 	/* Let SCTP know about this IPIF, so that it can add it to its list */
5158 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
5159 
5160 	/*
5161 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5162 	 */
5163 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5164 		/* Loopback ills aren't in any IPMP group */
5165 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5166 		ipsq_delete(old_ipsq);
5167 	}
5168 
5169 	/*
5170 	 * Delay this till the ipif is allocated as ipif_allocate
5171 	 * de-references ill_phyint for getting the ifindex. We
5172 	 * can't do this before ipif_allocate because ill_phyint_reinit
5173 	 * -> phyint_assign_ifindex expects ipif to be present.
5174 	 */
5175 	mutex_enter(&ill->ill_phyint->phyint_lock);
5176 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5177 	mutex_exit(&ill->ill_phyint->phyint_lock);
5178 
5179 	if (loopback_ksp == NULL) {
5180 		/* Export loopback interface statistics */
5181 		loopback_ksp = kstat_create("lo", 0, ipif_loopback_name, "net",
5182 		    KSTAT_TYPE_NAMED, 2, 0);
5183 		if (loopback_ksp != NULL) {
5184 			loopback_ksp->ks_update = loopback_kstat_update;
5185 			kn = KSTAT_NAMED_PTR(loopback_ksp);
5186 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5187 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5188 			kstat_install(loopback_ksp);
5189 		}
5190 	}
5191 
5192 	if (error != NULL)
5193 		*error = 0;
5194 	*did_alloc = B_TRUE;
5195 	rw_exit(&ill_g_lock);
5196 	return (ill);
5197 done:
5198 	if (ill != NULL) {
5199 		if (ill->ill_phyint != NULL) {
5200 			ipsq_t	*ipsq;
5201 
5202 			ipsq = ill->ill_phyint->phyint_ipsq;
5203 			if (ipsq != NULL)
5204 				kmem_free(ipsq, sizeof (ipsq_t));
5205 			mi_free(ill->ill_phyint);
5206 		}
5207 		ill_free_mib(ill);
5208 		mi_free(ill);
5209 	}
5210 	rw_exit(&ill_g_lock);
5211 	if (error != NULL)
5212 		*error = ENOMEM;
5213 	return (NULL);
5214 }
5215 
5216 /*
5217  * Return a pointer to the ill which matches the index and IP version type.
5218  */
5219 ill_t *
5220 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5221     ipsq_func_t func, int *err)
5222 {
5223 	ill_t	*ill;
5224 	ipsq_t  *ipsq;
5225 	phyint_t *phyi;
5226 
5227 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5228 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5229 
5230 	if (err != NULL)
5231 		*err = 0;
5232 
5233 	/*
5234 	 * Indexes are stored in the phyint - a common structure
5235 	 * to both IPv4 and IPv6.
5236 	 */
5237 	rw_enter(&ill_g_lock, RW_READER);
5238 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
5239 	    (void *) &index, NULL);
5240 	if (phyi != NULL) {
5241 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5242 		if (ill != NULL) {
5243 			/*
5244 			 * The block comment at the start of ipif_down
5245 			 * explains the use of the macros used below
5246 			 */
5247 			GRAB_CONN_LOCK(q);
5248 			mutex_enter(&ill->ill_lock);
5249 			if (ILL_CAN_LOOKUP(ill)) {
5250 				ill_refhold_locked(ill);
5251 				mutex_exit(&ill->ill_lock);
5252 				RELEASE_CONN_LOCK(q);
5253 				rw_exit(&ill_g_lock);
5254 				return (ill);
5255 			} else if (ILL_CAN_WAIT(ill, q)) {
5256 				ipsq = ill->ill_phyint->phyint_ipsq;
5257 				mutex_enter(&ipsq->ipsq_lock);
5258 				rw_exit(&ill_g_lock);
5259 				mutex_exit(&ill->ill_lock);
5260 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5261 				mutex_exit(&ipsq->ipsq_lock);
5262 				RELEASE_CONN_LOCK(q);
5263 				*err = EINPROGRESS;
5264 				return (NULL);
5265 			}
5266 			RELEASE_CONN_LOCK(q);
5267 			mutex_exit(&ill->ill_lock);
5268 		}
5269 	}
5270 	rw_exit(&ill_g_lock);
5271 	if (err != NULL)
5272 		*err = ENXIO;
5273 	return (NULL);
5274 }
5275 
5276 /*
5277  * Return the ifindex next in sequence after the passed in ifindex.
5278  * If there is no next ifindex for the given protocol, return 0.
5279  */
5280 uint_t
5281 ill_get_next_ifindex(uint_t index, boolean_t isv6)
5282 {
5283 	phyint_t *phyi;
5284 	phyint_t *phyi_initial;
5285 	uint_t   ifindex;
5286 
5287 	rw_enter(&ill_g_lock, RW_READER);
5288 
5289 	if (index == 0) {
5290 		phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
5291 	} else {
5292 		phyi = phyi_initial = avl_find(
5293 		    &phyint_g_list.phyint_list_avl_by_index,
5294 		    (void *) &index, NULL);
5295 	}
5296 
5297 	for (; phyi != NULL;
5298 	    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
5299 	    phyi, AVL_AFTER)) {
5300 		/*
5301 		 * If we're not returning the first interface in the tree
5302 		 * and we still haven't moved past the phyint_t that
5303 		 * corresponds to index, avl_walk needs to be called again
5304 		 */
5305 		if (!((index != 0) && (phyi == phyi_initial))) {
5306 			if (isv6) {
5307 				if ((phyi->phyint_illv6) &&
5308 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5309 				    (phyi->phyint_illv6->ill_isv6 == 1))
5310 					break;
5311 			} else {
5312 				if ((phyi->phyint_illv4) &&
5313 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5314 				    (phyi->phyint_illv4->ill_isv6 == 0))
5315 					break;
5316 			}
5317 		}
5318 	}
5319 
5320 	rw_exit(&ill_g_lock);
5321 
5322 	if (phyi != NULL)
5323 		ifindex = phyi->phyint_ifindex;
5324 	else
5325 		ifindex = 0;
5326 
5327 	return (ifindex);
5328 }
5329 
5330 
5331 /*
5332  * Return the ifindex for the named interface.
5333  * If there is no next ifindex for the interface, return 0.
5334  */
5335 uint_t
5336 ill_get_ifindex_by_name(char *name)
5337 {
5338 	phyint_t	*phyi;
5339 	avl_index_t	where = 0;
5340 	uint_t		ifindex;
5341 
5342 	rw_enter(&ill_g_lock, RW_READER);
5343 
5344 	if ((phyi = avl_find(&phyint_g_list.phyint_list_avl_by_name,
5345 	    name, &where)) == NULL) {
5346 		rw_exit(&ill_g_lock);
5347 		return (0);
5348 	}
5349 
5350 	ifindex = phyi->phyint_ifindex;
5351 
5352 	rw_exit(&ill_g_lock);
5353 
5354 	return (ifindex);
5355 }
5356 
5357 
5358 /*
5359  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5360  * that gives a running thread a reference to the ill. This reference must be
5361  * released by the thread when it is done accessing the ill and related
5362  * objects. ill_refcnt can not be used to account for static references
5363  * such as other structures pointing to an ill. Callers must generally
5364  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5365  * or be sure that the ill is not being deleted or changing state before
5366  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5367  * ill won't change any of its critical state such as address, netmask etc.
5368  */
5369 void
5370 ill_refhold(ill_t *ill)
5371 {
5372 	mutex_enter(&ill->ill_lock);
5373 	ill->ill_refcnt++;
5374 	ILL_TRACE_REF(ill);
5375 	mutex_exit(&ill->ill_lock);
5376 }
5377 
5378 void
5379 ill_refhold_locked(ill_t *ill)
5380 {
5381 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5382 	ill->ill_refcnt++;
5383 	ILL_TRACE_REF(ill);
5384 }
5385 
5386 int
5387 ill_check_and_refhold(ill_t *ill)
5388 {
5389 	mutex_enter(&ill->ill_lock);
5390 	if (ILL_CAN_LOOKUP(ill)) {
5391 		ill_refhold_locked(ill);
5392 		mutex_exit(&ill->ill_lock);
5393 		return (0);
5394 	}
5395 	mutex_exit(&ill->ill_lock);
5396 	return (ILL_LOOKUP_FAILED);
5397 }
5398 
5399 /*
5400  * Must not be called while holding any locks. Otherwise if this is
5401  * the last reference to be released, there is a chance of recursive mutex
5402  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5403  * to restart an ioctl.
5404  */
5405 void
5406 ill_refrele(ill_t *ill)
5407 {
5408 	mutex_enter(&ill->ill_lock);
5409 	ASSERT(ill->ill_refcnt != 0);
5410 	ill->ill_refcnt--;
5411 	ILL_UNTRACE_REF(ill);
5412 	if (ill->ill_refcnt != 0) {
5413 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5414 		mutex_exit(&ill->ill_lock);
5415 		return;
5416 	}
5417 
5418 	/* Drops the ill_lock */
5419 	ipif_ill_refrele_tail(ill);
5420 }
5421 
5422 /*
5423  * Obtain a weak reference count on the ill. This reference ensures the
5424  * ill won't be freed, but the ill may change any of its critical state
5425  * such as netmask, address etc. Returns an error if the ill has started
5426  * closing.
5427  */
5428 boolean_t
5429 ill_waiter_inc(ill_t *ill)
5430 {
5431 	mutex_enter(&ill->ill_lock);
5432 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5433 		mutex_exit(&ill->ill_lock);
5434 		return (B_FALSE);
5435 	}
5436 	ill->ill_waiters++;
5437 	mutex_exit(&ill->ill_lock);
5438 	return (B_TRUE);
5439 }
5440 
5441 void
5442 ill_waiter_dcr(ill_t *ill)
5443 {
5444 	mutex_enter(&ill->ill_lock);
5445 	ill->ill_waiters--;
5446 	if (ill->ill_waiters == 0)
5447 		cv_broadcast(&ill->ill_cv);
5448 	mutex_exit(&ill->ill_lock);
5449 }
5450 
5451 /*
5452  * Named Dispatch routine to produce a formatted report on all ILLs.
5453  * This report is accessed by using the ndd utility to "get" ND variable
5454  * "ip_ill_status".
5455  */
5456 /* ARGSUSED */
5457 int
5458 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5459 {
5460 	ill_t		*ill;
5461 	ill_walk_context_t ctx;
5462 
5463 	(void) mi_mpprintf(mp,
5464 	    "ILL      " MI_COL_HDRPAD_STR
5465 	/*   01234567[89ABCDEF] */
5466 	    "rq       " MI_COL_HDRPAD_STR
5467 	/*   01234567[89ABCDEF] */
5468 	    "wq       " MI_COL_HDRPAD_STR
5469 	/*   01234567[89ABCDEF] */
5470 	    "upcnt mxfrg err name");
5471 	/*   12345 12345 123 xxxxxxxx  */
5472 
5473 	rw_enter(&ill_g_lock, RW_READER);
5474 	ill = ILL_START_WALK_ALL(&ctx);
5475 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5476 		(void) mi_mpprintf(mp,
5477 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5478 		    "%05u %05u %03d %s",
5479 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5480 		    ill->ill_ipif_up_count,
5481 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5482 	}
5483 	rw_exit(&ill_g_lock);
5484 
5485 	return (0);
5486 }
5487 
5488 /*
5489  * Named Dispatch routine to produce a formatted report on all IPIFs.
5490  * This report is accessed by using the ndd utility to "get" ND variable
5491  * "ip_ipif_status".
5492  */
5493 /* ARGSUSED */
5494 int
5495 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5496 {
5497 	char	buf1[INET6_ADDRSTRLEN];
5498 	char	buf2[INET6_ADDRSTRLEN];
5499 	char	buf3[INET6_ADDRSTRLEN];
5500 	char	buf4[INET6_ADDRSTRLEN];
5501 	char	buf5[INET6_ADDRSTRLEN];
5502 	char	buf6[INET6_ADDRSTRLEN];
5503 	char	buf[LIFNAMSIZ];
5504 	ill_t	*ill;
5505 	ipif_t	*ipif;
5506 	nv_t	*nvp;
5507 	uint64_t flags;
5508 	zoneid_t zoneid;
5509 	ill_walk_context_t ctx;
5510 
5511 	(void) mi_mpprintf(mp,
5512 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5513 	    "\tlocal address\n"
5514 	    "\tsrc address\n"
5515 	    "\tsubnet\n"
5516 	    "\tmask\n"
5517 	    "\tbroadcast\n"
5518 	    "\tp-p-dst");
5519 
5520 	ASSERT(q->q_next == NULL);
5521 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5522 
5523 	rw_enter(&ill_g_lock, RW_READER);
5524 	ill = ILL_START_WALK_ALL(&ctx);
5525 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5526 		for (ipif = ill->ill_ipif; ipif != NULL;
5527 		    ipif = ipif->ipif_next) {
5528 			if (zoneid != GLOBAL_ZONEID &&
5529 			    zoneid != ipif->ipif_zoneid &&
5530 			    ipif->ipif_zoneid != ALL_ZONES)
5531 				continue;
5532 			(void) mi_mpprintf(mp,
5533 			    MI_COL_PTRFMT_STR
5534 			    "%04u %05u %u/%u/%u %s %d",
5535 			    (void *)ipif,
5536 			    ipif->ipif_metric, ipif->ipif_mtu,
5537 			    ipif->ipif_ib_pkt_count,
5538 			    ipif->ipif_ob_pkt_count,
5539 			    ipif->ipif_fo_pkt_count,
5540 			    ipif_get_name(ipif, buf, sizeof (buf)),
5541 			    ipif->ipif_zoneid);
5542 
5543 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5544 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5545 
5546 		/* Tack on text strings for any flags. */
5547 		nvp = ipif_nv_tbl;
5548 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5549 			if (nvp->nv_value & flags)
5550 				(void) mi_mpprintf_nr(mp, " %s",
5551 				    nvp->nv_name);
5552 		}
5553 		(void) mi_mpprintf(mp,
5554 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5555 		    inet_ntop(AF_INET6,
5556 			&ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5557 		    inet_ntop(AF_INET6,
5558 			&ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5559 		    inet_ntop(AF_INET6,
5560 			&ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5561 		    inet_ntop(AF_INET6,
5562 			&ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5563 		    inet_ntop(AF_INET6,
5564 			&ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5565 		    inet_ntop(AF_INET6,
5566 			&ipif->ipif_v6pp_dst_addr,
5567 			buf6, sizeof (buf6)));
5568 		}
5569 	}
5570 	rw_exit(&ill_g_lock);
5571 	return (0);
5572 }
5573 
5574 /*
5575  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5576  * driver.  We construct best guess defaults for lower level information that
5577  * we need.  If an interface is brought up without injection of any overriding
5578  * information from outside, we have to be ready to go with these defaults.
5579  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5580  * we primarely want the dl_provider_style.
5581  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5582  * at which point we assume the other part of the information is valid.
5583  */
5584 void
5585 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5586 {
5587 	uchar_t		*brdcst_addr;
5588 	uint_t		brdcst_addr_length, phys_addr_length;
5589 	t_scalar_t	sap_length;
5590 	dl_info_ack_t	*dlia;
5591 	ip_m_t		*ipm;
5592 	dl_qos_cl_sel1_t *sel1;
5593 
5594 	ASSERT(IAM_WRITER_ILL(ill));
5595 
5596 	/*
5597 	 * Till the ill is fully up ILL_CHANGING will be set and
5598 	 * the ill is not globally visible. So no need for a lock.
5599 	 */
5600 	dlia = (dl_info_ack_t *)mp->b_rptr;
5601 	ill->ill_mactype = dlia->dl_mac_type;
5602 
5603 	ipm = ip_m_lookup(dlia->dl_mac_type);
5604 	if (ipm == NULL) {
5605 		ipm = ip_m_lookup(DL_OTHER);
5606 		ASSERT(ipm != NULL);
5607 	}
5608 	ill->ill_media = ipm;
5609 
5610 	/*
5611 	 * When the new DLPI stuff is ready we'll pull lengths
5612 	 * from dlia.
5613 	 */
5614 	if (dlia->dl_version == DL_VERSION_2) {
5615 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5616 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5617 		    brdcst_addr_length);
5618 		if (brdcst_addr == NULL) {
5619 			brdcst_addr_length = 0;
5620 		}
5621 		sap_length = dlia->dl_sap_length;
5622 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5623 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5624 		    brdcst_addr_length, sap_length, phys_addr_length));
5625 	} else {
5626 		brdcst_addr_length = 6;
5627 		brdcst_addr = ip_six_byte_all_ones;
5628 		sap_length = -2;
5629 		phys_addr_length = brdcst_addr_length;
5630 	}
5631 
5632 	ill->ill_bcast_addr_length = brdcst_addr_length;
5633 	ill->ill_phys_addr_length = phys_addr_length;
5634 	ill->ill_sap_length = sap_length;
5635 	ill->ill_max_frag = dlia->dl_max_sdu;
5636 	ill->ill_max_mtu = ill->ill_max_frag;
5637 
5638 	ill->ill_type = ipm->ip_m_type;
5639 
5640 	if (!ill->ill_dlpi_style_set) {
5641 		if (dlia->dl_provider_style == DL_STYLE2)
5642 			ill->ill_needs_attach = 1;
5643 
5644 		/*
5645 		 * Allocate the first ipif on this ill. We don't delay it
5646 		 * further as ioctl handling assumes atleast one ipif to
5647 		 * be present.
5648 		 *
5649 		 * At this point we don't know whether the ill is v4 or v6.
5650 		 * We will know this whan the SIOCSLIFNAME happens and
5651 		 * the correct value for ill_isv6 will be assigned in
5652 		 * ipif_set_values(). We need to hold the ill lock and
5653 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5654 		 * the wakeup.
5655 		 */
5656 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5657 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5658 		mutex_enter(&ill->ill_lock);
5659 		ASSERT(ill->ill_dlpi_style_set == 0);
5660 		ill->ill_dlpi_style_set = 1;
5661 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5662 		cv_broadcast(&ill->ill_cv);
5663 		mutex_exit(&ill->ill_lock);
5664 		freemsg(mp);
5665 		return;
5666 	}
5667 	ASSERT(ill->ill_ipif != NULL);
5668 	/*
5669 	 * We know whether it is IPv4 or IPv6 now, as this is the
5670 	 * second DL_INFO_ACK we are recieving in response to the
5671 	 * DL_INFO_REQ sent in ipif_set_values.
5672 	 */
5673 	if (ill->ill_isv6)
5674 		ill->ill_sap = IP6_DL_SAP;
5675 	else
5676 		ill->ill_sap = IP_DL_SAP;
5677 	/*
5678 	 * Set ipif_mtu which is used to set the IRE's
5679 	 * ire_max_frag value. The driver could have sent
5680 	 * a different mtu from what it sent last time. No
5681 	 * need to call ipif_mtu_change because IREs have
5682 	 * not yet been created.
5683 	 */
5684 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5685 	/*
5686 	 * Clear all the flags that were set based on ill_bcast_addr_length
5687 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5688 	 * changed now and we need to re-evaluate.
5689 	 */
5690 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5691 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5692 
5693 	/*
5694 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5695 	 * changed now.
5696 	 */
5697 	if (ill->ill_bcast_addr_length == 0) {
5698 		if (ill->ill_resolver_mp != NULL)
5699 			freemsg(ill->ill_resolver_mp);
5700 		if (ill->ill_bcast_mp != NULL)
5701 			freemsg(ill->ill_bcast_mp);
5702 		if (ill->ill_flags & ILLF_XRESOLV)
5703 			ill->ill_net_type = IRE_IF_RESOLVER;
5704 		else
5705 			ill->ill_net_type = IRE_IF_NORESOLVER;
5706 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5707 		    ill->ill_phys_addr_length,
5708 		    ill->ill_sap,
5709 		    ill->ill_sap_length);
5710 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5711 
5712 		if (ill->ill_isv6)
5713 			/*
5714 			 * Note: xresolv interfaces will eventually need NOARP
5715 			 * set here as well, but that will require those
5716 			 * external resolvers to have some knowledge of
5717 			 * that flag and act appropriately. Not to be changed
5718 			 * at present.
5719 			 */
5720 			ill->ill_flags |= ILLF_NONUD;
5721 		else
5722 			ill->ill_flags |= ILLF_NOARP;
5723 
5724 		if (ill->ill_phys_addr_length == 0) {
5725 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5726 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5727 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5728 			} else {
5729 				/* pt-pt supports multicast. */
5730 				ill->ill_flags |= ILLF_MULTICAST;
5731 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5732 			}
5733 		}
5734 	} else {
5735 		ill->ill_net_type = IRE_IF_RESOLVER;
5736 		if (ill->ill_bcast_mp != NULL)
5737 			freemsg(ill->ill_bcast_mp);
5738 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5739 		    ill->ill_bcast_addr_length, ill->ill_sap,
5740 		    ill->ill_sap_length);
5741 		/*
5742 		 * Later detect lack of DLPI driver multicast
5743 		 * capability by catching DL_ENABMULTI errors in
5744 		 * ip_rput_dlpi.
5745 		 */
5746 		ill->ill_flags |= ILLF_MULTICAST;
5747 		if (!ill->ill_isv6)
5748 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5749 	}
5750 	/* By default an interface does not support any CoS marking */
5751 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5752 
5753 	/*
5754 	 * If we get QoS information in DL_INFO_ACK, the device supports
5755 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5756 	 */
5757 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5758 	    dlia->dl_qos_length);
5759 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5760 		ill->ill_flags |= ILLF_COS_ENABLED;
5761 	}
5762 
5763 	/* Clear any previous error indication. */
5764 	ill->ill_error = 0;
5765 	freemsg(mp);
5766 }
5767 
5768 /*
5769  * Perform various checks to verify that an address would make sense as a
5770  * local, remote, or subnet interface address.
5771  */
5772 static boolean_t
5773 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5774 {
5775 	ipaddr_t	net_mask;
5776 
5777 	/*
5778 	 * Don't allow all zeroes, all ones or experimental address, but allow
5779 	 * all ones netmask.
5780 	 */
5781 	if ((net_mask = ip_net_mask(addr)) == 0)
5782 		return (B_FALSE);
5783 	/* A given netmask overrides the "guess" netmask */
5784 	if (subnet_mask != 0)
5785 		net_mask = subnet_mask;
5786 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5787 	    (addr == (addr | ~net_mask)))) {
5788 		return (B_FALSE);
5789 	}
5790 	if (CLASSD(addr))
5791 		return (B_FALSE);
5792 
5793 	return (B_TRUE);
5794 }
5795 
5796 /*
5797  * ipif_lookup_group
5798  * Returns held ipif
5799  */
5800 ipif_t *
5801 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid)
5802 {
5803 	ire_t	*ire;
5804 	ipif_t	*ipif;
5805 
5806 	ire = ire_lookup_multi(group, zoneid);
5807 	if (ire == NULL)
5808 		return (NULL);
5809 	ipif = ire->ire_ipif;
5810 	ipif_refhold(ipif);
5811 	ire_refrele(ire);
5812 	return (ipif);
5813 }
5814 
5815 /*
5816  * Look for an ipif with the specified interface address and destination.
5817  * The destination address is used only for matching point-to-point interfaces.
5818  */
5819 ipif_t *
5820 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5821     ipsq_func_t func, int *error)
5822 {
5823 	ipif_t	*ipif;
5824 	ill_t	*ill;
5825 	ill_walk_context_t ctx;
5826 	ipsq_t	*ipsq;
5827 
5828 	if (error != NULL)
5829 		*error = 0;
5830 
5831 	/*
5832 	 * First match all the point-to-point interfaces
5833 	 * before looking at non-point-to-point interfaces.
5834 	 * This is done to avoid returning non-point-to-point
5835 	 * ipif instead of unnumbered point-to-point ipif.
5836 	 */
5837 	rw_enter(&ill_g_lock, RW_READER);
5838 	ill = ILL_START_WALK_V4(&ctx);
5839 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5840 		GRAB_CONN_LOCK(q);
5841 		mutex_enter(&ill->ill_lock);
5842 		for (ipif = ill->ill_ipif; ipif != NULL;
5843 		    ipif = ipif->ipif_next) {
5844 			/* Allow the ipif to be down */
5845 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5846 			    (ipif->ipif_lcl_addr == if_addr) &&
5847 			    (ipif->ipif_pp_dst_addr == dst)) {
5848 				/*
5849 				 * The block comment at the start of ipif_down
5850 				 * explains the use of the macros used below
5851 				 */
5852 				if (IPIF_CAN_LOOKUP(ipif)) {
5853 					ipif_refhold_locked(ipif);
5854 					mutex_exit(&ill->ill_lock);
5855 					RELEASE_CONN_LOCK(q);
5856 					rw_exit(&ill_g_lock);
5857 					return (ipif);
5858 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5859 					ipsq = ill->ill_phyint->phyint_ipsq;
5860 					mutex_enter(&ipsq->ipsq_lock);
5861 					mutex_exit(&ill->ill_lock);
5862 					rw_exit(&ill_g_lock);
5863 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5864 						ill);
5865 					mutex_exit(&ipsq->ipsq_lock);
5866 					RELEASE_CONN_LOCK(q);
5867 					*error = EINPROGRESS;
5868 					return (NULL);
5869 				}
5870 			}
5871 		}
5872 		mutex_exit(&ill->ill_lock);
5873 		RELEASE_CONN_LOCK(q);
5874 	}
5875 	rw_exit(&ill_g_lock);
5876 
5877 	/* lookup the ipif based on interface address */
5878 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error);
5879 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5880 	return (ipif);
5881 }
5882 
5883 /*
5884  * Look for an ipif with the specified address. For point-point links
5885  * we look for matches on either the destination address and the local
5886  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5887  * is set.
5888  * Matches on a specific ill if match_ill is set.
5889  */
5890 ipif_t *
5891 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5892     mblk_t *mp, ipsq_func_t func, int *error)
5893 {
5894 	ipif_t  *ipif;
5895 	ill_t   *ill;
5896 	boolean_t ptp = B_FALSE;
5897 	ipsq_t	*ipsq;
5898 	ill_walk_context_t	ctx;
5899 
5900 	if (error != NULL)
5901 		*error = 0;
5902 
5903 	rw_enter(&ill_g_lock, RW_READER);
5904 	/*
5905 	 * Repeat twice, first based on local addresses and
5906 	 * next time for pointopoint.
5907 	 */
5908 repeat:
5909 	ill = ILL_START_WALK_V4(&ctx);
5910 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5911 		if (match_ill != NULL && ill != match_ill) {
5912 			continue;
5913 		}
5914 		GRAB_CONN_LOCK(q);
5915 		mutex_enter(&ill->ill_lock);
5916 		for (ipif = ill->ill_ipif; ipif != NULL;
5917 		    ipif = ipif->ipif_next) {
5918 			if (zoneid != ALL_ZONES &&
5919 			    zoneid != ipif->ipif_zoneid &&
5920 			    ipif->ipif_zoneid != ALL_ZONES)
5921 				continue;
5922 			/* Allow the ipif to be down */
5923 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5924 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5925 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5926 			    (ipif->ipif_pp_dst_addr == addr))) {
5927 				/*
5928 				 * The block comment at the start of ipif_down
5929 				 * explains the use of the macros used below
5930 				 */
5931 				if (IPIF_CAN_LOOKUP(ipif)) {
5932 					ipif_refhold_locked(ipif);
5933 					mutex_exit(&ill->ill_lock);
5934 					RELEASE_CONN_LOCK(q);
5935 					rw_exit(&ill_g_lock);
5936 					return (ipif);
5937 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5938 					ipsq = ill->ill_phyint->phyint_ipsq;
5939 					mutex_enter(&ipsq->ipsq_lock);
5940 					mutex_exit(&ill->ill_lock);
5941 					rw_exit(&ill_g_lock);
5942 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5943 						ill);
5944 					mutex_exit(&ipsq->ipsq_lock);
5945 					RELEASE_CONN_LOCK(q);
5946 					*error = EINPROGRESS;
5947 					return (NULL);
5948 				}
5949 			}
5950 		}
5951 		mutex_exit(&ill->ill_lock);
5952 		RELEASE_CONN_LOCK(q);
5953 	}
5954 
5955 	/* If we already did the ptp case, then we are done */
5956 	if (ptp) {
5957 		rw_exit(&ill_g_lock);
5958 		if (error != NULL)
5959 			*error = ENXIO;
5960 		return (NULL);
5961 	}
5962 	ptp = B_TRUE;
5963 	goto repeat;
5964 }
5965 
5966 /*
5967  * Look for an ipif with the specified address. For point-point links
5968  * we look for matches on either the destination address and the local
5969  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5970  * is set.
5971  * Matches on a specific ill if match_ill is set.
5972  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
5973  */
5974 zoneid_t
5975 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill)
5976 {
5977 	zoneid_t zoneid;
5978 	ipif_t  *ipif;
5979 	ill_t   *ill;
5980 	boolean_t ptp = B_FALSE;
5981 	ill_walk_context_t	ctx;
5982 
5983 	rw_enter(&ill_g_lock, RW_READER);
5984 	/*
5985 	 * Repeat twice, first based on local addresses and
5986 	 * next time for pointopoint.
5987 	 */
5988 repeat:
5989 	ill = ILL_START_WALK_V4(&ctx);
5990 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5991 		if (match_ill != NULL && ill != match_ill) {
5992 			continue;
5993 		}
5994 		mutex_enter(&ill->ill_lock);
5995 		for (ipif = ill->ill_ipif; ipif != NULL;
5996 		    ipif = ipif->ipif_next) {
5997 			/* Allow the ipif to be down */
5998 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5999 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6000 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6001 			    (ipif->ipif_pp_dst_addr == addr)) &&
6002 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6003 				zoneid = ipif->ipif_zoneid;
6004 				mutex_exit(&ill->ill_lock);
6005 				rw_exit(&ill_g_lock);
6006 				/*
6007 				 * If ipif_zoneid was ALL_ZONES then we have
6008 				 * a trusted extensions shared IP address.
6009 				 * In that case GLOBAL_ZONEID works to send.
6010 				 */
6011 				if (zoneid == ALL_ZONES)
6012 					zoneid = GLOBAL_ZONEID;
6013 				return (zoneid);
6014 			}
6015 		}
6016 		mutex_exit(&ill->ill_lock);
6017 	}
6018 
6019 	/* If we already did the ptp case, then we are done */
6020 	if (ptp) {
6021 		rw_exit(&ill_g_lock);
6022 		return (ALL_ZONES);
6023 	}
6024 	ptp = B_TRUE;
6025 	goto repeat;
6026 }
6027 
6028 /*
6029  * Look for an ipif that matches the specified remote address i.e. the
6030  * ipif that would receive the specified packet.
6031  * First look for directly connected interfaces and then do a recursive
6032  * IRE lookup and pick the first ipif corresponding to the source address in the
6033  * ire.
6034  * Returns: held ipif
6035  */
6036 ipif_t *
6037 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6038 {
6039 	ipif_t	*ipif;
6040 	ire_t	*ire;
6041 
6042 	ASSERT(!ill->ill_isv6);
6043 
6044 	/*
6045 	 * Someone could be changing this ipif currently or change it
6046 	 * after we return this. Thus  a few packets could use the old
6047 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6048 	 * will atomically be updated or cleaned up with the new value
6049 	 * Thus we don't need a lock to check the flags or other attrs below.
6050 	 */
6051 	mutex_enter(&ill->ill_lock);
6052 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6053 		if (!IPIF_CAN_LOOKUP(ipif))
6054 			continue;
6055 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6056 		    ipif->ipif_zoneid != ALL_ZONES)
6057 			continue;
6058 		/* Allow the ipif to be down */
6059 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6060 			if ((ipif->ipif_pp_dst_addr == addr) ||
6061 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6062 			    ipif->ipif_lcl_addr == addr)) {
6063 				ipif_refhold_locked(ipif);
6064 				mutex_exit(&ill->ill_lock);
6065 				return (ipif);
6066 			}
6067 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6068 			ipif_refhold_locked(ipif);
6069 			mutex_exit(&ill->ill_lock);
6070 			return (ipif);
6071 		}
6072 	}
6073 	mutex_exit(&ill->ill_lock);
6074 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6075 	    NULL, MATCH_IRE_RECURSIVE);
6076 	if (ire != NULL) {
6077 		/*
6078 		 * The callers of this function wants to know the
6079 		 * interface on which they have to send the replies
6080 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6081 		 * derived from different ills, we really don't care
6082 		 * what we return here.
6083 		 */
6084 		ipif = ire->ire_ipif;
6085 		if (ipif != NULL) {
6086 			ipif_refhold(ipif);
6087 			ire_refrele(ire);
6088 			return (ipif);
6089 		}
6090 		ire_refrele(ire);
6091 	}
6092 	/* Pick the first interface */
6093 	ipif = ipif_get_next_ipif(NULL, ill);
6094 	return (ipif);
6095 }
6096 
6097 /*
6098  * This func does not prevent refcnt from increasing. But if
6099  * the caller has taken steps to that effect, then this func
6100  * can be used to determine whether the ill has become quiescent
6101  */
6102 boolean_t
6103 ill_is_quiescent(ill_t *ill)
6104 {
6105 	ipif_t	*ipif;
6106 
6107 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6108 
6109 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6110 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6111 			return (B_FALSE);
6112 		}
6113 	}
6114 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
6115 	    ill->ill_nce_cnt != 0 || ill->ill_srcif_refcnt != 0 ||
6116 	    ill->ill_mrtun_refcnt != 0) {
6117 		return (B_FALSE);
6118 	}
6119 	return (B_TRUE);
6120 }
6121 
6122 /*
6123  * This func does not prevent refcnt from increasing. But if
6124  * the caller has taken steps to that effect, then this func
6125  * can be used to determine whether the ipif has become quiescent
6126  */
6127 static boolean_t
6128 ipif_is_quiescent(ipif_t *ipif)
6129 {
6130 	ill_t *ill;
6131 
6132 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6133 
6134 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6135 		return (B_FALSE);
6136 	}
6137 
6138 	ill = ipif->ipif_ill;
6139 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6140 	    ill->ill_logical_down) {
6141 		return (B_TRUE);
6142 	}
6143 
6144 	/* This is the last ipif going down or being deleted on this ill */
6145 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
6146 		return (B_FALSE);
6147 	}
6148 
6149 	return (B_TRUE);
6150 }
6151 
6152 /*
6153  * This func does not prevent refcnt from increasing. But if
6154  * the caller has taken steps to that effect, then this func
6155  * can be used to determine whether the ipifs marked with IPIF_MOVING
6156  * have become quiescent and can be moved in a failover/failback.
6157  */
6158 static ipif_t *
6159 ill_quiescent_to_move(ill_t *ill)
6160 {
6161 	ipif_t  *ipif;
6162 
6163 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6164 
6165 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6166 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6167 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6168 				return (ipif);
6169 			}
6170 		}
6171 	}
6172 	return (NULL);
6173 }
6174 
6175 /*
6176  * The ipif/ill/ire has been refreled. Do the tail processing.
6177  * Determine if the ipif or ill in question has become quiescent and if so
6178  * wakeup close and/or restart any queued pending ioctl that is waiting
6179  * for the ipif_down (or ill_down)
6180  */
6181 void
6182 ipif_ill_refrele_tail(ill_t *ill)
6183 {
6184 	mblk_t	*mp;
6185 	conn_t	*connp;
6186 	ipsq_t	*ipsq;
6187 	ipif_t	*ipif;
6188 	dl_notify_ind_t *dlindp;
6189 
6190 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6191 
6192 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6193 	    ill_is_quiescent(ill)) {
6194 		/* ill_close may be waiting */
6195 		cv_broadcast(&ill->ill_cv);
6196 	}
6197 
6198 	/* ipsq can't change because ill_lock  is held */
6199 	ipsq = ill->ill_phyint->phyint_ipsq;
6200 	if (ipsq->ipsq_waitfor == 0) {
6201 		/* Not waiting for anything, just return. */
6202 		mutex_exit(&ill->ill_lock);
6203 		return;
6204 	}
6205 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6206 		ipsq->ipsq_pending_ipif != NULL);
6207 	/*
6208 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6209 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6210 	 * be zero for restarting an ioctl that ends up downing the ill.
6211 	 */
6212 	ipif = ipsq->ipsq_pending_ipif;
6213 	if (ipif->ipif_ill != ill) {
6214 		/* The ioctl is pending on some other ill. */
6215 		mutex_exit(&ill->ill_lock);
6216 		return;
6217 	}
6218 
6219 	switch (ipsq->ipsq_waitfor) {
6220 	case IPIF_DOWN:
6221 	case IPIF_FREE:
6222 		if (!ipif_is_quiescent(ipif)) {
6223 			mutex_exit(&ill->ill_lock);
6224 			return;
6225 		}
6226 		break;
6227 
6228 	case ILL_DOWN:
6229 	case ILL_FREE:
6230 		/*
6231 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6232 		 * waits synchronously in ip_close, and no message is queued in
6233 		 * ipsq_pending_mp at all in this case
6234 		 */
6235 		if (!ill_is_quiescent(ill)) {
6236 			mutex_exit(&ill->ill_lock);
6237 			return;
6238 		}
6239 
6240 		break;
6241 
6242 	case ILL_MOVE_OK:
6243 		if (ill_quiescent_to_move(ill) != NULL) {
6244 			mutex_exit(&ill->ill_lock);
6245 			return;
6246 		}
6247 
6248 		break;
6249 	default:
6250 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6251 		    (void *)ipsq, ipsq->ipsq_waitfor);
6252 	}
6253 
6254 	/*
6255 	 * Incr refcnt for the qwriter_ip call below which
6256 	 * does a refrele
6257 	 */
6258 	ill_refhold_locked(ill);
6259 	mutex_exit(&ill->ill_lock);
6260 
6261 	mp = ipsq_pending_mp_get(ipsq, &connp);
6262 	ASSERT(mp != NULL);
6263 
6264 	switch (mp->b_datap->db_type) {
6265 	case M_PCPROTO:
6266 	case M_PROTO:
6267 		/*
6268 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6269 		 */
6270 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6271 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6272 
6273 		switch (dlindp->dl_notification) {
6274 		case DL_NOTE_PHYS_ADDR:
6275 			qwriter_ip(NULL, ill, ill->ill_rq, mp,
6276 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6277 			return;
6278 		default:
6279 			ASSERT(0);
6280 		}
6281 		break;
6282 
6283 	case M_ERROR:
6284 	case M_HANGUP:
6285 		qwriter_ip(NULL, ill, ill->ill_rq, mp, ipif_all_down_tail,
6286 		    CUR_OP, B_TRUE);
6287 		return;
6288 
6289 	case M_IOCTL:
6290 	case M_IOCDATA:
6291 		qwriter_ip(NULL, ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6292 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6293 		return;
6294 
6295 	default:
6296 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6297 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6298 	}
6299 }
6300 
6301 #ifdef ILL_DEBUG
6302 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6303 void
6304 th_trace_rrecord(th_trace_t *th_trace)
6305 {
6306 	tr_buf_t *tr_buf;
6307 	uint_t lastref;
6308 
6309 	lastref = th_trace->th_trace_lastref;
6310 	lastref++;
6311 	if (lastref == TR_BUF_MAX)
6312 		lastref = 0;
6313 	th_trace->th_trace_lastref = lastref;
6314 	tr_buf = &th_trace->th_trbuf[lastref];
6315 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, IP_STACK_DEPTH);
6316 }
6317 
6318 th_trace_t *
6319 th_trace_ipif_lookup(ipif_t *ipif)
6320 {
6321 	int bucket_id;
6322 	th_trace_t *th_trace;
6323 
6324 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6325 
6326 	bucket_id = IP_TR_HASH(curthread);
6327 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6328 
6329 	for (th_trace = ipif->ipif_trace[bucket_id]; th_trace != NULL;
6330 	    th_trace = th_trace->th_next) {
6331 		if (th_trace->th_id == curthread)
6332 			return (th_trace);
6333 	}
6334 	return (NULL);
6335 }
6336 
6337 void
6338 ipif_trace_ref(ipif_t *ipif)
6339 {
6340 	int bucket_id;
6341 	th_trace_t *th_trace;
6342 
6343 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6344 
6345 	if (ipif->ipif_trace_disable)
6346 		return;
6347 
6348 	/*
6349 	 * Attempt to locate the trace buffer for the curthread.
6350 	 * If it does not exist, then allocate a new trace buffer
6351 	 * and link it in list of trace bufs for this ipif, at the head
6352 	 */
6353 	th_trace = th_trace_ipif_lookup(ipif);
6354 	if (th_trace == NULL) {
6355 		bucket_id = IP_TR_HASH(curthread);
6356 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6357 		    KM_NOSLEEP);
6358 		if (th_trace == NULL) {
6359 			ipif->ipif_trace_disable = B_TRUE;
6360 			ipif_trace_cleanup(ipif);
6361 			return;
6362 		}
6363 		th_trace->th_id = curthread;
6364 		th_trace->th_next = ipif->ipif_trace[bucket_id];
6365 		th_trace->th_prev = &ipif->ipif_trace[bucket_id];
6366 		if (th_trace->th_next != NULL)
6367 			th_trace->th_next->th_prev = &th_trace->th_next;
6368 		ipif->ipif_trace[bucket_id] = th_trace;
6369 	}
6370 	ASSERT(th_trace->th_refcnt >= 0 &&
6371 		th_trace->th_refcnt < TR_BUF_MAX -1);
6372 	th_trace->th_refcnt++;
6373 	th_trace_rrecord(th_trace);
6374 }
6375 
6376 void
6377 ipif_untrace_ref(ipif_t *ipif)
6378 {
6379 	th_trace_t *th_trace;
6380 
6381 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6382 
6383 	if (ipif->ipif_trace_disable)
6384 		return;
6385 	th_trace = th_trace_ipif_lookup(ipif);
6386 	ASSERT(th_trace != NULL);
6387 	ASSERT(th_trace->th_refcnt > 0);
6388 
6389 	th_trace->th_refcnt--;
6390 	th_trace_rrecord(th_trace);
6391 }
6392 
6393 th_trace_t *
6394 th_trace_ill_lookup(ill_t *ill)
6395 {
6396 	th_trace_t *th_trace;
6397 	int bucket_id;
6398 
6399 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6400 
6401 	bucket_id = IP_TR_HASH(curthread);
6402 	ASSERT(bucket_id < IP_TR_HASH_MAX);
6403 
6404 	for (th_trace = ill->ill_trace[bucket_id]; th_trace != NULL;
6405 	    th_trace = th_trace->th_next) {
6406 		if (th_trace->th_id == curthread)
6407 			return (th_trace);
6408 	}
6409 	return (NULL);
6410 }
6411 
6412 void
6413 ill_trace_ref(ill_t *ill)
6414 {
6415 	int bucket_id;
6416 	th_trace_t *th_trace;
6417 
6418 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6419 	if (ill->ill_trace_disable)
6420 		return;
6421 	/*
6422 	 * Attempt to locate the trace buffer for the curthread.
6423 	 * If it does not exist, then allocate a new trace buffer
6424 	 * and link it in list of trace bufs for this ill, at the head
6425 	 */
6426 	th_trace = th_trace_ill_lookup(ill);
6427 	if (th_trace == NULL) {
6428 		bucket_id = IP_TR_HASH(curthread);
6429 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
6430 		    KM_NOSLEEP);
6431 		if (th_trace == NULL) {
6432 			ill->ill_trace_disable = B_TRUE;
6433 			ill_trace_cleanup(ill);
6434 			return;
6435 		}
6436 		th_trace->th_id = curthread;
6437 		th_trace->th_next = ill->ill_trace[bucket_id];
6438 		th_trace->th_prev = &ill->ill_trace[bucket_id];
6439 		if (th_trace->th_next != NULL)
6440 			th_trace->th_next->th_prev = &th_trace->th_next;
6441 		ill->ill_trace[bucket_id] = th_trace;
6442 	}
6443 	ASSERT(th_trace->th_refcnt >= 0 &&
6444 		th_trace->th_refcnt < TR_BUF_MAX - 1);
6445 
6446 	th_trace->th_refcnt++;
6447 	th_trace_rrecord(th_trace);
6448 }
6449 
6450 void
6451 ill_untrace_ref(ill_t *ill)
6452 {
6453 	th_trace_t *th_trace;
6454 
6455 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6456 
6457 	if (ill->ill_trace_disable)
6458 		return;
6459 	th_trace = th_trace_ill_lookup(ill);
6460 	ASSERT(th_trace != NULL);
6461 	ASSERT(th_trace->th_refcnt > 0);
6462 
6463 	th_trace->th_refcnt--;
6464 	th_trace_rrecord(th_trace);
6465 }
6466 
6467 /*
6468  * Verify that this thread has no refs to the ipif and free
6469  * the trace buffers
6470  */
6471 /* ARGSUSED */
6472 void
6473 ipif_thread_exit(ipif_t *ipif, void *dummy)
6474 {
6475 	th_trace_t *th_trace;
6476 
6477 	mutex_enter(&ipif->ipif_ill->ill_lock);
6478 
6479 	th_trace = th_trace_ipif_lookup(ipif);
6480 	if (th_trace == NULL) {
6481 		mutex_exit(&ipif->ipif_ill->ill_lock);
6482 		return;
6483 	}
6484 	ASSERT(th_trace->th_refcnt == 0);
6485 	/* unlink th_trace and free it */
6486 	*th_trace->th_prev = th_trace->th_next;
6487 	if (th_trace->th_next != NULL)
6488 		th_trace->th_next->th_prev = th_trace->th_prev;
6489 	th_trace->th_next = NULL;
6490 	th_trace->th_prev = NULL;
6491 	kmem_free(th_trace, sizeof (th_trace_t));
6492 
6493 	mutex_exit(&ipif->ipif_ill->ill_lock);
6494 }
6495 
6496 /*
6497  * Verify that this thread has no refs to the ill and free
6498  * the trace buffers
6499  */
6500 /* ARGSUSED */
6501 void
6502 ill_thread_exit(ill_t *ill, void *dummy)
6503 {
6504 	th_trace_t *th_trace;
6505 
6506 	mutex_enter(&ill->ill_lock);
6507 
6508 	th_trace = th_trace_ill_lookup(ill);
6509 	if (th_trace == NULL) {
6510 		mutex_exit(&ill->ill_lock);
6511 		return;
6512 	}
6513 	ASSERT(th_trace->th_refcnt == 0);
6514 	/* unlink th_trace and free it */
6515 	*th_trace->th_prev = th_trace->th_next;
6516 	if (th_trace->th_next != NULL)
6517 		th_trace->th_next->th_prev = th_trace->th_prev;
6518 	th_trace->th_next = NULL;
6519 	th_trace->th_prev = NULL;
6520 	kmem_free(th_trace, sizeof (th_trace_t));
6521 
6522 	mutex_exit(&ill->ill_lock);
6523 }
6524 #endif
6525 
6526 #ifdef ILL_DEBUG
6527 void
6528 ip_thread_exit(void)
6529 {
6530 	ill_t	*ill;
6531 	ipif_t	*ipif;
6532 	ill_walk_context_t	ctx;
6533 
6534 	rw_enter(&ill_g_lock, RW_READER);
6535 	ill = ILL_START_WALK_ALL(&ctx);
6536 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6537 		for (ipif = ill->ill_ipif; ipif != NULL;
6538 		    ipif = ipif->ipif_next) {
6539 			ipif_thread_exit(ipif, NULL);
6540 		}
6541 		ill_thread_exit(ill, NULL);
6542 	}
6543 	rw_exit(&ill_g_lock);
6544 
6545 	ire_walk(ire_thread_exit, NULL);
6546 	ndp_walk_common(&ndp4, NULL, nce_thread_exit, NULL, B_FALSE);
6547 	ndp_walk_common(&ndp6, NULL, nce_thread_exit, NULL, B_FALSE);
6548 }
6549 
6550 /*
6551  * Called when ipif is unplumbed or when memory alloc fails
6552  */
6553 void
6554 ipif_trace_cleanup(ipif_t *ipif)
6555 {
6556 	int	i;
6557 	th_trace_t	*th_trace;
6558 	th_trace_t	*th_trace_next;
6559 
6560 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6561 		for (th_trace = ipif->ipif_trace[i]; th_trace != NULL;
6562 		    th_trace = th_trace_next) {
6563 			th_trace_next = th_trace->th_next;
6564 			kmem_free(th_trace, sizeof (th_trace_t));
6565 		}
6566 		ipif->ipif_trace[i] = NULL;
6567 	}
6568 }
6569 
6570 /*
6571  * Called when ill is unplumbed or when memory alloc fails
6572  */
6573 void
6574 ill_trace_cleanup(ill_t *ill)
6575 {
6576 	int	i;
6577 	th_trace_t	*th_trace;
6578 	th_trace_t	*th_trace_next;
6579 
6580 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6581 		for (th_trace = ill->ill_trace[i]; th_trace != NULL;
6582 		    th_trace = th_trace_next) {
6583 			th_trace_next = th_trace->th_next;
6584 			kmem_free(th_trace, sizeof (th_trace_t));
6585 		}
6586 		ill->ill_trace[i] = NULL;
6587 	}
6588 }
6589 
6590 #else
6591 void ip_thread_exit(void) {}
6592 #endif
6593 
6594 void
6595 ipif_refhold_locked(ipif_t *ipif)
6596 {
6597 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6598 	ipif->ipif_refcnt++;
6599 	IPIF_TRACE_REF(ipif);
6600 }
6601 
6602 void
6603 ipif_refhold(ipif_t *ipif)
6604 {
6605 	ill_t	*ill;
6606 
6607 	ill = ipif->ipif_ill;
6608 	mutex_enter(&ill->ill_lock);
6609 	ipif->ipif_refcnt++;
6610 	IPIF_TRACE_REF(ipif);
6611 	mutex_exit(&ill->ill_lock);
6612 }
6613 
6614 /*
6615  * Must not be called while holding any locks. Otherwise if this is
6616  * the last reference to be released there is a chance of recursive mutex
6617  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6618  * to restart an ioctl.
6619  */
6620 void
6621 ipif_refrele(ipif_t *ipif)
6622 {
6623 	ill_t	*ill;
6624 
6625 	ill = ipif->ipif_ill;
6626 
6627 	mutex_enter(&ill->ill_lock);
6628 	ASSERT(ipif->ipif_refcnt != 0);
6629 	ipif->ipif_refcnt--;
6630 	IPIF_UNTRACE_REF(ipif);
6631 	if (ipif->ipif_refcnt != 0) {
6632 		mutex_exit(&ill->ill_lock);
6633 		return;
6634 	}
6635 
6636 	/* Drops the ill_lock */
6637 	ipif_ill_refrele_tail(ill);
6638 }
6639 
6640 ipif_t *
6641 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6642 {
6643 	ipif_t	*ipif;
6644 
6645 	mutex_enter(&ill->ill_lock);
6646 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6647 	    ipif != NULL; ipif = ipif->ipif_next) {
6648 		if (!IPIF_CAN_LOOKUP(ipif))
6649 			continue;
6650 		ipif_refhold_locked(ipif);
6651 		mutex_exit(&ill->ill_lock);
6652 		return (ipif);
6653 	}
6654 	mutex_exit(&ill->ill_lock);
6655 	return (NULL);
6656 }
6657 
6658 /*
6659  * TODO: make this table extendible at run time
6660  * Return a pointer to the mac type info for 'mac_type'
6661  */
6662 static ip_m_t *
6663 ip_m_lookup(t_uscalar_t mac_type)
6664 {
6665 	ip_m_t	*ipm;
6666 
6667 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6668 		if (ipm->ip_m_mac_type == mac_type)
6669 			return (ipm);
6670 	return (NULL);
6671 }
6672 
6673 /*
6674  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6675  * ipif_arg is passed in to associate it with the correct interface.
6676  * We may need to restart this operation if the ipif cannot be looked up
6677  * due to an exclusive operation that is currently in progress. The restart
6678  * entry point is specified by 'func'
6679  */
6680 int
6681 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6682     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6683     ire_t **ire_arg, boolean_t ioctl_msg, queue_t *q, mblk_t *mp,
6684     ipsq_func_t func, struct rtsa_s *sp)
6685 {
6686 	ire_t	*ire;
6687 	ire_t	*gw_ire = NULL;
6688 	ipif_t	*ipif = NULL;
6689 	boolean_t ipif_refheld = B_FALSE;
6690 	uint_t	type;
6691 	int	match_flags = MATCH_IRE_TYPE;
6692 	int	error;
6693 	tsol_gc_t *gc = NULL;
6694 	tsol_gcgrp_t *gcgrp = NULL;
6695 	boolean_t gcgrp_xtraref = B_FALSE;
6696 
6697 	ip1dbg(("ip_rt_add:"));
6698 
6699 	if (ire_arg != NULL)
6700 		*ire_arg = NULL;
6701 
6702 	/*
6703 	 * If this is the case of RTF_HOST being set, then we set the netmask
6704 	 * to all ones (regardless if one was supplied).
6705 	 */
6706 	if (flags & RTF_HOST)
6707 		mask = IP_HOST_MASK;
6708 
6709 	/*
6710 	 * Prevent routes with a zero gateway from being created (since
6711 	 * interfaces can currently be plumbed and brought up no assigned
6712 	 * address).
6713 	 * For routes with RTA_SRCIFP, the gateway address can be 0.0.0.0.
6714 	 */
6715 	if (gw_addr == 0 && src_ipif == NULL)
6716 		return (ENETUNREACH);
6717 	/*
6718 	 * Get the ipif, if any, corresponding to the gw_addr
6719 	 */
6720 	if (gw_addr != 0) {
6721 		ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func,
6722 		    &error);
6723 		if (ipif != NULL) {
6724 			if (IS_VNI(ipif->ipif_ill)) {
6725 				ipif_refrele(ipif);
6726 				return (EINVAL);
6727 			}
6728 			ipif_refheld = B_TRUE;
6729 		} else if (error == EINPROGRESS) {
6730 			ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6731 			return (EINPROGRESS);
6732 		} else {
6733 			error = 0;
6734 		}
6735 	}
6736 
6737 	if (ipif != NULL) {
6738 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6739 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6740 	} else {
6741 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6742 	}
6743 
6744 	/*
6745 	 * GateD will attempt to create routes with a loopback interface
6746 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6747 	 * these routes to be added, but create them as interface routes
6748 	 * since the gateway is an interface address.
6749 	 */
6750 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6751 		flags &= ~RTF_GATEWAY;
6752 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6753 		    mask == IP_HOST_MASK) {
6754 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6755 			    ALL_ZONES, NULL, match_flags);
6756 			if (ire != NULL) {
6757 				ire_refrele(ire);
6758 				if (ipif_refheld)
6759 					ipif_refrele(ipif);
6760 				return (EEXIST);
6761 			}
6762 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6763 			    "for 0x%x\n", (void *)ipif,
6764 			    ipif->ipif_ire_type,
6765 			    ntohl(ipif->ipif_lcl_addr)));
6766 			ire = ire_create(
6767 			    (uchar_t *)&dst_addr,	/* dest address */
6768 			    (uchar_t *)&mask,		/* mask */
6769 			    (uchar_t *)&ipif->ipif_src_addr,
6770 			    NULL,			/* no gateway */
6771 			    NULL,
6772 			    &ipif->ipif_mtu,
6773 			    NULL,
6774 			    ipif->ipif_rq,		/* recv-from queue */
6775 			    NULL,			/* no send-to queue */
6776 			    ipif->ipif_ire_type,	/* LOOPBACK */
6777 			    NULL,
6778 			    ipif,
6779 			    NULL,
6780 			    0,
6781 			    0,
6782 			    0,
6783 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6784 			    RTF_PRIVATE : 0,
6785 			    &ire_uinfo_null,
6786 			    NULL,
6787 			    NULL);
6788 
6789 			if (ire == NULL) {
6790 				if (ipif_refheld)
6791 					ipif_refrele(ipif);
6792 				return (ENOMEM);
6793 			}
6794 			error = ire_add(&ire, q, mp, func, B_FALSE);
6795 			if (error == 0)
6796 				goto save_ire;
6797 			if (ipif_refheld)
6798 				ipif_refrele(ipif);
6799 			return (error);
6800 
6801 		}
6802 	}
6803 
6804 	/*
6805 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6806 	 * and the gateway address provided is one of the system's interface
6807 	 * addresses.  By using the routing socket interface and supplying an
6808 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6809 	 * specifying an interface route to be created is available which uses
6810 	 * the interface index that specifies the outgoing interface rather than
6811 	 * the address of an outgoing interface (which may not be able to
6812 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6813 	 * flag, routes can be specified which not only specify the next-hop to
6814 	 * be used when routing to a certain prefix, but also which outgoing
6815 	 * interface should be used.
6816 	 *
6817 	 * Previously, interfaces would have unique addresses assigned to them
6818 	 * and so the address assigned to a particular interface could be used
6819 	 * to identify a particular interface.  One exception to this was the
6820 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6821 	 *
6822 	 * With the advent of IPv6 and its link-local addresses, this
6823 	 * restriction was relaxed and interfaces could share addresses between
6824 	 * themselves.  In fact, typically all of the link-local interfaces on
6825 	 * an IPv6 node or router will have the same link-local address.  In
6826 	 * order to differentiate between these interfaces, the use of an
6827 	 * interface index is necessary and this index can be carried inside a
6828 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6829 	 * of using the interface index, however, is that all of the ipif's that
6830 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6831 	 * cannot be used to differentiate between ipif's (or logical
6832 	 * interfaces) that belong to the same ill (physical interface).
6833 	 *
6834 	 * For example, in the following case involving IPv4 interfaces and
6835 	 * logical interfaces
6836 	 *
6837 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6838 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6839 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6840 	 *
6841 	 * the ipif's corresponding to each of these interface routes can be
6842 	 * uniquely identified by the "gateway" (actually interface address).
6843 	 *
6844 	 * In this case involving multiple IPv6 default routes to a particular
6845 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6846 	 * default route is of interest:
6847 	 *
6848 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6849 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6850 	 */
6851 
6852 	/* RTF_GATEWAY not set */
6853 	if (!(flags & RTF_GATEWAY)) {
6854 		queue_t	*stq;
6855 		queue_t	*rfq = NULL;
6856 		ill_t	*in_ill = NULL;
6857 
6858 		if (sp != NULL) {
6859 			ip2dbg(("ip_rt_add: gateway security attributes "
6860 			    "cannot be set with interface route\n"));
6861 			if (ipif_refheld)
6862 				ipif_refrele(ipif);
6863 			return (EINVAL);
6864 		}
6865 
6866 		/*
6867 		 * As the interface index specified with the RTA_IFP sockaddr is
6868 		 * the same for all ipif's off of an ill, the matching logic
6869 		 * below uses MATCH_IRE_ILL if such an index was specified.
6870 		 * This means that routes sharing the same prefix when added
6871 		 * using a RTA_IFP sockaddr must have distinct interface
6872 		 * indices (namely, they must be on distinct ill's).
6873 		 *
6874 		 * On the other hand, since the gateway address will usually be
6875 		 * different for each ipif on the system, the matching logic
6876 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6877 		 * route.  This means that interface routes for the same prefix
6878 		 * can be created if they belong to distinct ipif's and if a
6879 		 * RTA_IFP sockaddr is not present.
6880 		 */
6881 		if (ipif_arg != NULL) {
6882 			if (ipif_refheld)  {
6883 				ipif_refrele(ipif);
6884 				ipif_refheld = B_FALSE;
6885 			}
6886 			ipif = ipif_arg;
6887 			match_flags |= MATCH_IRE_ILL;
6888 		} else {
6889 			/*
6890 			 * Check the ipif corresponding to the gw_addr
6891 			 */
6892 			if (ipif == NULL)
6893 				return (ENETUNREACH);
6894 			match_flags |= MATCH_IRE_IPIF;
6895 		}
6896 		ASSERT(ipif != NULL);
6897 		/*
6898 		 * If src_ipif is not NULL, we have to create
6899 		 * an ire with non-null ire_in_ill value
6900 		 */
6901 		if (src_ipif != NULL) {
6902 			in_ill = src_ipif->ipif_ill;
6903 		}
6904 
6905 		/*
6906 		 * We check for an existing entry at this point.
6907 		 *
6908 		 * Since a netmask isn't passed in via the ioctl interface
6909 		 * (SIOCADDRT), we don't check for a matching netmask in that
6910 		 * case.
6911 		 */
6912 		if (!ioctl_msg)
6913 			match_flags |= MATCH_IRE_MASK;
6914 		if (src_ipif != NULL) {
6915 			/* Look up in the special table */
6916 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
6917 			    ipif, src_ipif->ipif_ill, match_flags);
6918 		} else {
6919 			ire = ire_ftable_lookup(dst_addr, mask, 0,
6920 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
6921 			    NULL, match_flags);
6922 		}
6923 		if (ire != NULL) {
6924 			ire_refrele(ire);
6925 			if (ipif_refheld)
6926 				ipif_refrele(ipif);
6927 			return (EEXIST);
6928 		}
6929 
6930 		if (src_ipif != NULL) {
6931 			/*
6932 			 * Create the special ire for the IRE table
6933 			 * which hangs out of ire_in_ill. This ire
6934 			 * is in-between IRE_CACHE and IRE_INTERFACE.
6935 			 * Thus rfq is non-NULL.
6936 			 */
6937 			rfq = ipif->ipif_rq;
6938 		}
6939 		/* Create the usual interface ires */
6940 
6941 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6942 		    ? ipif->ipif_rq : ipif->ipif_wq;
6943 
6944 		/*
6945 		 * Create a copy of the IRE_LOOPBACK,
6946 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6947 		 * the modified address and netmask.
6948 		 */
6949 		ire = ire_create(
6950 		    (uchar_t *)&dst_addr,
6951 		    (uint8_t *)&mask,
6952 		    (uint8_t *)&ipif->ipif_src_addr,
6953 		    NULL,
6954 		    NULL,
6955 		    &ipif->ipif_mtu,
6956 		    NULL,
6957 		    rfq,
6958 		    stq,
6959 		    ipif->ipif_net_type,
6960 		    ipif->ipif_resolver_mp,
6961 		    ipif,
6962 		    in_ill,
6963 		    0,
6964 		    0,
6965 		    0,
6966 		    flags,
6967 		    &ire_uinfo_null,
6968 		    NULL,
6969 		    NULL);
6970 		if (ire == NULL) {
6971 			if (ipif_refheld)
6972 				ipif_refrele(ipif);
6973 			return (ENOMEM);
6974 		}
6975 
6976 		/*
6977 		 * Some software (for example, GateD and Sun Cluster) attempts
6978 		 * to create (what amount to) IRE_PREFIX routes with the
6979 		 * loopback address as the gateway.  This is primarily done to
6980 		 * set up prefixes with the RTF_REJECT flag set (for example,
6981 		 * when generating aggregate routes.)
6982 		 *
6983 		 * If the IRE type (as defined by ipif->ipif_net_type) is
6984 		 * IRE_LOOPBACK, then we map the request into a
6985 		 * IRE_IF_NORESOLVER.
6986 		 *
6987 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
6988 		 * routine, but rather using ire_create() directly.
6989 		 *
6990 		 */
6991 		if (ipif->ipif_net_type == IRE_LOOPBACK)
6992 			ire->ire_type = IRE_IF_NORESOLVER;
6993 
6994 		error = ire_add(&ire, q, mp, func, B_FALSE);
6995 		if (error == 0)
6996 			goto save_ire;
6997 
6998 		/*
6999 		 * In the result of failure, ire_add() will have already
7000 		 * deleted the ire in question, so there is no need to
7001 		 * do that here.
7002 		 */
7003 		if (ipif_refheld)
7004 			ipif_refrele(ipif);
7005 		return (error);
7006 	}
7007 	if (ipif_refheld) {
7008 		ipif_refrele(ipif);
7009 		ipif_refheld = B_FALSE;
7010 	}
7011 
7012 	if (src_ipif != NULL) {
7013 		/* RTA_SRCIFP is not supported on RTF_GATEWAY */
7014 		ip2dbg(("ip_rt_add: SRCIF cannot be set with gateway route\n"));
7015 		return (EINVAL);
7016 	}
7017 	/*
7018 	 * Get an interface IRE for the specified gateway.
7019 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7020 	 * gateway, it is currently unreachable and we fail the request
7021 	 * accordingly.
7022 	 */
7023 	ipif = ipif_arg;
7024 	if (ipif_arg != NULL)
7025 		match_flags |= MATCH_IRE_ILL;
7026 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7027 	    ALL_ZONES, 0, NULL, match_flags);
7028 	if (gw_ire == NULL)
7029 		return (ENETUNREACH);
7030 
7031 	/*
7032 	 * We create one of three types of IREs as a result of this request
7033 	 * based on the netmask.  A netmask of all ones (which is automatically
7034 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7035 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7036 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7037 	 * destination prefix.
7038 	 */
7039 	if (mask == IP_HOST_MASK)
7040 		type = IRE_HOST;
7041 	else if (mask == 0)
7042 		type = IRE_DEFAULT;
7043 	else
7044 		type = IRE_PREFIX;
7045 
7046 	/* check for a duplicate entry */
7047 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7048 	    NULL, ALL_ZONES, 0, NULL,
7049 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW);
7050 	if (ire != NULL) {
7051 		ire_refrele(gw_ire);
7052 		ire_refrele(ire);
7053 		return (EEXIST);
7054 	}
7055 
7056 	/* Security attribute exists */
7057 	if (sp != NULL) {
7058 		tsol_gcgrp_addr_t ga;
7059 
7060 		/* find or create the gateway credentials group */
7061 		ga.ga_af = AF_INET;
7062 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7063 
7064 		/* we hold reference to it upon success */
7065 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7066 		if (gcgrp == NULL) {
7067 			ire_refrele(gw_ire);
7068 			return (ENOMEM);
7069 		}
7070 
7071 		/*
7072 		 * Create and add the security attribute to the group; a
7073 		 * reference to the group is made upon allocating a new
7074 		 * entry successfully.  If it finds an already-existing
7075 		 * entry for the security attribute in the group, it simply
7076 		 * returns it and no new reference is made to the group.
7077 		 */
7078 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7079 		if (gc == NULL) {
7080 			/* release reference held by gcgrp_lookup */
7081 			GCGRP_REFRELE(gcgrp);
7082 			ire_refrele(gw_ire);
7083 			return (ENOMEM);
7084 		}
7085 	}
7086 
7087 	/* Create the IRE. */
7088 	ire = ire_create(
7089 	    (uchar_t *)&dst_addr,		/* dest address */
7090 	    (uchar_t *)&mask,			/* mask */
7091 	    /* src address assigned by the caller? */
7092 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7093 		(flags & RTF_SETSRC)) ?  &src_addr : NULL),
7094 	    (uchar_t *)&gw_addr,		/* gateway address */
7095 	    NULL,				/* no in-srcaddress */
7096 	    &gw_ire->ire_max_frag,
7097 	    NULL,				/* no Fast Path header */
7098 	    NULL,				/* no recv-from queue */
7099 	    NULL,				/* no send-to queue */
7100 	    (ushort_t)type,			/* IRE type */
7101 	    NULL,
7102 	    ipif_arg,
7103 	    NULL,
7104 	    0,
7105 	    0,
7106 	    0,
7107 	    flags,
7108 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7109 	    gc,					/* security attribute */
7110 	    NULL);
7111 	/*
7112 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7113 	 * reference to the 'gcgrp'. We can now release the extra reference
7114 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7115 	 */
7116 	if (gcgrp_xtraref)
7117 		GCGRP_REFRELE(gcgrp);
7118 	if (ire == NULL) {
7119 		if (gc != NULL)
7120 			GC_REFRELE(gc);
7121 		ire_refrele(gw_ire);
7122 		return (ENOMEM);
7123 	}
7124 
7125 	/*
7126 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7127 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7128 	 */
7129 
7130 	/* Add the new IRE. */
7131 	error = ire_add(&ire, q, mp, func, B_FALSE);
7132 	if (error != 0) {
7133 		/*
7134 		 * In the result of failure, ire_add() will have already
7135 		 * deleted the ire in question, so there is no need to
7136 		 * do that here.
7137 		 */
7138 		ire_refrele(gw_ire);
7139 		return (error);
7140 	}
7141 
7142 	if (flags & RTF_MULTIRT) {
7143 		/*
7144 		 * Invoke the CGTP (multirouting) filtering module
7145 		 * to add the dst address in the filtering database.
7146 		 * Replicated inbound packets coming from that address
7147 		 * will be filtered to discard the duplicates.
7148 		 * It is not necessary to call the CGTP filter hook
7149 		 * when the dst address is a broadcast or multicast,
7150 		 * because an IP source address cannot be a broadcast
7151 		 * or a multicast.
7152 		 */
7153 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7154 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
7155 		if (ire_dst != NULL) {
7156 			ip_cgtp_bcast_add(ire, ire_dst);
7157 			ire_refrele(ire_dst);
7158 			goto save_ire;
7159 		}
7160 		if ((ip_cgtp_filter_ops != NULL) && !CLASSD(ire->ire_addr)) {
7161 			int res = ip_cgtp_filter_ops->cfo_add_dest_v4(
7162 			    ire->ire_addr,
7163 			    ire->ire_gateway_addr,
7164 			    ire->ire_src_addr,
7165 			    gw_ire->ire_src_addr);
7166 			if (res != 0) {
7167 				ire_refrele(gw_ire);
7168 				ire_delete(ire);
7169 				return (res);
7170 			}
7171 		}
7172 	}
7173 
7174 	/*
7175 	 * Now that the prefix IRE entry has been created, delete any
7176 	 * existing gateway IRE cache entries as well as any IRE caches
7177 	 * using the gateway, and force them to be created through
7178 	 * ip_newroute.
7179 	 */
7180 	if (gc != NULL) {
7181 		ASSERT(gcgrp != NULL);
7182 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES);
7183 	}
7184 
7185 save_ire:
7186 	if (gw_ire != NULL) {
7187 		ire_refrele(gw_ire);
7188 	}
7189 	/*
7190 	 * We do not do save_ire for the routes added with RTA_SRCIFP
7191 	 * flag. This route is only added and deleted by mipagent.
7192 	 * So, for simplicity of design, we refrain from saving
7193 	 * ires that are created with srcif value. This may change
7194 	 * in future if we find more usage of srcifp feature.
7195 	 */
7196 	if (ipif != NULL && src_ipif == NULL) {
7197 		/*
7198 		 * Save enough information so that we can recreate the IRE if
7199 		 * the interface goes down and then up.  The metrics associated
7200 		 * with the route will be saved as well when rts_setmetrics() is
7201 		 * called after the IRE has been created.  In the case where
7202 		 * memory cannot be allocated, none of this information will be
7203 		 * saved.
7204 		 */
7205 		ipif_save_ire(ipif, ire);
7206 	}
7207 	if (ioctl_msg)
7208 		ip_rts_rtmsg(RTM_OLDADD, ire, 0);
7209 	if (ire_arg != NULL) {
7210 		/*
7211 		 * Store the ire that was successfully added into where ire_arg
7212 		 * points to so that callers don't have to look it up
7213 		 * themselves (but they are responsible for ire_refrele()ing
7214 		 * the ire when they are finished with it).
7215 		 */
7216 		*ire_arg = ire;
7217 	} else {
7218 		ire_refrele(ire);		/* Held in ire_add */
7219 	}
7220 	if (ipif_refheld)
7221 		ipif_refrele(ipif);
7222 	return (0);
7223 }
7224 
7225 /*
7226  * ip_rt_delete is called to delete an IPv4 route.
7227  * ipif_arg is passed in to associate it with the correct interface.
7228  * src_ipif is passed to associate the incoming interface of the packet.
7229  * We may need to restart this operation if the ipif cannot be looked up
7230  * due to an exclusive operation that is currently in progress. The restart
7231  * entry point is specified by 'func'
7232  */
7233 /* ARGSUSED4 */
7234 int
7235 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7236     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
7237     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func)
7238 {
7239 	ire_t	*ire = NULL;
7240 	ipif_t	*ipif;
7241 	boolean_t ipif_refheld = B_FALSE;
7242 	uint_t	type;
7243 	uint_t	match_flags = MATCH_IRE_TYPE;
7244 	int	err = 0;
7245 
7246 	ip1dbg(("ip_rt_delete:"));
7247 	/*
7248 	 * If this is the case of RTF_HOST being set, then we set the netmask
7249 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7250 	 */
7251 	if (flags & RTF_HOST) {
7252 		mask = IP_HOST_MASK;
7253 		match_flags |= MATCH_IRE_MASK;
7254 	} else if (rtm_addrs & RTA_NETMASK) {
7255 		match_flags |= MATCH_IRE_MASK;
7256 	}
7257 
7258 	/*
7259 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7260 	 * we check if the gateway address is one of our interfaces first,
7261 	 * and fall back on RTF_GATEWAY routes.
7262 	 *
7263 	 * This makes it possible to delete an original
7264 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7265 	 *
7266 	 * As the interface index specified with the RTA_IFP sockaddr is the
7267 	 * same for all ipif's off of an ill, the matching logic below uses
7268 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7269 	 * sharing the same prefix and interface index as the the route
7270 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7271 	 * is specified in the request.
7272 	 *
7273 	 * On the other hand, since the gateway address will usually be
7274 	 * different for each ipif on the system, the matching logic
7275 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7276 	 * route.  This means that interface routes for the same prefix can be
7277 	 * uniquely identified if they belong to distinct ipif's and if a
7278 	 * RTA_IFP sockaddr is not present.
7279 	 *
7280 	 * For more detail on specifying routes by gateway address and by
7281 	 * interface index, see the comments in ip_rt_add().
7282 	 * gw_addr could be zero in some cases when both RTA_SRCIFP and
7283 	 * RTA_IFP are specified. If RTA_SRCIFP is specified and  both
7284 	 * RTA_IFP and gateway_addr are NULL/zero, then delete will not
7285 	 * succeed.
7286 	 */
7287 	if (src_ipif != NULL) {
7288 		if (ipif_arg == NULL && gw_addr != 0) {
7289 			ipif_arg = ipif_lookup_interface(gw_addr, dst_addr,
7290 			    q, mp, func, &err);
7291 			if (ipif_arg != NULL)
7292 				ipif_refheld = B_TRUE;
7293 		}
7294 		if (ipif_arg == NULL) {
7295 			err = (err == EINPROGRESS) ? err : ESRCH;
7296 			return (err);
7297 		}
7298 		ipif = ipif_arg;
7299 	} else {
7300 		ipif = ipif_lookup_interface(gw_addr, dst_addr,
7301 			    q, mp, func, &err);
7302 		if (ipif != NULL)
7303 			ipif_refheld = B_TRUE;
7304 		else if (err == EINPROGRESS)
7305 			return (err);
7306 		else
7307 			err = 0;
7308 	}
7309 	if (ipif != NULL) {
7310 		if (ipif_arg != NULL) {
7311 			if (ipif_refheld) {
7312 				ipif_refrele(ipif);
7313 				ipif_refheld = B_FALSE;
7314 			}
7315 			ipif = ipif_arg;
7316 			match_flags |= MATCH_IRE_ILL;
7317 		} else {
7318 			match_flags |= MATCH_IRE_IPIF;
7319 		}
7320 		if (src_ipif != NULL) {
7321 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
7322 			    ipif, src_ipif->ipif_ill, match_flags);
7323 		} else {
7324 			if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7325 				ire = ire_ctable_lookup(dst_addr, 0,
7326 				    IRE_LOOPBACK, ipif, ALL_ZONES, NULL,
7327 				    match_flags);
7328 			}
7329 			if (ire == NULL) {
7330 				ire = ire_ftable_lookup(dst_addr, mask, 0,
7331 				    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
7332 				    NULL, match_flags);
7333 			}
7334 		}
7335 	}
7336 
7337 	if (ire == NULL) {
7338 		/*
7339 		 * At this point, the gateway address is not one of our own
7340 		 * addresses or a matching interface route was not found.  We
7341 		 * set the IRE type to lookup based on whether
7342 		 * this is a host route, a default route or just a prefix.
7343 		 *
7344 		 * If an ipif_arg was passed in, then the lookup is based on an
7345 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7346 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7347 		 * set as the route being looked up is not a traditional
7348 		 * interface route.
7349 		 * Since we do not add gateway route with srcipif, we don't
7350 		 * expect to find it either.
7351 		 */
7352 		if (src_ipif != NULL) {
7353 			if (ipif_refheld)
7354 				ipif_refrele(ipif);
7355 			return (ESRCH);
7356 		} else {
7357 			match_flags &= ~MATCH_IRE_IPIF;
7358 			match_flags |= MATCH_IRE_GW;
7359 			if (ipif_arg != NULL)
7360 				match_flags |= MATCH_IRE_ILL;
7361 			if (mask == IP_HOST_MASK)
7362 				type = IRE_HOST;
7363 			else if (mask == 0)
7364 				type = IRE_DEFAULT;
7365 			else
7366 				type = IRE_PREFIX;
7367 			ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type,
7368 			    ipif_arg, NULL, ALL_ZONES, 0, NULL, match_flags);
7369 		}
7370 	}
7371 
7372 	if (ipif_refheld)
7373 		ipif_refrele(ipif);
7374 
7375 	/* ipif is not refheld anymore */
7376 	if (ire == NULL)
7377 		return (ESRCH);
7378 
7379 	if (ire->ire_flags & RTF_MULTIRT) {
7380 		/*
7381 		 * Invoke the CGTP (multirouting) filtering module
7382 		 * to remove the dst address from the filtering database.
7383 		 * Packets coming from that address will no longer be
7384 		 * filtered to remove duplicates.
7385 		 */
7386 		if (ip_cgtp_filter_ops != NULL) {
7387 			err = ip_cgtp_filter_ops->cfo_del_dest_v4(ire->ire_addr,
7388 			    ire->ire_gateway_addr);
7389 		}
7390 		ip_cgtp_bcast_delete(ire);
7391 	}
7392 
7393 	ipif = ire->ire_ipif;
7394 	/*
7395 	 * Removing from ipif_saved_ire_mp is not necessary
7396 	 * when src_ipif being non-NULL. ip_rt_add does not
7397 	 * save the ires which src_ipif being non-NULL.
7398 	 */
7399 	if (ipif != NULL && src_ipif == NULL) {
7400 		ipif_remove_ire(ipif, ire);
7401 	}
7402 	if (ioctl_msg)
7403 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0);
7404 	ire_delete(ire);
7405 	ire_refrele(ire);
7406 	return (err);
7407 }
7408 
7409 /*
7410  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7411  */
7412 /* ARGSUSED */
7413 int
7414 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7415     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7416 {
7417 	ipaddr_t dst_addr;
7418 	ipaddr_t gw_addr;
7419 	ipaddr_t mask;
7420 	int error = 0;
7421 	mblk_t *mp1;
7422 	struct rtentry *rt;
7423 	ipif_t *ipif = NULL;
7424 
7425 	ip1dbg(("ip_siocaddrt:"));
7426 	/* Existence of mp1 verified in ip_wput_nondata */
7427 	mp1 = mp->b_cont->b_cont;
7428 	rt = (struct rtentry *)mp1->b_rptr;
7429 
7430 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7431 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7432 
7433 	/*
7434 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7435 	 * to a particular host address.  In this case, we set the netmask to
7436 	 * all ones for the particular destination address.  Otherwise,
7437 	 * determine the netmask to be used based on dst_addr and the interfaces
7438 	 * in use.
7439 	 */
7440 	if (rt->rt_flags & RTF_HOST) {
7441 		mask = IP_HOST_MASK;
7442 	} else {
7443 		/*
7444 		 * Note that ip_subnet_mask returns a zero mask in the case of
7445 		 * default (an all-zeroes address).
7446 		 */
7447 		mask = ip_subnet_mask(dst_addr, &ipif);
7448 	}
7449 
7450 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7451 	    NULL, B_TRUE, q, mp, ip_process_ioctl, NULL);
7452 	if (ipif != NULL)
7453 		ipif_refrele(ipif);
7454 	return (error);
7455 }
7456 
7457 /*
7458  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7459  */
7460 /* ARGSUSED */
7461 int
7462 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7463     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7464 {
7465 	ipaddr_t dst_addr;
7466 	ipaddr_t gw_addr;
7467 	ipaddr_t mask;
7468 	int error;
7469 	mblk_t *mp1;
7470 	struct rtentry *rt;
7471 	ipif_t *ipif = NULL;
7472 
7473 	ip1dbg(("ip_siocdelrt:"));
7474 	/* Existence of mp1 verified in ip_wput_nondata */
7475 	mp1 = mp->b_cont->b_cont;
7476 	rt = (struct rtentry *)mp1->b_rptr;
7477 
7478 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7479 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7480 
7481 	/*
7482 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7483 	 * to a particular host address.  In this case, we set the netmask to
7484 	 * all ones for the particular destination address.  Otherwise,
7485 	 * determine the netmask to be used based on dst_addr and the interfaces
7486 	 * in use.
7487 	 */
7488 	if (rt->rt_flags & RTF_HOST) {
7489 		mask = IP_HOST_MASK;
7490 	} else {
7491 		/*
7492 		 * Note that ip_subnet_mask returns a zero mask in the case of
7493 		 * default (an all-zeroes address).
7494 		 */
7495 		mask = ip_subnet_mask(dst_addr, &ipif);
7496 	}
7497 
7498 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7499 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, NULL,
7500 	    B_TRUE, q, mp, ip_process_ioctl);
7501 	if (ipif != NULL)
7502 		ipif_refrele(ipif);
7503 	return (error);
7504 }
7505 
7506 /*
7507  * Enqueue the mp onto the ipsq, chained by b_next.
7508  * b_prev stores the function to be executed later, and b_queue the queue
7509  * where this mp originated.
7510  */
7511 void
7512 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7513     ill_t *pending_ill)
7514 {
7515 	conn_t	*connp = NULL;
7516 
7517 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7518 	ASSERT(func != NULL);
7519 
7520 	mp->b_queue = q;
7521 	mp->b_prev = (void *)func;
7522 	mp->b_next = NULL;
7523 
7524 	switch (type) {
7525 	case CUR_OP:
7526 		if (ipsq->ipsq_mptail != NULL) {
7527 			ASSERT(ipsq->ipsq_mphead != NULL);
7528 			ipsq->ipsq_mptail->b_next = mp;
7529 		} else {
7530 			ASSERT(ipsq->ipsq_mphead == NULL);
7531 			ipsq->ipsq_mphead = mp;
7532 		}
7533 		ipsq->ipsq_mptail = mp;
7534 		break;
7535 
7536 	case NEW_OP:
7537 		if (ipsq->ipsq_xopq_mptail != NULL) {
7538 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7539 			ipsq->ipsq_xopq_mptail->b_next = mp;
7540 		} else {
7541 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7542 			ipsq->ipsq_xopq_mphead = mp;
7543 		}
7544 		ipsq->ipsq_xopq_mptail = mp;
7545 		break;
7546 	default:
7547 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7548 	}
7549 
7550 	if (CONN_Q(q) && pending_ill != NULL) {
7551 		connp = Q_TO_CONN(q);
7552 
7553 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7554 		connp->conn_oper_pending_ill = pending_ill;
7555 	}
7556 }
7557 
7558 /*
7559  * Return the mp at the head of the ipsq. After emptying the ipsq
7560  * look at the next ioctl, if this ioctl is complete. Otherwise
7561  * return, we will resume when we complete the current ioctl.
7562  * The current ioctl will wait till it gets a response from the
7563  * driver below.
7564  */
7565 static mblk_t *
7566 ipsq_dq(ipsq_t *ipsq)
7567 {
7568 	mblk_t	*mp;
7569 
7570 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7571 
7572 	mp = ipsq->ipsq_mphead;
7573 	if (mp != NULL) {
7574 		ipsq->ipsq_mphead = mp->b_next;
7575 		if (ipsq->ipsq_mphead == NULL)
7576 			ipsq->ipsq_mptail = NULL;
7577 		mp->b_next = NULL;
7578 		return (mp);
7579 	}
7580 	if (ipsq->ipsq_current_ipif != NULL)
7581 		return (NULL);
7582 	mp = ipsq->ipsq_xopq_mphead;
7583 	if (mp != NULL) {
7584 		ipsq->ipsq_xopq_mphead = mp->b_next;
7585 		if (ipsq->ipsq_xopq_mphead == NULL)
7586 			ipsq->ipsq_xopq_mptail = NULL;
7587 		mp->b_next = NULL;
7588 		return (mp);
7589 	}
7590 	return (NULL);
7591 }
7592 
7593 /*
7594  * Enter the ipsq corresponding to ill, by waiting synchronously till
7595  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7596  * will have to drain completely before ipsq_enter returns success.
7597  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7598  * and the ipsq_exit logic will start the next enqueued ioctl after
7599  * completion of the current ioctl. If 'force' is used, we don't wait
7600  * for the enqueued ioctls. This is needed when a conn_close wants to
7601  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7602  * of an ill can also use this option. But we dont' use it currently.
7603  */
7604 #define	ENTER_SQ_WAIT_TICKS 100
7605 boolean_t
7606 ipsq_enter(ill_t *ill, boolean_t force)
7607 {
7608 	ipsq_t	*ipsq;
7609 	boolean_t waited_enough = B_FALSE;
7610 
7611 	/*
7612 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7613 	 * Since the <ill-ipsq> assocs could change while we wait for the
7614 	 * writer, it is easier to wait on a fixed global rather than try to
7615 	 * cv_wait on a changing ipsq.
7616 	 */
7617 	mutex_enter(&ill->ill_lock);
7618 	for (;;) {
7619 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7620 			mutex_exit(&ill->ill_lock);
7621 			return (B_FALSE);
7622 		}
7623 
7624 		ipsq = ill->ill_phyint->phyint_ipsq;
7625 		mutex_enter(&ipsq->ipsq_lock);
7626 		if (ipsq->ipsq_writer == NULL &&
7627 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7628 			break;
7629 		} else if (ipsq->ipsq_writer != NULL) {
7630 			mutex_exit(&ipsq->ipsq_lock);
7631 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7632 		} else {
7633 			mutex_exit(&ipsq->ipsq_lock);
7634 			if (force) {
7635 				(void) cv_timedwait(&ill->ill_cv,
7636 				    &ill->ill_lock,
7637 				    lbolt + ENTER_SQ_WAIT_TICKS);
7638 				waited_enough = B_TRUE;
7639 				continue;
7640 			} else {
7641 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7642 			}
7643 		}
7644 	}
7645 
7646 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7647 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7648 	ipsq->ipsq_writer = curthread;
7649 	ipsq->ipsq_reentry_cnt++;
7650 #ifdef ILL_DEBUG
7651 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7652 #endif
7653 	mutex_exit(&ipsq->ipsq_lock);
7654 	mutex_exit(&ill->ill_lock);
7655 	return (B_TRUE);
7656 }
7657 
7658 /*
7659  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7660  * certain critical operations like plumbing (i.e. most set ioctls),
7661  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7662  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7663  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7664  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7665  * threads executing in the ipsq. Responses from the driver pertain to the
7666  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7667  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7668  *
7669  * If a thread does not want to reenter the ipsq when it is already writer,
7670  * it must make sure that the specified reentry point to be called later
7671  * when the ipsq is empty, nor any code path starting from the specified reentry
7672  * point must never ever try to enter the ipsq again. Otherwise it can lead
7673  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7674  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7675  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7676  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7677  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7678  * ioctl if the current ioctl has completed. If the current ioctl is still
7679  * in progress it simply returns. The current ioctl could be waiting for
7680  * a response from another module (arp_ or the driver or could be waiting for
7681  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7682  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7683  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7684  * ipsq_current_ipif is clear which happens only on ioctl completion.
7685  */
7686 
7687 /*
7688  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7689  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7690  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7691  * completion.
7692  */
7693 ipsq_t *
7694 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7695     ipsq_func_t func, int type, boolean_t reentry_ok)
7696 {
7697 	ipsq_t	*ipsq;
7698 
7699 	/* Only 1 of ipif or ill can be specified */
7700 	ASSERT((ipif != NULL) ^ (ill != NULL));
7701 	if (ipif != NULL)
7702 		ill = ipif->ipif_ill;
7703 
7704 	/*
7705 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7706 	 * ipsq of an ill can't change when ill_lock is held.
7707 	 */
7708 	GRAB_CONN_LOCK(q);
7709 	mutex_enter(&ill->ill_lock);
7710 	ipsq = ill->ill_phyint->phyint_ipsq;
7711 	mutex_enter(&ipsq->ipsq_lock);
7712 
7713 	/*
7714 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7715 	 *    (Note: If the caller does not specify reentry_ok then neither
7716 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7717 	 *    again. Otherwise it can lead to an infinite loop
7718 	 * 2. Enter the ipsq if there is no current writer and this attempted
7719 	 *    entry is part of the current ioctl or operation
7720 	 * 3. Enter the ipsq if there is no current writer and this is a new
7721 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7722 	 *    empty and there is no ioctl (or operation) currently in progress
7723 	 */
7724 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7725 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7726 	    ipsq->ipsq_current_ipif == NULL))) ||
7727 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7728 		/* Success. */
7729 		ipsq->ipsq_reentry_cnt++;
7730 		ipsq->ipsq_writer = curthread;
7731 		mutex_exit(&ipsq->ipsq_lock);
7732 		mutex_exit(&ill->ill_lock);
7733 		RELEASE_CONN_LOCK(q);
7734 #ifdef ILL_DEBUG
7735 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7736 #endif
7737 		return (ipsq);
7738 	}
7739 
7740 	ipsq_enq(ipsq, q, mp, func, type, ill);
7741 
7742 	mutex_exit(&ipsq->ipsq_lock);
7743 	mutex_exit(&ill->ill_lock);
7744 	RELEASE_CONN_LOCK(q);
7745 	return (NULL);
7746 }
7747 
7748 /*
7749  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7750  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7751  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7752  * completion.
7753  *
7754  * This function does a refrele on the ipif/ill.
7755  */
7756 void
7757 qwriter_ip(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7758     ipsq_func_t func, int type, boolean_t reentry_ok)
7759 {
7760 	ipsq_t	*ipsq;
7761 
7762 	ipsq = ipsq_try_enter(ipif, ill, q, mp, func, type, reentry_ok);
7763 	/*
7764 	 * Caller must have done a refhold on the ipif. ipif_refrele
7765 	 * happens on the passed ipif. We can do this since we are
7766 	 * already exclusive, or we won't access ipif henceforth, Both
7767 	 * this func and caller will just return if we ipsq_try_enter
7768 	 * fails above. This is needed because func needs to
7769 	 * see the correct refcount. Eg. removeif can work only then.
7770 	 */
7771 	if (ipif != NULL)
7772 		ipif_refrele(ipif);
7773 	else
7774 		ill_refrele(ill);
7775 	if (ipsq != NULL) {
7776 		(*func)(ipsq, q, mp, NULL);
7777 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7778 	}
7779 }
7780 
7781 /*
7782  * If there are more than ILL_GRP_CNT ills in a group,
7783  * we use kmem alloc'd buffers, else use the stack
7784  */
7785 #define	ILL_GRP_CNT	14
7786 /*
7787  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7788  * Called by a thread that is currently exclusive on this ipsq.
7789  */
7790 void
7791 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7792 {
7793 	queue_t	*q;
7794 	mblk_t	*mp;
7795 	ipsq_func_t	func;
7796 	int	next;
7797 	ill_t	**ill_list = NULL;
7798 	size_t	ill_list_size = 0;
7799 	int	cnt = 0;
7800 	boolean_t need_ipsq_free = B_FALSE;
7801 
7802 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7803 	mutex_enter(&ipsq->ipsq_lock);
7804 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7805 	if (ipsq->ipsq_reentry_cnt != 1) {
7806 		ipsq->ipsq_reentry_cnt--;
7807 		mutex_exit(&ipsq->ipsq_lock);
7808 		return;
7809 	}
7810 
7811 	mp = ipsq_dq(ipsq);
7812 	while (mp != NULL) {
7813 again:
7814 		mutex_exit(&ipsq->ipsq_lock);
7815 		func = (ipsq_func_t)mp->b_prev;
7816 		q = (queue_t *)mp->b_queue;
7817 		mp->b_prev = NULL;
7818 		mp->b_queue = NULL;
7819 
7820 		/*
7821 		 * If 'q' is an conn queue, it is valid, since we did a
7822 		 * a refhold on the connp, at the start of the ioctl.
7823 		 * If 'q' is an ill queue, it is valid, since close of an
7824 		 * ill will clean up the 'ipsq'.
7825 		 */
7826 		(*func)(ipsq, q, mp, NULL);
7827 
7828 		mutex_enter(&ipsq->ipsq_lock);
7829 		mp = ipsq_dq(ipsq);
7830 	}
7831 
7832 	mutex_exit(&ipsq->ipsq_lock);
7833 
7834 	/*
7835 	 * Need to grab the locks in the right order. Need to
7836 	 * atomically check (under ipsq_lock) that there are no
7837 	 * messages before relinquishing the ipsq. Also need to
7838 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7839 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7840 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7841 	 * to grab ill_g_lock as writer.
7842 	 */
7843 	rw_enter(&ill_g_lock, ipsq->ipsq_split ? RW_WRITER : RW_READER);
7844 
7845 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7846 	if (ipsq->ipsq_refs != 0) {
7847 		/* At most 2 ills v4/v6 per phyint */
7848 		cnt = ipsq->ipsq_refs << 1;
7849 		ill_list_size = cnt * sizeof (ill_t *);
7850 		/*
7851 		 * If memory allocation fails, we will do the split
7852 		 * the next time ipsq_exit is called for whatever reason.
7853 		 * As long as the ipsq_split flag is set the need to
7854 		 * split is remembered.
7855 		 */
7856 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7857 		if (ill_list != NULL)
7858 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7859 	}
7860 	mutex_enter(&ipsq->ipsq_lock);
7861 	mp = ipsq_dq(ipsq);
7862 	if (mp != NULL) {
7863 		/* oops, some message has landed up, we can't get out */
7864 		if (ill_list != NULL)
7865 			ill_unlock_ills(ill_list, cnt);
7866 		rw_exit(&ill_g_lock);
7867 		if (ill_list != NULL)
7868 			kmem_free(ill_list, ill_list_size);
7869 		ill_list = NULL;
7870 		ill_list_size = 0;
7871 		cnt = 0;
7872 		goto again;
7873 	}
7874 
7875 	/*
7876 	 * Split only if no ioctl is pending and if memory alloc succeeded
7877 	 * above.
7878 	 */
7879 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7880 		ill_list != NULL) {
7881 		/*
7882 		 * No new ill can join this ipsq since we are holding the
7883 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7884 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7885 		 * If so we will retry on the next ipsq_exit.
7886 		 */
7887 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7888 	}
7889 
7890 	/*
7891 	 * We are holding the ipsq lock, hence no new messages can
7892 	 * land up on the ipsq, and there are no messages currently.
7893 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7894 	 * atomically while holding ill locks.
7895 	 */
7896 	ipsq->ipsq_writer = NULL;
7897 	ipsq->ipsq_reentry_cnt--;
7898 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7899 #ifdef ILL_DEBUG
7900 	ipsq->ipsq_depth = 0;
7901 #endif
7902 	mutex_exit(&ipsq->ipsq_lock);
7903 	/*
7904 	 * For IPMP this should wake up all ills in this ipsq.
7905 	 * We need to hold the ill_lock while waking up waiters to
7906 	 * avoid missed wakeups. But there is no need to acquire all
7907 	 * the ill locks and then wakeup. If we have not acquired all
7908 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7909 	 * wakes up ills one at a time after getting the right ill_lock
7910 	 */
7911 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7912 	if (ill_list != NULL)
7913 		ill_unlock_ills(ill_list, cnt);
7914 	if (ipsq->ipsq_refs == 0)
7915 		need_ipsq_free = B_TRUE;
7916 	rw_exit(&ill_g_lock);
7917 	if (ill_list != 0)
7918 		kmem_free(ill_list, ill_list_size);
7919 
7920 	if (need_ipsq_free) {
7921 		/*
7922 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7923 		 * looked up. ipsq can be looked up only thru ill or phyint
7924 		 * and there are no ills/phyint on this ipsq.
7925 		 */
7926 		ipsq_delete(ipsq);
7927 	}
7928 	/*
7929 	 * Now start any igmp or mld timers that could not be started
7930 	 * while inside the ipsq. The timers can't be started while inside
7931 	 * the ipsq, since igmp_start_timers may need to call untimeout()
7932 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
7933 	 * there could be a deadlock since the timeout handlers
7934 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
7935 	 * wait in ipsq_enter() trying to get the ipsq.
7936 	 *
7937 	 * However there is one exception to the above. If this thread is
7938 	 * itself the igmp/mld timeout handler thread, then we don't want
7939 	 * to start any new timer until the current handler is done. The
7940 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
7941 	 * all others pass B_TRUE.
7942 	 */
7943 	if (start_igmp_timer) {
7944 		mutex_enter(&igmp_timer_lock);
7945 		next = igmp_deferred_next;
7946 		igmp_deferred_next = INFINITY;
7947 		mutex_exit(&igmp_timer_lock);
7948 
7949 		if (next != INFINITY)
7950 			igmp_start_timers(next);
7951 	}
7952 
7953 	if (start_mld_timer) {
7954 		mutex_enter(&mld_timer_lock);
7955 		next = mld_deferred_next;
7956 		mld_deferred_next = INFINITY;
7957 		mutex_exit(&mld_timer_lock);
7958 
7959 		if (next != INFINITY)
7960 			mld_start_timers(next);
7961 	}
7962 }
7963 
7964 /*
7965  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
7966  * and `ioccmd'.
7967  */
7968 void
7969 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
7970 {
7971 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7972 
7973 	mutex_enter(&ipsq->ipsq_lock);
7974 	ASSERT(ipsq->ipsq_current_ipif == NULL);
7975 	ASSERT(ipsq->ipsq_current_ioctl == 0);
7976 	ipsq->ipsq_current_ipif = ipif;
7977 	ipsq->ipsq_current_ioctl = ioccmd;
7978 	mutex_exit(&ipsq->ipsq_lock);
7979 }
7980 
7981 /*
7982  * Finish the current exclusive operation on `ipsq'.  Note that other
7983  * operations will not be able to proceed until an ipsq_exit() is done.
7984  */
7985 void
7986 ipsq_current_finish(ipsq_t *ipsq)
7987 {
7988 	ipif_t *ipif = ipsq->ipsq_current_ipif;
7989 	hook_nic_event_t *info;
7990 
7991 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7992 
7993 	/*
7994 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
7995 	 * (but we're careful to never set IPIF_CHANGING in that case).
7996 	 */
7997 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
7998 		mutex_enter(&ipif->ipif_ill->ill_lock);
7999 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8000 		/*
8001 		 * Unhook the nic event message from the ill and enqueue it
8002 		 * into the nic event taskq.
8003 		 */
8004 		if ((info = ipif->ipif_ill->ill_nic_event_info) != NULL) {
8005 			if (ddi_taskq_dispatch(eventq_queue_nic,
8006 			    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
8007 				ip2dbg(("ipsq_current_finish: "
8008 				    "ddi_taskq_dispatch failed\n"));
8009 				if (info->hne_data != NULL)
8010 					kmem_free(info->hne_data,
8011 					    info->hne_datalen);
8012 				kmem_free(info, sizeof (hook_nic_event_t));
8013 			}
8014 			ipif->ipif_ill->ill_nic_event_info = NULL;
8015 		}
8016 		mutex_exit(&ipif->ipif_ill->ill_lock);
8017 	}
8018 
8019 	mutex_enter(&ipsq->ipsq_lock);
8020 	ASSERT(ipsq->ipsq_current_ipif != NULL);
8021 	ipsq->ipsq_current_ipif = NULL;
8022 	ipsq->ipsq_current_ioctl = 0;
8023 	mutex_exit(&ipsq->ipsq_lock);
8024 }
8025 
8026 /*
8027  * The ill is closing. Flush all messages on the ipsq that originated
8028  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8029  * for this ill since ipsq_enter could not have entered until then.
8030  * New messages can't be queued since the CONDEMNED flag is set.
8031  */
8032 static void
8033 ipsq_flush(ill_t *ill)
8034 {
8035 	queue_t	*q;
8036 	mblk_t	*prev;
8037 	mblk_t	*mp;
8038 	mblk_t	*mp_next;
8039 	ipsq_t	*ipsq;
8040 
8041 	ASSERT(IAM_WRITER_ILL(ill));
8042 	ipsq = ill->ill_phyint->phyint_ipsq;
8043 	/*
8044 	 * Flush any messages sent up by the driver.
8045 	 */
8046 	mutex_enter(&ipsq->ipsq_lock);
8047 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8048 		mp_next = mp->b_next;
8049 		q = mp->b_queue;
8050 		if (q == ill->ill_rq || q == ill->ill_wq) {
8051 			/* Remove the mp from the ipsq */
8052 			if (prev == NULL)
8053 				ipsq->ipsq_mphead = mp->b_next;
8054 			else
8055 				prev->b_next = mp->b_next;
8056 			if (ipsq->ipsq_mptail == mp) {
8057 				ASSERT(mp_next == NULL);
8058 				ipsq->ipsq_mptail = prev;
8059 			}
8060 			inet_freemsg(mp);
8061 		} else {
8062 			prev = mp;
8063 		}
8064 	}
8065 	mutex_exit(&ipsq->ipsq_lock);
8066 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8067 	ipsq_xopq_mp_cleanup(ill, NULL);
8068 	ill_pending_mp_cleanup(ill);
8069 }
8070 
8071 /*
8072  * Clean up one squeue element. ill_inuse_ref is protected by ill_lock.
8073  * The real cleanup happens behind the squeue via ip_squeue_clean function but
8074  * we need to protect ourselfs from 2 threads trying to cleanup at the same
8075  * time (possible with one port going down for aggr and someone tearing down the
8076  * entire aggr simultaneously. So we use ill_inuse_ref protected by ill_lock
8077  * to indicate when the cleanup has started (1 ref) and when the cleanup
8078  * is done (0 ref). When a new ring gets assigned to squeue, we start by
8079  * putting 2 ref on ill_inuse_ref.
8080  */
8081 static void
8082 ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring)
8083 {
8084 	conn_t *connp;
8085 	squeue_t *sqp;
8086 	mblk_t *mp;
8087 
8088 	ASSERT(rx_ring != NULL);
8089 
8090 	/* Just clean one squeue */
8091 	mutex_enter(&ill->ill_lock);
8092 	/*
8093 	 * Reset the ILL_SOFT_RING_ASSIGN bit so that
8094 	 * ip_squeue_soft_ring_affinty() will not go
8095 	 * ahead with assigning rings.
8096 	 */
8097 	ill->ill_state_flags &= ~ILL_SOFT_RING_ASSIGN;
8098 	while (rx_ring->rr_ring_state == ILL_RING_INPROC)
8099 		/* Some operations pending on the ring. Wait */
8100 		cv_wait(&ill->ill_cv, &ill->ill_lock);
8101 
8102 	if (rx_ring->rr_ring_state != ILL_RING_INUSE) {
8103 		/*
8104 		 * Someone already trying to clean
8105 		 * this squeue or its already been cleaned.
8106 		 */
8107 		mutex_exit(&ill->ill_lock);
8108 		return;
8109 	}
8110 	sqp = rx_ring->rr_sqp;
8111 
8112 	if (sqp == NULL) {
8113 		/*
8114 		 * The rx_ring never had a squeue assigned to it.
8115 		 * We are under ill_lock so we can clean it up
8116 		 * here itself since no one can get to it.
8117 		 */
8118 		rx_ring->rr_blank = NULL;
8119 		rx_ring->rr_handle = NULL;
8120 		rx_ring->rr_sqp = NULL;
8121 		rx_ring->rr_ring_state = ILL_RING_FREE;
8122 		mutex_exit(&ill->ill_lock);
8123 		return;
8124 	}
8125 
8126 	/* Set the state that its being cleaned */
8127 	rx_ring->rr_ring_state = ILL_RING_BEING_FREED;
8128 	ASSERT(sqp != NULL);
8129 	mutex_exit(&ill->ill_lock);
8130 
8131 	/*
8132 	 * Use the preallocated ill_unbind_conn for this purpose
8133 	 */
8134 	connp = ill->ill_dls_capab->ill_unbind_conn;
8135 
8136 	ASSERT(!connp->conn_tcp->tcp_closemp.b_prev);
8137 	TCP_DEBUG_GETPCSTACK(connp->conn_tcp->tcmp_stk, 15);
8138 	if (connp->conn_tcp->tcp_closemp.b_prev == NULL)
8139 		connp->conn_tcp->tcp_closemp_used = 1;
8140 	else
8141 		connp->conn_tcp->tcp_closemp_used++;
8142 	mp = &connp->conn_tcp->tcp_closemp;
8143 	CONN_INC_REF(connp);
8144 	squeue_enter(sqp, mp, ip_squeue_clean, connp, NULL);
8145 
8146 	mutex_enter(&ill->ill_lock);
8147 	while (rx_ring->rr_ring_state != ILL_RING_FREE)
8148 		cv_wait(&ill->ill_cv, &ill->ill_lock);
8149 
8150 	mutex_exit(&ill->ill_lock);
8151 }
8152 
8153 static void
8154 ipsq_clean_all(ill_t *ill)
8155 {
8156 	int idx;
8157 
8158 	/*
8159 	 * No need to clean if poll_capab isn't set for this ill
8160 	 */
8161 	if (!(ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING)))
8162 		return;
8163 
8164 	for (idx = 0; idx < ILL_MAX_RINGS; idx++) {
8165 		ill_rx_ring_t *ipr = &ill->ill_dls_capab->ill_ring_tbl[idx];
8166 		ipsq_clean_ring(ill, ipr);
8167 	}
8168 
8169 	ill->ill_capabilities &= ~(ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING);
8170 }
8171 
8172 /* ARGSUSED */
8173 int
8174 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8175     ip_ioctl_cmd_t *ipip, void *ifreq)
8176 {
8177 	ill_t	*ill;
8178 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8179 	boolean_t isv6;
8180 	conn_t	*connp;
8181 
8182 	connp = Q_TO_CONN(q);
8183 	isv6 = connp->conn_af_isv6;
8184 	/*
8185 	 * Set original index.
8186 	 * Failover and failback move logical interfaces
8187 	 * from one physical interface to another.  The
8188 	 * original index indicates the parent of a logical
8189 	 * interface, in other words, the physical interface
8190 	 * the logical interface will be moved back to on
8191 	 * failback.
8192 	 */
8193 
8194 	/*
8195 	 * Don't allow the original index to be changed
8196 	 * for non-failover addresses, autoconfigured
8197 	 * addresses, or IPv6 link local addresses.
8198 	 */
8199 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8200 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8201 		return (EINVAL);
8202 	}
8203 	/*
8204 	 * The new original index must be in use by some
8205 	 * physical interface.
8206 	 */
8207 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8208 	    NULL, NULL);
8209 	if (ill == NULL)
8210 		return (ENXIO);
8211 	ill_refrele(ill);
8212 
8213 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8214 	/*
8215 	 * When this ipif gets failed back, don't
8216 	 * preserve the original id, as it is no
8217 	 * longer applicable.
8218 	 */
8219 	ipif->ipif_orig_ipifid = 0;
8220 	/*
8221 	 * For IPv4, change the original index of any
8222 	 * multicast addresses associated with the
8223 	 * ipif to the new value.
8224 	 */
8225 	if (!isv6) {
8226 		ilm_t *ilm;
8227 
8228 		mutex_enter(&ipif->ipif_ill->ill_lock);
8229 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8230 		    ilm = ilm->ilm_next) {
8231 			if (ilm->ilm_ipif == ipif) {
8232 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8233 			}
8234 		}
8235 		mutex_exit(&ipif->ipif_ill->ill_lock);
8236 	}
8237 	return (0);
8238 }
8239 
8240 /* ARGSUSED */
8241 int
8242 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8243     ip_ioctl_cmd_t *ipip, void *ifreq)
8244 {
8245 	struct lifreq *lifr = (struct lifreq *)ifreq;
8246 
8247 	/*
8248 	 * Get the original interface index i.e the one
8249 	 * before FAILOVER if it ever happened.
8250 	 */
8251 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8252 	return (0);
8253 }
8254 
8255 /*
8256  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8257  * refhold and return the associated ipif
8258  */
8259 int
8260 ip_extract_tunreq(queue_t *q, mblk_t *mp, ipif_t **ipifp, ipsq_func_t func)
8261 {
8262 	boolean_t exists;
8263 	struct iftun_req *ta;
8264 	ipif_t	*ipif;
8265 	ill_t	*ill;
8266 	boolean_t isv6;
8267 	mblk_t	*mp1;
8268 	int	error;
8269 	conn_t	*connp;
8270 
8271 	/* Existence verified in ip_wput_nondata */
8272 	mp1 = mp->b_cont->b_cont;
8273 	ta = (struct iftun_req *)mp1->b_rptr;
8274 	/*
8275 	 * Null terminate the string to protect against buffer
8276 	 * overrun. String was generated by user code and may not
8277 	 * be trusted.
8278 	 */
8279 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8280 
8281 	connp = Q_TO_CONN(q);
8282 	isv6 = connp->conn_af_isv6;
8283 
8284 	/* Disallows implicit create */
8285 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8286 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8287 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error);
8288 	if (ipif == NULL)
8289 		return (error);
8290 
8291 	if (ipif->ipif_id != 0) {
8292 		/*
8293 		 * We really don't want to set/get tunnel parameters
8294 		 * on virtual tunnel interfaces.  Only allow the
8295 		 * base tunnel to do these.
8296 		 */
8297 		ipif_refrele(ipif);
8298 		return (EINVAL);
8299 	}
8300 
8301 	/*
8302 	 * Send down to tunnel mod for ioctl processing.
8303 	 * Will finish ioctl in ip_rput_other().
8304 	 */
8305 	ill = ipif->ipif_ill;
8306 	if (ill->ill_net_type == IRE_LOOPBACK) {
8307 		ipif_refrele(ipif);
8308 		return (EOPNOTSUPP);
8309 	}
8310 
8311 	if (ill->ill_wq == NULL) {
8312 		ipif_refrele(ipif);
8313 		return (ENXIO);
8314 	}
8315 	/*
8316 	 * Mark the ioctl as coming from an IPv6 interface for
8317 	 * tun's convenience.
8318 	 */
8319 	if (ill->ill_isv6)
8320 		ta->ifta_flags |= 0x80000000;
8321 	*ipifp = ipif;
8322 	return (0);
8323 }
8324 
8325 /*
8326  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8327  * and return the associated ipif.
8328  * Return value:
8329  *	Non zero: An error has occurred. ci may not be filled out.
8330  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8331  *	a held ipif in ci.ci_ipif.
8332  */
8333 int
8334 ip_extract_lifreq_cmn(queue_t *q, mblk_t *mp, int cmd_type, int flags,
8335     cmd_info_t *ci, ipsq_func_t func)
8336 {
8337 	sin_t		*sin;
8338 	sin6_t		*sin6;
8339 	char		*name;
8340 	struct ifreq    *ifr;
8341 	struct lifreq    *lifr;
8342 	ipif_t		*ipif = NULL;
8343 	ill_t		*ill;
8344 	conn_t		*connp;
8345 	boolean_t	isv6;
8346 	struct iocblk   *iocp = (struct iocblk *)mp->b_rptr;
8347 	boolean_t	exists;
8348 	int		err;
8349 	mblk_t		*mp1;
8350 	zoneid_t	zoneid;
8351 
8352 	if (q->q_next != NULL) {
8353 		ill = (ill_t *)q->q_ptr;
8354 		isv6 = ill->ill_isv6;
8355 		connp = NULL;
8356 		zoneid = ALL_ZONES;
8357 	} else {
8358 		ill = NULL;
8359 		connp = Q_TO_CONN(q);
8360 		isv6 = connp->conn_af_isv6;
8361 		zoneid = connp->conn_zoneid;
8362 		if (zoneid == GLOBAL_ZONEID) {
8363 			/* global zone can access ipifs in all zones */
8364 			zoneid = ALL_ZONES;
8365 		}
8366 	}
8367 
8368 	/* Has been checked in ip_wput_nondata */
8369 	mp1 = mp->b_cont->b_cont;
8370 
8371 
8372 	if (cmd_type == IF_CMD) {
8373 		/* This a old style SIOC[GS]IF* command */
8374 		ifr = (struct ifreq *)mp1->b_rptr;
8375 		/*
8376 		 * Null terminate the string to protect against buffer
8377 		 * overrun. String was generated by user code and may not
8378 		 * be trusted.
8379 		 */
8380 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8381 		sin = (sin_t *)&ifr->ifr_addr;
8382 		name = ifr->ifr_name;
8383 		ci->ci_sin = sin;
8384 		ci->ci_sin6 = NULL;
8385 		ci->ci_lifr = (struct lifreq *)ifr;
8386 	} else {
8387 		/* This a new style SIOC[GS]LIF* command */
8388 		ASSERT(cmd_type == LIF_CMD);
8389 		lifr = (struct lifreq *)mp1->b_rptr;
8390 		/*
8391 		 * Null terminate the string to protect against buffer
8392 		 * overrun. String was generated by user code and may not
8393 		 * be trusted.
8394 		 */
8395 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8396 		name = lifr->lifr_name;
8397 		sin = (sin_t *)&lifr->lifr_addr;
8398 		sin6 = (sin6_t *)&lifr->lifr_addr;
8399 		if (iocp->ioc_cmd == SIOCSLIFGROUPNAME) {
8400 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8401 			    LIFNAMSIZ);
8402 		}
8403 		ci->ci_sin = sin;
8404 		ci->ci_sin6 = sin6;
8405 		ci->ci_lifr = lifr;
8406 	}
8407 
8408 
8409 	if (iocp->ioc_cmd == SIOCSLIFNAME) {
8410 		/*
8411 		 * The ioctl will be failed if the ioctl comes down
8412 		 * an conn stream
8413 		 */
8414 		if (ill == NULL) {
8415 			/*
8416 			 * Not an ill queue, return EINVAL same as the
8417 			 * old error code.
8418 			 */
8419 			return (ENXIO);
8420 		}
8421 		ipif = ill->ill_ipif;
8422 		ipif_refhold(ipif);
8423 	} else {
8424 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8425 		    &exists, isv6, zoneid,
8426 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err);
8427 		if (ipif == NULL) {
8428 			if (err == EINPROGRESS)
8429 				return (err);
8430 			if (iocp->ioc_cmd == SIOCLIFFAILOVER ||
8431 			    iocp->ioc_cmd == SIOCLIFFAILBACK) {
8432 				/*
8433 				 * Need to try both v4 and v6 since this
8434 				 * ioctl can come down either v4 or v6
8435 				 * socket. The lifreq.lifr_family passed
8436 				 * down by this ioctl is AF_UNSPEC.
8437 				 */
8438 				ipif = ipif_lookup_on_name(name,
8439 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8440 				    zoneid, (connp == NULL) ? q :
8441 				    CONNP_TO_WQ(connp), mp, func, &err);
8442 				if (err == EINPROGRESS)
8443 					return (err);
8444 			}
8445 			err = 0;	/* Ensure we don't use it below */
8446 		}
8447 	}
8448 
8449 	/*
8450 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8451 	 */
8452 	if (ipif != NULL && ipif->ipif_isv6 && cmd_type == IF_CMD) {
8453 		ipif_refrele(ipif);
8454 		return (ENXIO);
8455 	}
8456 
8457 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8458 	    name[0] == '\0') {
8459 		/*
8460 		 * Handle a or a SIOC?IF* with a null name
8461 		 * during plumb (on the ill queue before the I_PLINK).
8462 		 */
8463 		ipif = ill->ill_ipif;
8464 		ipif_refhold(ipif);
8465 	}
8466 
8467 	if (ipif == NULL)
8468 		return (ENXIO);
8469 
8470 	/*
8471 	 * Allow only GET operations if this ipif has been created
8472 	 * temporarily due to a MOVE operation.
8473 	 */
8474 	if (ipif->ipif_replace_zero && !(flags & IPI_REPL)) {
8475 		ipif_refrele(ipif);
8476 		return (EINVAL);
8477 	}
8478 
8479 	ci->ci_ipif = ipif;
8480 	return (0);
8481 }
8482 
8483 /*
8484  * Return the total number of ipifs.
8485  */
8486 static uint_t
8487 ip_get_numifs(zoneid_t zoneid)
8488 {
8489 	uint_t numifs = 0;
8490 	ill_t	*ill;
8491 	ill_walk_context_t	ctx;
8492 	ipif_t	*ipif;
8493 
8494 	rw_enter(&ill_g_lock, RW_READER);
8495 	ill = ILL_START_WALK_V4(&ctx);
8496 
8497 	while (ill != NULL) {
8498 		for (ipif = ill->ill_ipif; ipif != NULL;
8499 		    ipif = ipif->ipif_next) {
8500 			if (ipif->ipif_zoneid == zoneid ||
8501 			    ipif->ipif_zoneid == ALL_ZONES)
8502 				numifs++;
8503 		}
8504 		ill = ill_next(&ctx, ill);
8505 	}
8506 	rw_exit(&ill_g_lock);
8507 	return (numifs);
8508 }
8509 
8510 /*
8511  * Return the total number of ipifs.
8512  */
8513 static uint_t
8514 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid)
8515 {
8516 	uint_t numifs = 0;
8517 	ill_t	*ill;
8518 	ipif_t	*ipif;
8519 	ill_walk_context_t	ctx;
8520 
8521 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8522 
8523 	rw_enter(&ill_g_lock, RW_READER);
8524 	if (family == AF_INET)
8525 		ill = ILL_START_WALK_V4(&ctx);
8526 	else if (family == AF_INET6)
8527 		ill = ILL_START_WALK_V6(&ctx);
8528 	else
8529 		ill = ILL_START_WALK_ALL(&ctx);
8530 
8531 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8532 		for (ipif = ill->ill_ipif; ipif != NULL;
8533 		    ipif = ipif->ipif_next) {
8534 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8535 			    !(lifn_flags & LIFC_NOXMIT))
8536 				continue;
8537 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8538 			    !(lifn_flags & LIFC_TEMPORARY))
8539 				continue;
8540 			if (((ipif->ipif_flags &
8541 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8542 			    IPIF_DEPRECATED)) ||
8543 			    (ill->ill_phyint->phyint_flags &
8544 			    PHYI_LOOPBACK) ||
8545 			    !(ipif->ipif_flags & IPIF_UP)) &&
8546 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8547 				continue;
8548 
8549 			if (zoneid != ipif->ipif_zoneid &&
8550 			    ipif->ipif_zoneid != ALL_ZONES &&
8551 			    (zoneid != GLOBAL_ZONEID ||
8552 			    !(lifn_flags & LIFC_ALLZONES)))
8553 				continue;
8554 
8555 			numifs++;
8556 		}
8557 	}
8558 	rw_exit(&ill_g_lock);
8559 	return (numifs);
8560 }
8561 
8562 uint_t
8563 ip_get_lifsrcofnum(ill_t *ill)
8564 {
8565 	uint_t numifs = 0;
8566 	ill_t	*ill_head = ill;
8567 
8568 	/*
8569 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8570 	 * other thread may be trying to relink the ILLs in this usesrc group
8571 	 * and adjusting the ill_usesrc_grp_next pointers
8572 	 */
8573 	rw_enter(&ill_g_usesrc_lock, RW_READER);
8574 	if ((ill->ill_usesrc_ifindex == 0) &&
8575 	    (ill->ill_usesrc_grp_next != NULL)) {
8576 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8577 		    ill = ill->ill_usesrc_grp_next)
8578 			numifs++;
8579 	}
8580 	rw_exit(&ill_g_usesrc_lock);
8581 
8582 	return (numifs);
8583 }
8584 
8585 /* Null values are passed in for ipif, sin, and ifreq */
8586 /* ARGSUSED */
8587 int
8588 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8589     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8590 {
8591 	int *nump;
8592 
8593 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8594 
8595 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8596 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8597 
8598 	*nump = ip_get_numifs(Q_TO_CONN(q)->conn_zoneid);
8599 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8600 	return (0);
8601 }
8602 
8603 /* Null values are passed in for ipif, sin, and ifreq */
8604 /* ARGSUSED */
8605 int
8606 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8607     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8608 {
8609 	struct lifnum *lifn;
8610 	mblk_t	*mp1;
8611 
8612 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8613 
8614 	/* Existence checked in ip_wput_nondata */
8615 	mp1 = mp->b_cont->b_cont;
8616 
8617 	lifn = (struct lifnum *)mp1->b_rptr;
8618 	switch (lifn->lifn_family) {
8619 	case AF_UNSPEC:
8620 	case AF_INET:
8621 	case AF_INET6:
8622 		break;
8623 	default:
8624 		return (EAFNOSUPPORT);
8625 	}
8626 
8627 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8628 	    Q_TO_CONN(q)->conn_zoneid);
8629 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8630 	return (0);
8631 }
8632 
8633 /* ARGSUSED */
8634 int
8635 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8636     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8637 {
8638 	STRUCT_HANDLE(ifconf, ifc);
8639 	mblk_t *mp1;
8640 	struct iocblk *iocp;
8641 	struct ifreq *ifr;
8642 	ill_walk_context_t	ctx;
8643 	ill_t	*ill;
8644 	ipif_t	*ipif;
8645 	struct sockaddr_in *sin;
8646 	int32_t	ifclen;
8647 	zoneid_t zoneid;
8648 
8649 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8650 
8651 	ip1dbg(("ip_sioctl_get_ifconf"));
8652 	/* Existence verified in ip_wput_nondata */
8653 	mp1 = mp->b_cont->b_cont;
8654 	iocp = (struct iocblk *)mp->b_rptr;
8655 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8656 
8657 	/*
8658 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8659 	 * the user buffer address and length into which the list of struct
8660 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8661 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8662 	 * the SIOCGIFCONF operation was redefined to simply provide
8663 	 * a large output buffer into which we are supposed to jam the ifreq
8664 	 * array.  The same ioctl command code was used, despite the fact that
8665 	 * both the applications and the kernel code had to change, thus making
8666 	 * it impossible to support both interfaces.
8667 	 *
8668 	 * For reasons not good enough to try to explain, the following
8669 	 * algorithm is used for deciding what to do with one of these:
8670 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8671 	 * form with the output buffer coming down as the continuation message.
8672 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8673 	 * and we have to copy in the ifconf structure to find out how big the
8674 	 * output buffer is and where to copy out to.  Sure no problem...
8675 	 *
8676 	 */
8677 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8678 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8679 		int numifs = 0;
8680 		size_t ifc_bufsize;
8681 
8682 		/*
8683 		 * Must be (better be!) continuation of a TRANSPARENT
8684 		 * IOCTL.  We just copied in the ifconf structure.
8685 		 */
8686 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8687 		    (struct ifconf *)mp1->b_rptr);
8688 
8689 		/*
8690 		 * Allocate a buffer to hold requested information.
8691 		 *
8692 		 * If ifc_len is larger than what is needed, we only
8693 		 * allocate what we will use.
8694 		 *
8695 		 * If ifc_len is smaller than what is needed, return
8696 		 * EINVAL.
8697 		 *
8698 		 * XXX: the ill_t structure can hava 2 counters, for
8699 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8700 		 * number of interfaces for a device, so we don't need
8701 		 * to count them here...
8702 		 */
8703 		numifs = ip_get_numifs(zoneid);
8704 
8705 		ifclen = STRUCT_FGET(ifc, ifc_len);
8706 		ifc_bufsize = numifs * sizeof (struct ifreq);
8707 		if (ifc_bufsize > ifclen) {
8708 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8709 				/* old behaviour */
8710 				return (EINVAL);
8711 			} else {
8712 				ifc_bufsize = ifclen;
8713 			}
8714 		}
8715 
8716 		mp1 = mi_copyout_alloc(q, mp,
8717 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8718 		if (mp1 == NULL)
8719 			return (ENOMEM);
8720 
8721 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8722 	}
8723 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8724 	/*
8725 	 * the SIOCGIFCONF ioctl only knows about
8726 	 * IPv4 addresses, so don't try to tell
8727 	 * it about interfaces with IPv6-only
8728 	 * addresses. (Last parm 'isv6' is B_FALSE)
8729 	 */
8730 
8731 	ifr = (struct ifreq *)mp1->b_rptr;
8732 
8733 	rw_enter(&ill_g_lock, RW_READER);
8734 	ill = ILL_START_WALK_V4(&ctx);
8735 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8736 		for (ipif = ill->ill_ipif; ipif != NULL;
8737 		    ipif = ipif->ipif_next) {
8738 			if (zoneid != ipif->ipif_zoneid &&
8739 			    ipif->ipif_zoneid != ALL_ZONES)
8740 				continue;
8741 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8742 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8743 					/* old behaviour */
8744 					rw_exit(&ill_g_lock);
8745 					return (EINVAL);
8746 				} else {
8747 					goto if_copydone;
8748 				}
8749 			}
8750 			(void) ipif_get_name(ipif,
8751 			    ifr->ifr_name,
8752 			    sizeof (ifr->ifr_name));
8753 			sin = (sin_t *)&ifr->ifr_addr;
8754 			*sin = sin_null;
8755 			sin->sin_family = AF_INET;
8756 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8757 			ifr++;
8758 		}
8759 	}
8760 if_copydone:
8761 	rw_exit(&ill_g_lock);
8762 	mp1->b_wptr = (uchar_t *)ifr;
8763 
8764 	if (STRUCT_BUF(ifc) != NULL) {
8765 		STRUCT_FSET(ifc, ifc_len,
8766 			(int)((uchar_t *)ifr - mp1->b_rptr));
8767 	}
8768 	return (0);
8769 }
8770 
8771 /*
8772  * Get the interfaces using the address hosted on the interface passed in,
8773  * as a source adddress
8774  */
8775 /* ARGSUSED */
8776 int
8777 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8778     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8779 {
8780 	mblk_t *mp1;
8781 	ill_t	*ill, *ill_head;
8782 	ipif_t	*ipif, *orig_ipif;
8783 	int	numlifs = 0;
8784 	size_t	lifs_bufsize, lifsmaxlen;
8785 	struct	lifreq *lifr;
8786 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8787 	uint_t	ifindex;
8788 	zoneid_t zoneid;
8789 	int err = 0;
8790 	boolean_t isv6 = B_FALSE;
8791 	struct	sockaddr_in	*sin;
8792 	struct	sockaddr_in6	*sin6;
8793 
8794 	STRUCT_HANDLE(lifsrcof, lifs);
8795 
8796 	ASSERT(q->q_next == NULL);
8797 
8798 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8799 
8800 	/* Existence verified in ip_wput_nondata */
8801 	mp1 = mp->b_cont->b_cont;
8802 
8803 	/*
8804 	 * Must be (better be!) continuation of a TRANSPARENT
8805 	 * IOCTL.  We just copied in the lifsrcof structure.
8806 	 */
8807 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8808 	    (struct lifsrcof *)mp1->b_rptr);
8809 
8810 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8811 		return (EINVAL);
8812 
8813 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8814 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8815 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8816 	    ip_process_ioctl, &err);
8817 	if (ipif == NULL) {
8818 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8819 		    ifindex));
8820 		return (err);
8821 	}
8822 
8823 
8824 	/* Allocate a buffer to hold requested information */
8825 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8826 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8827 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8828 	/* The actual size needed is always returned in lifs_len */
8829 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8830 
8831 	/* If the amount we need is more than what is passed in, abort */
8832 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8833 		ipif_refrele(ipif);
8834 		return (0);
8835 	}
8836 
8837 	mp1 = mi_copyout_alloc(q, mp,
8838 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8839 	if (mp1 == NULL) {
8840 		ipif_refrele(ipif);
8841 		return (ENOMEM);
8842 	}
8843 
8844 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8845 	bzero(mp1->b_rptr, lifs_bufsize);
8846 
8847 	lifr = (struct lifreq *)mp1->b_rptr;
8848 
8849 	ill = ill_head = ipif->ipif_ill;
8850 	orig_ipif = ipif;
8851 
8852 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8853 	rw_enter(&ill_g_usesrc_lock, RW_READER);
8854 	rw_enter(&ill_g_lock, RW_READER);
8855 
8856 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8857 	for (; (ill != NULL) && (ill != ill_head);
8858 	    ill = ill->ill_usesrc_grp_next) {
8859 
8860 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8861 			break;
8862 
8863 		ipif = ill->ill_ipif;
8864 		(void) ipif_get_name(ipif,
8865 		    lifr->lifr_name, sizeof (lifr->lifr_name));
8866 		if (ipif->ipif_isv6) {
8867 			sin6 = (sin6_t *)&lifr->lifr_addr;
8868 			*sin6 = sin6_null;
8869 			sin6->sin6_family = AF_INET6;
8870 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8871 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8872 			    &ipif->ipif_v6net_mask);
8873 		} else {
8874 			sin = (sin_t *)&lifr->lifr_addr;
8875 			*sin = sin_null;
8876 			sin->sin_family = AF_INET;
8877 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8878 			lifr->lifr_addrlen = ip_mask_to_plen(
8879 			    ipif->ipif_net_mask);
8880 		}
8881 		lifr++;
8882 	}
8883 	rw_exit(&ill_g_usesrc_lock);
8884 	rw_exit(&ill_g_lock);
8885 	ipif_refrele(orig_ipif);
8886 	mp1->b_wptr = (uchar_t *)lifr;
8887 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8888 
8889 	return (0);
8890 }
8891 
8892 /* ARGSUSED */
8893 int
8894 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8895     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8896 {
8897 	mblk_t *mp1;
8898 	int	list;
8899 	ill_t	*ill;
8900 	ipif_t	*ipif;
8901 	int	flags;
8902 	int	numlifs = 0;
8903 	size_t	lifc_bufsize;
8904 	struct	lifreq *lifr;
8905 	sa_family_t	family;
8906 	struct	sockaddr_in	*sin;
8907 	struct	sockaddr_in6	*sin6;
8908 	ill_walk_context_t	ctx;
8909 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8910 	int32_t	lifclen;
8911 	zoneid_t zoneid;
8912 	STRUCT_HANDLE(lifconf, lifc);
8913 
8914 	ip1dbg(("ip_sioctl_get_lifconf"));
8915 
8916 	ASSERT(q->q_next == NULL);
8917 
8918 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8919 
8920 	/* Existence verified in ip_wput_nondata */
8921 	mp1 = mp->b_cont->b_cont;
8922 
8923 	/*
8924 	 * An extended version of SIOCGIFCONF that takes an
8925 	 * additional address family and flags field.
8926 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8927 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8928 	 * interfaces are omitted.
8929 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8930 	 * unless LIFC_TEMPORARY is specified.
8931 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8932 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8933 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8934 	 * has priority over LIFC_NOXMIT.
8935 	 */
8936 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8937 
8938 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8939 		return (EINVAL);
8940 
8941 	/*
8942 	 * Must be (better be!) continuation of a TRANSPARENT
8943 	 * IOCTL.  We just copied in the lifconf structure.
8944 	 */
8945 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8946 
8947 	family = STRUCT_FGET(lifc, lifc_family);
8948 	flags = STRUCT_FGET(lifc, lifc_flags);
8949 
8950 	switch (family) {
8951 	case AF_UNSPEC:
8952 		/*
8953 		 * walk all ILL's.
8954 		 */
8955 		list = MAX_G_HEADS;
8956 		break;
8957 	case AF_INET:
8958 		/*
8959 		 * walk only IPV4 ILL's.
8960 		 */
8961 		list = IP_V4_G_HEAD;
8962 		break;
8963 	case AF_INET6:
8964 		/*
8965 		 * walk only IPV6 ILL's.
8966 		 */
8967 		list = IP_V6_G_HEAD;
8968 		break;
8969 	default:
8970 		return (EAFNOSUPPORT);
8971 	}
8972 
8973 	/*
8974 	 * Allocate a buffer to hold requested information.
8975 	 *
8976 	 * If lifc_len is larger than what is needed, we only
8977 	 * allocate what we will use.
8978 	 *
8979 	 * If lifc_len is smaller than what is needed, return
8980 	 * EINVAL.
8981 	 */
8982 	numlifs = ip_get_numlifs(family, flags, zoneid);
8983 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8984 	lifclen = STRUCT_FGET(lifc, lifc_len);
8985 	if (lifc_bufsize > lifclen) {
8986 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8987 			return (EINVAL);
8988 		else
8989 			lifc_bufsize = lifclen;
8990 	}
8991 
8992 	mp1 = mi_copyout_alloc(q, mp,
8993 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8994 	if (mp1 == NULL)
8995 		return (ENOMEM);
8996 
8997 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8998 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8999 
9000 	lifr = (struct lifreq *)mp1->b_rptr;
9001 
9002 	rw_enter(&ill_g_lock, RW_READER);
9003 	ill = ill_first(list, list, &ctx);
9004 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
9005 		for (ipif = ill->ill_ipif; ipif != NULL;
9006 		    ipif = ipif->ipif_next) {
9007 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
9008 			    !(flags & LIFC_NOXMIT))
9009 				continue;
9010 
9011 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
9012 			    !(flags & LIFC_TEMPORARY))
9013 				continue;
9014 
9015 			if (((ipif->ipif_flags &
9016 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
9017 			    IPIF_DEPRECATED)) ||
9018 			    (ill->ill_phyint->phyint_flags &
9019 			    PHYI_LOOPBACK) ||
9020 			    !(ipif->ipif_flags & IPIF_UP)) &&
9021 			    (flags & LIFC_EXTERNAL_SOURCE))
9022 				continue;
9023 
9024 			if (zoneid != ipif->ipif_zoneid &&
9025 			    ipif->ipif_zoneid != ALL_ZONES &&
9026 			    (zoneid != GLOBAL_ZONEID ||
9027 			    !(flags & LIFC_ALLZONES)))
9028 				continue;
9029 
9030 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
9031 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
9032 					rw_exit(&ill_g_lock);
9033 					return (EINVAL);
9034 				} else {
9035 					goto lif_copydone;
9036 				}
9037 			}
9038 
9039 			(void) ipif_get_name(ipif,
9040 				lifr->lifr_name,
9041 				sizeof (lifr->lifr_name));
9042 			if (ipif->ipif_isv6) {
9043 				sin6 = (sin6_t *)&lifr->lifr_addr;
9044 				*sin6 = sin6_null;
9045 				sin6->sin6_family = AF_INET6;
9046 				sin6->sin6_addr =
9047 				ipif->ipif_v6lcl_addr;
9048 				lifr->lifr_addrlen =
9049 				ip_mask_to_plen_v6(
9050 				    &ipif->ipif_v6net_mask);
9051 			} else {
9052 				sin = (sin_t *)&lifr->lifr_addr;
9053 				*sin = sin_null;
9054 				sin->sin_family = AF_INET;
9055 				sin->sin_addr.s_addr =
9056 				    ipif->ipif_lcl_addr;
9057 				lifr->lifr_addrlen =
9058 				    ip_mask_to_plen(
9059 				    ipif->ipif_net_mask);
9060 			}
9061 			lifr++;
9062 		}
9063 	}
9064 lif_copydone:
9065 	rw_exit(&ill_g_lock);
9066 
9067 	mp1->b_wptr = (uchar_t *)lifr;
9068 	if (STRUCT_BUF(lifc) != NULL) {
9069 		STRUCT_FSET(lifc, lifc_len,
9070 			(int)((uchar_t *)lifr - mp1->b_rptr));
9071 	}
9072 	return (0);
9073 }
9074 
9075 /* ARGSUSED */
9076 int
9077 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
9078     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9079 {
9080 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9081 	ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9082 	return (0);
9083 }
9084 
9085 static void
9086 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9087 {
9088 	ip6_asp_t *table;
9089 	size_t table_size;
9090 	mblk_t *data_mp;
9091 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9092 
9093 	/* These two ioctls are I_STR only */
9094 	if (iocp->ioc_count == TRANSPARENT) {
9095 		miocnak(q, mp, 0, EINVAL);
9096 		return;
9097 	}
9098 
9099 	data_mp = mp->b_cont;
9100 	if (data_mp == NULL) {
9101 		/* The user passed us a NULL argument */
9102 		table = NULL;
9103 		table_size = iocp->ioc_count;
9104 	} else {
9105 		/*
9106 		 * The user provided a table.  The stream head
9107 		 * may have copied in the user data in chunks,
9108 		 * so make sure everything is pulled up
9109 		 * properly.
9110 		 */
9111 		if (MBLKL(data_mp) < iocp->ioc_count) {
9112 			mblk_t *new_data_mp;
9113 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9114 			    NULL) {
9115 				miocnak(q, mp, 0, ENOMEM);
9116 				return;
9117 			}
9118 			freemsg(data_mp);
9119 			data_mp = new_data_mp;
9120 			mp->b_cont = data_mp;
9121 		}
9122 		table = (ip6_asp_t *)data_mp->b_rptr;
9123 		table_size = iocp->ioc_count;
9124 	}
9125 
9126 	switch (iocp->ioc_cmd) {
9127 	case SIOCGIP6ADDRPOLICY:
9128 		iocp->ioc_rval = ip6_asp_get(table, table_size);
9129 		if (iocp->ioc_rval == -1)
9130 			iocp->ioc_error = EINVAL;
9131 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9132 		else if (table != NULL &&
9133 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9134 			ip6_asp_t *src = table;
9135 			ip6_asp32_t *dst = (void *)table;
9136 			int count = table_size / sizeof (ip6_asp_t);
9137 			int i;
9138 
9139 			/*
9140 			 * We need to do an in-place shrink of the array
9141 			 * to match the alignment attributes of the
9142 			 * 32-bit ABI looking at it.
9143 			 */
9144 			/* LINTED: logical expression always true: op "||" */
9145 			ASSERT(sizeof (*src) > sizeof (*dst));
9146 			for (i = 1; i < count; i++)
9147 				bcopy(src + i, dst + i, sizeof (*dst));
9148 		}
9149 #endif
9150 		break;
9151 
9152 	case SIOCSIP6ADDRPOLICY:
9153 		ASSERT(mp->b_prev == NULL);
9154 		mp->b_prev = (void *)q;
9155 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9156 		/*
9157 		 * We pass in the datamodel here so that the ip6_asp_replace()
9158 		 * routine can handle converting from 32-bit to native formats
9159 		 * where necessary.
9160 		 *
9161 		 * A better way to handle this might be to convert the inbound
9162 		 * data structure here, and hang it off a new 'mp'; thus the
9163 		 * ip6_asp_replace() logic would always be dealing with native
9164 		 * format data structures..
9165 		 *
9166 		 * (An even simpler way to handle these ioctls is to just
9167 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9168 		 * and just recompile everything that depends on it.)
9169 		 */
9170 #endif
9171 		ip6_asp_replace(mp, table, table_size, B_FALSE,
9172 		    iocp->ioc_flag & IOC_MODELS);
9173 		return;
9174 	}
9175 
9176 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9177 	qreply(q, mp);
9178 }
9179 
9180 static void
9181 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9182 {
9183 	mblk_t 		*data_mp;
9184 	struct dstinforeq	*dir;
9185 	uint8_t		*end, *cur;
9186 	in6_addr_t	*daddr, *saddr;
9187 	ipaddr_t	v4daddr;
9188 	ire_t		*ire;
9189 	char		*slabel, *dlabel;
9190 	boolean_t	isipv4;
9191 	int		match_ire;
9192 	ill_t		*dst_ill;
9193 	ipif_t		*src_ipif, *ire_ipif;
9194 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9195 	zoneid_t	zoneid;
9196 
9197 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9198 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9199 
9200 	/*
9201 	 * This ioctl is I_STR only, and must have a
9202 	 * data mblk following the M_IOCTL mblk.
9203 	 */
9204 	data_mp = mp->b_cont;
9205 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9206 		miocnak(q, mp, 0, EINVAL);
9207 		return;
9208 	}
9209 
9210 	if (MBLKL(data_mp) < iocp->ioc_count) {
9211 		mblk_t *new_data_mp;
9212 
9213 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9214 			miocnak(q, mp, 0, ENOMEM);
9215 			return;
9216 		}
9217 		freemsg(data_mp);
9218 		data_mp = new_data_mp;
9219 		mp->b_cont = data_mp;
9220 	}
9221 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9222 
9223 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9224 	    end - cur >= sizeof (struct dstinforeq);
9225 	    cur += sizeof (struct dstinforeq)) {
9226 		dir = (struct dstinforeq *)cur;
9227 		daddr = &dir->dir_daddr;
9228 		saddr = &dir->dir_saddr;
9229 
9230 		/*
9231 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9232 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9233 		 * and ipif_select_source[_v6]() do not.
9234 		 */
9235 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9236 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence);
9237 
9238 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9239 		if (isipv4) {
9240 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9241 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9242 			    0, NULL, NULL, zoneid, 0, NULL, match_ire);
9243 		} else {
9244 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9245 			    0, NULL, NULL, zoneid, 0, NULL, match_ire);
9246 		}
9247 		if (ire == NULL) {
9248 			dir->dir_dreachable = 0;
9249 
9250 			/* move on to next dst addr */
9251 			continue;
9252 		}
9253 		dir->dir_dreachable = 1;
9254 
9255 		ire_ipif = ire->ire_ipif;
9256 		if (ire_ipif == NULL)
9257 			goto next_dst;
9258 
9259 		/*
9260 		 * We expect to get back an interface ire or a
9261 		 * gateway ire cache entry.  For both types, the
9262 		 * output interface is ire_ipif->ipif_ill.
9263 		 */
9264 		dst_ill = ire_ipif->ipif_ill;
9265 		dir->dir_dmactype = dst_ill->ill_mactype;
9266 
9267 		if (isipv4) {
9268 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9269 		} else {
9270 			src_ipif = ipif_select_source_v6(dst_ill,
9271 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9272 			    zoneid);
9273 		}
9274 		if (src_ipif == NULL)
9275 			goto next_dst;
9276 
9277 		*saddr = src_ipif->ipif_v6lcl_addr;
9278 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9279 		slabel = ip6_asp_lookup(saddr, NULL);
9280 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9281 		dir->dir_sdeprecated =
9282 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9283 		ipif_refrele(src_ipif);
9284 next_dst:
9285 		ire_refrele(ire);
9286 	}
9287 	miocack(q, mp, iocp->ioc_count, 0);
9288 }
9289 
9290 
9291 /*
9292  * Check if this is an address assigned to this machine.
9293  * Skips interfaces that are down by using ire checks.
9294  * Translates mapped addresses to v4 addresses and then
9295  * treats them as such, returning true if the v4 address
9296  * associated with this mapped address is configured.
9297  * Note: Applications will have to be careful what they do
9298  * with the response; use of mapped addresses limits
9299  * what can be done with the socket, especially with
9300  * respect to socket options and ioctls - neither IPv4
9301  * options nor IPv6 sticky options/ancillary data options
9302  * may be used.
9303  */
9304 /* ARGSUSED */
9305 int
9306 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9307     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9308 {
9309 	struct sioc_addrreq *sia;
9310 	sin_t *sin;
9311 	ire_t *ire;
9312 	mblk_t *mp1;
9313 	zoneid_t zoneid;
9314 
9315 	ip1dbg(("ip_sioctl_tmyaddr"));
9316 
9317 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9318 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9319 
9320 	/* Existence verified in ip_wput_nondata */
9321 	mp1 = mp->b_cont->b_cont;
9322 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9323 	sin = (sin_t *)&sia->sa_addr;
9324 	switch (sin->sin_family) {
9325 	case AF_INET6: {
9326 		sin6_t *sin6 = (sin6_t *)sin;
9327 
9328 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9329 			ipaddr_t v4_addr;
9330 
9331 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9332 			    v4_addr);
9333 			ire = ire_ctable_lookup(v4_addr, 0,
9334 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9335 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
9336 		} else {
9337 			in6_addr_t v6addr;
9338 
9339 			v6addr = sin6->sin6_addr;
9340 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9341 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9342 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
9343 		}
9344 		break;
9345 	}
9346 	case AF_INET: {
9347 		ipaddr_t v4addr;
9348 
9349 		v4addr = sin->sin_addr.s_addr;
9350 		ire = ire_ctable_lookup(v4addr, 0,
9351 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9352 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
9353 		break;
9354 	}
9355 	default:
9356 		return (EAFNOSUPPORT);
9357 	}
9358 	if (ire != NULL) {
9359 		sia->sa_res = 1;
9360 		ire_refrele(ire);
9361 	} else {
9362 		sia->sa_res = 0;
9363 	}
9364 	return (0);
9365 }
9366 
9367 /*
9368  * Check if this is an address assigned on-link i.e. neighbor,
9369  * and makes sure it's reachable from the current zone.
9370  * Returns true for my addresses as well.
9371  * Translates mapped addresses to v4 addresses and then
9372  * treats them as such, returning true if the v4 address
9373  * associated with this mapped address is configured.
9374  * Note: Applications will have to be careful what they do
9375  * with the response; use of mapped addresses limits
9376  * what can be done with the socket, especially with
9377  * respect to socket options and ioctls - neither IPv4
9378  * options nor IPv6 sticky options/ancillary data options
9379  * may be used.
9380  */
9381 /* ARGSUSED */
9382 int
9383 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9384     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9385 {
9386 	struct sioc_addrreq *sia;
9387 	sin_t *sin;
9388 	mblk_t	*mp1;
9389 	ire_t *ire = NULL;
9390 	zoneid_t zoneid;
9391 
9392 	ip1dbg(("ip_sioctl_tonlink"));
9393 
9394 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9395 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9396 
9397 	/* Existence verified in ip_wput_nondata */
9398 	mp1 = mp->b_cont->b_cont;
9399 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9400 	sin = (sin_t *)&sia->sa_addr;
9401 
9402 	/*
9403 	 * Match addresses with a zero gateway field to avoid
9404 	 * routes going through a router.
9405 	 * Exclude broadcast and multicast addresses.
9406 	 */
9407 	switch (sin->sin_family) {
9408 	case AF_INET6: {
9409 		sin6_t *sin6 = (sin6_t *)sin;
9410 
9411 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9412 			ipaddr_t v4_addr;
9413 
9414 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9415 			    v4_addr);
9416 			if (!CLASSD(v4_addr)) {
9417 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9418 				    NULL, NULL, zoneid, NULL,
9419 				    MATCH_IRE_GW);
9420 			}
9421 		} else {
9422 			in6_addr_t v6addr;
9423 			in6_addr_t v6gw;
9424 
9425 			v6addr = sin6->sin6_addr;
9426 			v6gw = ipv6_all_zeros;
9427 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9428 				ire = ire_route_lookup_v6(&v6addr, 0,
9429 				    &v6gw, 0, NULL, NULL, zoneid,
9430 				    NULL, MATCH_IRE_GW);
9431 			}
9432 		}
9433 		break;
9434 	}
9435 	case AF_INET: {
9436 		ipaddr_t v4addr;
9437 
9438 		v4addr = sin->sin_addr.s_addr;
9439 		if (!CLASSD(v4addr)) {
9440 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9441 			    NULL, NULL, zoneid, NULL,
9442 			    MATCH_IRE_GW);
9443 		}
9444 		break;
9445 	}
9446 	default:
9447 		return (EAFNOSUPPORT);
9448 	}
9449 	sia->sa_res = 0;
9450 	if (ire != NULL) {
9451 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9452 		    IRE_LOCAL|IRE_LOOPBACK)) {
9453 			sia->sa_res = 1;
9454 		}
9455 		ire_refrele(ire);
9456 	}
9457 	return (0);
9458 }
9459 
9460 /*
9461  * TBD: implement when kernel maintaines a list of site prefixes.
9462  */
9463 /* ARGSUSED */
9464 int
9465 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9466     ip_ioctl_cmd_t *ipip, void *ifreq)
9467 {
9468 	return (ENXIO);
9469 }
9470 
9471 /* ARGSUSED */
9472 int
9473 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9474     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9475 {
9476 	ill_t  		*ill;
9477 	mblk_t		*mp1;
9478 	conn_t		*connp;
9479 	boolean_t	success;
9480 
9481 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9482 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9483 	/* ioctl comes down on an conn */
9484 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9485 	connp = Q_TO_CONN(q);
9486 
9487 	mp->b_datap->db_type = M_IOCTL;
9488 
9489 	/*
9490 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9491 	 * The original mp contains contaminated b_next values due to 'mi',
9492 	 * which is needed to do the mi_copy_done. Unfortunately if we
9493 	 * send down the original mblk itself and if we are popped due to an
9494 	 * an unplumb before the response comes back from tunnel,
9495 	 * the streamhead (which does a freemsg) will see this contaminated
9496 	 * message and the assertion in freemsg about non-null b_next/b_prev
9497 	 * will panic a DEBUG kernel.
9498 	 */
9499 	mp1 = copymsg(mp);
9500 	if (mp1 == NULL)
9501 		return (ENOMEM);
9502 
9503 	ill = ipif->ipif_ill;
9504 	mutex_enter(&connp->conn_lock);
9505 	mutex_enter(&ill->ill_lock);
9506 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9507 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9508 		    mp, 0);
9509 	} else {
9510 		success = ill_pending_mp_add(ill, connp, mp);
9511 	}
9512 	mutex_exit(&ill->ill_lock);
9513 	mutex_exit(&connp->conn_lock);
9514 
9515 	if (success) {
9516 		ip1dbg(("sending down tunparam request "));
9517 		putnext(ill->ill_wq, mp1);
9518 		return (EINPROGRESS);
9519 	} else {
9520 		/* The conn has started closing */
9521 		freemsg(mp1);
9522 		return (EINTR);
9523 	}
9524 }
9525 
9526 static int
9527 ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp, sin_t *sin,
9528     boolean_t x_arp_ioctl, boolean_t if_arp_ioctl)
9529 {
9530 	mblk_t *mp1;
9531 	mblk_t *mp2;
9532 	mblk_t *pending_mp;
9533 	ipaddr_t ipaddr;
9534 	area_t *area;
9535 	struct iocblk *iocp;
9536 	conn_t *connp;
9537 	struct arpreq *ar;
9538 	struct xarpreq *xar;
9539 	boolean_t success;
9540 	int flags, alength;
9541 	char *lladdr;
9542 
9543 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9544 	connp = Q_TO_CONN(q);
9545 
9546 	iocp = (struct iocblk *)mp->b_rptr;
9547 	/*
9548 	 * ill has already been set depending on whether
9549 	 * bsd style or interface style ioctl.
9550 	 */
9551 	ASSERT(ill != NULL);
9552 
9553 	/*
9554 	 * Is this one of the new SIOC*XARP ioctls?
9555 	 */
9556 	if (x_arp_ioctl) {
9557 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9558 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9559 		ar = NULL;
9560 
9561 		flags = xar->xarp_flags;
9562 		lladdr = LLADDR(&xar->xarp_ha);
9563 		/*
9564 		 * Validate against user's link layer address length
9565 		 * input and name and addr length limits.
9566 		 */
9567 		alength = ill->ill_phys_addr_length;
9568 		if (iocp->ioc_cmd == SIOCSXARP) {
9569 			if (alength != xar->xarp_ha.sdl_alen ||
9570 			    (alength + xar->xarp_ha.sdl_nlen >
9571 			    sizeof (xar->xarp_ha.sdl_data)))
9572 				return (EINVAL);
9573 		}
9574 	} else {
9575 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9576 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9577 		xar = NULL;
9578 
9579 		flags = ar->arp_flags;
9580 		lladdr = ar->arp_ha.sa_data;
9581 		/*
9582 		 * Theoretically, the sa_family could tell us what link
9583 		 * layer type this operation is trying to deal with. By
9584 		 * common usage AF_UNSPEC means ethernet. We'll assume
9585 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9586 		 * for now. Our new SIOC*XARP ioctls can be used more
9587 		 * generally.
9588 		 *
9589 		 * If the underlying media happens to have a non 6 byte
9590 		 * address, arp module will fail set/get, but the del
9591 		 * operation will succeed.
9592 		 */
9593 		alength = 6;
9594 		if ((iocp->ioc_cmd != SIOCDARP) &&
9595 		    (alength != ill->ill_phys_addr_length)) {
9596 			return (EINVAL);
9597 		}
9598 	}
9599 
9600 	/*
9601 	 * We are going to pass up to ARP a packet chain that looks
9602 	 * like:
9603 	 *
9604 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9605 	 *
9606 	 * Get a copy of the original IOCTL mblk to head the chain,
9607 	 * to be sent up (in mp1). Also get another copy to store
9608 	 * in the ill_pending_mp list, for matching the response
9609 	 * when it comes back from ARP.
9610 	 */
9611 	mp1 = copyb(mp);
9612 	pending_mp = copymsg(mp);
9613 	if (mp1 == NULL || pending_mp == NULL) {
9614 		if (mp1 != NULL)
9615 			freeb(mp1);
9616 		if (pending_mp != NULL)
9617 			inet_freemsg(pending_mp);
9618 		return (ENOMEM);
9619 	}
9620 
9621 	ipaddr = sin->sin_addr.s_addr;
9622 
9623 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9624 	    (caddr_t)&ipaddr);
9625 	if (mp2 == NULL) {
9626 		freeb(mp1);
9627 		inet_freemsg(pending_mp);
9628 		return (ENOMEM);
9629 	}
9630 	/* Put together the chain. */
9631 	mp1->b_cont = mp2;
9632 	mp1->b_datap->db_type = M_IOCTL;
9633 	mp2->b_cont = mp;
9634 	mp2->b_datap->db_type = M_DATA;
9635 
9636 	iocp = (struct iocblk *)mp1->b_rptr;
9637 
9638 	/*
9639 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9640 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9641 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9642 	 * ioc_count field; set ioc_count to be correct.
9643 	 */
9644 	iocp->ioc_count = MBLKL(mp1->b_cont);
9645 
9646 	/*
9647 	 * Set the proper command in the ARP message.
9648 	 * Convert the SIOC{G|S|D}ARP calls into our
9649 	 * AR_ENTRY_xxx calls.
9650 	 */
9651 	area = (area_t *)mp2->b_rptr;
9652 	switch (iocp->ioc_cmd) {
9653 	case SIOCDARP:
9654 	case SIOCDXARP:
9655 		/*
9656 		 * We defer deleting the corresponding IRE until
9657 		 * we return from arp.
9658 		 */
9659 		area->area_cmd = AR_ENTRY_DELETE;
9660 		area->area_proto_mask_offset = 0;
9661 		break;
9662 	case SIOCGARP:
9663 	case SIOCGXARP:
9664 		area->area_cmd = AR_ENTRY_SQUERY;
9665 		area->area_proto_mask_offset = 0;
9666 		break;
9667 	case SIOCSARP:
9668 	case SIOCSXARP: {
9669 		/*
9670 		 * Delete the corresponding ire to make sure IP will
9671 		 * pick up any change from arp.
9672 		 */
9673 		if (!if_arp_ioctl) {
9674 			(void) ip_ire_clookup_and_delete(ipaddr, NULL);
9675 			break;
9676 		} else {
9677 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9678 			if (ipif != NULL) {
9679 				(void) ip_ire_clookup_and_delete(ipaddr, ipif);
9680 				ipif_refrele(ipif);
9681 			}
9682 			break;
9683 		}
9684 	}
9685 	}
9686 	iocp->ioc_cmd = area->area_cmd;
9687 
9688 	/*
9689 	 * Before sending 'mp' to ARP, we have to clear the b_next
9690 	 * and b_prev. Otherwise if STREAMS encounters such a message
9691 	 * in freemsg(), (because ARP can close any time) it can cause
9692 	 * a panic. But mi code needs the b_next and b_prev values of
9693 	 * mp->b_cont, to complete the ioctl. So we store it here
9694 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9695 	 * when the response comes down from ARP.
9696 	 */
9697 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9698 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9699 	mp->b_cont->b_next = NULL;
9700 	mp->b_cont->b_prev = NULL;
9701 
9702 	mutex_enter(&connp->conn_lock);
9703 	mutex_enter(&ill->ill_lock);
9704 	/* conn has not yet started closing, hence this can't fail */
9705 	success = ill_pending_mp_add(ill, connp, pending_mp);
9706 	ASSERT(success);
9707 	mutex_exit(&ill->ill_lock);
9708 	mutex_exit(&connp->conn_lock);
9709 
9710 	/*
9711 	 * Fill in the rest of the ARP operation fields.
9712 	 */
9713 	area->area_hw_addr_length = alength;
9714 	bcopy(lladdr,
9715 	    (char *)area + area->area_hw_addr_offset,
9716 	    area->area_hw_addr_length);
9717 	/* Translate the flags. */
9718 	if (flags & ATF_PERM)
9719 		area->area_flags |= ACE_F_PERMANENT;
9720 	if (flags & ATF_PUBL)
9721 		area->area_flags |= ACE_F_PUBLISH;
9722 	if (flags & ATF_AUTHORITY)
9723 		area->area_flags |= ACE_F_AUTHORITY;
9724 
9725 	/*
9726 	 * Up to ARP it goes.  The response will come
9727 	 * back in ip_wput as an M_IOCACK message, and
9728 	 * will be handed to ip_sioctl_iocack for
9729 	 * completion.
9730 	 */
9731 	putnext(ill->ill_rq, mp1);
9732 	return (EINPROGRESS);
9733 }
9734 
9735 /* ARGSUSED */
9736 int
9737 ip_sioctl_xarp(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9738     ip_ioctl_cmd_t *ipip, void *ifreq)
9739 {
9740 	struct xarpreq *xar;
9741 	boolean_t isv6;
9742 	mblk_t	*mp1;
9743 	int	err;
9744 	conn_t	*connp;
9745 	int ifnamelen;
9746 	ire_t	*ire = NULL;
9747 	ill_t	*ill = NULL;
9748 	struct sockaddr_in *sin;
9749 	boolean_t if_arp_ioctl = B_FALSE;
9750 
9751 	/* ioctl comes down on an conn */
9752 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9753 	connp = Q_TO_CONN(q);
9754 	isv6 = connp->conn_af_isv6;
9755 
9756 	/* Existance verified in ip_wput_nondata */
9757 	mp1 = mp->b_cont->b_cont;
9758 
9759 	ASSERT(MBLKL(mp1) >= sizeof (*xar));
9760 	xar = (struct xarpreq *)mp1->b_rptr;
9761 	sin = (sin_t *)&xar->xarp_pa;
9762 
9763 	if (isv6 || (xar->xarp_ha.sdl_family != AF_LINK) ||
9764 	    (xar->xarp_pa.ss_family != AF_INET))
9765 		return (ENXIO);
9766 
9767 	ifnamelen = xar->xarp_ha.sdl_nlen;
9768 	if (ifnamelen != 0) {
9769 		char	*cptr, cval;
9770 
9771 		if (ifnamelen >= LIFNAMSIZ)
9772 			return (EINVAL);
9773 
9774 		/*
9775 		 * Instead of bcopying a bunch of bytes,
9776 		 * null-terminate the string in-situ.
9777 		 */
9778 		cptr = xar->xarp_ha.sdl_data + ifnamelen;
9779 		cval = *cptr;
9780 		*cptr = '\0';
9781 		ill = ill_lookup_on_name(xar->xarp_ha.sdl_data,
9782 		    B_FALSE, isv6, CONNP_TO_WQ(connp), mp, ip_process_ioctl,
9783 		    &err, NULL);
9784 		*cptr = cval;
9785 		if (ill == NULL)
9786 			return (err);
9787 		if (ill->ill_net_type != IRE_IF_RESOLVER) {
9788 			ill_refrele(ill);
9789 			return (ENXIO);
9790 		}
9791 
9792 		if_arp_ioctl = B_TRUE;
9793 	} else {
9794 		/*
9795 		 * PSARC 2003/088 states that if sdl_nlen == 0, it behaves
9796 		 * as an extended BSD ioctl. The kernel uses the IP address
9797 		 * to figure out the network interface.
9798 		 */
9799 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL);
9800 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9801 		    ((ill = ire_to_ill(ire)) == NULL) ||
9802 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9803 			if (ire != NULL)
9804 				ire_refrele(ire);
9805 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9806 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9807 			    NULL, MATCH_IRE_TYPE);
9808 			if ((ire == NULL) ||
9809 			    ((ill = ire_to_ill(ire)) == NULL)) {
9810 				if (ire != NULL)
9811 					ire_refrele(ire);
9812 				return (ENXIO);
9813 			}
9814 		}
9815 		ASSERT(ire != NULL && ill != NULL);
9816 	}
9817 
9818 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_TRUE, if_arp_ioctl);
9819 	if (if_arp_ioctl)
9820 		ill_refrele(ill);
9821 	if (ire != NULL)
9822 		ire_refrele(ire);
9823 
9824 	return (err);
9825 }
9826 
9827 /*
9828  * ARP IOCTLs.
9829  * How does IP get in the business of fronting ARP configuration/queries?
9830  * Well its like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9831  * are by tradition passed in through a datagram socket.  That lands in IP.
9832  * As it happens, this is just as well since the interface is quite crude in
9833  * that it passes in no information about protocol or hardware types, or
9834  * interface association.  After making the protocol assumption, IP is in
9835  * the position to look up the name of the ILL, which ARP will need, and
9836  * format a request that can be handled by ARP.	 The request is passed up
9837  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9838  * back a response.  ARP supports its own set of more general IOCTLs, in
9839  * case anyone is interested.
9840  */
9841 /* ARGSUSED */
9842 int
9843 ip_sioctl_arp(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9844     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9845 {
9846 	struct arpreq *ar;
9847 	struct sockaddr_in *sin;
9848 	ire_t	*ire;
9849 	boolean_t isv6;
9850 	mblk_t	*mp1;
9851 	int	err;
9852 	conn_t	*connp;
9853 	ill_t	*ill;
9854 
9855 	/* ioctl comes down on an conn */
9856 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9857 	connp = Q_TO_CONN(q);
9858 	isv6 = connp->conn_af_isv6;
9859 	if (isv6)
9860 		return (ENXIO);
9861 
9862 	/* Existance verified in ip_wput_nondata */
9863 	mp1 = mp->b_cont->b_cont;
9864 
9865 	ar = (struct arpreq *)mp1->b_rptr;
9866 	sin = (sin_t *)&ar->arp_pa;
9867 
9868 	/*
9869 	 * We need to let ARP know on which interface the IP
9870 	 * address has an ARP mapping. In the IPMP case, a
9871 	 * simple forwarding table lookup will return the
9872 	 * IRE_IF_RESOLVER for the first interface in the group,
9873 	 * which might not be the interface on which the
9874 	 * requested IP address was resolved due to the ill
9875 	 * selection algorithm (see ip_newroute_get_dst_ill()).
9876 	 * So we do a cache table lookup first: if the IRE cache
9877 	 * entry for the IP address is still there, it will
9878 	 * contain the ill pointer for the right interface, so
9879 	 * we use that. If the cache entry has been flushed, we
9880 	 * fall back to the forwarding table lookup. This should
9881 	 * be rare enough since IRE cache entries have a longer
9882 	 * life expectancy than ARP cache entries.
9883 	 */
9884 	ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL);
9885 	if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9886 	    ((ill = ire_to_ill(ire)) == NULL)) {
9887 		if (ire != NULL)
9888 			ire_refrele(ire);
9889 		ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9890 		    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9891 		    NULL, MATCH_IRE_TYPE);
9892 		if ((ire == NULL) || ((ill = ire_to_ill(ire)) == NULL)) {
9893 			if (ire != NULL)
9894 				ire_refrele(ire);
9895 			return (ENXIO);
9896 		}
9897 	}
9898 	ASSERT(ire != NULL && ill != NULL);
9899 
9900 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_FALSE, B_FALSE);
9901 	ire_refrele(ire);
9902 	return (err);
9903 }
9904 
9905 /*
9906  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9907  * atomically set/clear the muxids. Also complete the ioctl by acking or
9908  * naking it.  Note that the code is structured such that the link type,
9909  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9910  * its clones use the persistent link, while pppd(1M) and perhaps many
9911  * other daemons may use non-persistent link.  When combined with some
9912  * ill_t states, linking and unlinking lower streams may be used as
9913  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9914  */
9915 /* ARGSUSED */
9916 void
9917 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9918 {
9919 	mblk_t *mp1;
9920 	mblk_t *mp2;
9921 	struct linkblk *li;
9922 	queue_t	*ipwq;
9923 	char	*name;
9924 	struct qinit *qinfo;
9925 	struct ipmx_s *ipmxp;
9926 	ill_t	*ill = NULL;
9927 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9928 	int	err = 0;
9929 	boolean_t	entered_ipsq = B_FALSE;
9930 	boolean_t islink;
9931 	queue_t *dwq = NULL;
9932 
9933 	ASSERT(iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_PUNLINK ||
9934 	    iocp->ioc_cmd == I_LINK || iocp->ioc_cmd == I_UNLINK);
9935 
9936 	islink = (iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_LINK) ?
9937 	    B_TRUE : B_FALSE;
9938 
9939 	mp1 = mp->b_cont;	/* This is the linkblk info */
9940 	li = (struct linkblk *)mp1->b_rptr;
9941 
9942 	/*
9943 	 * ARP has added this special mblk, and the utility is asking us
9944 	 * to perform consistency checks, and also atomically set the
9945 	 * muxid. Ifconfig is an example.  It achieves this by using
9946 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9947 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9948 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9949 	 * and other comments in this routine for more details.
9950 	 */
9951 	mp2 = mp1->b_cont;	/* This is added by ARP */
9952 
9953 	/*
9954 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9955 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9956 	 * get the special mblk above.  For backward compatibility, we just
9957 	 * return success.  The utility will use SIOCSLIFMUXID to store
9958 	 * the muxids.  This is not atomic, and can leave the streams
9959 	 * unplumbable if the utility is interrrupted, before it does the
9960 	 * SIOCSLIFMUXID.
9961 	 */
9962 	if (mp2 == NULL) {
9963 		/*
9964 		 * At this point we don't know whether or not this is the
9965 		 * IP module stream or the ARP device stream.  We need to
9966 		 * walk the lower stream in order to find this out, since
9967 		 * the capability negotiation is done only on the IP module
9968 		 * stream.  IP module instance is identified by the module
9969 		 * name IP, non-null q_next, and it's wput not being ip_lwput.
9970 		 * STREAMS ensures that the lower stream (l_qbot) will not
9971 		 * vanish until this ioctl completes. So we can safely walk
9972 		 * the stream or refer to the q_ptr.
9973 		 */
9974 		ipwq = li->l_qbot;
9975 		while (ipwq != NULL) {
9976 			qinfo = ipwq->q_qinfo;
9977 			name = qinfo->qi_minfo->mi_idname;
9978 			if (name != NULL && name[0] != NULL &&
9979 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
9980 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
9981 			    (ipwq->q_next != NULL)) {
9982 				break;
9983 			}
9984 			ipwq = ipwq->q_next;
9985 		}
9986 		/*
9987 		 * This looks like an IP module stream, so trigger
9988 		 * the capability reset or re-negotiation if necessary.
9989 		 */
9990 		if (ipwq != NULL) {
9991 			ill = ipwq->q_ptr;
9992 			ASSERT(ill != NULL);
9993 
9994 			if (ipsq == NULL) {
9995 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
9996 				    ip_sioctl_plink, NEW_OP, B_TRUE);
9997 				if (ipsq == NULL)
9998 					return;
9999 				entered_ipsq = B_TRUE;
10000 			}
10001 			ASSERT(IAM_WRITER_ILL(ill));
10002 			/*
10003 			 * Store the upper read queue of the module
10004 			 * immediately below IP, and count the total
10005 			 * number of lower modules.  Do this only
10006 			 * for I_PLINK or I_LINK event.
10007 			 */
10008 			ill->ill_lmod_rq = NULL;
10009 			ill->ill_lmod_cnt = 0;
10010 			if (islink && (dwq = ipwq->q_next) != NULL) {
10011 				ill->ill_lmod_rq = RD(dwq);
10012 
10013 				while (dwq != NULL) {
10014 					ill->ill_lmod_cnt++;
10015 					dwq = dwq->q_next;
10016 				}
10017 			}
10018 			/*
10019 			 * There's no point in resetting or re-negotiating if
10020 			 * we are not bound to the driver, so only do this if
10021 			 * the DLPI state is idle (up); we assume such state
10022 			 * since ill_ipif_up_count gets incremented in
10023 			 * ipif_up_done(), which is after we are bound to the
10024 			 * driver.  Note that in the case of logical
10025 			 * interfaces, IP won't rebind to the driver unless
10026 			 * the ill_ipif_up_count is 0, meaning that all other
10027 			 * IP interfaces (including the main ipif) are in the
10028 			 * down state.  Because of this, we use such counter
10029 			 * as an indicator, instead of relying on the IPIF_UP
10030 			 * flag, which is per ipif instance.
10031 			 */
10032 			if (ill->ill_ipif_up_count > 0) {
10033 				if (islink)
10034 					ill_capability_probe(ill);
10035 				else
10036 					ill_capability_reset(ill);
10037 			}
10038 		}
10039 		goto done;
10040 	}
10041 
10042 	/*
10043 	 * This is an I_{P}LINK sent down by ifconfig on
10044 	 * /dev/arp. ARP has appended this last (3rd) mblk,
10045 	 * giving more info. STREAMS ensures that the lower
10046 	 * stream (l_qbot) will not vanish until this ioctl
10047 	 * completes. So we can safely walk the stream or refer
10048 	 * to the q_ptr.
10049 	 */
10050 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
10051 	if (ipmxp->ipmx_arpdev_stream) {
10052 		/*
10053 		 * The operation is occuring on the arp-device
10054 		 * stream.
10055 		 */
10056 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
10057 		    q, mp, ip_sioctl_plink, &err, NULL);
10058 		if (ill == NULL) {
10059 			if (err == EINPROGRESS) {
10060 				return;
10061 			} else {
10062 				err = EINVAL;
10063 				goto done;
10064 			}
10065 		}
10066 
10067 		if (ipsq == NULL) {
10068 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
10069 			    NEW_OP, B_TRUE);
10070 			if (ipsq == NULL) {
10071 				ill_refrele(ill);
10072 				return;
10073 			}
10074 			entered_ipsq = B_TRUE;
10075 		}
10076 		ASSERT(IAM_WRITER_ILL(ill));
10077 		ill_refrele(ill);
10078 		/*
10079 		 * To ensure consistency between IP and ARP,
10080 		 * the following LIFO scheme is used in
10081 		 * plink/punlink. (IP first, ARP last).
10082 		 * This is because the muxid's are stored
10083 		 * in the IP stream on the ill.
10084 		 *
10085 		 * I_{P}LINK: ifconfig plinks the IP stream before
10086 		 * plinking the ARP stream. On an arp-dev
10087 		 * stream, IP checks that it is not yet
10088 		 * plinked, and it also checks that the
10089 		 * corresponding IP stream is already plinked.
10090 		 *
10091 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream
10092 		 * before punlinking the IP stream. IP does
10093 		 * not allow punlink of the IP stream unless
10094 		 * the arp stream has been punlinked.
10095 		 *
10096 		 */
10097 		if ((islink &&
10098 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
10099 		    (!islink &&
10100 		    ill->ill_arp_muxid != li->l_index)) {
10101 			err = EINVAL;
10102 			goto done;
10103 		}
10104 		if (islink) {
10105 			ill->ill_arp_muxid = li->l_index;
10106 		} else {
10107 			ill->ill_arp_muxid = 0;
10108 		}
10109 	} else {
10110 		/*
10111 		 * This must be the IP module stream with or
10112 		 * without arp. Walk the stream and locate the
10113 		 * IP module. An IP module instance is
10114 		 * identified by the module name IP, non-null
10115 		 * q_next, and it's wput not being ip_lwput.
10116 		 */
10117 		ipwq = li->l_qbot;
10118 		while (ipwq != NULL) {
10119 			qinfo = ipwq->q_qinfo;
10120 			name = qinfo->qi_minfo->mi_idname;
10121 			if (name != NULL && name[0] != NULL &&
10122 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
10123 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
10124 			    (ipwq->q_next != NULL)) {
10125 				break;
10126 			}
10127 			ipwq = ipwq->q_next;
10128 		}
10129 		if (ipwq != NULL) {
10130 			ill = ipwq->q_ptr;
10131 			ASSERT(ill != NULL);
10132 
10133 			if (ipsq == NULL) {
10134 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
10135 				    ip_sioctl_plink, NEW_OP, B_TRUE);
10136 				if (ipsq == NULL)
10137 					return;
10138 				entered_ipsq = B_TRUE;
10139 			}
10140 			ASSERT(IAM_WRITER_ILL(ill));
10141 			/*
10142 			 * Return error if the ip_mux_id is
10143 			 * non-zero and command is I_{P}LINK.
10144 			 * If command is I_{P}UNLINK, return
10145 			 * error if the arp-devstr is not
10146 			 * yet punlinked.
10147 			 */
10148 			if ((islink && ill->ill_ip_muxid != 0) ||
10149 			    (!islink && ill->ill_arp_muxid != 0)) {
10150 				err = EINVAL;
10151 				goto done;
10152 			}
10153 			ill->ill_lmod_rq = NULL;
10154 			ill->ill_lmod_cnt = 0;
10155 			if (islink) {
10156 				/*
10157 				 * Store the upper read queue of the module
10158 				 * immediately below IP, and count the total
10159 				 * number of lower modules.
10160 				 */
10161 				if ((dwq = ipwq->q_next) != NULL) {
10162 					ill->ill_lmod_rq = RD(dwq);
10163 
10164 					while (dwq != NULL) {
10165 						ill->ill_lmod_cnt++;
10166 						dwq = dwq->q_next;
10167 					}
10168 				}
10169 				ill->ill_ip_muxid = li->l_index;
10170 			} else {
10171 				ill->ill_ip_muxid = 0;
10172 			}
10173 
10174 			/*
10175 			 * See comments above about resetting/re-
10176 			 * negotiating driver sub-capabilities.
10177 			 */
10178 			if (ill->ill_ipif_up_count > 0) {
10179 				if (islink)
10180 					ill_capability_probe(ill);
10181 				else
10182 					ill_capability_reset(ill);
10183 			}
10184 		}
10185 	}
10186 done:
10187 	iocp->ioc_count = 0;
10188 	iocp->ioc_error = err;
10189 	if (err == 0)
10190 		mp->b_datap->db_type = M_IOCACK;
10191 	else
10192 		mp->b_datap->db_type = M_IOCNAK;
10193 	qreply(q, mp);
10194 
10195 	/* Conn was refheld in ip_sioctl_copyin_setup */
10196 	if (CONN_Q(q))
10197 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
10198 	if (entered_ipsq)
10199 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
10200 }
10201 
10202 /*
10203  * Search the ioctl command in the ioctl tables and return a pointer
10204  * to the ioctl command information. The ioctl command tables are
10205  * static and fully populated at compile time.
10206  */
10207 ip_ioctl_cmd_t *
10208 ip_sioctl_lookup(int ioc_cmd)
10209 {
10210 	int index;
10211 	ip_ioctl_cmd_t *ipip;
10212 	ip_ioctl_cmd_t *ipip_end;
10213 
10214 	if (ioc_cmd == IPI_DONTCARE)
10215 		return (NULL);
10216 
10217 	/*
10218 	 * Do a 2 step search. First search the indexed table
10219 	 * based on the least significant byte of the ioctl cmd.
10220 	 * If we don't find a match, then search the misc table
10221 	 * serially.
10222 	 */
10223 	index = ioc_cmd & 0xFF;
10224 	if (index < ip_ndx_ioctl_count) {
10225 		ipip = &ip_ndx_ioctl_table[index];
10226 		if (ipip->ipi_cmd == ioc_cmd) {
10227 			/* Found a match in the ndx table */
10228 			return (ipip);
10229 		}
10230 	}
10231 
10232 	/* Search the misc table */
10233 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10234 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10235 		if (ipip->ipi_cmd == ioc_cmd)
10236 			/* Found a match in the misc table */
10237 			return (ipip);
10238 	}
10239 
10240 	return (NULL);
10241 }
10242 
10243 /*
10244  * Wrapper function for resuming deferred ioctl processing
10245  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10246  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10247  */
10248 /* ARGSUSED */
10249 void
10250 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10251     void *dummy_arg)
10252 {
10253 	ip_sioctl_copyin_setup(q, mp);
10254 }
10255 
10256 /*
10257  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10258  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10259  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10260  * We establish here the size of the block to be copied in.  mi_copyin
10261  * arranges for this to happen, an processing continues in ip_wput with
10262  * an M_IOCDATA message.
10263  */
10264 void
10265 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10266 {
10267 	int	copyin_size;
10268 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10269 	ip_ioctl_cmd_t *ipip;
10270 	cred_t *cr;
10271 
10272 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10273 	if (ipip == NULL) {
10274 		/*
10275 		 * The ioctl is not one we understand or own.
10276 		 * Pass it along to be processed down stream,
10277 		 * if this is a module instance of IP, else nak
10278 		 * the ioctl.
10279 		 */
10280 		if (q->q_next == NULL) {
10281 			goto nak;
10282 		} else {
10283 			putnext(q, mp);
10284 			return;
10285 		}
10286 	}
10287 
10288 	/*
10289 	 * If this is deferred, then we will do all the checks when we
10290 	 * come back.
10291 	 */
10292 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10293 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup()) {
10294 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10295 		return;
10296 	}
10297 
10298 	/*
10299 	 * Only allow a very small subset of IP ioctls on this stream if
10300 	 * IP is a module and not a driver. Allowing ioctls to be processed
10301 	 * in this case may cause assert failures or data corruption.
10302 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10303 	 * ioctls allowed on an IP module stream, after which this stream
10304 	 * normally becomes a multiplexor (at which time the stream head
10305 	 * will fail all ioctls).
10306 	 */
10307 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10308 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10309 			/*
10310 			 * Pass common Streams ioctls which the IP
10311 			 * module does not own or consume along to
10312 			 * be processed down stream.
10313 			 */
10314 			putnext(q, mp);
10315 			return;
10316 		} else {
10317 			goto nak;
10318 		}
10319 	}
10320 
10321 	/* Make sure we have ioctl data to process. */
10322 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10323 		goto nak;
10324 
10325 	/*
10326 	 * Prefer dblk credential over ioctl credential; some synthesized
10327 	 * ioctls have kcred set because there's no way to crhold()
10328 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10329 	 * the framework; the caller of ioctl needs to hold the reference
10330 	 * for the duration of the call).
10331 	 */
10332 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10333 
10334 	/* Make sure normal users don't send down privileged ioctls */
10335 	if ((ipip->ipi_flags & IPI_PRIV) &&
10336 	    (cr != NULL) && secpolicy_net_config(cr, B_TRUE) != 0) {
10337 		/* We checked the privilege earlier but log it here */
10338 		miocnak(q, mp, 0, secpolicy_net_config(cr, B_FALSE));
10339 		return;
10340 	}
10341 
10342 	/*
10343 	 * The ioctl command tables can only encode fixed length
10344 	 * ioctl data. If the length is variable, the table will
10345 	 * encode the length as zero. Such special cases are handled
10346 	 * below in the switch.
10347 	 */
10348 	if (ipip->ipi_copyin_size != 0) {
10349 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10350 		return;
10351 	}
10352 
10353 	switch (iocp->ioc_cmd) {
10354 	case O_SIOCGIFCONF:
10355 	case SIOCGIFCONF:
10356 		/*
10357 		 * This IOCTL is hilarious.  See comments in
10358 		 * ip_sioctl_get_ifconf for the story.
10359 		 */
10360 		if (iocp->ioc_count == TRANSPARENT)
10361 			copyin_size = SIZEOF_STRUCT(ifconf,
10362 			    iocp->ioc_flag);
10363 		else
10364 			copyin_size = iocp->ioc_count;
10365 		mi_copyin(q, mp, NULL, copyin_size);
10366 		return;
10367 
10368 	case O_SIOCGLIFCONF:
10369 	case SIOCGLIFCONF:
10370 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10371 		mi_copyin(q, mp, NULL, copyin_size);
10372 		return;
10373 
10374 	case SIOCGLIFSRCOF:
10375 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10376 		mi_copyin(q, mp, NULL, copyin_size);
10377 		return;
10378 	case SIOCGIP6ADDRPOLICY:
10379 		ip_sioctl_ip6addrpolicy(q, mp);
10380 		ip6_asp_table_refrele();
10381 		return;
10382 
10383 	case SIOCSIP6ADDRPOLICY:
10384 		ip_sioctl_ip6addrpolicy(q, mp);
10385 		return;
10386 
10387 	case SIOCGDSTINFO:
10388 		ip_sioctl_dstinfo(q, mp);
10389 		ip6_asp_table_refrele();
10390 		return;
10391 
10392 	case I_PLINK:
10393 	case I_PUNLINK:
10394 	case I_LINK:
10395 	case I_UNLINK:
10396 		/*
10397 		 * We treat non-persistent link similarly as the persistent
10398 		 * link case, in terms of plumbing/unplumbing, as well as
10399 		 * dynamic re-plumbing events indicator.  See comments
10400 		 * in ip_sioctl_plink() for more.
10401 		 *
10402 		 * Request can be enqueued in the 'ipsq' while waiting
10403 		 * to become exclusive. So bump up the conn ref.
10404 		 */
10405 		if (CONN_Q(q))
10406 			CONN_INC_REF(Q_TO_CONN(q));
10407 		ip_sioctl_plink(NULL, q, mp, NULL);
10408 		return;
10409 
10410 	case ND_GET:
10411 	case ND_SET:
10412 		/*
10413 		 * Use of the nd table requires holding the reader lock.
10414 		 * Modifying the nd table thru nd_load/nd_unload requires
10415 		 * the writer lock.
10416 		 */
10417 		rw_enter(&ip_g_nd_lock, RW_READER);
10418 		if (nd_getset(q, ip_g_nd, mp)) {
10419 			rw_exit(&ip_g_nd_lock);
10420 
10421 			if (iocp->ioc_error)
10422 				iocp->ioc_count = 0;
10423 			mp->b_datap->db_type = M_IOCACK;
10424 			qreply(q, mp);
10425 			return;
10426 		}
10427 		rw_exit(&ip_g_nd_lock);
10428 		/*
10429 		 * We don't understand this subioctl of ND_GET / ND_SET.
10430 		 * Maybe intended for some driver / module below us
10431 		 */
10432 		if (q->q_next) {
10433 			putnext(q, mp);
10434 		} else {
10435 			iocp->ioc_error = ENOENT;
10436 			mp->b_datap->db_type = M_IOCNAK;
10437 			iocp->ioc_count = 0;
10438 			qreply(q, mp);
10439 		}
10440 		return;
10441 
10442 	case IP_IOCTL:
10443 		ip_wput_ioctl(q, mp);
10444 		return;
10445 	default:
10446 		cmn_err(CE_PANIC, "should not happen ");
10447 	}
10448 nak:
10449 	if (mp->b_cont != NULL) {
10450 		freemsg(mp->b_cont);
10451 		mp->b_cont = NULL;
10452 	}
10453 	iocp->ioc_error = EINVAL;
10454 	mp->b_datap->db_type = M_IOCNAK;
10455 	iocp->ioc_count = 0;
10456 	qreply(q, mp);
10457 }
10458 
10459 /* ip_wput hands off ARP IOCTL responses to us */
10460 void
10461 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10462 {
10463 	struct arpreq *ar;
10464 	struct xarpreq *xar;
10465 	area_t	*area;
10466 	mblk_t	*area_mp;
10467 	struct iocblk *iocp;
10468 	mblk_t	*orig_ioc_mp, *tmp;
10469 	struct iocblk	*orig_iocp;
10470 	ill_t *ill;
10471 	conn_t *connp = NULL;
10472 	uint_t ioc_id;
10473 	mblk_t *pending_mp;
10474 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10475 	int *flagsp;
10476 	char *storage = NULL;
10477 	sin_t *sin;
10478 	ipaddr_t addr;
10479 	int err;
10480 
10481 	ill = q->q_ptr;
10482 	ASSERT(ill != NULL);
10483 
10484 	/*
10485 	 * We should get back from ARP a packet chain that looks like:
10486 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10487 	 */
10488 	if (!(area_mp = mp->b_cont) ||
10489 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10490 	    !(orig_ioc_mp = area_mp->b_cont) ||
10491 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10492 		freemsg(mp);
10493 		return;
10494 	}
10495 
10496 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10497 
10498 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10499 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10500 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10501 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10502 		x_arp_ioctl = B_TRUE;
10503 		xar = (struct xarpreq *)tmp->b_rptr;
10504 		sin = (sin_t *)&xar->xarp_pa;
10505 		flagsp = &xar->xarp_flags;
10506 		storage = xar->xarp_ha.sdl_data;
10507 		if (xar->xarp_ha.sdl_nlen != 0)
10508 			ifx_arp_ioctl = B_TRUE;
10509 	} else {
10510 		ar = (struct arpreq *)tmp->b_rptr;
10511 		sin = (sin_t *)&ar->arp_pa;
10512 		flagsp = &ar->arp_flags;
10513 		storage = ar->arp_ha.sa_data;
10514 	}
10515 
10516 	iocp = (struct iocblk *)mp->b_rptr;
10517 
10518 	/*
10519 	 * Pick out the originating queue based on the ioc_id.
10520 	 */
10521 	ioc_id = iocp->ioc_id;
10522 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10523 	if (pending_mp == NULL) {
10524 		ASSERT(connp == NULL);
10525 		inet_freemsg(mp);
10526 		return;
10527 	}
10528 	ASSERT(connp != NULL);
10529 	q = CONNP_TO_WQ(connp);
10530 
10531 	/* Uncouple the internally generated IOCTL from the original one */
10532 	area = (area_t *)area_mp->b_rptr;
10533 	area_mp->b_cont = NULL;
10534 
10535 	/*
10536 	 * Restore the b_next and b_prev used by mi code. This is needed
10537 	 * to complete the ioctl using mi* functions. We stored them in
10538 	 * the pending mp prior to sending the request to ARP.
10539 	 */
10540 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10541 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10542 	inet_freemsg(pending_mp);
10543 
10544 	/*
10545 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10546 	 * Catch the case where there is an IRE_CACHE by no entry in the
10547 	 * arp table.
10548 	 */
10549 	addr = sin->sin_addr.s_addr;
10550 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10551 		ire_t			*ire;
10552 		dl_unitdata_req_t	*dlup;
10553 		mblk_t			*llmp;
10554 		int			addr_len;
10555 		ill_t			*ipsqill = NULL;
10556 
10557 		if (ifx_arp_ioctl) {
10558 			/*
10559 			 * There's no need to lookup the ill, since
10560 			 * we've already done that when we started
10561 			 * processing the ioctl and sent the message
10562 			 * to ARP on that ill.  So use the ill that
10563 			 * is stored in q->q_ptr.
10564 			 */
10565 			ipsqill = ill;
10566 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10567 			    ipsqill->ill_ipif, ALL_ZONES,
10568 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL);
10569 		} else {
10570 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10571 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
10572 			if (ire != NULL)
10573 				ipsqill = ire_to_ill(ire);
10574 		}
10575 
10576 		if ((x_arp_ioctl) && (ipsqill != NULL))
10577 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10578 
10579 		if (ire != NULL) {
10580 			/*
10581 			 * Since the ire obtained from cachetable is used for
10582 			 * mac addr copying below, treat an incomplete ire as if
10583 			 * as if we never found it.
10584 			 */
10585 			if (ire->ire_nce != NULL &&
10586 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10587 				ire_refrele(ire);
10588 				ire = NULL;
10589 				ipsqill = NULL;
10590 				goto errack;
10591 			}
10592 			*flagsp = ATF_INUSE;
10593 			llmp = (ire->ire_nce != NULL ?
10594 			    ire->ire_nce->nce_res_mp : NULL);
10595 			if (llmp != NULL && ipsqill != NULL) {
10596 				uchar_t *macaddr;
10597 
10598 				addr_len = ipsqill->ill_phys_addr_length;
10599 				if (x_arp_ioctl && ((addr_len +
10600 				    ipsqill->ill_name_length) >
10601 				    sizeof (xar->xarp_ha.sdl_data))) {
10602 					ire_refrele(ire);
10603 					freemsg(mp);
10604 					ip_ioctl_finish(q, orig_ioc_mp,
10605 					    EINVAL, NO_COPYOUT, NULL);
10606 					return;
10607 				}
10608 				*flagsp |= ATF_COM;
10609 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10610 				if (ipsqill->ill_sap_length < 0)
10611 					macaddr = llmp->b_rptr +
10612 					    dlup->dl_dest_addr_offset;
10613 				else
10614 					macaddr = llmp->b_rptr +
10615 					    dlup->dl_dest_addr_offset +
10616 					    ipsqill->ill_sap_length;
10617 				/*
10618 				 * For SIOCGARP, MAC address length
10619 				 * validation has already been done
10620 				 * before the ioctl was issued to ARP to
10621 				 * allow it to progress only on 6 byte
10622 				 * addressable (ethernet like) media. Thus
10623 				 * the mac address copying can not overwrite
10624 				 * the sa_data area below.
10625 				 */
10626 				bcopy(macaddr, storage, addr_len);
10627 			}
10628 			/* Ditch the internal IOCTL. */
10629 			freemsg(mp);
10630 			ire_refrele(ire);
10631 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10632 			return;
10633 		}
10634 	}
10635 
10636 	/*
10637 	 * Delete the coresponding IRE_CACHE if any.
10638 	 * Reset the error if there was one (in case there was no entry
10639 	 * in arp.)
10640 	 */
10641 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10642 		ipif_t *ipintf = NULL;
10643 
10644 		if (ifx_arp_ioctl) {
10645 			/*
10646 			 * There's no need to lookup the ill, since
10647 			 * we've already done that when we started
10648 			 * processing the ioctl and sent the message
10649 			 * to ARP on that ill.  So use the ill that
10650 			 * is stored in q->q_ptr.
10651 			 */
10652 			ipintf = ill->ill_ipif;
10653 		}
10654 		if (ip_ire_clookup_and_delete(addr, ipintf)) {
10655 			/*
10656 			 * The address in "addr" may be an entry for a
10657 			 * router. If that's true, then any off-net
10658 			 * IRE_CACHE entries that go through the router
10659 			 * with address "addr" must be clobbered. Use
10660 			 * ire_walk to achieve this goal.
10661 			 */
10662 			if (ifx_arp_ioctl)
10663 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10664 				    ire_delete_cache_gw, (char *)&addr, ill);
10665 			else
10666 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10667 				    ALL_ZONES);
10668 			iocp->ioc_error = 0;
10669 		}
10670 	}
10671 errack:
10672 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10673 		err = iocp->ioc_error;
10674 		freemsg(mp);
10675 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10676 		return;
10677 	}
10678 
10679 	/*
10680 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10681 	 * the area_t into the struct {x}arpreq.
10682 	 */
10683 	if (x_arp_ioctl) {
10684 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10685 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10686 		    sizeof (xar->xarp_ha.sdl_data)) {
10687 			freemsg(mp);
10688 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10689 			    NULL);
10690 			return;
10691 		}
10692 	}
10693 	*flagsp = ATF_INUSE;
10694 	if (area->area_flags & ACE_F_PERMANENT)
10695 		*flagsp |= ATF_PERM;
10696 	if (area->area_flags & ACE_F_PUBLISH)
10697 		*flagsp |= ATF_PUBL;
10698 	if (area->area_flags & ACE_F_AUTHORITY)
10699 		*flagsp |= ATF_AUTHORITY;
10700 	if (area->area_hw_addr_length != 0) {
10701 		*flagsp |= ATF_COM;
10702 		/*
10703 		 * For SIOCGARP, MAC address length validation has
10704 		 * already been done before the ioctl was issued to ARP
10705 		 * to allow it to progress only on 6 byte addressable
10706 		 * (ethernet like) media. Thus the mac address copying
10707 		 * can not overwrite the sa_data area below.
10708 		 */
10709 		bcopy((char *)area + area->area_hw_addr_offset,
10710 		    storage, area->area_hw_addr_length);
10711 	}
10712 
10713 	/* Ditch the internal IOCTL. */
10714 	freemsg(mp);
10715 	/* Complete the original. */
10716 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10717 }
10718 
10719 /*
10720  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10721  * interface) create the next available logical interface for this
10722  * physical interface.
10723  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10724  * ipif with the specified name.
10725  *
10726  * If the address family is not AF_UNSPEC then set the address as well.
10727  *
10728  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10729  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10730  *
10731  * Executed as a writer on the ill or ill group.
10732  * So no lock is needed to traverse the ipif chain, or examine the
10733  * phyint flags.
10734  */
10735 /* ARGSUSED */
10736 int
10737 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10738     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10739 {
10740 	mblk_t	*mp1;
10741 	struct lifreq *lifr;
10742 	boolean_t	isv6;
10743 	boolean_t	exists;
10744 	char 	*name;
10745 	char	*endp;
10746 	char	*cp;
10747 	int	namelen;
10748 	ipif_t	*ipif;
10749 	long	id;
10750 	ipsq_t	*ipsq;
10751 	ill_t	*ill;
10752 	sin_t	*sin;
10753 	int	err = 0;
10754 	boolean_t found_sep = B_FALSE;
10755 	conn_t	*connp;
10756 	zoneid_t zoneid;
10757 	int	orig_ifindex = 0;
10758 
10759 	ip1dbg(("ip_sioctl_addif\n"));
10760 	/* Existence of mp1 has been checked in ip_wput_nondata */
10761 	mp1 = mp->b_cont->b_cont;
10762 	/*
10763 	 * Null terminate the string to protect against buffer
10764 	 * overrun. String was generated by user code and may not
10765 	 * be trusted.
10766 	 */
10767 	lifr = (struct lifreq *)mp1->b_rptr;
10768 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10769 	name = lifr->lifr_name;
10770 	ASSERT(CONN_Q(q));
10771 	connp = Q_TO_CONN(q);
10772 	isv6 = connp->conn_af_isv6;
10773 	zoneid = connp->conn_zoneid;
10774 	namelen = mi_strlen(name);
10775 	if (namelen == 0)
10776 		return (EINVAL);
10777 
10778 	exists = B_FALSE;
10779 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10780 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10781 		/*
10782 		 * Allow creating lo0 using SIOCLIFADDIF.
10783 		 * can't be any other writer thread. So can pass null below
10784 		 * for the last 4 args to ipif_lookup_name.
10785 		 */
10786 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen,
10787 		    B_TRUE, &exists, isv6, zoneid, NULL, NULL, NULL, NULL);
10788 		/* Prevent any further action */
10789 		if (ipif == NULL) {
10790 			return (ENOBUFS);
10791 		} else if (!exists) {
10792 			/* We created the ipif now and as writer */
10793 			ipif_refrele(ipif);
10794 			return (0);
10795 		} else {
10796 			ill = ipif->ipif_ill;
10797 			ill_refhold(ill);
10798 			ipif_refrele(ipif);
10799 		}
10800 	} else {
10801 		/* Look for a colon in the name. */
10802 		endp = &name[namelen];
10803 		for (cp = endp; --cp > name; ) {
10804 			if (*cp == IPIF_SEPARATOR_CHAR) {
10805 				found_sep = B_TRUE;
10806 				/*
10807 				 * Reject any non-decimal aliases for plumbing
10808 				 * of logical interfaces. Aliases with leading
10809 				 * zeroes are also rejected as they introduce
10810 				 * ambiguity in the naming of the interfaces.
10811 				 * Comparing with "0" takes care of all such
10812 				 * cases.
10813 				 */
10814 				if ((strncmp("0", cp+1, 1)) == 0)
10815 					return (EINVAL);
10816 
10817 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10818 				    id <= 0 || *endp != '\0') {
10819 					return (EINVAL);
10820 				}
10821 				*cp = '\0';
10822 				break;
10823 			}
10824 		}
10825 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10826 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL);
10827 		if (found_sep)
10828 			*cp = IPIF_SEPARATOR_CHAR;
10829 		if (ill == NULL)
10830 			return (err);
10831 	}
10832 
10833 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10834 	    B_TRUE);
10835 
10836 	/*
10837 	 * Release the refhold due to the lookup, now that we are excl
10838 	 * or we are just returning
10839 	 */
10840 	ill_refrele(ill);
10841 
10842 	if (ipsq == NULL)
10843 		return (EINPROGRESS);
10844 
10845 	/*
10846 	 * If the interface is failed, inactive or offlined, look for a working
10847 	 * interface in the ill group and create the ipif there. If we can't
10848 	 * find a good interface, create the ipif anyway so that in.mpathd can
10849 	 * move it to the first repaired interface.
10850 	 */
10851 	if ((ill->ill_phyint->phyint_flags &
10852 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10853 	    ill->ill_phyint->phyint_groupname_len != 0) {
10854 		phyint_t *phyi;
10855 		char *groupname = ill->ill_phyint->phyint_groupname;
10856 
10857 		/*
10858 		 * We're looking for a working interface, but it doesn't matter
10859 		 * if it's up or down; so instead of following the group lists,
10860 		 * we look at each physical interface and compare the groupname.
10861 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10862 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10863 		 * Otherwise we create the ipif on the failed interface.
10864 		 */
10865 		rw_enter(&ill_g_lock, RW_READER);
10866 		phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
10867 		for (; phyi != NULL;
10868 		    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
10869 		    phyi, AVL_AFTER)) {
10870 			if (phyi->phyint_groupname_len == 0)
10871 				continue;
10872 			ASSERT(phyi->phyint_groupname != NULL);
10873 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10874 			    !(phyi->phyint_flags &
10875 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10876 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10877 			    (phyi->phyint_illv4 != NULL))) {
10878 				break;
10879 			}
10880 		}
10881 		rw_exit(&ill_g_lock);
10882 
10883 		if (phyi != NULL) {
10884 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10885 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10886 			    phyi->phyint_illv4);
10887 		}
10888 	}
10889 
10890 	/*
10891 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10892 	 * before or after us.
10893 	 */
10894 	ASSERT(IAM_WRITER_ILL(ill));
10895 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10896 
10897 	if (found_sep && orig_ifindex == 0) {
10898 		/* Now see if there is an IPIF with this unit number. */
10899 		for (ipif = ill->ill_ipif; ipif != NULL;
10900 		    ipif = ipif->ipif_next) {
10901 			if (ipif->ipif_id == id) {
10902 				err = EEXIST;
10903 				goto done;
10904 			}
10905 		}
10906 	}
10907 
10908 	/*
10909 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10910 	 * of lo0. We never come here when we plumb lo0:0. It
10911 	 * happens in ipif_lookup_on_name.
10912 	 * The specified unit number is ignored when we create the ipif on a
10913 	 * different interface. However, we save it in ipif_orig_ipifid below so
10914 	 * that the ipif fails back to the right position.
10915 	 */
10916 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10917 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10918 		err = ENOBUFS;
10919 		goto done;
10920 	}
10921 
10922 	/* Return created name with ioctl */
10923 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10924 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10925 	ip1dbg(("created %s\n", lifr->lifr_name));
10926 
10927 	/* Set address */
10928 	sin = (sin_t *)&lifr->lifr_addr;
10929 	if (sin->sin_family != AF_UNSPEC) {
10930 		err = ip_sioctl_addr(ipif, sin, q, mp,
10931 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10932 	}
10933 
10934 	/* Set ifindex and unit number for failback */
10935 	if (err == 0 && orig_ifindex != 0) {
10936 		ipif->ipif_orig_ifindex = orig_ifindex;
10937 		if (found_sep) {
10938 			ipif->ipif_orig_ipifid = id;
10939 		}
10940 	}
10941 
10942 done:
10943 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10944 	return (err);
10945 }
10946 
10947 /*
10948  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10949  * interface) delete it based on the IP address (on this physical interface).
10950  * Otherwise delete it based on the ipif_id.
10951  * Also, special handling to allow a removeif of lo0.
10952  */
10953 /* ARGSUSED */
10954 int
10955 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10956     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10957 {
10958 	conn_t		*connp;
10959 	ill_t		*ill = ipif->ipif_ill;
10960 	boolean_t	 success;
10961 
10962 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10963 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10964 	ASSERT(IAM_WRITER_IPIF(ipif));
10965 
10966 	connp = Q_TO_CONN(q);
10967 	/*
10968 	 * Special case for unplumbing lo0 (the loopback physical interface).
10969 	 * If unplumbing lo0, the incoming address structure has been
10970 	 * initialized to all zeros. When unplumbing lo0, all its logical
10971 	 * interfaces must be removed too.
10972 	 *
10973 	 * Note that this interface may be called to remove a specific
10974 	 * loopback logical interface (eg, lo0:1). But in that case
10975 	 * ipif->ipif_id != 0 so that the code path for that case is the
10976 	 * same as any other interface (meaning it skips the code directly
10977 	 * below).
10978 	 */
10979 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10980 		if (sin->sin_family == AF_UNSPEC &&
10981 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10982 			/*
10983 			 * Mark it condemned. No new ref. will be made to ill.
10984 			 */
10985 			mutex_enter(&ill->ill_lock);
10986 			ill->ill_state_flags |= ILL_CONDEMNED;
10987 			for (ipif = ill->ill_ipif; ipif != NULL;
10988 			    ipif = ipif->ipif_next) {
10989 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10990 			}
10991 			mutex_exit(&ill->ill_lock);
10992 
10993 			ipif = ill->ill_ipif;
10994 			/* unplumb the loopback interface */
10995 			ill_delete(ill);
10996 			mutex_enter(&connp->conn_lock);
10997 			mutex_enter(&ill->ill_lock);
10998 			ASSERT(ill->ill_group == NULL);
10999 
11000 			/* Are any references to this ill active */
11001 			if (ill_is_quiescent(ill)) {
11002 				mutex_exit(&ill->ill_lock);
11003 				mutex_exit(&connp->conn_lock);
11004 				ill_delete_tail(ill);
11005 				mi_free(ill);
11006 				return (0);
11007 			}
11008 			success = ipsq_pending_mp_add(connp, ipif,
11009 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
11010 			mutex_exit(&connp->conn_lock);
11011 			mutex_exit(&ill->ill_lock);
11012 			if (success)
11013 				return (EINPROGRESS);
11014 			else
11015 				return (EINTR);
11016 		}
11017 	}
11018 
11019 	/*
11020 	 * We are exclusive on the ipsq, so an ill move will be serialized
11021 	 * before or after us.
11022 	 */
11023 	ASSERT(ill->ill_move_in_progress == B_FALSE);
11024 
11025 	if (ipif->ipif_id == 0) {
11026 		/* Find based on address */
11027 		if (ipif->ipif_isv6) {
11028 			sin6_t *sin6;
11029 
11030 			if (sin->sin_family != AF_INET6)
11031 				return (EAFNOSUPPORT);
11032 
11033 			sin6 = (sin6_t *)sin;
11034 			/* We are a writer, so we should be able to lookup */
11035 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
11036 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL);
11037 			if (ipif == NULL) {
11038 				/*
11039 				 * Maybe the address in on another interface in
11040 				 * the same IPMP group? We check this below.
11041 				 */
11042 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
11043 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL);
11044 			}
11045 		} else {
11046 			ipaddr_t addr;
11047 
11048 			if (sin->sin_family != AF_INET)
11049 				return (EAFNOSUPPORT);
11050 
11051 			addr = sin->sin_addr.s_addr;
11052 			/* We are a writer, so we should be able to lookup */
11053 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
11054 			    NULL, NULL, NULL);
11055 			if (ipif == NULL) {
11056 				/*
11057 				 * Maybe the address in on another interface in
11058 				 * the same IPMP group? We check this below.
11059 				 */
11060 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
11061 				    NULL, NULL, NULL, NULL);
11062 			}
11063 		}
11064 		if (ipif == NULL) {
11065 			return (EADDRNOTAVAIL);
11066 		}
11067 		/*
11068 		 * When the address to be removed is hosted on a different
11069 		 * interface, we check if the interface is in the same IPMP
11070 		 * group as the specified one; if so we proceed with the
11071 		 * removal.
11072 		 * ill->ill_group is NULL when the ill is down, so we have to
11073 		 * compare the group names instead.
11074 		 */
11075 		if (ipif->ipif_ill != ill &&
11076 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
11077 		    ill->ill_phyint->phyint_groupname_len == 0 ||
11078 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
11079 		    ill->ill_phyint->phyint_groupname) != 0)) {
11080 			ipif_refrele(ipif);
11081 			return (EADDRNOTAVAIL);
11082 		}
11083 
11084 		/* This is a writer */
11085 		ipif_refrele(ipif);
11086 	}
11087 
11088 	/*
11089 	 * Can not delete instance zero since it is tied to the ill.
11090 	 */
11091 	if (ipif->ipif_id == 0)
11092 		return (EBUSY);
11093 
11094 	mutex_enter(&ill->ill_lock);
11095 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
11096 	mutex_exit(&ill->ill_lock);
11097 
11098 	ipif_free(ipif);
11099 
11100 	mutex_enter(&connp->conn_lock);
11101 	mutex_enter(&ill->ill_lock);
11102 
11103 	/* Are any references to this ipif active */
11104 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
11105 		mutex_exit(&ill->ill_lock);
11106 		mutex_exit(&connp->conn_lock);
11107 		ipif_non_duplicate(ipif);
11108 		ipif_down_tail(ipif);
11109 		ipif_free_tail(ipif);
11110 		return (0);
11111 	}
11112 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
11113 	    IPIF_FREE);
11114 	mutex_exit(&ill->ill_lock);
11115 	mutex_exit(&connp->conn_lock);
11116 	if (success)
11117 		return (EINPROGRESS);
11118 	else
11119 		return (EINTR);
11120 }
11121 
11122 /*
11123  * Restart the removeif ioctl. The refcnt has gone down to 0.
11124  * The ipif is already condemned. So can't find it thru lookups.
11125  */
11126 /* ARGSUSED */
11127 int
11128 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
11129     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11130 {
11131 	ill_t *ill;
11132 
11133 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
11134 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11135 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11136 		ill = ipif->ipif_ill;
11137 		ASSERT(IAM_WRITER_ILL(ill));
11138 		ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) &&
11139 		    (ill->ill_state_flags & IPIF_CONDEMNED));
11140 		ill_delete_tail(ill);
11141 		mi_free(ill);
11142 		return (0);
11143 	}
11144 
11145 	ill = ipif->ipif_ill;
11146 	ASSERT(IAM_WRITER_IPIF(ipif));
11147 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
11148 
11149 	ipif_non_duplicate(ipif);
11150 	ipif_down_tail(ipif);
11151 	ipif_free_tail(ipif);
11152 
11153 	ILL_UNMARK_CHANGING(ill);
11154 	return (0);
11155 }
11156 
11157 /*
11158  * Set the local interface address.
11159  * Allow an address of all zero when the interface is down.
11160  */
11161 /* ARGSUSED */
11162 int
11163 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11164     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11165 {
11166 	int err = 0;
11167 	in6_addr_t v6addr;
11168 	boolean_t need_up = B_FALSE;
11169 
11170 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11171 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11172 
11173 	ASSERT(IAM_WRITER_IPIF(ipif));
11174 
11175 	if (ipif->ipif_isv6) {
11176 		sin6_t *sin6;
11177 		ill_t *ill;
11178 		phyint_t *phyi;
11179 
11180 		if (sin->sin_family != AF_INET6)
11181 			return (EAFNOSUPPORT);
11182 
11183 		sin6 = (sin6_t *)sin;
11184 		v6addr = sin6->sin6_addr;
11185 		ill = ipif->ipif_ill;
11186 		phyi = ill->ill_phyint;
11187 
11188 		/*
11189 		 * Enforce that true multicast interfaces have a link-local
11190 		 * address for logical unit 0.
11191 		 */
11192 		if (ipif->ipif_id == 0 &&
11193 		    (ill->ill_flags & ILLF_MULTICAST) &&
11194 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11195 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11196 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11197 			return (EADDRNOTAVAIL);
11198 		}
11199 
11200 		/*
11201 		 * up interfaces shouldn't have the unspecified address
11202 		 * unless they also have the IPIF_NOLOCAL flags set and
11203 		 * have a subnet assigned.
11204 		 */
11205 		if ((ipif->ipif_flags & IPIF_UP) &&
11206 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11207 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11208 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11209 			return (EADDRNOTAVAIL);
11210 		}
11211 
11212 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11213 			return (EADDRNOTAVAIL);
11214 	} else {
11215 		ipaddr_t addr;
11216 
11217 		if (sin->sin_family != AF_INET)
11218 			return (EAFNOSUPPORT);
11219 
11220 		addr = sin->sin_addr.s_addr;
11221 
11222 		/* Allow 0 as the local address. */
11223 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11224 			return (EADDRNOTAVAIL);
11225 
11226 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11227 	}
11228 
11229 
11230 	/*
11231 	 * Even if there is no change we redo things just to rerun
11232 	 * ipif_set_default.
11233 	 */
11234 	if (ipif->ipif_flags & IPIF_UP) {
11235 		/*
11236 		 * Setting a new local address, make sure
11237 		 * we have net and subnet bcast ire's for
11238 		 * the old address if we need them.
11239 		 */
11240 		if (!ipif->ipif_isv6)
11241 			ipif_check_bcast_ires(ipif);
11242 		/*
11243 		 * If the interface is already marked up,
11244 		 * we call ipif_down which will take care
11245 		 * of ditching any IREs that have been set
11246 		 * up based on the old interface address.
11247 		 */
11248 		err = ipif_logical_down(ipif, q, mp);
11249 		if (err == EINPROGRESS)
11250 			return (err);
11251 		ipif_down_tail(ipif);
11252 		need_up = 1;
11253 	}
11254 
11255 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11256 	return (err);
11257 }
11258 
11259 int
11260 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11261     boolean_t need_up)
11262 {
11263 	in6_addr_t v6addr;
11264 	ipaddr_t addr;
11265 	sin6_t	*sin6;
11266 	int	sinlen;
11267 	int	err = 0;
11268 	ill_t	*ill = ipif->ipif_ill;
11269 	boolean_t need_dl_down;
11270 	boolean_t need_arp_down;
11271 	struct iocblk *iocp;
11272 
11273 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11274 
11275 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11276 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11277 	ASSERT(IAM_WRITER_IPIF(ipif));
11278 
11279 	/* Must cancel any pending timer before taking the ill_lock */
11280 	if (ipif->ipif_recovery_id != 0)
11281 		(void) untimeout(ipif->ipif_recovery_id);
11282 	ipif->ipif_recovery_id = 0;
11283 
11284 	if (ipif->ipif_isv6) {
11285 		sin6 = (sin6_t *)sin;
11286 		v6addr = sin6->sin6_addr;
11287 		sinlen = sizeof (struct sockaddr_in6);
11288 	} else {
11289 		addr = sin->sin_addr.s_addr;
11290 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11291 		sinlen = sizeof (struct sockaddr_in);
11292 	}
11293 	mutex_enter(&ill->ill_lock);
11294 	ipif->ipif_v6lcl_addr = v6addr;
11295 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11296 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11297 	} else {
11298 		ipif->ipif_v6src_addr = v6addr;
11299 	}
11300 	ipif->ipif_addr_ready = 0;
11301 
11302 	/*
11303 	 * If the interface was previously marked as a duplicate, then since
11304 	 * we've now got a "new" address, it should no longer be considered a
11305 	 * duplicate -- even if the "new" address is the same as the old one.
11306 	 * Note that if all ipifs are down, we may have a pending ARP down
11307 	 * event to handle.  This is because we want to recover from duplicates
11308 	 * and thus delay tearing down ARP until the duplicates have been
11309 	 * removed or disabled.
11310 	 */
11311 	need_dl_down = need_arp_down = B_FALSE;
11312 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11313 		need_arp_down = !need_up;
11314 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11315 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11316 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11317 			need_dl_down = B_TRUE;
11318 		}
11319 	}
11320 
11321 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11322 	    !ill->ill_is_6to4tun) {
11323 		queue_t *wqp = ill->ill_wq;
11324 
11325 		/*
11326 		 * The local address of this interface is a 6to4 address,
11327 		 * check if this interface is in fact a 6to4 tunnel or just
11328 		 * an interface configured with a 6to4 address.  We are only
11329 		 * interested in the former.
11330 		 */
11331 		if (wqp != NULL) {
11332 			while ((wqp->q_next != NULL) &&
11333 			    (wqp->q_next->q_qinfo != NULL) &&
11334 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11335 
11336 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11337 				    == TUN6TO4_MODID) {
11338 					/* set for use in IP */
11339 					ill->ill_is_6to4tun = 1;
11340 					break;
11341 				}
11342 				wqp = wqp->q_next;
11343 			}
11344 		}
11345 	}
11346 
11347 	ipif_set_default(ipif);
11348 
11349 	/*
11350 	 * When publishing an interface address change event, we only notify
11351 	 * the event listeners of the new address.  It is assumed that if they
11352 	 * actively care about the addresses assigned that they will have
11353 	 * already discovered the previous address assigned (if there was one.)
11354 	 *
11355 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11356 	 */
11357 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11358 		hook_nic_event_t *info;
11359 		if ((info = ipif->ipif_ill->ill_nic_event_info) != NULL) {
11360 			ip2dbg(("ip_sioctl_addr_tail: unexpected nic event %d "
11361 			    "attached for %s\n", info->hne_event,
11362 			    ill->ill_name));
11363 			if (info->hne_data != NULL)
11364 				kmem_free(info->hne_data, info->hne_datalen);
11365 			kmem_free(info, sizeof (hook_nic_event_t));
11366 		}
11367 
11368 		info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
11369 		if (info != NULL) {
11370 			info->hne_nic =
11371 			    ipif->ipif_ill->ill_phyint->phyint_ifindex;
11372 			info->hne_lif = MAP_IPIF_ID(ipif->ipif_id);
11373 			info->hne_event = NE_ADDRESS_CHANGE;
11374 			info->hne_family = ipif->ipif_isv6 ? ipv6 : ipv4;
11375 			info->hne_data = kmem_alloc(sinlen, KM_NOSLEEP);
11376 			if (info->hne_data != NULL) {
11377 				info->hne_datalen = sinlen;
11378 				bcopy(sin, info->hne_data, sinlen);
11379 			} else {
11380 				ip2dbg(("ip_sioctl_addr_tail: could not attach "
11381 				    "address information for ADDRESS_CHANGE nic"
11382 				    " event of %s (ENOMEM)\n",
11383 				    ipif->ipif_ill->ill_name));
11384 				kmem_free(info, sizeof (hook_nic_event_t));
11385 			}
11386 		} else
11387 			ip2dbg(("ip_sioctl_addr_tail: could not attach "
11388 			    "ADDRESS_CHANGE nic event information for %s "
11389 			    "(ENOMEM)\n", ipif->ipif_ill->ill_name));
11390 
11391 		ipif->ipif_ill->ill_nic_event_info = info;
11392 	}
11393 
11394 	mutex_exit(&ipif->ipif_ill->ill_lock);
11395 
11396 	if (need_up) {
11397 		/*
11398 		 * Now bring the interface back up.  If this
11399 		 * is the only IPIF for the ILL, ipif_up
11400 		 * will have to re-bind to the device, so
11401 		 * we may get back EINPROGRESS, in which
11402 		 * case, this IOCTL will get completed in
11403 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11404 		 */
11405 		err = ipif_up(ipif, q, mp);
11406 	} else {
11407 		/*
11408 		 * Update the IPIF list in SCTP, ipif_up_done() will do it
11409 		 * if need_up is true.
11410 		 */
11411 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11412 	}
11413 
11414 	if (need_dl_down)
11415 		ill_dl_down(ill);
11416 	if (need_arp_down)
11417 		ipif_arp_down(ipif);
11418 
11419 	return (err);
11420 }
11421 
11422 
11423 /*
11424  * Restart entry point to restart the address set operation after the
11425  * refcounts have dropped to zero.
11426  */
11427 /* ARGSUSED */
11428 int
11429 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11430     ip_ioctl_cmd_t *ipip, void *ifreq)
11431 {
11432 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11433 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11434 	ASSERT(IAM_WRITER_IPIF(ipif));
11435 	ipif_down_tail(ipif);
11436 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11437 }
11438 
11439 /* ARGSUSED */
11440 int
11441 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11442     ip_ioctl_cmd_t *ipip, void *if_req)
11443 {
11444 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11445 	struct lifreq *lifr = (struct lifreq *)if_req;
11446 
11447 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11448 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11449 	/*
11450 	 * The net mask and address can't change since we have a
11451 	 * reference to the ipif. So no lock is necessary.
11452 	 */
11453 	if (ipif->ipif_isv6) {
11454 		*sin6 = sin6_null;
11455 		sin6->sin6_family = AF_INET6;
11456 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11457 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11458 		lifr->lifr_addrlen =
11459 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11460 	} else {
11461 		*sin = sin_null;
11462 		sin->sin_family = AF_INET;
11463 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11464 		if (ipip->ipi_cmd_type == LIF_CMD) {
11465 			lifr->lifr_addrlen =
11466 			    ip_mask_to_plen(ipif->ipif_net_mask);
11467 		}
11468 	}
11469 	return (0);
11470 }
11471 
11472 /*
11473  * Set the destination address for a pt-pt interface.
11474  */
11475 /* ARGSUSED */
11476 int
11477 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11478     ip_ioctl_cmd_t *ipip, void *if_req)
11479 {
11480 	int err = 0;
11481 	in6_addr_t v6addr;
11482 	boolean_t need_up = B_FALSE;
11483 
11484 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11485 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11486 	ASSERT(IAM_WRITER_IPIF(ipif));
11487 
11488 	if (ipif->ipif_isv6) {
11489 		sin6_t *sin6;
11490 
11491 		if (sin->sin_family != AF_INET6)
11492 			return (EAFNOSUPPORT);
11493 
11494 		sin6 = (sin6_t *)sin;
11495 		v6addr = sin6->sin6_addr;
11496 
11497 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11498 			return (EADDRNOTAVAIL);
11499 	} else {
11500 		ipaddr_t addr;
11501 
11502 		if (sin->sin_family != AF_INET)
11503 			return (EAFNOSUPPORT);
11504 
11505 		addr = sin->sin_addr.s_addr;
11506 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11507 			return (EADDRNOTAVAIL);
11508 
11509 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11510 	}
11511 
11512 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11513 		return (0);	/* No change */
11514 
11515 	if (ipif->ipif_flags & IPIF_UP) {
11516 		/*
11517 		 * If the interface is already marked up,
11518 		 * we call ipif_down which will take care
11519 		 * of ditching any IREs that have been set
11520 		 * up based on the old pp dst address.
11521 		 */
11522 		err = ipif_logical_down(ipif, q, mp);
11523 		if (err == EINPROGRESS)
11524 			return (err);
11525 		ipif_down_tail(ipif);
11526 		need_up = B_TRUE;
11527 	}
11528 	/*
11529 	 * could return EINPROGRESS. If so ioctl will complete in
11530 	 * ip_rput_dlpi_writer
11531 	 */
11532 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11533 	return (err);
11534 }
11535 
11536 static int
11537 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11538     boolean_t need_up)
11539 {
11540 	in6_addr_t v6addr;
11541 	ill_t	*ill = ipif->ipif_ill;
11542 	int	err = 0;
11543 	boolean_t need_dl_down;
11544 	boolean_t need_arp_down;
11545 
11546 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11547 	    ipif->ipif_id, (void *)ipif));
11548 
11549 	/* Must cancel any pending timer before taking the ill_lock */
11550 	if (ipif->ipif_recovery_id != 0)
11551 		(void) untimeout(ipif->ipif_recovery_id);
11552 	ipif->ipif_recovery_id = 0;
11553 
11554 	if (ipif->ipif_isv6) {
11555 		sin6_t *sin6;
11556 
11557 		sin6 = (sin6_t *)sin;
11558 		v6addr = sin6->sin6_addr;
11559 	} else {
11560 		ipaddr_t addr;
11561 
11562 		addr = sin->sin_addr.s_addr;
11563 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11564 	}
11565 	mutex_enter(&ill->ill_lock);
11566 	/* Set point to point destination address. */
11567 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11568 		/*
11569 		 * Allow this as a means of creating logical
11570 		 * pt-pt interfaces on top of e.g. an Ethernet.
11571 		 * XXX Undocumented HACK for testing.
11572 		 * pt-pt interfaces are created with NUD disabled.
11573 		 */
11574 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11575 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11576 		if (ipif->ipif_isv6)
11577 			ill->ill_flags |= ILLF_NONUD;
11578 	}
11579 
11580 	/*
11581 	 * If the interface was previously marked as a duplicate, then since
11582 	 * we've now got a "new" address, it should no longer be considered a
11583 	 * duplicate -- even if the "new" address is the same as the old one.
11584 	 * Note that if all ipifs are down, we may have a pending ARP down
11585 	 * event to handle.
11586 	 */
11587 	need_dl_down = need_arp_down = B_FALSE;
11588 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11589 		need_arp_down = !need_up;
11590 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11591 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11592 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11593 			need_dl_down = B_TRUE;
11594 		}
11595 	}
11596 
11597 	/* Set the new address. */
11598 	ipif->ipif_v6pp_dst_addr = v6addr;
11599 	/* Make sure subnet tracks pp_dst */
11600 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11601 	mutex_exit(&ill->ill_lock);
11602 
11603 	if (need_up) {
11604 		/*
11605 		 * Now bring the interface back up.  If this
11606 		 * is the only IPIF for the ILL, ipif_up
11607 		 * will have to re-bind to the device, so
11608 		 * we may get back EINPROGRESS, in which
11609 		 * case, this IOCTL will get completed in
11610 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11611 		 */
11612 		err = ipif_up(ipif, q, mp);
11613 	}
11614 
11615 	if (need_dl_down)
11616 		ill_dl_down(ill);
11617 
11618 	if (need_arp_down)
11619 		ipif_arp_down(ipif);
11620 	return (err);
11621 }
11622 
11623 /*
11624  * Restart entry point to restart the dstaddress set operation after the
11625  * refcounts have dropped to zero.
11626  */
11627 /* ARGSUSED */
11628 int
11629 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11630     ip_ioctl_cmd_t *ipip, void *ifreq)
11631 {
11632 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11633 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11634 	ipif_down_tail(ipif);
11635 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11636 }
11637 
11638 /* ARGSUSED */
11639 int
11640 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11641     ip_ioctl_cmd_t *ipip, void *if_req)
11642 {
11643 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11644 
11645 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11646 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11647 	/*
11648 	 * Get point to point destination address. The addresses can't
11649 	 * change since we hold a reference to the ipif.
11650 	 */
11651 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11652 		return (EADDRNOTAVAIL);
11653 
11654 	if (ipif->ipif_isv6) {
11655 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11656 		*sin6 = sin6_null;
11657 		sin6->sin6_family = AF_INET6;
11658 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11659 	} else {
11660 		*sin = sin_null;
11661 		sin->sin_family = AF_INET;
11662 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11663 	}
11664 	return (0);
11665 }
11666 
11667 /*
11668  * part of ipmp, make this func return the active/inactive state and
11669  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11670  */
11671 /*
11672  * This function either sets or clears the IFF_INACTIVE flag.
11673  *
11674  * As long as there are some addresses or multicast memberships on the
11675  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11676  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11677  * will be used for outbound packets.
11678  *
11679  * Caller needs to verify the validity of setting IFF_INACTIVE.
11680  */
11681 static void
11682 phyint_inactive(phyint_t *phyi)
11683 {
11684 	ill_t *ill_v4;
11685 	ill_t *ill_v6;
11686 	ipif_t *ipif;
11687 	ilm_t *ilm;
11688 
11689 	ill_v4 = phyi->phyint_illv4;
11690 	ill_v6 = phyi->phyint_illv6;
11691 
11692 	/*
11693 	 * No need for a lock while traversing the list since iam
11694 	 * a writer
11695 	 */
11696 	if (ill_v4 != NULL) {
11697 		ASSERT(IAM_WRITER_ILL(ill_v4));
11698 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11699 		    ipif = ipif->ipif_next) {
11700 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11701 				mutex_enter(&phyi->phyint_lock);
11702 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11703 				mutex_exit(&phyi->phyint_lock);
11704 				return;
11705 			}
11706 		}
11707 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11708 		    ilm = ilm->ilm_next) {
11709 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11710 				mutex_enter(&phyi->phyint_lock);
11711 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11712 				mutex_exit(&phyi->phyint_lock);
11713 				return;
11714 			}
11715 		}
11716 	}
11717 	if (ill_v6 != NULL) {
11718 		ill_v6 = phyi->phyint_illv6;
11719 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11720 		    ipif = ipif->ipif_next) {
11721 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11722 				mutex_enter(&phyi->phyint_lock);
11723 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11724 				mutex_exit(&phyi->phyint_lock);
11725 				return;
11726 			}
11727 		}
11728 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11729 		    ilm = ilm->ilm_next) {
11730 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11731 				mutex_enter(&phyi->phyint_lock);
11732 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11733 				mutex_exit(&phyi->phyint_lock);
11734 				return;
11735 			}
11736 		}
11737 	}
11738 	mutex_enter(&phyi->phyint_lock);
11739 	phyi->phyint_flags |= PHYI_INACTIVE;
11740 	mutex_exit(&phyi->phyint_lock);
11741 }
11742 
11743 /*
11744  * This function is called only when the phyint flags change. Currently
11745  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11746  * that we can select a good ill.
11747  */
11748 static void
11749 ip_redo_nomination(phyint_t *phyi)
11750 {
11751 	ill_t *ill_v4;
11752 
11753 	ill_v4 = phyi->phyint_illv4;
11754 
11755 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11756 		ASSERT(IAM_WRITER_ILL(ill_v4));
11757 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11758 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11759 	}
11760 }
11761 
11762 /*
11763  * Heuristic to check if ill is INACTIVE.
11764  * Checks if ill has an ipif with an usable ip address.
11765  *
11766  * Return values:
11767  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11768  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11769  */
11770 static boolean_t
11771 ill_is_inactive(ill_t *ill)
11772 {
11773 	ipif_t *ipif;
11774 
11775 	/* Check whether it is in an IPMP group */
11776 	if (ill->ill_phyint->phyint_groupname == NULL)
11777 		return (B_FALSE);
11778 
11779 	if (ill->ill_ipif_up_count == 0)
11780 		return (B_TRUE);
11781 
11782 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11783 		uint64_t flags = ipif->ipif_flags;
11784 
11785 		/*
11786 		 * This ipif is usable if it is IPIF_UP and not a
11787 		 * dedicated test address.  A dedicated test address
11788 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11789 		 * (note in particular that V6 test addresses are
11790 		 * link-local data addresses and thus are marked
11791 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11792 		 */
11793 		if ((flags & IPIF_UP) &&
11794 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11795 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11796 			return (B_FALSE);
11797 	}
11798 	return (B_TRUE);
11799 }
11800 
11801 /*
11802  * Set interface flags.
11803  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11804  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11805  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11806  *
11807  * NOTE : We really don't enforce that ipif_id zero should be used
11808  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11809  *	  is because applications generally does SICGLIFFLAGS and
11810  *	  ORs in the new flags (that affects the logical) and does a
11811  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11812  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11813  *	  flags that will be turned on is correct with respect to
11814  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11815  */
11816 /* ARGSUSED */
11817 int
11818 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11819     ip_ioctl_cmd_t *ipip, void *if_req)
11820 {
11821 	uint64_t turn_on;
11822 	uint64_t turn_off;
11823 	int	err;
11824 	boolean_t need_up = B_FALSE;
11825 	phyint_t *phyi;
11826 	ill_t *ill;
11827 	uint64_t intf_flags;
11828 	boolean_t phyint_flags_modified = B_FALSE;
11829 	uint64_t flags;
11830 	struct ifreq *ifr;
11831 	struct lifreq *lifr;
11832 	boolean_t set_linklocal = B_FALSE;
11833 	boolean_t zero_source = B_FALSE;
11834 
11835 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11836 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11837 
11838 	ASSERT(IAM_WRITER_IPIF(ipif));
11839 
11840 	ill = ipif->ipif_ill;
11841 	phyi = ill->ill_phyint;
11842 
11843 	if (ipip->ipi_cmd_type == IF_CMD) {
11844 		ifr = (struct ifreq *)if_req;
11845 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11846 	} else {
11847 		lifr = (struct lifreq *)if_req;
11848 		flags = lifr->lifr_flags;
11849 	}
11850 
11851 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11852 
11853 	/*
11854 	 * Has the flags been set correctly till now ?
11855 	 */
11856 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11857 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11858 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11859 	/*
11860 	 * Compare the new flags to the old, and partition
11861 	 * into those coming on and those going off.
11862 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11863 	 */
11864 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11865 		flags |= intf_flags & ~0xFFFF;
11866 
11867 	/*
11868 	 * First check which bits will change and then which will
11869 	 * go on and off
11870 	 */
11871 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11872 	if (!turn_on)
11873 		return (0);	/* No change */
11874 
11875 	turn_off = intf_flags & turn_on;
11876 	turn_on ^= turn_off;
11877 	err = 0;
11878 
11879 	/*
11880 	 * Don't allow any bits belonging to the logical interface
11881 	 * to be set or cleared on the replacement ipif that was
11882 	 * created temporarily during a MOVE.
11883 	 */
11884 	if (ipif->ipif_replace_zero &&
11885 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11886 		return (EINVAL);
11887 	}
11888 
11889 	/*
11890 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11891 	 * IPv6 interfaces.
11892 	 */
11893 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11894 		return (EINVAL);
11895 
11896 	/*
11897 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11898 	 * interfaces.  It makes no sense in that context.
11899 	 */
11900 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11901 		return (EINVAL);
11902 
11903 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11904 		zero_source = B_TRUE;
11905 
11906 	/*
11907 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11908 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11909 	 * If the link local address isn't set, and can be set, it will get
11910 	 * set later on in this function.
11911 	 */
11912 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11913 	    (flags & IFF_UP) && !zero_source &&
11914 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11915 		if (ipif_cant_setlinklocal(ipif))
11916 			return (EINVAL);
11917 		set_linklocal = B_TRUE;
11918 	}
11919 
11920 	/*
11921 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11922 	 * same time. No need to grab ill_g_usesrc_lock here, see
11923 	 * synchronization notes in ip.c
11924 	 */
11925 	if (turn_on & PHYI_STANDBY &&
11926 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11927 		return (EINVAL);
11928 	}
11929 
11930 	/*
11931 	 * If we modify physical interface flags, we'll potentially need to
11932 	 * send up two routing socket messages for the changes (one for the
11933 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11934 	 */
11935 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11936 		phyint_flags_modified = B_TRUE;
11937 
11938 	/*
11939 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11940 	 * we need to flush the IRE_CACHES belonging to this ill.
11941 	 * We handle this case here without doing the DOWN/UP dance
11942 	 * like it is done for other flags. If some other flags are
11943 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11944 	 * below will handle it by bringing it down and then
11945 	 * bringing it UP.
11946 	 */
11947 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11948 		ill_t *ill_v4, *ill_v6;
11949 
11950 		ill_v4 = phyi->phyint_illv4;
11951 		ill_v6 = phyi->phyint_illv6;
11952 
11953 		/*
11954 		 * First set the INACTIVE flag if needed. Then delete the ires.
11955 		 * ire_add will atomically prevent creating new IRE_CACHEs
11956 		 * unless hidden flag is set.
11957 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11958 		 */
11959 		if ((turn_on & PHYI_FAILED) &&
11960 		    ((intf_flags & PHYI_STANDBY) || !ipmp_enable_failback)) {
11961 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11962 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11963 		}
11964 		if ((turn_off & PHYI_FAILED) &&
11965 		    ((intf_flags & PHYI_STANDBY) ||
11966 		    (!ipmp_enable_failback && ill_is_inactive(ill)))) {
11967 			phyint_inactive(phyi);
11968 		}
11969 
11970 		if (turn_on & PHYI_STANDBY) {
11971 			/*
11972 			 * We implicitly set INACTIVE only when STANDBY is set.
11973 			 * INACTIVE is also set on non-STANDBY phyint when user
11974 			 * disables FAILBACK using configuration file.
11975 			 * Do not allow STANDBY to be set on such INACTIVE
11976 			 * phyint
11977 			 */
11978 			if (phyi->phyint_flags & PHYI_INACTIVE)
11979 				return (EINVAL);
11980 			if (!(phyi->phyint_flags & PHYI_FAILED))
11981 				phyint_inactive(phyi);
11982 		}
11983 		if (turn_off & PHYI_STANDBY) {
11984 			if (ipmp_enable_failback) {
11985 				/*
11986 				 * Reset PHYI_INACTIVE.
11987 				 */
11988 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11989 			} else if (ill_is_inactive(ill) &&
11990 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11991 				/*
11992 				 * Need to set INACTIVE, when user sets
11993 				 * STANDBY on a non-STANDBY phyint and
11994 				 * later resets STANDBY
11995 				 */
11996 				phyint_inactive(phyi);
11997 			}
11998 		}
11999 		/*
12000 		 * We should always send up a message so that the
12001 		 * daemons come to know of it. Note that the zeroth
12002 		 * interface can be down and the check below for IPIF_UP
12003 		 * will not make sense as we are actually setting
12004 		 * a phyint flag here. We assume that the ipif used
12005 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
12006 		 * send up any message for non-zero ipifs).
12007 		 */
12008 		phyint_flags_modified = B_TRUE;
12009 
12010 		if (ill_v4 != NULL) {
12011 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
12012 			    IRE_CACHE, ill_stq_cache_delete,
12013 			    (char *)ill_v4, ill_v4);
12014 			illgrp_reset_schednext(ill_v4);
12015 		}
12016 		if (ill_v6 != NULL) {
12017 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
12018 			    IRE_CACHE, ill_stq_cache_delete,
12019 			    (char *)ill_v6, ill_v6);
12020 			illgrp_reset_schednext(ill_v6);
12021 		}
12022 	}
12023 
12024 	/*
12025 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
12026 	 * status of the interface and, if the interface is part of an IPMP
12027 	 * group, all other interfaces that are part of the same IPMP
12028 	 * group.
12029 	 */
12030 	if ((turn_on | turn_off) & ILLF_ROUTER) {
12031 		(void) ill_forward_set(q, mp, ((turn_on & ILLF_ROUTER) != 0),
12032 		    (caddr_t)ill);
12033 	}
12034 
12035 	/*
12036 	 * If the interface is not UP and we are not going to
12037 	 * bring it UP, record the flags and return. When the
12038 	 * interface comes UP later, the right actions will be
12039 	 * taken.
12040 	 */
12041 	if (!(ipif->ipif_flags & IPIF_UP) &&
12042 	    !(turn_on & IPIF_UP)) {
12043 		/* Record new flags in their respective places. */
12044 		mutex_enter(&ill->ill_lock);
12045 		mutex_enter(&ill->ill_phyint->phyint_lock);
12046 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12047 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12048 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12049 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12050 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12051 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12052 		mutex_exit(&ill->ill_lock);
12053 		mutex_exit(&ill->ill_phyint->phyint_lock);
12054 
12055 		/*
12056 		 * We do the broadcast and nomination here rather
12057 		 * than waiting for a FAILOVER/FAILBACK to happen. In
12058 		 * the case of FAILBACK from INACTIVE standby to the
12059 		 * interface that has been repaired, PHYI_FAILED has not
12060 		 * been cleared yet. If there are only two interfaces in
12061 		 * that group, all we have is a FAILED and INACTIVE
12062 		 * interface. If we do the nomination soon after a failback,
12063 		 * the broadcast nomination code would select the
12064 		 * INACTIVE interface for receiving broadcasts as FAILED is
12065 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
12066 		 * receive broadcast packets, we need to redo nomination
12067 		 * when the FAILED is cleared here. Thus, in general we
12068 		 * always do the nomination here for FAILED, STANDBY
12069 		 * and OFFLINE.
12070 		 */
12071 		if (((turn_on | turn_off) &
12072 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
12073 			ip_redo_nomination(phyi);
12074 		}
12075 		if (phyint_flags_modified) {
12076 			if (phyi->phyint_illv4 != NULL) {
12077 				ip_rts_ifmsg(phyi->phyint_illv4->
12078 				    ill_ipif);
12079 			}
12080 			if (phyi->phyint_illv6 != NULL) {
12081 				ip_rts_ifmsg(phyi->phyint_illv6->
12082 				    ill_ipif);
12083 			}
12084 		}
12085 		return (0);
12086 	} else if (set_linklocal || zero_source) {
12087 		mutex_enter(&ill->ill_lock);
12088 		if (set_linklocal)
12089 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
12090 		if (zero_source)
12091 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
12092 		mutex_exit(&ill->ill_lock);
12093 	}
12094 
12095 	/*
12096 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
12097 	 * or point-to-point interfaces with an unspecified destination. We do
12098 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
12099 	 * have a subnet assigned, which is how in.ndpd currently manages its
12100 	 * onlink prefix list when no addresses are configured with those
12101 	 * prefixes.
12102 	 */
12103 	if (ipif->ipif_isv6 &&
12104 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
12105 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
12106 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
12107 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12108 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
12109 		return (EINVAL);
12110 	}
12111 
12112 	/*
12113 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
12114 	 * from being brought up.
12115 	 */
12116 	if (!ipif->ipif_isv6 &&
12117 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
12118 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
12119 		return (EINVAL);
12120 	}
12121 
12122 	/*
12123 	 * The only flag changes that we currently take specific action on
12124 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
12125 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
12126 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
12127 	 * the flags and bringing it back up again.
12128 	 */
12129 	if ((turn_on|turn_off) &
12130 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
12131 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
12132 		/*
12133 		 * Taking this ipif down, make sure we have
12134 		 * valid net and subnet bcast ire's for other
12135 		 * logical interfaces, if we need them.
12136 		 */
12137 		if (!ipif->ipif_isv6)
12138 			ipif_check_bcast_ires(ipif);
12139 
12140 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
12141 		    !(turn_off & IPIF_UP)) {
12142 			need_up = B_TRUE;
12143 			if (ipif->ipif_flags & IPIF_UP)
12144 				ill->ill_logical_down = 1;
12145 			turn_on &= ~IPIF_UP;
12146 		}
12147 		err = ipif_down(ipif, q, mp);
12148 		ip1dbg(("ipif_down returns %d err ", err));
12149 		if (err == EINPROGRESS)
12150 			return (err);
12151 		ipif_down_tail(ipif);
12152 	}
12153 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
12154 }
12155 
12156 static int
12157 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
12158     boolean_t need_up)
12159 {
12160 	ill_t	*ill;
12161 	phyint_t *phyi;
12162 	uint64_t turn_on;
12163 	uint64_t turn_off;
12164 	uint64_t intf_flags;
12165 	boolean_t phyint_flags_modified = B_FALSE;
12166 	int	err = 0;
12167 	boolean_t set_linklocal = B_FALSE;
12168 	boolean_t zero_source = B_FALSE;
12169 
12170 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12171 		ipif->ipif_ill->ill_name, ipif->ipif_id));
12172 
12173 	ASSERT(IAM_WRITER_IPIF(ipif));
12174 
12175 	ill = ipif->ipif_ill;
12176 	phyi = ill->ill_phyint;
12177 
12178 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12179 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12180 
12181 	turn_off = intf_flags & turn_on;
12182 	turn_on ^= turn_off;
12183 
12184 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12185 		phyint_flags_modified = B_TRUE;
12186 
12187 	/*
12188 	 * Now we change the flags. Track current value of
12189 	 * other flags in their respective places.
12190 	 */
12191 	mutex_enter(&ill->ill_lock);
12192 	mutex_enter(&phyi->phyint_lock);
12193 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12194 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12195 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12196 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12197 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12198 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12199 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12200 		set_linklocal = B_TRUE;
12201 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12202 	}
12203 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12204 		zero_source = B_TRUE;
12205 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12206 	}
12207 	mutex_exit(&ill->ill_lock);
12208 	mutex_exit(&phyi->phyint_lock);
12209 
12210 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12211 		ip_redo_nomination(phyi);
12212 
12213 	if (set_linklocal)
12214 		(void) ipif_setlinklocal(ipif);
12215 
12216 	if (zero_source)
12217 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12218 	else
12219 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12220 
12221 	if (need_up) {
12222 		/*
12223 		 * XXX ipif_up really does not know whether a phyint flags
12224 		 * was modified or not. So, it sends up information on
12225 		 * only one routing sockets message. As we don't bring up
12226 		 * the interface and also set STANDBY/FAILED simultaneously
12227 		 * it should be okay.
12228 		 */
12229 		err = ipif_up(ipif, q, mp);
12230 	} else {
12231 		/*
12232 		 * Make sure routing socket sees all changes to the flags.
12233 		 * ipif_up_done* handles this when we use ipif_up.
12234 		 */
12235 		if (phyint_flags_modified) {
12236 			if (phyi->phyint_illv4 != NULL) {
12237 				ip_rts_ifmsg(phyi->phyint_illv4->
12238 				    ill_ipif);
12239 			}
12240 			if (phyi->phyint_illv6 != NULL) {
12241 				ip_rts_ifmsg(phyi->phyint_illv6->
12242 				    ill_ipif);
12243 			}
12244 		} else {
12245 			ip_rts_ifmsg(ipif);
12246 		}
12247 	}
12248 	return (err);
12249 }
12250 
12251 /*
12252  * Restart entry point to restart the flags restart operation after the
12253  * refcounts have dropped to zero.
12254  */
12255 /* ARGSUSED */
12256 int
12257 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12258     ip_ioctl_cmd_t *ipip, void *if_req)
12259 {
12260 	int	err;
12261 	struct ifreq *ifr = (struct ifreq *)if_req;
12262 	struct lifreq *lifr = (struct lifreq *)if_req;
12263 
12264 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12265 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12266 
12267 	ipif_down_tail(ipif);
12268 	if (ipip->ipi_cmd_type == IF_CMD) {
12269 		/*
12270 		 * Since ip_sioctl_flags expects an int and ifr_flags
12271 		 * is a short we need to cast ifr_flags into an int
12272 		 * to avoid having sign extension cause bits to get
12273 		 * set that should not be.
12274 		 */
12275 		err = ip_sioctl_flags_tail(ipif,
12276 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
12277 		    q, mp, B_TRUE);
12278 	} else {
12279 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
12280 		    q, mp, B_TRUE);
12281 	}
12282 	return (err);
12283 }
12284 
12285 /* ARGSUSED */
12286 int
12287 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12288     ip_ioctl_cmd_t *ipip, void *if_req)
12289 {
12290 	/*
12291 	 * Has the flags been set correctly till now ?
12292 	 */
12293 	ill_t *ill = ipif->ipif_ill;
12294 	phyint_t *phyi = ill->ill_phyint;
12295 
12296 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12297 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12298 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12299 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12300 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12301 
12302 	/*
12303 	 * Need a lock since some flags can be set even when there are
12304 	 * references to the ipif.
12305 	 */
12306 	mutex_enter(&ill->ill_lock);
12307 	if (ipip->ipi_cmd_type == IF_CMD) {
12308 		struct ifreq *ifr = (struct ifreq *)if_req;
12309 
12310 		/* Get interface flags (low 16 only). */
12311 		ifr->ifr_flags = ((ipif->ipif_flags |
12312 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12313 	} else {
12314 		struct lifreq *lifr = (struct lifreq *)if_req;
12315 
12316 		/* Get interface flags. */
12317 		lifr->lifr_flags = ipif->ipif_flags |
12318 		    ill->ill_flags | phyi->phyint_flags;
12319 	}
12320 	mutex_exit(&ill->ill_lock);
12321 	return (0);
12322 }
12323 
12324 /* ARGSUSED */
12325 int
12326 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12327     ip_ioctl_cmd_t *ipip, void *if_req)
12328 {
12329 	int mtu;
12330 	int ip_min_mtu;
12331 	struct ifreq	*ifr;
12332 	struct lifreq *lifr;
12333 	ire_t	*ire;
12334 
12335 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12336 	    ipif->ipif_id, (void *)ipif));
12337 	if (ipip->ipi_cmd_type == IF_CMD) {
12338 		ifr = (struct ifreq *)if_req;
12339 		mtu = ifr->ifr_metric;
12340 	} else {
12341 		lifr = (struct lifreq *)if_req;
12342 		mtu = lifr->lifr_mtu;
12343 	}
12344 
12345 	if (ipif->ipif_isv6)
12346 		ip_min_mtu = IPV6_MIN_MTU;
12347 	else
12348 		ip_min_mtu = IP_MIN_MTU;
12349 
12350 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12351 		return (EINVAL);
12352 
12353 	/*
12354 	 * Change the MTU size in all relevant ire's.
12355 	 * Mtu change Vs. new ire creation - protocol below.
12356 	 * First change ipif_mtu and the ire_max_frag of the
12357 	 * interface ire. Then do an ire walk and change the
12358 	 * ire_max_frag of all affected ires. During ire_add
12359 	 * under the bucket lock, set the ire_max_frag of the
12360 	 * new ire being created from the ipif/ire from which
12361 	 * it is being derived. If an mtu change happens after
12362 	 * the ire is added, the new ire will be cleaned up.
12363 	 * Conversely if the mtu change happens before the ire
12364 	 * is added, ire_add will see the new value of the mtu.
12365 	 */
12366 	ipif->ipif_mtu = mtu;
12367 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12368 
12369 	if (ipif->ipif_isv6)
12370 		ire = ipif_to_ire_v6(ipif);
12371 	else
12372 		ire = ipif_to_ire(ipif);
12373 	if (ire != NULL) {
12374 		ire->ire_max_frag = ipif->ipif_mtu;
12375 		ire_refrele(ire);
12376 	}
12377 	if (ipif->ipif_flags & IPIF_UP) {
12378 		if (ipif->ipif_isv6)
12379 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES);
12380 		else
12381 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES);
12382 	}
12383 	/* Update the MTU in SCTP's list */
12384 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12385 	return (0);
12386 }
12387 
12388 /* Get interface MTU. */
12389 /* ARGSUSED */
12390 int
12391 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12392 	ip_ioctl_cmd_t *ipip, void *if_req)
12393 {
12394 	struct ifreq	*ifr;
12395 	struct lifreq	*lifr;
12396 
12397 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12398 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12399 	if (ipip->ipi_cmd_type == IF_CMD) {
12400 		ifr = (struct ifreq *)if_req;
12401 		ifr->ifr_metric = ipif->ipif_mtu;
12402 	} else {
12403 		lifr = (struct lifreq *)if_req;
12404 		lifr->lifr_mtu = ipif->ipif_mtu;
12405 	}
12406 	return (0);
12407 }
12408 
12409 /* Set interface broadcast address. */
12410 /* ARGSUSED2 */
12411 int
12412 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12413 	ip_ioctl_cmd_t *ipip, void *if_req)
12414 {
12415 	ipaddr_t addr;
12416 	ire_t	*ire;
12417 
12418 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12419 	    ipif->ipif_id));
12420 
12421 	ASSERT(IAM_WRITER_IPIF(ipif));
12422 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12423 		return (EADDRNOTAVAIL);
12424 
12425 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12426 
12427 	if (sin->sin_family != AF_INET)
12428 		return (EAFNOSUPPORT);
12429 
12430 	addr = sin->sin_addr.s_addr;
12431 	if (ipif->ipif_flags & IPIF_UP) {
12432 		/*
12433 		 * If we are already up, make sure the new
12434 		 * broadcast address makes sense.  If it does,
12435 		 * there should be an IRE for it already.
12436 		 * Don't match on ipif, only on the ill
12437 		 * since we are sharing these now. Don't use
12438 		 * MATCH_IRE_ILL_GROUP as we are looking for
12439 		 * the broadcast ire on this ill and each ill
12440 		 * in the group has its own broadcast ire.
12441 		 */
12442 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12443 		    ipif, ALL_ZONES, NULL,
12444 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE));
12445 		if (ire == NULL) {
12446 			return (EINVAL);
12447 		} else {
12448 			ire_refrele(ire);
12449 		}
12450 	}
12451 	/*
12452 	 * Changing the broadcast addr for this ipif.
12453 	 * Make sure we have valid net and subnet bcast
12454 	 * ire's for other logical interfaces, if needed.
12455 	 */
12456 	if (addr != ipif->ipif_brd_addr)
12457 		ipif_check_bcast_ires(ipif);
12458 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12459 	return (0);
12460 }
12461 
12462 /* Get interface broadcast address. */
12463 /* ARGSUSED */
12464 int
12465 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12466     ip_ioctl_cmd_t *ipip, void *if_req)
12467 {
12468 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12469 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12470 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12471 		return (EADDRNOTAVAIL);
12472 
12473 	/* IPIF_BROADCAST not possible with IPv6 */
12474 	ASSERT(!ipif->ipif_isv6);
12475 	*sin = sin_null;
12476 	sin->sin_family = AF_INET;
12477 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12478 	return (0);
12479 }
12480 
12481 /*
12482  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12483  */
12484 /* ARGSUSED */
12485 int
12486 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12487     ip_ioctl_cmd_t *ipip, void *if_req)
12488 {
12489 	int err = 0;
12490 	in6_addr_t v6mask;
12491 
12492 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12493 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12494 
12495 	ASSERT(IAM_WRITER_IPIF(ipif));
12496 
12497 	if (ipif->ipif_isv6) {
12498 		sin6_t *sin6;
12499 
12500 		if (sin->sin_family != AF_INET6)
12501 			return (EAFNOSUPPORT);
12502 
12503 		sin6 = (sin6_t *)sin;
12504 		v6mask = sin6->sin6_addr;
12505 	} else {
12506 		ipaddr_t mask;
12507 
12508 		if (sin->sin_family != AF_INET)
12509 			return (EAFNOSUPPORT);
12510 
12511 		mask = sin->sin_addr.s_addr;
12512 		V4MASK_TO_V6(mask, v6mask);
12513 	}
12514 
12515 	/*
12516 	 * No big deal if the interface isn't already up, or the mask
12517 	 * isn't really changing, or this is pt-pt.
12518 	 */
12519 	if (!(ipif->ipif_flags & IPIF_UP) ||
12520 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12521 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12522 		ipif->ipif_v6net_mask = v6mask;
12523 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12524 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12525 			    ipif->ipif_v6net_mask,
12526 			    ipif->ipif_v6subnet);
12527 		}
12528 		return (0);
12529 	}
12530 	/*
12531 	 * Make sure we have valid net and subnet broadcast ire's
12532 	 * for the old netmask, if needed by other logical interfaces.
12533 	 */
12534 	if (!ipif->ipif_isv6)
12535 		ipif_check_bcast_ires(ipif);
12536 
12537 	err = ipif_logical_down(ipif, q, mp);
12538 	if (err == EINPROGRESS)
12539 		return (err);
12540 	ipif_down_tail(ipif);
12541 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12542 	return (err);
12543 }
12544 
12545 static int
12546 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12547 {
12548 	in6_addr_t v6mask;
12549 	int err = 0;
12550 
12551 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12552 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12553 
12554 	if (ipif->ipif_isv6) {
12555 		sin6_t *sin6;
12556 
12557 		sin6 = (sin6_t *)sin;
12558 		v6mask = sin6->sin6_addr;
12559 	} else {
12560 		ipaddr_t mask;
12561 
12562 		mask = sin->sin_addr.s_addr;
12563 		V4MASK_TO_V6(mask, v6mask);
12564 	}
12565 
12566 	ipif->ipif_v6net_mask = v6mask;
12567 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12568 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12569 		    ipif->ipif_v6subnet);
12570 	}
12571 	err = ipif_up(ipif, q, mp);
12572 
12573 	if (err == 0 || err == EINPROGRESS) {
12574 		/*
12575 		 * The interface must be DL_BOUND if this packet has to
12576 		 * go out on the wire. Since we only go through a logical
12577 		 * down and are bound with the driver during an internal
12578 		 * down/up that is satisfied.
12579 		 */
12580 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12581 			/* Potentially broadcast an address mask reply. */
12582 			ipif_mask_reply(ipif);
12583 		}
12584 	}
12585 	return (err);
12586 }
12587 
12588 /* ARGSUSED */
12589 int
12590 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12591     ip_ioctl_cmd_t *ipip, void *if_req)
12592 {
12593 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12594 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12595 	ipif_down_tail(ipif);
12596 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12597 }
12598 
12599 /* Get interface net mask. */
12600 /* ARGSUSED */
12601 int
12602 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12603     ip_ioctl_cmd_t *ipip, void *if_req)
12604 {
12605 	struct lifreq *lifr = (struct lifreq *)if_req;
12606 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12607 
12608 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12609 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12610 
12611 	/*
12612 	 * net mask can't change since we have a reference to the ipif.
12613 	 */
12614 	if (ipif->ipif_isv6) {
12615 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12616 		*sin6 = sin6_null;
12617 		sin6->sin6_family = AF_INET6;
12618 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12619 		lifr->lifr_addrlen =
12620 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12621 	} else {
12622 		*sin = sin_null;
12623 		sin->sin_family = AF_INET;
12624 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12625 		if (ipip->ipi_cmd_type == LIF_CMD) {
12626 			lifr->lifr_addrlen =
12627 			    ip_mask_to_plen(ipif->ipif_net_mask);
12628 		}
12629 	}
12630 	return (0);
12631 }
12632 
12633 /* ARGSUSED */
12634 int
12635 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12636     ip_ioctl_cmd_t *ipip, void *if_req)
12637 {
12638 
12639 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12640 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12641 	/*
12642 	 * Set interface metric.  We don't use this for
12643 	 * anything but we keep track of it in case it is
12644 	 * important to routing applications or such.
12645 	 */
12646 	if (ipip->ipi_cmd_type == IF_CMD) {
12647 		struct ifreq    *ifr;
12648 
12649 		ifr = (struct ifreq *)if_req;
12650 		ipif->ipif_metric = ifr->ifr_metric;
12651 	} else {
12652 		struct lifreq   *lifr;
12653 
12654 		lifr = (struct lifreq *)if_req;
12655 		ipif->ipif_metric = lifr->lifr_metric;
12656 	}
12657 	return (0);
12658 }
12659 
12660 
12661 /* ARGSUSED */
12662 int
12663 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12664     ip_ioctl_cmd_t *ipip, void *if_req)
12665 {
12666 
12667 	/* Get interface metric. */
12668 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12669 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12670 	if (ipip->ipi_cmd_type == IF_CMD) {
12671 		struct ifreq    *ifr;
12672 
12673 		ifr = (struct ifreq *)if_req;
12674 		ifr->ifr_metric = ipif->ipif_metric;
12675 	} else {
12676 		struct lifreq   *lifr;
12677 
12678 		lifr = (struct lifreq *)if_req;
12679 		lifr->lifr_metric = ipif->ipif_metric;
12680 	}
12681 
12682 	return (0);
12683 }
12684 
12685 /* ARGSUSED */
12686 int
12687 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12688     ip_ioctl_cmd_t *ipip, void *if_req)
12689 {
12690 
12691 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12692 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12693 	/*
12694 	 * Set the muxid returned from I_PLINK.
12695 	 */
12696 	if (ipip->ipi_cmd_type == IF_CMD) {
12697 		struct ifreq *ifr = (struct ifreq *)if_req;
12698 
12699 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12700 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12701 	} else {
12702 		struct lifreq *lifr = (struct lifreq *)if_req;
12703 
12704 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12705 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12706 	}
12707 	return (0);
12708 }
12709 
12710 /* ARGSUSED */
12711 int
12712 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12713     ip_ioctl_cmd_t *ipip, void *if_req)
12714 {
12715 
12716 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12717 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12718 	/*
12719 	 * Get the muxid saved in ill for I_PUNLINK.
12720 	 */
12721 	if (ipip->ipi_cmd_type == IF_CMD) {
12722 		struct ifreq *ifr = (struct ifreq *)if_req;
12723 
12724 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12725 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12726 	} else {
12727 		struct lifreq *lifr = (struct lifreq *)if_req;
12728 
12729 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12730 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12731 	}
12732 	return (0);
12733 }
12734 
12735 /*
12736  * Set the subnet prefix. Does not modify the broadcast address.
12737  */
12738 /* ARGSUSED */
12739 int
12740 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12741     ip_ioctl_cmd_t *ipip, void *if_req)
12742 {
12743 	int err = 0;
12744 	in6_addr_t v6addr;
12745 	in6_addr_t v6mask;
12746 	boolean_t need_up = B_FALSE;
12747 	int addrlen;
12748 
12749 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12750 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12751 
12752 	ASSERT(IAM_WRITER_IPIF(ipif));
12753 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12754 
12755 	if (ipif->ipif_isv6) {
12756 		sin6_t *sin6;
12757 
12758 		if (sin->sin_family != AF_INET6)
12759 			return (EAFNOSUPPORT);
12760 
12761 		sin6 = (sin6_t *)sin;
12762 		v6addr = sin6->sin6_addr;
12763 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12764 			return (EADDRNOTAVAIL);
12765 	} else {
12766 		ipaddr_t addr;
12767 
12768 		if (sin->sin_family != AF_INET)
12769 			return (EAFNOSUPPORT);
12770 
12771 		addr = sin->sin_addr.s_addr;
12772 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12773 			return (EADDRNOTAVAIL);
12774 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12775 		/* Add 96 bits */
12776 		addrlen += IPV6_ABITS - IP_ABITS;
12777 	}
12778 
12779 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12780 		return (EINVAL);
12781 
12782 	/* Check if bits in the address is set past the mask */
12783 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12784 		return (EINVAL);
12785 
12786 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12787 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12788 		return (0);	/* No change */
12789 
12790 	if (ipif->ipif_flags & IPIF_UP) {
12791 		/*
12792 		 * If the interface is already marked up,
12793 		 * we call ipif_down which will take care
12794 		 * of ditching any IREs that have been set
12795 		 * up based on the old interface address.
12796 		 */
12797 		err = ipif_logical_down(ipif, q, mp);
12798 		if (err == EINPROGRESS)
12799 			return (err);
12800 		ipif_down_tail(ipif);
12801 		need_up = B_TRUE;
12802 	}
12803 
12804 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12805 	return (err);
12806 }
12807 
12808 static int
12809 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12810     queue_t *q, mblk_t *mp, boolean_t need_up)
12811 {
12812 	ill_t	*ill = ipif->ipif_ill;
12813 	int	err = 0;
12814 
12815 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12816 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12817 
12818 	/* Set the new address. */
12819 	mutex_enter(&ill->ill_lock);
12820 	ipif->ipif_v6net_mask = v6mask;
12821 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12822 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12823 		    ipif->ipif_v6subnet);
12824 	}
12825 	mutex_exit(&ill->ill_lock);
12826 
12827 	if (need_up) {
12828 		/*
12829 		 * Now bring the interface back up.  If this
12830 		 * is the only IPIF for the ILL, ipif_up
12831 		 * will have to re-bind to the device, so
12832 		 * we may get back EINPROGRESS, in which
12833 		 * case, this IOCTL will get completed in
12834 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12835 		 */
12836 		err = ipif_up(ipif, q, mp);
12837 		if (err == EINPROGRESS)
12838 			return (err);
12839 	}
12840 	return (err);
12841 }
12842 
12843 /* ARGSUSED */
12844 int
12845 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12846     ip_ioctl_cmd_t *ipip, void *if_req)
12847 {
12848 	int	addrlen;
12849 	in6_addr_t v6addr;
12850 	in6_addr_t v6mask;
12851 	struct lifreq *lifr = (struct lifreq *)if_req;
12852 
12853 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12854 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12855 	ipif_down_tail(ipif);
12856 
12857 	addrlen = lifr->lifr_addrlen;
12858 	if (ipif->ipif_isv6) {
12859 		sin6_t *sin6;
12860 
12861 		sin6 = (sin6_t *)sin;
12862 		v6addr = sin6->sin6_addr;
12863 	} else {
12864 		ipaddr_t addr;
12865 
12866 		addr = sin->sin_addr.s_addr;
12867 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12868 		addrlen += IPV6_ABITS - IP_ABITS;
12869 	}
12870 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12871 
12872 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12873 }
12874 
12875 /* ARGSUSED */
12876 int
12877 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12878     ip_ioctl_cmd_t *ipip, void *if_req)
12879 {
12880 	struct lifreq *lifr = (struct lifreq *)if_req;
12881 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12882 
12883 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12884 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12885 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12886 
12887 	if (ipif->ipif_isv6) {
12888 		*sin6 = sin6_null;
12889 		sin6->sin6_family = AF_INET6;
12890 		sin6->sin6_addr = ipif->ipif_v6subnet;
12891 		lifr->lifr_addrlen =
12892 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12893 	} else {
12894 		*sin = sin_null;
12895 		sin->sin_family = AF_INET;
12896 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12897 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12898 	}
12899 	return (0);
12900 }
12901 
12902 /*
12903  * Set the IPv6 address token.
12904  */
12905 /* ARGSUSED */
12906 int
12907 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12908     ip_ioctl_cmd_t *ipi, void *if_req)
12909 {
12910 	ill_t *ill = ipif->ipif_ill;
12911 	int err;
12912 	in6_addr_t v6addr;
12913 	in6_addr_t v6mask;
12914 	boolean_t need_up = B_FALSE;
12915 	int i;
12916 	sin6_t *sin6 = (sin6_t *)sin;
12917 	struct lifreq *lifr = (struct lifreq *)if_req;
12918 	int addrlen;
12919 
12920 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12921 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12922 	ASSERT(IAM_WRITER_IPIF(ipif));
12923 
12924 	addrlen = lifr->lifr_addrlen;
12925 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12926 	if (ipif->ipif_id != 0)
12927 		return (EINVAL);
12928 
12929 	if (!ipif->ipif_isv6)
12930 		return (EINVAL);
12931 
12932 	if (addrlen > IPV6_ABITS)
12933 		return (EINVAL);
12934 
12935 	v6addr = sin6->sin6_addr;
12936 
12937 	/*
12938 	 * The length of the token is the length from the end.  To get
12939 	 * the proper mask for this, compute the mask of the bits not
12940 	 * in the token; ie. the prefix, and then xor to get the mask.
12941 	 */
12942 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12943 		return (EINVAL);
12944 	for (i = 0; i < 4; i++) {
12945 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12946 	}
12947 
12948 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12949 	    ill->ill_token_length == addrlen)
12950 		return (0);	/* No change */
12951 
12952 	if (ipif->ipif_flags & IPIF_UP) {
12953 		err = ipif_logical_down(ipif, q, mp);
12954 		if (err == EINPROGRESS)
12955 			return (err);
12956 		ipif_down_tail(ipif);
12957 		need_up = B_TRUE;
12958 	}
12959 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12960 	return (err);
12961 }
12962 
12963 static int
12964 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12965     mblk_t *mp, boolean_t need_up)
12966 {
12967 	in6_addr_t v6addr;
12968 	in6_addr_t v6mask;
12969 	ill_t	*ill = ipif->ipif_ill;
12970 	int	i;
12971 	int	err = 0;
12972 
12973 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12974 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12975 	v6addr = sin6->sin6_addr;
12976 	/*
12977 	 * The length of the token is the length from the end.  To get
12978 	 * the proper mask for this, compute the mask of the bits not
12979 	 * in the token; ie. the prefix, and then xor to get the mask.
12980 	 */
12981 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12982 	for (i = 0; i < 4; i++)
12983 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12984 
12985 	mutex_enter(&ill->ill_lock);
12986 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12987 	ill->ill_token_length = addrlen;
12988 	mutex_exit(&ill->ill_lock);
12989 
12990 	if (need_up) {
12991 		/*
12992 		 * Now bring the interface back up.  If this
12993 		 * is the only IPIF for the ILL, ipif_up
12994 		 * will have to re-bind to the device, so
12995 		 * we may get back EINPROGRESS, in which
12996 		 * case, this IOCTL will get completed in
12997 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12998 		 */
12999 		err = ipif_up(ipif, q, mp);
13000 		if (err == EINPROGRESS)
13001 			return (err);
13002 	}
13003 	return (err);
13004 }
13005 
13006 /* ARGSUSED */
13007 int
13008 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13009     ip_ioctl_cmd_t *ipi, void *if_req)
13010 {
13011 	ill_t *ill;
13012 	sin6_t *sin6 = (sin6_t *)sin;
13013 	struct lifreq *lifr = (struct lifreq *)if_req;
13014 
13015 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
13016 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13017 	if (ipif->ipif_id != 0)
13018 		return (EINVAL);
13019 
13020 	ill = ipif->ipif_ill;
13021 	if (!ill->ill_isv6)
13022 		return (ENXIO);
13023 
13024 	*sin6 = sin6_null;
13025 	sin6->sin6_family = AF_INET6;
13026 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
13027 	sin6->sin6_addr = ill->ill_token;
13028 	lifr->lifr_addrlen = ill->ill_token_length;
13029 	return (0);
13030 }
13031 
13032 /*
13033  * Set (hardware) link specific information that might override
13034  * what was acquired through the DL_INFO_ACK.
13035  * The logic is as follows.
13036  *
13037  * become exclusive
13038  * set CHANGING flag
13039  * change mtu on affected IREs
13040  * clear CHANGING flag
13041  *
13042  * An ire add that occurs before the CHANGING flag is set will have its mtu
13043  * changed by the ip_sioctl_lnkinfo.
13044  *
13045  * During the time the CHANGING flag is set, no new ires will be added to the
13046  * bucket, and ire add will fail (due the CHANGING flag).
13047  *
13048  * An ire add that occurs after the CHANGING flag is set will have the right mtu
13049  * before it is added to the bucket.
13050  *
13051  * Obviously only 1 thread can set the CHANGING flag and we need to become
13052  * exclusive to set the flag.
13053  */
13054 /* ARGSUSED */
13055 int
13056 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13057     ip_ioctl_cmd_t *ipi, void *if_req)
13058 {
13059 	ill_t		*ill = ipif->ipif_ill;
13060 	ipif_t		*nipif;
13061 	int		ip_min_mtu;
13062 	boolean_t	mtu_walk = B_FALSE;
13063 	struct lifreq	*lifr = (struct lifreq *)if_req;
13064 	lif_ifinfo_req_t *lir;
13065 	ire_t		*ire;
13066 
13067 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
13068 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13069 	lir = &lifr->lifr_ifinfo;
13070 	ASSERT(IAM_WRITER_IPIF(ipif));
13071 
13072 	/* Only allow for logical unit zero i.e. not on "le0:17" */
13073 	if (ipif->ipif_id != 0)
13074 		return (EINVAL);
13075 
13076 	/* Set interface MTU. */
13077 	if (ipif->ipif_isv6)
13078 		ip_min_mtu = IPV6_MIN_MTU;
13079 	else
13080 		ip_min_mtu = IP_MIN_MTU;
13081 
13082 	/*
13083 	 * Verify values before we set anything. Allow zero to
13084 	 * mean unspecified.
13085 	 */
13086 	if (lir->lir_maxmtu != 0 &&
13087 	    (lir->lir_maxmtu > ill->ill_max_frag ||
13088 	    lir->lir_maxmtu < ip_min_mtu))
13089 		return (EINVAL);
13090 	if (lir->lir_reachtime != 0 &&
13091 	    lir->lir_reachtime > ND_MAX_REACHTIME)
13092 		return (EINVAL);
13093 	if (lir->lir_reachretrans != 0 &&
13094 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
13095 		return (EINVAL);
13096 
13097 	mutex_enter(&ill->ill_lock);
13098 	ill->ill_state_flags |= ILL_CHANGING;
13099 	for (nipif = ill->ill_ipif; nipif != NULL;
13100 	    nipif = nipif->ipif_next) {
13101 		nipif->ipif_state_flags |= IPIF_CHANGING;
13102 	}
13103 
13104 	mutex_exit(&ill->ill_lock);
13105 
13106 	if (lir->lir_maxmtu != 0) {
13107 		ill->ill_max_mtu = lir->lir_maxmtu;
13108 		ill->ill_mtu_userspecified = 1;
13109 		mtu_walk = B_TRUE;
13110 	}
13111 
13112 	if (lir->lir_reachtime != 0)
13113 		ill->ill_reachable_time = lir->lir_reachtime;
13114 
13115 	if (lir->lir_reachretrans != 0)
13116 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
13117 
13118 	ill->ill_max_hops = lir->lir_maxhops;
13119 
13120 	ill->ill_max_buf = ND_MAX_Q;
13121 
13122 	if (mtu_walk) {
13123 		/*
13124 		 * Set the MTU on all ipifs associated with this ill except
13125 		 * for those whose MTU was fixed via SIOCSLIFMTU.
13126 		 */
13127 		for (nipif = ill->ill_ipif; nipif != NULL;
13128 		    nipif = nipif->ipif_next) {
13129 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
13130 				continue;
13131 
13132 			nipif->ipif_mtu = ill->ill_max_mtu;
13133 
13134 			if (!(nipif->ipif_flags & IPIF_UP))
13135 				continue;
13136 
13137 			if (nipif->ipif_isv6)
13138 				ire = ipif_to_ire_v6(nipif);
13139 			else
13140 				ire = ipif_to_ire(nipif);
13141 			if (ire != NULL) {
13142 				ire->ire_max_frag = ipif->ipif_mtu;
13143 				ire_refrele(ire);
13144 			}
13145 			if (ill->ill_isv6) {
13146 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
13147 				    ipif_mtu_change, (char *)nipif,
13148 				    ill);
13149 			} else {
13150 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
13151 				    ipif_mtu_change, (char *)nipif,
13152 				    ill);
13153 			}
13154 		}
13155 	}
13156 
13157 	mutex_enter(&ill->ill_lock);
13158 	for (nipif = ill->ill_ipif; nipif != NULL;
13159 	    nipif = nipif->ipif_next) {
13160 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
13161 	}
13162 	ILL_UNMARK_CHANGING(ill);
13163 	mutex_exit(&ill->ill_lock);
13164 
13165 	return (0);
13166 }
13167 
13168 /* ARGSUSED */
13169 int
13170 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13171     ip_ioctl_cmd_t *ipi, void *if_req)
13172 {
13173 	struct lif_ifinfo_req *lir;
13174 	ill_t *ill = ipif->ipif_ill;
13175 
13176 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13177 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13178 	if (ipif->ipif_id != 0)
13179 		return (EINVAL);
13180 
13181 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13182 	lir->lir_maxhops = ill->ill_max_hops;
13183 	lir->lir_reachtime = ill->ill_reachable_time;
13184 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13185 	lir->lir_maxmtu = ill->ill_max_mtu;
13186 
13187 	return (0);
13188 }
13189 
13190 /*
13191  * Return best guess as to the subnet mask for the specified address.
13192  * Based on the subnet masks for all the configured interfaces.
13193  *
13194  * We end up returning a zero mask in the case of default, multicast or
13195  * experimental.
13196  */
13197 static ipaddr_t
13198 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp)
13199 {
13200 	ipaddr_t net_mask;
13201 	ill_t	*ill;
13202 	ipif_t	*ipif;
13203 	ill_walk_context_t ctx;
13204 	ipif_t	*fallback_ipif = NULL;
13205 
13206 	net_mask = ip_net_mask(addr);
13207 	if (net_mask == 0) {
13208 		*ipifp = NULL;
13209 		return (0);
13210 	}
13211 
13212 	/* Let's check to see if this is maybe a local subnet route. */
13213 	/* this function only applies to IPv4 interfaces */
13214 	rw_enter(&ill_g_lock, RW_READER);
13215 	ill = ILL_START_WALK_V4(&ctx);
13216 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13217 		mutex_enter(&ill->ill_lock);
13218 		for (ipif = ill->ill_ipif; ipif != NULL;
13219 		    ipif = ipif->ipif_next) {
13220 			if (!IPIF_CAN_LOOKUP(ipif))
13221 				continue;
13222 			if (!(ipif->ipif_flags & IPIF_UP))
13223 				continue;
13224 			if ((ipif->ipif_subnet & net_mask) ==
13225 			    (addr & net_mask)) {
13226 				/*
13227 				 * Don't trust pt-pt interfaces if there are
13228 				 * other interfaces.
13229 				 */
13230 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13231 					if (fallback_ipif == NULL) {
13232 						ipif_refhold_locked(ipif);
13233 						fallback_ipif = ipif;
13234 					}
13235 					continue;
13236 				}
13237 
13238 				/*
13239 				 * Fine. Just assume the same net mask as the
13240 				 * directly attached subnet interface is using.
13241 				 */
13242 				ipif_refhold_locked(ipif);
13243 				mutex_exit(&ill->ill_lock);
13244 				rw_exit(&ill_g_lock);
13245 				if (fallback_ipif != NULL)
13246 					ipif_refrele(fallback_ipif);
13247 				*ipifp = ipif;
13248 				return (ipif->ipif_net_mask);
13249 			}
13250 		}
13251 		mutex_exit(&ill->ill_lock);
13252 	}
13253 	rw_exit(&ill_g_lock);
13254 
13255 	*ipifp = fallback_ipif;
13256 	return ((fallback_ipif != NULL) ?
13257 	    fallback_ipif->ipif_net_mask : net_mask);
13258 }
13259 
13260 /*
13261  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13262  */
13263 static void
13264 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13265 {
13266 	IOCP	iocp;
13267 	ipft_t	*ipft;
13268 	ipllc_t	*ipllc;
13269 	mblk_t	*mp1;
13270 	cred_t	*cr;
13271 	int	error = 0;
13272 	conn_t	*connp;
13273 
13274 	ip1dbg(("ip_wput_ioctl"));
13275 	iocp = (IOCP)mp->b_rptr;
13276 	mp1 = mp->b_cont;
13277 	if (mp1 == NULL) {
13278 		iocp->ioc_error = EINVAL;
13279 		mp->b_datap->db_type = M_IOCNAK;
13280 		iocp->ioc_count = 0;
13281 		qreply(q, mp);
13282 		return;
13283 	}
13284 
13285 	/*
13286 	 * These IOCTLs provide various control capabilities to
13287 	 * upstream agents such as ULPs and processes.	There
13288 	 * are currently two such IOCTLs implemented.  They
13289 	 * are used by TCP to provide update information for
13290 	 * existing IREs and to forcibly delete an IRE for a
13291 	 * host that is not responding, thereby forcing an
13292 	 * attempt at a new route.
13293 	 */
13294 	iocp->ioc_error = EINVAL;
13295 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13296 		goto done;
13297 
13298 	ipllc = (ipllc_t *)mp1->b_rptr;
13299 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13300 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13301 			break;
13302 	}
13303 	/*
13304 	 * prefer credential from mblk over ioctl;
13305 	 * see ip_sioctl_copyin_setup
13306 	 */
13307 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13308 
13309 	/*
13310 	 * Refhold the conn in case the request gets queued up in some lookup
13311 	 */
13312 	ASSERT(CONN_Q(q));
13313 	connp = Q_TO_CONN(q);
13314 	CONN_INC_REF(connp);
13315 	if (ipft->ipft_pfi &&
13316 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13317 		pullupmsg(mp1, ipft->ipft_min_size))) {
13318 		error = (*ipft->ipft_pfi)(q,
13319 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13320 	}
13321 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13322 		/*
13323 		 * CONN_OPER_PENDING_DONE happens in the function called
13324 		 * through ipft_pfi above.
13325 		 */
13326 		return;
13327 	}
13328 
13329 	CONN_OPER_PENDING_DONE(connp);
13330 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13331 		freemsg(mp);
13332 		return;
13333 	}
13334 	iocp->ioc_error = error;
13335 
13336 done:
13337 	mp->b_datap->db_type = M_IOCACK;
13338 	if (iocp->ioc_error)
13339 		iocp->ioc_count = 0;
13340 	qreply(q, mp);
13341 }
13342 
13343 /*
13344  * Lookup an ipif using the sequence id (ipif_seqid)
13345  */
13346 ipif_t *
13347 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13348 {
13349 	ipif_t *ipif;
13350 
13351 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13352 
13353 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13354 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13355 			return (ipif);
13356 	}
13357 	return (NULL);
13358 }
13359 
13360 uint64_t ipif_g_seqid;
13361 
13362 /*
13363  * Assign a unique id for the ipif. This is used later when we send
13364  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13365  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13366  * IRE is added, we verify that ipif has not disappeared.
13367  */
13368 
13369 static void
13370 ipif_assign_seqid(ipif_t *ipif)
13371 {
13372 	ipif->ipif_seqid = atomic_add_64_nv(&ipif_g_seqid, 1);
13373 }
13374 
13375 /*
13376  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13377  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13378  * be inserted into the first space available in the list. The value of
13379  * ipif_id will then be set to the appropriate value for its position.
13380  */
13381 static int
13382 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13383 {
13384 	ill_t *ill;
13385 	ipif_t *tipif;
13386 	ipif_t **tipifp;
13387 	int id;
13388 
13389 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13390 	    IAM_WRITER_IPIF(ipif));
13391 
13392 	ill = ipif->ipif_ill;
13393 	ASSERT(ill != NULL);
13394 
13395 	/*
13396 	 * In the case of lo0:0 we already hold the ill_g_lock.
13397 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13398 	 * ipif_insert. Another such caller is ipif_move.
13399 	 */
13400 	if (acquire_g_lock)
13401 		rw_enter(&ill_g_lock, RW_WRITER);
13402 	if (acquire_ill_lock)
13403 		mutex_enter(&ill->ill_lock);
13404 	id = ipif->ipif_id;
13405 	tipifp = &(ill->ill_ipif);
13406 	if (id == -1) {	/* need to find a real id */
13407 		id = 0;
13408 		while ((tipif = *tipifp) != NULL) {
13409 			ASSERT(tipif->ipif_id >= id);
13410 			if (tipif->ipif_id != id)
13411 				break; /* non-consecutive id */
13412 			id++;
13413 			tipifp = &(tipif->ipif_next);
13414 		}
13415 		/* limit number of logical interfaces */
13416 		if (id >= ip_addrs_per_if) {
13417 			if (acquire_ill_lock)
13418 				mutex_exit(&ill->ill_lock);
13419 			if (acquire_g_lock)
13420 				rw_exit(&ill_g_lock);
13421 			return (-1);
13422 		}
13423 		ipif->ipif_id = id; /* assign new id */
13424 	} else if (id < ip_addrs_per_if) {
13425 		/* we have a real id; insert ipif in the right place */
13426 		while ((tipif = *tipifp) != NULL) {
13427 			ASSERT(tipif->ipif_id != id);
13428 			if (tipif->ipif_id > id)
13429 				break; /* found correct location */
13430 			tipifp = &(tipif->ipif_next);
13431 		}
13432 	} else {
13433 		if (acquire_ill_lock)
13434 			mutex_exit(&ill->ill_lock);
13435 		if (acquire_g_lock)
13436 			rw_exit(&ill_g_lock);
13437 		return (-1);
13438 	}
13439 
13440 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13441 
13442 	ipif->ipif_next = tipif;
13443 	*tipifp = ipif;
13444 	if (acquire_ill_lock)
13445 		mutex_exit(&ill->ill_lock);
13446 	if (acquire_g_lock)
13447 		rw_exit(&ill_g_lock);
13448 	return (0);
13449 }
13450 
13451 /*
13452  * Allocate and initialize a new interface control structure.  (Always
13453  * called as writer.)
13454  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13455  * is not part of the global linked list of ills. ipif_seqid is unique
13456  * in the system and to preserve the uniqueness, it is assigned only
13457  * when ill becomes part of the global list. At that point ill will
13458  * have a name. If it doesn't get assigned here, it will get assigned
13459  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13460  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13461  * the interface flags or any other information from the DL_INFO_ACK for
13462  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13463  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13464  * second DL_INFO_ACK comes in from the driver.
13465  */
13466 static ipif_t *
13467 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13468 {
13469 	ipif_t	*ipif;
13470 	phyint_t *phyi;
13471 
13472 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13473 	    ill->ill_name, id, (void *)ill));
13474 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13475 
13476 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13477 		return (NULL);
13478 	*ipif = ipif_zero;	/* start clean */
13479 
13480 	ipif->ipif_ill = ill;
13481 	ipif->ipif_id = id;	/* could be -1 */
13482 	ipif->ipif_zoneid = GLOBAL_ZONEID;
13483 
13484 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13485 
13486 	ipif->ipif_refcnt = 0;
13487 	ipif->ipif_saved_ire_cnt = 0;
13488 
13489 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13490 		mi_free(ipif);
13491 		return (NULL);
13492 	}
13493 	/* -1 id should have been replaced by real id */
13494 	id = ipif->ipif_id;
13495 	ASSERT(id >= 0);
13496 
13497 	if (ill->ill_name[0] != '\0') {
13498 		ipif_assign_seqid(ipif);
13499 		if (ill->ill_phyint->phyint_ifindex != 0)
13500 			sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
13501 	}
13502 	/*
13503 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13504 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13505 	 * ioctl sets ipif_orig_ipifid to zero.
13506 	 */
13507 	ipif->ipif_orig_ipifid = id;
13508 
13509 	/*
13510 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13511 	 * The ipif is still not up and can't be looked up until the
13512 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13513 	 */
13514 	mutex_enter(&ill->ill_lock);
13515 	mutex_enter(&ill->ill_phyint->phyint_lock);
13516 	/*
13517 	 * Set the running flag when logical interface zero is created.
13518 	 * For subsequent logical interfaces, a DLPI link down
13519 	 * notification message may have cleared the running flag to
13520 	 * indicate the link is down, so we shouldn't just blindly set it.
13521 	 */
13522 	if (id == 0)
13523 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13524 	ipif->ipif_ire_type = ire_type;
13525 	phyi = ill->ill_phyint;
13526 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13527 
13528 	if (ipif->ipif_isv6) {
13529 		ill->ill_flags |= ILLF_IPV6;
13530 	} else {
13531 		ipaddr_t inaddr_any = INADDR_ANY;
13532 
13533 		ill->ill_flags |= ILLF_IPV4;
13534 
13535 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13536 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13537 		    &ipif->ipif_v6lcl_addr);
13538 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13539 		    &ipif->ipif_v6src_addr);
13540 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13541 		    &ipif->ipif_v6subnet);
13542 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13543 		    &ipif->ipif_v6net_mask);
13544 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13545 		    &ipif->ipif_v6brd_addr);
13546 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13547 		    &ipif->ipif_v6pp_dst_addr);
13548 	}
13549 
13550 	/*
13551 	 * Don't set the interface flags etc. now, will do it in
13552 	 * ip_ll_subnet_defaults.
13553 	 */
13554 	if (!initialize) {
13555 		mutex_exit(&ill->ill_lock);
13556 		mutex_exit(&ill->ill_phyint->phyint_lock);
13557 		return (ipif);
13558 	}
13559 	ipif->ipif_mtu = ill->ill_max_mtu;
13560 
13561 	if (ill->ill_bcast_addr_length != 0) {
13562 		/*
13563 		 * Later detect lack of DLPI driver multicast
13564 		 * capability by catching DL_ENABMULTI errors in
13565 		 * ip_rput_dlpi.
13566 		 */
13567 		ill->ill_flags |= ILLF_MULTICAST;
13568 		if (!ipif->ipif_isv6)
13569 			ipif->ipif_flags |= IPIF_BROADCAST;
13570 	} else {
13571 		if (ill->ill_net_type != IRE_LOOPBACK) {
13572 			if (ipif->ipif_isv6)
13573 				/*
13574 				 * Note: xresolv interfaces will eventually need
13575 				 * NOARP set here as well, but that will require
13576 				 * those external resolvers to have some
13577 				 * knowledge of that flag and act appropriately.
13578 				 * Not to be changed at present.
13579 				 */
13580 				ill->ill_flags |= ILLF_NONUD;
13581 			else
13582 				ill->ill_flags |= ILLF_NOARP;
13583 		}
13584 		if (ill->ill_phys_addr_length == 0) {
13585 			if (ill->ill_media &&
13586 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13587 				ipif->ipif_flags |= IPIF_NOXMIT;
13588 				phyi->phyint_flags |= PHYI_VIRTUAL;
13589 			} else {
13590 				/* pt-pt supports multicast. */
13591 				ill->ill_flags |= ILLF_MULTICAST;
13592 				if (ill->ill_net_type == IRE_LOOPBACK) {
13593 					phyi->phyint_flags |=
13594 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13595 				} else {
13596 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13597 				}
13598 			}
13599 		}
13600 	}
13601 	mutex_exit(&ill->ill_lock);
13602 	mutex_exit(&ill->ill_phyint->phyint_lock);
13603 	return (ipif);
13604 }
13605 
13606 /*
13607  * If appropriate, send a message up to the resolver delete the entry
13608  * for the address of this interface which is going out of business.
13609  * (Always called as writer).
13610  *
13611  * NOTE : We need to check for NULL mps as some of the fields are
13612  *	  initialized only for some interface types. See ipif_resolver_up()
13613  *	  for details.
13614  */
13615 void
13616 ipif_arp_down(ipif_t *ipif)
13617 {
13618 	mblk_t	*mp;
13619 	ill_t	*ill = ipif->ipif_ill;
13620 
13621 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13622 	ASSERT(IAM_WRITER_IPIF(ipif));
13623 
13624 	/* Delete the mapping for the local address */
13625 	mp = ipif->ipif_arp_del_mp;
13626 	if (mp != NULL) {
13627 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13628 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13629 		putnext(ill->ill_rq, mp);
13630 		ipif->ipif_arp_del_mp = NULL;
13631 	}
13632 
13633 	/*
13634 	 * If this is the last ipif that is going down and there are no
13635 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13636 	 * clean up ARP completely.
13637 	 */
13638 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13639 
13640 		/* Send up AR_INTERFACE_DOWN message */
13641 		mp = ill->ill_arp_down_mp;
13642 		if (mp != NULL) {
13643 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13644 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13645 			    ipif->ipif_id));
13646 			putnext(ill->ill_rq, mp);
13647 			ill->ill_arp_down_mp = NULL;
13648 		}
13649 
13650 		/* Tell ARP to delete the multicast mappings */
13651 		mp = ill->ill_arp_del_mapping_mp;
13652 		if (mp != NULL) {
13653 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13654 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13655 			    ipif->ipif_id));
13656 			putnext(ill->ill_rq, mp);
13657 			ill->ill_arp_del_mapping_mp = NULL;
13658 		}
13659 	}
13660 }
13661 
13662 /*
13663  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13664  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13665  * that it wants the add_mp allocated in this function to be returned
13666  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13667  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13668  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13669  * as it does a ipif_arp_down after calling this function - which will
13670  * remove what we add here.
13671  *
13672  * Returns -1 on failures and 0 on success.
13673  */
13674 int
13675 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13676 {
13677 	mblk_t	*del_mp = NULL;
13678 	mblk_t *add_mp = NULL;
13679 	mblk_t *mp;
13680 	ill_t	*ill = ipif->ipif_ill;
13681 	phyint_t *phyi = ill->ill_phyint;
13682 	ipaddr_t addr, mask, extract_mask = 0;
13683 	arma_t	*arma;
13684 	uint8_t *maddr, *bphys_addr;
13685 	uint32_t hw_start;
13686 	dl_unitdata_req_t *dlur;
13687 
13688 	ASSERT(IAM_WRITER_IPIF(ipif));
13689 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13690 		return (0);
13691 
13692 	/*
13693 	 * Delete the existing mapping from ARP. Normally ipif_down
13694 	 * -> ipif_arp_down should send this up to ARP. The only
13695 	 * reason we would find this when we are switching from
13696 	 * Multicast to Broadcast where we did not do a down.
13697 	 */
13698 	mp = ill->ill_arp_del_mapping_mp;
13699 	if (mp != NULL) {
13700 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13701 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13702 		putnext(ill->ill_rq, mp);
13703 		ill->ill_arp_del_mapping_mp = NULL;
13704 	}
13705 
13706 	if (arp_add_mapping_mp != NULL)
13707 		*arp_add_mapping_mp = NULL;
13708 
13709 	/*
13710 	 * Check that the address is not to long for the constant
13711 	 * length reserved in the template arma_t.
13712 	 */
13713 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13714 		return (-1);
13715 
13716 	/* Add mapping mblk */
13717 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13718 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13719 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13720 	    (caddr_t)&addr);
13721 	if (add_mp == NULL)
13722 		return (-1);
13723 	arma = (arma_t *)add_mp->b_rptr;
13724 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13725 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13726 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13727 
13728 	/*
13729 	 * Determine the broadcast address.
13730 	 */
13731 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13732 	if (ill->ill_sap_length < 0)
13733 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13734 	else
13735 		bphys_addr = (uchar_t *)dlur +
13736 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13737 	/*
13738 	 * Check PHYI_MULTI_BCAST and length of physical
13739 	 * address to determine if we use the mapping or the
13740 	 * broadcast address.
13741 	 */
13742 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13743 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13744 		    bphys_addr, maddr, &hw_start, &extract_mask))
13745 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13746 
13747 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13748 	    (ill->ill_flags & ILLF_MULTICAST)) {
13749 		/* Make sure this will not match the "exact" entry. */
13750 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13751 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13752 		    (caddr_t)&addr);
13753 		if (del_mp == NULL) {
13754 			freemsg(add_mp);
13755 			return (-1);
13756 		}
13757 		bcopy(&extract_mask, (char *)arma +
13758 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13759 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13760 			/* Use link-layer broadcast address for MULTI_BCAST */
13761 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13762 			ip2dbg(("ipif_arp_setup_multicast: adding"
13763 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13764 		} else {
13765 			arma->arma_hw_mapping_start = hw_start;
13766 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13767 			    " ARP setup for %s\n", ill->ill_name));
13768 		}
13769 	} else {
13770 		freemsg(add_mp);
13771 		ASSERT(del_mp == NULL);
13772 		/* It is neither MULTICAST nor MULTI_BCAST */
13773 		return (0);
13774 	}
13775 	ASSERT(add_mp != NULL && del_mp != NULL);
13776 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13777 	ill->ill_arp_del_mapping_mp = del_mp;
13778 	if (arp_add_mapping_mp != NULL) {
13779 		/* The caller just wants the mblks allocated */
13780 		*arp_add_mapping_mp = add_mp;
13781 	} else {
13782 		/* The caller wants us to send it to arp */
13783 		putnext(ill->ill_rq, add_mp);
13784 	}
13785 	return (0);
13786 }
13787 
13788 /*
13789  * Get the resolver set up for a new interface address.
13790  * (Always called as writer.)
13791  * Called both for IPv4 and IPv6 interfaces,
13792  * though it only sets up the resolver for v6
13793  * if it's an xresolv interface (one using an external resolver).
13794  * Honors ILLF_NOARP.
13795  * The enumerated value res_act is used to tune the behavior.
13796  * If set to Res_act_initial, then we set up all the resolver
13797  * structures for a new interface.  If set to Res_act_move, then
13798  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13799  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13800  * asynchronous hardware address change notification.  If set to
13801  * Res_act_defend, then we tell ARP that it needs to send a single
13802  * gratuitous message in defense of the address.
13803  * Returns error on failure.
13804  */
13805 int
13806 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13807 {
13808 	caddr_t	addr;
13809 	mblk_t	*arp_up_mp = NULL;
13810 	mblk_t	*arp_down_mp = NULL;
13811 	mblk_t	*arp_add_mp = NULL;
13812 	mblk_t	*arp_del_mp = NULL;
13813 	mblk_t	*arp_add_mapping_mp = NULL;
13814 	mblk_t	*arp_del_mapping_mp = NULL;
13815 	ill_t	*ill = ipif->ipif_ill;
13816 	uchar_t	*area_p = NULL;
13817 	uchar_t	*ared_p = NULL;
13818 	int	err = ENOMEM;
13819 	boolean_t was_dup;
13820 
13821 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13822 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13823 	ASSERT(IAM_WRITER_IPIF(ipif));
13824 
13825 	was_dup = B_FALSE;
13826 	if (res_act == Res_act_initial) {
13827 		ipif->ipif_addr_ready = 0;
13828 		/*
13829 		 * We're bringing an interface up here.  There's no way that we
13830 		 * should need to shut down ARP now.
13831 		 */
13832 		mutex_enter(&ill->ill_lock);
13833 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13834 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13835 			ill->ill_ipif_dup_count--;
13836 			was_dup = B_TRUE;
13837 		}
13838 		mutex_exit(&ill->ill_lock);
13839 	}
13840 	if (ipif->ipif_recovery_id != 0)
13841 		(void) untimeout(ipif->ipif_recovery_id);
13842 	ipif->ipif_recovery_id = 0;
13843 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13844 		ipif->ipif_addr_ready = 1;
13845 		return (0);
13846 	}
13847 	/* NDP will set the ipif_addr_ready flag when it's ready */
13848 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13849 		return (0);
13850 
13851 	if (ill->ill_isv6) {
13852 		/*
13853 		 * External resolver for IPv6
13854 		 */
13855 		ASSERT(res_act == Res_act_initial);
13856 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13857 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13858 			area_p = (uchar_t *)&ip6_area_template;
13859 			ared_p = (uchar_t *)&ip6_ared_template;
13860 		}
13861 	} else {
13862 		/*
13863 		 * IPv4 arp case. If the ARP stream has already started
13864 		 * closing, fail this request for ARP bringup. Else
13865 		 * record the fact that an ARP bringup is pending.
13866 		 */
13867 		mutex_enter(&ill->ill_lock);
13868 		if (ill->ill_arp_closing) {
13869 			mutex_exit(&ill->ill_lock);
13870 			err = EINVAL;
13871 			goto failed;
13872 		} else {
13873 			if (ill->ill_ipif_up_count == 0 &&
13874 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13875 				ill->ill_arp_bringup_pending = 1;
13876 			mutex_exit(&ill->ill_lock);
13877 		}
13878 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13879 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13880 			area_p = (uchar_t *)&ip_area_template;
13881 			ared_p = (uchar_t *)&ip_ared_template;
13882 		}
13883 	}
13884 
13885 	/*
13886 	 * Add an entry for the local address in ARP only if it
13887 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13888 	 */
13889 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13890 		area_t *area;
13891 
13892 		/* Now ask ARP to publish our address. */
13893 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13894 		if (arp_add_mp == NULL)
13895 			goto failed;
13896 		area = (area_t *)arp_add_mp->b_rptr;
13897 		if (res_act != Res_act_initial) {
13898 			/*
13899 			 * Copy the new hardware address and length into
13900 			 * arp_add_mp to be sent to ARP.
13901 			 */
13902 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13903 			bcopy(ill->ill_phys_addr,
13904 			    ((char *)area + area->area_hw_addr_offset),
13905 			    area->area_hw_addr_length);
13906 		}
13907 
13908 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13909 		    ACE_F_MYADDR;
13910 
13911 		if (res_act == Res_act_defend) {
13912 			area->area_flags |= ACE_F_DEFEND;
13913 			/*
13914 			 * If we're just defending our address now, then
13915 			 * there's no need to set up ARP multicast mappings.
13916 			 * The publish command is enough.
13917 			 */
13918 			goto done;
13919 		}
13920 
13921 		if (res_act != Res_act_initial)
13922 			goto arp_setup_multicast;
13923 
13924 		/*
13925 		 * Allocate an ARP deletion message so we know we can tell ARP
13926 		 * when the interface goes down.
13927 		 */
13928 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13929 		if (arp_del_mp == NULL)
13930 			goto failed;
13931 
13932 	} else {
13933 		if (res_act != Res_act_initial)
13934 			goto done;
13935 	}
13936 	/*
13937 	 * Need to bring up ARP or setup multicast mapping only
13938 	 * when the first interface is coming UP.
13939 	 */
13940 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13941 	    was_dup) {
13942 		goto done;
13943 	}
13944 
13945 	/*
13946 	 * Allocate an ARP down message (to be saved) and an ARP up
13947 	 * message.
13948 	 */
13949 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13950 	if (arp_down_mp == NULL)
13951 		goto failed;
13952 
13953 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13954 	if (arp_up_mp == NULL)
13955 		goto failed;
13956 
13957 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13958 		goto done;
13959 
13960 arp_setup_multicast:
13961 	/*
13962 	 * Setup the multicast mappings. This function initializes
13963 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13964 	 * IPv6.
13965 	 */
13966 	if (!ill->ill_isv6) {
13967 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13968 		if (err != 0)
13969 			goto failed;
13970 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13971 		ASSERT(arp_add_mapping_mp != NULL);
13972 	}
13973 
13974 done:
13975 	if (arp_del_mp != NULL) {
13976 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13977 		ipif->ipif_arp_del_mp = arp_del_mp;
13978 	}
13979 	if (arp_down_mp != NULL) {
13980 		ASSERT(ill->ill_arp_down_mp == NULL);
13981 		ill->ill_arp_down_mp = arp_down_mp;
13982 	}
13983 	if (arp_del_mapping_mp != NULL) {
13984 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13985 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13986 	}
13987 	if (arp_up_mp != NULL) {
13988 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13989 		    ill->ill_name, ipif->ipif_id));
13990 		putnext(ill->ill_rq, arp_up_mp);
13991 	}
13992 	if (arp_add_mp != NULL) {
13993 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13994 		    ill->ill_name, ipif->ipif_id));
13995 		/*
13996 		 * If it's an extended ARP implementation, then we'll wait to
13997 		 * hear that DAD has finished before using the interface.
13998 		 */
13999 		if (!ill->ill_arp_extend)
14000 			ipif->ipif_addr_ready = 1;
14001 		putnext(ill->ill_rq, arp_add_mp);
14002 	} else {
14003 		ipif->ipif_addr_ready = 1;
14004 	}
14005 	if (arp_add_mapping_mp != NULL) {
14006 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
14007 		    ill->ill_name, ipif->ipif_id));
14008 		putnext(ill->ill_rq, arp_add_mapping_mp);
14009 	}
14010 	if (res_act != Res_act_initial)
14011 		return (0);
14012 
14013 	if (ill->ill_flags & ILLF_NOARP)
14014 		err = ill_arp_off(ill);
14015 	else
14016 		err = ill_arp_on(ill);
14017 	if (err != 0) {
14018 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
14019 		freemsg(ipif->ipif_arp_del_mp);
14020 		freemsg(ill->ill_arp_down_mp);
14021 		freemsg(ill->ill_arp_del_mapping_mp);
14022 		ipif->ipif_arp_del_mp = NULL;
14023 		ill->ill_arp_down_mp = NULL;
14024 		ill->ill_arp_del_mapping_mp = NULL;
14025 		return (err);
14026 	}
14027 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
14028 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
14029 
14030 failed:
14031 	ip1dbg(("ipif_resolver_up: FAILED\n"));
14032 	freemsg(arp_add_mp);
14033 	freemsg(arp_del_mp);
14034 	freemsg(arp_add_mapping_mp);
14035 	freemsg(arp_up_mp);
14036 	freemsg(arp_down_mp);
14037 	ill->ill_arp_bringup_pending = 0;
14038 	return (err);
14039 }
14040 
14041 /*
14042  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
14043  * just gone back up.
14044  */
14045 static void
14046 ipif_arp_start_dad(ipif_t *ipif)
14047 {
14048 	ill_t *ill = ipif->ipif_ill;
14049 	mblk_t *arp_add_mp;
14050 	area_t *area;
14051 
14052 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
14053 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
14054 	    ipif->ipif_lcl_addr == INADDR_ANY ||
14055 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
14056 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
14057 		/*
14058 		 * If we can't contact ARP for some reason, that's not really a
14059 		 * problem.  Just send out the routing socket notification that
14060 		 * DAD completion would have done, and continue.
14061 		 */
14062 		ipif_mask_reply(ipif);
14063 		ip_rts_ifmsg(ipif);
14064 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14065 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14066 		ipif->ipif_addr_ready = 1;
14067 		return;
14068 	}
14069 
14070 	/* Setting the 'unverified' flag restarts DAD */
14071 	area = (area_t *)arp_add_mp->b_rptr;
14072 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
14073 	    ACE_F_UNVERIFIED;
14074 	putnext(ill->ill_rq, arp_add_mp);
14075 }
14076 
14077 static void
14078 ipif_ndp_start_dad(ipif_t *ipif)
14079 {
14080 	nce_t *nce;
14081 
14082 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
14083 	if (nce == NULL)
14084 		return;
14085 
14086 	if (!ndp_restart_dad(nce)) {
14087 		/*
14088 		 * If we can't restart DAD for some reason, that's not really a
14089 		 * problem.  Just send out the routing socket notification that
14090 		 * DAD completion would have done, and continue.
14091 		 */
14092 		ip_rts_ifmsg(ipif);
14093 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
14094 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
14095 		ipif->ipif_addr_ready = 1;
14096 	}
14097 	NCE_REFRELE(nce);
14098 }
14099 
14100 /*
14101  * Restart duplicate address detection on all interfaces on the given ill.
14102  *
14103  * This is called when an interface transitions from down to up
14104  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
14105  *
14106  * Note that since the underlying physical link has transitioned, we must cause
14107  * at least one routing socket message to be sent here, either via DAD
14108  * completion or just by default on the first ipif.  (If we don't do this, then
14109  * in.mpathd will see long delays when doing link-based failure recovery.)
14110  */
14111 void
14112 ill_restart_dad(ill_t *ill, boolean_t went_up)
14113 {
14114 	ipif_t *ipif;
14115 
14116 	if (ill == NULL)
14117 		return;
14118 
14119 	/*
14120 	 * If layer two doesn't support duplicate address detection, then just
14121 	 * send the routing socket message now and be done with it.
14122 	 */
14123 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
14124 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
14125 		ip_rts_ifmsg(ill->ill_ipif);
14126 		return;
14127 	}
14128 
14129 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14130 		if (went_up) {
14131 			if (ipif->ipif_flags & IPIF_UP) {
14132 				if (ill->ill_isv6)
14133 					ipif_ndp_start_dad(ipif);
14134 				else
14135 					ipif_arp_start_dad(ipif);
14136 			} else if (ill->ill_isv6 &&
14137 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14138 				/*
14139 				 * For IPv4, the ARP module itself will
14140 				 * automatically start the DAD process when it
14141 				 * sees DL_NOTE_LINK_UP.  We respond to the
14142 				 * AR_CN_READY at the completion of that task.
14143 				 * For IPv6, we must kick off the bring-up
14144 				 * process now.
14145 				 */
14146 				ndp_do_recovery(ipif);
14147 			} else {
14148 				/*
14149 				 * Unfortunately, the first ipif is "special"
14150 				 * and represents the underlying ill in the
14151 				 * routing socket messages.  Thus, when this
14152 				 * one ipif is down, we must still notify so
14153 				 * that the user knows the IFF_RUNNING status
14154 				 * change.  (If the first ipif is up, then
14155 				 * we'll handle eventual routing socket
14156 				 * notification via DAD completion.)
14157 				 */
14158 				if (ipif == ill->ill_ipif)
14159 					ip_rts_ifmsg(ill->ill_ipif);
14160 			}
14161 		} else {
14162 			/*
14163 			 * After link down, we'll need to send a new routing
14164 			 * message when the link comes back, so clear
14165 			 * ipif_addr_ready.
14166 			 */
14167 			ipif->ipif_addr_ready = 0;
14168 		}
14169 	}
14170 
14171 	/*
14172 	 * If we've torn down links, then notify the user right away.
14173 	 */
14174 	if (!went_up)
14175 		ip_rts_ifmsg(ill->ill_ipif);
14176 }
14177 
14178 /*
14179  * Wakeup all threads waiting to enter the ipsq, and sleeping
14180  * on any of the ills in this ipsq. The ill_lock of the ill
14181  * must be held so that waiters don't miss wakeups
14182  */
14183 static void
14184 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14185 {
14186 	phyint_t *phyint;
14187 
14188 	phyint = ipsq->ipsq_phyint_list;
14189 	while (phyint != NULL) {
14190 		if (phyint->phyint_illv4) {
14191 			if (!caller_holds_lock)
14192 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14193 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14194 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14195 			if (!caller_holds_lock)
14196 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14197 		}
14198 		if (phyint->phyint_illv6) {
14199 			if (!caller_holds_lock)
14200 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14201 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14202 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14203 			if (!caller_holds_lock)
14204 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14205 		}
14206 		phyint = phyint->phyint_ipsq_next;
14207 	}
14208 }
14209 
14210 static ipsq_t *
14211 ipsq_create(char *groupname)
14212 {
14213 	ipsq_t	*ipsq;
14214 
14215 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
14216 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14217 	if (ipsq == NULL) {
14218 		return (NULL);
14219 	}
14220 
14221 	if (groupname != NULL)
14222 		(void) strcpy(ipsq->ipsq_name, groupname);
14223 	else
14224 		ipsq->ipsq_name[0] = '\0';
14225 
14226 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14227 	ipsq->ipsq_flags |= IPSQ_GROUP;
14228 	ipsq->ipsq_next = ipsq_g_head;
14229 	ipsq_g_head = ipsq;
14230 	return (ipsq);
14231 }
14232 
14233 /*
14234  * Return an ipsq correspoding to the groupname. If 'create' is true
14235  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14236  * uniquely with an IPMP group. However during IPMP groupname operations,
14237  * multiple IPMP groups may be associated with a single ipsq. But no
14238  * IPMP group can be associated with more than 1 ipsq at any time.
14239  * For example
14240  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14241  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14242  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14243  *
14244  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14245  * status shown below during the execution of the above command.
14246  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14247  *
14248  * After the completion of the above groupname command we return to the stable
14249  * state shown below.
14250  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14251  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14252  *
14253  * Because of the above, we don't search based on the ipsq_name since that
14254  * would miss the correct ipsq during certain windows as shown above.
14255  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14256  * natural state.
14257  */
14258 static ipsq_t *
14259 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq)
14260 {
14261 	ipsq_t	*ipsq;
14262 	int	group_len;
14263 	phyint_t *phyint;
14264 
14265 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
14266 
14267 	group_len = strlen(groupname);
14268 	ASSERT(group_len != 0);
14269 	group_len++;
14270 
14271 	for (ipsq = ipsq_g_head; ipsq != NULL; ipsq = ipsq->ipsq_next) {
14272 		/*
14273 		 * When an ipsq is being split, and ill_split_ipsq
14274 		 * calls this function, we exclude it from being considered.
14275 		 */
14276 		if (ipsq == exclude_ipsq)
14277 			continue;
14278 
14279 		/*
14280 		 * Compare against the ipsq_name. The groupname change happens
14281 		 * in 2 phases. The 1st phase merges the from group into
14282 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14283 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14284 		 * ipsq_name. At this point the phyint_groupname has not been
14285 		 * updated.
14286 		 */
14287 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14288 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14289 			/*
14290 			 * Verify that an ipmp groupname is exactly
14291 			 * part of 1 ipsq and is not found in any other
14292 			 * ipsq.
14293 			 */
14294 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq) ==
14295 			    NULL);
14296 			return (ipsq);
14297 		}
14298 
14299 		/*
14300 		 * Comparison against ipsq_name alone is not sufficient.
14301 		 * In the case when groups are currently being
14302 		 * merged, the ipsq could hold other IPMP groups temporarily.
14303 		 * so we walk the phyint list and compare against the
14304 		 * phyint_groupname as well.
14305 		 */
14306 		phyint = ipsq->ipsq_phyint_list;
14307 		while (phyint != NULL) {
14308 			if ((group_len == phyint->phyint_groupname_len) &&
14309 			    (bcmp(phyint->phyint_groupname, groupname,
14310 			    group_len) == 0)) {
14311 				/*
14312 				 * Verify that an ipmp groupname is exactly
14313 				 * part of 1 ipsq and is not found in any other
14314 				 * ipsq.
14315 				 */
14316 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq)
14317 					== NULL);
14318 				return (ipsq);
14319 			}
14320 			phyint = phyint->phyint_ipsq_next;
14321 		}
14322 	}
14323 	if (create)
14324 		ipsq = ipsq_create(groupname);
14325 	return (ipsq);
14326 }
14327 
14328 static void
14329 ipsq_delete(ipsq_t *ipsq)
14330 {
14331 	ipsq_t *nipsq;
14332 	ipsq_t *pipsq = NULL;
14333 
14334 	/*
14335 	 * We don't hold the ipsq lock, but we are sure no new
14336 	 * messages can land up, since the ipsq_refs is zero.
14337 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14338 	 * is associated with this ipsq. (Lookups are based on ill_name
14339 	 * or phyint_group_name)
14340 	 */
14341 	ASSERT(ipsq->ipsq_refs == 0);
14342 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14343 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14344 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14345 		/*
14346 		 * This is not the ipsq of an IPMP group.
14347 		 */
14348 		kmem_free(ipsq, sizeof (ipsq_t));
14349 		return;
14350 	}
14351 
14352 	rw_enter(&ill_g_lock, RW_WRITER);
14353 
14354 	/*
14355 	 * Locate the ipsq  before we can remove it from
14356 	 * the singly linked list of ipsq's.
14357 	 */
14358 	for (nipsq = ipsq_g_head; nipsq != NULL; nipsq = nipsq->ipsq_next) {
14359 		if (nipsq == ipsq) {
14360 			break;
14361 		}
14362 		pipsq = nipsq;
14363 	}
14364 
14365 	ASSERT(nipsq == ipsq);
14366 
14367 	/* unlink ipsq from the list */
14368 	if (pipsq != NULL)
14369 		pipsq->ipsq_next = ipsq->ipsq_next;
14370 	else
14371 		ipsq_g_head = ipsq->ipsq_next;
14372 	kmem_free(ipsq, sizeof (ipsq_t));
14373 	rw_exit(&ill_g_lock);
14374 }
14375 
14376 static void
14377 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14378     queue_t *q)
14379 
14380 {
14381 
14382 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14383 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14384 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14385 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14386 	ASSERT(current_mp != NULL);
14387 
14388 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14389 		NEW_OP, NULL);
14390 
14391 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14392 	    new_ipsq->ipsq_xopq_mphead != NULL);
14393 
14394 	/*
14395 	 * move from old ipsq to the new ipsq.
14396 	 */
14397 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14398 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14399 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14400 
14401 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14402 }
14403 
14404 void
14405 ill_group_cleanup(ill_t *ill)
14406 {
14407 	ill_t *ill_v4;
14408 	ill_t *ill_v6;
14409 	ipif_t *ipif;
14410 
14411 	ill_v4 = ill->ill_phyint->phyint_illv4;
14412 	ill_v6 = ill->ill_phyint->phyint_illv6;
14413 
14414 	if (ill_v4 != NULL) {
14415 		mutex_enter(&ill_v4->ill_lock);
14416 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14417 		    ipif = ipif->ipif_next) {
14418 			IPIF_UNMARK_MOVING(ipif);
14419 		}
14420 		ill_v4->ill_up_ipifs = B_FALSE;
14421 		mutex_exit(&ill_v4->ill_lock);
14422 	}
14423 
14424 	if (ill_v6 != NULL) {
14425 		mutex_enter(&ill_v6->ill_lock);
14426 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14427 		    ipif = ipif->ipif_next) {
14428 			IPIF_UNMARK_MOVING(ipif);
14429 		}
14430 		ill_v6->ill_up_ipifs = B_FALSE;
14431 		mutex_exit(&ill_v6->ill_lock);
14432 	}
14433 }
14434 /*
14435  * This function is called when an ill has had a change in its group status
14436  * to bring up all the ipifs that were up before the change.
14437  */
14438 int
14439 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14440 {
14441 	ipif_t *ipif;
14442 	ill_t *ill_v4;
14443 	ill_t *ill_v6;
14444 	ill_t *from_ill;
14445 	int err = 0;
14446 
14447 
14448 	ASSERT(IAM_WRITER_ILL(ill));
14449 
14450 	/*
14451 	 * Except for ipif_state_flags and ill_state_flags the other
14452 	 * fields of the ipif/ill that are modified below are protected
14453 	 * implicitly since we are a writer. We would have tried to down
14454 	 * even an ipif that was already down, in ill_down_ipifs. So we
14455 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14456 	 */
14457 	ill_v4 = ill->ill_phyint->phyint_illv4;
14458 	ill_v6 = ill->ill_phyint->phyint_illv6;
14459 	if (ill_v4 != NULL) {
14460 		ill_v4->ill_up_ipifs = B_TRUE;
14461 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14462 		    ipif = ipif->ipif_next) {
14463 			mutex_enter(&ill_v4->ill_lock);
14464 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14465 			IPIF_UNMARK_MOVING(ipif);
14466 			mutex_exit(&ill_v4->ill_lock);
14467 			if (ipif->ipif_was_up) {
14468 				if (!(ipif->ipif_flags & IPIF_UP))
14469 					err = ipif_up(ipif, q, mp);
14470 				ipif->ipif_was_up = B_FALSE;
14471 				if (err != 0) {
14472 					/*
14473 					 * Can there be any other error ?
14474 					 */
14475 					ASSERT(err == EINPROGRESS);
14476 					return (err);
14477 				}
14478 			}
14479 		}
14480 		mutex_enter(&ill_v4->ill_lock);
14481 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14482 		mutex_exit(&ill_v4->ill_lock);
14483 		ill_v4->ill_up_ipifs = B_FALSE;
14484 		if (ill_v4->ill_move_in_progress) {
14485 			ASSERT(ill_v4->ill_move_peer != NULL);
14486 			ill_v4->ill_move_in_progress = B_FALSE;
14487 			from_ill = ill_v4->ill_move_peer;
14488 			from_ill->ill_move_in_progress = B_FALSE;
14489 			from_ill->ill_move_peer = NULL;
14490 			mutex_enter(&from_ill->ill_lock);
14491 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14492 			mutex_exit(&from_ill->ill_lock);
14493 			if (ill_v6 == NULL) {
14494 				if (from_ill->ill_phyint->phyint_flags &
14495 				    PHYI_STANDBY) {
14496 					phyint_inactive(from_ill->ill_phyint);
14497 				}
14498 				if (ill_v4->ill_phyint->phyint_flags &
14499 				    PHYI_STANDBY) {
14500 					phyint_inactive(ill_v4->ill_phyint);
14501 				}
14502 			}
14503 			ill_v4->ill_move_peer = NULL;
14504 		}
14505 	}
14506 
14507 	if (ill_v6 != NULL) {
14508 		ill_v6->ill_up_ipifs = B_TRUE;
14509 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14510 		    ipif = ipif->ipif_next) {
14511 			mutex_enter(&ill_v6->ill_lock);
14512 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14513 			IPIF_UNMARK_MOVING(ipif);
14514 			mutex_exit(&ill_v6->ill_lock);
14515 			if (ipif->ipif_was_up) {
14516 				if (!(ipif->ipif_flags & IPIF_UP))
14517 					err = ipif_up(ipif, q, mp);
14518 				ipif->ipif_was_up = B_FALSE;
14519 				if (err != 0) {
14520 					/*
14521 					 * Can there be any other error ?
14522 					 */
14523 					ASSERT(err == EINPROGRESS);
14524 					return (err);
14525 				}
14526 			}
14527 		}
14528 		mutex_enter(&ill_v6->ill_lock);
14529 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14530 		mutex_exit(&ill_v6->ill_lock);
14531 		ill_v6->ill_up_ipifs = B_FALSE;
14532 		if (ill_v6->ill_move_in_progress) {
14533 			ASSERT(ill_v6->ill_move_peer != NULL);
14534 			ill_v6->ill_move_in_progress = B_FALSE;
14535 			from_ill = ill_v6->ill_move_peer;
14536 			from_ill->ill_move_in_progress = B_FALSE;
14537 			from_ill->ill_move_peer = NULL;
14538 			mutex_enter(&from_ill->ill_lock);
14539 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14540 			mutex_exit(&from_ill->ill_lock);
14541 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14542 				phyint_inactive(from_ill->ill_phyint);
14543 			}
14544 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14545 				phyint_inactive(ill_v6->ill_phyint);
14546 			}
14547 			ill_v6->ill_move_peer = NULL;
14548 		}
14549 	}
14550 	return (0);
14551 }
14552 
14553 /*
14554  * bring down all the approriate ipifs.
14555  */
14556 /* ARGSUSED */
14557 static void
14558 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14559 {
14560 	ipif_t *ipif;
14561 
14562 	ASSERT(IAM_WRITER_ILL(ill));
14563 
14564 	/*
14565 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14566 	 * are modified below are protected implicitly since we are a writer
14567 	 */
14568 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14569 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14570 			continue;
14571 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14572 			/*
14573 			 * We go through the ipif_down logic even if the ipif
14574 			 * is already down, since routes can be added based
14575 			 * on down ipifs. Going through ipif_down once again
14576 			 * will delete any IREs created based on these routes.
14577 			 */
14578 			if (ipif->ipif_flags & IPIF_UP)
14579 				ipif->ipif_was_up = B_TRUE;
14580 			/*
14581 			 * If called with chk_nofailover true ipif is moving.
14582 			 */
14583 			mutex_enter(&ill->ill_lock);
14584 			if (chk_nofailover) {
14585 				ipif->ipif_state_flags |=
14586 					IPIF_MOVING | IPIF_CHANGING;
14587 			} else {
14588 				ipif->ipif_state_flags |= IPIF_CHANGING;
14589 			}
14590 			mutex_exit(&ill->ill_lock);
14591 			/*
14592 			 * Need to re-create net/subnet bcast ires if
14593 			 * they are dependent on ipif.
14594 			 */
14595 			if (!ipif->ipif_isv6)
14596 				ipif_check_bcast_ires(ipif);
14597 			(void) ipif_logical_down(ipif, NULL, NULL);
14598 			ipif_non_duplicate(ipif);
14599 			ipif_down_tail(ipif);
14600 			/*
14601 			 * We don't do ipif_multicast_down for IPv4 in
14602 			 * ipif_down. We need to set this so that
14603 			 * ipif_multicast_up will join the
14604 			 * ALLHOSTS_GROUP on to_ill.
14605 			 */
14606 			ipif->ipif_multicast_up = B_FALSE;
14607 		}
14608 	}
14609 }
14610 
14611 #define	IPSQ_INC_REF(ipsq)	{			\
14612 	ASSERT(RW_WRITE_HELD(&ill_g_lock));		\
14613 	(ipsq)->ipsq_refs++;				\
14614 }
14615 
14616 #define	IPSQ_DEC_REF(ipsq)	{			\
14617 	ASSERT(RW_WRITE_HELD(&ill_g_lock));		\
14618 	(ipsq)->ipsq_refs--;				\
14619 	if ((ipsq)->ipsq_refs == 0)				\
14620 		(ipsq)->ipsq_name[0] = '\0'; 		\
14621 }
14622 
14623 /*
14624  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14625  * new_ipsq.
14626  */
14627 static void
14628 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq)
14629 {
14630 	phyint_t *phyint;
14631 	phyint_t *next_phyint;
14632 
14633 	/*
14634 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14635 	 * writer and the ill_lock of the ill in question. Also the dest
14636 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14637 	 */
14638 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
14639 
14640 	phyint = cur_ipsq->ipsq_phyint_list;
14641 	cur_ipsq->ipsq_phyint_list = NULL;
14642 	while (phyint != NULL) {
14643 		next_phyint = phyint->phyint_ipsq_next;
14644 		IPSQ_DEC_REF(cur_ipsq);
14645 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14646 		new_ipsq->ipsq_phyint_list = phyint;
14647 		IPSQ_INC_REF(new_ipsq);
14648 		phyint->phyint_ipsq = new_ipsq;
14649 		phyint = next_phyint;
14650 	}
14651 }
14652 
14653 #define	SPLIT_SUCCESS		0
14654 #define	SPLIT_NOT_NEEDED	1
14655 #define	SPLIT_FAILED		2
14656 
14657 int
14658 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry)
14659 {
14660 	ipsq_t *newipsq = NULL;
14661 
14662 	/*
14663 	 * Assertions denote pre-requisites for changing the ipsq of
14664 	 * a phyint
14665 	 */
14666 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
14667 	/*
14668 	 * <ill-phyint> assocs can't change while ill_g_lock
14669 	 * is held as writer. See ill_phyint_reinit()
14670 	 */
14671 	ASSERT(phyint->phyint_illv4 == NULL ||
14672 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14673 	ASSERT(phyint->phyint_illv6 == NULL ||
14674 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14675 
14676 	if ((phyint->phyint_groupname_len !=
14677 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14678 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14679 	    phyint->phyint_groupname_len) != 0)) {
14680 		/*
14681 		 * Once we fail in creating a new ipsq due to memory shortage,
14682 		 * don't attempt to create new ipsq again, based on another
14683 		 * phyint, since we want all phyints belonging to an IPMP group
14684 		 * to be in the same ipsq even in the event of mem alloc fails.
14685 		 */
14686 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14687 		    cur_ipsq);
14688 		if (newipsq == NULL) {
14689 			/* Memory allocation failure */
14690 			return (SPLIT_FAILED);
14691 		} else {
14692 			/* ipsq_refs protected by ill_g_lock (writer) */
14693 			IPSQ_DEC_REF(cur_ipsq);
14694 			phyint->phyint_ipsq = newipsq;
14695 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14696 			newipsq->ipsq_phyint_list = phyint;
14697 			IPSQ_INC_REF(newipsq);
14698 			return (SPLIT_SUCCESS);
14699 		}
14700 	}
14701 	return (SPLIT_NOT_NEEDED);
14702 }
14703 
14704 /*
14705  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14706  * to do this split
14707  */
14708 static int
14709 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq)
14710 {
14711 	ipsq_t *newipsq;
14712 
14713 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
14714 	/*
14715 	 * <ill-phyint> assocs can't change while ill_g_lock
14716 	 * is held as writer. See ill_phyint_reinit()
14717 	 */
14718 
14719 	ASSERT(phyint->phyint_illv4 == NULL ||
14720 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14721 	ASSERT(phyint->phyint_illv6 == NULL ||
14722 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14723 
14724 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14725 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14726 		/*
14727 		 * ipsq_init failed due to no memory
14728 		 * caller will use the same ipsq
14729 		 */
14730 		return (SPLIT_FAILED);
14731 	}
14732 
14733 	/* ipsq_ref is protected by ill_g_lock (writer) */
14734 	IPSQ_DEC_REF(cur_ipsq);
14735 
14736 	/*
14737 	 * This is a new ipsq that is unknown to the world.
14738 	 * So we don't need to hold ipsq_lock,
14739 	 */
14740 	newipsq = phyint->phyint_ipsq;
14741 	newipsq->ipsq_writer = NULL;
14742 	newipsq->ipsq_reentry_cnt--;
14743 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14744 #ifdef ILL_DEBUG
14745 	newipsq->ipsq_depth = 0;
14746 #endif
14747 
14748 	return (SPLIT_SUCCESS);
14749 }
14750 
14751 /*
14752  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14753  * ipsq's representing their individual groups or themselves. Return
14754  * whether split needs to be retried again later.
14755  */
14756 static boolean_t
14757 ill_split_ipsq(ipsq_t *cur_ipsq)
14758 {
14759 	phyint_t *phyint;
14760 	phyint_t *next_phyint;
14761 	int	error;
14762 	boolean_t need_retry = B_FALSE;
14763 
14764 	phyint = cur_ipsq->ipsq_phyint_list;
14765 	cur_ipsq->ipsq_phyint_list = NULL;
14766 	while (phyint != NULL) {
14767 		next_phyint = phyint->phyint_ipsq_next;
14768 		/*
14769 		 * 'created' will tell us whether the callee actually
14770 		 * created an ipsq. Lack of memory may force the callee
14771 		 * to return without creating an ipsq.
14772 		 */
14773 		if (phyint->phyint_groupname == NULL) {
14774 			error = ill_split_to_own_ipsq(phyint, cur_ipsq);
14775 		} else {
14776 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14777 					need_retry);
14778 		}
14779 
14780 		switch (error) {
14781 		case SPLIT_FAILED:
14782 			need_retry = B_TRUE;
14783 			/* FALLTHRU */
14784 		case SPLIT_NOT_NEEDED:
14785 			/*
14786 			 * Keep it on the list.
14787 			 */
14788 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14789 			cur_ipsq->ipsq_phyint_list = phyint;
14790 			break;
14791 		case SPLIT_SUCCESS:
14792 			break;
14793 		default:
14794 			ASSERT(0);
14795 		}
14796 
14797 		phyint = next_phyint;
14798 	}
14799 	return (need_retry);
14800 }
14801 
14802 /*
14803  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14804  * and return the ills in the list. This list will be
14805  * needed to unlock all the ills later on by the caller.
14806  * The <ill-ipsq> associations could change between the
14807  * lock and unlock. Hence the unlock can't traverse the
14808  * ipsq to get the list of ills.
14809  */
14810 static int
14811 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14812 {
14813 	int	cnt = 0;
14814 	phyint_t	*phyint;
14815 
14816 	/*
14817 	 * The caller holds ill_g_lock to ensure that the ill memberships
14818 	 * of the ipsq don't change
14819 	 */
14820 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
14821 
14822 	phyint = ipsq->ipsq_phyint_list;
14823 	while (phyint != NULL) {
14824 		if (phyint->phyint_illv4 != NULL) {
14825 			ASSERT(cnt < list_max);
14826 			list[cnt++] = phyint->phyint_illv4;
14827 		}
14828 		if (phyint->phyint_illv6 != NULL) {
14829 			ASSERT(cnt < list_max);
14830 			list[cnt++] = phyint->phyint_illv6;
14831 		}
14832 		phyint = phyint->phyint_ipsq_next;
14833 	}
14834 	ill_lock_ills(list, cnt);
14835 	return (cnt);
14836 }
14837 
14838 void
14839 ill_lock_ills(ill_t **list, int cnt)
14840 {
14841 	int	i;
14842 
14843 	if (cnt > 1) {
14844 		boolean_t try_again;
14845 		do {
14846 			try_again = B_FALSE;
14847 			for (i = 0; i < cnt - 1; i++) {
14848 				if (list[i] < list[i + 1]) {
14849 					ill_t	*tmp;
14850 
14851 					/* swap the elements */
14852 					tmp = list[i];
14853 					list[i] = list[i + 1];
14854 					list[i + 1] = tmp;
14855 					try_again = B_TRUE;
14856 				}
14857 			}
14858 		} while (try_again);
14859 	}
14860 
14861 	for (i = 0; i < cnt; i++) {
14862 		if (i == 0) {
14863 			if (list[i] != NULL)
14864 				mutex_enter(&list[i]->ill_lock);
14865 			else
14866 				return;
14867 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14868 			mutex_enter(&list[i]->ill_lock);
14869 		}
14870 	}
14871 }
14872 
14873 void
14874 ill_unlock_ills(ill_t **list, int cnt)
14875 {
14876 	int	i;
14877 
14878 	for (i = 0; i < cnt; i++) {
14879 		if ((i == 0) && (list[i] != NULL)) {
14880 			mutex_exit(&list[i]->ill_lock);
14881 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14882 			mutex_exit(&list[i]->ill_lock);
14883 		}
14884 	}
14885 }
14886 
14887 /*
14888  * Merge all the ills from 1 ipsq group into another ipsq group.
14889  * The source ipsq group is specified by the ipsq associated with
14890  * 'from_ill'. The destination ipsq group is specified by the ipsq
14891  * associated with 'to_ill' or 'groupname' respectively.
14892  * Note that ipsq itself does not have a reference count mechanism
14893  * and functions don't look up an ipsq and pass it around. Instead
14894  * functions pass around an ill or groupname, and the ipsq is looked
14895  * up from the ill or groupname and the required operation performed
14896  * atomically with the lookup on the ipsq.
14897  */
14898 static int
14899 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14900     queue_t *q)
14901 {
14902 	ipsq_t *old_ipsq;
14903 	ipsq_t *new_ipsq;
14904 	ill_t	**ill_list;
14905 	int	cnt;
14906 	size_t	ill_list_size;
14907 	boolean_t became_writer_on_new_sq = B_FALSE;
14908 
14909 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14910 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14911 
14912 	/*
14913 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14914 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14915 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14916 	 */
14917 	rw_enter(&ill_g_lock, RW_WRITER);
14918 
14919 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14920 	if (groupname != NULL)
14921 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL);
14922 	else {
14923 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14924 	}
14925 
14926 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14927 
14928 	/*
14929 	 * both groups are on the same ipsq.
14930 	 */
14931 	if (old_ipsq == new_ipsq) {
14932 		rw_exit(&ill_g_lock);
14933 		return (0);
14934 	}
14935 
14936 	cnt = old_ipsq->ipsq_refs << 1;
14937 	ill_list_size = cnt * sizeof (ill_t *);
14938 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14939 	if (ill_list == NULL) {
14940 		rw_exit(&ill_g_lock);
14941 		return (ENOMEM);
14942 	}
14943 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14944 
14945 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14946 	mutex_enter(&new_ipsq->ipsq_lock);
14947 	if ((new_ipsq->ipsq_writer == NULL &&
14948 		new_ipsq->ipsq_current_ipif == NULL) ||
14949 	    (new_ipsq->ipsq_writer == curthread)) {
14950 		new_ipsq->ipsq_writer = curthread;
14951 		new_ipsq->ipsq_reentry_cnt++;
14952 		became_writer_on_new_sq = B_TRUE;
14953 	}
14954 
14955 	/*
14956 	 * We are holding ill_g_lock as writer and all the ill locks of
14957 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14958 	 * message can land up on the old ipsq even though we don't hold the
14959 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14960 	 */
14961 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14962 
14963 	/*
14964 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14965 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14966 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14967 	 */
14968 	ill_merge_ipsq(old_ipsq, new_ipsq);
14969 
14970 	/*
14971 	 * Mark the new ipsq as needing a split since it is currently
14972 	 * being shared by more than 1 IPMP group. The split will
14973 	 * occur at the end of ipsq_exit
14974 	 */
14975 	new_ipsq->ipsq_split = B_TRUE;
14976 
14977 	/* Now release all the locks */
14978 	mutex_exit(&new_ipsq->ipsq_lock);
14979 	ill_unlock_ills(ill_list, cnt);
14980 	rw_exit(&ill_g_lock);
14981 
14982 	kmem_free(ill_list, ill_list_size);
14983 
14984 	/*
14985 	 * If we succeeded in becoming writer on the new ipsq, then
14986 	 * drain the new ipsq and start processing  all enqueued messages
14987 	 * including the current ioctl we are processing which is either
14988 	 * a set groupname or failover/failback.
14989 	 */
14990 	if (became_writer_on_new_sq)
14991 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
14992 
14993 	/*
14994 	 * syncq has been changed and all the messages have been moved.
14995 	 */
14996 	mutex_enter(&old_ipsq->ipsq_lock);
14997 	old_ipsq->ipsq_current_ipif = NULL;
14998 	old_ipsq->ipsq_current_ioctl = 0;
14999 	mutex_exit(&old_ipsq->ipsq_lock);
15000 	return (EINPROGRESS);
15001 }
15002 
15003 /*
15004  * Delete and add the loopback copy and non-loopback copy of
15005  * the BROADCAST ire corresponding to ill and addr. Used to
15006  * group broadcast ires together when ill becomes part of
15007  * a group.
15008  *
15009  * This function is also called when ill is leaving the group
15010  * so that the ires belonging to the group gets re-grouped.
15011  */
15012 static void
15013 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
15014 {
15015 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
15016 	ire_t **ire_ptpn = &ire_head;
15017 
15018 	/*
15019 	 * The loopback and non-loopback IREs are inserted in the order in which
15020 	 * they're found, on the basis that they are correctly ordered (loopback
15021 	 * first).
15022 	 */
15023 	for (;;) {
15024 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15025 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL);
15026 		if (ire == NULL)
15027 			break;
15028 
15029 		/*
15030 		 * we are passing in KM_SLEEP because it is not easy to
15031 		 * go back to a sane state in case of memory failure.
15032 		 */
15033 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
15034 		ASSERT(nire != NULL);
15035 		bzero(nire, sizeof (ire_t));
15036 		/*
15037 		 * Don't use ire_max_frag directly since we don't
15038 		 * hold on to 'ire' until we add the new ire 'nire' and
15039 		 * we don't want the new ire to have a dangling reference
15040 		 * to 'ire'. The ire_max_frag of a broadcast ire must
15041 		 * be in sync with the ipif_mtu of the associate ipif.
15042 		 * For eg. this happens as a result of SIOCSLIFNAME,
15043 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
15044 		 * the driver. A change in ire_max_frag triggered as
15045 		 * as a result of path mtu discovery, or due to an
15046 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
15047 		 * route change -mtu command does not apply to broadcast ires.
15048 		 *
15049 		 * XXX We need a recovery strategy here if ire_init fails
15050 		 */
15051 		if (ire_init(nire,
15052 		    (uchar_t *)&ire->ire_addr,
15053 		    (uchar_t *)&ire->ire_mask,
15054 		    (uchar_t *)&ire->ire_src_addr,
15055 		    (uchar_t *)&ire->ire_gateway_addr,
15056 		    (uchar_t *)&ire->ire_in_src_addr,
15057 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
15058 			&ire->ire_ipif->ipif_mtu,
15059 		    (ire->ire_nce != NULL ? ire->ire_nce->nce_fp_mp : NULL),
15060 		    ire->ire_rfq,
15061 		    ire->ire_stq,
15062 		    ire->ire_type,
15063 		    (ire->ire_nce != NULL? ire->ire_nce->nce_res_mp : NULL),
15064 		    ire->ire_ipif,
15065 		    ire->ire_in_ill,
15066 		    ire->ire_cmask,
15067 		    ire->ire_phandle,
15068 		    ire->ire_ihandle,
15069 		    ire->ire_flags,
15070 		    &ire->ire_uinfo,
15071 		    NULL,
15072 		    NULL) == NULL) {
15073 			cmn_err(CE_PANIC, "ire_init() failed");
15074 		}
15075 		ire_delete(ire);
15076 		ire_refrele(ire);
15077 
15078 		/*
15079 		 * The newly created IREs are inserted at the tail of the list
15080 		 * starting with ire_head. As we've just allocated them no one
15081 		 * knows about them so it's safe.
15082 		 */
15083 		*ire_ptpn = nire;
15084 		ire_ptpn = &nire->ire_next;
15085 	}
15086 
15087 	for (nire = ire_head; nire != NULL; nire = nire_next) {
15088 		int error;
15089 		ire_t *oire;
15090 		/* unlink the IRE from our list before calling ire_add() */
15091 		nire_next = nire->ire_next;
15092 		nire->ire_next = NULL;
15093 
15094 		/* ire_add adds the ire at the right place in the list */
15095 		oire = nire;
15096 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
15097 		ASSERT(error == 0);
15098 		ASSERT(oire == nire);
15099 		ire_refrele(nire);	/* Held in ire_add */
15100 	}
15101 }
15102 
15103 /*
15104  * This function is usually called when an ill is inserted in
15105  * a group and all the ipifs are already UP. As all the ipifs
15106  * are already UP, the broadcast ires have already been created
15107  * and been inserted. But, ire_add_v4 would not have grouped properly.
15108  * We need to re-group for the benefit of ip_wput_ire which
15109  * expects BROADCAST ires to be grouped properly to avoid sending
15110  * more than one copy of the broadcast packet per group.
15111  *
15112  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
15113  *	  because when ipif_up_done ends up calling this, ires have
15114  *        already been added before illgrp_insert i.e before ill_group
15115  *	  has been initialized.
15116  */
15117 static void
15118 ill_group_bcast_for_xmit(ill_t *ill)
15119 {
15120 	ill_group_t *illgrp;
15121 	ipif_t *ipif;
15122 	ipaddr_t addr;
15123 	ipaddr_t net_mask;
15124 	ipaddr_t subnet_netmask;
15125 
15126 	illgrp = ill->ill_group;
15127 
15128 	/*
15129 	 * This function is called even when an ill is deleted from
15130 	 * the group. Hence, illgrp could be null.
15131 	 */
15132 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15133 		return;
15134 
15135 	/*
15136 	 * Delete all the BROADCAST ires matching this ill and add
15137 	 * them back. This time, ire_add_v4 should take care of
15138 	 * grouping them with others because ill is part of the
15139 	 * group.
15140 	 */
15141 	ill_bcast_delete_and_add(ill, 0);
15142 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15143 
15144 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15145 
15146 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15147 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15148 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15149 		} else {
15150 			net_mask = htonl(IN_CLASSA_NET);
15151 		}
15152 		addr = net_mask & ipif->ipif_subnet;
15153 		ill_bcast_delete_and_add(ill, addr);
15154 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15155 
15156 		subnet_netmask = ipif->ipif_net_mask;
15157 		addr = ipif->ipif_subnet;
15158 		ill_bcast_delete_and_add(ill, addr);
15159 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15160 	}
15161 }
15162 
15163 /*
15164  * This function is called from illgrp_delete when ill is being deleted
15165  * from the group.
15166  *
15167  * As ill is not there in the group anymore, any address belonging
15168  * to this ill should be cleared of IRE_MARK_NORECV.
15169  */
15170 static void
15171 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15172 {
15173 	ire_t *ire;
15174 	irb_t *irb;
15175 
15176 	ASSERT(ill->ill_group == NULL);
15177 
15178 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15179 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL);
15180 
15181 	if (ire != NULL) {
15182 		/*
15183 		 * IPMP and plumbing operations are serialized on the ipsq, so
15184 		 * no one will insert or delete a broadcast ire under our feet.
15185 		 */
15186 		irb = ire->ire_bucket;
15187 		rw_enter(&irb->irb_lock, RW_READER);
15188 		ire_refrele(ire);
15189 
15190 		for (; ire != NULL; ire = ire->ire_next) {
15191 			if (ire->ire_addr != addr)
15192 				break;
15193 			if (ire_to_ill(ire) != ill)
15194 				continue;
15195 
15196 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15197 			ire->ire_marks &= ~IRE_MARK_NORECV;
15198 		}
15199 		rw_exit(&irb->irb_lock);
15200 	}
15201 }
15202 
15203 /*
15204  * This function must be called only after the broadcast ires
15205  * have been grouped together. For a given address addr, nominate
15206  * only one of the ires whose interface is not FAILED or OFFLINE.
15207  *
15208  * This is also called when an ipif goes down, so that we can nominate
15209  * a different ire with the same address for receiving.
15210  */
15211 static void
15212 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr)
15213 {
15214 	irb_t *irb;
15215 	ire_t *ire;
15216 	ire_t *ire1;
15217 	ire_t *save_ire;
15218 	ire_t **irep = NULL;
15219 	boolean_t first = B_TRUE;
15220 	ire_t *clear_ire = NULL;
15221 	ire_t *start_ire = NULL;
15222 	ire_t	*new_lb_ire;
15223 	ire_t	*new_nlb_ire;
15224 	boolean_t new_lb_ire_used = B_FALSE;
15225 	boolean_t new_nlb_ire_used = B_FALSE;
15226 	uint64_t match_flags;
15227 	uint64_t phyi_flags;
15228 	boolean_t fallback = B_FALSE;
15229 
15230 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15231 	    NULL, MATCH_IRE_TYPE);
15232 	/*
15233 	 * We may not be able to find some ires if a previous
15234 	 * ire_create failed. This happens when an ipif goes
15235 	 * down and we are unable to create BROADCAST ires due
15236 	 * to memory failure. Thus, we have to check for NULL
15237 	 * below. This should handle the case for LOOPBACK,
15238 	 * POINTOPOINT and interfaces with some POINTOPOINT
15239 	 * logicals for which there are no BROADCAST ires.
15240 	 */
15241 	if (ire == NULL)
15242 		return;
15243 	/*
15244 	 * Currently IRE_BROADCASTS are deleted when an ipif
15245 	 * goes down which runs exclusively. Thus, setting
15246 	 * IRE_MARK_RCVD should not race with ire_delete marking
15247 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15248 	 * be consistent with other parts of the code that walks
15249 	 * a given bucket.
15250 	 */
15251 	save_ire = ire;
15252 	irb = ire->ire_bucket;
15253 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15254 	if (new_lb_ire == NULL) {
15255 		ire_refrele(ire);
15256 		return;
15257 	}
15258 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15259 	if (new_nlb_ire == NULL) {
15260 		ire_refrele(ire);
15261 		kmem_cache_free(ire_cache, new_lb_ire);
15262 		return;
15263 	}
15264 	IRB_REFHOLD(irb);
15265 	rw_enter(&irb->irb_lock, RW_WRITER);
15266 	/*
15267 	 * Get to the first ire matching the address and the
15268 	 * group. If the address does not match we are done
15269 	 * as we could not find the IRE. If the address matches
15270 	 * we should get to the first one matching the group.
15271 	 */
15272 	while (ire != NULL) {
15273 		if (ire->ire_addr != addr ||
15274 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15275 			break;
15276 		}
15277 		ire = ire->ire_next;
15278 	}
15279 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15280 	start_ire = ire;
15281 redo:
15282 	while (ire != NULL && ire->ire_addr == addr &&
15283 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15284 		/*
15285 		 * The first ire for any address within a group
15286 		 * should always be the one with IRE_MARK_NORECV cleared
15287 		 * so that ip_wput_ire can avoid searching for one.
15288 		 * Note down the insertion point which will be used
15289 		 * later.
15290 		 */
15291 		if (first && (irep == NULL))
15292 			irep = ire->ire_ptpn;
15293 		/*
15294 		 * PHYI_FAILED is set when the interface fails.
15295 		 * This interface might have become good, but the
15296 		 * daemon has not yet detected. We should still
15297 		 * not receive on this. PHYI_OFFLINE should never
15298 		 * be picked as this has been offlined and soon
15299 		 * be removed.
15300 		 */
15301 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15302 		if (phyi_flags & PHYI_OFFLINE) {
15303 			ire->ire_marks |= IRE_MARK_NORECV;
15304 			ire = ire->ire_next;
15305 			continue;
15306 		}
15307 		if (phyi_flags & match_flags) {
15308 			ire->ire_marks |= IRE_MARK_NORECV;
15309 			ire = ire->ire_next;
15310 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15311 			    PHYI_INACTIVE) {
15312 				fallback = B_TRUE;
15313 			}
15314 			continue;
15315 		}
15316 		if (first) {
15317 			/*
15318 			 * We will move this to the front of the list later
15319 			 * on.
15320 			 */
15321 			clear_ire = ire;
15322 			ire->ire_marks &= ~IRE_MARK_NORECV;
15323 		} else {
15324 			ire->ire_marks |= IRE_MARK_NORECV;
15325 		}
15326 		first = B_FALSE;
15327 		ire = ire->ire_next;
15328 	}
15329 	/*
15330 	 * If we never nominated anybody, try nominating at least
15331 	 * an INACTIVE, if we found one. Do it only once though.
15332 	 */
15333 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15334 	    fallback) {
15335 		match_flags = PHYI_FAILED;
15336 		ire = start_ire;
15337 		irep = NULL;
15338 		goto redo;
15339 	}
15340 	ire_refrele(save_ire);
15341 
15342 	/*
15343 	 * irep non-NULL indicates that we entered the while loop
15344 	 * above. If clear_ire is at the insertion point, we don't
15345 	 * have to do anything. clear_ire will be NULL if all the
15346 	 * interfaces are failed.
15347 	 *
15348 	 * We cannot unlink and reinsert the ire at the right place
15349 	 * in the list since there can be other walkers of this bucket.
15350 	 * Instead we delete and recreate the ire
15351 	 */
15352 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15353 		ire_t *clear_ire_stq = NULL;
15354 		mblk_t *fp_mp = NULL, *res_mp = NULL;
15355 
15356 		bzero(new_lb_ire, sizeof (ire_t));
15357 		if (clear_ire->ire_nce != NULL) {
15358 			fp_mp = clear_ire->ire_nce->nce_fp_mp;
15359 			res_mp = clear_ire->ire_nce->nce_res_mp;
15360 		}
15361 		/* XXX We need a recovery strategy here. */
15362 		if (ire_init(new_lb_ire,
15363 		    (uchar_t *)&clear_ire->ire_addr,
15364 		    (uchar_t *)&clear_ire->ire_mask,
15365 		    (uchar_t *)&clear_ire->ire_src_addr,
15366 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15367 		    (uchar_t *)&clear_ire->ire_in_src_addr,
15368 		    &clear_ire->ire_max_frag,
15369 		    fp_mp,
15370 		    clear_ire->ire_rfq,
15371 		    clear_ire->ire_stq,
15372 		    clear_ire->ire_type,
15373 		    res_mp,
15374 		    clear_ire->ire_ipif,
15375 		    clear_ire->ire_in_ill,
15376 		    clear_ire->ire_cmask,
15377 		    clear_ire->ire_phandle,
15378 		    clear_ire->ire_ihandle,
15379 		    clear_ire->ire_flags,
15380 		    &clear_ire->ire_uinfo,
15381 		    NULL,
15382 		    NULL) == NULL)
15383 			cmn_err(CE_PANIC, "ire_init() failed");
15384 		if (clear_ire->ire_stq == NULL) {
15385 			ire_t *ire_next = clear_ire->ire_next;
15386 			if (ire_next != NULL &&
15387 			    ire_next->ire_stq != NULL &&
15388 			    ire_next->ire_addr == clear_ire->ire_addr &&
15389 			    ire_next->ire_ipif->ipif_ill ==
15390 			    clear_ire->ire_ipif->ipif_ill) {
15391 				clear_ire_stq = ire_next;
15392 
15393 				bzero(new_nlb_ire, sizeof (ire_t));
15394 				if (clear_ire_stq->ire_nce != NULL) {
15395 					fp_mp =
15396 					    clear_ire_stq->ire_nce->nce_fp_mp;
15397 					res_mp =
15398 					    clear_ire_stq->ire_nce->nce_res_mp;
15399 				} else {
15400 					fp_mp = res_mp = NULL;
15401 				}
15402 				/* XXX We need a recovery strategy here. */
15403 				if (ire_init(new_nlb_ire,
15404 				    (uchar_t *)&clear_ire_stq->ire_addr,
15405 				    (uchar_t *)&clear_ire_stq->ire_mask,
15406 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15407 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15408 				    (uchar_t *)&clear_ire_stq->ire_in_src_addr,
15409 				    &clear_ire_stq->ire_max_frag,
15410 				    fp_mp,
15411 				    clear_ire_stq->ire_rfq,
15412 				    clear_ire_stq->ire_stq,
15413 				    clear_ire_stq->ire_type,
15414 				    res_mp,
15415 				    clear_ire_stq->ire_ipif,
15416 				    clear_ire_stq->ire_in_ill,
15417 				    clear_ire_stq->ire_cmask,
15418 				    clear_ire_stq->ire_phandle,
15419 				    clear_ire_stq->ire_ihandle,
15420 				    clear_ire_stq->ire_flags,
15421 				    &clear_ire_stq->ire_uinfo,
15422 				    NULL,
15423 				    NULL) == NULL)
15424 					cmn_err(CE_PANIC, "ire_init() failed");
15425 			}
15426 		}
15427 
15428 		/*
15429 		 * Delete the ire. We can't call ire_delete() since
15430 		 * we are holding the bucket lock. We can't release the
15431 		 * bucket lock since we can't allow irep to change. So just
15432 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15433 		 * ire from the list and do the refrele.
15434 		 */
15435 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15436 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15437 
15438 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15439 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15440 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15441 		}
15442 
15443 		/*
15444 		 * Also take care of otherfields like ib/ob pkt count
15445 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15446 		 */
15447 
15448 		/* Add the new ire's. Insert at *irep */
15449 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15450 		ire1 = *irep;
15451 		if (ire1 != NULL)
15452 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15453 		new_lb_ire->ire_next = ire1;
15454 		/* Link the new one in. */
15455 		new_lb_ire->ire_ptpn = irep;
15456 		membar_producer();
15457 		*irep = new_lb_ire;
15458 		new_lb_ire_used = B_TRUE;
15459 		BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
15460 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15461 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15462 
15463 		if (clear_ire_stq != NULL) {
15464 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15465 			irep = &new_lb_ire->ire_next;
15466 			/* Add the new ire. Insert at *irep */
15467 			ire1 = *irep;
15468 			if (ire1 != NULL)
15469 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15470 			new_nlb_ire->ire_next = ire1;
15471 			/* Link the new one in. */
15472 			new_nlb_ire->ire_ptpn = irep;
15473 			membar_producer();
15474 			*irep = new_nlb_ire;
15475 			new_nlb_ire_used = B_TRUE;
15476 			BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
15477 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15478 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15479 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
15480 		}
15481 	}
15482 	rw_exit(&irb->irb_lock);
15483 	if (!new_lb_ire_used)
15484 		kmem_cache_free(ire_cache, new_lb_ire);
15485 	if (!new_nlb_ire_used)
15486 		kmem_cache_free(ire_cache, new_nlb_ire);
15487 	IRB_REFRELE(irb);
15488 }
15489 
15490 /*
15491  * Whenever an ipif goes down we have to renominate a different
15492  * broadcast ire to receive. Whenever an ipif comes up, we need
15493  * to make sure that we have only one nominated to receive.
15494  */
15495 static void
15496 ipif_renominate_bcast(ipif_t *ipif)
15497 {
15498 	ill_t *ill = ipif->ipif_ill;
15499 	ipaddr_t subnet_addr;
15500 	ipaddr_t net_addr;
15501 	ipaddr_t net_mask = 0;
15502 	ipaddr_t subnet_netmask;
15503 	ipaddr_t addr;
15504 	ill_group_t *illgrp;
15505 
15506 	illgrp = ill->ill_group;
15507 	/*
15508 	 * If this is the last ipif going down, it might take
15509 	 * the ill out of the group. In that case ipif_down ->
15510 	 * illgrp_delete takes care of doing the nomination.
15511 	 * ipif_down does not call for this case.
15512 	 */
15513 	ASSERT(illgrp != NULL);
15514 
15515 	/* There could not have been any ires associated with this */
15516 	if (ipif->ipif_subnet == 0)
15517 		return;
15518 
15519 	ill_mark_bcast(illgrp, 0);
15520 	ill_mark_bcast(illgrp, INADDR_BROADCAST);
15521 
15522 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15523 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15524 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15525 	} else {
15526 		net_mask = htonl(IN_CLASSA_NET);
15527 	}
15528 	addr = net_mask & ipif->ipif_subnet;
15529 	ill_mark_bcast(illgrp, addr);
15530 
15531 	net_addr = ~net_mask | addr;
15532 	ill_mark_bcast(illgrp, net_addr);
15533 
15534 	subnet_netmask = ipif->ipif_net_mask;
15535 	addr = ipif->ipif_subnet;
15536 	ill_mark_bcast(illgrp, addr);
15537 
15538 	subnet_addr = ~subnet_netmask | addr;
15539 	ill_mark_bcast(illgrp, subnet_addr);
15540 }
15541 
15542 /*
15543  * Whenever we form or delete ill groups, we need to nominate one set of
15544  * BROADCAST ires for receiving in the group.
15545  *
15546  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15547  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15548  *    for ill_ipif_up_count to be non-zero. This is the only case where
15549  *    ill_ipif_up_count is zero and we would still find the ires.
15550  *
15551  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15552  *    ipif is UP and we just have to do the nomination.
15553  *
15554  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15555  *    from the group. So, we have to do the nomination.
15556  *
15557  * Because of (3), there could be just one ill in the group. But we have
15558  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15559  * Thus, this function does not optimize when there is only one ill as
15560  * it is not correct for (3).
15561  */
15562 static void
15563 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15564 {
15565 	ill_t *ill;
15566 	ipif_t *ipif;
15567 	ipaddr_t subnet_addr;
15568 	ipaddr_t prev_subnet_addr = 0;
15569 	ipaddr_t net_addr;
15570 	ipaddr_t prev_net_addr = 0;
15571 	ipaddr_t net_mask = 0;
15572 	ipaddr_t subnet_netmask;
15573 	ipaddr_t addr;
15574 
15575 	/*
15576 	 * When the last memeber is leaving, there is nothing to
15577 	 * nominate.
15578 	 */
15579 	if (illgrp->illgrp_ill_count == 0) {
15580 		ASSERT(illgrp->illgrp_ill == NULL);
15581 		return;
15582 	}
15583 
15584 	ill = illgrp->illgrp_ill;
15585 	ASSERT(!ill->ill_isv6);
15586 	/*
15587 	 * We assume that ires with same address and belonging to the
15588 	 * same group, has been grouped together. Nominating a *single*
15589 	 * ill in the group for sending and receiving broadcast is done
15590 	 * by making sure that the first BROADCAST ire (which will be
15591 	 * the one returned by ire_ctable_lookup for ip_rput and the
15592 	 * one that will be used in ip_wput_ire) will be the one that
15593 	 * will not have IRE_MARK_NORECV set.
15594 	 *
15595 	 * 1) ip_rput checks and discards packets received on ires marked
15596 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15597 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15598 	 *    first ire in the group for every broadcast address in the group.
15599 	 *    ip_rput will accept packets only on the first ire i.e only
15600 	 *    one copy of the ill.
15601 	 *
15602 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15603 	 *    packet for the whole group. It needs to send out on the ill
15604 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15605 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15606 	 *    the copy echoed back on other port where the ire is not marked
15607 	 *    with IRE_MARK_NORECV.
15608 	 *
15609 	 * Note that we just need to have the first IRE either loopback or
15610 	 * non-loopback (either of them may not exist if ire_create failed
15611 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15612 	 * always hit the first one and hence will always accept one copy.
15613 	 *
15614 	 * We have a broadcast ire per ill for all the unique prefixes
15615 	 * hosted on that ill. As we don't have a way of knowing the
15616 	 * unique prefixes on a given ill and hence in the whole group,
15617 	 * we just call ill_mark_bcast on all the prefixes that exist
15618 	 * in the group. For the common case of one prefix, the code
15619 	 * below optimizes by remebering the last address used for
15620 	 * markng. In the case of multiple prefixes, this will still
15621 	 * optimize depending the order of prefixes.
15622 	 *
15623 	 * The only unique address across the whole group is 0.0.0.0 and
15624 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15625 	 * the first ire in the bucket for receiving and disables the
15626 	 * others.
15627 	 */
15628 	ill_mark_bcast(illgrp, 0);
15629 	ill_mark_bcast(illgrp, INADDR_BROADCAST);
15630 	for (; ill != NULL; ill = ill->ill_group_next) {
15631 
15632 		for (ipif = ill->ill_ipif; ipif != NULL;
15633 		    ipif = ipif->ipif_next) {
15634 
15635 			if (!(ipif->ipif_flags & IPIF_UP) ||
15636 			    ipif->ipif_subnet == 0) {
15637 				continue;
15638 			}
15639 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15640 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15641 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15642 			} else {
15643 				net_mask = htonl(IN_CLASSA_NET);
15644 			}
15645 			addr = net_mask & ipif->ipif_subnet;
15646 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15647 				ill_mark_bcast(illgrp, addr);
15648 				net_addr = ~net_mask | addr;
15649 				ill_mark_bcast(illgrp, net_addr);
15650 			}
15651 			prev_net_addr = addr;
15652 
15653 			subnet_netmask = ipif->ipif_net_mask;
15654 			addr = ipif->ipif_subnet;
15655 			if (prev_subnet_addr == 0 ||
15656 			    prev_subnet_addr != addr) {
15657 				ill_mark_bcast(illgrp, addr);
15658 				subnet_addr = ~subnet_netmask | addr;
15659 				ill_mark_bcast(illgrp, subnet_addr);
15660 			}
15661 			prev_subnet_addr = addr;
15662 		}
15663 	}
15664 }
15665 
15666 /*
15667  * This function is called while forming ill groups.
15668  *
15669  * Currently, we handle only allmulti groups. We want to join
15670  * allmulti on only one of the ills in the groups. In future,
15671  * when we have link aggregation, we may have to join normal
15672  * multicast groups on multiple ills as switch does inbound load
15673  * balancing. Following are the functions that calls this
15674  * function :
15675  *
15676  * 1) ill_recover_multicast : Interface is coming back UP.
15677  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15678  *    will call ill_recover_multicast to recover all the multicast
15679  *    groups. We need to make sure that only one member is joined
15680  *    in the ill group.
15681  *
15682  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15683  *    Somebody is joining allmulti. We need to make sure that only one
15684  *    member is joined in the group.
15685  *
15686  * 3) illgrp_insert : If allmulti has already joined, we need to make
15687  *    sure that only one member is joined in the group.
15688  *
15689  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15690  *    allmulti who we have nominated. We need to pick someother ill.
15691  *
15692  * 5) illgrp_delete : The ill we nominated is leaving the group,
15693  *    we need to pick a new ill to join the group.
15694  *
15695  * For (1), (2), (5) - we just have to check whether there is
15696  * a good ill joined in the group. If we could not find any ills
15697  * joined the group, we should join.
15698  *
15699  * For (4), the one that was nominated to receive, left the group.
15700  * There could be nobody joined in the group when this function is
15701  * called.
15702  *
15703  * For (3) - we need to explicitly check whether there are multiple
15704  * ills joined in the group.
15705  *
15706  * For simplicity, we don't differentiate any of the above cases. We
15707  * just leave the group if it is joined on any of them and join on
15708  * the first good ill.
15709  */
15710 int
15711 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15712 {
15713 	ilm_t *ilm;
15714 	ill_t *ill;
15715 	ill_t *fallback_inactive_ill = NULL;
15716 	ill_t *fallback_failed_ill = NULL;
15717 	int ret = 0;
15718 
15719 	/*
15720 	 * Leave the allmulti on all the ills and start fresh.
15721 	 */
15722 	for (ill = illgrp->illgrp_ill; ill != NULL;
15723 	    ill = ill->ill_group_next) {
15724 		if (ill->ill_join_allmulti)
15725 			(void) ip_leave_allmulti(ill->ill_ipif);
15726 	}
15727 
15728 	/*
15729 	 * Choose a good ill. Fallback to inactive or failed if
15730 	 * none available. We need to fallback to FAILED in the
15731 	 * case where we have 2 interfaces in a group - where
15732 	 * one of them is failed and another is a good one and
15733 	 * the good one (not marked inactive) is leaving the group.
15734 	 */
15735 	ret = 0;
15736 	for (ill = illgrp->illgrp_ill; ill != NULL;
15737 	    ill = ill->ill_group_next) {
15738 		/* Never pick an offline interface */
15739 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15740 			continue;
15741 
15742 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15743 			fallback_failed_ill = ill;
15744 			continue;
15745 		}
15746 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15747 			fallback_inactive_ill = ill;
15748 			continue;
15749 		}
15750 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15751 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15752 				ret = ip_join_allmulti(ill->ill_ipif);
15753 				/*
15754 				 * ip_join_allmulti can fail because of memory
15755 				 * failures. So, make sure we join at least
15756 				 * on one ill.
15757 				 */
15758 				if (ill->ill_join_allmulti)
15759 					return (0);
15760 			}
15761 		}
15762 	}
15763 	if (ret != 0) {
15764 		/*
15765 		 * If we tried nominating above and failed to do so,
15766 		 * return error. We might have tried multiple times.
15767 		 * But, return the latest error.
15768 		 */
15769 		return (ret);
15770 	}
15771 	if ((ill = fallback_inactive_ill) != NULL) {
15772 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15773 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15774 				ret = ip_join_allmulti(ill->ill_ipif);
15775 				return (ret);
15776 			}
15777 		}
15778 	} else if ((ill = fallback_failed_ill) != NULL) {
15779 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15780 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15781 				ret = ip_join_allmulti(ill->ill_ipif);
15782 				return (ret);
15783 			}
15784 		}
15785 	}
15786 	return (0);
15787 }
15788 
15789 /*
15790  * This function is called from illgrp_delete after it is
15791  * deleted from the group to reschedule responsibilities
15792  * to a different ill.
15793  */
15794 static void
15795 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15796 {
15797 	ilm_t	*ilm;
15798 	ipif_t	*ipif;
15799 	ipaddr_t subnet_addr;
15800 	ipaddr_t net_addr;
15801 	ipaddr_t net_mask = 0;
15802 	ipaddr_t subnet_netmask;
15803 	ipaddr_t addr;
15804 
15805 	ASSERT(ill->ill_group == NULL);
15806 	/*
15807 	 * Broadcast Responsibility:
15808 	 *
15809 	 * 1. If this ill has been nominated for receiving broadcast
15810 	 * packets, we need to find a new one. Before we find a new
15811 	 * one, we need to re-group the ires that are part of this new
15812 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15813 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15814 	 * thing for us.
15815 	 *
15816 	 * 2. If this ill was not nominated for receiving broadcast
15817 	 * packets, we need to clear the IRE_MARK_NORECV flag
15818 	 * so that we continue to send up broadcast packets.
15819 	 */
15820 	if (!ill->ill_isv6) {
15821 		/*
15822 		 * Case 1 above : No optimization here. Just redo the
15823 		 * nomination.
15824 		 */
15825 		ill_group_bcast_for_xmit(ill);
15826 		ill_nominate_bcast_rcv(illgrp);
15827 
15828 		/*
15829 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15830 		 */
15831 		ill_clear_bcast_mark(ill, 0);
15832 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15833 
15834 		for (ipif = ill->ill_ipif; ipif != NULL;
15835 		    ipif = ipif->ipif_next) {
15836 
15837 			if (!(ipif->ipif_flags & IPIF_UP) ||
15838 			    ipif->ipif_subnet == 0) {
15839 				continue;
15840 			}
15841 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15842 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15843 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15844 			} else {
15845 				net_mask = htonl(IN_CLASSA_NET);
15846 			}
15847 			addr = net_mask & ipif->ipif_subnet;
15848 			ill_clear_bcast_mark(ill, addr);
15849 
15850 			net_addr = ~net_mask | addr;
15851 			ill_clear_bcast_mark(ill, net_addr);
15852 
15853 			subnet_netmask = ipif->ipif_net_mask;
15854 			addr = ipif->ipif_subnet;
15855 			ill_clear_bcast_mark(ill, addr);
15856 
15857 			subnet_addr = ~subnet_netmask | addr;
15858 			ill_clear_bcast_mark(ill, subnet_addr);
15859 		}
15860 	}
15861 
15862 	/*
15863 	 * Multicast Responsibility.
15864 	 *
15865 	 * If we have joined allmulti on this one, find a new member
15866 	 * in the group to join allmulti. As this ill is already part
15867 	 * of allmulti, we don't have to join on this one.
15868 	 *
15869 	 * If we have not joined allmulti on this one, there is no
15870 	 * responsibility to handoff. But we need to take new
15871 	 * responsibility i.e, join allmulti on this one if we need
15872 	 * to.
15873 	 */
15874 	if (ill->ill_join_allmulti) {
15875 		(void) ill_nominate_mcast_rcv(illgrp);
15876 	} else {
15877 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15878 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15879 				(void) ip_join_allmulti(ill->ill_ipif);
15880 				break;
15881 			}
15882 		}
15883 	}
15884 
15885 	/*
15886 	 * We intentionally do the flushing of IRE_CACHES only matching
15887 	 * on the ill and not on groups. Note that we are already deleted
15888 	 * from the group.
15889 	 *
15890 	 * This will make sure that all IRE_CACHES whose stq is pointing
15891 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15892 	 * deleted and IRE_CACHES that are not pointing at this ill will
15893 	 * be left alone.
15894 	 */
15895 	if (ill->ill_isv6) {
15896 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15897 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15898 	} else {
15899 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15900 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15901 	}
15902 
15903 	/*
15904 	 * Some conn may have cached one of the IREs deleted above. By removing
15905 	 * the ire reference, we clean up the extra reference to the ill held in
15906 	 * ire->ire_stq.
15907 	 */
15908 	ipcl_walk(conn_cleanup_stale_ire, NULL);
15909 
15910 	/*
15911 	 * Re-do source address selection for all the members in the
15912 	 * group, if they borrowed source address from one of the ipifs
15913 	 * in this ill.
15914 	 */
15915 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15916 		if (ill->ill_isv6) {
15917 			ipif_update_other_ipifs_v6(ipif, illgrp);
15918 		} else {
15919 			ipif_update_other_ipifs(ipif, illgrp);
15920 		}
15921 	}
15922 }
15923 
15924 /*
15925  * Delete the ill from the group. The caller makes sure that it is
15926  * in a group and it okay to delete from the group. So, we always
15927  * delete here.
15928  */
15929 static void
15930 illgrp_delete(ill_t *ill)
15931 {
15932 	ill_group_t *illgrp;
15933 	ill_group_t *tmpg;
15934 	ill_t *tmp_ill;
15935 
15936 	/*
15937 	 * Reset illgrp_ill_schednext if it was pointing at us.
15938 	 * We need to do this before we set ill_group to NULL.
15939 	 */
15940 	rw_enter(&ill_g_lock, RW_WRITER);
15941 	mutex_enter(&ill->ill_lock);
15942 
15943 	illgrp_reset_schednext(ill);
15944 
15945 	illgrp = ill->ill_group;
15946 
15947 	/* Delete the ill from illgrp. */
15948 	if (illgrp->illgrp_ill == ill) {
15949 		illgrp->illgrp_ill = ill->ill_group_next;
15950 	} else {
15951 		tmp_ill = illgrp->illgrp_ill;
15952 		while (tmp_ill->ill_group_next != ill) {
15953 			tmp_ill = tmp_ill->ill_group_next;
15954 			ASSERT(tmp_ill != NULL);
15955 		}
15956 		tmp_ill->ill_group_next = ill->ill_group_next;
15957 	}
15958 	ill->ill_group = NULL;
15959 	ill->ill_group_next = NULL;
15960 
15961 	illgrp->illgrp_ill_count--;
15962 	mutex_exit(&ill->ill_lock);
15963 	rw_exit(&ill_g_lock);
15964 
15965 	/*
15966 	 * As this ill is leaving the group, we need to hand off
15967 	 * the responsibilities to the other ills in the group, if
15968 	 * this ill had some responsibilities.
15969 	 */
15970 
15971 	ill_handoff_responsibility(ill, illgrp);
15972 
15973 	rw_enter(&ill_g_lock, RW_WRITER);
15974 
15975 	if (illgrp->illgrp_ill_count == 0) {
15976 
15977 		ASSERT(illgrp->illgrp_ill == NULL);
15978 		if (ill->ill_isv6) {
15979 			if (illgrp == illgrp_head_v6) {
15980 				illgrp_head_v6 = illgrp->illgrp_next;
15981 			} else {
15982 				tmpg = illgrp_head_v6;
15983 				while (tmpg->illgrp_next != illgrp) {
15984 					tmpg = tmpg->illgrp_next;
15985 					ASSERT(tmpg != NULL);
15986 				}
15987 				tmpg->illgrp_next = illgrp->illgrp_next;
15988 			}
15989 		} else {
15990 			if (illgrp == illgrp_head_v4) {
15991 				illgrp_head_v4 = illgrp->illgrp_next;
15992 			} else {
15993 				tmpg = illgrp_head_v4;
15994 				while (tmpg->illgrp_next != illgrp) {
15995 					tmpg = tmpg->illgrp_next;
15996 					ASSERT(tmpg != NULL);
15997 				}
15998 				tmpg->illgrp_next = illgrp->illgrp_next;
15999 			}
16000 		}
16001 		mutex_destroy(&illgrp->illgrp_lock);
16002 		mi_free(illgrp);
16003 	}
16004 	rw_exit(&ill_g_lock);
16005 
16006 	/*
16007 	 * Even though the ill is out of the group its not necessary
16008 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
16009 	 * We will split the ipsq when phyint_groupname is set to NULL.
16010 	 */
16011 
16012 	/*
16013 	 * Send a routing sockets message if we are deleting from
16014 	 * groups with names.
16015 	 */
16016 	if (ill->ill_phyint->phyint_groupname_len != 0)
16017 		ip_rts_ifmsg(ill->ill_ipif);
16018 }
16019 
16020 /*
16021  * Re-do source address selection. This is normally called when
16022  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
16023  * ipif comes up.
16024  */
16025 void
16026 ill_update_source_selection(ill_t *ill)
16027 {
16028 	ipif_t *ipif;
16029 
16030 	ASSERT(IAM_WRITER_ILL(ill));
16031 
16032 	if (ill->ill_group != NULL)
16033 		ill = ill->ill_group->illgrp_ill;
16034 
16035 	for (; ill != NULL; ill = ill->ill_group_next) {
16036 		for (ipif = ill->ill_ipif; ipif != NULL;
16037 		    ipif = ipif->ipif_next) {
16038 			if (ill->ill_isv6)
16039 				ipif_recreate_interface_routes_v6(NULL, ipif);
16040 			else
16041 				ipif_recreate_interface_routes(NULL, ipif);
16042 		}
16043 	}
16044 }
16045 
16046 /*
16047  * Insert ill in a group headed by illgrp_head. The caller can either
16048  * pass a groupname in which case we search for a group with the
16049  * same name to insert in or pass a group to insert in. This function
16050  * would only search groups with names.
16051  *
16052  * NOTE : The caller should make sure that there is at least one ipif
16053  *	  UP on this ill so that illgrp_scheduler can pick this ill
16054  *	  for outbound packets. If ill_ipif_up_count is zero, we have
16055  *	  already sent a DL_UNBIND to the driver and we don't want to
16056  *	  send anymore packets. We don't assert for ipif_up_count
16057  *	  to be greater than zero, because ipif_up_done wants to call
16058  *	  this function before bumping up the ipif_up_count. See
16059  *	  ipif_up_done() for details.
16060  */
16061 int
16062 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
16063     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
16064 {
16065 	ill_group_t *illgrp;
16066 	ill_t *prev_ill;
16067 	phyint_t *phyi;
16068 
16069 	ASSERT(ill->ill_group == NULL);
16070 
16071 	rw_enter(&ill_g_lock, RW_WRITER);
16072 	mutex_enter(&ill->ill_lock);
16073 
16074 	if (groupname != NULL) {
16075 		/*
16076 		 * Look for a group with a matching groupname to insert.
16077 		 */
16078 		for (illgrp = *illgrp_head; illgrp != NULL;
16079 		    illgrp = illgrp->illgrp_next) {
16080 
16081 			ill_t *tmp_ill;
16082 
16083 			/*
16084 			 * If we have an ill_group_t in the list which has
16085 			 * no ill_t assigned then we must be in the process of
16086 			 * removing this group. We skip this as illgrp_delete()
16087 			 * will remove it from the list.
16088 			 */
16089 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
16090 				ASSERT(illgrp->illgrp_ill_count == 0);
16091 				continue;
16092 			}
16093 
16094 			ASSERT(tmp_ill->ill_phyint != NULL);
16095 			phyi = tmp_ill->ill_phyint;
16096 			/*
16097 			 * Look at groups which has names only.
16098 			 */
16099 			if (phyi->phyint_groupname_len == 0)
16100 				continue;
16101 			/*
16102 			 * Names are stored in the phyint common to both
16103 			 * IPv4 and IPv6.
16104 			 */
16105 			if (mi_strcmp(phyi->phyint_groupname,
16106 			    groupname) == 0) {
16107 				break;
16108 			}
16109 		}
16110 	} else {
16111 		/*
16112 		 * If the caller passes in a NULL "grp_to_insert", we
16113 		 * allocate one below and insert this singleton.
16114 		 */
16115 		illgrp = grp_to_insert;
16116 	}
16117 
16118 	ill->ill_group_next = NULL;
16119 
16120 	if (illgrp == NULL) {
16121 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16122 		if (illgrp == NULL) {
16123 			return (ENOMEM);
16124 		}
16125 		illgrp->illgrp_next = *illgrp_head;
16126 		*illgrp_head = illgrp;
16127 		illgrp->illgrp_ill = ill;
16128 		illgrp->illgrp_ill_count = 1;
16129 		ill->ill_group = illgrp;
16130 		/*
16131 		 * Used in illgrp_scheduler to protect multiple threads
16132 		 * from traversing the list.
16133 		 */
16134 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16135 	} else {
16136 		ASSERT(ill->ill_net_type ==
16137 		    illgrp->illgrp_ill->ill_net_type);
16138 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16139 
16140 		/* Insert ill at tail of this group */
16141 		prev_ill = illgrp->illgrp_ill;
16142 		while (prev_ill->ill_group_next != NULL)
16143 			prev_ill = prev_ill->ill_group_next;
16144 		prev_ill->ill_group_next = ill;
16145 		ill->ill_group = illgrp;
16146 		illgrp->illgrp_ill_count++;
16147 		/*
16148 		 * Inherit group properties. Currently only forwarding
16149 		 * is the property we try to keep the same with all the
16150 		 * ills. When there are more, we will abstract this into
16151 		 * a function.
16152 		 */
16153 		ill->ill_flags &= ~ILLF_ROUTER;
16154 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16155 	}
16156 	mutex_exit(&ill->ill_lock);
16157 	rw_exit(&ill_g_lock);
16158 
16159 	/*
16160 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16161 	 *    may be zero as it has not yet been bumped. But the ires
16162 	 *    have already been added. So, we do the nomination here
16163 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16164 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16165 	 *    ill_ipif_up_count here while nominating broadcast ires for
16166 	 *    receive.
16167 	 *
16168 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16169 	 *    to group them properly as ire_add() has already happened
16170 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16171 	 *    case, we need to do it here anyway.
16172 	 */
16173 	if (!ill->ill_isv6) {
16174 		ill_group_bcast_for_xmit(ill);
16175 		ill_nominate_bcast_rcv(illgrp);
16176 	}
16177 
16178 	if (!ipif_is_coming_up) {
16179 		/*
16180 		 * When ipif_up_done() calls this function, the multicast
16181 		 * groups have not been joined yet. So, there is no point in
16182 		 * nomination. ip_join_allmulti will handle groups when
16183 		 * ill_recover_multicast is called from ipif_up_done() later.
16184 		 */
16185 		(void) ill_nominate_mcast_rcv(illgrp);
16186 		/*
16187 		 * ipif_up_done calls ill_update_source_selection
16188 		 * anyway. Moreover, we don't want to re-create
16189 		 * interface routes while ipif_up_done() still has reference
16190 		 * to them. Refer to ipif_up_done() for more details.
16191 		 */
16192 		ill_update_source_selection(ill);
16193 	}
16194 
16195 	/*
16196 	 * Send a routing sockets message if we are inserting into
16197 	 * groups with names.
16198 	 */
16199 	if (groupname != NULL)
16200 		ip_rts_ifmsg(ill->ill_ipif);
16201 	return (0);
16202 }
16203 
16204 /*
16205  * Return the first phyint matching the groupname. There could
16206  * be more than one when there are ill groups.
16207  *
16208  * Needs work: called only from ip_sioctl_groupname
16209  */
16210 static phyint_t *
16211 phyint_lookup_group(char *groupname)
16212 {
16213 	phyint_t *phyi;
16214 
16215 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
16216 	/*
16217 	 * Group names are stored in the phyint - a common structure
16218 	 * to both IPv4 and IPv6.
16219 	 */
16220 	phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
16221 	for (; phyi != NULL;
16222 	    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
16223 	    phyi, AVL_AFTER)) {
16224 		if (phyi->phyint_groupname_len == 0)
16225 			continue;
16226 		ASSERT(phyi->phyint_groupname != NULL);
16227 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16228 			return (phyi);
16229 	}
16230 	return (NULL);
16231 }
16232 
16233 
16234 
16235 /*
16236  * MT notes on creation and deletion of IPMP groups
16237  *
16238  * Creation and deletion of IPMP groups introduce the need to merge or
16239  * split the associated serialization objects i.e the ipsq's. Normally all
16240  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16241  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16242  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16243  * is a need to change the <ill-ipsq> association and we have to operate on both
16244  * the source and destination IPMP groups. For eg. attempting to set the
16245  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16246  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16247  * source or destination IPMP group are mapped to a single ipsq for executing
16248  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16249  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16250  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16251  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16252  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16253  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16254  *
16255  * In the above example the ioctl handling code locates the current ipsq of hme0
16256  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16257  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16258  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16259  * the destination ipsq. If the destination ipsq is not busy, it also enters
16260  * the destination ipsq exclusively. Now the actual groupname setting operation
16261  * can proceed. If the destination ipsq is busy, the operation is enqueued
16262  * on the destination (merged) ipsq and will be handled in the unwind from
16263  * ipsq_exit.
16264  *
16265  * To prevent other threads accessing the ill while the group name change is
16266  * in progres, we bring down the ipifs which also removes the ill from the
16267  * group. The group is changed in phyint and when the first ipif on the ill
16268  * is brought up, the ill is inserted into the right IPMP group by
16269  * illgrp_insert.
16270  */
16271 /* ARGSUSED */
16272 int
16273 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16274     ip_ioctl_cmd_t *ipip, void *ifreq)
16275 {
16276 	int i;
16277 	char *tmp;
16278 	int namelen;
16279 	ill_t *ill = ipif->ipif_ill;
16280 	ill_t *ill_v4, *ill_v6;
16281 	int err = 0;
16282 	phyint_t *phyi;
16283 	phyint_t *phyi_tmp;
16284 	struct lifreq *lifr;
16285 	mblk_t	*mp1;
16286 	char *groupname;
16287 	ipsq_t *ipsq;
16288 
16289 	ASSERT(IAM_WRITER_IPIF(ipif));
16290 
16291 	/* Existance verified in ip_wput_nondata */
16292 	mp1 = mp->b_cont->b_cont;
16293 	lifr = (struct lifreq *)mp1->b_rptr;
16294 	groupname = lifr->lifr_groupname;
16295 
16296 	if (ipif->ipif_id != 0)
16297 		return (EINVAL);
16298 
16299 	phyi = ill->ill_phyint;
16300 	ASSERT(phyi != NULL);
16301 
16302 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16303 		return (EINVAL);
16304 
16305 	tmp = groupname;
16306 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16307 		;
16308 
16309 	if (i == LIFNAMSIZ) {
16310 		/* no null termination */
16311 		return (EINVAL);
16312 	}
16313 
16314 	/*
16315 	 * Calculate the namelen exclusive of the null
16316 	 * termination character.
16317 	 */
16318 	namelen = tmp - groupname;
16319 
16320 	ill_v4 = phyi->phyint_illv4;
16321 	ill_v6 = phyi->phyint_illv6;
16322 
16323 	/*
16324 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16325 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16326 	 * synchronization notes in ip.c
16327 	 */
16328 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16329 		return (EINVAL);
16330 	}
16331 
16332 	/*
16333 	 * mark the ill as changing.
16334 	 * this should queue all new requests on the syncq.
16335 	 */
16336 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16337 
16338 	if (ill_v4 != NULL)
16339 		ill_v4->ill_state_flags |= ILL_CHANGING;
16340 	if (ill_v6 != NULL)
16341 		ill_v6->ill_state_flags |= ILL_CHANGING;
16342 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16343 
16344 	if (namelen == 0) {
16345 		/*
16346 		 * Null string means remove this interface from the
16347 		 * existing group.
16348 		 */
16349 		if (phyi->phyint_groupname_len == 0) {
16350 			/*
16351 			 * Never was in a group.
16352 			 */
16353 			err = 0;
16354 			goto done;
16355 		}
16356 
16357 		/*
16358 		 * IPv4 or IPv6 may be temporarily out of the group when all
16359 		 * the ipifs are down. Thus, we need to check for ill_group to
16360 		 * be non-NULL.
16361 		 */
16362 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16363 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16364 			mutex_enter(&ill_v4->ill_lock);
16365 			if (!ill_is_quiescent(ill_v4)) {
16366 				/*
16367 				 * ipsq_pending_mp_add will not fail since
16368 				 * connp is NULL
16369 				 */
16370 				(void) ipsq_pending_mp_add(NULL,
16371 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16372 				mutex_exit(&ill_v4->ill_lock);
16373 				err = EINPROGRESS;
16374 				goto done;
16375 			}
16376 			mutex_exit(&ill_v4->ill_lock);
16377 		}
16378 
16379 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16380 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16381 			mutex_enter(&ill_v6->ill_lock);
16382 			if (!ill_is_quiescent(ill_v6)) {
16383 				(void) ipsq_pending_mp_add(NULL,
16384 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16385 				mutex_exit(&ill_v6->ill_lock);
16386 				err = EINPROGRESS;
16387 				goto done;
16388 			}
16389 			mutex_exit(&ill_v6->ill_lock);
16390 		}
16391 
16392 		rw_enter(&ill_g_lock, RW_WRITER);
16393 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16394 		mutex_enter(&phyi->phyint_lock);
16395 		ASSERT(phyi->phyint_groupname != NULL);
16396 		mi_free(phyi->phyint_groupname);
16397 		phyi->phyint_groupname = NULL;
16398 		phyi->phyint_groupname_len = 0;
16399 		mutex_exit(&phyi->phyint_lock);
16400 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16401 		rw_exit(&ill_g_lock);
16402 		err = ill_up_ipifs(ill, q, mp);
16403 
16404 		/*
16405 		 * set the split flag so that the ipsq can be split
16406 		 */
16407 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16408 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16409 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16410 
16411 	} else {
16412 		if (phyi->phyint_groupname_len != 0) {
16413 			ASSERT(phyi->phyint_groupname != NULL);
16414 			/* Are we inserting in the same group ? */
16415 			if (mi_strcmp(groupname,
16416 			    phyi->phyint_groupname) == 0) {
16417 				err = 0;
16418 				goto done;
16419 			}
16420 		}
16421 
16422 		rw_enter(&ill_g_lock, RW_READER);
16423 		/*
16424 		 * Merge ipsq for the group's.
16425 		 * This check is here as multiple groups/ills might be
16426 		 * sharing the same ipsq.
16427 		 * If we have to merege than the operation is restarted
16428 		 * on the new ipsq.
16429 		 */
16430 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL);
16431 		if (phyi->phyint_ipsq != ipsq) {
16432 			rw_exit(&ill_g_lock);
16433 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16434 			goto done;
16435 		}
16436 		/*
16437 		 * Running exclusive on new ipsq.
16438 		 */
16439 
16440 		ASSERT(ipsq != NULL);
16441 		ASSERT(ipsq->ipsq_writer == curthread);
16442 
16443 		/*
16444 		 * Check whether the ill_type and ill_net_type matches before
16445 		 * we allocate any memory so that the cleanup is easier.
16446 		 *
16447 		 * We can't group dissimilar ones as we can't load spread
16448 		 * packets across the group because of potential link-level
16449 		 * header differences.
16450 		 */
16451 		phyi_tmp = phyint_lookup_group(groupname);
16452 		if (phyi_tmp != NULL) {
16453 			if ((ill_v4 != NULL &&
16454 			    phyi_tmp->phyint_illv4 != NULL) &&
16455 			    ((ill_v4->ill_net_type !=
16456 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16457 			    (ill_v4->ill_type !=
16458 			    phyi_tmp->phyint_illv4->ill_type))) {
16459 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16460 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16461 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16462 				rw_exit(&ill_g_lock);
16463 				return (EINVAL);
16464 			}
16465 			if ((ill_v6 != NULL &&
16466 			    phyi_tmp->phyint_illv6 != NULL) &&
16467 			    ((ill_v6->ill_net_type !=
16468 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16469 			    (ill_v6->ill_type !=
16470 			    phyi_tmp->phyint_illv6->ill_type))) {
16471 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16472 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16473 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16474 				rw_exit(&ill_g_lock);
16475 				return (EINVAL);
16476 			}
16477 		}
16478 
16479 		rw_exit(&ill_g_lock);
16480 
16481 		/*
16482 		 * bring down all v4 ipifs.
16483 		 */
16484 		if (ill_v4 != NULL) {
16485 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16486 		}
16487 
16488 		/*
16489 		 * bring down all v6 ipifs.
16490 		 */
16491 		if (ill_v6 != NULL) {
16492 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16493 		}
16494 
16495 		/*
16496 		 * make sure all ipifs are down and there are no active
16497 		 * references. Call to ipsq_pending_mp_add will not fail
16498 		 * since connp is NULL.
16499 		 */
16500 		if (ill_v4 != NULL) {
16501 			mutex_enter(&ill_v4->ill_lock);
16502 			if (!ill_is_quiescent(ill_v4)) {
16503 				(void) ipsq_pending_mp_add(NULL,
16504 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16505 				mutex_exit(&ill_v4->ill_lock);
16506 				err = EINPROGRESS;
16507 				goto done;
16508 			}
16509 			mutex_exit(&ill_v4->ill_lock);
16510 		}
16511 
16512 		if (ill_v6 != NULL) {
16513 			mutex_enter(&ill_v6->ill_lock);
16514 			if (!ill_is_quiescent(ill_v6)) {
16515 				(void) ipsq_pending_mp_add(NULL,
16516 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16517 				mutex_exit(&ill_v6->ill_lock);
16518 				err = EINPROGRESS;
16519 				goto done;
16520 			}
16521 			mutex_exit(&ill_v6->ill_lock);
16522 		}
16523 
16524 		/*
16525 		 * allocate including space for null terminator
16526 		 * before we insert.
16527 		 */
16528 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16529 		if (tmp == NULL)
16530 			return (ENOMEM);
16531 
16532 		rw_enter(&ill_g_lock, RW_WRITER);
16533 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16534 		mutex_enter(&phyi->phyint_lock);
16535 		if (phyi->phyint_groupname_len != 0) {
16536 			ASSERT(phyi->phyint_groupname != NULL);
16537 			mi_free(phyi->phyint_groupname);
16538 		}
16539 
16540 		/*
16541 		 * setup the new group name.
16542 		 */
16543 		phyi->phyint_groupname = tmp;
16544 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16545 		phyi->phyint_groupname_len = namelen + 1;
16546 		mutex_exit(&phyi->phyint_lock);
16547 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16548 		rw_exit(&ill_g_lock);
16549 
16550 		err = ill_up_ipifs(ill, q, mp);
16551 	}
16552 
16553 done:
16554 	/*
16555 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16556 	 */
16557 	if (err != EINPROGRESS) {
16558 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16559 		if (ill_v4 != NULL)
16560 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16561 		if (ill_v6 != NULL)
16562 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16563 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16564 	}
16565 	return (err);
16566 }
16567 
16568 /* ARGSUSED */
16569 int
16570 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16571     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16572 {
16573 	ill_t *ill;
16574 	phyint_t *phyi;
16575 	struct lifreq *lifr;
16576 	mblk_t	*mp1;
16577 
16578 	/* Existence verified in ip_wput_nondata */
16579 	mp1 = mp->b_cont->b_cont;
16580 	lifr = (struct lifreq *)mp1->b_rptr;
16581 	ill = ipif->ipif_ill;
16582 	phyi = ill->ill_phyint;
16583 
16584 	lifr->lifr_groupname[0] = '\0';
16585 	/*
16586 	 * ill_group may be null if all the interfaces
16587 	 * are down. But still, the phyint should always
16588 	 * hold the name.
16589 	 */
16590 	if (phyi->phyint_groupname_len != 0) {
16591 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16592 		    phyi->phyint_groupname_len);
16593 	}
16594 
16595 	return (0);
16596 }
16597 
16598 
16599 typedef struct conn_move_s {
16600 	ill_t	*cm_from_ill;
16601 	ill_t	*cm_to_ill;
16602 	int	cm_ifindex;
16603 } conn_move_t;
16604 
16605 /*
16606  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16607  */
16608 static void
16609 conn_move(conn_t *connp, caddr_t arg)
16610 {
16611 	conn_move_t *connm;
16612 	int ifindex;
16613 	int i;
16614 	ill_t *from_ill;
16615 	ill_t *to_ill;
16616 	ilg_t *ilg;
16617 	ilm_t *ret_ilm;
16618 
16619 	connm = (conn_move_t *)arg;
16620 	ifindex = connm->cm_ifindex;
16621 	from_ill = connm->cm_from_ill;
16622 	to_ill = connm->cm_to_ill;
16623 
16624 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16625 
16626 	/* All multicast fields protected by conn_lock */
16627 	mutex_enter(&connp->conn_lock);
16628 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16629 	if ((connp->conn_outgoing_ill == from_ill) &&
16630 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16631 		connp->conn_outgoing_ill = to_ill;
16632 		connp->conn_incoming_ill = to_ill;
16633 	}
16634 
16635 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16636 
16637 	if ((connp->conn_multicast_ill == from_ill) &&
16638 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16639 		connp->conn_multicast_ill = connm->cm_to_ill;
16640 	}
16641 
16642 	/* Change IP_XMIT_IF associations */
16643 	if ((connp->conn_xmit_if_ill == from_ill) &&
16644 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
16645 		connp->conn_xmit_if_ill = to_ill;
16646 	}
16647 	/*
16648 	 * Change the ilg_ill to point to the new one. This assumes
16649 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16650 	 * has been told to receive packets on this interface.
16651 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16652 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16653 	 * some ilms may not have moved. We check to see whether
16654 	 * the ilms have moved to to_ill. We can't check on from_ill
16655 	 * as in the process of moving, we could have split an ilm
16656 	 * in to two - which has the same orig_ifindex and v6group.
16657 	 *
16658 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16659 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16660 	 */
16661 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16662 		ilg = &connp->conn_ilg[i];
16663 		if ((ilg->ilg_ill == from_ill) &&
16664 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16665 			/* ifindex != 0 indicates failback */
16666 			if (ifindex != 0) {
16667 				connp->conn_ilg[i].ilg_ill = to_ill;
16668 				continue;
16669 			}
16670 
16671 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16672 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16673 			    connp->conn_zoneid);
16674 
16675 			if (ret_ilm != NULL)
16676 				connp->conn_ilg[i].ilg_ill = to_ill;
16677 		}
16678 	}
16679 	mutex_exit(&connp->conn_lock);
16680 }
16681 
16682 static void
16683 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16684 {
16685 	conn_move_t connm;
16686 
16687 	connm.cm_from_ill = from_ill;
16688 	connm.cm_to_ill = to_ill;
16689 	connm.cm_ifindex = ifindex;
16690 
16691 	ipcl_walk(conn_move, (caddr_t)&connm);
16692 }
16693 
16694 /*
16695  * ilm has been moved from from_ill to to_ill.
16696  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16697  * appropriately.
16698  *
16699  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16700  *	  the code there de-references ipif_ill to get the ill to
16701  *	  send multicast requests. It does not work as ipif is on its
16702  *	  move and already moved when this function is called.
16703  *	  Thus, we need to use from_ill and to_ill send down multicast
16704  *	  requests.
16705  */
16706 static void
16707 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16708 {
16709 	ipif_t *ipif;
16710 	ilm_t *ilm;
16711 
16712 	/*
16713 	 * See whether we need to send down DL_ENABMULTI_REQ on
16714 	 * to_ill as ilm has just been added.
16715 	 */
16716 	ASSERT(IAM_WRITER_ILL(to_ill));
16717 	ASSERT(IAM_WRITER_ILL(from_ill));
16718 
16719 	ILM_WALKER_HOLD(to_ill);
16720 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16721 
16722 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16723 			continue;
16724 		/*
16725 		 * no locks held, ill/ipif cannot dissappear as long
16726 		 * as we are writer.
16727 		 */
16728 		ipif = to_ill->ill_ipif;
16729 		/*
16730 		 * No need to hold any lock as we are the writer and this
16731 		 * can only be changed by a writer.
16732 		 */
16733 		ilm->ilm_is_new = B_FALSE;
16734 
16735 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16736 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16737 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16738 			    "resolver\n"));
16739 			continue;		/* Must be IRE_IF_NORESOLVER */
16740 		}
16741 
16742 
16743 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16744 			ip1dbg(("ilm_send_multicast_reqs: "
16745 			    "to_ill MULTI_BCAST\n"));
16746 			goto from;
16747 		}
16748 
16749 		if (to_ill->ill_isv6)
16750 			mld_joingroup(ilm);
16751 		else
16752 			igmp_joingroup(ilm);
16753 
16754 		if (to_ill->ill_ipif_up_count == 0) {
16755 			/*
16756 			 * Nobody there. All multicast addresses will be
16757 			 * re-joined when we get the DL_BIND_ACK bringing the
16758 			 * interface up.
16759 			 */
16760 			ilm->ilm_notify_driver = B_FALSE;
16761 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16762 			goto from;
16763 		}
16764 
16765 		/*
16766 		 * For allmulti address, we want to join on only one interface.
16767 		 * Checking for ilm_numentries_v6 is not correct as you may
16768 		 * find an ilm with zero address on to_ill, but we may not
16769 		 * have nominated to_ill for receiving. Thus, if we have
16770 		 * nominated from_ill (ill_join_allmulti is set), nominate
16771 		 * only if to_ill is not already nominated (to_ill normally
16772 		 * should not have been nominated if "from_ill" has already
16773 		 * been nominated. As we don't prevent failovers from happening
16774 		 * across groups, we don't assert).
16775 		 */
16776 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16777 			/*
16778 			 * There is no need to hold ill locks as we are
16779 			 * writer on both ills and when ill_join_allmulti
16780 			 * is changed the thread is always a writer.
16781 			 */
16782 			if (from_ill->ill_join_allmulti &&
16783 			    !to_ill->ill_join_allmulti) {
16784 				(void) ip_join_allmulti(to_ill->ill_ipif);
16785 			}
16786 		} else if (ilm->ilm_notify_driver) {
16787 
16788 			/*
16789 			 * This is a newly moved ilm so we need to tell the
16790 			 * driver about the new group. There can be more than
16791 			 * one ilm's for the same group in the list each with a
16792 			 * different orig_ifindex. We have to inform the driver
16793 			 * once. In ilm_move_v[4,6] we only set the flag
16794 			 * ilm_notify_driver for the first ilm.
16795 			 */
16796 
16797 			(void) ip_ll_send_enabmulti_req(to_ill,
16798 			    &ilm->ilm_v6addr);
16799 		}
16800 
16801 		ilm->ilm_notify_driver = B_FALSE;
16802 
16803 		/*
16804 		 * See whether we need to send down DL_DISABMULTI_REQ on
16805 		 * from_ill as ilm has just been removed.
16806 		 */
16807 from:
16808 		ipif = from_ill->ill_ipif;
16809 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16810 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16811 			ip1dbg(("ilm_send_multicast_reqs: "
16812 			    "from_ill not resolver\n"));
16813 			continue;		/* Must be IRE_IF_NORESOLVER */
16814 		}
16815 
16816 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16817 			ip1dbg(("ilm_send_multicast_reqs: "
16818 			    "from_ill MULTI_BCAST\n"));
16819 			continue;
16820 		}
16821 
16822 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16823 			if (from_ill->ill_join_allmulti)
16824 			    (void) ip_leave_allmulti(from_ill->ill_ipif);
16825 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16826 			(void) ip_ll_send_disabmulti_req(from_ill,
16827 		    &ilm->ilm_v6addr);
16828 		}
16829 	}
16830 	ILM_WALKER_RELE(to_ill);
16831 }
16832 
16833 /*
16834  * This function is called when all multicast memberships needs
16835  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16836  * called only once unlike the IPv4 counterpart where it is called after
16837  * every logical interface is moved. The reason is due to multicast
16838  * memberships are joined using an interface address in IPv4 while in
16839  * IPv6, interface index is used.
16840  */
16841 static void
16842 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16843 {
16844 	ilm_t	*ilm;
16845 	ilm_t	*ilm_next;
16846 	ilm_t	*new_ilm;
16847 	ilm_t	**ilmp;
16848 	int	count;
16849 	char buf[INET6_ADDRSTRLEN];
16850 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16851 
16852 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16853 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16854 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
16855 
16856 	if (ifindex == 0) {
16857 		/*
16858 		 * Form the solicited node mcast address which is used later.
16859 		 */
16860 		ipif_t *ipif;
16861 
16862 		ipif = from_ill->ill_ipif;
16863 		ASSERT(ipif->ipif_id == 0);
16864 
16865 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16866 	}
16867 
16868 	ilmp = &from_ill->ill_ilm;
16869 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16870 		ilm_next = ilm->ilm_next;
16871 
16872 		if (ilm->ilm_flags & ILM_DELETED) {
16873 			ilmp = &ilm->ilm_next;
16874 			continue;
16875 		}
16876 
16877 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16878 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16879 		ASSERT(ilm->ilm_orig_ifindex != 0);
16880 		if (ilm->ilm_orig_ifindex == ifindex) {
16881 			/*
16882 			 * We are failing back multicast memberships.
16883 			 * If the same ilm exists in to_ill, it means somebody
16884 			 * has joined the same group there e.g. ff02::1
16885 			 * is joined within the kernel when the interfaces
16886 			 * came UP.
16887 			 */
16888 			ASSERT(ilm->ilm_ipif == NULL);
16889 			if (new_ilm != NULL) {
16890 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16891 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16892 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16893 					new_ilm->ilm_is_new = B_TRUE;
16894 				}
16895 			} else {
16896 				/*
16897 				 * check if we can just move the ilm
16898 				 */
16899 				if (from_ill->ill_ilm_walker_cnt != 0) {
16900 					/*
16901 					 * We have walkers we cannot move
16902 					 * the ilm, so allocate a new ilm,
16903 					 * this (old) ilm will be marked
16904 					 * ILM_DELETED at the end of the loop
16905 					 * and will be freed when the
16906 					 * last walker exits.
16907 					 */
16908 					new_ilm = (ilm_t *)mi_zalloc
16909 					    (sizeof (ilm_t));
16910 					if (new_ilm == NULL) {
16911 						ip0dbg(("ilm_move_v6: "
16912 						    "FAILBACK of IPv6"
16913 						    " multicast address %s : "
16914 						    "from %s to"
16915 						    " %s failed : ENOMEM \n",
16916 						    inet_ntop(AF_INET6,
16917 						    &ilm->ilm_v6addr, buf,
16918 						    sizeof (buf)),
16919 						    from_ill->ill_name,
16920 						    to_ill->ill_name));
16921 
16922 							ilmp = &ilm->ilm_next;
16923 							continue;
16924 					}
16925 					*new_ilm = *ilm;
16926 					/*
16927 					 * we don't want new_ilm linked to
16928 					 * ilm's filter list.
16929 					 */
16930 					new_ilm->ilm_filter = NULL;
16931 				} else {
16932 					/*
16933 					 * No walkers we can move the ilm.
16934 					 * lets take it out of the list.
16935 					 */
16936 					*ilmp = ilm->ilm_next;
16937 					ilm->ilm_next = NULL;
16938 					new_ilm = ilm;
16939 				}
16940 
16941 				/*
16942 				 * if this is the first ilm for the group
16943 				 * set ilm_notify_driver so that we notify the
16944 				 * driver in ilm_send_multicast_reqs.
16945 				 */
16946 				if (ilm_lookup_ill_v6(to_ill,
16947 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16948 					new_ilm->ilm_notify_driver = B_TRUE;
16949 
16950 				new_ilm->ilm_ill = to_ill;
16951 				/* Add to the to_ill's list */
16952 				new_ilm->ilm_next = to_ill->ill_ilm;
16953 				to_ill->ill_ilm = new_ilm;
16954 				/*
16955 				 * set the flag so that mld_joingroup is
16956 				 * called in ilm_send_multicast_reqs().
16957 				 */
16958 				new_ilm->ilm_is_new = B_TRUE;
16959 			}
16960 			goto bottom;
16961 		} else if (ifindex != 0) {
16962 			/*
16963 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16964 			 * has not matched above, look at the next ilm.
16965 			 */
16966 			ilmp = &ilm->ilm_next;
16967 			continue;
16968 		}
16969 		/*
16970 		 * If we are here, it means ifindex is 0. Failover
16971 		 * everything.
16972 		 *
16973 		 * We need to handle solicited node mcast address
16974 		 * and all_nodes mcast address differently as they
16975 		 * are joined witin the kenrel (ipif_multicast_up)
16976 		 * and potentially from the userland. We are called
16977 		 * after the ipifs of from_ill has been moved.
16978 		 * If we still find ilms on ill with solicited node
16979 		 * mcast address or all_nodes mcast address, it must
16980 		 * belong to the UP interface that has not moved e.g.
16981 		 * ipif_id 0 with the link local prefix does not move.
16982 		 * We join this on the new ill accounting for all the
16983 		 * userland memberships so that applications don't
16984 		 * see any failure.
16985 		 *
16986 		 * We need to make sure that we account only for the
16987 		 * solicited node and all node multicast addresses
16988 		 * that was brought UP on these. In the case of
16989 		 * a failover from A to B, we might have ilms belonging
16990 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
16991 		 * for the membership from the userland. If we are failing
16992 		 * over from B to C now, we will find the ones belonging
16993 		 * to A on B. These don't account for the ill_ipif_up_count.
16994 		 * They just move from B to C. The check below on
16995 		 * ilm_orig_ifindex ensures that.
16996 		 */
16997 		if ((ilm->ilm_orig_ifindex ==
16998 		    from_ill->ill_phyint->phyint_ifindex) &&
16999 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
17000 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
17001 		    &ilm->ilm_v6addr))) {
17002 			ASSERT(ilm->ilm_refcnt > 0);
17003 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
17004 			/*
17005 			 * For indentation reasons, we are not using a
17006 			 * "else" here.
17007 			 */
17008 			if (count == 0) {
17009 				ilmp = &ilm->ilm_next;
17010 				continue;
17011 			}
17012 			ilm->ilm_refcnt -= count;
17013 			if (new_ilm != NULL) {
17014 				/*
17015 				 * Can find one with the same
17016 				 * ilm_orig_ifindex, if we are failing
17017 				 * over to a STANDBY. This happens
17018 				 * when somebody wants to join a group
17019 				 * on a STANDBY interface and we
17020 				 * internally join on a different one.
17021 				 * If we had joined on from_ill then, a
17022 				 * failover now will find a new ilm
17023 				 * with this index.
17024 				 */
17025 				ip1dbg(("ilm_move_v6: FAILOVER, found"
17026 				    " new ilm on %s, group address %s\n",
17027 				    to_ill->ill_name,
17028 				    inet_ntop(AF_INET6,
17029 				    &ilm->ilm_v6addr, buf,
17030 				    sizeof (buf))));
17031 				new_ilm->ilm_refcnt += count;
17032 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17033 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17034 					new_ilm->ilm_is_new = B_TRUE;
17035 				}
17036 			} else {
17037 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17038 				if (new_ilm == NULL) {
17039 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17040 					    " multicast address %s : from %s to"
17041 					    " %s failed : ENOMEM \n",
17042 					    inet_ntop(AF_INET6,
17043 					    &ilm->ilm_v6addr, buf,
17044 					    sizeof (buf)), from_ill->ill_name,
17045 					    to_ill->ill_name));
17046 					ilmp = &ilm->ilm_next;
17047 					continue;
17048 				}
17049 				*new_ilm = *ilm;
17050 				new_ilm->ilm_filter = NULL;
17051 				new_ilm->ilm_refcnt = count;
17052 				new_ilm->ilm_timer = INFINITY;
17053 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17054 				new_ilm->ilm_is_new = B_TRUE;
17055 				/*
17056 				 * If the to_ill has not joined this
17057 				 * group we need to tell the driver in
17058 				 * ill_send_multicast_reqs.
17059 				 */
17060 				if (ilm_lookup_ill_v6(to_ill,
17061 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17062 					new_ilm->ilm_notify_driver = B_TRUE;
17063 
17064 				new_ilm->ilm_ill = to_ill;
17065 				/* Add to the to_ill's list */
17066 				new_ilm->ilm_next = to_ill->ill_ilm;
17067 				to_ill->ill_ilm = new_ilm;
17068 				ASSERT(new_ilm->ilm_ipif == NULL);
17069 			}
17070 			if (ilm->ilm_refcnt == 0) {
17071 				goto bottom;
17072 			} else {
17073 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17074 				CLEAR_SLIST(new_ilm->ilm_filter);
17075 				ilmp = &ilm->ilm_next;
17076 			}
17077 			continue;
17078 		} else {
17079 			/*
17080 			 * ifindex = 0 means, move everything pointing at
17081 			 * from_ill. We are doing this becuase ill has
17082 			 * either FAILED or became INACTIVE.
17083 			 *
17084 			 * As we would like to move things later back to
17085 			 * from_ill, we want to retain the identity of this
17086 			 * ilm. Thus, we don't blindly increment the reference
17087 			 * count on the ilms matching the address alone. We
17088 			 * need to match on the ilm_orig_index also. new_ilm
17089 			 * was obtained by matching ilm_orig_index also.
17090 			 */
17091 			if (new_ilm != NULL) {
17092 				/*
17093 				 * This is possible only if a previous restore
17094 				 * was incomplete i.e restore to
17095 				 * ilm_orig_ifindex left some ilms because
17096 				 * of some failures. Thus when we are failing
17097 				 * again, we might find our old friends there.
17098 				 */
17099 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17100 				    " on %s, group address %s\n",
17101 				    to_ill->ill_name,
17102 				    inet_ntop(AF_INET6,
17103 				    &ilm->ilm_v6addr, buf,
17104 				    sizeof (buf))));
17105 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17106 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17107 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17108 					new_ilm->ilm_is_new = B_TRUE;
17109 				}
17110 			} else {
17111 				if (from_ill->ill_ilm_walker_cnt != 0) {
17112 					new_ilm = (ilm_t *)
17113 					    mi_zalloc(sizeof (ilm_t));
17114 					if (new_ilm == NULL) {
17115 						ip0dbg(("ilm_move_v6: "
17116 						    "FAILOVER of IPv6"
17117 						    " multicast address %s : "
17118 						    "from %s to"
17119 						    " %s failed : ENOMEM \n",
17120 						    inet_ntop(AF_INET6,
17121 						    &ilm->ilm_v6addr, buf,
17122 						    sizeof (buf)),
17123 						    from_ill->ill_name,
17124 						    to_ill->ill_name));
17125 
17126 							ilmp = &ilm->ilm_next;
17127 							continue;
17128 					}
17129 					*new_ilm = *ilm;
17130 					new_ilm->ilm_filter = NULL;
17131 				} else {
17132 					*ilmp = ilm->ilm_next;
17133 					new_ilm = ilm;
17134 				}
17135 				/*
17136 				 * If the to_ill has not joined this
17137 				 * group we need to tell the driver in
17138 				 * ill_send_multicast_reqs.
17139 				 */
17140 				if (ilm_lookup_ill_v6(to_ill,
17141 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17142 					new_ilm->ilm_notify_driver = B_TRUE;
17143 
17144 				/* Add to the to_ill's list */
17145 				new_ilm->ilm_next = to_ill->ill_ilm;
17146 				to_ill->ill_ilm = new_ilm;
17147 				ASSERT(ilm->ilm_ipif == NULL);
17148 				new_ilm->ilm_ill = to_ill;
17149 				new_ilm->ilm_is_new = B_TRUE;
17150 			}
17151 
17152 		}
17153 
17154 bottom:
17155 		/*
17156 		 * Revert multicast filter state to (EXCLUDE, NULL).
17157 		 * new_ilm->ilm_is_new should already be set if needed.
17158 		 */
17159 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17160 		CLEAR_SLIST(new_ilm->ilm_filter);
17161 		/*
17162 		 * We allocated/got a new ilm, free the old one.
17163 		 */
17164 		if (new_ilm != ilm) {
17165 			if (from_ill->ill_ilm_walker_cnt == 0) {
17166 				*ilmp = ilm->ilm_next;
17167 				ilm->ilm_next = NULL;
17168 				FREE_SLIST(ilm->ilm_filter);
17169 				FREE_SLIST(ilm->ilm_pendsrcs);
17170 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17171 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17172 				mi_free((char *)ilm);
17173 			} else {
17174 				ilm->ilm_flags |= ILM_DELETED;
17175 				from_ill->ill_ilm_cleanup_reqd = 1;
17176 				ilmp = &ilm->ilm_next;
17177 			}
17178 		}
17179 	}
17180 }
17181 
17182 /*
17183  * Move all the multicast memberships to to_ill. Called when
17184  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17185  * different from IPv6 counterpart as multicast memberships are associated
17186  * with ills in IPv6. This function is called after every ipif is moved
17187  * unlike IPv6, where it is moved only once.
17188  */
17189 static void
17190 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17191 {
17192 	ilm_t	*ilm;
17193 	ilm_t	*ilm_next;
17194 	ilm_t	*new_ilm;
17195 	ilm_t	**ilmp;
17196 
17197 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17198 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17199 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
17200 
17201 	ilmp = &from_ill->ill_ilm;
17202 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17203 		ilm_next = ilm->ilm_next;
17204 
17205 		if (ilm->ilm_flags & ILM_DELETED) {
17206 			ilmp = &ilm->ilm_next;
17207 			continue;
17208 		}
17209 
17210 		ASSERT(ilm->ilm_ipif != NULL);
17211 
17212 		if (ilm->ilm_ipif != ipif) {
17213 			ilmp = &ilm->ilm_next;
17214 			continue;
17215 		}
17216 
17217 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17218 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17219 			/*
17220 			 * We joined this in ipif_multicast_up
17221 			 * and we never did an ipif_multicast_down
17222 			 * for IPv4. If nobody else from the userland
17223 			 * has reference, we free the ilm, and later
17224 			 * when this ipif comes up on the new ill,
17225 			 * we will join this again.
17226 			 */
17227 			if (--ilm->ilm_refcnt == 0)
17228 				goto delete_ilm;
17229 
17230 			new_ilm = ilm_lookup_ipif(ipif,
17231 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17232 			if (new_ilm != NULL) {
17233 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17234 				/*
17235 				 * We still need to deal with the from_ill.
17236 				 */
17237 				new_ilm->ilm_is_new = B_TRUE;
17238 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17239 				CLEAR_SLIST(new_ilm->ilm_filter);
17240 				goto delete_ilm;
17241 			}
17242 			/*
17243 			 * If we could not find one e.g. ipif is
17244 			 * still down on to_ill, we add this ilm
17245 			 * on ill_new to preserve the reference
17246 			 * count.
17247 			 */
17248 		}
17249 		/*
17250 		 * When ipifs move, ilms always move with it
17251 		 * to the NEW ill. Thus we should never be
17252 		 * able to find ilm till we really move it here.
17253 		 */
17254 		ASSERT(ilm_lookup_ipif(ipif,
17255 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17256 
17257 		if (from_ill->ill_ilm_walker_cnt != 0) {
17258 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17259 			if (new_ilm == NULL) {
17260 				char buf[INET6_ADDRSTRLEN];
17261 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17262 				    " multicast address %s : "
17263 				    "from %s to"
17264 				    " %s failed : ENOMEM \n",
17265 				    inet_ntop(AF_INET,
17266 				    &ilm->ilm_v6addr, buf,
17267 				    sizeof (buf)),
17268 				    from_ill->ill_name,
17269 				    to_ill->ill_name));
17270 
17271 				ilmp = &ilm->ilm_next;
17272 				continue;
17273 			}
17274 			*new_ilm = *ilm;
17275 			/* We don't want new_ilm linked to ilm's filter list */
17276 			new_ilm->ilm_filter = NULL;
17277 		} else {
17278 			/* Remove from the list */
17279 			*ilmp = ilm->ilm_next;
17280 			new_ilm = ilm;
17281 		}
17282 
17283 		/*
17284 		 * If we have never joined this group on the to_ill
17285 		 * make sure we tell the driver.
17286 		 */
17287 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17288 		    ALL_ZONES) == NULL)
17289 			new_ilm->ilm_notify_driver = B_TRUE;
17290 
17291 		/* Add to the to_ill's list */
17292 		new_ilm->ilm_next = to_ill->ill_ilm;
17293 		to_ill->ill_ilm = new_ilm;
17294 		new_ilm->ilm_is_new = B_TRUE;
17295 
17296 		/*
17297 		 * Revert multicast filter state to (EXCLUDE, NULL)
17298 		 */
17299 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17300 		CLEAR_SLIST(new_ilm->ilm_filter);
17301 
17302 		/*
17303 		 * Delete only if we have allocated a new ilm.
17304 		 */
17305 		if (new_ilm != ilm) {
17306 delete_ilm:
17307 			if (from_ill->ill_ilm_walker_cnt == 0) {
17308 				/* Remove from the list */
17309 				*ilmp = ilm->ilm_next;
17310 				ilm->ilm_next = NULL;
17311 				FREE_SLIST(ilm->ilm_filter);
17312 				FREE_SLIST(ilm->ilm_pendsrcs);
17313 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17314 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17315 				mi_free((char *)ilm);
17316 			} else {
17317 				ilm->ilm_flags |= ILM_DELETED;
17318 				from_ill->ill_ilm_cleanup_reqd = 1;
17319 				ilmp = &ilm->ilm_next;
17320 			}
17321 		}
17322 	}
17323 }
17324 
17325 static uint_t
17326 ipif_get_id(ill_t *ill, uint_t id)
17327 {
17328 	uint_t	unit;
17329 	ipif_t	*tipif;
17330 	boolean_t found = B_FALSE;
17331 
17332 	/*
17333 	 * During failback, we want to go back to the same id
17334 	 * instead of the smallest id so that the original
17335 	 * configuration is maintained. id is non-zero in that
17336 	 * case.
17337 	 */
17338 	if (id != 0) {
17339 		/*
17340 		 * While failing back, if we still have an ipif with
17341 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17342 		 * as soon as we return from this function. It was
17343 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17344 		 * we can choose the smallest id. Thus we return zero
17345 		 * in that case ignoring the hint.
17346 		 */
17347 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17348 			return (0);
17349 		for (tipif = ill->ill_ipif; tipif != NULL;
17350 		    tipif = tipif->ipif_next) {
17351 			if (tipif->ipif_id == id) {
17352 				found = B_TRUE;
17353 				break;
17354 			}
17355 		}
17356 		/*
17357 		 * If somebody already plumbed another logical
17358 		 * with the same id, we won't be able to find it.
17359 		 */
17360 		if (!found)
17361 			return (id);
17362 	}
17363 	for (unit = 0; unit <= ip_addrs_per_if; unit++) {
17364 		found = B_FALSE;
17365 		for (tipif = ill->ill_ipif; tipif != NULL;
17366 		    tipif = tipif->ipif_next) {
17367 			if (tipif->ipif_id == unit) {
17368 				found = B_TRUE;
17369 				break;
17370 			}
17371 		}
17372 		if (!found)
17373 			break;
17374 	}
17375 	return (unit);
17376 }
17377 
17378 /* ARGSUSED */
17379 static int
17380 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17381     ipif_t **rep_ipif_ptr)
17382 {
17383 	ill_t	*from_ill;
17384 	ipif_t	*rep_ipif;
17385 	ipif_t	**ipifp;
17386 	uint_t	unit;
17387 	int err = 0;
17388 	ipif_t	*to_ipif;
17389 	struct iocblk	*iocp;
17390 	boolean_t failback_cmd;
17391 	boolean_t remove_ipif;
17392 	int	rc;
17393 
17394 	ASSERT(IAM_WRITER_ILL(to_ill));
17395 	ASSERT(IAM_WRITER_IPIF(ipif));
17396 
17397 	iocp = (struct iocblk *)mp->b_rptr;
17398 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17399 	remove_ipif = B_FALSE;
17400 
17401 	from_ill = ipif->ipif_ill;
17402 
17403 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17404 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17405 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
17406 
17407 	/*
17408 	 * Don't move LINK LOCAL addresses as they are tied to
17409 	 * physical interface.
17410 	 */
17411 	if (from_ill->ill_isv6 &&
17412 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17413 		ipif->ipif_was_up = B_FALSE;
17414 		IPIF_UNMARK_MOVING(ipif);
17415 		return (0);
17416 	}
17417 
17418 	/*
17419 	 * We set the ipif_id to maximum so that the search for
17420 	 * ipif_id will pick the lowest number i.e 0 in the
17421 	 * following 2 cases :
17422 	 *
17423 	 * 1) We have a replacement ipif at the head of to_ill.
17424 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17425 	 *    on to_ill and hence the MOVE might fail. We want to
17426 	 *    remove it only if we could move the ipif. Thus, by
17427 	 *    setting it to the MAX value, we make the search in
17428 	 *    ipif_get_id return the zeroth id.
17429 	 *
17430 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17431 	 *    we might just have a zero address plumbed on the ipif
17432 	 *    with zero id in the case of IPv4. We remove that while
17433 	 *    doing the failback. We want to remove it only if we
17434 	 *    could move the ipif. Thus, by setting it to the MAX
17435 	 *    value, we make the search in ipif_get_id return the
17436 	 *    zeroth id.
17437 	 *
17438 	 * Both (1) and (2) are done only when when we are moving
17439 	 * an ipif (either due to failover/failback) which originally
17440 	 * belonged to this interface i.e the ipif_orig_ifindex is
17441 	 * the same as to_ill's ifindex. This is needed so that
17442 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17443 	 * from B -> A (B is being removed from the group) and
17444 	 * FAILBACK from A -> B restores the original configuration.
17445 	 * Without the check for orig_ifindex, the second FAILOVER
17446 	 * could make the ipif belonging to B replace the A's zeroth
17447 	 * ipif and the subsequent failback re-creating the replacement
17448 	 * ipif again.
17449 	 *
17450 	 * NOTE : We created the replacement ipif when we did a
17451 	 * FAILOVER (See below). We could check for FAILBACK and
17452 	 * then look for replacement ipif to be removed. But we don't
17453 	 * want to do that because we wan't to allow the possibility
17454 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17455 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17456 	 * from B -> A.
17457 	 */
17458 	to_ipif = to_ill->ill_ipif;
17459 	if ((to_ill->ill_phyint->phyint_ifindex ==
17460 	    ipif->ipif_orig_ifindex) &&
17461 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17462 		ASSERT(to_ipif->ipif_id == 0);
17463 		remove_ipif = B_TRUE;
17464 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17465 	}
17466 	/*
17467 	 * Find the lowest logical unit number on the to_ill.
17468 	 * If we are failing back, try to get the original id
17469 	 * rather than the lowest one so that the original
17470 	 * configuration is maintained.
17471 	 *
17472 	 * XXX need a better scheme for this.
17473 	 */
17474 	if (failback_cmd) {
17475 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17476 	} else {
17477 		unit = ipif_get_id(to_ill, 0);
17478 	}
17479 
17480 	/* Reset back to zero in case we fail below */
17481 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17482 		to_ipif->ipif_id = 0;
17483 
17484 	if (unit == ip_addrs_per_if) {
17485 		ipif->ipif_was_up = B_FALSE;
17486 		IPIF_UNMARK_MOVING(ipif);
17487 		return (EINVAL);
17488 	}
17489 
17490 	/*
17491 	 * ipif is ready to move from "from_ill" to "to_ill".
17492 	 *
17493 	 * 1) If we are moving ipif with id zero, create a
17494 	 *    replacement ipif for this ipif on from_ill. If this fails
17495 	 *    fail the MOVE operation.
17496 	 *
17497 	 * 2) Remove the replacement ipif on to_ill if any.
17498 	 *    We could remove the replacement ipif when we are moving
17499 	 *    the ipif with id zero. But what if somebody already
17500 	 *    unplumbed it ? Thus we always remove it if it is present.
17501 	 *    We want to do it only if we are sure we are going to
17502 	 *    move the ipif to to_ill which is why there are no
17503 	 *    returns due to error till ipif is linked to to_ill.
17504 	 *    Note that the first ipif that we failback will always
17505 	 *    be zero if it is present.
17506 	 */
17507 	if (ipif->ipif_id == 0) {
17508 		ipaddr_t inaddr_any = INADDR_ANY;
17509 
17510 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17511 		if (rep_ipif == NULL) {
17512 			ipif->ipif_was_up = B_FALSE;
17513 			IPIF_UNMARK_MOVING(ipif);
17514 			return (ENOMEM);
17515 		}
17516 		*rep_ipif = ipif_zero;
17517 		/*
17518 		 * Before we put the ipif on the list, store the addresses
17519 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17520 		 * assumes so. This logic is not any different from what
17521 		 * ipif_allocate does.
17522 		 */
17523 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17524 		    &rep_ipif->ipif_v6lcl_addr);
17525 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17526 		    &rep_ipif->ipif_v6src_addr);
17527 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17528 		    &rep_ipif->ipif_v6subnet);
17529 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17530 		    &rep_ipif->ipif_v6net_mask);
17531 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17532 		    &rep_ipif->ipif_v6brd_addr);
17533 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17534 		    &rep_ipif->ipif_v6pp_dst_addr);
17535 		/*
17536 		 * We mark IPIF_NOFAILOVER so that this can never
17537 		 * move.
17538 		 */
17539 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17540 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17541 		rep_ipif->ipif_replace_zero = B_TRUE;
17542 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17543 		    MUTEX_DEFAULT, NULL);
17544 		rep_ipif->ipif_id = 0;
17545 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17546 		rep_ipif->ipif_ill = from_ill;
17547 		rep_ipif->ipif_orig_ifindex =
17548 		    from_ill->ill_phyint->phyint_ifindex;
17549 		/* Insert at head */
17550 		rep_ipif->ipif_next = from_ill->ill_ipif;
17551 		from_ill->ill_ipif = rep_ipif;
17552 		/*
17553 		 * We don't really care to let apps know about
17554 		 * this interface.
17555 		 */
17556 	}
17557 
17558 	if (remove_ipif) {
17559 		/*
17560 		 * We set to a max value above for this case to get
17561 		 * id zero. ASSERT that we did get one.
17562 		 */
17563 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17564 		rep_ipif = to_ipif;
17565 		to_ill->ill_ipif = rep_ipif->ipif_next;
17566 		rep_ipif->ipif_next = NULL;
17567 		/*
17568 		 * If some apps scanned and find this interface,
17569 		 * it is time to let them know, so that they can
17570 		 * delete it.
17571 		 */
17572 
17573 		*rep_ipif_ptr = rep_ipif;
17574 	}
17575 
17576 	/* Get it out of the ILL interface list. */
17577 	ipifp = &ipif->ipif_ill->ill_ipif;
17578 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
17579 		if (*ipifp == ipif) {
17580 			*ipifp = ipif->ipif_next;
17581 			break;
17582 		}
17583 	}
17584 
17585 	/* Assign the new ill */
17586 	ipif->ipif_ill = to_ill;
17587 	ipif->ipif_id = unit;
17588 	/* id has already been checked */
17589 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17590 	ASSERT(rc == 0);
17591 	/* Let SCTP update its list */
17592 	sctp_move_ipif(ipif, from_ill, to_ill);
17593 	/*
17594 	 * Handle the failover and failback of ipif_t between
17595 	 * ill_t that have differing maximum mtu values.
17596 	 */
17597 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17598 		if (ipif->ipif_saved_mtu == 0) {
17599 			/*
17600 			 * As this ipif_t is moving to an ill_t
17601 			 * that has a lower ill_max_mtu, its
17602 			 * ipif_mtu needs to be saved so it can
17603 			 * be restored during failback or during
17604 			 * failover to an ill_t which has a
17605 			 * higher ill_max_mtu.
17606 			 */
17607 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17608 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17609 		} else {
17610 			/*
17611 			 * The ipif_t is, once again, moving to
17612 			 * an ill_t that has a lower maximum mtu
17613 			 * value.
17614 			 */
17615 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17616 		}
17617 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17618 	    ipif->ipif_saved_mtu != 0) {
17619 		/*
17620 		 * The mtu of this ipif_t had to be reduced
17621 		 * during an earlier failover; this is an
17622 		 * opportunity for it to be increased (either as
17623 		 * part of another failover or a failback).
17624 		 */
17625 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17626 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17627 			ipif->ipif_saved_mtu = 0;
17628 		} else {
17629 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17630 		}
17631 	}
17632 
17633 	/*
17634 	 * We preserve all the other fields of the ipif including
17635 	 * ipif_saved_ire_mp. The routes that are saved here will
17636 	 * be recreated on the new interface and back on the old
17637 	 * interface when we move back.
17638 	 */
17639 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17640 
17641 	return (err);
17642 }
17643 
17644 static int
17645 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17646     int ifindex, ipif_t **rep_ipif_ptr)
17647 {
17648 	ipif_t *mipif;
17649 	ipif_t *ipif_next;
17650 	int err;
17651 
17652 	/*
17653 	 * We don't really try to MOVE back things if some of the
17654 	 * operations fail. The daemon will take care of moving again
17655 	 * later on.
17656 	 */
17657 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17658 		ipif_next = mipif->ipif_next;
17659 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17660 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17661 
17662 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17663 
17664 			/*
17665 			 * When the MOVE fails, it is the job of the
17666 			 * application to take care of this properly
17667 			 * i.e try again if it is ENOMEM.
17668 			 */
17669 			if (mipif->ipif_ill != from_ill) {
17670 				/*
17671 				 * ipif has moved.
17672 				 *
17673 				 * Move the multicast memberships associated
17674 				 * with this ipif to the new ill. For IPv6, we
17675 				 * do it once after all the ipifs are moved
17676 				 * (in ill_move) as they are not associated
17677 				 * with ipifs.
17678 				 *
17679 				 * We need to move the ilms as the ipif has
17680 				 * already been moved to a new ill even
17681 				 * in the case of errors. Neither
17682 				 * ilm_free(ipif) will find the ilm
17683 				 * when somebody unplumbs this ipif nor
17684 				 * ilm_delete(ilm) will be able to find the
17685 				 * ilm, if we don't move now.
17686 				 */
17687 				if (!from_ill->ill_isv6)
17688 					ilm_move_v4(from_ill, to_ill, mipif);
17689 			}
17690 
17691 			if (err != 0)
17692 				return (err);
17693 		}
17694 	}
17695 	return (0);
17696 }
17697 
17698 static int
17699 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17700 {
17701 	int ifindex;
17702 	int err;
17703 	struct iocblk	*iocp;
17704 	ipif_t	*ipif;
17705 	ipif_t *rep_ipif_ptr = NULL;
17706 	ipif_t	*from_ipif = NULL;
17707 	boolean_t check_rep_if = B_FALSE;
17708 
17709 	iocp = (struct iocblk *)mp->b_rptr;
17710 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17711 		/*
17712 		 * Move everything pointing at from_ill to to_ill.
17713 		 * We acheive this by passing in 0 as ifindex.
17714 		 */
17715 		ifindex = 0;
17716 	} else {
17717 		/*
17718 		 * Move everything pointing at from_ill whose original
17719 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17720 		 * We acheive this by passing in ifindex rather than 0.
17721 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17722 		 */
17723 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17724 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17725 	}
17726 
17727 	/*
17728 	 * Determine if there is at least one ipif that would move from
17729 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17730 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17731 	 * the move, in which case we need to quiesce the replacement ipif also.
17732 	 */
17733 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17734 	    from_ipif = from_ipif->ipif_next) {
17735 		if (((ifindex == 0) ||
17736 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17737 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17738 			check_rep_if = B_TRUE;
17739 			break;
17740 		}
17741 	}
17742 
17743 
17744 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17745 
17746 	GRAB_ILL_LOCKS(from_ill, to_ill);
17747 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17748 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17749 		    mp, ILL_MOVE_OK);
17750 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17751 		return (EINPROGRESS);
17752 	}
17753 
17754 	/* Check if the replacement ipif is quiescent to delete */
17755 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17756 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17757 		to_ill->ill_ipif->ipif_state_flags |=
17758 		    IPIF_MOVING | IPIF_CHANGING;
17759 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17760 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17761 			    mp, ILL_MOVE_OK);
17762 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17763 			return (EINPROGRESS);
17764 		}
17765 	}
17766 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17767 
17768 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17769 	rw_enter(&ill_g_lock, RW_WRITER);
17770 	GRAB_ILL_LOCKS(from_ill, to_ill);
17771 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17772 
17773 	/* ilm_move is done inside ipif_move for IPv4 */
17774 	if (err == 0 && from_ill->ill_isv6)
17775 		ilm_move_v6(from_ill, to_ill, ifindex);
17776 
17777 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17778 	rw_exit(&ill_g_lock);
17779 
17780 	/*
17781 	 * send rts messages and multicast messages.
17782 	 */
17783 	if (rep_ipif_ptr != NULL) {
17784 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17785 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17786 			rep_ipif_ptr->ipif_recovery_id = 0;
17787 		}
17788 		ip_rts_ifmsg(rep_ipif_ptr);
17789 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17790 		IPIF_TRACE_CLEANUP(rep_ipif_ptr);
17791 		mi_free(rep_ipif_ptr);
17792 	}
17793 
17794 	conn_move_ill(from_ill, to_ill, ifindex);
17795 
17796 	return (err);
17797 }
17798 
17799 /*
17800  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17801  * Also checks for the validity of the arguments.
17802  * Note: We are already exclusive inside the from group.
17803  * It is upto the caller to release refcnt on the to_ill's.
17804  */
17805 static int
17806 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17807     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17808 {
17809 	int dst_index;
17810 	ipif_t *ipif_v4, *ipif_v6;
17811 	struct lifreq *lifr;
17812 	mblk_t *mp1;
17813 	boolean_t exists;
17814 	sin_t	*sin;
17815 	int	err = 0;
17816 
17817 	if ((mp1 = mp->b_cont) == NULL)
17818 		return (EPROTO);
17819 
17820 	if ((mp1 = mp1->b_cont) == NULL)
17821 		return (EPROTO);
17822 
17823 	lifr = (struct lifreq *)mp1->b_rptr;
17824 	sin = (sin_t *)&lifr->lifr_addr;
17825 
17826 	/*
17827 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17828 	 * specific operations.
17829 	 */
17830 	if (sin->sin_family != AF_UNSPEC)
17831 		return (EINVAL);
17832 
17833 	/*
17834 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17835 	 * NULLs for the last 4 args and we know the lookup won't fail
17836 	 * with EINPROGRESS.
17837 	 */
17838 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17839 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17840 	    ALL_ZONES, NULL, NULL, NULL, NULL);
17841 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17842 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17843 	    ALL_ZONES, NULL, NULL, NULL, NULL);
17844 
17845 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17846 		return (ENXIO);
17847 
17848 	if (ipif_v4 != NULL) {
17849 		ASSERT(ipif_v4->ipif_refcnt != 0);
17850 		if (ipif_v4->ipif_id != 0) {
17851 			err = EINVAL;
17852 			goto done;
17853 		}
17854 
17855 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17856 		*ill_from_v4 = ipif_v4->ipif_ill;
17857 	}
17858 
17859 	if (ipif_v6 != NULL) {
17860 		ASSERT(ipif_v6->ipif_refcnt != 0);
17861 		if (ipif_v6->ipif_id != 0) {
17862 			err = EINVAL;
17863 			goto done;
17864 		}
17865 
17866 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17867 		*ill_from_v6 = ipif_v6->ipif_ill;
17868 	}
17869 
17870 	err = 0;
17871 	dst_index = lifr->lifr_movetoindex;
17872 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17873 	    q, mp, ip_process_ioctl, &err);
17874 	if (err != 0) {
17875 		/*
17876 		 * There could be only v6.
17877 		 */
17878 		if (err != ENXIO)
17879 			goto done;
17880 		err = 0;
17881 	}
17882 
17883 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17884 	    q, mp, ip_process_ioctl, &err);
17885 	if (err != 0) {
17886 		if (err != ENXIO)
17887 			goto done;
17888 		if (*ill_to_v4 == NULL) {
17889 			err = ENXIO;
17890 			goto done;
17891 		}
17892 		err = 0;
17893 	}
17894 
17895 	/*
17896 	 * If we have something to MOVE i.e "from" not NULL,
17897 	 * "to" should be non-NULL.
17898 	 */
17899 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
17900 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
17901 		err = EINVAL;
17902 	}
17903 
17904 done:
17905 	if (ipif_v4 != NULL)
17906 		ipif_refrele(ipif_v4);
17907 	if (ipif_v6 != NULL)
17908 		ipif_refrele(ipif_v6);
17909 	return (err);
17910 }
17911 
17912 /*
17913  * FAILOVER and FAILBACK are modelled as MOVE operations.
17914  *
17915  * We don't check whether the MOVE is within the same group or
17916  * not, because this ioctl can be used as a generic mechanism
17917  * to failover from interface A to B, though things will function
17918  * only if they are really part of the same group. Moreover,
17919  * all ipifs may be down and hence temporarily out of the group.
17920  *
17921  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17922  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17923  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17924  * have been deleted and there are no active references. Once quiescent the
17925  * ipif's are moved and brought up on the new ill.
17926  *
17927  * Normally the source ill and destination ill belong to the same IPMP group
17928  * and hence the same ipsq_t. In the event they don't belong to the same
17929  * same group the two ipsq's are first merged into one ipsq - that of the
17930  * to_ill. The multicast memberships on the source and destination ill cannot
17931  * change during the move operation since multicast joins/leaves also have to
17932  * execute on the same ipsq and are hence serialized.
17933  */
17934 /* ARGSUSED */
17935 int
17936 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17937     ip_ioctl_cmd_t *ipip, void *ifreq)
17938 {
17939 	ill_t *ill_to_v4 = NULL;
17940 	ill_t *ill_to_v6 = NULL;
17941 	ill_t *ill_from_v4 = NULL;
17942 	ill_t *ill_from_v6 = NULL;
17943 	int err = 0;
17944 
17945 	/*
17946 	 * setup from and to ill's, we can get EINPROGRESS only for
17947 	 * to_ill's.
17948 	 */
17949 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
17950 	    &ill_to_v4, &ill_to_v6);
17951 
17952 	if (err != 0) {
17953 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
17954 		goto done;
17955 	}
17956 
17957 	/*
17958 	 * nothing to do.
17959 	 */
17960 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
17961 		goto done;
17962 	}
17963 
17964 	/*
17965 	 * nothing to do.
17966 	 */
17967 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
17968 		goto done;
17969 	}
17970 
17971 	/*
17972 	 * Mark the ill as changing.
17973 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
17974 	 * in ill_up_ipifs in case of error they are cleared below.
17975 	 */
17976 
17977 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17978 	if (ill_from_v4 != NULL)
17979 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
17980 	if (ill_from_v6 != NULL)
17981 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
17982 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17983 
17984 	/*
17985 	 * Make sure that both src and dst are
17986 	 * in the same syncq group. If not make it happen.
17987 	 * We are not holding any locks because we are the writer
17988 	 * on the from_ipsq and we will hold locks in ill_merge_groups
17989 	 * to protect to_ipsq against changing.
17990 	 */
17991 	if (ill_from_v4 != NULL) {
17992 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
17993 		    ill_to_v4->ill_phyint->phyint_ipsq) {
17994 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
17995 			    NULL, mp, q);
17996 			goto err_ret;
17997 
17998 		}
17999 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
18000 	} else {
18001 
18002 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
18003 		    ill_to_v6->ill_phyint->phyint_ipsq) {
18004 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
18005 			    NULL, mp, q);
18006 			goto err_ret;
18007 
18008 		}
18009 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
18010 	}
18011 
18012 	/*
18013 	 * Now that the ipsq's have been merged and we are the writer
18014 	 * lets mark to_ill as changing as well.
18015 	 */
18016 
18017 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18018 	if (ill_to_v4 != NULL)
18019 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
18020 	if (ill_to_v6 != NULL)
18021 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
18022 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18023 
18024 	/*
18025 	 * Its ok for us to proceed with the move even if
18026 	 * ill_pending_mp is non null on one of the from ill's as the reply
18027 	 * should not be looking at the ipif, it should only care about the
18028 	 * ill itself.
18029 	 */
18030 
18031 	/*
18032 	 * lets move ipv4 first.
18033 	 */
18034 	if (ill_from_v4 != NULL) {
18035 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18036 		ill_from_v4->ill_move_in_progress = B_TRUE;
18037 		ill_to_v4->ill_move_in_progress = B_TRUE;
18038 		ill_to_v4->ill_move_peer = ill_from_v4;
18039 		ill_from_v4->ill_move_peer = ill_to_v4;
18040 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18041 	}
18042 
18043 	/*
18044 	 * Now lets move ipv6.
18045 	 */
18046 	if (err == 0 && ill_from_v6 != NULL) {
18047 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18048 		ill_from_v6->ill_move_in_progress = B_TRUE;
18049 		ill_to_v6->ill_move_in_progress = B_TRUE;
18050 		ill_to_v6->ill_move_peer = ill_from_v6;
18051 		ill_from_v6->ill_move_peer = ill_to_v6;
18052 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18053 	}
18054 
18055 err_ret:
18056 	/*
18057 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18058 	 * moved to become quiescent.
18059 	 */
18060 	if (err == EINPROGRESS) {
18061 		goto done;
18062 	}
18063 
18064 	/*
18065 	 * if err is set ill_up_ipifs will not be called
18066 	 * lets clear the flags.
18067 	 */
18068 
18069 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18070 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18071 	/*
18072 	 * Some of the clearing may be redundant. But it is simple
18073 	 * not making any extra checks.
18074 	 */
18075 	if (ill_from_v6 != NULL) {
18076 		ill_from_v6->ill_move_in_progress = B_FALSE;
18077 		ill_from_v6->ill_move_peer = NULL;
18078 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18079 	}
18080 	if (ill_from_v4 != NULL) {
18081 		ill_from_v4->ill_move_in_progress = B_FALSE;
18082 		ill_from_v4->ill_move_peer = NULL;
18083 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18084 	}
18085 	if (ill_to_v6 != NULL) {
18086 		ill_to_v6->ill_move_in_progress = B_FALSE;
18087 		ill_to_v6->ill_move_peer = NULL;
18088 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18089 	}
18090 	if (ill_to_v4 != NULL) {
18091 		ill_to_v4->ill_move_in_progress = B_FALSE;
18092 		ill_to_v4->ill_move_peer = NULL;
18093 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18094 	}
18095 
18096 	/*
18097 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18098 	 * Do this always to maintain proper state i.e even in case of errors.
18099 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18100 	 * we need not call on both v4 and v6 interfaces.
18101 	 */
18102 	if (ill_from_v4 != NULL) {
18103 		if ((ill_from_v4->ill_phyint->phyint_flags &
18104 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18105 			phyint_inactive(ill_from_v4->ill_phyint);
18106 		}
18107 	} else if (ill_from_v6 != NULL) {
18108 		if ((ill_from_v6->ill_phyint->phyint_flags &
18109 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18110 			phyint_inactive(ill_from_v6->ill_phyint);
18111 		}
18112 	}
18113 
18114 	if (ill_to_v4 != NULL) {
18115 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18116 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18117 		}
18118 	} else if (ill_to_v6 != NULL) {
18119 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18120 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18121 		}
18122 	}
18123 
18124 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18125 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18126 
18127 no_err:
18128 	/*
18129 	 * lets bring the interfaces up on the to_ill.
18130 	 */
18131 	if (err == 0) {
18132 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18133 		    q, mp);
18134 	}
18135 
18136 	if (err == 0) {
18137 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18138 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18139 
18140 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18141 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18142 	}
18143 done:
18144 
18145 	if (ill_to_v4 != NULL) {
18146 		ill_refrele(ill_to_v4);
18147 	}
18148 	if (ill_to_v6 != NULL) {
18149 		ill_refrele(ill_to_v6);
18150 	}
18151 
18152 	return (err);
18153 }
18154 
18155 static void
18156 ill_dl_down(ill_t *ill)
18157 {
18158 	/*
18159 	 * The ill is down; unbind but stay attached since we're still
18160 	 * associated with a PPA. If we have negotiated DLPI capabilites
18161 	 * with the data link service provider (IDS_OK) then reset them.
18162 	 * The interval between unbinding and rebinding is potentially
18163 	 * unbounded hence we cannot assume things will be the same.
18164 	 * The DLPI capabilities will be probed again when the data link
18165 	 * is brought up.
18166 	 */
18167 	mblk_t	*mp = ill->ill_unbind_mp;
18168 	hook_nic_event_t *info;
18169 
18170 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18171 
18172 	ill->ill_unbind_mp = NULL;
18173 	if (mp != NULL) {
18174 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18175 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18176 		    ill->ill_name));
18177 		mutex_enter(&ill->ill_lock);
18178 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18179 		mutex_exit(&ill->ill_lock);
18180 		if (ill->ill_dlpi_capab_state == IDS_OK)
18181 			ill_capability_reset(ill);
18182 		ill_dlpi_send(ill, mp);
18183 	}
18184 
18185 	/*
18186 	 * Toss all of our multicast memberships.  We could keep them, but
18187 	 * then we'd have to do bookkeeping of any joins and leaves performed
18188 	 * by the application while the the interface is down (we can't just
18189 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18190 	 * on a downed interface).
18191 	 */
18192 	ill_leave_multicast(ill);
18193 
18194 	mutex_enter(&ill->ill_lock);
18195 
18196 	ill->ill_dl_up = 0;
18197 
18198 	if ((info = ill->ill_nic_event_info) != NULL) {
18199 		ip2dbg(("ill_dl_down:unexpected nic event %d attached for %s\n",
18200 		    info->hne_event, ill->ill_name));
18201 		if (info->hne_data != NULL)
18202 			kmem_free(info->hne_data, info->hne_datalen);
18203 		kmem_free(info, sizeof (hook_nic_event_t));
18204 	}
18205 
18206 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
18207 	if (info != NULL) {
18208 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
18209 		info->hne_lif = 0;
18210 		info->hne_event = NE_DOWN;
18211 		info->hne_data = NULL;
18212 		info->hne_datalen = 0;
18213 		info->hne_family = ill->ill_isv6 ? ipv6 : ipv4;
18214 	} else
18215 		ip2dbg(("ill_dl_down: could not attach DOWN nic event "
18216 		    "information for %s (ENOMEM)\n", ill->ill_name));
18217 
18218 	ill->ill_nic_event_info = info;
18219 
18220 	mutex_exit(&ill->ill_lock);
18221 }
18222 
18223 void
18224 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18225 {
18226 	union DL_primitives *dlp;
18227 	t_uscalar_t prim;
18228 
18229 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18230 
18231 	dlp = (union DL_primitives *)mp->b_rptr;
18232 	prim = dlp->dl_primitive;
18233 
18234 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18235 		dlpi_prim_str(prim), prim, ill->ill_name));
18236 
18237 	switch (prim) {
18238 	case DL_PHYS_ADDR_REQ:
18239 	{
18240 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18241 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18242 		break;
18243 	}
18244 	case DL_BIND_REQ:
18245 		mutex_enter(&ill->ill_lock);
18246 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18247 		mutex_exit(&ill->ill_lock);
18248 		break;
18249 	}
18250 
18251 	/*
18252 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18253 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18254 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18255 	 */
18256 	mutex_enter(&ill->ill_lock);
18257 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18258 	    (prim == DL_UNBIND_REQ)) {
18259 		ill->ill_dlpi_pending = prim;
18260 	}
18261 	mutex_exit(&ill->ill_lock);
18262 
18263 	/*
18264 	 * Some drivers send M_FLUSH up to IP as part of unbind
18265 	 * request.  When this M_FLUSH is sent back to the driver,
18266 	 * this can go after we send the detach request if the
18267 	 * M_FLUSH ends up in IP's syncq. To avoid that, we reply
18268 	 * to the M_FLUSH in ip_rput and locally generate another
18269 	 * M_FLUSH for the correctness.  This will get freed in
18270 	 * ip_wput_nondata.
18271 	 */
18272 	if (prim == DL_UNBIND_REQ)
18273 		(void) putnextctl1(ill->ill_rq, M_FLUSH, FLUSHRW);
18274 
18275 	putnext(ill->ill_wq, mp);
18276 }
18277 
18278 /*
18279  * Send a DLPI control message to the driver but make sure there
18280  * is only one outstanding message. Uses ill_dlpi_pending to tell
18281  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18282  * when an ACK or a NAK is received to process the next queued message.
18283  *
18284  * We don't protect ill_dlpi_pending with any lock. This is okay as
18285  * every place where its accessed, ip is exclusive while accessing
18286  * ill_dlpi_pending except when this function is called from ill_init()
18287  */
18288 void
18289 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18290 {
18291 	mblk_t **mpp;
18292 
18293 	ASSERT(IAM_WRITER_ILL(ill));
18294 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18295 
18296 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18297 		/* Must queue message. Tail insertion */
18298 		mpp = &ill->ill_dlpi_deferred;
18299 		while (*mpp != NULL)
18300 			mpp = &((*mpp)->b_next);
18301 
18302 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18303 		    ill->ill_name));
18304 
18305 		*mpp = mp;
18306 		return;
18307 	}
18308 
18309 	ill_dlpi_dispatch(ill, mp);
18310 }
18311 
18312 /*
18313  * Called when an DLPI control message has been acked or nacked to
18314  * send down the next queued message (if any).
18315  */
18316 void
18317 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18318 {
18319 	mblk_t *mp;
18320 
18321 	ASSERT(IAM_WRITER_ILL(ill));
18322 
18323 	ASSERT(prim != DL_PRIM_INVAL);
18324 	if (ill->ill_dlpi_pending != prim) {
18325 		if (ill->ill_dlpi_pending == DL_PRIM_INVAL) {
18326 			(void) mi_strlog(ill->ill_rq, 1,
18327 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
18328 			    "ill_dlpi_done: unsolicited ack for %s from %s\n",
18329 			    dlpi_prim_str(prim), ill->ill_name);
18330 		} else {
18331 			(void) mi_strlog(ill->ill_rq, 1,
18332 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
18333 			    "ill_dlpi_done: unexpected ack for %s from %s "
18334 			    "(expecting ack for %s)\n",
18335 			    dlpi_prim_str(prim), ill->ill_name,
18336 			    dlpi_prim_str(ill->ill_dlpi_pending));
18337 		}
18338 		return;
18339 	}
18340 
18341 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18342 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18343 
18344 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18345 		mutex_enter(&ill->ill_lock);
18346 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18347 		cv_signal(&ill->ill_cv);
18348 		mutex_exit(&ill->ill_lock);
18349 		return;
18350 	}
18351 
18352 	ill->ill_dlpi_deferred = mp->b_next;
18353 	mp->b_next = NULL;
18354 
18355 	ill_dlpi_dispatch(ill, mp);
18356 }
18357 
18358 void
18359 conn_delete_ire(conn_t *connp, caddr_t arg)
18360 {
18361 	ipif_t	*ipif = (ipif_t *)arg;
18362 	ire_t	*ire;
18363 
18364 	/*
18365 	 * Look at the cached ires on conns which has pointers to ipifs.
18366 	 * We just call ire_refrele which clears up the reference
18367 	 * to ire. Called when a conn closes. Also called from ipif_free
18368 	 * to cleanup indirect references to the stale ipif via the cached ire.
18369 	 */
18370 	mutex_enter(&connp->conn_lock);
18371 	ire = connp->conn_ire_cache;
18372 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18373 		connp->conn_ire_cache = NULL;
18374 		mutex_exit(&connp->conn_lock);
18375 		IRE_REFRELE_NOTR(ire);
18376 		return;
18377 	}
18378 	mutex_exit(&connp->conn_lock);
18379 
18380 }
18381 
18382 /*
18383  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18384  * of IREs. Those IREs may have been previously cached in the conn structure.
18385  * This ipcl_walk() walker function releases all references to such IREs based
18386  * on the condemned flag.
18387  */
18388 /* ARGSUSED */
18389 void
18390 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18391 {
18392 	ire_t	*ire;
18393 
18394 	mutex_enter(&connp->conn_lock);
18395 	ire = connp->conn_ire_cache;
18396 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18397 		connp->conn_ire_cache = NULL;
18398 		mutex_exit(&connp->conn_lock);
18399 		IRE_REFRELE_NOTR(ire);
18400 		return;
18401 	}
18402 	mutex_exit(&connp->conn_lock);
18403 }
18404 
18405 /*
18406  * Take down a specific interface, but don't lose any information about it.
18407  * Also delete interface from its interface group (ifgrp).
18408  * (Always called as writer.)
18409  * This function goes through the down sequence even if the interface is
18410  * already down. There are 2 reasons.
18411  * a. Currently we permit interface routes that depend on down interfaces
18412  *    to be added. This behaviour itself is questionable. However it appears
18413  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18414  *    time. We go thru the cleanup in order to remove these routes.
18415  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18416  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18417  *    down, but we need to cleanup i.e. do ill_dl_down and
18418  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18419  *
18420  * IP-MT notes:
18421  *
18422  * Model of reference to interfaces.
18423  *
18424  * The following members in ipif_t track references to the ipif.
18425  *	int     ipif_refcnt;    Active reference count
18426  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18427  * The following members in ill_t track references to the ill.
18428  *	int             ill_refcnt;     active refcnt
18429  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18430  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18431  *
18432  * Reference to an ipif or ill can be obtained in any of the following ways.
18433  *
18434  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18435  * Pointers to ipif / ill from other data structures viz ire and conn.
18436  * Implicit reference to the ipif / ill by holding a reference to the ire.
18437  *
18438  * The ipif/ill lookup functions return a reference held ipif / ill.
18439  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18440  * This is a purely dynamic reference count associated with threads holding
18441  * references to the ipif / ill. Pointers from other structures do not
18442  * count towards this reference count.
18443  *
18444  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
18445  * ipif/ill. This is incremented whenever a new ire is created referencing the
18446  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
18447  * actually added to the ire hash table. The count is decremented in
18448  * ire_inactive where the ire is destroyed.
18449  *
18450  * nce's reference ill's thru nce_ill and the count of nce's associated with
18451  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18452  * ndp_add() where the nce is actually added to the table. Similarly it is
18453  * decremented in ndp_inactive where the nce is destroyed.
18454  *
18455  * Flow of ioctls involving interface down/up
18456  *
18457  * The following is the sequence of an attempt to set some critical flags on an
18458  * up interface.
18459  * ip_sioctl_flags
18460  * ipif_down
18461  * wait for ipif to be quiescent
18462  * ipif_down_tail
18463  * ip_sioctl_flags_tail
18464  *
18465  * All set ioctls that involve down/up sequence would have a skeleton similar
18466  * to the above. All the *tail functions are called after the refcounts have
18467  * dropped to the appropriate values.
18468  *
18469  * The mechanism to quiesce an ipif is as follows.
18470  *
18471  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18472  * on the ipif. Callers either pass a flag requesting wait or the lookup
18473  *  functions will return NULL.
18474  *
18475  * Delete all ires referencing this ipif
18476  *
18477  * Any thread attempting to do an ipif_refhold on an ipif that has been
18478  * obtained thru a cached pointer will first make sure that
18479  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18480  * increment the refcount.
18481  *
18482  * The above guarantees that the ipif refcount will eventually come down to
18483  * zero and the ipif will quiesce, once all threads that currently hold a
18484  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18485  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18486  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
18487  * drop to zero.
18488  *
18489  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18490  *
18491  * Threads trying to lookup an ipif or ill can pass a flag requesting
18492  * wait and restart if the ipif / ill cannot be looked up currently.
18493  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18494  * failure if the ipif is currently undergoing an exclusive operation, and
18495  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18496  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18497  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18498  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18499  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18500  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18501  * until we release the ipsq_lock, even though the the ill/ipif state flags
18502  * can change after we drop the ill_lock.
18503  *
18504  * An attempt to send out a packet using an ipif that is currently
18505  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18506  * operation and restart it later when the exclusive condition on the ipif ends.
18507  * This is an example of not passing the wait flag to the lookup functions. For
18508  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18509  * out a multicast packet on that ipif will fail while the ipif is
18510  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18511  * currently IPIF_CHANGING will also fail.
18512  */
18513 int
18514 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18515 {
18516 	ill_t		*ill = ipif->ipif_ill;
18517 	phyint_t	*phyi;
18518 	conn_t		*connp;
18519 	boolean_t	success;
18520 	boolean_t	ipif_was_up = B_FALSE;
18521 
18522 	ASSERT(IAM_WRITER_IPIF(ipif));
18523 
18524 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18525 
18526 	if (ipif->ipif_flags & IPIF_UP) {
18527 		mutex_enter(&ill->ill_lock);
18528 		ipif->ipif_flags &= ~IPIF_UP;
18529 		ASSERT(ill->ill_ipif_up_count > 0);
18530 		--ill->ill_ipif_up_count;
18531 		mutex_exit(&ill->ill_lock);
18532 		ipif_was_up = B_TRUE;
18533 		/* Update status in SCTP's list */
18534 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18535 	}
18536 
18537 	/*
18538 	 * Blow away v6 memberships we established in ipif_multicast_up(); the
18539 	 * v4 ones are left alone (as is the ipif_multicast_up flag, so we
18540 	 * know not to rejoin when the interface is brought back up).
18541 	 */
18542 	if (ipif->ipif_isv6)
18543 		ipif_multicast_down(ipif);
18544 	/*
18545 	 * Remove from the mapping for __sin6_src_id. We insert only
18546 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18547 	 * stored as mapped addresses, we need to check for mapped
18548 	 * INADDR_ANY also.
18549 	 */
18550 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18551 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18552 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18553 		int err;
18554 
18555 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18556 		    ipif->ipif_zoneid);
18557 		if (err != 0) {
18558 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18559 		}
18560 	}
18561 
18562 	/*
18563 	 * Before we delete the ill from the group (if any), we need
18564 	 * to make sure that we delete all the routes dependent on
18565 	 * this and also any ipifs dependent on this ipif for
18566 	 * source address. We need to do before we delete from
18567 	 * the group because
18568 	 *
18569 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18570 	 *
18571 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18572 	 *    for re-doing source address selection. Note that
18573 	 *    ipif_select_source[_v6] called from
18574 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18575 	 *    because we have already marked down here i.e cleared
18576 	 *    IPIF_UP.
18577 	 */
18578 	if (ipif->ipif_isv6)
18579 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES);
18580 	else
18581 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES);
18582 
18583 	/*
18584 	 * Need to add these also to be saved and restored when the
18585 	 * ipif is brought down and up
18586 	 */
18587 	mutex_enter(&ire_mrtun_lock);
18588 	if (ire_mrtun_count != 0) {
18589 		mutex_exit(&ire_mrtun_lock);
18590 		ire_walk_ill_mrtun(0, 0, ipif_down_delete_ire,
18591 		    (char *)ipif, NULL);
18592 	} else {
18593 		mutex_exit(&ire_mrtun_lock);
18594 	}
18595 
18596 	mutex_enter(&ire_srcif_table_lock);
18597 	if (ire_srcif_table_count > 0) {
18598 		mutex_exit(&ire_srcif_table_lock);
18599 		ire_walk_srcif_table_v4(ipif_down_delete_ire, (char *)ipif);
18600 	} else {
18601 		mutex_exit(&ire_srcif_table_lock);
18602 	}
18603 
18604 	/*
18605 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18606 	 * ires have been deleted above. Otherwise a thread could end up
18607 	 * caching an ire in a conn after we have finished the cleanup of the
18608 	 * conn. The caching is done after making sure that the ire is not yet
18609 	 * condemned. Also documented in the block comment above ip_output
18610 	 */
18611 	ipcl_walk(conn_cleanup_stale_ire, NULL);
18612 	/* Also, delete the ires cached in SCTP */
18613 	sctp_ire_cache_flush(ipif);
18614 
18615 	/* Resolve any IPsec/IKE NAT-T instances that depend on this ipif. */
18616 	nattymod_clean_ipif(ipif);
18617 
18618 	/*
18619 	 * Update any other ipifs which have used "our" local address as
18620 	 * a source address. This entails removing and recreating IRE_INTERFACE
18621 	 * entries for such ipifs.
18622 	 */
18623 	if (ipif->ipif_isv6)
18624 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18625 	else
18626 		ipif_update_other_ipifs(ipif, ill->ill_group);
18627 
18628 	if (ipif_was_up) {
18629 		/*
18630 		 * Check whether it is last ipif to leave this group.
18631 		 * If this is the last ipif to leave, we should remove
18632 		 * this ill from the group as ipif_select_source will not
18633 		 * be able to find any useful ipifs if this ill is selected
18634 		 * for load balancing.
18635 		 *
18636 		 * For nameless groups, we should call ifgrp_delete if this
18637 		 * belongs to some group. As this ipif is going down, we may
18638 		 * need to reconstruct groups.
18639 		 */
18640 		phyi = ill->ill_phyint;
18641 		/*
18642 		 * If the phyint_groupname_len is 0, it may or may not
18643 		 * be in the nameless group. If the phyint_groupname_len is
18644 		 * not 0, then this ill should be part of some group.
18645 		 * As we always insert this ill in the group if
18646 		 * phyint_groupname_len is not zero when the first ipif
18647 		 * comes up (in ipif_up_done), it should be in a group
18648 		 * when the namelen is not 0.
18649 		 *
18650 		 * NOTE : When we delete the ill from the group,it will
18651 		 * blow away all the IRE_CACHES pointing either at this ipif or
18652 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18653 		 * should be pointing at this ill.
18654 		 */
18655 		ASSERT(phyi->phyint_groupname_len == 0 ||
18656 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18657 
18658 		if (phyi->phyint_groupname_len != 0) {
18659 			if (ill->ill_ipif_up_count == 0)
18660 				illgrp_delete(ill);
18661 		}
18662 
18663 		/*
18664 		 * If we have deleted some of the broadcast ires associated
18665 		 * with this ipif, we need to re-nominate somebody else if
18666 		 * the ires that we deleted were the nominated ones.
18667 		 */
18668 		if (ill->ill_group != NULL && !ill->ill_isv6)
18669 			ipif_renominate_bcast(ipif);
18670 	}
18671 
18672 	/*
18673 	 * neighbor-discovery or arp entries for this interface.
18674 	 */
18675 	ipif_ndp_down(ipif);
18676 
18677 	/*
18678 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18679 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18680 	 * and ill_delete -> ipif_free -> ipif_down
18681 	 */
18682 	if (mp == NULL) {
18683 		ASSERT(q == NULL);
18684 		return (0);
18685 	}
18686 
18687 	if (CONN_Q(q)) {
18688 		connp = Q_TO_CONN(q);
18689 		mutex_enter(&connp->conn_lock);
18690 	} else {
18691 		connp = NULL;
18692 	}
18693 	mutex_enter(&ill->ill_lock);
18694 	/*
18695 	 * Are there any ire's pointing to this ipif that are still active ?
18696 	 * If this is the last ipif going down, are there any ire's pointing
18697 	 * to this ill that are still active ?
18698 	 */
18699 	if (ipif_is_quiescent(ipif)) {
18700 		mutex_exit(&ill->ill_lock);
18701 		if (connp != NULL)
18702 			mutex_exit(&connp->conn_lock);
18703 		return (0);
18704 	}
18705 
18706 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18707 	    ill->ill_name, (void *)ill));
18708 	/*
18709 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18710 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18711 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18712 	 */
18713 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18714 	if (!success) {
18715 		/* The conn is closing. So just return */
18716 		ASSERT(connp != NULL);
18717 		mutex_exit(&ill->ill_lock);
18718 		mutex_exit(&connp->conn_lock);
18719 		return (EINTR);
18720 	}
18721 
18722 	mutex_exit(&ill->ill_lock);
18723 	if (connp != NULL)
18724 		mutex_exit(&connp->conn_lock);
18725 	return (EINPROGRESS);
18726 }
18727 
18728 void
18729 ipif_down_tail(ipif_t *ipif)
18730 {
18731 	ill_t	*ill = ipif->ipif_ill;
18732 
18733 	/*
18734 	 * Skip any loopback interface (null wq).
18735 	 * If this is the last logical interface on the ill
18736 	 * have ill_dl_down tell the driver we are gone (unbind)
18737 	 * Note that lun 0 can ipif_down even though
18738 	 * there are other logical units that are up.
18739 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18740 	 */
18741 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18742 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18743 	    ill->ill_dl_up) {
18744 		ill_dl_down(ill);
18745 	}
18746 	ill->ill_logical_down = 0;
18747 
18748 	/*
18749 	 * Have to be after removing the routes in ipif_down_delete_ire.
18750 	 */
18751 	if (ipif->ipif_isv6) {
18752 		if (ill->ill_flags & ILLF_XRESOLV)
18753 			ipif_arp_down(ipif);
18754 	} else {
18755 		ipif_arp_down(ipif);
18756 	}
18757 
18758 	ip_rts_ifmsg(ipif);
18759 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18760 }
18761 
18762 /*
18763  * Bring interface logically down without bringing the physical interface
18764  * down e.g. when the netmask is changed. This avoids long lasting link
18765  * negotiations between an ethernet interface and a certain switches.
18766  */
18767 static int
18768 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18769 {
18770 	/*
18771 	 * The ill_logical_down flag is a transient flag. It is set here
18772 	 * and is cleared once the down has completed in ipif_down_tail.
18773 	 * This flag does not indicate whether the ill stream is in the
18774 	 * DL_BOUND state with the driver. Instead this flag is used by
18775 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18776 	 * the driver. The state of the ill stream i.e. whether it is
18777 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18778 	 */
18779 	ipif->ipif_ill->ill_logical_down = 1;
18780 	return (ipif_down(ipif, q, mp));
18781 }
18782 
18783 /*
18784  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18785  * If the usesrc client ILL is already part of a usesrc group or not,
18786  * in either case a ire_stq with the matching usesrc client ILL will
18787  * locate the IRE's that need to be deleted. We want IREs to be created
18788  * with the new source address.
18789  */
18790 static void
18791 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18792 {
18793 	ill_t	*ucill = (ill_t *)ill_arg;
18794 
18795 	ASSERT(IAM_WRITER_ILL(ucill));
18796 
18797 	if (ire->ire_stq == NULL)
18798 		return;
18799 
18800 	if ((ire->ire_type == IRE_CACHE) &&
18801 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18802 		ire_delete(ire);
18803 }
18804 
18805 /*
18806  * ire_walk routine to delete every IRE dependent on the interface
18807  * address that is going down.	(Always called as writer.)
18808  * Works for both v4 and v6.
18809  * In addition for checking for ire_ipif matches it also checks for
18810  * IRE_CACHE entries which have the same source address as the
18811  * disappearing ipif since ipif_select_source might have picked
18812  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18813  * care of any IRE_INTERFACE with the disappearing source address.
18814  */
18815 static void
18816 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18817 {
18818 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18819 	ill_t *ire_ill;
18820 	ill_t *ipif_ill;
18821 
18822 	ASSERT(IAM_WRITER_IPIF(ipif));
18823 	if (ire->ire_ipif == NULL)
18824 		return;
18825 
18826 	/*
18827 	 * For IPv4, we derive source addresses for an IRE from ipif's
18828 	 * belonging to the same IPMP group as the IRE's outgoing
18829 	 * interface.  If an IRE's outgoing interface isn't in the
18830 	 * same IPMP group as a particular ipif, then that ipif
18831 	 * couldn't have been used as a source address for this IRE.
18832 	 *
18833 	 * For IPv6, source addresses are only restricted to the IPMP group
18834 	 * if the IRE is for a link-local address or a multicast address.
18835 	 * Otherwise, source addresses for an IRE can be chosen from
18836 	 * interfaces other than the the outgoing interface for that IRE.
18837 	 *
18838 	 * For source address selection details, see ipif_select_source()
18839 	 * and ipif_select_source_v6().
18840 	 */
18841 	if (ire->ire_ipversion == IPV4_VERSION ||
18842 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18843 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18844 		ire_ill = ire->ire_ipif->ipif_ill;
18845 		ipif_ill = ipif->ipif_ill;
18846 
18847 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18848 			return;
18849 		}
18850 	}
18851 
18852 
18853 	if (ire->ire_ipif != ipif) {
18854 		/*
18855 		 * Look for a matching source address.
18856 		 */
18857 		if (ire->ire_type != IRE_CACHE)
18858 			return;
18859 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18860 			return;
18861 
18862 		if (ire->ire_ipversion == IPV4_VERSION) {
18863 			if (ire->ire_src_addr != ipif->ipif_src_addr)
18864 				return;
18865 		} else {
18866 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
18867 			    &ipif->ipif_v6lcl_addr))
18868 				return;
18869 		}
18870 		ire_delete(ire);
18871 		return;
18872 	}
18873 	/*
18874 	 * ire_delete() will do an ire_flush_cache which will delete
18875 	 * all ire_ipif matches
18876 	 */
18877 	ire_delete(ire);
18878 }
18879 
18880 /*
18881  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
18882  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
18883  * 2) when an interface is brought up or down (on that ill).
18884  * This ensures that the IRE_CACHE entries don't retain stale source
18885  * address selection results.
18886  */
18887 void
18888 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
18889 {
18890 	ill_t	*ill = (ill_t *)ill_arg;
18891 	ill_t	*ipif_ill;
18892 
18893 	ASSERT(IAM_WRITER_ILL(ill));
18894 	/*
18895 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18896 	 * Hence this should be IRE_CACHE.
18897 	 */
18898 	ASSERT(ire->ire_type == IRE_CACHE);
18899 
18900 	/*
18901 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
18902 	 * We are only interested in IRE_CACHES that has borrowed
18903 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
18904 	 * for which we need to look at ire_ipif->ipif_ill match
18905 	 * with ill.
18906 	 */
18907 	ASSERT(ire->ire_ipif != NULL);
18908 	ipif_ill = ire->ire_ipif->ipif_ill;
18909 	if (ipif_ill == ill || (ill->ill_group != NULL &&
18910 	    ipif_ill->ill_group == ill->ill_group)) {
18911 		ire_delete(ire);
18912 	}
18913 }
18914 
18915 /*
18916  * Delete all the ire whose stq references ill_arg.
18917  */
18918 static void
18919 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
18920 {
18921 	ill_t	*ill = (ill_t *)ill_arg;
18922 	ill_t	*ire_ill;
18923 
18924 	ASSERT(IAM_WRITER_ILL(ill));
18925 	/*
18926 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18927 	 * Hence this should be IRE_CACHE.
18928 	 */
18929 	ASSERT(ire->ire_type == IRE_CACHE);
18930 
18931 	/*
18932 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18933 	 * matches ill. We are only interested in IRE_CACHES that
18934 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
18935 	 * filtering here.
18936 	 */
18937 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
18938 
18939 	if (ire_ill == ill)
18940 		ire_delete(ire);
18941 }
18942 
18943 /*
18944  * This is called when an ill leaves the group. We want to delete
18945  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
18946  * pointing at ill.
18947  */
18948 static void
18949 illgrp_cache_delete(ire_t *ire, char *ill_arg)
18950 {
18951 	ill_t	*ill = (ill_t *)ill_arg;
18952 
18953 	ASSERT(IAM_WRITER_ILL(ill));
18954 	ASSERT(ill->ill_group == NULL);
18955 	/*
18956 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18957 	 * Hence this should be IRE_CACHE.
18958 	 */
18959 	ASSERT(ire->ire_type == IRE_CACHE);
18960 	/*
18961 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18962 	 * matches ill. We are interested in both.
18963 	 */
18964 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
18965 	    (ire->ire_ipif->ipif_ill == ill));
18966 
18967 	ire_delete(ire);
18968 }
18969 
18970 /*
18971  * Initiate deallocate of an IPIF. Always called as writer. Called by
18972  * ill_delete or ip_sioctl_removeif.
18973  */
18974 static void
18975 ipif_free(ipif_t *ipif)
18976 {
18977 	ASSERT(IAM_WRITER_IPIF(ipif));
18978 
18979 	if (ipif->ipif_recovery_id != 0)
18980 		(void) untimeout(ipif->ipif_recovery_id);
18981 	ipif->ipif_recovery_id = 0;
18982 
18983 	/* Remove conn references */
18984 	reset_conn_ipif(ipif);
18985 
18986 	/*
18987 	 * Make sure we have valid net and subnet broadcast ire's for the
18988 	 * other ipif's which share them with this ipif.
18989 	 */
18990 	if (!ipif->ipif_isv6)
18991 		ipif_check_bcast_ires(ipif);
18992 
18993 	/*
18994 	 * Take down the interface. We can be called either from ill_delete
18995 	 * or from ip_sioctl_removeif.
18996 	 */
18997 	(void) ipif_down(ipif, NULL, NULL);
18998 
18999 	/*
19000 	 * Now that the interface is down, there's no chance it can still
19001 	 * become a duplicate.  Cancel any timer that may have been set while
19002 	 * tearing down.
19003 	 */
19004 	if (ipif->ipif_recovery_id != 0)
19005 		(void) untimeout(ipif->ipif_recovery_id);
19006 	ipif->ipif_recovery_id = 0;
19007 
19008 	rw_enter(&ill_g_lock, RW_WRITER);
19009 	/* Remove pointers to this ill in the multicast routing tables */
19010 	reset_mrt_vif_ipif(ipif);
19011 	rw_exit(&ill_g_lock);
19012 }
19013 
19014 /*
19015  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19016  * also ill_move().
19017  */
19018 static void
19019 ipif_free_tail(ipif_t *ipif)
19020 {
19021 	mblk_t	*mp;
19022 	ipif_t	**ipifp;
19023 
19024 	/*
19025 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19026 	 */
19027 	mutex_enter(&ipif->ipif_saved_ire_lock);
19028 	mp = ipif->ipif_saved_ire_mp;
19029 	ipif->ipif_saved_ire_mp = NULL;
19030 	mutex_exit(&ipif->ipif_saved_ire_lock);
19031 	freemsg(mp);
19032 
19033 	/*
19034 	 * Need to hold both ill_g_lock and ill_lock while
19035 	 * inserting or removing an ipif from the linked list
19036 	 * of ipifs hanging off the ill.
19037 	 */
19038 	rw_enter(&ill_g_lock, RW_WRITER);
19039 	/*
19040 	 * Remove all multicast memberships on the interface now.
19041 	 * This removes IPv4 multicast memberships joined within
19042 	 * the kernel as ipif_down does not do ipif_multicast_down
19043 	 * for IPv4. IPv6 is not handled here as the multicast memberships
19044 	 * are based on ill and not on ipif.
19045 	 */
19046 	ilm_free(ipif);
19047 
19048 	/*
19049 	 * Since we held the ill_g_lock while doing the ilm_free above,
19050 	 * we can assert the ilms were really deleted and not just marked
19051 	 * ILM_DELETED.
19052 	 */
19053 	ASSERT(ilm_walk_ipif(ipif) == 0);
19054 
19055 
19056 	IPIF_TRACE_CLEANUP(ipif);
19057 
19058 	/* Ask SCTP to take it out of it list */
19059 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19060 
19061 	mutex_enter(&ipif->ipif_ill->ill_lock);
19062 	/* Get it out of the ILL interface list. */
19063 	ipifp = &ipif->ipif_ill->ill_ipif;
19064 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
19065 		if (*ipifp == ipif) {
19066 			*ipifp = ipif->ipif_next;
19067 			break;
19068 		}
19069 	}
19070 
19071 	mutex_exit(&ipif->ipif_ill->ill_lock);
19072 	rw_exit(&ill_g_lock);
19073 
19074 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19075 
19076 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19077 	ASSERT(ipif->ipif_recovery_id == 0);
19078 
19079 	/* Free the memory. */
19080 	mi_free((char *)ipif);
19081 }
19082 
19083 /*
19084  * Returns an ipif name in the form "ill_name/unit" if ipif_id is not zero,
19085  * "ill_name" otherwise.
19086  */
19087 char *
19088 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19089 {
19090 	char	lbuf[32];
19091 	char	*name;
19092 	size_t	name_len;
19093 
19094 	buf[0] = '\0';
19095 	if (!ipif)
19096 		return (buf);
19097 	name = ipif->ipif_ill->ill_name;
19098 	name_len = ipif->ipif_ill->ill_name_length;
19099 	if (ipif->ipif_id != 0) {
19100 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19101 		    ipif->ipif_id);
19102 		name = lbuf;
19103 		name_len = mi_strlen(name) + 1;
19104 	}
19105 	len -= 1;
19106 	buf[len] = '\0';
19107 	len = MIN(len, name_len);
19108 	bcopy(name, buf, len);
19109 	return (buf);
19110 }
19111 
19112 /*
19113  * Find an IPIF based on the name passed in.  Names can be of the
19114  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19115  * The <phys> string can have forms like <dev><#> (e.g., le0),
19116  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19117  * When there is no colon, the implied unit id is zero. <phys> must
19118  * correspond to the name of an ILL.  (May be called as writer.)
19119  */
19120 static ipif_t *
19121 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19122     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19123     mblk_t *mp, ipsq_func_t func, int *error)
19124 {
19125 	char	*cp;
19126 	char	*endp;
19127 	long	id;
19128 	ill_t	*ill;
19129 	ipif_t	*ipif;
19130 	uint_t	ire_type;
19131 	boolean_t did_alloc = B_FALSE;
19132 	ipsq_t	*ipsq;
19133 
19134 	if (error != NULL)
19135 		*error = 0;
19136 
19137 	/*
19138 	 * If the caller wants to us to create the ipif, make sure we have a
19139 	 * valid zoneid
19140 	 */
19141 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19142 
19143 	if (namelen == 0) {
19144 		if (error != NULL)
19145 			*error = ENXIO;
19146 		return (NULL);
19147 	}
19148 
19149 	*exists = B_FALSE;
19150 	/* Look for a colon in the name. */
19151 	endp = &name[namelen];
19152 	for (cp = endp; --cp > name; ) {
19153 		if (*cp == IPIF_SEPARATOR_CHAR)
19154 			break;
19155 	}
19156 
19157 	if (*cp == IPIF_SEPARATOR_CHAR) {
19158 		/*
19159 		 * Reject any non-decimal aliases for logical
19160 		 * interfaces. Aliases with leading zeroes
19161 		 * are also rejected as they introduce ambiguity
19162 		 * in the naming of the interfaces.
19163 		 * In order to confirm with existing semantics,
19164 		 * and to not break any programs/script relying
19165 		 * on that behaviour, if<0>:0 is considered to be
19166 		 * a valid interface.
19167 		 *
19168 		 * If alias has two or more digits and the first
19169 		 * is zero, fail.
19170 		 */
19171 		if (&cp[2] < endp && cp[1] == '0')
19172 			return (NULL);
19173 	}
19174 
19175 	if (cp <= name) {
19176 		cp = endp;
19177 	} else {
19178 		*cp = '\0';
19179 	}
19180 
19181 	/*
19182 	 * Look up the ILL, based on the portion of the name
19183 	 * before the slash. ill_lookup_on_name returns a held ill.
19184 	 * Temporary to check whether ill exists already. If so
19185 	 * ill_lookup_on_name will clear it.
19186 	 */
19187 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19188 	    q, mp, func, error, &did_alloc);
19189 	if (cp != endp)
19190 		*cp = IPIF_SEPARATOR_CHAR;
19191 	if (ill == NULL)
19192 		return (NULL);
19193 
19194 	/* Establish the unit number in the name. */
19195 	id = 0;
19196 	if (cp < endp && *endp == '\0') {
19197 		/* If there was a colon, the unit number follows. */
19198 		cp++;
19199 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19200 			ill_refrele(ill);
19201 			if (error != NULL)
19202 				*error = ENXIO;
19203 			return (NULL);
19204 		}
19205 	}
19206 
19207 	GRAB_CONN_LOCK(q);
19208 	mutex_enter(&ill->ill_lock);
19209 	/* Now see if there is an IPIF with this unit number. */
19210 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19211 		if (ipif->ipif_id == id) {
19212 			if (zoneid != ALL_ZONES &&
19213 			    zoneid != ipif->ipif_zoneid &&
19214 			    ipif->ipif_zoneid != ALL_ZONES) {
19215 				mutex_exit(&ill->ill_lock);
19216 				RELEASE_CONN_LOCK(q);
19217 				ill_refrele(ill);
19218 				if (error != NULL)
19219 					*error = ENXIO;
19220 				return (NULL);
19221 			}
19222 			/*
19223 			 * The block comment at the start of ipif_down
19224 			 * explains the use of the macros used below
19225 			 */
19226 			if (IPIF_CAN_LOOKUP(ipif)) {
19227 				ipif_refhold_locked(ipif);
19228 				mutex_exit(&ill->ill_lock);
19229 				if (!did_alloc)
19230 					*exists = B_TRUE;
19231 				/*
19232 				 * Drop locks before calling ill_refrele
19233 				 * since it can potentially call into
19234 				 * ipif_ill_refrele_tail which can end up
19235 				 * in trying to acquire any lock.
19236 				 */
19237 				RELEASE_CONN_LOCK(q);
19238 				ill_refrele(ill);
19239 				return (ipif);
19240 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19241 				ipsq = ill->ill_phyint->phyint_ipsq;
19242 				mutex_enter(&ipsq->ipsq_lock);
19243 				mutex_exit(&ill->ill_lock);
19244 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19245 				mutex_exit(&ipsq->ipsq_lock);
19246 				RELEASE_CONN_LOCK(q);
19247 				ill_refrele(ill);
19248 				*error = EINPROGRESS;
19249 				return (NULL);
19250 			}
19251 		}
19252 	}
19253 	RELEASE_CONN_LOCK(q);
19254 
19255 	if (!do_alloc) {
19256 		mutex_exit(&ill->ill_lock);
19257 		ill_refrele(ill);
19258 		if (error != NULL)
19259 			*error = ENXIO;
19260 		return (NULL);
19261 	}
19262 
19263 	/*
19264 	 * If none found, atomically allocate and return a new one.
19265 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19266 	 * to support "receive only" use of lo0:1 etc. as is still done
19267 	 * below as an initial guess.
19268 	 * However, this is now likely to be overriden later in ipif_up_done()
19269 	 * when we know for sure what address has been configured on the
19270 	 * interface, since we might have more than one loopback interface
19271 	 * with a loopback address, e.g. in the case of zones, and all the
19272 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19273 	 */
19274 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19275 		ire_type = IRE_LOOPBACK;
19276 	else
19277 		ire_type = IRE_LOCAL;
19278 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19279 	if (ipif != NULL)
19280 		ipif_refhold_locked(ipif);
19281 	else if (error != NULL)
19282 		*error = ENOMEM;
19283 	mutex_exit(&ill->ill_lock);
19284 	ill_refrele(ill);
19285 	return (ipif);
19286 }
19287 
19288 /*
19289  * This routine is called whenever a new address comes up on an ipif.  If
19290  * we are configured to respond to address mask requests, then we are supposed
19291  * to broadcast an address mask reply at this time.  This routine is also
19292  * called if we are already up, but a netmask change is made.  This is legal
19293  * but might not make the system manager very popular.	(May be called
19294  * as writer.)
19295  */
19296 void
19297 ipif_mask_reply(ipif_t *ipif)
19298 {
19299 	icmph_t	*icmph;
19300 	ipha_t	*ipha;
19301 	mblk_t	*mp;
19302 
19303 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19304 
19305 	if (!ip_respond_to_address_mask_broadcast)
19306 		return;
19307 
19308 	/* ICMP mask reply is IPv4 only */
19309 	ASSERT(!ipif->ipif_isv6);
19310 	/* ICMP mask reply is not for a loopback interface */
19311 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19312 
19313 	mp = allocb(REPLY_LEN, BPRI_HI);
19314 	if (mp == NULL)
19315 		return;
19316 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19317 
19318 	ipha = (ipha_t *)mp->b_rptr;
19319 	bzero(ipha, REPLY_LEN);
19320 	*ipha = icmp_ipha;
19321 	ipha->ipha_ttl = ip_broadcast_ttl;
19322 	ipha->ipha_src = ipif->ipif_src_addr;
19323 	ipha->ipha_dst = ipif->ipif_brd_addr;
19324 	ipha->ipha_length = htons(REPLY_LEN);
19325 	ipha->ipha_ident = 0;
19326 
19327 	icmph = (icmph_t *)&ipha[1];
19328 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19329 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19330 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19331 	if (icmph->icmph_checksum == 0)
19332 		icmph->icmph_checksum = 0xffff;
19333 
19334 	put(ipif->ipif_wq, mp);
19335 
19336 #undef	REPLY_LEN
19337 }
19338 
19339 /*
19340  * When the mtu in the ipif changes, we call this routine through ire_walk
19341  * to update all the relevant IREs.
19342  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19343  */
19344 static void
19345 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19346 {
19347 	ipif_t *ipif = (ipif_t *)ipif_arg;
19348 
19349 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19350 		return;
19351 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19352 }
19353 
19354 /*
19355  * When the mtu in the ill changes, we call this routine through ire_walk
19356  * to update all the relevant IREs.
19357  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19358  */
19359 void
19360 ill_mtu_change(ire_t *ire, char *ill_arg)
19361 {
19362 	ill_t	*ill = (ill_t *)ill_arg;
19363 
19364 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19365 		return;
19366 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19367 }
19368 
19369 /*
19370  * Join the ipif specific multicast groups.
19371  * Must be called after a mapping has been set up in the resolver.  (Always
19372  * called as writer.)
19373  */
19374 void
19375 ipif_multicast_up(ipif_t *ipif)
19376 {
19377 	int err, index;
19378 	ill_t *ill;
19379 
19380 	ASSERT(IAM_WRITER_IPIF(ipif));
19381 
19382 	ill = ipif->ipif_ill;
19383 	index = ill->ill_phyint->phyint_ifindex;
19384 
19385 	ip1dbg(("ipif_multicast_up\n"));
19386 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19387 		return;
19388 
19389 	if (ipif->ipif_isv6) {
19390 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19391 			return;
19392 
19393 		/* Join the all hosts multicast address */
19394 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19395 		/*
19396 		 * Passing B_TRUE means we have to join the multicast
19397 		 * membership on this interface even though this is
19398 		 * FAILED. If we join on a different one in the group,
19399 		 * we will not be able to delete the membership later
19400 		 * as we currently don't track where we join when we
19401 		 * join within the kernel unlike applications where
19402 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19403 		 * for more on this.
19404 		 */
19405 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19406 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19407 		if (err != 0) {
19408 			ip0dbg(("ipif_multicast_up: "
19409 			    "all_hosts_mcast failed %d\n",
19410 			    err));
19411 			return;
19412 		}
19413 		/*
19414 		 * Enable multicast for the solicited node multicast address
19415 		 */
19416 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19417 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19418 
19419 			ipv6_multi.s6_addr32[3] |=
19420 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19421 
19422 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19423 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19424 			    NULL);
19425 			if (err != 0) {
19426 				ip0dbg(("ipif_multicast_up: solicited MC"
19427 				    " failed %d\n", err));
19428 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19429 				    ill, ill->ill_phyint->phyint_ifindex,
19430 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19431 				return;
19432 			}
19433 		}
19434 	} else {
19435 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19436 			return;
19437 
19438 		/* Join the all hosts multicast address */
19439 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19440 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19441 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19442 		if (err) {
19443 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19444 			return;
19445 		}
19446 	}
19447 	ipif->ipif_multicast_up = 1;
19448 }
19449 
19450 /*
19451  * Blow away any IPv6 multicast groups that we joined in ipif_multicast_up();
19452  * any explicit memberships are blown away in ill_leave_multicast() when the
19453  * ill is brought down.
19454  */
19455 static void
19456 ipif_multicast_down(ipif_t *ipif)
19457 {
19458 	int err;
19459 
19460 	ASSERT(IAM_WRITER_IPIF(ipif));
19461 
19462 	ip1dbg(("ipif_multicast_down\n"));
19463 	if (!ipif->ipif_multicast_up)
19464 		return;
19465 
19466 	ASSERT(ipif->ipif_isv6);
19467 
19468 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19469 
19470 	/*
19471 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19472 	 * we should look for ilms on this ill rather than the ones that have
19473 	 * been failed over here.  They are here temporarily. As
19474 	 * ipif_multicast_up has joined on this ill, we should delete only
19475 	 * from this ill.
19476 	 */
19477 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19478 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19479 	    B_TRUE, B_TRUE);
19480 	if (err != 0) {
19481 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19482 		    err));
19483 	}
19484 	/*
19485 	 * Disable multicast for the solicited node multicast address
19486 	 */
19487 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19488 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19489 
19490 		ipv6_multi.s6_addr32[3] |=
19491 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19492 
19493 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19494 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19495 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19496 
19497 		if (err != 0) {
19498 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19499 			    err));
19500 		}
19501 	}
19502 
19503 	ipif->ipif_multicast_up = 0;
19504 }
19505 
19506 /*
19507  * Used when an interface comes up to recreate any extra routes on this
19508  * interface.
19509  */
19510 static ire_t **
19511 ipif_recover_ire(ipif_t *ipif)
19512 {
19513 	mblk_t	*mp;
19514 	ire_t	**ipif_saved_irep;
19515 	ire_t	**irep;
19516 
19517 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19518 	    ipif->ipif_id));
19519 
19520 	mutex_enter(&ipif->ipif_saved_ire_lock);
19521 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19522 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19523 	if (ipif_saved_irep == NULL) {
19524 		mutex_exit(&ipif->ipif_saved_ire_lock);
19525 		return (NULL);
19526 	}
19527 
19528 	irep = ipif_saved_irep;
19529 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19530 		ire_t		*ire;
19531 		queue_t		*rfq;
19532 		queue_t		*stq;
19533 		ifrt_t		*ifrt;
19534 		uchar_t		*src_addr;
19535 		uchar_t		*gateway_addr;
19536 		mblk_t		*resolver_mp;
19537 		ushort_t	type;
19538 
19539 		/*
19540 		 * When the ire was initially created and then added in
19541 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19542 		 * in the case of a traditional interface route, or as one of
19543 		 * the IRE_OFFSUBNET types (with the exception of
19544 		 * IRE_HOST types ire which is created by icmp_redirect() and
19545 		 * which we don't need to save or recover).  In the case where
19546 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19547 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19548 		 * to satisfy software like GateD and Sun Cluster which creates
19549 		 * routes using the the loopback interface's address as a
19550 		 * gateway.
19551 		 *
19552 		 * As ifrt->ifrt_type reflects the already updated ire_type and
19553 		 * since ire_create() expects that IRE_IF_NORESOLVER will have
19554 		 * a valid nce_res_mp field (which doesn't make sense for a
19555 		 * IRE_LOOPBACK), ire_create() will be called in the same way
19556 		 * here as in ip_rt_add(), namely using ipif->ipif_net_type when
19557 		 * the route looks like a traditional interface route (where
19558 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19559 		 * the saved ifrt->ifrt_type.  This means that in the case where
19560 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19561 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19562 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19563 		 */
19564 		ifrt = (ifrt_t *)mp->b_rptr;
19565 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19566 			rfq = NULL;
19567 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19568 			    ? ipif->ipif_rq : ipif->ipif_wq;
19569 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19570 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19571 			    : (uint8_t *)&ipif->ipif_src_addr;
19572 			gateway_addr = NULL;
19573 			resolver_mp = ipif->ipif_resolver_mp;
19574 			type = ipif->ipif_net_type;
19575 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19576 			/* Recover multiroute broadcast IRE. */
19577 			rfq = ipif->ipif_rq;
19578 			stq = ipif->ipif_wq;
19579 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19580 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19581 			    : (uint8_t *)&ipif->ipif_src_addr;
19582 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19583 			resolver_mp = ipif->ipif_bcast_mp;
19584 			type = ifrt->ifrt_type;
19585 		} else {
19586 			rfq = NULL;
19587 			stq = NULL;
19588 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19589 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19590 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19591 			resolver_mp = NULL;
19592 			type = ifrt->ifrt_type;
19593 		}
19594 
19595 		/*
19596 		 * Create a copy of the IRE with the saved address and netmask.
19597 		 */
19598 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19599 		    "0x%x/0x%x\n",
19600 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19601 		    ntohl(ifrt->ifrt_addr),
19602 		    ntohl(ifrt->ifrt_mask)));
19603 		ire = ire_create(
19604 		    (uint8_t *)&ifrt->ifrt_addr,
19605 		    (uint8_t *)&ifrt->ifrt_mask,
19606 		    src_addr,
19607 		    gateway_addr,
19608 		    NULL,
19609 		    &ifrt->ifrt_max_frag,
19610 		    NULL,
19611 		    rfq,
19612 		    stq,
19613 		    type,
19614 		    resolver_mp,
19615 		    ipif,
19616 		    NULL,
19617 		    0,
19618 		    0,
19619 		    0,
19620 		    ifrt->ifrt_flags,
19621 		    &ifrt->ifrt_iulp_info,
19622 		    NULL,
19623 		    NULL);
19624 
19625 		if (ire == NULL) {
19626 			mutex_exit(&ipif->ipif_saved_ire_lock);
19627 			kmem_free(ipif_saved_irep,
19628 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19629 			return (NULL);
19630 		}
19631 
19632 		/*
19633 		 * Some software (for example, GateD and Sun Cluster) attempts
19634 		 * to create (what amount to) IRE_PREFIX routes with the
19635 		 * loopback address as the gateway.  This is primarily done to
19636 		 * set up prefixes with the RTF_REJECT flag set (for example,
19637 		 * when generating aggregate routes.)
19638 		 *
19639 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19640 		 * IRE_LOOPBACK, then we map the request into a
19641 		 * IRE_IF_NORESOLVER.
19642 		 */
19643 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19644 			ire->ire_type = IRE_IF_NORESOLVER;
19645 		/*
19646 		 * ire held by ire_add, will be refreled' towards the
19647 		 * the end of ipif_up_done
19648 		 */
19649 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19650 		*irep = ire;
19651 		irep++;
19652 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19653 	}
19654 	mutex_exit(&ipif->ipif_saved_ire_lock);
19655 	return (ipif_saved_irep);
19656 }
19657 
19658 /*
19659  * Used to set the netmask and broadcast address to default values when the
19660  * interface is brought up.  (Always called as writer.)
19661  */
19662 static void
19663 ipif_set_default(ipif_t *ipif)
19664 {
19665 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19666 
19667 	if (!ipif->ipif_isv6) {
19668 		/*
19669 		 * Interface holds an IPv4 address. Default
19670 		 * mask is the natural netmask.
19671 		 */
19672 		if (!ipif->ipif_net_mask) {
19673 			ipaddr_t	v4mask;
19674 
19675 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19676 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19677 		}
19678 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19679 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19680 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19681 		} else {
19682 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19683 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19684 		}
19685 		/*
19686 		 * NOTE: SunOS 4.X does this even if the broadcast address
19687 		 * has been already set thus we do the same here.
19688 		 */
19689 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19690 			ipaddr_t	v4addr;
19691 
19692 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19693 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19694 		}
19695 	} else {
19696 		/*
19697 		 * Interface holds an IPv6-only address.  Default
19698 		 * mask is all-ones.
19699 		 */
19700 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19701 			ipif->ipif_v6net_mask = ipv6_all_ones;
19702 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19703 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19704 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19705 		} else {
19706 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19707 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19708 		}
19709 	}
19710 }
19711 
19712 /*
19713  * Return 0 if this address can be used as local address without causing
19714  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19715  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19716  * Special checks are needed to allow the same IPv6 link-local address
19717  * on different ills.
19718  * TODO: allowing the same site-local address on different ill's.
19719  */
19720 int
19721 ip_addr_availability_check(ipif_t *new_ipif)
19722 {
19723 	in6_addr_t our_v6addr;
19724 	ill_t *ill;
19725 	ipif_t *ipif;
19726 	ill_walk_context_t ctx;
19727 
19728 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19729 	ASSERT(MUTEX_HELD(&ip_addr_avail_lock));
19730 	ASSERT(RW_READ_HELD(&ill_g_lock));
19731 
19732 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19733 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19734 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19735 		return (0);
19736 
19737 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19738 
19739 	if (new_ipif->ipif_isv6)
19740 		ill = ILL_START_WALK_V6(&ctx);
19741 	else
19742 		ill = ILL_START_WALK_V4(&ctx);
19743 
19744 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19745 		for (ipif = ill->ill_ipif; ipif != NULL;
19746 		    ipif = ipif->ipif_next) {
19747 			if ((ipif == new_ipif) ||
19748 			    !(ipif->ipif_flags & IPIF_UP) ||
19749 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19750 				continue;
19751 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19752 			    &our_v6addr)) {
19753 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19754 				    new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19755 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19756 				    ipif->ipif_flags |= IPIF_UNNUMBERED;
19757 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19758 				    new_ipif->ipif_ill != ill)
19759 					continue;
19760 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19761 				    new_ipif->ipif_ill != ill)
19762 					continue;
19763 				else if (new_ipif->ipif_zoneid !=
19764 				    ipif->ipif_zoneid &&
19765 				    ipif->ipif_zoneid != ALL_ZONES &&
19766 				    (ill->ill_phyint->phyint_flags &
19767 				    PHYI_LOOPBACK))
19768 					continue;
19769 				else if (new_ipif->ipif_ill == ill)
19770 					return (EADDRINUSE);
19771 				else
19772 					return (EADDRNOTAVAIL);
19773 			}
19774 		}
19775 	}
19776 
19777 	return (0);
19778 }
19779 
19780 /*
19781  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19782  * IREs for the ipif.
19783  * When the routine returns EINPROGRESS then mp has been consumed and
19784  * the ioctl will be acked from ip_rput_dlpi.
19785  */
19786 static int
19787 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19788 {
19789 	ill_t	*ill = ipif->ipif_ill;
19790 	boolean_t isv6 = ipif->ipif_isv6;
19791 	int	err = 0;
19792 	boolean_t success;
19793 
19794 	ASSERT(IAM_WRITER_IPIF(ipif));
19795 
19796 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19797 
19798 	/* Shouldn't get here if it is already up. */
19799 	if (ipif->ipif_flags & IPIF_UP)
19800 		return (EALREADY);
19801 
19802 	/* Skip arp/ndp for any loopback interface. */
19803 	if (ill->ill_wq != NULL) {
19804 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19805 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19806 
19807 		if (!ill->ill_dl_up) {
19808 			/*
19809 			 * ill_dl_up is not yet set. i.e. we are yet to
19810 			 * DL_BIND with the driver and this is the first
19811 			 * logical interface on the ill to become "up".
19812 			 * Tell the driver to get going (via DL_BIND_REQ).
19813 			 * Note that changing "significant" IFF_ flags
19814 			 * address/netmask etc cause a down/up dance, but
19815 			 * does not cause an unbind (DL_UNBIND) with the driver
19816 			 */
19817 			return (ill_dl_up(ill, ipif, mp, q));
19818 		}
19819 
19820 		/*
19821 		 * ipif_resolver_up may end up sending an
19822 		 * AR_INTERFACE_UP message to ARP, which would, in
19823 		 * turn send a DLPI message to the driver. ioctls are
19824 		 * serialized and so we cannot send more than one
19825 		 * interface up message at a time. If ipif_resolver_up
19826 		 * does send an interface up message to ARP, we get
19827 		 * EINPROGRESS and we will complete in ip_arp_done.
19828 		 */
19829 
19830 		ASSERT(connp != NULL || !CONN_Q(q));
19831 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19832 		if (connp != NULL)
19833 			mutex_enter(&connp->conn_lock);
19834 		mutex_enter(&ill->ill_lock);
19835 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19836 		mutex_exit(&ill->ill_lock);
19837 		if (connp != NULL)
19838 			mutex_exit(&connp->conn_lock);
19839 		if (!success)
19840 			return (EINTR);
19841 
19842 		/*
19843 		 * Crank up IPv6 neighbor discovery
19844 		 * Unlike ARP, this should complete when
19845 		 * ipif_ndp_up returns. However, for
19846 		 * ILLF_XRESOLV interfaces we also send a
19847 		 * AR_INTERFACE_UP to the external resolver.
19848 		 * That ioctl will complete in ip_rput.
19849 		 */
19850 		if (isv6) {
19851 			err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr);
19852 			if (err != 0) {
19853 				if (err != EINPROGRESS)
19854 					mp = ipsq_pending_mp_get(ipsq, &connp);
19855 				return (err);
19856 			}
19857 		}
19858 		/* Now, ARP */
19859 		err = ipif_resolver_up(ipif, Res_act_initial);
19860 		if (err == EINPROGRESS) {
19861 			/* We will complete it in ip_arp_done */
19862 			return (err);
19863 		}
19864 		mp = ipsq_pending_mp_get(ipsq, &connp);
19865 		ASSERT(mp != NULL);
19866 		if (err != 0)
19867 			return (err);
19868 	} else {
19869 		/*
19870 		 * Interfaces without underlying hardware don't do duplicate
19871 		 * address detection.
19872 		 */
19873 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
19874 		ipif->ipif_addr_ready = 1;
19875 	}
19876 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
19877 }
19878 
19879 /*
19880  * Perform a bind for the physical device.
19881  * When the routine returns EINPROGRESS then mp has been consumed and
19882  * the ioctl will be acked from ip_rput_dlpi.
19883  * Allocate an unbind message and save it until ipif_down.
19884  */
19885 static int
19886 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
19887 {
19888 	mblk_t	*areq_mp = NULL;
19889 	mblk_t	*bind_mp = NULL;
19890 	mblk_t	*unbind_mp = NULL;
19891 	conn_t	*connp;
19892 	boolean_t success;
19893 
19894 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
19895 	ASSERT(IAM_WRITER_ILL(ill));
19896 
19897 	ASSERT(mp != NULL);
19898 
19899 	/* Create a resolver cookie for ARP */
19900 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
19901 		areq_t		*areq;
19902 		uint16_t	sap_addr;
19903 
19904 		areq_mp = ill_arp_alloc(ill,
19905 			(uchar_t *)&ip_areq_template, 0);
19906 		if (areq_mp == NULL) {
19907 			return (ENOMEM);
19908 		}
19909 		freemsg(ill->ill_resolver_mp);
19910 		ill->ill_resolver_mp = areq_mp;
19911 		areq = (areq_t *)areq_mp->b_rptr;
19912 		sap_addr = ill->ill_sap;
19913 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
19914 		/*
19915 		 * Wait till we call ill_pending_mp_add to determine
19916 		 * the success before we free the ill_resolver_mp and
19917 		 * attach areq_mp in it's place.
19918 		 */
19919 	}
19920 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
19921 	    DL_BIND_REQ);
19922 	if (bind_mp == NULL)
19923 		goto bad;
19924 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
19925 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
19926 
19927 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
19928 	if (unbind_mp == NULL)
19929 		goto bad;
19930 
19931 	/*
19932 	 * Record state needed to complete this operation when the
19933 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
19934 	 */
19935 	ASSERT(WR(q)->q_next == NULL);
19936 	connp = Q_TO_CONN(q);
19937 
19938 	mutex_enter(&connp->conn_lock);
19939 	mutex_enter(&ipif->ipif_ill->ill_lock);
19940 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19941 	mutex_exit(&ipif->ipif_ill->ill_lock);
19942 	mutex_exit(&connp->conn_lock);
19943 	if (!success)
19944 		goto bad;
19945 
19946 	/*
19947 	 * Save the unbind message for ill_dl_down(); it will be consumed when
19948 	 * the interface goes down.
19949 	 */
19950 	ASSERT(ill->ill_unbind_mp == NULL);
19951 	ill->ill_unbind_mp = unbind_mp;
19952 
19953 	ill_dlpi_send(ill, bind_mp);
19954 	/* Send down link-layer capabilities probe if not already done. */
19955 	ill_capability_probe(ill);
19956 
19957 	/*
19958 	 * Sysid used to rely on the fact that netboots set domainname
19959 	 * and the like. Now that miniroot boots aren't strictly netboots
19960 	 * and miniroot network configuration is driven from userland
19961 	 * these things still need to be set. This situation can be detected
19962 	 * by comparing the interface being configured here to the one
19963 	 * dhcack was set to reference by the boot loader. Once sysid is
19964 	 * converted to use dhcp_ipc_getinfo() this call can go away.
19965 	 */
19966 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
19967 	    (strcmp(ill->ill_name, dhcack) == 0) &&
19968 	    (strlen(srpc_domain) == 0)) {
19969 		if (dhcpinit() != 0)
19970 			cmn_err(CE_WARN, "no cached dhcp response");
19971 	}
19972 
19973 	/*
19974 	 * This operation will complete in ip_rput_dlpi with either
19975 	 * a DL_BIND_ACK or DL_ERROR_ACK.
19976 	 */
19977 	return (EINPROGRESS);
19978 bad:
19979 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
19980 	/*
19981 	 * We don't have to check for possible removal from illgrp
19982 	 * as we have not yet inserted in illgrp. For groups
19983 	 * without names, this ipif is still not UP and hence
19984 	 * this could not have possibly had any influence in forming
19985 	 * groups.
19986 	 */
19987 
19988 	freemsg(bind_mp);
19989 	freemsg(unbind_mp);
19990 	return (ENOMEM);
19991 }
19992 
19993 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
19994 
19995 /*
19996  * DLPI and ARP is up.
19997  * Create all the IREs associated with an interface bring up multicast.
19998  * Set the interface flag and finish other initialization
19999  * that potentially had to be differed to after DL_BIND_ACK.
20000  */
20001 int
20002 ipif_up_done(ipif_t *ipif)
20003 {
20004 	ire_t	*ire_array[20];
20005 	ire_t	**irep = ire_array;
20006 	ire_t	**irep1;
20007 	ipaddr_t net_mask = 0;
20008 	ipaddr_t subnet_mask, route_mask;
20009 	ill_t	*ill = ipif->ipif_ill;
20010 	queue_t	*stq;
20011 	ipif_t	 *src_ipif;
20012 	ipif_t   *tmp_ipif;
20013 	boolean_t	flush_ire_cache = B_TRUE;
20014 	int	err = 0;
20015 	phyint_t *phyi;
20016 	ire_t	**ipif_saved_irep = NULL;
20017 	int ipif_saved_ire_cnt;
20018 	int	cnt;
20019 	boolean_t	src_ipif_held = B_FALSE;
20020 	boolean_t	ire_added = B_FALSE;
20021 	boolean_t	loopback = B_FALSE;
20022 
20023 	ip1dbg(("ipif_up_done(%s:%u)\n",
20024 		ipif->ipif_ill->ill_name, ipif->ipif_id));
20025 	/* Check if this is a loopback interface */
20026 	if (ipif->ipif_ill->ill_wq == NULL)
20027 		loopback = B_TRUE;
20028 
20029 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20030 	/*
20031 	 * If all other interfaces for this ill are down or DEPRECATED,
20032 	 * or otherwise unsuitable for source address selection, remove
20033 	 * any IRE_CACHE entries for this ill to make sure source
20034 	 * address selection gets to take this new ipif into account.
20035 	 * No need to hold ill_lock while traversing the ipif list since
20036 	 * we are writer
20037 	 */
20038 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20039 		tmp_ipif = tmp_ipif->ipif_next) {
20040 		if (((tmp_ipif->ipif_flags &
20041 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20042 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20043 		    (tmp_ipif == ipif))
20044 			continue;
20045 		/* first useable pre-existing interface */
20046 		flush_ire_cache = B_FALSE;
20047 		break;
20048 	}
20049 	if (flush_ire_cache)
20050 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20051 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20052 
20053 	/*
20054 	 * Figure out which way the send-to queue should go.  Only
20055 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20056 	 * should show up here.
20057 	 */
20058 	switch (ill->ill_net_type) {
20059 	case IRE_IF_RESOLVER:
20060 		stq = ill->ill_rq;
20061 		break;
20062 	case IRE_IF_NORESOLVER:
20063 	case IRE_LOOPBACK:
20064 		stq = ill->ill_wq;
20065 		break;
20066 	default:
20067 		return (EINVAL);
20068 	}
20069 
20070 	if (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) {
20071 		/*
20072 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20073 		 * ipif_lookup_on_name(), but in the case of zones we can have
20074 		 * several loopback addresses on lo0. So all the interfaces with
20075 		 * loopback addresses need to be marked IRE_LOOPBACK.
20076 		 */
20077 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20078 		    htonl(INADDR_LOOPBACK))
20079 			ipif->ipif_ire_type = IRE_LOOPBACK;
20080 		else
20081 			ipif->ipif_ire_type = IRE_LOCAL;
20082 	}
20083 
20084 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20085 		/*
20086 		 * Can't use our source address. Select a different
20087 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20088 		 */
20089 		src_ipif = ipif_select_source(ipif->ipif_ill,
20090 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20091 		if (src_ipif == NULL)
20092 			src_ipif = ipif;	/* Last resort */
20093 		else
20094 			src_ipif_held = B_TRUE;
20095 	} else {
20096 		src_ipif = ipif;
20097 	}
20098 
20099 	/* Create all the IREs associated with this interface */
20100 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20101 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20102 
20103 		/*
20104 		 * If we're on a labeled system then make sure that zone-
20105 		 * private addresses have proper remote host database entries.
20106 		 */
20107 		if (is_system_labeled() &&
20108 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20109 		    !tsol_check_interface_address(ipif))
20110 			return (EINVAL);
20111 
20112 		/* Register the source address for __sin6_src_id */
20113 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20114 		    ipif->ipif_zoneid);
20115 		if (err != 0) {
20116 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20117 			return (err);
20118 		}
20119 
20120 		/* If the interface address is set, create the local IRE. */
20121 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20122 			(void *)ipif,
20123 			ipif->ipif_ire_type,
20124 			ntohl(ipif->ipif_lcl_addr)));
20125 		*irep++ = ire_create(
20126 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20127 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20128 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20129 		    NULL,				/* no gateway */
20130 		    NULL,
20131 		    &ip_loopback_mtuplus,		/* max frag size */
20132 		    NULL,
20133 		    ipif->ipif_rq,			/* recv-from queue */
20134 		    NULL,				/* no send-to queue */
20135 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20136 		    NULL,
20137 		    ipif,
20138 		    NULL,
20139 		    0,
20140 		    0,
20141 		    0,
20142 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20143 		    RTF_PRIVATE : 0,
20144 		    &ire_uinfo_null,
20145 		    NULL,
20146 		    NULL);
20147 	} else {
20148 		ip1dbg((
20149 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20150 		    ipif->ipif_ire_type,
20151 		    ntohl(ipif->ipif_lcl_addr),
20152 		    (uint_t)ipif->ipif_flags));
20153 	}
20154 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20155 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20156 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20157 	} else {
20158 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20159 	}
20160 
20161 	subnet_mask = ipif->ipif_net_mask;
20162 
20163 	/*
20164 	 * If mask was not specified, use natural netmask of
20165 	 * interface address. Also, store this mask back into the
20166 	 * ipif struct.
20167 	 */
20168 	if (subnet_mask == 0) {
20169 		subnet_mask = net_mask;
20170 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20171 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20172 		    ipif->ipif_v6subnet);
20173 	}
20174 
20175 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20176 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20177 	    ipif->ipif_subnet != INADDR_ANY) {
20178 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20179 
20180 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20181 			route_mask = IP_HOST_MASK;
20182 		} else {
20183 			route_mask = subnet_mask;
20184 		}
20185 
20186 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20187 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20188 			(void *)ipif, (void *)ill,
20189 			ill->ill_net_type,
20190 			ntohl(ipif->ipif_subnet)));
20191 		*irep++ = ire_create(
20192 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20193 		    (uchar_t *)&route_mask,		/* mask */
20194 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20195 		    NULL,				/* no gateway */
20196 		    NULL,
20197 		    &ipif->ipif_mtu,			/* max frag */
20198 		    NULL,
20199 		    NULL,				/* no recv queue */
20200 		    stq,				/* send-to queue */
20201 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20202 		    ill->ill_resolver_mp,		/* xmit header */
20203 		    ipif,
20204 		    NULL,
20205 		    0,
20206 		    0,
20207 		    0,
20208 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20209 		    &ire_uinfo_null,
20210 		    NULL,
20211 		    NULL);
20212 	}
20213 
20214 	/*
20215 	 * If the interface address is set, create the broadcast IREs.
20216 	 *
20217 	 * ire_create_bcast checks if the proposed new IRE matches
20218 	 * any existing IRE's with the same physical interface (ILL).
20219 	 * This should get rid of duplicates.
20220 	 * ire_create_bcast also check IPIF_NOXMIT and does not create
20221 	 * any broadcast ires.
20222 	 */
20223 	if ((ipif->ipif_subnet != INADDR_ANY) &&
20224 	    (ipif->ipif_flags & IPIF_BROADCAST)) {
20225 		ipaddr_t addr;
20226 
20227 		ip1dbg(("ipif_up_done: creating broadcast IRE\n"));
20228 		irep = ire_check_and_create_bcast(ipif, 0, irep,
20229 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20230 		irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep,
20231 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20232 
20233 		/*
20234 		 * For backward compatibility, we need to create net
20235 		 * broadcast ire's based on the old "IP address class
20236 		 * system."  The reason is that some old machines only
20237 		 * respond to these class derived net broadcast.
20238 		 *
20239 		 * But we should not create these net broadcast ire's if
20240 		 * the subnet_mask is shorter than the IP address class based
20241 		 * derived netmask.  Otherwise, we may create a net
20242 		 * broadcast address which is the same as an IP address
20243 		 * on the subnet.  Then TCP will refuse to talk to that
20244 		 * address.
20245 		 *
20246 		 * Nor do we need IRE_BROADCAST ire's for the interface
20247 		 * with the netmask as 0xFFFFFFFF, as IRE_LOCAL for that
20248 		 * interface is already created.  Creating these broadcast
20249 		 * ire's will only create confusion as the "addr" is going
20250 		 * to be same as that of the IP address of the interface.
20251 		 */
20252 		if (net_mask < subnet_mask) {
20253 			addr = net_mask & ipif->ipif_subnet;
20254 			irep = ire_check_and_create_bcast(ipif, addr, irep,
20255 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20256 			irep = ire_check_and_create_bcast(ipif,
20257 			    ~net_mask | addr, irep,
20258 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20259 		}
20260 
20261 		if (subnet_mask != 0xFFFFFFFF) {
20262 			addr = ipif->ipif_subnet;
20263 			irep = ire_check_and_create_bcast(ipif, addr, irep,
20264 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20265 			irep = ire_check_and_create_bcast(ipif,
20266 			    ~subnet_mask|addr, irep,
20267 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20268 		}
20269 	}
20270 
20271 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20272 
20273 	/* If an earlier ire_create failed, get out now */
20274 	for (irep1 = irep; irep1 > ire_array; ) {
20275 		irep1--;
20276 		if (*irep1 == NULL) {
20277 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20278 			err = ENOMEM;
20279 			goto bad;
20280 		}
20281 	}
20282 
20283 	/*
20284 	 * Need to atomically check for ip_addr_availablity_check
20285 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20286 	 * from group also.The ill_g_lock is grabbed as reader
20287 	 * just to make sure no new ills or new ipifs are being added
20288 	 * to the system while we are checking the uniqueness of addresses.
20289 	 */
20290 	rw_enter(&ill_g_lock, RW_READER);
20291 	mutex_enter(&ip_addr_avail_lock);
20292 	/* Mark it up, and increment counters. */
20293 	ipif->ipif_flags |= IPIF_UP;
20294 	ill->ill_ipif_up_count++;
20295 	err = ip_addr_availability_check(ipif);
20296 	mutex_exit(&ip_addr_avail_lock);
20297 	rw_exit(&ill_g_lock);
20298 
20299 	if (err != 0) {
20300 		/*
20301 		 * Our address may already be up on the same ill. In this case,
20302 		 * the ARP entry for our ipif replaced the one for the other
20303 		 * ipif. So we don't want to delete it (otherwise the other ipif
20304 		 * would be unable to send packets).
20305 		 * ip_addr_availability_check() identifies this case for us and
20306 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20307 		 * which is the expected error code.
20308 		 */
20309 		if (err == EADDRINUSE) {
20310 			freemsg(ipif->ipif_arp_del_mp);
20311 			ipif->ipif_arp_del_mp = NULL;
20312 			err = EADDRNOTAVAIL;
20313 		}
20314 		ill->ill_ipif_up_count--;
20315 		ipif->ipif_flags &= ~IPIF_UP;
20316 		goto bad;
20317 	}
20318 
20319 	/*
20320 	 * Add in all newly created IREs.  ire_create_bcast() has
20321 	 * already checked for duplicates of the IRE_BROADCAST type.
20322 	 * We want to add before we call ifgrp_insert which wants
20323 	 * to know whether IRE_IF_RESOLVER exists or not.
20324 	 *
20325 	 * NOTE : We refrele the ire though we may branch to "bad"
20326 	 *	  later on where we do ire_delete. This is okay
20327 	 *	  because nobody can delete it as we are running
20328 	 *	  exclusively.
20329 	 */
20330 	for (irep1 = irep; irep1 > ire_array; ) {
20331 		irep1--;
20332 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20333 		/*
20334 		 * refheld by ire_add. refele towards the end of the func
20335 		 */
20336 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20337 	}
20338 	ire_added = B_TRUE;
20339 	/*
20340 	 * Form groups if possible.
20341 	 *
20342 	 * If we are supposed to be in a ill_group with a name, insert it
20343 	 * now as we know that at least one ipif is UP. Otherwise form
20344 	 * nameless groups.
20345 	 *
20346 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20347 	 * this ipif into the appropriate interface group, or create a
20348 	 * new one. If this is already in a nameless group, we try to form
20349 	 * a bigger group looking at other ills potentially sharing this
20350 	 * ipif's prefix.
20351 	 */
20352 	phyi = ill->ill_phyint;
20353 	if (phyi->phyint_groupname_len != 0) {
20354 		ASSERT(phyi->phyint_groupname != NULL);
20355 		if (ill->ill_ipif_up_count == 1) {
20356 			ASSERT(ill->ill_group == NULL);
20357 			err = illgrp_insert(&illgrp_head_v4, ill,
20358 			    phyi->phyint_groupname, NULL, B_TRUE);
20359 			if (err != 0) {
20360 				ip1dbg(("ipif_up_done: illgrp allocation "
20361 				    "failed, error %d\n", err));
20362 				goto bad;
20363 			}
20364 		}
20365 		ASSERT(ill->ill_group != NULL);
20366 	}
20367 
20368 	/*
20369 	 * When this is part of group, we need to make sure that
20370 	 * any broadcast ires created because of this ipif coming
20371 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20372 	 * so that we don't receive duplicate broadcast packets.
20373 	 */
20374 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20375 		ipif_renominate_bcast(ipif);
20376 
20377 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20378 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20379 	ipif_saved_irep = ipif_recover_ire(ipif);
20380 
20381 	if (!loopback) {
20382 		/*
20383 		 * If the broadcast address has been set, make sure it makes
20384 		 * sense based on the interface address.
20385 		 * Only match on ill since we are sharing broadcast addresses.
20386 		 */
20387 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20388 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20389 			ire_t	*ire;
20390 
20391 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20392 			    IRE_BROADCAST, ipif, ALL_ZONES,
20393 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL));
20394 
20395 			if (ire == NULL) {
20396 				/*
20397 				 * If there isn't a matching broadcast IRE,
20398 				 * revert to the default for this netmask.
20399 				 */
20400 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20401 				mutex_enter(&ipif->ipif_ill->ill_lock);
20402 				ipif_set_default(ipif);
20403 				mutex_exit(&ipif->ipif_ill->ill_lock);
20404 			} else {
20405 				ire_refrele(ire);
20406 			}
20407 		}
20408 
20409 	}
20410 
20411 	/* This is the first interface on this ill */
20412 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20413 		/*
20414 		 * Need to recover all multicast memberships in the driver.
20415 		 * This had to be deferred until we had attached.
20416 		 */
20417 		ill_recover_multicast(ill);
20418 	}
20419 	/* Join the allhosts multicast address */
20420 	ipif_multicast_up(ipif);
20421 
20422 	if (!loopback) {
20423 		/*
20424 		 * See whether anybody else would benefit from the
20425 		 * new ipif that we added. We call this always rather
20426 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20427 		 * ipif is for the benefit of illgrp_insert (done above)
20428 		 * which does not do source address selection as it does
20429 		 * not want to re-create interface routes that we are
20430 		 * having reference to it here.
20431 		 */
20432 		ill_update_source_selection(ill);
20433 	}
20434 
20435 	for (irep1 = irep; irep1 > ire_array; ) {
20436 		irep1--;
20437 		if (*irep1 != NULL) {
20438 			/* was held in ire_add */
20439 			ire_refrele(*irep1);
20440 		}
20441 	}
20442 
20443 	cnt = ipif_saved_ire_cnt;
20444 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20445 		if (*irep1 != NULL) {
20446 			/* was held in ire_add */
20447 			ire_refrele(*irep1);
20448 		}
20449 	}
20450 
20451 	if (!loopback && ipif->ipif_addr_ready) {
20452 		/* Broadcast an address mask reply. */
20453 		ipif_mask_reply(ipif);
20454 	}
20455 	if (ipif_saved_irep != NULL) {
20456 		kmem_free(ipif_saved_irep,
20457 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20458 	}
20459 	if (src_ipif_held)
20460 		ipif_refrele(src_ipif);
20461 
20462 	/*
20463 	 * This had to be deferred until we had bound.  Tell routing sockets and
20464 	 * others that this interface is up if it looks like the address has
20465 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20466 	 * duplicate address detection to do its thing.
20467 	 */
20468 	if (ipif->ipif_addr_ready) {
20469 		ip_rts_ifmsg(ipif);
20470 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20471 		/* Let SCTP update the status for this ipif */
20472 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20473 	}
20474 	return (0);
20475 
20476 bad:
20477 	ip1dbg(("ipif_up_done: FAILED \n"));
20478 	/*
20479 	 * We don't have to bother removing from ill groups because
20480 	 *
20481 	 * 1) For groups with names, we insert only when the first ipif
20482 	 *    comes up. In that case if it fails, it will not be in any
20483 	 *    group. So, we need not try to remove for that case.
20484 	 *
20485 	 * 2) For groups without names, either we tried to insert ipif_ill
20486 	 *    in a group as singleton or found some other group to become
20487 	 *    a bigger group. For the former, if it fails we don't have
20488 	 *    anything to do as ipif_ill is not in the group and for the
20489 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20490 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20491 	 */
20492 	while (irep > ire_array) {
20493 		irep--;
20494 		if (*irep != NULL) {
20495 			ire_delete(*irep);
20496 			if (ire_added)
20497 				ire_refrele(*irep);
20498 		}
20499 	}
20500 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid);
20501 
20502 	if (ipif_saved_irep != NULL) {
20503 		kmem_free(ipif_saved_irep,
20504 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20505 	}
20506 	if (src_ipif_held)
20507 		ipif_refrele(src_ipif);
20508 
20509 	ipif_arp_down(ipif);
20510 	return (err);
20511 }
20512 
20513 /*
20514  * Turn off the ARP with the ILLF_NOARP flag.
20515  */
20516 static int
20517 ill_arp_off(ill_t *ill)
20518 {
20519 	mblk_t	*arp_off_mp = NULL;
20520 	mblk_t	*arp_on_mp = NULL;
20521 
20522 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20523 
20524 	ASSERT(IAM_WRITER_ILL(ill));
20525 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20526 
20527 	/*
20528 	 * If the on message is still around we've already done
20529 	 * an arp_off without doing an arp_on thus there is no
20530 	 * work needed.
20531 	 */
20532 	if (ill->ill_arp_on_mp != NULL)
20533 		return (0);
20534 
20535 	/*
20536 	 * Allocate an ARP on message (to be saved) and an ARP off message
20537 	 */
20538 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20539 	if (!arp_off_mp)
20540 		return (ENOMEM);
20541 
20542 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20543 	if (!arp_on_mp)
20544 		goto failed;
20545 
20546 	ASSERT(ill->ill_arp_on_mp == NULL);
20547 	ill->ill_arp_on_mp = arp_on_mp;
20548 
20549 	/* Send an AR_INTERFACE_OFF request */
20550 	putnext(ill->ill_rq, arp_off_mp);
20551 	return (0);
20552 failed:
20553 
20554 	if (arp_off_mp)
20555 		freemsg(arp_off_mp);
20556 	return (ENOMEM);
20557 }
20558 
20559 /*
20560  * Turn on ARP by turning off the ILLF_NOARP flag.
20561  */
20562 static int
20563 ill_arp_on(ill_t *ill)
20564 {
20565 	mblk_t	*mp;
20566 
20567 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20568 
20569 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20570 
20571 	ASSERT(IAM_WRITER_ILL(ill));
20572 	/*
20573 	 * Send an AR_INTERFACE_ON request if we have already done
20574 	 * an arp_off (which allocated the message).
20575 	 */
20576 	if (ill->ill_arp_on_mp != NULL) {
20577 		mp = ill->ill_arp_on_mp;
20578 		ill->ill_arp_on_mp = NULL;
20579 		putnext(ill->ill_rq, mp);
20580 	}
20581 	return (0);
20582 }
20583 
20584 /*
20585  * Called after either deleting ill from the group or when setting
20586  * FAILED or STANDBY on the interface.
20587  */
20588 static void
20589 illgrp_reset_schednext(ill_t *ill)
20590 {
20591 	ill_group_t *illgrp;
20592 	ill_t *save_ill;
20593 
20594 	ASSERT(IAM_WRITER_ILL(ill));
20595 	/*
20596 	 * When called from illgrp_delete, ill_group will be non-NULL.
20597 	 * But when called from ip_sioctl_flags, it could be NULL if
20598 	 * somebody is setting FAILED/INACTIVE on some interface which
20599 	 * is not part of a group.
20600 	 */
20601 	illgrp = ill->ill_group;
20602 	if (illgrp == NULL)
20603 		return;
20604 	if (illgrp->illgrp_ill_schednext != ill)
20605 		return;
20606 
20607 	illgrp->illgrp_ill_schednext = NULL;
20608 	save_ill = ill;
20609 	/*
20610 	 * Choose a good ill to be the next one for
20611 	 * outbound traffic. As the flags FAILED/STANDBY is
20612 	 * not yet marked when called from ip_sioctl_flags,
20613 	 * we check for ill separately.
20614 	 */
20615 	for (ill = illgrp->illgrp_ill; ill != NULL;
20616 	    ill = ill->ill_group_next) {
20617 		if ((ill != save_ill) &&
20618 		    !(ill->ill_phyint->phyint_flags &
20619 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20620 			illgrp->illgrp_ill_schednext = ill;
20621 			return;
20622 		}
20623 	}
20624 }
20625 
20626 /*
20627  * Given an ill, find the next ill in the group to be scheduled.
20628  * (This should be called by ip_newroute() before ire_create().)
20629  * The passed in ill may be pulled out of the group, after we have picked
20630  * up a different outgoing ill from the same group. However ire add will
20631  * atomically check this.
20632  */
20633 ill_t *
20634 illgrp_scheduler(ill_t *ill)
20635 {
20636 	ill_t *retill;
20637 	ill_group_t *illgrp;
20638 	int illcnt;
20639 	int i;
20640 	uint64_t flags;
20641 
20642 	/*
20643 	 * We don't use a lock to check for the ill_group. If this ill
20644 	 * is currently being inserted we may end up just returning this
20645 	 * ill itself. That is ok.
20646 	 */
20647 	if (ill->ill_group == NULL) {
20648 		ill_refhold(ill);
20649 		return (ill);
20650 	}
20651 
20652 	/*
20653 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20654 	 * a set of stable ills. No ill can be added or deleted or change
20655 	 * group while we hold the reader lock.
20656 	 */
20657 	rw_enter(&ill_g_lock, RW_READER);
20658 	if ((illgrp = ill->ill_group) == NULL) {
20659 		rw_exit(&ill_g_lock);
20660 		ill_refhold(ill);
20661 		return (ill);
20662 	}
20663 
20664 	illcnt = illgrp->illgrp_ill_count;
20665 	mutex_enter(&illgrp->illgrp_lock);
20666 	retill = illgrp->illgrp_ill_schednext;
20667 
20668 	if (retill == NULL)
20669 		retill = illgrp->illgrp_ill;
20670 
20671 	/*
20672 	 * We do a circular search beginning at illgrp_ill_schednext
20673 	 * or illgrp_ill. We don't check the flags against the ill lock
20674 	 * since it can change anytime. The ire creation will be atomic
20675 	 * and will fail if the ill is FAILED or OFFLINE.
20676 	 */
20677 	for (i = 0; i < illcnt; i++) {
20678 		flags = retill->ill_phyint->phyint_flags;
20679 
20680 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20681 		    ILL_CAN_LOOKUP(retill)) {
20682 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20683 			ill_refhold(retill);
20684 			break;
20685 		}
20686 		retill = retill->ill_group_next;
20687 		if (retill == NULL)
20688 			retill = illgrp->illgrp_ill;
20689 	}
20690 	mutex_exit(&illgrp->illgrp_lock);
20691 	rw_exit(&ill_g_lock);
20692 
20693 	return (i == illcnt ? NULL : retill);
20694 }
20695 
20696 /*
20697  * Checks for availbility of a usable source address (if there is one) when the
20698  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20699  * this selection is done regardless of the destination.
20700  */
20701 boolean_t
20702 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20703 {
20704 	uint_t	ifindex;
20705 	ipif_t	*ipif = NULL;
20706 	ill_t	*uill;
20707 	boolean_t isv6;
20708 
20709 	ASSERT(ill != NULL);
20710 
20711 	isv6 = ill->ill_isv6;
20712 	ifindex = ill->ill_usesrc_ifindex;
20713 	if (ifindex != 0) {
20714 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20715 		    NULL);
20716 		if (uill == NULL)
20717 			return (NULL);
20718 		mutex_enter(&uill->ill_lock);
20719 		for (ipif = uill->ill_ipif; ipif != NULL;
20720 		    ipif = ipif->ipif_next) {
20721 			if (!IPIF_CAN_LOOKUP(ipif))
20722 				continue;
20723 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20724 				continue;
20725 			if (!(ipif->ipif_flags & IPIF_UP))
20726 				continue;
20727 			if (ipif->ipif_zoneid != zoneid)
20728 				continue;
20729 			if ((isv6 &&
20730 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20731 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20732 				continue;
20733 			mutex_exit(&uill->ill_lock);
20734 			ill_refrele(uill);
20735 			return (B_TRUE);
20736 		}
20737 		mutex_exit(&uill->ill_lock);
20738 		ill_refrele(uill);
20739 	}
20740 	return (B_FALSE);
20741 }
20742 
20743 /*
20744  * Determine the best source address given a destination address and an ill.
20745  * Prefers non-deprecated over deprecated but will return a deprecated
20746  * address if there is no other choice. If there is a usable source address
20747  * on the interface pointed to by ill_usesrc_ifindex then that is given
20748  * first preference.
20749  *
20750  * Returns NULL if there is no suitable source address for the ill.
20751  * This only occurs when there is no valid source address for the ill.
20752  */
20753 ipif_t *
20754 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20755 {
20756 	ipif_t *ipif;
20757 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20758 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20759 	int index = 0;
20760 	boolean_t wrapped = B_FALSE;
20761 	boolean_t same_subnet_only = B_FALSE;
20762 	boolean_t ipif_same_found, ipif_other_found;
20763 	boolean_t specific_found;
20764 	ill_t	*till, *usill = NULL;
20765 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20766 
20767 	if (ill->ill_usesrc_ifindex != 0) {
20768 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex, B_FALSE,
20769 		    NULL, NULL, NULL, NULL);
20770 		if (usill != NULL)
20771 			ill = usill;	/* Select source from usesrc ILL */
20772 		else
20773 			return (NULL);
20774 	}
20775 
20776 	/*
20777 	 * If we're dealing with an unlabeled destination on a labeled system,
20778 	 * make sure that we ignore source addresses that are incompatible with
20779 	 * the destination's default label.  That destination's default label
20780 	 * must dominate the minimum label on the source address.
20781 	 */
20782 	dst_rhtp = NULL;
20783 	if (is_system_labeled()) {
20784 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20785 		if (dst_rhtp == NULL)
20786 			return (NULL);
20787 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20788 			TPC_RELE(dst_rhtp);
20789 			dst_rhtp = NULL;
20790 		}
20791 	}
20792 
20793 	/*
20794 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20795 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20796 	 * After selecting the right ipif, under ill_lock make sure ipif is
20797 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20798 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20799 	 * but not under a lock.
20800 	 */
20801 	rw_enter(&ill_g_lock, RW_READER);
20802 
20803 retry:
20804 	till = ill;
20805 	ipif_arr[0] = NULL;
20806 
20807 	if (till->ill_group != NULL)
20808 		till = till->ill_group->illgrp_ill;
20809 
20810 	/*
20811 	 * Choose one good source address from each ill across the group.
20812 	 * If possible choose a source address in the same subnet as
20813 	 * the destination address.
20814 	 *
20815 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20816 	 * This is okay because of the following.
20817 	 *
20818 	 *    If PHYI_FAILED is set and we still have non-deprecated
20819 	 *    addresses, it means the addresses have not yet been
20820 	 *    failed over to a different interface. We potentially
20821 	 *    select them to create IRE_CACHES, which will be later
20822 	 *    flushed when the addresses move over.
20823 	 *
20824 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20825 	 *    addresses, it means either the user has configured them
20826 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20827 	 *    been moved over. For the former, in.mpathd does a failover
20828 	 *    when the interface becomes INACTIVE and hence we should
20829 	 *    not find them. Once INACTIVE is set, we don't allow them
20830 	 *    to create logical interfaces anymore. For the latter, a
20831 	 *    flush will happen when INACTIVE is cleared which will
20832 	 *    flush the IRE_CACHES.
20833 	 *
20834 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20835 	 *    over soon. We potentially select them to create IRE_CACHEs,
20836 	 *    which will be later flushed when the addresses move over.
20837 	 *
20838 	 * NOTE : As ipif_select_source is called to borrow source address
20839 	 * for an ipif that is part of a group, source address selection
20840 	 * will be re-done whenever the group changes i.e either an
20841 	 * insertion/deletion in the group.
20842 	 *
20843 	 * Fill ipif_arr[] with source addresses, using these rules:
20844 	 *
20845 	 *	1. At most one source address from a given ill ends up
20846 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20847 	 *	   associated with a given ill ends up in ipif_arr[].
20848 	 *
20849 	 *	2. If there is at least one non-deprecated ipif in the
20850 	 *	   IPMP group with a source address on the same subnet as
20851 	 *	   our destination, then fill ipif_arr[] only with
20852 	 *	   source addresses on the same subnet as our destination.
20853 	 *	   Note that because of (1), only the first
20854 	 *	   non-deprecated ipif found with a source address
20855 	 *	   matching the destination ends up in ipif_arr[].
20856 	 *
20857 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20858 	 *	   addresses not in the same subnet as our destination.
20859 	 *	   Again, because of (1), only the first off-subnet source
20860 	 *	   address will be chosen.
20861 	 *
20862 	 *	4. If there are no non-deprecated ipifs, then just use
20863 	 *	   the source address associated with the last deprecated
20864 	 *	   one we find that happens to be on the same subnet,
20865 	 *	   otherwise the first one not in the same subnet.
20866 	 */
20867 	specific_found = B_FALSE;
20868 	for (; till != NULL; till = till->ill_group_next) {
20869 		ipif_same_found = B_FALSE;
20870 		ipif_other_found = B_FALSE;
20871 		for (ipif = till->ill_ipif; ipif != NULL;
20872 		    ipif = ipif->ipif_next) {
20873 			if (!IPIF_CAN_LOOKUP(ipif))
20874 				continue;
20875 			/* Always skip NOLOCAL and ANYCAST interfaces */
20876 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20877 				continue;
20878 			if (!(ipif->ipif_flags & IPIF_UP) ||
20879 			    !ipif->ipif_addr_ready)
20880 				continue;
20881 			if (ipif->ipif_zoneid != zoneid &&
20882 			    ipif->ipif_zoneid != ALL_ZONES)
20883 				continue;
20884 			/*
20885 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20886 			 * but are not valid as source addresses.
20887 			 */
20888 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20889 				continue;
20890 
20891 			/*
20892 			 * Check compatibility of local address for
20893 			 * destination's default label if we're on a labeled
20894 			 * system.  Incompatible addresses can't be used at
20895 			 * all.
20896 			 */
20897 			if (dst_rhtp != NULL) {
20898 				boolean_t incompat;
20899 
20900 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20901 				    IPV4_VERSION, B_FALSE);
20902 				if (src_rhtp == NULL)
20903 					continue;
20904 				incompat =
20905 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20906 				    src_rhtp->tpc_tp.tp_doi !=
20907 				    dst_rhtp->tpc_tp.tp_doi ||
20908 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20909 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20910 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20911 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20912 				TPC_RELE(src_rhtp);
20913 				if (incompat)
20914 					continue;
20915 			}
20916 
20917 			/*
20918 			 * We prefer not to use all all-zones addresses, if we
20919 			 * can avoid it, as they pose problems with unlabeled
20920 			 * destinations.
20921 			 */
20922 			if (ipif->ipif_zoneid != ALL_ZONES) {
20923 				if (!specific_found &&
20924 				    (!same_subnet_only ||
20925 				    (ipif->ipif_net_mask & dst) ==
20926 				    ipif->ipif_subnet)) {
20927 					index = 0;
20928 					specific_found = B_TRUE;
20929 					ipif_other_found = B_FALSE;
20930 				}
20931 			} else {
20932 				if (specific_found)
20933 					continue;
20934 			}
20935 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
20936 				if (ipif_dep == NULL ||
20937 				    (ipif->ipif_net_mask & dst) ==
20938 				    ipif->ipif_subnet)
20939 					ipif_dep = ipif;
20940 				continue;
20941 			}
20942 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
20943 				/* found a source address in the same subnet */
20944 				if (!same_subnet_only) {
20945 					same_subnet_only = B_TRUE;
20946 					index = 0;
20947 				}
20948 				ipif_same_found = B_TRUE;
20949 			} else {
20950 				if (same_subnet_only || ipif_other_found)
20951 					continue;
20952 				ipif_other_found = B_TRUE;
20953 			}
20954 			ipif_arr[index++] = ipif;
20955 			if (index == MAX_IPIF_SELECT_SOURCE) {
20956 				wrapped = B_TRUE;
20957 				index = 0;
20958 			}
20959 			if (ipif_same_found)
20960 				break;
20961 		}
20962 	}
20963 
20964 	if (ipif_arr[0] == NULL) {
20965 		ipif = ipif_dep;
20966 	} else {
20967 		if (wrapped)
20968 			index = MAX_IPIF_SELECT_SOURCE;
20969 		ipif = ipif_arr[ipif_rand() % index];
20970 		ASSERT(ipif != NULL);
20971 	}
20972 
20973 	if (ipif != NULL) {
20974 		mutex_enter(&ipif->ipif_ill->ill_lock);
20975 		if (!IPIF_CAN_LOOKUP(ipif)) {
20976 			mutex_exit(&ipif->ipif_ill->ill_lock);
20977 			goto retry;
20978 		}
20979 		ipif_refhold_locked(ipif);
20980 		mutex_exit(&ipif->ipif_ill->ill_lock);
20981 	}
20982 
20983 	rw_exit(&ill_g_lock);
20984 	if (usill != NULL)
20985 		ill_refrele(usill);
20986 	if (dst_rhtp != NULL)
20987 		TPC_RELE(dst_rhtp);
20988 
20989 #ifdef DEBUG
20990 	if (ipif == NULL) {
20991 		char buf1[INET6_ADDRSTRLEN];
20992 
20993 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
20994 		    ill->ill_name,
20995 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
20996 	} else {
20997 		char buf1[INET6_ADDRSTRLEN];
20998 		char buf2[INET6_ADDRSTRLEN];
20999 
21000 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
21001 		    ipif->ipif_ill->ill_name,
21002 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
21003 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
21004 		    buf2, sizeof (buf2))));
21005 	}
21006 #endif /* DEBUG */
21007 	return (ipif);
21008 }
21009 
21010 
21011 /*
21012  * If old_ipif is not NULL, see if ipif was derived from old
21013  * ipif and if so, recreate the interface route by re-doing
21014  * source address selection. This happens when ipif_down ->
21015  * ipif_update_other_ipifs calls us.
21016  *
21017  * If old_ipif is NULL, just redo the source address selection
21018  * if needed. This happens when illgrp_insert or ipif_up_done
21019  * calls us.
21020  */
21021 static void
21022 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
21023 {
21024 	ire_t *ire;
21025 	ire_t *ipif_ire;
21026 	queue_t *stq;
21027 	ipif_t *nipif;
21028 	ill_t *ill;
21029 	boolean_t need_rele = B_FALSE;
21030 
21031 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
21032 	ASSERT(IAM_WRITER_IPIF(ipif));
21033 
21034 	ill = ipif->ipif_ill;
21035 	if (!(ipif->ipif_flags &
21036 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
21037 		/*
21038 		 * Can't possibly have borrowed the source
21039 		 * from old_ipif.
21040 		 */
21041 		return;
21042 	}
21043 
21044 	/*
21045 	 * Is there any work to be done? No work if the address
21046 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21047 	 * ipif_select_source() does not borrow addresses from
21048 	 * NOLOCAL and ANYCAST interfaces).
21049 	 */
21050 	if ((old_ipif != NULL) &&
21051 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21052 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21053 	    (old_ipif->ipif_flags &
21054 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21055 		return;
21056 	}
21057 
21058 	/*
21059 	 * Perform the same checks as when creating the
21060 	 * IRE_INTERFACE in ipif_up_done.
21061 	 */
21062 	if (!(ipif->ipif_flags & IPIF_UP))
21063 		return;
21064 
21065 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21066 	    (ipif->ipif_subnet == INADDR_ANY))
21067 		return;
21068 
21069 	ipif_ire = ipif_to_ire(ipif);
21070 	if (ipif_ire == NULL)
21071 		return;
21072 
21073 	/*
21074 	 * We know that ipif uses some other source for its
21075 	 * IRE_INTERFACE. Is it using the source of this
21076 	 * old_ipif?
21077 	 */
21078 	if (old_ipif != NULL &&
21079 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21080 		ire_refrele(ipif_ire);
21081 		return;
21082 	}
21083 	if (ip_debug > 2) {
21084 		/* ip1dbg */
21085 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21086 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21087 	}
21088 
21089 	stq = ipif_ire->ire_stq;
21090 
21091 	/*
21092 	 * Can't use our source address. Select a different
21093 	 * source address for the IRE_INTERFACE.
21094 	 */
21095 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21096 	if (nipif == NULL) {
21097 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21098 		nipif = ipif;
21099 	} else {
21100 		need_rele = B_TRUE;
21101 	}
21102 
21103 	ire = ire_create(
21104 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21105 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21106 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21107 	    NULL,				/* no gateway */
21108 	    NULL,
21109 	    &ipif->ipif_mtu,			/* max frag */
21110 	    NULL,				/* fast path header */
21111 	    NULL,				/* no recv from queue */
21112 	    stq,				/* send-to queue */
21113 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21114 	    ill->ill_resolver_mp,		/* xmit header */
21115 	    ipif,
21116 	    NULL,
21117 	    0,
21118 	    0,
21119 	    0,
21120 	    0,
21121 	    &ire_uinfo_null,
21122 	    NULL,
21123 	    NULL);
21124 
21125 	if (ire != NULL) {
21126 		ire_t *ret_ire;
21127 		int error;
21128 
21129 		/*
21130 		 * We don't need ipif_ire anymore. We need to delete
21131 		 * before we add so that ire_add does not detect
21132 		 * duplicates.
21133 		 */
21134 		ire_delete(ipif_ire);
21135 		ret_ire = ire;
21136 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21137 		ASSERT(error == 0);
21138 		ASSERT(ire == ret_ire);
21139 		/* Held in ire_add */
21140 		ire_refrele(ret_ire);
21141 	}
21142 	/*
21143 	 * Either we are falling through from above or could not
21144 	 * allocate a replacement.
21145 	 */
21146 	ire_refrele(ipif_ire);
21147 	if (need_rele)
21148 		ipif_refrele(nipif);
21149 }
21150 
21151 /*
21152  * This old_ipif is going away.
21153  *
21154  * Determine if any other ipif's is using our address as
21155  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21156  * IPIF_DEPRECATED).
21157  * Find the IRE_INTERFACE for such ipifs and recreate them
21158  * to use an different source address following the rules in
21159  * ipif_up_done.
21160  *
21161  * This function takes an illgrp as an argument so that illgrp_delete
21162  * can call this to update source address even after deleting the
21163  * old_ipif->ipif_ill from the ill group.
21164  */
21165 static void
21166 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21167 {
21168 	ipif_t *ipif;
21169 	ill_t *ill;
21170 	char	buf[INET6_ADDRSTRLEN];
21171 
21172 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21173 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21174 
21175 	ill = old_ipif->ipif_ill;
21176 
21177 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21178 	    ill->ill_name,
21179 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21180 	    buf, sizeof (buf))));
21181 	/*
21182 	 * If this part of a group, look at all ills as ipif_select_source
21183 	 * borrows source address across all the ills in the group.
21184 	 */
21185 	if (illgrp != NULL)
21186 		ill = illgrp->illgrp_ill;
21187 
21188 	for (; ill != NULL; ill = ill->ill_group_next) {
21189 		for (ipif = ill->ill_ipif; ipif != NULL;
21190 		    ipif = ipif->ipif_next) {
21191 
21192 			if (ipif == old_ipif)
21193 				continue;
21194 
21195 			ipif_recreate_interface_routes(old_ipif, ipif);
21196 		}
21197 	}
21198 }
21199 
21200 /* ARGSUSED */
21201 int
21202 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21203 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21204 {
21205 	/*
21206 	 * ill_phyint_reinit merged the v4 and v6 into a single
21207 	 * ipsq. Could also have become part of a ipmp group in the
21208 	 * process, and we might not have been able to complete the
21209 	 * operation in ipif_set_values, if we could not become
21210 	 * exclusive.  If so restart it here.
21211 	 */
21212 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21213 }
21214 
21215 
21216 /* ARGSUSED */
21217 int
21218 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21219     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21220 {
21221 	queue_t		*q1 = q;
21222 	char 		*cp;
21223 	char		interf_name[LIFNAMSIZ];
21224 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21225 
21226 	if (!q->q_next) {
21227 		ip1dbg((
21228 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21229 		return (EINVAL);
21230 	}
21231 
21232 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21233 		return (EALREADY);
21234 
21235 	do {
21236 		q1 = q1->q_next;
21237 	} while (q1->q_next);
21238 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21239 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21240 
21241 	/*
21242 	 * Here we are not going to delay the ioack until after
21243 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21244 	 * original ioctl message before sending the requests.
21245 	 */
21246 	return (ipif_set_values(q, mp, interf_name, &ppa));
21247 }
21248 
21249 /* ARGSUSED */
21250 int
21251 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21252     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21253 {
21254 	return (ENXIO);
21255 }
21256 
21257 /*
21258  * Net and subnet broadcast ire's are now specific to the particular
21259  * physical interface (ill) and not to any one locigal interface (ipif).
21260  * However, if a particular logical interface is being taken down, it's
21261  * associated ire's will be taken down as well.  Hence, when we go to
21262  * take down or change the local address, broadcast address or netmask
21263  * of a specific logical interface, we must check to make sure that we
21264  * have valid net and subnet broadcast ire's for the other logical
21265  * interfaces which may have been shared with the logical interface
21266  * being brought down or changed.
21267  *
21268  * There is one set of 0.0.0.0 and 255.255.255.255 per ill. Usually it
21269  * is tied to the first interface coming UP. If that ipif is going down,
21270  * we need to recreate them on the next valid ipif.
21271  *
21272  * Note: assume that the ipif passed in is still up so that it's IRE
21273  * entries are still valid.
21274  */
21275 static void
21276 ipif_check_bcast_ires(ipif_t *test_ipif)
21277 {
21278 	ipif_t	*ipif;
21279 	ire_t	*test_subnet_ire, *test_net_ire;
21280 	ire_t	*test_allzero_ire, *test_allone_ire;
21281 	ire_t	*ire_array[12];
21282 	ire_t	**irep = &ire_array[0];
21283 	ire_t	**irep1;
21284 
21285 	ipaddr_t net_addr, subnet_addr, net_mask, subnet_mask;
21286 	ipaddr_t test_net_addr, test_subnet_addr;
21287 	ipaddr_t test_net_mask, test_subnet_mask;
21288 	boolean_t need_net_bcast_ire = B_FALSE;
21289 	boolean_t need_subnet_bcast_ire = B_FALSE;
21290 	boolean_t allzero_bcast_ire_created = B_FALSE;
21291 	boolean_t allone_bcast_ire_created = B_FALSE;
21292 	boolean_t net_bcast_ire_created = B_FALSE;
21293 	boolean_t subnet_bcast_ire_created = B_FALSE;
21294 
21295 	ipif_t  *backup_ipif_net = (ipif_t *)NULL;
21296 	ipif_t  *backup_ipif_subnet = (ipif_t *)NULL;
21297 	ipif_t  *backup_ipif_allzeros = (ipif_t *)NULL;
21298 	ipif_t  *backup_ipif_allones = (ipif_t *)NULL;
21299 	uint64_t check_flags = IPIF_DEPRECATED | IPIF_NOLOCAL | IPIF_ANYCAST;
21300 
21301 	ASSERT(!test_ipif->ipif_isv6);
21302 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21303 
21304 	/*
21305 	 * No broadcast IREs for the LOOPBACK interface
21306 	 * or others such as point to point and IPIF_NOXMIT.
21307 	 */
21308 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21309 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21310 		return;
21311 
21312 	test_allzero_ire = ire_ctable_lookup(0, 0, IRE_BROADCAST,
21313 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
21314 
21315 	test_allone_ire = ire_ctable_lookup(INADDR_BROADCAST, 0, IRE_BROADCAST,
21316 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
21317 
21318 	test_net_mask = ip_net_mask(test_ipif->ipif_subnet);
21319 	test_subnet_mask = test_ipif->ipif_net_mask;
21320 
21321 	/*
21322 	 * If no net mask set, assume the default based on net class.
21323 	 */
21324 	if (test_subnet_mask == 0)
21325 		test_subnet_mask = test_net_mask;
21326 
21327 	/*
21328 	 * Check if there is a network broadcast ire associated with this ipif
21329 	 */
21330 	test_net_addr = test_net_mask  & test_ipif->ipif_subnet;
21331 	test_net_ire = ire_ctable_lookup(test_net_addr, 0, IRE_BROADCAST,
21332 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
21333 
21334 	/*
21335 	 * Check if there is a subnet broadcast IRE associated with this ipif
21336 	 */
21337 	test_subnet_addr = test_subnet_mask  & test_ipif->ipif_subnet;
21338 	test_subnet_ire = ire_ctable_lookup(test_subnet_addr, 0, IRE_BROADCAST,
21339 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
21340 
21341 	/*
21342 	 * No broadcast ire's associated with this ipif.
21343 	 */
21344 	if ((test_subnet_ire == NULL) && (test_net_ire == NULL) &&
21345 	    (test_allzero_ire == NULL) && (test_allone_ire == NULL)) {
21346 		return;
21347 	}
21348 
21349 	/*
21350 	 * We have established which bcast ires have to be replaced.
21351 	 * Next we try to locate ipifs that match there ires.
21352 	 * The rules are simple: If we find an ipif that matches on the subnet
21353 	 * address it will also match on the net address, the allzeros and
21354 	 * allones address. Any ipif that matches only on the net address will
21355 	 * also match the allzeros and allones addresses.
21356 	 * The other criterion is the ipif_flags. We look for non-deprecated
21357 	 * (and non-anycast and non-nolocal) ipifs as the best choice.
21358 	 * ipifs with check_flags matching (deprecated, etc) are used only
21359 	 * if good ipifs are not available. While looping, we save existing
21360 	 * deprecated ipifs as backup_ipif.
21361 	 * We loop through all the ipifs for this ill looking for ipifs
21362 	 * whose broadcast addr match the ipif passed in, but do not have
21363 	 * their own broadcast ires. For creating 0.0.0.0 and
21364 	 * 255.255.255.255 we just need an ipif on this ill to create.
21365 	 */
21366 	for (ipif = test_ipif->ipif_ill->ill_ipif; ipif != NULL;
21367 	    ipif = ipif->ipif_next) {
21368 
21369 		ASSERT(!ipif->ipif_isv6);
21370 		/*
21371 		 * Already checked the ipif passed in.
21372 		 */
21373 		if (ipif == test_ipif) {
21374 			continue;
21375 		}
21376 
21377 		/*
21378 		 * We only need to recreate broadcast ires if another ipif in
21379 		 * the same zone uses them. The new ires must be created in the
21380 		 * same zone.
21381 		 */
21382 		if (ipif->ipif_zoneid != test_ipif->ipif_zoneid) {
21383 			continue;
21384 		}
21385 
21386 		/*
21387 		 * Only interested in logical interfaces with valid local
21388 		 * addresses or with the ability to broadcast.
21389 		 */
21390 		if ((ipif->ipif_subnet == 0) ||
21391 		    !(ipif->ipif_flags & IPIF_BROADCAST) ||
21392 		    (ipif->ipif_flags & IPIF_NOXMIT) ||
21393 		    !(ipif->ipif_flags & IPIF_UP)) {
21394 			continue;
21395 		}
21396 		/*
21397 		 * Check if there is a net broadcast ire for this
21398 		 * net address.  If it turns out that the ipif we are
21399 		 * about to take down owns this ire, we must make a
21400 		 * new one because it is potentially going away.
21401 		 */
21402 		if (test_net_ire && (!net_bcast_ire_created)) {
21403 			net_mask = ip_net_mask(ipif->ipif_subnet);
21404 			net_addr = net_mask & ipif->ipif_subnet;
21405 			if (net_addr == test_net_addr) {
21406 				need_net_bcast_ire = B_TRUE;
21407 				/*
21408 				 * Use DEPRECATED ipif only if no good
21409 				 * ires are available. subnet_addr is
21410 				 * a better match than net_addr.
21411 				 */
21412 				if ((ipif->ipif_flags & check_flags) &&
21413 				    (backup_ipif_net == NULL)) {
21414 					backup_ipif_net = ipif;
21415 				}
21416 			}
21417 		}
21418 		/*
21419 		 * Check if there is a subnet broadcast ire for this
21420 		 * net address.  If it turns out that the ipif we are
21421 		 * about to take down owns this ire, we must make a
21422 		 * new one because it is potentially going away.
21423 		 */
21424 		if (test_subnet_ire && (!subnet_bcast_ire_created)) {
21425 			subnet_mask = ipif->ipif_net_mask;
21426 			subnet_addr = ipif->ipif_subnet;
21427 			if (subnet_addr == test_subnet_addr) {
21428 				need_subnet_bcast_ire = B_TRUE;
21429 				if ((ipif->ipif_flags & check_flags) &&
21430 				    (backup_ipif_subnet == NULL)) {
21431 					backup_ipif_subnet = ipif;
21432 				}
21433 			}
21434 		}
21435 
21436 
21437 		/* Short circuit here if this ipif is deprecated */
21438 		if (ipif->ipif_flags & check_flags) {
21439 			if ((test_allzero_ire != NULL) &&
21440 			    (!allzero_bcast_ire_created) &&
21441 			    (backup_ipif_allzeros == NULL)) {
21442 				backup_ipif_allzeros = ipif;
21443 			}
21444 			if ((test_allone_ire != NULL) &&
21445 			    (!allone_bcast_ire_created) &&
21446 			    (backup_ipif_allones == NULL)) {
21447 				backup_ipif_allones = ipif;
21448 			}
21449 			continue;
21450 		}
21451 
21452 		/*
21453 		 * Found an ipif which has the same broadcast ire as the
21454 		 * ipif passed in and the ipif passed in "owns" the ire.
21455 		 * Create new broadcast ire's for this broadcast addr.
21456 		 */
21457 		if (need_net_bcast_ire && !net_bcast_ire_created) {
21458 			irep = ire_create_bcast(ipif, net_addr, irep);
21459 			irep = ire_create_bcast(ipif,
21460 			    ~net_mask | net_addr, irep);
21461 			net_bcast_ire_created = B_TRUE;
21462 		}
21463 		if (need_subnet_bcast_ire && !subnet_bcast_ire_created) {
21464 			irep = ire_create_bcast(ipif, subnet_addr, irep);
21465 			irep = ire_create_bcast(ipif,
21466 			    ~subnet_mask | subnet_addr, irep);
21467 			subnet_bcast_ire_created = B_TRUE;
21468 		}
21469 		if (test_allzero_ire != NULL && !allzero_bcast_ire_created) {
21470 			irep = ire_create_bcast(ipif, 0, irep);
21471 			allzero_bcast_ire_created = B_TRUE;
21472 		}
21473 		if (test_allone_ire != NULL && !allone_bcast_ire_created) {
21474 			irep = ire_create_bcast(ipif, INADDR_BROADCAST, irep);
21475 			allone_bcast_ire_created = B_TRUE;
21476 		}
21477 		/*
21478 		 * Once we have created all the appropriate ires, we
21479 		 * just break out of this loop to add what we have created.
21480 		 * This has been indented similar to ire_match_args for
21481 		 * readability.
21482 		 */
21483 		if (((test_net_ire == NULL) ||
21484 			(net_bcast_ire_created)) &&
21485 		    ((test_subnet_ire == NULL) ||
21486 			(subnet_bcast_ire_created)) &&
21487 		    ((test_allzero_ire == NULL) ||
21488 			(allzero_bcast_ire_created)) &&
21489 		    ((test_allone_ire == NULL) ||
21490 			(allone_bcast_ire_created))) {
21491 			break;
21492 		}
21493 	}
21494 
21495 	/*
21496 	 * Create bcast ires on deprecated ipifs if no non-deprecated ipifs
21497 	 * exist. 6 pairs of bcast ires are needed.
21498 	 * Note - the old ires are deleted in ipif_down.
21499 	 */
21500 	if (need_net_bcast_ire && !net_bcast_ire_created && backup_ipif_net) {
21501 		ipif = backup_ipif_net;
21502 		irep = ire_create_bcast(ipif, net_addr, irep);
21503 		irep = ire_create_bcast(ipif, ~net_mask | net_addr, irep);
21504 		net_bcast_ire_created = B_TRUE;
21505 	}
21506 	if (need_subnet_bcast_ire && !subnet_bcast_ire_created &&
21507 	    backup_ipif_subnet) {
21508 		ipif = backup_ipif_subnet;
21509 		irep = ire_create_bcast(ipif, subnet_addr, irep);
21510 		irep = ire_create_bcast(ipif,
21511 		    ~subnet_mask | subnet_addr, irep);
21512 		subnet_bcast_ire_created = B_TRUE;
21513 	}
21514 	if (test_allzero_ire != NULL && !allzero_bcast_ire_created &&
21515 	    backup_ipif_allzeros) {
21516 		irep = ire_create_bcast(backup_ipif_allzeros, 0, irep);
21517 		allzero_bcast_ire_created = B_TRUE;
21518 	}
21519 	if (test_allone_ire != NULL && !allone_bcast_ire_created &&
21520 	    backup_ipif_allones) {
21521 		irep = ire_create_bcast(backup_ipif_allones,
21522 		    INADDR_BROADCAST, irep);
21523 		allone_bcast_ire_created = B_TRUE;
21524 	}
21525 
21526 	/*
21527 	 * If we can't create all of them, don't add any of them.
21528 	 * Code in ip_wput_ire and ire_to_ill assumes that we
21529 	 * always have a non-loopback copy and loopback copy
21530 	 * for a given address.
21531 	 */
21532 	for (irep1 = irep; irep1 > ire_array; ) {
21533 		irep1--;
21534 		if (*irep1 == NULL) {
21535 			ip0dbg(("ipif_check_bcast_ires: can't create "
21536 			    "IRE_BROADCAST, memory allocation failure\n"));
21537 			while (irep > ire_array) {
21538 				irep--;
21539 				if (*irep != NULL)
21540 					ire_delete(*irep);
21541 			}
21542 			goto bad;
21543 		}
21544 	}
21545 	for (irep1 = irep; irep1 > ire_array; ) {
21546 		int error;
21547 
21548 		irep1--;
21549 		error = ire_add(irep1, NULL, NULL, NULL, B_FALSE);
21550 		if (error == 0) {
21551 			ire_refrele(*irep1);		/* Held in ire_add */
21552 		}
21553 	}
21554 bad:
21555 	if (test_allzero_ire != NULL)
21556 		ire_refrele(test_allzero_ire);
21557 	if (test_allone_ire != NULL)
21558 		ire_refrele(test_allone_ire);
21559 	if (test_net_ire != NULL)
21560 		ire_refrele(test_net_ire);
21561 	if (test_subnet_ire != NULL)
21562 		ire_refrele(test_subnet_ire);
21563 }
21564 
21565 /*
21566  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21567  * from lifr_flags and the name from lifr_name.
21568  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21569  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21570  * Returns EINPROGRESS when mp has been consumed by queueing it on
21571  * ill_pending_mp and the ioctl will complete in ip_rput.
21572  */
21573 /* ARGSUSED */
21574 int
21575 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21576     ip_ioctl_cmd_t *ipip, void *if_req)
21577 {
21578 	int	err;
21579 	ill_t	*ill;
21580 	struct lifreq *lifr = (struct lifreq *)if_req;
21581 
21582 	ASSERT(ipif != NULL);
21583 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21584 	ASSERT(q->q_next != NULL);
21585 
21586 	ill = (ill_t *)q->q_ptr;
21587 	/*
21588 	 * If we are not writer on 'q' then this interface exists already
21589 	 * and previous lookups (ipif_extract_lifreq_cmn) found this ipif.
21590 	 * So return EALREADY
21591 	 */
21592 	if (ill != ipif->ipif_ill)
21593 		return (EALREADY);
21594 
21595 	if (ill->ill_name[0] != '\0')
21596 		return (EALREADY);
21597 
21598 	/*
21599 	 * Set all the flags. Allows all kinds of override. Provide some
21600 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21601 	 * unless there is either multicast/broadcast support in the driver
21602 	 * or it is a pt-pt link.
21603 	 */
21604 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21605 		/* Meaningless to IP thus don't allow them to be set. */
21606 		ip1dbg(("ip_setname: EINVAL 1\n"));
21607 		return (EINVAL);
21608 	}
21609 	/*
21610 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21611 	 * ill_bcast_addr_length info.
21612 	 */
21613 	if (!ill->ill_needs_attach &&
21614 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21615 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21616 	    ill->ill_bcast_addr_length == 0)) {
21617 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21618 		ip1dbg(("ip_setname: EINVAL 2\n"));
21619 		return (EINVAL);
21620 	}
21621 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21622 	    ((lifr->lifr_flags & IFF_IPV6) ||
21623 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21624 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21625 		ip1dbg(("ip_setname: EINVAL 3\n"));
21626 		return (EINVAL);
21627 	}
21628 	if (lifr->lifr_flags & IFF_UP) {
21629 		/* Can only be set with SIOCSLIFFLAGS */
21630 		ip1dbg(("ip_setname: EINVAL 4\n"));
21631 		return (EINVAL);
21632 	}
21633 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21634 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21635 		ip1dbg(("ip_setname: EINVAL 5\n"));
21636 		return (EINVAL);
21637 	}
21638 	/*
21639 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21640 	 */
21641 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21642 	    !(lifr->lifr_flags & IFF_IPV6) &&
21643 	    !(ipif->ipif_isv6)) {
21644 		ip1dbg(("ip_setname: EINVAL 6\n"));
21645 		return (EINVAL);
21646 	}
21647 
21648 	/*
21649 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21650 	 * we have all the flags here. So, we assign rather than we OR.
21651 	 * We can't OR the flags here because we don't want to set
21652 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21653 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21654 	 * on lifr_flags value here.
21655 	 */
21656 	/*
21657 	 * This ill has not been inserted into the global list.
21658 	 * So we are still single threaded and don't need any lock
21659 	 */
21660 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS &
21661 	    ~IFF_DUPLICATE;
21662 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21663 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21664 
21665 	/* We started off as V4. */
21666 	if (ill->ill_flags & ILLF_IPV6) {
21667 		ill->ill_phyint->phyint_illv6 = ill;
21668 		ill->ill_phyint->phyint_illv4 = NULL;
21669 	}
21670 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
21671 	return (err);
21672 }
21673 
21674 /* ARGSUSED */
21675 int
21676 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21677     ip_ioctl_cmd_t *ipip, void *if_req)
21678 {
21679 	/*
21680 	 * ill_phyint_reinit merged the v4 and v6 into a single
21681 	 * ipsq. Could also have become part of a ipmp group in the
21682 	 * process, and we might not have been able to complete the
21683 	 * slifname in ipif_set_values, if we could not become
21684 	 * exclusive.  If so restart it here
21685 	 */
21686 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21687 }
21688 
21689 /*
21690  * Return a pointer to the ipif which matches the index, IP version type and
21691  * zoneid.
21692  */
21693 ipif_t *
21694 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21695     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
21696 {
21697 	ill_t	*ill;
21698 	ipsq_t  *ipsq;
21699 	phyint_t *phyi;
21700 	ipif_t	*ipif;
21701 
21702 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21703 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21704 
21705 	if (err != NULL)
21706 		*err = 0;
21707 
21708 	/*
21709 	 * Indexes are stored in the phyint - a common structure
21710 	 * to both IPv4 and IPv6.
21711 	 */
21712 
21713 	rw_enter(&ill_g_lock, RW_READER);
21714 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
21715 	    (void *) &index, NULL);
21716 	if (phyi != NULL) {
21717 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
21718 		if (ill == NULL) {
21719 			rw_exit(&ill_g_lock);
21720 			if (err != NULL)
21721 				*err = ENXIO;
21722 			return (NULL);
21723 		}
21724 		GRAB_CONN_LOCK(q);
21725 		mutex_enter(&ill->ill_lock);
21726 		if (ILL_CAN_LOOKUP(ill)) {
21727 			for (ipif = ill->ill_ipif; ipif != NULL;
21728 			    ipif = ipif->ipif_next) {
21729 				if (IPIF_CAN_LOOKUP(ipif) &&
21730 				    (zoneid == ALL_ZONES ||
21731 				    zoneid == ipif->ipif_zoneid ||
21732 				    ipif->ipif_zoneid == ALL_ZONES)) {
21733 					ipif_refhold_locked(ipif);
21734 					mutex_exit(&ill->ill_lock);
21735 					RELEASE_CONN_LOCK(q);
21736 					rw_exit(&ill_g_lock);
21737 					return (ipif);
21738 				}
21739 			}
21740 		} else if (ILL_CAN_WAIT(ill, q)) {
21741 			ipsq = ill->ill_phyint->phyint_ipsq;
21742 			mutex_enter(&ipsq->ipsq_lock);
21743 			rw_exit(&ill_g_lock);
21744 			mutex_exit(&ill->ill_lock);
21745 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
21746 			mutex_exit(&ipsq->ipsq_lock);
21747 			RELEASE_CONN_LOCK(q);
21748 			*err = EINPROGRESS;
21749 			return (NULL);
21750 		}
21751 		mutex_exit(&ill->ill_lock);
21752 		RELEASE_CONN_LOCK(q);
21753 	}
21754 	rw_exit(&ill_g_lock);
21755 	if (err != NULL)
21756 		*err = ENXIO;
21757 	return (NULL);
21758 }
21759 
21760 typedef struct conn_change_s {
21761 	uint_t cc_old_ifindex;
21762 	uint_t cc_new_ifindex;
21763 } conn_change_t;
21764 
21765 /*
21766  * ipcl_walk function for changing interface index.
21767  */
21768 static void
21769 conn_change_ifindex(conn_t *connp, caddr_t arg)
21770 {
21771 	conn_change_t *connc;
21772 	uint_t old_ifindex;
21773 	uint_t new_ifindex;
21774 	int i;
21775 	ilg_t *ilg;
21776 
21777 	connc = (conn_change_t *)arg;
21778 	old_ifindex = connc->cc_old_ifindex;
21779 	new_ifindex = connc->cc_new_ifindex;
21780 
21781 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21782 		connp->conn_orig_bound_ifindex = new_ifindex;
21783 
21784 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21785 		connp->conn_orig_multicast_ifindex = new_ifindex;
21786 
21787 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
21788 		connp->conn_orig_xmit_ifindex = new_ifindex;
21789 
21790 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21791 		ilg = &connp->conn_ilg[i];
21792 		if (ilg->ilg_orig_ifindex == old_ifindex)
21793 			ilg->ilg_orig_ifindex = new_ifindex;
21794 	}
21795 }
21796 
21797 /*
21798  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21799  * to new_index if it matches the old_index.
21800  *
21801  * Failovers typically happen within a group of ills. But somebody
21802  * can remove an ill from the group after a failover happened. If
21803  * we are setting the ifindex after this, we potentially need to
21804  * look at all the ills rather than just the ones in the group.
21805  * We cut down the work by looking at matching ill_net_types
21806  * and ill_types as we could not possibly grouped them together.
21807  */
21808 static void
21809 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21810 {
21811 	ill_t *ill;
21812 	ipif_t *ipif;
21813 	uint_t old_ifindex;
21814 	uint_t new_ifindex;
21815 	ilm_t *ilm;
21816 	ill_walk_context_t ctx;
21817 
21818 	old_ifindex = connc->cc_old_ifindex;
21819 	new_ifindex = connc->cc_new_ifindex;
21820 
21821 	rw_enter(&ill_g_lock, RW_READER);
21822 	ill = ILL_START_WALK_ALL(&ctx);
21823 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21824 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21825 			(ill_orig->ill_type != ill->ill_type)) {
21826 			continue;
21827 		}
21828 		for (ipif = ill->ill_ipif; ipif != NULL;
21829 				ipif = ipif->ipif_next) {
21830 			if (ipif->ipif_orig_ifindex == old_ifindex)
21831 				ipif->ipif_orig_ifindex = new_ifindex;
21832 		}
21833 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21834 			if (ilm->ilm_orig_ifindex == old_ifindex)
21835 				ilm->ilm_orig_ifindex = new_ifindex;
21836 		}
21837 	}
21838 	rw_exit(&ill_g_lock);
21839 }
21840 
21841 /*
21842  * We first need to ensure that the new index is unique, and
21843  * then carry the change across both v4 and v6 ill representation
21844  * of the physical interface.
21845  */
21846 /* ARGSUSED */
21847 int
21848 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21849     ip_ioctl_cmd_t *ipip, void *ifreq)
21850 {
21851 	ill_t		*ill;
21852 	ill_t		*ill_other;
21853 	phyint_t	*phyi;
21854 	int		old_index;
21855 	conn_change_t	connc;
21856 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21857 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21858 	uint_t	index;
21859 	ill_t	*ill_v4;
21860 	ill_t	*ill_v6;
21861 
21862 	if (ipip->ipi_cmd_type == IF_CMD)
21863 		index = ifr->ifr_index;
21864 	else
21865 		index = lifr->lifr_index;
21866 
21867 	/*
21868 	 * Only allow on physical interface. Also, index zero is illegal.
21869 	 *
21870 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21871 	 *
21872 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21873 	 *    implies a possible failback might have to happen. As failback
21874 	 *    depends on the old index, we should fail setting the index.
21875 	 *
21876 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21877 	 *    any addresses or multicast memberships are failed over to
21878 	 *    a non-STANDBY interface. As failback depends on the old
21879 	 *    index, we should fail setting the index for this case also.
21880 	 *
21881 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21882 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21883 	 */
21884 	ill = ipif->ipif_ill;
21885 	phyi = ill->ill_phyint;
21886 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21887 	    ipif->ipif_id != 0 || index == 0) {
21888 		return (EINVAL);
21889 	}
21890 	old_index = phyi->phyint_ifindex;
21891 
21892 	/* If the index is not changing, no work to do */
21893 	if (old_index == index)
21894 		return (0);
21895 
21896 	/*
21897 	 * Use ill_lookup_on_ifindex to determine if the
21898 	 * new index is unused and if so allow the change.
21899 	 */
21900 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL);
21901 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL);
21902 	if (ill_v6 != NULL || ill_v4 != NULL) {
21903 		if (ill_v4 != NULL)
21904 			ill_refrele(ill_v4);
21905 		if (ill_v6 != NULL)
21906 			ill_refrele(ill_v6);
21907 		return (EBUSY);
21908 	}
21909 
21910 	/*
21911 	 * The new index is unused. Set it in the phyint.
21912 	 * Locate the other ill so that we can send a routing
21913 	 * sockets message.
21914 	 */
21915 	if (ill->ill_isv6) {
21916 		ill_other = phyi->phyint_illv4;
21917 	} else {
21918 		ill_other = phyi->phyint_illv6;
21919 	}
21920 
21921 	phyi->phyint_ifindex = index;
21922 
21923 	connc.cc_old_ifindex = old_index;
21924 	connc.cc_new_ifindex = index;
21925 	ip_change_ifindex(ill, &connc);
21926 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc);
21927 
21928 	/* Send the routing sockets message */
21929 	ip_rts_ifmsg(ipif);
21930 	if (ill_other != NULL)
21931 		ip_rts_ifmsg(ill_other->ill_ipif);
21932 
21933 	return (0);
21934 }
21935 
21936 /* ARGSUSED */
21937 int
21938 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21939     ip_ioctl_cmd_t *ipip, void *ifreq)
21940 {
21941 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21942 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21943 
21944 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
21945 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21946 	/* Get the interface index */
21947 	if (ipip->ipi_cmd_type == IF_CMD) {
21948 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21949 	} else {
21950 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21951 	}
21952 	return (0);
21953 }
21954 
21955 /* ARGSUSED */
21956 int
21957 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21958     ip_ioctl_cmd_t *ipip, void *ifreq)
21959 {
21960 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21961 
21962 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
21963 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21964 	/* Get the interface zone */
21965 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21966 	lifr->lifr_zoneid = ipif->ipif_zoneid;
21967 	return (0);
21968 }
21969 
21970 /*
21971  * Set the zoneid of an interface.
21972  */
21973 /* ARGSUSED */
21974 int
21975 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21976     ip_ioctl_cmd_t *ipip, void *ifreq)
21977 {
21978 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21979 	int err = 0;
21980 	boolean_t need_up = B_FALSE;
21981 	zone_t *zptr;
21982 	zone_status_t status;
21983 	zoneid_t zoneid;
21984 
21985 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21986 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
21987 		if (!is_system_labeled())
21988 			return (ENOTSUP);
21989 		zoneid = GLOBAL_ZONEID;
21990 	}
21991 
21992 	/* cannot assign instance zero to a non-global zone */
21993 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
21994 		return (ENOTSUP);
21995 
21996 	/*
21997 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
21998 	 * the event of a race with the zone shutdown processing, since IP
21999 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
22000 	 * interface will be cleaned up even if the zone is shut down
22001 	 * immediately after the status check. If the interface can't be brought
22002 	 * down right away, and the zone is shut down before the restart
22003 	 * function is called, we resolve the possible races by rechecking the
22004 	 * zone status in the restart function.
22005 	 */
22006 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
22007 		return (EINVAL);
22008 	status = zone_status_get(zptr);
22009 	zone_rele(zptr);
22010 
22011 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
22012 		return (EINVAL);
22013 
22014 	if (ipif->ipif_flags & IPIF_UP) {
22015 		/*
22016 		 * If the interface is already marked up,
22017 		 * we call ipif_down which will take care
22018 		 * of ditching any IREs that have been set
22019 		 * up based on the old interface address.
22020 		 */
22021 		err = ipif_logical_down(ipif, q, mp);
22022 		if (err == EINPROGRESS)
22023 			return (err);
22024 		ipif_down_tail(ipif);
22025 		need_up = B_TRUE;
22026 	}
22027 
22028 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
22029 	return (err);
22030 }
22031 
22032 static int
22033 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
22034     queue_t *q, mblk_t *mp, boolean_t need_up)
22035 {
22036 	int	err = 0;
22037 
22038 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
22039 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22040 
22041 	/* Set the new zone id. */
22042 	ipif->ipif_zoneid = zoneid;
22043 
22044 	/* Update sctp list */
22045 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22046 
22047 	if (need_up) {
22048 		/*
22049 		 * Now bring the interface back up.  If this
22050 		 * is the only IPIF for the ILL, ipif_up
22051 		 * will have to re-bind to the device, so
22052 		 * we may get back EINPROGRESS, in which
22053 		 * case, this IOCTL will get completed in
22054 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22055 		 */
22056 		err = ipif_up(ipif, q, mp);
22057 	}
22058 	return (err);
22059 }
22060 
22061 /* ARGSUSED */
22062 int
22063 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22064     ip_ioctl_cmd_t *ipip, void *if_req)
22065 {
22066 	struct lifreq *lifr = (struct lifreq *)if_req;
22067 	zoneid_t zoneid;
22068 	zone_t *zptr;
22069 	zone_status_t status;
22070 
22071 	ASSERT(ipif->ipif_id != 0);
22072 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22073 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22074 		zoneid = GLOBAL_ZONEID;
22075 
22076 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22077 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22078 
22079 	/*
22080 	 * We recheck the zone status to resolve the following race condition:
22081 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22082 	 * 2) hme0:1 is up and can't be brought down right away;
22083 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22084 	 * 3) zone "myzone" is halted; the zone status switches to
22085 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22086 	 * the interfaces to remove - hme0:1 is not returned because it's not
22087 	 * yet in "myzone", so it won't be removed;
22088 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22089 	 * status check here, we would have hme0:1 in "myzone" after it's been
22090 	 * destroyed.
22091 	 * Note that if the status check fails, we need to bring the interface
22092 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22093 	 * ipif_up_done[_v6]().
22094 	 */
22095 	status = ZONE_IS_UNINITIALIZED;
22096 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22097 		status = zone_status_get(zptr);
22098 		zone_rele(zptr);
22099 	}
22100 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22101 		if (ipif->ipif_isv6) {
22102 			(void) ipif_up_done_v6(ipif);
22103 		} else {
22104 			(void) ipif_up_done(ipif);
22105 		}
22106 		return (EINVAL);
22107 	}
22108 
22109 	ipif_down_tail(ipif);
22110 
22111 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22112 	    B_TRUE));
22113 }
22114 
22115 /* ARGSUSED */
22116 int
22117 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22118 	ip_ioctl_cmd_t *ipip, void *ifreq)
22119 {
22120 	struct lifreq	*lifr = ifreq;
22121 
22122 	ASSERT(q->q_next == NULL);
22123 	ASSERT(CONN_Q(q));
22124 
22125 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22126 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22127 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22128 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22129 
22130 	return (0);
22131 }
22132 
22133 
22134 /* Find the previous ILL in this usesrc group */
22135 static ill_t *
22136 ill_prev_usesrc(ill_t *uill)
22137 {
22138 	ill_t *ill;
22139 
22140 	for (ill = uill->ill_usesrc_grp_next;
22141 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22142 	    ill = ill->ill_usesrc_grp_next)
22143 		/* do nothing */;
22144 	return (ill);
22145 }
22146 
22147 /*
22148  * Release all members of the usesrc group. This routine is called
22149  * from ill_delete when the interface being unplumbed is the
22150  * group head.
22151  */
22152 static void
22153 ill_disband_usesrc_group(ill_t *uill)
22154 {
22155 	ill_t *next_ill, *tmp_ill;
22156 	ASSERT(RW_WRITE_HELD(&ill_g_usesrc_lock));
22157 	next_ill = uill->ill_usesrc_grp_next;
22158 
22159 	do {
22160 		ASSERT(next_ill != NULL);
22161 		tmp_ill = next_ill->ill_usesrc_grp_next;
22162 		ASSERT(tmp_ill != NULL);
22163 		next_ill->ill_usesrc_grp_next = NULL;
22164 		next_ill->ill_usesrc_ifindex = 0;
22165 		next_ill = tmp_ill;
22166 	} while (next_ill->ill_usesrc_ifindex != 0);
22167 	uill->ill_usesrc_grp_next = NULL;
22168 }
22169 
22170 /*
22171  * Remove the client usesrc ILL from the list and relink to a new list
22172  */
22173 int
22174 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22175 {
22176 	ill_t *ill, *tmp_ill;
22177 
22178 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22179 	    (uill != NULL) && RW_WRITE_HELD(&ill_g_usesrc_lock));
22180 
22181 	/*
22182 	 * Check if the usesrc client ILL passed in is not already
22183 	 * in use as a usesrc ILL i.e one whose source address is
22184 	 * in use OR a usesrc ILL is not already in use as a usesrc
22185 	 * client ILL
22186 	 */
22187 	if ((ucill->ill_usesrc_ifindex == 0) ||
22188 	    (uill->ill_usesrc_ifindex != 0)) {
22189 		return (-1);
22190 	}
22191 
22192 	ill = ill_prev_usesrc(ucill);
22193 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22194 
22195 	/* Remove from the current list */
22196 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22197 		/* Only two elements in the list */
22198 		ASSERT(ill->ill_usesrc_ifindex == 0);
22199 		ill->ill_usesrc_grp_next = NULL;
22200 	} else {
22201 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22202 	}
22203 
22204 	if (ifindex == 0) {
22205 		ucill->ill_usesrc_ifindex = 0;
22206 		ucill->ill_usesrc_grp_next = NULL;
22207 		return (0);
22208 	}
22209 
22210 	ucill->ill_usesrc_ifindex = ifindex;
22211 	tmp_ill = uill->ill_usesrc_grp_next;
22212 	uill->ill_usesrc_grp_next = ucill;
22213 	ucill->ill_usesrc_grp_next =
22214 	    (tmp_ill != NULL) ? tmp_ill : uill;
22215 	return (0);
22216 }
22217 
22218 /*
22219  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22220  * ip.c for locking details.
22221  */
22222 /* ARGSUSED */
22223 int
22224 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22225     ip_ioctl_cmd_t *ipip, void *ifreq)
22226 {
22227 	struct lifreq *lifr = (struct lifreq *)ifreq;
22228 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22229 	    ill_flag_changed = B_FALSE;
22230 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22231 	int err = 0, ret;
22232 	uint_t ifindex;
22233 	phyint_t *us_phyint, *us_cli_phyint;
22234 	ipsq_t *ipsq = NULL;
22235 
22236 	ASSERT(IAM_WRITER_IPIF(ipif));
22237 	ASSERT(q->q_next == NULL);
22238 	ASSERT(CONN_Q(q));
22239 
22240 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22241 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22242 
22243 	ASSERT(us_cli_phyint != NULL);
22244 
22245 	/*
22246 	 * If the client ILL is being used for IPMP, abort.
22247 	 * Note, this can be done before ipsq_try_enter since we are already
22248 	 * exclusive on this ILL
22249 	 */
22250 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22251 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22252 		return (EINVAL);
22253 	}
22254 
22255 	ifindex = lifr->lifr_index;
22256 	if (ifindex == 0) {
22257 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22258 			/* non usesrc group interface, nothing to reset */
22259 			return (0);
22260 		}
22261 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22262 		/* valid reset request */
22263 		reset_flg = B_TRUE;
22264 	}
22265 
22266 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22267 	    ip_process_ioctl, &err);
22268 
22269 	if (usesrc_ill == NULL) {
22270 		return (err);
22271 	}
22272 
22273 	/*
22274 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22275 	 * group nor can either of the interfaces be used for standy. So
22276 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22277 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22278 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22279 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22280 	 * the usesrc_cli_ill
22281 	 */
22282 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22283 	    NEW_OP, B_TRUE);
22284 	if (ipsq == NULL) {
22285 		err = EINPROGRESS;
22286 		/* Operation enqueued on the ipsq of the usesrc ILL */
22287 		goto done;
22288 	}
22289 
22290 	/* Check if the usesrc_ill is used for IPMP */
22291 	us_phyint = usesrc_ill->ill_phyint;
22292 	if ((us_phyint->phyint_groupname != NULL) ||
22293 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22294 		err = EINVAL;
22295 		goto done;
22296 	}
22297 
22298 	/*
22299 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22300 	 * already a client then return EINVAL
22301 	 */
22302 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22303 		err = EINVAL;
22304 		goto done;
22305 	}
22306 
22307 	/*
22308 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22309 	 * be then this is a duplicate operation.
22310 	 */
22311 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22312 		err = 0;
22313 		goto done;
22314 	}
22315 
22316 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22317 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22318 	    usesrc_ill->ill_isv6));
22319 
22320 	/*
22321 	 * The next step ensures that no new ires will be created referencing
22322 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22323 	 * we go through an ire walk deleting all ire caches that reference
22324 	 * the client ill. New ires referencing the client ill that are added
22325 	 * to the ire table before the ILL_CHANGING flag is set, will be
22326 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22327 	 * the client ill while the ILL_CHANGING flag is set will be failed
22328 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22329 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22330 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22331 	 * belong to the same usesrc group.
22332 	 */
22333 	mutex_enter(&usesrc_cli_ill->ill_lock);
22334 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22335 	mutex_exit(&usesrc_cli_ill->ill_lock);
22336 	ill_flag_changed = B_TRUE;
22337 
22338 	if (ipif->ipif_isv6)
22339 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22340 		    ALL_ZONES);
22341 	else
22342 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22343 		    ALL_ZONES);
22344 
22345 	/*
22346 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22347 	 * and the ill_usesrc_ifindex fields
22348 	 */
22349 	rw_enter(&ill_g_usesrc_lock, RW_WRITER);
22350 
22351 	if (reset_flg) {
22352 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22353 		if (ret != 0) {
22354 			err = EINVAL;
22355 		}
22356 		rw_exit(&ill_g_usesrc_lock);
22357 		goto done;
22358 	}
22359 
22360 	/*
22361 	 * Four possibilities to consider:
22362 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22363 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22364 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22365 	 * 4. Both are part of their respective usesrc groups
22366 	 */
22367 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22368 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22369 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22370 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22371 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22372 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22373 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22374 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22375 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22376 		/* Insert at head of list */
22377 		usesrc_cli_ill->ill_usesrc_grp_next =
22378 		    usesrc_ill->ill_usesrc_grp_next;
22379 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22380 	} else {
22381 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22382 		    ifindex);
22383 		if (ret != 0)
22384 			err = EINVAL;
22385 	}
22386 	rw_exit(&ill_g_usesrc_lock);
22387 
22388 done:
22389 	if (ill_flag_changed) {
22390 		mutex_enter(&usesrc_cli_ill->ill_lock);
22391 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22392 		mutex_exit(&usesrc_cli_ill->ill_lock);
22393 	}
22394 	if (ipsq != NULL)
22395 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
22396 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22397 	ill_refrele(usesrc_ill);
22398 	return (err);
22399 }
22400 
22401 /*
22402  * comparison function used by avl.
22403  */
22404 static int
22405 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22406 {
22407 
22408 	uint_t index;
22409 
22410 	ASSERT(phyip != NULL && index_ptr != NULL);
22411 
22412 	index = *((uint_t *)index_ptr);
22413 	/*
22414 	 * let the phyint with the lowest index be on top.
22415 	 */
22416 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22417 		return (1);
22418 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22419 		return (-1);
22420 	return (0);
22421 }
22422 
22423 /*
22424  * comparison function used by avl.
22425  */
22426 static int
22427 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22428 {
22429 	ill_t *ill;
22430 	int res = 0;
22431 
22432 	ASSERT(phyip != NULL && name_ptr != NULL);
22433 
22434 	if (((phyint_t *)phyip)->phyint_illv4)
22435 		ill = ((phyint_t *)phyip)->phyint_illv4;
22436 	else
22437 		ill = ((phyint_t *)phyip)->phyint_illv6;
22438 	ASSERT(ill != NULL);
22439 
22440 	res = strcmp(ill->ill_name, (char *)name_ptr);
22441 	if (res > 0)
22442 		return (1);
22443 	else if (res < 0)
22444 		return (-1);
22445 	return (0);
22446 }
22447 /*
22448  * This function is called from ill_delete when the ill is being
22449  * unplumbed. We remove the reference from the phyint and we also
22450  * free the phyint when there are no more references to it.
22451  */
22452 static void
22453 ill_phyint_free(ill_t *ill)
22454 {
22455 	phyint_t *phyi;
22456 	phyint_t *next_phyint;
22457 	ipsq_t *cur_ipsq;
22458 
22459 	ASSERT(ill->ill_phyint != NULL);
22460 
22461 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
22462 	phyi = ill->ill_phyint;
22463 	ill->ill_phyint = NULL;
22464 	/*
22465 	 * ill_init allocates a phyint always to store the copy
22466 	 * of flags relevant to phyint. At that point in time, we could
22467 	 * not assign the name and hence phyint_illv4/v6 could not be
22468 	 * initialized. Later in ipif_set_values, we assign the name to
22469 	 * the ill, at which point in time we assign phyint_illv4/v6.
22470 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22471 	 */
22472 	if (ill->ill_flags & ILLF_IPV6) {
22473 		phyi->phyint_illv6 = NULL;
22474 	} else {
22475 		phyi->phyint_illv4 = NULL;
22476 	}
22477 	/*
22478 	 * ipif_down removes it from the group when the last ipif goes
22479 	 * down.
22480 	 */
22481 	ASSERT(ill->ill_group == NULL);
22482 
22483 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22484 		return;
22485 
22486 	/*
22487 	 * Make sure this phyint was put in the list.
22488 	 */
22489 	if (phyi->phyint_ifindex > 0) {
22490 		avl_remove(&phyint_g_list.phyint_list_avl_by_index,
22491 		    phyi);
22492 		avl_remove(&phyint_g_list.phyint_list_avl_by_name,
22493 		    phyi);
22494 	}
22495 	/*
22496 	 * remove phyint from the ipsq list.
22497 	 */
22498 	cur_ipsq = phyi->phyint_ipsq;
22499 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22500 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22501 	} else {
22502 		next_phyint = cur_ipsq->ipsq_phyint_list;
22503 		while (next_phyint != NULL) {
22504 			if (next_phyint->phyint_ipsq_next == phyi) {
22505 				next_phyint->phyint_ipsq_next =
22506 					phyi->phyint_ipsq_next;
22507 				break;
22508 			}
22509 			next_phyint = next_phyint->phyint_ipsq_next;
22510 		}
22511 		ASSERT(next_phyint != NULL);
22512 	}
22513 	IPSQ_DEC_REF(cur_ipsq);
22514 
22515 	if (phyi->phyint_groupname_len != 0) {
22516 		ASSERT(phyi->phyint_groupname != NULL);
22517 		mi_free(phyi->phyint_groupname);
22518 	}
22519 	mi_free(phyi);
22520 }
22521 
22522 /*
22523  * Attach the ill to the phyint structure which can be shared by both
22524  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22525  * function is called from ipif_set_values and ill_lookup_on_name (for
22526  * loopback) where we know the name of the ill. We lookup the ill and if
22527  * there is one present already with the name use that phyint. Otherwise
22528  * reuse the one allocated by ill_init.
22529  */
22530 static void
22531 ill_phyint_reinit(ill_t *ill)
22532 {
22533 	boolean_t isv6 = ill->ill_isv6;
22534 	phyint_t *phyi_old;
22535 	phyint_t *phyi;
22536 	avl_index_t where = 0;
22537 	ill_t	*ill_other = NULL;
22538 	ipsq_t	*ipsq;
22539 
22540 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
22541 
22542 	phyi_old = ill->ill_phyint;
22543 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22544 	    phyi_old->phyint_illv6 == NULL));
22545 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22546 	    phyi_old->phyint_illv4 == NULL));
22547 	ASSERT(phyi_old->phyint_ifindex == 0);
22548 
22549 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_name,
22550 	    ill->ill_name, &where);
22551 
22552 	/*
22553 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22554 	 *    the global list of ills. So no other thread could have located
22555 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22556 	 * 2. Now locate the other protocol instance of this ill.
22557 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22558 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22559 	 *    of neither ill can change.
22560 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22561 	 *    other ill.
22562 	 * 5. Release all locks.
22563 	 */
22564 
22565 	/*
22566 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22567 	 * we are initializing IPv4.
22568 	 */
22569 	if (phyi != NULL) {
22570 		ill_other = (isv6) ? phyi->phyint_illv4 :
22571 		    phyi->phyint_illv6;
22572 		ASSERT(ill_other->ill_phyint != NULL);
22573 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22574 		    (!isv6 && ill_other->ill_isv6));
22575 		GRAB_ILL_LOCKS(ill, ill_other);
22576 		/*
22577 		 * We are potentially throwing away phyint_flags which
22578 		 * could be different from the one that we obtain from
22579 		 * ill_other->ill_phyint. But it is okay as we are assuming
22580 		 * that the state maintained within IP is correct.
22581 		 */
22582 		mutex_enter(&phyi->phyint_lock);
22583 		if (isv6) {
22584 			ASSERT(phyi->phyint_illv6 == NULL);
22585 			phyi->phyint_illv6 = ill;
22586 		} else {
22587 			ASSERT(phyi->phyint_illv4 == NULL);
22588 			phyi->phyint_illv4 = ill;
22589 		}
22590 		/*
22591 		 * This is a new ill, currently undergoing SLIFNAME
22592 		 * So we could not have joined an IPMP group until now.
22593 		 */
22594 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22595 		    phyi_old->phyint_groupname == NULL);
22596 
22597 		/*
22598 		 * This phyi_old is going away. Decref ipsq_refs and
22599 		 * assert it is zero. The ipsq itself will be freed in
22600 		 * ipsq_exit
22601 		 */
22602 		ipsq = phyi_old->phyint_ipsq;
22603 		IPSQ_DEC_REF(ipsq);
22604 		ASSERT(ipsq->ipsq_refs == 0);
22605 		/* Get the singleton phyint out of the ipsq list */
22606 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22607 		ipsq->ipsq_phyint_list = NULL;
22608 		phyi_old->phyint_illv4 = NULL;
22609 		phyi_old->phyint_illv6 = NULL;
22610 		mi_free(phyi_old);
22611 	} else {
22612 		mutex_enter(&ill->ill_lock);
22613 		/*
22614 		 * We don't need to acquire any lock, since
22615 		 * the ill is not yet visible globally  and we
22616 		 * have not yet released the ill_g_lock.
22617 		 */
22618 		phyi = phyi_old;
22619 		mutex_enter(&phyi->phyint_lock);
22620 		/* XXX We need a recovery strategy here. */
22621 		if (!phyint_assign_ifindex(phyi))
22622 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22623 
22624 		avl_insert(&phyint_g_list.phyint_list_avl_by_name,
22625 		    (void *)phyi, where);
22626 
22627 		(void) avl_find(&phyint_g_list.phyint_list_avl_by_index,
22628 		    &phyi->phyint_ifindex, &where);
22629 		avl_insert(&phyint_g_list.phyint_list_avl_by_index,
22630 		    (void *)phyi, where);
22631 	}
22632 
22633 	/*
22634 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22635 	 * pending mp is not affected because that is per ill basis.
22636 	 */
22637 	ill->ill_phyint = phyi;
22638 
22639 	/*
22640 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22641 	 * We do this here as when the first ipif was allocated,
22642 	 * ipif_allocate does not know the right interface index.
22643 	 */
22644 
22645 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22646 	/*
22647 	 * Now that the phyint's ifindex has been assigned, complete the
22648 	 * remaining
22649 	 */
22650 
22651 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22652 	if (ill->ill_isv6) {
22653 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22654 		    ill->ill_phyint->phyint_ifindex;
22655 	}
22656 
22657 	/*
22658 	 * Generate an event within the hooks framework to indicate that
22659 	 * a new interface has just been added to IP.  For this event to
22660 	 * be generated, the network interface must, at least, have an
22661 	 * ifindex assigned to it.
22662 	 *
22663 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22664 	 * that the ordering of delivered events to listeners matches the
22665 	 * order of them in the kernel.
22666 	 *
22667 	 * This function could be called from ill_lookup_on_name. In that case
22668 	 * the interface is loopback "lo", which will not generate a NIC event.
22669 	 */
22670 	if (ill->ill_name_length <= 2 ||
22671 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22672 		hook_nic_event_t *info;
22673 		if ((info = ill->ill_nic_event_info) != NULL) {
22674 			ip2dbg(("ill_phyint_reinit: unexpected nic event %d "
22675 			    "attached for %s\n", info->hne_event,
22676 			    ill->ill_name));
22677 			if (info->hne_data != NULL)
22678 				kmem_free(info->hne_data, info->hne_datalen);
22679 			kmem_free(info, sizeof (hook_nic_event_t));
22680 		}
22681 
22682 		info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
22683 		if (info != NULL) {
22684 			info->hne_nic = ill->ill_phyint->phyint_ifindex;
22685 			info->hne_lif = 0;
22686 			info->hne_event = NE_PLUMB;
22687 			info->hne_family = ill->ill_isv6 ? ipv6 : ipv4;
22688 			info->hne_data = kmem_alloc(ill->ill_name_length,
22689 			    KM_NOSLEEP);
22690 			if (info->hne_data != NULL) {
22691 				info->hne_datalen = ill->ill_name_length;
22692 				bcopy(ill->ill_name, info->hne_data,
22693 				    info->hne_datalen);
22694 			} else {
22695 				ip2dbg(("ill_phyint_reinit: could not attach "
22696 				    "ill_name information for PLUMB nic event "
22697 				    "of %s (ENOMEM)\n", ill->ill_name));
22698 				kmem_free(info, sizeof (hook_nic_event_t));
22699 			}
22700 		} else
22701 			ip2dbg(("ill_phyint_reinit: could not attach PLUMB nic "
22702 			    "event information for %s (ENOMEM)\n",
22703 			    ill->ill_name));
22704 
22705 		ill->ill_nic_event_info = info;
22706 	}
22707 
22708 	RELEASE_ILL_LOCKS(ill, ill_other);
22709 	mutex_exit(&phyi->phyint_lock);
22710 }
22711 
22712 /*
22713  * Notify any downstream modules of the name of this interface.
22714  * An M_IOCTL is used even though we don't expect a successful reply.
22715  * Any reply message from the driver (presumably an M_IOCNAK) will
22716  * eventually get discarded somewhere upstream.  The message format is
22717  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22718  * to IP.
22719  */
22720 static void
22721 ip_ifname_notify(ill_t *ill, queue_t *q)
22722 {
22723 	mblk_t *mp1, *mp2;
22724 	struct iocblk *iocp;
22725 	struct lifreq *lifr;
22726 
22727 	mp1 = mkiocb(SIOCSLIFNAME);
22728 	if (mp1 == NULL)
22729 		return;
22730 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22731 	if (mp2 == NULL) {
22732 		freeb(mp1);
22733 		return;
22734 	}
22735 
22736 	mp1->b_cont = mp2;
22737 	iocp = (struct iocblk *)mp1->b_rptr;
22738 	iocp->ioc_count = sizeof (struct lifreq);
22739 
22740 	lifr = (struct lifreq *)mp2->b_rptr;
22741 	mp2->b_wptr += sizeof (struct lifreq);
22742 	bzero(lifr, sizeof (struct lifreq));
22743 
22744 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22745 	lifr->lifr_ppa = ill->ill_ppa;
22746 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22747 
22748 	putnext(q, mp1);
22749 }
22750 
22751 static boolean_t ip_trash_timer_started = B_FALSE;
22752 
22753 static int
22754 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22755 {
22756 	int err;
22757 
22758 	/* Set the obsolete NDD per-interface forwarding name. */
22759 	err = ill_set_ndd_name(ill);
22760 	if (err != 0) {
22761 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22762 		    err);
22763 	}
22764 
22765 	/* Tell downstream modules where they are. */
22766 	ip_ifname_notify(ill, q);
22767 
22768 	/*
22769 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22770 	 * Error cases are ENOMEM ...
22771 	 */
22772 	err = ill_dl_phys(ill, ipif, mp, q);
22773 
22774 	/*
22775 	 * If there is no IRE expiration timer running, get one started.
22776 	 * igmp and mld timers will be triggered by the first multicast
22777 	 */
22778 	if (!ip_trash_timer_started) {
22779 		/*
22780 		 * acquire the lock and check again.
22781 		 */
22782 		mutex_enter(&ip_trash_timer_lock);
22783 		if (!ip_trash_timer_started) {
22784 			ip_ire_expire_id = timeout(ip_trash_timer_expire, NULL,
22785 			    MSEC_TO_TICK(ip_timer_interval));
22786 			ip_trash_timer_started = B_TRUE;
22787 		}
22788 		mutex_exit(&ip_trash_timer_lock);
22789 	}
22790 
22791 	if (ill->ill_isv6) {
22792 		mutex_enter(&mld_slowtimeout_lock);
22793 		if (mld_slowtimeout_id == 0) {
22794 			mld_slowtimeout_id = timeout(mld_slowtimo, NULL,
22795 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22796 		}
22797 		mutex_exit(&mld_slowtimeout_lock);
22798 	} else {
22799 		mutex_enter(&igmp_slowtimeout_lock);
22800 		if (igmp_slowtimeout_id == 0) {
22801 			igmp_slowtimeout_id = timeout(igmp_slowtimo, NULL,
22802 				MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22803 		}
22804 		mutex_exit(&igmp_slowtimeout_lock);
22805 	}
22806 
22807 	return (err);
22808 }
22809 
22810 /*
22811  * Common routine for ppa and ifname setting. Should be called exclusive.
22812  *
22813  * Returns EINPROGRESS when mp has been consumed by queueing it on
22814  * ill_pending_mp and the ioctl will complete in ip_rput.
22815  *
22816  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22817  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22818  * For SLIFNAME, we pass these values back to the userland.
22819  */
22820 static int
22821 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22822 {
22823 	ill_t	*ill;
22824 	ipif_t	*ipif;
22825 	ipsq_t	*ipsq;
22826 	char	*ppa_ptr;
22827 	char	*old_ptr;
22828 	char	old_char;
22829 	int	error;
22830 
22831 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
22832 	ASSERT(q->q_next != NULL);
22833 	ASSERT(interf_name != NULL);
22834 
22835 	ill = (ill_t *)q->q_ptr;
22836 
22837 	ASSERT(ill->ill_name[0] == '\0');
22838 	ASSERT(IAM_WRITER_ILL(ill));
22839 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
22840 	ASSERT(ill->ill_ppa == UINT_MAX);
22841 
22842 	/* The ppa is sent down by ifconfig or is chosen */
22843 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
22844 		return (EINVAL);
22845 	}
22846 
22847 	/*
22848 	 * make sure ppa passed in is same as ppa in the name.
22849 	 * This check is not made when ppa == UINT_MAX in that case ppa
22850 	 * in the name could be anything. System will choose a ppa and
22851 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
22852 	 */
22853 	if (*new_ppa_ptr != UINT_MAX) {
22854 		/* stoi changes the pointer */
22855 		old_ptr = ppa_ptr;
22856 		/*
22857 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
22858 		 * (they don't have an externally visible ppa).  We assign one
22859 		 * here so that we can manage the interface.  Note that in
22860 		 * the past this value was always 0 for DLPI 1 drivers.
22861 		 */
22862 		if (*new_ppa_ptr == 0)
22863 			*new_ppa_ptr = stoi(&old_ptr);
22864 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
22865 			return (EINVAL);
22866 	}
22867 	/*
22868 	 * terminate string before ppa
22869 	 * save char at that location.
22870 	 */
22871 	old_char = ppa_ptr[0];
22872 	ppa_ptr[0] = '\0';
22873 
22874 	ill->ill_ppa = *new_ppa_ptr;
22875 	/*
22876 	 * Finish as much work now as possible before calling ill_glist_insert
22877 	 * which makes the ill globally visible and also merges it with the
22878 	 * other protocol instance of this phyint. The remaining work is
22879 	 * done after entering the ipsq which may happen sometime later.
22880 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
22881 	 */
22882 	ipif = ill->ill_ipif;
22883 
22884 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
22885 	ipif_assign_seqid(ipif);
22886 
22887 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
22888 		ill->ill_flags |= ILLF_IPV4;
22889 
22890 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
22891 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
22892 
22893 	if (ill->ill_flags & ILLF_IPV6) {
22894 
22895 		ill->ill_isv6 = B_TRUE;
22896 		if (ill->ill_rq != NULL) {
22897 			ill->ill_rq->q_qinfo = &rinit_ipv6;
22898 			ill->ill_wq->q_qinfo = &winit_ipv6;
22899 		}
22900 
22901 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
22902 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
22903 		ipif->ipif_v6src_addr = ipv6_all_zeros;
22904 		ipif->ipif_v6subnet = ipv6_all_zeros;
22905 		ipif->ipif_v6net_mask = ipv6_all_zeros;
22906 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
22907 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
22908 		/*
22909 		 * point-to-point or Non-mulicast capable
22910 		 * interfaces won't do NUD unless explicitly
22911 		 * configured to do so.
22912 		 */
22913 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
22914 		    !(ill->ill_flags & ILLF_MULTICAST)) {
22915 			ill->ill_flags |= ILLF_NONUD;
22916 		}
22917 		/* Make sure IPv4 specific flag is not set on IPv6 if */
22918 		if (ill->ill_flags & ILLF_NOARP) {
22919 			/*
22920 			 * Note: xresolv interfaces will eventually need
22921 			 * NOARP set here as well, but that will require
22922 			 * those external resolvers to have some
22923 			 * knowledge of that flag and act appropriately.
22924 			 * Not to be changed at present.
22925 			 */
22926 			ill->ill_flags &= ~ILLF_NOARP;
22927 		}
22928 		/*
22929 		 * Set the ILLF_ROUTER flag according to the global
22930 		 * IPv6 forwarding policy.
22931 		 */
22932 		if (ipv6_forward != 0)
22933 			ill->ill_flags |= ILLF_ROUTER;
22934 	} else if (ill->ill_flags & ILLF_IPV4) {
22935 		ill->ill_isv6 = B_FALSE;
22936 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
22937 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
22938 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
22939 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
22940 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
22941 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
22942 		/*
22943 		 * Set the ILLF_ROUTER flag according to the global
22944 		 * IPv4 forwarding policy.
22945 		 */
22946 		if (ip_g_forward != 0)
22947 			ill->ill_flags |= ILLF_ROUTER;
22948 	}
22949 
22950 	ASSERT(ill->ill_phyint != NULL);
22951 
22952 	/*
22953 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
22954 	 * be completed in ill_glist_insert -> ill_phyint_reinit
22955 	 */
22956 	if (!ill_allocate_mibs(ill))
22957 		return (ENOMEM);
22958 
22959 	/*
22960 	 * Pick a default sap until we get the DL_INFO_ACK back from
22961 	 * the driver.
22962 	 */
22963 	if (ill->ill_sap == 0) {
22964 		if (ill->ill_isv6)
22965 			ill->ill_sap  = IP6_DL_SAP;
22966 		else
22967 			ill->ill_sap  = IP_DL_SAP;
22968 	}
22969 
22970 	ill->ill_ifname_pending = 1;
22971 	ill->ill_ifname_pending_err = 0;
22972 
22973 	ill_refhold(ill);
22974 	rw_enter(&ill_g_lock, RW_WRITER);
22975 	if ((error = ill_glist_insert(ill, interf_name,
22976 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
22977 		ill->ill_ppa = UINT_MAX;
22978 		ill->ill_name[0] = '\0';
22979 		/*
22980 		 * undo null termination done above.
22981 		 */
22982 		ppa_ptr[0] = old_char;
22983 		rw_exit(&ill_g_lock);
22984 		ill_refrele(ill);
22985 		return (error);
22986 	}
22987 
22988 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
22989 
22990 	/*
22991 	 * When we return the buffer pointed to by interf_name should contain
22992 	 * the same name as in ill_name.
22993 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
22994 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
22995 	 * so copy full name and update the ppa ptr.
22996 	 * When ppa passed in != UINT_MAX all values are correct just undo
22997 	 * null termination, this saves a bcopy.
22998 	 */
22999 	if (*new_ppa_ptr == UINT_MAX) {
23000 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23001 		*new_ppa_ptr = ill->ill_ppa;
23002 	} else {
23003 		/*
23004 		 * undo null termination done above.
23005 		 */
23006 		ppa_ptr[0] = old_char;
23007 	}
23008 
23009 	/* Let SCTP know about this ILL */
23010 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23011 
23012 	/* and also about the first ipif */
23013 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
23014 
23015 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23016 	    B_TRUE);
23017 
23018 	rw_exit(&ill_g_lock);
23019 	ill_refrele(ill);
23020 	if (ipsq == NULL)
23021 		return (EINPROGRESS);
23022 
23023 	/*
23024 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23025 	 */
23026 	if (ipsq->ipsq_current_ipif == NULL)
23027 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23028 	else
23029 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23030 
23031 	error = ipif_set_values_tail(ill, ipif, mp, q);
23032 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
23033 	if (error != 0 && error != EINPROGRESS) {
23034 		/*
23035 		 * restore previous values
23036 		 */
23037 		ill->ill_isv6 = B_FALSE;
23038 	}
23039 	return (error);
23040 }
23041 
23042 
23043 extern void (*ip_cleanup_func)(void);
23044 
23045 void
23046 ipif_init(void)
23047 {
23048 	hrtime_t hrt;
23049 	int i;
23050 
23051 	/*
23052 	 * Can't call drv_getparm here as it is too early in the boot.
23053 	 * As we use ipif_src_random just for picking a different
23054 	 * source address everytime, this need not be really random.
23055 	 */
23056 	hrt = gethrtime();
23057 	ipif_src_random = ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23058 
23059 	for (i = 0; i < MAX_G_HEADS; i++) {
23060 		ill_g_heads[i].ill_g_list_head = (ill_if_t *)&ill_g_heads[i];
23061 		ill_g_heads[i].ill_g_list_tail = (ill_if_t *)&ill_g_heads[i];
23062 	}
23063 
23064 	avl_create(&phyint_g_list.phyint_list_avl_by_index,
23065 	    ill_phyint_compare_index,
23066 	    sizeof (phyint_t),
23067 	    offsetof(struct phyint, phyint_avl_by_index));
23068 	avl_create(&phyint_g_list.phyint_list_avl_by_name,
23069 	    ill_phyint_compare_name,
23070 	    sizeof (phyint_t),
23071 	    offsetof(struct phyint, phyint_avl_by_name));
23072 
23073 	ip_cleanup_func = ip_thread_exit;
23074 }
23075 
23076 /*
23077  * This is called by ip_rt_add when src_addr value is other than zero.
23078  * src_addr signifies the source address of the incoming packet. For
23079  * reverse tunnel route we need to create a source addr based routing
23080  * table. This routine creates ip_mrtun_table if it's empty and then
23081  * it adds the route entry hashed by source address. It verifies that
23082  * the outgoing interface is always a non-resolver interface (tunnel).
23083  */
23084 int
23085 ip_mrtun_rt_add(ipaddr_t in_src_addr, int flags, ipif_t *ipif_arg,
23086     ipif_t *src_ipif, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func)
23087 {
23088 	ire_t   *ire;
23089 	ire_t	*save_ire;
23090 	ipif_t  *ipif;
23091 	ill_t   *in_ill = NULL;
23092 	ill_t	*out_ill;
23093 	queue_t	*stq;
23094 	mblk_t	*dlureq_mp;
23095 	int	error;
23096 
23097 	if (ire_arg != NULL)
23098 		*ire_arg = NULL;
23099 	ASSERT(in_src_addr != INADDR_ANY);
23100 
23101 	ipif = ipif_arg;
23102 	if (ipif != NULL) {
23103 		out_ill = ipif->ipif_ill;
23104 	} else {
23105 		ip1dbg(("ip_mrtun_rt_add: ipif is NULL\n"));
23106 		return (EINVAL);
23107 	}
23108 
23109 	if (src_ipif == NULL) {
23110 		ip1dbg(("ip_mrtun_rt_add: src_ipif is NULL\n"));
23111 		return (EINVAL);
23112 	}
23113 	in_ill = src_ipif->ipif_ill;
23114 
23115 	/*
23116 	 * Check for duplicates. We don't need to
23117 	 * match out_ill, because the uniqueness of
23118 	 * a route is only dependent on src_addr and
23119 	 * in_ill.
23120 	 */
23121 	ire = ire_mrtun_lookup(in_src_addr, in_ill);
23122 	if (ire != NULL) {
23123 		ire_refrele(ire);
23124 		return (EEXIST);
23125 	}
23126 	if (ipif->ipif_net_type != IRE_IF_NORESOLVER) {
23127 		ip2dbg(("ip_mrtun_rt_add: outgoing interface is type %d\n",
23128 		    ipif->ipif_net_type));
23129 		return (EINVAL);
23130 	}
23131 
23132 	stq = ipif->ipif_wq;
23133 	ASSERT(stq != NULL);
23134 
23135 	/*
23136 	 * The outgoing interface must be non-resolver
23137 	 * interface.
23138 	 */
23139 	dlureq_mp = ill_dlur_gen(NULL,
23140 	    out_ill->ill_phys_addr_length, out_ill->ill_sap,
23141 	    out_ill->ill_sap_length);
23142 
23143 	if (dlureq_mp == NULL) {
23144 		ip1dbg(("ip_newroute: dlureq_mp NULL\n"));
23145 		return (ENOMEM);
23146 	}
23147 
23148 	/* Create the IRE. */
23149 
23150 	ire = ire_create(
23151 	    NULL,				/* Zero dst addr */
23152 	    NULL,				/* Zero mask */
23153 	    NULL,				/* Zero gateway addr */
23154 	    NULL,				/* Zero ipif_src addr */
23155 	    (uint8_t *)&in_src_addr,		/* in_src-addr */
23156 	    &ipif->ipif_mtu,
23157 	    NULL,
23158 	    NULL,				/* rfq */
23159 	    stq,
23160 	    IRE_MIPRTUN,
23161 	    dlureq_mp,
23162 	    ipif,
23163 	    in_ill,
23164 	    0,
23165 	    0,
23166 	    0,
23167 	    flags,
23168 	    &ire_uinfo_null,
23169 	    NULL,
23170 	    NULL);
23171 
23172 	if (ire == NULL) {
23173 		freeb(dlureq_mp);
23174 		return (ENOMEM);
23175 	}
23176 	ip2dbg(("ip_mrtun_rt_add: mrtun route is created with type %d\n",
23177 	    ire->ire_type));
23178 	save_ire = ire;
23179 	ASSERT(save_ire != NULL);
23180 	error = ire_add_mrtun(&ire, q, mp, func);
23181 	/*
23182 	 * If ire_add_mrtun() failed, the ire passed in was freed
23183 	 * so there is no need to do so here.
23184 	 */
23185 	if (error != 0) {
23186 		return (error);
23187 	}
23188 
23189 	/* Duplicate check */
23190 	if (ire != save_ire) {
23191 		/* route already exists by now */
23192 		ire_refrele(ire);
23193 		return (EEXIST);
23194 	}
23195 
23196 	if (ire_arg != NULL) {
23197 		/*
23198 		 * Store the ire that was just added. the caller
23199 		 * ip_rts_request responsible for doing ire_refrele()
23200 		 * on it.
23201 		 */
23202 		*ire_arg = ire;
23203 	} else {
23204 		ire_refrele(ire);	/* held in ire_add_mrtun */
23205 	}
23206 
23207 	return (0);
23208 }
23209 
23210 /*
23211  * It is called by ip_rt_delete() only when mipagent requests to delete
23212  * a reverse tunnel route that was added by ip_mrtun_rt_add() before.
23213  */
23214 
23215 int
23216 ip_mrtun_rt_delete(ipaddr_t in_src_addr, ipif_t *src_ipif)
23217 {
23218 	ire_t   *ire = NULL;
23219 
23220 	if (in_src_addr == INADDR_ANY)
23221 		return (EINVAL);
23222 	if (src_ipif == NULL)
23223 		return (EINVAL);
23224 
23225 	/* search if this route exists in the ip_mrtun_table */
23226 	ire = ire_mrtun_lookup(in_src_addr, src_ipif->ipif_ill);
23227 	if (ire == NULL) {
23228 		ip2dbg(("ip_mrtun_rt_delete: ire not found\n"));
23229 		return (ESRCH);
23230 	}
23231 	ire_delete(ire);
23232 	ire_refrele(ire);
23233 	return (0);
23234 }
23235 
23236 /*
23237  * Lookup the ipif corresponding to the onlink destination address. For
23238  * point-to-point interfaces, it matches with remote endpoint destination
23239  * address. For point-to-multipoint interfaces it only tries to match the
23240  * destination with the interface's subnet address. The longest, most specific
23241  * match is found to take care of such rare network configurations like -
23242  * le0: 129.146.1.1/16
23243  * le1: 129.146.2.2/24
23244  * It is used only by SO_DONTROUTE at the moment.
23245  */
23246 ipif_t *
23247 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid)
23248 {
23249 	ipif_t	*ipif, *best_ipif;
23250 	ill_t	*ill;
23251 	ill_walk_context_t ctx;
23252 
23253 	ASSERT(zoneid != ALL_ZONES);
23254 	best_ipif = NULL;
23255 
23256 	rw_enter(&ill_g_lock, RW_READER);
23257 	ill = ILL_START_WALK_V4(&ctx);
23258 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23259 		mutex_enter(&ill->ill_lock);
23260 		for (ipif = ill->ill_ipif; ipif != NULL;
23261 		    ipif = ipif->ipif_next) {
23262 			if (!IPIF_CAN_LOOKUP(ipif))
23263 				continue;
23264 			if (ipif->ipif_zoneid != zoneid &&
23265 			    ipif->ipif_zoneid != ALL_ZONES)
23266 				continue;
23267 			/*
23268 			 * Point-to-point case. Look for exact match with
23269 			 * destination address.
23270 			 */
23271 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23272 				if (ipif->ipif_pp_dst_addr == addr) {
23273 					ipif_refhold_locked(ipif);
23274 					mutex_exit(&ill->ill_lock);
23275 					rw_exit(&ill_g_lock);
23276 					if (best_ipif != NULL)
23277 						ipif_refrele(best_ipif);
23278 					return (ipif);
23279 				}
23280 			} else if (ipif->ipif_subnet == (addr &
23281 			    ipif->ipif_net_mask)) {
23282 				/*
23283 				 * Point-to-multipoint case. Looping through to
23284 				 * find the most specific match. If there are
23285 				 * multiple best match ipif's then prefer ipif's
23286 				 * that are UP. If there is only one best match
23287 				 * ipif and it is DOWN we must still return it.
23288 				 */
23289 				if ((best_ipif == NULL) ||
23290 				    (ipif->ipif_net_mask >
23291 				    best_ipif->ipif_net_mask) ||
23292 				    ((ipif->ipif_net_mask ==
23293 				    best_ipif->ipif_net_mask) &&
23294 				    ((ipif->ipif_flags & IPIF_UP) &&
23295 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23296 					ipif_refhold_locked(ipif);
23297 					mutex_exit(&ill->ill_lock);
23298 					rw_exit(&ill_g_lock);
23299 					if (best_ipif != NULL)
23300 						ipif_refrele(best_ipif);
23301 					best_ipif = ipif;
23302 					rw_enter(&ill_g_lock, RW_READER);
23303 					mutex_enter(&ill->ill_lock);
23304 				}
23305 			}
23306 		}
23307 		mutex_exit(&ill->ill_lock);
23308 	}
23309 	rw_exit(&ill_g_lock);
23310 	return (best_ipif);
23311 }
23312 
23313 
23314 /*
23315  * Save enough information so that we can recreate the IRE if
23316  * the interface goes down and then up.
23317  */
23318 static void
23319 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23320 {
23321 	mblk_t	*save_mp;
23322 
23323 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23324 	if (save_mp != NULL) {
23325 		ifrt_t	*ifrt;
23326 
23327 		save_mp->b_wptr += sizeof (ifrt_t);
23328 		ifrt = (ifrt_t *)save_mp->b_rptr;
23329 		bzero(ifrt, sizeof (ifrt_t));
23330 		ifrt->ifrt_type = ire->ire_type;
23331 		ifrt->ifrt_addr = ire->ire_addr;
23332 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23333 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23334 		ifrt->ifrt_mask = ire->ire_mask;
23335 		ifrt->ifrt_flags = ire->ire_flags;
23336 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23337 		mutex_enter(&ipif->ipif_saved_ire_lock);
23338 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23339 		ipif->ipif_saved_ire_mp = save_mp;
23340 		ipif->ipif_saved_ire_cnt++;
23341 		mutex_exit(&ipif->ipif_saved_ire_lock);
23342 	}
23343 }
23344 
23345 
23346 static void
23347 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23348 {
23349 	mblk_t	**mpp;
23350 	mblk_t	*mp;
23351 	ifrt_t	*ifrt;
23352 
23353 	/* Remove from ipif_saved_ire_mp list if it is there */
23354 	mutex_enter(&ipif->ipif_saved_ire_lock);
23355 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23356 	    mpp = &(*mpp)->b_cont) {
23357 		/*
23358 		 * On a given ipif, the triple of address, gateway and
23359 		 * mask is unique for each saved IRE (in the case of
23360 		 * ordinary interface routes, the gateway address is
23361 		 * all-zeroes).
23362 		 */
23363 		mp = *mpp;
23364 		ifrt = (ifrt_t *)mp->b_rptr;
23365 		if (ifrt->ifrt_addr == ire->ire_addr &&
23366 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23367 		    ifrt->ifrt_mask == ire->ire_mask) {
23368 			*mpp = mp->b_cont;
23369 			ipif->ipif_saved_ire_cnt--;
23370 			freeb(mp);
23371 			break;
23372 		}
23373 	}
23374 	mutex_exit(&ipif->ipif_saved_ire_lock);
23375 }
23376 
23377 
23378 /*
23379  * IP multirouting broadcast routes handling
23380  * Append CGTP broadcast IREs to regular ones created
23381  * at ifconfig time.
23382  */
23383 static void
23384 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst)
23385 {
23386 	ire_t *ire_prim;
23387 
23388 	ASSERT(ire != NULL);
23389 	ASSERT(ire_dst != NULL);
23390 
23391 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23392 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
23393 	if (ire_prim != NULL) {
23394 		/*
23395 		 * We are in the special case of broadcasts for
23396 		 * CGTP. We add an IRE_BROADCAST that holds
23397 		 * the RTF_MULTIRT flag, the destination
23398 		 * address of ire_dst and the low level
23399 		 * info of ire_prim. In other words, CGTP
23400 		 * broadcast is added to the redundant ipif.
23401 		 */
23402 		ipif_t *ipif_prim;
23403 		ire_t  *bcast_ire;
23404 
23405 		ipif_prim = ire_prim->ire_ipif;
23406 
23407 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23408 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23409 		    (void *)ire_dst, (void *)ire_prim,
23410 		    (void *)ipif_prim));
23411 
23412 		bcast_ire = ire_create(
23413 		    (uchar_t *)&ire->ire_addr,
23414 		    (uchar_t *)&ip_g_all_ones,
23415 		    (uchar_t *)&ire_dst->ire_src_addr,
23416 		    (uchar_t *)&ire->ire_gateway_addr,
23417 		    NULL,
23418 		    &ipif_prim->ipif_mtu,
23419 		    NULL,
23420 		    ipif_prim->ipif_rq,
23421 		    ipif_prim->ipif_wq,
23422 		    IRE_BROADCAST,
23423 		    ipif_prim->ipif_bcast_mp,
23424 		    ipif_prim,
23425 		    NULL,
23426 		    0,
23427 		    0,
23428 		    0,
23429 		    ire->ire_flags,
23430 		    &ire_uinfo_null,
23431 		    NULL,
23432 		    NULL);
23433 
23434 		if (bcast_ire != NULL) {
23435 
23436 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23437 			    B_FALSE) == 0) {
23438 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23439 				    "added bcast_ire %p\n",
23440 				    (void *)bcast_ire));
23441 
23442 				ipif_save_ire(bcast_ire->ire_ipif,
23443 				    bcast_ire);
23444 				ire_refrele(bcast_ire);
23445 			}
23446 		}
23447 		ire_refrele(ire_prim);
23448 	}
23449 }
23450 
23451 
23452 /*
23453  * IP multirouting broadcast routes handling
23454  * Remove the broadcast ire
23455  */
23456 static void
23457 ip_cgtp_bcast_delete(ire_t *ire)
23458 {
23459 	ire_t *ire_dst;
23460 
23461 	ASSERT(ire != NULL);
23462 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23463 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
23464 	if (ire_dst != NULL) {
23465 		ire_t *ire_prim;
23466 
23467 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23468 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
23469 		if (ire_prim != NULL) {
23470 			ipif_t *ipif_prim;
23471 			ire_t  *bcast_ire;
23472 
23473 			ipif_prim = ire_prim->ire_ipif;
23474 
23475 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23476 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23477 			    (void *)ire_dst, (void *)ire_prim,
23478 			    (void *)ipif_prim));
23479 
23480 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23481 			    ire->ire_gateway_addr,
23482 			    IRE_BROADCAST,
23483 			    ipif_prim, ALL_ZONES,
23484 			    NULL,
23485 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23486 			    MATCH_IRE_MASK);
23487 
23488 			if (bcast_ire != NULL) {
23489 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23490 				    "looked up bcast_ire %p\n",
23491 				    (void *)bcast_ire));
23492 				ipif_remove_ire(bcast_ire->ire_ipif,
23493 					bcast_ire);
23494 				ire_delete(bcast_ire);
23495 			}
23496 			ire_refrele(ire_prim);
23497 		}
23498 		ire_refrele(ire_dst);
23499 	}
23500 }
23501 
23502 /*
23503  * IPsec hardware acceleration capabilities related functions.
23504  */
23505 
23506 /*
23507  * Free a per-ill IPsec capabilities structure.
23508  */
23509 static void
23510 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23511 {
23512 	if (capab->auth_hw_algs != NULL)
23513 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23514 	if (capab->encr_hw_algs != NULL)
23515 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23516 	if (capab->encr_algparm != NULL)
23517 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23518 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23519 }
23520 
23521 /*
23522  * Allocate a new per-ill IPsec capabilities structure. This structure
23523  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23524  * an array which specifies, for each algorithm, whether this algorithm
23525  * is supported by the ill or not.
23526  */
23527 static ill_ipsec_capab_t *
23528 ill_ipsec_capab_alloc(void)
23529 {
23530 	ill_ipsec_capab_t *capab;
23531 	uint_t nelems;
23532 
23533 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23534 	if (capab == NULL)
23535 		return (NULL);
23536 
23537 	/* we need one bit per algorithm */
23538 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23539 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23540 
23541 	/* allocate memory to store algorithm flags */
23542 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23543 	if (capab->encr_hw_algs == NULL)
23544 		goto nomem;
23545 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23546 	if (capab->auth_hw_algs == NULL)
23547 		goto nomem;
23548 	/*
23549 	 * Leave encr_algparm NULL for now since we won't need it half
23550 	 * the time
23551 	 */
23552 	return (capab);
23553 
23554 nomem:
23555 	ill_ipsec_capab_free(capab);
23556 	return (NULL);
23557 }
23558 
23559 /*
23560  * Resize capability array.  Since we're exclusive, this is OK.
23561  */
23562 static boolean_t
23563 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23564 {
23565 	ipsec_capab_algparm_t *nalp, *oalp;
23566 	uint32_t olen, nlen;
23567 
23568 	oalp = capab->encr_algparm;
23569 	olen = capab->encr_algparm_size;
23570 
23571 	if (oalp != NULL) {
23572 		if (algid < capab->encr_algparm_end)
23573 			return (B_TRUE);
23574 	}
23575 
23576 	nlen = (algid + 1) * sizeof (*nalp);
23577 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23578 	if (nalp == NULL)
23579 		return (B_FALSE);
23580 
23581 	if (oalp != NULL) {
23582 		bcopy(oalp, nalp, olen);
23583 		kmem_free(oalp, olen);
23584 	}
23585 	capab->encr_algparm = nalp;
23586 	capab->encr_algparm_size = nlen;
23587 	capab->encr_algparm_end = algid + 1;
23588 
23589 	return (B_TRUE);
23590 }
23591 
23592 /*
23593  * Compare the capabilities of the specified ill with the protocol
23594  * and algorithms specified by the SA passed as argument.
23595  * If they match, returns B_TRUE, B_FALSE if they do not match.
23596  *
23597  * The ill can be passed as a pointer to it, or by specifying its index
23598  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23599  *
23600  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23601  * packet is eligible for hardware acceleration, and by
23602  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23603  * to a particular ill.
23604  */
23605 boolean_t
23606 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23607     ipsa_t *sa)
23608 {
23609 	boolean_t sa_isv6;
23610 	uint_t algid;
23611 	struct ill_ipsec_capab_s *cpp;
23612 	boolean_t need_refrele = B_FALSE;
23613 
23614 	if (ill == NULL) {
23615 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23616 		    NULL, NULL, NULL);
23617 		if (ill == NULL) {
23618 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23619 			return (B_FALSE);
23620 		}
23621 		need_refrele = B_TRUE;
23622 	}
23623 
23624 	/*
23625 	 * Use the address length specified by the SA to determine
23626 	 * if it corresponds to a IPv6 address, and fail the matching
23627 	 * if the isv6 flag passed as argument does not match.
23628 	 * Note: this check is used for SADB capability checking before
23629 	 * sending SA information to an ill.
23630 	 */
23631 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23632 	if (sa_isv6 != ill_isv6)
23633 		/* protocol mismatch */
23634 		goto done;
23635 
23636 	/*
23637 	 * Check if the ill supports the protocol, algorithm(s) and
23638 	 * key size(s) specified by the SA, and get the pointers to
23639 	 * the algorithms supported by the ill.
23640 	 */
23641 	switch (sa->ipsa_type) {
23642 
23643 	case SADB_SATYPE_ESP:
23644 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23645 			/* ill does not support ESP acceleration */
23646 			goto done;
23647 		cpp = ill->ill_ipsec_capab_esp;
23648 		algid = sa->ipsa_auth_alg;
23649 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23650 			goto done;
23651 		algid = sa->ipsa_encr_alg;
23652 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23653 			goto done;
23654 		if (algid < cpp->encr_algparm_end) {
23655 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23656 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23657 				goto done;
23658 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23659 				goto done;
23660 		}
23661 		break;
23662 
23663 	case SADB_SATYPE_AH:
23664 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23665 			/* ill does not support AH acceleration */
23666 			goto done;
23667 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23668 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23669 			goto done;
23670 		break;
23671 	}
23672 
23673 	if (need_refrele)
23674 		ill_refrele(ill);
23675 	return (B_TRUE);
23676 done:
23677 	if (need_refrele)
23678 		ill_refrele(ill);
23679 	return (B_FALSE);
23680 }
23681 
23682 
23683 /*
23684  * Add a new ill to the list of IPsec capable ills.
23685  * Called from ill_capability_ipsec_ack() when an ACK was received
23686  * indicating that IPsec hardware processing was enabled for an ill.
23687  *
23688  * ill must point to the ill for which acceleration was enabled.
23689  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23690  */
23691 static void
23692 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23693 {
23694 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23695 	uint_t sa_type;
23696 	uint_t ipproto;
23697 
23698 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23699 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23700 
23701 	switch (dl_cap) {
23702 	case DL_CAPAB_IPSEC_AH:
23703 		sa_type = SADB_SATYPE_AH;
23704 		ills = &ipsec_capab_ills_ah;
23705 		ipproto = IPPROTO_AH;
23706 		break;
23707 	case DL_CAPAB_IPSEC_ESP:
23708 		sa_type = SADB_SATYPE_ESP;
23709 		ills = &ipsec_capab_ills_esp;
23710 		ipproto = IPPROTO_ESP;
23711 		break;
23712 	}
23713 
23714 	rw_enter(&ipsec_capab_ills_lock, RW_WRITER);
23715 
23716 	/*
23717 	 * Add ill index to list of hardware accelerators. If
23718 	 * already in list, do nothing.
23719 	 */
23720 	for (cur_ill = *ills; cur_ill != NULL &&
23721 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23722 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23723 		;
23724 
23725 	if (cur_ill == NULL) {
23726 		/* if this is a new entry for this ill */
23727 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23728 		if (new_ill == NULL) {
23729 			rw_exit(&ipsec_capab_ills_lock);
23730 			return;
23731 		}
23732 
23733 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23734 		new_ill->ill_isv6 = ill->ill_isv6;
23735 		new_ill->next = *ills;
23736 		*ills = new_ill;
23737 	} else if (!sadb_resync) {
23738 		/* not resync'ing SADB and an entry exists for this ill */
23739 		rw_exit(&ipsec_capab_ills_lock);
23740 		return;
23741 	}
23742 
23743 	rw_exit(&ipsec_capab_ills_lock);
23744 
23745 	if (ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23746 		/*
23747 		 * IPsec module for protocol loaded, initiate dump
23748 		 * of the SADB to this ill.
23749 		 */
23750 		sadb_ill_download(ill, sa_type);
23751 }
23752 
23753 /*
23754  * Remove an ill from the list of IPsec capable ills.
23755  */
23756 static void
23757 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23758 {
23759 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23760 
23761 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23762 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23763 
23764 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipsec_capab_ills_ah :
23765 	    &ipsec_capab_ills_esp;
23766 
23767 	rw_enter(&ipsec_capab_ills_lock, RW_WRITER);
23768 
23769 	prev_ill = NULL;
23770 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23771 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23772 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23773 		;
23774 	if (cur_ill == NULL) {
23775 		/* entry not found */
23776 		rw_exit(&ipsec_capab_ills_lock);
23777 		return;
23778 	}
23779 	if (prev_ill == NULL) {
23780 		/* entry at front of list */
23781 		*ills = NULL;
23782 	} else {
23783 		prev_ill->next = cur_ill->next;
23784 	}
23785 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23786 	rw_exit(&ipsec_capab_ills_lock);
23787 }
23788 
23789 
23790 /*
23791  * Handling of DL_CONTROL_REQ messages that must be sent down to
23792  * an ill while having exclusive access.
23793  */
23794 /* ARGSUSED */
23795 static void
23796 ill_ipsec_capab_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
23797 {
23798 	ill_t *ill = (ill_t *)q->q_ptr;
23799 
23800 	ill_dlpi_send(ill, mp);
23801 }
23802 
23803 
23804 /*
23805  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23806  * supporting the specified IPsec protocol acceleration.
23807  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23808  * We free the mblk and, if sa is non-null, release the held referece.
23809  */
23810 void
23811 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa)
23812 {
23813 	ipsec_capab_ill_t *ici, *cur_ici;
23814 	ill_t *ill;
23815 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23816 
23817 	ici = (sa_type == SADB_SATYPE_AH) ? ipsec_capab_ills_ah :
23818 	    ipsec_capab_ills_esp;
23819 
23820 	rw_enter(&ipsec_capab_ills_lock, RW_READER);
23821 
23822 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23823 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23824 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL);
23825 
23826 		/*
23827 		 * Handle the case where the ill goes away while the SADB is
23828 		 * attempting to send messages.  If it's going away, it's
23829 		 * nuking its shadow SADB, so we don't care..
23830 		 */
23831 
23832 		if (ill == NULL)
23833 			continue;
23834 
23835 		if (sa != NULL) {
23836 			/*
23837 			 * Make sure capabilities match before
23838 			 * sending SA to ill.
23839 			 */
23840 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23841 			    cur_ici->ill_isv6, sa)) {
23842 				ill_refrele(ill);
23843 				continue;
23844 			}
23845 
23846 			mutex_enter(&sa->ipsa_lock);
23847 			sa->ipsa_flags |= IPSA_F_HW;
23848 			mutex_exit(&sa->ipsa_lock);
23849 		}
23850 
23851 		/*
23852 		 * Copy template message, and add it to the front
23853 		 * of the mblk ship list. We want to avoid holding
23854 		 * the ipsec_capab_ills_lock while sending the
23855 		 * message to the ills.
23856 		 *
23857 		 * The b_next and b_prev are temporarily used
23858 		 * to build a list of mblks to be sent down, and to
23859 		 * save the ill to which they must be sent.
23860 		 */
23861 		nmp = copymsg(mp);
23862 		if (nmp == NULL) {
23863 			ill_refrele(ill);
23864 			continue;
23865 		}
23866 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23867 		nmp->b_next = mp_ship_list;
23868 		mp_ship_list = nmp;
23869 		nmp->b_prev = (mblk_t *)ill;
23870 	}
23871 
23872 	rw_exit(&ipsec_capab_ills_lock);
23873 
23874 	nmp = mp_ship_list;
23875 	while (nmp != NULL) {
23876 		/* restore the mblk to a sane state */
23877 		next_mp = nmp->b_next;
23878 		nmp->b_next = NULL;
23879 		ill = (ill_t *)nmp->b_prev;
23880 		nmp->b_prev = NULL;
23881 
23882 		/*
23883 		 * Ship the mblk to the ill, must be exclusive. Keep the
23884 		 * reference to the ill as qwriter_ip() does a ill_referele().
23885 		 */
23886 		(void) qwriter_ip(NULL, ill, ill->ill_wq, nmp,
23887 		    ill_ipsec_capab_send_writer, NEW_OP, B_TRUE);
23888 
23889 		nmp = next_mp;
23890 	}
23891 
23892 	if (sa != NULL)
23893 		IPSA_REFRELE(sa);
23894 	freemsg(mp);
23895 }
23896 
23897 
23898 /*
23899  * Derive an interface id from the link layer address.
23900  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23901  */
23902 static boolean_t
23903 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23904 {
23905 	char		*addr;
23906 
23907 	if (phys_length != ETHERADDRL)
23908 		return (B_FALSE);
23909 
23910 	/* Form EUI-64 like address */
23911 	addr = (char *)&v6addr->s6_addr32[2];
23912 	bcopy((char *)phys_addr, addr, 3);
23913 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23914 	addr[3] = (char)0xff;
23915 	addr[4] = (char)0xfe;
23916 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23917 	return (B_TRUE);
23918 }
23919 
23920 /* ARGSUSED */
23921 static boolean_t
23922 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23923 {
23924 	return (B_FALSE);
23925 }
23926 
23927 /* ARGSUSED */
23928 static boolean_t
23929 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23930     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23931 {
23932 	/*
23933 	 * Multicast address mappings used over Ethernet/802.X.
23934 	 * This address is used as a base for mappings.
23935 	 */
23936 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23937 	    0x00, 0x00, 0x00};
23938 
23939 	/*
23940 	 * Extract low order 32 bits from IPv6 multicast address.
23941 	 * Or that into the link layer address, starting from the
23942 	 * second byte.
23943 	 */
23944 	*hw_start = 2;
23945 	v6_extract_mask->s6_addr32[0] = 0;
23946 	v6_extract_mask->s6_addr32[1] = 0;
23947 	v6_extract_mask->s6_addr32[2] = 0;
23948 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23949 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23950 	return (B_TRUE);
23951 }
23952 
23953 /*
23954  * Indicate by return value whether multicast is supported. If not,
23955  * this code should not touch/change any parameters.
23956  */
23957 /* ARGSUSED */
23958 static boolean_t
23959 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23960     uint32_t *hw_start, ipaddr_t *extract_mask)
23961 {
23962 	/*
23963 	 * Multicast address mappings used over Ethernet/802.X.
23964 	 * This address is used as a base for mappings.
23965 	 */
23966 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23967 	    0x00, 0x00, 0x00 };
23968 
23969 	if (phys_length != ETHERADDRL)
23970 		return (B_FALSE);
23971 
23972 	*extract_mask = htonl(0x007fffff);
23973 	*hw_start = 2;
23974 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23975 	return (B_TRUE);
23976 }
23977 
23978 /*
23979  * Derive IPoIB interface id from the link layer address.
23980  */
23981 static boolean_t
23982 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23983 {
23984 	char		*addr;
23985 
23986 	if (phys_length != 20)
23987 		return (B_FALSE);
23988 	addr = (char *)&v6addr->s6_addr32[2];
23989 	bcopy(phys_addr + 12, addr, 8);
23990 	/*
23991 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23992 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23993 	 * rules. In these cases, the IBA considers these GUIDs to be in
23994 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23995 	 * required; vendors are required not to assign global EUI-64's
23996 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23997 	 * of the interface identifier. Whether the GUID is in modified
23998 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23999 	 * bit set to 1.
24000 	 */
24001 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
24002 	return (B_TRUE);
24003 }
24004 
24005 /*
24006  * Note on mapping from multicast IP addresses to IPoIB multicast link
24007  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
24008  * The format of an IPoIB multicast address is:
24009  *
24010  *  4 byte QPN      Scope Sign.  Pkey
24011  * +--------------------------------------------+
24012  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
24013  * +--------------------------------------------+
24014  *
24015  * The Scope and Pkey components are properties of the IBA port and
24016  * network interface. They can be ascertained from the broadcast address.
24017  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
24018  */
24019 
24020 static boolean_t
24021 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
24022     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
24023 {
24024 	/*
24025 	 * Base IPoIB IPv6 multicast address used for mappings.
24026 	 * Does not contain the IBA scope/Pkey values.
24027 	 */
24028 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
24029 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
24030 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24031 
24032 	/*
24033 	 * Extract low order 80 bits from IPv6 multicast address.
24034 	 * Or that into the link layer address, starting from the
24035 	 * sixth byte.
24036 	 */
24037 	*hw_start = 6;
24038 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
24039 
24040 	/*
24041 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24042 	 */
24043 	*(maddr + 5) = *(bphys_addr + 5);
24044 	*(maddr + 8) = *(bphys_addr + 8);
24045 	*(maddr + 9) = *(bphys_addr + 9);
24046 
24047 	v6_extract_mask->s6_addr32[0] = 0;
24048 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
24049 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
24050 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
24051 	return (B_TRUE);
24052 }
24053 
24054 static boolean_t
24055 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
24056     uint32_t *hw_start, ipaddr_t *extract_mask)
24057 {
24058 	/*
24059 	 * Base IPoIB IPv4 multicast address used for mappings.
24060 	 * Does not contain the IBA scope/Pkey values.
24061 	 */
24062 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
24063 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
24064 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24065 
24066 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
24067 		return (B_FALSE);
24068 
24069 	/*
24070 	 * Extract low order 28 bits from IPv4 multicast address.
24071 	 * Or that into the link layer address, starting from the
24072 	 * sixteenth byte.
24073 	 */
24074 	*extract_mask = htonl(0x0fffffff);
24075 	*hw_start = 16;
24076 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
24077 
24078 	/*
24079 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
24080 	 */
24081 	*(maddr + 5) = *(bphys_addr + 5);
24082 	*(maddr + 8) = *(bphys_addr + 8);
24083 	*(maddr + 9) = *(bphys_addr + 9);
24084 	return (B_TRUE);
24085 }
24086 
24087 /*
24088  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
24089  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
24090  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
24091  * the link-local address is preferred.
24092  */
24093 boolean_t
24094 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24095 {
24096 	ipif_t	*ipif;
24097 	ipif_t	*maybe_ipif = NULL;
24098 
24099 	mutex_enter(&ill->ill_lock);
24100 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24101 		mutex_exit(&ill->ill_lock);
24102 		if (ipifp != NULL)
24103 			*ipifp = NULL;
24104 		return (B_FALSE);
24105 	}
24106 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24107 		if (!IPIF_CAN_LOOKUP(ipif))
24108 			continue;
24109 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
24110 		    ipif->ipif_zoneid != ALL_ZONES)
24111 			continue;
24112 		if ((ipif->ipif_flags & flags) != flags)
24113 			continue;
24114 
24115 		if (ipifp == NULL) {
24116 			mutex_exit(&ill->ill_lock);
24117 			ASSERT(maybe_ipif == NULL);
24118 			return (B_TRUE);
24119 		}
24120 		if (!ill->ill_isv6 ||
24121 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
24122 			ipif_refhold_locked(ipif);
24123 			mutex_exit(&ill->ill_lock);
24124 			*ipifp = ipif;
24125 			return (B_TRUE);
24126 		}
24127 		if (maybe_ipif == NULL)
24128 			maybe_ipif = ipif;
24129 	}
24130 	if (ipifp != NULL) {
24131 		if (maybe_ipif != NULL)
24132 			ipif_refhold_locked(maybe_ipif);
24133 		*ipifp = maybe_ipif;
24134 	}
24135 	mutex_exit(&ill->ill_lock);
24136 	return (maybe_ipif != NULL);
24137 }
24138 
24139 /*
24140  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
24141  */
24142 boolean_t
24143 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24144 {
24145 	ill_t *illg;
24146 
24147 	/*
24148 	 * We look at the passed-in ill first without grabbing ill_g_lock.
24149 	 */
24150 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
24151 		return (B_TRUE);
24152 	}
24153 	rw_enter(&ill_g_lock, RW_READER);
24154 	if (ill->ill_group == NULL) {
24155 		/* ill not in a group */
24156 		rw_exit(&ill_g_lock);
24157 		return (B_FALSE);
24158 	}
24159 
24160 	/*
24161 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24162 	 * group. We need to look for an ipif in the zone on all the ills in the
24163 	 * group.
24164 	 */
24165 	illg = ill->ill_group->illgrp_ill;
24166 	do {
24167 		/*
24168 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24169 		 * that it's not there.
24170 		 */
24171 		if (illg != ill &&
24172 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24173 			break;
24174 		}
24175 	} while ((illg = illg->ill_group_next) != NULL);
24176 	rw_exit(&ill_g_lock);
24177 	return (illg != NULL);
24178 }
24179 
24180 /*
24181  * Check if this ill is only being used to send ICMP probes for IPMP
24182  */
24183 boolean_t
24184 ill_is_probeonly(ill_t *ill)
24185 {
24186 	/*
24187 	 * Check if the interface is FAILED, or INACTIVE
24188 	 */
24189 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24190 		return (B_TRUE);
24191 
24192 	return (B_FALSE);
24193 }
24194 
24195 /*
24196  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24197  * If a pointer to an ipif_t is returned then the caller will need to do
24198  * an ill_refrele().
24199  */
24200 ipif_t *
24201 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6)
24202 {
24203 	ipif_t *ipif;
24204 	ill_t *ill;
24205 
24206 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL);
24207 
24208 	if (ill == NULL)
24209 		return (NULL);
24210 
24211 	mutex_enter(&ill->ill_lock);
24212 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24213 		mutex_exit(&ill->ill_lock);
24214 		ill_refrele(ill);
24215 		return (NULL);
24216 	}
24217 
24218 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24219 		if (!IPIF_CAN_LOOKUP(ipif))
24220 			continue;
24221 		if (lifidx == ipif->ipif_id) {
24222 			ipif_refhold_locked(ipif);
24223 			break;
24224 		}
24225 	}
24226 
24227 	mutex_exit(&ill->ill_lock);
24228 	ill_refrele(ill);
24229 	return (ipif);
24230 }
24231 
24232 /*
24233  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24234  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24235  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24236  * for details.
24237  */
24238 void
24239 ill_fastpath_flush(ill_t *ill)
24240 {
24241 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24242 	ndp_walk_common((ill->ill_isv6 ? &ndp6 : &ndp4), ill,
24243 	    (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24244 }
24245 
24246 /*
24247  * Set the physical address information for `ill' to the contents of the
24248  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24249  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24250  * EINPROGRESS will be returned.
24251  */
24252 int
24253 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24254 {
24255 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24256 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24257 
24258 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24259 
24260 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24261 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24262 		/* Changing DL_IPV6_TOKEN is not yet supported */
24263 		return (0);
24264 	}
24265 
24266 	/*
24267 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24268 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24269 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24270 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24271 	 */
24272 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24273 		freemsg(mp);
24274 		return (ENOMEM);
24275 	}
24276 
24277 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24278 
24279 	/*
24280 	 * If we can quiesce the ill, then set the address.  If not, then
24281 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24282 	 */
24283 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24284 	mutex_enter(&ill->ill_lock);
24285 	if (!ill_is_quiescent(ill)) {
24286 		/* call cannot fail since `conn_t *' argument is NULL */
24287 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24288 		    mp, ILL_DOWN);
24289 		mutex_exit(&ill->ill_lock);
24290 		return (EINPROGRESS);
24291 	}
24292 	mutex_exit(&ill->ill_lock);
24293 
24294 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24295 	return (0);
24296 }
24297 
24298 /*
24299  * Once the ill associated with `q' has quiesced, set its physical address
24300  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24301  * are passed (linked by b_cont), since we sometimes need to save two distinct
24302  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24303  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24304  * is quiesced, we know any stale IREs with the old address information have
24305  * already been removed, so we don't need to call ill_fastpath_flush().
24306  */
24307 /* ARGSUSED */
24308 static void
24309 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24310 {
24311 	ill_t		*ill = q->q_ptr;
24312 	mblk_t		*addrmp2 = unlinkb(addrmp);
24313 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24314 	uint_t		addrlen, addroff;
24315 
24316 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24317 	mutex_enter(&ill->ill_lock);
24318 	ASSERT(ill_is_quiescent(ill));
24319 	mutex_exit(&ill->ill_lock);
24320 
24321 	addroff	= dlindp->dl_addr_offset;
24322 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24323 
24324 	switch (dlindp->dl_data) {
24325 	case DL_IPV6_LINK_LAYER_ADDR:
24326 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24327 		freemsg(addrmp2);
24328 		break;
24329 
24330 	case DL_CURR_PHYS_ADDR:
24331 		freemsg(ill->ill_phys_addr_mp);
24332 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24333 		ill->ill_phys_addr_mp = addrmp;
24334 		ill->ill_phys_addr_length = addrlen;
24335 
24336 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24337 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24338 		else
24339 			freemsg(addrmp2);
24340 		break;
24341 	default:
24342 		ASSERT(0);
24343 	}
24344 
24345 	/*
24346 	 * If there are ipifs to bring up, ill_up_ipifs() will return nonzero,
24347 	 * and ipsq_current_finish() will be called by ip_rput_dlpi_writer()
24348 	 * or ip_arp_done() when the last ipif is brought up.
24349 	 */
24350 	if (ill_up_ipifs(ill, q, addrmp) == 0)
24351 		ipsq_current_finish(ipsq);
24352 }
24353 
24354 /*
24355  * Helper routine for setting the ill_nd_lla fields.
24356  */
24357 void
24358 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24359 {
24360 	freemsg(ill->ill_nd_lla_mp);
24361 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24362 	ill->ill_nd_lla_mp = ndmp;
24363 	ill->ill_nd_lla_len = addrlen;
24364 }
24365