xref: /titanic_44/usr/src/uts/common/inet/ip/ip_if.c (revision f9b62eacbfd4012fd23fb60123be899ada20c450)
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 2008 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 #include <sys/sunldi.h>
47 #include <sys/file.h>
48 #include <sys/bitmap.h>
49 
50 #include <sys/kmem.h>
51 #include <sys/systm.h>
52 #include <sys/param.h>
53 #include <sys/socket.h>
54 #include <sys/isa_defs.h>
55 #include <net/if.h>
56 #include <net/if_arp.h>
57 #include <net/if_types.h>
58 #include <net/if_dl.h>
59 #include <net/route.h>
60 #include <sys/sockio.h>
61 #include <netinet/in.h>
62 #include <netinet/ip6.h>
63 #include <netinet/icmp6.h>
64 #include <netinet/igmp_var.h>
65 #include <sys/strsun.h>
66 #include <sys/policy.h>
67 #include <sys/ethernet.h>
68 
69 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
70 #include <inet/mi.h>
71 #include <inet/nd.h>
72 #include <inet/arp.h>
73 #include <inet/mib2.h>
74 #include <inet/ip.h>
75 #include <inet/ip6.h>
76 #include <inet/ip6_asp.h>
77 #include <inet/tcp.h>
78 #include <inet/ip_multi.h>
79 #include <inet/ip_ire.h>
80 #include <inet/ip_ftable.h>
81 #include <inet/ip_rts.h>
82 #include <inet/ip_ndp.h>
83 #include <inet/ip_if.h>
84 #include <inet/ip_impl.h>
85 #include <inet/tun.h>
86 #include <inet/sctp_ip.h>
87 #include <inet/ip_netinfo.h>
88 #include <inet/mib2.h>
89 
90 #include <net/pfkeyv2.h>
91 #include <inet/ipsec_info.h>
92 #include <inet/sadb.h>
93 #include <inet/ipsec_impl.h>
94 #include <sys/iphada.h>
95 
96 
97 #include <netinet/igmp.h>
98 #include <inet/ip_listutils.h>
99 #include <inet/ipclassifier.h>
100 #include <sys/mac.h>
101 
102 #include <sys/systeminfo.h>
103 #include <sys/bootconf.h>
104 
105 #include <sys/tsol/tndb.h>
106 #include <sys/tsol/tnet.h>
107 
108 /* The character which tells where the ill_name ends */
109 #define	IPIF_SEPARATOR_CHAR	':'
110 
111 /* IP ioctl function table entry */
112 typedef struct ipft_s {
113 	int	ipft_cmd;
114 	pfi_t	ipft_pfi;
115 	int	ipft_min_size;
116 	int	ipft_flags;
117 } ipft_t;
118 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
119 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
120 
121 typedef struct ip_sock_ar_s {
122 	union {
123 		area_t	ip_sock_area;
124 		ared_t	ip_sock_ared;
125 		areq_t	ip_sock_areq;
126 	} ip_sock_ar_u;
127 	queue_t	*ip_sock_ar_q;
128 } ip_sock_ar_t;
129 
130 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
131 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
132 		    char *value, caddr_t cp, cred_t *ioc_cr);
133 
134 static boolean_t ill_is_quiescent(ill_t *);
135 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
136 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
137 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
138     mblk_t *mp, boolean_t need_up);
139 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
140     mblk_t *mp, boolean_t need_up);
141 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
142     queue_t *q, mblk_t *mp, boolean_t need_up);
143 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
144     mblk_t *mp, boolean_t need_up);
145 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
146     mblk_t *mp);
147 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
148     queue_t *q, mblk_t *mp, boolean_t need_up);
149 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
150     int ioccmd, struct linkblk *li, boolean_t doconsist);
151 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
152 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
153 static void	ipsq_flush(ill_t *ill);
154 
155 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
156     queue_t *q, mblk_t *mp, boolean_t need_up);
157 static void	ipsq_delete(ipsq_t *);
158 
159 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
160 		    boolean_t initialize);
161 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
162 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
163 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
164 		    boolean_t isv6);
165 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
166 static void	ipif_delete_cache_ire(ire_t *, char *);
167 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
168 static void	ipif_free(ipif_t *ipif);
169 static void	ipif_free_tail(ipif_t *ipif);
170 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
171 static void	ipif_multicast_down(ipif_t *ipif);
172 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
173 static void	ipif_set_default(ipif_t *ipif);
174 static int	ipif_set_values(queue_t *q, mblk_t *mp,
175     char *interf_name, uint_t *ppa);
176 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
177     queue_t *q);
178 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
179     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
180     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
181 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
182 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
183 
184 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
185 static int	ill_arp_off(ill_t *ill);
186 static int	ill_arp_on(ill_t *ill);
187 static void	ill_delete_interface_type(ill_if_t *);
188 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
189 static void	ill_dl_down(ill_t *ill);
190 static void	ill_down(ill_t *ill);
191 static void	ill_downi(ire_t *ire, char *ill_arg);
192 static void	ill_free_mib(ill_t *ill);
193 static void	ill_glist_delete(ill_t *);
194 static boolean_t ill_has_usable_ipif(ill_t *);
195 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
196 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
197 static void	ill_phyint_free(ill_t *ill);
198 static void	ill_phyint_reinit(ill_t *ill);
199 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
200 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
201 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
202 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
203 static void	ill_stq_cache_delete(ire_t *, char *);
204 
205 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
206 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
207 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
208     in6_addr_t *);
209 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
210     ipaddr_t *);
211 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
212 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
213     in6_addr_t *);
214 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
215     ipaddr_t *);
216 
217 static void	ipif_save_ire(ipif_t *, ire_t *);
218 static void	ipif_remove_ire(ipif_t *, ire_t *);
219 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
220 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
221 
222 /*
223  * Per-ill IPsec capabilities management.
224  */
225 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
226 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
227 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
228 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
229 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
230 static void ill_capability_proto(ill_t *, int, mblk_t *);
231 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
232     boolean_t);
233 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
234 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
235 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
236 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
237 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
238 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
239 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
240 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
241     dl_capability_sub_t *);
242 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
243 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
244 static void ill_capability_lso_reset(ill_t *, mblk_t **);
245 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
246 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
247 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
248 static void	ill_capability_dls_disable(ill_t *);
249 
250 static void	illgrp_cache_delete(ire_t *, char *);
251 static void	illgrp_delete(ill_t *ill);
252 static void	illgrp_reset_schednext(ill_t *ill);
253 
254 static ill_t	*ill_prev_usesrc(ill_t *);
255 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
256 static void	ill_disband_usesrc_group(ill_t *);
257 
258 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
259 
260 #ifdef DEBUG
261 static	void	ill_trace_cleanup(const ill_t *);
262 static	void	ipif_trace_cleanup(const ipif_t *);
263 #endif
264 
265 /*
266  * if we go over the memory footprint limit more than once in this msec
267  * interval, we'll start pruning aggressively.
268  */
269 int ip_min_frag_prune_time = 0;
270 
271 /*
272  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
273  * and the IPsec DOI
274  */
275 #define	MAX_IPSEC_ALGS	256
276 
277 #define	BITSPERBYTE	8
278 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
279 
280 #define	IPSEC_ALG_ENABLE(algs, algid) \
281 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
282 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
283 
284 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
285 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
286 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
287 
288 typedef uint8_t ipsec_capab_elem_t;
289 
290 /*
291  * Per-algorithm parameters.  Note that at present, only encryption
292  * algorithms have variable keysize (IKE does not provide a way to negotiate
293  * auth algorithm keysize).
294  *
295  * All sizes here are in bits.
296  */
297 typedef struct
298 {
299 	uint16_t	minkeylen;
300 	uint16_t	maxkeylen;
301 } ipsec_capab_algparm_t;
302 
303 /*
304  * Per-ill capabilities.
305  */
306 struct ill_ipsec_capab_s {
307 	ipsec_capab_elem_t *encr_hw_algs;
308 	ipsec_capab_elem_t *auth_hw_algs;
309 	uint32_t algs_size;	/* size of _hw_algs in bytes */
310 	/* algorithm key lengths */
311 	ipsec_capab_algparm_t *encr_algparm;
312 	uint32_t encr_algparm_size;
313 	uint32_t encr_algparm_end;
314 };
315 
316 /*
317  * The field values are larger than strictly necessary for simple
318  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
319  */
320 static area_t	ip_area_template = {
321 	AR_ENTRY_ADD,			/* area_cmd */
322 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
323 					/* area_name_offset */
324 	/* area_name_length temporarily holds this structure length */
325 	sizeof (area_t),			/* area_name_length */
326 	IP_ARP_PROTO_TYPE,		/* area_proto */
327 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
328 	IP_ADDR_LEN,			/* area_proto_addr_length */
329 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
330 					/* area_proto_mask_offset */
331 	0,				/* area_flags */
332 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
333 					/* area_hw_addr_offset */
334 	/* Zero length hw_addr_length means 'use your idea of the address' */
335 	0				/* area_hw_addr_length */
336 };
337 
338 /*
339  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
340  * support
341  */
342 static area_t	ip6_area_template = {
343 	AR_ENTRY_ADD,			/* area_cmd */
344 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
345 					/* area_name_offset */
346 	/* area_name_length temporarily holds this structure length */
347 	sizeof (area_t),			/* area_name_length */
348 	IP_ARP_PROTO_TYPE,		/* area_proto */
349 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
350 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
351 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
352 					/* area_proto_mask_offset */
353 	0,				/* area_flags */
354 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
355 					/* area_hw_addr_offset */
356 	/* Zero length hw_addr_length means 'use your idea of the address' */
357 	0				/* area_hw_addr_length */
358 };
359 
360 static ared_t	ip_ared_template = {
361 	AR_ENTRY_DELETE,
362 	sizeof (ared_t) + IP_ADDR_LEN,
363 	sizeof (ared_t),
364 	IP_ARP_PROTO_TYPE,
365 	sizeof (ared_t),
366 	IP_ADDR_LEN
367 };
368 
369 static ared_t	ip6_ared_template = {
370 	AR_ENTRY_DELETE,
371 	sizeof (ared_t) + IPV6_ADDR_LEN,
372 	sizeof (ared_t),
373 	IP_ARP_PROTO_TYPE,
374 	sizeof (ared_t),
375 	IPV6_ADDR_LEN
376 };
377 
378 /*
379  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
380  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
381  * areq is used).
382  */
383 static areq_t	ip_areq_template = {
384 	AR_ENTRY_QUERY,			/* cmd */
385 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
386 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
387 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
388 	sizeof (areq_t),			/* target addr offset */
389 	IP_ADDR_LEN,			/* target addr_length */
390 	0,				/* flags */
391 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
392 	IP_ADDR_LEN,			/* sender addr length */
393 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
394 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
395 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
396 	/* anything else filled in by the code */
397 };
398 
399 static arc_t	ip_aru_template = {
400 	AR_INTERFACE_UP,
401 	sizeof (arc_t),		/* Name offset */
402 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
403 };
404 
405 static arc_t	ip_ard_template = {
406 	AR_INTERFACE_DOWN,
407 	sizeof (arc_t),		/* Name offset */
408 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
409 };
410 
411 static arc_t	ip_aron_template = {
412 	AR_INTERFACE_ON,
413 	sizeof (arc_t),		/* Name offset */
414 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
415 };
416 
417 static arc_t	ip_aroff_template = {
418 	AR_INTERFACE_OFF,
419 	sizeof (arc_t),		/* Name offset */
420 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
421 };
422 
423 
424 static arma_t	ip_arma_multi_template = {
425 	AR_MAPPING_ADD,
426 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
427 				/* Name offset */
428 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
429 	IP_ARP_PROTO_TYPE,
430 	sizeof (arma_t),			/* proto_addr_offset */
431 	IP_ADDR_LEN,				/* proto_addr_length */
432 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
433 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
434 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
435 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
436 	IP_MAX_HW_LEN,				/* hw_addr_length */
437 	0,					/* hw_mapping_start */
438 };
439 
440 static ipft_t	ip_ioctl_ftbl[] = {
441 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
442 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
443 		IPFT_F_NO_REPLY },
444 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
445 		IPFT_F_NO_REPLY },
446 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
447 	{ 0 }
448 };
449 
450 /* Simple ICMP IP Header Template */
451 static ipha_t icmp_ipha = {
452 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
453 };
454 
455 /* Flag descriptors for ip_ipif_report */
456 static nv_t	ipif_nv_tbl[] = {
457 	{ IPIF_UP,		"UP" },
458 	{ IPIF_BROADCAST,	"BROADCAST" },
459 	{ ILLF_DEBUG,		"DEBUG" },
460 	{ PHYI_LOOPBACK,	"LOOPBACK" },
461 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
462 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
463 	{ PHYI_RUNNING,		"RUNNING" },
464 	{ ILLF_NOARP,		"NOARP" },
465 	{ PHYI_PROMISC,		"PROMISC" },
466 	{ PHYI_ALLMULTI,	"ALLMULTI" },
467 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
468 	{ ILLF_MULTICAST,	"MULTICAST" },
469 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
470 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
471 	{ IPIF_DHCPRUNNING,	"DHCP" },
472 	{ IPIF_PRIVATE,		"PRIVATE" },
473 	{ IPIF_NOXMIT,		"NOXMIT" },
474 	{ IPIF_NOLOCAL,		"NOLOCAL" },
475 	{ IPIF_DEPRECATED,	"DEPRECATED" },
476 	{ IPIF_PREFERRED,	"PREFERRED" },
477 	{ IPIF_TEMPORARY,	"TEMPORARY" },
478 	{ IPIF_ADDRCONF,	"ADDRCONF" },
479 	{ PHYI_VIRTUAL,		"VIRTUAL" },
480 	{ ILLF_ROUTER,		"ROUTER" },
481 	{ ILLF_NONUD,		"NONUD" },
482 	{ IPIF_ANYCAST,		"ANYCAST" },
483 	{ ILLF_NORTEXCH,	"NORTEXCH" },
484 	{ ILLF_IPV4,		"IPV4" },
485 	{ ILLF_IPV6,		"IPV6" },
486 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
487 	{ PHYI_FAILED,		"FAILED" },
488 	{ PHYI_STANDBY,		"STANDBY" },
489 	{ PHYI_INACTIVE,	"INACTIVE" },
490 	{ PHYI_OFFLINE,		"OFFLINE" },
491 };
492 
493 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
494 
495 static ip_m_t	ip_m_tbl[] = {
496 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
497 	    ip_ether_v6intfid },
498 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
499 	    ip_nodef_v6intfid },
500 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
501 	    ip_nodef_v6intfid },
502 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
503 	    ip_nodef_v6intfid },
504 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
505 	    ip_ether_v6intfid },
506 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
507 	    ip_ib_v6intfid },
508 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
509 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
510 	    ip_nodef_v6intfid }
511 };
512 
513 static ill_t	ill_null;		/* Empty ILL for init. */
514 char	ipif_loopback_name[] = "lo0";
515 static char *ipv4_forward_suffix = ":ip_forwarding";
516 static char *ipv6_forward_suffix = ":ip6_forwarding";
517 static	sin6_t	sin6_null;	/* Zero address for quick clears */
518 static	sin_t	sin_null;	/* Zero address for quick clears */
519 
520 /* When set search for unused ipif_seqid */
521 static ipif_t	ipif_zero;
522 
523 /*
524  * ppa arena is created after these many
525  * interfaces have been plumbed.
526  */
527 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
528 
529 /*
530  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
531  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
532  * set through platform specific code (Niagara/Ontario).
533  */
534 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
535 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
536 
537 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
538 
539 static uint_t
540 ipif_rand(ip_stack_t *ipst)
541 {
542 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
543 	    12345;
544 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
545 }
546 
547 /*
548  * Allocate per-interface mibs.
549  * Returns true if ok. False otherwise.
550  *  ipsq  may not yet be allocated (loopback case ).
551  */
552 static boolean_t
553 ill_allocate_mibs(ill_t *ill)
554 {
555 	/* Already allocated? */
556 	if (ill->ill_ip_mib != NULL) {
557 		if (ill->ill_isv6)
558 			ASSERT(ill->ill_icmp6_mib != NULL);
559 		return (B_TRUE);
560 	}
561 
562 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
563 	    KM_NOSLEEP);
564 	if (ill->ill_ip_mib == NULL) {
565 		return (B_FALSE);
566 	}
567 
568 	/* Setup static information */
569 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
570 	    sizeof (mib2_ipIfStatsEntry_t));
571 	if (ill->ill_isv6) {
572 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
573 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
574 		    sizeof (mib2_ipv6AddrEntry_t));
575 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
576 		    sizeof (mib2_ipv6RouteEntry_t));
577 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
578 		    sizeof (mib2_ipv6NetToMediaEntry_t));
579 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
580 		    sizeof (ipv6_member_t));
581 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
582 		    sizeof (ipv6_grpsrc_t));
583 	} else {
584 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
585 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
586 		    sizeof (mib2_ipAddrEntry_t));
587 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
588 		    sizeof (mib2_ipRouteEntry_t));
589 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
590 		    sizeof (mib2_ipNetToMediaEntry_t));
591 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
592 		    sizeof (ip_member_t));
593 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
594 		    sizeof (ip_grpsrc_t));
595 
596 		/*
597 		 * For a v4 ill, we are done at this point, because per ill
598 		 * icmp mibs are only used for v6.
599 		 */
600 		return (B_TRUE);
601 	}
602 
603 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
604 	    KM_NOSLEEP);
605 	if (ill->ill_icmp6_mib == NULL) {
606 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
607 		ill->ill_ip_mib = NULL;
608 		return (B_FALSE);
609 	}
610 	/* static icmp info */
611 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
612 	    sizeof (mib2_ipv6IfIcmpEntry_t);
613 	/*
614 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
615 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
616 	 * -> ill_phyint_reinit
617 	 */
618 	return (B_TRUE);
619 }
620 
621 /*
622  * Common code for preparation of ARP commands.  Two points to remember:
623  * 	1) The ill_name is tacked on at the end of the allocated space so
624  *	   the templates name_offset field must contain the total space
625  *	   to allocate less the name length.
626  *
627  *	2) The templates name_length field should contain the *template*
628  *	   length.  We use it as a parameter to bcopy() and then write
629  *	   the real ill_name_length into the name_length field of the copy.
630  * (Always called as writer.)
631  */
632 mblk_t *
633 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
634 {
635 	arc_t	*arc = (arc_t *)template;
636 	char	*cp;
637 	int	len;
638 	mblk_t	*mp;
639 	uint_t	name_length = ill->ill_name_length;
640 	uint_t	template_len = arc->arc_name_length;
641 
642 	len = arc->arc_name_offset + name_length;
643 	mp = allocb(len, BPRI_HI);
644 	if (mp == NULL)
645 		return (NULL);
646 	cp = (char *)mp->b_rptr;
647 	mp->b_wptr = (uchar_t *)&cp[len];
648 	if (template_len)
649 		bcopy(template, cp, template_len);
650 	if (len > template_len)
651 		bzero(&cp[template_len], len - template_len);
652 	mp->b_datap->db_type = M_PROTO;
653 
654 	arc = (arc_t *)cp;
655 	arc->arc_name_length = name_length;
656 	cp = (char *)arc + arc->arc_name_offset;
657 	bcopy(ill->ill_name, cp, name_length);
658 
659 	if (addr) {
660 		area_t	*area = (area_t *)mp->b_rptr;
661 
662 		cp = (char *)area + area->area_proto_addr_offset;
663 		bcopy(addr, cp, area->area_proto_addr_length);
664 		if (area->area_cmd == AR_ENTRY_ADD) {
665 			cp = (char *)area;
666 			len = area->area_proto_addr_length;
667 			if (area->area_proto_mask_offset)
668 				cp += area->area_proto_mask_offset;
669 			else
670 				cp += area->area_proto_addr_offset + len;
671 			while (len-- > 0)
672 				*cp++ = (char)~0;
673 		}
674 	}
675 	return (mp);
676 }
677 
678 mblk_t *
679 ipif_area_alloc(ipif_t *ipif)
680 {
681 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
682 	    (char *)&ipif->ipif_lcl_addr));
683 }
684 
685 mblk_t *
686 ipif_ared_alloc(ipif_t *ipif)
687 {
688 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
689 	    (char *)&ipif->ipif_lcl_addr));
690 }
691 
692 mblk_t *
693 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
694 {
695 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
696 	    (char *)&addr));
697 }
698 
699 /*
700  * Completely vaporize a lower level tap and all associated interfaces.
701  * ill_delete is called only out of ip_close when the device control
702  * stream is being closed.
703  */
704 void
705 ill_delete(ill_t *ill)
706 {
707 	ipif_t	*ipif;
708 	ill_t	*prev_ill;
709 	ip_stack_t	*ipst = ill->ill_ipst;
710 
711 	/*
712 	 * ill_delete may be forcibly entering the ipsq. The previous
713 	 * ioctl may not have completed and may need to be aborted.
714 	 * ipsq_flush takes care of it. If we don't need to enter the
715 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
716 	 * ill_delete_tail is sufficient.
717 	 */
718 	ipsq_flush(ill);
719 
720 	/*
721 	 * Nuke all interfaces.  ipif_free will take down the interface,
722 	 * remove it from the list, and free the data structure.
723 	 * Walk down the ipif list and remove the logical interfaces
724 	 * first before removing the main ipif. We can't unplumb
725 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
726 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
727 	 * POINTOPOINT.
728 	 *
729 	 * If ill_ipif was not properly initialized (i.e low on memory),
730 	 * then no interfaces to clean up. In this case just clean up the
731 	 * ill.
732 	 */
733 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
734 		ipif_free(ipif);
735 
736 	/*
737 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
738 	 * So nobody can be using this mp now. Free the mp allocated for
739 	 * honoring ILLF_NOARP
740 	 */
741 	freemsg(ill->ill_arp_on_mp);
742 	ill->ill_arp_on_mp = NULL;
743 
744 	/* Clean up msgs on pending upcalls for mrouted */
745 	reset_mrt_ill(ill);
746 
747 	/*
748 	 * ipif_free -> reset_conn_ipif will remove all multicast
749 	 * references for IPv4. For IPv6, we need to do it here as
750 	 * it points only at ills.
751 	 */
752 	reset_conn_ill(ill);
753 
754 	/*
755 	 * ill_down will arrange to blow off any IRE's dependent on this
756 	 * ILL, and shut down fragmentation reassembly.
757 	 */
758 	ill_down(ill);
759 
760 	/* Let SCTP know, so that it can remove this from its list. */
761 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
762 
763 	/*
764 	 * If an address on this ILL is being used as a source address then
765 	 * clear out the pointers in other ILLs that point to this ILL.
766 	 */
767 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
768 	if (ill->ill_usesrc_grp_next != NULL) {
769 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
770 			ill_disband_usesrc_group(ill);
771 		} else {	/* consumer of the usesrc ILL */
772 			prev_ill = ill_prev_usesrc(ill);
773 			prev_ill->ill_usesrc_grp_next =
774 			    ill->ill_usesrc_grp_next;
775 		}
776 	}
777 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
778 }
779 
780 static void
781 ipif_non_duplicate(ipif_t *ipif)
782 {
783 	ill_t *ill = ipif->ipif_ill;
784 	mutex_enter(&ill->ill_lock);
785 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
786 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
787 		ASSERT(ill->ill_ipif_dup_count > 0);
788 		ill->ill_ipif_dup_count--;
789 	}
790 	mutex_exit(&ill->ill_lock);
791 }
792 
793 /*
794  * ill_delete_tail is called from ip_modclose after all references
795  * to the closing ill are gone. The wait is done in ip_modclose
796  */
797 void
798 ill_delete_tail(ill_t *ill)
799 {
800 	mblk_t	**mpp;
801 	ipif_t	*ipif;
802 	ip_stack_t	*ipst = ill->ill_ipst;
803 
804 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
805 		ipif_non_duplicate(ipif);
806 		ipif_down_tail(ipif);
807 	}
808 
809 	ASSERT(ill->ill_ipif_dup_count == 0 &&
810 	    ill->ill_arp_down_mp == NULL &&
811 	    ill->ill_arp_del_mapping_mp == NULL);
812 
813 	/*
814 	 * If polling capability is enabled (which signifies direct
815 	 * upcall into IP and driver has ill saved as a handle),
816 	 * we need to make sure that unbind has completed before we
817 	 * let the ill disappear and driver no longer has any reference
818 	 * to this ill.
819 	 */
820 	mutex_enter(&ill->ill_lock);
821 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
822 		cv_wait(&ill->ill_cv, &ill->ill_lock);
823 	mutex_exit(&ill->ill_lock);
824 
825 	/*
826 	 * Clean up polling and soft ring capabilities
827 	 */
828 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
829 		ill_capability_dls_disable(ill);
830 
831 	if (ill->ill_net_type != IRE_LOOPBACK)
832 		qprocsoff(ill->ill_rq);
833 
834 	/*
835 	 * We do an ipsq_flush once again now. New messages could have
836 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
837 	 * could also have landed up if an ioctl thread had looked up
838 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
839 	 * enqueued the ioctl when we did the ipsq_flush last time.
840 	 */
841 	ipsq_flush(ill);
842 
843 	/*
844 	 * Free capabilities.
845 	 */
846 	if (ill->ill_ipsec_capab_ah != NULL) {
847 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
848 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
849 		ill->ill_ipsec_capab_ah = NULL;
850 	}
851 
852 	if (ill->ill_ipsec_capab_esp != NULL) {
853 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
854 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
855 		ill->ill_ipsec_capab_esp = NULL;
856 	}
857 
858 	if (ill->ill_mdt_capab != NULL) {
859 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
860 		ill->ill_mdt_capab = NULL;
861 	}
862 
863 	if (ill->ill_hcksum_capab != NULL) {
864 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
865 		ill->ill_hcksum_capab = NULL;
866 	}
867 
868 	if (ill->ill_zerocopy_capab != NULL) {
869 		kmem_free(ill->ill_zerocopy_capab,
870 		    sizeof (ill_zerocopy_capab_t));
871 		ill->ill_zerocopy_capab = NULL;
872 	}
873 
874 	if (ill->ill_lso_capab != NULL) {
875 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
876 		ill->ill_lso_capab = NULL;
877 	}
878 
879 	if (ill->ill_dls_capab != NULL) {
880 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
881 		ill->ill_dls_capab->ill_unbind_conn = NULL;
882 		kmem_free(ill->ill_dls_capab,
883 		    sizeof (ill_dls_capab_t) +
884 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
885 		ill->ill_dls_capab = NULL;
886 	}
887 
888 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
889 
890 	while (ill->ill_ipif != NULL)
891 		ipif_free_tail(ill->ill_ipif);
892 
893 	/*
894 	 * We have removed all references to ilm from conn and the ones joined
895 	 * within the kernel.
896 	 *
897 	 * We don't walk conns, mrts and ires because
898 	 *
899 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
900 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
901 	 *    ill references.
902 	 */
903 	ASSERT(ilm_walk_ill(ill) == 0);
904 	/*
905 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
906 	 * could free the phyint. No more reference to the phyint after this
907 	 * point.
908 	 */
909 	(void) ill_glist_delete(ill);
910 
911 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
912 	if (ill->ill_ndd_name != NULL)
913 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
914 	rw_exit(&ipst->ips_ip_g_nd_lock);
915 
916 
917 	if (ill->ill_frag_ptr != NULL) {
918 		uint_t count;
919 
920 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
921 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
922 		}
923 		mi_free(ill->ill_frag_ptr);
924 		ill->ill_frag_ptr = NULL;
925 		ill->ill_frag_hash_tbl = NULL;
926 	}
927 
928 	freemsg(ill->ill_nd_lla_mp);
929 	/* Free all retained control messages. */
930 	mpp = &ill->ill_first_mp_to_free;
931 	do {
932 		while (mpp[0]) {
933 			mblk_t  *mp;
934 			mblk_t  *mp1;
935 
936 			mp = mpp[0];
937 			mpp[0] = mp->b_next;
938 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
939 				mp1->b_next = NULL;
940 				mp1->b_prev = NULL;
941 			}
942 			freemsg(mp);
943 		}
944 	} while (mpp++ != &ill->ill_last_mp_to_free);
945 
946 	ill_free_mib(ill);
947 
948 #ifdef DEBUG
949 	ill_trace_cleanup(ill);
950 #endif
951 
952 	/* Drop refcnt here */
953 	netstack_rele(ill->ill_ipst->ips_netstack);
954 	ill->ill_ipst = NULL;
955 }
956 
957 static void
958 ill_free_mib(ill_t *ill)
959 {
960 	ip_stack_t *ipst = ill->ill_ipst;
961 
962 	/*
963 	 * MIB statistics must not be lost, so when an interface
964 	 * goes away the counter values will be added to the global
965 	 * MIBs.
966 	 */
967 	if (ill->ill_ip_mib != NULL) {
968 		if (ill->ill_isv6) {
969 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
970 			    ill->ill_ip_mib);
971 		} else {
972 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
973 			    ill->ill_ip_mib);
974 		}
975 
976 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
977 		ill->ill_ip_mib = NULL;
978 	}
979 	if (ill->ill_icmp6_mib != NULL) {
980 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
981 		    ill->ill_icmp6_mib);
982 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
983 		ill->ill_icmp6_mib = NULL;
984 	}
985 }
986 
987 /*
988  * Concatenate together a physical address and a sap.
989  *
990  * Sap_lengths are interpreted as follows:
991  *   sap_length == 0	==>	no sap
992  *   sap_length > 0	==>	sap is at the head of the dlpi address
993  *   sap_length < 0	==>	sap is at the tail of the dlpi address
994  */
995 static void
996 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
997     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
998 {
999 	uint16_t sap_addr = (uint16_t)sap_src;
1000 
1001 	if (sap_length == 0) {
1002 		if (phys_src == NULL)
1003 			bzero(dst, phys_length);
1004 		else
1005 			bcopy(phys_src, dst, phys_length);
1006 	} else if (sap_length < 0) {
1007 		if (phys_src == NULL)
1008 			bzero(dst, phys_length);
1009 		else
1010 			bcopy(phys_src, dst, phys_length);
1011 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1012 	} else {
1013 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1014 		if (phys_src == NULL)
1015 			bzero((char *)dst + sap_length, phys_length);
1016 		else
1017 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1018 	}
1019 }
1020 
1021 /*
1022  * Generate a dl_unitdata_req mblk for the device and address given.
1023  * addr_length is the length of the physical portion of the address.
1024  * If addr is NULL include an all zero address of the specified length.
1025  * TRUE? In any case, addr_length is taken to be the entire length of the
1026  * dlpi address, including the absolute value of sap_length.
1027  */
1028 mblk_t *
1029 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1030 		t_scalar_t sap_length)
1031 {
1032 	dl_unitdata_req_t *dlur;
1033 	mblk_t	*mp;
1034 	t_scalar_t	abs_sap_length;		/* absolute value */
1035 
1036 	abs_sap_length = ABS(sap_length);
1037 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1038 	    DL_UNITDATA_REQ);
1039 	if (mp == NULL)
1040 		return (NULL);
1041 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1042 	/* HACK: accomodate incompatible DLPI drivers */
1043 	if (addr_length == 8)
1044 		addr_length = 6;
1045 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1046 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1047 	dlur->dl_priority.dl_min = 0;
1048 	dlur->dl_priority.dl_max = 0;
1049 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1050 	    (uchar_t *)&dlur[1]);
1051 	return (mp);
1052 }
1053 
1054 /*
1055  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1056  * Return an error if we already have 1 or more ioctls in progress.
1057  * This is used only for non-exclusive ioctls. Currently this is used
1058  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1059  * and thus need to use ipsq_pending_mp_add.
1060  */
1061 boolean_t
1062 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1063 {
1064 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1065 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1066 	/*
1067 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1068 	 */
1069 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1070 	    (add_mp->b_datap->db_type == M_IOCTL));
1071 
1072 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1073 	/*
1074 	 * Return error if the conn has started closing. The conn
1075 	 * could have finished cleaning up the pending mp list,
1076 	 * If so we should not add another mp to the list negating
1077 	 * the cleanup.
1078 	 */
1079 	if (connp->conn_state_flags & CONN_CLOSING)
1080 		return (B_FALSE);
1081 	/*
1082 	 * Add the pending mp to the head of the list, chained by b_next.
1083 	 * Note down the conn on which the ioctl request came, in b_prev.
1084 	 * This will be used to later get the conn, when we get a response
1085 	 * on the ill queue, from some other module (typically arp)
1086 	 */
1087 	add_mp->b_next = (void *)ill->ill_pending_mp;
1088 	add_mp->b_queue = CONNP_TO_WQ(connp);
1089 	ill->ill_pending_mp = add_mp;
1090 	if (connp != NULL)
1091 		connp->conn_oper_pending_ill = ill;
1092 	return (B_TRUE);
1093 }
1094 
1095 /*
1096  * Retrieve the ill_pending_mp and return it. We have to walk the list
1097  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1098  */
1099 mblk_t *
1100 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1101 {
1102 	mblk_t	*prev = NULL;
1103 	mblk_t	*curr = NULL;
1104 	uint_t	id;
1105 	conn_t	*connp;
1106 
1107 	/*
1108 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1109 	 * up the pending mp, but it does not know the ioc_id and
1110 	 * passes in a zero for it.
1111 	 */
1112 	mutex_enter(&ill->ill_lock);
1113 	if (ioc_id != 0)
1114 		*connpp = NULL;
1115 
1116 	/* Search the list for the appropriate ioctl based on ioc_id */
1117 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1118 	    prev = curr, curr = curr->b_next) {
1119 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1120 		connp = Q_TO_CONN(curr->b_queue);
1121 		/* Match based on the ioc_id or based on the conn */
1122 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1123 			break;
1124 	}
1125 
1126 	if (curr != NULL) {
1127 		/* Unlink the mblk from the pending mp list */
1128 		if (prev != NULL) {
1129 			prev->b_next = curr->b_next;
1130 		} else {
1131 			ASSERT(ill->ill_pending_mp == curr);
1132 			ill->ill_pending_mp = curr->b_next;
1133 		}
1134 
1135 		/*
1136 		 * conn refcnt must have been bumped up at the start of
1137 		 * the ioctl. So we can safely access the conn.
1138 		 */
1139 		ASSERT(CONN_Q(curr->b_queue));
1140 		*connpp = Q_TO_CONN(curr->b_queue);
1141 		curr->b_next = NULL;
1142 		curr->b_queue = NULL;
1143 	}
1144 
1145 	mutex_exit(&ill->ill_lock);
1146 
1147 	return (curr);
1148 }
1149 
1150 /*
1151  * Add the pending mp to the list. There can be only 1 pending mp
1152  * in the list. Any exclusive ioctl that needs to wait for a response
1153  * from another module or driver needs to use this function to set
1154  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1155  * the other module/driver. This is also used while waiting for the
1156  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1157  */
1158 boolean_t
1159 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1160     int waitfor)
1161 {
1162 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1163 
1164 	ASSERT(IAM_WRITER_IPIF(ipif));
1165 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1166 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1167 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1168 	/*
1169 	 * The caller may be using a different ipif than the one passed into
1170 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1171 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1172 	 * that `ipsq_current_ipif == ipif'.
1173 	 */
1174 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1175 
1176 	/*
1177 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1178 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1179 	 */
1180 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1181 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1182 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1183 
1184 	if (connp != NULL) {
1185 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1186 		/*
1187 		 * Return error if the conn has started closing. The conn
1188 		 * could have finished cleaning up the pending mp list,
1189 		 * If so we should not add another mp to the list negating
1190 		 * the cleanup.
1191 		 */
1192 		if (connp->conn_state_flags & CONN_CLOSING)
1193 			return (B_FALSE);
1194 	}
1195 	mutex_enter(&ipsq->ipsq_lock);
1196 	ipsq->ipsq_pending_ipif = ipif;
1197 	/*
1198 	 * Note down the queue in b_queue. This will be returned by
1199 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1200 	 * the processing
1201 	 */
1202 	add_mp->b_next = NULL;
1203 	add_mp->b_queue = q;
1204 	ipsq->ipsq_pending_mp = add_mp;
1205 	ipsq->ipsq_waitfor = waitfor;
1206 
1207 	if (connp != NULL)
1208 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1209 	mutex_exit(&ipsq->ipsq_lock);
1210 	return (B_TRUE);
1211 }
1212 
1213 /*
1214  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1215  * queued in the list.
1216  */
1217 mblk_t *
1218 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1219 {
1220 	mblk_t	*curr = NULL;
1221 
1222 	mutex_enter(&ipsq->ipsq_lock);
1223 	*connpp = NULL;
1224 	if (ipsq->ipsq_pending_mp == NULL) {
1225 		mutex_exit(&ipsq->ipsq_lock);
1226 		return (NULL);
1227 	}
1228 
1229 	/* There can be only 1 such excl message */
1230 	curr = ipsq->ipsq_pending_mp;
1231 	ASSERT(curr != NULL && curr->b_next == NULL);
1232 	ipsq->ipsq_pending_ipif = NULL;
1233 	ipsq->ipsq_pending_mp = NULL;
1234 	ipsq->ipsq_waitfor = 0;
1235 	mutex_exit(&ipsq->ipsq_lock);
1236 
1237 	if (CONN_Q(curr->b_queue)) {
1238 		/*
1239 		 * This mp did a refhold on the conn, at the start of the ioctl.
1240 		 * So we can safely return a pointer to the conn to the caller.
1241 		 */
1242 		*connpp = Q_TO_CONN(curr->b_queue);
1243 	} else {
1244 		*connpp = NULL;
1245 	}
1246 	curr->b_next = NULL;
1247 	curr->b_prev = NULL;
1248 	return (curr);
1249 }
1250 
1251 /*
1252  * Cleanup the ioctl mp queued in ipsq_pending_mp
1253  * - Called in the ill_delete path
1254  * - Called in the M_ERROR or M_HANGUP path on the ill.
1255  * - Called in the conn close path.
1256  */
1257 boolean_t
1258 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1259 {
1260 	mblk_t	*mp;
1261 	ipsq_t	*ipsq;
1262 	queue_t	*q;
1263 	ipif_t	*ipif;
1264 
1265 	ASSERT(IAM_WRITER_ILL(ill));
1266 	ipsq = ill->ill_phyint->phyint_ipsq;
1267 	mutex_enter(&ipsq->ipsq_lock);
1268 	/*
1269 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1270 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1271 	 * even if it is meant for another ill, since we have to enqueue
1272 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1273 	 * If connp is non-null we are called from the conn close path.
1274 	 */
1275 	mp = ipsq->ipsq_pending_mp;
1276 	if (mp == NULL || (connp != NULL &&
1277 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1278 		mutex_exit(&ipsq->ipsq_lock);
1279 		return (B_FALSE);
1280 	}
1281 	/* Now remove from the ipsq_pending_mp */
1282 	ipsq->ipsq_pending_mp = NULL;
1283 	q = mp->b_queue;
1284 	mp->b_next = NULL;
1285 	mp->b_prev = NULL;
1286 	mp->b_queue = NULL;
1287 
1288 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1289 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1290 	if (ill->ill_move_in_progress) {
1291 		ILL_CLEAR_MOVE(ill);
1292 	} else if (ill->ill_up_ipifs) {
1293 		ill_group_cleanup(ill);
1294 	}
1295 
1296 	ipif = ipsq->ipsq_pending_ipif;
1297 	ipsq->ipsq_pending_ipif = NULL;
1298 	ipsq->ipsq_waitfor = 0;
1299 	ipsq->ipsq_current_ipif = NULL;
1300 	ipsq->ipsq_current_ioctl = 0;
1301 	ipsq->ipsq_current_done = B_TRUE;
1302 	mutex_exit(&ipsq->ipsq_lock);
1303 
1304 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1305 		if (connp == NULL) {
1306 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1307 		} else {
1308 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1309 			mutex_enter(&ipif->ipif_ill->ill_lock);
1310 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1311 			mutex_exit(&ipif->ipif_ill->ill_lock);
1312 		}
1313 	} else {
1314 		/*
1315 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1316 		 * be just inet_freemsg. we have to restart it
1317 		 * otherwise the thread will be stuck.
1318 		 */
1319 		inet_freemsg(mp);
1320 	}
1321 	return (B_TRUE);
1322 }
1323 
1324 /*
1325  * The ill is closing. Cleanup all the pending mps. Called exclusively
1326  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1327  * knows this ill, and hence nobody can add an mp to this list
1328  */
1329 static void
1330 ill_pending_mp_cleanup(ill_t *ill)
1331 {
1332 	mblk_t	*mp;
1333 	queue_t	*q;
1334 
1335 	ASSERT(IAM_WRITER_ILL(ill));
1336 
1337 	mutex_enter(&ill->ill_lock);
1338 	/*
1339 	 * Every mp on the pending mp list originating from an ioctl
1340 	 * added 1 to the conn refcnt, at the start of the ioctl.
1341 	 * So bump it down now.  See comments in ip_wput_nondata()
1342 	 */
1343 	while (ill->ill_pending_mp != NULL) {
1344 		mp = ill->ill_pending_mp;
1345 		ill->ill_pending_mp = mp->b_next;
1346 		mutex_exit(&ill->ill_lock);
1347 
1348 		q = mp->b_queue;
1349 		ASSERT(CONN_Q(q));
1350 		mp->b_next = NULL;
1351 		mp->b_prev = NULL;
1352 		mp->b_queue = NULL;
1353 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1354 		mutex_enter(&ill->ill_lock);
1355 	}
1356 	ill->ill_pending_ipif = NULL;
1357 
1358 	mutex_exit(&ill->ill_lock);
1359 }
1360 
1361 /*
1362  * Called in the conn close path and ill delete path
1363  */
1364 static void
1365 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1366 {
1367 	ipsq_t	*ipsq;
1368 	mblk_t	*prev;
1369 	mblk_t	*curr;
1370 	mblk_t	*next;
1371 	queue_t	*q;
1372 	mblk_t	*tmp_list = NULL;
1373 
1374 	ASSERT(IAM_WRITER_ILL(ill));
1375 	if (connp != NULL)
1376 		q = CONNP_TO_WQ(connp);
1377 	else
1378 		q = ill->ill_wq;
1379 
1380 	ipsq = ill->ill_phyint->phyint_ipsq;
1381 	/*
1382 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1383 	 * In the case of ioctl from a conn, there can be only 1 mp
1384 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1385 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1386 	 * ioctls meant for this ill form conn's are not flushed. They will
1387 	 * be processed during ipsq_exit and will not find the ill and will
1388 	 * return error.
1389 	 */
1390 	mutex_enter(&ipsq->ipsq_lock);
1391 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1392 	    curr = next) {
1393 		next = curr->b_next;
1394 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1395 			/* Unlink the mblk from the pending mp list */
1396 			if (prev != NULL) {
1397 				prev->b_next = curr->b_next;
1398 			} else {
1399 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1400 				ipsq->ipsq_xopq_mphead = curr->b_next;
1401 			}
1402 			if (ipsq->ipsq_xopq_mptail == curr)
1403 				ipsq->ipsq_xopq_mptail = prev;
1404 			/*
1405 			 * Create a temporary list and release the ipsq lock
1406 			 * New elements are added to the head of the tmp_list
1407 			 */
1408 			curr->b_next = tmp_list;
1409 			tmp_list = curr;
1410 		} else {
1411 			prev = curr;
1412 		}
1413 	}
1414 	mutex_exit(&ipsq->ipsq_lock);
1415 
1416 	while (tmp_list != NULL) {
1417 		curr = tmp_list;
1418 		tmp_list = curr->b_next;
1419 		curr->b_next = NULL;
1420 		curr->b_prev = NULL;
1421 		curr->b_queue = NULL;
1422 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1423 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1424 			    CONN_CLOSE : NO_COPYOUT, NULL);
1425 		} else {
1426 			/*
1427 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1428 			 * this can't be just inet_freemsg. we have to
1429 			 * restart it otherwise the thread will be stuck.
1430 			 */
1431 			inet_freemsg(curr);
1432 		}
1433 	}
1434 }
1435 
1436 /*
1437  * This conn has started closing. Cleanup any pending ioctl from this conn.
1438  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1439  */
1440 void
1441 conn_ioctl_cleanup(conn_t *connp)
1442 {
1443 	mblk_t *curr;
1444 	ipsq_t	*ipsq;
1445 	ill_t	*ill;
1446 	boolean_t refheld;
1447 
1448 	/*
1449 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1450 	 * ioctl has not yet started, the mp is pending in the list headed by
1451 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1452 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1453 	 * is currently executing now the mp is not queued anywhere but
1454 	 * conn_oper_pending_ill is null. The conn close will wait
1455 	 * till the conn_ref drops to zero.
1456 	 */
1457 	mutex_enter(&connp->conn_lock);
1458 	ill = connp->conn_oper_pending_ill;
1459 	if (ill == NULL) {
1460 		mutex_exit(&connp->conn_lock);
1461 		return;
1462 	}
1463 
1464 	curr = ill_pending_mp_get(ill, &connp, 0);
1465 	if (curr != NULL) {
1466 		mutex_exit(&connp->conn_lock);
1467 		CONN_DEC_REF(connp);
1468 		inet_freemsg(curr);
1469 		return;
1470 	}
1471 	/*
1472 	 * We may not be able to refhold the ill if the ill/ipif
1473 	 * is changing. But we need to make sure that the ill will
1474 	 * not vanish. So we just bump up the ill_waiter count.
1475 	 */
1476 	refheld = ill_waiter_inc(ill);
1477 	mutex_exit(&connp->conn_lock);
1478 	if (refheld) {
1479 		if (ipsq_enter(ill, B_TRUE)) {
1480 			ill_waiter_dcr(ill);
1481 			/*
1482 			 * Check whether this ioctl has started and is
1483 			 * pending now in ipsq_pending_mp. If it is not
1484 			 * found there then check whether this ioctl has
1485 			 * not even started and is in the ipsq_xopq list.
1486 			 */
1487 			if (!ipsq_pending_mp_cleanup(ill, connp))
1488 				ipsq_xopq_mp_cleanup(ill, connp);
1489 			ipsq = ill->ill_phyint->phyint_ipsq;
1490 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1491 			return;
1492 		}
1493 	}
1494 
1495 	/*
1496 	 * The ill is also closing and we could not bump up the
1497 	 * ill_waiter_count or we could not enter the ipsq. Leave
1498 	 * the cleanup to ill_delete
1499 	 */
1500 	mutex_enter(&connp->conn_lock);
1501 	while (connp->conn_oper_pending_ill != NULL)
1502 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1503 	mutex_exit(&connp->conn_lock);
1504 	if (refheld)
1505 		ill_waiter_dcr(ill);
1506 }
1507 
1508 /*
1509  * ipcl_walk function for cleaning up conn_*_ill fields.
1510  */
1511 static void
1512 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1513 {
1514 	ill_t	*ill = (ill_t *)arg;
1515 	ire_t	*ire;
1516 
1517 	mutex_enter(&connp->conn_lock);
1518 	if (connp->conn_multicast_ill == ill) {
1519 		/* Revert to late binding */
1520 		connp->conn_multicast_ill = NULL;
1521 		connp->conn_orig_multicast_ifindex = 0;
1522 	}
1523 	if (connp->conn_incoming_ill == ill)
1524 		connp->conn_incoming_ill = NULL;
1525 	if (connp->conn_outgoing_ill == ill)
1526 		connp->conn_outgoing_ill = NULL;
1527 	if (connp->conn_outgoing_pill == ill)
1528 		connp->conn_outgoing_pill = NULL;
1529 	if (connp->conn_nofailover_ill == ill)
1530 		connp->conn_nofailover_ill = NULL;
1531 	if (connp->conn_dhcpinit_ill == ill) {
1532 		connp->conn_dhcpinit_ill = NULL;
1533 		ASSERT(ill->ill_dhcpinit != 0);
1534 		atomic_dec_32(&ill->ill_dhcpinit);
1535 	}
1536 	if (connp->conn_ire_cache != NULL) {
1537 		ire = connp->conn_ire_cache;
1538 		/*
1539 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1540 		 * interface X and ipif coming from interface Y, if interface
1541 		 * X and Y are part of the same IPMPgroup. Thus whenever
1542 		 * interface X goes down, remove all references to it by
1543 		 * checking both on ire_ipif and ire_stq.
1544 		 */
1545 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1546 		    (ire->ire_type == IRE_CACHE &&
1547 		    ire->ire_stq == ill->ill_wq)) {
1548 			connp->conn_ire_cache = NULL;
1549 			mutex_exit(&connp->conn_lock);
1550 			ire_refrele_notr(ire);
1551 			return;
1552 		}
1553 	}
1554 	mutex_exit(&connp->conn_lock);
1555 
1556 }
1557 
1558 /* ARGSUSED */
1559 void
1560 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1561 {
1562 	ill_t	*ill = q->q_ptr;
1563 	ipif_t	*ipif;
1564 
1565 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1566 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1567 		ipif_non_duplicate(ipif);
1568 		ipif_down_tail(ipif);
1569 	}
1570 	freemsg(mp);
1571 	ipsq_current_finish(ipsq);
1572 }
1573 
1574 /*
1575  * ill_down_start is called when we want to down this ill and bring it up again
1576  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1577  * all interfaces, but don't tear down any plumbing.
1578  */
1579 boolean_t
1580 ill_down_start(queue_t *q, mblk_t *mp)
1581 {
1582 	ill_t	*ill = q->q_ptr;
1583 	ipif_t	*ipif;
1584 
1585 	ASSERT(IAM_WRITER_ILL(ill));
1586 
1587 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1588 		(void) ipif_down(ipif, NULL, NULL);
1589 
1590 	ill_down(ill);
1591 
1592 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1593 
1594 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1595 
1596 	/*
1597 	 * Atomically test and add the pending mp if references are active.
1598 	 */
1599 	mutex_enter(&ill->ill_lock);
1600 	if (!ill_is_quiescent(ill)) {
1601 		/* call cannot fail since `conn_t *' argument is NULL */
1602 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1603 		    mp, ILL_DOWN);
1604 		mutex_exit(&ill->ill_lock);
1605 		return (B_FALSE);
1606 	}
1607 	mutex_exit(&ill->ill_lock);
1608 	return (B_TRUE);
1609 }
1610 
1611 static void
1612 ill_down(ill_t *ill)
1613 {
1614 	ip_stack_t	*ipst = ill->ill_ipst;
1615 
1616 	/* Blow off any IREs dependent on this ILL. */
1617 	ire_walk(ill_downi, (char *)ill, ipst);
1618 
1619 	/* Remove any conn_*_ill depending on this ill */
1620 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1621 
1622 	if (ill->ill_group != NULL) {
1623 		illgrp_delete(ill);
1624 	}
1625 }
1626 
1627 /*
1628  * ire_walk routine used to delete every IRE that depends on queues
1629  * associated with 'ill'.  (Always called as writer.)
1630  */
1631 static void
1632 ill_downi(ire_t *ire, char *ill_arg)
1633 {
1634 	ill_t	*ill = (ill_t *)ill_arg;
1635 
1636 	/*
1637 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1638 	 * interface X and ipif coming from interface Y, if interface
1639 	 * X and Y are part of the same IPMP group. Thus whenever interface
1640 	 * X goes down, remove all references to it by checking both
1641 	 * on ire_ipif and ire_stq.
1642 	 */
1643 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1644 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1645 		ire_delete(ire);
1646 	}
1647 }
1648 
1649 /*
1650  * Remove ire/nce from the fastpath list.
1651  */
1652 void
1653 ill_fastpath_nack(ill_t *ill)
1654 {
1655 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1656 }
1657 
1658 /* Consume an M_IOCACK of the fastpath probe. */
1659 void
1660 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1661 {
1662 	mblk_t	*mp1 = mp;
1663 
1664 	/*
1665 	 * If this was the first attempt turn on the fastpath probing.
1666 	 */
1667 	mutex_enter(&ill->ill_lock);
1668 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1669 		ill->ill_dlpi_fastpath_state = IDS_OK;
1670 	mutex_exit(&ill->ill_lock);
1671 
1672 	/* Free the M_IOCACK mblk, hold on to the data */
1673 	mp = mp->b_cont;
1674 	freeb(mp1);
1675 	if (mp == NULL)
1676 		return;
1677 	if (mp->b_cont != NULL) {
1678 		/*
1679 		 * Update all IRE's or NCE's that are waiting for
1680 		 * fastpath update.
1681 		 */
1682 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1683 		mp1 = mp->b_cont;
1684 		freeb(mp);
1685 		mp = mp1;
1686 	} else {
1687 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1688 	}
1689 
1690 	freeb(mp);
1691 }
1692 
1693 /*
1694  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1695  * The data portion of the request is a dl_unitdata_req_t template for
1696  * what we would send downstream in the absence of a fastpath confirmation.
1697  */
1698 int
1699 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1700 {
1701 	struct iocblk	*ioc;
1702 	mblk_t	*mp;
1703 
1704 	if (dlur_mp == NULL)
1705 		return (EINVAL);
1706 
1707 	mutex_enter(&ill->ill_lock);
1708 	switch (ill->ill_dlpi_fastpath_state) {
1709 	case IDS_FAILED:
1710 		/*
1711 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1712 		 * support it.
1713 		 */
1714 		mutex_exit(&ill->ill_lock);
1715 		return (ENOTSUP);
1716 	case IDS_UNKNOWN:
1717 		/* This is the first probe */
1718 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1719 		break;
1720 	default:
1721 		break;
1722 	}
1723 	mutex_exit(&ill->ill_lock);
1724 
1725 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1726 		return (EAGAIN);
1727 
1728 	mp->b_cont = copyb(dlur_mp);
1729 	if (mp->b_cont == NULL) {
1730 		freeb(mp);
1731 		return (EAGAIN);
1732 	}
1733 
1734 	ioc = (struct iocblk *)mp->b_rptr;
1735 	ioc->ioc_count = msgdsize(mp->b_cont);
1736 
1737 	putnext(ill->ill_wq, mp);
1738 	return (0);
1739 }
1740 
1741 void
1742 ill_capability_probe(ill_t *ill)
1743 {
1744 	/*
1745 	 * Do so only if capabilities are still unknown.
1746 	 */
1747 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
1748 		return;
1749 
1750 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1751 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1752 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1753 }
1754 
1755 void
1756 ill_capability_reset(ill_t *ill)
1757 {
1758 	mblk_t *sc_mp = NULL;
1759 	mblk_t *tmp;
1760 
1761 	/*
1762 	 * Note here that we reset the state to UNKNOWN, and later send
1763 	 * down the DL_CAPABILITY_REQ without first setting the state to
1764 	 * INPROGRESS.  We do this in order to distinguish the
1765 	 * DL_CAPABILITY_ACK response which may come back in response to
1766 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1767 	 * also handle the case where the driver doesn't send us back
1768 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1769 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1770 	 * features are turned off until the state reaches IDS_OK.
1771 	 */
1772 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1773 	ill->ill_capab_reneg = B_FALSE;
1774 
1775 	/*
1776 	 * Disable sub-capabilities and request a list of sub-capability
1777 	 * messages which will be sent down to the driver.  Each handler
1778 	 * allocates the corresponding dl_capability_sub_t inside an
1779 	 * mblk, and links it to the existing sc_mp mblk, or return it
1780 	 * as sc_mp if it's the first sub-capability (the passed in
1781 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1782 	 * sc_mp will be pulled-up, before passing it downstream.
1783 	 */
1784 	ill_capability_mdt_reset(ill, &sc_mp);
1785 	ill_capability_hcksum_reset(ill, &sc_mp);
1786 	ill_capability_zerocopy_reset(ill, &sc_mp);
1787 	ill_capability_ipsec_reset(ill, &sc_mp);
1788 	ill_capability_dls_reset(ill, &sc_mp);
1789 	ill_capability_lso_reset(ill, &sc_mp);
1790 
1791 	/* Nothing to send down in order to disable the capabilities? */
1792 	if (sc_mp == NULL)
1793 		return;
1794 
1795 	tmp = msgpullup(sc_mp, -1);
1796 	freemsg(sc_mp);
1797 	if ((sc_mp = tmp) == NULL) {
1798 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1799 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1800 		return;
1801 	}
1802 
1803 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1804 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1805 }
1806 
1807 /*
1808  * Request or set new-style hardware capabilities supported by DLS provider.
1809  */
1810 static void
1811 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1812 {
1813 	mblk_t *mp;
1814 	dl_capability_req_t *capb;
1815 	size_t size = 0;
1816 	uint8_t *ptr;
1817 
1818 	if (reqp != NULL)
1819 		size = MBLKL(reqp);
1820 
1821 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1822 	if (mp == NULL) {
1823 		freemsg(reqp);
1824 		return;
1825 	}
1826 	ptr = mp->b_rptr;
1827 
1828 	capb = (dl_capability_req_t *)ptr;
1829 	ptr += sizeof (dl_capability_req_t);
1830 
1831 	if (reqp != NULL) {
1832 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1833 		capb->dl_sub_length = size;
1834 		bcopy(reqp->b_rptr, ptr, size);
1835 		ptr += size;
1836 		mp->b_cont = reqp->b_cont;
1837 		freeb(reqp);
1838 	}
1839 	ASSERT(ptr == mp->b_wptr);
1840 
1841 	ill_dlpi_send(ill, mp);
1842 }
1843 
1844 static void
1845 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1846 {
1847 	dl_capab_id_t *id_ic;
1848 	uint_t sub_dl_cap = outers->dl_cap;
1849 	dl_capability_sub_t *inners;
1850 	uint8_t *capend;
1851 
1852 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1853 
1854 	/*
1855 	 * Note: range checks here are not absolutely sufficient to
1856 	 * make us robust against malformed messages sent by drivers;
1857 	 * this is in keeping with the rest of IP's dlpi handling.
1858 	 * (Remember, it's coming from something else in the kernel
1859 	 * address space)
1860 	 */
1861 
1862 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1863 	if (capend > mp->b_wptr) {
1864 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1865 		    "malformed sub-capability too long for mblk");
1866 		return;
1867 	}
1868 
1869 	id_ic = (dl_capab_id_t *)(outers + 1);
1870 
1871 	if (outers->dl_length < sizeof (*id_ic) ||
1872 	    (inners = &id_ic->id_subcap,
1873 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1874 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1875 		    "encapsulated capab type %d too long for mblk",
1876 		    inners->dl_cap);
1877 		return;
1878 	}
1879 
1880 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1881 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1882 		    "isn't as expected; pass-thru module(s) detected, "
1883 		    "discarding capability\n", inners->dl_cap));
1884 		return;
1885 	}
1886 
1887 	/* Process the encapsulated sub-capability */
1888 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1889 }
1890 
1891 /*
1892  * Process Multidata Transmit capability negotiation ack received from a
1893  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1894  * DL_CAPABILITY_ACK message.
1895  */
1896 static void
1897 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1898 {
1899 	mblk_t *nmp = NULL;
1900 	dl_capability_req_t *oc;
1901 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1902 	ill_mdt_capab_t **ill_mdt_capab;
1903 	uint_t sub_dl_cap = isub->dl_cap;
1904 	uint8_t *capend;
1905 
1906 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1907 
1908 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1909 
1910 	/*
1911 	 * Note: range checks here are not absolutely sufficient to
1912 	 * make us robust against malformed messages sent by drivers;
1913 	 * this is in keeping with the rest of IP's dlpi handling.
1914 	 * (Remember, it's coming from something else in the kernel
1915 	 * address space)
1916 	 */
1917 
1918 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1919 	if (capend > mp->b_wptr) {
1920 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1921 		    "malformed sub-capability too long for mblk");
1922 		return;
1923 	}
1924 
1925 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1926 
1927 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1928 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1929 		    "unsupported MDT sub-capability (version %d, expected %d)",
1930 		    mdt_ic->mdt_version, MDT_VERSION_2);
1931 		return;
1932 	}
1933 
1934 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1935 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1936 		    "capability isn't as expected; pass-thru module(s) "
1937 		    "detected, discarding capability\n"));
1938 		return;
1939 	}
1940 
1941 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1942 
1943 		if (*ill_mdt_capab == NULL) {
1944 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1945 			    KM_NOSLEEP);
1946 
1947 			if (*ill_mdt_capab == NULL) {
1948 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1949 				    "could not enable MDT version %d "
1950 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1951 				    ill->ill_name);
1952 				return;
1953 			}
1954 		}
1955 
1956 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1957 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1958 		    "header spaces, %d max pld bufs, %d span limit)\n",
1959 		    ill->ill_name, MDT_VERSION_2,
1960 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1961 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1962 
1963 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1964 		(*ill_mdt_capab)->ill_mdt_on = 1;
1965 		/*
1966 		 * Round the following values to the nearest 32-bit; ULP
1967 		 * may further adjust them to accomodate for additional
1968 		 * protocol headers.  We pass these values to ULP during
1969 		 * bind time.
1970 		 */
1971 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1972 		    roundup(mdt_ic->mdt_hdr_head, 4);
1973 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1974 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1975 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1976 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1977 
1978 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1979 	} else {
1980 		uint_t size;
1981 		uchar_t *rptr;
1982 
1983 		size = sizeof (dl_capability_req_t) +
1984 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1985 
1986 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1987 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1988 			    "could not enable MDT for %s (ENOMEM)\n",
1989 			    ill->ill_name);
1990 			return;
1991 		}
1992 
1993 		rptr = nmp->b_rptr;
1994 		/* initialize dl_capability_req_t */
1995 		oc = (dl_capability_req_t *)nmp->b_rptr;
1996 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1997 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1998 		    sizeof (dl_capab_mdt_t);
1999 		nmp->b_rptr += sizeof (dl_capability_req_t);
2000 
2001 		/* initialize dl_capability_sub_t */
2002 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2003 		nmp->b_rptr += sizeof (*isub);
2004 
2005 		/* initialize dl_capab_mdt_t */
2006 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2007 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2008 
2009 		nmp->b_rptr = rptr;
2010 
2011 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2012 		    "to enable MDT version %d\n", ill->ill_name,
2013 		    MDT_VERSION_2));
2014 
2015 		/* set ENABLE flag */
2016 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2017 
2018 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2019 		ill_dlpi_send(ill, nmp);
2020 	}
2021 }
2022 
2023 static void
2024 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2025 {
2026 	mblk_t *mp;
2027 	dl_capab_mdt_t *mdt_subcap;
2028 	dl_capability_sub_t *dl_subcap;
2029 	int size;
2030 
2031 	if (!ILL_MDT_CAPABLE(ill))
2032 		return;
2033 
2034 	ASSERT(ill->ill_mdt_capab != NULL);
2035 	/*
2036 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2037 	 * structure since it's possible that another thread is still
2038 	 * referring to it.  The structure only gets deallocated when
2039 	 * we destroy the ill.
2040 	 */
2041 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2042 
2043 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2044 
2045 	mp = allocb(size, BPRI_HI);
2046 	if (mp == NULL) {
2047 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2048 		    "request to disable MDT\n"));
2049 		return;
2050 	}
2051 
2052 	mp->b_wptr = mp->b_rptr + size;
2053 
2054 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2055 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2056 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2057 
2058 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2059 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2060 	mdt_subcap->mdt_flags = 0;
2061 	mdt_subcap->mdt_hdr_head = 0;
2062 	mdt_subcap->mdt_hdr_tail = 0;
2063 
2064 	if (*sc_mp != NULL)
2065 		linkb(*sc_mp, mp);
2066 	else
2067 		*sc_mp = mp;
2068 }
2069 
2070 /*
2071  * Send a DL_NOTIFY_REQ to the specified ill to enable
2072  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2073  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2074  * acceleration.
2075  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2076  */
2077 static boolean_t
2078 ill_enable_promisc_notify(ill_t *ill)
2079 {
2080 	mblk_t *mp;
2081 	dl_notify_req_t *req;
2082 
2083 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2084 
2085 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2086 	if (mp == NULL)
2087 		return (B_FALSE);
2088 
2089 	req = (dl_notify_req_t *)mp->b_rptr;
2090 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2091 	    DL_NOTE_PROMISC_OFF_PHYS;
2092 
2093 	ill_dlpi_send(ill, mp);
2094 
2095 	return (B_TRUE);
2096 }
2097 
2098 
2099 /*
2100  * Allocate an IPsec capability request which will be filled by our
2101  * caller to turn on support for one or more algorithms.
2102  */
2103 static mblk_t *
2104 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2105 {
2106 	mblk_t *nmp;
2107 	dl_capability_req_t	*ocap;
2108 	dl_capab_ipsec_t	*ocip;
2109 	dl_capab_ipsec_t	*icip;
2110 	uint8_t			*ptr;
2111 	icip = (dl_capab_ipsec_t *)(isub + 1);
2112 
2113 	/*
2114 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2115 	 * PROMISC_ON/OFF notification from the provider. We need to
2116 	 * do this before enabling the algorithms to avoid leakage of
2117 	 * cleartext packets.
2118 	 */
2119 
2120 	if (!ill_enable_promisc_notify(ill))
2121 		return (NULL);
2122 
2123 	/*
2124 	 * Allocate new mblk which will contain a new capability
2125 	 * request to enable the capabilities.
2126 	 */
2127 
2128 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2129 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2130 	if (nmp == NULL)
2131 		return (NULL);
2132 
2133 	ptr = nmp->b_rptr;
2134 
2135 	/* initialize dl_capability_req_t */
2136 	ocap = (dl_capability_req_t *)ptr;
2137 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2138 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2139 	ptr += sizeof (dl_capability_req_t);
2140 
2141 	/* initialize dl_capability_sub_t */
2142 	bcopy(isub, ptr, sizeof (*isub));
2143 	ptr += sizeof (*isub);
2144 
2145 	/* initialize dl_capab_ipsec_t */
2146 	ocip = (dl_capab_ipsec_t *)ptr;
2147 	bcopy(icip, ocip, sizeof (*icip));
2148 
2149 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2150 	return (nmp);
2151 }
2152 
2153 /*
2154  * Process an IPsec capability negotiation ack received from a DLS Provider.
2155  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2156  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2157  */
2158 static void
2159 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2160 {
2161 	dl_capab_ipsec_t	*icip;
2162 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2163 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2164 	uint_t cipher, nciphers;
2165 	mblk_t *nmp;
2166 	uint_t alg_len;
2167 	boolean_t need_sadb_dump;
2168 	uint_t sub_dl_cap = isub->dl_cap;
2169 	ill_ipsec_capab_t **ill_capab;
2170 	uint64_t ill_capab_flag;
2171 	uint8_t *capend, *ciphend;
2172 	boolean_t sadb_resync;
2173 
2174 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2175 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2176 
2177 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2178 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2179 		ill_capab_flag = ILL_CAPAB_AH;
2180 	} else {
2181 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2182 		ill_capab_flag = ILL_CAPAB_ESP;
2183 	}
2184 
2185 	/*
2186 	 * If the ill capability structure exists, then this incoming
2187 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2188 	 * If this is so, then we'd need to resynchronize the SADB
2189 	 * after re-enabling the offloaded ciphers.
2190 	 */
2191 	sadb_resync = (*ill_capab != NULL);
2192 
2193 	/*
2194 	 * Note: range checks here are not absolutely sufficient to
2195 	 * make us robust against malformed messages sent by drivers;
2196 	 * this is in keeping with the rest of IP's dlpi handling.
2197 	 * (Remember, it's coming from something else in the kernel
2198 	 * address space)
2199 	 */
2200 
2201 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2202 	if (capend > mp->b_wptr) {
2203 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2204 		    "malformed sub-capability too long for mblk");
2205 		return;
2206 	}
2207 
2208 	/*
2209 	 * There are two types of acks we process here:
2210 	 * 1. acks in reply to a (first form) generic capability req
2211 	 *    (no ENABLE flag set)
2212 	 * 2. acks in reply to a ENABLE capability req.
2213 	 *    (ENABLE flag set)
2214 	 *
2215 	 * We process the subcapability passed as argument as follows:
2216 	 * 1 do initializations
2217 	 *   1.1 initialize nmp = NULL
2218 	 *   1.2 set need_sadb_dump to B_FALSE
2219 	 * 2 for each cipher in subcapability:
2220 	 *   2.1 if ENABLE flag is set:
2221 	 *	2.1.1 update per-ill ipsec capabilities info
2222 	 *	2.1.2 set need_sadb_dump to B_TRUE
2223 	 *   2.2 if ENABLE flag is not set:
2224 	 *	2.2.1 if nmp is NULL:
2225 	 *		2.2.1.1 allocate and initialize nmp
2226 	 *		2.2.1.2 init current pos in nmp
2227 	 *	2.2.2 copy current cipher to current pos in nmp
2228 	 *	2.2.3 set ENABLE flag in nmp
2229 	 *	2.2.4 update current pos
2230 	 * 3 if nmp is not equal to NULL, send enable request
2231 	 *   3.1 send capability request
2232 	 * 4 if need_sadb_dump is B_TRUE
2233 	 *   4.1 enable promiscuous on/off notifications
2234 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2235 	 *	AH or ESP SA's to interface.
2236 	 */
2237 
2238 	nmp = NULL;
2239 	oalg = NULL;
2240 	need_sadb_dump = B_FALSE;
2241 	icip = (dl_capab_ipsec_t *)(isub + 1);
2242 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2243 
2244 	nciphers = icip->cip_nciphers;
2245 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2246 
2247 	if (ciphend > capend) {
2248 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2249 		    "too many ciphers for sub-capability len");
2250 		return;
2251 	}
2252 
2253 	for (cipher = 0; cipher < nciphers; cipher++) {
2254 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2255 
2256 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2257 			/*
2258 			 * TBD: when we provide a way to disable capabilities
2259 			 * from above, need to manage the request-pending state
2260 			 * and fail if we were not expecting this ACK.
2261 			 */
2262 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2263 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2264 
2265 			/*
2266 			 * Update IPsec capabilities for this ill
2267 			 */
2268 
2269 			if (*ill_capab == NULL) {
2270 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2271 				    ("ill_capability_ipsec_ack: "
2272 				    "allocating ipsec_capab for ill\n"));
2273 				*ill_capab = ill_ipsec_capab_alloc();
2274 
2275 				if (*ill_capab == NULL) {
2276 					cmn_err(CE_WARN,
2277 					    "ill_capability_ipsec_ack: "
2278 					    "could not enable IPsec Hardware "
2279 					    "acceleration for %s (ENOMEM)\n",
2280 					    ill->ill_name);
2281 					return;
2282 				}
2283 			}
2284 
2285 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2286 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2287 
2288 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2289 				cmn_err(CE_WARN,
2290 				    "ill_capability_ipsec_ack: "
2291 				    "malformed IPsec algorithm id %d",
2292 				    ialg->alg_prim);
2293 				continue;
2294 			}
2295 
2296 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2297 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2298 				    ialg->alg_prim);
2299 			} else {
2300 				ipsec_capab_algparm_t *alp;
2301 
2302 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2303 				    ialg->alg_prim);
2304 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2305 				    ialg->alg_prim)) {
2306 					cmn_err(CE_WARN,
2307 					    "ill_capability_ipsec_ack: "
2308 					    "no space for IPsec alg id %d",
2309 					    ialg->alg_prim);
2310 					continue;
2311 				}
2312 				alp = &((*ill_capab)->encr_algparm[
2313 				    ialg->alg_prim]);
2314 				alp->minkeylen = ialg->alg_minbits;
2315 				alp->maxkeylen = ialg->alg_maxbits;
2316 			}
2317 			ill->ill_capabilities |= ill_capab_flag;
2318 			/*
2319 			 * indicate that a capability was enabled, which
2320 			 * will be used below to kick off a SADB dump
2321 			 * to the ill.
2322 			 */
2323 			need_sadb_dump = B_TRUE;
2324 		} else {
2325 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2326 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2327 			    ialg->alg_prim));
2328 
2329 			if (nmp == NULL) {
2330 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2331 				if (nmp == NULL) {
2332 					/*
2333 					 * Sending the PROMISC_ON/OFF
2334 					 * notification request failed.
2335 					 * We cannot enable the algorithms
2336 					 * since the Provider will not
2337 					 * notify IP of promiscous mode
2338 					 * changes, which could lead
2339 					 * to leakage of packets.
2340 					 */
2341 					cmn_err(CE_WARN,
2342 					    "ill_capability_ipsec_ack: "
2343 					    "could not enable IPsec Hardware "
2344 					    "acceleration for %s (ENOMEM)\n",
2345 					    ill->ill_name);
2346 					return;
2347 				}
2348 				/* ptr to current output alg specifier */
2349 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2350 			}
2351 
2352 			/*
2353 			 * Copy current alg specifier, set ENABLE
2354 			 * flag, and advance to next output alg.
2355 			 * For now we enable all IPsec capabilities.
2356 			 */
2357 			ASSERT(oalg != NULL);
2358 			bcopy(ialg, oalg, alg_len);
2359 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2360 			nmp->b_wptr += alg_len;
2361 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2362 		}
2363 
2364 		/* move to next input algorithm specifier */
2365 		ialg = (dl_capab_ipsec_alg_t *)
2366 		    ((char *)ialg + alg_len);
2367 	}
2368 
2369 	if (nmp != NULL)
2370 		/*
2371 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2372 		 * IPsec hardware acceleration.
2373 		 */
2374 		ill_dlpi_send(ill, nmp);
2375 
2376 	if (need_sadb_dump)
2377 		/*
2378 		 * An acknowledgement corresponding to a request to
2379 		 * enable acceleration was received, notify SADB.
2380 		 */
2381 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2382 }
2383 
2384 /*
2385  * Given an mblk with enough space in it, create sub-capability entries for
2386  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2387  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2388  * in preparation for the reset the DL_CAPABILITY_REQ message.
2389  */
2390 static void
2391 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2392     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2393 {
2394 	dl_capab_ipsec_t *oipsec;
2395 	dl_capab_ipsec_alg_t *oalg;
2396 	dl_capability_sub_t *dl_subcap;
2397 	int i, k;
2398 
2399 	ASSERT(nciphers > 0);
2400 	ASSERT(ill_cap != NULL);
2401 	ASSERT(mp != NULL);
2402 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2403 
2404 	/* dl_capability_sub_t for "stype" */
2405 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2406 	dl_subcap->dl_cap = stype;
2407 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2408 	mp->b_wptr += sizeof (dl_capability_sub_t);
2409 
2410 	/* dl_capab_ipsec_t for "stype" */
2411 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2412 	oipsec->cip_version = 1;
2413 	oipsec->cip_nciphers = nciphers;
2414 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2415 
2416 	/* create entries for "stype" AUTH ciphers */
2417 	for (i = 0; i < ill_cap->algs_size; i++) {
2418 		for (k = 0; k < BITSPERBYTE; k++) {
2419 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2420 				continue;
2421 
2422 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2423 			bzero((void *)oalg, sizeof (*oalg));
2424 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2425 			oalg->alg_prim = k + (BITSPERBYTE * i);
2426 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2427 		}
2428 	}
2429 	/* create entries for "stype" ENCR ciphers */
2430 	for (i = 0; i < ill_cap->algs_size; i++) {
2431 		for (k = 0; k < BITSPERBYTE; k++) {
2432 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2433 				continue;
2434 
2435 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2436 			bzero((void *)oalg, sizeof (*oalg));
2437 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2438 			oalg->alg_prim = k + (BITSPERBYTE * i);
2439 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2440 		}
2441 	}
2442 }
2443 
2444 /*
2445  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2446  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2447  * POPC instruction, but our macro is more flexible for an arbitrary length
2448  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2449  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2450  * stays that way, we can reduce the number of iterations required.
2451  */
2452 #define	COUNT_1S(val, sum) {					\
2453 	uint8_t x = val & 0xff;					\
2454 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2455 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2456 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2457 }
2458 
2459 /* ARGSUSED */
2460 static void
2461 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2462 {
2463 	mblk_t *mp;
2464 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2465 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2466 	uint64_t ill_capabilities = ill->ill_capabilities;
2467 	int ah_cnt = 0, esp_cnt = 0;
2468 	int ah_len = 0, esp_len = 0;
2469 	int i, size = 0;
2470 
2471 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2472 		return;
2473 
2474 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2475 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2476 
2477 	/* Find out the number of ciphers for AH */
2478 	if (cap_ah != NULL) {
2479 		for (i = 0; i < cap_ah->algs_size; i++) {
2480 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2481 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2482 		}
2483 		if (ah_cnt > 0) {
2484 			size += sizeof (dl_capability_sub_t) +
2485 			    sizeof (dl_capab_ipsec_t);
2486 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2487 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2488 			size += ah_len;
2489 		}
2490 	}
2491 
2492 	/* Find out the number of ciphers for ESP */
2493 	if (cap_esp != NULL) {
2494 		for (i = 0; i < cap_esp->algs_size; i++) {
2495 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2496 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2497 		}
2498 		if (esp_cnt > 0) {
2499 			size += sizeof (dl_capability_sub_t) +
2500 			    sizeof (dl_capab_ipsec_t);
2501 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2502 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2503 			size += esp_len;
2504 		}
2505 	}
2506 
2507 	if (size == 0) {
2508 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2509 		    "there's nothing to reset\n"));
2510 		return;
2511 	}
2512 
2513 	mp = allocb(size, BPRI_HI);
2514 	if (mp == NULL) {
2515 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2516 		    "request to disable IPSEC Hardware Acceleration\n"));
2517 		return;
2518 	}
2519 
2520 	/*
2521 	 * Clear the capability flags for IPsec HA but retain the ill
2522 	 * capability structures since it's possible that another thread
2523 	 * is still referring to them.  The structures only get deallocated
2524 	 * when we destroy the ill.
2525 	 *
2526 	 * Various places check the flags to see if the ill is capable of
2527 	 * hardware acceleration, and by clearing them we ensure that new
2528 	 * outbound IPsec packets are sent down encrypted.
2529 	 */
2530 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2531 
2532 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2533 	if (ah_cnt > 0) {
2534 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2535 		    cap_ah, mp);
2536 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2537 	}
2538 
2539 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2540 	if (esp_cnt > 0) {
2541 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2542 		    cap_esp, mp);
2543 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2544 	}
2545 
2546 	/*
2547 	 * At this point we've composed a bunch of sub-capabilities to be
2548 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2549 	 * by the caller.  Upon receiving this reset message, the driver
2550 	 * must stop inbound decryption (by destroying all inbound SAs)
2551 	 * and let the corresponding packets come in encrypted.
2552 	 */
2553 
2554 	if (*sc_mp != NULL)
2555 		linkb(*sc_mp, mp);
2556 	else
2557 		*sc_mp = mp;
2558 }
2559 
2560 static void
2561 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2562     boolean_t encapsulated)
2563 {
2564 	boolean_t legacy = B_FALSE;
2565 
2566 	/*
2567 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2568 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2569 	 * instructed the driver to disable its advertised capabilities,
2570 	 * so there's no point in accepting any response at this moment.
2571 	 */
2572 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2573 		return;
2574 
2575 	/*
2576 	 * Note that only the following two sub-capabilities may be
2577 	 * considered as "legacy", since their original definitions
2578 	 * do not incorporate the dl_mid_t module ID token, and hence
2579 	 * may require the use of the wrapper sub-capability.
2580 	 */
2581 	switch (subp->dl_cap) {
2582 	case DL_CAPAB_IPSEC_AH:
2583 	case DL_CAPAB_IPSEC_ESP:
2584 		legacy = B_TRUE;
2585 		break;
2586 	}
2587 
2588 	/*
2589 	 * For legacy sub-capabilities which don't incorporate a queue_t
2590 	 * pointer in their structures, discard them if we detect that
2591 	 * there are intermediate modules in between IP and the driver.
2592 	 */
2593 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2594 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2595 		    "%d discarded; %d module(s) present below IP\n",
2596 		    subp->dl_cap, ill->ill_lmod_cnt));
2597 		return;
2598 	}
2599 
2600 	switch (subp->dl_cap) {
2601 	case DL_CAPAB_IPSEC_AH:
2602 	case DL_CAPAB_IPSEC_ESP:
2603 		ill_capability_ipsec_ack(ill, mp, subp);
2604 		break;
2605 	case DL_CAPAB_MDT:
2606 		ill_capability_mdt_ack(ill, mp, subp);
2607 		break;
2608 	case DL_CAPAB_HCKSUM:
2609 		ill_capability_hcksum_ack(ill, mp, subp);
2610 		break;
2611 	case DL_CAPAB_ZEROCOPY:
2612 		ill_capability_zerocopy_ack(ill, mp, subp);
2613 		break;
2614 	case DL_CAPAB_POLL:
2615 		if (!SOFT_RINGS_ENABLED())
2616 			ill_capability_dls_ack(ill, mp, subp);
2617 		break;
2618 	case DL_CAPAB_SOFT_RING:
2619 		if (SOFT_RINGS_ENABLED())
2620 			ill_capability_dls_ack(ill, mp, subp);
2621 		break;
2622 	case DL_CAPAB_LSO:
2623 		ill_capability_lso_ack(ill, mp, subp);
2624 		break;
2625 	default:
2626 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2627 		    subp->dl_cap));
2628 	}
2629 }
2630 
2631 /*
2632  * As part of negotiating polling capability, the driver tells us
2633  * the default (or normal) blanking interval and packet threshold
2634  * (the receive timer fires if blanking interval is reached or
2635  * the packet threshold is reached).
2636  *
2637  * As part of manipulating the polling interval, we always use our
2638  * estimated interval (avg service time * number of packets queued
2639  * on the squeue) but we try to blank for a minimum of
2640  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2641  * packet threshold during this time. When we are not in polling mode
2642  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2643  * rr_min_blank_ratio but up the packet cnt by a ratio of
2644  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2645  * possible although for a shorter interval.
2646  */
2647 #define	RR_MAX_BLANK_RATIO	20
2648 #define	RR_MIN_BLANK_RATIO	10
2649 #define	RR_MAX_PKT_CNT_RATIO	3
2650 #define	RR_MIN_PKT_CNT_RATIO	3
2651 
2652 /*
2653  * These can be tuned via /etc/system.
2654  */
2655 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2656 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2657 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2658 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2659 
2660 static mac_resource_handle_t
2661 ill_ring_add(void *arg, mac_resource_t *mrp)
2662 {
2663 	ill_t			*ill = (ill_t *)arg;
2664 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2665 	ill_rx_ring_t		*rx_ring;
2666 	int			ip_rx_index;
2667 
2668 	ASSERT(mrp != NULL);
2669 	if (mrp->mr_type != MAC_RX_FIFO) {
2670 		return (NULL);
2671 	}
2672 	ASSERT(ill != NULL);
2673 	ASSERT(ill->ill_dls_capab != NULL);
2674 
2675 	mutex_enter(&ill->ill_lock);
2676 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2677 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2678 		ASSERT(rx_ring != NULL);
2679 
2680 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2681 			time_t normal_blank_time =
2682 			    mrfp->mrf_normal_blank_time;
2683 			uint_t normal_pkt_cnt =
2684 			    mrfp->mrf_normal_pkt_count;
2685 
2686 	bzero(rx_ring, sizeof (ill_rx_ring_t));
2687 
2688 	rx_ring->rr_blank = mrfp->mrf_blank;
2689 	rx_ring->rr_handle = mrfp->mrf_arg;
2690 	rx_ring->rr_ill = ill;
2691 	rx_ring->rr_normal_blank_time = normal_blank_time;
2692 	rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2693 
2694 			rx_ring->rr_max_blank_time =
2695 			    normal_blank_time * rr_max_blank_ratio;
2696 			rx_ring->rr_min_blank_time =
2697 			    normal_blank_time * rr_min_blank_ratio;
2698 			rx_ring->rr_max_pkt_cnt =
2699 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2700 			rx_ring->rr_min_pkt_cnt =
2701 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2702 
2703 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2704 			mutex_exit(&ill->ill_lock);
2705 
2706 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2707 			    (int), ip_rx_index);
2708 			return ((mac_resource_handle_t)rx_ring);
2709 		}
2710 	}
2711 
2712 	/*
2713 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2714 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2715 	 * should be made configurable. Meanwhile it cause no panic because
2716 	 * driver will pass ip_input a NULL handle which will make
2717 	 * IP allocate the default squeue and Polling mode will not
2718 	 * be used for this ring.
2719 	 */
2720 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2721 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2722 
2723 	mutex_exit(&ill->ill_lock);
2724 	return (NULL);
2725 }
2726 
2727 static boolean_t
2728 ill_capability_dls_init(ill_t *ill)
2729 {
2730 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2731 	conn_t 			*connp;
2732 	size_t			sz;
2733 	ip_stack_t *ipst = ill->ill_ipst;
2734 
2735 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2736 		if (ill_dls == NULL) {
2737 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2738 			    "soft_ring enabled for ill=%s (%p) but data "
2739 			    "structs uninitialized\n", ill->ill_name,
2740 			    (void *)ill);
2741 		}
2742 		return (B_TRUE);
2743 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2744 		if (ill_dls == NULL) {
2745 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2746 			    "polling enabled for ill=%s (%p) but data "
2747 			    "structs uninitialized\n", ill->ill_name,
2748 			    (void *)ill);
2749 		}
2750 		return (B_TRUE);
2751 	}
2752 
2753 	if (ill_dls != NULL) {
2754 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2755 		/* Soft_Ring or polling is being re-enabled */
2756 
2757 		connp = ill_dls->ill_unbind_conn;
2758 		ASSERT(rx_ring != NULL);
2759 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2760 		bzero((void *)rx_ring,
2761 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2762 		ill_dls->ill_ring_tbl = rx_ring;
2763 		ill_dls->ill_unbind_conn = connp;
2764 		return (B_TRUE);
2765 	}
2766 
2767 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
2768 	    ipst->ips_netstack)) == NULL)
2769 		return (B_FALSE);
2770 
2771 	sz = sizeof (ill_dls_capab_t);
2772 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2773 
2774 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2775 	if (ill_dls == NULL) {
2776 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2777 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2778 		    (void *)ill);
2779 		CONN_DEC_REF(connp);
2780 		return (B_FALSE);
2781 	}
2782 
2783 	/* Allocate space to hold ring table */
2784 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2785 	ill->ill_dls_capab = ill_dls;
2786 	ill_dls->ill_unbind_conn = connp;
2787 	return (B_TRUE);
2788 }
2789 
2790 /*
2791  * ill_capability_dls_disable: disable soft_ring and/or polling
2792  * capability. Since any of the rings might already be in use, need
2793  * to call ip_squeue_clean_all() which gets behind the squeue to disable
2794  * direct calls if necessary.
2795  */
2796 static void
2797 ill_capability_dls_disable(ill_t *ill)
2798 {
2799 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2800 
2801 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2802 		ip_squeue_clean_all(ill);
2803 		ill_dls->ill_tx = NULL;
2804 		ill_dls->ill_tx_handle = NULL;
2805 		ill_dls->ill_dls_change_status = NULL;
2806 		ill_dls->ill_dls_bind = NULL;
2807 		ill_dls->ill_dls_unbind = NULL;
2808 	}
2809 
2810 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2811 }
2812 
2813 static void
2814 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2815     dl_capability_sub_t *isub)
2816 {
2817 	uint_t			size;
2818 	uchar_t			*rptr;
2819 	dl_capab_dls_t	dls, *odls;
2820 	ill_dls_capab_t	*ill_dls;
2821 	mblk_t			*nmp = NULL;
2822 	dl_capability_req_t	*ocap;
2823 	uint_t			sub_dl_cap = isub->dl_cap;
2824 
2825 	if (!ill_capability_dls_init(ill))
2826 		return;
2827 	ill_dls = ill->ill_dls_capab;
2828 
2829 	/* Copy locally to get the members aligned */
2830 	bcopy((void *)idls, (void *)&dls,
2831 	    sizeof (dl_capab_dls_t));
2832 
2833 	/* Get the tx function and handle from dld */
2834 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2835 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2836 
2837 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2838 		ill_dls->ill_dls_change_status =
2839 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2840 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2841 		ill_dls->ill_dls_unbind =
2842 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2843 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2844 	}
2845 
2846 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2847 	    isub->dl_length;
2848 
2849 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2850 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2851 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2852 		    ill->ill_name, (void *)ill);
2853 		return;
2854 	}
2855 
2856 	/* initialize dl_capability_req_t */
2857 	rptr = nmp->b_rptr;
2858 	ocap = (dl_capability_req_t *)rptr;
2859 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2860 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2861 	rptr += sizeof (dl_capability_req_t);
2862 
2863 	/* initialize dl_capability_sub_t */
2864 	bcopy(isub, rptr, sizeof (*isub));
2865 	rptr += sizeof (*isub);
2866 
2867 	odls = (dl_capab_dls_t *)rptr;
2868 	rptr += sizeof (dl_capab_dls_t);
2869 
2870 	/* initialize dl_capab_dls_t to be sent down */
2871 	dls.dls_rx_handle = (uintptr_t)ill;
2872 	dls.dls_rx = (uintptr_t)ip_input;
2873 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2874 
2875 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2876 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2877 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2878 		dls.dls_flags = SOFT_RING_ENABLE;
2879 	} else {
2880 		dls.dls_flags = POLL_ENABLE;
2881 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2882 		    "to enable polling\n", ill->ill_name));
2883 	}
2884 	bcopy((void *)&dls, (void *)odls,
2885 	    sizeof (dl_capab_dls_t));
2886 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2887 	/*
2888 	 * nmp points to a DL_CAPABILITY_REQ message to
2889 	 * enable either soft_ring or polling
2890 	 */
2891 	ill_dlpi_send(ill, nmp);
2892 }
2893 
2894 static void
2895 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2896 {
2897 	mblk_t *mp;
2898 	dl_capab_dls_t *idls;
2899 	dl_capability_sub_t *dl_subcap;
2900 	int size;
2901 
2902 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2903 		return;
2904 
2905 	ASSERT(ill->ill_dls_capab != NULL);
2906 
2907 	size = sizeof (*dl_subcap) + sizeof (*idls);
2908 
2909 	mp = allocb(size, BPRI_HI);
2910 	if (mp == NULL) {
2911 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2912 		    "request to disable soft_ring\n"));
2913 		return;
2914 	}
2915 
2916 	mp->b_wptr = mp->b_rptr + size;
2917 
2918 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2919 	dl_subcap->dl_length = sizeof (*idls);
2920 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2921 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2922 	else
2923 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2924 
2925 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2926 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2927 		idls->dls_flags = SOFT_RING_DISABLE;
2928 	else
2929 		idls->dls_flags = POLL_DISABLE;
2930 
2931 	if (*sc_mp != NULL)
2932 		linkb(*sc_mp, mp);
2933 	else
2934 		*sc_mp = mp;
2935 }
2936 
2937 /*
2938  * Process a soft_ring/poll capability negotiation ack received
2939  * from a DLS Provider.isub must point to the sub-capability
2940  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
2941  */
2942 static void
2943 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2944 {
2945 	dl_capab_dls_t		*idls;
2946 	uint_t			sub_dl_cap = isub->dl_cap;
2947 	uint8_t			*capend;
2948 
2949 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
2950 	    sub_dl_cap == DL_CAPAB_POLL);
2951 
2952 	if (ill->ill_isv6)
2953 		return;
2954 
2955 	/*
2956 	 * Note: range checks here are not absolutely sufficient to
2957 	 * make us robust against malformed messages sent by drivers;
2958 	 * this is in keeping with the rest of IP's dlpi handling.
2959 	 * (Remember, it's coming from something else in the kernel
2960 	 * address space)
2961 	 */
2962 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2963 	if (capend > mp->b_wptr) {
2964 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
2965 		    "malformed sub-capability too long for mblk");
2966 		return;
2967 	}
2968 
2969 	/*
2970 	 * There are two types of acks we process here:
2971 	 * 1. acks in reply to a (first form) generic capability req
2972 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
2973 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
2974 	 *    capability req.
2975 	 */
2976 	idls = (dl_capab_dls_t *)(isub + 1);
2977 
2978 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
2979 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
2980 		    "capability isn't as expected; pass-thru "
2981 		    "module(s) detected, discarding capability\n"));
2982 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2983 			/*
2984 			 * This is a capability renegotitation case.
2985 			 * The interface better be unusable at this
2986 			 * point other wise bad things will happen
2987 			 * if we disable direct calls on a running
2988 			 * and up interface.
2989 			 */
2990 			ill_capability_dls_disable(ill);
2991 		}
2992 		return;
2993 	}
2994 
2995 	switch (idls->dls_flags) {
2996 	default:
2997 		/* Disable if unknown flag */
2998 	case SOFT_RING_DISABLE:
2999 	case POLL_DISABLE:
3000 		ill_capability_dls_disable(ill);
3001 		break;
3002 	case SOFT_RING_CAPABLE:
3003 	case POLL_CAPABLE:
3004 		/*
3005 		 * If the capability was already enabled, its safe
3006 		 * to disable it first to get rid of stale information
3007 		 * and then start enabling it again.
3008 		 */
3009 		ill_capability_dls_disable(ill);
3010 		ill_capability_dls_capable(ill, idls, isub);
3011 		break;
3012 	case SOFT_RING_ENABLE:
3013 	case POLL_ENABLE:
3014 		mutex_enter(&ill->ill_lock);
3015 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3016 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3017 			ASSERT(ill->ill_dls_capab != NULL);
3018 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3019 		}
3020 		if (sub_dl_cap == DL_CAPAB_POLL &&
3021 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3022 			ASSERT(ill->ill_dls_capab != NULL);
3023 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3024 			ip1dbg(("ill_capability_dls_ack: interface %s "
3025 			    "has enabled polling\n", ill->ill_name));
3026 		}
3027 		mutex_exit(&ill->ill_lock);
3028 		break;
3029 	}
3030 }
3031 
3032 /*
3033  * Process a hardware checksum offload capability negotiation ack received
3034  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3035  * of a DL_CAPABILITY_ACK message.
3036  */
3037 static void
3038 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3039 {
3040 	dl_capability_req_t	*ocap;
3041 	dl_capab_hcksum_t	*ihck, *ohck;
3042 	ill_hcksum_capab_t	**ill_hcksum;
3043 	mblk_t			*nmp = NULL;
3044 	uint_t			sub_dl_cap = isub->dl_cap;
3045 	uint8_t			*capend;
3046 
3047 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3048 
3049 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3050 
3051 	/*
3052 	 * Note: range checks here are not absolutely sufficient to
3053 	 * make us robust against malformed messages sent by drivers;
3054 	 * this is in keeping with the rest of IP's dlpi handling.
3055 	 * (Remember, it's coming from something else in the kernel
3056 	 * address space)
3057 	 */
3058 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3059 	if (capend > mp->b_wptr) {
3060 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3061 		    "malformed sub-capability too long for mblk");
3062 		return;
3063 	}
3064 
3065 	/*
3066 	 * There are two types of acks we process here:
3067 	 * 1. acks in reply to a (first form) generic capability req
3068 	 *    (no ENABLE flag set)
3069 	 * 2. acks in reply to a ENABLE capability req.
3070 	 *    (ENABLE flag set)
3071 	 */
3072 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3073 
3074 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3075 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3076 		    "unsupported hardware checksum "
3077 		    "sub-capability (version %d, expected %d)",
3078 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3079 		return;
3080 	}
3081 
3082 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3083 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3084 		    "checksum capability isn't as expected; pass-thru "
3085 		    "module(s) detected, discarding capability\n"));
3086 		return;
3087 	}
3088 
3089 #define	CURR_HCKSUM_CAPAB				\
3090 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3091 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3092 
3093 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3094 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3095 		/* do ENABLE processing */
3096 		if (*ill_hcksum == NULL) {
3097 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3098 			    KM_NOSLEEP);
3099 
3100 			if (*ill_hcksum == NULL) {
3101 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3102 				    "could not enable hcksum version %d "
3103 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3104 				    ill->ill_name);
3105 				return;
3106 			}
3107 		}
3108 
3109 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3110 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3111 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3112 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3113 		    "has enabled hardware checksumming\n ",
3114 		    ill->ill_name));
3115 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3116 		/*
3117 		 * Enabling hardware checksum offload
3118 		 * Currently IP supports {TCP,UDP}/IPv4
3119 		 * partial and full cksum offload and
3120 		 * IPv4 header checksum offload.
3121 		 * Allocate new mblk which will
3122 		 * contain a new capability request
3123 		 * to enable hardware checksum offload.
3124 		 */
3125 		uint_t	size;
3126 		uchar_t	*rptr;
3127 
3128 		size = sizeof (dl_capability_req_t) +
3129 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3130 
3131 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3132 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3133 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3134 			    ill->ill_name);
3135 			return;
3136 		}
3137 
3138 		rptr = nmp->b_rptr;
3139 		/* initialize dl_capability_req_t */
3140 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3141 		ocap->dl_sub_offset =
3142 		    sizeof (dl_capability_req_t);
3143 		ocap->dl_sub_length =
3144 		    sizeof (dl_capability_sub_t) +
3145 		    isub->dl_length;
3146 		nmp->b_rptr += sizeof (dl_capability_req_t);
3147 
3148 		/* initialize dl_capability_sub_t */
3149 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3150 		nmp->b_rptr += sizeof (*isub);
3151 
3152 		/* initialize dl_capab_hcksum_t */
3153 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3154 		bcopy(ihck, ohck, sizeof (*ihck));
3155 
3156 		nmp->b_rptr = rptr;
3157 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3158 
3159 		/* Set ENABLE flag */
3160 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3161 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3162 
3163 		/*
3164 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3165 		 * hardware checksum acceleration.
3166 		 */
3167 		ill_dlpi_send(ill, nmp);
3168 	} else {
3169 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3170 		    "advertised %x hardware checksum capability flags\n",
3171 		    ill->ill_name, ihck->hcksum_txflags));
3172 	}
3173 }
3174 
3175 static void
3176 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3177 {
3178 	mblk_t *mp;
3179 	dl_capab_hcksum_t *hck_subcap;
3180 	dl_capability_sub_t *dl_subcap;
3181 	int size;
3182 
3183 	if (!ILL_HCKSUM_CAPABLE(ill))
3184 		return;
3185 
3186 	ASSERT(ill->ill_hcksum_capab != NULL);
3187 	/*
3188 	 * Clear the capability flag for hardware checksum offload but
3189 	 * retain the ill_hcksum_capab structure since it's possible that
3190 	 * another thread is still referring to it.  The structure only
3191 	 * gets deallocated when we destroy the ill.
3192 	 */
3193 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3194 
3195 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3196 
3197 	mp = allocb(size, BPRI_HI);
3198 	if (mp == NULL) {
3199 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3200 		    "request to disable hardware checksum offload\n"));
3201 		return;
3202 	}
3203 
3204 	mp->b_wptr = mp->b_rptr + size;
3205 
3206 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3207 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3208 	dl_subcap->dl_length = sizeof (*hck_subcap);
3209 
3210 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3211 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3212 	hck_subcap->hcksum_txflags = 0;
3213 
3214 	if (*sc_mp != NULL)
3215 		linkb(*sc_mp, mp);
3216 	else
3217 		*sc_mp = mp;
3218 }
3219 
3220 static void
3221 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3222 {
3223 	mblk_t *nmp = NULL;
3224 	dl_capability_req_t *oc;
3225 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3226 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3227 	uint_t sub_dl_cap = isub->dl_cap;
3228 	uint8_t *capend;
3229 
3230 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3231 
3232 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3233 
3234 	/*
3235 	 * Note: range checks here are not absolutely sufficient to
3236 	 * make us robust against malformed messages sent by drivers;
3237 	 * this is in keeping with the rest of IP's dlpi handling.
3238 	 * (Remember, it's coming from something else in the kernel
3239 	 * address space)
3240 	 */
3241 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3242 	if (capend > mp->b_wptr) {
3243 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3244 		    "malformed sub-capability too long for mblk");
3245 		return;
3246 	}
3247 
3248 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3249 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3250 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3251 		    "unsupported ZEROCOPY sub-capability (version %d, "
3252 		    "expected %d)", zc_ic->zerocopy_version,
3253 		    ZEROCOPY_VERSION_1);
3254 		return;
3255 	}
3256 
3257 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3258 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3259 		    "capability isn't as expected; pass-thru module(s) "
3260 		    "detected, discarding capability\n"));
3261 		return;
3262 	}
3263 
3264 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3265 		if (*ill_zerocopy_capab == NULL) {
3266 			*ill_zerocopy_capab =
3267 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3268 			    KM_NOSLEEP);
3269 
3270 			if (*ill_zerocopy_capab == NULL) {
3271 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3272 				    "could not enable Zero-copy version %d "
3273 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3274 				    ill->ill_name);
3275 				return;
3276 			}
3277 		}
3278 
3279 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3280 		    "supports Zero-copy version %d\n", ill->ill_name,
3281 		    ZEROCOPY_VERSION_1));
3282 
3283 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3284 		    zc_ic->zerocopy_version;
3285 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3286 		    zc_ic->zerocopy_flags;
3287 
3288 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3289 	} else {
3290 		uint_t size;
3291 		uchar_t *rptr;
3292 
3293 		size = sizeof (dl_capability_req_t) +
3294 		    sizeof (dl_capability_sub_t) +
3295 		    sizeof (dl_capab_zerocopy_t);
3296 
3297 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3298 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3299 			    "could not enable zerocopy for %s (ENOMEM)\n",
3300 			    ill->ill_name);
3301 			return;
3302 		}
3303 
3304 		rptr = nmp->b_rptr;
3305 		/* initialize dl_capability_req_t */
3306 		oc = (dl_capability_req_t *)rptr;
3307 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3308 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3309 		    sizeof (dl_capab_zerocopy_t);
3310 		rptr += sizeof (dl_capability_req_t);
3311 
3312 		/* initialize dl_capability_sub_t */
3313 		bcopy(isub, rptr, sizeof (*isub));
3314 		rptr += sizeof (*isub);
3315 
3316 		/* initialize dl_capab_zerocopy_t */
3317 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3318 		*zc_oc = *zc_ic;
3319 
3320 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3321 		    "to enable zero-copy version %d\n", ill->ill_name,
3322 		    ZEROCOPY_VERSION_1));
3323 
3324 		/* set VMSAFE_MEM flag */
3325 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3326 
3327 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3328 		ill_dlpi_send(ill, nmp);
3329 	}
3330 }
3331 
3332 static void
3333 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3334 {
3335 	mblk_t *mp;
3336 	dl_capab_zerocopy_t *zerocopy_subcap;
3337 	dl_capability_sub_t *dl_subcap;
3338 	int size;
3339 
3340 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3341 		return;
3342 
3343 	ASSERT(ill->ill_zerocopy_capab != NULL);
3344 	/*
3345 	 * Clear the capability flag for Zero-copy but retain the
3346 	 * ill_zerocopy_capab structure since it's possible that another
3347 	 * thread is still referring to it.  The structure only gets
3348 	 * deallocated when we destroy the ill.
3349 	 */
3350 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3351 
3352 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3353 
3354 	mp = allocb(size, BPRI_HI);
3355 	if (mp == NULL) {
3356 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3357 		    "request to disable Zero-copy\n"));
3358 		return;
3359 	}
3360 
3361 	mp->b_wptr = mp->b_rptr + size;
3362 
3363 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3364 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3365 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3366 
3367 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3368 	zerocopy_subcap->zerocopy_version =
3369 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3370 	zerocopy_subcap->zerocopy_flags = 0;
3371 
3372 	if (*sc_mp != NULL)
3373 		linkb(*sc_mp, mp);
3374 	else
3375 		*sc_mp = mp;
3376 }
3377 
3378 /*
3379  * Process Large Segment Offload capability negotiation ack received from a
3380  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3381  * DL_CAPABILITY_ACK message.
3382  */
3383 static void
3384 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3385 {
3386 	mblk_t *nmp = NULL;
3387 	dl_capability_req_t *oc;
3388 	dl_capab_lso_t *lso_ic, *lso_oc;
3389 	ill_lso_capab_t **ill_lso_capab;
3390 	uint_t sub_dl_cap = isub->dl_cap;
3391 	uint8_t *capend;
3392 
3393 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3394 
3395 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3396 
3397 	/*
3398 	 * Note: range checks here are not absolutely sufficient to
3399 	 * make us robust against malformed messages sent by drivers;
3400 	 * this is in keeping with the rest of IP's dlpi handling.
3401 	 * (Remember, it's coming from something else in the kernel
3402 	 * address space)
3403 	 */
3404 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3405 	if (capend > mp->b_wptr) {
3406 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3407 		    "malformed sub-capability too long for mblk");
3408 		return;
3409 	}
3410 
3411 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3412 
3413 	if (lso_ic->lso_version != LSO_VERSION_1) {
3414 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3415 		    "unsupported LSO sub-capability (version %d, expected %d)",
3416 		    lso_ic->lso_version, LSO_VERSION_1);
3417 		return;
3418 	}
3419 
3420 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3421 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3422 		    "capability isn't as expected; pass-thru module(s) "
3423 		    "detected, discarding capability\n"));
3424 		return;
3425 	}
3426 
3427 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3428 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3429 		if (*ill_lso_capab == NULL) {
3430 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3431 			    KM_NOSLEEP);
3432 
3433 			if (*ill_lso_capab == NULL) {
3434 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3435 				    "could not enable LSO version %d "
3436 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3437 				    ill->ill_name);
3438 				return;
3439 			}
3440 		}
3441 
3442 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3443 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3444 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3445 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3446 
3447 		ip1dbg(("ill_capability_lso_ack: interface %s "
3448 		    "has enabled LSO\n ", ill->ill_name));
3449 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3450 		uint_t size;
3451 		uchar_t *rptr;
3452 
3453 		size = sizeof (dl_capability_req_t) +
3454 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3455 
3456 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3457 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3458 			    "could not enable LSO for %s (ENOMEM)\n",
3459 			    ill->ill_name);
3460 			return;
3461 		}
3462 
3463 		rptr = nmp->b_rptr;
3464 		/* initialize dl_capability_req_t */
3465 		oc = (dl_capability_req_t *)nmp->b_rptr;
3466 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3467 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3468 		    sizeof (dl_capab_lso_t);
3469 		nmp->b_rptr += sizeof (dl_capability_req_t);
3470 
3471 		/* initialize dl_capability_sub_t */
3472 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3473 		nmp->b_rptr += sizeof (*isub);
3474 
3475 		/* initialize dl_capab_lso_t */
3476 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3477 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3478 
3479 		nmp->b_rptr = rptr;
3480 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3481 
3482 		/* set ENABLE flag */
3483 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3484 
3485 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3486 		ill_dlpi_send(ill, nmp);
3487 	} else {
3488 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3489 		    "advertised %x LSO capability flags\n",
3490 		    ill->ill_name, lso_ic->lso_flags));
3491 	}
3492 }
3493 
3494 
3495 static void
3496 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3497 {
3498 	mblk_t *mp;
3499 	dl_capab_lso_t *lso_subcap;
3500 	dl_capability_sub_t *dl_subcap;
3501 	int size;
3502 
3503 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3504 		return;
3505 
3506 	ASSERT(ill->ill_lso_capab != NULL);
3507 	/*
3508 	 * Clear the capability flag for LSO but retain the
3509 	 * ill_lso_capab structure since it's possible that another
3510 	 * thread is still referring to it.  The structure only gets
3511 	 * deallocated when we destroy the ill.
3512 	 */
3513 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3514 
3515 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3516 
3517 	mp = allocb(size, BPRI_HI);
3518 	if (mp == NULL) {
3519 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3520 		    "request to disable LSO\n"));
3521 		return;
3522 	}
3523 
3524 	mp->b_wptr = mp->b_rptr + size;
3525 
3526 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3527 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3528 	dl_subcap->dl_length = sizeof (*lso_subcap);
3529 
3530 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3531 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3532 	lso_subcap->lso_flags = 0;
3533 
3534 	if (*sc_mp != NULL)
3535 		linkb(*sc_mp, mp);
3536 	else
3537 		*sc_mp = mp;
3538 }
3539 
3540 /*
3541  * Consume a new-style hardware capabilities negotiation ack.
3542  * Called from ip_rput_dlpi_writer().
3543  */
3544 void
3545 ill_capability_ack(ill_t *ill, mblk_t *mp)
3546 {
3547 	dl_capability_ack_t *capp;
3548 	dl_capability_sub_t *subp, *endp;
3549 
3550 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3551 		ill->ill_dlpi_capab_state = IDS_OK;
3552 
3553 	capp = (dl_capability_ack_t *)mp->b_rptr;
3554 
3555 	if (capp->dl_sub_length == 0)
3556 		/* no new-style capabilities */
3557 		return;
3558 
3559 	/* make sure the driver supplied correct dl_sub_length */
3560 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3561 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3562 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3563 		return;
3564 	}
3565 
3566 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3567 	/*
3568 	 * There are sub-capabilities. Process the ones we know about.
3569 	 * Loop until we don't have room for another sub-cap header..
3570 	 */
3571 	for (subp = SC(capp, capp->dl_sub_offset),
3572 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3573 	    subp <= endp;
3574 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3575 
3576 		switch (subp->dl_cap) {
3577 		case DL_CAPAB_ID_WRAPPER:
3578 			ill_capability_id_ack(ill, mp, subp);
3579 			break;
3580 		default:
3581 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3582 			break;
3583 		}
3584 	}
3585 #undef SC
3586 }
3587 
3588 /*
3589  * This routine is called to scan the fragmentation reassembly table for
3590  * the specified ILL for any packets that are starting to smell.
3591  * dead_interval is the maximum time in seconds that will be tolerated.  It
3592  * will either be the value specified in ip_g_frag_timeout, or zero if the
3593  * ILL is shutting down and it is time to blow everything off.
3594  *
3595  * It returns the number of seconds (as a time_t) that the next frag timer
3596  * should be scheduled for, 0 meaning that the timer doesn't need to be
3597  * re-started.  Note that the method of calculating next_timeout isn't
3598  * entirely accurate since time will flow between the time we grab
3599  * current_time and the time we schedule the next timeout.  This isn't a
3600  * big problem since this is the timer for sending an ICMP reassembly time
3601  * exceeded messages, and it doesn't have to be exactly accurate.
3602  *
3603  * This function is
3604  * sometimes called as writer, although this is not required.
3605  */
3606 time_t
3607 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3608 {
3609 	ipfb_t	*ipfb;
3610 	ipfb_t	*endp;
3611 	ipf_t	*ipf;
3612 	ipf_t	*ipfnext;
3613 	mblk_t	*mp;
3614 	time_t	current_time = gethrestime_sec();
3615 	time_t	next_timeout = 0;
3616 	uint32_t	hdr_length;
3617 	mblk_t	*send_icmp_head;
3618 	mblk_t	*send_icmp_head_v6;
3619 	zoneid_t zoneid;
3620 	ip_stack_t *ipst = ill->ill_ipst;
3621 
3622 	ipfb = ill->ill_frag_hash_tbl;
3623 	if (ipfb == NULL)
3624 		return (B_FALSE);
3625 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3626 	/* Walk the frag hash table. */
3627 	for (; ipfb < endp; ipfb++) {
3628 		send_icmp_head = NULL;
3629 		send_icmp_head_v6 = NULL;
3630 		mutex_enter(&ipfb->ipfb_lock);
3631 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3632 			time_t frag_time = current_time - ipf->ipf_timestamp;
3633 			time_t frag_timeout;
3634 
3635 			if (frag_time < dead_interval) {
3636 				/*
3637 				 * There are some outstanding fragments
3638 				 * that will timeout later.  Make note of
3639 				 * the time so that we can reschedule the
3640 				 * next timeout appropriately.
3641 				 */
3642 				frag_timeout = dead_interval - frag_time;
3643 				if (next_timeout == 0 ||
3644 				    frag_timeout < next_timeout) {
3645 					next_timeout = frag_timeout;
3646 				}
3647 				break;
3648 			}
3649 			/* Time's up.  Get it out of here. */
3650 			hdr_length = ipf->ipf_nf_hdr_len;
3651 			ipfnext = ipf->ipf_hash_next;
3652 			if (ipfnext)
3653 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3654 			*ipf->ipf_ptphn = ipfnext;
3655 			mp = ipf->ipf_mp->b_cont;
3656 			for (; mp; mp = mp->b_cont) {
3657 				/* Extra points for neatness. */
3658 				IP_REASS_SET_START(mp, 0);
3659 				IP_REASS_SET_END(mp, 0);
3660 			}
3661 			mp = ipf->ipf_mp->b_cont;
3662 			ill->ill_frag_count -= ipf->ipf_count;
3663 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3664 			ipfb->ipfb_count -= ipf->ipf_count;
3665 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3666 			ipfb->ipfb_frag_pkts--;
3667 			/*
3668 			 * We do not send any icmp message from here because
3669 			 * we currently are holding the ipfb_lock for this
3670 			 * hash chain. If we try and send any icmp messages
3671 			 * from here we may end up via a put back into ip
3672 			 * trying to get the same lock, causing a recursive
3673 			 * mutex panic. Instead we build a list and send all
3674 			 * the icmp messages after we have dropped the lock.
3675 			 */
3676 			if (ill->ill_isv6) {
3677 				if (hdr_length != 0) {
3678 					mp->b_next = send_icmp_head_v6;
3679 					send_icmp_head_v6 = mp;
3680 				} else {
3681 					freemsg(mp);
3682 				}
3683 			} else {
3684 				if (hdr_length != 0) {
3685 					mp->b_next = send_icmp_head;
3686 					send_icmp_head = mp;
3687 				} else {
3688 					freemsg(mp);
3689 				}
3690 			}
3691 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3692 			freeb(ipf->ipf_mp);
3693 		}
3694 		mutex_exit(&ipfb->ipfb_lock);
3695 		/*
3696 		 * Now need to send any icmp messages that we delayed from
3697 		 * above.
3698 		 */
3699 		while (send_icmp_head_v6 != NULL) {
3700 			ip6_t *ip6h;
3701 
3702 			mp = send_icmp_head_v6;
3703 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3704 			mp->b_next = NULL;
3705 			if (mp->b_datap->db_type == M_CTL)
3706 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3707 			else
3708 				ip6h = (ip6_t *)mp->b_rptr;
3709 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3710 			    ill, ipst);
3711 			if (zoneid == ALL_ZONES) {
3712 				freemsg(mp);
3713 			} else {
3714 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3715 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3716 				    B_FALSE, zoneid, ipst);
3717 			}
3718 		}
3719 		while (send_icmp_head != NULL) {
3720 			ipaddr_t dst;
3721 
3722 			mp = send_icmp_head;
3723 			send_icmp_head = send_icmp_head->b_next;
3724 			mp->b_next = NULL;
3725 
3726 			if (mp->b_datap->db_type == M_CTL)
3727 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3728 			else
3729 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3730 
3731 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3732 			if (zoneid == ALL_ZONES) {
3733 				freemsg(mp);
3734 			} else {
3735 				icmp_time_exceeded(ill->ill_wq, mp,
3736 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3737 				    ipst);
3738 			}
3739 		}
3740 	}
3741 	/*
3742 	 * A non-dying ILL will use the return value to decide whether to
3743 	 * restart the frag timer, and for how long.
3744 	 */
3745 	return (next_timeout);
3746 }
3747 
3748 /*
3749  * This routine is called when the approximate count of mblk memory used
3750  * for the specified ILL has exceeded max_count.
3751  */
3752 void
3753 ill_frag_prune(ill_t *ill, uint_t max_count)
3754 {
3755 	ipfb_t	*ipfb;
3756 	ipf_t	*ipf;
3757 	size_t	count;
3758 
3759 	/*
3760 	 * If we are here within ip_min_frag_prune_time msecs remove
3761 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3762 	 * ill_frag_free_num_pkts.
3763 	 */
3764 	mutex_enter(&ill->ill_lock);
3765 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3766 	    (ip_min_frag_prune_time != 0 ?
3767 	    ip_min_frag_prune_time : msec_per_tick)) {
3768 
3769 		ill->ill_frag_free_num_pkts++;
3770 
3771 	} else {
3772 		ill->ill_frag_free_num_pkts = 0;
3773 	}
3774 	ill->ill_last_frag_clean_time = lbolt;
3775 	mutex_exit(&ill->ill_lock);
3776 
3777 	/*
3778 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3779 	 */
3780 	if (ill->ill_frag_free_num_pkts != 0) {
3781 		int ix;
3782 
3783 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3784 			ipfb = &ill->ill_frag_hash_tbl[ix];
3785 			mutex_enter(&ipfb->ipfb_lock);
3786 			if (ipfb->ipfb_ipf != NULL) {
3787 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3788 				    ill->ill_frag_free_num_pkts);
3789 			}
3790 			mutex_exit(&ipfb->ipfb_lock);
3791 		}
3792 	}
3793 	/*
3794 	 * While the reassembly list for this ILL is too big, prune a fragment
3795 	 * queue by age, oldest first.  Note that the per ILL count is
3796 	 * approximate, while the per frag hash bucket counts are accurate.
3797 	 */
3798 	while (ill->ill_frag_count > max_count) {
3799 		int	ix;
3800 		ipfb_t	*oipfb = NULL;
3801 		uint_t	oldest = UINT_MAX;
3802 
3803 		count = 0;
3804 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3805 			ipfb = &ill->ill_frag_hash_tbl[ix];
3806 			mutex_enter(&ipfb->ipfb_lock);
3807 			ipf = ipfb->ipfb_ipf;
3808 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3809 				oldest = ipf->ipf_gen;
3810 				oipfb = ipfb;
3811 			}
3812 			count += ipfb->ipfb_count;
3813 			mutex_exit(&ipfb->ipfb_lock);
3814 		}
3815 		/* Refresh the per ILL count */
3816 		ill->ill_frag_count = count;
3817 		if (oipfb == NULL) {
3818 			ill->ill_frag_count = 0;
3819 			break;
3820 		}
3821 		if (count <= max_count)
3822 			return;	/* Somebody beat us to it, nothing to do */
3823 		mutex_enter(&oipfb->ipfb_lock);
3824 		ipf = oipfb->ipfb_ipf;
3825 		if (ipf != NULL) {
3826 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3827 		}
3828 		mutex_exit(&oipfb->ipfb_lock);
3829 	}
3830 }
3831 
3832 /*
3833  * free 'free_cnt' fragmented packets starting at ipf.
3834  */
3835 void
3836 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3837 {
3838 	size_t	count;
3839 	mblk_t	*mp;
3840 	mblk_t	*tmp;
3841 	ipf_t **ipfp = ipf->ipf_ptphn;
3842 
3843 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3844 	ASSERT(ipfp != NULL);
3845 	ASSERT(ipf != NULL);
3846 
3847 	while (ipf != NULL && free_cnt-- > 0) {
3848 		count = ipf->ipf_count;
3849 		mp = ipf->ipf_mp;
3850 		ipf = ipf->ipf_hash_next;
3851 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3852 			IP_REASS_SET_START(tmp, 0);
3853 			IP_REASS_SET_END(tmp, 0);
3854 		}
3855 		ill->ill_frag_count -= count;
3856 		ASSERT(ipfb->ipfb_count >= count);
3857 		ipfb->ipfb_count -= count;
3858 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3859 		ipfb->ipfb_frag_pkts--;
3860 		freemsg(mp);
3861 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3862 	}
3863 
3864 	if (ipf)
3865 		ipf->ipf_ptphn = ipfp;
3866 	ipfp[0] = ipf;
3867 }
3868 
3869 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3870 	"obsolete and may be removed in a future release of Solaris.  Use " \
3871 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3872 
3873 /*
3874  * For obsolete per-interface forwarding configuration;
3875  * called in response to ND_GET.
3876  */
3877 /* ARGSUSED */
3878 static int
3879 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3880 {
3881 	ill_t *ill = (ill_t *)cp;
3882 
3883 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3884 
3885 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3886 	return (0);
3887 }
3888 
3889 /*
3890  * For obsolete per-interface forwarding configuration;
3891  * called in response to ND_SET.
3892  */
3893 /* ARGSUSED */
3894 static int
3895 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3896     cred_t *ioc_cr)
3897 {
3898 	long value;
3899 	int retval;
3900 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3901 
3902 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3903 
3904 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3905 	    value < 0 || value > 1) {
3906 		return (EINVAL);
3907 	}
3908 
3909 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3910 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3911 	rw_exit(&ipst->ips_ill_g_lock);
3912 	return (retval);
3913 }
3914 
3915 /*
3916  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3917  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3918  * up RTS_IFINFO routing socket messages for each interface whose flags we
3919  * change.
3920  */
3921 int
3922 ill_forward_set(ill_t *ill, boolean_t enable)
3923 {
3924 	ill_group_t *illgrp;
3925 	ip_stack_t	*ipst = ill->ill_ipst;
3926 
3927 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3928 
3929 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3930 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3931 		return (0);
3932 
3933 	if (IS_LOOPBACK(ill))
3934 		return (EINVAL);
3935 
3936 	/*
3937 	 * If the ill is in an IPMP group, set the forwarding policy on all
3938 	 * members of the group to the same value.
3939 	 */
3940 	illgrp = ill->ill_group;
3941 	if (illgrp != NULL) {
3942 		ill_t *tmp_ill;
3943 
3944 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3945 		    tmp_ill = tmp_ill->ill_group_next) {
3946 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3947 			    (enable ? "Enabling" : "Disabling"),
3948 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3949 			    tmp_ill->ill_name));
3950 			mutex_enter(&tmp_ill->ill_lock);
3951 			if (enable)
3952 				tmp_ill->ill_flags |= ILLF_ROUTER;
3953 			else
3954 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3955 			mutex_exit(&tmp_ill->ill_lock);
3956 			if (tmp_ill->ill_isv6)
3957 				ill_set_nce_router_flags(tmp_ill, enable);
3958 			/* Notify routing socket listeners of this change. */
3959 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3960 		}
3961 	} else {
3962 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3963 		    (enable ? "Enabling" : "Disabling"),
3964 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3965 		mutex_enter(&ill->ill_lock);
3966 		if (enable)
3967 			ill->ill_flags |= ILLF_ROUTER;
3968 		else
3969 			ill->ill_flags &= ~ILLF_ROUTER;
3970 		mutex_exit(&ill->ill_lock);
3971 		if (ill->ill_isv6)
3972 			ill_set_nce_router_flags(ill, enable);
3973 		/* Notify routing socket listeners of this change. */
3974 		ip_rts_ifmsg(ill->ill_ipif);
3975 	}
3976 
3977 	return (0);
3978 }
3979 
3980 /*
3981  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3982  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3983  * set or clear.
3984  */
3985 static void
3986 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3987 {
3988 	ipif_t *ipif;
3989 	nce_t *nce;
3990 
3991 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3992 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3993 		if (nce != NULL) {
3994 			mutex_enter(&nce->nce_lock);
3995 			if (enable)
3996 				nce->nce_flags |= NCE_F_ISROUTER;
3997 			else
3998 				nce->nce_flags &= ~NCE_F_ISROUTER;
3999 			mutex_exit(&nce->nce_lock);
4000 			NCE_REFRELE(nce);
4001 		}
4002 	}
4003 }
4004 
4005 /*
4006  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
4007  * for this ill.  Make sure the v6/v4 question has been answered about this
4008  * ill.  The creation of this ndd variable is only for backwards compatibility.
4009  * The preferred way to control per-interface IP forwarding is through the
4010  * ILLF_ROUTER interface flag.
4011  */
4012 static int
4013 ill_set_ndd_name(ill_t *ill)
4014 {
4015 	char *suffix;
4016 	ip_stack_t	*ipst = ill->ill_ipst;
4017 
4018 	ASSERT(IAM_WRITER_ILL(ill));
4019 
4020 	if (ill->ill_isv6)
4021 		suffix = ipv6_forward_suffix;
4022 	else
4023 		suffix = ipv4_forward_suffix;
4024 
4025 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4026 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4027 	/*
4028 	 * Copies over the '\0'.
4029 	 * Note that strlen(suffix) is always bounded.
4030 	 */
4031 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4032 	    strlen(suffix) + 1);
4033 
4034 	/*
4035 	 * Use of the nd table requires holding the reader lock.
4036 	 * Modifying the nd table thru nd_load/nd_unload requires
4037 	 * the writer lock.
4038 	 */
4039 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
4040 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4041 	    nd_ill_forward_set, (caddr_t)ill)) {
4042 		/*
4043 		 * If the nd_load failed, it only meant that it could not
4044 		 * allocate a new bunch of room for further NDD expansion.
4045 		 * Because of that, the ill_ndd_name will be set to 0, and
4046 		 * this interface is at the mercy of the global ip_forwarding
4047 		 * variable.
4048 		 */
4049 		rw_exit(&ipst->ips_ip_g_nd_lock);
4050 		ill->ill_ndd_name = NULL;
4051 		return (ENOMEM);
4052 	}
4053 	rw_exit(&ipst->ips_ip_g_nd_lock);
4054 	return (0);
4055 }
4056 
4057 /*
4058  * Intializes the context structure and returns the first ill in the list
4059  * cuurently start_list and end_list can have values:
4060  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4061  * IP_V4_G_HEAD		Traverse IPV4 list only.
4062  * IP_V6_G_HEAD		Traverse IPV6 list only.
4063  */
4064 
4065 /*
4066  * We don't check for CONDEMNED ills here. Caller must do that if
4067  * necessary under the ill lock.
4068  */
4069 ill_t *
4070 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
4071     ip_stack_t *ipst)
4072 {
4073 	ill_if_t *ifp;
4074 	ill_t *ill;
4075 	avl_tree_t *avl_tree;
4076 
4077 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4078 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4079 
4080 	/*
4081 	 * setup the lists to search
4082 	 */
4083 	if (end_list != MAX_G_HEADS) {
4084 		ctx->ctx_current_list = start_list;
4085 		ctx->ctx_last_list = end_list;
4086 	} else {
4087 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4088 		ctx->ctx_current_list = 0;
4089 	}
4090 
4091 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4092 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4093 		if (ifp != (ill_if_t *)
4094 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4095 			avl_tree = &ifp->illif_avl_by_ppa;
4096 			ill = avl_first(avl_tree);
4097 			/*
4098 			 * ill is guaranteed to be non NULL or ifp should have
4099 			 * not existed.
4100 			 */
4101 			ASSERT(ill != NULL);
4102 			return (ill);
4103 		}
4104 		ctx->ctx_current_list++;
4105 	}
4106 
4107 	return (NULL);
4108 }
4109 
4110 /*
4111  * returns the next ill in the list. ill_first() must have been called
4112  * before calling ill_next() or bad things will happen.
4113  */
4114 
4115 /*
4116  * We don't check for CONDEMNED ills here. Caller must do that if
4117  * necessary under the ill lock.
4118  */
4119 ill_t *
4120 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4121 {
4122 	ill_if_t *ifp;
4123 	ill_t *ill;
4124 	ip_stack_t	*ipst = lastill->ill_ipst;
4125 
4126 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4127 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
4128 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4129 	    AVL_AFTER)) != NULL) {
4130 		return (ill);
4131 	}
4132 
4133 	/* goto next ill_ifp in the list. */
4134 	ifp = lastill->ill_ifptr->illif_next;
4135 
4136 	/* make sure not at end of circular list */
4137 	while (ifp ==
4138 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4139 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4140 			return (NULL);
4141 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4142 	}
4143 
4144 	return (avl_first(&ifp->illif_avl_by_ppa));
4145 }
4146 
4147 /*
4148  * Check interface name for correct format which is name+ppa.
4149  * name can contain characters and digits, the right most digits
4150  * make up the ppa number. use of octal is not allowed, name must contain
4151  * a ppa, return pointer to the start of ppa.
4152  * In case of error return NULL.
4153  */
4154 static char *
4155 ill_get_ppa_ptr(char *name)
4156 {
4157 	int namelen = mi_strlen(name);
4158 
4159 	int len = namelen;
4160 
4161 	name += len;
4162 	while (len > 0) {
4163 		name--;
4164 		if (*name < '0' || *name > '9')
4165 			break;
4166 		len--;
4167 	}
4168 
4169 	/* empty string, all digits, or no trailing digits */
4170 	if (len == 0 || len == (int)namelen)
4171 		return (NULL);
4172 
4173 	name++;
4174 	/* check for attempted use of octal */
4175 	if (*name == '0' && len != (int)namelen - 1)
4176 		return (NULL);
4177 	return (name);
4178 }
4179 
4180 /*
4181  * use avl tree to locate the ill.
4182  */
4183 static ill_t *
4184 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4185     ipsq_func_t func, int *error, ip_stack_t *ipst)
4186 {
4187 	char *ppa_ptr = NULL;
4188 	int len;
4189 	uint_t ppa;
4190 	ill_t *ill = NULL;
4191 	ill_if_t *ifp;
4192 	int list;
4193 	ipsq_t *ipsq;
4194 
4195 	if (error != NULL)
4196 		*error = 0;
4197 
4198 	/*
4199 	 * get ppa ptr
4200 	 */
4201 	if (isv6)
4202 		list = IP_V6_G_HEAD;
4203 	else
4204 		list = IP_V4_G_HEAD;
4205 
4206 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4207 		if (error != NULL)
4208 			*error = ENXIO;
4209 		return (NULL);
4210 	}
4211 
4212 	len = ppa_ptr - name + 1;
4213 
4214 	ppa = stoi(&ppa_ptr);
4215 
4216 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4217 
4218 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4219 		/*
4220 		 * match is done on len - 1 as the name is not null
4221 		 * terminated it contains ppa in addition to the interface
4222 		 * name.
4223 		 */
4224 		if ((ifp->illif_name_len == len) &&
4225 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4226 			break;
4227 		} else {
4228 			ifp = ifp->illif_next;
4229 		}
4230 	}
4231 
4232 
4233 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4234 		/*
4235 		 * Even the interface type does not exist.
4236 		 */
4237 		if (error != NULL)
4238 			*error = ENXIO;
4239 		return (NULL);
4240 	}
4241 
4242 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4243 	if (ill != NULL) {
4244 		/*
4245 		 * The block comment at the start of ipif_down
4246 		 * explains the use of the macros used below
4247 		 */
4248 		GRAB_CONN_LOCK(q);
4249 		mutex_enter(&ill->ill_lock);
4250 		if (ILL_CAN_LOOKUP(ill)) {
4251 			ill_refhold_locked(ill);
4252 			mutex_exit(&ill->ill_lock);
4253 			RELEASE_CONN_LOCK(q);
4254 			return (ill);
4255 		} else if (ILL_CAN_WAIT(ill, q)) {
4256 			ipsq = ill->ill_phyint->phyint_ipsq;
4257 			mutex_enter(&ipsq->ipsq_lock);
4258 			mutex_exit(&ill->ill_lock);
4259 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4260 			mutex_exit(&ipsq->ipsq_lock);
4261 			RELEASE_CONN_LOCK(q);
4262 			if (error != NULL)
4263 				*error = EINPROGRESS;
4264 			return (NULL);
4265 		}
4266 		mutex_exit(&ill->ill_lock);
4267 		RELEASE_CONN_LOCK(q);
4268 	}
4269 	if (error != NULL)
4270 		*error = ENXIO;
4271 	return (NULL);
4272 }
4273 
4274 /*
4275  * comparison function for use with avl.
4276  */
4277 static int
4278 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4279 {
4280 	uint_t ppa;
4281 	uint_t ill_ppa;
4282 
4283 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4284 
4285 	ppa = *((uint_t *)ppa_ptr);
4286 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4287 	/*
4288 	 * We want the ill with the lowest ppa to be on the
4289 	 * top.
4290 	 */
4291 	if (ill_ppa < ppa)
4292 		return (1);
4293 	if (ill_ppa > ppa)
4294 		return (-1);
4295 	return (0);
4296 }
4297 
4298 /*
4299  * remove an interface type from the global list.
4300  */
4301 static void
4302 ill_delete_interface_type(ill_if_t *interface)
4303 {
4304 	ASSERT(interface != NULL);
4305 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4306 
4307 	avl_destroy(&interface->illif_avl_by_ppa);
4308 	if (interface->illif_ppa_arena != NULL)
4309 		vmem_destroy(interface->illif_ppa_arena);
4310 
4311 	remque(interface);
4312 
4313 	mi_free(interface);
4314 }
4315 
4316 /*
4317  * remove ill from the global list.
4318  */
4319 static void
4320 ill_glist_delete(ill_t *ill)
4321 {
4322 	hook_nic_event_t *info;
4323 	ip_stack_t	*ipst;
4324 
4325 	if (ill == NULL)
4326 		return;
4327 	ipst = ill->ill_ipst;
4328 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4329 
4330 	/*
4331 	 * If the ill was never inserted into the AVL tree
4332 	 * we skip the if branch.
4333 	 */
4334 	if (ill->ill_ifptr != NULL) {
4335 		/*
4336 		 * remove from AVL tree and free ppa number
4337 		 */
4338 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4339 
4340 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4341 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4342 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4343 		}
4344 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4345 			ill_delete_interface_type(ill->ill_ifptr);
4346 		}
4347 
4348 		/*
4349 		 * Indicate ill is no longer in the list.
4350 		 */
4351 		ill->ill_ifptr = NULL;
4352 		ill->ill_name_length = 0;
4353 		ill->ill_name[0] = '\0';
4354 		ill->ill_ppa = UINT_MAX;
4355 	}
4356 
4357 	/*
4358 	 * Run the unplumb hook after the NIC has disappeared from being
4359 	 * visible so that attempts to revalidate its existance will fail.
4360 	 *
4361 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4362 	 * that the ordering of delivered events to listeners matches the
4363 	 * order of them in the kernel.
4364 	 */
4365 	info = ill->ill_nic_event_info;
4366 	if (info != NULL && info->hne_event == NE_DOWN) {
4367 		mutex_enter(&ill->ill_lock);
4368 		ill_nic_info_dispatch(ill);
4369 		mutex_exit(&ill->ill_lock);
4370 	}
4371 
4372 	/* Generate NE_UNPLUMB event for ill_name. */
4373 	(void) ill_hook_event_create(ill, 0, NE_UNPLUMB, ill->ill_name,
4374 	    ill->ill_name_length);
4375 
4376 	ill_phyint_free(ill);
4377 	rw_exit(&ipst->ips_ill_g_lock);
4378 }
4379 
4380 /*
4381  * allocate a ppa, if the number of plumbed interfaces of this type are
4382  * less than ill_no_arena do a linear search to find a unused ppa.
4383  * When the number goes beyond ill_no_arena switch to using an arena.
4384  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4385  * is the return value for an error condition, so allocation starts at one
4386  * and is decremented by one.
4387  */
4388 static int
4389 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4390 {
4391 	ill_t *tmp_ill;
4392 	uint_t start, end;
4393 	int ppa;
4394 
4395 	if (ifp->illif_ppa_arena == NULL &&
4396 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4397 		/*
4398 		 * Create an arena.
4399 		 */
4400 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4401 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4402 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4403 			/* allocate what has already been assigned */
4404 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4405 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4406 		    tmp_ill, AVL_AFTER)) {
4407 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4408 			    1,		/* size */
4409 			    1,		/* align/quantum */
4410 			    0,		/* phase */
4411 			    0,		/* nocross */
4412 			    /* minaddr */
4413 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4414 			    /* maxaddr */
4415 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4416 			    VM_NOSLEEP|VM_FIRSTFIT);
4417 			if (ppa == 0) {
4418 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4419 				    " failed while switching"));
4420 				vmem_destroy(ifp->illif_ppa_arena);
4421 				ifp->illif_ppa_arena = NULL;
4422 				break;
4423 			}
4424 		}
4425 	}
4426 
4427 	if (ifp->illif_ppa_arena != NULL) {
4428 		if (ill->ill_ppa == UINT_MAX) {
4429 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4430 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4431 			if (ppa == 0)
4432 				return (EAGAIN);
4433 			ill->ill_ppa = --ppa;
4434 		} else {
4435 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4436 			    1, 		/* size */
4437 			    1, 		/* align/quantum */
4438 			    0, 		/* phase */
4439 			    0, 		/* nocross */
4440 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4441 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4442 			    VM_NOSLEEP|VM_FIRSTFIT);
4443 			/*
4444 			 * Most likely the allocation failed because
4445 			 * the requested ppa was in use.
4446 			 */
4447 			if (ppa == 0)
4448 				return (EEXIST);
4449 		}
4450 		return (0);
4451 	}
4452 
4453 	/*
4454 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4455 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4456 	 */
4457 	if (ill->ill_ppa == UINT_MAX) {
4458 		end = UINT_MAX - 1;
4459 		start = 0;
4460 	} else {
4461 		end = start = ill->ill_ppa;
4462 	}
4463 
4464 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4465 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4466 		if (start++ >= end) {
4467 			if (ill->ill_ppa == UINT_MAX)
4468 				return (EAGAIN);
4469 			else
4470 				return (EEXIST);
4471 		}
4472 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4473 	}
4474 	ill->ill_ppa = start;
4475 	return (0);
4476 }
4477 
4478 /*
4479  * Insert ill into the list of configured ill's. Once this function completes,
4480  * the ill is globally visible and is available through lookups. More precisely
4481  * this happens after the caller drops the ill_g_lock.
4482  */
4483 static int
4484 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4485 {
4486 	ill_if_t *ill_interface;
4487 	avl_index_t where = 0;
4488 	int error;
4489 	int name_length;
4490 	int index;
4491 	boolean_t check_length = B_FALSE;
4492 	ip_stack_t	*ipst = ill->ill_ipst;
4493 
4494 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4495 
4496 	name_length = mi_strlen(name) + 1;
4497 
4498 	if (isv6)
4499 		index = IP_V6_G_HEAD;
4500 	else
4501 		index = IP_V4_G_HEAD;
4502 
4503 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4504 	/*
4505 	 * Search for interface type based on name
4506 	 */
4507 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4508 		if ((ill_interface->illif_name_len == name_length) &&
4509 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4510 			break;
4511 		}
4512 		ill_interface = ill_interface->illif_next;
4513 	}
4514 
4515 	/*
4516 	 * Interface type not found, create one.
4517 	 */
4518 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4519 
4520 		ill_g_head_t ghead;
4521 
4522 		/*
4523 		 * allocate ill_if_t structure
4524 		 */
4525 
4526 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4527 		if (ill_interface == NULL) {
4528 			return (ENOMEM);
4529 		}
4530 
4531 
4532 
4533 		(void) strcpy(ill_interface->illif_name, name);
4534 		ill_interface->illif_name_len = name_length;
4535 
4536 		avl_create(&ill_interface->illif_avl_by_ppa,
4537 		    ill_compare_ppa, sizeof (ill_t),
4538 		    offsetof(struct ill_s, ill_avl_byppa));
4539 
4540 		/*
4541 		 * link the structure in the back to maintain order
4542 		 * of configuration for ifconfig output.
4543 		 */
4544 		ghead = ipst->ips_ill_g_heads[index];
4545 		insque(ill_interface, ghead.ill_g_list_tail);
4546 
4547 	}
4548 
4549 	if (ill->ill_ppa == UINT_MAX)
4550 		check_length = B_TRUE;
4551 
4552 	error = ill_alloc_ppa(ill_interface, ill);
4553 	if (error != 0) {
4554 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4555 			ill_delete_interface_type(ill->ill_ifptr);
4556 		return (error);
4557 	}
4558 
4559 	/*
4560 	 * When the ppa is choosen by the system, check that there is
4561 	 * enough space to insert ppa. if a specific ppa was passed in this
4562 	 * check is not required as the interface name passed in will have
4563 	 * the right ppa in it.
4564 	 */
4565 	if (check_length) {
4566 		/*
4567 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4568 		 */
4569 		char buf[sizeof (uint_t) * 3];
4570 
4571 		/*
4572 		 * convert ppa to string to calculate the amount of space
4573 		 * required for it in the name.
4574 		 */
4575 		numtos(ill->ill_ppa, buf);
4576 
4577 		/* Do we have enough space to insert ppa ? */
4578 
4579 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4580 			/* Free ppa and interface type struct */
4581 			if (ill_interface->illif_ppa_arena != NULL) {
4582 				vmem_free(ill_interface->illif_ppa_arena,
4583 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4584 			}
4585 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4586 			    0) {
4587 				ill_delete_interface_type(ill->ill_ifptr);
4588 			}
4589 
4590 			return (EINVAL);
4591 		}
4592 	}
4593 
4594 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4595 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4596 
4597 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4598 	    &where);
4599 	ill->ill_ifptr = ill_interface;
4600 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4601 
4602 	ill_phyint_reinit(ill);
4603 	return (0);
4604 }
4605 
4606 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4607 static boolean_t
4608 ipsq_init(ill_t *ill)
4609 {
4610 	ipsq_t  *ipsq;
4611 
4612 	/* Init the ipsq and impicitly enter as writer */
4613 	ill->ill_phyint->phyint_ipsq =
4614 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4615 	if (ill->ill_phyint->phyint_ipsq == NULL)
4616 		return (B_FALSE);
4617 	ipsq = ill->ill_phyint->phyint_ipsq;
4618 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4619 	ill->ill_phyint->phyint_ipsq_next = NULL;
4620 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4621 	ipsq->ipsq_refs = 1;
4622 	ipsq->ipsq_writer = curthread;
4623 	ipsq->ipsq_reentry_cnt = 1;
4624 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4625 #ifdef DEBUG
4626 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack,
4627 	    IPSQ_STACK_DEPTH);
4628 #endif
4629 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4630 	return (B_TRUE);
4631 }
4632 
4633 /*
4634  * ill_init is called by ip_open when a device control stream is opened.
4635  * It does a few initializations, and shoots a DL_INFO_REQ message down
4636  * to the driver.  The response is later picked up in ip_rput_dlpi and
4637  * used to set up default mechanisms for talking to the driver.  (Always
4638  * called as writer.)
4639  *
4640  * If this function returns error, ip_open will call ip_close which in
4641  * turn will call ill_delete to clean up any memory allocated here that
4642  * is not yet freed.
4643  */
4644 int
4645 ill_init(queue_t *q, ill_t *ill)
4646 {
4647 	int	count;
4648 	dl_info_req_t	*dlir;
4649 	mblk_t	*info_mp;
4650 	uchar_t *frag_ptr;
4651 
4652 	/*
4653 	 * The ill is initialized to zero by mi_alloc*(). In addition
4654 	 * some fields already contain valid values, initialized in
4655 	 * ip_open(), before we reach here.
4656 	 */
4657 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4658 
4659 	ill->ill_rq = q;
4660 	ill->ill_wq = WR(q);
4661 
4662 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4663 	    BPRI_HI);
4664 	if (info_mp == NULL)
4665 		return (ENOMEM);
4666 
4667 	/*
4668 	 * Allocate sufficient space to contain our fragment hash table and
4669 	 * the device name.
4670 	 */
4671 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4672 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4673 	if (frag_ptr == NULL) {
4674 		freemsg(info_mp);
4675 		return (ENOMEM);
4676 	}
4677 	ill->ill_frag_ptr = frag_ptr;
4678 	ill->ill_frag_free_num_pkts = 0;
4679 	ill->ill_last_frag_clean_time = 0;
4680 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4681 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4682 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4683 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4684 		    NULL, MUTEX_DEFAULT, NULL);
4685 	}
4686 
4687 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4688 	if (ill->ill_phyint == NULL) {
4689 		freemsg(info_mp);
4690 		mi_free(frag_ptr);
4691 		return (ENOMEM);
4692 	}
4693 
4694 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4695 	/*
4696 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4697 	 * at this point because of the following reason. If we can't
4698 	 * enter the ipsq at some point and cv_wait, the writer that
4699 	 * wakes us up tries to locate us using the list of all phyints
4700 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4701 	 * If we don't set it now, we risk a missed wakeup.
4702 	 */
4703 	ill->ill_phyint->phyint_illv4 = ill;
4704 	ill->ill_ppa = UINT_MAX;
4705 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4706 
4707 	if (!ipsq_init(ill)) {
4708 		freemsg(info_mp);
4709 		mi_free(frag_ptr);
4710 		mi_free(ill->ill_phyint);
4711 		return (ENOMEM);
4712 	}
4713 
4714 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4715 
4716 
4717 	/* Frag queue limit stuff */
4718 	ill->ill_frag_count = 0;
4719 	ill->ill_ipf_gen = 0;
4720 
4721 	ill->ill_global_timer = INFINITY;
4722 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4723 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4724 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4725 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4726 
4727 	/*
4728 	 * Initialize IPv6 configuration variables.  The IP module is always
4729 	 * opened as an IPv4 module.  Instead tracking down the cases where
4730 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4731 	 * here for convenience, this has no effect until the ill is set to do
4732 	 * IPv6.
4733 	 */
4734 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4735 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4736 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4737 	ill->ill_max_buf = ND_MAX_Q;
4738 	ill->ill_refcnt = 0;
4739 
4740 	/* Send down the Info Request to the driver. */
4741 	info_mp->b_datap->db_type = M_PCPROTO;
4742 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4743 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4744 	dlir->dl_primitive = DL_INFO_REQ;
4745 
4746 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4747 
4748 	qprocson(q);
4749 	ill_dlpi_send(ill, info_mp);
4750 
4751 	return (0);
4752 }
4753 
4754 /*
4755  * ill_dls_info
4756  * creates datalink socket info from the device.
4757  */
4758 int
4759 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4760 {
4761 	size_t	len;
4762 	ill_t	*ill = ipif->ipif_ill;
4763 
4764 	sdl->sdl_family = AF_LINK;
4765 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4766 	sdl->sdl_type = ill->ill_type;
4767 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4768 	len = strlen(sdl->sdl_data);
4769 	ASSERT(len < 256);
4770 	sdl->sdl_nlen = (uchar_t)len;
4771 	sdl->sdl_alen = ill->ill_phys_addr_length;
4772 	sdl->sdl_slen = 0;
4773 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4774 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4775 
4776 	return (sizeof (struct sockaddr_dl));
4777 }
4778 
4779 /*
4780  * ill_xarp_info
4781  * creates xarp info from the device.
4782  */
4783 static int
4784 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4785 {
4786 	sdl->sdl_family = AF_LINK;
4787 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4788 	sdl->sdl_type = ill->ill_type;
4789 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4790 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4791 	sdl->sdl_alen = ill->ill_phys_addr_length;
4792 	sdl->sdl_slen = 0;
4793 	return (sdl->sdl_nlen);
4794 }
4795 
4796 static int
4797 loopback_kstat_update(kstat_t *ksp, int rw)
4798 {
4799 	kstat_named_t *kn;
4800 	netstackid_t	stackid;
4801 	netstack_t	*ns;
4802 	ip_stack_t	*ipst;
4803 
4804 	if (ksp == NULL || ksp->ks_data == NULL)
4805 		return (EIO);
4806 
4807 	if (rw == KSTAT_WRITE)
4808 		return (EACCES);
4809 
4810 	kn = KSTAT_NAMED_PTR(ksp);
4811 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4812 
4813 	ns = netstack_find_by_stackid(stackid);
4814 	if (ns == NULL)
4815 		return (-1);
4816 
4817 	ipst = ns->netstack_ip;
4818 	if (ipst == NULL) {
4819 		netstack_rele(ns);
4820 		return (-1);
4821 	}
4822 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4823 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4824 	netstack_rele(ns);
4825 	return (0);
4826 }
4827 
4828 
4829 /*
4830  * Has ifindex been plumbed already.
4831  * Compares both phyint_ifindex and phyint_group_ifindex.
4832  */
4833 static boolean_t
4834 phyint_exists(uint_t index, ip_stack_t *ipst)
4835 {
4836 	phyint_t *phyi;
4837 
4838 	ASSERT(index != 0);
4839 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4840 	/*
4841 	 * Indexes are stored in the phyint - a common structure
4842 	 * to both IPv4 and IPv6.
4843 	 */
4844 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4845 	for (; phyi != NULL;
4846 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4847 	    phyi, AVL_AFTER)) {
4848 		if (phyi->phyint_ifindex == index ||
4849 		    phyi->phyint_group_ifindex == index)
4850 			return (B_TRUE);
4851 	}
4852 	return (B_FALSE);
4853 }
4854 
4855 /* Pick a unique ifindex */
4856 boolean_t
4857 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4858 {
4859 	uint_t starting_index;
4860 
4861 	if (!ipst->ips_ill_index_wrap) {
4862 		*indexp = ipst->ips_ill_index++;
4863 		if (ipst->ips_ill_index == 0) {
4864 			/* Reached the uint_t limit Next time wrap  */
4865 			ipst->ips_ill_index_wrap = B_TRUE;
4866 		}
4867 		return (B_TRUE);
4868 	}
4869 
4870 	/*
4871 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4872 	 * at this point and don't want to call any function that attempts
4873 	 * to get the lock again.
4874 	 */
4875 	starting_index = ipst->ips_ill_index++;
4876 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4877 		if (ipst->ips_ill_index != 0 &&
4878 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4879 			/* found unused index - use it */
4880 			*indexp = ipst->ips_ill_index;
4881 			return (B_TRUE);
4882 		}
4883 	}
4884 
4885 	/*
4886 	 * all interface indicies are inuse.
4887 	 */
4888 	return (B_FALSE);
4889 }
4890 
4891 /*
4892  * Assign a unique interface index for the phyint.
4893  */
4894 static boolean_t
4895 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4896 {
4897 	ASSERT(phyi->phyint_ifindex == 0);
4898 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4899 }
4900 
4901 /*
4902  * Return a pointer to the ill which matches the supplied name.  Note that
4903  * the ill name length includes the null termination character.  (May be
4904  * called as writer.)
4905  * If do_alloc and the interface is "lo0" it will be automatically created.
4906  * Cannot bump up reference on condemned ills. So dup detect can't be done
4907  * using this func.
4908  */
4909 ill_t *
4910 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4911     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4912     ip_stack_t *ipst)
4913 {
4914 	ill_t	*ill;
4915 	ipif_t	*ipif;
4916 	kstat_named_t	*kn;
4917 	boolean_t isloopback;
4918 	ipsq_t *old_ipsq;
4919 	in6_addr_t ov6addr;
4920 
4921 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4922 
4923 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4924 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4925 	rw_exit(&ipst->ips_ill_g_lock);
4926 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4927 		return (ill);
4928 
4929 	/*
4930 	 * Couldn't find it.  Does this happen to be a lookup for the
4931 	 * loopback device and are we allowed to allocate it?
4932 	 */
4933 	if (!isloopback || !do_alloc)
4934 		return (NULL);
4935 
4936 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4937 
4938 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4939 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4940 		rw_exit(&ipst->ips_ill_g_lock);
4941 		return (ill);
4942 	}
4943 
4944 	/* Create the loopback device on demand */
4945 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4946 	    sizeof (ipif_loopback_name), BPRI_MED));
4947 	if (ill == NULL)
4948 		goto done;
4949 
4950 	*ill = ill_null;
4951 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4952 	ill->ill_ipst = ipst;
4953 	netstack_hold(ipst->ips_netstack);
4954 	/*
4955 	 * For exclusive stacks we set the zoneid to zero
4956 	 * to make IP operate as if in the global zone.
4957 	 */
4958 	ill->ill_zoneid = GLOBAL_ZONEID;
4959 
4960 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4961 	if (ill->ill_phyint == NULL)
4962 		goto done;
4963 
4964 	if (isv6)
4965 		ill->ill_phyint->phyint_illv6 = ill;
4966 	else
4967 		ill->ill_phyint->phyint_illv4 = ill;
4968 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4969 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4970 	/* Add room for tcp+ip headers */
4971 	if (isv6) {
4972 		ill->ill_isv6 = B_TRUE;
4973 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4974 	} else {
4975 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4976 	}
4977 	if (!ill_allocate_mibs(ill))
4978 		goto done;
4979 	ill->ill_max_mtu = ill->ill_max_frag;
4980 	/*
4981 	 * ipif_loopback_name can't be pointed at directly because its used
4982 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4983 	 * from the glist, ill_glist_delete() sets the first character of
4984 	 * ill_name to '\0'.
4985 	 */
4986 	ill->ill_name = (char *)ill + sizeof (*ill);
4987 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4988 	ill->ill_name_length = sizeof (ipif_loopback_name);
4989 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
4990 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4991 
4992 	ill->ill_global_timer = INFINITY;
4993 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4994 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4995 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4996 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4997 
4998 	/* No resolver here. */
4999 	ill->ill_net_type = IRE_LOOPBACK;
5000 
5001 	/* Initialize the ipsq */
5002 	if (!ipsq_init(ill))
5003 		goto done;
5004 
5005 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
5006 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
5007 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
5008 #ifdef DEBUG
5009 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
5010 #endif
5011 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
5012 	if (ipif == NULL)
5013 		goto done;
5014 
5015 	ill->ill_flags = ILLF_MULTICAST;
5016 
5017 	ov6addr = ipif->ipif_v6lcl_addr;
5018 	/* Set up default loopback address and mask. */
5019 	if (!isv6) {
5020 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5021 
5022 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5023 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5024 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5025 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5026 		    ipif->ipif_v6subnet);
5027 		ill->ill_flags |= ILLF_IPV4;
5028 	} else {
5029 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5030 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5031 		ipif->ipif_v6net_mask = ipv6_all_ones;
5032 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5033 		    ipif->ipif_v6subnet);
5034 		ill->ill_flags |= ILLF_IPV6;
5035 	}
5036 
5037 	/*
5038 	 * Chain us in at the end of the ill list. hold the ill
5039 	 * before we make it globally visible. 1 for the lookup.
5040 	 */
5041 	ill->ill_refcnt = 0;
5042 	ill_refhold(ill);
5043 
5044 	ill->ill_frag_count = 0;
5045 	ill->ill_frag_free_num_pkts = 0;
5046 	ill->ill_last_frag_clean_time = 0;
5047 
5048 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5049 
5050 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5051 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5052 
5053 	/* Let SCTP know so that it can add this to its list */
5054 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5055 
5056 	/*
5057 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5058 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5059 	 * requires to be after ill_glist_insert() since we need the
5060 	 * ill_index set. Pass on ipv6_loopback as the old address.
5061 	 */
5062 	sctp_update_ipif_addr(ipif, ov6addr);
5063 
5064 	/*
5065 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5066 	 */
5067 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5068 		/* Loopback ills aren't in any IPMP group */
5069 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5070 		ipsq_delete(old_ipsq);
5071 	}
5072 
5073 	/*
5074 	 * Delay this till the ipif is allocated as ipif_allocate
5075 	 * de-references ill_phyint for getting the ifindex. We
5076 	 * can't do this before ipif_allocate because ill_phyint_reinit
5077 	 * -> phyint_assign_ifindex expects ipif to be present.
5078 	 */
5079 	mutex_enter(&ill->ill_phyint->phyint_lock);
5080 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5081 	mutex_exit(&ill->ill_phyint->phyint_lock);
5082 
5083 	if (ipst->ips_loopback_ksp == NULL) {
5084 		/* Export loopback interface statistics */
5085 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5086 		    ipif_loopback_name, "net",
5087 		    KSTAT_TYPE_NAMED, 2, 0,
5088 		    ipst->ips_netstack->netstack_stackid);
5089 		if (ipst->ips_loopback_ksp != NULL) {
5090 			ipst->ips_loopback_ksp->ks_update =
5091 			    loopback_kstat_update;
5092 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5093 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5094 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5095 			ipst->ips_loopback_ksp->ks_private =
5096 			    (void *)(uintptr_t)ipst->ips_netstack->
5097 			    netstack_stackid;
5098 			kstat_install(ipst->ips_loopback_ksp);
5099 		}
5100 	}
5101 
5102 	if (error != NULL)
5103 		*error = 0;
5104 	*did_alloc = B_TRUE;
5105 	rw_exit(&ipst->ips_ill_g_lock);
5106 	return (ill);
5107 done:
5108 	if (ill != NULL) {
5109 		if (ill->ill_phyint != NULL) {
5110 			ipsq_t	*ipsq;
5111 
5112 			ipsq = ill->ill_phyint->phyint_ipsq;
5113 			if (ipsq != NULL) {
5114 				ipsq->ipsq_ipst = NULL;
5115 				kmem_free(ipsq, sizeof (ipsq_t));
5116 			}
5117 			mi_free(ill->ill_phyint);
5118 		}
5119 		ill_free_mib(ill);
5120 		if (ill->ill_ipst != NULL)
5121 			netstack_rele(ill->ill_ipst->ips_netstack);
5122 		mi_free(ill);
5123 	}
5124 	rw_exit(&ipst->ips_ill_g_lock);
5125 	if (error != NULL)
5126 		*error = ENOMEM;
5127 	return (NULL);
5128 }
5129 
5130 /*
5131  * For IPP calls - use the ip_stack_t for global stack.
5132  */
5133 ill_t *
5134 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5135     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5136 {
5137 	ip_stack_t	*ipst;
5138 	ill_t		*ill;
5139 
5140 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5141 	if (ipst == NULL) {
5142 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5143 		return (NULL);
5144 	}
5145 
5146 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5147 	netstack_rele(ipst->ips_netstack);
5148 	return (ill);
5149 }
5150 
5151 /*
5152  * Return a pointer to the ill which matches the index and IP version type.
5153  */
5154 ill_t *
5155 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5156     ipsq_func_t func, int *err, ip_stack_t *ipst)
5157 {
5158 	ill_t	*ill;
5159 	ipsq_t  *ipsq;
5160 	phyint_t *phyi;
5161 
5162 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5163 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5164 
5165 	if (err != NULL)
5166 		*err = 0;
5167 
5168 	/*
5169 	 * Indexes are stored in the phyint - a common structure
5170 	 * to both IPv4 and IPv6.
5171 	 */
5172 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5173 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5174 	    (void *) &index, NULL);
5175 	if (phyi != NULL) {
5176 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5177 		if (ill != NULL) {
5178 			/*
5179 			 * The block comment at the start of ipif_down
5180 			 * explains the use of the macros used below
5181 			 */
5182 			GRAB_CONN_LOCK(q);
5183 			mutex_enter(&ill->ill_lock);
5184 			if (ILL_CAN_LOOKUP(ill)) {
5185 				ill_refhold_locked(ill);
5186 				mutex_exit(&ill->ill_lock);
5187 				RELEASE_CONN_LOCK(q);
5188 				rw_exit(&ipst->ips_ill_g_lock);
5189 				return (ill);
5190 			} else if (ILL_CAN_WAIT(ill, q)) {
5191 				ipsq = ill->ill_phyint->phyint_ipsq;
5192 				mutex_enter(&ipsq->ipsq_lock);
5193 				rw_exit(&ipst->ips_ill_g_lock);
5194 				mutex_exit(&ill->ill_lock);
5195 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5196 				mutex_exit(&ipsq->ipsq_lock);
5197 				RELEASE_CONN_LOCK(q);
5198 				if (err != NULL)
5199 					*err = EINPROGRESS;
5200 				return (NULL);
5201 			}
5202 			RELEASE_CONN_LOCK(q);
5203 			mutex_exit(&ill->ill_lock);
5204 		}
5205 	}
5206 	rw_exit(&ipst->ips_ill_g_lock);
5207 	if (err != NULL)
5208 		*err = ENXIO;
5209 	return (NULL);
5210 }
5211 
5212 /*
5213  * Return the ifindex next in sequence after the passed in ifindex.
5214  * If there is no next ifindex for the given protocol, return 0.
5215  */
5216 uint_t
5217 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5218 {
5219 	phyint_t *phyi;
5220 	phyint_t *phyi_initial;
5221 	uint_t   ifindex;
5222 
5223 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5224 
5225 	if (index == 0) {
5226 		phyi = avl_first(
5227 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5228 	} else {
5229 		phyi = phyi_initial = avl_find(
5230 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5231 		    (void *) &index, NULL);
5232 	}
5233 
5234 	for (; phyi != NULL;
5235 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5236 	    phyi, AVL_AFTER)) {
5237 		/*
5238 		 * If we're not returning the first interface in the tree
5239 		 * and we still haven't moved past the phyint_t that
5240 		 * corresponds to index, avl_walk needs to be called again
5241 		 */
5242 		if (!((index != 0) && (phyi == phyi_initial))) {
5243 			if (isv6) {
5244 				if ((phyi->phyint_illv6) &&
5245 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5246 				    (phyi->phyint_illv6->ill_isv6 == 1))
5247 					break;
5248 			} else {
5249 				if ((phyi->phyint_illv4) &&
5250 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5251 				    (phyi->phyint_illv4->ill_isv6 == 0))
5252 					break;
5253 			}
5254 		}
5255 	}
5256 
5257 	rw_exit(&ipst->ips_ill_g_lock);
5258 
5259 	if (phyi != NULL)
5260 		ifindex = phyi->phyint_ifindex;
5261 	else
5262 		ifindex = 0;
5263 
5264 	return (ifindex);
5265 }
5266 
5267 
5268 /*
5269  * Return the ifindex for the named interface.
5270  * If there is no next ifindex for the interface, return 0.
5271  */
5272 uint_t
5273 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5274 {
5275 	phyint_t	*phyi;
5276 	avl_index_t	where = 0;
5277 	uint_t		ifindex;
5278 
5279 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5280 
5281 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5282 	    name, &where)) == NULL) {
5283 		rw_exit(&ipst->ips_ill_g_lock);
5284 		return (0);
5285 	}
5286 
5287 	ifindex = phyi->phyint_ifindex;
5288 
5289 	rw_exit(&ipst->ips_ill_g_lock);
5290 
5291 	return (ifindex);
5292 }
5293 
5294 
5295 /*
5296  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5297  * that gives a running thread a reference to the ill. This reference must be
5298  * released by the thread when it is done accessing the ill and related
5299  * objects. ill_refcnt can not be used to account for static references
5300  * such as other structures pointing to an ill. Callers must generally
5301  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5302  * or be sure that the ill is not being deleted or changing state before
5303  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5304  * ill won't change any of its critical state such as address, netmask etc.
5305  */
5306 void
5307 ill_refhold(ill_t *ill)
5308 {
5309 	mutex_enter(&ill->ill_lock);
5310 	ill->ill_refcnt++;
5311 	ILL_TRACE_REF(ill);
5312 	mutex_exit(&ill->ill_lock);
5313 }
5314 
5315 void
5316 ill_refhold_locked(ill_t *ill)
5317 {
5318 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5319 	ill->ill_refcnt++;
5320 	ILL_TRACE_REF(ill);
5321 }
5322 
5323 int
5324 ill_check_and_refhold(ill_t *ill)
5325 {
5326 	mutex_enter(&ill->ill_lock);
5327 	if (ILL_CAN_LOOKUP(ill)) {
5328 		ill_refhold_locked(ill);
5329 		mutex_exit(&ill->ill_lock);
5330 		return (0);
5331 	}
5332 	mutex_exit(&ill->ill_lock);
5333 	return (ILL_LOOKUP_FAILED);
5334 }
5335 
5336 /*
5337  * Must not be called while holding any locks. Otherwise if this is
5338  * the last reference to be released, there is a chance of recursive mutex
5339  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5340  * to restart an ioctl.
5341  */
5342 void
5343 ill_refrele(ill_t *ill)
5344 {
5345 	mutex_enter(&ill->ill_lock);
5346 	ASSERT(ill->ill_refcnt != 0);
5347 	ill->ill_refcnt--;
5348 	ILL_UNTRACE_REF(ill);
5349 	if (ill->ill_refcnt != 0) {
5350 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5351 		mutex_exit(&ill->ill_lock);
5352 		return;
5353 	}
5354 
5355 	/* Drops the ill_lock */
5356 	ipif_ill_refrele_tail(ill);
5357 }
5358 
5359 /*
5360  * Obtain a weak reference count on the ill. This reference ensures the
5361  * ill won't be freed, but the ill may change any of its critical state
5362  * such as netmask, address etc. Returns an error if the ill has started
5363  * closing.
5364  */
5365 boolean_t
5366 ill_waiter_inc(ill_t *ill)
5367 {
5368 	mutex_enter(&ill->ill_lock);
5369 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5370 		mutex_exit(&ill->ill_lock);
5371 		return (B_FALSE);
5372 	}
5373 	ill->ill_waiters++;
5374 	mutex_exit(&ill->ill_lock);
5375 	return (B_TRUE);
5376 }
5377 
5378 void
5379 ill_waiter_dcr(ill_t *ill)
5380 {
5381 	mutex_enter(&ill->ill_lock);
5382 	ill->ill_waiters--;
5383 	if (ill->ill_waiters == 0)
5384 		cv_broadcast(&ill->ill_cv);
5385 	mutex_exit(&ill->ill_lock);
5386 }
5387 
5388 /*
5389  * Named Dispatch routine to produce a formatted report on all ILLs.
5390  * This report is accessed by using the ndd utility to "get" ND variable
5391  * "ip_ill_status".
5392  */
5393 /* ARGSUSED */
5394 int
5395 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5396 {
5397 	ill_t		*ill;
5398 	ill_walk_context_t ctx;
5399 	ip_stack_t	*ipst;
5400 
5401 	ipst = CONNQ_TO_IPST(q);
5402 
5403 	(void) mi_mpprintf(mp,
5404 	    "ILL      " MI_COL_HDRPAD_STR
5405 	/*   01234567[89ABCDEF] */
5406 	    "rq       " MI_COL_HDRPAD_STR
5407 	/*   01234567[89ABCDEF] */
5408 	    "wq       " MI_COL_HDRPAD_STR
5409 	/*   01234567[89ABCDEF] */
5410 	    "upcnt mxfrg err name");
5411 	/*   12345 12345 123 xxxxxxxx  */
5412 
5413 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5414 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5415 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5416 		(void) mi_mpprintf(mp,
5417 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5418 		    "%05u %05u %03d %s",
5419 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5420 		    ill->ill_ipif_up_count,
5421 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5422 	}
5423 	rw_exit(&ipst->ips_ill_g_lock);
5424 
5425 	return (0);
5426 }
5427 
5428 /*
5429  * Named Dispatch routine to produce a formatted report on all IPIFs.
5430  * This report is accessed by using the ndd utility to "get" ND variable
5431  * "ip_ipif_status".
5432  */
5433 /* ARGSUSED */
5434 int
5435 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5436 {
5437 	char	buf1[INET6_ADDRSTRLEN];
5438 	char	buf2[INET6_ADDRSTRLEN];
5439 	char	buf3[INET6_ADDRSTRLEN];
5440 	char	buf4[INET6_ADDRSTRLEN];
5441 	char	buf5[INET6_ADDRSTRLEN];
5442 	char	buf6[INET6_ADDRSTRLEN];
5443 	char	buf[LIFNAMSIZ];
5444 	ill_t	*ill;
5445 	ipif_t	*ipif;
5446 	nv_t	*nvp;
5447 	uint64_t flags;
5448 	zoneid_t zoneid;
5449 	ill_walk_context_t ctx;
5450 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5451 
5452 	(void) mi_mpprintf(mp,
5453 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5454 	    "\tlocal address\n"
5455 	    "\tsrc address\n"
5456 	    "\tsubnet\n"
5457 	    "\tmask\n"
5458 	    "\tbroadcast\n"
5459 	    "\tp-p-dst");
5460 
5461 	ASSERT(q->q_next == NULL);
5462 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5463 
5464 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5465 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5466 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5467 		for (ipif = ill->ill_ipif; ipif != NULL;
5468 		    ipif = ipif->ipif_next) {
5469 			if (zoneid != GLOBAL_ZONEID &&
5470 			    zoneid != ipif->ipif_zoneid &&
5471 			    ipif->ipif_zoneid != ALL_ZONES)
5472 				continue;
5473 
5474 			ipif_get_name(ipif, buf, sizeof (buf));
5475 			(void) mi_mpprintf(mp,
5476 			    MI_COL_PTRFMT_STR
5477 			    "%04u %05u %u/%u/%u %s %d",
5478 			    (void *)ipif,
5479 			    ipif->ipif_metric, ipif->ipif_mtu,
5480 			    ipif->ipif_ib_pkt_count,
5481 			    ipif->ipif_ob_pkt_count,
5482 			    ipif->ipif_fo_pkt_count,
5483 			    buf,
5484 			    ipif->ipif_zoneid);
5485 
5486 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5487 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5488 
5489 		/* Tack on text strings for any flags. */
5490 		nvp = ipif_nv_tbl;
5491 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5492 			if (nvp->nv_value & flags)
5493 				(void) mi_mpprintf_nr(mp, " %s",
5494 				    nvp->nv_name);
5495 		}
5496 		(void) mi_mpprintf(mp,
5497 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5498 		    inet_ntop(AF_INET6,
5499 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5500 		    inet_ntop(AF_INET6,
5501 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5502 		    inet_ntop(AF_INET6,
5503 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5504 		    inet_ntop(AF_INET6,
5505 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5506 		    inet_ntop(AF_INET6,
5507 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5508 		    inet_ntop(AF_INET6,
5509 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5510 		}
5511 	}
5512 	rw_exit(&ipst->ips_ill_g_lock);
5513 	return (0);
5514 }
5515 
5516 /*
5517  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5518  * driver.  We construct best guess defaults for lower level information that
5519  * we need.  If an interface is brought up without injection of any overriding
5520  * information from outside, we have to be ready to go with these defaults.
5521  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5522  * we primarely want the dl_provider_style.
5523  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5524  * at which point we assume the other part of the information is valid.
5525  */
5526 void
5527 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5528 {
5529 	uchar_t		*brdcst_addr;
5530 	uint_t		brdcst_addr_length, phys_addr_length;
5531 	t_scalar_t	sap_length;
5532 	dl_info_ack_t	*dlia;
5533 	ip_m_t		*ipm;
5534 	dl_qos_cl_sel1_t *sel1;
5535 
5536 	ASSERT(IAM_WRITER_ILL(ill));
5537 
5538 	/*
5539 	 * Till the ill is fully up ILL_CHANGING will be set and
5540 	 * the ill is not globally visible. So no need for a lock.
5541 	 */
5542 	dlia = (dl_info_ack_t *)mp->b_rptr;
5543 	ill->ill_mactype = dlia->dl_mac_type;
5544 
5545 	ipm = ip_m_lookup(dlia->dl_mac_type);
5546 	if (ipm == NULL) {
5547 		ipm = ip_m_lookup(DL_OTHER);
5548 		ASSERT(ipm != NULL);
5549 	}
5550 	ill->ill_media = ipm;
5551 
5552 	/*
5553 	 * When the new DLPI stuff is ready we'll pull lengths
5554 	 * from dlia.
5555 	 */
5556 	if (dlia->dl_version == DL_VERSION_2) {
5557 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5558 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5559 		    brdcst_addr_length);
5560 		if (brdcst_addr == NULL) {
5561 			brdcst_addr_length = 0;
5562 		}
5563 		sap_length = dlia->dl_sap_length;
5564 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5565 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5566 		    brdcst_addr_length, sap_length, phys_addr_length));
5567 	} else {
5568 		brdcst_addr_length = 6;
5569 		brdcst_addr = ip_six_byte_all_ones;
5570 		sap_length = -2;
5571 		phys_addr_length = brdcst_addr_length;
5572 	}
5573 
5574 	ill->ill_bcast_addr_length = brdcst_addr_length;
5575 	ill->ill_phys_addr_length = phys_addr_length;
5576 	ill->ill_sap_length = sap_length;
5577 	ill->ill_max_frag = dlia->dl_max_sdu;
5578 	ill->ill_max_mtu = ill->ill_max_frag;
5579 
5580 	ill->ill_type = ipm->ip_m_type;
5581 
5582 	if (!ill->ill_dlpi_style_set) {
5583 		if (dlia->dl_provider_style == DL_STYLE2)
5584 			ill->ill_needs_attach = 1;
5585 
5586 		/*
5587 		 * Allocate the first ipif on this ill. We don't delay it
5588 		 * further as ioctl handling assumes atleast one ipif to
5589 		 * be present.
5590 		 *
5591 		 * At this point we don't know whether the ill is v4 or v6.
5592 		 * We will know this whan the SIOCSLIFNAME happens and
5593 		 * the correct value for ill_isv6 will be assigned in
5594 		 * ipif_set_values(). We need to hold the ill lock and
5595 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5596 		 * the wakeup.
5597 		 */
5598 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5599 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5600 		mutex_enter(&ill->ill_lock);
5601 		ASSERT(ill->ill_dlpi_style_set == 0);
5602 		ill->ill_dlpi_style_set = 1;
5603 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5604 		cv_broadcast(&ill->ill_cv);
5605 		mutex_exit(&ill->ill_lock);
5606 		freemsg(mp);
5607 		return;
5608 	}
5609 	ASSERT(ill->ill_ipif != NULL);
5610 	/*
5611 	 * We know whether it is IPv4 or IPv6 now, as this is the
5612 	 * second DL_INFO_ACK we are recieving in response to the
5613 	 * DL_INFO_REQ sent in ipif_set_values.
5614 	 */
5615 	if (ill->ill_isv6)
5616 		ill->ill_sap = IP6_DL_SAP;
5617 	else
5618 		ill->ill_sap = IP_DL_SAP;
5619 	/*
5620 	 * Set ipif_mtu which is used to set the IRE's
5621 	 * ire_max_frag value. The driver could have sent
5622 	 * a different mtu from what it sent last time. No
5623 	 * need to call ipif_mtu_change because IREs have
5624 	 * not yet been created.
5625 	 */
5626 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5627 	/*
5628 	 * Clear all the flags that were set based on ill_bcast_addr_length
5629 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5630 	 * changed now and we need to re-evaluate.
5631 	 */
5632 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5633 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5634 
5635 	/*
5636 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5637 	 * changed now.
5638 	 */
5639 	if (ill->ill_bcast_addr_length == 0) {
5640 		if (ill->ill_resolver_mp != NULL)
5641 			freemsg(ill->ill_resolver_mp);
5642 		if (ill->ill_bcast_mp != NULL)
5643 			freemsg(ill->ill_bcast_mp);
5644 		if (ill->ill_flags & ILLF_XRESOLV)
5645 			ill->ill_net_type = IRE_IF_RESOLVER;
5646 		else
5647 			ill->ill_net_type = IRE_IF_NORESOLVER;
5648 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5649 		    ill->ill_phys_addr_length,
5650 		    ill->ill_sap,
5651 		    ill->ill_sap_length);
5652 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5653 
5654 		if (ill->ill_isv6)
5655 			/*
5656 			 * Note: xresolv interfaces will eventually need NOARP
5657 			 * set here as well, but that will require those
5658 			 * external resolvers to have some knowledge of
5659 			 * that flag and act appropriately. Not to be changed
5660 			 * at present.
5661 			 */
5662 			ill->ill_flags |= ILLF_NONUD;
5663 		else
5664 			ill->ill_flags |= ILLF_NOARP;
5665 
5666 		if (ill->ill_phys_addr_length == 0) {
5667 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5668 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5669 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5670 			} else {
5671 				/* pt-pt supports multicast. */
5672 				ill->ill_flags |= ILLF_MULTICAST;
5673 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5674 			}
5675 		}
5676 	} else {
5677 		ill->ill_net_type = IRE_IF_RESOLVER;
5678 		if (ill->ill_bcast_mp != NULL)
5679 			freemsg(ill->ill_bcast_mp);
5680 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5681 		    ill->ill_bcast_addr_length, ill->ill_sap,
5682 		    ill->ill_sap_length);
5683 		/*
5684 		 * Later detect lack of DLPI driver multicast
5685 		 * capability by catching DL_ENABMULTI errors in
5686 		 * ip_rput_dlpi.
5687 		 */
5688 		ill->ill_flags |= ILLF_MULTICAST;
5689 		if (!ill->ill_isv6)
5690 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5691 	}
5692 	/* By default an interface does not support any CoS marking */
5693 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5694 
5695 	/*
5696 	 * If we get QoS information in DL_INFO_ACK, the device supports
5697 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5698 	 */
5699 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5700 	    dlia->dl_qos_length);
5701 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5702 		ill->ill_flags |= ILLF_COS_ENABLED;
5703 	}
5704 
5705 	/* Clear any previous error indication. */
5706 	ill->ill_error = 0;
5707 	freemsg(mp);
5708 }
5709 
5710 /*
5711  * Perform various checks to verify that an address would make sense as a
5712  * local, remote, or subnet interface address.
5713  */
5714 static boolean_t
5715 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5716 {
5717 	ipaddr_t	net_mask;
5718 
5719 	/*
5720 	 * Don't allow all zeroes, or all ones, but allow
5721 	 * all ones netmask.
5722 	 */
5723 	if ((net_mask = ip_net_mask(addr)) == 0)
5724 		return (B_FALSE);
5725 	/* A given netmask overrides the "guess" netmask */
5726 	if (subnet_mask != 0)
5727 		net_mask = subnet_mask;
5728 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5729 	    (addr == (addr | ~net_mask)))) {
5730 		return (B_FALSE);
5731 	}
5732 
5733 	/*
5734 	 * Even if the netmask is all ones, we do not allow address to be
5735 	 * 255.255.255.255
5736 	 */
5737 	if (addr == INADDR_BROADCAST)
5738 		return (B_FALSE);
5739 
5740 	if (CLASSD(addr))
5741 		return (B_FALSE);
5742 
5743 	return (B_TRUE);
5744 }
5745 
5746 #define	V6_IPIF_LINKLOCAL(p)	\
5747 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5748 
5749 /*
5750  * Compare two given ipifs and check if the second one is better than
5751  * the first one using the order of preference (not taking deprecated
5752  * into acount) specified in ipif_lookup_multicast().
5753  */
5754 static boolean_t
5755 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5756 {
5757 	/* Check the least preferred first. */
5758 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5759 		/* If both ipifs are the same, use the first one. */
5760 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5761 			return (B_FALSE);
5762 		else
5763 			return (B_TRUE);
5764 	}
5765 
5766 	/* For IPv6, check for link local address. */
5767 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5768 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5769 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5770 			/* The second one is equal or less preferred. */
5771 			return (B_FALSE);
5772 		} else {
5773 			return (B_TRUE);
5774 		}
5775 	}
5776 
5777 	/* Then check for point to point interface. */
5778 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5779 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5780 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5781 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5782 			return (B_FALSE);
5783 		} else {
5784 			return (B_TRUE);
5785 		}
5786 	}
5787 
5788 	/* old_ipif is a normal interface, so no need to use the new one. */
5789 	return (B_FALSE);
5790 }
5791 
5792 /*
5793  * Find any non-virtual, not condemned, and up multicast capable interface
5794  * given an IP instance and zoneid.  Order of preference is:
5795  *
5796  * 1. normal
5797  * 1.1 normal, but deprecated
5798  * 2. point to point
5799  * 2.1 point to point, but deprecated
5800  * 3. link local
5801  * 3.1 link local, but deprecated
5802  * 4. loopback.
5803  */
5804 ipif_t *
5805 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5806 {
5807 	ill_t			*ill;
5808 	ill_walk_context_t	ctx;
5809 	ipif_t			*ipif;
5810 	ipif_t			*saved_ipif = NULL;
5811 	ipif_t			*dep_ipif = NULL;
5812 
5813 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5814 	if (isv6)
5815 		ill = ILL_START_WALK_V6(&ctx, ipst);
5816 	else
5817 		ill = ILL_START_WALK_V4(&ctx, ipst);
5818 
5819 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5820 		mutex_enter(&ill->ill_lock);
5821 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5822 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5823 			mutex_exit(&ill->ill_lock);
5824 			continue;
5825 		}
5826 		for (ipif = ill->ill_ipif; ipif != NULL;
5827 		    ipif = ipif->ipif_next) {
5828 			if (zoneid != ipif->ipif_zoneid &&
5829 			    zoneid != ALL_ZONES &&
5830 			    ipif->ipif_zoneid != ALL_ZONES) {
5831 				continue;
5832 			}
5833 			if (!(ipif->ipif_flags & IPIF_UP) ||
5834 			    !IPIF_CAN_LOOKUP(ipif)) {
5835 				continue;
5836 			}
5837 
5838 			/*
5839 			 * Found one candidate.  If it is deprecated,
5840 			 * remember it in dep_ipif.  If it is not deprecated,
5841 			 * remember it in saved_ipif.
5842 			 */
5843 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5844 				if (dep_ipif == NULL) {
5845 					dep_ipif = ipif;
5846 				} else if (ipif_comp_multi(dep_ipif, ipif,
5847 				    isv6)) {
5848 					/*
5849 					 * If the previous dep_ipif does not
5850 					 * belong to the same ill, we've done
5851 					 * a ipif_refhold() on it.  So we need
5852 					 * to release it.
5853 					 */
5854 					if (dep_ipif->ipif_ill != ill)
5855 						ipif_refrele(dep_ipif);
5856 					dep_ipif = ipif;
5857 				}
5858 				continue;
5859 			}
5860 			if (saved_ipif == NULL) {
5861 				saved_ipif = ipif;
5862 			} else {
5863 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5864 					if (saved_ipif->ipif_ill != ill)
5865 						ipif_refrele(saved_ipif);
5866 					saved_ipif = ipif;
5867 				}
5868 			}
5869 		}
5870 		/*
5871 		 * Before going to the next ill, do a ipif_refhold() on the
5872 		 * saved ones.
5873 		 */
5874 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5875 			ipif_refhold_locked(saved_ipif);
5876 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5877 			ipif_refhold_locked(dep_ipif);
5878 		mutex_exit(&ill->ill_lock);
5879 	}
5880 	rw_exit(&ipst->ips_ill_g_lock);
5881 
5882 	/*
5883 	 * If we have only the saved_ipif, return it.  But if we have both
5884 	 * saved_ipif and dep_ipif, check to see which one is better.
5885 	 */
5886 	if (saved_ipif != NULL) {
5887 		if (dep_ipif != NULL) {
5888 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5889 				ipif_refrele(saved_ipif);
5890 				return (dep_ipif);
5891 			} else {
5892 				ipif_refrele(dep_ipif);
5893 				return (saved_ipif);
5894 			}
5895 		}
5896 		return (saved_ipif);
5897 	} else {
5898 		return (dep_ipif);
5899 	}
5900 }
5901 
5902 /*
5903  * This function is called when an application does not specify an interface
5904  * to be used for multicast traffic (joining a group/sending data).  It
5905  * calls ire_lookup_multi() to look for an interface route for the
5906  * specified multicast group.  Doing this allows the administrator to add
5907  * prefix routes for multicast to indicate which interface to be used for
5908  * multicast traffic in the above scenario.  The route could be for all
5909  * multicast (224.0/4), for a single multicast group (a /32 route) or
5910  * anything in between.  If there is no such multicast route, we just find
5911  * any multicast capable interface and return it.  The returned ipif
5912  * is refhold'ed.
5913  */
5914 ipif_t *
5915 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5916 {
5917 	ire_t			*ire;
5918 	ipif_t			*ipif;
5919 
5920 	ire = ire_lookup_multi(group, zoneid, ipst);
5921 	if (ire != NULL) {
5922 		ipif = ire->ire_ipif;
5923 		ipif_refhold(ipif);
5924 		ire_refrele(ire);
5925 		return (ipif);
5926 	}
5927 
5928 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5929 }
5930 
5931 /*
5932  * Look for an ipif with the specified interface address and destination.
5933  * The destination address is used only for matching point-to-point interfaces.
5934  */
5935 ipif_t *
5936 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5937     ipsq_func_t func, int *error, ip_stack_t *ipst)
5938 {
5939 	ipif_t	*ipif;
5940 	ill_t	*ill;
5941 	ill_walk_context_t ctx;
5942 	ipsq_t	*ipsq;
5943 
5944 	if (error != NULL)
5945 		*error = 0;
5946 
5947 	/*
5948 	 * First match all the point-to-point interfaces
5949 	 * before looking at non-point-to-point interfaces.
5950 	 * This is done to avoid returning non-point-to-point
5951 	 * ipif instead of unnumbered point-to-point ipif.
5952 	 */
5953 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5954 	ill = ILL_START_WALK_V4(&ctx, ipst);
5955 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5956 		GRAB_CONN_LOCK(q);
5957 		mutex_enter(&ill->ill_lock);
5958 		for (ipif = ill->ill_ipif; ipif != NULL;
5959 		    ipif = ipif->ipif_next) {
5960 			/* Allow the ipif to be down */
5961 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5962 			    (ipif->ipif_lcl_addr == if_addr) &&
5963 			    (ipif->ipif_pp_dst_addr == dst)) {
5964 				/*
5965 				 * The block comment at the start of ipif_down
5966 				 * explains the use of the macros used below
5967 				 */
5968 				if (IPIF_CAN_LOOKUP(ipif)) {
5969 					ipif_refhold_locked(ipif);
5970 					mutex_exit(&ill->ill_lock);
5971 					RELEASE_CONN_LOCK(q);
5972 					rw_exit(&ipst->ips_ill_g_lock);
5973 					return (ipif);
5974 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5975 					ipsq = ill->ill_phyint->phyint_ipsq;
5976 					mutex_enter(&ipsq->ipsq_lock);
5977 					mutex_exit(&ill->ill_lock);
5978 					rw_exit(&ipst->ips_ill_g_lock);
5979 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5980 					    ill);
5981 					mutex_exit(&ipsq->ipsq_lock);
5982 					RELEASE_CONN_LOCK(q);
5983 					if (error != NULL)
5984 						*error = EINPROGRESS;
5985 					return (NULL);
5986 				}
5987 			}
5988 		}
5989 		mutex_exit(&ill->ill_lock);
5990 		RELEASE_CONN_LOCK(q);
5991 	}
5992 	rw_exit(&ipst->ips_ill_g_lock);
5993 
5994 	/* lookup the ipif based on interface address */
5995 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5996 	    ipst);
5997 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5998 	return (ipif);
5999 }
6000 
6001 /*
6002  * Look for an ipif with the specified address. For point-point links
6003  * we look for matches on either the destination address and the local
6004  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6005  * is set.
6006  * Matches on a specific ill if match_ill is set.
6007  */
6008 ipif_t *
6009 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
6010     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
6011 {
6012 	ipif_t  *ipif;
6013 	ill_t   *ill;
6014 	boolean_t ptp = B_FALSE;
6015 	ipsq_t	*ipsq;
6016 	ill_walk_context_t	ctx;
6017 
6018 	if (error != NULL)
6019 		*error = 0;
6020 
6021 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6022 	/*
6023 	 * Repeat twice, first based on local addresses and
6024 	 * next time for pointopoint.
6025 	 */
6026 repeat:
6027 	ill = ILL_START_WALK_V4(&ctx, ipst);
6028 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6029 		if (match_ill != NULL && ill != match_ill) {
6030 			continue;
6031 		}
6032 		GRAB_CONN_LOCK(q);
6033 		mutex_enter(&ill->ill_lock);
6034 		for (ipif = ill->ill_ipif; ipif != NULL;
6035 		    ipif = ipif->ipif_next) {
6036 			if (zoneid != ALL_ZONES &&
6037 			    zoneid != ipif->ipif_zoneid &&
6038 			    ipif->ipif_zoneid != ALL_ZONES)
6039 				continue;
6040 			/* Allow the ipif to be down */
6041 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6042 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6043 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6044 			    (ipif->ipif_pp_dst_addr == addr))) {
6045 				/*
6046 				 * The block comment at the start of ipif_down
6047 				 * explains the use of the macros used below
6048 				 */
6049 				if (IPIF_CAN_LOOKUP(ipif)) {
6050 					ipif_refhold_locked(ipif);
6051 					mutex_exit(&ill->ill_lock);
6052 					RELEASE_CONN_LOCK(q);
6053 					rw_exit(&ipst->ips_ill_g_lock);
6054 					return (ipif);
6055 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6056 					ipsq = ill->ill_phyint->phyint_ipsq;
6057 					mutex_enter(&ipsq->ipsq_lock);
6058 					mutex_exit(&ill->ill_lock);
6059 					rw_exit(&ipst->ips_ill_g_lock);
6060 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6061 					    ill);
6062 					mutex_exit(&ipsq->ipsq_lock);
6063 					RELEASE_CONN_LOCK(q);
6064 					if (error != NULL)
6065 						*error = EINPROGRESS;
6066 					return (NULL);
6067 				}
6068 			}
6069 		}
6070 		mutex_exit(&ill->ill_lock);
6071 		RELEASE_CONN_LOCK(q);
6072 	}
6073 
6074 	/* If we already did the ptp case, then we are done */
6075 	if (ptp) {
6076 		rw_exit(&ipst->ips_ill_g_lock);
6077 		if (error != NULL)
6078 			*error = ENXIO;
6079 		return (NULL);
6080 	}
6081 	ptp = B_TRUE;
6082 	goto repeat;
6083 }
6084 
6085 /*
6086  * Look for an ipif with the specified address. For point-point links
6087  * we look for matches on either the destination address and the local
6088  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6089  * is set.
6090  * Matches on a specific ill if match_ill is set.
6091  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6092  */
6093 zoneid_t
6094 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6095 {
6096 	zoneid_t zoneid;
6097 	ipif_t  *ipif;
6098 	ill_t   *ill;
6099 	boolean_t ptp = B_FALSE;
6100 	ill_walk_context_t	ctx;
6101 
6102 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6103 	/*
6104 	 * Repeat twice, first based on local addresses and
6105 	 * next time for pointopoint.
6106 	 */
6107 repeat:
6108 	ill = ILL_START_WALK_V4(&ctx, ipst);
6109 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6110 		if (match_ill != NULL && ill != match_ill) {
6111 			continue;
6112 		}
6113 		mutex_enter(&ill->ill_lock);
6114 		for (ipif = ill->ill_ipif; ipif != NULL;
6115 		    ipif = ipif->ipif_next) {
6116 			/* Allow the ipif to be down */
6117 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6118 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6119 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6120 			    (ipif->ipif_pp_dst_addr == addr)) &&
6121 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6122 				zoneid = ipif->ipif_zoneid;
6123 				mutex_exit(&ill->ill_lock);
6124 				rw_exit(&ipst->ips_ill_g_lock);
6125 				/*
6126 				 * If ipif_zoneid was ALL_ZONES then we have
6127 				 * a trusted extensions shared IP address.
6128 				 * In that case GLOBAL_ZONEID works to send.
6129 				 */
6130 				if (zoneid == ALL_ZONES)
6131 					zoneid = GLOBAL_ZONEID;
6132 				return (zoneid);
6133 			}
6134 		}
6135 		mutex_exit(&ill->ill_lock);
6136 	}
6137 
6138 	/* If we already did the ptp case, then we are done */
6139 	if (ptp) {
6140 		rw_exit(&ipst->ips_ill_g_lock);
6141 		return (ALL_ZONES);
6142 	}
6143 	ptp = B_TRUE;
6144 	goto repeat;
6145 }
6146 
6147 /*
6148  * Look for an ipif that matches the specified remote address i.e. the
6149  * ipif that would receive the specified packet.
6150  * First look for directly connected interfaces and then do a recursive
6151  * IRE lookup and pick the first ipif corresponding to the source address in the
6152  * ire.
6153  * Returns: held ipif
6154  */
6155 ipif_t *
6156 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6157 {
6158 	ipif_t	*ipif;
6159 	ire_t	*ire;
6160 	ip_stack_t	*ipst = ill->ill_ipst;
6161 
6162 	ASSERT(!ill->ill_isv6);
6163 
6164 	/*
6165 	 * Someone could be changing this ipif currently or change it
6166 	 * after we return this. Thus  a few packets could use the old
6167 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6168 	 * will atomically be updated or cleaned up with the new value
6169 	 * Thus we don't need a lock to check the flags or other attrs below.
6170 	 */
6171 	mutex_enter(&ill->ill_lock);
6172 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6173 		if (!IPIF_CAN_LOOKUP(ipif))
6174 			continue;
6175 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6176 		    ipif->ipif_zoneid != ALL_ZONES)
6177 			continue;
6178 		/* Allow the ipif to be down */
6179 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6180 			if ((ipif->ipif_pp_dst_addr == addr) ||
6181 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6182 			    ipif->ipif_lcl_addr == addr)) {
6183 				ipif_refhold_locked(ipif);
6184 				mutex_exit(&ill->ill_lock);
6185 				return (ipif);
6186 			}
6187 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6188 			ipif_refhold_locked(ipif);
6189 			mutex_exit(&ill->ill_lock);
6190 			return (ipif);
6191 		}
6192 	}
6193 	mutex_exit(&ill->ill_lock);
6194 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6195 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6196 	if (ire != NULL) {
6197 		/*
6198 		 * The callers of this function wants to know the
6199 		 * interface on which they have to send the replies
6200 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6201 		 * derived from different ills, we really don't care
6202 		 * what we return here.
6203 		 */
6204 		ipif = ire->ire_ipif;
6205 		if (ipif != NULL) {
6206 			ipif_refhold(ipif);
6207 			ire_refrele(ire);
6208 			return (ipif);
6209 		}
6210 		ire_refrele(ire);
6211 	}
6212 	/* Pick the first interface */
6213 	ipif = ipif_get_next_ipif(NULL, ill);
6214 	return (ipif);
6215 }
6216 
6217 /*
6218  * This func does not prevent refcnt from increasing. But if
6219  * the caller has taken steps to that effect, then this func
6220  * can be used to determine whether the ill has become quiescent
6221  */
6222 static boolean_t
6223 ill_is_quiescent(ill_t *ill)
6224 {
6225 	ipif_t	*ipif;
6226 
6227 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6228 
6229 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6230 		if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6231 			return (B_FALSE);
6232 		}
6233 	}
6234 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6235 		return (B_FALSE);
6236 	}
6237 	return (B_TRUE);
6238 }
6239 
6240 boolean_t
6241 ill_is_freeable(ill_t *ill)
6242 {
6243 	ipif_t	*ipif;
6244 
6245 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6246 
6247 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6248 		if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6249 			return (B_FALSE);
6250 		}
6251 	}
6252 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6253 		return (B_FALSE);
6254 	}
6255 	return (B_TRUE);
6256 }
6257 
6258 /*
6259  * This func does not prevent refcnt from increasing. But if
6260  * the caller has taken steps to that effect, then this func
6261  * can be used to determine whether the ipif has become quiescent
6262  */
6263 static boolean_t
6264 ipif_is_quiescent(ipif_t *ipif)
6265 {
6266 	ill_t *ill;
6267 
6268 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6269 
6270 	if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6271 		return (B_FALSE);
6272 	}
6273 
6274 	ill = ipif->ipif_ill;
6275 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6276 	    ill->ill_logical_down) {
6277 		return (B_TRUE);
6278 	}
6279 
6280 	/* This is the last ipif going down or being deleted on this ill */
6281 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6282 		return (B_FALSE);
6283 	}
6284 
6285 	return (B_TRUE);
6286 }
6287 
6288 /*
6289  * return true if the ipif can be destroyed: the ipif has to be quiescent
6290  * with zero references from ire/nce/ilm to it.
6291  */
6292 static boolean_t
6293 ipif_is_freeable(ipif_t *ipif)
6294 {
6295 
6296 	ill_t *ill;
6297 
6298 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6299 
6300 	if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6301 		return (B_FALSE);
6302 	}
6303 
6304 	ill = ipif->ipif_ill;
6305 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6306 	    ill->ill_logical_down) {
6307 		return (B_TRUE);
6308 	}
6309 
6310 	/* This is the last ipif going down or being deleted on this ill */
6311 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6312 		return (B_FALSE);
6313 	}
6314 
6315 	return (B_TRUE);
6316 }
6317 
6318 /*
6319  * This func does not prevent refcnt from increasing. But if
6320  * the caller has taken steps to that effect, then this func
6321  * can be used to determine whether the ipifs marked with IPIF_MOVING
6322  * have become quiescent and can be moved in a failover/failback.
6323  */
6324 static ipif_t *
6325 ill_quiescent_to_move(ill_t *ill)
6326 {
6327 	ipif_t  *ipif;
6328 
6329 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6330 
6331 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6332 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6333 			if (ipif->ipif_refcnt != 0 ||
6334 			    !IPIF_DOWN_OK(ipif)) {
6335 				return (ipif);
6336 			}
6337 		}
6338 	}
6339 	return (NULL);
6340 }
6341 
6342 /*
6343  * The ipif/ill/ire has been refreled. Do the tail processing.
6344  * Determine if the ipif or ill in question has become quiescent and if so
6345  * wakeup close and/or restart any queued pending ioctl that is waiting
6346  * for the ipif_down (or ill_down)
6347  */
6348 void
6349 ipif_ill_refrele_tail(ill_t *ill)
6350 {
6351 	mblk_t	*mp;
6352 	conn_t	*connp;
6353 	ipsq_t	*ipsq;
6354 	ipif_t	*ipif;
6355 	dl_notify_ind_t *dlindp;
6356 
6357 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6358 
6359 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6360 	    ill_is_freeable(ill)) {
6361 		/* ill_close may be waiting */
6362 		cv_broadcast(&ill->ill_cv);
6363 	}
6364 
6365 	/* ipsq can't change because ill_lock  is held */
6366 	ipsq = ill->ill_phyint->phyint_ipsq;
6367 	if (ipsq->ipsq_waitfor == 0) {
6368 		/* Not waiting for anything, just return. */
6369 		mutex_exit(&ill->ill_lock);
6370 		return;
6371 	}
6372 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6373 	    ipsq->ipsq_pending_ipif != NULL);
6374 	/*
6375 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6376 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6377 	 * be zero for restarting an ioctl that ends up downing the ill.
6378 	 */
6379 	ipif = ipsq->ipsq_pending_ipif;
6380 	if (ipif->ipif_ill != ill) {
6381 		/* The ioctl is pending on some other ill. */
6382 		mutex_exit(&ill->ill_lock);
6383 		return;
6384 	}
6385 
6386 	switch (ipsq->ipsq_waitfor) {
6387 	case IPIF_DOWN:
6388 		if (!ipif_is_quiescent(ipif)) {
6389 			mutex_exit(&ill->ill_lock);
6390 			return;
6391 		}
6392 		break;
6393 	case IPIF_FREE:
6394 		if (!ipif_is_freeable(ipif)) {
6395 			mutex_exit(&ill->ill_lock);
6396 			return;
6397 		}
6398 		break;
6399 
6400 	case ILL_DOWN:
6401 		if (!ill_is_quiescent(ill)) {
6402 			mutex_exit(&ill->ill_lock);
6403 			return;
6404 		}
6405 		break;
6406 	case ILL_FREE:
6407 		/*
6408 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6409 		 * waits synchronously in ip_close, and no message is queued in
6410 		 * ipsq_pending_mp at all in this case
6411 		 */
6412 		if (!ill_is_freeable(ill)) {
6413 			mutex_exit(&ill->ill_lock);
6414 			return;
6415 		}
6416 		break;
6417 
6418 	case ILL_MOVE_OK:
6419 		if (ill_quiescent_to_move(ill) != NULL) {
6420 			mutex_exit(&ill->ill_lock);
6421 			return;
6422 		}
6423 		break;
6424 	default:
6425 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6426 		    (void *)ipsq, ipsq->ipsq_waitfor);
6427 	}
6428 
6429 	/*
6430 	 * Incr refcnt for the qwriter_ip call below which
6431 	 * does a refrele
6432 	 */
6433 	ill_refhold_locked(ill);
6434 	mp = ipsq_pending_mp_get(ipsq, &connp);
6435 	mutex_exit(&ill->ill_lock);
6436 
6437 	ASSERT(mp != NULL);
6438 	/*
6439 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6440 	 * we can only get here when the current operation decides it
6441 	 * it needs to quiesce via ipsq_pending_mp_add().
6442 	 */
6443 	switch (mp->b_datap->db_type) {
6444 	case M_PCPROTO:
6445 	case M_PROTO:
6446 		/*
6447 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6448 		 */
6449 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6450 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6451 
6452 		switch (dlindp->dl_notification) {
6453 		case DL_NOTE_PHYS_ADDR:
6454 			qwriter_ip(ill, ill->ill_rq, mp,
6455 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6456 			return;
6457 		default:
6458 			ASSERT(0);
6459 		}
6460 		break;
6461 
6462 	case M_ERROR:
6463 	case M_HANGUP:
6464 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6465 		    B_TRUE);
6466 		return;
6467 
6468 	case M_IOCTL:
6469 	case M_IOCDATA:
6470 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6471 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6472 		return;
6473 
6474 	default:
6475 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6476 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6477 	}
6478 }
6479 
6480 #ifdef DEBUG
6481 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6482 static void
6483 th_trace_rrecord(th_trace_t *th_trace)
6484 {
6485 	tr_buf_t *tr_buf;
6486 	uint_t lastref;
6487 
6488 	lastref = th_trace->th_trace_lastref;
6489 	lastref++;
6490 	if (lastref == TR_BUF_MAX)
6491 		lastref = 0;
6492 	th_trace->th_trace_lastref = lastref;
6493 	tr_buf = &th_trace->th_trbuf[lastref];
6494 	tr_buf->tr_time = lbolt;
6495 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6496 }
6497 
6498 static void
6499 th_trace_free(void *value)
6500 {
6501 	th_trace_t *th_trace = value;
6502 
6503 	ASSERT(th_trace->th_refcnt == 0);
6504 	kmem_free(th_trace, sizeof (*th_trace));
6505 }
6506 
6507 /*
6508  * Find or create the per-thread hash table used to track object references.
6509  * The ipst argument is NULL if we shouldn't allocate.
6510  *
6511  * Accesses per-thread data, so there's no need to lock here.
6512  */
6513 static mod_hash_t *
6514 th_trace_gethash(ip_stack_t *ipst)
6515 {
6516 	th_hash_t *thh;
6517 
6518 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6519 		mod_hash_t *mh;
6520 		char name[256];
6521 		size_t objsize, rshift;
6522 		int retv;
6523 
6524 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6525 			return (NULL);
6526 		(void) snprintf(name, sizeof (name), "th_trace_%p", curthread);
6527 
6528 		/*
6529 		 * We use mod_hash_create_extended here rather than the more
6530 		 * obvious mod_hash_create_ptrhash because the latter has a
6531 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6532 		 * block.
6533 		 */
6534 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6535 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6536 		rshift = highbit(objsize);
6537 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6538 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6539 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6540 		if (mh == NULL) {
6541 			kmem_free(thh, sizeof (*thh));
6542 			return (NULL);
6543 		}
6544 		thh->thh_hash = mh;
6545 		thh->thh_ipst = ipst;
6546 		/*
6547 		 * We trace ills, ipifs, ires, and nces.  All of these are
6548 		 * per-IP-stack, so the lock on the thread list is as well.
6549 		 */
6550 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6551 		list_insert_tail(&ip_thread_list, thh);
6552 		rw_exit(&ip_thread_rwlock);
6553 		retv = tsd_set(ip_thread_data, thh);
6554 		ASSERT(retv == 0);
6555 	}
6556 	return (thh != NULL ? thh->thh_hash : NULL);
6557 }
6558 
6559 boolean_t
6560 th_trace_ref(const void *obj, ip_stack_t *ipst)
6561 {
6562 	th_trace_t *th_trace;
6563 	mod_hash_t *mh;
6564 	mod_hash_val_t val;
6565 
6566 	if ((mh = th_trace_gethash(ipst)) == NULL)
6567 		return (B_FALSE);
6568 
6569 	/*
6570 	 * Attempt to locate the trace buffer for this obj and thread.
6571 	 * If it does not exist, then allocate a new trace buffer and
6572 	 * insert into the hash.
6573 	 */
6574 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6575 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6576 		if (th_trace == NULL)
6577 			return (B_FALSE);
6578 
6579 		th_trace->th_id = curthread;
6580 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6581 		    (mod_hash_val_t)th_trace) != 0) {
6582 			kmem_free(th_trace, sizeof (th_trace_t));
6583 			return (B_FALSE);
6584 		}
6585 	} else {
6586 		th_trace = (th_trace_t *)val;
6587 	}
6588 
6589 	ASSERT(th_trace->th_refcnt >= 0 &&
6590 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6591 
6592 	th_trace->th_refcnt++;
6593 	th_trace_rrecord(th_trace);
6594 	return (B_TRUE);
6595 }
6596 
6597 /*
6598  * For the purpose of tracing a reference release, we assume that global
6599  * tracing is always on and that the same thread initiated the reference hold
6600  * is releasing.
6601  */
6602 void
6603 th_trace_unref(const void *obj)
6604 {
6605 	int retv;
6606 	mod_hash_t *mh;
6607 	th_trace_t *th_trace;
6608 	mod_hash_val_t val;
6609 
6610 	mh = th_trace_gethash(NULL);
6611 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6612 	ASSERT(retv == 0);
6613 	th_trace = (th_trace_t *)val;
6614 
6615 	ASSERT(th_trace->th_refcnt > 0);
6616 	th_trace->th_refcnt--;
6617 	th_trace_rrecord(th_trace);
6618 }
6619 
6620 /*
6621  * If tracing has been disabled, then we assume that the reference counts are
6622  * now useless, and we clear them out before destroying the entries.
6623  */
6624 void
6625 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6626 {
6627 	th_hash_t	*thh;
6628 	mod_hash_t	*mh;
6629 	mod_hash_val_t	val;
6630 	th_trace_t	*th_trace;
6631 	int		retv;
6632 
6633 	rw_enter(&ip_thread_rwlock, RW_READER);
6634 	for (thh = list_head(&ip_thread_list); thh != NULL;
6635 	    thh = list_next(&ip_thread_list, thh)) {
6636 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6637 		    &val) == 0) {
6638 			th_trace = (th_trace_t *)val;
6639 			if (trace_disable)
6640 				th_trace->th_refcnt = 0;
6641 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6642 			ASSERT(retv == 0);
6643 		}
6644 	}
6645 	rw_exit(&ip_thread_rwlock);
6646 }
6647 
6648 void
6649 ipif_trace_ref(ipif_t *ipif)
6650 {
6651 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6652 
6653 	if (ipif->ipif_trace_disable)
6654 		return;
6655 
6656 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6657 		ipif->ipif_trace_disable = B_TRUE;
6658 		ipif_trace_cleanup(ipif);
6659 	}
6660 }
6661 
6662 void
6663 ipif_untrace_ref(ipif_t *ipif)
6664 {
6665 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6666 
6667 	if (!ipif->ipif_trace_disable)
6668 		th_trace_unref(ipif);
6669 }
6670 
6671 void
6672 ill_trace_ref(ill_t *ill)
6673 {
6674 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6675 
6676 	if (ill->ill_trace_disable)
6677 		return;
6678 
6679 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6680 		ill->ill_trace_disable = B_TRUE;
6681 		ill_trace_cleanup(ill);
6682 	}
6683 }
6684 
6685 void
6686 ill_untrace_ref(ill_t *ill)
6687 {
6688 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6689 
6690 	if (!ill->ill_trace_disable)
6691 		th_trace_unref(ill);
6692 }
6693 
6694 /*
6695  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6696  * failure, ipif_trace_disable is set.
6697  */
6698 static void
6699 ipif_trace_cleanup(const ipif_t *ipif)
6700 {
6701 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6702 }
6703 
6704 /*
6705  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6706  * failure, ill_trace_disable is set.
6707  */
6708 static void
6709 ill_trace_cleanup(const ill_t *ill)
6710 {
6711 	th_trace_cleanup(ill, ill->ill_trace_disable);
6712 }
6713 #endif /* DEBUG */
6714 
6715 void
6716 ipif_refhold_locked(ipif_t *ipif)
6717 {
6718 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6719 	ipif->ipif_refcnt++;
6720 	IPIF_TRACE_REF(ipif);
6721 }
6722 
6723 void
6724 ipif_refhold(ipif_t *ipif)
6725 {
6726 	ill_t	*ill;
6727 
6728 	ill = ipif->ipif_ill;
6729 	mutex_enter(&ill->ill_lock);
6730 	ipif->ipif_refcnt++;
6731 	IPIF_TRACE_REF(ipif);
6732 	mutex_exit(&ill->ill_lock);
6733 }
6734 
6735 /*
6736  * Must not be called while holding any locks. Otherwise if this is
6737  * the last reference to be released there is a chance of recursive mutex
6738  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6739  * to restart an ioctl.
6740  */
6741 void
6742 ipif_refrele(ipif_t *ipif)
6743 {
6744 	ill_t	*ill;
6745 
6746 	ill = ipif->ipif_ill;
6747 
6748 	mutex_enter(&ill->ill_lock);
6749 	ASSERT(ipif->ipif_refcnt != 0);
6750 	ipif->ipif_refcnt--;
6751 	IPIF_UNTRACE_REF(ipif);
6752 	if (ipif->ipif_refcnt != 0) {
6753 		mutex_exit(&ill->ill_lock);
6754 		return;
6755 	}
6756 
6757 	/* Drops the ill_lock */
6758 	ipif_ill_refrele_tail(ill);
6759 }
6760 
6761 ipif_t *
6762 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6763 {
6764 	ipif_t	*ipif;
6765 
6766 	mutex_enter(&ill->ill_lock);
6767 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6768 	    ipif != NULL; ipif = ipif->ipif_next) {
6769 		if (!IPIF_CAN_LOOKUP(ipif))
6770 			continue;
6771 		ipif_refhold_locked(ipif);
6772 		mutex_exit(&ill->ill_lock);
6773 		return (ipif);
6774 	}
6775 	mutex_exit(&ill->ill_lock);
6776 	return (NULL);
6777 }
6778 
6779 /*
6780  * TODO: make this table extendible at run time
6781  * Return a pointer to the mac type info for 'mac_type'
6782  */
6783 static ip_m_t *
6784 ip_m_lookup(t_uscalar_t mac_type)
6785 {
6786 	ip_m_t	*ipm;
6787 
6788 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6789 		if (ipm->ip_m_mac_type == mac_type)
6790 			return (ipm);
6791 	return (NULL);
6792 }
6793 
6794 /*
6795  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6796  * ipif_arg is passed in to associate it with the correct interface.
6797  * We may need to restart this operation if the ipif cannot be looked up
6798  * due to an exclusive operation that is currently in progress. The restart
6799  * entry point is specified by 'func'
6800  */
6801 int
6802 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6803     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6804     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6805     struct rtsa_s *sp, ip_stack_t *ipst)
6806 {
6807 	ire_t	*ire;
6808 	ire_t	*gw_ire = NULL;
6809 	ipif_t	*ipif = NULL;
6810 	boolean_t ipif_refheld = B_FALSE;
6811 	uint_t	type;
6812 	int	match_flags = MATCH_IRE_TYPE;
6813 	int	error;
6814 	tsol_gc_t *gc = NULL;
6815 	tsol_gcgrp_t *gcgrp = NULL;
6816 	boolean_t gcgrp_xtraref = B_FALSE;
6817 
6818 	ip1dbg(("ip_rt_add:"));
6819 
6820 	if (ire_arg != NULL)
6821 		*ire_arg = NULL;
6822 
6823 	/*
6824 	 * If this is the case of RTF_HOST being set, then we set the netmask
6825 	 * to all ones (regardless if one was supplied).
6826 	 */
6827 	if (flags & RTF_HOST)
6828 		mask = IP_HOST_MASK;
6829 
6830 	/*
6831 	 * Prevent routes with a zero gateway from being created (since
6832 	 * interfaces can currently be plumbed and brought up no assigned
6833 	 * address).
6834 	 */
6835 	if (gw_addr == 0)
6836 		return (ENETUNREACH);
6837 	/*
6838 	 * Get the ipif, if any, corresponding to the gw_addr
6839 	 */
6840 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6841 	    ipst);
6842 	if (ipif != NULL) {
6843 		if (IS_VNI(ipif->ipif_ill)) {
6844 			ipif_refrele(ipif);
6845 			return (EINVAL);
6846 		}
6847 		ipif_refheld = B_TRUE;
6848 	} else if (error == EINPROGRESS) {
6849 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6850 		return (EINPROGRESS);
6851 	} else {
6852 		error = 0;
6853 	}
6854 
6855 	if (ipif != NULL) {
6856 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6857 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6858 	} else {
6859 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6860 	}
6861 
6862 	/*
6863 	 * GateD will attempt to create routes with a loopback interface
6864 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6865 	 * these routes to be added, but create them as interface routes
6866 	 * since the gateway is an interface address.
6867 	 */
6868 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6869 		flags &= ~RTF_GATEWAY;
6870 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6871 		    mask == IP_HOST_MASK) {
6872 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6873 			    ALL_ZONES, NULL, match_flags, ipst);
6874 			if (ire != NULL) {
6875 				ire_refrele(ire);
6876 				if (ipif_refheld)
6877 					ipif_refrele(ipif);
6878 				return (EEXIST);
6879 			}
6880 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6881 			    "for 0x%x\n", (void *)ipif,
6882 			    ipif->ipif_ire_type,
6883 			    ntohl(ipif->ipif_lcl_addr)));
6884 			ire = ire_create(
6885 			    (uchar_t *)&dst_addr,	/* dest address */
6886 			    (uchar_t *)&mask,		/* mask */
6887 			    (uchar_t *)&ipif->ipif_src_addr,
6888 			    NULL,			/* no gateway */
6889 			    &ipif->ipif_mtu,
6890 			    NULL,
6891 			    ipif->ipif_rq,		/* recv-from queue */
6892 			    NULL,			/* no send-to queue */
6893 			    ipif->ipif_ire_type,	/* LOOPBACK */
6894 			    ipif,
6895 			    0,
6896 			    0,
6897 			    0,
6898 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6899 			    RTF_PRIVATE : 0,
6900 			    &ire_uinfo_null,
6901 			    NULL,
6902 			    NULL,
6903 			    ipst);
6904 
6905 			if (ire == NULL) {
6906 				if (ipif_refheld)
6907 					ipif_refrele(ipif);
6908 				return (ENOMEM);
6909 			}
6910 			error = ire_add(&ire, q, mp, func, B_FALSE);
6911 			if (error == 0)
6912 				goto save_ire;
6913 			if (ipif_refheld)
6914 				ipif_refrele(ipif);
6915 			return (error);
6916 
6917 		}
6918 	}
6919 
6920 	/*
6921 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6922 	 * and the gateway address provided is one of the system's interface
6923 	 * addresses.  By using the routing socket interface and supplying an
6924 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6925 	 * specifying an interface route to be created is available which uses
6926 	 * the interface index that specifies the outgoing interface rather than
6927 	 * the address of an outgoing interface (which may not be able to
6928 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6929 	 * flag, routes can be specified which not only specify the next-hop to
6930 	 * be used when routing to a certain prefix, but also which outgoing
6931 	 * interface should be used.
6932 	 *
6933 	 * Previously, interfaces would have unique addresses assigned to them
6934 	 * and so the address assigned to a particular interface could be used
6935 	 * to identify a particular interface.  One exception to this was the
6936 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6937 	 *
6938 	 * With the advent of IPv6 and its link-local addresses, this
6939 	 * restriction was relaxed and interfaces could share addresses between
6940 	 * themselves.  In fact, typically all of the link-local interfaces on
6941 	 * an IPv6 node or router will have the same link-local address.  In
6942 	 * order to differentiate between these interfaces, the use of an
6943 	 * interface index is necessary and this index can be carried inside a
6944 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6945 	 * of using the interface index, however, is that all of the ipif's that
6946 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6947 	 * cannot be used to differentiate between ipif's (or logical
6948 	 * interfaces) that belong to the same ill (physical interface).
6949 	 *
6950 	 * For example, in the following case involving IPv4 interfaces and
6951 	 * logical interfaces
6952 	 *
6953 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6954 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6955 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6956 	 *
6957 	 * the ipif's corresponding to each of these interface routes can be
6958 	 * uniquely identified by the "gateway" (actually interface address).
6959 	 *
6960 	 * In this case involving multiple IPv6 default routes to a particular
6961 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6962 	 * default route is of interest:
6963 	 *
6964 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6965 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6966 	 */
6967 
6968 	/* RTF_GATEWAY not set */
6969 	if (!(flags & RTF_GATEWAY)) {
6970 		queue_t	*stq;
6971 
6972 		if (sp != NULL) {
6973 			ip2dbg(("ip_rt_add: gateway security attributes "
6974 			    "cannot be set with interface route\n"));
6975 			if (ipif_refheld)
6976 				ipif_refrele(ipif);
6977 			return (EINVAL);
6978 		}
6979 
6980 		/*
6981 		 * As the interface index specified with the RTA_IFP sockaddr is
6982 		 * the same for all ipif's off of an ill, the matching logic
6983 		 * below uses MATCH_IRE_ILL if such an index was specified.
6984 		 * This means that routes sharing the same prefix when added
6985 		 * using a RTA_IFP sockaddr must have distinct interface
6986 		 * indices (namely, they must be on distinct ill's).
6987 		 *
6988 		 * On the other hand, since the gateway address will usually be
6989 		 * different for each ipif on the system, the matching logic
6990 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6991 		 * route.  This means that interface routes for the same prefix
6992 		 * can be created if they belong to distinct ipif's and if a
6993 		 * RTA_IFP sockaddr is not present.
6994 		 */
6995 		if (ipif_arg != NULL) {
6996 			if (ipif_refheld)  {
6997 				ipif_refrele(ipif);
6998 				ipif_refheld = B_FALSE;
6999 			}
7000 			ipif = ipif_arg;
7001 			match_flags |= MATCH_IRE_ILL;
7002 		} else {
7003 			/*
7004 			 * Check the ipif corresponding to the gw_addr
7005 			 */
7006 			if (ipif == NULL)
7007 				return (ENETUNREACH);
7008 			match_flags |= MATCH_IRE_IPIF;
7009 		}
7010 		ASSERT(ipif != NULL);
7011 
7012 		/*
7013 		 * We check for an existing entry at this point.
7014 		 *
7015 		 * Since a netmask isn't passed in via the ioctl interface
7016 		 * (SIOCADDRT), we don't check for a matching netmask in that
7017 		 * case.
7018 		 */
7019 		if (!ioctl_msg)
7020 			match_flags |= MATCH_IRE_MASK;
7021 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
7022 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7023 		if (ire != NULL) {
7024 			ire_refrele(ire);
7025 			if (ipif_refheld)
7026 				ipif_refrele(ipif);
7027 			return (EEXIST);
7028 		}
7029 
7030 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
7031 		    ? ipif->ipif_rq : ipif->ipif_wq;
7032 
7033 		/*
7034 		 * Create a copy of the IRE_LOOPBACK,
7035 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
7036 		 * the modified address and netmask.
7037 		 */
7038 		ire = ire_create(
7039 		    (uchar_t *)&dst_addr,
7040 		    (uint8_t *)&mask,
7041 		    (uint8_t *)&ipif->ipif_src_addr,
7042 		    NULL,
7043 		    &ipif->ipif_mtu,
7044 		    NULL,
7045 		    NULL,
7046 		    stq,
7047 		    ipif->ipif_net_type,
7048 		    ipif,
7049 		    0,
7050 		    0,
7051 		    0,
7052 		    flags,
7053 		    &ire_uinfo_null,
7054 		    NULL,
7055 		    NULL,
7056 		    ipst);
7057 		if (ire == NULL) {
7058 			if (ipif_refheld)
7059 				ipif_refrele(ipif);
7060 			return (ENOMEM);
7061 		}
7062 
7063 		/*
7064 		 * Some software (for example, GateD and Sun Cluster) attempts
7065 		 * to create (what amount to) IRE_PREFIX routes with the
7066 		 * loopback address as the gateway.  This is primarily done to
7067 		 * set up prefixes with the RTF_REJECT flag set (for example,
7068 		 * when generating aggregate routes.)
7069 		 *
7070 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7071 		 * IRE_LOOPBACK, then we map the request into a
7072 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
7073 		 * these interface routes, by definition, can only be that.
7074 		 *
7075 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7076 		 * routine, but rather using ire_create() directly.
7077 		 *
7078 		 */
7079 		if (ipif->ipif_net_type == IRE_LOOPBACK) {
7080 			ire->ire_type = IRE_IF_NORESOLVER;
7081 			ire->ire_flags |= RTF_BLACKHOLE;
7082 		}
7083 
7084 		error = ire_add(&ire, q, mp, func, B_FALSE);
7085 		if (error == 0)
7086 			goto save_ire;
7087 
7088 		/*
7089 		 * In the result of failure, ire_add() will have already
7090 		 * deleted the ire in question, so there is no need to
7091 		 * do that here.
7092 		 */
7093 		if (ipif_refheld)
7094 			ipif_refrele(ipif);
7095 		return (error);
7096 	}
7097 	if (ipif_refheld) {
7098 		ipif_refrele(ipif);
7099 		ipif_refheld = B_FALSE;
7100 	}
7101 
7102 	/*
7103 	 * Get an interface IRE for the specified gateway.
7104 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7105 	 * gateway, it is currently unreachable and we fail the request
7106 	 * accordingly.
7107 	 */
7108 	ipif = ipif_arg;
7109 	if (ipif_arg != NULL)
7110 		match_flags |= MATCH_IRE_ILL;
7111 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7112 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7113 	if (gw_ire == NULL)
7114 		return (ENETUNREACH);
7115 
7116 	/*
7117 	 * We create one of three types of IREs as a result of this request
7118 	 * based on the netmask.  A netmask of all ones (which is automatically
7119 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7120 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7121 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7122 	 * destination prefix.
7123 	 */
7124 	if (mask == IP_HOST_MASK)
7125 		type = IRE_HOST;
7126 	else if (mask == 0)
7127 		type = IRE_DEFAULT;
7128 	else
7129 		type = IRE_PREFIX;
7130 
7131 	/* check for a duplicate entry */
7132 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7133 	    NULL, ALL_ZONES, 0, NULL,
7134 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7135 	if (ire != NULL) {
7136 		ire_refrele(gw_ire);
7137 		ire_refrele(ire);
7138 		return (EEXIST);
7139 	}
7140 
7141 	/* Security attribute exists */
7142 	if (sp != NULL) {
7143 		tsol_gcgrp_addr_t ga;
7144 
7145 		/* find or create the gateway credentials group */
7146 		ga.ga_af = AF_INET;
7147 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7148 
7149 		/* we hold reference to it upon success */
7150 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7151 		if (gcgrp == NULL) {
7152 			ire_refrele(gw_ire);
7153 			return (ENOMEM);
7154 		}
7155 
7156 		/*
7157 		 * Create and add the security attribute to the group; a
7158 		 * reference to the group is made upon allocating a new
7159 		 * entry successfully.  If it finds an already-existing
7160 		 * entry for the security attribute in the group, it simply
7161 		 * returns it and no new reference is made to the group.
7162 		 */
7163 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7164 		if (gc == NULL) {
7165 			/* release reference held by gcgrp_lookup */
7166 			GCGRP_REFRELE(gcgrp);
7167 			ire_refrele(gw_ire);
7168 			return (ENOMEM);
7169 		}
7170 	}
7171 
7172 	/* Create the IRE. */
7173 	ire = ire_create(
7174 	    (uchar_t *)&dst_addr,		/* dest address */
7175 	    (uchar_t *)&mask,			/* mask */
7176 	    /* src address assigned by the caller? */
7177 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7178 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7179 	    (uchar_t *)&gw_addr,		/* gateway address */
7180 	    &gw_ire->ire_max_frag,
7181 	    NULL,				/* no src nce */
7182 	    NULL,				/* no recv-from queue */
7183 	    NULL,				/* no send-to queue */
7184 	    (ushort_t)type,			/* IRE type */
7185 	    ipif_arg,
7186 	    0,
7187 	    0,
7188 	    0,
7189 	    flags,
7190 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7191 	    gc,					/* security attribute */
7192 	    NULL,
7193 	    ipst);
7194 
7195 	/*
7196 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7197 	 * reference to the 'gcgrp'. We can now release the extra reference
7198 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7199 	 */
7200 	if (gcgrp_xtraref)
7201 		GCGRP_REFRELE(gcgrp);
7202 	if (ire == NULL) {
7203 		if (gc != NULL)
7204 			GC_REFRELE(gc);
7205 		ire_refrele(gw_ire);
7206 		return (ENOMEM);
7207 	}
7208 
7209 	/*
7210 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7211 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7212 	 */
7213 
7214 	/* Add the new IRE. */
7215 	error = ire_add(&ire, q, mp, func, B_FALSE);
7216 	if (error != 0) {
7217 		/*
7218 		 * In the result of failure, ire_add() will have already
7219 		 * deleted the ire in question, so there is no need to
7220 		 * do that here.
7221 		 */
7222 		ire_refrele(gw_ire);
7223 		return (error);
7224 	}
7225 
7226 	if (flags & RTF_MULTIRT) {
7227 		/*
7228 		 * Invoke the CGTP (multirouting) filtering module
7229 		 * to add the dst address in the filtering database.
7230 		 * Replicated inbound packets coming from that address
7231 		 * will be filtered to discard the duplicates.
7232 		 * It is not necessary to call the CGTP filter hook
7233 		 * when the dst address is a broadcast or multicast,
7234 		 * because an IP source address cannot be a broadcast
7235 		 * or a multicast.
7236 		 */
7237 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7238 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7239 		if (ire_dst != NULL) {
7240 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7241 			ire_refrele(ire_dst);
7242 			goto save_ire;
7243 		}
7244 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7245 		    !CLASSD(ire->ire_addr)) {
7246 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7247 			    ipst->ips_netstack->netstack_stackid,
7248 			    ire->ire_addr,
7249 			    ire->ire_gateway_addr,
7250 			    ire->ire_src_addr,
7251 			    gw_ire->ire_src_addr);
7252 			if (res != 0) {
7253 				ire_refrele(gw_ire);
7254 				ire_delete(ire);
7255 				return (res);
7256 			}
7257 		}
7258 	}
7259 
7260 	/*
7261 	 * Now that the prefix IRE entry has been created, delete any
7262 	 * existing gateway IRE cache entries as well as any IRE caches
7263 	 * using the gateway, and force them to be created through
7264 	 * ip_newroute.
7265 	 */
7266 	if (gc != NULL) {
7267 		ASSERT(gcgrp != NULL);
7268 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7269 	}
7270 
7271 save_ire:
7272 	if (gw_ire != NULL) {
7273 		ire_refrele(gw_ire);
7274 	}
7275 	if (ipif != NULL) {
7276 		/*
7277 		 * Save enough information so that we can recreate the IRE if
7278 		 * the interface goes down and then up.  The metrics associated
7279 		 * with the route will be saved as well when rts_setmetrics() is
7280 		 * called after the IRE has been created.  In the case where
7281 		 * memory cannot be allocated, none of this information will be
7282 		 * saved.
7283 		 */
7284 		ipif_save_ire(ipif, ire);
7285 	}
7286 	if (ioctl_msg)
7287 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7288 	if (ire_arg != NULL) {
7289 		/*
7290 		 * Store the ire that was successfully added into where ire_arg
7291 		 * points to so that callers don't have to look it up
7292 		 * themselves (but they are responsible for ire_refrele()ing
7293 		 * the ire when they are finished with it).
7294 		 */
7295 		*ire_arg = ire;
7296 	} else {
7297 		ire_refrele(ire);		/* Held in ire_add */
7298 	}
7299 	if (ipif_refheld)
7300 		ipif_refrele(ipif);
7301 	return (0);
7302 }
7303 
7304 /*
7305  * ip_rt_delete is called to delete an IPv4 route.
7306  * ipif_arg is passed in to associate it with the correct interface.
7307  * We may need to restart this operation if the ipif cannot be looked up
7308  * due to an exclusive operation that is currently in progress. The restart
7309  * entry point is specified by 'func'
7310  */
7311 /* ARGSUSED4 */
7312 int
7313 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7314     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7315     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7316 {
7317 	ire_t	*ire = NULL;
7318 	ipif_t	*ipif;
7319 	boolean_t ipif_refheld = B_FALSE;
7320 	uint_t	type;
7321 	uint_t	match_flags = MATCH_IRE_TYPE;
7322 	int	err = 0;
7323 
7324 	ip1dbg(("ip_rt_delete:"));
7325 	/*
7326 	 * If this is the case of RTF_HOST being set, then we set the netmask
7327 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7328 	 */
7329 	if (flags & RTF_HOST) {
7330 		mask = IP_HOST_MASK;
7331 		match_flags |= MATCH_IRE_MASK;
7332 	} else if (rtm_addrs & RTA_NETMASK) {
7333 		match_flags |= MATCH_IRE_MASK;
7334 	}
7335 
7336 	/*
7337 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7338 	 * we check if the gateway address is one of our interfaces first,
7339 	 * and fall back on RTF_GATEWAY routes.
7340 	 *
7341 	 * This makes it possible to delete an original
7342 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7343 	 *
7344 	 * As the interface index specified with the RTA_IFP sockaddr is the
7345 	 * same for all ipif's off of an ill, the matching logic below uses
7346 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7347 	 * sharing the same prefix and interface index as the the route
7348 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7349 	 * is specified in the request.
7350 	 *
7351 	 * On the other hand, since the gateway address will usually be
7352 	 * different for each ipif on the system, the matching logic
7353 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7354 	 * route.  This means that interface routes for the same prefix can be
7355 	 * uniquely identified if they belong to distinct ipif's and if a
7356 	 * RTA_IFP sockaddr is not present.
7357 	 *
7358 	 * For more detail on specifying routes by gateway address and by
7359 	 * interface index, see the comments in ip_rt_add().
7360 	 */
7361 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7362 	    ipst);
7363 	if (ipif != NULL)
7364 		ipif_refheld = B_TRUE;
7365 	else if (err == EINPROGRESS)
7366 		return (err);
7367 	else
7368 		err = 0;
7369 	if (ipif != NULL) {
7370 		if (ipif_arg != NULL) {
7371 			if (ipif_refheld) {
7372 				ipif_refrele(ipif);
7373 				ipif_refheld = B_FALSE;
7374 			}
7375 			ipif = ipif_arg;
7376 			match_flags |= MATCH_IRE_ILL;
7377 		} else {
7378 			match_flags |= MATCH_IRE_IPIF;
7379 		}
7380 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7381 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7382 			    ALL_ZONES, NULL, match_flags, ipst);
7383 		}
7384 		if (ire == NULL) {
7385 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7386 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7387 			    match_flags, ipst);
7388 		}
7389 	}
7390 
7391 	if (ire == NULL) {
7392 		/*
7393 		 * At this point, the gateway address is not one of our own
7394 		 * addresses or a matching interface route was not found.  We
7395 		 * set the IRE type to lookup based on whether
7396 		 * this is a host route, a default route or just a prefix.
7397 		 *
7398 		 * If an ipif_arg was passed in, then the lookup is based on an
7399 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7400 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7401 		 * set as the route being looked up is not a traditional
7402 		 * interface route.
7403 		 */
7404 		match_flags &= ~MATCH_IRE_IPIF;
7405 		match_flags |= MATCH_IRE_GW;
7406 		if (ipif_arg != NULL)
7407 			match_flags |= MATCH_IRE_ILL;
7408 		if (mask == IP_HOST_MASK)
7409 			type = IRE_HOST;
7410 		else if (mask == 0)
7411 			type = IRE_DEFAULT;
7412 		else
7413 			type = IRE_PREFIX;
7414 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7415 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7416 	}
7417 
7418 	if (ipif_refheld)
7419 		ipif_refrele(ipif);
7420 
7421 	/* ipif is not refheld anymore */
7422 	if (ire == NULL)
7423 		return (ESRCH);
7424 
7425 	if (ire->ire_flags & RTF_MULTIRT) {
7426 		/*
7427 		 * Invoke the CGTP (multirouting) filtering module
7428 		 * to remove the dst address from the filtering database.
7429 		 * Packets coming from that address will no longer be
7430 		 * filtered to remove duplicates.
7431 		 */
7432 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7433 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7434 			    ipst->ips_netstack->netstack_stackid,
7435 			    ire->ire_addr, ire->ire_gateway_addr);
7436 		}
7437 		ip_cgtp_bcast_delete(ire, ipst);
7438 	}
7439 
7440 	ipif = ire->ire_ipif;
7441 	if (ipif != NULL)
7442 		ipif_remove_ire(ipif, ire);
7443 	if (ioctl_msg)
7444 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7445 	ire_delete(ire);
7446 	ire_refrele(ire);
7447 	return (err);
7448 }
7449 
7450 /*
7451  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7452  */
7453 /* ARGSUSED */
7454 int
7455 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7456     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7457 {
7458 	ipaddr_t dst_addr;
7459 	ipaddr_t gw_addr;
7460 	ipaddr_t mask;
7461 	int error = 0;
7462 	mblk_t *mp1;
7463 	struct rtentry *rt;
7464 	ipif_t *ipif = NULL;
7465 	ip_stack_t	*ipst;
7466 
7467 	ASSERT(q->q_next == NULL);
7468 	ipst = CONNQ_TO_IPST(q);
7469 
7470 	ip1dbg(("ip_siocaddrt:"));
7471 	/* Existence of mp1 verified in ip_wput_nondata */
7472 	mp1 = mp->b_cont->b_cont;
7473 	rt = (struct rtentry *)mp1->b_rptr;
7474 
7475 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7476 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7477 
7478 	/*
7479 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7480 	 * to a particular host address.  In this case, we set the netmask to
7481 	 * all ones for the particular destination address.  Otherwise,
7482 	 * determine the netmask to be used based on dst_addr and the interfaces
7483 	 * in use.
7484 	 */
7485 	if (rt->rt_flags & RTF_HOST) {
7486 		mask = IP_HOST_MASK;
7487 	} else {
7488 		/*
7489 		 * Note that ip_subnet_mask returns a zero mask in the case of
7490 		 * default (an all-zeroes address).
7491 		 */
7492 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7493 	}
7494 
7495 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7496 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7497 	if (ipif != NULL)
7498 		ipif_refrele(ipif);
7499 	return (error);
7500 }
7501 
7502 /*
7503  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7504  */
7505 /* ARGSUSED */
7506 int
7507 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7508     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7509 {
7510 	ipaddr_t dst_addr;
7511 	ipaddr_t gw_addr;
7512 	ipaddr_t mask;
7513 	int error;
7514 	mblk_t *mp1;
7515 	struct rtentry *rt;
7516 	ipif_t *ipif = NULL;
7517 	ip_stack_t	*ipst;
7518 
7519 	ASSERT(q->q_next == NULL);
7520 	ipst = CONNQ_TO_IPST(q);
7521 
7522 	ip1dbg(("ip_siocdelrt:"));
7523 	/* Existence of mp1 verified in ip_wput_nondata */
7524 	mp1 = mp->b_cont->b_cont;
7525 	rt = (struct rtentry *)mp1->b_rptr;
7526 
7527 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7528 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7529 
7530 	/*
7531 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7532 	 * to a particular host address.  In this case, we set the netmask to
7533 	 * all ones for the particular destination address.  Otherwise,
7534 	 * determine the netmask to be used based on dst_addr and the interfaces
7535 	 * in use.
7536 	 */
7537 	if (rt->rt_flags & RTF_HOST) {
7538 		mask = IP_HOST_MASK;
7539 	} else {
7540 		/*
7541 		 * Note that ip_subnet_mask returns a zero mask in the case of
7542 		 * default (an all-zeroes address).
7543 		 */
7544 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7545 	}
7546 
7547 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7548 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7549 	    mp, ip_process_ioctl, ipst);
7550 	if (ipif != NULL)
7551 		ipif_refrele(ipif);
7552 	return (error);
7553 }
7554 
7555 /*
7556  * Enqueue the mp onto the ipsq, chained by b_next.
7557  * b_prev stores the function to be executed later, and b_queue the queue
7558  * where this mp originated.
7559  */
7560 void
7561 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7562     ill_t *pending_ill)
7563 {
7564 	conn_t	*connp = NULL;
7565 
7566 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7567 	ASSERT(func != NULL);
7568 
7569 	mp->b_queue = q;
7570 	mp->b_prev = (void *)func;
7571 	mp->b_next = NULL;
7572 
7573 	switch (type) {
7574 	case CUR_OP:
7575 		if (ipsq->ipsq_mptail != NULL) {
7576 			ASSERT(ipsq->ipsq_mphead != NULL);
7577 			ipsq->ipsq_mptail->b_next = mp;
7578 		} else {
7579 			ASSERT(ipsq->ipsq_mphead == NULL);
7580 			ipsq->ipsq_mphead = mp;
7581 		}
7582 		ipsq->ipsq_mptail = mp;
7583 		break;
7584 
7585 	case NEW_OP:
7586 		if (ipsq->ipsq_xopq_mptail != NULL) {
7587 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7588 			ipsq->ipsq_xopq_mptail->b_next = mp;
7589 		} else {
7590 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7591 			ipsq->ipsq_xopq_mphead = mp;
7592 		}
7593 		ipsq->ipsq_xopq_mptail = mp;
7594 		break;
7595 	default:
7596 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7597 	}
7598 
7599 	if (CONN_Q(q) && pending_ill != NULL) {
7600 		connp = Q_TO_CONN(q);
7601 
7602 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7603 		connp->conn_oper_pending_ill = pending_ill;
7604 	}
7605 }
7606 
7607 /*
7608  * Return the mp at the head of the ipsq. After emptying the ipsq
7609  * look at the next ioctl, if this ioctl is complete. Otherwise
7610  * return, we will resume when we complete the current ioctl.
7611  * The current ioctl will wait till it gets a response from the
7612  * driver below.
7613  */
7614 static mblk_t *
7615 ipsq_dq(ipsq_t *ipsq)
7616 {
7617 	mblk_t	*mp;
7618 
7619 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7620 
7621 	mp = ipsq->ipsq_mphead;
7622 	if (mp != NULL) {
7623 		ipsq->ipsq_mphead = mp->b_next;
7624 		if (ipsq->ipsq_mphead == NULL)
7625 			ipsq->ipsq_mptail = NULL;
7626 		mp->b_next = NULL;
7627 		return (mp);
7628 	}
7629 	if (ipsq->ipsq_current_ipif != NULL)
7630 		return (NULL);
7631 	mp = ipsq->ipsq_xopq_mphead;
7632 	if (mp != NULL) {
7633 		ipsq->ipsq_xopq_mphead = mp->b_next;
7634 		if (ipsq->ipsq_xopq_mphead == NULL)
7635 			ipsq->ipsq_xopq_mptail = NULL;
7636 		mp->b_next = NULL;
7637 		return (mp);
7638 	}
7639 	return (NULL);
7640 }
7641 
7642 /*
7643  * Enter the ipsq corresponding to ill, by waiting synchronously till
7644  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7645  * will have to drain completely before ipsq_enter returns success.
7646  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7647  * and the ipsq_exit logic will start the next enqueued ioctl after
7648  * completion of the current ioctl. If 'force' is used, we don't wait
7649  * for the enqueued ioctls. This is needed when a conn_close wants to
7650  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7651  * of an ill can also use this option. But we dont' use it currently.
7652  */
7653 #define	ENTER_SQ_WAIT_TICKS 100
7654 boolean_t
7655 ipsq_enter(ill_t *ill, boolean_t force)
7656 {
7657 	ipsq_t	*ipsq;
7658 	boolean_t waited_enough = B_FALSE;
7659 
7660 	/*
7661 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7662 	 * Since the <ill-ipsq> assocs could change while we wait for the
7663 	 * writer, it is easier to wait on a fixed global rather than try to
7664 	 * cv_wait on a changing ipsq.
7665 	 */
7666 	mutex_enter(&ill->ill_lock);
7667 	for (;;) {
7668 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7669 			mutex_exit(&ill->ill_lock);
7670 			return (B_FALSE);
7671 		}
7672 
7673 		ipsq = ill->ill_phyint->phyint_ipsq;
7674 		mutex_enter(&ipsq->ipsq_lock);
7675 		if (ipsq->ipsq_writer == NULL &&
7676 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7677 			break;
7678 		} else if (ipsq->ipsq_writer != NULL) {
7679 			mutex_exit(&ipsq->ipsq_lock);
7680 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7681 		} else {
7682 			mutex_exit(&ipsq->ipsq_lock);
7683 			if (force) {
7684 				(void) cv_timedwait(&ill->ill_cv,
7685 				    &ill->ill_lock,
7686 				    lbolt + ENTER_SQ_WAIT_TICKS);
7687 				waited_enough = B_TRUE;
7688 				continue;
7689 			} else {
7690 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7691 			}
7692 		}
7693 	}
7694 
7695 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7696 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7697 	ipsq->ipsq_writer = curthread;
7698 	ipsq->ipsq_reentry_cnt++;
7699 #ifdef DEBUG
7700 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IPSQ_STACK_DEPTH);
7701 #endif
7702 	mutex_exit(&ipsq->ipsq_lock);
7703 	mutex_exit(&ill->ill_lock);
7704 	return (B_TRUE);
7705 }
7706 
7707 /*
7708  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7709  * certain critical operations like plumbing (i.e. most set ioctls),
7710  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7711  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7712  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7713  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7714  * threads executing in the ipsq. Responses from the driver pertain to the
7715  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7716  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7717  *
7718  * If a thread does not want to reenter the ipsq when it is already writer,
7719  * it must make sure that the specified reentry point to be called later
7720  * when the ipsq is empty, nor any code path starting from the specified reentry
7721  * point must never ever try to enter the ipsq again. Otherwise it can lead
7722  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7723  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7724  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7725  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7726  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7727  * ioctl if the current ioctl has completed. If the current ioctl is still
7728  * in progress it simply returns. The current ioctl could be waiting for
7729  * a response from another module (arp_ or the driver or could be waiting for
7730  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7731  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7732  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7733  * ipsq_current_ipif is clear which happens only on ioctl completion.
7734  */
7735 
7736 /*
7737  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7738  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7739  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7740  * completion.
7741  */
7742 ipsq_t *
7743 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7744     ipsq_func_t func, int type, boolean_t reentry_ok)
7745 {
7746 	ipsq_t	*ipsq;
7747 
7748 	/* Only 1 of ipif or ill can be specified */
7749 	ASSERT((ipif != NULL) ^ (ill != NULL));
7750 	if (ipif != NULL)
7751 		ill = ipif->ipif_ill;
7752 
7753 	/*
7754 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7755 	 * ipsq of an ill can't change when ill_lock is held.
7756 	 */
7757 	GRAB_CONN_LOCK(q);
7758 	mutex_enter(&ill->ill_lock);
7759 	ipsq = ill->ill_phyint->phyint_ipsq;
7760 	mutex_enter(&ipsq->ipsq_lock);
7761 
7762 	/*
7763 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7764 	 *    (Note: If the caller does not specify reentry_ok then neither
7765 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7766 	 *    again. Otherwise it can lead to an infinite loop
7767 	 * 2. Enter the ipsq if there is no current writer and this attempted
7768 	 *    entry is part of the current ioctl or operation
7769 	 * 3. Enter the ipsq if there is no current writer and this is a new
7770 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7771 	 *    empty and there is no ioctl (or operation) currently in progress
7772 	 */
7773 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7774 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7775 	    ipsq->ipsq_current_ipif == NULL))) ||
7776 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7777 		/* Success. */
7778 		ipsq->ipsq_reentry_cnt++;
7779 		ipsq->ipsq_writer = curthread;
7780 		mutex_exit(&ipsq->ipsq_lock);
7781 		mutex_exit(&ill->ill_lock);
7782 		RELEASE_CONN_LOCK(q);
7783 #ifdef DEBUG
7784 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack,
7785 		    IPSQ_STACK_DEPTH);
7786 #endif
7787 		return (ipsq);
7788 	}
7789 
7790 	ipsq_enq(ipsq, q, mp, func, type, ill);
7791 
7792 	mutex_exit(&ipsq->ipsq_lock);
7793 	mutex_exit(&ill->ill_lock);
7794 	RELEASE_CONN_LOCK(q);
7795 	return (NULL);
7796 }
7797 
7798 /*
7799  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7800  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7801  * cannot be entered, the mp is queued for completion.
7802  */
7803 void
7804 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7805     boolean_t reentry_ok)
7806 {
7807 	ipsq_t	*ipsq;
7808 
7809 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7810 
7811 	/*
7812 	 * Drop the caller's refhold on the ill.  This is safe since we either
7813 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7814 	 * IPSQ, in which case we return without accessing ill anymore.  This
7815 	 * is needed because func needs to see the correct refcount.
7816 	 * e.g. removeif can work only then.
7817 	 */
7818 	ill_refrele(ill);
7819 	if (ipsq != NULL) {
7820 		(*func)(ipsq, q, mp, NULL);
7821 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7822 	}
7823 }
7824 
7825 /*
7826  * If there are more than ILL_GRP_CNT ills in a group,
7827  * we use kmem alloc'd buffers, else use the stack
7828  */
7829 #define	ILL_GRP_CNT	14
7830 /*
7831  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7832  * Called by a thread that is currently exclusive on this ipsq.
7833  */
7834 void
7835 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7836 {
7837 	queue_t	*q;
7838 	mblk_t	*mp;
7839 	ipsq_func_t	func;
7840 	int	next;
7841 	ill_t	**ill_list = NULL;
7842 	size_t	ill_list_size = 0;
7843 	int	cnt = 0;
7844 	boolean_t need_ipsq_free = B_FALSE;
7845 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7846 
7847 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7848 	mutex_enter(&ipsq->ipsq_lock);
7849 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7850 	if (ipsq->ipsq_reentry_cnt != 1) {
7851 		ipsq->ipsq_reentry_cnt--;
7852 		mutex_exit(&ipsq->ipsq_lock);
7853 		return;
7854 	}
7855 
7856 	mp = ipsq_dq(ipsq);
7857 	while (mp != NULL) {
7858 again:
7859 		mutex_exit(&ipsq->ipsq_lock);
7860 		func = (ipsq_func_t)mp->b_prev;
7861 		q = (queue_t *)mp->b_queue;
7862 		mp->b_prev = NULL;
7863 		mp->b_queue = NULL;
7864 
7865 		/*
7866 		 * If 'q' is an conn queue, it is valid, since we did a
7867 		 * a refhold on the connp, at the start of the ioctl.
7868 		 * If 'q' is an ill queue, it is valid, since close of an
7869 		 * ill will clean up the 'ipsq'.
7870 		 */
7871 		(*func)(ipsq, q, mp, NULL);
7872 
7873 		mutex_enter(&ipsq->ipsq_lock);
7874 		mp = ipsq_dq(ipsq);
7875 	}
7876 
7877 	mutex_exit(&ipsq->ipsq_lock);
7878 
7879 	/*
7880 	 * Need to grab the locks in the right order. Need to
7881 	 * atomically check (under ipsq_lock) that there are no
7882 	 * messages before relinquishing the ipsq. Also need to
7883 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7884 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7885 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7886 	 * to grab ill_g_lock as writer.
7887 	 */
7888 	rw_enter(&ipst->ips_ill_g_lock,
7889 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7890 
7891 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7892 	if (ipsq->ipsq_refs != 0) {
7893 		/* At most 2 ills v4/v6 per phyint */
7894 		cnt = ipsq->ipsq_refs << 1;
7895 		ill_list_size = cnt * sizeof (ill_t *);
7896 		/*
7897 		 * If memory allocation fails, we will do the split
7898 		 * the next time ipsq_exit is called for whatever reason.
7899 		 * As long as the ipsq_split flag is set the need to
7900 		 * split is remembered.
7901 		 */
7902 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7903 		if (ill_list != NULL)
7904 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7905 	}
7906 	mutex_enter(&ipsq->ipsq_lock);
7907 	mp = ipsq_dq(ipsq);
7908 	if (mp != NULL) {
7909 		/* oops, some message has landed up, we can't get out */
7910 		if (ill_list != NULL)
7911 			ill_unlock_ills(ill_list, cnt);
7912 		rw_exit(&ipst->ips_ill_g_lock);
7913 		if (ill_list != NULL)
7914 			kmem_free(ill_list, ill_list_size);
7915 		ill_list = NULL;
7916 		ill_list_size = 0;
7917 		cnt = 0;
7918 		goto again;
7919 	}
7920 
7921 	/*
7922 	 * Split only if no ioctl is pending and if memory alloc succeeded
7923 	 * above.
7924 	 */
7925 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7926 	    ill_list != NULL) {
7927 		/*
7928 		 * No new ill can join this ipsq since we are holding the
7929 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7930 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7931 		 * If so we will retry on the next ipsq_exit.
7932 		 */
7933 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7934 	}
7935 
7936 	/*
7937 	 * We are holding the ipsq lock, hence no new messages can
7938 	 * land up on the ipsq, and there are no messages currently.
7939 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7940 	 * atomically while holding ill locks.
7941 	 */
7942 	ipsq->ipsq_writer = NULL;
7943 	ipsq->ipsq_reentry_cnt--;
7944 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7945 #ifdef DEBUG
7946 	ipsq->ipsq_depth = 0;
7947 #endif
7948 	mutex_exit(&ipsq->ipsq_lock);
7949 	/*
7950 	 * For IPMP this should wake up all ills in this ipsq.
7951 	 * We need to hold the ill_lock while waking up waiters to
7952 	 * avoid missed wakeups. But there is no need to acquire all
7953 	 * the ill locks and then wakeup. If we have not acquired all
7954 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7955 	 * wakes up ills one at a time after getting the right ill_lock
7956 	 */
7957 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7958 	if (ill_list != NULL)
7959 		ill_unlock_ills(ill_list, cnt);
7960 	if (ipsq->ipsq_refs == 0)
7961 		need_ipsq_free = B_TRUE;
7962 	rw_exit(&ipst->ips_ill_g_lock);
7963 	if (ill_list != 0)
7964 		kmem_free(ill_list, ill_list_size);
7965 
7966 	if (need_ipsq_free) {
7967 		/*
7968 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7969 		 * looked up. ipsq can be looked up only thru ill or phyint
7970 		 * and there are no ills/phyint on this ipsq.
7971 		 */
7972 		ipsq_delete(ipsq);
7973 	}
7974 	/*
7975 	 * Now start any igmp or mld timers that could not be started
7976 	 * while inside the ipsq. The timers can't be started while inside
7977 	 * the ipsq, since igmp_start_timers may need to call untimeout()
7978 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
7979 	 * there could be a deadlock since the timeout handlers
7980 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
7981 	 * wait in ipsq_enter() trying to get the ipsq.
7982 	 *
7983 	 * However there is one exception to the above. If this thread is
7984 	 * itself the igmp/mld timeout handler thread, then we don't want
7985 	 * to start any new timer until the current handler is done. The
7986 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
7987 	 * all others pass B_TRUE.
7988 	 */
7989 	if (start_igmp_timer) {
7990 		mutex_enter(&ipst->ips_igmp_timer_lock);
7991 		next = ipst->ips_igmp_deferred_next;
7992 		ipst->ips_igmp_deferred_next = INFINITY;
7993 		mutex_exit(&ipst->ips_igmp_timer_lock);
7994 
7995 		if (next != INFINITY)
7996 			igmp_start_timers(next, ipst);
7997 	}
7998 
7999 	if (start_mld_timer) {
8000 		mutex_enter(&ipst->ips_mld_timer_lock);
8001 		next = ipst->ips_mld_deferred_next;
8002 		ipst->ips_mld_deferred_next = INFINITY;
8003 		mutex_exit(&ipst->ips_mld_timer_lock);
8004 
8005 		if (next != INFINITY)
8006 			mld_start_timers(next, ipst);
8007 	}
8008 }
8009 
8010 /*
8011  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
8012  * and `ioccmd'.
8013  */
8014 void
8015 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
8016 {
8017 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8018 
8019 	mutex_enter(&ipsq->ipsq_lock);
8020 	ASSERT(ipsq->ipsq_current_ipif == NULL);
8021 	ASSERT(ipsq->ipsq_current_ioctl == 0);
8022 	ipsq->ipsq_current_done = B_FALSE;
8023 	ipsq->ipsq_current_ipif = ipif;
8024 	ipsq->ipsq_current_ioctl = ioccmd;
8025 	mutex_exit(&ipsq->ipsq_lock);
8026 }
8027 
8028 /*
8029  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
8030  * the next exclusive operation to begin once we ipsq_exit().  However, if
8031  * pending DLPI operations remain, then we will wait for the queue to drain
8032  * before allowing the next exclusive operation to begin.  This ensures that
8033  * DLPI operations from one exclusive operation are never improperly processed
8034  * as part of a subsequent exclusive operation.
8035  */
8036 void
8037 ipsq_current_finish(ipsq_t *ipsq)
8038 {
8039 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8040 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
8041 
8042 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8043 
8044 	/*
8045 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8046 	 * (but in that case, IPIF_CHANGING will already be clear and no
8047 	 * pending DLPI messages can remain).
8048 	 */
8049 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8050 		ill_t *ill = ipif->ipif_ill;
8051 
8052 		mutex_enter(&ill->ill_lock);
8053 		dlpi_pending = ill->ill_dlpi_pending;
8054 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8055 		/* Send any queued event */
8056 		ill_nic_info_dispatch(ill);
8057 		mutex_exit(&ill->ill_lock);
8058 	}
8059 
8060 	mutex_enter(&ipsq->ipsq_lock);
8061 	ipsq->ipsq_current_ioctl = 0;
8062 	ipsq->ipsq_current_done = B_TRUE;
8063 	if (dlpi_pending == DL_PRIM_INVAL)
8064 		ipsq->ipsq_current_ipif = NULL;
8065 	mutex_exit(&ipsq->ipsq_lock);
8066 }
8067 
8068 /*
8069  * The ill is closing. Flush all messages on the ipsq that originated
8070  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8071  * for this ill since ipsq_enter could not have entered until then.
8072  * New messages can't be queued since the CONDEMNED flag is set.
8073  */
8074 static void
8075 ipsq_flush(ill_t *ill)
8076 {
8077 	queue_t	*q;
8078 	mblk_t	*prev;
8079 	mblk_t	*mp;
8080 	mblk_t	*mp_next;
8081 	ipsq_t	*ipsq;
8082 
8083 	ASSERT(IAM_WRITER_ILL(ill));
8084 	ipsq = ill->ill_phyint->phyint_ipsq;
8085 	/*
8086 	 * Flush any messages sent up by the driver.
8087 	 */
8088 	mutex_enter(&ipsq->ipsq_lock);
8089 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8090 		mp_next = mp->b_next;
8091 		q = mp->b_queue;
8092 		if (q == ill->ill_rq || q == ill->ill_wq) {
8093 			/* Remove the mp from the ipsq */
8094 			if (prev == NULL)
8095 				ipsq->ipsq_mphead = mp->b_next;
8096 			else
8097 				prev->b_next = mp->b_next;
8098 			if (ipsq->ipsq_mptail == mp) {
8099 				ASSERT(mp_next == NULL);
8100 				ipsq->ipsq_mptail = prev;
8101 			}
8102 			inet_freemsg(mp);
8103 		} else {
8104 			prev = mp;
8105 		}
8106 	}
8107 	mutex_exit(&ipsq->ipsq_lock);
8108 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8109 	ipsq_xopq_mp_cleanup(ill, NULL);
8110 	ill_pending_mp_cleanup(ill);
8111 }
8112 
8113 /* ARGSUSED */
8114 int
8115 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8116     ip_ioctl_cmd_t *ipip, void *ifreq)
8117 {
8118 	ill_t	*ill;
8119 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8120 	boolean_t isv6;
8121 	conn_t	*connp;
8122 	ip_stack_t	*ipst;
8123 
8124 	connp = Q_TO_CONN(q);
8125 	ipst = connp->conn_netstack->netstack_ip;
8126 	isv6 = connp->conn_af_isv6;
8127 	/*
8128 	 * Set original index.
8129 	 * Failover and failback move logical interfaces
8130 	 * from one physical interface to another.  The
8131 	 * original index indicates the parent of a logical
8132 	 * interface, in other words, the physical interface
8133 	 * the logical interface will be moved back to on
8134 	 * failback.
8135 	 */
8136 
8137 	/*
8138 	 * Don't allow the original index to be changed
8139 	 * for non-failover addresses, autoconfigured
8140 	 * addresses, or IPv6 link local addresses.
8141 	 */
8142 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8143 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8144 		return (EINVAL);
8145 	}
8146 	/*
8147 	 * The new original index must be in use by some
8148 	 * physical interface.
8149 	 */
8150 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8151 	    NULL, NULL, ipst);
8152 	if (ill == NULL)
8153 		return (ENXIO);
8154 	ill_refrele(ill);
8155 
8156 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8157 	/*
8158 	 * When this ipif gets failed back, don't
8159 	 * preserve the original id, as it is no
8160 	 * longer applicable.
8161 	 */
8162 	ipif->ipif_orig_ipifid = 0;
8163 	/*
8164 	 * For IPv4, change the original index of any
8165 	 * multicast addresses associated with the
8166 	 * ipif to the new value.
8167 	 */
8168 	if (!isv6) {
8169 		ilm_t *ilm;
8170 
8171 		mutex_enter(&ipif->ipif_ill->ill_lock);
8172 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8173 		    ilm = ilm->ilm_next) {
8174 			if (ilm->ilm_ipif == ipif) {
8175 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8176 			}
8177 		}
8178 		mutex_exit(&ipif->ipif_ill->ill_lock);
8179 	}
8180 	return (0);
8181 }
8182 
8183 /* ARGSUSED */
8184 int
8185 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8186     ip_ioctl_cmd_t *ipip, void *ifreq)
8187 {
8188 	struct lifreq *lifr = (struct lifreq *)ifreq;
8189 
8190 	/*
8191 	 * Get the original interface index i.e the one
8192 	 * before FAILOVER if it ever happened.
8193 	 */
8194 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8195 	return (0);
8196 }
8197 
8198 /*
8199  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8200  * refhold and return the associated ipif
8201  */
8202 /* ARGSUSED */
8203 int
8204 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8205     cmd_info_t *ci, ipsq_func_t func)
8206 {
8207 	boolean_t exists;
8208 	struct iftun_req *ta;
8209 	ipif_t	*ipif;
8210 	ill_t	*ill;
8211 	boolean_t isv6;
8212 	mblk_t	*mp1;
8213 	int	error;
8214 	conn_t	*connp;
8215 	ip_stack_t	*ipst;
8216 
8217 	/* Existence verified in ip_wput_nondata */
8218 	mp1 = mp->b_cont->b_cont;
8219 	ta = (struct iftun_req *)mp1->b_rptr;
8220 	/*
8221 	 * Null terminate the string to protect against buffer
8222 	 * overrun. String was generated by user code and may not
8223 	 * be trusted.
8224 	 */
8225 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8226 
8227 	connp = Q_TO_CONN(q);
8228 	isv6 = connp->conn_af_isv6;
8229 	ipst = connp->conn_netstack->netstack_ip;
8230 
8231 	/* Disallows implicit create */
8232 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8233 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8234 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8235 	if (ipif == NULL)
8236 		return (error);
8237 
8238 	if (ipif->ipif_id != 0) {
8239 		/*
8240 		 * We really don't want to set/get tunnel parameters
8241 		 * on virtual tunnel interfaces.  Only allow the
8242 		 * base tunnel to do these.
8243 		 */
8244 		ipif_refrele(ipif);
8245 		return (EINVAL);
8246 	}
8247 
8248 	/*
8249 	 * Send down to tunnel mod for ioctl processing.
8250 	 * Will finish ioctl in ip_rput_other().
8251 	 */
8252 	ill = ipif->ipif_ill;
8253 	if (ill->ill_net_type == IRE_LOOPBACK) {
8254 		ipif_refrele(ipif);
8255 		return (EOPNOTSUPP);
8256 	}
8257 
8258 	if (ill->ill_wq == NULL) {
8259 		ipif_refrele(ipif);
8260 		return (ENXIO);
8261 	}
8262 	/*
8263 	 * Mark the ioctl as coming from an IPv6 interface for
8264 	 * tun's convenience.
8265 	 */
8266 	if (ill->ill_isv6)
8267 		ta->ifta_flags |= 0x80000000;
8268 	ci->ci_ipif = ipif;
8269 	return (0);
8270 }
8271 
8272 /*
8273  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8274  * and return the associated ipif.
8275  * Return value:
8276  *	Non zero: An error has occurred. ci may not be filled out.
8277  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8278  *	a held ipif in ci.ci_ipif.
8279  */
8280 int
8281 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8282     cmd_info_t *ci, ipsq_func_t func)
8283 {
8284 	sin_t		*sin;
8285 	sin6_t		*sin6;
8286 	char		*name;
8287 	struct ifreq    *ifr;
8288 	struct lifreq    *lifr;
8289 	ipif_t		*ipif = NULL;
8290 	ill_t		*ill;
8291 	conn_t		*connp;
8292 	boolean_t	isv6;
8293 	boolean_t	exists;
8294 	int		err;
8295 	mblk_t		*mp1;
8296 	zoneid_t	zoneid;
8297 	ip_stack_t	*ipst;
8298 
8299 	if (q->q_next != NULL) {
8300 		ill = (ill_t *)q->q_ptr;
8301 		isv6 = ill->ill_isv6;
8302 		connp = NULL;
8303 		zoneid = ALL_ZONES;
8304 		ipst = ill->ill_ipst;
8305 	} else {
8306 		ill = NULL;
8307 		connp = Q_TO_CONN(q);
8308 		isv6 = connp->conn_af_isv6;
8309 		zoneid = connp->conn_zoneid;
8310 		if (zoneid == GLOBAL_ZONEID) {
8311 			/* global zone can access ipifs in all zones */
8312 			zoneid = ALL_ZONES;
8313 		}
8314 		ipst = connp->conn_netstack->netstack_ip;
8315 	}
8316 
8317 	/* Has been checked in ip_wput_nondata */
8318 	mp1 = mp->b_cont->b_cont;
8319 
8320 	if (ipip->ipi_cmd_type == IF_CMD) {
8321 		/* This a old style SIOC[GS]IF* command */
8322 		ifr = (struct ifreq *)mp1->b_rptr;
8323 		/*
8324 		 * Null terminate the string to protect against buffer
8325 		 * overrun. String was generated by user code and may not
8326 		 * be trusted.
8327 		 */
8328 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8329 		sin = (sin_t *)&ifr->ifr_addr;
8330 		name = ifr->ifr_name;
8331 		ci->ci_sin = sin;
8332 		ci->ci_sin6 = NULL;
8333 		ci->ci_lifr = (struct lifreq *)ifr;
8334 	} else {
8335 		/* This a new style SIOC[GS]LIF* command */
8336 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8337 		lifr = (struct lifreq *)mp1->b_rptr;
8338 		/*
8339 		 * Null terminate the string to protect against buffer
8340 		 * overrun. String was generated by user code and may not
8341 		 * be trusted.
8342 		 */
8343 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8344 		name = lifr->lifr_name;
8345 		sin = (sin_t *)&lifr->lifr_addr;
8346 		sin6 = (sin6_t *)&lifr->lifr_addr;
8347 		if (ipip->ipi_cmd == SIOCSLIFGROUPNAME) {
8348 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8349 			    LIFNAMSIZ);
8350 		}
8351 		ci->ci_sin = sin;
8352 		ci->ci_sin6 = sin6;
8353 		ci->ci_lifr = lifr;
8354 	}
8355 
8356 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8357 		/*
8358 		 * The ioctl will be failed if the ioctl comes down
8359 		 * an conn stream
8360 		 */
8361 		if (ill == NULL) {
8362 			/*
8363 			 * Not an ill queue, return EINVAL same as the
8364 			 * old error code.
8365 			 */
8366 			return (ENXIO);
8367 		}
8368 		ipif = ill->ill_ipif;
8369 		ipif_refhold(ipif);
8370 	} else {
8371 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8372 		    &exists, isv6, zoneid,
8373 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8374 		    ipst);
8375 		if (ipif == NULL) {
8376 			if (err == EINPROGRESS)
8377 				return (err);
8378 			if (ipip->ipi_cmd == SIOCLIFFAILOVER ||
8379 			    ipip->ipi_cmd == SIOCLIFFAILBACK) {
8380 				/*
8381 				 * Need to try both v4 and v6 since this
8382 				 * ioctl can come down either v4 or v6
8383 				 * socket. The lifreq.lifr_family passed
8384 				 * down by this ioctl is AF_UNSPEC.
8385 				 */
8386 				ipif = ipif_lookup_on_name(name,
8387 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8388 				    zoneid, (connp == NULL) ? q :
8389 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8390 				if (err == EINPROGRESS)
8391 					return (err);
8392 			}
8393 			err = 0;	/* Ensure we don't use it below */
8394 		}
8395 	}
8396 
8397 	/*
8398 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8399 	 */
8400 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8401 		ipif_refrele(ipif);
8402 		return (ENXIO);
8403 	}
8404 
8405 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8406 	    name[0] == '\0') {
8407 		/*
8408 		 * Handle a or a SIOC?IF* with a null name
8409 		 * during plumb (on the ill queue before the I_PLINK).
8410 		 */
8411 		ipif = ill->ill_ipif;
8412 		ipif_refhold(ipif);
8413 	}
8414 
8415 	if (ipif == NULL)
8416 		return (ENXIO);
8417 
8418 	/*
8419 	 * Allow only GET operations if this ipif has been created
8420 	 * temporarily due to a MOVE operation.
8421 	 */
8422 	if (ipif->ipif_replace_zero && !(ipip->ipi_flags & IPI_REPL)) {
8423 		ipif_refrele(ipif);
8424 		return (EINVAL);
8425 	}
8426 
8427 	ci->ci_ipif = ipif;
8428 	return (0);
8429 }
8430 
8431 /*
8432  * Return the total number of ipifs.
8433  */
8434 static uint_t
8435 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8436 {
8437 	uint_t numifs = 0;
8438 	ill_t	*ill;
8439 	ill_walk_context_t	ctx;
8440 	ipif_t	*ipif;
8441 
8442 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8443 	ill = ILL_START_WALK_V4(&ctx, ipst);
8444 
8445 	while (ill != NULL) {
8446 		for (ipif = ill->ill_ipif; ipif != NULL;
8447 		    ipif = ipif->ipif_next) {
8448 			if (ipif->ipif_zoneid == zoneid ||
8449 			    ipif->ipif_zoneid == ALL_ZONES)
8450 				numifs++;
8451 		}
8452 		ill = ill_next(&ctx, ill);
8453 	}
8454 	rw_exit(&ipst->ips_ill_g_lock);
8455 	return (numifs);
8456 }
8457 
8458 /*
8459  * Return the total number of ipifs.
8460  */
8461 static uint_t
8462 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8463 {
8464 	uint_t numifs = 0;
8465 	ill_t	*ill;
8466 	ipif_t	*ipif;
8467 	ill_walk_context_t	ctx;
8468 
8469 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8470 
8471 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8472 	if (family == AF_INET)
8473 		ill = ILL_START_WALK_V4(&ctx, ipst);
8474 	else if (family == AF_INET6)
8475 		ill = ILL_START_WALK_V6(&ctx, ipst);
8476 	else
8477 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8478 
8479 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8480 		for (ipif = ill->ill_ipif; ipif != NULL;
8481 		    ipif = ipif->ipif_next) {
8482 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8483 			    !(lifn_flags & LIFC_NOXMIT))
8484 				continue;
8485 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8486 			    !(lifn_flags & LIFC_TEMPORARY))
8487 				continue;
8488 			if (((ipif->ipif_flags &
8489 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8490 			    IPIF_DEPRECATED)) ||
8491 			    IS_LOOPBACK(ill) ||
8492 			    !(ipif->ipif_flags & IPIF_UP)) &&
8493 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8494 				continue;
8495 
8496 			if (zoneid != ipif->ipif_zoneid &&
8497 			    ipif->ipif_zoneid != ALL_ZONES &&
8498 			    (zoneid != GLOBAL_ZONEID ||
8499 			    !(lifn_flags & LIFC_ALLZONES)))
8500 				continue;
8501 
8502 			numifs++;
8503 		}
8504 	}
8505 	rw_exit(&ipst->ips_ill_g_lock);
8506 	return (numifs);
8507 }
8508 
8509 uint_t
8510 ip_get_lifsrcofnum(ill_t *ill)
8511 {
8512 	uint_t numifs = 0;
8513 	ill_t	*ill_head = ill;
8514 	ip_stack_t	*ipst = ill->ill_ipst;
8515 
8516 	/*
8517 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8518 	 * other thread may be trying to relink the ILLs in this usesrc group
8519 	 * and adjusting the ill_usesrc_grp_next pointers
8520 	 */
8521 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8522 	if ((ill->ill_usesrc_ifindex == 0) &&
8523 	    (ill->ill_usesrc_grp_next != NULL)) {
8524 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8525 		    ill = ill->ill_usesrc_grp_next)
8526 			numifs++;
8527 	}
8528 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8529 
8530 	return (numifs);
8531 }
8532 
8533 /* Null values are passed in for ipif, sin, and ifreq */
8534 /* ARGSUSED */
8535 int
8536 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8537     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8538 {
8539 	int *nump;
8540 	conn_t *connp = Q_TO_CONN(q);
8541 
8542 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8543 
8544 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8545 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8546 
8547 	*nump = ip_get_numifs(connp->conn_zoneid,
8548 	    connp->conn_netstack->netstack_ip);
8549 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8550 	return (0);
8551 }
8552 
8553 /* Null values are passed in for ipif, sin, and ifreq */
8554 /* ARGSUSED */
8555 int
8556 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8557     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8558 {
8559 	struct lifnum *lifn;
8560 	mblk_t	*mp1;
8561 	conn_t *connp = Q_TO_CONN(q);
8562 
8563 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8564 
8565 	/* Existence checked in ip_wput_nondata */
8566 	mp1 = mp->b_cont->b_cont;
8567 
8568 	lifn = (struct lifnum *)mp1->b_rptr;
8569 	switch (lifn->lifn_family) {
8570 	case AF_UNSPEC:
8571 	case AF_INET:
8572 	case AF_INET6:
8573 		break;
8574 	default:
8575 		return (EAFNOSUPPORT);
8576 	}
8577 
8578 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8579 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8580 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8581 	return (0);
8582 }
8583 
8584 /* ARGSUSED */
8585 int
8586 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8587     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8588 {
8589 	STRUCT_HANDLE(ifconf, ifc);
8590 	mblk_t *mp1;
8591 	struct iocblk *iocp;
8592 	struct ifreq *ifr;
8593 	ill_walk_context_t	ctx;
8594 	ill_t	*ill;
8595 	ipif_t	*ipif;
8596 	struct sockaddr_in *sin;
8597 	int32_t	ifclen;
8598 	zoneid_t zoneid;
8599 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8600 
8601 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8602 
8603 	ip1dbg(("ip_sioctl_get_ifconf"));
8604 	/* Existence verified in ip_wput_nondata */
8605 	mp1 = mp->b_cont->b_cont;
8606 	iocp = (struct iocblk *)mp->b_rptr;
8607 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8608 
8609 	/*
8610 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8611 	 * the user buffer address and length into which the list of struct
8612 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8613 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8614 	 * the SIOCGIFCONF operation was redefined to simply provide
8615 	 * a large output buffer into which we are supposed to jam the ifreq
8616 	 * array.  The same ioctl command code was used, despite the fact that
8617 	 * both the applications and the kernel code had to change, thus making
8618 	 * it impossible to support both interfaces.
8619 	 *
8620 	 * For reasons not good enough to try to explain, the following
8621 	 * algorithm is used for deciding what to do with one of these:
8622 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8623 	 * form with the output buffer coming down as the continuation message.
8624 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8625 	 * and we have to copy in the ifconf structure to find out how big the
8626 	 * output buffer is and where to copy out to.  Sure no problem...
8627 	 *
8628 	 */
8629 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8630 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8631 		int numifs = 0;
8632 		size_t ifc_bufsize;
8633 
8634 		/*
8635 		 * Must be (better be!) continuation of a TRANSPARENT
8636 		 * IOCTL.  We just copied in the ifconf structure.
8637 		 */
8638 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8639 		    (struct ifconf *)mp1->b_rptr);
8640 
8641 		/*
8642 		 * Allocate a buffer to hold requested information.
8643 		 *
8644 		 * If ifc_len is larger than what is needed, we only
8645 		 * allocate what we will use.
8646 		 *
8647 		 * If ifc_len is smaller than what is needed, return
8648 		 * EINVAL.
8649 		 *
8650 		 * XXX: the ill_t structure can hava 2 counters, for
8651 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8652 		 * number of interfaces for a device, so we don't need
8653 		 * to count them here...
8654 		 */
8655 		numifs = ip_get_numifs(zoneid, ipst);
8656 
8657 		ifclen = STRUCT_FGET(ifc, ifc_len);
8658 		ifc_bufsize = numifs * sizeof (struct ifreq);
8659 		if (ifc_bufsize > ifclen) {
8660 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8661 				/* old behaviour */
8662 				return (EINVAL);
8663 			} else {
8664 				ifc_bufsize = ifclen;
8665 			}
8666 		}
8667 
8668 		mp1 = mi_copyout_alloc(q, mp,
8669 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8670 		if (mp1 == NULL)
8671 			return (ENOMEM);
8672 
8673 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8674 	}
8675 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8676 	/*
8677 	 * the SIOCGIFCONF ioctl only knows about
8678 	 * IPv4 addresses, so don't try to tell
8679 	 * it about interfaces with IPv6-only
8680 	 * addresses. (Last parm 'isv6' is B_FALSE)
8681 	 */
8682 
8683 	ifr = (struct ifreq *)mp1->b_rptr;
8684 
8685 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8686 	ill = ILL_START_WALK_V4(&ctx, ipst);
8687 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8688 		for (ipif = ill->ill_ipif; ipif != NULL;
8689 		    ipif = ipif->ipif_next) {
8690 			if (zoneid != ipif->ipif_zoneid &&
8691 			    ipif->ipif_zoneid != ALL_ZONES)
8692 				continue;
8693 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8694 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8695 					/* old behaviour */
8696 					rw_exit(&ipst->ips_ill_g_lock);
8697 					return (EINVAL);
8698 				} else {
8699 					goto if_copydone;
8700 				}
8701 			}
8702 			ipif_get_name(ipif, ifr->ifr_name,
8703 			    sizeof (ifr->ifr_name));
8704 			sin = (sin_t *)&ifr->ifr_addr;
8705 			*sin = sin_null;
8706 			sin->sin_family = AF_INET;
8707 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8708 			ifr++;
8709 		}
8710 	}
8711 if_copydone:
8712 	rw_exit(&ipst->ips_ill_g_lock);
8713 	mp1->b_wptr = (uchar_t *)ifr;
8714 
8715 	if (STRUCT_BUF(ifc) != NULL) {
8716 		STRUCT_FSET(ifc, ifc_len,
8717 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8718 	}
8719 	return (0);
8720 }
8721 
8722 /*
8723  * Get the interfaces using the address hosted on the interface passed in,
8724  * as a source adddress
8725  */
8726 /* ARGSUSED */
8727 int
8728 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8729     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8730 {
8731 	mblk_t *mp1;
8732 	ill_t	*ill, *ill_head;
8733 	ipif_t	*ipif, *orig_ipif;
8734 	int	numlifs = 0;
8735 	size_t	lifs_bufsize, lifsmaxlen;
8736 	struct	lifreq *lifr;
8737 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8738 	uint_t	ifindex;
8739 	zoneid_t zoneid;
8740 	int err = 0;
8741 	boolean_t isv6 = B_FALSE;
8742 	struct	sockaddr_in	*sin;
8743 	struct	sockaddr_in6	*sin6;
8744 	STRUCT_HANDLE(lifsrcof, lifs);
8745 	ip_stack_t		*ipst;
8746 
8747 	ipst = CONNQ_TO_IPST(q);
8748 
8749 	ASSERT(q->q_next == NULL);
8750 
8751 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8752 
8753 	/* Existence verified in ip_wput_nondata */
8754 	mp1 = mp->b_cont->b_cont;
8755 
8756 	/*
8757 	 * Must be (better be!) continuation of a TRANSPARENT
8758 	 * IOCTL.  We just copied in the lifsrcof structure.
8759 	 */
8760 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8761 	    (struct lifsrcof *)mp1->b_rptr);
8762 
8763 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8764 		return (EINVAL);
8765 
8766 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8767 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8768 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8769 	    ip_process_ioctl, &err, ipst);
8770 	if (ipif == NULL) {
8771 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8772 		    ifindex));
8773 		return (err);
8774 	}
8775 
8776 
8777 	/* Allocate a buffer to hold requested information */
8778 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8779 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8780 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8781 	/* The actual size needed is always returned in lifs_len */
8782 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8783 
8784 	/* If the amount we need is more than what is passed in, abort */
8785 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8786 		ipif_refrele(ipif);
8787 		return (0);
8788 	}
8789 
8790 	mp1 = mi_copyout_alloc(q, mp,
8791 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8792 	if (mp1 == NULL) {
8793 		ipif_refrele(ipif);
8794 		return (ENOMEM);
8795 	}
8796 
8797 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8798 	bzero(mp1->b_rptr, lifs_bufsize);
8799 
8800 	lifr = (struct lifreq *)mp1->b_rptr;
8801 
8802 	ill = ill_head = ipif->ipif_ill;
8803 	orig_ipif = ipif;
8804 
8805 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8806 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8807 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8808 
8809 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8810 	for (; (ill != NULL) && (ill != ill_head);
8811 	    ill = ill->ill_usesrc_grp_next) {
8812 
8813 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8814 			break;
8815 
8816 		ipif = ill->ill_ipif;
8817 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8818 		if (ipif->ipif_isv6) {
8819 			sin6 = (sin6_t *)&lifr->lifr_addr;
8820 			*sin6 = sin6_null;
8821 			sin6->sin6_family = AF_INET6;
8822 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8823 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8824 			    &ipif->ipif_v6net_mask);
8825 		} else {
8826 			sin = (sin_t *)&lifr->lifr_addr;
8827 			*sin = sin_null;
8828 			sin->sin_family = AF_INET;
8829 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8830 			lifr->lifr_addrlen = ip_mask_to_plen(
8831 			    ipif->ipif_net_mask);
8832 		}
8833 		lifr++;
8834 	}
8835 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8836 	rw_exit(&ipst->ips_ill_g_lock);
8837 	ipif_refrele(orig_ipif);
8838 	mp1->b_wptr = (uchar_t *)lifr;
8839 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8840 
8841 	return (0);
8842 }
8843 
8844 /* ARGSUSED */
8845 int
8846 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8847     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8848 {
8849 	mblk_t *mp1;
8850 	int	list;
8851 	ill_t	*ill;
8852 	ipif_t	*ipif;
8853 	int	flags;
8854 	int	numlifs = 0;
8855 	size_t	lifc_bufsize;
8856 	struct	lifreq *lifr;
8857 	sa_family_t	family;
8858 	struct	sockaddr_in	*sin;
8859 	struct	sockaddr_in6	*sin6;
8860 	ill_walk_context_t	ctx;
8861 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8862 	int32_t	lifclen;
8863 	zoneid_t zoneid;
8864 	STRUCT_HANDLE(lifconf, lifc);
8865 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8866 
8867 	ip1dbg(("ip_sioctl_get_lifconf"));
8868 
8869 	ASSERT(q->q_next == NULL);
8870 
8871 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8872 
8873 	/* Existence verified in ip_wput_nondata */
8874 	mp1 = mp->b_cont->b_cont;
8875 
8876 	/*
8877 	 * An extended version of SIOCGIFCONF that takes an
8878 	 * additional address family and flags field.
8879 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8880 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8881 	 * interfaces are omitted.
8882 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8883 	 * unless LIFC_TEMPORARY is specified.
8884 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8885 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8886 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8887 	 * has priority over LIFC_NOXMIT.
8888 	 */
8889 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8890 
8891 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8892 		return (EINVAL);
8893 
8894 	/*
8895 	 * Must be (better be!) continuation of a TRANSPARENT
8896 	 * IOCTL.  We just copied in the lifconf structure.
8897 	 */
8898 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8899 
8900 	family = STRUCT_FGET(lifc, lifc_family);
8901 	flags = STRUCT_FGET(lifc, lifc_flags);
8902 
8903 	switch (family) {
8904 	case AF_UNSPEC:
8905 		/*
8906 		 * walk all ILL's.
8907 		 */
8908 		list = MAX_G_HEADS;
8909 		break;
8910 	case AF_INET:
8911 		/*
8912 		 * walk only IPV4 ILL's.
8913 		 */
8914 		list = IP_V4_G_HEAD;
8915 		break;
8916 	case AF_INET6:
8917 		/*
8918 		 * walk only IPV6 ILL's.
8919 		 */
8920 		list = IP_V6_G_HEAD;
8921 		break;
8922 	default:
8923 		return (EAFNOSUPPORT);
8924 	}
8925 
8926 	/*
8927 	 * Allocate a buffer to hold requested information.
8928 	 *
8929 	 * If lifc_len is larger than what is needed, we only
8930 	 * allocate what we will use.
8931 	 *
8932 	 * If lifc_len is smaller than what is needed, return
8933 	 * EINVAL.
8934 	 */
8935 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8936 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8937 	lifclen = STRUCT_FGET(lifc, lifc_len);
8938 	if (lifc_bufsize > lifclen) {
8939 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8940 			return (EINVAL);
8941 		else
8942 			lifc_bufsize = lifclen;
8943 	}
8944 
8945 	mp1 = mi_copyout_alloc(q, mp,
8946 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8947 	if (mp1 == NULL)
8948 		return (ENOMEM);
8949 
8950 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8951 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8952 
8953 	lifr = (struct lifreq *)mp1->b_rptr;
8954 
8955 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8956 	ill = ill_first(list, list, &ctx, ipst);
8957 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8958 		for (ipif = ill->ill_ipif; ipif != NULL;
8959 		    ipif = ipif->ipif_next) {
8960 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8961 			    !(flags & LIFC_NOXMIT))
8962 				continue;
8963 
8964 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8965 			    !(flags & LIFC_TEMPORARY))
8966 				continue;
8967 
8968 			if (((ipif->ipif_flags &
8969 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8970 			    IPIF_DEPRECATED)) ||
8971 			    IS_LOOPBACK(ill) ||
8972 			    !(ipif->ipif_flags & IPIF_UP)) &&
8973 			    (flags & LIFC_EXTERNAL_SOURCE))
8974 				continue;
8975 
8976 			if (zoneid != ipif->ipif_zoneid &&
8977 			    ipif->ipif_zoneid != ALL_ZONES &&
8978 			    (zoneid != GLOBAL_ZONEID ||
8979 			    !(flags & LIFC_ALLZONES)))
8980 				continue;
8981 
8982 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8983 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8984 					rw_exit(&ipst->ips_ill_g_lock);
8985 					return (EINVAL);
8986 				} else {
8987 					goto lif_copydone;
8988 				}
8989 			}
8990 
8991 			ipif_get_name(ipif, lifr->lifr_name,
8992 			    sizeof (lifr->lifr_name));
8993 			if (ipif->ipif_isv6) {
8994 				sin6 = (sin6_t *)&lifr->lifr_addr;
8995 				*sin6 = sin6_null;
8996 				sin6->sin6_family = AF_INET6;
8997 				sin6->sin6_addr =
8998 				    ipif->ipif_v6lcl_addr;
8999 				lifr->lifr_addrlen =
9000 				    ip_mask_to_plen_v6(
9001 				    &ipif->ipif_v6net_mask);
9002 			} else {
9003 				sin = (sin_t *)&lifr->lifr_addr;
9004 				*sin = sin_null;
9005 				sin->sin_family = AF_INET;
9006 				sin->sin_addr.s_addr =
9007 				    ipif->ipif_lcl_addr;
9008 				lifr->lifr_addrlen =
9009 				    ip_mask_to_plen(
9010 				    ipif->ipif_net_mask);
9011 			}
9012 			lifr++;
9013 		}
9014 	}
9015 lif_copydone:
9016 	rw_exit(&ipst->ips_ill_g_lock);
9017 
9018 	mp1->b_wptr = (uchar_t *)lifr;
9019 	if (STRUCT_BUF(lifc) != NULL) {
9020 		STRUCT_FSET(lifc, lifc_len,
9021 		    (int)((uchar_t *)lifr - mp1->b_rptr));
9022 	}
9023 	return (0);
9024 }
9025 
9026 /* ARGSUSED */
9027 int
9028 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
9029     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9030 {
9031 	ip_stack_t	*ipst;
9032 
9033 	if (q->q_next == NULL)
9034 		ipst = CONNQ_TO_IPST(q);
9035 	else
9036 		ipst = ILLQ_TO_IPST(q);
9037 
9038 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9039 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9040 	return (0);
9041 }
9042 
9043 static void
9044 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9045 {
9046 	ip6_asp_t *table;
9047 	size_t table_size;
9048 	mblk_t *data_mp;
9049 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9050 	ip_stack_t	*ipst;
9051 
9052 	if (q->q_next == NULL)
9053 		ipst = CONNQ_TO_IPST(q);
9054 	else
9055 		ipst = ILLQ_TO_IPST(q);
9056 
9057 	/* These two ioctls are I_STR only */
9058 	if (iocp->ioc_count == TRANSPARENT) {
9059 		miocnak(q, mp, 0, EINVAL);
9060 		return;
9061 	}
9062 
9063 	data_mp = mp->b_cont;
9064 	if (data_mp == NULL) {
9065 		/* The user passed us a NULL argument */
9066 		table = NULL;
9067 		table_size = iocp->ioc_count;
9068 	} else {
9069 		/*
9070 		 * The user provided a table.  The stream head
9071 		 * may have copied in the user data in chunks,
9072 		 * so make sure everything is pulled up
9073 		 * properly.
9074 		 */
9075 		if (MBLKL(data_mp) < iocp->ioc_count) {
9076 			mblk_t *new_data_mp;
9077 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9078 			    NULL) {
9079 				miocnak(q, mp, 0, ENOMEM);
9080 				return;
9081 			}
9082 			freemsg(data_mp);
9083 			data_mp = new_data_mp;
9084 			mp->b_cont = data_mp;
9085 		}
9086 		table = (ip6_asp_t *)data_mp->b_rptr;
9087 		table_size = iocp->ioc_count;
9088 	}
9089 
9090 	switch (iocp->ioc_cmd) {
9091 	case SIOCGIP6ADDRPOLICY:
9092 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9093 		if (iocp->ioc_rval == -1)
9094 			iocp->ioc_error = EINVAL;
9095 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9096 		else if (table != NULL &&
9097 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9098 			ip6_asp_t *src = table;
9099 			ip6_asp32_t *dst = (void *)table;
9100 			int count = table_size / sizeof (ip6_asp_t);
9101 			int i;
9102 
9103 			/*
9104 			 * We need to do an in-place shrink of the array
9105 			 * to match the alignment attributes of the
9106 			 * 32-bit ABI looking at it.
9107 			 */
9108 			/* LINTED: logical expression always true: op "||" */
9109 			ASSERT(sizeof (*src) > sizeof (*dst));
9110 			for (i = 1; i < count; i++)
9111 				bcopy(src + i, dst + i, sizeof (*dst));
9112 		}
9113 #endif
9114 		break;
9115 
9116 	case SIOCSIP6ADDRPOLICY:
9117 		ASSERT(mp->b_prev == NULL);
9118 		mp->b_prev = (void *)q;
9119 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9120 		/*
9121 		 * We pass in the datamodel here so that the ip6_asp_replace()
9122 		 * routine can handle converting from 32-bit to native formats
9123 		 * where necessary.
9124 		 *
9125 		 * A better way to handle this might be to convert the inbound
9126 		 * data structure here, and hang it off a new 'mp'; thus the
9127 		 * ip6_asp_replace() logic would always be dealing with native
9128 		 * format data structures..
9129 		 *
9130 		 * (An even simpler way to handle these ioctls is to just
9131 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9132 		 * and just recompile everything that depends on it.)
9133 		 */
9134 #endif
9135 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9136 		    iocp->ioc_flag & IOC_MODELS);
9137 		return;
9138 	}
9139 
9140 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9141 	qreply(q, mp);
9142 }
9143 
9144 static void
9145 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9146 {
9147 	mblk_t 		*data_mp;
9148 	struct dstinforeq	*dir;
9149 	uint8_t		*end, *cur;
9150 	in6_addr_t	*daddr, *saddr;
9151 	ipaddr_t	v4daddr;
9152 	ire_t		*ire;
9153 	char		*slabel, *dlabel;
9154 	boolean_t	isipv4;
9155 	int		match_ire;
9156 	ill_t		*dst_ill;
9157 	ipif_t		*src_ipif, *ire_ipif;
9158 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9159 	zoneid_t	zoneid;
9160 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9161 
9162 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9163 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9164 
9165 	/*
9166 	 * This ioctl is I_STR only, and must have a
9167 	 * data mblk following the M_IOCTL mblk.
9168 	 */
9169 	data_mp = mp->b_cont;
9170 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9171 		miocnak(q, mp, 0, EINVAL);
9172 		return;
9173 	}
9174 
9175 	if (MBLKL(data_mp) < iocp->ioc_count) {
9176 		mblk_t *new_data_mp;
9177 
9178 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9179 			miocnak(q, mp, 0, ENOMEM);
9180 			return;
9181 		}
9182 		freemsg(data_mp);
9183 		data_mp = new_data_mp;
9184 		mp->b_cont = data_mp;
9185 	}
9186 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9187 
9188 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9189 	    end - cur >= sizeof (struct dstinforeq);
9190 	    cur += sizeof (struct dstinforeq)) {
9191 		dir = (struct dstinforeq *)cur;
9192 		daddr = &dir->dir_daddr;
9193 		saddr = &dir->dir_saddr;
9194 
9195 		/*
9196 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9197 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9198 		 * and ipif_select_source[_v6]() do not.
9199 		 */
9200 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9201 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9202 
9203 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9204 		if (isipv4) {
9205 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9206 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9207 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9208 		} else {
9209 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9210 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9211 		}
9212 		if (ire == NULL) {
9213 			dir->dir_dreachable = 0;
9214 
9215 			/* move on to next dst addr */
9216 			continue;
9217 		}
9218 		dir->dir_dreachable = 1;
9219 
9220 		ire_ipif = ire->ire_ipif;
9221 		if (ire_ipif == NULL)
9222 			goto next_dst;
9223 
9224 		/*
9225 		 * We expect to get back an interface ire or a
9226 		 * gateway ire cache entry.  For both types, the
9227 		 * output interface is ire_ipif->ipif_ill.
9228 		 */
9229 		dst_ill = ire_ipif->ipif_ill;
9230 		dir->dir_dmactype = dst_ill->ill_mactype;
9231 
9232 		if (isipv4) {
9233 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9234 		} else {
9235 			src_ipif = ipif_select_source_v6(dst_ill,
9236 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9237 			    zoneid);
9238 		}
9239 		if (src_ipif == NULL)
9240 			goto next_dst;
9241 
9242 		*saddr = src_ipif->ipif_v6lcl_addr;
9243 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9244 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9245 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9246 		dir->dir_sdeprecated =
9247 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9248 		ipif_refrele(src_ipif);
9249 next_dst:
9250 		ire_refrele(ire);
9251 	}
9252 	miocack(q, mp, iocp->ioc_count, 0);
9253 }
9254 
9255 
9256 /*
9257  * Check if this is an address assigned to this machine.
9258  * Skips interfaces that are down by using ire checks.
9259  * Translates mapped addresses to v4 addresses and then
9260  * treats them as such, returning true if the v4 address
9261  * associated with this mapped address is configured.
9262  * Note: Applications will have to be careful what they do
9263  * with the response; use of mapped addresses limits
9264  * what can be done with the socket, especially with
9265  * respect to socket options and ioctls - neither IPv4
9266  * options nor IPv6 sticky options/ancillary data options
9267  * may be used.
9268  */
9269 /* ARGSUSED */
9270 int
9271 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9272     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9273 {
9274 	struct sioc_addrreq *sia;
9275 	sin_t *sin;
9276 	ire_t *ire;
9277 	mblk_t *mp1;
9278 	zoneid_t zoneid;
9279 	ip_stack_t	*ipst;
9280 
9281 	ip1dbg(("ip_sioctl_tmyaddr"));
9282 
9283 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9284 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9285 	ipst = CONNQ_TO_IPST(q);
9286 
9287 	/* Existence verified in ip_wput_nondata */
9288 	mp1 = mp->b_cont->b_cont;
9289 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9290 	sin = (sin_t *)&sia->sa_addr;
9291 	switch (sin->sin_family) {
9292 	case AF_INET6: {
9293 		sin6_t *sin6 = (sin6_t *)sin;
9294 
9295 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9296 			ipaddr_t v4_addr;
9297 
9298 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9299 			    v4_addr);
9300 			ire = ire_ctable_lookup(v4_addr, 0,
9301 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9302 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9303 		} else {
9304 			in6_addr_t v6addr;
9305 
9306 			v6addr = sin6->sin6_addr;
9307 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9308 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9309 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9310 		}
9311 		break;
9312 	}
9313 	case AF_INET: {
9314 		ipaddr_t v4addr;
9315 
9316 		v4addr = sin->sin_addr.s_addr;
9317 		ire = ire_ctable_lookup(v4addr, 0,
9318 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9319 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9320 		break;
9321 	}
9322 	default:
9323 		return (EAFNOSUPPORT);
9324 	}
9325 	if (ire != NULL) {
9326 		sia->sa_res = 1;
9327 		ire_refrele(ire);
9328 	} else {
9329 		sia->sa_res = 0;
9330 	}
9331 	return (0);
9332 }
9333 
9334 /*
9335  * Check if this is an address assigned on-link i.e. neighbor,
9336  * and makes sure it's reachable from the current zone.
9337  * Returns true for my addresses as well.
9338  * Translates mapped addresses to v4 addresses and then
9339  * treats them as such, returning true if the v4 address
9340  * associated with this mapped address is configured.
9341  * Note: Applications will have to be careful what they do
9342  * with the response; use of mapped addresses limits
9343  * what can be done with the socket, especially with
9344  * respect to socket options and ioctls - neither IPv4
9345  * options nor IPv6 sticky options/ancillary data options
9346  * may be used.
9347  */
9348 /* ARGSUSED */
9349 int
9350 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9351     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9352 {
9353 	struct sioc_addrreq *sia;
9354 	sin_t *sin;
9355 	mblk_t	*mp1;
9356 	ire_t *ire = NULL;
9357 	zoneid_t zoneid;
9358 	ip_stack_t	*ipst;
9359 
9360 	ip1dbg(("ip_sioctl_tonlink"));
9361 
9362 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9363 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9364 	ipst = CONNQ_TO_IPST(q);
9365 
9366 	/* Existence verified in ip_wput_nondata */
9367 	mp1 = mp->b_cont->b_cont;
9368 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9369 	sin = (sin_t *)&sia->sa_addr;
9370 
9371 	/*
9372 	 * Match addresses with a zero gateway field to avoid
9373 	 * routes going through a router.
9374 	 * Exclude broadcast and multicast addresses.
9375 	 */
9376 	switch (sin->sin_family) {
9377 	case AF_INET6: {
9378 		sin6_t *sin6 = (sin6_t *)sin;
9379 
9380 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9381 			ipaddr_t v4_addr;
9382 
9383 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9384 			    v4_addr);
9385 			if (!CLASSD(v4_addr)) {
9386 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9387 				    NULL, NULL, zoneid, NULL,
9388 				    MATCH_IRE_GW, ipst);
9389 			}
9390 		} else {
9391 			in6_addr_t v6addr;
9392 			in6_addr_t v6gw;
9393 
9394 			v6addr = sin6->sin6_addr;
9395 			v6gw = ipv6_all_zeros;
9396 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9397 				ire = ire_route_lookup_v6(&v6addr, 0,
9398 				    &v6gw, 0, NULL, NULL, zoneid,
9399 				    NULL, MATCH_IRE_GW, ipst);
9400 			}
9401 		}
9402 		break;
9403 	}
9404 	case AF_INET: {
9405 		ipaddr_t v4addr;
9406 
9407 		v4addr = sin->sin_addr.s_addr;
9408 		if (!CLASSD(v4addr)) {
9409 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9410 			    NULL, NULL, zoneid, NULL,
9411 			    MATCH_IRE_GW, ipst);
9412 		}
9413 		break;
9414 	}
9415 	default:
9416 		return (EAFNOSUPPORT);
9417 	}
9418 	sia->sa_res = 0;
9419 	if (ire != NULL) {
9420 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9421 		    IRE_LOCAL|IRE_LOOPBACK)) {
9422 			sia->sa_res = 1;
9423 		}
9424 		ire_refrele(ire);
9425 	}
9426 	return (0);
9427 }
9428 
9429 /*
9430  * TBD: implement when kernel maintaines a list of site prefixes.
9431  */
9432 /* ARGSUSED */
9433 int
9434 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9435     ip_ioctl_cmd_t *ipip, void *ifreq)
9436 {
9437 	return (ENXIO);
9438 }
9439 
9440 /* ARGSUSED */
9441 int
9442 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9443     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9444 {
9445 	ill_t  		*ill;
9446 	mblk_t		*mp1;
9447 	conn_t		*connp;
9448 	boolean_t	success;
9449 
9450 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9451 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9452 	/* ioctl comes down on an conn */
9453 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9454 	connp = Q_TO_CONN(q);
9455 
9456 	mp->b_datap->db_type = M_IOCTL;
9457 
9458 	/*
9459 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9460 	 * The original mp contains contaminated b_next values due to 'mi',
9461 	 * which is needed to do the mi_copy_done. Unfortunately if we
9462 	 * send down the original mblk itself and if we are popped due to an
9463 	 * an unplumb before the response comes back from tunnel,
9464 	 * the streamhead (which does a freemsg) will see this contaminated
9465 	 * message and the assertion in freemsg about non-null b_next/b_prev
9466 	 * will panic a DEBUG kernel.
9467 	 */
9468 	mp1 = copymsg(mp);
9469 	if (mp1 == NULL)
9470 		return (ENOMEM);
9471 
9472 	ill = ipif->ipif_ill;
9473 	mutex_enter(&connp->conn_lock);
9474 	mutex_enter(&ill->ill_lock);
9475 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9476 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9477 		    mp, 0);
9478 	} else {
9479 		success = ill_pending_mp_add(ill, connp, mp);
9480 	}
9481 	mutex_exit(&ill->ill_lock);
9482 	mutex_exit(&connp->conn_lock);
9483 
9484 	if (success) {
9485 		ip1dbg(("sending down tunparam request "));
9486 		putnext(ill->ill_wq, mp1);
9487 		return (EINPROGRESS);
9488 	} else {
9489 		/* The conn has started closing */
9490 		freemsg(mp1);
9491 		return (EINTR);
9492 	}
9493 }
9494 
9495 /*
9496  * ARP IOCTLs.
9497  * How does IP get in the business of fronting ARP configuration/queries?
9498  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9499  * are by tradition passed in through a datagram socket.  That lands in IP.
9500  * As it happens, this is just as well since the interface is quite crude in
9501  * that it passes in no information about protocol or hardware types, or
9502  * interface association.  After making the protocol assumption, IP is in
9503  * the position to look up the name of the ILL, which ARP will need, and
9504  * format a request that can be handled by ARP.  The request is passed up
9505  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9506  * back a response.  ARP supports its own set of more general IOCTLs, in
9507  * case anyone is interested.
9508  */
9509 /* ARGSUSED */
9510 int
9511 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9512     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9513 {
9514 	mblk_t *mp1;
9515 	mblk_t *mp2;
9516 	mblk_t *pending_mp;
9517 	ipaddr_t ipaddr;
9518 	area_t *area;
9519 	struct iocblk *iocp;
9520 	conn_t *connp;
9521 	struct arpreq *ar;
9522 	struct xarpreq *xar;
9523 	int flags, alength;
9524 	char *lladdr;
9525 	ip_stack_t	*ipst;
9526 	ill_t *ill = ipif->ipif_ill;
9527 	boolean_t if_arp_ioctl = B_FALSE;
9528 
9529 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9530 	connp = Q_TO_CONN(q);
9531 	ipst = connp->conn_netstack->netstack_ip;
9532 
9533 	if (ipip->ipi_cmd_type == XARP_CMD) {
9534 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9535 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9536 		ar = NULL;
9537 
9538 		flags = xar->xarp_flags;
9539 		lladdr = LLADDR(&xar->xarp_ha);
9540 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9541 		/*
9542 		 * Validate against user's link layer address length
9543 		 * input and name and addr length limits.
9544 		 */
9545 		alength = ill->ill_phys_addr_length;
9546 		if (ipip->ipi_cmd == SIOCSXARP) {
9547 			if (alength != xar->xarp_ha.sdl_alen ||
9548 			    (alength + xar->xarp_ha.sdl_nlen >
9549 			    sizeof (xar->xarp_ha.sdl_data)))
9550 				return (EINVAL);
9551 		}
9552 	} else {
9553 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9554 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9555 		xar = NULL;
9556 
9557 		flags = ar->arp_flags;
9558 		lladdr = ar->arp_ha.sa_data;
9559 		/*
9560 		 * Theoretically, the sa_family could tell us what link
9561 		 * layer type this operation is trying to deal with. By
9562 		 * common usage AF_UNSPEC means ethernet. We'll assume
9563 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9564 		 * for now. Our new SIOC*XARP ioctls can be used more
9565 		 * generally.
9566 		 *
9567 		 * If the underlying media happens to have a non 6 byte
9568 		 * address, arp module will fail set/get, but the del
9569 		 * operation will succeed.
9570 		 */
9571 		alength = 6;
9572 		if ((ipip->ipi_cmd != SIOCDARP) &&
9573 		    (alength != ill->ill_phys_addr_length)) {
9574 			return (EINVAL);
9575 		}
9576 	}
9577 
9578 	/*
9579 	 * We are going to pass up to ARP a packet chain that looks
9580 	 * like:
9581 	 *
9582 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9583 	 *
9584 	 * Get a copy of the original IOCTL mblk to head the chain,
9585 	 * to be sent up (in mp1). Also get another copy to store
9586 	 * in the ill_pending_mp list, for matching the response
9587 	 * when it comes back from ARP.
9588 	 */
9589 	mp1 = copyb(mp);
9590 	pending_mp = copymsg(mp);
9591 	if (mp1 == NULL || pending_mp == NULL) {
9592 		if (mp1 != NULL)
9593 			freeb(mp1);
9594 		if (pending_mp != NULL)
9595 			inet_freemsg(pending_mp);
9596 		return (ENOMEM);
9597 	}
9598 
9599 	ipaddr = sin->sin_addr.s_addr;
9600 
9601 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9602 	    (caddr_t)&ipaddr);
9603 	if (mp2 == NULL) {
9604 		freeb(mp1);
9605 		inet_freemsg(pending_mp);
9606 		return (ENOMEM);
9607 	}
9608 	/* Put together the chain. */
9609 	mp1->b_cont = mp2;
9610 	mp1->b_datap->db_type = M_IOCTL;
9611 	mp2->b_cont = mp;
9612 	mp2->b_datap->db_type = M_DATA;
9613 
9614 	iocp = (struct iocblk *)mp1->b_rptr;
9615 
9616 	/*
9617 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9618 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9619 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9620 	 * ioc_count field; set ioc_count to be correct.
9621 	 */
9622 	iocp->ioc_count = MBLKL(mp1->b_cont);
9623 
9624 	/*
9625 	 * Set the proper command in the ARP message.
9626 	 * Convert the SIOC{G|S|D}ARP calls into our
9627 	 * AR_ENTRY_xxx calls.
9628 	 */
9629 	area = (area_t *)mp2->b_rptr;
9630 	switch (iocp->ioc_cmd) {
9631 	case SIOCDARP:
9632 	case SIOCDXARP:
9633 		/*
9634 		 * We defer deleting the corresponding IRE until
9635 		 * we return from arp.
9636 		 */
9637 		area->area_cmd = AR_ENTRY_DELETE;
9638 		area->area_proto_mask_offset = 0;
9639 		break;
9640 	case SIOCGARP:
9641 	case SIOCGXARP:
9642 		area->area_cmd = AR_ENTRY_SQUERY;
9643 		area->area_proto_mask_offset = 0;
9644 		break;
9645 	case SIOCSARP:
9646 	case SIOCSXARP:
9647 		/*
9648 		 * Delete the corresponding ire to make sure IP will
9649 		 * pick up any change from arp.
9650 		 */
9651 		if (!if_arp_ioctl) {
9652 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9653 		} else {
9654 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9655 			if (ipif != NULL) {
9656 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9657 				    ipst);
9658 				ipif_refrele(ipif);
9659 			}
9660 		}
9661 		break;
9662 	}
9663 	iocp->ioc_cmd = area->area_cmd;
9664 
9665 	/*
9666 	 * Fill in the rest of the ARP operation fields.
9667 	 */
9668 	area->area_hw_addr_length = alength;
9669 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9670 
9671 	/* Translate the flags. */
9672 	if (flags & ATF_PERM)
9673 		area->area_flags |= ACE_F_PERMANENT;
9674 	if (flags & ATF_PUBL)
9675 		area->area_flags |= ACE_F_PUBLISH;
9676 	if (flags & ATF_AUTHORITY)
9677 		area->area_flags |= ACE_F_AUTHORITY;
9678 
9679 	/*
9680 	 * Before sending 'mp' to ARP, we have to clear the b_next
9681 	 * and b_prev. Otherwise if STREAMS encounters such a message
9682 	 * in freemsg(), (because ARP can close any time) it can cause
9683 	 * a panic. But mi code needs the b_next and b_prev values of
9684 	 * mp->b_cont, to complete the ioctl. So we store it here
9685 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9686 	 * when the response comes down from ARP.
9687 	 */
9688 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9689 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9690 	mp->b_cont->b_next = NULL;
9691 	mp->b_cont->b_prev = NULL;
9692 
9693 	mutex_enter(&connp->conn_lock);
9694 	mutex_enter(&ill->ill_lock);
9695 	/* conn has not yet started closing, hence this can't fail */
9696 	VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9697 	mutex_exit(&ill->ill_lock);
9698 	mutex_exit(&connp->conn_lock);
9699 
9700 	/*
9701 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9702 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9703 	 */
9704 	putnext(ill->ill_rq, mp1);
9705 	return (EINPROGRESS);
9706 }
9707 
9708 /*
9709  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9710  * the associated sin and refhold and return the associated ipif via `ci'.
9711  */
9712 int
9713 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9714     cmd_info_t *ci, ipsq_func_t func)
9715 {
9716 	mblk_t	*mp1;
9717 	int	err;
9718 	sin_t	*sin;
9719 	conn_t	*connp;
9720 	ipif_t	*ipif;
9721 	ire_t	*ire = NULL;
9722 	ill_t	*ill = NULL;
9723 	boolean_t exists;
9724 	ip_stack_t *ipst;
9725 	struct arpreq *ar;
9726 	struct xarpreq *xar;
9727 	struct sockaddr_dl *sdl;
9728 
9729 	/* ioctl comes down on a conn */
9730 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9731 	connp = Q_TO_CONN(q);
9732 	if (connp->conn_af_isv6)
9733 		return (ENXIO);
9734 
9735 	ipst = connp->conn_netstack->netstack_ip;
9736 
9737 	/* Verified in ip_wput_nondata */
9738 	mp1 = mp->b_cont->b_cont;
9739 
9740 	if (ipip->ipi_cmd_type == XARP_CMD) {
9741 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9742 		xar = (struct xarpreq *)mp1->b_rptr;
9743 		sin = (sin_t *)&xar->xarp_pa;
9744 		sdl = &xar->xarp_ha;
9745 
9746 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9747 			return (ENXIO);
9748 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9749 			return (EINVAL);
9750 	} else {
9751 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9752 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9753 		ar = (struct arpreq *)mp1->b_rptr;
9754 		sin = (sin_t *)&ar->arp_pa;
9755 	}
9756 
9757 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9758 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9759 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9760 		    mp, func, &err, ipst);
9761 		if (ipif == NULL)
9762 			return (err);
9763 		if (ipif->ipif_id != 0 ||
9764 		    ipif->ipif_net_type != IRE_IF_RESOLVER) {
9765 			ipif_refrele(ipif);
9766 			return (ENXIO);
9767 		}
9768 	} else {
9769 		/*
9770 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with sdl_nlen ==
9771 		 * 0: use the IP address to figure out the ill.	 In the IPMP
9772 		 * case, a simple forwarding table lookup will return the
9773 		 * IRE_IF_RESOLVER for the first interface in the group, which
9774 		 * might not be the interface on which the requested IP
9775 		 * address was resolved due to the ill selection algorithm
9776 		 * (see ip_newroute_get_dst_ill()).  So we do a cache table
9777 		 * lookup first: if the IRE cache entry for the IP address is
9778 		 * still there, it will contain the ill pointer for the right
9779 		 * interface, so we use that. If the cache entry has been
9780 		 * flushed, we fall back to the forwarding table lookup. This
9781 		 * should be rare enough since IRE cache entries have a longer
9782 		 * life expectancy than ARP cache entries.
9783 		 */
9784 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9785 		    ipst);
9786 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9787 		    ((ill = ire_to_ill(ire)) == NULL) ||
9788 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9789 			if (ire != NULL)
9790 				ire_refrele(ire);
9791 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9792 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9793 			    NULL, MATCH_IRE_TYPE, ipst);
9794 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9795 
9796 				if (ire != NULL)
9797 					ire_refrele(ire);
9798 				return (ENXIO);
9799 			}
9800 		}
9801 		ASSERT(ire != NULL && ill != NULL);
9802 		ipif = ill->ill_ipif;
9803 		ipif_refhold(ipif);
9804 		ire_refrele(ire);
9805 	}
9806 	ci->ci_sin = sin;
9807 	ci->ci_ipif = ipif;
9808 	return (0);
9809 }
9810 
9811 /*
9812  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9813  * atomically set/clear the muxids. Also complete the ioctl by acking or
9814  * naking it.  Note that the code is structured such that the link type,
9815  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9816  * its clones use the persistent link, while pppd(1M) and perhaps many
9817  * other daemons may use non-persistent link.  When combined with some
9818  * ill_t states, linking and unlinking lower streams may be used as
9819  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9820  */
9821 /* ARGSUSED */
9822 void
9823 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9824 {
9825 	mblk_t		*mp1, *mp2;
9826 	struct linkblk	*li;
9827 	struct ipmx_s	*ipmxp;
9828 	ill_t		*ill;
9829 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9830 	int		err = 0;
9831 	boolean_t	entered_ipsq = B_FALSE;
9832 	boolean_t	islink;
9833 	ip_stack_t	*ipst;
9834 
9835 	if (CONN_Q(q))
9836 		ipst = CONNQ_TO_IPST(q);
9837 	else
9838 		ipst = ILLQ_TO_IPST(q);
9839 
9840 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9841 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9842 
9843 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9844 
9845 	mp1 = mp->b_cont;	/* This is the linkblk info */
9846 	li = (struct linkblk *)mp1->b_rptr;
9847 
9848 	/*
9849 	 * ARP has added this special mblk, and the utility is asking us
9850 	 * to perform consistency checks, and also atomically set the
9851 	 * muxid. Ifconfig is an example.  It achieves this by using
9852 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9853 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9854 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9855 	 * and other comments in this routine for more details.
9856 	 */
9857 	mp2 = mp1->b_cont;	/* This is added by ARP */
9858 
9859 	/*
9860 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9861 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9862 	 * get the special mblk above.  For backward compatibility, we
9863 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9864 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9865 	 * not atomic, and can leave the streams unplumbable if the utility
9866 	 * is interrupted before it does the SIOCSLIFMUXID.
9867 	 */
9868 	if (mp2 == NULL) {
9869 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9870 		if (err == EINPROGRESS)
9871 			return;
9872 		goto done;
9873 	}
9874 
9875 	/*
9876 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9877 	 * ARP has appended this last mblk to tell us whether the lower stream
9878 	 * is an arp-dev stream or an IP module stream.
9879 	 */
9880 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9881 	if (ipmxp->ipmx_arpdev_stream) {
9882 		/*
9883 		 * The lower stream is the arp-dev stream.
9884 		 */
9885 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9886 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9887 		if (ill == NULL) {
9888 			if (err == EINPROGRESS)
9889 				return;
9890 			err = EINVAL;
9891 			goto done;
9892 		}
9893 
9894 		if (ipsq == NULL) {
9895 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9896 			    NEW_OP, B_TRUE);
9897 			if (ipsq == NULL) {
9898 				ill_refrele(ill);
9899 				return;
9900 			}
9901 			entered_ipsq = B_TRUE;
9902 		}
9903 		ASSERT(IAM_WRITER_ILL(ill));
9904 		ill_refrele(ill);
9905 
9906 		/*
9907 		 * To ensure consistency between IP and ARP, the following
9908 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9909 		 * This is because the muxid's are stored in the IP stream on
9910 		 * the ill.
9911 		 *
9912 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9913 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9914 		 * not yet plinked, and it also checks that the corresponding
9915 		 * IP stream is already plinked.
9916 		 *
9917 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9918 		 * punlinking the IP stream. IP does not allow punlink of the
9919 		 * IP stream unless the arp stream has been punlinked.
9920 		 */
9921 		if ((islink &&
9922 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9923 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9924 			err = EINVAL;
9925 			goto done;
9926 		}
9927 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9928 	} else {
9929 		/*
9930 		 * The lower stream is probably an IP module stream.  Do
9931 		 * consistency checking.
9932 		 */
9933 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9934 		if (err == EINPROGRESS)
9935 			return;
9936 	}
9937 done:
9938 	if (err == 0)
9939 		miocack(q, mp, 0, 0);
9940 	else
9941 		miocnak(q, mp, 0, err);
9942 
9943 	/* Conn was refheld in ip_sioctl_copyin_setup */
9944 	if (CONN_Q(q))
9945 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9946 	if (entered_ipsq)
9947 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
9948 }
9949 
9950 /*
9951  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9952  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9953  * module stream).  If `doconsist' is set, then do the extended consistency
9954  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9955  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9956  * an error code on failure.
9957  */
9958 static int
9959 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9960     struct linkblk *li, boolean_t doconsist)
9961 {
9962 	ill_t  		*ill;
9963 	queue_t		*ipwq, *dwq;
9964 	const char	*name;
9965 	struct qinit	*qinfo;
9966 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9967 	boolean_t	entered_ipsq = B_FALSE;
9968 
9969 	/*
9970 	 * Walk the lower stream to verify it's the IP module stream.
9971 	 * The IP module is identified by its name, wput function,
9972 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9973 	 * (li->l_qbot) will not vanish until this ioctl completes.
9974 	 */
9975 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
9976 		qinfo = ipwq->q_qinfo;
9977 		name = qinfo->qi_minfo->mi_idname;
9978 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
9979 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
9980 			break;
9981 		}
9982 	}
9983 
9984 	/*
9985 	 * If this isn't an IP module stream, bail.
9986 	 */
9987 	if (ipwq == NULL)
9988 		return (0);
9989 
9990 	ill = ipwq->q_ptr;
9991 	ASSERT(ill != NULL);
9992 
9993 	if (ipsq == NULL) {
9994 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9995 		    NEW_OP, B_TRUE);
9996 		if (ipsq == NULL)
9997 			return (EINPROGRESS);
9998 		entered_ipsq = B_TRUE;
9999 	}
10000 	ASSERT(IAM_WRITER_ILL(ill));
10001 
10002 	if (doconsist) {
10003 		/*
10004 		 * Consistency checking requires that I_{P}LINK occurs
10005 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
10006 		 * occurs prior to clearing ill_arp_muxid.
10007 		 */
10008 		if ((islink && ill->ill_ip_muxid != 0) ||
10009 		    (!islink && ill->ill_arp_muxid != 0)) {
10010 			if (entered_ipsq)
10011 				ipsq_exit(ipsq, B_TRUE, B_TRUE);
10012 			return (EINVAL);
10013 		}
10014 	}
10015 
10016 	/*
10017 	 * As part of I_{P}LINKing, stash the number of downstream modules and
10018 	 * the read queue of the module immediately below IP in the ill.
10019 	 * These are used during the capability negotiation below.
10020 	 */
10021 	ill->ill_lmod_rq = NULL;
10022 	ill->ill_lmod_cnt = 0;
10023 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
10024 		ill->ill_lmod_rq = RD(dwq);
10025 		for (; dwq != NULL; dwq = dwq->q_next)
10026 			ill->ill_lmod_cnt++;
10027 	}
10028 
10029 	if (doconsist)
10030 		ill->ill_ip_muxid = islink ? li->l_index : 0;
10031 
10032 	/*
10033 	 * If there's at least one up ipif on this ill, then we're bound to
10034 	 * the underlying driver via DLPI.  In that case, renegotiate
10035 	 * capabilities to account for any possible change in modules
10036 	 * interposed between IP and the driver.
10037 	 */
10038 	if (ill->ill_ipif_up_count > 0) {
10039 		if (islink)
10040 			ill_capability_probe(ill);
10041 		else
10042 			ill_capability_reset(ill);
10043 	}
10044 
10045 	if (entered_ipsq)
10046 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
10047 
10048 	return (0);
10049 }
10050 
10051 /*
10052  * Search the ioctl command in the ioctl tables and return a pointer
10053  * to the ioctl command information. The ioctl command tables are
10054  * static and fully populated at compile time.
10055  */
10056 ip_ioctl_cmd_t *
10057 ip_sioctl_lookup(int ioc_cmd)
10058 {
10059 	int index;
10060 	ip_ioctl_cmd_t *ipip;
10061 	ip_ioctl_cmd_t *ipip_end;
10062 
10063 	if (ioc_cmd == IPI_DONTCARE)
10064 		return (NULL);
10065 
10066 	/*
10067 	 * Do a 2 step search. First search the indexed table
10068 	 * based on the least significant byte of the ioctl cmd.
10069 	 * If we don't find a match, then search the misc table
10070 	 * serially.
10071 	 */
10072 	index = ioc_cmd & 0xFF;
10073 	if (index < ip_ndx_ioctl_count) {
10074 		ipip = &ip_ndx_ioctl_table[index];
10075 		if (ipip->ipi_cmd == ioc_cmd) {
10076 			/* Found a match in the ndx table */
10077 			return (ipip);
10078 		}
10079 	}
10080 
10081 	/* Search the misc table */
10082 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10083 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10084 		if (ipip->ipi_cmd == ioc_cmd)
10085 			/* Found a match in the misc table */
10086 			return (ipip);
10087 	}
10088 
10089 	return (NULL);
10090 }
10091 
10092 /*
10093  * Wrapper function for resuming deferred ioctl processing
10094  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10095  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10096  */
10097 /* ARGSUSED */
10098 void
10099 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10100     void *dummy_arg)
10101 {
10102 	ip_sioctl_copyin_setup(q, mp);
10103 }
10104 
10105 /*
10106  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10107  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10108  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10109  * We establish here the size of the block to be copied in.  mi_copyin
10110  * arranges for this to happen, an processing continues in ip_wput with
10111  * an M_IOCDATA message.
10112  */
10113 void
10114 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10115 {
10116 	int	copyin_size;
10117 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10118 	ip_ioctl_cmd_t *ipip;
10119 	cred_t *cr;
10120 	ip_stack_t	*ipst;
10121 
10122 	if (CONN_Q(q))
10123 		ipst = CONNQ_TO_IPST(q);
10124 	else
10125 		ipst = ILLQ_TO_IPST(q);
10126 
10127 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10128 	if (ipip == NULL) {
10129 		/*
10130 		 * The ioctl is not one we understand or own.
10131 		 * Pass it along to be processed down stream,
10132 		 * if this is a module instance of IP, else nak
10133 		 * the ioctl.
10134 		 */
10135 		if (q->q_next == NULL) {
10136 			goto nak;
10137 		} else {
10138 			putnext(q, mp);
10139 			return;
10140 		}
10141 	}
10142 
10143 	/*
10144 	 * If this is deferred, then we will do all the checks when we
10145 	 * come back.
10146 	 */
10147 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10148 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10149 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10150 		return;
10151 	}
10152 
10153 	/*
10154 	 * Only allow a very small subset of IP ioctls on this stream if
10155 	 * IP is a module and not a driver. Allowing ioctls to be processed
10156 	 * in this case may cause assert failures or data corruption.
10157 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10158 	 * ioctls allowed on an IP module stream, after which this stream
10159 	 * normally becomes a multiplexor (at which time the stream head
10160 	 * will fail all ioctls).
10161 	 */
10162 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10163 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10164 			/*
10165 			 * Pass common Streams ioctls which the IP
10166 			 * module does not own or consume along to
10167 			 * be processed down stream.
10168 			 */
10169 			putnext(q, mp);
10170 			return;
10171 		} else {
10172 			goto nak;
10173 		}
10174 	}
10175 
10176 	/* Make sure we have ioctl data to process. */
10177 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10178 		goto nak;
10179 
10180 	/*
10181 	 * Prefer dblk credential over ioctl credential; some synthesized
10182 	 * ioctls have kcred set because there's no way to crhold()
10183 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10184 	 * the framework; the caller of ioctl needs to hold the reference
10185 	 * for the duration of the call).
10186 	 */
10187 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10188 
10189 	/* Make sure normal users don't send down privileged ioctls */
10190 	if ((ipip->ipi_flags & IPI_PRIV) &&
10191 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10192 		/* We checked the privilege earlier but log it here */
10193 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10194 		return;
10195 	}
10196 
10197 	/*
10198 	 * The ioctl command tables can only encode fixed length
10199 	 * ioctl data. If the length is variable, the table will
10200 	 * encode the length as zero. Such special cases are handled
10201 	 * below in the switch.
10202 	 */
10203 	if (ipip->ipi_copyin_size != 0) {
10204 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10205 		return;
10206 	}
10207 
10208 	switch (iocp->ioc_cmd) {
10209 	case O_SIOCGIFCONF:
10210 	case SIOCGIFCONF:
10211 		/*
10212 		 * This IOCTL is hilarious.  See comments in
10213 		 * ip_sioctl_get_ifconf for the story.
10214 		 */
10215 		if (iocp->ioc_count == TRANSPARENT)
10216 			copyin_size = SIZEOF_STRUCT(ifconf,
10217 			    iocp->ioc_flag);
10218 		else
10219 			copyin_size = iocp->ioc_count;
10220 		mi_copyin(q, mp, NULL, copyin_size);
10221 		return;
10222 
10223 	case O_SIOCGLIFCONF:
10224 	case SIOCGLIFCONF:
10225 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10226 		mi_copyin(q, mp, NULL, copyin_size);
10227 		return;
10228 
10229 	case SIOCGLIFSRCOF:
10230 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10231 		mi_copyin(q, mp, NULL, copyin_size);
10232 		return;
10233 	case SIOCGIP6ADDRPOLICY:
10234 		ip_sioctl_ip6addrpolicy(q, mp);
10235 		ip6_asp_table_refrele(ipst);
10236 		return;
10237 
10238 	case SIOCSIP6ADDRPOLICY:
10239 		ip_sioctl_ip6addrpolicy(q, mp);
10240 		return;
10241 
10242 	case SIOCGDSTINFO:
10243 		ip_sioctl_dstinfo(q, mp);
10244 		ip6_asp_table_refrele(ipst);
10245 		return;
10246 
10247 	case I_PLINK:
10248 	case I_PUNLINK:
10249 	case I_LINK:
10250 	case I_UNLINK:
10251 		/*
10252 		 * We treat non-persistent link similarly as the persistent
10253 		 * link case, in terms of plumbing/unplumbing, as well as
10254 		 * dynamic re-plumbing events indicator.  See comments
10255 		 * in ip_sioctl_plink() for more.
10256 		 *
10257 		 * Request can be enqueued in the 'ipsq' while waiting
10258 		 * to become exclusive. So bump up the conn ref.
10259 		 */
10260 		if (CONN_Q(q))
10261 			CONN_INC_REF(Q_TO_CONN(q));
10262 		ip_sioctl_plink(NULL, q, mp, NULL);
10263 		return;
10264 
10265 	case ND_GET:
10266 	case ND_SET:
10267 		/*
10268 		 * Use of the nd table requires holding the reader lock.
10269 		 * Modifying the nd table thru nd_load/nd_unload requires
10270 		 * the writer lock.
10271 		 */
10272 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10273 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10274 			rw_exit(&ipst->ips_ip_g_nd_lock);
10275 
10276 			if (iocp->ioc_error)
10277 				iocp->ioc_count = 0;
10278 			mp->b_datap->db_type = M_IOCACK;
10279 			qreply(q, mp);
10280 			return;
10281 		}
10282 		rw_exit(&ipst->ips_ip_g_nd_lock);
10283 		/*
10284 		 * We don't understand this subioctl of ND_GET / ND_SET.
10285 		 * Maybe intended for some driver / module below us
10286 		 */
10287 		if (q->q_next) {
10288 			putnext(q, mp);
10289 		} else {
10290 			iocp->ioc_error = ENOENT;
10291 			mp->b_datap->db_type = M_IOCNAK;
10292 			iocp->ioc_count = 0;
10293 			qreply(q, mp);
10294 		}
10295 		return;
10296 
10297 	case IP_IOCTL:
10298 		ip_wput_ioctl(q, mp);
10299 		return;
10300 	default:
10301 		cmn_err(CE_PANIC, "should not happen ");
10302 	}
10303 nak:
10304 	if (mp->b_cont != NULL) {
10305 		freemsg(mp->b_cont);
10306 		mp->b_cont = NULL;
10307 	}
10308 	iocp->ioc_error = EINVAL;
10309 	mp->b_datap->db_type = M_IOCNAK;
10310 	iocp->ioc_count = 0;
10311 	qreply(q, mp);
10312 }
10313 
10314 /* ip_wput hands off ARP IOCTL responses to us */
10315 void
10316 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10317 {
10318 	struct arpreq *ar;
10319 	struct xarpreq *xar;
10320 	area_t	*area;
10321 	mblk_t	*area_mp;
10322 	struct iocblk *iocp;
10323 	mblk_t	*orig_ioc_mp, *tmp;
10324 	struct iocblk	*orig_iocp;
10325 	ill_t *ill;
10326 	conn_t *connp = NULL;
10327 	uint_t ioc_id;
10328 	mblk_t *pending_mp;
10329 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10330 	int *flagsp;
10331 	char *storage = NULL;
10332 	sin_t *sin;
10333 	ipaddr_t addr;
10334 	int err;
10335 	ip_stack_t *ipst;
10336 
10337 	ill = q->q_ptr;
10338 	ASSERT(ill != NULL);
10339 	ipst = ill->ill_ipst;
10340 
10341 	/*
10342 	 * We should get back from ARP a packet chain that looks like:
10343 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10344 	 */
10345 	if (!(area_mp = mp->b_cont) ||
10346 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10347 	    !(orig_ioc_mp = area_mp->b_cont) ||
10348 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10349 		freemsg(mp);
10350 		return;
10351 	}
10352 
10353 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10354 
10355 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10356 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10357 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10358 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10359 		x_arp_ioctl = B_TRUE;
10360 		xar = (struct xarpreq *)tmp->b_rptr;
10361 		sin = (sin_t *)&xar->xarp_pa;
10362 		flagsp = &xar->xarp_flags;
10363 		storage = xar->xarp_ha.sdl_data;
10364 		if (xar->xarp_ha.sdl_nlen != 0)
10365 			ifx_arp_ioctl = B_TRUE;
10366 	} else {
10367 		ar = (struct arpreq *)tmp->b_rptr;
10368 		sin = (sin_t *)&ar->arp_pa;
10369 		flagsp = &ar->arp_flags;
10370 		storage = ar->arp_ha.sa_data;
10371 	}
10372 
10373 	iocp = (struct iocblk *)mp->b_rptr;
10374 
10375 	/*
10376 	 * Pick out the originating queue based on the ioc_id.
10377 	 */
10378 	ioc_id = iocp->ioc_id;
10379 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10380 	if (pending_mp == NULL) {
10381 		ASSERT(connp == NULL);
10382 		inet_freemsg(mp);
10383 		return;
10384 	}
10385 	ASSERT(connp != NULL);
10386 	q = CONNP_TO_WQ(connp);
10387 
10388 	/* Uncouple the internally generated IOCTL from the original one */
10389 	area = (area_t *)area_mp->b_rptr;
10390 	area_mp->b_cont = NULL;
10391 
10392 	/*
10393 	 * Restore the b_next and b_prev used by mi code. This is needed
10394 	 * to complete the ioctl using mi* functions. We stored them in
10395 	 * the pending mp prior to sending the request to ARP.
10396 	 */
10397 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10398 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10399 	inet_freemsg(pending_mp);
10400 
10401 	/*
10402 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10403 	 * Catch the case where there is an IRE_CACHE by no entry in the
10404 	 * arp table.
10405 	 */
10406 	addr = sin->sin_addr.s_addr;
10407 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10408 		ire_t			*ire;
10409 		dl_unitdata_req_t	*dlup;
10410 		mblk_t			*llmp;
10411 		int			addr_len;
10412 		ill_t			*ipsqill = NULL;
10413 
10414 		if (ifx_arp_ioctl) {
10415 			/*
10416 			 * There's no need to lookup the ill, since
10417 			 * we've already done that when we started
10418 			 * processing the ioctl and sent the message
10419 			 * to ARP on that ill.  So use the ill that
10420 			 * is stored in q->q_ptr.
10421 			 */
10422 			ipsqill = ill;
10423 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10424 			    ipsqill->ill_ipif, ALL_ZONES,
10425 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10426 		} else {
10427 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10428 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10429 			if (ire != NULL)
10430 				ipsqill = ire_to_ill(ire);
10431 		}
10432 
10433 		if ((x_arp_ioctl) && (ipsqill != NULL))
10434 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10435 
10436 		if (ire != NULL) {
10437 			/*
10438 			 * Since the ire obtained from cachetable is used for
10439 			 * mac addr copying below, treat an incomplete ire as if
10440 			 * as if we never found it.
10441 			 */
10442 			if (ire->ire_nce != NULL &&
10443 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10444 				ire_refrele(ire);
10445 				ire = NULL;
10446 				ipsqill = NULL;
10447 				goto errack;
10448 			}
10449 			*flagsp = ATF_INUSE;
10450 			llmp = (ire->ire_nce != NULL ?
10451 			    ire->ire_nce->nce_res_mp : NULL);
10452 			if (llmp != NULL && ipsqill != NULL) {
10453 				uchar_t *macaddr;
10454 
10455 				addr_len = ipsqill->ill_phys_addr_length;
10456 				if (x_arp_ioctl && ((addr_len +
10457 				    ipsqill->ill_name_length) >
10458 				    sizeof (xar->xarp_ha.sdl_data))) {
10459 					ire_refrele(ire);
10460 					freemsg(mp);
10461 					ip_ioctl_finish(q, orig_ioc_mp,
10462 					    EINVAL, NO_COPYOUT, NULL);
10463 					return;
10464 				}
10465 				*flagsp |= ATF_COM;
10466 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10467 				if (ipsqill->ill_sap_length < 0)
10468 					macaddr = llmp->b_rptr +
10469 					    dlup->dl_dest_addr_offset;
10470 				else
10471 					macaddr = llmp->b_rptr +
10472 					    dlup->dl_dest_addr_offset +
10473 					    ipsqill->ill_sap_length;
10474 				/*
10475 				 * For SIOCGARP, MAC address length
10476 				 * validation has already been done
10477 				 * before the ioctl was issued to ARP to
10478 				 * allow it to progress only on 6 byte
10479 				 * addressable (ethernet like) media. Thus
10480 				 * the mac address copying can not overwrite
10481 				 * the sa_data area below.
10482 				 */
10483 				bcopy(macaddr, storage, addr_len);
10484 			}
10485 			/* Ditch the internal IOCTL. */
10486 			freemsg(mp);
10487 			ire_refrele(ire);
10488 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10489 			return;
10490 		}
10491 	}
10492 
10493 	/*
10494 	 * Delete the coresponding IRE_CACHE if any.
10495 	 * Reset the error if there was one (in case there was no entry
10496 	 * in arp.)
10497 	 */
10498 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10499 		ipif_t *ipintf = NULL;
10500 
10501 		if (ifx_arp_ioctl) {
10502 			/*
10503 			 * There's no need to lookup the ill, since
10504 			 * we've already done that when we started
10505 			 * processing the ioctl and sent the message
10506 			 * to ARP on that ill.  So use the ill that
10507 			 * is stored in q->q_ptr.
10508 			 */
10509 			ipintf = ill->ill_ipif;
10510 		}
10511 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10512 			/*
10513 			 * The address in "addr" may be an entry for a
10514 			 * router. If that's true, then any off-net
10515 			 * IRE_CACHE entries that go through the router
10516 			 * with address "addr" must be clobbered. Use
10517 			 * ire_walk to achieve this goal.
10518 			 */
10519 			if (ifx_arp_ioctl)
10520 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10521 				    ire_delete_cache_gw, (char *)&addr, ill);
10522 			else
10523 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10524 				    ALL_ZONES, ipst);
10525 			iocp->ioc_error = 0;
10526 		}
10527 	}
10528 errack:
10529 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10530 		err = iocp->ioc_error;
10531 		freemsg(mp);
10532 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10533 		return;
10534 	}
10535 
10536 	/*
10537 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10538 	 * the area_t into the struct {x}arpreq.
10539 	 */
10540 	if (x_arp_ioctl) {
10541 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10542 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10543 		    sizeof (xar->xarp_ha.sdl_data)) {
10544 			freemsg(mp);
10545 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10546 			    NULL);
10547 			return;
10548 		}
10549 	}
10550 	*flagsp = ATF_INUSE;
10551 	if (area->area_flags & ACE_F_PERMANENT)
10552 		*flagsp |= ATF_PERM;
10553 	if (area->area_flags & ACE_F_PUBLISH)
10554 		*flagsp |= ATF_PUBL;
10555 	if (area->area_flags & ACE_F_AUTHORITY)
10556 		*flagsp |= ATF_AUTHORITY;
10557 	if (area->area_hw_addr_length != 0) {
10558 		*flagsp |= ATF_COM;
10559 		/*
10560 		 * For SIOCGARP, MAC address length validation has
10561 		 * already been done before the ioctl was issued to ARP
10562 		 * to allow it to progress only on 6 byte addressable
10563 		 * (ethernet like) media. Thus the mac address copying
10564 		 * can not overwrite the sa_data area below.
10565 		 */
10566 		bcopy((char *)area + area->area_hw_addr_offset,
10567 		    storage, area->area_hw_addr_length);
10568 	}
10569 
10570 	/* Ditch the internal IOCTL. */
10571 	freemsg(mp);
10572 	/* Complete the original. */
10573 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10574 }
10575 
10576 /*
10577  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10578  * interface) create the next available logical interface for this
10579  * physical interface.
10580  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10581  * ipif with the specified name.
10582  *
10583  * If the address family is not AF_UNSPEC then set the address as well.
10584  *
10585  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10586  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10587  *
10588  * Executed as a writer on the ill or ill group.
10589  * So no lock is needed to traverse the ipif chain, or examine the
10590  * phyint flags.
10591  */
10592 /* ARGSUSED */
10593 int
10594 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10595     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10596 {
10597 	mblk_t	*mp1;
10598 	struct lifreq *lifr;
10599 	boolean_t	isv6;
10600 	boolean_t	exists;
10601 	char 	*name;
10602 	char	*endp;
10603 	char	*cp;
10604 	int	namelen;
10605 	ipif_t	*ipif;
10606 	long	id;
10607 	ipsq_t	*ipsq;
10608 	ill_t	*ill;
10609 	sin_t	*sin;
10610 	int	err = 0;
10611 	boolean_t found_sep = B_FALSE;
10612 	conn_t	*connp;
10613 	zoneid_t zoneid;
10614 	int	orig_ifindex = 0;
10615 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10616 
10617 	ASSERT(q->q_next == NULL);
10618 	ip1dbg(("ip_sioctl_addif\n"));
10619 	/* Existence of mp1 has been checked in ip_wput_nondata */
10620 	mp1 = mp->b_cont->b_cont;
10621 	/*
10622 	 * Null terminate the string to protect against buffer
10623 	 * overrun. String was generated by user code and may not
10624 	 * be trusted.
10625 	 */
10626 	lifr = (struct lifreq *)mp1->b_rptr;
10627 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10628 	name = lifr->lifr_name;
10629 	ASSERT(CONN_Q(q));
10630 	connp = Q_TO_CONN(q);
10631 	isv6 = connp->conn_af_isv6;
10632 	zoneid = connp->conn_zoneid;
10633 	namelen = mi_strlen(name);
10634 	if (namelen == 0)
10635 		return (EINVAL);
10636 
10637 	exists = B_FALSE;
10638 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10639 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10640 		/*
10641 		 * Allow creating lo0 using SIOCLIFADDIF.
10642 		 * can't be any other writer thread. So can pass null below
10643 		 * for the last 4 args to ipif_lookup_name.
10644 		 */
10645 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10646 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10647 		/* Prevent any further action */
10648 		if (ipif == NULL) {
10649 			return (ENOBUFS);
10650 		} else if (!exists) {
10651 			/* We created the ipif now and as writer */
10652 			ipif_refrele(ipif);
10653 			return (0);
10654 		} else {
10655 			ill = ipif->ipif_ill;
10656 			ill_refhold(ill);
10657 			ipif_refrele(ipif);
10658 		}
10659 	} else {
10660 		/* Look for a colon in the name. */
10661 		endp = &name[namelen];
10662 		for (cp = endp; --cp > name; ) {
10663 			if (*cp == IPIF_SEPARATOR_CHAR) {
10664 				found_sep = B_TRUE;
10665 				/*
10666 				 * Reject any non-decimal aliases for plumbing
10667 				 * of logical interfaces. Aliases with leading
10668 				 * zeroes are also rejected as they introduce
10669 				 * ambiguity in the naming of the interfaces.
10670 				 * Comparing with "0" takes care of all such
10671 				 * cases.
10672 				 */
10673 				if ((strncmp("0", cp+1, 1)) == 0)
10674 					return (EINVAL);
10675 
10676 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10677 				    id <= 0 || *endp != '\0') {
10678 					return (EINVAL);
10679 				}
10680 				*cp = '\0';
10681 				break;
10682 			}
10683 		}
10684 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10685 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10686 		if (found_sep)
10687 			*cp = IPIF_SEPARATOR_CHAR;
10688 		if (ill == NULL)
10689 			return (err);
10690 	}
10691 
10692 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10693 	    B_TRUE);
10694 
10695 	/*
10696 	 * Release the refhold due to the lookup, now that we are excl
10697 	 * or we are just returning
10698 	 */
10699 	ill_refrele(ill);
10700 
10701 	if (ipsq == NULL)
10702 		return (EINPROGRESS);
10703 
10704 	/*
10705 	 * If the interface is failed, inactive or offlined, look for a working
10706 	 * interface in the ill group and create the ipif there. If we can't
10707 	 * find a good interface, create the ipif anyway so that in.mpathd can
10708 	 * move it to the first repaired interface.
10709 	 */
10710 	if ((ill->ill_phyint->phyint_flags &
10711 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10712 	    ill->ill_phyint->phyint_groupname_len != 0) {
10713 		phyint_t *phyi;
10714 		char *groupname = ill->ill_phyint->phyint_groupname;
10715 
10716 		/*
10717 		 * We're looking for a working interface, but it doesn't matter
10718 		 * if it's up or down; so instead of following the group lists,
10719 		 * we look at each physical interface and compare the groupname.
10720 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10721 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10722 		 * Otherwise we create the ipif on the failed interface.
10723 		 */
10724 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10725 		phyi = avl_first(&ipst->ips_phyint_g_list->
10726 		    phyint_list_avl_by_index);
10727 		for (; phyi != NULL;
10728 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10729 		    phyint_list_avl_by_index,
10730 		    phyi, AVL_AFTER)) {
10731 			if (phyi->phyint_groupname_len == 0)
10732 				continue;
10733 			ASSERT(phyi->phyint_groupname != NULL);
10734 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10735 			    !(phyi->phyint_flags &
10736 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10737 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10738 			    (phyi->phyint_illv4 != NULL))) {
10739 				break;
10740 			}
10741 		}
10742 		rw_exit(&ipst->ips_ill_g_lock);
10743 
10744 		if (phyi != NULL) {
10745 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10746 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10747 			    phyi->phyint_illv4);
10748 		}
10749 	}
10750 
10751 	/*
10752 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10753 	 * before or after us.
10754 	 */
10755 	ASSERT(IAM_WRITER_ILL(ill));
10756 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10757 
10758 	if (found_sep && orig_ifindex == 0) {
10759 		/* Now see if there is an IPIF with this unit number. */
10760 		for (ipif = ill->ill_ipif; ipif != NULL;
10761 		    ipif = ipif->ipif_next) {
10762 			if (ipif->ipif_id == id) {
10763 				err = EEXIST;
10764 				goto done;
10765 			}
10766 		}
10767 	}
10768 
10769 	/*
10770 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10771 	 * of lo0. We never come here when we plumb lo0:0. It
10772 	 * happens in ipif_lookup_on_name.
10773 	 * The specified unit number is ignored when we create the ipif on a
10774 	 * different interface. However, we save it in ipif_orig_ipifid below so
10775 	 * that the ipif fails back to the right position.
10776 	 */
10777 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10778 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10779 		err = ENOBUFS;
10780 		goto done;
10781 	}
10782 
10783 	/* Return created name with ioctl */
10784 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10785 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10786 	ip1dbg(("created %s\n", lifr->lifr_name));
10787 
10788 	/* Set address */
10789 	sin = (sin_t *)&lifr->lifr_addr;
10790 	if (sin->sin_family != AF_UNSPEC) {
10791 		err = ip_sioctl_addr(ipif, sin, q, mp,
10792 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10793 	}
10794 
10795 	/* Set ifindex and unit number for failback */
10796 	if (err == 0 && orig_ifindex != 0) {
10797 		ipif->ipif_orig_ifindex = orig_ifindex;
10798 		if (found_sep) {
10799 			ipif->ipif_orig_ipifid = id;
10800 		}
10801 	}
10802 
10803 done:
10804 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10805 	return (err);
10806 }
10807 
10808 /*
10809  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10810  * interface) delete it based on the IP address (on this physical interface).
10811  * Otherwise delete it based on the ipif_id.
10812  * Also, special handling to allow a removeif of lo0.
10813  */
10814 /* ARGSUSED */
10815 int
10816 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10817     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10818 {
10819 	conn_t		*connp;
10820 	ill_t		*ill = ipif->ipif_ill;
10821 	boolean_t	 success;
10822 	ip_stack_t	*ipst;
10823 
10824 	ipst = CONNQ_TO_IPST(q);
10825 
10826 	ASSERT(q->q_next == NULL);
10827 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10828 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10829 	ASSERT(IAM_WRITER_IPIF(ipif));
10830 
10831 	connp = Q_TO_CONN(q);
10832 	/*
10833 	 * Special case for unplumbing lo0 (the loopback physical interface).
10834 	 * If unplumbing lo0, the incoming address structure has been
10835 	 * initialized to all zeros. When unplumbing lo0, all its logical
10836 	 * interfaces must be removed too.
10837 	 *
10838 	 * Note that this interface may be called to remove a specific
10839 	 * loopback logical interface (eg, lo0:1). But in that case
10840 	 * ipif->ipif_id != 0 so that the code path for that case is the
10841 	 * same as any other interface (meaning it skips the code directly
10842 	 * below).
10843 	 */
10844 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10845 		if (sin->sin_family == AF_UNSPEC &&
10846 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10847 			/*
10848 			 * Mark it condemned. No new ref. will be made to ill.
10849 			 */
10850 			mutex_enter(&ill->ill_lock);
10851 			ill->ill_state_flags |= ILL_CONDEMNED;
10852 			for (ipif = ill->ill_ipif; ipif != NULL;
10853 			    ipif = ipif->ipif_next) {
10854 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10855 			}
10856 			mutex_exit(&ill->ill_lock);
10857 
10858 			ipif = ill->ill_ipif;
10859 			/* unplumb the loopback interface */
10860 			ill_delete(ill);
10861 			mutex_enter(&connp->conn_lock);
10862 			mutex_enter(&ill->ill_lock);
10863 			ASSERT(ill->ill_group == NULL);
10864 
10865 			/* Are any references to this ill active */
10866 			if (ill_is_freeable(ill)) {
10867 				mutex_exit(&ill->ill_lock);
10868 				mutex_exit(&connp->conn_lock);
10869 				ill_delete_tail(ill);
10870 				mutex_enter(&ill->ill_lock);
10871 				ill_nic_info_dispatch(ill);
10872 				mutex_exit(&ill->ill_lock);
10873 				mi_free(ill);
10874 				return (0);
10875 			}
10876 			success = ipsq_pending_mp_add(connp, ipif,
10877 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10878 			mutex_exit(&connp->conn_lock);
10879 			mutex_exit(&ill->ill_lock);
10880 			if (success)
10881 				return (EINPROGRESS);
10882 			else
10883 				return (EINTR);
10884 		}
10885 	}
10886 
10887 	/*
10888 	 * We are exclusive on the ipsq, so an ill move will be serialized
10889 	 * before or after us.
10890 	 */
10891 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10892 
10893 	if (ipif->ipif_id == 0) {
10894 
10895 		ipsq_t *ipsq;
10896 
10897 		/* Find based on address */
10898 		if (ipif->ipif_isv6) {
10899 			sin6_t *sin6;
10900 
10901 			if (sin->sin_family != AF_INET6)
10902 				return (EAFNOSUPPORT);
10903 
10904 			sin6 = (sin6_t *)sin;
10905 			/* We are a writer, so we should be able to lookup */
10906 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10907 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
10908 			if (ipif == NULL) {
10909 				/*
10910 				 * Maybe the address in on another interface in
10911 				 * the same IPMP group? We check this below.
10912 				 */
10913 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10914 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
10915 				    ipst);
10916 			}
10917 		} else {
10918 			ipaddr_t addr;
10919 
10920 			if (sin->sin_family != AF_INET)
10921 				return (EAFNOSUPPORT);
10922 
10923 			addr = sin->sin_addr.s_addr;
10924 			/* We are a writer, so we should be able to lookup */
10925 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10926 			    NULL, NULL, NULL, ipst);
10927 			if (ipif == NULL) {
10928 				/*
10929 				 * Maybe the address in on another interface in
10930 				 * the same IPMP group? We check this below.
10931 				 */
10932 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10933 				    NULL, NULL, NULL, NULL, ipst);
10934 			}
10935 		}
10936 		if (ipif == NULL) {
10937 			return (EADDRNOTAVAIL);
10938 		}
10939 
10940 		/*
10941 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
10942 		 * lifr_name of the physical interface but with an ip address
10943 		 * lifr_addr of a logical interface plumbed over it.
10944 		 * So update ipsq_current_ipif once ipif points to the
10945 		 * correct interface after doing ipif_lookup_addr().
10946 		 */
10947 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
10948 		ASSERT(ipsq != NULL);
10949 
10950 		mutex_enter(&ipsq->ipsq_lock);
10951 		ipsq->ipsq_current_ipif = ipif;
10952 		mutex_exit(&ipsq->ipsq_lock);
10953 
10954 		/*
10955 		 * When the address to be removed is hosted on a different
10956 		 * interface, we check if the interface is in the same IPMP
10957 		 * group as the specified one; if so we proceed with the
10958 		 * removal.
10959 		 * ill->ill_group is NULL when the ill is down, so we have to
10960 		 * compare the group names instead.
10961 		 */
10962 		if (ipif->ipif_ill != ill &&
10963 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10964 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10965 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10966 		    ill->ill_phyint->phyint_groupname) != 0)) {
10967 			ipif_refrele(ipif);
10968 			return (EADDRNOTAVAIL);
10969 		}
10970 
10971 		/* This is a writer */
10972 		ipif_refrele(ipif);
10973 	}
10974 
10975 	/*
10976 	 * Can not delete instance zero since it is tied to the ill.
10977 	 */
10978 	if (ipif->ipif_id == 0)
10979 		return (EBUSY);
10980 
10981 	mutex_enter(&ill->ill_lock);
10982 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10983 	mutex_exit(&ill->ill_lock);
10984 
10985 	ipif_free(ipif);
10986 
10987 	mutex_enter(&connp->conn_lock);
10988 	mutex_enter(&ill->ill_lock);
10989 
10990 
10991 	/* Are any references to this ipif active */
10992 	if (ipif_is_freeable(ipif)) {
10993 		mutex_exit(&ill->ill_lock);
10994 		mutex_exit(&connp->conn_lock);
10995 		ipif_non_duplicate(ipif);
10996 		ipif_down_tail(ipif);
10997 		ipif_free_tail(ipif); /* frees ipif */
10998 		return (0);
10999 	}
11000 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
11001 	    IPIF_FREE);
11002 	mutex_exit(&ill->ill_lock);
11003 	mutex_exit(&connp->conn_lock);
11004 	if (success)
11005 		return (EINPROGRESS);
11006 	else
11007 		return (EINTR);
11008 }
11009 
11010 /*
11011  * Restart the removeif ioctl. The refcnt has gone down to 0.
11012  * The ipif is already condemned. So can't find it thru lookups.
11013  */
11014 /* ARGSUSED */
11015 int
11016 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
11017     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11018 {
11019 	ill_t *ill = ipif->ipif_ill;
11020 
11021 	ASSERT(IAM_WRITER_IPIF(ipif));
11022 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
11023 
11024 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
11025 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11026 
11027 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11028 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
11029 		ill_delete_tail(ill);
11030 		mutex_enter(&ill->ill_lock);
11031 		ill_nic_info_dispatch(ill);
11032 		mutex_exit(&ill->ill_lock);
11033 		mi_free(ill);
11034 		return (0);
11035 	}
11036 
11037 	ipif_non_duplicate(ipif);
11038 	ipif_down_tail(ipif);
11039 	ipif_free_tail(ipif);
11040 
11041 	ILL_UNMARK_CHANGING(ill);
11042 	return (0);
11043 }
11044 
11045 /*
11046  * Set the local interface address.
11047  * Allow an address of all zero when the interface is down.
11048  */
11049 /* ARGSUSED */
11050 int
11051 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11052     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11053 {
11054 	int err = 0;
11055 	in6_addr_t v6addr;
11056 	boolean_t need_up = B_FALSE;
11057 
11058 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11059 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11060 
11061 	ASSERT(IAM_WRITER_IPIF(ipif));
11062 
11063 	if (ipif->ipif_isv6) {
11064 		sin6_t *sin6;
11065 		ill_t *ill;
11066 		phyint_t *phyi;
11067 
11068 		if (sin->sin_family != AF_INET6)
11069 			return (EAFNOSUPPORT);
11070 
11071 		sin6 = (sin6_t *)sin;
11072 		v6addr = sin6->sin6_addr;
11073 		ill = ipif->ipif_ill;
11074 		phyi = ill->ill_phyint;
11075 
11076 		/*
11077 		 * Enforce that true multicast interfaces have a link-local
11078 		 * address for logical unit 0.
11079 		 */
11080 		if (ipif->ipif_id == 0 &&
11081 		    (ill->ill_flags & ILLF_MULTICAST) &&
11082 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11083 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11084 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11085 			return (EADDRNOTAVAIL);
11086 		}
11087 
11088 		/*
11089 		 * up interfaces shouldn't have the unspecified address
11090 		 * unless they also have the IPIF_NOLOCAL flags set and
11091 		 * have a subnet assigned.
11092 		 */
11093 		if ((ipif->ipif_flags & IPIF_UP) &&
11094 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11095 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11096 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11097 			return (EADDRNOTAVAIL);
11098 		}
11099 
11100 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11101 			return (EADDRNOTAVAIL);
11102 	} else {
11103 		ipaddr_t addr;
11104 
11105 		if (sin->sin_family != AF_INET)
11106 			return (EAFNOSUPPORT);
11107 
11108 		addr = sin->sin_addr.s_addr;
11109 
11110 		/* Allow 0 as the local address. */
11111 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11112 			return (EADDRNOTAVAIL);
11113 
11114 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11115 	}
11116 
11117 
11118 	/*
11119 	 * Even if there is no change we redo things just to rerun
11120 	 * ipif_set_default.
11121 	 */
11122 	if (ipif->ipif_flags & IPIF_UP) {
11123 		/*
11124 		 * Setting a new local address, make sure
11125 		 * we have net and subnet bcast ire's for
11126 		 * the old address if we need them.
11127 		 */
11128 		if (!ipif->ipif_isv6)
11129 			ipif_check_bcast_ires(ipif);
11130 		/*
11131 		 * If the interface is already marked up,
11132 		 * we call ipif_down which will take care
11133 		 * of ditching any IREs that have been set
11134 		 * up based on the old interface address.
11135 		 */
11136 		err = ipif_logical_down(ipif, q, mp);
11137 		if (err == EINPROGRESS)
11138 			return (err);
11139 		ipif_down_tail(ipif);
11140 		need_up = 1;
11141 	}
11142 
11143 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11144 	return (err);
11145 }
11146 
11147 int
11148 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11149     boolean_t need_up)
11150 {
11151 	in6_addr_t v6addr;
11152 	in6_addr_t ov6addr;
11153 	ipaddr_t addr;
11154 	sin6_t	*sin6;
11155 	int	sinlen;
11156 	int	err = 0;
11157 	ill_t	*ill = ipif->ipif_ill;
11158 	boolean_t need_dl_down;
11159 	boolean_t need_arp_down;
11160 	struct iocblk *iocp;
11161 
11162 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11163 
11164 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11165 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11166 	ASSERT(IAM_WRITER_IPIF(ipif));
11167 
11168 	/* Must cancel any pending timer before taking the ill_lock */
11169 	if (ipif->ipif_recovery_id != 0)
11170 		(void) untimeout(ipif->ipif_recovery_id);
11171 	ipif->ipif_recovery_id = 0;
11172 
11173 	if (ipif->ipif_isv6) {
11174 		sin6 = (sin6_t *)sin;
11175 		v6addr = sin6->sin6_addr;
11176 		sinlen = sizeof (struct sockaddr_in6);
11177 	} else {
11178 		addr = sin->sin_addr.s_addr;
11179 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11180 		sinlen = sizeof (struct sockaddr_in);
11181 	}
11182 	mutex_enter(&ill->ill_lock);
11183 	ov6addr = ipif->ipif_v6lcl_addr;
11184 	ipif->ipif_v6lcl_addr = v6addr;
11185 	sctp_update_ipif_addr(ipif, ov6addr);
11186 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11187 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11188 	} else {
11189 		ipif->ipif_v6src_addr = v6addr;
11190 	}
11191 	ipif->ipif_addr_ready = 0;
11192 
11193 	/*
11194 	 * If the interface was previously marked as a duplicate, then since
11195 	 * we've now got a "new" address, it should no longer be considered a
11196 	 * duplicate -- even if the "new" address is the same as the old one.
11197 	 * Note that if all ipifs are down, we may have a pending ARP down
11198 	 * event to handle.  This is because we want to recover from duplicates
11199 	 * and thus delay tearing down ARP until the duplicates have been
11200 	 * removed or disabled.
11201 	 */
11202 	need_dl_down = need_arp_down = B_FALSE;
11203 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11204 		need_arp_down = !need_up;
11205 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11206 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11207 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11208 			need_dl_down = B_TRUE;
11209 		}
11210 	}
11211 
11212 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11213 	    !ill->ill_is_6to4tun) {
11214 		queue_t *wqp = ill->ill_wq;
11215 
11216 		/*
11217 		 * The local address of this interface is a 6to4 address,
11218 		 * check if this interface is in fact a 6to4 tunnel or just
11219 		 * an interface configured with a 6to4 address.  We are only
11220 		 * interested in the former.
11221 		 */
11222 		if (wqp != NULL) {
11223 			while ((wqp->q_next != NULL) &&
11224 			    (wqp->q_next->q_qinfo != NULL) &&
11225 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11226 
11227 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11228 				    == TUN6TO4_MODID) {
11229 					/* set for use in IP */
11230 					ill->ill_is_6to4tun = 1;
11231 					break;
11232 				}
11233 				wqp = wqp->q_next;
11234 			}
11235 		}
11236 	}
11237 
11238 	ipif_set_default(ipif);
11239 
11240 	/*
11241 	 * When publishing an interface address change event, we only notify
11242 	 * the event listeners of the new address.  It is assumed that if they
11243 	 * actively care about the addresses assigned that they will have
11244 	 * already discovered the previous address assigned (if there was one.)
11245 	 *
11246 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11247 	 */
11248 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11249 		(void) ill_hook_event_create(ill, MAP_IPIF_ID(ipif->ipif_id),
11250 		    NE_ADDRESS_CHANGE, sin, sinlen);
11251 	}
11252 
11253 	mutex_exit(&ill->ill_lock);
11254 
11255 	if (need_up) {
11256 		/*
11257 		 * Now bring the interface back up.  If this
11258 		 * is the only IPIF for the ILL, ipif_up
11259 		 * will have to re-bind to the device, so
11260 		 * we may get back EINPROGRESS, in which
11261 		 * case, this IOCTL will get completed in
11262 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11263 		 */
11264 		err = ipif_up(ipif, q, mp);
11265 	}
11266 
11267 	if (need_dl_down)
11268 		ill_dl_down(ill);
11269 	if (need_arp_down)
11270 		ipif_arp_down(ipif);
11271 
11272 	return (err);
11273 }
11274 
11275 
11276 /*
11277  * Restart entry point to restart the address set operation after the
11278  * refcounts have dropped to zero.
11279  */
11280 /* ARGSUSED */
11281 int
11282 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11283     ip_ioctl_cmd_t *ipip, void *ifreq)
11284 {
11285 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11286 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11287 	ASSERT(IAM_WRITER_IPIF(ipif));
11288 	ipif_down_tail(ipif);
11289 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11290 }
11291 
11292 /* ARGSUSED */
11293 int
11294 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11295     ip_ioctl_cmd_t *ipip, void *if_req)
11296 {
11297 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11298 	struct lifreq *lifr = (struct lifreq *)if_req;
11299 
11300 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11301 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11302 	/*
11303 	 * The net mask and address can't change since we have a
11304 	 * reference to the ipif. So no lock is necessary.
11305 	 */
11306 	if (ipif->ipif_isv6) {
11307 		*sin6 = sin6_null;
11308 		sin6->sin6_family = AF_INET6;
11309 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11310 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11311 		lifr->lifr_addrlen =
11312 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11313 	} else {
11314 		*sin = sin_null;
11315 		sin->sin_family = AF_INET;
11316 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11317 		if (ipip->ipi_cmd_type == LIF_CMD) {
11318 			lifr->lifr_addrlen =
11319 			    ip_mask_to_plen(ipif->ipif_net_mask);
11320 		}
11321 	}
11322 	return (0);
11323 }
11324 
11325 /*
11326  * Set the destination address for a pt-pt interface.
11327  */
11328 /* ARGSUSED */
11329 int
11330 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11331     ip_ioctl_cmd_t *ipip, void *if_req)
11332 {
11333 	int err = 0;
11334 	in6_addr_t v6addr;
11335 	boolean_t need_up = B_FALSE;
11336 
11337 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11338 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11339 	ASSERT(IAM_WRITER_IPIF(ipif));
11340 
11341 	if (ipif->ipif_isv6) {
11342 		sin6_t *sin6;
11343 
11344 		if (sin->sin_family != AF_INET6)
11345 			return (EAFNOSUPPORT);
11346 
11347 		sin6 = (sin6_t *)sin;
11348 		v6addr = sin6->sin6_addr;
11349 
11350 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11351 			return (EADDRNOTAVAIL);
11352 	} else {
11353 		ipaddr_t addr;
11354 
11355 		if (sin->sin_family != AF_INET)
11356 			return (EAFNOSUPPORT);
11357 
11358 		addr = sin->sin_addr.s_addr;
11359 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11360 			return (EADDRNOTAVAIL);
11361 
11362 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11363 	}
11364 
11365 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11366 		return (0);	/* No change */
11367 
11368 	if (ipif->ipif_flags & IPIF_UP) {
11369 		/*
11370 		 * If the interface is already marked up,
11371 		 * we call ipif_down which will take care
11372 		 * of ditching any IREs that have been set
11373 		 * up based on the old pp dst address.
11374 		 */
11375 		err = ipif_logical_down(ipif, q, mp);
11376 		if (err == EINPROGRESS)
11377 			return (err);
11378 		ipif_down_tail(ipif);
11379 		need_up = B_TRUE;
11380 	}
11381 	/*
11382 	 * could return EINPROGRESS. If so ioctl will complete in
11383 	 * ip_rput_dlpi_writer
11384 	 */
11385 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11386 	return (err);
11387 }
11388 
11389 static int
11390 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11391     boolean_t need_up)
11392 {
11393 	in6_addr_t v6addr;
11394 	ill_t	*ill = ipif->ipif_ill;
11395 	int	err = 0;
11396 	boolean_t need_dl_down;
11397 	boolean_t need_arp_down;
11398 
11399 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11400 	    ipif->ipif_id, (void *)ipif));
11401 
11402 	/* Must cancel any pending timer before taking the ill_lock */
11403 	if (ipif->ipif_recovery_id != 0)
11404 		(void) untimeout(ipif->ipif_recovery_id);
11405 	ipif->ipif_recovery_id = 0;
11406 
11407 	if (ipif->ipif_isv6) {
11408 		sin6_t *sin6;
11409 
11410 		sin6 = (sin6_t *)sin;
11411 		v6addr = sin6->sin6_addr;
11412 	} else {
11413 		ipaddr_t addr;
11414 
11415 		addr = sin->sin_addr.s_addr;
11416 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11417 	}
11418 	mutex_enter(&ill->ill_lock);
11419 	/* Set point to point destination address. */
11420 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11421 		/*
11422 		 * Allow this as a means of creating logical
11423 		 * pt-pt interfaces on top of e.g. an Ethernet.
11424 		 * XXX Undocumented HACK for testing.
11425 		 * pt-pt interfaces are created with NUD disabled.
11426 		 */
11427 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11428 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11429 		if (ipif->ipif_isv6)
11430 			ill->ill_flags |= ILLF_NONUD;
11431 	}
11432 
11433 	/*
11434 	 * If the interface was previously marked as a duplicate, then since
11435 	 * we've now got a "new" address, it should no longer be considered a
11436 	 * duplicate -- even if the "new" address is the same as the old one.
11437 	 * Note that if all ipifs are down, we may have a pending ARP down
11438 	 * event to handle.
11439 	 */
11440 	need_dl_down = need_arp_down = B_FALSE;
11441 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11442 		need_arp_down = !need_up;
11443 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11444 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11445 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11446 			need_dl_down = B_TRUE;
11447 		}
11448 	}
11449 
11450 	/* Set the new address. */
11451 	ipif->ipif_v6pp_dst_addr = v6addr;
11452 	/* Make sure subnet tracks pp_dst */
11453 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11454 	mutex_exit(&ill->ill_lock);
11455 
11456 	if (need_up) {
11457 		/*
11458 		 * Now bring the interface back up.  If this
11459 		 * is the only IPIF for the ILL, ipif_up
11460 		 * will have to re-bind to the device, so
11461 		 * we may get back EINPROGRESS, in which
11462 		 * case, this IOCTL will get completed in
11463 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11464 		 */
11465 		err = ipif_up(ipif, q, mp);
11466 	}
11467 
11468 	if (need_dl_down)
11469 		ill_dl_down(ill);
11470 
11471 	if (need_arp_down)
11472 		ipif_arp_down(ipif);
11473 	return (err);
11474 }
11475 
11476 /*
11477  * Restart entry point to restart the dstaddress set operation after the
11478  * refcounts have dropped to zero.
11479  */
11480 /* ARGSUSED */
11481 int
11482 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11483     ip_ioctl_cmd_t *ipip, void *ifreq)
11484 {
11485 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11486 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11487 	ipif_down_tail(ipif);
11488 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11489 }
11490 
11491 /* ARGSUSED */
11492 int
11493 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11494     ip_ioctl_cmd_t *ipip, void *if_req)
11495 {
11496 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11497 
11498 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11499 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11500 	/*
11501 	 * Get point to point destination address. The addresses can't
11502 	 * change since we hold a reference to the ipif.
11503 	 */
11504 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11505 		return (EADDRNOTAVAIL);
11506 
11507 	if (ipif->ipif_isv6) {
11508 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11509 		*sin6 = sin6_null;
11510 		sin6->sin6_family = AF_INET6;
11511 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11512 	} else {
11513 		*sin = sin_null;
11514 		sin->sin_family = AF_INET;
11515 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11516 	}
11517 	return (0);
11518 }
11519 
11520 /*
11521  * part of ipmp, make this func return the active/inactive state and
11522  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11523  */
11524 /*
11525  * This function either sets or clears the IFF_INACTIVE flag.
11526  *
11527  * As long as there are some addresses or multicast memberships on the
11528  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11529  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11530  * will be used for outbound packets.
11531  *
11532  * Caller needs to verify the validity of setting IFF_INACTIVE.
11533  */
11534 static void
11535 phyint_inactive(phyint_t *phyi)
11536 {
11537 	ill_t *ill_v4;
11538 	ill_t *ill_v6;
11539 	ipif_t *ipif;
11540 	ilm_t *ilm;
11541 
11542 	ill_v4 = phyi->phyint_illv4;
11543 	ill_v6 = phyi->phyint_illv6;
11544 
11545 	/*
11546 	 * No need for a lock while traversing the list since iam
11547 	 * a writer
11548 	 */
11549 	if (ill_v4 != NULL) {
11550 		ASSERT(IAM_WRITER_ILL(ill_v4));
11551 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11552 		    ipif = ipif->ipif_next) {
11553 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11554 				mutex_enter(&phyi->phyint_lock);
11555 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11556 				mutex_exit(&phyi->phyint_lock);
11557 				return;
11558 			}
11559 		}
11560 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11561 		    ilm = ilm->ilm_next) {
11562 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11563 				mutex_enter(&phyi->phyint_lock);
11564 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11565 				mutex_exit(&phyi->phyint_lock);
11566 				return;
11567 			}
11568 		}
11569 	}
11570 	if (ill_v6 != NULL) {
11571 		ill_v6 = phyi->phyint_illv6;
11572 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11573 		    ipif = ipif->ipif_next) {
11574 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11575 				mutex_enter(&phyi->phyint_lock);
11576 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11577 				mutex_exit(&phyi->phyint_lock);
11578 				return;
11579 			}
11580 		}
11581 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11582 		    ilm = ilm->ilm_next) {
11583 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11584 				mutex_enter(&phyi->phyint_lock);
11585 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11586 				mutex_exit(&phyi->phyint_lock);
11587 				return;
11588 			}
11589 		}
11590 	}
11591 	mutex_enter(&phyi->phyint_lock);
11592 	phyi->phyint_flags |= PHYI_INACTIVE;
11593 	mutex_exit(&phyi->phyint_lock);
11594 }
11595 
11596 /*
11597  * This function is called only when the phyint flags change. Currently
11598  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11599  * that we can select a good ill.
11600  */
11601 static void
11602 ip_redo_nomination(phyint_t *phyi)
11603 {
11604 	ill_t *ill_v4;
11605 
11606 	ill_v4 = phyi->phyint_illv4;
11607 
11608 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11609 		ASSERT(IAM_WRITER_ILL(ill_v4));
11610 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11611 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11612 	}
11613 }
11614 
11615 /*
11616  * Heuristic to check if ill is INACTIVE.
11617  * Checks if ill has an ipif with an usable ip address.
11618  *
11619  * Return values:
11620  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11621  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11622  */
11623 static boolean_t
11624 ill_is_inactive(ill_t *ill)
11625 {
11626 	ipif_t *ipif;
11627 
11628 	/* Check whether it is in an IPMP group */
11629 	if (ill->ill_phyint->phyint_groupname == NULL)
11630 		return (B_FALSE);
11631 
11632 	if (ill->ill_ipif_up_count == 0)
11633 		return (B_TRUE);
11634 
11635 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11636 		uint64_t flags = ipif->ipif_flags;
11637 
11638 		/*
11639 		 * This ipif is usable if it is IPIF_UP and not a
11640 		 * dedicated test address.  A dedicated test address
11641 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11642 		 * (note in particular that V6 test addresses are
11643 		 * link-local data addresses and thus are marked
11644 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11645 		 */
11646 		if ((flags & IPIF_UP) &&
11647 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11648 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11649 			return (B_FALSE);
11650 	}
11651 	return (B_TRUE);
11652 }
11653 
11654 /*
11655  * Set interface flags.
11656  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11657  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11658  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11659  *
11660  * NOTE : We really don't enforce that ipif_id zero should be used
11661  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11662  *	  is because applications generally does SICGLIFFLAGS and
11663  *	  ORs in the new flags (that affects the logical) and does a
11664  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11665  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11666  *	  flags that will be turned on is correct with respect to
11667  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11668  */
11669 /* ARGSUSED */
11670 int
11671 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11672     ip_ioctl_cmd_t *ipip, void *if_req)
11673 {
11674 	uint64_t turn_on;
11675 	uint64_t turn_off;
11676 	int	err;
11677 	boolean_t need_up = B_FALSE;
11678 	phyint_t *phyi;
11679 	ill_t *ill;
11680 	uint64_t intf_flags;
11681 	boolean_t phyint_flags_modified = B_FALSE;
11682 	uint64_t flags;
11683 	struct ifreq *ifr;
11684 	struct lifreq *lifr;
11685 	boolean_t set_linklocal = B_FALSE;
11686 	boolean_t zero_source = B_FALSE;
11687 	ip_stack_t *ipst;
11688 
11689 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11690 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11691 
11692 	ASSERT(IAM_WRITER_IPIF(ipif));
11693 
11694 	ill = ipif->ipif_ill;
11695 	phyi = ill->ill_phyint;
11696 	ipst = ill->ill_ipst;
11697 
11698 	if (ipip->ipi_cmd_type == IF_CMD) {
11699 		ifr = (struct ifreq *)if_req;
11700 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11701 	} else {
11702 		lifr = (struct lifreq *)if_req;
11703 		flags = lifr->lifr_flags;
11704 	}
11705 
11706 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11707 
11708 	/*
11709 	 * Has the flags been set correctly till now ?
11710 	 */
11711 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11712 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11713 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11714 	/*
11715 	 * Compare the new flags to the old, and partition
11716 	 * into those coming on and those going off.
11717 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11718 	 */
11719 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11720 		flags |= intf_flags & ~0xFFFF;
11721 
11722 	/*
11723 	 * First check which bits will change and then which will
11724 	 * go on and off
11725 	 */
11726 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11727 	if (!turn_on)
11728 		return (0);	/* No change */
11729 
11730 	turn_off = intf_flags & turn_on;
11731 	turn_on ^= turn_off;
11732 	err = 0;
11733 
11734 	/*
11735 	 * Don't allow any bits belonging to the logical interface
11736 	 * to be set or cleared on the replacement ipif that was
11737 	 * created temporarily during a MOVE.
11738 	 */
11739 	if (ipif->ipif_replace_zero &&
11740 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11741 		return (EINVAL);
11742 	}
11743 
11744 	/*
11745 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11746 	 * IPv6 interfaces.
11747 	 */
11748 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11749 		return (EINVAL);
11750 
11751 	/*
11752 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11753 	 */
11754 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11755 		return (EINVAL);
11756 
11757 	/*
11758 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11759 	 * interfaces.  It makes no sense in that context.
11760 	 */
11761 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11762 		return (EINVAL);
11763 
11764 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11765 		zero_source = B_TRUE;
11766 
11767 	/*
11768 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11769 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11770 	 * If the link local address isn't set, and can be set, it will get
11771 	 * set later on in this function.
11772 	 */
11773 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11774 	    (flags & IFF_UP) && !zero_source &&
11775 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11776 		if (ipif_cant_setlinklocal(ipif))
11777 			return (EINVAL);
11778 		set_linklocal = B_TRUE;
11779 	}
11780 
11781 	/*
11782 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11783 	 * same time. No need to grab ill_g_usesrc_lock here, see
11784 	 * synchronization notes in ip.c
11785 	 */
11786 	if (turn_on & PHYI_STANDBY &&
11787 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11788 		return (EINVAL);
11789 	}
11790 
11791 	/*
11792 	 * If we modify physical interface flags, we'll potentially need to
11793 	 * send up two routing socket messages for the changes (one for the
11794 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11795 	 */
11796 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11797 		phyint_flags_modified = B_TRUE;
11798 
11799 	/*
11800 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11801 	 * we need to flush the IRE_CACHES belonging to this ill.
11802 	 * We handle this case here without doing the DOWN/UP dance
11803 	 * like it is done for other flags. If some other flags are
11804 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11805 	 * below will handle it by bringing it down and then
11806 	 * bringing it UP.
11807 	 */
11808 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11809 		ill_t *ill_v4, *ill_v6;
11810 
11811 		ill_v4 = phyi->phyint_illv4;
11812 		ill_v6 = phyi->phyint_illv6;
11813 
11814 		/*
11815 		 * First set the INACTIVE flag if needed. Then delete the ires.
11816 		 * ire_add will atomically prevent creating new IRE_CACHEs
11817 		 * unless hidden flag is set.
11818 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11819 		 */
11820 		if ((turn_on & PHYI_FAILED) &&
11821 		    ((intf_flags & PHYI_STANDBY) ||
11822 		    !ipst->ips_ipmp_enable_failback)) {
11823 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11824 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11825 		}
11826 		if ((turn_off & PHYI_FAILED) &&
11827 		    ((intf_flags & PHYI_STANDBY) ||
11828 		    (!ipst->ips_ipmp_enable_failback &&
11829 		    ill_is_inactive(ill)))) {
11830 			phyint_inactive(phyi);
11831 		}
11832 
11833 		if (turn_on & PHYI_STANDBY) {
11834 			/*
11835 			 * We implicitly set INACTIVE only when STANDBY is set.
11836 			 * INACTIVE is also set on non-STANDBY phyint when user
11837 			 * disables FAILBACK using configuration file.
11838 			 * Do not allow STANDBY to be set on such INACTIVE
11839 			 * phyint
11840 			 */
11841 			if (phyi->phyint_flags & PHYI_INACTIVE)
11842 				return (EINVAL);
11843 			if (!(phyi->phyint_flags & PHYI_FAILED))
11844 				phyint_inactive(phyi);
11845 		}
11846 		if (turn_off & PHYI_STANDBY) {
11847 			if (ipst->ips_ipmp_enable_failback) {
11848 				/*
11849 				 * Reset PHYI_INACTIVE.
11850 				 */
11851 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11852 			} else if (ill_is_inactive(ill) &&
11853 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11854 				/*
11855 				 * Need to set INACTIVE, when user sets
11856 				 * STANDBY on a non-STANDBY phyint and
11857 				 * later resets STANDBY
11858 				 */
11859 				phyint_inactive(phyi);
11860 			}
11861 		}
11862 		/*
11863 		 * We should always send up a message so that the
11864 		 * daemons come to know of it. Note that the zeroth
11865 		 * interface can be down and the check below for IPIF_UP
11866 		 * will not make sense as we are actually setting
11867 		 * a phyint flag here. We assume that the ipif used
11868 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11869 		 * send up any message for non-zero ipifs).
11870 		 */
11871 		phyint_flags_modified = B_TRUE;
11872 
11873 		if (ill_v4 != NULL) {
11874 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11875 			    IRE_CACHE, ill_stq_cache_delete,
11876 			    (char *)ill_v4, ill_v4);
11877 			illgrp_reset_schednext(ill_v4);
11878 		}
11879 		if (ill_v6 != NULL) {
11880 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11881 			    IRE_CACHE, ill_stq_cache_delete,
11882 			    (char *)ill_v6, ill_v6);
11883 			illgrp_reset_schednext(ill_v6);
11884 		}
11885 	}
11886 
11887 	/*
11888 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11889 	 * status of the interface and, if the interface is part of an IPMP
11890 	 * group, all other interfaces that are part of the same IPMP
11891 	 * group.
11892 	 */
11893 	if ((turn_on | turn_off) & ILLF_ROUTER)
11894 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11895 
11896 	/*
11897 	 * If the interface is not UP and we are not going to
11898 	 * bring it UP, record the flags and return. When the
11899 	 * interface comes UP later, the right actions will be
11900 	 * taken.
11901 	 */
11902 	if (!(ipif->ipif_flags & IPIF_UP) &&
11903 	    !(turn_on & IPIF_UP)) {
11904 		/* Record new flags in their respective places. */
11905 		mutex_enter(&ill->ill_lock);
11906 		mutex_enter(&ill->ill_phyint->phyint_lock);
11907 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11908 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11909 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11910 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11911 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11912 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11913 		mutex_exit(&ill->ill_lock);
11914 		mutex_exit(&ill->ill_phyint->phyint_lock);
11915 
11916 		/*
11917 		 * We do the broadcast and nomination here rather
11918 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11919 		 * the case of FAILBACK from INACTIVE standby to the
11920 		 * interface that has been repaired, PHYI_FAILED has not
11921 		 * been cleared yet. If there are only two interfaces in
11922 		 * that group, all we have is a FAILED and INACTIVE
11923 		 * interface. If we do the nomination soon after a failback,
11924 		 * the broadcast nomination code would select the
11925 		 * INACTIVE interface for receiving broadcasts as FAILED is
11926 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11927 		 * receive broadcast packets, we need to redo nomination
11928 		 * when the FAILED is cleared here. Thus, in general we
11929 		 * always do the nomination here for FAILED, STANDBY
11930 		 * and OFFLINE.
11931 		 */
11932 		if (((turn_on | turn_off) &
11933 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11934 			ip_redo_nomination(phyi);
11935 		}
11936 		if (phyint_flags_modified) {
11937 			if (phyi->phyint_illv4 != NULL) {
11938 				ip_rts_ifmsg(phyi->phyint_illv4->
11939 				    ill_ipif);
11940 			}
11941 			if (phyi->phyint_illv6 != NULL) {
11942 				ip_rts_ifmsg(phyi->phyint_illv6->
11943 				    ill_ipif);
11944 			}
11945 		}
11946 		return (0);
11947 	} else if (set_linklocal || zero_source) {
11948 		mutex_enter(&ill->ill_lock);
11949 		if (set_linklocal)
11950 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11951 		if (zero_source)
11952 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11953 		mutex_exit(&ill->ill_lock);
11954 	}
11955 
11956 	/*
11957 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11958 	 * or point-to-point interfaces with an unspecified destination. We do
11959 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11960 	 * have a subnet assigned, which is how in.ndpd currently manages its
11961 	 * onlink prefix list when no addresses are configured with those
11962 	 * prefixes.
11963 	 */
11964 	if (ipif->ipif_isv6 &&
11965 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11966 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11967 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11968 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11969 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11970 		return (EINVAL);
11971 	}
11972 
11973 	/*
11974 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11975 	 * from being brought up.
11976 	 */
11977 	if (!ipif->ipif_isv6 &&
11978 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11979 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11980 		return (EINVAL);
11981 	}
11982 
11983 	/*
11984 	 * The only flag changes that we currently take specific action on
11985 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11986 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11987 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11988 	 * the flags and bringing it back up again.
11989 	 */
11990 	if ((turn_on|turn_off) &
11991 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11992 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11993 		/*
11994 		 * Taking this ipif down, make sure we have
11995 		 * valid net and subnet bcast ire's for other
11996 		 * logical interfaces, if we need them.
11997 		 */
11998 		if (!ipif->ipif_isv6)
11999 			ipif_check_bcast_ires(ipif);
12000 
12001 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
12002 		    !(turn_off & IPIF_UP)) {
12003 			need_up = B_TRUE;
12004 			if (ipif->ipif_flags & IPIF_UP)
12005 				ill->ill_logical_down = 1;
12006 			turn_on &= ~IPIF_UP;
12007 		}
12008 		err = ipif_down(ipif, q, mp);
12009 		ip1dbg(("ipif_down returns %d err ", err));
12010 		if (err == EINPROGRESS)
12011 			return (err);
12012 		ipif_down_tail(ipif);
12013 	}
12014 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
12015 }
12016 
12017 static int
12018 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
12019     boolean_t need_up)
12020 {
12021 	ill_t	*ill;
12022 	phyint_t *phyi;
12023 	uint64_t turn_on;
12024 	uint64_t turn_off;
12025 	uint64_t intf_flags;
12026 	boolean_t phyint_flags_modified = B_FALSE;
12027 	int	err = 0;
12028 	boolean_t set_linklocal = B_FALSE;
12029 	boolean_t zero_source = B_FALSE;
12030 
12031 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12032 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12033 
12034 	ASSERT(IAM_WRITER_IPIF(ipif));
12035 
12036 	ill = ipif->ipif_ill;
12037 	phyi = ill->ill_phyint;
12038 
12039 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12040 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12041 
12042 	turn_off = intf_flags & turn_on;
12043 	turn_on ^= turn_off;
12044 
12045 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12046 		phyint_flags_modified = B_TRUE;
12047 
12048 	/*
12049 	 * Now we change the flags. Track current value of
12050 	 * other flags in their respective places.
12051 	 */
12052 	mutex_enter(&ill->ill_lock);
12053 	mutex_enter(&phyi->phyint_lock);
12054 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12055 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12056 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12057 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12058 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12059 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12060 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12061 		set_linklocal = B_TRUE;
12062 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12063 	}
12064 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12065 		zero_source = B_TRUE;
12066 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12067 	}
12068 	mutex_exit(&ill->ill_lock);
12069 	mutex_exit(&phyi->phyint_lock);
12070 
12071 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12072 		ip_redo_nomination(phyi);
12073 
12074 	if (set_linklocal)
12075 		(void) ipif_setlinklocal(ipif);
12076 
12077 	if (zero_source)
12078 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12079 	else
12080 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12081 
12082 	if (need_up) {
12083 		/*
12084 		 * XXX ipif_up really does not know whether a phyint flags
12085 		 * was modified or not. So, it sends up information on
12086 		 * only one routing sockets message. As we don't bring up
12087 		 * the interface and also set STANDBY/FAILED simultaneously
12088 		 * it should be okay.
12089 		 */
12090 		err = ipif_up(ipif, q, mp);
12091 	} else {
12092 		/*
12093 		 * Make sure routing socket sees all changes to the flags.
12094 		 * ipif_up_done* handles this when we use ipif_up.
12095 		 */
12096 		if (phyint_flags_modified) {
12097 			if (phyi->phyint_illv4 != NULL) {
12098 				ip_rts_ifmsg(phyi->phyint_illv4->
12099 				    ill_ipif);
12100 			}
12101 			if (phyi->phyint_illv6 != NULL) {
12102 				ip_rts_ifmsg(phyi->phyint_illv6->
12103 				    ill_ipif);
12104 			}
12105 		} else {
12106 			ip_rts_ifmsg(ipif);
12107 		}
12108 		/*
12109 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12110 		 * this in need_up case.
12111 		 */
12112 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12113 	}
12114 	return (err);
12115 }
12116 
12117 /*
12118  * Restart entry point to restart the flags restart operation after the
12119  * refcounts have dropped to zero.
12120  */
12121 /* ARGSUSED */
12122 int
12123 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12124     ip_ioctl_cmd_t *ipip, void *if_req)
12125 {
12126 	int	err;
12127 	struct ifreq *ifr = (struct ifreq *)if_req;
12128 	struct lifreq *lifr = (struct lifreq *)if_req;
12129 
12130 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12131 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12132 
12133 	ipif_down_tail(ipif);
12134 	if (ipip->ipi_cmd_type == IF_CMD) {
12135 		/*
12136 		 * Since ip_sioctl_flags expects an int and ifr_flags
12137 		 * is a short we need to cast ifr_flags into an int
12138 		 * to avoid having sign extension cause bits to get
12139 		 * set that should not be.
12140 		 */
12141 		err = ip_sioctl_flags_tail(ipif,
12142 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
12143 		    q, mp, B_TRUE);
12144 	} else {
12145 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
12146 		    q, mp, B_TRUE);
12147 	}
12148 	return (err);
12149 }
12150 
12151 /*
12152  * Can operate on either a module or a driver queue.
12153  */
12154 /* ARGSUSED */
12155 int
12156 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12157     ip_ioctl_cmd_t *ipip, void *if_req)
12158 {
12159 	/*
12160 	 * Has the flags been set correctly till now ?
12161 	 */
12162 	ill_t *ill = ipif->ipif_ill;
12163 	phyint_t *phyi = ill->ill_phyint;
12164 
12165 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12166 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12167 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12168 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12169 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12170 
12171 	/*
12172 	 * Need a lock since some flags can be set even when there are
12173 	 * references to the ipif.
12174 	 */
12175 	mutex_enter(&ill->ill_lock);
12176 	if (ipip->ipi_cmd_type == IF_CMD) {
12177 		struct ifreq *ifr = (struct ifreq *)if_req;
12178 
12179 		/* Get interface flags (low 16 only). */
12180 		ifr->ifr_flags = ((ipif->ipif_flags |
12181 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12182 	} else {
12183 		struct lifreq *lifr = (struct lifreq *)if_req;
12184 
12185 		/* Get interface flags. */
12186 		lifr->lifr_flags = ipif->ipif_flags |
12187 		    ill->ill_flags | phyi->phyint_flags;
12188 	}
12189 	mutex_exit(&ill->ill_lock);
12190 	return (0);
12191 }
12192 
12193 /* ARGSUSED */
12194 int
12195 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12196     ip_ioctl_cmd_t *ipip, void *if_req)
12197 {
12198 	int mtu;
12199 	int ip_min_mtu;
12200 	struct ifreq	*ifr;
12201 	struct lifreq *lifr;
12202 	ire_t	*ire;
12203 	ip_stack_t *ipst;
12204 
12205 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12206 	    ipif->ipif_id, (void *)ipif));
12207 	if (ipip->ipi_cmd_type == IF_CMD) {
12208 		ifr = (struct ifreq *)if_req;
12209 		mtu = ifr->ifr_metric;
12210 	} else {
12211 		lifr = (struct lifreq *)if_req;
12212 		mtu = lifr->lifr_mtu;
12213 	}
12214 
12215 	if (ipif->ipif_isv6)
12216 		ip_min_mtu = IPV6_MIN_MTU;
12217 	else
12218 		ip_min_mtu = IP_MIN_MTU;
12219 
12220 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12221 		return (EINVAL);
12222 
12223 	/*
12224 	 * Change the MTU size in all relevant ire's.
12225 	 * Mtu change Vs. new ire creation - protocol below.
12226 	 * First change ipif_mtu and the ire_max_frag of the
12227 	 * interface ire. Then do an ire walk and change the
12228 	 * ire_max_frag of all affected ires. During ire_add
12229 	 * under the bucket lock, set the ire_max_frag of the
12230 	 * new ire being created from the ipif/ire from which
12231 	 * it is being derived. If an mtu change happens after
12232 	 * the ire is added, the new ire will be cleaned up.
12233 	 * Conversely if the mtu change happens before the ire
12234 	 * is added, ire_add will see the new value of the mtu.
12235 	 */
12236 	ipif->ipif_mtu = mtu;
12237 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12238 
12239 	if (ipif->ipif_isv6)
12240 		ire = ipif_to_ire_v6(ipif);
12241 	else
12242 		ire = ipif_to_ire(ipif);
12243 	if (ire != NULL) {
12244 		ire->ire_max_frag = ipif->ipif_mtu;
12245 		ire_refrele(ire);
12246 	}
12247 	ipst = ipif->ipif_ill->ill_ipst;
12248 	if (ipif->ipif_flags & IPIF_UP) {
12249 		if (ipif->ipif_isv6)
12250 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12251 			    ipst);
12252 		else
12253 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12254 			    ipst);
12255 	}
12256 	/* Update the MTU in SCTP's list */
12257 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12258 	return (0);
12259 }
12260 
12261 /* Get interface MTU. */
12262 /* ARGSUSED */
12263 int
12264 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12265 	ip_ioctl_cmd_t *ipip, void *if_req)
12266 {
12267 	struct ifreq	*ifr;
12268 	struct lifreq	*lifr;
12269 
12270 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12271 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12272 	if (ipip->ipi_cmd_type == IF_CMD) {
12273 		ifr = (struct ifreq *)if_req;
12274 		ifr->ifr_metric = ipif->ipif_mtu;
12275 	} else {
12276 		lifr = (struct lifreq *)if_req;
12277 		lifr->lifr_mtu = ipif->ipif_mtu;
12278 	}
12279 	return (0);
12280 }
12281 
12282 /* Set interface broadcast address. */
12283 /* ARGSUSED2 */
12284 int
12285 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12286 	ip_ioctl_cmd_t *ipip, void *if_req)
12287 {
12288 	ipaddr_t addr;
12289 	ire_t	*ire;
12290 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12291 
12292 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12293 	    ipif->ipif_id));
12294 
12295 	ASSERT(IAM_WRITER_IPIF(ipif));
12296 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12297 		return (EADDRNOTAVAIL);
12298 
12299 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12300 
12301 	if (sin->sin_family != AF_INET)
12302 		return (EAFNOSUPPORT);
12303 
12304 	addr = sin->sin_addr.s_addr;
12305 	if (ipif->ipif_flags & IPIF_UP) {
12306 		/*
12307 		 * If we are already up, make sure the new
12308 		 * broadcast address makes sense.  If it does,
12309 		 * there should be an IRE for it already.
12310 		 * Don't match on ipif, only on the ill
12311 		 * since we are sharing these now. Don't use
12312 		 * MATCH_IRE_ILL_GROUP as we are looking for
12313 		 * the broadcast ire on this ill and each ill
12314 		 * in the group has its own broadcast ire.
12315 		 */
12316 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12317 		    ipif, ALL_ZONES, NULL,
12318 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12319 		if (ire == NULL) {
12320 			return (EINVAL);
12321 		} else {
12322 			ire_refrele(ire);
12323 		}
12324 	}
12325 	/*
12326 	 * Changing the broadcast addr for this ipif.
12327 	 * Make sure we have valid net and subnet bcast
12328 	 * ire's for other logical interfaces, if needed.
12329 	 */
12330 	if (addr != ipif->ipif_brd_addr)
12331 		ipif_check_bcast_ires(ipif);
12332 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12333 	return (0);
12334 }
12335 
12336 /* Get interface broadcast address. */
12337 /* ARGSUSED */
12338 int
12339 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12340     ip_ioctl_cmd_t *ipip, void *if_req)
12341 {
12342 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12343 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12344 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12345 		return (EADDRNOTAVAIL);
12346 
12347 	/* IPIF_BROADCAST not possible with IPv6 */
12348 	ASSERT(!ipif->ipif_isv6);
12349 	*sin = sin_null;
12350 	sin->sin_family = AF_INET;
12351 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12352 	return (0);
12353 }
12354 
12355 /*
12356  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12357  */
12358 /* ARGSUSED */
12359 int
12360 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12361     ip_ioctl_cmd_t *ipip, void *if_req)
12362 {
12363 	int err = 0;
12364 	in6_addr_t v6mask;
12365 
12366 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12367 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12368 
12369 	ASSERT(IAM_WRITER_IPIF(ipif));
12370 
12371 	if (ipif->ipif_isv6) {
12372 		sin6_t *sin6;
12373 
12374 		if (sin->sin_family != AF_INET6)
12375 			return (EAFNOSUPPORT);
12376 
12377 		sin6 = (sin6_t *)sin;
12378 		v6mask = sin6->sin6_addr;
12379 	} else {
12380 		ipaddr_t mask;
12381 
12382 		if (sin->sin_family != AF_INET)
12383 			return (EAFNOSUPPORT);
12384 
12385 		mask = sin->sin_addr.s_addr;
12386 		V4MASK_TO_V6(mask, v6mask);
12387 	}
12388 
12389 	/*
12390 	 * No big deal if the interface isn't already up, or the mask
12391 	 * isn't really changing, or this is pt-pt.
12392 	 */
12393 	if (!(ipif->ipif_flags & IPIF_UP) ||
12394 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12395 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12396 		ipif->ipif_v6net_mask = v6mask;
12397 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12398 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12399 			    ipif->ipif_v6net_mask,
12400 			    ipif->ipif_v6subnet);
12401 		}
12402 		return (0);
12403 	}
12404 	/*
12405 	 * Make sure we have valid net and subnet broadcast ire's
12406 	 * for the old netmask, if needed by other logical interfaces.
12407 	 */
12408 	if (!ipif->ipif_isv6)
12409 		ipif_check_bcast_ires(ipif);
12410 
12411 	err = ipif_logical_down(ipif, q, mp);
12412 	if (err == EINPROGRESS)
12413 		return (err);
12414 	ipif_down_tail(ipif);
12415 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12416 	return (err);
12417 }
12418 
12419 static int
12420 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12421 {
12422 	in6_addr_t v6mask;
12423 	int err = 0;
12424 
12425 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12426 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12427 
12428 	if (ipif->ipif_isv6) {
12429 		sin6_t *sin6;
12430 
12431 		sin6 = (sin6_t *)sin;
12432 		v6mask = sin6->sin6_addr;
12433 	} else {
12434 		ipaddr_t mask;
12435 
12436 		mask = sin->sin_addr.s_addr;
12437 		V4MASK_TO_V6(mask, v6mask);
12438 	}
12439 
12440 	ipif->ipif_v6net_mask = v6mask;
12441 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12442 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12443 		    ipif->ipif_v6subnet);
12444 	}
12445 	err = ipif_up(ipif, q, mp);
12446 
12447 	if (err == 0 || err == EINPROGRESS) {
12448 		/*
12449 		 * The interface must be DL_BOUND if this packet has to
12450 		 * go out on the wire. Since we only go through a logical
12451 		 * down and are bound with the driver during an internal
12452 		 * down/up that is satisfied.
12453 		 */
12454 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12455 			/* Potentially broadcast an address mask reply. */
12456 			ipif_mask_reply(ipif);
12457 		}
12458 	}
12459 	return (err);
12460 }
12461 
12462 /* ARGSUSED */
12463 int
12464 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12465     ip_ioctl_cmd_t *ipip, void *if_req)
12466 {
12467 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12468 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12469 	ipif_down_tail(ipif);
12470 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12471 }
12472 
12473 /* Get interface net mask. */
12474 /* ARGSUSED */
12475 int
12476 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12477     ip_ioctl_cmd_t *ipip, void *if_req)
12478 {
12479 	struct lifreq *lifr = (struct lifreq *)if_req;
12480 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12481 
12482 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12483 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12484 
12485 	/*
12486 	 * net mask can't change since we have a reference to the ipif.
12487 	 */
12488 	if (ipif->ipif_isv6) {
12489 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12490 		*sin6 = sin6_null;
12491 		sin6->sin6_family = AF_INET6;
12492 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12493 		lifr->lifr_addrlen =
12494 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12495 	} else {
12496 		*sin = sin_null;
12497 		sin->sin_family = AF_INET;
12498 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12499 		if (ipip->ipi_cmd_type == LIF_CMD) {
12500 			lifr->lifr_addrlen =
12501 			    ip_mask_to_plen(ipif->ipif_net_mask);
12502 		}
12503 	}
12504 	return (0);
12505 }
12506 
12507 /* ARGSUSED */
12508 int
12509 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12510     ip_ioctl_cmd_t *ipip, void *if_req)
12511 {
12512 
12513 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12514 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12515 	/*
12516 	 * Set interface metric.  We don't use this for
12517 	 * anything but we keep track of it in case it is
12518 	 * important to routing applications or such.
12519 	 */
12520 	if (ipip->ipi_cmd_type == IF_CMD) {
12521 		struct ifreq    *ifr;
12522 
12523 		ifr = (struct ifreq *)if_req;
12524 		ipif->ipif_metric = ifr->ifr_metric;
12525 	} else {
12526 		struct lifreq   *lifr;
12527 
12528 		lifr = (struct lifreq *)if_req;
12529 		ipif->ipif_metric = lifr->lifr_metric;
12530 	}
12531 	return (0);
12532 }
12533 
12534 
12535 /* ARGSUSED */
12536 int
12537 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12538     ip_ioctl_cmd_t *ipip, void *if_req)
12539 {
12540 
12541 	/* Get interface metric. */
12542 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12543 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12544 	if (ipip->ipi_cmd_type == IF_CMD) {
12545 		struct ifreq    *ifr;
12546 
12547 		ifr = (struct ifreq *)if_req;
12548 		ifr->ifr_metric = ipif->ipif_metric;
12549 	} else {
12550 		struct lifreq   *lifr;
12551 
12552 		lifr = (struct lifreq *)if_req;
12553 		lifr->lifr_metric = ipif->ipif_metric;
12554 	}
12555 
12556 	return (0);
12557 }
12558 
12559 /* ARGSUSED */
12560 int
12561 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12562     ip_ioctl_cmd_t *ipip, void *if_req)
12563 {
12564 
12565 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12566 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12567 	/*
12568 	 * Set the muxid returned from I_PLINK.
12569 	 */
12570 	if (ipip->ipi_cmd_type == IF_CMD) {
12571 		struct ifreq *ifr = (struct ifreq *)if_req;
12572 
12573 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12574 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12575 	} else {
12576 		struct lifreq *lifr = (struct lifreq *)if_req;
12577 
12578 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12579 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12580 	}
12581 	return (0);
12582 }
12583 
12584 /* ARGSUSED */
12585 int
12586 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12587     ip_ioctl_cmd_t *ipip, void *if_req)
12588 {
12589 
12590 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12591 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12592 	/*
12593 	 * Get the muxid saved in ill for I_PUNLINK.
12594 	 */
12595 	if (ipip->ipi_cmd_type == IF_CMD) {
12596 		struct ifreq *ifr = (struct ifreq *)if_req;
12597 
12598 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12599 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12600 	} else {
12601 		struct lifreq *lifr = (struct lifreq *)if_req;
12602 
12603 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12604 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12605 	}
12606 	return (0);
12607 }
12608 
12609 /*
12610  * Set the subnet prefix. Does not modify the broadcast address.
12611  */
12612 /* ARGSUSED */
12613 int
12614 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12615     ip_ioctl_cmd_t *ipip, void *if_req)
12616 {
12617 	int err = 0;
12618 	in6_addr_t v6addr;
12619 	in6_addr_t v6mask;
12620 	boolean_t need_up = B_FALSE;
12621 	int addrlen;
12622 
12623 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12624 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12625 
12626 	ASSERT(IAM_WRITER_IPIF(ipif));
12627 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12628 
12629 	if (ipif->ipif_isv6) {
12630 		sin6_t *sin6;
12631 
12632 		if (sin->sin_family != AF_INET6)
12633 			return (EAFNOSUPPORT);
12634 
12635 		sin6 = (sin6_t *)sin;
12636 		v6addr = sin6->sin6_addr;
12637 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12638 			return (EADDRNOTAVAIL);
12639 	} else {
12640 		ipaddr_t addr;
12641 
12642 		if (sin->sin_family != AF_INET)
12643 			return (EAFNOSUPPORT);
12644 
12645 		addr = sin->sin_addr.s_addr;
12646 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12647 			return (EADDRNOTAVAIL);
12648 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12649 		/* Add 96 bits */
12650 		addrlen += IPV6_ABITS - IP_ABITS;
12651 	}
12652 
12653 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12654 		return (EINVAL);
12655 
12656 	/* Check if bits in the address is set past the mask */
12657 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12658 		return (EINVAL);
12659 
12660 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12661 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12662 		return (0);	/* No change */
12663 
12664 	if (ipif->ipif_flags & IPIF_UP) {
12665 		/*
12666 		 * If the interface is already marked up,
12667 		 * we call ipif_down which will take care
12668 		 * of ditching any IREs that have been set
12669 		 * up based on the old interface address.
12670 		 */
12671 		err = ipif_logical_down(ipif, q, mp);
12672 		if (err == EINPROGRESS)
12673 			return (err);
12674 		ipif_down_tail(ipif);
12675 		need_up = B_TRUE;
12676 	}
12677 
12678 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12679 	return (err);
12680 }
12681 
12682 static int
12683 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12684     queue_t *q, mblk_t *mp, boolean_t need_up)
12685 {
12686 	ill_t	*ill = ipif->ipif_ill;
12687 	int	err = 0;
12688 
12689 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12690 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12691 
12692 	/* Set the new address. */
12693 	mutex_enter(&ill->ill_lock);
12694 	ipif->ipif_v6net_mask = v6mask;
12695 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12696 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12697 		    ipif->ipif_v6subnet);
12698 	}
12699 	mutex_exit(&ill->ill_lock);
12700 
12701 	if (need_up) {
12702 		/*
12703 		 * Now bring the interface back up.  If this
12704 		 * is the only IPIF for the ILL, ipif_up
12705 		 * will have to re-bind to the device, so
12706 		 * we may get back EINPROGRESS, in which
12707 		 * case, this IOCTL will get completed in
12708 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12709 		 */
12710 		err = ipif_up(ipif, q, mp);
12711 		if (err == EINPROGRESS)
12712 			return (err);
12713 	}
12714 	return (err);
12715 }
12716 
12717 /* ARGSUSED */
12718 int
12719 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12720     ip_ioctl_cmd_t *ipip, void *if_req)
12721 {
12722 	int	addrlen;
12723 	in6_addr_t v6addr;
12724 	in6_addr_t v6mask;
12725 	struct lifreq *lifr = (struct lifreq *)if_req;
12726 
12727 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12728 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12729 	ipif_down_tail(ipif);
12730 
12731 	addrlen = lifr->lifr_addrlen;
12732 	if (ipif->ipif_isv6) {
12733 		sin6_t *sin6;
12734 
12735 		sin6 = (sin6_t *)sin;
12736 		v6addr = sin6->sin6_addr;
12737 	} else {
12738 		ipaddr_t addr;
12739 
12740 		addr = sin->sin_addr.s_addr;
12741 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12742 		addrlen += IPV6_ABITS - IP_ABITS;
12743 	}
12744 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12745 
12746 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12747 }
12748 
12749 /* ARGSUSED */
12750 int
12751 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12752     ip_ioctl_cmd_t *ipip, void *if_req)
12753 {
12754 	struct lifreq *lifr = (struct lifreq *)if_req;
12755 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12756 
12757 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12758 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12759 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12760 
12761 	if (ipif->ipif_isv6) {
12762 		*sin6 = sin6_null;
12763 		sin6->sin6_family = AF_INET6;
12764 		sin6->sin6_addr = ipif->ipif_v6subnet;
12765 		lifr->lifr_addrlen =
12766 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12767 	} else {
12768 		*sin = sin_null;
12769 		sin->sin_family = AF_INET;
12770 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12771 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12772 	}
12773 	return (0);
12774 }
12775 
12776 /*
12777  * Set the IPv6 address token.
12778  */
12779 /* ARGSUSED */
12780 int
12781 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12782     ip_ioctl_cmd_t *ipi, void *if_req)
12783 {
12784 	ill_t *ill = ipif->ipif_ill;
12785 	int err;
12786 	in6_addr_t v6addr;
12787 	in6_addr_t v6mask;
12788 	boolean_t need_up = B_FALSE;
12789 	int i;
12790 	sin6_t *sin6 = (sin6_t *)sin;
12791 	struct lifreq *lifr = (struct lifreq *)if_req;
12792 	int addrlen;
12793 
12794 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12795 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12796 	ASSERT(IAM_WRITER_IPIF(ipif));
12797 
12798 	addrlen = lifr->lifr_addrlen;
12799 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12800 	if (ipif->ipif_id != 0)
12801 		return (EINVAL);
12802 
12803 	if (!ipif->ipif_isv6)
12804 		return (EINVAL);
12805 
12806 	if (addrlen > IPV6_ABITS)
12807 		return (EINVAL);
12808 
12809 	v6addr = sin6->sin6_addr;
12810 
12811 	/*
12812 	 * The length of the token is the length from the end.  To get
12813 	 * the proper mask for this, compute the mask of the bits not
12814 	 * in the token; ie. the prefix, and then xor to get the mask.
12815 	 */
12816 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12817 		return (EINVAL);
12818 	for (i = 0; i < 4; i++) {
12819 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12820 	}
12821 
12822 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12823 	    ill->ill_token_length == addrlen)
12824 		return (0);	/* No change */
12825 
12826 	if (ipif->ipif_flags & IPIF_UP) {
12827 		err = ipif_logical_down(ipif, q, mp);
12828 		if (err == EINPROGRESS)
12829 			return (err);
12830 		ipif_down_tail(ipif);
12831 		need_up = B_TRUE;
12832 	}
12833 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12834 	return (err);
12835 }
12836 
12837 static int
12838 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12839     mblk_t *mp, boolean_t need_up)
12840 {
12841 	in6_addr_t v6addr;
12842 	in6_addr_t v6mask;
12843 	ill_t	*ill = ipif->ipif_ill;
12844 	int	i;
12845 	int	err = 0;
12846 
12847 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12848 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12849 	v6addr = sin6->sin6_addr;
12850 	/*
12851 	 * The length of the token is the length from the end.  To get
12852 	 * the proper mask for this, compute the mask of the bits not
12853 	 * in the token; ie. the prefix, and then xor to get the mask.
12854 	 */
12855 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12856 	for (i = 0; i < 4; i++)
12857 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12858 
12859 	mutex_enter(&ill->ill_lock);
12860 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12861 	ill->ill_token_length = addrlen;
12862 	mutex_exit(&ill->ill_lock);
12863 
12864 	if (need_up) {
12865 		/*
12866 		 * Now bring the interface back up.  If this
12867 		 * is the only IPIF for the ILL, ipif_up
12868 		 * will have to re-bind to the device, so
12869 		 * we may get back EINPROGRESS, in which
12870 		 * case, this IOCTL will get completed in
12871 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12872 		 */
12873 		err = ipif_up(ipif, q, mp);
12874 		if (err == EINPROGRESS)
12875 			return (err);
12876 	}
12877 	return (err);
12878 }
12879 
12880 /* ARGSUSED */
12881 int
12882 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12883     ip_ioctl_cmd_t *ipi, void *if_req)
12884 {
12885 	ill_t *ill;
12886 	sin6_t *sin6 = (sin6_t *)sin;
12887 	struct lifreq *lifr = (struct lifreq *)if_req;
12888 
12889 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12890 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12891 	if (ipif->ipif_id != 0)
12892 		return (EINVAL);
12893 
12894 	ill = ipif->ipif_ill;
12895 	if (!ill->ill_isv6)
12896 		return (ENXIO);
12897 
12898 	*sin6 = sin6_null;
12899 	sin6->sin6_family = AF_INET6;
12900 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12901 	sin6->sin6_addr = ill->ill_token;
12902 	lifr->lifr_addrlen = ill->ill_token_length;
12903 	return (0);
12904 }
12905 
12906 /*
12907  * Set (hardware) link specific information that might override
12908  * what was acquired through the DL_INFO_ACK.
12909  * The logic is as follows.
12910  *
12911  * become exclusive
12912  * set CHANGING flag
12913  * change mtu on affected IREs
12914  * clear CHANGING flag
12915  *
12916  * An ire add that occurs before the CHANGING flag is set will have its mtu
12917  * changed by the ip_sioctl_lnkinfo.
12918  *
12919  * During the time the CHANGING flag is set, no new ires will be added to the
12920  * bucket, and ire add will fail (due the CHANGING flag).
12921  *
12922  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12923  * before it is added to the bucket.
12924  *
12925  * Obviously only 1 thread can set the CHANGING flag and we need to become
12926  * exclusive to set the flag.
12927  */
12928 /* ARGSUSED */
12929 int
12930 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12931     ip_ioctl_cmd_t *ipi, void *if_req)
12932 {
12933 	ill_t		*ill = ipif->ipif_ill;
12934 	ipif_t		*nipif;
12935 	int		ip_min_mtu;
12936 	boolean_t	mtu_walk = B_FALSE;
12937 	struct lifreq	*lifr = (struct lifreq *)if_req;
12938 	lif_ifinfo_req_t *lir;
12939 	ire_t		*ire;
12940 
12941 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12942 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12943 	lir = &lifr->lifr_ifinfo;
12944 	ASSERT(IAM_WRITER_IPIF(ipif));
12945 
12946 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12947 	if (ipif->ipif_id != 0)
12948 		return (EINVAL);
12949 
12950 	/* Set interface MTU. */
12951 	if (ipif->ipif_isv6)
12952 		ip_min_mtu = IPV6_MIN_MTU;
12953 	else
12954 		ip_min_mtu = IP_MIN_MTU;
12955 
12956 	/*
12957 	 * Verify values before we set anything. Allow zero to
12958 	 * mean unspecified.
12959 	 */
12960 	if (lir->lir_maxmtu != 0 &&
12961 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12962 	    lir->lir_maxmtu < ip_min_mtu))
12963 		return (EINVAL);
12964 	if (lir->lir_reachtime != 0 &&
12965 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12966 		return (EINVAL);
12967 	if (lir->lir_reachretrans != 0 &&
12968 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12969 		return (EINVAL);
12970 
12971 	mutex_enter(&ill->ill_lock);
12972 	ill->ill_state_flags |= ILL_CHANGING;
12973 	for (nipif = ill->ill_ipif; nipif != NULL;
12974 	    nipif = nipif->ipif_next) {
12975 		nipif->ipif_state_flags |= IPIF_CHANGING;
12976 	}
12977 
12978 	mutex_exit(&ill->ill_lock);
12979 
12980 	if (lir->lir_maxmtu != 0) {
12981 		ill->ill_max_mtu = lir->lir_maxmtu;
12982 		ill->ill_mtu_userspecified = 1;
12983 		mtu_walk = B_TRUE;
12984 	}
12985 
12986 	if (lir->lir_reachtime != 0)
12987 		ill->ill_reachable_time = lir->lir_reachtime;
12988 
12989 	if (lir->lir_reachretrans != 0)
12990 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12991 
12992 	ill->ill_max_hops = lir->lir_maxhops;
12993 
12994 	ill->ill_max_buf = ND_MAX_Q;
12995 
12996 	if (mtu_walk) {
12997 		/*
12998 		 * Set the MTU on all ipifs associated with this ill except
12999 		 * for those whose MTU was fixed via SIOCSLIFMTU.
13000 		 */
13001 		for (nipif = ill->ill_ipif; nipif != NULL;
13002 		    nipif = nipif->ipif_next) {
13003 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
13004 				continue;
13005 
13006 			nipif->ipif_mtu = ill->ill_max_mtu;
13007 
13008 			if (!(nipif->ipif_flags & IPIF_UP))
13009 				continue;
13010 
13011 			if (nipif->ipif_isv6)
13012 				ire = ipif_to_ire_v6(nipif);
13013 			else
13014 				ire = ipif_to_ire(nipif);
13015 			if (ire != NULL) {
13016 				ire->ire_max_frag = ipif->ipif_mtu;
13017 				ire_refrele(ire);
13018 			}
13019 			if (ill->ill_isv6) {
13020 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
13021 				    ipif_mtu_change, (char *)nipif,
13022 				    ill);
13023 			} else {
13024 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
13025 				    ipif_mtu_change, (char *)nipif,
13026 				    ill);
13027 			}
13028 		}
13029 	}
13030 
13031 	mutex_enter(&ill->ill_lock);
13032 	for (nipif = ill->ill_ipif; nipif != NULL;
13033 	    nipif = nipif->ipif_next) {
13034 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
13035 	}
13036 	ILL_UNMARK_CHANGING(ill);
13037 	mutex_exit(&ill->ill_lock);
13038 
13039 	return (0);
13040 }
13041 
13042 /* ARGSUSED */
13043 int
13044 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13045     ip_ioctl_cmd_t *ipi, void *if_req)
13046 {
13047 	struct lif_ifinfo_req *lir;
13048 	ill_t *ill = ipif->ipif_ill;
13049 
13050 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13051 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13052 	if (ipif->ipif_id != 0)
13053 		return (EINVAL);
13054 
13055 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13056 	lir->lir_maxhops = ill->ill_max_hops;
13057 	lir->lir_reachtime = ill->ill_reachable_time;
13058 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13059 	lir->lir_maxmtu = ill->ill_max_mtu;
13060 
13061 	return (0);
13062 }
13063 
13064 /*
13065  * Return best guess as to the subnet mask for the specified address.
13066  * Based on the subnet masks for all the configured interfaces.
13067  *
13068  * We end up returning a zero mask in the case of default, multicast or
13069  * experimental.
13070  */
13071 static ipaddr_t
13072 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13073 {
13074 	ipaddr_t net_mask;
13075 	ill_t	*ill;
13076 	ipif_t	*ipif;
13077 	ill_walk_context_t ctx;
13078 	ipif_t	*fallback_ipif = NULL;
13079 
13080 	net_mask = ip_net_mask(addr);
13081 	if (net_mask == 0) {
13082 		*ipifp = NULL;
13083 		return (0);
13084 	}
13085 
13086 	/* Let's check to see if this is maybe a local subnet route. */
13087 	/* this function only applies to IPv4 interfaces */
13088 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13089 	ill = ILL_START_WALK_V4(&ctx, ipst);
13090 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13091 		mutex_enter(&ill->ill_lock);
13092 		for (ipif = ill->ill_ipif; ipif != NULL;
13093 		    ipif = ipif->ipif_next) {
13094 			if (!IPIF_CAN_LOOKUP(ipif))
13095 				continue;
13096 			if (!(ipif->ipif_flags & IPIF_UP))
13097 				continue;
13098 			if ((ipif->ipif_subnet & net_mask) ==
13099 			    (addr & net_mask)) {
13100 				/*
13101 				 * Don't trust pt-pt interfaces if there are
13102 				 * other interfaces.
13103 				 */
13104 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13105 					if (fallback_ipif == NULL) {
13106 						ipif_refhold_locked(ipif);
13107 						fallback_ipif = ipif;
13108 					}
13109 					continue;
13110 				}
13111 
13112 				/*
13113 				 * Fine. Just assume the same net mask as the
13114 				 * directly attached subnet interface is using.
13115 				 */
13116 				ipif_refhold_locked(ipif);
13117 				mutex_exit(&ill->ill_lock);
13118 				rw_exit(&ipst->ips_ill_g_lock);
13119 				if (fallback_ipif != NULL)
13120 					ipif_refrele(fallback_ipif);
13121 				*ipifp = ipif;
13122 				return (ipif->ipif_net_mask);
13123 			}
13124 		}
13125 		mutex_exit(&ill->ill_lock);
13126 	}
13127 	rw_exit(&ipst->ips_ill_g_lock);
13128 
13129 	*ipifp = fallback_ipif;
13130 	return ((fallback_ipif != NULL) ?
13131 	    fallback_ipif->ipif_net_mask : net_mask);
13132 }
13133 
13134 /*
13135  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13136  */
13137 static void
13138 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13139 {
13140 	IOCP	iocp;
13141 	ipft_t	*ipft;
13142 	ipllc_t	*ipllc;
13143 	mblk_t	*mp1;
13144 	cred_t	*cr;
13145 	int	error = 0;
13146 	conn_t	*connp;
13147 
13148 	ip1dbg(("ip_wput_ioctl"));
13149 	iocp = (IOCP)mp->b_rptr;
13150 	mp1 = mp->b_cont;
13151 	if (mp1 == NULL) {
13152 		iocp->ioc_error = EINVAL;
13153 		mp->b_datap->db_type = M_IOCNAK;
13154 		iocp->ioc_count = 0;
13155 		qreply(q, mp);
13156 		return;
13157 	}
13158 
13159 	/*
13160 	 * These IOCTLs provide various control capabilities to
13161 	 * upstream agents such as ULPs and processes.	There
13162 	 * are currently two such IOCTLs implemented.  They
13163 	 * are used by TCP to provide update information for
13164 	 * existing IREs and to forcibly delete an IRE for a
13165 	 * host that is not responding, thereby forcing an
13166 	 * attempt at a new route.
13167 	 */
13168 	iocp->ioc_error = EINVAL;
13169 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13170 		goto done;
13171 
13172 	ipllc = (ipllc_t *)mp1->b_rptr;
13173 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13174 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13175 			break;
13176 	}
13177 	/*
13178 	 * prefer credential from mblk over ioctl;
13179 	 * see ip_sioctl_copyin_setup
13180 	 */
13181 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13182 
13183 	/*
13184 	 * Refhold the conn in case the request gets queued up in some lookup
13185 	 */
13186 	ASSERT(CONN_Q(q));
13187 	connp = Q_TO_CONN(q);
13188 	CONN_INC_REF(connp);
13189 	if (ipft->ipft_pfi &&
13190 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13191 	    pullupmsg(mp1, ipft->ipft_min_size))) {
13192 		error = (*ipft->ipft_pfi)(q,
13193 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13194 	}
13195 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13196 		/*
13197 		 * CONN_OPER_PENDING_DONE happens in the function called
13198 		 * through ipft_pfi above.
13199 		 */
13200 		return;
13201 	}
13202 
13203 	CONN_OPER_PENDING_DONE(connp);
13204 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13205 		freemsg(mp);
13206 		return;
13207 	}
13208 	iocp->ioc_error = error;
13209 
13210 done:
13211 	mp->b_datap->db_type = M_IOCACK;
13212 	if (iocp->ioc_error)
13213 		iocp->ioc_count = 0;
13214 	qreply(q, mp);
13215 }
13216 
13217 /*
13218  * Lookup an ipif using the sequence id (ipif_seqid)
13219  */
13220 ipif_t *
13221 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13222 {
13223 	ipif_t *ipif;
13224 
13225 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13226 
13227 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13228 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13229 			return (ipif);
13230 	}
13231 	return (NULL);
13232 }
13233 
13234 /*
13235  * Assign a unique id for the ipif. This is used later when we send
13236  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13237  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13238  * IRE is added, we verify that ipif has not disappeared.
13239  */
13240 
13241 static void
13242 ipif_assign_seqid(ipif_t *ipif)
13243 {
13244 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13245 
13246 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13247 }
13248 
13249 /*
13250  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13251  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13252  * be inserted into the first space available in the list. The value of
13253  * ipif_id will then be set to the appropriate value for its position.
13254  */
13255 static int
13256 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13257 {
13258 	ill_t *ill;
13259 	ipif_t *tipif;
13260 	ipif_t **tipifp;
13261 	int id;
13262 	ip_stack_t	*ipst;
13263 
13264 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13265 	    IAM_WRITER_IPIF(ipif));
13266 
13267 	ill = ipif->ipif_ill;
13268 	ASSERT(ill != NULL);
13269 	ipst = ill->ill_ipst;
13270 
13271 	/*
13272 	 * In the case of lo0:0 we already hold the ill_g_lock.
13273 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13274 	 * ipif_insert. Another such caller is ipif_move.
13275 	 */
13276 	if (acquire_g_lock)
13277 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13278 	if (acquire_ill_lock)
13279 		mutex_enter(&ill->ill_lock);
13280 	id = ipif->ipif_id;
13281 	tipifp = &(ill->ill_ipif);
13282 	if (id == -1) {	/* need to find a real id */
13283 		id = 0;
13284 		while ((tipif = *tipifp) != NULL) {
13285 			ASSERT(tipif->ipif_id >= id);
13286 			if (tipif->ipif_id != id)
13287 				break; /* non-consecutive id */
13288 			id++;
13289 			tipifp = &(tipif->ipif_next);
13290 		}
13291 		/* limit number of logical interfaces */
13292 		if (id >= ipst->ips_ip_addrs_per_if) {
13293 			if (acquire_ill_lock)
13294 				mutex_exit(&ill->ill_lock);
13295 			if (acquire_g_lock)
13296 				rw_exit(&ipst->ips_ill_g_lock);
13297 			return (-1);
13298 		}
13299 		ipif->ipif_id = id; /* assign new id */
13300 	} else if (id < ipst->ips_ip_addrs_per_if) {
13301 		/* we have a real id; insert ipif in the right place */
13302 		while ((tipif = *tipifp) != NULL) {
13303 			ASSERT(tipif->ipif_id != id);
13304 			if (tipif->ipif_id > id)
13305 				break; /* found correct location */
13306 			tipifp = &(tipif->ipif_next);
13307 		}
13308 	} else {
13309 		if (acquire_ill_lock)
13310 			mutex_exit(&ill->ill_lock);
13311 		if (acquire_g_lock)
13312 			rw_exit(&ipst->ips_ill_g_lock);
13313 		return (-1);
13314 	}
13315 
13316 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13317 
13318 	ipif->ipif_next = tipif;
13319 	*tipifp = ipif;
13320 	if (acquire_ill_lock)
13321 		mutex_exit(&ill->ill_lock);
13322 	if (acquire_g_lock)
13323 		rw_exit(&ipst->ips_ill_g_lock);
13324 	return (0);
13325 }
13326 
13327 static void
13328 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13329 {
13330 	ipif_t	**ipifp;
13331 	ill_t	*ill = ipif->ipif_ill;
13332 
13333 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13334 	if (acquire_ill_lock)
13335 		mutex_enter(&ill->ill_lock);
13336 	else
13337 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13338 
13339 	ipifp = &ill->ill_ipif;
13340 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13341 		if (*ipifp == ipif) {
13342 			*ipifp = ipif->ipif_next;
13343 			break;
13344 		}
13345 	}
13346 
13347 	if (acquire_ill_lock)
13348 		mutex_exit(&ill->ill_lock);
13349 }
13350 
13351 /*
13352  * Allocate and initialize a new interface control structure.  (Always
13353  * called as writer.)
13354  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13355  * is not part of the global linked list of ills. ipif_seqid is unique
13356  * in the system and to preserve the uniqueness, it is assigned only
13357  * when ill becomes part of the global list. At that point ill will
13358  * have a name. If it doesn't get assigned here, it will get assigned
13359  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13360  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13361  * the interface flags or any other information from the DL_INFO_ACK for
13362  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13363  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13364  * second DL_INFO_ACK comes in from the driver.
13365  */
13366 static ipif_t *
13367 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13368 {
13369 	ipif_t	*ipif;
13370 	phyint_t *phyi;
13371 
13372 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13373 	    ill->ill_name, id, (void *)ill));
13374 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13375 
13376 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13377 		return (NULL);
13378 	*ipif = ipif_zero;	/* start clean */
13379 
13380 	ipif->ipif_ill = ill;
13381 	ipif->ipif_id = id;	/* could be -1 */
13382 	/*
13383 	 * Inherit the zoneid from the ill; for the shared stack instance
13384 	 * this is always the global zone
13385 	 */
13386 	ipif->ipif_zoneid = ill->ill_zoneid;
13387 
13388 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13389 
13390 	ipif->ipif_refcnt = 0;
13391 	ipif->ipif_saved_ire_cnt = 0;
13392 
13393 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13394 		mi_free(ipif);
13395 		return (NULL);
13396 	}
13397 	/* -1 id should have been replaced by real id */
13398 	id = ipif->ipif_id;
13399 	ASSERT(id >= 0);
13400 
13401 	if (ill->ill_name[0] != '\0')
13402 		ipif_assign_seqid(ipif);
13403 
13404 	/*
13405 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13406 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13407 	 * ioctl sets ipif_orig_ipifid to zero.
13408 	 */
13409 	ipif->ipif_orig_ipifid = id;
13410 
13411 	/*
13412 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13413 	 * The ipif is still not up and can't be looked up until the
13414 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13415 	 */
13416 	mutex_enter(&ill->ill_lock);
13417 	mutex_enter(&ill->ill_phyint->phyint_lock);
13418 	/*
13419 	 * Set the running flag when logical interface zero is created.
13420 	 * For subsequent logical interfaces, a DLPI link down
13421 	 * notification message may have cleared the running flag to
13422 	 * indicate the link is down, so we shouldn't just blindly set it.
13423 	 */
13424 	if (id == 0)
13425 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13426 	ipif->ipif_ire_type = ire_type;
13427 	phyi = ill->ill_phyint;
13428 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13429 
13430 	if (ipif->ipif_isv6) {
13431 		ill->ill_flags |= ILLF_IPV6;
13432 	} else {
13433 		ipaddr_t inaddr_any = INADDR_ANY;
13434 
13435 		ill->ill_flags |= ILLF_IPV4;
13436 
13437 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13438 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13439 		    &ipif->ipif_v6lcl_addr);
13440 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13441 		    &ipif->ipif_v6src_addr);
13442 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13443 		    &ipif->ipif_v6subnet);
13444 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13445 		    &ipif->ipif_v6net_mask);
13446 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13447 		    &ipif->ipif_v6brd_addr);
13448 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13449 		    &ipif->ipif_v6pp_dst_addr);
13450 	}
13451 
13452 	/*
13453 	 * Don't set the interface flags etc. now, will do it in
13454 	 * ip_ll_subnet_defaults.
13455 	 */
13456 	if (!initialize) {
13457 		mutex_exit(&ill->ill_lock);
13458 		mutex_exit(&ill->ill_phyint->phyint_lock);
13459 		return (ipif);
13460 	}
13461 	ipif->ipif_mtu = ill->ill_max_mtu;
13462 
13463 	if (ill->ill_bcast_addr_length != 0) {
13464 		/*
13465 		 * Later detect lack of DLPI driver multicast
13466 		 * capability by catching DL_ENABMULTI errors in
13467 		 * ip_rput_dlpi.
13468 		 */
13469 		ill->ill_flags |= ILLF_MULTICAST;
13470 		if (!ipif->ipif_isv6)
13471 			ipif->ipif_flags |= IPIF_BROADCAST;
13472 	} else {
13473 		if (ill->ill_net_type != IRE_LOOPBACK) {
13474 			if (ipif->ipif_isv6)
13475 				/*
13476 				 * Note: xresolv interfaces will eventually need
13477 				 * NOARP set here as well, but that will require
13478 				 * those external resolvers to have some
13479 				 * knowledge of that flag and act appropriately.
13480 				 * Not to be changed at present.
13481 				 */
13482 				ill->ill_flags |= ILLF_NONUD;
13483 			else
13484 				ill->ill_flags |= ILLF_NOARP;
13485 		}
13486 		if (ill->ill_phys_addr_length == 0) {
13487 			if (ill->ill_media &&
13488 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13489 				ipif->ipif_flags |= IPIF_NOXMIT;
13490 				phyi->phyint_flags |= PHYI_VIRTUAL;
13491 			} else {
13492 				/* pt-pt supports multicast. */
13493 				ill->ill_flags |= ILLF_MULTICAST;
13494 				if (ill->ill_net_type == IRE_LOOPBACK) {
13495 					phyi->phyint_flags |=
13496 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13497 				} else {
13498 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13499 				}
13500 			}
13501 		}
13502 	}
13503 	mutex_exit(&ill->ill_lock);
13504 	mutex_exit(&ill->ill_phyint->phyint_lock);
13505 	return (ipif);
13506 }
13507 
13508 /*
13509  * If appropriate, send a message up to the resolver delete the entry
13510  * for the address of this interface which is going out of business.
13511  * (Always called as writer).
13512  *
13513  * NOTE : We need to check for NULL mps as some of the fields are
13514  *	  initialized only for some interface types. See ipif_resolver_up()
13515  *	  for details.
13516  */
13517 void
13518 ipif_arp_down(ipif_t *ipif)
13519 {
13520 	mblk_t	*mp;
13521 	ill_t	*ill = ipif->ipif_ill;
13522 
13523 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13524 	ASSERT(IAM_WRITER_IPIF(ipif));
13525 
13526 	/* Delete the mapping for the local address */
13527 	mp = ipif->ipif_arp_del_mp;
13528 	if (mp != NULL) {
13529 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13530 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13531 		putnext(ill->ill_rq, mp);
13532 		ipif->ipif_arp_del_mp = NULL;
13533 	}
13534 
13535 	/*
13536 	 * If this is the last ipif that is going down and there are no
13537 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13538 	 * clean up ARP completely.
13539 	 */
13540 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13541 
13542 		/* Send up AR_INTERFACE_DOWN message */
13543 		mp = ill->ill_arp_down_mp;
13544 		if (mp != NULL) {
13545 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13546 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13547 			    ipif->ipif_id));
13548 			putnext(ill->ill_rq, mp);
13549 			ill->ill_arp_down_mp = NULL;
13550 		}
13551 
13552 		/* Tell ARP to delete the multicast mappings */
13553 		mp = ill->ill_arp_del_mapping_mp;
13554 		if (mp != NULL) {
13555 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13556 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13557 			    ipif->ipif_id));
13558 			putnext(ill->ill_rq, mp);
13559 			ill->ill_arp_del_mapping_mp = NULL;
13560 		}
13561 	}
13562 }
13563 
13564 /*
13565  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13566  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13567  * that it wants the add_mp allocated in this function to be returned
13568  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13569  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13570  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13571  * as it does a ipif_arp_down after calling this function - which will
13572  * remove what we add here.
13573  *
13574  * Returns -1 on failures and 0 on success.
13575  */
13576 int
13577 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13578 {
13579 	mblk_t	*del_mp = NULL;
13580 	mblk_t *add_mp = NULL;
13581 	mblk_t *mp;
13582 	ill_t	*ill = ipif->ipif_ill;
13583 	phyint_t *phyi = ill->ill_phyint;
13584 	ipaddr_t addr, mask, extract_mask = 0;
13585 	arma_t	*arma;
13586 	uint8_t *maddr, *bphys_addr;
13587 	uint32_t hw_start;
13588 	dl_unitdata_req_t *dlur;
13589 
13590 	ASSERT(IAM_WRITER_IPIF(ipif));
13591 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13592 		return (0);
13593 
13594 	/*
13595 	 * Delete the existing mapping from ARP. Normally ipif_down
13596 	 * -> ipif_arp_down should send this up to ARP. The only
13597 	 * reason we would find this when we are switching from
13598 	 * Multicast to Broadcast where we did not do a down.
13599 	 */
13600 	mp = ill->ill_arp_del_mapping_mp;
13601 	if (mp != NULL) {
13602 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13603 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13604 		putnext(ill->ill_rq, mp);
13605 		ill->ill_arp_del_mapping_mp = NULL;
13606 	}
13607 
13608 	if (arp_add_mapping_mp != NULL)
13609 		*arp_add_mapping_mp = NULL;
13610 
13611 	/*
13612 	 * Check that the address is not to long for the constant
13613 	 * length reserved in the template arma_t.
13614 	 */
13615 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13616 		return (-1);
13617 
13618 	/* Add mapping mblk */
13619 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13620 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13621 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13622 	    (caddr_t)&addr);
13623 	if (add_mp == NULL)
13624 		return (-1);
13625 	arma = (arma_t *)add_mp->b_rptr;
13626 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13627 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13628 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13629 
13630 	/*
13631 	 * Determine the broadcast address.
13632 	 */
13633 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13634 	if (ill->ill_sap_length < 0)
13635 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13636 	else
13637 		bphys_addr = (uchar_t *)dlur +
13638 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13639 	/*
13640 	 * Check PHYI_MULTI_BCAST and length of physical
13641 	 * address to determine if we use the mapping or the
13642 	 * broadcast address.
13643 	 */
13644 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13645 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13646 		    bphys_addr, maddr, &hw_start, &extract_mask))
13647 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13648 
13649 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13650 	    (ill->ill_flags & ILLF_MULTICAST)) {
13651 		/* Make sure this will not match the "exact" entry. */
13652 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13653 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13654 		    (caddr_t)&addr);
13655 		if (del_mp == NULL) {
13656 			freemsg(add_mp);
13657 			return (-1);
13658 		}
13659 		bcopy(&extract_mask, (char *)arma +
13660 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13661 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13662 			/* Use link-layer broadcast address for MULTI_BCAST */
13663 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13664 			ip2dbg(("ipif_arp_setup_multicast: adding"
13665 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13666 		} else {
13667 			arma->arma_hw_mapping_start = hw_start;
13668 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13669 			    " ARP setup for %s\n", ill->ill_name));
13670 		}
13671 	} else {
13672 		freemsg(add_mp);
13673 		ASSERT(del_mp == NULL);
13674 		/* It is neither MULTICAST nor MULTI_BCAST */
13675 		return (0);
13676 	}
13677 	ASSERT(add_mp != NULL && del_mp != NULL);
13678 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13679 	ill->ill_arp_del_mapping_mp = del_mp;
13680 	if (arp_add_mapping_mp != NULL) {
13681 		/* The caller just wants the mblks allocated */
13682 		*arp_add_mapping_mp = add_mp;
13683 	} else {
13684 		/* The caller wants us to send it to arp */
13685 		putnext(ill->ill_rq, add_mp);
13686 	}
13687 	return (0);
13688 }
13689 
13690 /*
13691  * Get the resolver set up for a new interface address.
13692  * (Always called as writer.)
13693  * Called both for IPv4 and IPv6 interfaces,
13694  * though it only sets up the resolver for v6
13695  * if it's an xresolv interface (one using an external resolver).
13696  * Honors ILLF_NOARP.
13697  * The enumerated value res_act is used to tune the behavior.
13698  * If set to Res_act_initial, then we set up all the resolver
13699  * structures for a new interface.  If set to Res_act_move, then
13700  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13701  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13702  * asynchronous hardware address change notification.  If set to
13703  * Res_act_defend, then we tell ARP that it needs to send a single
13704  * gratuitous message in defense of the address.
13705  * Returns error on failure.
13706  */
13707 int
13708 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13709 {
13710 	caddr_t	addr;
13711 	mblk_t	*arp_up_mp = NULL;
13712 	mblk_t	*arp_down_mp = NULL;
13713 	mblk_t	*arp_add_mp = NULL;
13714 	mblk_t	*arp_del_mp = NULL;
13715 	mblk_t	*arp_add_mapping_mp = NULL;
13716 	mblk_t	*arp_del_mapping_mp = NULL;
13717 	ill_t	*ill = ipif->ipif_ill;
13718 	uchar_t	*area_p = NULL;
13719 	uchar_t	*ared_p = NULL;
13720 	int	err = ENOMEM;
13721 	boolean_t was_dup;
13722 
13723 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13724 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13725 	ASSERT(IAM_WRITER_IPIF(ipif));
13726 
13727 	was_dup = B_FALSE;
13728 	if (res_act == Res_act_initial) {
13729 		ipif->ipif_addr_ready = 0;
13730 		/*
13731 		 * We're bringing an interface up here.  There's no way that we
13732 		 * should need to shut down ARP now.
13733 		 */
13734 		mutex_enter(&ill->ill_lock);
13735 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13736 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13737 			ill->ill_ipif_dup_count--;
13738 			was_dup = B_TRUE;
13739 		}
13740 		mutex_exit(&ill->ill_lock);
13741 	}
13742 	if (ipif->ipif_recovery_id != 0)
13743 		(void) untimeout(ipif->ipif_recovery_id);
13744 	ipif->ipif_recovery_id = 0;
13745 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13746 		ipif->ipif_addr_ready = 1;
13747 		return (0);
13748 	}
13749 	/* NDP will set the ipif_addr_ready flag when it's ready */
13750 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13751 		return (0);
13752 
13753 	if (ill->ill_isv6) {
13754 		/*
13755 		 * External resolver for IPv6
13756 		 */
13757 		ASSERT(res_act == Res_act_initial);
13758 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13759 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13760 			area_p = (uchar_t *)&ip6_area_template;
13761 			ared_p = (uchar_t *)&ip6_ared_template;
13762 		}
13763 	} else {
13764 		/*
13765 		 * IPv4 arp case. If the ARP stream has already started
13766 		 * closing, fail this request for ARP bringup. Else
13767 		 * record the fact that an ARP bringup is pending.
13768 		 */
13769 		mutex_enter(&ill->ill_lock);
13770 		if (ill->ill_arp_closing) {
13771 			mutex_exit(&ill->ill_lock);
13772 			err = EINVAL;
13773 			goto failed;
13774 		} else {
13775 			if (ill->ill_ipif_up_count == 0 &&
13776 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13777 				ill->ill_arp_bringup_pending = 1;
13778 			mutex_exit(&ill->ill_lock);
13779 		}
13780 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13781 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13782 			area_p = (uchar_t *)&ip_area_template;
13783 			ared_p = (uchar_t *)&ip_ared_template;
13784 		}
13785 	}
13786 
13787 	/*
13788 	 * Add an entry for the local address in ARP only if it
13789 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13790 	 */
13791 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13792 		area_t *area;
13793 
13794 		/* Now ask ARP to publish our address. */
13795 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13796 		if (arp_add_mp == NULL)
13797 			goto failed;
13798 		area = (area_t *)arp_add_mp->b_rptr;
13799 		if (res_act != Res_act_initial) {
13800 			/*
13801 			 * Copy the new hardware address and length into
13802 			 * arp_add_mp to be sent to ARP.
13803 			 */
13804 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13805 			bcopy(ill->ill_phys_addr,
13806 			    ((char *)area + area->area_hw_addr_offset),
13807 			    area->area_hw_addr_length);
13808 		}
13809 
13810 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13811 		    ACE_F_MYADDR;
13812 
13813 		if (res_act == Res_act_defend) {
13814 			area->area_flags |= ACE_F_DEFEND;
13815 			/*
13816 			 * If we're just defending our address now, then
13817 			 * there's no need to set up ARP multicast mappings.
13818 			 * The publish command is enough.
13819 			 */
13820 			goto done;
13821 		}
13822 
13823 		if (res_act != Res_act_initial)
13824 			goto arp_setup_multicast;
13825 
13826 		/*
13827 		 * Allocate an ARP deletion message so we know we can tell ARP
13828 		 * when the interface goes down.
13829 		 */
13830 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13831 		if (arp_del_mp == NULL)
13832 			goto failed;
13833 
13834 	} else {
13835 		if (res_act != Res_act_initial)
13836 			goto done;
13837 	}
13838 	/*
13839 	 * Need to bring up ARP or setup multicast mapping only
13840 	 * when the first interface is coming UP.
13841 	 */
13842 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13843 	    was_dup) {
13844 		goto done;
13845 	}
13846 
13847 	/*
13848 	 * Allocate an ARP down message (to be saved) and an ARP up
13849 	 * message.
13850 	 */
13851 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13852 	if (arp_down_mp == NULL)
13853 		goto failed;
13854 
13855 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13856 	if (arp_up_mp == NULL)
13857 		goto failed;
13858 
13859 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13860 		goto done;
13861 
13862 arp_setup_multicast:
13863 	/*
13864 	 * Setup the multicast mappings. This function initializes
13865 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13866 	 * IPv6.
13867 	 */
13868 	if (!ill->ill_isv6) {
13869 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13870 		if (err != 0)
13871 			goto failed;
13872 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13873 		ASSERT(arp_add_mapping_mp != NULL);
13874 	}
13875 
13876 done:
13877 	if (arp_del_mp != NULL) {
13878 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13879 		ipif->ipif_arp_del_mp = arp_del_mp;
13880 	}
13881 	if (arp_down_mp != NULL) {
13882 		ASSERT(ill->ill_arp_down_mp == NULL);
13883 		ill->ill_arp_down_mp = arp_down_mp;
13884 	}
13885 	if (arp_del_mapping_mp != NULL) {
13886 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13887 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13888 	}
13889 	if (arp_up_mp != NULL) {
13890 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13891 		    ill->ill_name, ipif->ipif_id));
13892 		putnext(ill->ill_rq, arp_up_mp);
13893 	}
13894 	if (arp_add_mp != NULL) {
13895 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13896 		    ill->ill_name, ipif->ipif_id));
13897 		/*
13898 		 * If it's an extended ARP implementation, then we'll wait to
13899 		 * hear that DAD has finished before using the interface.
13900 		 */
13901 		if (!ill->ill_arp_extend)
13902 			ipif->ipif_addr_ready = 1;
13903 		putnext(ill->ill_rq, arp_add_mp);
13904 	} else {
13905 		ipif->ipif_addr_ready = 1;
13906 	}
13907 	if (arp_add_mapping_mp != NULL) {
13908 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13909 		    ill->ill_name, ipif->ipif_id));
13910 		putnext(ill->ill_rq, arp_add_mapping_mp);
13911 	}
13912 	if (res_act != Res_act_initial)
13913 		return (0);
13914 
13915 	if (ill->ill_flags & ILLF_NOARP)
13916 		err = ill_arp_off(ill);
13917 	else
13918 		err = ill_arp_on(ill);
13919 	if (err != 0) {
13920 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13921 		freemsg(ipif->ipif_arp_del_mp);
13922 		freemsg(ill->ill_arp_down_mp);
13923 		freemsg(ill->ill_arp_del_mapping_mp);
13924 		ipif->ipif_arp_del_mp = NULL;
13925 		ill->ill_arp_down_mp = NULL;
13926 		ill->ill_arp_del_mapping_mp = NULL;
13927 		return (err);
13928 	}
13929 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13930 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13931 
13932 failed:
13933 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13934 	freemsg(arp_add_mp);
13935 	freemsg(arp_del_mp);
13936 	freemsg(arp_add_mapping_mp);
13937 	freemsg(arp_up_mp);
13938 	freemsg(arp_down_mp);
13939 	ill->ill_arp_bringup_pending = 0;
13940 	return (err);
13941 }
13942 
13943 /*
13944  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13945  * just gone back up.
13946  */
13947 static void
13948 ipif_arp_start_dad(ipif_t *ipif)
13949 {
13950 	ill_t *ill = ipif->ipif_ill;
13951 	mblk_t *arp_add_mp;
13952 	area_t *area;
13953 
13954 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13955 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13956 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13957 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
13958 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
13959 		/*
13960 		 * If we can't contact ARP for some reason, that's not really a
13961 		 * problem.  Just send out the routing socket notification that
13962 		 * DAD completion would have done, and continue.
13963 		 */
13964 		ipif_mask_reply(ipif);
13965 		ip_rts_ifmsg(ipif);
13966 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13967 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13968 		ipif->ipif_addr_ready = 1;
13969 		return;
13970 	}
13971 
13972 	/* Setting the 'unverified' flag restarts DAD */
13973 	area = (area_t *)arp_add_mp->b_rptr;
13974 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
13975 	    ACE_F_UNVERIFIED;
13976 	putnext(ill->ill_rq, arp_add_mp);
13977 }
13978 
13979 static void
13980 ipif_ndp_start_dad(ipif_t *ipif)
13981 {
13982 	nce_t *nce;
13983 
13984 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
13985 	if (nce == NULL)
13986 		return;
13987 
13988 	if (!ndp_restart_dad(nce)) {
13989 		/*
13990 		 * If we can't restart DAD for some reason, that's not really a
13991 		 * problem.  Just send out the routing socket notification that
13992 		 * DAD completion would have done, and continue.
13993 		 */
13994 		ip_rts_ifmsg(ipif);
13995 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13996 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13997 		ipif->ipif_addr_ready = 1;
13998 	}
13999 	NCE_REFRELE(nce);
14000 }
14001 
14002 /*
14003  * Restart duplicate address detection on all interfaces on the given ill.
14004  *
14005  * This is called when an interface transitions from down to up
14006  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
14007  *
14008  * Note that since the underlying physical link has transitioned, we must cause
14009  * at least one routing socket message to be sent here, either via DAD
14010  * completion or just by default on the first ipif.  (If we don't do this, then
14011  * in.mpathd will see long delays when doing link-based failure recovery.)
14012  */
14013 void
14014 ill_restart_dad(ill_t *ill, boolean_t went_up)
14015 {
14016 	ipif_t *ipif;
14017 
14018 	if (ill == NULL)
14019 		return;
14020 
14021 	/*
14022 	 * If layer two doesn't support duplicate address detection, then just
14023 	 * send the routing socket message now and be done with it.
14024 	 */
14025 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
14026 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
14027 		ip_rts_ifmsg(ill->ill_ipif);
14028 		return;
14029 	}
14030 
14031 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14032 		if (went_up) {
14033 			if (ipif->ipif_flags & IPIF_UP) {
14034 				if (ill->ill_isv6)
14035 					ipif_ndp_start_dad(ipif);
14036 				else
14037 					ipif_arp_start_dad(ipif);
14038 			} else if (ill->ill_isv6 &&
14039 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14040 				/*
14041 				 * For IPv4, the ARP module itself will
14042 				 * automatically start the DAD process when it
14043 				 * sees DL_NOTE_LINK_UP.  We respond to the
14044 				 * AR_CN_READY at the completion of that task.
14045 				 * For IPv6, we must kick off the bring-up
14046 				 * process now.
14047 				 */
14048 				ndp_do_recovery(ipif);
14049 			} else {
14050 				/*
14051 				 * Unfortunately, the first ipif is "special"
14052 				 * and represents the underlying ill in the
14053 				 * routing socket messages.  Thus, when this
14054 				 * one ipif is down, we must still notify so
14055 				 * that the user knows the IFF_RUNNING status
14056 				 * change.  (If the first ipif is up, then
14057 				 * we'll handle eventual routing socket
14058 				 * notification via DAD completion.)
14059 				 */
14060 				if (ipif == ill->ill_ipif)
14061 					ip_rts_ifmsg(ill->ill_ipif);
14062 			}
14063 		} else {
14064 			/*
14065 			 * After link down, we'll need to send a new routing
14066 			 * message when the link comes back, so clear
14067 			 * ipif_addr_ready.
14068 			 */
14069 			ipif->ipif_addr_ready = 0;
14070 		}
14071 	}
14072 
14073 	/*
14074 	 * If we've torn down links, then notify the user right away.
14075 	 */
14076 	if (!went_up)
14077 		ip_rts_ifmsg(ill->ill_ipif);
14078 }
14079 
14080 /*
14081  * Wakeup all threads waiting to enter the ipsq, and sleeping
14082  * on any of the ills in this ipsq. The ill_lock of the ill
14083  * must be held so that waiters don't miss wakeups
14084  */
14085 static void
14086 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14087 {
14088 	phyint_t *phyint;
14089 
14090 	phyint = ipsq->ipsq_phyint_list;
14091 	while (phyint != NULL) {
14092 		if (phyint->phyint_illv4) {
14093 			if (!caller_holds_lock)
14094 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14095 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14096 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14097 			if (!caller_holds_lock)
14098 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14099 		}
14100 		if (phyint->phyint_illv6) {
14101 			if (!caller_holds_lock)
14102 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14103 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14104 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14105 			if (!caller_holds_lock)
14106 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14107 		}
14108 		phyint = phyint->phyint_ipsq_next;
14109 	}
14110 }
14111 
14112 static ipsq_t *
14113 ipsq_create(char *groupname, ip_stack_t *ipst)
14114 {
14115 	ipsq_t	*ipsq;
14116 
14117 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14118 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14119 	if (ipsq == NULL) {
14120 		return (NULL);
14121 	}
14122 
14123 	if (groupname != NULL)
14124 		(void) strcpy(ipsq->ipsq_name, groupname);
14125 	else
14126 		ipsq->ipsq_name[0] = '\0';
14127 
14128 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14129 	ipsq->ipsq_flags |= IPSQ_GROUP;
14130 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14131 	ipst->ips_ipsq_g_head = ipsq;
14132 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14133 	return (ipsq);
14134 }
14135 
14136 /*
14137  * Return an ipsq correspoding to the groupname. If 'create' is true
14138  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14139  * uniquely with an IPMP group. However during IPMP groupname operations,
14140  * multiple IPMP groups may be associated with a single ipsq. But no
14141  * IPMP group can be associated with more than 1 ipsq at any time.
14142  * For example
14143  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14144  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14145  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14146  *
14147  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14148  * status shown below during the execution of the above command.
14149  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14150  *
14151  * After the completion of the above groupname command we return to the stable
14152  * state shown below.
14153  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14154  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14155  *
14156  * Because of the above, we don't search based on the ipsq_name since that
14157  * would miss the correct ipsq during certain windows as shown above.
14158  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14159  * natural state.
14160  */
14161 static ipsq_t *
14162 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14163     ip_stack_t *ipst)
14164 {
14165 	ipsq_t	*ipsq;
14166 	int	group_len;
14167 	phyint_t *phyint;
14168 
14169 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14170 
14171 	group_len = strlen(groupname);
14172 	ASSERT(group_len != 0);
14173 	group_len++;
14174 
14175 	for (ipsq = ipst->ips_ipsq_g_head;
14176 	    ipsq != NULL;
14177 	    ipsq = ipsq->ipsq_next) {
14178 		/*
14179 		 * When an ipsq is being split, and ill_split_ipsq
14180 		 * calls this function, we exclude it from being considered.
14181 		 */
14182 		if (ipsq == exclude_ipsq)
14183 			continue;
14184 
14185 		/*
14186 		 * Compare against the ipsq_name. The groupname change happens
14187 		 * in 2 phases. The 1st phase merges the from group into
14188 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14189 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14190 		 * ipsq_name. At this point the phyint_groupname has not been
14191 		 * updated.
14192 		 */
14193 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14194 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14195 			/*
14196 			 * Verify that an ipmp groupname is exactly
14197 			 * part of 1 ipsq and is not found in any other
14198 			 * ipsq.
14199 			 */
14200 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14201 			    NULL);
14202 			return (ipsq);
14203 		}
14204 
14205 		/*
14206 		 * Comparison against ipsq_name alone is not sufficient.
14207 		 * In the case when groups are currently being
14208 		 * merged, the ipsq could hold other IPMP groups temporarily.
14209 		 * so we walk the phyint list and compare against the
14210 		 * phyint_groupname as well.
14211 		 */
14212 		phyint = ipsq->ipsq_phyint_list;
14213 		while (phyint != NULL) {
14214 			if ((group_len == phyint->phyint_groupname_len) &&
14215 			    (bcmp(phyint->phyint_groupname, groupname,
14216 			    group_len) == 0)) {
14217 				/*
14218 				 * Verify that an ipmp groupname is exactly
14219 				 * part of 1 ipsq and is not found in any other
14220 				 * ipsq.
14221 				 */
14222 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14223 				    ipst) == NULL);
14224 				return (ipsq);
14225 			}
14226 			phyint = phyint->phyint_ipsq_next;
14227 		}
14228 	}
14229 	if (create)
14230 		ipsq = ipsq_create(groupname, ipst);
14231 	return (ipsq);
14232 }
14233 
14234 static void
14235 ipsq_delete(ipsq_t *ipsq)
14236 {
14237 	ipsq_t *nipsq;
14238 	ipsq_t *pipsq = NULL;
14239 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14240 
14241 	/*
14242 	 * We don't hold the ipsq lock, but we are sure no new
14243 	 * messages can land up, since the ipsq_refs is zero.
14244 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14245 	 * is associated with this ipsq. (Lookups are based on ill_name
14246 	 * or phyint_groupname)
14247 	 */
14248 	ASSERT(ipsq->ipsq_refs == 0);
14249 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14250 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14251 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14252 		/*
14253 		 * This is not the ipsq of an IPMP group.
14254 		 */
14255 		ipsq->ipsq_ipst = NULL;
14256 		kmem_free(ipsq, sizeof (ipsq_t));
14257 		return;
14258 	}
14259 
14260 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14261 
14262 	/*
14263 	 * Locate the ipsq  before we can remove it from
14264 	 * the singly linked list of ipsq's.
14265 	 */
14266 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14267 	    nipsq = nipsq->ipsq_next) {
14268 		if (nipsq == ipsq) {
14269 			break;
14270 		}
14271 		pipsq = nipsq;
14272 	}
14273 
14274 	ASSERT(nipsq == ipsq);
14275 
14276 	/* unlink ipsq from the list */
14277 	if (pipsq != NULL)
14278 		pipsq->ipsq_next = ipsq->ipsq_next;
14279 	else
14280 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14281 	ipsq->ipsq_ipst = NULL;
14282 	kmem_free(ipsq, sizeof (ipsq_t));
14283 	rw_exit(&ipst->ips_ill_g_lock);
14284 }
14285 
14286 static void
14287 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14288     queue_t *q)
14289 {
14290 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14291 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14292 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14293 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14294 	ASSERT(current_mp != NULL);
14295 
14296 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14297 	    NEW_OP, NULL);
14298 
14299 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14300 	    new_ipsq->ipsq_xopq_mphead != NULL);
14301 
14302 	/*
14303 	 * move from old ipsq to the new ipsq.
14304 	 */
14305 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14306 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14307 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14308 
14309 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14310 }
14311 
14312 void
14313 ill_group_cleanup(ill_t *ill)
14314 {
14315 	ill_t *ill_v4;
14316 	ill_t *ill_v6;
14317 	ipif_t *ipif;
14318 
14319 	ill_v4 = ill->ill_phyint->phyint_illv4;
14320 	ill_v6 = ill->ill_phyint->phyint_illv6;
14321 
14322 	if (ill_v4 != NULL) {
14323 		mutex_enter(&ill_v4->ill_lock);
14324 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14325 		    ipif = ipif->ipif_next) {
14326 			IPIF_UNMARK_MOVING(ipif);
14327 		}
14328 		ill_v4->ill_up_ipifs = B_FALSE;
14329 		mutex_exit(&ill_v4->ill_lock);
14330 	}
14331 
14332 	if (ill_v6 != NULL) {
14333 		mutex_enter(&ill_v6->ill_lock);
14334 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14335 		    ipif = ipif->ipif_next) {
14336 			IPIF_UNMARK_MOVING(ipif);
14337 		}
14338 		ill_v6->ill_up_ipifs = B_FALSE;
14339 		mutex_exit(&ill_v6->ill_lock);
14340 	}
14341 }
14342 /*
14343  * This function is called when an ill has had a change in its group status
14344  * to bring up all the ipifs that were up before the change.
14345  */
14346 int
14347 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14348 {
14349 	ipif_t *ipif;
14350 	ill_t *ill_v4;
14351 	ill_t *ill_v6;
14352 	ill_t *from_ill;
14353 	int err = 0;
14354 
14355 
14356 	ASSERT(IAM_WRITER_ILL(ill));
14357 
14358 	/*
14359 	 * Except for ipif_state_flags and ill_state_flags the other
14360 	 * fields of the ipif/ill that are modified below are protected
14361 	 * implicitly since we are a writer. We would have tried to down
14362 	 * even an ipif that was already down, in ill_down_ipifs. So we
14363 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14364 	 */
14365 	ill_v4 = ill->ill_phyint->phyint_illv4;
14366 	ill_v6 = ill->ill_phyint->phyint_illv6;
14367 	if (ill_v4 != NULL) {
14368 		ill_v4->ill_up_ipifs = B_TRUE;
14369 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14370 		    ipif = ipif->ipif_next) {
14371 			mutex_enter(&ill_v4->ill_lock);
14372 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14373 			IPIF_UNMARK_MOVING(ipif);
14374 			mutex_exit(&ill_v4->ill_lock);
14375 			if (ipif->ipif_was_up) {
14376 				if (!(ipif->ipif_flags & IPIF_UP))
14377 					err = ipif_up(ipif, q, mp);
14378 				ipif->ipif_was_up = B_FALSE;
14379 				if (err != 0) {
14380 					/*
14381 					 * Can there be any other error ?
14382 					 */
14383 					ASSERT(err == EINPROGRESS);
14384 					return (err);
14385 				}
14386 			}
14387 		}
14388 		mutex_enter(&ill_v4->ill_lock);
14389 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14390 		mutex_exit(&ill_v4->ill_lock);
14391 		ill_v4->ill_up_ipifs = B_FALSE;
14392 		if (ill_v4->ill_move_in_progress) {
14393 			ASSERT(ill_v4->ill_move_peer != NULL);
14394 			ill_v4->ill_move_in_progress = B_FALSE;
14395 			from_ill = ill_v4->ill_move_peer;
14396 			from_ill->ill_move_in_progress = B_FALSE;
14397 			from_ill->ill_move_peer = NULL;
14398 			mutex_enter(&from_ill->ill_lock);
14399 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14400 			mutex_exit(&from_ill->ill_lock);
14401 			if (ill_v6 == NULL) {
14402 				if (from_ill->ill_phyint->phyint_flags &
14403 				    PHYI_STANDBY) {
14404 					phyint_inactive(from_ill->ill_phyint);
14405 				}
14406 				if (ill_v4->ill_phyint->phyint_flags &
14407 				    PHYI_STANDBY) {
14408 					phyint_inactive(ill_v4->ill_phyint);
14409 				}
14410 			}
14411 			ill_v4->ill_move_peer = NULL;
14412 		}
14413 	}
14414 
14415 	if (ill_v6 != NULL) {
14416 		ill_v6->ill_up_ipifs = B_TRUE;
14417 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14418 		    ipif = ipif->ipif_next) {
14419 			mutex_enter(&ill_v6->ill_lock);
14420 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14421 			IPIF_UNMARK_MOVING(ipif);
14422 			mutex_exit(&ill_v6->ill_lock);
14423 			if (ipif->ipif_was_up) {
14424 				if (!(ipif->ipif_flags & IPIF_UP))
14425 					err = ipif_up(ipif, q, mp);
14426 				ipif->ipif_was_up = B_FALSE;
14427 				if (err != 0) {
14428 					/*
14429 					 * Can there be any other error ?
14430 					 */
14431 					ASSERT(err == EINPROGRESS);
14432 					return (err);
14433 				}
14434 			}
14435 		}
14436 		mutex_enter(&ill_v6->ill_lock);
14437 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14438 		mutex_exit(&ill_v6->ill_lock);
14439 		ill_v6->ill_up_ipifs = B_FALSE;
14440 		if (ill_v6->ill_move_in_progress) {
14441 			ASSERT(ill_v6->ill_move_peer != NULL);
14442 			ill_v6->ill_move_in_progress = B_FALSE;
14443 			from_ill = ill_v6->ill_move_peer;
14444 			from_ill->ill_move_in_progress = B_FALSE;
14445 			from_ill->ill_move_peer = NULL;
14446 			mutex_enter(&from_ill->ill_lock);
14447 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14448 			mutex_exit(&from_ill->ill_lock);
14449 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14450 				phyint_inactive(from_ill->ill_phyint);
14451 			}
14452 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14453 				phyint_inactive(ill_v6->ill_phyint);
14454 			}
14455 			ill_v6->ill_move_peer = NULL;
14456 		}
14457 	}
14458 	return (0);
14459 }
14460 
14461 /*
14462  * bring down all the approriate ipifs.
14463  */
14464 /* ARGSUSED */
14465 static void
14466 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14467 {
14468 	ipif_t *ipif;
14469 
14470 	ASSERT(IAM_WRITER_ILL(ill));
14471 
14472 	/*
14473 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14474 	 * are modified below are protected implicitly since we are a writer
14475 	 */
14476 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14477 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14478 			continue;
14479 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14480 			/*
14481 			 * We go through the ipif_down logic even if the ipif
14482 			 * is already down, since routes can be added based
14483 			 * on down ipifs. Going through ipif_down once again
14484 			 * will delete any IREs created based on these routes.
14485 			 */
14486 			if (ipif->ipif_flags & IPIF_UP)
14487 				ipif->ipif_was_up = B_TRUE;
14488 			/*
14489 			 * If called with chk_nofailover true ipif is moving.
14490 			 */
14491 			mutex_enter(&ill->ill_lock);
14492 			if (chk_nofailover) {
14493 				ipif->ipif_state_flags |=
14494 				    IPIF_MOVING | IPIF_CHANGING;
14495 			} else {
14496 				ipif->ipif_state_flags |= IPIF_CHANGING;
14497 			}
14498 			mutex_exit(&ill->ill_lock);
14499 			/*
14500 			 * Need to re-create net/subnet bcast ires if
14501 			 * they are dependent on ipif.
14502 			 */
14503 			if (!ipif->ipif_isv6)
14504 				ipif_check_bcast_ires(ipif);
14505 			(void) ipif_logical_down(ipif, NULL, NULL);
14506 			ipif_non_duplicate(ipif);
14507 			ipif_down_tail(ipif);
14508 		}
14509 	}
14510 }
14511 
14512 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14513 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14514 	(ipsq)->ipsq_refs++;				\
14515 }
14516 
14517 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14518 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14519 	(ipsq)->ipsq_refs--;				\
14520 	if ((ipsq)->ipsq_refs == 0)				\
14521 		(ipsq)->ipsq_name[0] = '\0'; 		\
14522 }
14523 
14524 /*
14525  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14526  * new_ipsq.
14527  */
14528 static void
14529 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14530 {
14531 	phyint_t *phyint;
14532 	phyint_t *next_phyint;
14533 
14534 	/*
14535 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14536 	 * writer and the ill_lock of the ill in question. Also the dest
14537 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14538 	 */
14539 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14540 
14541 	phyint = cur_ipsq->ipsq_phyint_list;
14542 	cur_ipsq->ipsq_phyint_list = NULL;
14543 	while (phyint != NULL) {
14544 		next_phyint = phyint->phyint_ipsq_next;
14545 		IPSQ_DEC_REF(cur_ipsq, ipst);
14546 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14547 		new_ipsq->ipsq_phyint_list = phyint;
14548 		IPSQ_INC_REF(new_ipsq, ipst);
14549 		phyint->phyint_ipsq = new_ipsq;
14550 		phyint = next_phyint;
14551 	}
14552 }
14553 
14554 #define	SPLIT_SUCCESS		0
14555 #define	SPLIT_NOT_NEEDED	1
14556 #define	SPLIT_FAILED		2
14557 
14558 int
14559 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14560     ip_stack_t *ipst)
14561 {
14562 	ipsq_t *newipsq = NULL;
14563 
14564 	/*
14565 	 * Assertions denote pre-requisites for changing the ipsq of
14566 	 * a phyint
14567 	 */
14568 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14569 	/*
14570 	 * <ill-phyint> assocs can't change while ill_g_lock
14571 	 * is held as writer. See ill_phyint_reinit()
14572 	 */
14573 	ASSERT(phyint->phyint_illv4 == NULL ||
14574 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14575 	ASSERT(phyint->phyint_illv6 == NULL ||
14576 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14577 
14578 	if ((phyint->phyint_groupname_len !=
14579 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14580 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14581 	    phyint->phyint_groupname_len) != 0)) {
14582 		/*
14583 		 * Once we fail in creating a new ipsq due to memory shortage,
14584 		 * don't attempt to create new ipsq again, based on another
14585 		 * phyint, since we want all phyints belonging to an IPMP group
14586 		 * to be in the same ipsq even in the event of mem alloc fails.
14587 		 */
14588 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14589 		    cur_ipsq, ipst);
14590 		if (newipsq == NULL) {
14591 			/* Memory allocation failure */
14592 			return (SPLIT_FAILED);
14593 		} else {
14594 			/* ipsq_refs protected by ill_g_lock (writer) */
14595 			IPSQ_DEC_REF(cur_ipsq, ipst);
14596 			phyint->phyint_ipsq = newipsq;
14597 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14598 			newipsq->ipsq_phyint_list = phyint;
14599 			IPSQ_INC_REF(newipsq, ipst);
14600 			return (SPLIT_SUCCESS);
14601 		}
14602 	}
14603 	return (SPLIT_NOT_NEEDED);
14604 }
14605 
14606 /*
14607  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14608  * to do this split
14609  */
14610 static int
14611 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14612 {
14613 	ipsq_t *newipsq;
14614 
14615 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14616 	/*
14617 	 * <ill-phyint> assocs can't change while ill_g_lock
14618 	 * is held as writer. See ill_phyint_reinit()
14619 	 */
14620 
14621 	ASSERT(phyint->phyint_illv4 == NULL ||
14622 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14623 	ASSERT(phyint->phyint_illv6 == NULL ||
14624 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14625 
14626 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14627 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14628 		/*
14629 		 * ipsq_init failed due to no memory
14630 		 * caller will use the same ipsq
14631 		 */
14632 		return (SPLIT_FAILED);
14633 	}
14634 
14635 	/* ipsq_ref is protected by ill_g_lock (writer) */
14636 	IPSQ_DEC_REF(cur_ipsq, ipst);
14637 
14638 	/*
14639 	 * This is a new ipsq that is unknown to the world.
14640 	 * So we don't need to hold ipsq_lock,
14641 	 */
14642 	newipsq = phyint->phyint_ipsq;
14643 	newipsq->ipsq_writer = NULL;
14644 	newipsq->ipsq_reentry_cnt--;
14645 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14646 #ifdef DEBUG
14647 	newipsq->ipsq_depth = 0;
14648 #endif
14649 
14650 	return (SPLIT_SUCCESS);
14651 }
14652 
14653 /*
14654  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14655  * ipsq's representing their individual groups or themselves. Return
14656  * whether split needs to be retried again later.
14657  */
14658 static boolean_t
14659 ill_split_ipsq(ipsq_t *cur_ipsq)
14660 {
14661 	phyint_t *phyint;
14662 	phyint_t *next_phyint;
14663 	int	error;
14664 	boolean_t need_retry = B_FALSE;
14665 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14666 
14667 	phyint = cur_ipsq->ipsq_phyint_list;
14668 	cur_ipsq->ipsq_phyint_list = NULL;
14669 	while (phyint != NULL) {
14670 		next_phyint = phyint->phyint_ipsq_next;
14671 		/*
14672 		 * 'created' will tell us whether the callee actually
14673 		 * created an ipsq. Lack of memory may force the callee
14674 		 * to return without creating an ipsq.
14675 		 */
14676 		if (phyint->phyint_groupname == NULL) {
14677 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14678 		} else {
14679 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14680 			    need_retry, ipst);
14681 		}
14682 
14683 		switch (error) {
14684 		case SPLIT_FAILED:
14685 			need_retry = B_TRUE;
14686 			/* FALLTHRU */
14687 		case SPLIT_NOT_NEEDED:
14688 			/*
14689 			 * Keep it on the list.
14690 			 */
14691 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14692 			cur_ipsq->ipsq_phyint_list = phyint;
14693 			break;
14694 		case SPLIT_SUCCESS:
14695 			break;
14696 		default:
14697 			ASSERT(0);
14698 		}
14699 
14700 		phyint = next_phyint;
14701 	}
14702 	return (need_retry);
14703 }
14704 
14705 /*
14706  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14707  * and return the ills in the list. This list will be
14708  * needed to unlock all the ills later on by the caller.
14709  * The <ill-ipsq> associations could change between the
14710  * lock and unlock. Hence the unlock can't traverse the
14711  * ipsq to get the list of ills.
14712  */
14713 static int
14714 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14715 {
14716 	int	cnt = 0;
14717 	phyint_t	*phyint;
14718 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14719 
14720 	/*
14721 	 * The caller holds ill_g_lock to ensure that the ill memberships
14722 	 * of the ipsq don't change
14723 	 */
14724 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14725 
14726 	phyint = ipsq->ipsq_phyint_list;
14727 	while (phyint != NULL) {
14728 		if (phyint->phyint_illv4 != NULL) {
14729 			ASSERT(cnt < list_max);
14730 			list[cnt++] = phyint->phyint_illv4;
14731 		}
14732 		if (phyint->phyint_illv6 != NULL) {
14733 			ASSERT(cnt < list_max);
14734 			list[cnt++] = phyint->phyint_illv6;
14735 		}
14736 		phyint = phyint->phyint_ipsq_next;
14737 	}
14738 	ill_lock_ills(list, cnt);
14739 	return (cnt);
14740 }
14741 
14742 void
14743 ill_lock_ills(ill_t **list, int cnt)
14744 {
14745 	int	i;
14746 
14747 	if (cnt > 1) {
14748 		boolean_t try_again;
14749 		do {
14750 			try_again = B_FALSE;
14751 			for (i = 0; i < cnt - 1; i++) {
14752 				if (list[i] < list[i + 1]) {
14753 					ill_t	*tmp;
14754 
14755 					/* swap the elements */
14756 					tmp = list[i];
14757 					list[i] = list[i + 1];
14758 					list[i + 1] = tmp;
14759 					try_again = B_TRUE;
14760 				}
14761 			}
14762 		} while (try_again);
14763 	}
14764 
14765 	for (i = 0; i < cnt; i++) {
14766 		if (i == 0) {
14767 			if (list[i] != NULL)
14768 				mutex_enter(&list[i]->ill_lock);
14769 			else
14770 				return;
14771 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14772 			mutex_enter(&list[i]->ill_lock);
14773 		}
14774 	}
14775 }
14776 
14777 void
14778 ill_unlock_ills(ill_t **list, int cnt)
14779 {
14780 	int	i;
14781 
14782 	for (i = 0; i < cnt; i++) {
14783 		if ((i == 0) && (list[i] != NULL)) {
14784 			mutex_exit(&list[i]->ill_lock);
14785 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14786 			mutex_exit(&list[i]->ill_lock);
14787 		}
14788 	}
14789 }
14790 
14791 /*
14792  * Merge all the ills from 1 ipsq group into another ipsq group.
14793  * The source ipsq group is specified by the ipsq associated with
14794  * 'from_ill'. The destination ipsq group is specified by the ipsq
14795  * associated with 'to_ill' or 'groupname' respectively.
14796  * Note that ipsq itself does not have a reference count mechanism
14797  * and functions don't look up an ipsq and pass it around. Instead
14798  * functions pass around an ill or groupname, and the ipsq is looked
14799  * up from the ill or groupname and the required operation performed
14800  * atomically with the lookup on the ipsq.
14801  */
14802 static int
14803 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14804     queue_t *q)
14805 {
14806 	ipsq_t *old_ipsq;
14807 	ipsq_t *new_ipsq;
14808 	ill_t	**ill_list;
14809 	int	cnt;
14810 	size_t	ill_list_size;
14811 	boolean_t became_writer_on_new_sq = B_FALSE;
14812 	ip_stack_t	*ipst = from_ill->ill_ipst;
14813 
14814 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
14815 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14816 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14817 
14818 	/*
14819 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14820 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14821 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14822 	 */
14823 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14824 
14825 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14826 	if (groupname != NULL)
14827 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
14828 	else {
14829 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14830 	}
14831 
14832 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14833 
14834 	/*
14835 	 * both groups are on the same ipsq.
14836 	 */
14837 	if (old_ipsq == new_ipsq) {
14838 		rw_exit(&ipst->ips_ill_g_lock);
14839 		return (0);
14840 	}
14841 
14842 	cnt = old_ipsq->ipsq_refs << 1;
14843 	ill_list_size = cnt * sizeof (ill_t *);
14844 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14845 	if (ill_list == NULL) {
14846 		rw_exit(&ipst->ips_ill_g_lock);
14847 		return (ENOMEM);
14848 	}
14849 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14850 
14851 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14852 	mutex_enter(&new_ipsq->ipsq_lock);
14853 	if ((new_ipsq->ipsq_writer == NULL &&
14854 	    new_ipsq->ipsq_current_ipif == NULL) ||
14855 	    (new_ipsq->ipsq_writer == curthread)) {
14856 		new_ipsq->ipsq_writer = curthread;
14857 		new_ipsq->ipsq_reentry_cnt++;
14858 		became_writer_on_new_sq = B_TRUE;
14859 	}
14860 
14861 	/*
14862 	 * We are holding ill_g_lock as writer and all the ill locks of
14863 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14864 	 * message can land up on the old ipsq even though we don't hold the
14865 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14866 	 */
14867 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14868 
14869 	/*
14870 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14871 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14872 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14873 	 */
14874 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
14875 
14876 	/*
14877 	 * Mark the new ipsq as needing a split since it is currently
14878 	 * being shared by more than 1 IPMP group. The split will
14879 	 * occur at the end of ipsq_exit
14880 	 */
14881 	new_ipsq->ipsq_split = B_TRUE;
14882 
14883 	/* Now release all the locks */
14884 	mutex_exit(&new_ipsq->ipsq_lock);
14885 	ill_unlock_ills(ill_list, cnt);
14886 	rw_exit(&ipst->ips_ill_g_lock);
14887 
14888 	kmem_free(ill_list, ill_list_size);
14889 
14890 	/*
14891 	 * If we succeeded in becoming writer on the new ipsq, then
14892 	 * drain the new ipsq and start processing  all enqueued messages
14893 	 * including the current ioctl we are processing which is either
14894 	 * a set groupname or failover/failback.
14895 	 */
14896 	if (became_writer_on_new_sq)
14897 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
14898 
14899 	/*
14900 	 * syncq has been changed and all the messages have been moved.
14901 	 */
14902 	mutex_enter(&old_ipsq->ipsq_lock);
14903 	old_ipsq->ipsq_current_ipif = NULL;
14904 	old_ipsq->ipsq_current_ioctl = 0;
14905 	old_ipsq->ipsq_current_done = B_TRUE;
14906 	mutex_exit(&old_ipsq->ipsq_lock);
14907 	return (EINPROGRESS);
14908 }
14909 
14910 /*
14911  * Delete and add the loopback copy and non-loopback copy of
14912  * the BROADCAST ire corresponding to ill and addr. Used to
14913  * group broadcast ires together when ill becomes part of
14914  * a group.
14915  *
14916  * This function is also called when ill is leaving the group
14917  * so that the ires belonging to the group gets re-grouped.
14918  */
14919 static void
14920 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14921 {
14922 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14923 	ire_t **ire_ptpn = &ire_head;
14924 	ip_stack_t	*ipst = ill->ill_ipst;
14925 
14926 	/*
14927 	 * The loopback and non-loopback IREs are inserted in the order in which
14928 	 * they're found, on the basis that they are correctly ordered (loopback
14929 	 * first).
14930 	 */
14931 	for (;;) {
14932 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14933 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
14934 		if (ire == NULL)
14935 			break;
14936 
14937 		/*
14938 		 * we are passing in KM_SLEEP because it is not easy to
14939 		 * go back to a sane state in case of memory failure.
14940 		 */
14941 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14942 		ASSERT(nire != NULL);
14943 		bzero(nire, sizeof (ire_t));
14944 		/*
14945 		 * Don't use ire_max_frag directly since we don't
14946 		 * hold on to 'ire' until we add the new ire 'nire' and
14947 		 * we don't want the new ire to have a dangling reference
14948 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14949 		 * be in sync with the ipif_mtu of the associate ipif.
14950 		 * For eg. this happens as a result of SIOCSLIFNAME,
14951 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14952 		 * the driver. A change in ire_max_frag triggered as
14953 		 * as a result of path mtu discovery, or due to an
14954 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14955 		 * route change -mtu command does not apply to broadcast ires.
14956 		 *
14957 		 * XXX We need a recovery strategy here if ire_init fails
14958 		 */
14959 		if (ire_init(nire,
14960 		    (uchar_t *)&ire->ire_addr,
14961 		    (uchar_t *)&ire->ire_mask,
14962 		    (uchar_t *)&ire->ire_src_addr,
14963 		    (uchar_t *)&ire->ire_gateway_addr,
14964 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
14965 		    &ire->ire_ipif->ipif_mtu,
14966 		    ire->ire_nce,
14967 		    ire->ire_rfq,
14968 		    ire->ire_stq,
14969 		    ire->ire_type,
14970 		    ire->ire_ipif,
14971 		    ire->ire_cmask,
14972 		    ire->ire_phandle,
14973 		    ire->ire_ihandle,
14974 		    ire->ire_flags,
14975 		    &ire->ire_uinfo,
14976 		    NULL,
14977 		    NULL,
14978 		    ipst) == NULL) {
14979 			cmn_err(CE_PANIC, "ire_init() failed");
14980 		}
14981 		ire_delete(ire);
14982 		ire_refrele(ire);
14983 
14984 		/*
14985 		 * The newly created IREs are inserted at the tail of the list
14986 		 * starting with ire_head. As we've just allocated them no one
14987 		 * knows about them so it's safe.
14988 		 */
14989 		*ire_ptpn = nire;
14990 		ire_ptpn = &nire->ire_next;
14991 	}
14992 
14993 	for (nire = ire_head; nire != NULL; nire = nire_next) {
14994 		int error;
14995 		ire_t *oire;
14996 		/* unlink the IRE from our list before calling ire_add() */
14997 		nire_next = nire->ire_next;
14998 		nire->ire_next = NULL;
14999 
15000 		/* ire_add adds the ire at the right place in the list */
15001 		oire = nire;
15002 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
15003 		ASSERT(error == 0);
15004 		ASSERT(oire == nire);
15005 		ire_refrele(nire);	/* Held in ire_add */
15006 	}
15007 }
15008 
15009 /*
15010  * This function is usually called when an ill is inserted in
15011  * a group and all the ipifs are already UP. As all the ipifs
15012  * are already UP, the broadcast ires have already been created
15013  * and been inserted. But, ire_add_v4 would not have grouped properly.
15014  * We need to re-group for the benefit of ip_wput_ire which
15015  * expects BROADCAST ires to be grouped properly to avoid sending
15016  * more than one copy of the broadcast packet per group.
15017  *
15018  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
15019  *	  because when ipif_up_done ends up calling this, ires have
15020  *        already been added before illgrp_insert i.e before ill_group
15021  *	  has been initialized.
15022  */
15023 static void
15024 ill_group_bcast_for_xmit(ill_t *ill)
15025 {
15026 	ill_group_t *illgrp;
15027 	ipif_t *ipif;
15028 	ipaddr_t addr;
15029 	ipaddr_t net_mask;
15030 	ipaddr_t subnet_netmask;
15031 
15032 	illgrp = ill->ill_group;
15033 
15034 	/*
15035 	 * This function is called even when an ill is deleted from
15036 	 * the group. Hence, illgrp could be null.
15037 	 */
15038 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15039 		return;
15040 
15041 	/*
15042 	 * Delete all the BROADCAST ires matching this ill and add
15043 	 * them back. This time, ire_add_v4 should take care of
15044 	 * grouping them with others because ill is part of the
15045 	 * group.
15046 	 */
15047 	ill_bcast_delete_and_add(ill, 0);
15048 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15049 
15050 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15051 
15052 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15053 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15054 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15055 		} else {
15056 			net_mask = htonl(IN_CLASSA_NET);
15057 		}
15058 		addr = net_mask & ipif->ipif_subnet;
15059 		ill_bcast_delete_and_add(ill, addr);
15060 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15061 
15062 		subnet_netmask = ipif->ipif_net_mask;
15063 		addr = ipif->ipif_subnet;
15064 		ill_bcast_delete_and_add(ill, addr);
15065 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15066 	}
15067 }
15068 
15069 /*
15070  * This function is called from illgrp_delete when ill is being deleted
15071  * from the group.
15072  *
15073  * As ill is not there in the group anymore, any address belonging
15074  * to this ill should be cleared of IRE_MARK_NORECV.
15075  */
15076 static void
15077 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15078 {
15079 	ire_t *ire;
15080 	irb_t *irb;
15081 	ip_stack_t	*ipst = ill->ill_ipst;
15082 
15083 	ASSERT(ill->ill_group == NULL);
15084 
15085 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15086 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15087 
15088 	if (ire != NULL) {
15089 		/*
15090 		 * IPMP and plumbing operations are serialized on the ipsq, so
15091 		 * no one will insert or delete a broadcast ire under our feet.
15092 		 */
15093 		irb = ire->ire_bucket;
15094 		rw_enter(&irb->irb_lock, RW_READER);
15095 		ire_refrele(ire);
15096 
15097 		for (; ire != NULL; ire = ire->ire_next) {
15098 			if (ire->ire_addr != addr)
15099 				break;
15100 			if (ire_to_ill(ire) != ill)
15101 				continue;
15102 
15103 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15104 			ire->ire_marks &= ~IRE_MARK_NORECV;
15105 		}
15106 		rw_exit(&irb->irb_lock);
15107 	}
15108 }
15109 
15110 /*
15111  * This function must be called only after the broadcast ires
15112  * have been grouped together. For a given address addr, nominate
15113  * only one of the ires whose interface is not FAILED or OFFLINE.
15114  *
15115  * This is also called when an ipif goes down, so that we can nominate
15116  * a different ire with the same address for receiving.
15117  */
15118 static void
15119 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15120 {
15121 	irb_t *irb;
15122 	ire_t *ire;
15123 	ire_t *ire1;
15124 	ire_t *save_ire;
15125 	ire_t **irep = NULL;
15126 	boolean_t first = B_TRUE;
15127 	ire_t *clear_ire = NULL;
15128 	ire_t *start_ire = NULL;
15129 	ire_t	*new_lb_ire;
15130 	ire_t	*new_nlb_ire;
15131 	boolean_t new_lb_ire_used = B_FALSE;
15132 	boolean_t new_nlb_ire_used = B_FALSE;
15133 	uint64_t match_flags;
15134 	uint64_t phyi_flags;
15135 	boolean_t fallback = B_FALSE;
15136 	uint_t	max_frag;
15137 
15138 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15139 	    NULL, MATCH_IRE_TYPE, ipst);
15140 	/*
15141 	 * We may not be able to find some ires if a previous
15142 	 * ire_create failed. This happens when an ipif goes
15143 	 * down and we are unable to create BROADCAST ires due
15144 	 * to memory failure. Thus, we have to check for NULL
15145 	 * below. This should handle the case for LOOPBACK,
15146 	 * POINTOPOINT and interfaces with some POINTOPOINT
15147 	 * logicals for which there are no BROADCAST ires.
15148 	 */
15149 	if (ire == NULL)
15150 		return;
15151 	/*
15152 	 * Currently IRE_BROADCASTS are deleted when an ipif
15153 	 * goes down which runs exclusively. Thus, setting
15154 	 * IRE_MARK_RCVD should not race with ire_delete marking
15155 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15156 	 * be consistent with other parts of the code that walks
15157 	 * a given bucket.
15158 	 */
15159 	save_ire = ire;
15160 	irb = ire->ire_bucket;
15161 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15162 	if (new_lb_ire == NULL) {
15163 		ire_refrele(ire);
15164 		return;
15165 	}
15166 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15167 	if (new_nlb_ire == NULL) {
15168 		ire_refrele(ire);
15169 		kmem_cache_free(ire_cache, new_lb_ire);
15170 		return;
15171 	}
15172 	IRB_REFHOLD(irb);
15173 	rw_enter(&irb->irb_lock, RW_WRITER);
15174 	/*
15175 	 * Get to the first ire matching the address and the
15176 	 * group. If the address does not match we are done
15177 	 * as we could not find the IRE. If the address matches
15178 	 * we should get to the first one matching the group.
15179 	 */
15180 	while (ire != NULL) {
15181 		if (ire->ire_addr != addr ||
15182 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15183 			break;
15184 		}
15185 		ire = ire->ire_next;
15186 	}
15187 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15188 	start_ire = ire;
15189 redo:
15190 	while (ire != NULL && ire->ire_addr == addr &&
15191 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15192 		/*
15193 		 * The first ire for any address within a group
15194 		 * should always be the one with IRE_MARK_NORECV cleared
15195 		 * so that ip_wput_ire can avoid searching for one.
15196 		 * Note down the insertion point which will be used
15197 		 * later.
15198 		 */
15199 		if (first && (irep == NULL))
15200 			irep = ire->ire_ptpn;
15201 		/*
15202 		 * PHYI_FAILED is set when the interface fails.
15203 		 * This interface might have become good, but the
15204 		 * daemon has not yet detected. We should still
15205 		 * not receive on this. PHYI_OFFLINE should never
15206 		 * be picked as this has been offlined and soon
15207 		 * be removed.
15208 		 */
15209 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15210 		if (phyi_flags & PHYI_OFFLINE) {
15211 			ire->ire_marks |= IRE_MARK_NORECV;
15212 			ire = ire->ire_next;
15213 			continue;
15214 		}
15215 		if (phyi_flags & match_flags) {
15216 			ire->ire_marks |= IRE_MARK_NORECV;
15217 			ire = ire->ire_next;
15218 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15219 			    PHYI_INACTIVE) {
15220 				fallback = B_TRUE;
15221 			}
15222 			continue;
15223 		}
15224 		if (first) {
15225 			/*
15226 			 * We will move this to the front of the list later
15227 			 * on.
15228 			 */
15229 			clear_ire = ire;
15230 			ire->ire_marks &= ~IRE_MARK_NORECV;
15231 		} else {
15232 			ire->ire_marks |= IRE_MARK_NORECV;
15233 		}
15234 		first = B_FALSE;
15235 		ire = ire->ire_next;
15236 	}
15237 	/*
15238 	 * If we never nominated anybody, try nominating at least
15239 	 * an INACTIVE, if we found one. Do it only once though.
15240 	 */
15241 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15242 	    fallback) {
15243 		match_flags = PHYI_FAILED;
15244 		ire = start_ire;
15245 		irep = NULL;
15246 		goto redo;
15247 	}
15248 	ire_refrele(save_ire);
15249 
15250 	/*
15251 	 * irep non-NULL indicates that we entered the while loop
15252 	 * above. If clear_ire is at the insertion point, we don't
15253 	 * have to do anything. clear_ire will be NULL if all the
15254 	 * interfaces are failed.
15255 	 *
15256 	 * We cannot unlink and reinsert the ire at the right place
15257 	 * in the list since there can be other walkers of this bucket.
15258 	 * Instead we delete and recreate the ire
15259 	 */
15260 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15261 		ire_t *clear_ire_stq = NULL;
15262 
15263 		bzero(new_lb_ire, sizeof (ire_t));
15264 		/* XXX We need a recovery strategy here. */
15265 		if (ire_init(new_lb_ire,
15266 		    (uchar_t *)&clear_ire->ire_addr,
15267 		    (uchar_t *)&clear_ire->ire_mask,
15268 		    (uchar_t *)&clear_ire->ire_src_addr,
15269 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15270 		    &clear_ire->ire_max_frag,
15271 		    NULL, /* let ire_nce_init derive the resolver info */
15272 		    clear_ire->ire_rfq,
15273 		    clear_ire->ire_stq,
15274 		    clear_ire->ire_type,
15275 		    clear_ire->ire_ipif,
15276 		    clear_ire->ire_cmask,
15277 		    clear_ire->ire_phandle,
15278 		    clear_ire->ire_ihandle,
15279 		    clear_ire->ire_flags,
15280 		    &clear_ire->ire_uinfo,
15281 		    NULL,
15282 		    NULL,
15283 		    ipst) == NULL)
15284 			cmn_err(CE_PANIC, "ire_init() failed");
15285 		if (clear_ire->ire_stq == NULL) {
15286 			ire_t *ire_next = clear_ire->ire_next;
15287 			if (ire_next != NULL &&
15288 			    ire_next->ire_stq != NULL &&
15289 			    ire_next->ire_addr == clear_ire->ire_addr &&
15290 			    ire_next->ire_ipif->ipif_ill ==
15291 			    clear_ire->ire_ipif->ipif_ill) {
15292 				clear_ire_stq = ire_next;
15293 
15294 				bzero(new_nlb_ire, sizeof (ire_t));
15295 				/* XXX We need a recovery strategy here. */
15296 				if (ire_init(new_nlb_ire,
15297 				    (uchar_t *)&clear_ire_stq->ire_addr,
15298 				    (uchar_t *)&clear_ire_stq->ire_mask,
15299 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15300 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15301 				    &clear_ire_stq->ire_max_frag,
15302 				    NULL,
15303 				    clear_ire_stq->ire_rfq,
15304 				    clear_ire_stq->ire_stq,
15305 				    clear_ire_stq->ire_type,
15306 				    clear_ire_stq->ire_ipif,
15307 				    clear_ire_stq->ire_cmask,
15308 				    clear_ire_stq->ire_phandle,
15309 				    clear_ire_stq->ire_ihandle,
15310 				    clear_ire_stq->ire_flags,
15311 				    &clear_ire_stq->ire_uinfo,
15312 				    NULL,
15313 				    NULL,
15314 				    ipst) == NULL)
15315 					cmn_err(CE_PANIC, "ire_init() failed");
15316 			}
15317 		}
15318 
15319 		/*
15320 		 * Delete the ire. We can't call ire_delete() since
15321 		 * we are holding the bucket lock. We can't release the
15322 		 * bucket lock since we can't allow irep to change. So just
15323 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15324 		 * ire from the list and do the refrele.
15325 		 */
15326 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15327 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15328 
15329 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15330 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15331 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15332 		}
15333 
15334 		/*
15335 		 * Also take care of otherfields like ib/ob pkt count
15336 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15337 		 */
15338 
15339 		/* Set the max_frag before adding the ire */
15340 		max_frag = *new_lb_ire->ire_max_fragp;
15341 		new_lb_ire->ire_max_fragp = NULL;
15342 		new_lb_ire->ire_max_frag = max_frag;
15343 
15344 		/* Add the new ire's. Insert at *irep */
15345 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15346 		ire1 = *irep;
15347 		if (ire1 != NULL)
15348 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15349 		new_lb_ire->ire_next = ire1;
15350 		/* Link the new one in. */
15351 		new_lb_ire->ire_ptpn = irep;
15352 		membar_producer();
15353 		*irep = new_lb_ire;
15354 		new_lb_ire_used = B_TRUE;
15355 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15356 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15357 		DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *), new_lb_ire->ire_ipif,
15358 		    (char *), "ire", (void *), new_lb_ire);
15359 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15360 
15361 		if (clear_ire_stq != NULL) {
15362 			/* Set the max_frag before adding the ire */
15363 			max_frag = *new_nlb_ire->ire_max_fragp;
15364 			new_nlb_ire->ire_max_fragp = NULL;
15365 			new_nlb_ire->ire_max_frag = max_frag;
15366 
15367 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15368 			irep = &new_lb_ire->ire_next;
15369 			/* Add the new ire. Insert at *irep */
15370 			ire1 = *irep;
15371 			if (ire1 != NULL)
15372 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15373 			new_nlb_ire->ire_next = ire1;
15374 			/* Link the new one in. */
15375 			new_nlb_ire->ire_ptpn = irep;
15376 			membar_producer();
15377 			*irep = new_nlb_ire;
15378 			new_nlb_ire_used = B_TRUE;
15379 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15380 			    ire_stats_inserted);
15381 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15382 			DTRACE_PROBE3(ipif__incr__cnt,
15383 			    (ipif_t *), new_nlb_ire->ire_ipif,
15384 			    (char *), "ire", (void *), new_nlb_ire);
15385 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15386 			DTRACE_PROBE3(ill__incr__cnt,
15387 			    (ill_t *), new_nlb_ire->ire_stq->q_ptr,
15388 			    (char *), "ire", (void *), new_nlb_ire);
15389 			((ill_t *)(new_nlb_ire->ire_stq->q_ptr))->ill_ire_cnt++;
15390 		}
15391 	}
15392 	rw_exit(&irb->irb_lock);
15393 	if (!new_lb_ire_used)
15394 		kmem_cache_free(ire_cache, new_lb_ire);
15395 	if (!new_nlb_ire_used)
15396 		kmem_cache_free(ire_cache, new_nlb_ire);
15397 	IRB_REFRELE(irb);
15398 }
15399 
15400 /*
15401  * Whenever an ipif goes down we have to renominate a different
15402  * broadcast ire to receive. Whenever an ipif comes up, we need
15403  * to make sure that we have only one nominated to receive.
15404  */
15405 static void
15406 ipif_renominate_bcast(ipif_t *ipif)
15407 {
15408 	ill_t *ill = ipif->ipif_ill;
15409 	ipaddr_t subnet_addr;
15410 	ipaddr_t net_addr;
15411 	ipaddr_t net_mask = 0;
15412 	ipaddr_t subnet_netmask;
15413 	ipaddr_t addr;
15414 	ill_group_t *illgrp;
15415 	ip_stack_t	*ipst = ill->ill_ipst;
15416 
15417 	illgrp = ill->ill_group;
15418 	/*
15419 	 * If this is the last ipif going down, it might take
15420 	 * the ill out of the group. In that case ipif_down ->
15421 	 * illgrp_delete takes care of doing the nomination.
15422 	 * ipif_down does not call for this case.
15423 	 */
15424 	ASSERT(illgrp != NULL);
15425 
15426 	/* There could not have been any ires associated with this */
15427 	if (ipif->ipif_subnet == 0)
15428 		return;
15429 
15430 	ill_mark_bcast(illgrp, 0, ipst);
15431 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15432 
15433 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15434 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15435 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15436 	} else {
15437 		net_mask = htonl(IN_CLASSA_NET);
15438 	}
15439 	addr = net_mask & ipif->ipif_subnet;
15440 	ill_mark_bcast(illgrp, addr, ipst);
15441 
15442 	net_addr = ~net_mask | addr;
15443 	ill_mark_bcast(illgrp, net_addr, ipst);
15444 
15445 	subnet_netmask = ipif->ipif_net_mask;
15446 	addr = ipif->ipif_subnet;
15447 	ill_mark_bcast(illgrp, addr, ipst);
15448 
15449 	subnet_addr = ~subnet_netmask | addr;
15450 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15451 }
15452 
15453 /*
15454  * Whenever we form or delete ill groups, we need to nominate one set of
15455  * BROADCAST ires for receiving in the group.
15456  *
15457  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15458  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15459  *    for ill_ipif_up_count to be non-zero. This is the only case where
15460  *    ill_ipif_up_count is zero and we would still find the ires.
15461  *
15462  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15463  *    ipif is UP and we just have to do the nomination.
15464  *
15465  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15466  *    from the group. So, we have to do the nomination.
15467  *
15468  * Because of (3), there could be just one ill in the group. But we have
15469  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15470  * Thus, this function does not optimize when there is only one ill as
15471  * it is not correct for (3).
15472  */
15473 static void
15474 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15475 {
15476 	ill_t *ill;
15477 	ipif_t *ipif;
15478 	ipaddr_t subnet_addr;
15479 	ipaddr_t prev_subnet_addr = 0;
15480 	ipaddr_t net_addr;
15481 	ipaddr_t prev_net_addr = 0;
15482 	ipaddr_t net_mask = 0;
15483 	ipaddr_t subnet_netmask;
15484 	ipaddr_t addr;
15485 	ip_stack_t	*ipst;
15486 
15487 	/*
15488 	 * When the last memeber is leaving, there is nothing to
15489 	 * nominate.
15490 	 */
15491 	if (illgrp->illgrp_ill_count == 0) {
15492 		ASSERT(illgrp->illgrp_ill == NULL);
15493 		return;
15494 	}
15495 
15496 	ill = illgrp->illgrp_ill;
15497 	ASSERT(!ill->ill_isv6);
15498 	ipst = ill->ill_ipst;
15499 	/*
15500 	 * We assume that ires with same address and belonging to the
15501 	 * same group, has been grouped together. Nominating a *single*
15502 	 * ill in the group for sending and receiving broadcast is done
15503 	 * by making sure that the first BROADCAST ire (which will be
15504 	 * the one returned by ire_ctable_lookup for ip_rput and the
15505 	 * one that will be used in ip_wput_ire) will be the one that
15506 	 * will not have IRE_MARK_NORECV set.
15507 	 *
15508 	 * 1) ip_rput checks and discards packets received on ires marked
15509 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15510 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15511 	 *    first ire in the group for every broadcast address in the group.
15512 	 *    ip_rput will accept packets only on the first ire i.e only
15513 	 *    one copy of the ill.
15514 	 *
15515 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15516 	 *    packet for the whole group. It needs to send out on the ill
15517 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15518 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15519 	 *    the copy echoed back on other port where the ire is not marked
15520 	 *    with IRE_MARK_NORECV.
15521 	 *
15522 	 * Note that we just need to have the first IRE either loopback or
15523 	 * non-loopback (either of them may not exist if ire_create failed
15524 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15525 	 * always hit the first one and hence will always accept one copy.
15526 	 *
15527 	 * We have a broadcast ire per ill for all the unique prefixes
15528 	 * hosted on that ill. As we don't have a way of knowing the
15529 	 * unique prefixes on a given ill and hence in the whole group,
15530 	 * we just call ill_mark_bcast on all the prefixes that exist
15531 	 * in the group. For the common case of one prefix, the code
15532 	 * below optimizes by remebering the last address used for
15533 	 * markng. In the case of multiple prefixes, this will still
15534 	 * optimize depending the order of prefixes.
15535 	 *
15536 	 * The only unique address across the whole group is 0.0.0.0 and
15537 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15538 	 * the first ire in the bucket for receiving and disables the
15539 	 * others.
15540 	 */
15541 	ill_mark_bcast(illgrp, 0, ipst);
15542 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15543 	for (; ill != NULL; ill = ill->ill_group_next) {
15544 
15545 		for (ipif = ill->ill_ipif; ipif != NULL;
15546 		    ipif = ipif->ipif_next) {
15547 
15548 			if (!(ipif->ipif_flags & IPIF_UP) ||
15549 			    ipif->ipif_subnet == 0) {
15550 				continue;
15551 			}
15552 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15553 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15554 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15555 			} else {
15556 				net_mask = htonl(IN_CLASSA_NET);
15557 			}
15558 			addr = net_mask & ipif->ipif_subnet;
15559 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15560 				ill_mark_bcast(illgrp, addr, ipst);
15561 				net_addr = ~net_mask | addr;
15562 				ill_mark_bcast(illgrp, net_addr, ipst);
15563 			}
15564 			prev_net_addr = addr;
15565 
15566 			subnet_netmask = ipif->ipif_net_mask;
15567 			addr = ipif->ipif_subnet;
15568 			if (prev_subnet_addr == 0 ||
15569 			    prev_subnet_addr != addr) {
15570 				ill_mark_bcast(illgrp, addr, ipst);
15571 				subnet_addr = ~subnet_netmask | addr;
15572 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15573 			}
15574 			prev_subnet_addr = addr;
15575 		}
15576 	}
15577 }
15578 
15579 /*
15580  * This function is called while forming ill groups.
15581  *
15582  * Currently, we handle only allmulti groups. We want to join
15583  * allmulti on only one of the ills in the groups. In future,
15584  * when we have link aggregation, we may have to join normal
15585  * multicast groups on multiple ills as switch does inbound load
15586  * balancing. Following are the functions that calls this
15587  * function :
15588  *
15589  * 1) ill_recover_multicast : Interface is coming back UP.
15590  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15591  *    will call ill_recover_multicast to recover all the multicast
15592  *    groups. We need to make sure that only one member is joined
15593  *    in the ill group.
15594  *
15595  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15596  *    Somebody is joining allmulti. We need to make sure that only one
15597  *    member is joined in the group.
15598  *
15599  * 3) illgrp_insert : If allmulti has already joined, we need to make
15600  *    sure that only one member is joined in the group.
15601  *
15602  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15603  *    allmulti who we have nominated. We need to pick someother ill.
15604  *
15605  * 5) illgrp_delete : The ill we nominated is leaving the group,
15606  *    we need to pick a new ill to join the group.
15607  *
15608  * For (1), (2), (5) - we just have to check whether there is
15609  * a good ill joined in the group. If we could not find any ills
15610  * joined the group, we should join.
15611  *
15612  * For (4), the one that was nominated to receive, left the group.
15613  * There could be nobody joined in the group when this function is
15614  * called.
15615  *
15616  * For (3) - we need to explicitly check whether there are multiple
15617  * ills joined in the group.
15618  *
15619  * For simplicity, we don't differentiate any of the above cases. We
15620  * just leave the group if it is joined on any of them and join on
15621  * the first good ill.
15622  */
15623 int
15624 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15625 {
15626 	ilm_t *ilm;
15627 	ill_t *ill;
15628 	ill_t *fallback_inactive_ill = NULL;
15629 	ill_t *fallback_failed_ill = NULL;
15630 	int ret = 0;
15631 
15632 	/*
15633 	 * Leave the allmulti on all the ills and start fresh.
15634 	 */
15635 	for (ill = illgrp->illgrp_ill; ill != NULL;
15636 	    ill = ill->ill_group_next) {
15637 		if (ill->ill_join_allmulti)
15638 			(void) ip_leave_allmulti(ill->ill_ipif);
15639 	}
15640 
15641 	/*
15642 	 * Choose a good ill. Fallback to inactive or failed if
15643 	 * none available. We need to fallback to FAILED in the
15644 	 * case where we have 2 interfaces in a group - where
15645 	 * one of them is failed and another is a good one and
15646 	 * the good one (not marked inactive) is leaving the group.
15647 	 */
15648 	ret = 0;
15649 	for (ill = illgrp->illgrp_ill; ill != NULL;
15650 	    ill = ill->ill_group_next) {
15651 		/* Never pick an offline interface */
15652 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15653 			continue;
15654 
15655 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15656 			fallback_failed_ill = ill;
15657 			continue;
15658 		}
15659 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15660 			fallback_inactive_ill = ill;
15661 			continue;
15662 		}
15663 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15664 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15665 				ret = ip_join_allmulti(ill->ill_ipif);
15666 				/*
15667 				 * ip_join_allmulti can fail because of memory
15668 				 * failures. So, make sure we join at least
15669 				 * on one ill.
15670 				 */
15671 				if (ill->ill_join_allmulti)
15672 					return (0);
15673 			}
15674 		}
15675 	}
15676 	if (ret != 0) {
15677 		/*
15678 		 * If we tried nominating above and failed to do so,
15679 		 * return error. We might have tried multiple times.
15680 		 * But, return the latest error.
15681 		 */
15682 		return (ret);
15683 	}
15684 	if ((ill = fallback_inactive_ill) != NULL) {
15685 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15686 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15687 				ret = ip_join_allmulti(ill->ill_ipif);
15688 				return (ret);
15689 			}
15690 		}
15691 	} else if ((ill = fallback_failed_ill) != NULL) {
15692 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15693 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15694 				ret = ip_join_allmulti(ill->ill_ipif);
15695 				return (ret);
15696 			}
15697 		}
15698 	}
15699 	return (0);
15700 }
15701 
15702 /*
15703  * This function is called from illgrp_delete after it is
15704  * deleted from the group to reschedule responsibilities
15705  * to a different ill.
15706  */
15707 static void
15708 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15709 {
15710 	ilm_t	*ilm;
15711 	ipif_t	*ipif;
15712 	ipaddr_t subnet_addr;
15713 	ipaddr_t net_addr;
15714 	ipaddr_t net_mask = 0;
15715 	ipaddr_t subnet_netmask;
15716 	ipaddr_t addr;
15717 	ip_stack_t *ipst = ill->ill_ipst;
15718 
15719 	ASSERT(ill->ill_group == NULL);
15720 	/*
15721 	 * Broadcast Responsibility:
15722 	 *
15723 	 * 1. If this ill has been nominated for receiving broadcast
15724 	 * packets, we need to find a new one. Before we find a new
15725 	 * one, we need to re-group the ires that are part of this new
15726 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15727 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15728 	 * thing for us.
15729 	 *
15730 	 * 2. If this ill was not nominated for receiving broadcast
15731 	 * packets, we need to clear the IRE_MARK_NORECV flag
15732 	 * so that we continue to send up broadcast packets.
15733 	 */
15734 	if (!ill->ill_isv6) {
15735 		/*
15736 		 * Case 1 above : No optimization here. Just redo the
15737 		 * nomination.
15738 		 */
15739 		ill_group_bcast_for_xmit(ill);
15740 		ill_nominate_bcast_rcv(illgrp);
15741 
15742 		/*
15743 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15744 		 */
15745 		ill_clear_bcast_mark(ill, 0);
15746 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15747 
15748 		for (ipif = ill->ill_ipif; ipif != NULL;
15749 		    ipif = ipif->ipif_next) {
15750 
15751 			if (!(ipif->ipif_flags & IPIF_UP) ||
15752 			    ipif->ipif_subnet == 0) {
15753 				continue;
15754 			}
15755 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15756 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15757 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15758 			} else {
15759 				net_mask = htonl(IN_CLASSA_NET);
15760 			}
15761 			addr = net_mask & ipif->ipif_subnet;
15762 			ill_clear_bcast_mark(ill, addr);
15763 
15764 			net_addr = ~net_mask | addr;
15765 			ill_clear_bcast_mark(ill, net_addr);
15766 
15767 			subnet_netmask = ipif->ipif_net_mask;
15768 			addr = ipif->ipif_subnet;
15769 			ill_clear_bcast_mark(ill, addr);
15770 
15771 			subnet_addr = ~subnet_netmask | addr;
15772 			ill_clear_bcast_mark(ill, subnet_addr);
15773 		}
15774 	}
15775 
15776 	/*
15777 	 * Multicast Responsibility.
15778 	 *
15779 	 * If we have joined allmulti on this one, find a new member
15780 	 * in the group to join allmulti. As this ill is already part
15781 	 * of allmulti, we don't have to join on this one.
15782 	 *
15783 	 * If we have not joined allmulti on this one, there is no
15784 	 * responsibility to handoff. But we need to take new
15785 	 * responsibility i.e, join allmulti on this one if we need
15786 	 * to.
15787 	 */
15788 	if (ill->ill_join_allmulti) {
15789 		(void) ill_nominate_mcast_rcv(illgrp);
15790 	} else {
15791 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15792 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15793 				(void) ip_join_allmulti(ill->ill_ipif);
15794 				break;
15795 			}
15796 		}
15797 	}
15798 
15799 	/*
15800 	 * We intentionally do the flushing of IRE_CACHES only matching
15801 	 * on the ill and not on groups. Note that we are already deleted
15802 	 * from the group.
15803 	 *
15804 	 * This will make sure that all IRE_CACHES whose stq is pointing
15805 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15806 	 * deleted and IRE_CACHES that are not pointing at this ill will
15807 	 * be left alone.
15808 	 */
15809 	if (ill->ill_isv6) {
15810 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15811 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15812 	} else {
15813 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15814 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15815 	}
15816 
15817 	/*
15818 	 * Some conn may have cached one of the IREs deleted above. By removing
15819 	 * the ire reference, we clean up the extra reference to the ill held in
15820 	 * ire->ire_stq.
15821 	 */
15822 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
15823 
15824 	/*
15825 	 * Re-do source address selection for all the members in the
15826 	 * group, if they borrowed source address from one of the ipifs
15827 	 * in this ill.
15828 	 */
15829 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15830 		if (ill->ill_isv6) {
15831 			ipif_update_other_ipifs_v6(ipif, illgrp);
15832 		} else {
15833 			ipif_update_other_ipifs(ipif, illgrp);
15834 		}
15835 	}
15836 }
15837 
15838 /*
15839  * Delete the ill from the group. The caller makes sure that it is
15840  * in a group and it okay to delete from the group. So, we always
15841  * delete here.
15842  */
15843 static void
15844 illgrp_delete(ill_t *ill)
15845 {
15846 	ill_group_t *illgrp;
15847 	ill_group_t *tmpg;
15848 	ill_t *tmp_ill;
15849 	ip_stack_t	*ipst = ill->ill_ipst;
15850 
15851 	/*
15852 	 * Reset illgrp_ill_schednext if it was pointing at us.
15853 	 * We need to do this before we set ill_group to NULL.
15854 	 */
15855 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15856 	mutex_enter(&ill->ill_lock);
15857 
15858 	illgrp_reset_schednext(ill);
15859 
15860 	illgrp = ill->ill_group;
15861 
15862 	/* Delete the ill from illgrp. */
15863 	if (illgrp->illgrp_ill == ill) {
15864 		illgrp->illgrp_ill = ill->ill_group_next;
15865 	} else {
15866 		tmp_ill = illgrp->illgrp_ill;
15867 		while (tmp_ill->ill_group_next != ill) {
15868 			tmp_ill = tmp_ill->ill_group_next;
15869 			ASSERT(tmp_ill != NULL);
15870 		}
15871 		tmp_ill->ill_group_next = ill->ill_group_next;
15872 	}
15873 	ill->ill_group = NULL;
15874 	ill->ill_group_next = NULL;
15875 
15876 	illgrp->illgrp_ill_count--;
15877 	mutex_exit(&ill->ill_lock);
15878 	rw_exit(&ipst->ips_ill_g_lock);
15879 
15880 	/*
15881 	 * As this ill is leaving the group, we need to hand off
15882 	 * the responsibilities to the other ills in the group, if
15883 	 * this ill had some responsibilities.
15884 	 */
15885 
15886 	ill_handoff_responsibility(ill, illgrp);
15887 
15888 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15889 
15890 	if (illgrp->illgrp_ill_count == 0) {
15891 
15892 		ASSERT(illgrp->illgrp_ill == NULL);
15893 		if (ill->ill_isv6) {
15894 			if (illgrp == ipst->ips_illgrp_head_v6) {
15895 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
15896 			} else {
15897 				tmpg = ipst->ips_illgrp_head_v6;
15898 				while (tmpg->illgrp_next != illgrp) {
15899 					tmpg = tmpg->illgrp_next;
15900 					ASSERT(tmpg != NULL);
15901 				}
15902 				tmpg->illgrp_next = illgrp->illgrp_next;
15903 			}
15904 		} else {
15905 			if (illgrp == ipst->ips_illgrp_head_v4) {
15906 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
15907 			} else {
15908 				tmpg = ipst->ips_illgrp_head_v4;
15909 				while (tmpg->illgrp_next != illgrp) {
15910 					tmpg = tmpg->illgrp_next;
15911 					ASSERT(tmpg != NULL);
15912 				}
15913 				tmpg->illgrp_next = illgrp->illgrp_next;
15914 			}
15915 		}
15916 		mutex_destroy(&illgrp->illgrp_lock);
15917 		mi_free(illgrp);
15918 	}
15919 	rw_exit(&ipst->ips_ill_g_lock);
15920 
15921 	/*
15922 	 * Even though the ill is out of the group its not necessary
15923 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15924 	 * We will split the ipsq when phyint_groupname is set to NULL.
15925 	 */
15926 
15927 	/*
15928 	 * Send a routing sockets message if we are deleting from
15929 	 * groups with names.
15930 	 */
15931 	if (ill->ill_phyint->phyint_groupname_len != 0)
15932 		ip_rts_ifmsg(ill->ill_ipif);
15933 }
15934 
15935 /*
15936  * Re-do source address selection. This is normally called when
15937  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15938  * ipif comes up.
15939  */
15940 void
15941 ill_update_source_selection(ill_t *ill)
15942 {
15943 	ipif_t *ipif;
15944 
15945 	ASSERT(IAM_WRITER_ILL(ill));
15946 
15947 	if (ill->ill_group != NULL)
15948 		ill = ill->ill_group->illgrp_ill;
15949 
15950 	for (; ill != NULL; ill = ill->ill_group_next) {
15951 		for (ipif = ill->ill_ipif; ipif != NULL;
15952 		    ipif = ipif->ipif_next) {
15953 			if (ill->ill_isv6)
15954 				ipif_recreate_interface_routes_v6(NULL, ipif);
15955 			else
15956 				ipif_recreate_interface_routes(NULL, ipif);
15957 		}
15958 	}
15959 }
15960 
15961 /*
15962  * Insert ill in a group headed by illgrp_head. The caller can either
15963  * pass a groupname in which case we search for a group with the
15964  * same name to insert in or pass a group to insert in. This function
15965  * would only search groups with names.
15966  *
15967  * NOTE : The caller should make sure that there is at least one ipif
15968  *	  UP on this ill so that illgrp_scheduler can pick this ill
15969  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15970  *	  already sent a DL_UNBIND to the driver and we don't want to
15971  *	  send anymore packets. We don't assert for ipif_up_count
15972  *	  to be greater than zero, because ipif_up_done wants to call
15973  *	  this function before bumping up the ipif_up_count. See
15974  *	  ipif_up_done() for details.
15975  */
15976 int
15977 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
15978     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
15979 {
15980 	ill_group_t *illgrp;
15981 	ill_t *prev_ill;
15982 	phyint_t *phyi;
15983 	ip_stack_t	*ipst = ill->ill_ipst;
15984 
15985 	ASSERT(ill->ill_group == NULL);
15986 
15987 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15988 	mutex_enter(&ill->ill_lock);
15989 
15990 	if (groupname != NULL) {
15991 		/*
15992 		 * Look for a group with a matching groupname to insert.
15993 		 */
15994 		for (illgrp = *illgrp_head; illgrp != NULL;
15995 		    illgrp = illgrp->illgrp_next) {
15996 
15997 			ill_t *tmp_ill;
15998 
15999 			/*
16000 			 * If we have an ill_group_t in the list which has
16001 			 * no ill_t assigned then we must be in the process of
16002 			 * removing this group. We skip this as illgrp_delete()
16003 			 * will remove it from the list.
16004 			 */
16005 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
16006 				ASSERT(illgrp->illgrp_ill_count == 0);
16007 				continue;
16008 			}
16009 
16010 			ASSERT(tmp_ill->ill_phyint != NULL);
16011 			phyi = tmp_ill->ill_phyint;
16012 			/*
16013 			 * Look at groups which has names only.
16014 			 */
16015 			if (phyi->phyint_groupname_len == 0)
16016 				continue;
16017 			/*
16018 			 * Names are stored in the phyint common to both
16019 			 * IPv4 and IPv6.
16020 			 */
16021 			if (mi_strcmp(phyi->phyint_groupname,
16022 			    groupname) == 0) {
16023 				break;
16024 			}
16025 		}
16026 	} else {
16027 		/*
16028 		 * If the caller passes in a NULL "grp_to_insert", we
16029 		 * allocate one below and insert this singleton.
16030 		 */
16031 		illgrp = grp_to_insert;
16032 	}
16033 
16034 	ill->ill_group_next = NULL;
16035 
16036 	if (illgrp == NULL) {
16037 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16038 		if (illgrp == NULL) {
16039 			return (ENOMEM);
16040 		}
16041 		illgrp->illgrp_next = *illgrp_head;
16042 		*illgrp_head = illgrp;
16043 		illgrp->illgrp_ill = ill;
16044 		illgrp->illgrp_ill_count = 1;
16045 		ill->ill_group = illgrp;
16046 		/*
16047 		 * Used in illgrp_scheduler to protect multiple threads
16048 		 * from traversing the list.
16049 		 */
16050 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16051 	} else {
16052 		ASSERT(ill->ill_net_type ==
16053 		    illgrp->illgrp_ill->ill_net_type);
16054 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16055 
16056 		/* Insert ill at tail of this group */
16057 		prev_ill = illgrp->illgrp_ill;
16058 		while (prev_ill->ill_group_next != NULL)
16059 			prev_ill = prev_ill->ill_group_next;
16060 		prev_ill->ill_group_next = ill;
16061 		ill->ill_group = illgrp;
16062 		illgrp->illgrp_ill_count++;
16063 		/*
16064 		 * Inherit group properties. Currently only forwarding
16065 		 * is the property we try to keep the same with all the
16066 		 * ills. When there are more, we will abstract this into
16067 		 * a function.
16068 		 */
16069 		ill->ill_flags &= ~ILLF_ROUTER;
16070 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16071 	}
16072 	mutex_exit(&ill->ill_lock);
16073 	rw_exit(&ipst->ips_ill_g_lock);
16074 
16075 	/*
16076 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16077 	 *    may be zero as it has not yet been bumped. But the ires
16078 	 *    have already been added. So, we do the nomination here
16079 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16080 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16081 	 *    ill_ipif_up_count here while nominating broadcast ires for
16082 	 *    receive.
16083 	 *
16084 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16085 	 *    to group them properly as ire_add() has already happened
16086 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16087 	 *    case, we need to do it here anyway.
16088 	 */
16089 	if (!ill->ill_isv6) {
16090 		ill_group_bcast_for_xmit(ill);
16091 		ill_nominate_bcast_rcv(illgrp);
16092 	}
16093 
16094 	if (!ipif_is_coming_up) {
16095 		/*
16096 		 * When ipif_up_done() calls this function, the multicast
16097 		 * groups have not been joined yet. So, there is no point in
16098 		 * nomination. ip_join_allmulti will handle groups when
16099 		 * ill_recover_multicast is called from ipif_up_done() later.
16100 		 */
16101 		(void) ill_nominate_mcast_rcv(illgrp);
16102 		/*
16103 		 * ipif_up_done calls ill_update_source_selection
16104 		 * anyway. Moreover, we don't want to re-create
16105 		 * interface routes while ipif_up_done() still has reference
16106 		 * to them. Refer to ipif_up_done() for more details.
16107 		 */
16108 		ill_update_source_selection(ill);
16109 	}
16110 
16111 	/*
16112 	 * Send a routing sockets message if we are inserting into
16113 	 * groups with names.
16114 	 */
16115 	if (groupname != NULL)
16116 		ip_rts_ifmsg(ill->ill_ipif);
16117 	return (0);
16118 }
16119 
16120 /*
16121  * Return the first phyint matching the groupname. There could
16122  * be more than one when there are ill groups.
16123  *
16124  * If 'usable' is set, then we exclude ones that are marked with any of
16125  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16126  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16127  * emulation of ipmp.
16128  */
16129 phyint_t *
16130 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16131 {
16132 	phyint_t *phyi;
16133 
16134 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16135 	/*
16136 	 * Group names are stored in the phyint - a common structure
16137 	 * to both IPv4 and IPv6.
16138 	 */
16139 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16140 	for (; phyi != NULL;
16141 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16142 	    phyi, AVL_AFTER)) {
16143 		if (phyi->phyint_groupname_len == 0)
16144 			continue;
16145 		/*
16146 		 * Skip the ones that should not be used since the callers
16147 		 * sometime use this for sending packets.
16148 		 */
16149 		if (usable && (phyi->phyint_flags &
16150 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16151 			continue;
16152 
16153 		ASSERT(phyi->phyint_groupname != NULL);
16154 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16155 			return (phyi);
16156 	}
16157 	return (NULL);
16158 }
16159 
16160 
16161 /*
16162  * Return the first usable phyint matching the group index. By 'usable'
16163  * we exclude ones that are marked ununsable with any of
16164  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16165  *
16166  * Used only for the ipmp/netinfo emulation of ipmp.
16167  */
16168 phyint_t *
16169 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16170 {
16171 	phyint_t *phyi;
16172 
16173 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16174 
16175 	if (!ipst->ips_ipmp_hook_emulation)
16176 		return (NULL);
16177 
16178 	/*
16179 	 * Group indicies are stored in the phyint - a common structure
16180 	 * to both IPv4 and IPv6.
16181 	 */
16182 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16183 	for (; phyi != NULL;
16184 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16185 	    phyi, AVL_AFTER)) {
16186 		/* Ignore the ones that do not have a group */
16187 		if (phyi->phyint_groupname_len == 0)
16188 			continue;
16189 
16190 		ASSERT(phyi->phyint_group_ifindex != 0);
16191 		/*
16192 		 * Skip the ones that should not be used since the callers
16193 		 * sometime use this for sending packets.
16194 		 */
16195 		if (phyi->phyint_flags &
16196 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16197 			continue;
16198 		if (phyi->phyint_group_ifindex == group_ifindex)
16199 			return (phyi);
16200 	}
16201 	return (NULL);
16202 }
16203 
16204 
16205 /*
16206  * MT notes on creation and deletion of IPMP groups
16207  *
16208  * Creation and deletion of IPMP groups introduce the need to merge or
16209  * split the associated serialization objects i.e the ipsq's. Normally all
16210  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16211  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16212  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16213  * is a need to change the <ill-ipsq> association and we have to operate on both
16214  * the source and destination IPMP groups. For eg. attempting to set the
16215  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16216  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16217  * source or destination IPMP group are mapped to a single ipsq for executing
16218  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16219  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16220  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16221  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16222  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16223  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16224  *
16225  * In the above example the ioctl handling code locates the current ipsq of hme0
16226  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16227  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16228  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16229  * the destination ipsq. If the destination ipsq is not busy, it also enters
16230  * the destination ipsq exclusively. Now the actual groupname setting operation
16231  * can proceed. If the destination ipsq is busy, the operation is enqueued
16232  * on the destination (merged) ipsq and will be handled in the unwind from
16233  * ipsq_exit.
16234  *
16235  * To prevent other threads accessing the ill while the group name change is
16236  * in progres, we bring down the ipifs which also removes the ill from the
16237  * group. The group is changed in phyint and when the first ipif on the ill
16238  * is brought up, the ill is inserted into the right IPMP group by
16239  * illgrp_insert.
16240  */
16241 /* ARGSUSED */
16242 int
16243 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16244     ip_ioctl_cmd_t *ipip, void *ifreq)
16245 {
16246 	int i;
16247 	char *tmp;
16248 	int namelen;
16249 	ill_t *ill = ipif->ipif_ill;
16250 	ill_t *ill_v4, *ill_v6;
16251 	int err = 0;
16252 	phyint_t *phyi;
16253 	phyint_t *phyi_tmp;
16254 	struct lifreq *lifr;
16255 	mblk_t	*mp1;
16256 	char *groupname;
16257 	ipsq_t *ipsq;
16258 	ip_stack_t	*ipst = ill->ill_ipst;
16259 
16260 	ASSERT(IAM_WRITER_IPIF(ipif));
16261 
16262 	/* Existance verified in ip_wput_nondata */
16263 	mp1 = mp->b_cont->b_cont;
16264 	lifr = (struct lifreq *)mp1->b_rptr;
16265 	groupname = lifr->lifr_groupname;
16266 
16267 	if (ipif->ipif_id != 0)
16268 		return (EINVAL);
16269 
16270 	phyi = ill->ill_phyint;
16271 	ASSERT(phyi != NULL);
16272 
16273 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16274 		return (EINVAL);
16275 
16276 	tmp = groupname;
16277 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16278 		;
16279 
16280 	if (i == LIFNAMSIZ) {
16281 		/* no null termination */
16282 		return (EINVAL);
16283 	}
16284 
16285 	/*
16286 	 * Calculate the namelen exclusive of the null
16287 	 * termination character.
16288 	 */
16289 	namelen = tmp - groupname;
16290 
16291 	ill_v4 = phyi->phyint_illv4;
16292 	ill_v6 = phyi->phyint_illv6;
16293 
16294 	/*
16295 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16296 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16297 	 * synchronization notes in ip.c
16298 	 */
16299 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16300 		return (EINVAL);
16301 	}
16302 
16303 	/*
16304 	 * mark the ill as changing.
16305 	 * this should queue all new requests on the syncq.
16306 	 */
16307 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16308 
16309 	if (ill_v4 != NULL)
16310 		ill_v4->ill_state_flags |= ILL_CHANGING;
16311 	if (ill_v6 != NULL)
16312 		ill_v6->ill_state_flags |= ILL_CHANGING;
16313 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16314 
16315 	if (namelen == 0) {
16316 		/*
16317 		 * Null string means remove this interface from the
16318 		 * existing group.
16319 		 */
16320 		if (phyi->phyint_groupname_len == 0) {
16321 			/*
16322 			 * Never was in a group.
16323 			 */
16324 			err = 0;
16325 			goto done;
16326 		}
16327 
16328 		/*
16329 		 * IPv4 or IPv6 may be temporarily out of the group when all
16330 		 * the ipifs are down. Thus, we need to check for ill_group to
16331 		 * be non-NULL.
16332 		 */
16333 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16334 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16335 			mutex_enter(&ill_v4->ill_lock);
16336 			if (!ill_is_quiescent(ill_v4)) {
16337 				/*
16338 				 * ipsq_pending_mp_add will not fail since
16339 				 * connp is NULL
16340 				 */
16341 				(void) ipsq_pending_mp_add(NULL,
16342 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16343 				mutex_exit(&ill_v4->ill_lock);
16344 				err = EINPROGRESS;
16345 				goto done;
16346 			}
16347 			mutex_exit(&ill_v4->ill_lock);
16348 		}
16349 
16350 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16351 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16352 			mutex_enter(&ill_v6->ill_lock);
16353 			if (!ill_is_quiescent(ill_v6)) {
16354 				(void) ipsq_pending_mp_add(NULL,
16355 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16356 				mutex_exit(&ill_v6->ill_lock);
16357 				err = EINPROGRESS;
16358 				goto done;
16359 			}
16360 			mutex_exit(&ill_v6->ill_lock);
16361 		}
16362 
16363 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16364 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16365 		mutex_enter(&phyi->phyint_lock);
16366 		ASSERT(phyi->phyint_groupname != NULL);
16367 		mi_free(phyi->phyint_groupname);
16368 		phyi->phyint_groupname = NULL;
16369 		phyi->phyint_groupname_len = 0;
16370 
16371 		/* Restore the ifindex used to be the per interface one */
16372 		phyi->phyint_group_ifindex = 0;
16373 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16374 		mutex_exit(&phyi->phyint_lock);
16375 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16376 		rw_exit(&ipst->ips_ill_g_lock);
16377 		err = ill_up_ipifs(ill, q, mp);
16378 
16379 		/*
16380 		 * set the split flag so that the ipsq can be split
16381 		 */
16382 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16383 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16384 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16385 
16386 	} else {
16387 		if (phyi->phyint_groupname_len != 0) {
16388 			ASSERT(phyi->phyint_groupname != NULL);
16389 			/* Are we inserting in the same group ? */
16390 			if (mi_strcmp(groupname,
16391 			    phyi->phyint_groupname) == 0) {
16392 				err = 0;
16393 				goto done;
16394 			}
16395 		}
16396 
16397 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16398 		/*
16399 		 * Merge ipsq for the group's.
16400 		 * This check is here as multiple groups/ills might be
16401 		 * sharing the same ipsq.
16402 		 * If we have to merege than the operation is restarted
16403 		 * on the new ipsq.
16404 		 */
16405 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16406 		if (phyi->phyint_ipsq != ipsq) {
16407 			rw_exit(&ipst->ips_ill_g_lock);
16408 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16409 			goto done;
16410 		}
16411 		/*
16412 		 * Running exclusive on new ipsq.
16413 		 */
16414 
16415 		ASSERT(ipsq != NULL);
16416 		ASSERT(ipsq->ipsq_writer == curthread);
16417 
16418 		/*
16419 		 * Check whether the ill_type and ill_net_type matches before
16420 		 * we allocate any memory so that the cleanup is easier.
16421 		 *
16422 		 * We can't group dissimilar ones as we can't load spread
16423 		 * packets across the group because of potential link-level
16424 		 * header differences.
16425 		 */
16426 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16427 		if (phyi_tmp != NULL) {
16428 			if ((ill_v4 != NULL &&
16429 			    phyi_tmp->phyint_illv4 != NULL) &&
16430 			    ((ill_v4->ill_net_type !=
16431 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16432 			    (ill_v4->ill_type !=
16433 			    phyi_tmp->phyint_illv4->ill_type))) {
16434 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16435 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16436 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16437 				rw_exit(&ipst->ips_ill_g_lock);
16438 				return (EINVAL);
16439 			}
16440 			if ((ill_v6 != NULL &&
16441 			    phyi_tmp->phyint_illv6 != NULL) &&
16442 			    ((ill_v6->ill_net_type !=
16443 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16444 			    (ill_v6->ill_type !=
16445 			    phyi_tmp->phyint_illv6->ill_type))) {
16446 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16447 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16448 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16449 				rw_exit(&ipst->ips_ill_g_lock);
16450 				return (EINVAL);
16451 			}
16452 		}
16453 
16454 		rw_exit(&ipst->ips_ill_g_lock);
16455 
16456 		/*
16457 		 * bring down all v4 ipifs.
16458 		 */
16459 		if (ill_v4 != NULL) {
16460 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16461 		}
16462 
16463 		/*
16464 		 * bring down all v6 ipifs.
16465 		 */
16466 		if (ill_v6 != NULL) {
16467 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16468 		}
16469 
16470 		/*
16471 		 * make sure all ipifs are down and there are no active
16472 		 * references. Call to ipsq_pending_mp_add will not fail
16473 		 * since connp is NULL.
16474 		 */
16475 		if (ill_v4 != NULL) {
16476 			mutex_enter(&ill_v4->ill_lock);
16477 			if (!ill_is_quiescent(ill_v4)) {
16478 				(void) ipsq_pending_mp_add(NULL,
16479 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16480 				mutex_exit(&ill_v4->ill_lock);
16481 				err = EINPROGRESS;
16482 				goto done;
16483 			}
16484 			mutex_exit(&ill_v4->ill_lock);
16485 		}
16486 
16487 		if (ill_v6 != NULL) {
16488 			mutex_enter(&ill_v6->ill_lock);
16489 			if (!ill_is_quiescent(ill_v6)) {
16490 				(void) ipsq_pending_mp_add(NULL,
16491 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16492 				mutex_exit(&ill_v6->ill_lock);
16493 				err = EINPROGRESS;
16494 				goto done;
16495 			}
16496 			mutex_exit(&ill_v6->ill_lock);
16497 		}
16498 
16499 		/*
16500 		 * allocate including space for null terminator
16501 		 * before we insert.
16502 		 */
16503 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16504 		if (tmp == NULL)
16505 			return (ENOMEM);
16506 
16507 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16508 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16509 		mutex_enter(&phyi->phyint_lock);
16510 		if (phyi->phyint_groupname_len != 0) {
16511 			ASSERT(phyi->phyint_groupname != NULL);
16512 			mi_free(phyi->phyint_groupname);
16513 		}
16514 
16515 		/*
16516 		 * setup the new group name.
16517 		 */
16518 		phyi->phyint_groupname = tmp;
16519 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16520 		phyi->phyint_groupname_len = namelen + 1;
16521 
16522 		if (ipst->ips_ipmp_hook_emulation) {
16523 			/*
16524 			 * If the group already exists we use the existing
16525 			 * group_ifindex, otherwise we pick a new index here.
16526 			 */
16527 			if (phyi_tmp != NULL) {
16528 				phyi->phyint_group_ifindex =
16529 				    phyi_tmp->phyint_group_ifindex;
16530 			} else {
16531 				/* XXX We need a recovery strategy here. */
16532 				if (!ip_assign_ifindex(
16533 				    &phyi->phyint_group_ifindex, ipst))
16534 					cmn_err(CE_PANIC,
16535 					    "ip_assign_ifindex() failed");
16536 			}
16537 		}
16538 		/*
16539 		 * Select whether the netinfo and hook use the per-interface
16540 		 * or per-group ifindex.
16541 		 */
16542 		if (ipst->ips_ipmp_hook_emulation)
16543 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16544 		else
16545 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16546 
16547 		if (ipst->ips_ipmp_hook_emulation &&
16548 		    phyi_tmp != NULL) {
16549 			/* First phyint in group - group PLUMB event */
16550 			ill_nic_info_plumb(ill, B_TRUE);
16551 		}
16552 		mutex_exit(&phyi->phyint_lock);
16553 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16554 		rw_exit(&ipst->ips_ill_g_lock);
16555 
16556 		err = ill_up_ipifs(ill, q, mp);
16557 	}
16558 
16559 done:
16560 	/*
16561 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16562 	 */
16563 	if (err != EINPROGRESS) {
16564 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16565 		if (ill_v4 != NULL)
16566 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16567 		if (ill_v6 != NULL)
16568 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16569 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16570 	}
16571 	return (err);
16572 }
16573 
16574 /* ARGSUSED */
16575 int
16576 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16577     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16578 {
16579 	ill_t *ill;
16580 	phyint_t *phyi;
16581 	struct lifreq *lifr;
16582 	mblk_t	*mp1;
16583 
16584 	/* Existence verified in ip_wput_nondata */
16585 	mp1 = mp->b_cont->b_cont;
16586 	lifr = (struct lifreq *)mp1->b_rptr;
16587 	ill = ipif->ipif_ill;
16588 	phyi = ill->ill_phyint;
16589 
16590 	lifr->lifr_groupname[0] = '\0';
16591 	/*
16592 	 * ill_group may be null if all the interfaces
16593 	 * are down. But still, the phyint should always
16594 	 * hold the name.
16595 	 */
16596 	if (phyi->phyint_groupname_len != 0) {
16597 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16598 		    phyi->phyint_groupname_len);
16599 	}
16600 
16601 	return (0);
16602 }
16603 
16604 
16605 typedef struct conn_move_s {
16606 	ill_t	*cm_from_ill;
16607 	ill_t	*cm_to_ill;
16608 	int	cm_ifindex;
16609 } conn_move_t;
16610 
16611 /*
16612  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16613  */
16614 static void
16615 conn_move(conn_t *connp, caddr_t arg)
16616 {
16617 	conn_move_t *connm;
16618 	int ifindex;
16619 	int i;
16620 	ill_t *from_ill;
16621 	ill_t *to_ill;
16622 	ilg_t *ilg;
16623 	ilm_t *ret_ilm;
16624 
16625 	connm = (conn_move_t *)arg;
16626 	ifindex = connm->cm_ifindex;
16627 	from_ill = connm->cm_from_ill;
16628 	to_ill = connm->cm_to_ill;
16629 
16630 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16631 
16632 	/* All multicast fields protected by conn_lock */
16633 	mutex_enter(&connp->conn_lock);
16634 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16635 	if ((connp->conn_outgoing_ill == from_ill) &&
16636 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16637 		connp->conn_outgoing_ill = to_ill;
16638 		connp->conn_incoming_ill = to_ill;
16639 	}
16640 
16641 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16642 
16643 	if ((connp->conn_multicast_ill == from_ill) &&
16644 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16645 		connp->conn_multicast_ill = connm->cm_to_ill;
16646 	}
16647 
16648 	/*
16649 	 * Change the ilg_ill to point to the new one. This assumes
16650 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16651 	 * has been told to receive packets on this interface.
16652 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16653 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16654 	 * some ilms may not have moved. We check to see whether
16655 	 * the ilms have moved to to_ill. We can't check on from_ill
16656 	 * as in the process of moving, we could have split an ilm
16657 	 * in to two - which has the same orig_ifindex and v6group.
16658 	 *
16659 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16660 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16661 	 */
16662 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16663 		ilg = &connp->conn_ilg[i];
16664 		if ((ilg->ilg_ill == from_ill) &&
16665 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16666 			/* ifindex != 0 indicates failback */
16667 			if (ifindex != 0) {
16668 				connp->conn_ilg[i].ilg_ill = to_ill;
16669 				continue;
16670 			}
16671 
16672 			mutex_enter(&to_ill->ill_lock);
16673 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16674 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16675 			    connp->conn_zoneid);
16676 			mutex_exit(&to_ill->ill_lock);
16677 
16678 			if (ret_ilm != NULL)
16679 				connp->conn_ilg[i].ilg_ill = to_ill;
16680 		}
16681 	}
16682 	mutex_exit(&connp->conn_lock);
16683 }
16684 
16685 static void
16686 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16687 {
16688 	conn_move_t connm;
16689 	ip_stack_t	*ipst = from_ill->ill_ipst;
16690 
16691 	connm.cm_from_ill = from_ill;
16692 	connm.cm_to_ill = to_ill;
16693 	connm.cm_ifindex = ifindex;
16694 
16695 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16696 }
16697 
16698 /*
16699  * ilm has been moved from from_ill to to_ill.
16700  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16701  * appropriately.
16702  *
16703  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16704  *	  the code there de-references ipif_ill to get the ill to
16705  *	  send multicast requests. It does not work as ipif is on its
16706  *	  move and already moved when this function is called.
16707  *	  Thus, we need to use from_ill and to_ill send down multicast
16708  *	  requests.
16709  */
16710 static void
16711 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16712 {
16713 	ipif_t *ipif;
16714 	ilm_t *ilm;
16715 
16716 	/*
16717 	 * See whether we need to send down DL_ENABMULTI_REQ on
16718 	 * to_ill as ilm has just been added.
16719 	 */
16720 	ASSERT(IAM_WRITER_ILL(to_ill));
16721 	ASSERT(IAM_WRITER_ILL(from_ill));
16722 
16723 	ILM_WALKER_HOLD(to_ill);
16724 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16725 
16726 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16727 			continue;
16728 		/*
16729 		 * no locks held, ill/ipif cannot dissappear as long
16730 		 * as we are writer.
16731 		 */
16732 		ipif = to_ill->ill_ipif;
16733 		/*
16734 		 * No need to hold any lock as we are the writer and this
16735 		 * can only be changed by a writer.
16736 		 */
16737 		ilm->ilm_is_new = B_FALSE;
16738 
16739 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16740 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16741 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16742 			    "resolver\n"));
16743 			continue;		/* Must be IRE_IF_NORESOLVER */
16744 		}
16745 
16746 
16747 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16748 			ip1dbg(("ilm_send_multicast_reqs: "
16749 			    "to_ill MULTI_BCAST\n"));
16750 			goto from;
16751 		}
16752 
16753 		if (to_ill->ill_isv6)
16754 			mld_joingroup(ilm);
16755 		else
16756 			igmp_joingroup(ilm);
16757 
16758 		if (to_ill->ill_ipif_up_count == 0) {
16759 			/*
16760 			 * Nobody there. All multicast addresses will be
16761 			 * re-joined when we get the DL_BIND_ACK bringing the
16762 			 * interface up.
16763 			 */
16764 			ilm->ilm_notify_driver = B_FALSE;
16765 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16766 			goto from;
16767 		}
16768 
16769 		/*
16770 		 * For allmulti address, we want to join on only one interface.
16771 		 * Checking for ilm_numentries_v6 is not correct as you may
16772 		 * find an ilm with zero address on to_ill, but we may not
16773 		 * have nominated to_ill for receiving. Thus, if we have
16774 		 * nominated from_ill (ill_join_allmulti is set), nominate
16775 		 * only if to_ill is not already nominated (to_ill normally
16776 		 * should not have been nominated if "from_ill" has already
16777 		 * been nominated. As we don't prevent failovers from happening
16778 		 * across groups, we don't assert).
16779 		 */
16780 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16781 			/*
16782 			 * There is no need to hold ill locks as we are
16783 			 * writer on both ills and when ill_join_allmulti
16784 			 * is changed the thread is always a writer.
16785 			 */
16786 			if (from_ill->ill_join_allmulti &&
16787 			    !to_ill->ill_join_allmulti) {
16788 				(void) ip_join_allmulti(to_ill->ill_ipif);
16789 			}
16790 		} else if (ilm->ilm_notify_driver) {
16791 
16792 			/*
16793 			 * This is a newly moved ilm so we need to tell the
16794 			 * driver about the new group. There can be more than
16795 			 * one ilm's for the same group in the list each with a
16796 			 * different orig_ifindex. We have to inform the driver
16797 			 * once. In ilm_move_v[4,6] we only set the flag
16798 			 * ilm_notify_driver for the first ilm.
16799 			 */
16800 
16801 			(void) ip_ll_send_enabmulti_req(to_ill,
16802 			    &ilm->ilm_v6addr);
16803 		}
16804 
16805 		ilm->ilm_notify_driver = B_FALSE;
16806 
16807 		/*
16808 		 * See whether we need to send down DL_DISABMULTI_REQ on
16809 		 * from_ill as ilm has just been removed.
16810 		 */
16811 from:
16812 		ipif = from_ill->ill_ipif;
16813 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16814 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16815 			ip1dbg(("ilm_send_multicast_reqs: "
16816 			    "from_ill not resolver\n"));
16817 			continue;		/* Must be IRE_IF_NORESOLVER */
16818 		}
16819 
16820 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16821 			ip1dbg(("ilm_send_multicast_reqs: "
16822 			    "from_ill MULTI_BCAST\n"));
16823 			continue;
16824 		}
16825 
16826 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16827 			if (from_ill->ill_join_allmulti)
16828 				(void) ip_leave_allmulti(from_ill->ill_ipif);
16829 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16830 			(void) ip_ll_send_disabmulti_req(from_ill,
16831 			    &ilm->ilm_v6addr);
16832 		}
16833 	}
16834 	ILM_WALKER_RELE(to_ill);
16835 }
16836 
16837 /*
16838  * This function is called when all multicast memberships needs
16839  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16840  * called only once unlike the IPv4 counterpart where it is called after
16841  * every logical interface is moved. The reason is due to multicast
16842  * memberships are joined using an interface address in IPv4 while in
16843  * IPv6, interface index is used.
16844  */
16845 static void
16846 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16847 {
16848 	ilm_t	*ilm;
16849 	ilm_t	*ilm_next;
16850 	ilm_t	*new_ilm;
16851 	ilm_t	**ilmp;
16852 	int	count;
16853 	char buf[INET6_ADDRSTRLEN];
16854 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16855 	ip_stack_t	*ipst = from_ill->ill_ipst;
16856 
16857 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16858 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16859 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16860 
16861 	if (ifindex == 0) {
16862 		/*
16863 		 * Form the solicited node mcast address which is used later.
16864 		 */
16865 		ipif_t *ipif;
16866 
16867 		ipif = from_ill->ill_ipif;
16868 		ASSERT(ipif->ipif_id == 0);
16869 
16870 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16871 	}
16872 
16873 	ilmp = &from_ill->ill_ilm;
16874 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16875 		ilm_next = ilm->ilm_next;
16876 
16877 		if (ilm->ilm_flags & ILM_DELETED) {
16878 			ilmp = &ilm->ilm_next;
16879 			continue;
16880 		}
16881 
16882 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16883 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16884 		ASSERT(ilm->ilm_orig_ifindex != 0);
16885 		if (ilm->ilm_orig_ifindex == ifindex) {
16886 			/*
16887 			 * We are failing back multicast memberships.
16888 			 * If the same ilm exists in to_ill, it means somebody
16889 			 * has joined the same group there e.g. ff02::1
16890 			 * is joined within the kernel when the interfaces
16891 			 * came UP.
16892 			 */
16893 			ASSERT(ilm->ilm_ipif == NULL);
16894 			if (new_ilm != NULL) {
16895 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16896 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16897 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16898 					new_ilm->ilm_is_new = B_TRUE;
16899 				}
16900 			} else {
16901 				/*
16902 				 * check if we can just move the ilm
16903 				 */
16904 				if (from_ill->ill_ilm_walker_cnt != 0) {
16905 					/*
16906 					 * We have walkers we cannot move
16907 					 * the ilm, so allocate a new ilm,
16908 					 * this (old) ilm will be marked
16909 					 * ILM_DELETED at the end of the loop
16910 					 * and will be freed when the
16911 					 * last walker exits.
16912 					 */
16913 					new_ilm = (ilm_t *)mi_zalloc
16914 					    (sizeof (ilm_t));
16915 					if (new_ilm == NULL) {
16916 						ip0dbg(("ilm_move_v6: "
16917 						    "FAILBACK of IPv6"
16918 						    " multicast address %s : "
16919 						    "from %s to"
16920 						    " %s failed : ENOMEM \n",
16921 						    inet_ntop(AF_INET6,
16922 						    &ilm->ilm_v6addr, buf,
16923 						    sizeof (buf)),
16924 						    from_ill->ill_name,
16925 						    to_ill->ill_name));
16926 
16927 							ilmp = &ilm->ilm_next;
16928 							continue;
16929 					}
16930 					*new_ilm = *ilm;
16931 					/*
16932 					 * we don't want new_ilm linked to
16933 					 * ilm's filter list.
16934 					 */
16935 					new_ilm->ilm_filter = NULL;
16936 				} else {
16937 					/*
16938 					 * No walkers we can move the ilm.
16939 					 * lets take it out of the list.
16940 					 */
16941 					*ilmp = ilm->ilm_next;
16942 					ilm->ilm_next = NULL;
16943 					new_ilm = ilm;
16944 				}
16945 
16946 				/*
16947 				 * if this is the first ilm for the group
16948 				 * set ilm_notify_driver so that we notify the
16949 				 * driver in ilm_send_multicast_reqs.
16950 				 */
16951 				if (ilm_lookup_ill_v6(to_ill,
16952 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16953 					new_ilm->ilm_notify_driver = B_TRUE;
16954 
16955 				new_ilm->ilm_ill = to_ill;
16956 				/* Add to the to_ill's list */
16957 				new_ilm->ilm_next = to_ill->ill_ilm;
16958 				to_ill->ill_ilm = new_ilm;
16959 				/*
16960 				 * set the flag so that mld_joingroup is
16961 				 * called in ilm_send_multicast_reqs().
16962 				 */
16963 				new_ilm->ilm_is_new = B_TRUE;
16964 			}
16965 			goto bottom;
16966 		} else if (ifindex != 0) {
16967 			/*
16968 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16969 			 * has not matched above, look at the next ilm.
16970 			 */
16971 			ilmp = &ilm->ilm_next;
16972 			continue;
16973 		}
16974 		/*
16975 		 * If we are here, it means ifindex is 0. Failover
16976 		 * everything.
16977 		 *
16978 		 * We need to handle solicited node mcast address
16979 		 * and all_nodes mcast address differently as they
16980 		 * are joined witin the kenrel (ipif_multicast_up)
16981 		 * and potentially from the userland. We are called
16982 		 * after the ipifs of from_ill has been moved.
16983 		 * If we still find ilms on ill with solicited node
16984 		 * mcast address or all_nodes mcast address, it must
16985 		 * belong to the UP interface that has not moved e.g.
16986 		 * ipif_id 0 with the link local prefix does not move.
16987 		 * We join this on the new ill accounting for all the
16988 		 * userland memberships so that applications don't
16989 		 * see any failure.
16990 		 *
16991 		 * We need to make sure that we account only for the
16992 		 * solicited node and all node multicast addresses
16993 		 * that was brought UP on these. In the case of
16994 		 * a failover from A to B, we might have ilms belonging
16995 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
16996 		 * for the membership from the userland. If we are failing
16997 		 * over from B to C now, we will find the ones belonging
16998 		 * to A on B. These don't account for the ill_ipif_up_count.
16999 		 * They just move from B to C. The check below on
17000 		 * ilm_orig_ifindex ensures that.
17001 		 */
17002 		if ((ilm->ilm_orig_ifindex ==
17003 		    from_ill->ill_phyint->phyint_ifindex) &&
17004 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
17005 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
17006 		    &ilm->ilm_v6addr))) {
17007 			ASSERT(ilm->ilm_refcnt > 0);
17008 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
17009 			/*
17010 			 * For indentation reasons, we are not using a
17011 			 * "else" here.
17012 			 */
17013 			if (count == 0) {
17014 				ilmp = &ilm->ilm_next;
17015 				continue;
17016 			}
17017 			ilm->ilm_refcnt -= count;
17018 			if (new_ilm != NULL) {
17019 				/*
17020 				 * Can find one with the same
17021 				 * ilm_orig_ifindex, if we are failing
17022 				 * over to a STANDBY. This happens
17023 				 * when somebody wants to join a group
17024 				 * on a STANDBY interface and we
17025 				 * internally join on a different one.
17026 				 * If we had joined on from_ill then, a
17027 				 * failover now will find a new ilm
17028 				 * with this index.
17029 				 */
17030 				ip1dbg(("ilm_move_v6: FAILOVER, found"
17031 				    " new ilm on %s, group address %s\n",
17032 				    to_ill->ill_name,
17033 				    inet_ntop(AF_INET6,
17034 				    &ilm->ilm_v6addr, buf,
17035 				    sizeof (buf))));
17036 				new_ilm->ilm_refcnt += count;
17037 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17038 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17039 					new_ilm->ilm_is_new = B_TRUE;
17040 				}
17041 			} else {
17042 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17043 				if (new_ilm == NULL) {
17044 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17045 					    " multicast address %s : from %s to"
17046 					    " %s failed : ENOMEM \n",
17047 					    inet_ntop(AF_INET6,
17048 					    &ilm->ilm_v6addr, buf,
17049 					    sizeof (buf)), from_ill->ill_name,
17050 					    to_ill->ill_name));
17051 					ilmp = &ilm->ilm_next;
17052 					continue;
17053 				}
17054 				*new_ilm = *ilm;
17055 				new_ilm->ilm_filter = NULL;
17056 				new_ilm->ilm_refcnt = count;
17057 				new_ilm->ilm_timer = INFINITY;
17058 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17059 				new_ilm->ilm_is_new = B_TRUE;
17060 				/*
17061 				 * If the to_ill has not joined this
17062 				 * group we need to tell the driver in
17063 				 * ill_send_multicast_reqs.
17064 				 */
17065 				if (ilm_lookup_ill_v6(to_ill,
17066 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17067 					new_ilm->ilm_notify_driver = B_TRUE;
17068 
17069 				new_ilm->ilm_ill = to_ill;
17070 				/* Add to the to_ill's list */
17071 				new_ilm->ilm_next = to_ill->ill_ilm;
17072 				to_ill->ill_ilm = new_ilm;
17073 				ASSERT(new_ilm->ilm_ipif == NULL);
17074 			}
17075 			if (ilm->ilm_refcnt == 0) {
17076 				goto bottom;
17077 			} else {
17078 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17079 				CLEAR_SLIST(new_ilm->ilm_filter);
17080 				ilmp = &ilm->ilm_next;
17081 			}
17082 			continue;
17083 		} else {
17084 			/*
17085 			 * ifindex = 0 means, move everything pointing at
17086 			 * from_ill. We are doing this becuase ill has
17087 			 * either FAILED or became INACTIVE.
17088 			 *
17089 			 * As we would like to move things later back to
17090 			 * from_ill, we want to retain the identity of this
17091 			 * ilm. Thus, we don't blindly increment the reference
17092 			 * count on the ilms matching the address alone. We
17093 			 * need to match on the ilm_orig_index also. new_ilm
17094 			 * was obtained by matching ilm_orig_index also.
17095 			 */
17096 			if (new_ilm != NULL) {
17097 				/*
17098 				 * This is possible only if a previous restore
17099 				 * was incomplete i.e restore to
17100 				 * ilm_orig_ifindex left some ilms because
17101 				 * of some failures. Thus when we are failing
17102 				 * again, we might find our old friends there.
17103 				 */
17104 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17105 				    " on %s, group address %s\n",
17106 				    to_ill->ill_name,
17107 				    inet_ntop(AF_INET6,
17108 				    &ilm->ilm_v6addr, buf,
17109 				    sizeof (buf))));
17110 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17111 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17112 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17113 					new_ilm->ilm_is_new = B_TRUE;
17114 				}
17115 			} else {
17116 				if (from_ill->ill_ilm_walker_cnt != 0) {
17117 					new_ilm = (ilm_t *)
17118 					    mi_zalloc(sizeof (ilm_t));
17119 					if (new_ilm == NULL) {
17120 						ip0dbg(("ilm_move_v6: "
17121 						    "FAILOVER of IPv6"
17122 						    " multicast address %s : "
17123 						    "from %s to"
17124 						    " %s failed : ENOMEM \n",
17125 						    inet_ntop(AF_INET6,
17126 						    &ilm->ilm_v6addr, buf,
17127 						    sizeof (buf)),
17128 						    from_ill->ill_name,
17129 						    to_ill->ill_name));
17130 
17131 							ilmp = &ilm->ilm_next;
17132 							continue;
17133 					}
17134 					*new_ilm = *ilm;
17135 					new_ilm->ilm_filter = NULL;
17136 				} else {
17137 					*ilmp = ilm->ilm_next;
17138 					new_ilm = ilm;
17139 				}
17140 				/*
17141 				 * If the to_ill has not joined this
17142 				 * group we need to tell the driver in
17143 				 * ill_send_multicast_reqs.
17144 				 */
17145 				if (ilm_lookup_ill_v6(to_ill,
17146 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17147 					new_ilm->ilm_notify_driver = B_TRUE;
17148 
17149 				/* Add to the to_ill's list */
17150 				new_ilm->ilm_next = to_ill->ill_ilm;
17151 				to_ill->ill_ilm = new_ilm;
17152 				ASSERT(ilm->ilm_ipif == NULL);
17153 				new_ilm->ilm_ill = to_ill;
17154 				new_ilm->ilm_is_new = B_TRUE;
17155 			}
17156 
17157 		}
17158 
17159 bottom:
17160 		/*
17161 		 * Revert multicast filter state to (EXCLUDE, NULL).
17162 		 * new_ilm->ilm_is_new should already be set if needed.
17163 		 */
17164 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17165 		CLEAR_SLIST(new_ilm->ilm_filter);
17166 		/*
17167 		 * We allocated/got a new ilm, free the old one.
17168 		 */
17169 		if (new_ilm != ilm) {
17170 			if (from_ill->ill_ilm_walker_cnt == 0) {
17171 				*ilmp = ilm->ilm_next;
17172 				ilm->ilm_next = NULL;
17173 				FREE_SLIST(ilm->ilm_filter);
17174 				FREE_SLIST(ilm->ilm_pendsrcs);
17175 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17176 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17177 				mi_free((char *)ilm);
17178 			} else {
17179 				ilm->ilm_flags |= ILM_DELETED;
17180 				from_ill->ill_ilm_cleanup_reqd = 1;
17181 				ilmp = &ilm->ilm_next;
17182 			}
17183 		}
17184 	}
17185 }
17186 
17187 /*
17188  * Move all the multicast memberships to to_ill. Called when
17189  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17190  * different from IPv6 counterpart as multicast memberships are associated
17191  * with ills in IPv6. This function is called after every ipif is moved
17192  * unlike IPv6, where it is moved only once.
17193  */
17194 static void
17195 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17196 {
17197 	ilm_t	*ilm;
17198 	ilm_t	*ilm_next;
17199 	ilm_t	*new_ilm;
17200 	ilm_t	**ilmp;
17201 	ip_stack_t	*ipst = from_ill->ill_ipst;
17202 
17203 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17204 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17205 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17206 
17207 	ilmp = &from_ill->ill_ilm;
17208 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17209 		ilm_next = ilm->ilm_next;
17210 
17211 		if (ilm->ilm_flags & ILM_DELETED) {
17212 			ilmp = &ilm->ilm_next;
17213 			continue;
17214 		}
17215 
17216 		ASSERT(ilm->ilm_ipif != NULL);
17217 
17218 		if (ilm->ilm_ipif != ipif) {
17219 			ilmp = &ilm->ilm_next;
17220 			continue;
17221 		}
17222 
17223 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17224 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17225 			new_ilm = ilm_lookup_ipif(ipif,
17226 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17227 			if (new_ilm != NULL) {
17228 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17229 				/*
17230 				 * We still need to deal with the from_ill.
17231 				 */
17232 				new_ilm->ilm_is_new = B_TRUE;
17233 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17234 				CLEAR_SLIST(new_ilm->ilm_filter);
17235 				goto delete_ilm;
17236 			}
17237 			/*
17238 			 * If we could not find one e.g. ipif is
17239 			 * still down on to_ill, we add this ilm
17240 			 * on ill_new to preserve the reference
17241 			 * count.
17242 			 */
17243 		}
17244 		/*
17245 		 * When ipifs move, ilms always move with it
17246 		 * to the NEW ill. Thus we should never be
17247 		 * able to find ilm till we really move it here.
17248 		 */
17249 		ASSERT(ilm_lookup_ipif(ipif,
17250 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17251 
17252 		if (from_ill->ill_ilm_walker_cnt != 0) {
17253 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17254 			if (new_ilm == NULL) {
17255 				char buf[INET6_ADDRSTRLEN];
17256 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17257 				    " multicast address %s : "
17258 				    "from %s to"
17259 				    " %s failed : ENOMEM \n",
17260 				    inet_ntop(AF_INET,
17261 				    &ilm->ilm_v6addr, buf,
17262 				    sizeof (buf)),
17263 				    from_ill->ill_name,
17264 				    to_ill->ill_name));
17265 
17266 				ilmp = &ilm->ilm_next;
17267 				continue;
17268 			}
17269 			*new_ilm = *ilm;
17270 			/* We don't want new_ilm linked to ilm's filter list */
17271 			new_ilm->ilm_filter = NULL;
17272 		} else {
17273 			/* Remove from the list */
17274 			*ilmp = ilm->ilm_next;
17275 			new_ilm = ilm;
17276 		}
17277 
17278 		/*
17279 		 * If we have never joined this group on the to_ill
17280 		 * make sure we tell the driver.
17281 		 */
17282 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17283 		    ALL_ZONES) == NULL)
17284 			new_ilm->ilm_notify_driver = B_TRUE;
17285 
17286 		/* Add to the to_ill's list */
17287 		new_ilm->ilm_next = to_ill->ill_ilm;
17288 		to_ill->ill_ilm = new_ilm;
17289 		new_ilm->ilm_is_new = B_TRUE;
17290 
17291 		/*
17292 		 * Revert multicast filter state to (EXCLUDE, NULL)
17293 		 */
17294 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17295 		CLEAR_SLIST(new_ilm->ilm_filter);
17296 
17297 		/*
17298 		 * Delete only if we have allocated a new ilm.
17299 		 */
17300 		if (new_ilm != ilm) {
17301 delete_ilm:
17302 			if (from_ill->ill_ilm_walker_cnt == 0) {
17303 				/* Remove from the list */
17304 				*ilmp = ilm->ilm_next;
17305 				ilm->ilm_next = NULL;
17306 				FREE_SLIST(ilm->ilm_filter);
17307 				FREE_SLIST(ilm->ilm_pendsrcs);
17308 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17309 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17310 				mi_free((char *)ilm);
17311 			} else {
17312 				ilm->ilm_flags |= ILM_DELETED;
17313 				from_ill->ill_ilm_cleanup_reqd = 1;
17314 				ilmp = &ilm->ilm_next;
17315 			}
17316 		}
17317 	}
17318 }
17319 
17320 static uint_t
17321 ipif_get_id(ill_t *ill, uint_t id)
17322 {
17323 	uint_t	unit;
17324 	ipif_t	*tipif;
17325 	boolean_t found = B_FALSE;
17326 	ip_stack_t	*ipst = ill->ill_ipst;
17327 
17328 	/*
17329 	 * During failback, we want to go back to the same id
17330 	 * instead of the smallest id so that the original
17331 	 * configuration is maintained. id is non-zero in that
17332 	 * case.
17333 	 */
17334 	if (id != 0) {
17335 		/*
17336 		 * While failing back, if we still have an ipif with
17337 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17338 		 * as soon as we return from this function. It was
17339 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17340 		 * we can choose the smallest id. Thus we return zero
17341 		 * in that case ignoring the hint.
17342 		 */
17343 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17344 			return (0);
17345 		for (tipif = ill->ill_ipif; tipif != NULL;
17346 		    tipif = tipif->ipif_next) {
17347 			if (tipif->ipif_id == id) {
17348 				found = B_TRUE;
17349 				break;
17350 			}
17351 		}
17352 		/*
17353 		 * If somebody already plumbed another logical
17354 		 * with the same id, we won't be able to find it.
17355 		 */
17356 		if (!found)
17357 			return (id);
17358 	}
17359 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17360 		found = B_FALSE;
17361 		for (tipif = ill->ill_ipif; tipif != NULL;
17362 		    tipif = tipif->ipif_next) {
17363 			if (tipif->ipif_id == unit) {
17364 				found = B_TRUE;
17365 				break;
17366 			}
17367 		}
17368 		if (!found)
17369 			break;
17370 	}
17371 	return (unit);
17372 }
17373 
17374 /* ARGSUSED */
17375 static int
17376 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17377     ipif_t **rep_ipif_ptr)
17378 {
17379 	ill_t	*from_ill;
17380 	ipif_t	*rep_ipif;
17381 	uint_t	unit;
17382 	int err = 0;
17383 	ipif_t	*to_ipif;
17384 	struct iocblk	*iocp;
17385 	boolean_t failback_cmd;
17386 	boolean_t remove_ipif;
17387 	int	rc;
17388 	ip_stack_t	*ipst;
17389 
17390 	ASSERT(IAM_WRITER_ILL(to_ill));
17391 	ASSERT(IAM_WRITER_IPIF(ipif));
17392 
17393 	iocp = (struct iocblk *)mp->b_rptr;
17394 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17395 	remove_ipif = B_FALSE;
17396 
17397 	from_ill = ipif->ipif_ill;
17398 	ipst = from_ill->ill_ipst;
17399 
17400 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17401 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17402 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17403 
17404 	/*
17405 	 * Don't move LINK LOCAL addresses as they are tied to
17406 	 * physical interface.
17407 	 */
17408 	if (from_ill->ill_isv6 &&
17409 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17410 		ipif->ipif_was_up = B_FALSE;
17411 		IPIF_UNMARK_MOVING(ipif);
17412 		return (0);
17413 	}
17414 
17415 	/*
17416 	 * We set the ipif_id to maximum so that the search for
17417 	 * ipif_id will pick the lowest number i.e 0 in the
17418 	 * following 2 cases :
17419 	 *
17420 	 * 1) We have a replacement ipif at the head of to_ill.
17421 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17422 	 *    on to_ill and hence the MOVE might fail. We want to
17423 	 *    remove it only if we could move the ipif. Thus, by
17424 	 *    setting it to the MAX value, we make the search in
17425 	 *    ipif_get_id return the zeroth id.
17426 	 *
17427 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17428 	 *    we might just have a zero address plumbed on the ipif
17429 	 *    with zero id in the case of IPv4. We remove that while
17430 	 *    doing the failback. We want to remove it only if we
17431 	 *    could move the ipif. Thus, by setting it to the MAX
17432 	 *    value, we make the search in ipif_get_id return the
17433 	 *    zeroth id.
17434 	 *
17435 	 * Both (1) and (2) are done only when when we are moving
17436 	 * an ipif (either due to failover/failback) which originally
17437 	 * belonged to this interface i.e the ipif_orig_ifindex is
17438 	 * the same as to_ill's ifindex. This is needed so that
17439 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17440 	 * from B -> A (B is being removed from the group) and
17441 	 * FAILBACK from A -> B restores the original configuration.
17442 	 * Without the check for orig_ifindex, the second FAILOVER
17443 	 * could make the ipif belonging to B replace the A's zeroth
17444 	 * ipif and the subsequent failback re-creating the replacement
17445 	 * ipif again.
17446 	 *
17447 	 * NOTE : We created the replacement ipif when we did a
17448 	 * FAILOVER (See below). We could check for FAILBACK and
17449 	 * then look for replacement ipif to be removed. But we don't
17450 	 * want to do that because we wan't to allow the possibility
17451 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17452 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17453 	 * from B -> A.
17454 	 */
17455 	to_ipif = to_ill->ill_ipif;
17456 	if ((to_ill->ill_phyint->phyint_ifindex ==
17457 	    ipif->ipif_orig_ifindex) &&
17458 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17459 		ASSERT(to_ipif->ipif_id == 0);
17460 		remove_ipif = B_TRUE;
17461 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17462 	}
17463 	/*
17464 	 * Find the lowest logical unit number on the to_ill.
17465 	 * If we are failing back, try to get the original id
17466 	 * rather than the lowest one so that the original
17467 	 * configuration is maintained.
17468 	 *
17469 	 * XXX need a better scheme for this.
17470 	 */
17471 	if (failback_cmd) {
17472 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17473 	} else {
17474 		unit = ipif_get_id(to_ill, 0);
17475 	}
17476 
17477 	/* Reset back to zero in case we fail below */
17478 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17479 		to_ipif->ipif_id = 0;
17480 
17481 	if (unit == ipst->ips_ip_addrs_per_if) {
17482 		ipif->ipif_was_up = B_FALSE;
17483 		IPIF_UNMARK_MOVING(ipif);
17484 		return (EINVAL);
17485 	}
17486 
17487 	/*
17488 	 * ipif is ready to move from "from_ill" to "to_ill".
17489 	 *
17490 	 * 1) If we are moving ipif with id zero, create a
17491 	 *    replacement ipif for this ipif on from_ill. If this fails
17492 	 *    fail the MOVE operation.
17493 	 *
17494 	 * 2) Remove the replacement ipif on to_ill if any.
17495 	 *    We could remove the replacement ipif when we are moving
17496 	 *    the ipif with id zero. But what if somebody already
17497 	 *    unplumbed it ? Thus we always remove it if it is present.
17498 	 *    We want to do it only if we are sure we are going to
17499 	 *    move the ipif to to_ill which is why there are no
17500 	 *    returns due to error till ipif is linked to to_ill.
17501 	 *    Note that the first ipif that we failback will always
17502 	 *    be zero if it is present.
17503 	 */
17504 	if (ipif->ipif_id == 0) {
17505 		ipaddr_t inaddr_any = INADDR_ANY;
17506 
17507 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17508 		if (rep_ipif == NULL) {
17509 			ipif->ipif_was_up = B_FALSE;
17510 			IPIF_UNMARK_MOVING(ipif);
17511 			return (ENOMEM);
17512 		}
17513 		*rep_ipif = ipif_zero;
17514 		/*
17515 		 * Before we put the ipif on the list, store the addresses
17516 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17517 		 * assumes so. This logic is not any different from what
17518 		 * ipif_allocate does.
17519 		 */
17520 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17521 		    &rep_ipif->ipif_v6lcl_addr);
17522 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17523 		    &rep_ipif->ipif_v6src_addr);
17524 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17525 		    &rep_ipif->ipif_v6subnet);
17526 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17527 		    &rep_ipif->ipif_v6net_mask);
17528 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17529 		    &rep_ipif->ipif_v6brd_addr);
17530 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17531 		    &rep_ipif->ipif_v6pp_dst_addr);
17532 		/*
17533 		 * We mark IPIF_NOFAILOVER so that this can never
17534 		 * move.
17535 		 */
17536 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17537 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17538 		rep_ipif->ipif_replace_zero = B_TRUE;
17539 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17540 		    MUTEX_DEFAULT, NULL);
17541 		rep_ipif->ipif_id = 0;
17542 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17543 		rep_ipif->ipif_ill = from_ill;
17544 		rep_ipif->ipif_orig_ifindex =
17545 		    from_ill->ill_phyint->phyint_ifindex;
17546 		/* Insert at head */
17547 		rep_ipif->ipif_next = from_ill->ill_ipif;
17548 		from_ill->ill_ipif = rep_ipif;
17549 		/*
17550 		 * We don't really care to let apps know about
17551 		 * this interface.
17552 		 */
17553 	}
17554 
17555 	if (remove_ipif) {
17556 		/*
17557 		 * We set to a max value above for this case to get
17558 		 * id zero. ASSERT that we did get one.
17559 		 */
17560 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17561 		rep_ipif = to_ipif;
17562 		to_ill->ill_ipif = rep_ipif->ipif_next;
17563 		rep_ipif->ipif_next = NULL;
17564 		/*
17565 		 * If some apps scanned and find this interface,
17566 		 * it is time to let them know, so that they can
17567 		 * delete it.
17568 		 */
17569 
17570 		*rep_ipif_ptr = rep_ipif;
17571 	}
17572 
17573 	/* Get it out of the ILL interface list. */
17574 	ipif_remove(ipif, B_FALSE);
17575 
17576 	/* Assign the new ill */
17577 	ipif->ipif_ill = to_ill;
17578 	ipif->ipif_id = unit;
17579 	/* id has already been checked */
17580 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17581 	ASSERT(rc == 0);
17582 	/* Let SCTP update its list */
17583 	sctp_move_ipif(ipif, from_ill, to_ill);
17584 	/*
17585 	 * Handle the failover and failback of ipif_t between
17586 	 * ill_t that have differing maximum mtu values.
17587 	 */
17588 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17589 		if (ipif->ipif_saved_mtu == 0) {
17590 			/*
17591 			 * As this ipif_t is moving to an ill_t
17592 			 * that has a lower ill_max_mtu, its
17593 			 * ipif_mtu needs to be saved so it can
17594 			 * be restored during failback or during
17595 			 * failover to an ill_t which has a
17596 			 * higher ill_max_mtu.
17597 			 */
17598 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17599 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17600 		} else {
17601 			/*
17602 			 * The ipif_t is, once again, moving to
17603 			 * an ill_t that has a lower maximum mtu
17604 			 * value.
17605 			 */
17606 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17607 		}
17608 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17609 	    ipif->ipif_saved_mtu != 0) {
17610 		/*
17611 		 * The mtu of this ipif_t had to be reduced
17612 		 * during an earlier failover; this is an
17613 		 * opportunity for it to be increased (either as
17614 		 * part of another failover or a failback).
17615 		 */
17616 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17617 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17618 			ipif->ipif_saved_mtu = 0;
17619 		} else {
17620 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17621 		}
17622 	}
17623 
17624 	/*
17625 	 * We preserve all the other fields of the ipif including
17626 	 * ipif_saved_ire_mp. The routes that are saved here will
17627 	 * be recreated on the new interface and back on the old
17628 	 * interface when we move back.
17629 	 */
17630 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17631 
17632 	return (err);
17633 }
17634 
17635 static int
17636 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17637     int ifindex, ipif_t **rep_ipif_ptr)
17638 {
17639 	ipif_t *mipif;
17640 	ipif_t *ipif_next;
17641 	int err;
17642 
17643 	/*
17644 	 * We don't really try to MOVE back things if some of the
17645 	 * operations fail. The daemon will take care of moving again
17646 	 * later on.
17647 	 */
17648 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17649 		ipif_next = mipif->ipif_next;
17650 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17651 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17652 
17653 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17654 
17655 			/*
17656 			 * When the MOVE fails, it is the job of the
17657 			 * application to take care of this properly
17658 			 * i.e try again if it is ENOMEM.
17659 			 */
17660 			if (mipif->ipif_ill != from_ill) {
17661 				/*
17662 				 * ipif has moved.
17663 				 *
17664 				 * Move the multicast memberships associated
17665 				 * with this ipif to the new ill. For IPv6, we
17666 				 * do it once after all the ipifs are moved
17667 				 * (in ill_move) as they are not associated
17668 				 * with ipifs.
17669 				 *
17670 				 * We need to move the ilms as the ipif has
17671 				 * already been moved to a new ill even
17672 				 * in the case of errors. Neither
17673 				 * ilm_free(ipif) will find the ilm
17674 				 * when somebody unplumbs this ipif nor
17675 				 * ilm_delete(ilm) will be able to find the
17676 				 * ilm, if we don't move now.
17677 				 */
17678 				if (!from_ill->ill_isv6)
17679 					ilm_move_v4(from_ill, to_ill, mipif);
17680 			}
17681 
17682 			if (err != 0)
17683 				return (err);
17684 		}
17685 	}
17686 	return (0);
17687 }
17688 
17689 static int
17690 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17691 {
17692 	int ifindex;
17693 	int err;
17694 	struct iocblk	*iocp;
17695 	ipif_t	*ipif;
17696 	ipif_t *rep_ipif_ptr = NULL;
17697 	ipif_t	*from_ipif = NULL;
17698 	boolean_t check_rep_if = B_FALSE;
17699 	ip_stack_t	*ipst = from_ill->ill_ipst;
17700 
17701 	iocp = (struct iocblk *)mp->b_rptr;
17702 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17703 		/*
17704 		 * Move everything pointing at from_ill to to_ill.
17705 		 * We acheive this by passing in 0 as ifindex.
17706 		 */
17707 		ifindex = 0;
17708 	} else {
17709 		/*
17710 		 * Move everything pointing at from_ill whose original
17711 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17712 		 * We acheive this by passing in ifindex rather than 0.
17713 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17714 		 */
17715 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17716 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17717 	}
17718 
17719 	/*
17720 	 * Determine if there is at least one ipif that would move from
17721 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17722 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17723 	 * the move, in which case we need to quiesce the replacement ipif also.
17724 	 */
17725 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17726 	    from_ipif = from_ipif->ipif_next) {
17727 		if (((ifindex == 0) ||
17728 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17729 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17730 			check_rep_if = B_TRUE;
17731 			break;
17732 		}
17733 	}
17734 
17735 
17736 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17737 
17738 	GRAB_ILL_LOCKS(from_ill, to_ill);
17739 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17740 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17741 		    mp, ILL_MOVE_OK);
17742 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17743 		return (EINPROGRESS);
17744 	}
17745 
17746 	/* Check if the replacement ipif is quiescent to delete */
17747 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17748 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17749 		to_ill->ill_ipif->ipif_state_flags |=
17750 		    IPIF_MOVING | IPIF_CHANGING;
17751 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17752 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17753 			    mp, ILL_MOVE_OK);
17754 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17755 			return (EINPROGRESS);
17756 		}
17757 	}
17758 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17759 
17760 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17761 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17762 	GRAB_ILL_LOCKS(from_ill, to_ill);
17763 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17764 
17765 	/* ilm_move is done inside ipif_move for IPv4 */
17766 	if (err == 0 && from_ill->ill_isv6)
17767 		ilm_move_v6(from_ill, to_ill, ifindex);
17768 
17769 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17770 	rw_exit(&ipst->ips_ill_g_lock);
17771 
17772 	/*
17773 	 * send rts messages and multicast messages.
17774 	 */
17775 	if (rep_ipif_ptr != NULL) {
17776 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17777 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17778 			rep_ipif_ptr->ipif_recovery_id = 0;
17779 		}
17780 		ip_rts_ifmsg(rep_ipif_ptr);
17781 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17782 #ifdef DEBUG
17783 		ipif_trace_cleanup(rep_ipif_ptr);
17784 #endif
17785 		mi_free(rep_ipif_ptr);
17786 	}
17787 
17788 	conn_move_ill(from_ill, to_ill, ifindex);
17789 
17790 	return (err);
17791 }
17792 
17793 /*
17794  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17795  * Also checks for the validity of the arguments.
17796  * Note: We are already exclusive inside the from group.
17797  * It is upto the caller to release refcnt on the to_ill's.
17798  */
17799 static int
17800 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17801     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17802 {
17803 	int dst_index;
17804 	ipif_t *ipif_v4, *ipif_v6;
17805 	struct lifreq *lifr;
17806 	mblk_t *mp1;
17807 	boolean_t exists;
17808 	sin_t	*sin;
17809 	int	err = 0;
17810 	ip_stack_t	*ipst;
17811 
17812 	if (CONN_Q(q))
17813 		ipst = CONNQ_TO_IPST(q);
17814 	else
17815 		ipst = ILLQ_TO_IPST(q);
17816 
17817 
17818 	if ((mp1 = mp->b_cont) == NULL)
17819 		return (EPROTO);
17820 
17821 	if ((mp1 = mp1->b_cont) == NULL)
17822 		return (EPROTO);
17823 
17824 	lifr = (struct lifreq *)mp1->b_rptr;
17825 	sin = (sin_t *)&lifr->lifr_addr;
17826 
17827 	/*
17828 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17829 	 * specific operations.
17830 	 */
17831 	if (sin->sin_family != AF_UNSPEC)
17832 		return (EINVAL);
17833 
17834 	/*
17835 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17836 	 * NULLs for the last 4 args and we know the lookup won't fail
17837 	 * with EINPROGRESS.
17838 	 */
17839 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17840 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17841 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17842 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17843 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17844 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17845 
17846 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17847 		return (ENXIO);
17848 
17849 	if (ipif_v4 != NULL) {
17850 		ASSERT(ipif_v4->ipif_refcnt != 0);
17851 		if (ipif_v4->ipif_id != 0) {
17852 			err = EINVAL;
17853 			goto done;
17854 		}
17855 
17856 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17857 		*ill_from_v4 = ipif_v4->ipif_ill;
17858 	}
17859 
17860 	if (ipif_v6 != NULL) {
17861 		ASSERT(ipif_v6->ipif_refcnt != 0);
17862 		if (ipif_v6->ipif_id != 0) {
17863 			err = EINVAL;
17864 			goto done;
17865 		}
17866 
17867 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17868 		*ill_from_v6 = ipif_v6->ipif_ill;
17869 	}
17870 
17871 	err = 0;
17872 	dst_index = lifr->lifr_movetoindex;
17873 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17874 	    q, mp, ip_process_ioctl, &err, ipst);
17875 	if (err != 0) {
17876 		/*
17877 		 * There could be only v6.
17878 		 */
17879 		if (err != ENXIO)
17880 			goto done;
17881 		err = 0;
17882 	}
17883 
17884 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17885 	    q, mp, ip_process_ioctl, &err, ipst);
17886 	if (err != 0) {
17887 		if (err != ENXIO)
17888 			goto done;
17889 		if (*ill_to_v4 == NULL) {
17890 			err = ENXIO;
17891 			goto done;
17892 		}
17893 		err = 0;
17894 	}
17895 
17896 	/*
17897 	 * If we have something to MOVE i.e "from" not NULL,
17898 	 * "to" should be non-NULL.
17899 	 */
17900 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
17901 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
17902 		err = EINVAL;
17903 	}
17904 
17905 done:
17906 	if (ipif_v4 != NULL)
17907 		ipif_refrele(ipif_v4);
17908 	if (ipif_v6 != NULL)
17909 		ipif_refrele(ipif_v6);
17910 	return (err);
17911 }
17912 
17913 /*
17914  * FAILOVER and FAILBACK are modelled as MOVE operations.
17915  *
17916  * We don't check whether the MOVE is within the same group or
17917  * not, because this ioctl can be used as a generic mechanism
17918  * to failover from interface A to B, though things will function
17919  * only if they are really part of the same group. Moreover,
17920  * all ipifs may be down and hence temporarily out of the group.
17921  *
17922  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17923  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17924  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17925  * have been deleted and there are no active references. Once quiescent the
17926  * ipif's are moved and brought up on the new ill.
17927  *
17928  * Normally the source ill and destination ill belong to the same IPMP group
17929  * and hence the same ipsq_t. In the event they don't belong to the same
17930  * same group the two ipsq's are first merged into one ipsq - that of the
17931  * to_ill. The multicast memberships on the source and destination ill cannot
17932  * change during the move operation since multicast joins/leaves also have to
17933  * execute on the same ipsq and are hence serialized.
17934  */
17935 /* ARGSUSED */
17936 int
17937 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17938     ip_ioctl_cmd_t *ipip, void *ifreq)
17939 {
17940 	ill_t *ill_to_v4 = NULL;
17941 	ill_t *ill_to_v6 = NULL;
17942 	ill_t *ill_from_v4 = NULL;
17943 	ill_t *ill_from_v6 = NULL;
17944 	int err = 0;
17945 
17946 	/*
17947 	 * setup from and to ill's, we can get EINPROGRESS only for
17948 	 * to_ill's.
17949 	 */
17950 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
17951 	    &ill_to_v4, &ill_to_v6);
17952 
17953 	if (err != 0) {
17954 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
17955 		goto done;
17956 	}
17957 
17958 	/*
17959 	 * nothing to do.
17960 	 */
17961 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
17962 		goto done;
17963 	}
17964 
17965 	/*
17966 	 * nothing to do.
17967 	 */
17968 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
17969 		goto done;
17970 	}
17971 
17972 	/*
17973 	 * Mark the ill as changing.
17974 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
17975 	 * in ill_up_ipifs in case of error they are cleared below.
17976 	 */
17977 
17978 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17979 	if (ill_from_v4 != NULL)
17980 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
17981 	if (ill_from_v6 != NULL)
17982 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
17983 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17984 
17985 	/*
17986 	 * Make sure that both src and dst are
17987 	 * in the same syncq group. If not make it happen.
17988 	 * We are not holding any locks because we are the writer
17989 	 * on the from_ipsq and we will hold locks in ill_merge_groups
17990 	 * to protect to_ipsq against changing.
17991 	 */
17992 	if (ill_from_v4 != NULL) {
17993 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
17994 		    ill_to_v4->ill_phyint->phyint_ipsq) {
17995 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
17996 			    NULL, mp, q);
17997 			goto err_ret;
17998 
17999 		}
18000 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
18001 	} else {
18002 
18003 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
18004 		    ill_to_v6->ill_phyint->phyint_ipsq) {
18005 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
18006 			    NULL, mp, q);
18007 			goto err_ret;
18008 
18009 		}
18010 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
18011 	}
18012 
18013 	/*
18014 	 * Now that the ipsq's have been merged and we are the writer
18015 	 * lets mark to_ill as changing as well.
18016 	 */
18017 
18018 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18019 	if (ill_to_v4 != NULL)
18020 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
18021 	if (ill_to_v6 != NULL)
18022 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
18023 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18024 
18025 	/*
18026 	 * Its ok for us to proceed with the move even if
18027 	 * ill_pending_mp is non null on one of the from ill's as the reply
18028 	 * should not be looking at the ipif, it should only care about the
18029 	 * ill itself.
18030 	 */
18031 
18032 	/*
18033 	 * lets move ipv4 first.
18034 	 */
18035 	if (ill_from_v4 != NULL) {
18036 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18037 		ill_from_v4->ill_move_in_progress = B_TRUE;
18038 		ill_to_v4->ill_move_in_progress = B_TRUE;
18039 		ill_to_v4->ill_move_peer = ill_from_v4;
18040 		ill_from_v4->ill_move_peer = ill_to_v4;
18041 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18042 	}
18043 
18044 	/*
18045 	 * Now lets move ipv6.
18046 	 */
18047 	if (err == 0 && ill_from_v6 != NULL) {
18048 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18049 		ill_from_v6->ill_move_in_progress = B_TRUE;
18050 		ill_to_v6->ill_move_in_progress = B_TRUE;
18051 		ill_to_v6->ill_move_peer = ill_from_v6;
18052 		ill_from_v6->ill_move_peer = ill_to_v6;
18053 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18054 	}
18055 
18056 err_ret:
18057 	/*
18058 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18059 	 * moved to become quiescent.
18060 	 */
18061 	if (err == EINPROGRESS) {
18062 		goto done;
18063 	}
18064 
18065 	/*
18066 	 * if err is set ill_up_ipifs will not be called
18067 	 * lets clear the flags.
18068 	 */
18069 
18070 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18071 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18072 	/*
18073 	 * Some of the clearing may be redundant. But it is simple
18074 	 * not making any extra checks.
18075 	 */
18076 	if (ill_from_v6 != NULL) {
18077 		ill_from_v6->ill_move_in_progress = B_FALSE;
18078 		ill_from_v6->ill_move_peer = NULL;
18079 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18080 	}
18081 	if (ill_from_v4 != NULL) {
18082 		ill_from_v4->ill_move_in_progress = B_FALSE;
18083 		ill_from_v4->ill_move_peer = NULL;
18084 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18085 	}
18086 	if (ill_to_v6 != NULL) {
18087 		ill_to_v6->ill_move_in_progress = B_FALSE;
18088 		ill_to_v6->ill_move_peer = NULL;
18089 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18090 	}
18091 	if (ill_to_v4 != NULL) {
18092 		ill_to_v4->ill_move_in_progress = B_FALSE;
18093 		ill_to_v4->ill_move_peer = NULL;
18094 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18095 	}
18096 
18097 	/*
18098 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18099 	 * Do this always to maintain proper state i.e even in case of errors.
18100 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18101 	 * we need not call on both v4 and v6 interfaces.
18102 	 */
18103 	if (ill_from_v4 != NULL) {
18104 		if ((ill_from_v4->ill_phyint->phyint_flags &
18105 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18106 			phyint_inactive(ill_from_v4->ill_phyint);
18107 		}
18108 	} else if (ill_from_v6 != NULL) {
18109 		if ((ill_from_v6->ill_phyint->phyint_flags &
18110 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18111 			phyint_inactive(ill_from_v6->ill_phyint);
18112 		}
18113 	}
18114 
18115 	if (ill_to_v4 != NULL) {
18116 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18117 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18118 		}
18119 	} else if (ill_to_v6 != NULL) {
18120 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18121 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18122 		}
18123 	}
18124 
18125 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18126 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18127 
18128 no_err:
18129 	/*
18130 	 * lets bring the interfaces up on the to_ill.
18131 	 */
18132 	if (err == 0) {
18133 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18134 		    q, mp);
18135 	}
18136 
18137 	if (err == 0) {
18138 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18139 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18140 
18141 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18142 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18143 	}
18144 done:
18145 
18146 	if (ill_to_v4 != NULL) {
18147 		ill_refrele(ill_to_v4);
18148 	}
18149 	if (ill_to_v6 != NULL) {
18150 		ill_refrele(ill_to_v6);
18151 	}
18152 
18153 	return (err);
18154 }
18155 
18156 static void
18157 ill_dl_down(ill_t *ill)
18158 {
18159 	/*
18160 	 * The ill is down; unbind but stay attached since we're still
18161 	 * associated with a PPA. If we have negotiated DLPI capabilites
18162 	 * with the data link service provider (IDS_OK) then reset them.
18163 	 * The interval between unbinding and rebinding is potentially
18164 	 * unbounded hence we cannot assume things will be the same.
18165 	 * The DLPI capabilities will be probed again when the data link
18166 	 * is brought up.
18167 	 */
18168 	mblk_t	*mp = ill->ill_unbind_mp;
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 		    dl_primstr(*(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 		/*
18181 		 * Reset the capabilities if the negotiation is done or is
18182 		 * still in progress. Note that ill_capability_reset() will
18183 		 * set ill_dlpi_capab_state to IDS_UNKNOWN, so the subsequent
18184 		 * DL_CAPABILITY_ACK and DL_NOTE_CAPAB_RENEG will be ignored.
18185 		 *
18186 		 * Further, reset ill_capab_reneg to be B_FALSE so that the
18187 		 * subsequent DL_CAPABILITY_ACK can be ignored, to prevent
18188 		 * the capabilities renegotiation from happening.
18189 		 */
18190 		if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
18191 			ill_capability_reset(ill);
18192 		ill->ill_capab_reneg = B_FALSE;
18193 
18194 		ill_dlpi_send(ill, mp);
18195 	}
18196 
18197 	/*
18198 	 * Toss all of our multicast memberships.  We could keep them, but
18199 	 * then we'd have to do bookkeeping of any joins and leaves performed
18200 	 * by the application while the the interface is down (we can't just
18201 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18202 	 * on a downed interface).
18203 	 */
18204 	ill_leave_multicast(ill);
18205 
18206 	mutex_enter(&ill->ill_lock);
18207 	ill->ill_dl_up = 0;
18208 	(void) ill_hook_event_create(ill, 0, NE_DOWN, NULL, 0);
18209 	mutex_exit(&ill->ill_lock);
18210 }
18211 
18212 static void
18213 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18214 {
18215 	union DL_primitives *dlp;
18216 	t_uscalar_t prim;
18217 
18218 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18219 
18220 	dlp = (union DL_primitives *)mp->b_rptr;
18221 	prim = dlp->dl_primitive;
18222 
18223 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18224 	    dl_primstr(prim), prim, ill->ill_name));
18225 
18226 	switch (prim) {
18227 	case DL_PHYS_ADDR_REQ:
18228 	{
18229 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18230 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18231 		break;
18232 	}
18233 	case DL_BIND_REQ:
18234 		mutex_enter(&ill->ill_lock);
18235 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18236 		mutex_exit(&ill->ill_lock);
18237 		break;
18238 	}
18239 
18240 	/*
18241 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18242 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18243 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18244 	 */
18245 	mutex_enter(&ill->ill_lock);
18246 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18247 	    (prim == DL_UNBIND_REQ)) {
18248 		ill->ill_dlpi_pending = prim;
18249 	}
18250 	mutex_exit(&ill->ill_lock);
18251 
18252 	putnext(ill->ill_wq, mp);
18253 }
18254 
18255 /*
18256  * Helper function for ill_dlpi_send().
18257  */
18258 /* ARGSUSED */
18259 static void
18260 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18261 {
18262 	ill_dlpi_send(q->q_ptr, mp);
18263 }
18264 
18265 /*
18266  * Send a DLPI control message to the driver but make sure there
18267  * is only one outstanding message. Uses ill_dlpi_pending to tell
18268  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18269  * when an ACK or a NAK is received to process the next queued message.
18270  */
18271 void
18272 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18273 {
18274 	mblk_t **mpp;
18275 
18276 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18277 
18278 	/*
18279 	 * To ensure that any DLPI requests for current exclusive operation
18280 	 * are always completely sent before any DLPI messages for other
18281 	 * operations, require writer access before enqueuing.
18282 	 */
18283 	if (!IAM_WRITER_ILL(ill)) {
18284 		ill_refhold(ill);
18285 		/* qwriter_ip() does the ill_refrele() */
18286 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18287 		    NEW_OP, B_TRUE);
18288 		return;
18289 	}
18290 
18291 	mutex_enter(&ill->ill_lock);
18292 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18293 		/* Must queue message. Tail insertion */
18294 		mpp = &ill->ill_dlpi_deferred;
18295 		while (*mpp != NULL)
18296 			mpp = &((*mpp)->b_next);
18297 
18298 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18299 		    ill->ill_name));
18300 
18301 		*mpp = mp;
18302 		mutex_exit(&ill->ill_lock);
18303 		return;
18304 	}
18305 	mutex_exit(&ill->ill_lock);
18306 	ill_dlpi_dispatch(ill, mp);
18307 }
18308 
18309 /*
18310  * Send all deferred DLPI messages without waiting for their ACKs.
18311  */
18312 void
18313 ill_dlpi_send_deferred(ill_t *ill)
18314 {
18315 	mblk_t *mp, *nextmp;
18316 
18317 	/*
18318 	 * Clear ill_dlpi_pending so that the message is not queued in
18319 	 * ill_dlpi_send().
18320 	 */
18321 	mutex_enter(&ill->ill_lock);
18322 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18323 	mp = ill->ill_dlpi_deferred;
18324 	ill->ill_dlpi_deferred = NULL;
18325 	mutex_exit(&ill->ill_lock);
18326 
18327 	for (; mp != NULL; mp = nextmp) {
18328 		nextmp = mp->b_next;
18329 		mp->b_next = NULL;
18330 		ill_dlpi_send(ill, mp);
18331 	}
18332 }
18333 
18334 /*
18335  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18336  */
18337 boolean_t
18338 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18339 {
18340 	t_uscalar_t pending;
18341 
18342 	mutex_enter(&ill->ill_lock);
18343 	if (ill->ill_dlpi_pending == prim) {
18344 		mutex_exit(&ill->ill_lock);
18345 		return (B_TRUE);
18346 	}
18347 
18348 	/*
18349 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
18350 	 * without waiting, so don't print any warnings in that case.
18351 	 */
18352 	if (ill->ill_state_flags & ILL_CONDEMNED) {
18353 		mutex_exit(&ill->ill_lock);
18354 		return (B_FALSE);
18355 	}
18356 	pending = ill->ill_dlpi_pending;
18357 	mutex_exit(&ill->ill_lock);
18358 
18359 	if (pending == DL_PRIM_INVAL) {
18360 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18361 		    "received unsolicited ack for %s on %s\n",
18362 		    dl_primstr(prim), ill->ill_name);
18363 	} else {
18364 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18365 		    "received unexpected ack for %s on %s (expecting %s)\n",
18366 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
18367 	}
18368 	return (B_FALSE);
18369 }
18370 
18371 /*
18372  * Complete the current DLPI operation associated with `prim' on `ill' and
18373  * start the next queued DLPI operation (if any).  If there are no queued DLPI
18374  * operations and the ill's current exclusive IPSQ operation has finished
18375  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
18376  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
18377  * the comments above ipsq_current_finish() for details.
18378  */
18379 void
18380 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18381 {
18382 	mblk_t *mp;
18383 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
18384 
18385 	ASSERT(IAM_WRITER_IPSQ(ipsq));
18386 	mutex_enter(&ill->ill_lock);
18387 
18388 	ASSERT(prim != DL_PRIM_INVAL);
18389 	ASSERT(ill->ill_dlpi_pending == prim);
18390 
18391 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18392 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18393 
18394 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18395 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18396 
18397 		mutex_enter(&ipsq->ipsq_lock);
18398 		if (ipsq->ipsq_current_done)
18399 			ipsq->ipsq_current_ipif = NULL;
18400 		mutex_exit(&ipsq->ipsq_lock);
18401 
18402 		cv_signal(&ill->ill_cv);
18403 		mutex_exit(&ill->ill_lock);
18404 		return;
18405 	}
18406 
18407 	ill->ill_dlpi_deferred = mp->b_next;
18408 	mp->b_next = NULL;
18409 	mutex_exit(&ill->ill_lock);
18410 
18411 	ill_dlpi_dispatch(ill, mp);
18412 }
18413 
18414 void
18415 conn_delete_ire(conn_t *connp, caddr_t arg)
18416 {
18417 	ipif_t	*ipif = (ipif_t *)arg;
18418 	ire_t	*ire;
18419 
18420 	/*
18421 	 * Look at the cached ires on conns which has pointers to ipifs.
18422 	 * We just call ire_refrele which clears up the reference
18423 	 * to ire. Called when a conn closes. Also called from ipif_free
18424 	 * to cleanup indirect references to the stale ipif via the cached ire.
18425 	 */
18426 	mutex_enter(&connp->conn_lock);
18427 	ire = connp->conn_ire_cache;
18428 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18429 		connp->conn_ire_cache = NULL;
18430 		mutex_exit(&connp->conn_lock);
18431 		IRE_REFRELE_NOTR(ire);
18432 		return;
18433 	}
18434 	mutex_exit(&connp->conn_lock);
18435 
18436 }
18437 
18438 /*
18439  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18440  * of IREs. Those IREs may have been previously cached in the conn structure.
18441  * This ipcl_walk() walker function releases all references to such IREs based
18442  * on the condemned flag.
18443  */
18444 /* ARGSUSED */
18445 void
18446 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18447 {
18448 	ire_t	*ire;
18449 
18450 	mutex_enter(&connp->conn_lock);
18451 	ire = connp->conn_ire_cache;
18452 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18453 		connp->conn_ire_cache = NULL;
18454 		mutex_exit(&connp->conn_lock);
18455 		IRE_REFRELE_NOTR(ire);
18456 		return;
18457 	}
18458 	mutex_exit(&connp->conn_lock);
18459 }
18460 
18461 /*
18462  * Take down a specific interface, but don't lose any information about it.
18463  * Also delete interface from its interface group (ifgrp).
18464  * (Always called as writer.)
18465  * This function goes through the down sequence even if the interface is
18466  * already down. There are 2 reasons.
18467  * a. Currently we permit interface routes that depend on down interfaces
18468  *    to be added. This behaviour itself is questionable. However it appears
18469  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18470  *    time. We go thru the cleanup in order to remove these routes.
18471  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18472  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18473  *    down, but we need to cleanup i.e. do ill_dl_down and
18474  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18475  *
18476  * IP-MT notes:
18477  *
18478  * Model of reference to interfaces.
18479  *
18480  * The following members in ipif_t track references to the ipif.
18481  *	int     ipif_refcnt;    Active reference count
18482  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18483  *	uint_t  ipif_ilm_cnt;   Number of ilms's references this ipif.
18484  *
18485  * The following members in ill_t track references to the ill.
18486  *	int             ill_refcnt;     active refcnt
18487  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18488  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18489  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
18490  *
18491  * Reference to an ipif or ill can be obtained in any of the following ways.
18492  *
18493  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18494  * Pointers to ipif / ill from other data structures viz ire and conn.
18495  * Implicit reference to the ipif / ill by holding a reference to the ire.
18496  *
18497  * The ipif/ill lookup functions return a reference held ipif / ill.
18498  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18499  * This is a purely dynamic reference count associated with threads holding
18500  * references to the ipif / ill. Pointers from other structures do not
18501  * count towards this reference count.
18502  *
18503  * ipif_ire_cnt/ill_ire_cnt is the number of ire's
18504  * associated with the ipif/ill. This is incremented whenever a new
18505  * ire is created referencing the ipif/ill. This is done atomically inside
18506  * ire_add_v[46] where the ire is actually added to the ire hash table.
18507  * The count is decremented in ire_inactive where the ire is destroyed.
18508  *
18509  * nce's reference ill's thru nce_ill and the count of nce's associated with
18510  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18511  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18512  * table. Similarly it is decremented in ndp_inactive() where the nce
18513  * is destroyed.
18514  *
18515  * ilm's reference to the ipif (for IPv4 ilm's) or the ill (for IPv6 ilm's)
18516  * is incremented in ilm_add_v6() and decremented before the ilm is freed
18517  * in ilm_walker_cleanup() or ilm_delete().
18518  *
18519  * Flow of ioctls involving interface down/up
18520  *
18521  * The following is the sequence of an attempt to set some critical flags on an
18522  * up interface.
18523  * ip_sioctl_flags
18524  * ipif_down
18525  * wait for ipif to be quiescent
18526  * ipif_down_tail
18527  * ip_sioctl_flags_tail
18528  *
18529  * All set ioctls that involve down/up sequence would have a skeleton similar
18530  * to the above. All the *tail functions are called after the refcounts have
18531  * dropped to the appropriate values.
18532  *
18533  * The mechanism to quiesce an ipif is as follows.
18534  *
18535  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18536  * on the ipif. Callers either pass a flag requesting wait or the lookup
18537  *  functions will return NULL.
18538  *
18539  * Delete all ires referencing this ipif
18540  *
18541  * Any thread attempting to do an ipif_refhold on an ipif that has been
18542  * obtained thru a cached pointer will first make sure that
18543  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18544  * increment the refcount.
18545  *
18546  * The above guarantees that the ipif refcount will eventually come down to
18547  * zero and the ipif will quiesce, once all threads that currently hold a
18548  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18549  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18550  * have also been ire_inactive'd. i.e. when ipif_{ire, ill}_cnt and
18551  * ipif_refcnt both drop to zero. See also: comments above IPIF_DOWN_OK()
18552  * in ip.h
18553  *
18554  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18555  *
18556  * Threads trying to lookup an ipif or ill can pass a flag requesting
18557  * wait and restart if the ipif / ill cannot be looked up currently.
18558  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18559  * failure if the ipif is currently undergoing an exclusive operation, and
18560  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18561  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18562  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18563  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18564  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18565  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18566  * until we release the ipsq_lock, even though the the ill/ipif state flags
18567  * can change after we drop the ill_lock.
18568  *
18569  * An attempt to send out a packet using an ipif that is currently
18570  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18571  * operation and restart it later when the exclusive condition on the ipif ends.
18572  * This is an example of not passing the wait flag to the lookup functions. For
18573  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18574  * out a multicast packet on that ipif will fail while the ipif is
18575  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18576  * currently IPIF_CHANGING will also fail.
18577  */
18578 int
18579 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18580 {
18581 	ill_t		*ill = ipif->ipif_ill;
18582 	phyint_t	*phyi;
18583 	conn_t		*connp;
18584 	boolean_t	success;
18585 	boolean_t	ipif_was_up = B_FALSE;
18586 	ip_stack_t	*ipst = ill->ill_ipst;
18587 
18588 	ASSERT(IAM_WRITER_IPIF(ipif));
18589 
18590 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18591 
18592 	if (ipif->ipif_flags & IPIF_UP) {
18593 		mutex_enter(&ill->ill_lock);
18594 		ipif->ipif_flags &= ~IPIF_UP;
18595 		ASSERT(ill->ill_ipif_up_count > 0);
18596 		--ill->ill_ipif_up_count;
18597 		mutex_exit(&ill->ill_lock);
18598 		ipif_was_up = B_TRUE;
18599 		/* Update status in SCTP's list */
18600 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18601 	}
18602 
18603 	/*
18604 	 * Blow away memberships we established in ipif_multicast_up().
18605 	 */
18606 	ipif_multicast_down(ipif);
18607 
18608 	/*
18609 	 * Remove from the mapping for __sin6_src_id. We insert only
18610 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18611 	 * stored as mapped addresses, we need to check for mapped
18612 	 * INADDR_ANY also.
18613 	 */
18614 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18615 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18616 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18617 		int err;
18618 
18619 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18620 		    ipif->ipif_zoneid, ipst);
18621 		if (err != 0) {
18622 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18623 		}
18624 	}
18625 
18626 	/*
18627 	 * Before we delete the ill from the group (if any), we need
18628 	 * to make sure that we delete all the routes dependent on
18629 	 * this and also any ipifs dependent on this ipif for
18630 	 * source address. We need to do before we delete from
18631 	 * the group because
18632 	 *
18633 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18634 	 *
18635 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18636 	 *    for re-doing source address selection. Note that
18637 	 *    ipif_select_source[_v6] called from
18638 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18639 	 *    because we have already marked down here i.e cleared
18640 	 *    IPIF_UP.
18641 	 */
18642 	if (ipif->ipif_isv6) {
18643 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18644 		    ipst);
18645 	} else {
18646 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18647 		    ipst);
18648 	}
18649 
18650 	/*
18651 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18652 	 * ires have been deleted above. Otherwise a thread could end up
18653 	 * caching an ire in a conn after we have finished the cleanup of the
18654 	 * conn. The caching is done after making sure that the ire is not yet
18655 	 * condemned. Also documented in the block comment above ip_output
18656 	 */
18657 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18658 	/* Also, delete the ires cached in SCTP */
18659 	sctp_ire_cache_flush(ipif);
18660 
18661 	/*
18662 	 * Update any other ipifs which have used "our" local address as
18663 	 * a source address. This entails removing and recreating IRE_INTERFACE
18664 	 * entries for such ipifs.
18665 	 */
18666 	if (ipif->ipif_isv6)
18667 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18668 	else
18669 		ipif_update_other_ipifs(ipif, ill->ill_group);
18670 
18671 	if (ipif_was_up) {
18672 		/*
18673 		 * Check whether it is last ipif to leave this group.
18674 		 * If this is the last ipif to leave, we should remove
18675 		 * this ill from the group as ipif_select_source will not
18676 		 * be able to find any useful ipifs if this ill is selected
18677 		 * for load balancing.
18678 		 *
18679 		 * For nameless groups, we should call ifgrp_delete if this
18680 		 * belongs to some group. As this ipif is going down, we may
18681 		 * need to reconstruct groups.
18682 		 */
18683 		phyi = ill->ill_phyint;
18684 		/*
18685 		 * If the phyint_groupname_len is 0, it may or may not
18686 		 * be in the nameless group. If the phyint_groupname_len is
18687 		 * not 0, then this ill should be part of some group.
18688 		 * As we always insert this ill in the group if
18689 		 * phyint_groupname_len is not zero when the first ipif
18690 		 * comes up (in ipif_up_done), it should be in a group
18691 		 * when the namelen is not 0.
18692 		 *
18693 		 * NOTE : When we delete the ill from the group,it will
18694 		 * blow away all the IRE_CACHES pointing either at this ipif or
18695 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18696 		 * should be pointing at this ill.
18697 		 */
18698 		ASSERT(phyi->phyint_groupname_len == 0 ||
18699 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18700 
18701 		if (phyi->phyint_groupname_len != 0) {
18702 			if (ill->ill_ipif_up_count == 0)
18703 				illgrp_delete(ill);
18704 		}
18705 
18706 		/*
18707 		 * If we have deleted some of the broadcast ires associated
18708 		 * with this ipif, we need to re-nominate somebody else if
18709 		 * the ires that we deleted were the nominated ones.
18710 		 */
18711 		if (ill->ill_group != NULL && !ill->ill_isv6)
18712 			ipif_renominate_bcast(ipif);
18713 	}
18714 
18715 	/*
18716 	 * neighbor-discovery or arp entries for this interface.
18717 	 */
18718 	ipif_ndp_down(ipif);
18719 
18720 	/*
18721 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18722 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18723 	 * and ill_delete -> ipif_free -> ipif_down
18724 	 */
18725 	if (mp == NULL) {
18726 		ASSERT(q == NULL);
18727 		return (0);
18728 	}
18729 
18730 	if (CONN_Q(q)) {
18731 		connp = Q_TO_CONN(q);
18732 		mutex_enter(&connp->conn_lock);
18733 	} else {
18734 		connp = NULL;
18735 	}
18736 	mutex_enter(&ill->ill_lock);
18737 	/*
18738 	 * Are there any ire's pointing to this ipif that are still active ?
18739 	 * If this is the last ipif going down, are there any ire's pointing
18740 	 * to this ill that are still active ?
18741 	 */
18742 	if (ipif_is_quiescent(ipif)) {
18743 		mutex_exit(&ill->ill_lock);
18744 		if (connp != NULL)
18745 			mutex_exit(&connp->conn_lock);
18746 		return (0);
18747 	}
18748 
18749 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18750 	    ill->ill_name, (void *)ill));
18751 	/*
18752 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18753 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18754 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18755 	 */
18756 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18757 	if (!success) {
18758 		/* The conn is closing. So just return */
18759 		ASSERT(connp != NULL);
18760 		mutex_exit(&ill->ill_lock);
18761 		mutex_exit(&connp->conn_lock);
18762 		return (EINTR);
18763 	}
18764 
18765 	mutex_exit(&ill->ill_lock);
18766 	if (connp != NULL)
18767 		mutex_exit(&connp->conn_lock);
18768 	return (EINPROGRESS);
18769 }
18770 
18771 void
18772 ipif_down_tail(ipif_t *ipif)
18773 {
18774 	ill_t	*ill = ipif->ipif_ill;
18775 
18776 	/*
18777 	 * Skip any loopback interface (null wq).
18778 	 * If this is the last logical interface on the ill
18779 	 * have ill_dl_down tell the driver we are gone (unbind)
18780 	 * Note that lun 0 can ipif_down even though
18781 	 * there are other logical units that are up.
18782 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18783 	 */
18784 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18785 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18786 	    ill->ill_dl_up) {
18787 		ill_dl_down(ill);
18788 	}
18789 	ill->ill_logical_down = 0;
18790 
18791 	/*
18792 	 * Have to be after removing the routes in ipif_down_delete_ire.
18793 	 */
18794 	if (ipif->ipif_isv6) {
18795 		if (ill->ill_flags & ILLF_XRESOLV)
18796 			ipif_arp_down(ipif);
18797 	} else {
18798 		ipif_arp_down(ipif);
18799 	}
18800 
18801 	ip_rts_ifmsg(ipif);
18802 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18803 }
18804 
18805 /*
18806  * Bring interface logically down without bringing the physical interface
18807  * down e.g. when the netmask is changed. This avoids long lasting link
18808  * negotiations between an ethernet interface and a certain switches.
18809  */
18810 static int
18811 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18812 {
18813 	/*
18814 	 * The ill_logical_down flag is a transient flag. It is set here
18815 	 * and is cleared once the down has completed in ipif_down_tail.
18816 	 * This flag does not indicate whether the ill stream is in the
18817 	 * DL_BOUND state with the driver. Instead this flag is used by
18818 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18819 	 * the driver. The state of the ill stream i.e. whether it is
18820 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18821 	 */
18822 	ipif->ipif_ill->ill_logical_down = 1;
18823 	return (ipif_down(ipif, q, mp));
18824 }
18825 
18826 /*
18827  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18828  * If the usesrc client ILL is already part of a usesrc group or not,
18829  * in either case a ire_stq with the matching usesrc client ILL will
18830  * locate the IRE's that need to be deleted. We want IREs to be created
18831  * with the new source address.
18832  */
18833 static void
18834 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18835 {
18836 	ill_t	*ucill = (ill_t *)ill_arg;
18837 
18838 	ASSERT(IAM_WRITER_ILL(ucill));
18839 
18840 	if (ire->ire_stq == NULL)
18841 		return;
18842 
18843 	if ((ire->ire_type == IRE_CACHE) &&
18844 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18845 		ire_delete(ire);
18846 }
18847 
18848 /*
18849  * ire_walk routine to delete every IRE dependent on the interface
18850  * address that is going down.	(Always called as writer.)
18851  * Works for both v4 and v6.
18852  * In addition for checking for ire_ipif matches it also checks for
18853  * IRE_CACHE entries which have the same source address as the
18854  * disappearing ipif since ipif_select_source might have picked
18855  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18856  * care of any IRE_INTERFACE with the disappearing source address.
18857  */
18858 static void
18859 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18860 {
18861 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18862 	ill_t *ire_ill;
18863 	ill_t *ipif_ill;
18864 
18865 	ASSERT(IAM_WRITER_IPIF(ipif));
18866 	if (ire->ire_ipif == NULL)
18867 		return;
18868 
18869 	/*
18870 	 * For IPv4, we derive source addresses for an IRE from ipif's
18871 	 * belonging to the same IPMP group as the IRE's outgoing
18872 	 * interface.  If an IRE's outgoing interface isn't in the
18873 	 * same IPMP group as a particular ipif, then that ipif
18874 	 * couldn't have been used as a source address for this IRE.
18875 	 *
18876 	 * For IPv6, source addresses are only restricted to the IPMP group
18877 	 * if the IRE is for a link-local address or a multicast address.
18878 	 * Otherwise, source addresses for an IRE can be chosen from
18879 	 * interfaces other than the the outgoing interface for that IRE.
18880 	 *
18881 	 * For source address selection details, see ipif_select_source()
18882 	 * and ipif_select_source_v6().
18883 	 */
18884 	if (ire->ire_ipversion == IPV4_VERSION ||
18885 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18886 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18887 		ire_ill = ire->ire_ipif->ipif_ill;
18888 		ipif_ill = ipif->ipif_ill;
18889 
18890 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18891 			return;
18892 		}
18893 	}
18894 
18895 
18896 	if (ire->ire_ipif != ipif) {
18897 		/*
18898 		 * Look for a matching source address.
18899 		 */
18900 		if (ire->ire_type != IRE_CACHE)
18901 			return;
18902 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18903 			return;
18904 
18905 		if (ire->ire_ipversion == IPV4_VERSION) {
18906 			if (ire->ire_src_addr != ipif->ipif_src_addr)
18907 				return;
18908 		} else {
18909 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
18910 			    &ipif->ipif_v6lcl_addr))
18911 				return;
18912 		}
18913 		ire_delete(ire);
18914 		return;
18915 	}
18916 	/*
18917 	 * ire_delete() will do an ire_flush_cache which will delete
18918 	 * all ire_ipif matches
18919 	 */
18920 	ire_delete(ire);
18921 }
18922 
18923 /*
18924  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
18925  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
18926  * 2) when an interface is brought up or down (on that ill).
18927  * This ensures that the IRE_CACHE entries don't retain stale source
18928  * address selection results.
18929  */
18930 void
18931 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
18932 {
18933 	ill_t	*ill = (ill_t *)ill_arg;
18934 	ill_t	*ipif_ill;
18935 
18936 	ASSERT(IAM_WRITER_ILL(ill));
18937 	/*
18938 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18939 	 * Hence this should be IRE_CACHE.
18940 	 */
18941 	ASSERT(ire->ire_type == IRE_CACHE);
18942 
18943 	/*
18944 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
18945 	 * We are only interested in IRE_CACHES that has borrowed
18946 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
18947 	 * for which we need to look at ire_ipif->ipif_ill match
18948 	 * with ill.
18949 	 */
18950 	ASSERT(ire->ire_ipif != NULL);
18951 	ipif_ill = ire->ire_ipif->ipif_ill;
18952 	if (ipif_ill == ill || (ill->ill_group != NULL &&
18953 	    ipif_ill->ill_group == ill->ill_group)) {
18954 		ire_delete(ire);
18955 	}
18956 }
18957 
18958 /*
18959  * Delete all the ire whose stq references ill_arg.
18960  */
18961 static void
18962 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
18963 {
18964 	ill_t	*ill = (ill_t *)ill_arg;
18965 	ill_t	*ire_ill;
18966 
18967 	ASSERT(IAM_WRITER_ILL(ill));
18968 	/*
18969 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18970 	 * Hence this should be IRE_CACHE.
18971 	 */
18972 	ASSERT(ire->ire_type == IRE_CACHE);
18973 
18974 	/*
18975 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18976 	 * matches ill. We are only interested in IRE_CACHES that
18977 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
18978 	 * filtering here.
18979 	 */
18980 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
18981 
18982 	if (ire_ill == ill)
18983 		ire_delete(ire);
18984 }
18985 
18986 /*
18987  * This is called when an ill leaves the group. We want to delete
18988  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
18989  * pointing at ill.
18990  */
18991 static void
18992 illgrp_cache_delete(ire_t *ire, char *ill_arg)
18993 {
18994 	ill_t	*ill = (ill_t *)ill_arg;
18995 
18996 	ASSERT(IAM_WRITER_ILL(ill));
18997 	ASSERT(ill->ill_group == NULL);
18998 	/*
18999 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19000 	 * Hence this should be IRE_CACHE.
19001 	 */
19002 	ASSERT(ire->ire_type == IRE_CACHE);
19003 	/*
19004 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19005 	 * matches ill. We are interested in both.
19006 	 */
19007 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
19008 	    (ire->ire_ipif->ipif_ill == ill));
19009 
19010 	ire_delete(ire);
19011 }
19012 
19013 /*
19014  * Initiate deallocate of an IPIF. Always called as writer. Called by
19015  * ill_delete or ip_sioctl_removeif.
19016  */
19017 static void
19018 ipif_free(ipif_t *ipif)
19019 {
19020 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19021 
19022 	ASSERT(IAM_WRITER_IPIF(ipif));
19023 
19024 	if (ipif->ipif_recovery_id != 0)
19025 		(void) untimeout(ipif->ipif_recovery_id);
19026 	ipif->ipif_recovery_id = 0;
19027 
19028 	/* Remove conn references */
19029 	reset_conn_ipif(ipif);
19030 
19031 	/*
19032 	 * Make sure we have valid net and subnet broadcast ire's for the
19033 	 * other ipif's which share them with this ipif.
19034 	 */
19035 	if (!ipif->ipif_isv6)
19036 		ipif_check_bcast_ires(ipif);
19037 
19038 	/*
19039 	 * Take down the interface. We can be called either from ill_delete
19040 	 * or from ip_sioctl_removeif.
19041 	 */
19042 	(void) ipif_down(ipif, NULL, NULL);
19043 
19044 	/*
19045 	 * Now that the interface is down, there's no chance it can still
19046 	 * become a duplicate.  Cancel any timer that may have been set while
19047 	 * tearing down.
19048 	 */
19049 	if (ipif->ipif_recovery_id != 0)
19050 		(void) untimeout(ipif->ipif_recovery_id);
19051 	ipif->ipif_recovery_id = 0;
19052 
19053 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19054 	/* Remove pointers to this ill in the multicast routing tables */
19055 	reset_mrt_vif_ipif(ipif);
19056 	rw_exit(&ipst->ips_ill_g_lock);
19057 }
19058 
19059 /*
19060  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19061  * also ill_move().
19062  */
19063 static void
19064 ipif_free_tail(ipif_t *ipif)
19065 {
19066 	mblk_t	*mp;
19067 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19068 
19069 	/*
19070 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19071 	 */
19072 	mutex_enter(&ipif->ipif_saved_ire_lock);
19073 	mp = ipif->ipif_saved_ire_mp;
19074 	ipif->ipif_saved_ire_mp = NULL;
19075 	mutex_exit(&ipif->ipif_saved_ire_lock);
19076 	freemsg(mp);
19077 
19078 	/*
19079 	 * Need to hold both ill_g_lock and ill_lock while
19080 	 * inserting or removing an ipif from the linked list
19081 	 * of ipifs hanging off the ill.
19082 	 */
19083 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19084 
19085 	ASSERT(ilm_walk_ipif(ipif) == 0);
19086 
19087 #ifdef DEBUG
19088 	ipif_trace_cleanup(ipif);
19089 #endif
19090 
19091 	/* Ask SCTP to take it out of it list */
19092 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19093 
19094 	/* Get it out of the ILL interface list. */
19095 	ipif_remove(ipif, B_TRUE);
19096 	rw_exit(&ipst->ips_ill_g_lock);
19097 
19098 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19099 
19100 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19101 	ASSERT(ipif->ipif_recovery_id == 0);
19102 
19103 	/* Free the memory. */
19104 	mi_free(ipif);
19105 }
19106 
19107 /*
19108  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
19109  * is zero.
19110  */
19111 void
19112 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19113 {
19114 	char	lbuf[LIFNAMSIZ];
19115 	char	*name;
19116 	size_t	name_len;
19117 
19118 	buf[0] = '\0';
19119 	name = ipif->ipif_ill->ill_name;
19120 	name_len = ipif->ipif_ill->ill_name_length;
19121 	if (ipif->ipif_id != 0) {
19122 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19123 		    ipif->ipif_id);
19124 		name = lbuf;
19125 		name_len = mi_strlen(name) + 1;
19126 	}
19127 	len -= 1;
19128 	buf[len] = '\0';
19129 	len = MIN(len, name_len);
19130 	bcopy(name, buf, len);
19131 }
19132 
19133 /*
19134  * Find an IPIF based on the name passed in.  Names can be of the
19135  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19136  * The <phys> string can have forms like <dev><#> (e.g., le0),
19137  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19138  * When there is no colon, the implied unit id is zero. <phys> must
19139  * correspond to the name of an ILL.  (May be called as writer.)
19140  */
19141 static ipif_t *
19142 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19143     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19144     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19145 {
19146 	char	*cp;
19147 	char	*endp;
19148 	long	id;
19149 	ill_t	*ill;
19150 	ipif_t	*ipif;
19151 	uint_t	ire_type;
19152 	boolean_t did_alloc = B_FALSE;
19153 	ipsq_t	*ipsq;
19154 
19155 	if (error != NULL)
19156 		*error = 0;
19157 
19158 	/*
19159 	 * If the caller wants to us to create the ipif, make sure we have a
19160 	 * valid zoneid
19161 	 */
19162 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19163 
19164 	if (namelen == 0) {
19165 		if (error != NULL)
19166 			*error = ENXIO;
19167 		return (NULL);
19168 	}
19169 
19170 	*exists = B_FALSE;
19171 	/* Look for a colon in the name. */
19172 	endp = &name[namelen];
19173 	for (cp = endp; --cp > name; ) {
19174 		if (*cp == IPIF_SEPARATOR_CHAR)
19175 			break;
19176 	}
19177 
19178 	if (*cp == IPIF_SEPARATOR_CHAR) {
19179 		/*
19180 		 * Reject any non-decimal aliases for logical
19181 		 * interfaces. Aliases with leading zeroes
19182 		 * are also rejected as they introduce ambiguity
19183 		 * in the naming of the interfaces.
19184 		 * In order to confirm with existing semantics,
19185 		 * and to not break any programs/script relying
19186 		 * on that behaviour, if<0>:0 is considered to be
19187 		 * a valid interface.
19188 		 *
19189 		 * If alias has two or more digits and the first
19190 		 * is zero, fail.
19191 		 */
19192 		if (&cp[2] < endp && cp[1] == '0') {
19193 			if (error != NULL)
19194 				*error = EINVAL;
19195 			return (NULL);
19196 		}
19197 	}
19198 
19199 	if (cp <= name) {
19200 		cp = endp;
19201 	} else {
19202 		*cp = '\0';
19203 	}
19204 
19205 	/*
19206 	 * Look up the ILL, based on the portion of the name
19207 	 * before the slash. ill_lookup_on_name returns a held ill.
19208 	 * Temporary to check whether ill exists already. If so
19209 	 * ill_lookup_on_name will clear it.
19210 	 */
19211 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19212 	    q, mp, func, error, &did_alloc, ipst);
19213 	if (cp != endp)
19214 		*cp = IPIF_SEPARATOR_CHAR;
19215 	if (ill == NULL)
19216 		return (NULL);
19217 
19218 	/* Establish the unit number in the name. */
19219 	id = 0;
19220 	if (cp < endp && *endp == '\0') {
19221 		/* If there was a colon, the unit number follows. */
19222 		cp++;
19223 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19224 			ill_refrele(ill);
19225 			if (error != NULL)
19226 				*error = ENXIO;
19227 			return (NULL);
19228 		}
19229 	}
19230 
19231 	GRAB_CONN_LOCK(q);
19232 	mutex_enter(&ill->ill_lock);
19233 	/* Now see if there is an IPIF with this unit number. */
19234 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19235 		if (ipif->ipif_id == id) {
19236 			if (zoneid != ALL_ZONES &&
19237 			    zoneid != ipif->ipif_zoneid &&
19238 			    ipif->ipif_zoneid != ALL_ZONES) {
19239 				mutex_exit(&ill->ill_lock);
19240 				RELEASE_CONN_LOCK(q);
19241 				ill_refrele(ill);
19242 				if (error != NULL)
19243 					*error = ENXIO;
19244 				return (NULL);
19245 			}
19246 			/*
19247 			 * The block comment at the start of ipif_down
19248 			 * explains the use of the macros used below
19249 			 */
19250 			if (IPIF_CAN_LOOKUP(ipif)) {
19251 				ipif_refhold_locked(ipif);
19252 				mutex_exit(&ill->ill_lock);
19253 				if (!did_alloc)
19254 					*exists = B_TRUE;
19255 				/*
19256 				 * Drop locks before calling ill_refrele
19257 				 * since it can potentially call into
19258 				 * ipif_ill_refrele_tail which can end up
19259 				 * in trying to acquire any lock.
19260 				 */
19261 				RELEASE_CONN_LOCK(q);
19262 				ill_refrele(ill);
19263 				return (ipif);
19264 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19265 				ipsq = ill->ill_phyint->phyint_ipsq;
19266 				mutex_enter(&ipsq->ipsq_lock);
19267 				mutex_exit(&ill->ill_lock);
19268 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19269 				mutex_exit(&ipsq->ipsq_lock);
19270 				RELEASE_CONN_LOCK(q);
19271 				ill_refrele(ill);
19272 				if (error != NULL)
19273 					*error = EINPROGRESS;
19274 				return (NULL);
19275 			}
19276 		}
19277 	}
19278 	RELEASE_CONN_LOCK(q);
19279 
19280 	if (!do_alloc) {
19281 		mutex_exit(&ill->ill_lock);
19282 		ill_refrele(ill);
19283 		if (error != NULL)
19284 			*error = ENXIO;
19285 		return (NULL);
19286 	}
19287 
19288 	/*
19289 	 * If none found, atomically allocate and return a new one.
19290 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19291 	 * to support "receive only" use of lo0:1 etc. as is still done
19292 	 * below as an initial guess.
19293 	 * However, this is now likely to be overriden later in ipif_up_done()
19294 	 * when we know for sure what address has been configured on the
19295 	 * interface, since we might have more than one loopback interface
19296 	 * with a loopback address, e.g. in the case of zones, and all the
19297 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19298 	 */
19299 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19300 		ire_type = IRE_LOOPBACK;
19301 	else
19302 		ire_type = IRE_LOCAL;
19303 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19304 	if (ipif != NULL)
19305 		ipif_refhold_locked(ipif);
19306 	else if (error != NULL)
19307 		*error = ENOMEM;
19308 	mutex_exit(&ill->ill_lock);
19309 	ill_refrele(ill);
19310 	return (ipif);
19311 }
19312 
19313 /*
19314  * This routine is called whenever a new address comes up on an ipif.  If
19315  * we are configured to respond to address mask requests, then we are supposed
19316  * to broadcast an address mask reply at this time.  This routine is also
19317  * called if we are already up, but a netmask change is made.  This is legal
19318  * but might not make the system manager very popular.	(May be called
19319  * as writer.)
19320  */
19321 void
19322 ipif_mask_reply(ipif_t *ipif)
19323 {
19324 	icmph_t	*icmph;
19325 	ipha_t	*ipha;
19326 	mblk_t	*mp;
19327 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19328 
19329 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19330 
19331 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19332 		return;
19333 
19334 	/* ICMP mask reply is IPv4 only */
19335 	ASSERT(!ipif->ipif_isv6);
19336 	/* ICMP mask reply is not for a loopback interface */
19337 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19338 
19339 	mp = allocb(REPLY_LEN, BPRI_HI);
19340 	if (mp == NULL)
19341 		return;
19342 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19343 
19344 	ipha = (ipha_t *)mp->b_rptr;
19345 	bzero(ipha, REPLY_LEN);
19346 	*ipha = icmp_ipha;
19347 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19348 	ipha->ipha_src = ipif->ipif_src_addr;
19349 	ipha->ipha_dst = ipif->ipif_brd_addr;
19350 	ipha->ipha_length = htons(REPLY_LEN);
19351 	ipha->ipha_ident = 0;
19352 
19353 	icmph = (icmph_t *)&ipha[1];
19354 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19355 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19356 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19357 
19358 	put(ipif->ipif_wq, mp);
19359 
19360 #undef	REPLY_LEN
19361 }
19362 
19363 /*
19364  * When the mtu in the ipif changes, we call this routine through ire_walk
19365  * to update all the relevant IREs.
19366  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19367  */
19368 static void
19369 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19370 {
19371 	ipif_t *ipif = (ipif_t *)ipif_arg;
19372 
19373 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19374 		return;
19375 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19376 }
19377 
19378 /*
19379  * When the mtu in the ill changes, we call this routine through ire_walk
19380  * to update all the relevant IREs.
19381  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19382  */
19383 void
19384 ill_mtu_change(ire_t *ire, char *ill_arg)
19385 {
19386 	ill_t	*ill = (ill_t *)ill_arg;
19387 
19388 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19389 		return;
19390 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19391 }
19392 
19393 /*
19394  * Join the ipif specific multicast groups.
19395  * Must be called after a mapping has been set up in the resolver.  (Always
19396  * called as writer.)
19397  */
19398 void
19399 ipif_multicast_up(ipif_t *ipif)
19400 {
19401 	int err, index;
19402 	ill_t *ill;
19403 
19404 	ASSERT(IAM_WRITER_IPIF(ipif));
19405 
19406 	ill = ipif->ipif_ill;
19407 	index = ill->ill_phyint->phyint_ifindex;
19408 
19409 	ip1dbg(("ipif_multicast_up\n"));
19410 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19411 		return;
19412 
19413 	if (ipif->ipif_isv6) {
19414 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19415 			return;
19416 
19417 		/* Join the all hosts multicast address */
19418 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19419 		/*
19420 		 * Passing B_TRUE means we have to join the multicast
19421 		 * membership on this interface even though this is
19422 		 * FAILED. If we join on a different one in the group,
19423 		 * we will not be able to delete the membership later
19424 		 * as we currently don't track where we join when we
19425 		 * join within the kernel unlike applications where
19426 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19427 		 * for more on this.
19428 		 */
19429 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19430 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19431 		if (err != 0) {
19432 			ip0dbg(("ipif_multicast_up: "
19433 			    "all_hosts_mcast failed %d\n",
19434 			    err));
19435 			return;
19436 		}
19437 		/*
19438 		 * Enable multicast for the solicited node multicast address
19439 		 */
19440 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19441 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19442 
19443 			ipv6_multi.s6_addr32[3] |=
19444 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19445 
19446 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19447 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19448 			    NULL);
19449 			if (err != 0) {
19450 				ip0dbg(("ipif_multicast_up: solicited MC"
19451 				    " failed %d\n", err));
19452 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19453 				    ill, ill->ill_phyint->phyint_ifindex,
19454 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19455 				return;
19456 			}
19457 		}
19458 	} else {
19459 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19460 			return;
19461 
19462 		/* Join the all hosts multicast address */
19463 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19464 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19465 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19466 		if (err) {
19467 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19468 			return;
19469 		}
19470 	}
19471 	ipif->ipif_multicast_up = 1;
19472 }
19473 
19474 /*
19475  * Blow away any multicast groups that we joined in ipif_multicast_up().
19476  * (Explicit memberships are blown away in ill_leave_multicast() when the
19477  * ill is brought down.)
19478  */
19479 static void
19480 ipif_multicast_down(ipif_t *ipif)
19481 {
19482 	int err;
19483 
19484 	ASSERT(IAM_WRITER_IPIF(ipif));
19485 
19486 	ip1dbg(("ipif_multicast_down\n"));
19487 	if (!ipif->ipif_multicast_up)
19488 		return;
19489 
19490 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19491 
19492 	if (!ipif->ipif_isv6) {
19493 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19494 		    B_TRUE);
19495 		if (err != 0)
19496 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19497 
19498 		ipif->ipif_multicast_up = 0;
19499 		return;
19500 	}
19501 
19502 	/*
19503 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19504 	 * we should look for ilms on this ill rather than the ones that have
19505 	 * been failed over here.  They are here temporarily. As
19506 	 * ipif_multicast_up has joined on this ill, we should delete only
19507 	 * from this ill.
19508 	 */
19509 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19510 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19511 	    B_TRUE, B_TRUE);
19512 	if (err != 0) {
19513 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19514 		    err));
19515 	}
19516 	/*
19517 	 * Disable multicast for the solicited node multicast address
19518 	 */
19519 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19520 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19521 
19522 		ipv6_multi.s6_addr32[3] |=
19523 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19524 
19525 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19526 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19527 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19528 
19529 		if (err != 0) {
19530 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19531 			    err));
19532 		}
19533 	}
19534 
19535 	ipif->ipif_multicast_up = 0;
19536 }
19537 
19538 /*
19539  * Used when an interface comes up to recreate any extra routes on this
19540  * interface.
19541  */
19542 static ire_t **
19543 ipif_recover_ire(ipif_t *ipif)
19544 {
19545 	mblk_t	*mp;
19546 	ire_t	**ipif_saved_irep;
19547 	ire_t	**irep;
19548 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19549 
19550 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19551 	    ipif->ipif_id));
19552 
19553 	mutex_enter(&ipif->ipif_saved_ire_lock);
19554 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19555 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19556 	if (ipif_saved_irep == NULL) {
19557 		mutex_exit(&ipif->ipif_saved_ire_lock);
19558 		return (NULL);
19559 	}
19560 
19561 	irep = ipif_saved_irep;
19562 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19563 		ire_t		*ire;
19564 		queue_t		*rfq;
19565 		queue_t		*stq;
19566 		ifrt_t		*ifrt;
19567 		uchar_t		*src_addr;
19568 		uchar_t		*gateway_addr;
19569 		ushort_t	type;
19570 
19571 		/*
19572 		 * When the ire was initially created and then added in
19573 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19574 		 * in the case of a traditional interface route, or as one of
19575 		 * the IRE_OFFSUBNET types (with the exception of
19576 		 * IRE_HOST types ire which is created by icmp_redirect() and
19577 		 * which we don't need to save or recover).  In the case where
19578 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19579 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19580 		 * to satisfy software like GateD and Sun Cluster which creates
19581 		 * routes using the the loopback interface's address as a
19582 		 * gateway.
19583 		 *
19584 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19585 		 * ire_create() will be called in the same way here as
19586 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19587 		 * the route looks like a traditional interface route (where
19588 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19589 		 * the saved ifrt->ifrt_type.  This means that in the case where
19590 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19591 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19592 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19593 		 */
19594 		ifrt = (ifrt_t *)mp->b_rptr;
19595 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19596 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19597 			rfq = NULL;
19598 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19599 			    ? ipif->ipif_rq : ipif->ipif_wq;
19600 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19601 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19602 			    : (uint8_t *)&ipif->ipif_src_addr;
19603 			gateway_addr = NULL;
19604 			type = ipif->ipif_net_type;
19605 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19606 			/* Recover multiroute broadcast IRE. */
19607 			rfq = ipif->ipif_rq;
19608 			stq = ipif->ipif_wq;
19609 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19610 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19611 			    : (uint8_t *)&ipif->ipif_src_addr;
19612 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19613 			type = ifrt->ifrt_type;
19614 		} else {
19615 			rfq = NULL;
19616 			stq = NULL;
19617 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19618 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19619 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19620 			type = ifrt->ifrt_type;
19621 		}
19622 
19623 		/*
19624 		 * Create a copy of the IRE with the saved address and netmask.
19625 		 */
19626 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19627 		    "0x%x/0x%x\n",
19628 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19629 		    ntohl(ifrt->ifrt_addr),
19630 		    ntohl(ifrt->ifrt_mask)));
19631 		ire = ire_create(
19632 		    (uint8_t *)&ifrt->ifrt_addr,
19633 		    (uint8_t *)&ifrt->ifrt_mask,
19634 		    src_addr,
19635 		    gateway_addr,
19636 		    &ifrt->ifrt_max_frag,
19637 		    NULL,
19638 		    rfq,
19639 		    stq,
19640 		    type,
19641 		    ipif,
19642 		    0,
19643 		    0,
19644 		    0,
19645 		    ifrt->ifrt_flags,
19646 		    &ifrt->ifrt_iulp_info,
19647 		    NULL,
19648 		    NULL,
19649 		    ipst);
19650 
19651 		if (ire == NULL) {
19652 			mutex_exit(&ipif->ipif_saved_ire_lock);
19653 			kmem_free(ipif_saved_irep,
19654 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19655 			return (NULL);
19656 		}
19657 
19658 		/*
19659 		 * Some software (for example, GateD and Sun Cluster) attempts
19660 		 * to create (what amount to) IRE_PREFIX routes with the
19661 		 * loopback address as the gateway.  This is primarily done to
19662 		 * set up prefixes with the RTF_REJECT flag set (for example,
19663 		 * when generating aggregate routes.)
19664 		 *
19665 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19666 		 * IRE_LOOPBACK, then we map the request into a
19667 		 * IRE_IF_NORESOLVER.
19668 		 */
19669 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19670 			ire->ire_type = IRE_IF_NORESOLVER;
19671 		/*
19672 		 * ire held by ire_add, will be refreled' towards the
19673 		 * the end of ipif_up_done
19674 		 */
19675 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19676 		*irep = ire;
19677 		irep++;
19678 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19679 	}
19680 	mutex_exit(&ipif->ipif_saved_ire_lock);
19681 	return (ipif_saved_irep);
19682 }
19683 
19684 /*
19685  * Used to set the netmask and broadcast address to default values when the
19686  * interface is brought up.  (Always called as writer.)
19687  */
19688 static void
19689 ipif_set_default(ipif_t *ipif)
19690 {
19691 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19692 
19693 	if (!ipif->ipif_isv6) {
19694 		/*
19695 		 * Interface holds an IPv4 address. Default
19696 		 * mask is the natural netmask.
19697 		 */
19698 		if (!ipif->ipif_net_mask) {
19699 			ipaddr_t	v4mask;
19700 
19701 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19702 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19703 		}
19704 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19705 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19706 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19707 		} else {
19708 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19709 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19710 		}
19711 		/*
19712 		 * NOTE: SunOS 4.X does this even if the broadcast address
19713 		 * has been already set thus we do the same here.
19714 		 */
19715 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19716 			ipaddr_t	v4addr;
19717 
19718 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19719 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19720 		}
19721 	} else {
19722 		/*
19723 		 * Interface holds an IPv6-only address.  Default
19724 		 * mask is all-ones.
19725 		 */
19726 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19727 			ipif->ipif_v6net_mask = ipv6_all_ones;
19728 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19729 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19730 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19731 		} else {
19732 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19733 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19734 		}
19735 	}
19736 }
19737 
19738 /*
19739  * Return 0 if this address can be used as local address without causing
19740  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19741  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19742  * Special checks are needed to allow the same IPv6 link-local address
19743  * on different ills.
19744  * TODO: allowing the same site-local address on different ill's.
19745  */
19746 int
19747 ip_addr_availability_check(ipif_t *new_ipif)
19748 {
19749 	in6_addr_t our_v6addr;
19750 	ill_t *ill;
19751 	ipif_t *ipif;
19752 	ill_walk_context_t ctx;
19753 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19754 
19755 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19756 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19757 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19758 
19759 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19760 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19761 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19762 		return (0);
19763 
19764 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19765 
19766 	if (new_ipif->ipif_isv6)
19767 		ill = ILL_START_WALK_V6(&ctx, ipst);
19768 	else
19769 		ill = ILL_START_WALK_V4(&ctx, ipst);
19770 
19771 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19772 		for (ipif = ill->ill_ipif; ipif != NULL;
19773 		    ipif = ipif->ipif_next) {
19774 			if ((ipif == new_ipif) ||
19775 			    !(ipif->ipif_flags & IPIF_UP) ||
19776 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19777 				continue;
19778 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19779 			    &our_v6addr)) {
19780 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19781 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19782 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19783 					ipif->ipif_flags |= IPIF_UNNUMBERED;
19784 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19785 				    new_ipif->ipif_ill != ill)
19786 					continue;
19787 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19788 				    new_ipif->ipif_ill != ill)
19789 					continue;
19790 				else if (new_ipif->ipif_zoneid !=
19791 				    ipif->ipif_zoneid &&
19792 				    ipif->ipif_zoneid != ALL_ZONES &&
19793 				    IS_LOOPBACK(ill))
19794 					continue;
19795 				else if (new_ipif->ipif_ill == ill)
19796 					return (EADDRINUSE);
19797 				else
19798 					return (EADDRNOTAVAIL);
19799 			}
19800 		}
19801 	}
19802 
19803 	return (0);
19804 }
19805 
19806 /*
19807  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19808  * IREs for the ipif.
19809  * When the routine returns EINPROGRESS then mp has been consumed and
19810  * the ioctl will be acked from ip_rput_dlpi.
19811  */
19812 static int
19813 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19814 {
19815 	ill_t	*ill = ipif->ipif_ill;
19816 	boolean_t isv6 = ipif->ipif_isv6;
19817 	int	err = 0;
19818 	boolean_t success;
19819 
19820 	ASSERT(IAM_WRITER_IPIF(ipif));
19821 
19822 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19823 
19824 	/* Shouldn't get here if it is already up. */
19825 	if (ipif->ipif_flags & IPIF_UP)
19826 		return (EALREADY);
19827 
19828 	/* Skip arp/ndp for any loopback interface. */
19829 	if (ill->ill_wq != NULL) {
19830 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19831 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19832 
19833 		if (!ill->ill_dl_up) {
19834 			/*
19835 			 * ill_dl_up is not yet set. i.e. we are yet to
19836 			 * DL_BIND with the driver and this is the first
19837 			 * logical interface on the ill to become "up".
19838 			 * Tell the driver to get going (via DL_BIND_REQ).
19839 			 * Note that changing "significant" IFF_ flags
19840 			 * address/netmask etc cause a down/up dance, but
19841 			 * does not cause an unbind (DL_UNBIND) with the driver
19842 			 */
19843 			return (ill_dl_up(ill, ipif, mp, q));
19844 		}
19845 
19846 		/*
19847 		 * ipif_resolver_up may end up sending an
19848 		 * AR_INTERFACE_UP message to ARP, which would, in
19849 		 * turn send a DLPI message to the driver. ioctls are
19850 		 * serialized and so we cannot send more than one
19851 		 * interface up message at a time. If ipif_resolver_up
19852 		 * does send an interface up message to ARP, we get
19853 		 * EINPROGRESS and we will complete in ip_arp_done.
19854 		 */
19855 
19856 		ASSERT(connp != NULL || !CONN_Q(q));
19857 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19858 		if (connp != NULL)
19859 			mutex_enter(&connp->conn_lock);
19860 		mutex_enter(&ill->ill_lock);
19861 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19862 		mutex_exit(&ill->ill_lock);
19863 		if (connp != NULL)
19864 			mutex_exit(&connp->conn_lock);
19865 		if (!success)
19866 			return (EINTR);
19867 
19868 		/*
19869 		 * Crank up IPv6 neighbor discovery
19870 		 * Unlike ARP, this should complete when
19871 		 * ipif_ndp_up returns. However, for
19872 		 * ILLF_XRESOLV interfaces we also send a
19873 		 * AR_INTERFACE_UP to the external resolver.
19874 		 * That ioctl will complete in ip_rput.
19875 		 */
19876 		if (isv6) {
19877 			err = ipif_ndp_up(ipif);
19878 			if (err != 0) {
19879 				if (err != EINPROGRESS)
19880 					mp = ipsq_pending_mp_get(ipsq, &connp);
19881 				return (err);
19882 			}
19883 		}
19884 		/* Now, ARP */
19885 		err = ipif_resolver_up(ipif, Res_act_initial);
19886 		if (err == EINPROGRESS) {
19887 			/* We will complete it in ip_arp_done */
19888 			return (err);
19889 		}
19890 		mp = ipsq_pending_mp_get(ipsq, &connp);
19891 		ASSERT(mp != NULL);
19892 		if (err != 0)
19893 			return (err);
19894 	} else {
19895 		/*
19896 		 * Interfaces without underlying hardware don't do duplicate
19897 		 * address detection.
19898 		 */
19899 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
19900 		ipif->ipif_addr_ready = 1;
19901 	}
19902 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
19903 }
19904 
19905 /*
19906  * Perform a bind for the physical device.
19907  * When the routine returns EINPROGRESS then mp has been consumed and
19908  * the ioctl will be acked from ip_rput_dlpi.
19909  * Allocate an unbind message and save it until ipif_down.
19910  */
19911 static int
19912 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
19913 {
19914 	areq_t	*areq;
19915 	mblk_t	*areq_mp = NULL;
19916 	mblk_t	*bind_mp = NULL;
19917 	mblk_t	*unbind_mp = NULL;
19918 	conn_t	*connp;
19919 	boolean_t success;
19920 	uint16_t sap_addr;
19921 
19922 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
19923 	ASSERT(IAM_WRITER_ILL(ill));
19924 	ASSERT(mp != NULL);
19925 
19926 	/* Create a resolver cookie for ARP */
19927 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
19928 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
19929 		if (areq_mp == NULL)
19930 			return (ENOMEM);
19931 
19932 		freemsg(ill->ill_resolver_mp);
19933 		ill->ill_resolver_mp = areq_mp;
19934 		areq = (areq_t *)areq_mp->b_rptr;
19935 		sap_addr = ill->ill_sap;
19936 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
19937 	}
19938 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
19939 	    DL_BIND_REQ);
19940 	if (bind_mp == NULL)
19941 		goto bad;
19942 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
19943 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
19944 
19945 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
19946 	if (unbind_mp == NULL)
19947 		goto bad;
19948 
19949 	/*
19950 	 * Record state needed to complete this operation when the
19951 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
19952 	 */
19953 	ASSERT(WR(q)->q_next == NULL);
19954 	connp = Q_TO_CONN(q);
19955 
19956 	mutex_enter(&connp->conn_lock);
19957 	mutex_enter(&ipif->ipif_ill->ill_lock);
19958 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19959 	mutex_exit(&ipif->ipif_ill->ill_lock);
19960 	mutex_exit(&connp->conn_lock);
19961 	if (!success)
19962 		goto bad;
19963 
19964 	/*
19965 	 * Save the unbind message for ill_dl_down(); it will be consumed when
19966 	 * the interface goes down.
19967 	 */
19968 	ASSERT(ill->ill_unbind_mp == NULL);
19969 	ill->ill_unbind_mp = unbind_mp;
19970 
19971 	ill_dlpi_send(ill, bind_mp);
19972 	/* Send down link-layer capabilities probe if not already done. */
19973 	ill_capability_probe(ill);
19974 
19975 	/*
19976 	 * Sysid used to rely on the fact that netboots set domainname
19977 	 * and the like. Now that miniroot boots aren't strictly netboots
19978 	 * and miniroot network configuration is driven from userland
19979 	 * these things still need to be set. This situation can be detected
19980 	 * by comparing the interface being configured here to the one
19981 	 * dhcifname was set to reference by the boot loader. Once sysid is
19982 	 * converted to use dhcp_ipc_getinfo() this call can go away.
19983 	 */
19984 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
19985 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
19986 	    (strlen(srpc_domain) == 0)) {
19987 		if (dhcpinit() != 0)
19988 			cmn_err(CE_WARN, "no cached dhcp response");
19989 	}
19990 
19991 	/*
19992 	 * This operation will complete in ip_rput_dlpi with either
19993 	 * a DL_BIND_ACK or DL_ERROR_ACK.
19994 	 */
19995 	return (EINPROGRESS);
19996 bad:
19997 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
19998 	/*
19999 	 * We don't have to check for possible removal from illgrp
20000 	 * as we have not yet inserted in illgrp. For groups
20001 	 * without names, this ipif is still not UP and hence
20002 	 * this could not have possibly had any influence in forming
20003 	 * groups.
20004 	 */
20005 
20006 	freemsg(bind_mp);
20007 	freemsg(unbind_mp);
20008 	return (ENOMEM);
20009 }
20010 
20011 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
20012 
20013 /*
20014  * DLPI and ARP is up.
20015  * Create all the IREs associated with an interface bring up multicast.
20016  * Set the interface flag and finish other initialization
20017  * that potentially had to be differed to after DL_BIND_ACK.
20018  */
20019 int
20020 ipif_up_done(ipif_t *ipif)
20021 {
20022 	ire_t	*ire_array[20];
20023 	ire_t	**irep = ire_array;
20024 	ire_t	**irep1;
20025 	ipaddr_t net_mask = 0;
20026 	ipaddr_t subnet_mask, route_mask;
20027 	ill_t	*ill = ipif->ipif_ill;
20028 	queue_t	*stq;
20029 	ipif_t	 *src_ipif;
20030 	ipif_t   *tmp_ipif;
20031 	boolean_t	flush_ire_cache = B_TRUE;
20032 	int	err = 0;
20033 	phyint_t *phyi;
20034 	ire_t	**ipif_saved_irep = NULL;
20035 	int ipif_saved_ire_cnt;
20036 	int	cnt;
20037 	boolean_t	src_ipif_held = B_FALSE;
20038 	boolean_t	ire_added = B_FALSE;
20039 	boolean_t	loopback = B_FALSE;
20040 	ip_stack_t	*ipst = ill->ill_ipst;
20041 
20042 	ip1dbg(("ipif_up_done(%s:%u)\n",
20043 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
20044 	/* Check if this is a loopback interface */
20045 	if (ipif->ipif_ill->ill_wq == NULL)
20046 		loopback = B_TRUE;
20047 
20048 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20049 	/*
20050 	 * If all other interfaces for this ill are down or DEPRECATED,
20051 	 * or otherwise unsuitable for source address selection, remove
20052 	 * any IRE_CACHE entries for this ill to make sure source
20053 	 * address selection gets to take this new ipif into account.
20054 	 * No need to hold ill_lock while traversing the ipif list since
20055 	 * we are writer
20056 	 */
20057 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20058 	    tmp_ipif = tmp_ipif->ipif_next) {
20059 		if (((tmp_ipif->ipif_flags &
20060 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20061 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20062 		    (tmp_ipif == ipif))
20063 			continue;
20064 		/* first useable pre-existing interface */
20065 		flush_ire_cache = B_FALSE;
20066 		break;
20067 	}
20068 	if (flush_ire_cache)
20069 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20070 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20071 
20072 	/*
20073 	 * Figure out which way the send-to queue should go.  Only
20074 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20075 	 * should show up here.
20076 	 */
20077 	switch (ill->ill_net_type) {
20078 	case IRE_IF_RESOLVER:
20079 		stq = ill->ill_rq;
20080 		break;
20081 	case IRE_IF_NORESOLVER:
20082 	case IRE_LOOPBACK:
20083 		stq = ill->ill_wq;
20084 		break;
20085 	default:
20086 		return (EINVAL);
20087 	}
20088 
20089 	if (IS_LOOPBACK(ill)) {
20090 		/*
20091 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20092 		 * ipif_lookup_on_name(), but in the case of zones we can have
20093 		 * several loopback addresses on lo0. So all the interfaces with
20094 		 * loopback addresses need to be marked IRE_LOOPBACK.
20095 		 */
20096 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20097 		    htonl(INADDR_LOOPBACK))
20098 			ipif->ipif_ire_type = IRE_LOOPBACK;
20099 		else
20100 			ipif->ipif_ire_type = IRE_LOCAL;
20101 	}
20102 
20103 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20104 		/*
20105 		 * Can't use our source address. Select a different
20106 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20107 		 */
20108 		src_ipif = ipif_select_source(ipif->ipif_ill,
20109 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20110 		if (src_ipif == NULL)
20111 			src_ipif = ipif;	/* Last resort */
20112 		else
20113 			src_ipif_held = B_TRUE;
20114 	} else {
20115 		src_ipif = ipif;
20116 	}
20117 
20118 	/* Create all the IREs associated with this interface */
20119 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20120 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20121 
20122 		/*
20123 		 * If we're on a labeled system then make sure that zone-
20124 		 * private addresses have proper remote host database entries.
20125 		 */
20126 		if (is_system_labeled() &&
20127 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20128 		    !tsol_check_interface_address(ipif))
20129 			return (EINVAL);
20130 
20131 		/* Register the source address for __sin6_src_id */
20132 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20133 		    ipif->ipif_zoneid, ipst);
20134 		if (err != 0) {
20135 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20136 			return (err);
20137 		}
20138 
20139 		/* If the interface address is set, create the local IRE. */
20140 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20141 		    (void *)ipif,
20142 		    ipif->ipif_ire_type,
20143 		    ntohl(ipif->ipif_lcl_addr)));
20144 		*irep++ = ire_create(
20145 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20146 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20147 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20148 		    NULL,				/* no gateway */
20149 		    &ip_loopback_mtuplus,		/* max frag size */
20150 		    NULL,
20151 		    ipif->ipif_rq,			/* recv-from queue */
20152 		    NULL,				/* no send-to queue */
20153 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20154 		    ipif,
20155 		    0,
20156 		    0,
20157 		    0,
20158 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20159 		    RTF_PRIVATE : 0,
20160 		    &ire_uinfo_null,
20161 		    NULL,
20162 		    NULL,
20163 		    ipst);
20164 	} else {
20165 		ip1dbg((
20166 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20167 		    ipif->ipif_ire_type,
20168 		    ntohl(ipif->ipif_lcl_addr),
20169 		    (uint_t)ipif->ipif_flags));
20170 	}
20171 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20172 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20173 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20174 	} else {
20175 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20176 	}
20177 
20178 	subnet_mask = ipif->ipif_net_mask;
20179 
20180 	/*
20181 	 * If mask was not specified, use natural netmask of
20182 	 * interface address. Also, store this mask back into the
20183 	 * ipif struct.
20184 	 */
20185 	if (subnet_mask == 0) {
20186 		subnet_mask = net_mask;
20187 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20188 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20189 		    ipif->ipif_v6subnet);
20190 	}
20191 
20192 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20193 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20194 	    ipif->ipif_subnet != INADDR_ANY) {
20195 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20196 
20197 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20198 			route_mask = IP_HOST_MASK;
20199 		} else {
20200 			route_mask = subnet_mask;
20201 		}
20202 
20203 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20204 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20205 		    (void *)ipif, (void *)ill,
20206 		    ill->ill_net_type,
20207 		    ntohl(ipif->ipif_subnet)));
20208 		*irep++ = ire_create(
20209 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20210 		    (uchar_t *)&route_mask,		/* mask */
20211 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20212 		    NULL,				/* no gateway */
20213 		    &ipif->ipif_mtu,			/* max frag */
20214 		    NULL,
20215 		    NULL,				/* no recv queue */
20216 		    stq,				/* send-to queue */
20217 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20218 		    ipif,
20219 		    0,
20220 		    0,
20221 		    0,
20222 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20223 		    &ire_uinfo_null,
20224 		    NULL,
20225 		    NULL,
20226 		    ipst);
20227 	}
20228 
20229 	/*
20230 	 * Create any necessary broadcast IREs.
20231 	 */
20232 	if (ipif->ipif_flags & IPIF_BROADCAST)
20233 		irep = ipif_create_bcast_ires(ipif, irep);
20234 
20235 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20236 
20237 	/* If an earlier ire_create failed, get out now */
20238 	for (irep1 = irep; irep1 > ire_array; ) {
20239 		irep1--;
20240 		if (*irep1 == NULL) {
20241 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20242 			err = ENOMEM;
20243 			goto bad;
20244 		}
20245 	}
20246 
20247 	/*
20248 	 * Need to atomically check for ip_addr_availablity_check
20249 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20250 	 * from group also.The ill_g_lock is grabbed as reader
20251 	 * just to make sure no new ills or new ipifs are being added
20252 	 * to the system while we are checking the uniqueness of addresses.
20253 	 */
20254 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20255 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20256 	/* Mark it up, and increment counters. */
20257 	ipif->ipif_flags |= IPIF_UP;
20258 	ill->ill_ipif_up_count++;
20259 	err = ip_addr_availability_check(ipif);
20260 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20261 	rw_exit(&ipst->ips_ill_g_lock);
20262 
20263 	if (err != 0) {
20264 		/*
20265 		 * Our address may already be up on the same ill. In this case,
20266 		 * the ARP entry for our ipif replaced the one for the other
20267 		 * ipif. So we don't want to delete it (otherwise the other ipif
20268 		 * would be unable to send packets).
20269 		 * ip_addr_availability_check() identifies this case for us and
20270 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20271 		 * which is the expected error code.
20272 		 */
20273 		if (err == EADDRINUSE) {
20274 			freemsg(ipif->ipif_arp_del_mp);
20275 			ipif->ipif_arp_del_mp = NULL;
20276 			err = EADDRNOTAVAIL;
20277 		}
20278 		ill->ill_ipif_up_count--;
20279 		ipif->ipif_flags &= ~IPIF_UP;
20280 		goto bad;
20281 	}
20282 
20283 	/*
20284 	 * Add in all newly created IREs.  ire_create_bcast() has
20285 	 * already checked for duplicates of the IRE_BROADCAST type.
20286 	 * We want to add before we call ifgrp_insert which wants
20287 	 * to know whether IRE_IF_RESOLVER exists or not.
20288 	 *
20289 	 * NOTE : We refrele the ire though we may branch to "bad"
20290 	 *	  later on where we do ire_delete. This is okay
20291 	 *	  because nobody can delete it as we are running
20292 	 *	  exclusively.
20293 	 */
20294 	for (irep1 = irep; irep1 > ire_array; ) {
20295 		irep1--;
20296 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20297 		/*
20298 		 * refheld by ire_add. refele towards the end of the func
20299 		 */
20300 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20301 	}
20302 	ire_added = B_TRUE;
20303 	/*
20304 	 * Form groups if possible.
20305 	 *
20306 	 * If we are supposed to be in a ill_group with a name, insert it
20307 	 * now as we know that at least one ipif is UP. Otherwise form
20308 	 * nameless groups.
20309 	 *
20310 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20311 	 * this ipif into the appropriate interface group, or create a
20312 	 * new one. If this is already in a nameless group, we try to form
20313 	 * a bigger group looking at other ills potentially sharing this
20314 	 * ipif's prefix.
20315 	 */
20316 	phyi = ill->ill_phyint;
20317 	if (phyi->phyint_groupname_len != 0) {
20318 		ASSERT(phyi->phyint_groupname != NULL);
20319 		if (ill->ill_ipif_up_count == 1) {
20320 			ASSERT(ill->ill_group == NULL);
20321 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20322 			    phyi->phyint_groupname, NULL, B_TRUE);
20323 			if (err != 0) {
20324 				ip1dbg(("ipif_up_done: illgrp allocation "
20325 				    "failed, error %d\n", err));
20326 				goto bad;
20327 			}
20328 		}
20329 		ASSERT(ill->ill_group != NULL);
20330 	}
20331 
20332 	/*
20333 	 * When this is part of group, we need to make sure that
20334 	 * any broadcast ires created because of this ipif coming
20335 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20336 	 * so that we don't receive duplicate broadcast packets.
20337 	 */
20338 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20339 		ipif_renominate_bcast(ipif);
20340 
20341 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20342 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20343 	ipif_saved_irep = ipif_recover_ire(ipif);
20344 
20345 	if (!loopback) {
20346 		/*
20347 		 * If the broadcast address has been set, make sure it makes
20348 		 * sense based on the interface address.
20349 		 * Only match on ill since we are sharing broadcast addresses.
20350 		 */
20351 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20352 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20353 			ire_t	*ire;
20354 
20355 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20356 			    IRE_BROADCAST, ipif, ALL_ZONES,
20357 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20358 
20359 			if (ire == NULL) {
20360 				/*
20361 				 * If there isn't a matching broadcast IRE,
20362 				 * revert to the default for this netmask.
20363 				 */
20364 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20365 				mutex_enter(&ipif->ipif_ill->ill_lock);
20366 				ipif_set_default(ipif);
20367 				mutex_exit(&ipif->ipif_ill->ill_lock);
20368 			} else {
20369 				ire_refrele(ire);
20370 			}
20371 		}
20372 
20373 	}
20374 
20375 	/* This is the first interface on this ill */
20376 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20377 		/*
20378 		 * Need to recover all multicast memberships in the driver.
20379 		 * This had to be deferred until we had attached.
20380 		 */
20381 		ill_recover_multicast(ill);
20382 	}
20383 	/* Join the allhosts multicast address */
20384 	ipif_multicast_up(ipif);
20385 
20386 	if (!loopback) {
20387 		/*
20388 		 * See whether anybody else would benefit from the
20389 		 * new ipif that we added. We call this always rather
20390 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20391 		 * ipif is for the benefit of illgrp_insert (done above)
20392 		 * which does not do source address selection as it does
20393 		 * not want to re-create interface routes that we are
20394 		 * having reference to it here.
20395 		 */
20396 		ill_update_source_selection(ill);
20397 	}
20398 
20399 	for (irep1 = irep; irep1 > ire_array; ) {
20400 		irep1--;
20401 		if (*irep1 != NULL) {
20402 			/* was held in ire_add */
20403 			ire_refrele(*irep1);
20404 		}
20405 	}
20406 
20407 	cnt = ipif_saved_ire_cnt;
20408 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20409 		if (*irep1 != NULL) {
20410 			/* was held in ire_add */
20411 			ire_refrele(*irep1);
20412 		}
20413 	}
20414 
20415 	if (!loopback && ipif->ipif_addr_ready) {
20416 		/* Broadcast an address mask reply. */
20417 		ipif_mask_reply(ipif);
20418 	}
20419 	if (ipif_saved_irep != NULL) {
20420 		kmem_free(ipif_saved_irep,
20421 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20422 	}
20423 	if (src_ipif_held)
20424 		ipif_refrele(src_ipif);
20425 
20426 	/*
20427 	 * This had to be deferred until we had bound.  Tell routing sockets and
20428 	 * others that this interface is up if it looks like the address has
20429 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20430 	 * duplicate address detection to do its thing.
20431 	 */
20432 	if (ipif->ipif_addr_ready) {
20433 		ip_rts_ifmsg(ipif);
20434 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20435 		/* Let SCTP update the status for this ipif */
20436 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20437 	}
20438 	return (0);
20439 
20440 bad:
20441 	ip1dbg(("ipif_up_done: FAILED \n"));
20442 	/*
20443 	 * We don't have to bother removing from ill groups because
20444 	 *
20445 	 * 1) For groups with names, we insert only when the first ipif
20446 	 *    comes up. In that case if it fails, it will not be in any
20447 	 *    group. So, we need not try to remove for that case.
20448 	 *
20449 	 * 2) For groups without names, either we tried to insert ipif_ill
20450 	 *    in a group as singleton or found some other group to become
20451 	 *    a bigger group. For the former, if it fails we don't have
20452 	 *    anything to do as ipif_ill is not in the group and for the
20453 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20454 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20455 	 */
20456 	while (irep > ire_array) {
20457 		irep--;
20458 		if (*irep != NULL) {
20459 			ire_delete(*irep);
20460 			if (ire_added)
20461 				ire_refrele(*irep);
20462 		}
20463 	}
20464 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20465 
20466 	if (ipif_saved_irep != NULL) {
20467 		kmem_free(ipif_saved_irep,
20468 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20469 	}
20470 	if (src_ipif_held)
20471 		ipif_refrele(src_ipif);
20472 
20473 	ipif_arp_down(ipif);
20474 	return (err);
20475 }
20476 
20477 /*
20478  * Turn off the ARP with the ILLF_NOARP flag.
20479  */
20480 static int
20481 ill_arp_off(ill_t *ill)
20482 {
20483 	mblk_t	*arp_off_mp = NULL;
20484 	mblk_t	*arp_on_mp = NULL;
20485 
20486 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20487 
20488 	ASSERT(IAM_WRITER_ILL(ill));
20489 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20490 
20491 	/*
20492 	 * If the on message is still around we've already done
20493 	 * an arp_off without doing an arp_on thus there is no
20494 	 * work needed.
20495 	 */
20496 	if (ill->ill_arp_on_mp != NULL)
20497 		return (0);
20498 
20499 	/*
20500 	 * Allocate an ARP on message (to be saved) and an ARP off message
20501 	 */
20502 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20503 	if (!arp_off_mp)
20504 		return (ENOMEM);
20505 
20506 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20507 	if (!arp_on_mp)
20508 		goto failed;
20509 
20510 	ASSERT(ill->ill_arp_on_mp == NULL);
20511 	ill->ill_arp_on_mp = arp_on_mp;
20512 
20513 	/* Send an AR_INTERFACE_OFF request */
20514 	putnext(ill->ill_rq, arp_off_mp);
20515 	return (0);
20516 failed:
20517 
20518 	if (arp_off_mp)
20519 		freemsg(arp_off_mp);
20520 	return (ENOMEM);
20521 }
20522 
20523 /*
20524  * Turn on ARP by turning off the ILLF_NOARP flag.
20525  */
20526 static int
20527 ill_arp_on(ill_t *ill)
20528 {
20529 	mblk_t	*mp;
20530 
20531 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20532 
20533 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20534 
20535 	ASSERT(IAM_WRITER_ILL(ill));
20536 	/*
20537 	 * Send an AR_INTERFACE_ON request if we have already done
20538 	 * an arp_off (which allocated the message).
20539 	 */
20540 	if (ill->ill_arp_on_mp != NULL) {
20541 		mp = ill->ill_arp_on_mp;
20542 		ill->ill_arp_on_mp = NULL;
20543 		putnext(ill->ill_rq, mp);
20544 	}
20545 	return (0);
20546 }
20547 
20548 /*
20549  * Called after either deleting ill from the group or when setting
20550  * FAILED or STANDBY on the interface.
20551  */
20552 static void
20553 illgrp_reset_schednext(ill_t *ill)
20554 {
20555 	ill_group_t *illgrp;
20556 	ill_t *save_ill;
20557 
20558 	ASSERT(IAM_WRITER_ILL(ill));
20559 	/*
20560 	 * When called from illgrp_delete, ill_group will be non-NULL.
20561 	 * But when called from ip_sioctl_flags, it could be NULL if
20562 	 * somebody is setting FAILED/INACTIVE on some interface which
20563 	 * is not part of a group.
20564 	 */
20565 	illgrp = ill->ill_group;
20566 	if (illgrp == NULL)
20567 		return;
20568 	if (illgrp->illgrp_ill_schednext != ill)
20569 		return;
20570 
20571 	illgrp->illgrp_ill_schednext = NULL;
20572 	save_ill = ill;
20573 	/*
20574 	 * Choose a good ill to be the next one for
20575 	 * outbound traffic. As the flags FAILED/STANDBY is
20576 	 * not yet marked when called from ip_sioctl_flags,
20577 	 * we check for ill separately.
20578 	 */
20579 	for (ill = illgrp->illgrp_ill; ill != NULL;
20580 	    ill = ill->ill_group_next) {
20581 		if ((ill != save_ill) &&
20582 		    !(ill->ill_phyint->phyint_flags &
20583 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20584 			illgrp->illgrp_ill_schednext = ill;
20585 			return;
20586 		}
20587 	}
20588 }
20589 
20590 /*
20591  * Given an ill, find the next ill in the group to be scheduled.
20592  * (This should be called by ip_newroute() before ire_create().)
20593  * The passed in ill may be pulled out of the group, after we have picked
20594  * up a different outgoing ill from the same group. However ire add will
20595  * atomically check this.
20596  */
20597 ill_t *
20598 illgrp_scheduler(ill_t *ill)
20599 {
20600 	ill_t *retill;
20601 	ill_group_t *illgrp;
20602 	int illcnt;
20603 	int i;
20604 	uint64_t flags;
20605 	ip_stack_t	*ipst = ill->ill_ipst;
20606 
20607 	/*
20608 	 * We don't use a lock to check for the ill_group. If this ill
20609 	 * is currently being inserted we may end up just returning this
20610 	 * ill itself. That is ok.
20611 	 */
20612 	if (ill->ill_group == NULL) {
20613 		ill_refhold(ill);
20614 		return (ill);
20615 	}
20616 
20617 	/*
20618 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20619 	 * a set of stable ills. No ill can be added or deleted or change
20620 	 * group while we hold the reader lock.
20621 	 */
20622 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20623 	if ((illgrp = ill->ill_group) == NULL) {
20624 		rw_exit(&ipst->ips_ill_g_lock);
20625 		ill_refhold(ill);
20626 		return (ill);
20627 	}
20628 
20629 	illcnt = illgrp->illgrp_ill_count;
20630 	mutex_enter(&illgrp->illgrp_lock);
20631 	retill = illgrp->illgrp_ill_schednext;
20632 
20633 	if (retill == NULL)
20634 		retill = illgrp->illgrp_ill;
20635 
20636 	/*
20637 	 * We do a circular search beginning at illgrp_ill_schednext
20638 	 * or illgrp_ill. We don't check the flags against the ill lock
20639 	 * since it can change anytime. The ire creation will be atomic
20640 	 * and will fail if the ill is FAILED or OFFLINE.
20641 	 */
20642 	for (i = 0; i < illcnt; i++) {
20643 		flags = retill->ill_phyint->phyint_flags;
20644 
20645 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20646 		    ILL_CAN_LOOKUP(retill)) {
20647 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20648 			ill_refhold(retill);
20649 			break;
20650 		}
20651 		retill = retill->ill_group_next;
20652 		if (retill == NULL)
20653 			retill = illgrp->illgrp_ill;
20654 	}
20655 	mutex_exit(&illgrp->illgrp_lock);
20656 	rw_exit(&ipst->ips_ill_g_lock);
20657 
20658 	return (i == illcnt ? NULL : retill);
20659 }
20660 
20661 /*
20662  * Checks for availbility of a usable source address (if there is one) when the
20663  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20664  * this selection is done regardless of the destination.
20665  */
20666 boolean_t
20667 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20668 {
20669 	uint_t	ifindex;
20670 	ipif_t	*ipif = NULL;
20671 	ill_t	*uill;
20672 	boolean_t isv6;
20673 	ip_stack_t	*ipst = ill->ill_ipst;
20674 
20675 	ASSERT(ill != NULL);
20676 
20677 	isv6 = ill->ill_isv6;
20678 	ifindex = ill->ill_usesrc_ifindex;
20679 	if (ifindex != 0) {
20680 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20681 		    NULL, ipst);
20682 		if (uill == NULL)
20683 			return (NULL);
20684 		mutex_enter(&uill->ill_lock);
20685 		for (ipif = uill->ill_ipif; ipif != NULL;
20686 		    ipif = ipif->ipif_next) {
20687 			if (!IPIF_CAN_LOOKUP(ipif))
20688 				continue;
20689 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20690 				continue;
20691 			if (!(ipif->ipif_flags & IPIF_UP))
20692 				continue;
20693 			if (ipif->ipif_zoneid != zoneid)
20694 				continue;
20695 			if ((isv6 &&
20696 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20697 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20698 				continue;
20699 			mutex_exit(&uill->ill_lock);
20700 			ill_refrele(uill);
20701 			return (B_TRUE);
20702 		}
20703 		mutex_exit(&uill->ill_lock);
20704 		ill_refrele(uill);
20705 	}
20706 	return (B_FALSE);
20707 }
20708 
20709 /*
20710  * Determine the best source address given a destination address and an ill.
20711  * Prefers non-deprecated over deprecated but will return a deprecated
20712  * address if there is no other choice. If there is a usable source address
20713  * on the interface pointed to by ill_usesrc_ifindex then that is given
20714  * first preference.
20715  *
20716  * Returns NULL if there is no suitable source address for the ill.
20717  * This only occurs when there is no valid source address for the ill.
20718  */
20719 ipif_t *
20720 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20721 {
20722 	ipif_t *ipif;
20723 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20724 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20725 	int index = 0;
20726 	boolean_t wrapped = B_FALSE;
20727 	boolean_t same_subnet_only = B_FALSE;
20728 	boolean_t ipif_same_found, ipif_other_found;
20729 	boolean_t specific_found;
20730 	ill_t	*till, *usill = NULL;
20731 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20732 	ip_stack_t	*ipst = ill->ill_ipst;
20733 
20734 	if (ill->ill_usesrc_ifindex != 0) {
20735 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20736 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20737 		if (usill != NULL)
20738 			ill = usill;	/* Select source from usesrc ILL */
20739 		else
20740 			return (NULL);
20741 	}
20742 
20743 	/*
20744 	 * If we're dealing with an unlabeled destination on a labeled system,
20745 	 * make sure that we ignore source addresses that are incompatible with
20746 	 * the destination's default label.  That destination's default label
20747 	 * must dominate the minimum label on the source address.
20748 	 */
20749 	dst_rhtp = NULL;
20750 	if (is_system_labeled()) {
20751 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20752 		if (dst_rhtp == NULL)
20753 			return (NULL);
20754 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20755 			TPC_RELE(dst_rhtp);
20756 			dst_rhtp = NULL;
20757 		}
20758 	}
20759 
20760 	/*
20761 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20762 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20763 	 * After selecting the right ipif, under ill_lock make sure ipif is
20764 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20765 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20766 	 * but not under a lock.
20767 	 */
20768 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20769 
20770 retry:
20771 	till = ill;
20772 	ipif_arr[0] = NULL;
20773 
20774 	if (till->ill_group != NULL)
20775 		till = till->ill_group->illgrp_ill;
20776 
20777 	/*
20778 	 * Choose one good source address from each ill across the group.
20779 	 * If possible choose a source address in the same subnet as
20780 	 * the destination address.
20781 	 *
20782 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20783 	 * This is okay because of the following.
20784 	 *
20785 	 *    If PHYI_FAILED is set and we still have non-deprecated
20786 	 *    addresses, it means the addresses have not yet been
20787 	 *    failed over to a different interface. We potentially
20788 	 *    select them to create IRE_CACHES, which will be later
20789 	 *    flushed when the addresses move over.
20790 	 *
20791 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20792 	 *    addresses, it means either the user has configured them
20793 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20794 	 *    been moved over. For the former, in.mpathd does a failover
20795 	 *    when the interface becomes INACTIVE and hence we should
20796 	 *    not find them. Once INACTIVE is set, we don't allow them
20797 	 *    to create logical interfaces anymore. For the latter, a
20798 	 *    flush will happen when INACTIVE is cleared which will
20799 	 *    flush the IRE_CACHES.
20800 	 *
20801 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20802 	 *    over soon. We potentially select them to create IRE_CACHEs,
20803 	 *    which will be later flushed when the addresses move over.
20804 	 *
20805 	 * NOTE : As ipif_select_source is called to borrow source address
20806 	 * for an ipif that is part of a group, source address selection
20807 	 * will be re-done whenever the group changes i.e either an
20808 	 * insertion/deletion in the group.
20809 	 *
20810 	 * Fill ipif_arr[] with source addresses, using these rules:
20811 	 *
20812 	 *	1. At most one source address from a given ill ends up
20813 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20814 	 *	   associated with a given ill ends up in ipif_arr[].
20815 	 *
20816 	 *	2. If there is at least one non-deprecated ipif in the
20817 	 *	   IPMP group with a source address on the same subnet as
20818 	 *	   our destination, then fill ipif_arr[] only with
20819 	 *	   source addresses on the same subnet as our destination.
20820 	 *	   Note that because of (1), only the first
20821 	 *	   non-deprecated ipif found with a source address
20822 	 *	   matching the destination ends up in ipif_arr[].
20823 	 *
20824 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20825 	 *	   addresses not in the same subnet as our destination.
20826 	 *	   Again, because of (1), only the first off-subnet source
20827 	 *	   address will be chosen.
20828 	 *
20829 	 *	4. If there are no non-deprecated ipifs, then just use
20830 	 *	   the source address associated with the last deprecated
20831 	 *	   one we find that happens to be on the same subnet,
20832 	 *	   otherwise the first one not in the same subnet.
20833 	 */
20834 	specific_found = B_FALSE;
20835 	for (; till != NULL; till = till->ill_group_next) {
20836 		ipif_same_found = B_FALSE;
20837 		ipif_other_found = B_FALSE;
20838 		for (ipif = till->ill_ipif; ipif != NULL;
20839 		    ipif = ipif->ipif_next) {
20840 			if (!IPIF_CAN_LOOKUP(ipif))
20841 				continue;
20842 			/* Always skip NOLOCAL and ANYCAST interfaces */
20843 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20844 				continue;
20845 			if (!(ipif->ipif_flags & IPIF_UP) ||
20846 			    !ipif->ipif_addr_ready)
20847 				continue;
20848 			if (ipif->ipif_zoneid != zoneid &&
20849 			    ipif->ipif_zoneid != ALL_ZONES)
20850 				continue;
20851 			/*
20852 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20853 			 * but are not valid as source addresses.
20854 			 */
20855 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20856 				continue;
20857 
20858 			/*
20859 			 * Check compatibility of local address for
20860 			 * destination's default label if we're on a labeled
20861 			 * system.  Incompatible addresses can't be used at
20862 			 * all.
20863 			 */
20864 			if (dst_rhtp != NULL) {
20865 				boolean_t incompat;
20866 
20867 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20868 				    IPV4_VERSION, B_FALSE);
20869 				if (src_rhtp == NULL)
20870 					continue;
20871 				incompat =
20872 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20873 				    src_rhtp->tpc_tp.tp_doi !=
20874 				    dst_rhtp->tpc_tp.tp_doi ||
20875 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20876 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20877 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20878 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20879 				TPC_RELE(src_rhtp);
20880 				if (incompat)
20881 					continue;
20882 			}
20883 
20884 			/*
20885 			 * We prefer not to use all all-zones addresses, if we
20886 			 * can avoid it, as they pose problems with unlabeled
20887 			 * destinations.
20888 			 */
20889 			if (ipif->ipif_zoneid != ALL_ZONES) {
20890 				if (!specific_found &&
20891 				    (!same_subnet_only ||
20892 				    (ipif->ipif_net_mask & dst) ==
20893 				    ipif->ipif_subnet)) {
20894 					index = 0;
20895 					specific_found = B_TRUE;
20896 					ipif_other_found = B_FALSE;
20897 				}
20898 			} else {
20899 				if (specific_found)
20900 					continue;
20901 			}
20902 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
20903 				if (ipif_dep == NULL ||
20904 				    (ipif->ipif_net_mask & dst) ==
20905 				    ipif->ipif_subnet)
20906 					ipif_dep = ipif;
20907 				continue;
20908 			}
20909 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
20910 				/* found a source address in the same subnet */
20911 				if (!same_subnet_only) {
20912 					same_subnet_only = B_TRUE;
20913 					index = 0;
20914 				}
20915 				ipif_same_found = B_TRUE;
20916 			} else {
20917 				if (same_subnet_only || ipif_other_found)
20918 					continue;
20919 				ipif_other_found = B_TRUE;
20920 			}
20921 			ipif_arr[index++] = ipif;
20922 			if (index == MAX_IPIF_SELECT_SOURCE) {
20923 				wrapped = B_TRUE;
20924 				index = 0;
20925 			}
20926 			if (ipif_same_found)
20927 				break;
20928 		}
20929 	}
20930 
20931 	if (ipif_arr[0] == NULL) {
20932 		ipif = ipif_dep;
20933 	} else {
20934 		if (wrapped)
20935 			index = MAX_IPIF_SELECT_SOURCE;
20936 		ipif = ipif_arr[ipif_rand(ipst) % index];
20937 		ASSERT(ipif != NULL);
20938 	}
20939 
20940 	if (ipif != NULL) {
20941 		mutex_enter(&ipif->ipif_ill->ill_lock);
20942 		if (!IPIF_CAN_LOOKUP(ipif)) {
20943 			mutex_exit(&ipif->ipif_ill->ill_lock);
20944 			goto retry;
20945 		}
20946 		ipif_refhold_locked(ipif);
20947 		mutex_exit(&ipif->ipif_ill->ill_lock);
20948 	}
20949 
20950 	rw_exit(&ipst->ips_ill_g_lock);
20951 	if (usill != NULL)
20952 		ill_refrele(usill);
20953 	if (dst_rhtp != NULL)
20954 		TPC_RELE(dst_rhtp);
20955 
20956 #ifdef DEBUG
20957 	if (ipif == NULL) {
20958 		char buf1[INET6_ADDRSTRLEN];
20959 
20960 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
20961 		    ill->ill_name,
20962 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
20963 	} else {
20964 		char buf1[INET6_ADDRSTRLEN];
20965 		char buf2[INET6_ADDRSTRLEN];
20966 
20967 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
20968 		    ipif->ipif_ill->ill_name,
20969 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
20970 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
20971 		    buf2, sizeof (buf2))));
20972 	}
20973 #endif /* DEBUG */
20974 	return (ipif);
20975 }
20976 
20977 
20978 /*
20979  * If old_ipif is not NULL, see if ipif was derived from old
20980  * ipif and if so, recreate the interface route by re-doing
20981  * source address selection. This happens when ipif_down ->
20982  * ipif_update_other_ipifs calls us.
20983  *
20984  * If old_ipif is NULL, just redo the source address selection
20985  * if needed. This happens when illgrp_insert or ipif_up_done
20986  * calls us.
20987  */
20988 static void
20989 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
20990 {
20991 	ire_t *ire;
20992 	ire_t *ipif_ire;
20993 	queue_t *stq;
20994 	ipif_t *nipif;
20995 	ill_t *ill;
20996 	boolean_t need_rele = B_FALSE;
20997 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
20998 
20999 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
21000 	ASSERT(IAM_WRITER_IPIF(ipif));
21001 
21002 	ill = ipif->ipif_ill;
21003 	if (!(ipif->ipif_flags &
21004 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
21005 		/*
21006 		 * Can't possibly have borrowed the source
21007 		 * from old_ipif.
21008 		 */
21009 		return;
21010 	}
21011 
21012 	/*
21013 	 * Is there any work to be done? No work if the address
21014 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21015 	 * ipif_select_source() does not borrow addresses from
21016 	 * NOLOCAL and ANYCAST interfaces).
21017 	 */
21018 	if ((old_ipif != NULL) &&
21019 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21020 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21021 	    (old_ipif->ipif_flags &
21022 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21023 		return;
21024 	}
21025 
21026 	/*
21027 	 * Perform the same checks as when creating the
21028 	 * IRE_INTERFACE in ipif_up_done.
21029 	 */
21030 	if (!(ipif->ipif_flags & IPIF_UP))
21031 		return;
21032 
21033 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21034 	    (ipif->ipif_subnet == INADDR_ANY))
21035 		return;
21036 
21037 	ipif_ire = ipif_to_ire(ipif);
21038 	if (ipif_ire == NULL)
21039 		return;
21040 
21041 	/*
21042 	 * We know that ipif uses some other source for its
21043 	 * IRE_INTERFACE. Is it using the source of this
21044 	 * old_ipif?
21045 	 */
21046 	if (old_ipif != NULL &&
21047 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21048 		ire_refrele(ipif_ire);
21049 		return;
21050 	}
21051 	if (ip_debug > 2) {
21052 		/* ip1dbg */
21053 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21054 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21055 	}
21056 
21057 	stq = ipif_ire->ire_stq;
21058 
21059 	/*
21060 	 * Can't use our source address. Select a different
21061 	 * source address for the IRE_INTERFACE.
21062 	 */
21063 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21064 	if (nipif == NULL) {
21065 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21066 		nipif = ipif;
21067 	} else {
21068 		need_rele = B_TRUE;
21069 	}
21070 
21071 	ire = ire_create(
21072 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21073 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21074 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21075 	    NULL,				/* no gateway */
21076 	    &ipif->ipif_mtu,			/* max frag */
21077 	    NULL,				/* no src nce */
21078 	    NULL,				/* no recv from queue */
21079 	    stq,				/* send-to queue */
21080 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21081 	    ipif,
21082 	    0,
21083 	    0,
21084 	    0,
21085 	    0,
21086 	    &ire_uinfo_null,
21087 	    NULL,
21088 	    NULL,
21089 	    ipst);
21090 
21091 	if (ire != NULL) {
21092 		ire_t *ret_ire;
21093 		int error;
21094 
21095 		/*
21096 		 * We don't need ipif_ire anymore. We need to delete
21097 		 * before we add so that ire_add does not detect
21098 		 * duplicates.
21099 		 */
21100 		ire_delete(ipif_ire);
21101 		ret_ire = ire;
21102 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21103 		ASSERT(error == 0);
21104 		ASSERT(ire == ret_ire);
21105 		/* Held in ire_add */
21106 		ire_refrele(ret_ire);
21107 	}
21108 	/*
21109 	 * Either we are falling through from above or could not
21110 	 * allocate a replacement.
21111 	 */
21112 	ire_refrele(ipif_ire);
21113 	if (need_rele)
21114 		ipif_refrele(nipif);
21115 }
21116 
21117 /*
21118  * This old_ipif is going away.
21119  *
21120  * Determine if any other ipif's is using our address as
21121  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21122  * IPIF_DEPRECATED).
21123  * Find the IRE_INTERFACE for such ipifs and recreate them
21124  * to use an different source address following the rules in
21125  * ipif_up_done.
21126  *
21127  * This function takes an illgrp as an argument so that illgrp_delete
21128  * can call this to update source address even after deleting the
21129  * old_ipif->ipif_ill from the ill group.
21130  */
21131 static void
21132 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21133 {
21134 	ipif_t *ipif;
21135 	ill_t *ill;
21136 	char	buf[INET6_ADDRSTRLEN];
21137 
21138 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21139 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21140 
21141 	ill = old_ipif->ipif_ill;
21142 
21143 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21144 	    ill->ill_name,
21145 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21146 	    buf, sizeof (buf))));
21147 	/*
21148 	 * If this part of a group, look at all ills as ipif_select_source
21149 	 * borrows source address across all the ills in the group.
21150 	 */
21151 	if (illgrp != NULL)
21152 		ill = illgrp->illgrp_ill;
21153 
21154 	for (; ill != NULL; ill = ill->ill_group_next) {
21155 		for (ipif = ill->ill_ipif; ipif != NULL;
21156 		    ipif = ipif->ipif_next) {
21157 
21158 			if (ipif == old_ipif)
21159 				continue;
21160 
21161 			ipif_recreate_interface_routes(old_ipif, ipif);
21162 		}
21163 	}
21164 }
21165 
21166 /* ARGSUSED */
21167 int
21168 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21169 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21170 {
21171 	/*
21172 	 * ill_phyint_reinit merged the v4 and v6 into a single
21173 	 * ipsq. Could also have become part of a ipmp group in the
21174 	 * process, and we might not have been able to complete the
21175 	 * operation in ipif_set_values, if we could not become
21176 	 * exclusive.  If so restart it here.
21177 	 */
21178 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21179 }
21180 
21181 
21182 /*
21183  * Can operate on either a module or a driver queue.
21184  * Returns an error if not a module queue.
21185  */
21186 /* ARGSUSED */
21187 int
21188 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21189     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21190 {
21191 	queue_t		*q1 = q;
21192 	char 		*cp;
21193 	char		interf_name[LIFNAMSIZ];
21194 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21195 
21196 	if (q->q_next == NULL) {
21197 		ip1dbg((
21198 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21199 		return (EINVAL);
21200 	}
21201 
21202 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21203 		return (EALREADY);
21204 
21205 	do {
21206 		q1 = q1->q_next;
21207 	} while (q1->q_next);
21208 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21209 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21210 
21211 	/*
21212 	 * Here we are not going to delay the ioack until after
21213 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21214 	 * original ioctl message before sending the requests.
21215 	 */
21216 	return (ipif_set_values(q, mp, interf_name, &ppa));
21217 }
21218 
21219 /* ARGSUSED */
21220 int
21221 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21222     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21223 {
21224 	return (ENXIO);
21225 }
21226 
21227 /*
21228  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21229  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21230  * ire_check_and_create_bcast()).
21231  */
21232 static ire_t **
21233 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21234 {
21235 	ipaddr_t addr;
21236 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21237 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21238 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21239 
21240 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21241 
21242 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21243 
21244 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21245 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21246 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21247 
21248 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21249 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21250 
21251 	/*
21252 	 * For backward compatibility, we create net broadcast IREs based on
21253 	 * the old "IP address class system", since some old machines only
21254 	 * respond to these class derived net broadcast.  However, we must not
21255 	 * create these net broadcast IREs if the subnetmask is shorter than
21256 	 * the IP address class based derived netmask.  Otherwise, we may
21257 	 * create a net broadcast address which is the same as an IP address
21258 	 * on the subnet -- and then TCP will refuse to talk to that address.
21259 	 */
21260 	if (netmask < subnetmask) {
21261 		addr = netmask & ipif->ipif_subnet;
21262 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21263 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21264 		    flags);
21265 	}
21266 
21267 	/*
21268 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21269 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21270 	 * created.  Creating these broadcast IREs will only create confusion
21271 	 * as `addr' will be the same as the IP address.
21272 	 */
21273 	if (subnetmask != 0xFFFFFFFF) {
21274 		addr = ipif->ipif_subnet;
21275 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21276 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21277 		    irep, flags);
21278 	}
21279 
21280 	return (irep);
21281 }
21282 
21283 /*
21284  * Broadcast IRE info structure used in the functions below.  Since we
21285  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21286  */
21287 typedef struct bcast_ireinfo {
21288 	uchar_t		bi_type;	/* BCAST_* value from below */
21289 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21290 			bi_needrep:1,	/* do we need to replace it? */
21291 			bi_haverep:1,	/* have we replaced it? */
21292 			bi_pad:5;
21293 	ipaddr_t	bi_addr;	/* IRE address */
21294 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21295 } bcast_ireinfo_t;
21296 
21297 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21298 
21299 /*
21300  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21301  * return B_TRUE if it should immediately be used to recreate the IRE.
21302  */
21303 static boolean_t
21304 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21305 {
21306 	ipaddr_t addr;
21307 
21308 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21309 
21310 	switch (bireinfop->bi_type) {
21311 	case BCAST_NET:
21312 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21313 		if (addr != bireinfop->bi_addr)
21314 			return (B_FALSE);
21315 		break;
21316 	case BCAST_SUBNET:
21317 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21318 			return (B_FALSE);
21319 		break;
21320 	}
21321 
21322 	bireinfop->bi_needrep = 1;
21323 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21324 		if (bireinfop->bi_backup == NULL)
21325 			bireinfop->bi_backup = ipif;
21326 		return (B_FALSE);
21327 	}
21328 	return (B_TRUE);
21329 }
21330 
21331 /*
21332  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21333  * them ala ire_check_and_create_bcast().
21334  */
21335 static ire_t **
21336 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21337 {
21338 	ipaddr_t mask, addr;
21339 
21340 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21341 
21342 	addr = bireinfop->bi_addr;
21343 	irep = ire_create_bcast(ipif, addr, irep);
21344 
21345 	switch (bireinfop->bi_type) {
21346 	case BCAST_NET:
21347 		mask = ip_net_mask(ipif->ipif_subnet);
21348 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21349 		break;
21350 	case BCAST_SUBNET:
21351 		mask = ipif->ipif_net_mask;
21352 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21353 		break;
21354 	}
21355 
21356 	bireinfop->bi_haverep = 1;
21357 	return (irep);
21358 }
21359 
21360 /*
21361  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21362  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21363  * that are going away are still needed.  If so, have ipif_create_bcast()
21364  * recreate them (except for the deprecated case, as explained below).
21365  */
21366 static ire_t **
21367 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21368     ire_t **irep)
21369 {
21370 	int i;
21371 	ipif_t *ipif;
21372 
21373 	ASSERT(!ill->ill_isv6);
21374 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21375 		/*
21376 		 * Skip this ipif if it's (a) the one being taken down, (b)
21377 		 * not in the same zone, or (c) has no valid local address.
21378 		 */
21379 		if (ipif == test_ipif ||
21380 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21381 		    ipif->ipif_subnet == 0 ||
21382 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21383 		    (IPIF_UP|IPIF_BROADCAST))
21384 			continue;
21385 
21386 		/*
21387 		 * For each dying IRE that hasn't yet been replaced, see if
21388 		 * `ipif' needs it and whether the IRE should be recreated on
21389 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21390 		 * will return B_FALSE even if `ipif' needs the IRE on the
21391 		 * hopes that we'll later find a needy non-deprecated ipif.
21392 		 * However, the ipif is recorded in bi_backup for possible
21393 		 * subsequent use by ipif_check_bcast_ires().
21394 		 */
21395 		for (i = 0; i < BCAST_COUNT; i++) {
21396 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21397 				continue;
21398 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21399 				continue;
21400 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21401 		}
21402 
21403 		/*
21404 		 * If we've replaced all of the broadcast IREs that are going
21405 		 * to be taken down, we know we're done.
21406 		 */
21407 		for (i = 0; i < BCAST_COUNT; i++) {
21408 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21409 				break;
21410 		}
21411 		if (i == BCAST_COUNT)
21412 			break;
21413 	}
21414 	return (irep);
21415 }
21416 
21417 /*
21418  * Check if `test_ipif' (which is going away) is associated with any existing
21419  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21420  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21421  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21422  *
21423  * This is necessary because broadcast IREs are shared.  In particular, a
21424  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21425  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21426  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21427  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21428  * same zone, they will share the same set of broadcast IREs.
21429  *
21430  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21431  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21432  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21433  */
21434 static void
21435 ipif_check_bcast_ires(ipif_t *test_ipif)
21436 {
21437 	ill_t		*ill = test_ipif->ipif_ill;
21438 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21439 	ire_t		**irep1, **irep = &ire_array[0];
21440 	uint_t 		i, willdie;
21441 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21442 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21443 
21444 	ASSERT(!test_ipif->ipif_isv6);
21445 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21446 
21447 	/*
21448 	 * No broadcast IREs for the LOOPBACK interface
21449 	 * or others such as point to point and IPIF_NOXMIT.
21450 	 */
21451 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21452 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21453 		return;
21454 
21455 	bzero(bireinfo, sizeof (bireinfo));
21456 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21457 	bireinfo[0].bi_addr = 0;
21458 
21459 	bireinfo[1].bi_type = BCAST_ALLONES;
21460 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21461 
21462 	bireinfo[2].bi_type = BCAST_NET;
21463 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21464 
21465 	if (test_ipif->ipif_net_mask != 0)
21466 		mask = test_ipif->ipif_net_mask;
21467 	bireinfo[3].bi_type = BCAST_SUBNET;
21468 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21469 
21470 	/*
21471 	 * Figure out what (if any) broadcast IREs will die as a result of
21472 	 * `test_ipif' going away.  If none will die, we're done.
21473 	 */
21474 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21475 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21476 		    test_ipif, ALL_ZONES, NULL,
21477 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21478 		if (ire != NULL) {
21479 			willdie++;
21480 			bireinfo[i].bi_willdie = 1;
21481 			ire_refrele(ire);
21482 		}
21483 	}
21484 
21485 	if (willdie == 0)
21486 		return;
21487 
21488 	/*
21489 	 * Walk through all the ipifs that will be affected by the dying IREs,
21490 	 * and recreate the IREs as necessary.
21491 	 */
21492 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21493 
21494 	/*
21495 	 * Scan through the set of broadcast IREs and see if there are any
21496 	 * that we need to replace that have not yet been replaced.  If so,
21497 	 * replace them using the appropriate backup ipif.
21498 	 */
21499 	for (i = 0; i < BCAST_COUNT; i++) {
21500 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21501 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21502 			    &bireinfo[i], irep);
21503 	}
21504 
21505 	/*
21506 	 * If we can't create all of them, don't add any of them.  (Code in
21507 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21508 	 * non-loopback copy and loopback copy for a given address.)
21509 	 */
21510 	for (irep1 = irep; irep1 > ire_array; ) {
21511 		irep1--;
21512 		if (*irep1 == NULL) {
21513 			ip0dbg(("ipif_check_bcast_ires: can't create "
21514 			    "IRE_BROADCAST, memory allocation failure\n"));
21515 			while (irep > ire_array) {
21516 				irep--;
21517 				if (*irep != NULL)
21518 					ire_delete(*irep);
21519 			}
21520 			return;
21521 		}
21522 	}
21523 
21524 	for (irep1 = irep; irep1 > ire_array; ) {
21525 		irep1--;
21526 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21527 			ire_refrele(*irep1);		/* Held in ire_add */
21528 	}
21529 }
21530 
21531 /*
21532  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21533  * from lifr_flags and the name from lifr_name.
21534  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21535  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21536  * Returns EINPROGRESS when mp has been consumed by queueing it on
21537  * ill_pending_mp and the ioctl will complete in ip_rput.
21538  *
21539  * Can operate on either a module or a driver queue.
21540  * Returns an error if not a module queue.
21541  */
21542 /* ARGSUSED */
21543 int
21544 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21545     ip_ioctl_cmd_t *ipip, void *if_req)
21546 {
21547 	ill_t	*ill = q->q_ptr;
21548 	phyint_t *phyi;
21549 	ip_stack_t *ipst;
21550 	struct lifreq *lifr = if_req;
21551 
21552 	ASSERT(ipif != NULL);
21553 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21554 
21555 	if (q->q_next == NULL) {
21556 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21557 		return (EINVAL);
21558 	}
21559 
21560 	/*
21561 	 * If we are not writer on 'q' then this interface exists already
21562 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
21563 	 * so return EALREADY.
21564 	 */
21565 	if (ill != ipif->ipif_ill)
21566 		return (EALREADY);
21567 
21568 	if (ill->ill_name[0] != '\0')
21569 		return (EALREADY);
21570 
21571 	/*
21572 	 * Set all the flags. Allows all kinds of override. Provide some
21573 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21574 	 * unless there is either multicast/broadcast support in the driver
21575 	 * or it is a pt-pt link.
21576 	 */
21577 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21578 		/* Meaningless to IP thus don't allow them to be set. */
21579 		ip1dbg(("ip_setname: EINVAL 1\n"));
21580 		return (EINVAL);
21581 	}
21582 
21583 	/*
21584 	 * If there's another ill already with the requested name, ensure
21585 	 * that it's of the same type.	Otherwise, ill_phyint_reinit() will
21586 	 * fuse together two unrelated ills, which will cause chaos.
21587 	 */
21588 	ipst = ill->ill_ipst;
21589 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
21590 	    lifr->lifr_name, NULL);
21591 	if (phyi != NULL) {
21592 		ill_t *ill_mate = phyi->phyint_illv4;
21593 
21594 		if (ill_mate == NULL)
21595 			ill_mate = phyi->phyint_illv6;
21596 		ASSERT(ill_mate != NULL);
21597 
21598 		if (ill_mate->ill_media->ip_m_mac_type !=
21599 		    ill->ill_media->ip_m_mac_type) {
21600 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
21601 			    "use the same ill name on differing media\n"));
21602 			return (EINVAL);
21603 		}
21604 	}
21605 
21606 	/*
21607 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21608 	 * ill_bcast_addr_length info.
21609 	 */
21610 	if (!ill->ill_needs_attach &&
21611 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21612 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21613 	    ill->ill_bcast_addr_length == 0)) {
21614 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21615 		ip1dbg(("ip_setname: EINVAL 2\n"));
21616 		return (EINVAL);
21617 	}
21618 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21619 	    ((lifr->lifr_flags & IFF_IPV6) ||
21620 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21621 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21622 		ip1dbg(("ip_setname: EINVAL 3\n"));
21623 		return (EINVAL);
21624 	}
21625 	if (lifr->lifr_flags & IFF_UP) {
21626 		/* Can only be set with SIOCSLIFFLAGS */
21627 		ip1dbg(("ip_setname: EINVAL 4\n"));
21628 		return (EINVAL);
21629 	}
21630 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21631 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21632 		ip1dbg(("ip_setname: EINVAL 5\n"));
21633 		return (EINVAL);
21634 	}
21635 	/*
21636 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21637 	 */
21638 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21639 	    !(lifr->lifr_flags & IFF_IPV6) &&
21640 	    !(ipif->ipif_isv6)) {
21641 		ip1dbg(("ip_setname: EINVAL 6\n"));
21642 		return (EINVAL);
21643 	}
21644 
21645 	/*
21646 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21647 	 * we have all the flags here. So, we assign rather than we OR.
21648 	 * We can't OR the flags here because we don't want to set
21649 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21650 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21651 	 * on lifr_flags value here.
21652 	 */
21653 	/*
21654 	 * This ill has not been inserted into the global list.
21655 	 * So we are still single threaded and don't need any lock
21656 	 */
21657 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS & ~IFF_DUPLICATE;
21658 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21659 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21660 
21661 	/* We started off as V4. */
21662 	if (ill->ill_flags & ILLF_IPV6) {
21663 		ill->ill_phyint->phyint_illv6 = ill;
21664 		ill->ill_phyint->phyint_illv4 = NULL;
21665 	}
21666 
21667 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
21668 }
21669 
21670 /* ARGSUSED */
21671 int
21672 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21673     ip_ioctl_cmd_t *ipip, void *if_req)
21674 {
21675 	/*
21676 	 * ill_phyint_reinit merged the v4 and v6 into a single
21677 	 * ipsq. Could also have become part of a ipmp group in the
21678 	 * process, and we might not have been able to complete the
21679 	 * slifname in ipif_set_values, if we could not become
21680 	 * exclusive.  If so restart it here
21681 	 */
21682 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21683 }
21684 
21685 /*
21686  * Return a pointer to the ipif which matches the index, IP version type and
21687  * zoneid.
21688  */
21689 ipif_t *
21690 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21691     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21692 {
21693 	ill_t	*ill;
21694 	ipif_t	*ipif = NULL;
21695 
21696 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21697 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21698 
21699 	if (err != NULL)
21700 		*err = 0;
21701 
21702 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
21703 	if (ill != NULL) {
21704 		mutex_enter(&ill->ill_lock);
21705 		for (ipif = ill->ill_ipif; ipif != NULL;
21706 		    ipif = ipif->ipif_next) {
21707 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
21708 			    zoneid == ipif->ipif_zoneid ||
21709 			    ipif->ipif_zoneid == ALL_ZONES)) {
21710 				ipif_refhold_locked(ipif);
21711 				break;
21712 			}
21713 		}
21714 		mutex_exit(&ill->ill_lock);
21715 		ill_refrele(ill);
21716 		if (ipif == NULL && err != NULL)
21717 			*err = ENXIO;
21718 	}
21719 	return (ipif);
21720 }
21721 
21722 typedef struct conn_change_s {
21723 	uint_t cc_old_ifindex;
21724 	uint_t cc_new_ifindex;
21725 } conn_change_t;
21726 
21727 /*
21728  * ipcl_walk function for changing interface index.
21729  */
21730 static void
21731 conn_change_ifindex(conn_t *connp, caddr_t arg)
21732 {
21733 	conn_change_t *connc;
21734 	uint_t old_ifindex;
21735 	uint_t new_ifindex;
21736 	int i;
21737 	ilg_t *ilg;
21738 
21739 	connc = (conn_change_t *)arg;
21740 	old_ifindex = connc->cc_old_ifindex;
21741 	new_ifindex = connc->cc_new_ifindex;
21742 
21743 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21744 		connp->conn_orig_bound_ifindex = new_ifindex;
21745 
21746 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21747 		connp->conn_orig_multicast_ifindex = new_ifindex;
21748 
21749 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21750 		ilg = &connp->conn_ilg[i];
21751 		if (ilg->ilg_orig_ifindex == old_ifindex)
21752 			ilg->ilg_orig_ifindex = new_ifindex;
21753 	}
21754 }
21755 
21756 /*
21757  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21758  * to new_index if it matches the old_index.
21759  *
21760  * Failovers typically happen within a group of ills. But somebody
21761  * can remove an ill from the group after a failover happened. If
21762  * we are setting the ifindex after this, we potentially need to
21763  * look at all the ills rather than just the ones in the group.
21764  * We cut down the work by looking at matching ill_net_types
21765  * and ill_types as we could not possibly grouped them together.
21766  */
21767 static void
21768 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21769 {
21770 	ill_t *ill;
21771 	ipif_t *ipif;
21772 	uint_t old_ifindex;
21773 	uint_t new_ifindex;
21774 	ilm_t *ilm;
21775 	ill_walk_context_t ctx;
21776 	ip_stack_t	*ipst = ill_orig->ill_ipst;
21777 
21778 	old_ifindex = connc->cc_old_ifindex;
21779 	new_ifindex = connc->cc_new_ifindex;
21780 
21781 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21782 	ill = ILL_START_WALK_ALL(&ctx, ipst);
21783 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21784 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21785 		    (ill_orig->ill_type != ill->ill_type)) {
21786 			continue;
21787 		}
21788 		for (ipif = ill->ill_ipif; ipif != NULL;
21789 		    ipif = ipif->ipif_next) {
21790 			if (ipif->ipif_orig_ifindex == old_ifindex)
21791 				ipif->ipif_orig_ifindex = new_ifindex;
21792 		}
21793 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21794 			if (ilm->ilm_orig_ifindex == old_ifindex)
21795 				ilm->ilm_orig_ifindex = new_ifindex;
21796 		}
21797 	}
21798 	rw_exit(&ipst->ips_ill_g_lock);
21799 }
21800 
21801 /*
21802  * We first need to ensure that the new index is unique, and
21803  * then carry the change across both v4 and v6 ill representation
21804  * of the physical interface.
21805  */
21806 /* ARGSUSED */
21807 int
21808 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21809     ip_ioctl_cmd_t *ipip, void *ifreq)
21810 {
21811 	ill_t		*ill;
21812 	ill_t		*ill_other;
21813 	phyint_t	*phyi;
21814 	int		old_index;
21815 	conn_change_t	connc;
21816 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21817 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21818 	uint_t	index;
21819 	ill_t	*ill_v4;
21820 	ill_t	*ill_v6;
21821 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21822 
21823 	if (ipip->ipi_cmd_type == IF_CMD)
21824 		index = ifr->ifr_index;
21825 	else
21826 		index = lifr->lifr_index;
21827 
21828 	/*
21829 	 * Only allow on physical interface. Also, index zero is illegal.
21830 	 *
21831 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21832 	 *
21833 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21834 	 *    implies a possible failback might have to happen. As failback
21835 	 *    depends on the old index, we should fail setting the index.
21836 	 *
21837 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21838 	 *    any addresses or multicast memberships are failed over to
21839 	 *    a non-STANDBY interface. As failback depends on the old
21840 	 *    index, we should fail setting the index for this case also.
21841 	 *
21842 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21843 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21844 	 */
21845 	ill = ipif->ipif_ill;
21846 	phyi = ill->ill_phyint;
21847 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21848 	    ipif->ipif_id != 0 || index == 0) {
21849 		return (EINVAL);
21850 	}
21851 	old_index = phyi->phyint_ifindex;
21852 
21853 	/* If the index is not changing, no work to do */
21854 	if (old_index == index)
21855 		return (0);
21856 
21857 	/*
21858 	 * Use ill_lookup_on_ifindex to determine if the
21859 	 * new index is unused and if so allow the change.
21860 	 */
21861 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
21862 	    ipst);
21863 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
21864 	    ipst);
21865 	if (ill_v6 != NULL || ill_v4 != NULL) {
21866 		if (ill_v4 != NULL)
21867 			ill_refrele(ill_v4);
21868 		if (ill_v6 != NULL)
21869 			ill_refrele(ill_v6);
21870 		return (EBUSY);
21871 	}
21872 
21873 	/*
21874 	 * The new index is unused. Set it in the phyint.
21875 	 * Locate the other ill so that we can send a routing
21876 	 * sockets message.
21877 	 */
21878 	if (ill->ill_isv6) {
21879 		ill_other = phyi->phyint_illv4;
21880 	} else {
21881 		ill_other = phyi->phyint_illv6;
21882 	}
21883 
21884 	phyi->phyint_ifindex = index;
21885 
21886 	/* Update SCTP's ILL list */
21887 	sctp_ill_reindex(ill, old_index);
21888 
21889 	connc.cc_old_ifindex = old_index;
21890 	connc.cc_new_ifindex = index;
21891 	ip_change_ifindex(ill, &connc);
21892 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
21893 
21894 	/* Send the routing sockets message */
21895 	ip_rts_ifmsg(ipif);
21896 	if (ill_other != NULL)
21897 		ip_rts_ifmsg(ill_other->ill_ipif);
21898 
21899 	return (0);
21900 }
21901 
21902 /* ARGSUSED */
21903 int
21904 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21905     ip_ioctl_cmd_t *ipip, void *ifreq)
21906 {
21907 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21908 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21909 
21910 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
21911 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21912 	/* Get the interface index */
21913 	if (ipip->ipi_cmd_type == IF_CMD) {
21914 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21915 	} else {
21916 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21917 	}
21918 	return (0);
21919 }
21920 
21921 /* ARGSUSED */
21922 int
21923 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21924     ip_ioctl_cmd_t *ipip, void *ifreq)
21925 {
21926 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21927 
21928 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
21929 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21930 	/* Get the interface zone */
21931 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21932 	lifr->lifr_zoneid = ipif->ipif_zoneid;
21933 	return (0);
21934 }
21935 
21936 /*
21937  * Set the zoneid of an interface.
21938  */
21939 /* ARGSUSED */
21940 int
21941 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21942     ip_ioctl_cmd_t *ipip, void *ifreq)
21943 {
21944 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21945 	int err = 0;
21946 	boolean_t need_up = B_FALSE;
21947 	zone_t *zptr;
21948 	zone_status_t status;
21949 	zoneid_t zoneid;
21950 
21951 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21952 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
21953 		if (!is_system_labeled())
21954 			return (ENOTSUP);
21955 		zoneid = GLOBAL_ZONEID;
21956 	}
21957 
21958 	/* cannot assign instance zero to a non-global zone */
21959 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
21960 		return (ENOTSUP);
21961 
21962 	/*
21963 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
21964 	 * the event of a race with the zone shutdown processing, since IP
21965 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
21966 	 * interface will be cleaned up even if the zone is shut down
21967 	 * immediately after the status check. If the interface can't be brought
21968 	 * down right away, and the zone is shut down before the restart
21969 	 * function is called, we resolve the possible races by rechecking the
21970 	 * zone status in the restart function.
21971 	 */
21972 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
21973 		return (EINVAL);
21974 	status = zone_status_get(zptr);
21975 	zone_rele(zptr);
21976 
21977 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
21978 		return (EINVAL);
21979 
21980 	if (ipif->ipif_flags & IPIF_UP) {
21981 		/*
21982 		 * If the interface is already marked up,
21983 		 * we call ipif_down which will take care
21984 		 * of ditching any IREs that have been set
21985 		 * up based on the old interface address.
21986 		 */
21987 		err = ipif_logical_down(ipif, q, mp);
21988 		if (err == EINPROGRESS)
21989 			return (err);
21990 		ipif_down_tail(ipif);
21991 		need_up = B_TRUE;
21992 	}
21993 
21994 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
21995 	return (err);
21996 }
21997 
21998 static int
21999 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
22000     queue_t *q, mblk_t *mp, boolean_t need_up)
22001 {
22002 	int	err = 0;
22003 	ip_stack_t	*ipst;
22004 
22005 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
22006 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22007 
22008 	if (CONN_Q(q))
22009 		ipst = CONNQ_TO_IPST(q);
22010 	else
22011 		ipst = ILLQ_TO_IPST(q);
22012 
22013 	/*
22014 	 * For exclusive stacks we don't allow a different zoneid than
22015 	 * global.
22016 	 */
22017 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
22018 	    zoneid != GLOBAL_ZONEID)
22019 		return (EINVAL);
22020 
22021 	/* Set the new zone id. */
22022 	ipif->ipif_zoneid = zoneid;
22023 
22024 	/* Update sctp list */
22025 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22026 
22027 	if (need_up) {
22028 		/*
22029 		 * Now bring the interface back up.  If this
22030 		 * is the only IPIF for the ILL, ipif_up
22031 		 * will have to re-bind to the device, so
22032 		 * we may get back EINPROGRESS, in which
22033 		 * case, this IOCTL will get completed in
22034 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22035 		 */
22036 		err = ipif_up(ipif, q, mp);
22037 	}
22038 	return (err);
22039 }
22040 
22041 /* ARGSUSED */
22042 int
22043 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22044     ip_ioctl_cmd_t *ipip, void *if_req)
22045 {
22046 	struct lifreq *lifr = (struct lifreq *)if_req;
22047 	zoneid_t zoneid;
22048 	zone_t *zptr;
22049 	zone_status_t status;
22050 
22051 	ASSERT(ipif->ipif_id != 0);
22052 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22053 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22054 		zoneid = GLOBAL_ZONEID;
22055 
22056 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22057 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22058 
22059 	/*
22060 	 * We recheck the zone status to resolve the following race condition:
22061 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22062 	 * 2) hme0:1 is up and can't be brought down right away;
22063 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22064 	 * 3) zone "myzone" is halted; the zone status switches to
22065 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22066 	 * the interfaces to remove - hme0:1 is not returned because it's not
22067 	 * yet in "myzone", so it won't be removed;
22068 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22069 	 * status check here, we would have hme0:1 in "myzone" after it's been
22070 	 * destroyed.
22071 	 * Note that if the status check fails, we need to bring the interface
22072 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22073 	 * ipif_up_done[_v6]().
22074 	 */
22075 	status = ZONE_IS_UNINITIALIZED;
22076 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22077 		status = zone_status_get(zptr);
22078 		zone_rele(zptr);
22079 	}
22080 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22081 		if (ipif->ipif_isv6) {
22082 			(void) ipif_up_done_v6(ipif);
22083 		} else {
22084 			(void) ipif_up_done(ipif);
22085 		}
22086 		return (EINVAL);
22087 	}
22088 
22089 	ipif_down_tail(ipif);
22090 
22091 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22092 	    B_TRUE));
22093 }
22094 
22095 /* ARGSUSED */
22096 int
22097 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22098 	ip_ioctl_cmd_t *ipip, void *ifreq)
22099 {
22100 	struct lifreq	*lifr = ifreq;
22101 
22102 	ASSERT(q->q_next == NULL);
22103 	ASSERT(CONN_Q(q));
22104 
22105 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22106 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22107 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22108 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22109 
22110 	return (0);
22111 }
22112 
22113 
22114 /* Find the previous ILL in this usesrc group */
22115 static ill_t *
22116 ill_prev_usesrc(ill_t *uill)
22117 {
22118 	ill_t *ill;
22119 
22120 	for (ill = uill->ill_usesrc_grp_next;
22121 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22122 	    ill = ill->ill_usesrc_grp_next)
22123 		/* do nothing */;
22124 	return (ill);
22125 }
22126 
22127 /*
22128  * Release all members of the usesrc group. This routine is called
22129  * from ill_delete when the interface being unplumbed is the
22130  * group head.
22131  */
22132 static void
22133 ill_disband_usesrc_group(ill_t *uill)
22134 {
22135 	ill_t *next_ill, *tmp_ill;
22136 	ip_stack_t	*ipst = uill->ill_ipst;
22137 
22138 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22139 	next_ill = uill->ill_usesrc_grp_next;
22140 
22141 	do {
22142 		ASSERT(next_ill != NULL);
22143 		tmp_ill = next_ill->ill_usesrc_grp_next;
22144 		ASSERT(tmp_ill != NULL);
22145 		next_ill->ill_usesrc_grp_next = NULL;
22146 		next_ill->ill_usesrc_ifindex = 0;
22147 		next_ill = tmp_ill;
22148 	} while (next_ill->ill_usesrc_ifindex != 0);
22149 	uill->ill_usesrc_grp_next = NULL;
22150 }
22151 
22152 /*
22153  * Remove the client usesrc ILL from the list and relink to a new list
22154  */
22155 int
22156 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22157 {
22158 	ill_t *ill, *tmp_ill;
22159 	ip_stack_t	*ipst = ucill->ill_ipst;
22160 
22161 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22162 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22163 
22164 	/*
22165 	 * Check if the usesrc client ILL passed in is not already
22166 	 * in use as a usesrc ILL i.e one whose source address is
22167 	 * in use OR a usesrc ILL is not already in use as a usesrc
22168 	 * client ILL
22169 	 */
22170 	if ((ucill->ill_usesrc_ifindex == 0) ||
22171 	    (uill->ill_usesrc_ifindex != 0)) {
22172 		return (-1);
22173 	}
22174 
22175 	ill = ill_prev_usesrc(ucill);
22176 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22177 
22178 	/* Remove from the current list */
22179 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22180 		/* Only two elements in the list */
22181 		ASSERT(ill->ill_usesrc_ifindex == 0);
22182 		ill->ill_usesrc_grp_next = NULL;
22183 	} else {
22184 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22185 	}
22186 
22187 	if (ifindex == 0) {
22188 		ucill->ill_usesrc_ifindex = 0;
22189 		ucill->ill_usesrc_grp_next = NULL;
22190 		return (0);
22191 	}
22192 
22193 	ucill->ill_usesrc_ifindex = ifindex;
22194 	tmp_ill = uill->ill_usesrc_grp_next;
22195 	uill->ill_usesrc_grp_next = ucill;
22196 	ucill->ill_usesrc_grp_next =
22197 	    (tmp_ill != NULL) ? tmp_ill : uill;
22198 	return (0);
22199 }
22200 
22201 /*
22202  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22203  * ip.c for locking details.
22204  */
22205 /* ARGSUSED */
22206 int
22207 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22208     ip_ioctl_cmd_t *ipip, void *ifreq)
22209 {
22210 	struct lifreq *lifr = (struct lifreq *)ifreq;
22211 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22212 	    ill_flag_changed = B_FALSE;
22213 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22214 	int err = 0, ret;
22215 	uint_t ifindex;
22216 	phyint_t *us_phyint, *us_cli_phyint;
22217 	ipsq_t *ipsq = NULL;
22218 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22219 
22220 	ASSERT(IAM_WRITER_IPIF(ipif));
22221 	ASSERT(q->q_next == NULL);
22222 	ASSERT(CONN_Q(q));
22223 
22224 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22225 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22226 
22227 	ASSERT(us_cli_phyint != NULL);
22228 
22229 	/*
22230 	 * If the client ILL is being used for IPMP, abort.
22231 	 * Note, this can be done before ipsq_try_enter since we are already
22232 	 * exclusive on this ILL
22233 	 */
22234 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22235 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22236 		return (EINVAL);
22237 	}
22238 
22239 	ifindex = lifr->lifr_index;
22240 	if (ifindex == 0) {
22241 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22242 			/* non usesrc group interface, nothing to reset */
22243 			return (0);
22244 		}
22245 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22246 		/* valid reset request */
22247 		reset_flg = B_TRUE;
22248 	}
22249 
22250 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22251 	    ip_process_ioctl, &err, ipst);
22252 
22253 	if (usesrc_ill == NULL) {
22254 		return (err);
22255 	}
22256 
22257 	/*
22258 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22259 	 * group nor can either of the interfaces be used for standy. So
22260 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22261 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22262 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22263 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22264 	 * the usesrc_cli_ill
22265 	 */
22266 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22267 	    NEW_OP, B_TRUE);
22268 	if (ipsq == NULL) {
22269 		err = EINPROGRESS;
22270 		/* Operation enqueued on the ipsq of the usesrc ILL */
22271 		goto done;
22272 	}
22273 
22274 	/* Check if the usesrc_ill is used for IPMP */
22275 	us_phyint = usesrc_ill->ill_phyint;
22276 	if ((us_phyint->phyint_groupname != NULL) ||
22277 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22278 		err = EINVAL;
22279 		goto done;
22280 	}
22281 
22282 	/*
22283 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22284 	 * already a client then return EINVAL
22285 	 */
22286 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22287 		err = EINVAL;
22288 		goto done;
22289 	}
22290 
22291 	/*
22292 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22293 	 * be then this is a duplicate operation.
22294 	 */
22295 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22296 		err = 0;
22297 		goto done;
22298 	}
22299 
22300 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22301 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22302 	    usesrc_ill->ill_isv6));
22303 
22304 	/*
22305 	 * The next step ensures that no new ires will be created referencing
22306 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22307 	 * we go through an ire walk deleting all ire caches that reference
22308 	 * the client ill. New ires referencing the client ill that are added
22309 	 * to the ire table before the ILL_CHANGING flag is set, will be
22310 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22311 	 * the client ill while the ILL_CHANGING flag is set will be failed
22312 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22313 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22314 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22315 	 * belong to the same usesrc group.
22316 	 */
22317 	mutex_enter(&usesrc_cli_ill->ill_lock);
22318 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22319 	mutex_exit(&usesrc_cli_ill->ill_lock);
22320 	ill_flag_changed = B_TRUE;
22321 
22322 	if (ipif->ipif_isv6)
22323 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22324 		    ALL_ZONES, ipst);
22325 	else
22326 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22327 		    ALL_ZONES, ipst);
22328 
22329 	/*
22330 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22331 	 * and the ill_usesrc_ifindex fields
22332 	 */
22333 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22334 
22335 	if (reset_flg) {
22336 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22337 		if (ret != 0) {
22338 			err = EINVAL;
22339 		}
22340 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22341 		goto done;
22342 	}
22343 
22344 	/*
22345 	 * Four possibilities to consider:
22346 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22347 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22348 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22349 	 * 4. Both are part of their respective usesrc groups
22350 	 */
22351 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22352 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22353 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22354 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22355 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22356 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22357 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22358 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22359 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22360 		/* Insert at head of list */
22361 		usesrc_cli_ill->ill_usesrc_grp_next =
22362 		    usesrc_ill->ill_usesrc_grp_next;
22363 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22364 	} else {
22365 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22366 		    ifindex);
22367 		if (ret != 0)
22368 			err = EINVAL;
22369 	}
22370 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22371 
22372 done:
22373 	if (ill_flag_changed) {
22374 		mutex_enter(&usesrc_cli_ill->ill_lock);
22375 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22376 		mutex_exit(&usesrc_cli_ill->ill_lock);
22377 	}
22378 	if (ipsq != NULL)
22379 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
22380 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22381 	ill_refrele(usesrc_ill);
22382 	return (err);
22383 }
22384 
22385 /*
22386  * comparison function used by avl.
22387  */
22388 static int
22389 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22390 {
22391 
22392 	uint_t index;
22393 
22394 	ASSERT(phyip != NULL && index_ptr != NULL);
22395 
22396 	index = *((uint_t *)index_ptr);
22397 	/*
22398 	 * let the phyint with the lowest index be on top.
22399 	 */
22400 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22401 		return (1);
22402 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22403 		return (-1);
22404 	return (0);
22405 }
22406 
22407 /*
22408  * comparison function used by avl.
22409  */
22410 static int
22411 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22412 {
22413 	ill_t *ill;
22414 	int res = 0;
22415 
22416 	ASSERT(phyip != NULL && name_ptr != NULL);
22417 
22418 	if (((phyint_t *)phyip)->phyint_illv4)
22419 		ill = ((phyint_t *)phyip)->phyint_illv4;
22420 	else
22421 		ill = ((phyint_t *)phyip)->phyint_illv6;
22422 	ASSERT(ill != NULL);
22423 
22424 	res = strcmp(ill->ill_name, (char *)name_ptr);
22425 	if (res > 0)
22426 		return (1);
22427 	else if (res < 0)
22428 		return (-1);
22429 	return (0);
22430 }
22431 /*
22432  * This function is called from ill_delete when the ill is being
22433  * unplumbed. We remove the reference from the phyint and we also
22434  * free the phyint when there are no more references to it.
22435  */
22436 static void
22437 ill_phyint_free(ill_t *ill)
22438 {
22439 	phyint_t *phyi;
22440 	phyint_t *next_phyint;
22441 	ipsq_t *cur_ipsq;
22442 	ip_stack_t	*ipst = ill->ill_ipst;
22443 
22444 	ASSERT(ill->ill_phyint != NULL);
22445 
22446 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22447 	phyi = ill->ill_phyint;
22448 	ill->ill_phyint = NULL;
22449 	/*
22450 	 * ill_init allocates a phyint always to store the copy
22451 	 * of flags relevant to phyint. At that point in time, we could
22452 	 * not assign the name and hence phyint_illv4/v6 could not be
22453 	 * initialized. Later in ipif_set_values, we assign the name to
22454 	 * the ill, at which point in time we assign phyint_illv4/v6.
22455 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22456 	 */
22457 	if (ill->ill_flags & ILLF_IPV6) {
22458 		phyi->phyint_illv6 = NULL;
22459 	} else {
22460 		phyi->phyint_illv4 = NULL;
22461 	}
22462 	/*
22463 	 * ipif_down removes it from the group when the last ipif goes
22464 	 * down.
22465 	 */
22466 	ASSERT(ill->ill_group == NULL);
22467 
22468 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22469 		return;
22470 
22471 	/*
22472 	 * Make sure this phyint was put in the list.
22473 	 */
22474 	if (phyi->phyint_ifindex > 0) {
22475 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22476 		    phyi);
22477 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22478 		    phyi);
22479 	}
22480 	/*
22481 	 * remove phyint from the ipsq list.
22482 	 */
22483 	cur_ipsq = phyi->phyint_ipsq;
22484 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22485 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22486 	} else {
22487 		next_phyint = cur_ipsq->ipsq_phyint_list;
22488 		while (next_phyint != NULL) {
22489 			if (next_phyint->phyint_ipsq_next == phyi) {
22490 				next_phyint->phyint_ipsq_next =
22491 				    phyi->phyint_ipsq_next;
22492 				break;
22493 			}
22494 			next_phyint = next_phyint->phyint_ipsq_next;
22495 		}
22496 		ASSERT(next_phyint != NULL);
22497 	}
22498 	IPSQ_DEC_REF(cur_ipsq, ipst);
22499 
22500 	if (phyi->phyint_groupname_len != 0) {
22501 		ASSERT(phyi->phyint_groupname != NULL);
22502 		mi_free(phyi->phyint_groupname);
22503 	}
22504 	mi_free(phyi);
22505 }
22506 
22507 /*
22508  * Attach the ill to the phyint structure which can be shared by both
22509  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22510  * function is called from ipif_set_values and ill_lookup_on_name (for
22511  * loopback) where we know the name of the ill. We lookup the ill and if
22512  * there is one present already with the name use that phyint. Otherwise
22513  * reuse the one allocated by ill_init.
22514  */
22515 static void
22516 ill_phyint_reinit(ill_t *ill)
22517 {
22518 	boolean_t isv6 = ill->ill_isv6;
22519 	phyint_t *phyi_old;
22520 	phyint_t *phyi;
22521 	avl_index_t where = 0;
22522 	ill_t	*ill_other = NULL;
22523 	ipsq_t	*ipsq;
22524 	ip_stack_t	*ipst = ill->ill_ipst;
22525 
22526 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22527 
22528 	phyi_old = ill->ill_phyint;
22529 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22530 	    phyi_old->phyint_illv6 == NULL));
22531 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22532 	    phyi_old->phyint_illv4 == NULL));
22533 	ASSERT(phyi_old->phyint_ifindex == 0);
22534 
22535 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22536 	    ill->ill_name, &where);
22537 
22538 	/*
22539 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22540 	 *    the global list of ills. So no other thread could have located
22541 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22542 	 * 2. Now locate the other protocol instance of this ill.
22543 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22544 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22545 	 *    of neither ill can change.
22546 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22547 	 *    other ill.
22548 	 * 5. Release all locks.
22549 	 */
22550 
22551 	/*
22552 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22553 	 * we are initializing IPv4.
22554 	 */
22555 	if (phyi != NULL) {
22556 		ill_other = (isv6) ? phyi->phyint_illv4 :
22557 		    phyi->phyint_illv6;
22558 		ASSERT(ill_other->ill_phyint != NULL);
22559 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22560 		    (!isv6 && ill_other->ill_isv6));
22561 		GRAB_ILL_LOCKS(ill, ill_other);
22562 		/*
22563 		 * We are potentially throwing away phyint_flags which
22564 		 * could be different from the one that we obtain from
22565 		 * ill_other->ill_phyint. But it is okay as we are assuming
22566 		 * that the state maintained within IP is correct.
22567 		 */
22568 		mutex_enter(&phyi->phyint_lock);
22569 		if (isv6) {
22570 			ASSERT(phyi->phyint_illv6 == NULL);
22571 			phyi->phyint_illv6 = ill;
22572 		} else {
22573 			ASSERT(phyi->phyint_illv4 == NULL);
22574 			phyi->phyint_illv4 = ill;
22575 		}
22576 		/*
22577 		 * This is a new ill, currently undergoing SLIFNAME
22578 		 * So we could not have joined an IPMP group until now.
22579 		 */
22580 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22581 		    phyi_old->phyint_groupname == NULL);
22582 
22583 		/*
22584 		 * This phyi_old is going away. Decref ipsq_refs and
22585 		 * assert it is zero. The ipsq itself will be freed in
22586 		 * ipsq_exit
22587 		 */
22588 		ipsq = phyi_old->phyint_ipsq;
22589 		IPSQ_DEC_REF(ipsq, ipst);
22590 		ASSERT(ipsq->ipsq_refs == 0);
22591 		/* Get the singleton phyint out of the ipsq list */
22592 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22593 		ipsq->ipsq_phyint_list = NULL;
22594 		phyi_old->phyint_illv4 = NULL;
22595 		phyi_old->phyint_illv6 = NULL;
22596 		mi_free(phyi_old);
22597 	} else {
22598 		mutex_enter(&ill->ill_lock);
22599 		/*
22600 		 * We don't need to acquire any lock, since
22601 		 * the ill is not yet visible globally  and we
22602 		 * have not yet released the ill_g_lock.
22603 		 */
22604 		phyi = phyi_old;
22605 		mutex_enter(&phyi->phyint_lock);
22606 		/* XXX We need a recovery strategy here. */
22607 		if (!phyint_assign_ifindex(phyi, ipst))
22608 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22609 
22610 		/* No IPMP group yet, thus the hook uses the ifindex */
22611 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22612 
22613 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22614 		    (void *)phyi, where);
22615 
22616 		(void) avl_find(&ipst->ips_phyint_g_list->
22617 		    phyint_list_avl_by_index,
22618 		    &phyi->phyint_ifindex, &where);
22619 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22620 		    (void *)phyi, where);
22621 	}
22622 
22623 	/*
22624 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22625 	 * pending mp is not affected because that is per ill basis.
22626 	 */
22627 	ill->ill_phyint = phyi;
22628 
22629 	/*
22630 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22631 	 * We do this here as when the first ipif was allocated,
22632 	 * ipif_allocate does not know the right interface index.
22633 	 */
22634 
22635 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22636 	/*
22637 	 * Now that the phyint's ifindex has been assigned, complete the
22638 	 * remaining
22639 	 */
22640 
22641 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22642 	if (ill->ill_isv6) {
22643 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22644 		    ill->ill_phyint->phyint_ifindex;
22645 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22646 	} else {
22647 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22648 	}
22649 
22650 	/*
22651 	 * Generate an event within the hooks framework to indicate that
22652 	 * a new interface has just been added to IP.  For this event to
22653 	 * be generated, the network interface must, at least, have an
22654 	 * ifindex assigned to it.
22655 	 *
22656 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22657 	 * that the ordering of delivered events to listeners matches the
22658 	 * order of them in the kernel.
22659 	 *
22660 	 * This function could be called from ill_lookup_on_name. In that case
22661 	 * the interface is loopback "lo", which will not generate a NIC event.
22662 	 */
22663 	if (ill->ill_name_length <= 2 ||
22664 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22665 		/*
22666 		 * Generate nic plumb event for ill_name even if
22667 		 * ipmp_hook_emulation is set. That avoids generating events
22668 		 * for the ill_names should ipmp_hook_emulation be turned on
22669 		 * later.
22670 		 */
22671 		ill_nic_info_plumb(ill, B_FALSE);
22672 	}
22673 	RELEASE_ILL_LOCKS(ill, ill_other);
22674 	mutex_exit(&phyi->phyint_lock);
22675 }
22676 
22677 /*
22678  * Allocate a NE_PLUMB nic info event and store in the ill.
22679  * If 'group' is set we do it for the group name, otherwise the ill name.
22680  * It will be sent when we leave the ipsq.
22681  */
22682 void
22683 ill_nic_info_plumb(ill_t *ill, boolean_t group)
22684 {
22685 	phyint_t	*phyi = ill->ill_phyint;
22686 	char		*name;
22687 	int		namelen;
22688 
22689 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22690 
22691 	if (group) {
22692 		ASSERT(phyi->phyint_groupname_len != 0);
22693 		namelen = phyi->phyint_groupname_len;
22694 		name = phyi->phyint_groupname;
22695 	} else {
22696 		namelen = ill->ill_name_length;
22697 		name = ill->ill_name;
22698 	}
22699 
22700 	(void) ill_hook_event_create(ill, 0, NE_PLUMB, name, namelen);
22701 }
22702 
22703 /*
22704  * Unhook the nic event message from the ill and enqueue it
22705  * into the nic event taskq.
22706  */
22707 void
22708 ill_nic_info_dispatch(ill_t *ill)
22709 {
22710 	hook_nic_event_t *info;
22711 
22712 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22713 
22714 	if ((info = ill->ill_nic_event_info) != NULL) {
22715 		if (ddi_taskq_dispatch(eventq_queue_nic,
22716 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
22717 			ip2dbg(("ill_nic_info_dispatch: "
22718 			    "ddi_taskq_dispatch failed\n"));
22719 			if (info->hne_data != NULL)
22720 				kmem_free(info->hne_data, info->hne_datalen);
22721 			kmem_free(info, sizeof (hook_nic_event_t));
22722 		}
22723 		ill->ill_nic_event_info = NULL;
22724 	}
22725 }
22726 
22727 /*
22728  * Notify any downstream modules of the name of this interface.
22729  * An M_IOCTL is used even though we don't expect a successful reply.
22730  * Any reply message from the driver (presumably an M_IOCNAK) will
22731  * eventually get discarded somewhere upstream.  The message format is
22732  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22733  * to IP.
22734  */
22735 static void
22736 ip_ifname_notify(ill_t *ill, queue_t *q)
22737 {
22738 	mblk_t *mp1, *mp2;
22739 	struct iocblk *iocp;
22740 	struct lifreq *lifr;
22741 
22742 	mp1 = mkiocb(SIOCSLIFNAME);
22743 	if (mp1 == NULL)
22744 		return;
22745 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22746 	if (mp2 == NULL) {
22747 		freeb(mp1);
22748 		return;
22749 	}
22750 
22751 	mp1->b_cont = mp2;
22752 	iocp = (struct iocblk *)mp1->b_rptr;
22753 	iocp->ioc_count = sizeof (struct lifreq);
22754 
22755 	lifr = (struct lifreq *)mp2->b_rptr;
22756 	mp2->b_wptr += sizeof (struct lifreq);
22757 	bzero(lifr, sizeof (struct lifreq));
22758 
22759 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22760 	lifr->lifr_ppa = ill->ill_ppa;
22761 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22762 
22763 	putnext(q, mp1);
22764 }
22765 
22766 static int
22767 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22768 {
22769 	int err;
22770 	ip_stack_t	*ipst = ill->ill_ipst;
22771 
22772 	/* Set the obsolete NDD per-interface forwarding name. */
22773 	err = ill_set_ndd_name(ill);
22774 	if (err != 0) {
22775 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22776 		    err);
22777 	}
22778 
22779 	/* Tell downstream modules where they are. */
22780 	ip_ifname_notify(ill, q);
22781 
22782 	/*
22783 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22784 	 * Error cases are ENOMEM ...
22785 	 */
22786 	err = ill_dl_phys(ill, ipif, mp, q);
22787 
22788 	/*
22789 	 * If there is no IRE expiration timer running, get one started.
22790 	 * igmp and mld timers will be triggered by the first multicast
22791 	 */
22792 	if (ipst->ips_ip_ire_expire_id == 0) {
22793 		/*
22794 		 * acquire the lock and check again.
22795 		 */
22796 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
22797 		if (ipst->ips_ip_ire_expire_id == 0) {
22798 			ipst->ips_ip_ire_expire_id = timeout(
22799 			    ip_trash_timer_expire, ipst,
22800 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
22801 		}
22802 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
22803 	}
22804 
22805 	if (ill->ill_isv6) {
22806 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
22807 		if (ipst->ips_mld_slowtimeout_id == 0) {
22808 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
22809 			    (void *)ipst,
22810 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22811 		}
22812 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
22813 	} else {
22814 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
22815 		if (ipst->ips_igmp_slowtimeout_id == 0) {
22816 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
22817 			    (void *)ipst,
22818 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22819 		}
22820 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
22821 	}
22822 
22823 	return (err);
22824 }
22825 
22826 /*
22827  * Common routine for ppa and ifname setting. Should be called exclusive.
22828  *
22829  * Returns EINPROGRESS when mp has been consumed by queueing it on
22830  * ill_pending_mp and the ioctl will complete in ip_rput.
22831  *
22832  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22833  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22834  * For SLIFNAME, we pass these values back to the userland.
22835  */
22836 static int
22837 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22838 {
22839 	ill_t	*ill;
22840 	ipif_t	*ipif;
22841 	ipsq_t	*ipsq;
22842 	char	*ppa_ptr;
22843 	char	*old_ptr;
22844 	char	old_char;
22845 	int	error;
22846 	ip_stack_t	*ipst;
22847 
22848 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
22849 	ASSERT(q->q_next != NULL);
22850 	ASSERT(interf_name != NULL);
22851 
22852 	ill = (ill_t *)q->q_ptr;
22853 	ipst = ill->ill_ipst;
22854 
22855 	ASSERT(ill->ill_ipst != NULL);
22856 	ASSERT(ill->ill_name[0] == '\0');
22857 	ASSERT(IAM_WRITER_ILL(ill));
22858 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
22859 	ASSERT(ill->ill_ppa == UINT_MAX);
22860 
22861 	/* The ppa is sent down by ifconfig or is chosen */
22862 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
22863 		return (EINVAL);
22864 	}
22865 
22866 	/*
22867 	 * make sure ppa passed in is same as ppa in the name.
22868 	 * This check is not made when ppa == UINT_MAX in that case ppa
22869 	 * in the name could be anything. System will choose a ppa and
22870 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
22871 	 */
22872 	if (*new_ppa_ptr != UINT_MAX) {
22873 		/* stoi changes the pointer */
22874 		old_ptr = ppa_ptr;
22875 		/*
22876 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
22877 		 * (they don't have an externally visible ppa).  We assign one
22878 		 * here so that we can manage the interface.  Note that in
22879 		 * the past this value was always 0 for DLPI 1 drivers.
22880 		 */
22881 		if (*new_ppa_ptr == 0)
22882 			*new_ppa_ptr = stoi(&old_ptr);
22883 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
22884 			return (EINVAL);
22885 	}
22886 	/*
22887 	 * terminate string before ppa
22888 	 * save char at that location.
22889 	 */
22890 	old_char = ppa_ptr[0];
22891 	ppa_ptr[0] = '\0';
22892 
22893 	ill->ill_ppa = *new_ppa_ptr;
22894 	/*
22895 	 * Finish as much work now as possible before calling ill_glist_insert
22896 	 * which makes the ill globally visible and also merges it with the
22897 	 * other protocol instance of this phyint. The remaining work is
22898 	 * done after entering the ipsq which may happen sometime later.
22899 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
22900 	 */
22901 	ipif = ill->ill_ipif;
22902 
22903 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
22904 	ipif_assign_seqid(ipif);
22905 
22906 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
22907 		ill->ill_flags |= ILLF_IPV4;
22908 
22909 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
22910 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
22911 
22912 	if (ill->ill_flags & ILLF_IPV6) {
22913 
22914 		ill->ill_isv6 = B_TRUE;
22915 		if (ill->ill_rq != NULL) {
22916 			ill->ill_rq->q_qinfo = &iprinitv6;
22917 			ill->ill_wq->q_qinfo = &ipwinitv6;
22918 		}
22919 
22920 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
22921 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
22922 		ipif->ipif_v6src_addr = ipv6_all_zeros;
22923 		ipif->ipif_v6subnet = ipv6_all_zeros;
22924 		ipif->ipif_v6net_mask = ipv6_all_zeros;
22925 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
22926 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
22927 		/*
22928 		 * point-to-point or Non-mulicast capable
22929 		 * interfaces won't do NUD unless explicitly
22930 		 * configured to do so.
22931 		 */
22932 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
22933 		    !(ill->ill_flags & ILLF_MULTICAST)) {
22934 			ill->ill_flags |= ILLF_NONUD;
22935 		}
22936 		/* Make sure IPv4 specific flag is not set on IPv6 if */
22937 		if (ill->ill_flags & ILLF_NOARP) {
22938 			/*
22939 			 * Note: xresolv interfaces will eventually need
22940 			 * NOARP set here as well, but that will require
22941 			 * those external resolvers to have some
22942 			 * knowledge of that flag and act appropriately.
22943 			 * Not to be changed at present.
22944 			 */
22945 			ill->ill_flags &= ~ILLF_NOARP;
22946 		}
22947 		/*
22948 		 * Set the ILLF_ROUTER flag according to the global
22949 		 * IPv6 forwarding policy.
22950 		 */
22951 		if (ipst->ips_ipv6_forward != 0)
22952 			ill->ill_flags |= ILLF_ROUTER;
22953 	} else if (ill->ill_flags & ILLF_IPV4) {
22954 		ill->ill_isv6 = B_FALSE;
22955 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
22956 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
22957 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
22958 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
22959 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
22960 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
22961 		/*
22962 		 * Set the ILLF_ROUTER flag according to the global
22963 		 * IPv4 forwarding policy.
22964 		 */
22965 		if (ipst->ips_ip_g_forward != 0)
22966 			ill->ill_flags |= ILLF_ROUTER;
22967 	}
22968 
22969 	ASSERT(ill->ill_phyint != NULL);
22970 
22971 	/*
22972 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
22973 	 * be completed in ill_glist_insert -> ill_phyint_reinit
22974 	 */
22975 	if (!ill_allocate_mibs(ill))
22976 		return (ENOMEM);
22977 
22978 	/*
22979 	 * Pick a default sap until we get the DL_INFO_ACK back from
22980 	 * the driver.
22981 	 */
22982 	if (ill->ill_sap == 0) {
22983 		if (ill->ill_isv6)
22984 			ill->ill_sap  = IP6_DL_SAP;
22985 		else
22986 			ill->ill_sap  = IP_DL_SAP;
22987 	}
22988 
22989 	ill->ill_ifname_pending = 1;
22990 	ill->ill_ifname_pending_err = 0;
22991 
22992 	ill_refhold(ill);
22993 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
22994 	if ((error = ill_glist_insert(ill, interf_name,
22995 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
22996 		ill->ill_ppa = UINT_MAX;
22997 		ill->ill_name[0] = '\0';
22998 		/*
22999 		 * undo null termination done above.
23000 		 */
23001 		ppa_ptr[0] = old_char;
23002 		rw_exit(&ipst->ips_ill_g_lock);
23003 		ill_refrele(ill);
23004 		return (error);
23005 	}
23006 
23007 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
23008 
23009 	/*
23010 	 * When we return the buffer pointed to by interf_name should contain
23011 	 * the same name as in ill_name.
23012 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
23013 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
23014 	 * so copy full name and update the ppa ptr.
23015 	 * When ppa passed in != UINT_MAX all values are correct just undo
23016 	 * null termination, this saves a bcopy.
23017 	 */
23018 	if (*new_ppa_ptr == UINT_MAX) {
23019 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23020 		*new_ppa_ptr = ill->ill_ppa;
23021 	} else {
23022 		/*
23023 		 * undo null termination done above.
23024 		 */
23025 		ppa_ptr[0] = old_char;
23026 	}
23027 
23028 	/* Let SCTP know about this ILL */
23029 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23030 
23031 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23032 	    B_TRUE);
23033 
23034 	rw_exit(&ipst->ips_ill_g_lock);
23035 	ill_refrele(ill);
23036 	if (ipsq == NULL)
23037 		return (EINPROGRESS);
23038 
23039 	/*
23040 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23041 	 */
23042 	if (ipsq->ipsq_current_ipif == NULL)
23043 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23044 	else
23045 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23046 
23047 	error = ipif_set_values_tail(ill, ipif, mp, q);
23048 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
23049 	if (error != 0 && error != EINPROGRESS) {
23050 		/*
23051 		 * restore previous values
23052 		 */
23053 		ill->ill_isv6 = B_FALSE;
23054 	}
23055 	return (error);
23056 }
23057 
23058 
23059 void
23060 ipif_init(ip_stack_t *ipst)
23061 {
23062 	hrtime_t hrt;
23063 	int i;
23064 
23065 	/*
23066 	 * Can't call drv_getparm here as it is too early in the boot.
23067 	 * As we use ipif_src_random just for picking a different
23068 	 * source address everytime, this need not be really random.
23069 	 */
23070 	hrt = gethrtime();
23071 	ipst->ips_ipif_src_random =
23072 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23073 
23074 	for (i = 0; i < MAX_G_HEADS; i++) {
23075 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23076 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23077 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23078 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23079 	}
23080 
23081 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23082 	    ill_phyint_compare_index,
23083 	    sizeof (phyint_t),
23084 	    offsetof(struct phyint, phyint_avl_by_index));
23085 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23086 	    ill_phyint_compare_name,
23087 	    sizeof (phyint_t),
23088 	    offsetof(struct phyint, phyint_avl_by_name));
23089 }
23090 
23091 /*
23092  * Lookup the ipif corresponding to the onlink destination address. For
23093  * point-to-point interfaces, it matches with remote endpoint destination
23094  * address. For point-to-multipoint interfaces it only tries to match the
23095  * destination with the interface's subnet address. The longest, most specific
23096  * match is found to take care of such rare network configurations like -
23097  * le0: 129.146.1.1/16
23098  * le1: 129.146.2.2/24
23099  * It is used only by SO_DONTROUTE at the moment.
23100  */
23101 ipif_t *
23102 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23103 {
23104 	ipif_t	*ipif, *best_ipif;
23105 	ill_t	*ill;
23106 	ill_walk_context_t ctx;
23107 
23108 	ASSERT(zoneid != ALL_ZONES);
23109 	best_ipif = NULL;
23110 
23111 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23112 	ill = ILL_START_WALK_V4(&ctx, ipst);
23113 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23114 		mutex_enter(&ill->ill_lock);
23115 		for (ipif = ill->ill_ipif; ipif != NULL;
23116 		    ipif = ipif->ipif_next) {
23117 			if (!IPIF_CAN_LOOKUP(ipif))
23118 				continue;
23119 			if (ipif->ipif_zoneid != zoneid &&
23120 			    ipif->ipif_zoneid != ALL_ZONES)
23121 				continue;
23122 			/*
23123 			 * Point-to-point case. Look for exact match with
23124 			 * destination address.
23125 			 */
23126 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23127 				if (ipif->ipif_pp_dst_addr == addr) {
23128 					ipif_refhold_locked(ipif);
23129 					mutex_exit(&ill->ill_lock);
23130 					rw_exit(&ipst->ips_ill_g_lock);
23131 					if (best_ipif != NULL)
23132 						ipif_refrele(best_ipif);
23133 					return (ipif);
23134 				}
23135 			} else if (ipif->ipif_subnet == (addr &
23136 			    ipif->ipif_net_mask)) {
23137 				/*
23138 				 * Point-to-multipoint case. Looping through to
23139 				 * find the most specific match. If there are
23140 				 * multiple best match ipif's then prefer ipif's
23141 				 * that are UP. If there is only one best match
23142 				 * ipif and it is DOWN we must still return it.
23143 				 */
23144 				if ((best_ipif == NULL) ||
23145 				    (ipif->ipif_net_mask >
23146 				    best_ipif->ipif_net_mask) ||
23147 				    ((ipif->ipif_net_mask ==
23148 				    best_ipif->ipif_net_mask) &&
23149 				    ((ipif->ipif_flags & IPIF_UP) &&
23150 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23151 					ipif_refhold_locked(ipif);
23152 					mutex_exit(&ill->ill_lock);
23153 					rw_exit(&ipst->ips_ill_g_lock);
23154 					if (best_ipif != NULL)
23155 						ipif_refrele(best_ipif);
23156 					best_ipif = ipif;
23157 					rw_enter(&ipst->ips_ill_g_lock,
23158 					    RW_READER);
23159 					mutex_enter(&ill->ill_lock);
23160 				}
23161 			}
23162 		}
23163 		mutex_exit(&ill->ill_lock);
23164 	}
23165 	rw_exit(&ipst->ips_ill_g_lock);
23166 	return (best_ipif);
23167 }
23168 
23169 
23170 /*
23171  * Save enough information so that we can recreate the IRE if
23172  * the interface goes down and then up.
23173  */
23174 static void
23175 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23176 {
23177 	mblk_t	*save_mp;
23178 
23179 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23180 	if (save_mp != NULL) {
23181 		ifrt_t	*ifrt;
23182 
23183 		save_mp->b_wptr += sizeof (ifrt_t);
23184 		ifrt = (ifrt_t *)save_mp->b_rptr;
23185 		bzero(ifrt, sizeof (ifrt_t));
23186 		ifrt->ifrt_type = ire->ire_type;
23187 		ifrt->ifrt_addr = ire->ire_addr;
23188 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23189 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23190 		ifrt->ifrt_mask = ire->ire_mask;
23191 		ifrt->ifrt_flags = ire->ire_flags;
23192 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23193 		mutex_enter(&ipif->ipif_saved_ire_lock);
23194 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23195 		ipif->ipif_saved_ire_mp = save_mp;
23196 		ipif->ipif_saved_ire_cnt++;
23197 		mutex_exit(&ipif->ipif_saved_ire_lock);
23198 	}
23199 }
23200 
23201 
23202 static void
23203 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23204 {
23205 	mblk_t	**mpp;
23206 	mblk_t	*mp;
23207 	ifrt_t	*ifrt;
23208 
23209 	/* Remove from ipif_saved_ire_mp list if it is there */
23210 	mutex_enter(&ipif->ipif_saved_ire_lock);
23211 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23212 	    mpp = &(*mpp)->b_cont) {
23213 		/*
23214 		 * On a given ipif, the triple of address, gateway and
23215 		 * mask is unique for each saved IRE (in the case of
23216 		 * ordinary interface routes, the gateway address is
23217 		 * all-zeroes).
23218 		 */
23219 		mp = *mpp;
23220 		ifrt = (ifrt_t *)mp->b_rptr;
23221 		if (ifrt->ifrt_addr == ire->ire_addr &&
23222 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23223 		    ifrt->ifrt_mask == ire->ire_mask) {
23224 			*mpp = mp->b_cont;
23225 			ipif->ipif_saved_ire_cnt--;
23226 			freeb(mp);
23227 			break;
23228 		}
23229 	}
23230 	mutex_exit(&ipif->ipif_saved_ire_lock);
23231 }
23232 
23233 
23234 /*
23235  * IP multirouting broadcast routes handling
23236  * Append CGTP broadcast IREs to regular ones created
23237  * at ifconfig time.
23238  */
23239 static void
23240 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23241 {
23242 	ire_t *ire_prim;
23243 
23244 	ASSERT(ire != NULL);
23245 	ASSERT(ire_dst != NULL);
23246 
23247 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23248 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23249 	if (ire_prim != NULL) {
23250 		/*
23251 		 * We are in the special case of broadcasts for
23252 		 * CGTP. We add an IRE_BROADCAST that holds
23253 		 * the RTF_MULTIRT flag, the destination
23254 		 * address of ire_dst and the low level
23255 		 * info of ire_prim. In other words, CGTP
23256 		 * broadcast is added to the redundant ipif.
23257 		 */
23258 		ipif_t *ipif_prim;
23259 		ire_t  *bcast_ire;
23260 
23261 		ipif_prim = ire_prim->ire_ipif;
23262 
23263 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23264 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23265 		    (void *)ire_dst, (void *)ire_prim,
23266 		    (void *)ipif_prim));
23267 
23268 		bcast_ire = ire_create(
23269 		    (uchar_t *)&ire->ire_addr,
23270 		    (uchar_t *)&ip_g_all_ones,
23271 		    (uchar_t *)&ire_dst->ire_src_addr,
23272 		    (uchar_t *)&ire->ire_gateway_addr,
23273 		    &ipif_prim->ipif_mtu,
23274 		    NULL,
23275 		    ipif_prim->ipif_rq,
23276 		    ipif_prim->ipif_wq,
23277 		    IRE_BROADCAST,
23278 		    ipif_prim,
23279 		    0,
23280 		    0,
23281 		    0,
23282 		    ire->ire_flags,
23283 		    &ire_uinfo_null,
23284 		    NULL,
23285 		    NULL,
23286 		    ipst);
23287 
23288 		if (bcast_ire != NULL) {
23289 
23290 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23291 			    B_FALSE) == 0) {
23292 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23293 				    "added bcast_ire %p\n",
23294 				    (void *)bcast_ire));
23295 
23296 				ipif_save_ire(bcast_ire->ire_ipif,
23297 				    bcast_ire);
23298 				ire_refrele(bcast_ire);
23299 			}
23300 		}
23301 		ire_refrele(ire_prim);
23302 	}
23303 }
23304 
23305 
23306 /*
23307  * IP multirouting broadcast routes handling
23308  * Remove the broadcast ire
23309  */
23310 static void
23311 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23312 {
23313 	ire_t *ire_dst;
23314 
23315 	ASSERT(ire != NULL);
23316 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23317 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23318 	if (ire_dst != NULL) {
23319 		ire_t *ire_prim;
23320 
23321 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23322 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23323 		if (ire_prim != NULL) {
23324 			ipif_t *ipif_prim;
23325 			ire_t  *bcast_ire;
23326 
23327 			ipif_prim = ire_prim->ire_ipif;
23328 
23329 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23330 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23331 			    (void *)ire_dst, (void *)ire_prim,
23332 			    (void *)ipif_prim));
23333 
23334 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23335 			    ire->ire_gateway_addr,
23336 			    IRE_BROADCAST,
23337 			    ipif_prim, ALL_ZONES,
23338 			    NULL,
23339 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23340 			    MATCH_IRE_MASK, ipst);
23341 
23342 			if (bcast_ire != NULL) {
23343 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23344 				    "looked up bcast_ire %p\n",
23345 				    (void *)bcast_ire));
23346 				ipif_remove_ire(bcast_ire->ire_ipif,
23347 				    bcast_ire);
23348 				ire_delete(bcast_ire);
23349 				ire_refrele(bcast_ire);
23350 			}
23351 			ire_refrele(ire_prim);
23352 		}
23353 		ire_refrele(ire_dst);
23354 	}
23355 }
23356 
23357 /*
23358  * IPsec hardware acceleration capabilities related functions.
23359  */
23360 
23361 /*
23362  * Free a per-ill IPsec capabilities structure.
23363  */
23364 static void
23365 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23366 {
23367 	if (capab->auth_hw_algs != NULL)
23368 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23369 	if (capab->encr_hw_algs != NULL)
23370 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23371 	if (capab->encr_algparm != NULL)
23372 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23373 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23374 }
23375 
23376 /*
23377  * Allocate a new per-ill IPsec capabilities structure. This structure
23378  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23379  * an array which specifies, for each algorithm, whether this algorithm
23380  * is supported by the ill or not.
23381  */
23382 static ill_ipsec_capab_t *
23383 ill_ipsec_capab_alloc(void)
23384 {
23385 	ill_ipsec_capab_t *capab;
23386 	uint_t nelems;
23387 
23388 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23389 	if (capab == NULL)
23390 		return (NULL);
23391 
23392 	/* we need one bit per algorithm */
23393 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23394 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23395 
23396 	/* allocate memory to store algorithm flags */
23397 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23398 	if (capab->encr_hw_algs == NULL)
23399 		goto nomem;
23400 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23401 	if (capab->auth_hw_algs == NULL)
23402 		goto nomem;
23403 	/*
23404 	 * Leave encr_algparm NULL for now since we won't need it half
23405 	 * the time
23406 	 */
23407 	return (capab);
23408 
23409 nomem:
23410 	ill_ipsec_capab_free(capab);
23411 	return (NULL);
23412 }
23413 
23414 /*
23415  * Resize capability array.  Since we're exclusive, this is OK.
23416  */
23417 static boolean_t
23418 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23419 {
23420 	ipsec_capab_algparm_t *nalp, *oalp;
23421 	uint32_t olen, nlen;
23422 
23423 	oalp = capab->encr_algparm;
23424 	olen = capab->encr_algparm_size;
23425 
23426 	if (oalp != NULL) {
23427 		if (algid < capab->encr_algparm_end)
23428 			return (B_TRUE);
23429 	}
23430 
23431 	nlen = (algid + 1) * sizeof (*nalp);
23432 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23433 	if (nalp == NULL)
23434 		return (B_FALSE);
23435 
23436 	if (oalp != NULL) {
23437 		bcopy(oalp, nalp, olen);
23438 		kmem_free(oalp, olen);
23439 	}
23440 	capab->encr_algparm = nalp;
23441 	capab->encr_algparm_size = nlen;
23442 	capab->encr_algparm_end = algid + 1;
23443 
23444 	return (B_TRUE);
23445 }
23446 
23447 /*
23448  * Compare the capabilities of the specified ill with the protocol
23449  * and algorithms specified by the SA passed as argument.
23450  * If they match, returns B_TRUE, B_FALSE if they do not match.
23451  *
23452  * The ill can be passed as a pointer to it, or by specifying its index
23453  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23454  *
23455  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23456  * packet is eligible for hardware acceleration, and by
23457  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23458  * to a particular ill.
23459  */
23460 boolean_t
23461 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23462     ipsa_t *sa, netstack_t *ns)
23463 {
23464 	boolean_t sa_isv6;
23465 	uint_t algid;
23466 	struct ill_ipsec_capab_s *cpp;
23467 	boolean_t need_refrele = B_FALSE;
23468 	ip_stack_t	*ipst = ns->netstack_ip;
23469 
23470 	if (ill == NULL) {
23471 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23472 		    NULL, NULL, NULL, ipst);
23473 		if (ill == NULL) {
23474 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23475 			return (B_FALSE);
23476 		}
23477 		need_refrele = B_TRUE;
23478 	}
23479 
23480 	/*
23481 	 * Use the address length specified by the SA to determine
23482 	 * if it corresponds to a IPv6 address, and fail the matching
23483 	 * if the isv6 flag passed as argument does not match.
23484 	 * Note: this check is used for SADB capability checking before
23485 	 * sending SA information to an ill.
23486 	 */
23487 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23488 	if (sa_isv6 != ill_isv6)
23489 		/* protocol mismatch */
23490 		goto done;
23491 
23492 	/*
23493 	 * Check if the ill supports the protocol, algorithm(s) and
23494 	 * key size(s) specified by the SA, and get the pointers to
23495 	 * the algorithms supported by the ill.
23496 	 */
23497 	switch (sa->ipsa_type) {
23498 
23499 	case SADB_SATYPE_ESP:
23500 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23501 			/* ill does not support ESP acceleration */
23502 			goto done;
23503 		cpp = ill->ill_ipsec_capab_esp;
23504 		algid = sa->ipsa_auth_alg;
23505 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23506 			goto done;
23507 		algid = sa->ipsa_encr_alg;
23508 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23509 			goto done;
23510 		if (algid < cpp->encr_algparm_end) {
23511 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23512 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23513 				goto done;
23514 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23515 				goto done;
23516 		}
23517 		break;
23518 
23519 	case SADB_SATYPE_AH:
23520 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23521 			/* ill does not support AH acceleration */
23522 			goto done;
23523 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23524 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23525 			goto done;
23526 		break;
23527 	}
23528 
23529 	if (need_refrele)
23530 		ill_refrele(ill);
23531 	return (B_TRUE);
23532 done:
23533 	if (need_refrele)
23534 		ill_refrele(ill);
23535 	return (B_FALSE);
23536 }
23537 
23538 
23539 /*
23540  * Add a new ill to the list of IPsec capable ills.
23541  * Called from ill_capability_ipsec_ack() when an ACK was received
23542  * indicating that IPsec hardware processing was enabled for an ill.
23543  *
23544  * ill must point to the ill for which acceleration was enabled.
23545  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23546  */
23547 static void
23548 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23549 {
23550 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23551 	uint_t sa_type;
23552 	uint_t ipproto;
23553 	ip_stack_t	*ipst = ill->ill_ipst;
23554 
23555 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23556 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23557 
23558 	switch (dl_cap) {
23559 	case DL_CAPAB_IPSEC_AH:
23560 		sa_type = SADB_SATYPE_AH;
23561 		ills = &ipst->ips_ipsec_capab_ills_ah;
23562 		ipproto = IPPROTO_AH;
23563 		break;
23564 	case DL_CAPAB_IPSEC_ESP:
23565 		sa_type = SADB_SATYPE_ESP;
23566 		ills = &ipst->ips_ipsec_capab_ills_esp;
23567 		ipproto = IPPROTO_ESP;
23568 		break;
23569 	}
23570 
23571 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23572 
23573 	/*
23574 	 * Add ill index to list of hardware accelerators. If
23575 	 * already in list, do nothing.
23576 	 */
23577 	for (cur_ill = *ills; cur_ill != NULL &&
23578 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23579 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23580 		;
23581 
23582 	if (cur_ill == NULL) {
23583 		/* if this is a new entry for this ill */
23584 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23585 		if (new_ill == NULL) {
23586 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23587 			return;
23588 		}
23589 
23590 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23591 		new_ill->ill_isv6 = ill->ill_isv6;
23592 		new_ill->next = *ills;
23593 		*ills = new_ill;
23594 	} else if (!sadb_resync) {
23595 		/* not resync'ing SADB and an entry exists for this ill */
23596 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23597 		return;
23598 	}
23599 
23600 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23601 
23602 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23603 		/*
23604 		 * IPsec module for protocol loaded, initiate dump
23605 		 * of the SADB to this ill.
23606 		 */
23607 		sadb_ill_download(ill, sa_type);
23608 }
23609 
23610 /*
23611  * Remove an ill from the list of IPsec capable ills.
23612  */
23613 static void
23614 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23615 {
23616 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23617 	ip_stack_t	*ipst = ill->ill_ipst;
23618 
23619 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23620 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23621 
23622 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
23623 	    &ipst->ips_ipsec_capab_ills_esp;
23624 
23625 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23626 
23627 	prev_ill = NULL;
23628 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23629 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23630 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23631 		;
23632 	if (cur_ill == NULL) {
23633 		/* entry not found */
23634 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23635 		return;
23636 	}
23637 	if (prev_ill == NULL) {
23638 		/* entry at front of list */
23639 		*ills = NULL;
23640 	} else {
23641 		prev_ill->next = cur_ill->next;
23642 	}
23643 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23644 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23645 }
23646 
23647 /*
23648  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23649  * supporting the specified IPsec protocol acceleration.
23650  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23651  * We free the mblk and, if sa is non-null, release the held referece.
23652  */
23653 void
23654 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
23655     netstack_t *ns)
23656 {
23657 	ipsec_capab_ill_t *ici, *cur_ici;
23658 	ill_t *ill;
23659 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23660 	ip_stack_t	*ipst = ns->netstack_ip;
23661 
23662 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
23663 	    ipst->ips_ipsec_capab_ills_esp;
23664 
23665 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
23666 
23667 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23668 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23669 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
23670 
23671 		/*
23672 		 * Handle the case where the ill goes away while the SADB is
23673 		 * attempting to send messages.  If it's going away, it's
23674 		 * nuking its shadow SADB, so we don't care..
23675 		 */
23676 
23677 		if (ill == NULL)
23678 			continue;
23679 
23680 		if (sa != NULL) {
23681 			/*
23682 			 * Make sure capabilities match before
23683 			 * sending SA to ill.
23684 			 */
23685 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23686 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
23687 				ill_refrele(ill);
23688 				continue;
23689 			}
23690 
23691 			mutex_enter(&sa->ipsa_lock);
23692 			sa->ipsa_flags |= IPSA_F_HW;
23693 			mutex_exit(&sa->ipsa_lock);
23694 		}
23695 
23696 		/*
23697 		 * Copy template message, and add it to the front
23698 		 * of the mblk ship list. We want to avoid holding
23699 		 * the ipsec_capab_ills_lock while sending the
23700 		 * message to the ills.
23701 		 *
23702 		 * The b_next and b_prev are temporarily used
23703 		 * to build a list of mblks to be sent down, and to
23704 		 * save the ill to which they must be sent.
23705 		 */
23706 		nmp = copymsg(mp);
23707 		if (nmp == NULL) {
23708 			ill_refrele(ill);
23709 			continue;
23710 		}
23711 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23712 		nmp->b_next = mp_ship_list;
23713 		mp_ship_list = nmp;
23714 		nmp->b_prev = (mblk_t *)ill;
23715 	}
23716 
23717 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23718 
23719 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
23720 		/* restore the mblk to a sane state */
23721 		next_mp = nmp->b_next;
23722 		nmp->b_next = NULL;
23723 		ill = (ill_t *)nmp->b_prev;
23724 		nmp->b_prev = NULL;
23725 
23726 		ill_dlpi_send(ill, nmp);
23727 		ill_refrele(ill);
23728 	}
23729 
23730 	if (sa != NULL)
23731 		IPSA_REFRELE(sa);
23732 	freemsg(mp);
23733 }
23734 
23735 /*
23736  * Derive an interface id from the link layer address.
23737  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23738  */
23739 static boolean_t
23740 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23741 {
23742 	char		*addr;
23743 
23744 	if (phys_length != ETHERADDRL)
23745 		return (B_FALSE);
23746 
23747 	/* Form EUI-64 like address */
23748 	addr = (char *)&v6addr->s6_addr32[2];
23749 	bcopy((char *)phys_addr, addr, 3);
23750 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23751 	addr[3] = (char)0xff;
23752 	addr[4] = (char)0xfe;
23753 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23754 	return (B_TRUE);
23755 }
23756 
23757 /* ARGSUSED */
23758 static boolean_t
23759 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23760 {
23761 	return (B_FALSE);
23762 }
23763 
23764 /* ARGSUSED */
23765 static boolean_t
23766 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23767     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23768 {
23769 	/*
23770 	 * Multicast address mappings used over Ethernet/802.X.
23771 	 * This address is used as a base for mappings.
23772 	 */
23773 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23774 	    0x00, 0x00, 0x00};
23775 
23776 	/*
23777 	 * Extract low order 32 bits from IPv6 multicast address.
23778 	 * Or that into the link layer address, starting from the
23779 	 * second byte.
23780 	 */
23781 	*hw_start = 2;
23782 	v6_extract_mask->s6_addr32[0] = 0;
23783 	v6_extract_mask->s6_addr32[1] = 0;
23784 	v6_extract_mask->s6_addr32[2] = 0;
23785 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23786 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23787 	return (B_TRUE);
23788 }
23789 
23790 /*
23791  * Indicate by return value whether multicast is supported. If not,
23792  * this code should not touch/change any parameters.
23793  */
23794 /* ARGSUSED */
23795 static boolean_t
23796 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23797     uint32_t *hw_start, ipaddr_t *extract_mask)
23798 {
23799 	/*
23800 	 * Multicast address mappings used over Ethernet/802.X.
23801 	 * This address is used as a base for mappings.
23802 	 */
23803 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23804 	    0x00, 0x00, 0x00 };
23805 
23806 	if (phys_length != ETHERADDRL)
23807 		return (B_FALSE);
23808 
23809 	*extract_mask = htonl(0x007fffff);
23810 	*hw_start = 2;
23811 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23812 	return (B_TRUE);
23813 }
23814 
23815 /*
23816  * Derive IPoIB interface id from the link layer address.
23817  */
23818 static boolean_t
23819 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23820 {
23821 	char		*addr;
23822 
23823 	if (phys_length != 20)
23824 		return (B_FALSE);
23825 	addr = (char *)&v6addr->s6_addr32[2];
23826 	bcopy(phys_addr + 12, addr, 8);
23827 	/*
23828 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23829 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23830 	 * rules. In these cases, the IBA considers these GUIDs to be in
23831 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23832 	 * required; vendors are required not to assign global EUI-64's
23833 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23834 	 * of the interface identifier. Whether the GUID is in modified
23835 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23836 	 * bit set to 1.
23837 	 */
23838 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23839 	return (B_TRUE);
23840 }
23841 
23842 /*
23843  * Note on mapping from multicast IP addresses to IPoIB multicast link
23844  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
23845  * The format of an IPoIB multicast address is:
23846  *
23847  *  4 byte QPN      Scope Sign.  Pkey
23848  * +--------------------------------------------+
23849  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
23850  * +--------------------------------------------+
23851  *
23852  * The Scope and Pkey components are properties of the IBA port and
23853  * network interface. They can be ascertained from the broadcast address.
23854  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
23855  */
23856 
23857 static boolean_t
23858 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23859     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23860 {
23861 	/*
23862 	 * Base IPoIB IPv6 multicast address used for mappings.
23863 	 * Does not contain the IBA scope/Pkey values.
23864 	 */
23865 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23866 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
23867 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23868 
23869 	/*
23870 	 * Extract low order 80 bits from IPv6 multicast address.
23871 	 * Or that into the link layer address, starting from the
23872 	 * sixth byte.
23873 	 */
23874 	*hw_start = 6;
23875 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
23876 
23877 	/*
23878 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23879 	 */
23880 	*(maddr + 5) = *(bphys_addr + 5);
23881 	*(maddr + 8) = *(bphys_addr + 8);
23882 	*(maddr + 9) = *(bphys_addr + 9);
23883 
23884 	v6_extract_mask->s6_addr32[0] = 0;
23885 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
23886 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
23887 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23888 	return (B_TRUE);
23889 }
23890 
23891 static boolean_t
23892 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23893     uint32_t *hw_start, ipaddr_t *extract_mask)
23894 {
23895 	/*
23896 	 * Base IPoIB IPv4 multicast address used for mappings.
23897 	 * Does not contain the IBA scope/Pkey values.
23898 	 */
23899 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23900 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
23901 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23902 
23903 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
23904 		return (B_FALSE);
23905 
23906 	/*
23907 	 * Extract low order 28 bits from IPv4 multicast address.
23908 	 * Or that into the link layer address, starting from the
23909 	 * sixteenth byte.
23910 	 */
23911 	*extract_mask = htonl(0x0fffffff);
23912 	*hw_start = 16;
23913 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
23914 
23915 	/*
23916 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23917 	 */
23918 	*(maddr + 5) = *(bphys_addr + 5);
23919 	*(maddr + 8) = *(bphys_addr + 8);
23920 	*(maddr + 9) = *(bphys_addr + 9);
23921 	return (B_TRUE);
23922 }
23923 
23924 /*
23925  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
23926  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
23927  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
23928  * the link-local address is preferred.
23929  */
23930 boolean_t
23931 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23932 {
23933 	ipif_t	*ipif;
23934 	ipif_t	*maybe_ipif = NULL;
23935 
23936 	mutex_enter(&ill->ill_lock);
23937 	if (ill->ill_state_flags & ILL_CONDEMNED) {
23938 		mutex_exit(&ill->ill_lock);
23939 		if (ipifp != NULL)
23940 			*ipifp = NULL;
23941 		return (B_FALSE);
23942 	}
23943 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23944 		if (!IPIF_CAN_LOOKUP(ipif))
23945 			continue;
23946 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
23947 		    ipif->ipif_zoneid != ALL_ZONES)
23948 			continue;
23949 		if ((ipif->ipif_flags & flags) != flags)
23950 			continue;
23951 
23952 		if (ipifp == NULL) {
23953 			mutex_exit(&ill->ill_lock);
23954 			ASSERT(maybe_ipif == NULL);
23955 			return (B_TRUE);
23956 		}
23957 		if (!ill->ill_isv6 ||
23958 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
23959 			ipif_refhold_locked(ipif);
23960 			mutex_exit(&ill->ill_lock);
23961 			*ipifp = ipif;
23962 			return (B_TRUE);
23963 		}
23964 		if (maybe_ipif == NULL)
23965 			maybe_ipif = ipif;
23966 	}
23967 	if (ipifp != NULL) {
23968 		if (maybe_ipif != NULL)
23969 			ipif_refhold_locked(maybe_ipif);
23970 		*ipifp = maybe_ipif;
23971 	}
23972 	mutex_exit(&ill->ill_lock);
23973 	return (maybe_ipif != NULL);
23974 }
23975 
23976 /*
23977  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
23978  */
23979 boolean_t
23980 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23981 {
23982 	ill_t *illg;
23983 	ip_stack_t	*ipst = ill->ill_ipst;
23984 
23985 	/*
23986 	 * We look at the passed-in ill first without grabbing ill_g_lock.
23987 	 */
23988 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
23989 		return (B_TRUE);
23990 	}
23991 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23992 	if (ill->ill_group == NULL) {
23993 		/* ill not in a group */
23994 		rw_exit(&ipst->ips_ill_g_lock);
23995 		return (B_FALSE);
23996 	}
23997 
23998 	/*
23999 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24000 	 * group. We need to look for an ipif in the zone on all the ills in the
24001 	 * group.
24002 	 */
24003 	illg = ill->ill_group->illgrp_ill;
24004 	do {
24005 		/*
24006 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24007 		 * that it's not there.
24008 		 */
24009 		if (illg != ill &&
24010 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24011 			break;
24012 		}
24013 	} while ((illg = illg->ill_group_next) != NULL);
24014 	rw_exit(&ipst->ips_ill_g_lock);
24015 	return (illg != NULL);
24016 }
24017 
24018 /*
24019  * Check if this ill is only being used to send ICMP probes for IPMP
24020  */
24021 boolean_t
24022 ill_is_probeonly(ill_t *ill)
24023 {
24024 	/*
24025 	 * Check if the interface is FAILED, or INACTIVE
24026 	 */
24027 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24028 		return (B_TRUE);
24029 
24030 	return (B_FALSE);
24031 }
24032 
24033 /*
24034  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24035  * If a pointer to an ipif_t is returned then the caller will need to do
24036  * an ill_refrele().
24037  *
24038  * If there is no real interface which matches the ifindex, then it looks
24039  * for a group that has a matching index. In the case of a group match the
24040  * lifidx must be zero. We don't need emulate the logical interfaces
24041  * since IP Filter's use of netinfo doesn't use that.
24042  */
24043 ipif_t *
24044 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24045     ip_stack_t *ipst)
24046 {
24047 	ipif_t *ipif;
24048 	ill_t *ill;
24049 
24050 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24051 	    ipst);
24052 
24053 	if (ill == NULL) {
24054 		/* Fallback to group names only if hook_emulation set */
24055 		if (!ipst->ips_ipmp_hook_emulation)
24056 			return (NULL);
24057 
24058 		if (lifidx != 0)
24059 			return (NULL);
24060 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24061 		if (ill == NULL)
24062 			return (NULL);
24063 	}
24064 
24065 	mutex_enter(&ill->ill_lock);
24066 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24067 		mutex_exit(&ill->ill_lock);
24068 		ill_refrele(ill);
24069 		return (NULL);
24070 	}
24071 
24072 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24073 		if (!IPIF_CAN_LOOKUP(ipif))
24074 			continue;
24075 		if (lifidx == ipif->ipif_id) {
24076 			ipif_refhold_locked(ipif);
24077 			break;
24078 		}
24079 	}
24080 
24081 	mutex_exit(&ill->ill_lock);
24082 	ill_refrele(ill);
24083 	return (ipif);
24084 }
24085 
24086 /*
24087  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24088  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24089  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24090  * for details.
24091  */
24092 void
24093 ill_fastpath_flush(ill_t *ill)
24094 {
24095 	ip_stack_t *ipst = ill->ill_ipst;
24096 
24097 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24098 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24099 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24100 }
24101 
24102 /*
24103  * Set the physical address information for `ill' to the contents of the
24104  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24105  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24106  * EINPROGRESS will be returned.
24107  */
24108 int
24109 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24110 {
24111 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24112 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24113 
24114 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24115 
24116 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24117 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24118 		/* Changing DL_IPV6_TOKEN is not yet supported */
24119 		return (0);
24120 	}
24121 
24122 	/*
24123 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24124 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24125 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24126 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24127 	 */
24128 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24129 		freemsg(mp);
24130 		return (ENOMEM);
24131 	}
24132 
24133 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24134 
24135 	/*
24136 	 * If we can quiesce the ill, then set the address.  If not, then
24137 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24138 	 */
24139 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24140 	mutex_enter(&ill->ill_lock);
24141 	if (!ill_is_quiescent(ill)) {
24142 		/* call cannot fail since `conn_t *' argument is NULL */
24143 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24144 		    mp, ILL_DOWN);
24145 		mutex_exit(&ill->ill_lock);
24146 		return (EINPROGRESS);
24147 	}
24148 	mutex_exit(&ill->ill_lock);
24149 
24150 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24151 	return (0);
24152 }
24153 
24154 /*
24155  * Once the ill associated with `q' has quiesced, set its physical address
24156  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24157  * are passed (linked by b_cont), since we sometimes need to save two distinct
24158  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24159  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24160  * is quiesced, we know any stale IREs with the old address information have
24161  * already been removed, so we don't need to call ill_fastpath_flush().
24162  */
24163 /* ARGSUSED */
24164 static void
24165 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24166 {
24167 	ill_t		*ill = q->q_ptr;
24168 	mblk_t		*addrmp2 = unlinkb(addrmp);
24169 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24170 	uint_t		addrlen, addroff;
24171 
24172 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24173 
24174 	addroff	= dlindp->dl_addr_offset;
24175 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24176 
24177 	switch (dlindp->dl_data) {
24178 	case DL_IPV6_LINK_LAYER_ADDR:
24179 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24180 		freemsg(addrmp2);
24181 		break;
24182 
24183 	case DL_CURR_PHYS_ADDR:
24184 		freemsg(ill->ill_phys_addr_mp);
24185 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24186 		ill->ill_phys_addr_mp = addrmp;
24187 		ill->ill_phys_addr_length = addrlen;
24188 
24189 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24190 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24191 		else
24192 			freemsg(addrmp2);
24193 		break;
24194 	default:
24195 		ASSERT(0);
24196 	}
24197 
24198 	/*
24199 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24200 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24201 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24202 	 * brought up.
24203 	 */
24204 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24205 		ipsq_current_finish(ipsq);
24206 }
24207 
24208 /*
24209  * Helper routine for setting the ill_nd_lla fields.
24210  */
24211 void
24212 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24213 {
24214 	freemsg(ill->ill_nd_lla_mp);
24215 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24216 	ill->ill_nd_lla_mp = ndmp;
24217 	ill->ill_nd_lla_len = addrlen;
24218 }
24219 
24220 major_t IP_MAJ;
24221 #define	IP	"ip"
24222 
24223 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24224 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24225 
24226 /*
24227  * Issue REMOVEIF ioctls to have the loopback interfaces
24228  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24229  * the former going away when the user-level processes in the zone
24230  * are killed  * and the latter are cleaned up by the stream head
24231  * str_stack_shutdown callback that undoes all I_PLINKs.
24232  */
24233 void
24234 ip_loopback_cleanup(ip_stack_t *ipst)
24235 {
24236 	int error;
24237 	ldi_handle_t	lh = NULL;
24238 	ldi_ident_t	li = NULL;
24239 	int		rval;
24240 	cred_t		*cr;
24241 	struct strioctl iocb;
24242 	struct lifreq	lifreq;
24243 
24244 	IP_MAJ = ddi_name_to_major(IP);
24245 
24246 #ifdef NS_DEBUG
24247 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24248 	    ipst->ips_netstack->netstack_stackid);
24249 #endif
24250 
24251 	bzero(&lifreq, sizeof (lifreq));
24252 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24253 
24254 	error = ldi_ident_from_major(IP_MAJ, &li);
24255 	if (error) {
24256 #ifdef DEBUG
24257 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24258 		    error);
24259 #endif
24260 		return;
24261 	}
24262 
24263 	cr = zone_get_kcred(netstackid_to_zoneid(
24264 	    ipst->ips_netstack->netstack_stackid));
24265 	ASSERT(cr != NULL);
24266 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24267 	if (error) {
24268 #ifdef DEBUG
24269 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24270 		    error);
24271 #endif
24272 		goto out;
24273 	}
24274 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24275 	iocb.ic_timout = 15;
24276 	iocb.ic_len = sizeof (lifreq);
24277 	iocb.ic_dp = (char *)&lifreq;
24278 
24279 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24280 	/* LINTED - statement has no consequent */
24281 	if (error) {
24282 #ifdef NS_DEBUG
24283 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24284 		    "UDP6 error %d\n", error);
24285 #endif
24286 	}
24287 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24288 	lh = NULL;
24289 
24290 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24291 	if (error) {
24292 #ifdef NS_DEBUG
24293 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24294 		    error);
24295 #endif
24296 		goto out;
24297 	}
24298 
24299 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24300 	iocb.ic_timout = 15;
24301 	iocb.ic_len = sizeof (lifreq);
24302 	iocb.ic_dp = (char *)&lifreq;
24303 
24304 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24305 	/* LINTED - statement has no consequent */
24306 	if (error) {
24307 #ifdef NS_DEBUG
24308 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24309 		    "UDP error %d\n", error);
24310 #endif
24311 	}
24312 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24313 	lh = NULL;
24314 
24315 out:
24316 	/* Close layered handles */
24317 	if (lh)
24318 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24319 	if (li)
24320 		ldi_ident_release(li);
24321 
24322 	crfree(cr);
24323 }
24324 
24325 /*
24326  * This needs to be in-sync with nic_event_t definition
24327  */
24328 static const char *
24329 ill_hook_event2str(nic_event_t event)
24330 {
24331 	switch (event) {
24332 	case NE_PLUMB:
24333 		return ("PLUMB");
24334 	case NE_UNPLUMB:
24335 		return ("UNPLUMB");
24336 	case NE_UP:
24337 		return ("UP");
24338 	case NE_DOWN:
24339 		return ("DOWN");
24340 	case NE_ADDRESS_CHANGE:
24341 		return ("ADDRESS_CHANGE");
24342 	default:
24343 		return ("UNKNOWN");
24344 	}
24345 }
24346 
24347 static void
24348 ill_hook_event_destroy(ill_t *ill)
24349 {
24350 	hook_nic_event_t	*info;
24351 
24352 	if ((info = ill->ill_nic_event_info) != NULL) {
24353 		if (info->hne_data != NULL)
24354 			kmem_free(info->hne_data, info->hne_datalen);
24355 		kmem_free(info, sizeof (hook_nic_event_t));
24356 
24357 		ill->ill_nic_event_info = NULL;
24358 	}
24359 
24360 }
24361 
24362 boolean_t
24363 ill_hook_event_create(ill_t *ill, lif_if_t lif, nic_event_t event,
24364     nic_event_data_t data, size_t datalen)
24365 {
24366 	ip_stack_t		*ipst = ill->ill_ipst;
24367 	hook_nic_event_t	*info;
24368 	const char		*str = NULL;
24369 
24370 	/* destroy nic event info if it exists */
24371 	if ((info = ill->ill_nic_event_info) != NULL) {
24372 		str = ill_hook_event2str(info->hne_event);
24373 		ip2dbg(("ill_hook_event_create: unexpected nic event %s "
24374 		    "attached for %s\n", str, ill->ill_name));
24375 		ill_hook_event_destroy(ill);
24376 	}
24377 
24378 	/* create a new nic event info */
24379 	if ((info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP)) == NULL)
24380 		goto fail;
24381 
24382 	ill->ill_nic_event_info = info;
24383 
24384 	if (event == NE_UNPLUMB)
24385 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
24386 	else
24387 		info->hne_nic = ill->ill_phyint->phyint_hook_ifindex;
24388 	info->hne_lif = lif;
24389 	info->hne_event = event;
24390 	info->hne_family = ill->ill_isv6 ?
24391 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
24392 	info->hne_data = NULL;
24393 	info->hne_datalen = 0;
24394 
24395 	if (data != NULL && datalen != 0) {
24396 		info->hne_data = kmem_alloc(datalen, KM_NOSLEEP);
24397 		if (info->hne_data != NULL) {
24398 			bcopy(data, info->hne_data, datalen);
24399 			info->hne_datalen = datalen;
24400 		} else {
24401 			ill_hook_event_destroy(ill);
24402 			goto fail;
24403 		}
24404 	}
24405 
24406 	return (B_TRUE);
24407 fail:
24408 	str = ill_hook_event2str(event);
24409 	ip2dbg(("ill_hook_event_create: could not attach %s nic event "
24410 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
24411 	return (B_FALSE);
24412 }
24413